Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jeff Garzik | af36d7f | 2005-08-28 20:18:39 -0400 | [diff] [blame] | 2 | * libata-core.c - helper library for ATA |
| 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-2004 Red Hat, Inc. All rights reserved. |
| 9 | * Copyright 2003-2004 Jeff Garzik |
| 10 | * |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or modify |
| 13 | * it under the terms of the GNU General Public License as published by |
| 14 | * the Free Software Foundation; either version 2, or (at your option) |
| 15 | * any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; see the file COPYING. If not, write to |
| 24 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
| 25 | * |
| 26 | * |
| 27 | * libata documentation is available via 'make {ps|pdf}docs', |
| 28 | * as Documentation/DocBook/libata.* |
| 29 | * |
| 30 | * Hardware documentation available from http://www.t13.org/ and |
| 31 | * http://www.sata-io.org/ |
| 32 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | */ |
| 34 | |
| 35 | #include <linux/config.h> |
| 36 | #include <linux/kernel.h> |
| 37 | #include <linux/module.h> |
| 38 | #include <linux/pci.h> |
| 39 | #include <linux/init.h> |
| 40 | #include <linux/list.h> |
| 41 | #include <linux/mm.h> |
| 42 | #include <linux/highmem.h> |
| 43 | #include <linux/spinlock.h> |
| 44 | #include <linux/blkdev.h> |
| 45 | #include <linux/delay.h> |
| 46 | #include <linux/timer.h> |
| 47 | #include <linux/interrupt.h> |
| 48 | #include <linux/completion.h> |
| 49 | #include <linux/suspend.h> |
| 50 | #include <linux/workqueue.h> |
Jeff Garzik | 67846b3 | 2005-10-05 02:58:32 -0400 | [diff] [blame] | 51 | #include <linux/jiffies.h> |
David Hardeman | 378f058 | 2005-09-17 17:55:31 +1000 | [diff] [blame] | 52 | #include <linux/scatterlist.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | #include <scsi/scsi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | #include "scsi_priv.h" |
Jeff Garzik | 193515d | 2005-11-07 00:59:37 -0500 | [diff] [blame] | 55 | #include <scsi/scsi_cmnd.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #include <scsi/scsi_host.h> |
| 57 | #include <linux/libata.h> |
| 58 | #include <asm/io.h> |
| 59 | #include <asm/semaphore.h> |
| 60 | #include <asm/byteorder.h> |
| 61 | |
| 62 | #include "libata.h" |
| 63 | |
Tejun Heo | 6aff8f1 | 2006-02-15 18:24:09 +0900 | [diff] [blame] | 64 | static unsigned int ata_dev_init_params(struct ata_port *ap, |
Albert Lee | 00b6f5e | 2006-03-27 16:39:18 +0800 | [diff] [blame] | 65 | struct ata_device *dev, |
| 66 | u16 heads, |
| 67 | u16 sectors); |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 68 | static unsigned int ata_dev_set_xfermode(struct ata_port *ap, |
| 69 | struct ata_device *dev); |
Tejun Heo | acf356b | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 70 | static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | |
| 72 | static unsigned int ata_unique_id = 1; |
| 73 | static struct workqueue_struct *ata_wq; |
| 74 | |
Jeff Garzik | 418dc1f | 2006-03-11 20:50:08 -0500 | [diff] [blame] | 75 | int atapi_enabled = 1; |
Jeff Garzik | 1623c81 | 2005-08-30 03:37:42 -0400 | [diff] [blame] | 76 | module_param(atapi_enabled, int, 0444); |
| 77 | MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)"); |
| 78 | |
Jeff Garzik | c3c013a | 2006-02-27 22:31:19 -0500 | [diff] [blame] | 79 | int libata_fua = 0; |
| 80 | module_param_named(fua, libata_fua, int, 0444); |
| 81 | MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)"); |
| 82 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | MODULE_AUTHOR("Jeff Garzik"); |
| 84 | MODULE_DESCRIPTION("Library module for ATA devices"); |
| 85 | MODULE_LICENSE("GPL"); |
| 86 | MODULE_VERSION(DRV_VERSION); |
| 87 | |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 88 | |
| 89 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure |
| 91 | * @tf: Taskfile to convert |
| 92 | * @fis: Buffer into which data will output |
| 93 | * @pmp: Port multiplier port |
| 94 | * |
| 95 | * Converts a standard ATA taskfile to a Serial ATA |
| 96 | * FIS structure (Register - Host to Device). |
| 97 | * |
| 98 | * LOCKING: |
| 99 | * Inherited from caller. |
| 100 | */ |
| 101 | |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 102 | void ata_tf_to_fis(const struct ata_taskfile *tf, u8 *fis, u8 pmp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
| 104 | fis[0] = 0x27; /* Register - Host to Device FIS */ |
| 105 | fis[1] = (pmp & 0xf) | (1 << 7); /* Port multiplier number, |
| 106 | bit 7 indicates Command FIS */ |
| 107 | fis[2] = tf->command; |
| 108 | fis[3] = tf->feature; |
| 109 | |
| 110 | fis[4] = tf->lbal; |
| 111 | fis[5] = tf->lbam; |
| 112 | fis[6] = tf->lbah; |
| 113 | fis[7] = tf->device; |
| 114 | |
| 115 | fis[8] = tf->hob_lbal; |
| 116 | fis[9] = tf->hob_lbam; |
| 117 | fis[10] = tf->hob_lbah; |
| 118 | fis[11] = tf->hob_feature; |
| 119 | |
| 120 | fis[12] = tf->nsect; |
| 121 | fis[13] = tf->hob_nsect; |
| 122 | fis[14] = 0; |
| 123 | fis[15] = tf->ctl; |
| 124 | |
| 125 | fis[16] = 0; |
| 126 | fis[17] = 0; |
| 127 | fis[18] = 0; |
| 128 | fis[19] = 0; |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * ata_tf_from_fis - Convert SATA FIS to ATA taskfile |
| 133 | * @fis: Buffer from which data will be input |
| 134 | * @tf: Taskfile to output |
| 135 | * |
Mark Lord | e12a1be | 2005-11-12 18:55:45 -0500 | [diff] [blame] | 136 | * Converts a serial ATA FIS structure to a standard ATA taskfile. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 137 | * |
| 138 | * LOCKING: |
| 139 | * Inherited from caller. |
| 140 | */ |
| 141 | |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 142 | void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | { |
| 144 | tf->command = fis[2]; /* status */ |
| 145 | tf->feature = fis[3]; /* error */ |
| 146 | |
| 147 | tf->lbal = fis[4]; |
| 148 | tf->lbam = fis[5]; |
| 149 | tf->lbah = fis[6]; |
| 150 | tf->device = fis[7]; |
| 151 | |
| 152 | tf->hob_lbal = fis[8]; |
| 153 | tf->hob_lbam = fis[9]; |
| 154 | tf->hob_lbah = fis[10]; |
| 155 | |
| 156 | tf->nsect = fis[12]; |
| 157 | tf->hob_nsect = fis[13]; |
| 158 | } |
| 159 | |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 160 | static const u8 ata_rw_cmds[] = { |
| 161 | /* pio multi */ |
| 162 | ATA_CMD_READ_MULTI, |
| 163 | ATA_CMD_WRITE_MULTI, |
| 164 | ATA_CMD_READ_MULTI_EXT, |
| 165 | ATA_CMD_WRITE_MULTI_EXT, |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 166 | 0, |
| 167 | 0, |
| 168 | 0, |
| 169 | ATA_CMD_WRITE_MULTI_FUA_EXT, |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 170 | /* pio */ |
| 171 | ATA_CMD_PIO_READ, |
| 172 | ATA_CMD_PIO_WRITE, |
| 173 | ATA_CMD_PIO_READ_EXT, |
| 174 | ATA_CMD_PIO_WRITE_EXT, |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 175 | 0, |
| 176 | 0, |
| 177 | 0, |
| 178 | 0, |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 179 | /* dma */ |
| 180 | ATA_CMD_READ, |
| 181 | ATA_CMD_WRITE, |
| 182 | ATA_CMD_READ_EXT, |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 183 | ATA_CMD_WRITE_EXT, |
| 184 | 0, |
| 185 | 0, |
| 186 | 0, |
| 187 | ATA_CMD_WRITE_FUA_EXT |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 188 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | |
| 190 | /** |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 191 | * ata_rwcmd_protocol - set taskfile r/w commands and protocol |
| 192 | * @qc: command to examine and configure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | * |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 194 | * Examine the device configuration and tf->flags to calculate |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 195 | * the proper read/write commands and protocol to use. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | * |
| 197 | * LOCKING: |
| 198 | * caller. |
| 199 | */ |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 200 | int ata_rwcmd_protocol(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | { |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 202 | struct ata_taskfile *tf = &qc->tf; |
| 203 | struct ata_device *dev = qc->dev; |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 204 | u8 cmd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 206 | int index, fua, lba48, write; |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 207 | |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 208 | fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0; |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 209 | lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0; |
| 210 | write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 212 | if (dev->flags & ATA_DFLAG_PIO) { |
| 213 | tf->protocol = ATA_PROT_PIO; |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 214 | index = dev->multi_count ? 0 : 8; |
Alan Cox | 8d238e0 | 2006-01-17 20:50:31 +0000 | [diff] [blame] | 215 | } else if (lba48 && (qc->ap->flags & ATA_FLAG_PIO_LBA48)) { |
| 216 | /* Unable to use DMA due to host limitation */ |
| 217 | tf->protocol = ATA_PROT_PIO; |
Albert Lee | 0565c26 | 2006-02-13 18:55:25 +0800 | [diff] [blame] | 218 | index = dev->multi_count ? 0 : 8; |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 219 | } else { |
| 220 | tf->protocol = ATA_PROT_DMA; |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 221 | index = 16; |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 222 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 224 | cmd = ata_rw_cmds[index + fua + lba48 + write]; |
| 225 | if (cmd) { |
| 226 | tf->command = cmd; |
| 227 | return 0; |
| 228 | } |
| 229 | return -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 232 | /** |
| 233 | * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask |
| 234 | * @pio_mask: pio_mask |
| 235 | * @mwdma_mask: mwdma_mask |
| 236 | * @udma_mask: udma_mask |
| 237 | * |
| 238 | * Pack @pio_mask, @mwdma_mask and @udma_mask into a single |
| 239 | * unsigned int xfer_mask. |
| 240 | * |
| 241 | * LOCKING: |
| 242 | * None. |
| 243 | * |
| 244 | * RETURNS: |
| 245 | * Packed xfer_mask. |
| 246 | */ |
| 247 | static unsigned int ata_pack_xfermask(unsigned int pio_mask, |
| 248 | unsigned int mwdma_mask, |
| 249 | unsigned int udma_mask) |
| 250 | { |
| 251 | return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) | |
| 252 | ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) | |
| 253 | ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA); |
| 254 | } |
| 255 | |
Tejun Heo | c0489e4 | 2006-03-24 14:07:49 +0900 | [diff] [blame] | 256 | /** |
| 257 | * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks |
| 258 | * @xfer_mask: xfer_mask to unpack |
| 259 | * @pio_mask: resulting pio_mask |
| 260 | * @mwdma_mask: resulting mwdma_mask |
| 261 | * @udma_mask: resulting udma_mask |
| 262 | * |
| 263 | * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask. |
| 264 | * Any NULL distination masks will be ignored. |
| 265 | */ |
| 266 | static void ata_unpack_xfermask(unsigned int xfer_mask, |
| 267 | unsigned int *pio_mask, |
| 268 | unsigned int *mwdma_mask, |
| 269 | unsigned int *udma_mask) |
| 270 | { |
| 271 | if (pio_mask) |
| 272 | *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO; |
| 273 | if (mwdma_mask) |
| 274 | *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA; |
| 275 | if (udma_mask) |
| 276 | *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA; |
| 277 | } |
| 278 | |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 279 | static const struct ata_xfer_ent { |
Tejun Heo | be9a50c | 2006-03-31 22:48:52 +0900 | [diff] [blame] | 280 | int shift, bits; |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 281 | u8 base; |
| 282 | } ata_xfer_tbl[] = { |
| 283 | { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 }, |
| 284 | { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 }, |
| 285 | { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 }, |
| 286 | { -1, }, |
| 287 | }; |
| 288 | |
| 289 | /** |
| 290 | * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask |
| 291 | * @xfer_mask: xfer_mask of interest |
| 292 | * |
| 293 | * Return matching XFER_* value for @xfer_mask. Only the highest |
| 294 | * bit of @xfer_mask is considered. |
| 295 | * |
| 296 | * LOCKING: |
| 297 | * None. |
| 298 | * |
| 299 | * RETURNS: |
| 300 | * Matching XFER_* value, 0 if no match found. |
| 301 | */ |
| 302 | static u8 ata_xfer_mask2mode(unsigned int xfer_mask) |
| 303 | { |
| 304 | int highbit = fls(xfer_mask) - 1; |
| 305 | const struct ata_xfer_ent *ent; |
| 306 | |
| 307 | for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) |
| 308 | if (highbit >= ent->shift && highbit < ent->shift + ent->bits) |
| 309 | return ent->base + highbit - ent->shift; |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * ata_xfer_mode2mask - Find matching xfer_mask for XFER_* |
| 315 | * @xfer_mode: XFER_* of interest |
| 316 | * |
| 317 | * Return matching xfer_mask for @xfer_mode. |
| 318 | * |
| 319 | * LOCKING: |
| 320 | * None. |
| 321 | * |
| 322 | * RETURNS: |
| 323 | * Matching xfer_mask, 0 if no match found. |
| 324 | */ |
| 325 | static unsigned int ata_xfer_mode2mask(u8 xfer_mode) |
| 326 | { |
| 327 | const struct ata_xfer_ent *ent; |
| 328 | |
| 329 | for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) |
| 330 | if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits) |
| 331 | return 1 << (ent->shift + xfer_mode - ent->base); |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * ata_xfer_mode2shift - Find matching xfer_shift for XFER_* |
| 337 | * @xfer_mode: XFER_* of interest |
| 338 | * |
| 339 | * Return matching xfer_shift for @xfer_mode. |
| 340 | * |
| 341 | * LOCKING: |
| 342 | * None. |
| 343 | * |
| 344 | * RETURNS: |
| 345 | * Matching xfer_shift, -1 if no match found. |
| 346 | */ |
| 347 | static int ata_xfer_mode2shift(unsigned int xfer_mode) |
| 348 | { |
| 349 | const struct ata_xfer_ent *ent; |
| 350 | |
| 351 | for (ent = ata_xfer_tbl; ent->shift >= 0; ent++) |
| 352 | if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits) |
| 353 | return ent->shift; |
| 354 | return -1; |
| 355 | } |
| 356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | /** |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 358 | * ata_mode_string - convert xfer_mask to string |
| 359 | * @xfer_mask: mask of bits supported; only highest bit counts. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | * |
| 361 | * Determine string which represents the highest speed |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 362 | * (highest bit in @modemask). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | * |
| 364 | * LOCKING: |
| 365 | * None. |
| 366 | * |
| 367 | * RETURNS: |
| 368 | * Constant C string representing highest speed listed in |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 369 | * @mode_mask, or the constant C string "<n/a>". |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | */ |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 371 | static const char *ata_mode_string(unsigned int xfer_mask) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | { |
Tejun Heo | 75f554b | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 373 | static const char * const xfer_mode_str[] = { |
| 374 | "PIO0", |
| 375 | "PIO1", |
| 376 | "PIO2", |
| 377 | "PIO3", |
| 378 | "PIO4", |
| 379 | "MWDMA0", |
| 380 | "MWDMA1", |
| 381 | "MWDMA2", |
| 382 | "UDMA/16", |
| 383 | "UDMA/25", |
| 384 | "UDMA/33", |
| 385 | "UDMA/44", |
| 386 | "UDMA/66", |
| 387 | "UDMA/100", |
| 388 | "UDMA/133", |
| 389 | "UDMA7", |
| 390 | }; |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 391 | int highbit; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | |
Tejun Heo | 1da7b0d | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 393 | highbit = fls(xfer_mask) - 1; |
| 394 | if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str)) |
| 395 | return xfer_mode_str[highbit]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | return "<n/a>"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Tejun Heo | 4c360c8 | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 399 | static const char *sata_spd_string(unsigned int spd) |
| 400 | { |
| 401 | static const char * const spd_str[] = { |
| 402 | "1.5 Gbps", |
| 403 | "3.0 Gbps", |
| 404 | }; |
| 405 | |
| 406 | if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str)) |
| 407 | return "<unknown>"; |
| 408 | return spd_str[spd - 1]; |
| 409 | } |
| 410 | |
Tejun Heo | 1ad8e7f | 2006-04-02 18:51:53 +0900 | [diff] [blame^] | 411 | void ata_dev_disable(struct ata_port *ap, struct ata_device *dev) |
Tejun Heo | 0b8efb0 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 412 | { |
Tejun Heo | e1211e3 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 413 | if (ata_dev_enabled(dev)) { |
Tejun Heo | 0b8efb0 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 414 | printk(KERN_WARNING "ata%u: dev %u disabled\n", |
| 415 | ap->id, dev->devno); |
| 416 | dev->class++; |
| 417 | } |
| 418 | } |
| 419 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | /** |
| 421 | * ata_pio_devchk - PATA device presence detection |
| 422 | * @ap: ATA channel to examine |
| 423 | * @device: Device to examine (starting at zero) |
| 424 | * |
| 425 | * This technique was originally described in |
| 426 | * Hale Landis's ATADRVR (www.ata-atapi.com), and |
| 427 | * later found its way into the ATA/ATAPI spec. |
| 428 | * |
| 429 | * Write a pattern to the ATA shadow registers, |
| 430 | * and if a device is present, it will respond by |
| 431 | * correctly storing and echoing back the |
| 432 | * ATA shadow register contents. |
| 433 | * |
| 434 | * LOCKING: |
| 435 | * caller. |
| 436 | */ |
| 437 | |
| 438 | static unsigned int ata_pio_devchk(struct ata_port *ap, |
| 439 | unsigned int device) |
| 440 | { |
| 441 | struct ata_ioports *ioaddr = &ap->ioaddr; |
| 442 | u8 nsect, lbal; |
| 443 | |
| 444 | ap->ops->dev_select(ap, device); |
| 445 | |
| 446 | outb(0x55, ioaddr->nsect_addr); |
| 447 | outb(0xaa, ioaddr->lbal_addr); |
| 448 | |
| 449 | outb(0xaa, ioaddr->nsect_addr); |
| 450 | outb(0x55, ioaddr->lbal_addr); |
| 451 | |
| 452 | outb(0x55, ioaddr->nsect_addr); |
| 453 | outb(0xaa, ioaddr->lbal_addr); |
| 454 | |
| 455 | nsect = inb(ioaddr->nsect_addr); |
| 456 | lbal = inb(ioaddr->lbal_addr); |
| 457 | |
| 458 | if ((nsect == 0x55) && (lbal == 0xaa)) |
| 459 | return 1; /* we found a device */ |
| 460 | |
| 461 | return 0; /* nothing found */ |
| 462 | } |
| 463 | |
| 464 | /** |
| 465 | * ata_mmio_devchk - PATA device presence detection |
| 466 | * @ap: ATA channel to examine |
| 467 | * @device: Device to examine (starting at zero) |
| 468 | * |
| 469 | * This technique was originally described in |
| 470 | * Hale Landis's ATADRVR (www.ata-atapi.com), and |
| 471 | * later found its way into the ATA/ATAPI spec. |
| 472 | * |
| 473 | * Write a pattern to the ATA shadow registers, |
| 474 | * and if a device is present, it will respond by |
| 475 | * correctly storing and echoing back the |
| 476 | * ATA shadow register contents. |
| 477 | * |
| 478 | * LOCKING: |
| 479 | * caller. |
| 480 | */ |
| 481 | |
| 482 | static unsigned int ata_mmio_devchk(struct ata_port *ap, |
| 483 | unsigned int device) |
| 484 | { |
| 485 | struct ata_ioports *ioaddr = &ap->ioaddr; |
| 486 | u8 nsect, lbal; |
| 487 | |
| 488 | ap->ops->dev_select(ap, device); |
| 489 | |
| 490 | writeb(0x55, (void __iomem *) ioaddr->nsect_addr); |
| 491 | writeb(0xaa, (void __iomem *) ioaddr->lbal_addr); |
| 492 | |
| 493 | writeb(0xaa, (void __iomem *) ioaddr->nsect_addr); |
| 494 | writeb(0x55, (void __iomem *) ioaddr->lbal_addr); |
| 495 | |
| 496 | writeb(0x55, (void __iomem *) ioaddr->nsect_addr); |
| 497 | writeb(0xaa, (void __iomem *) ioaddr->lbal_addr); |
| 498 | |
| 499 | nsect = readb((void __iomem *) ioaddr->nsect_addr); |
| 500 | lbal = readb((void __iomem *) ioaddr->lbal_addr); |
| 501 | |
| 502 | if ((nsect == 0x55) && (lbal == 0xaa)) |
| 503 | return 1; /* we found a device */ |
| 504 | |
| 505 | return 0; /* nothing found */ |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * ata_devchk - PATA device presence detection |
| 510 | * @ap: ATA channel to examine |
| 511 | * @device: Device to examine (starting at zero) |
| 512 | * |
| 513 | * Dispatch ATA device presence detection, depending |
| 514 | * on whether we are using PIO or MMIO to talk to the |
| 515 | * ATA shadow registers. |
| 516 | * |
| 517 | * LOCKING: |
| 518 | * caller. |
| 519 | */ |
| 520 | |
| 521 | static unsigned int ata_devchk(struct ata_port *ap, |
| 522 | unsigned int device) |
| 523 | { |
| 524 | if (ap->flags & ATA_FLAG_MMIO) |
| 525 | return ata_mmio_devchk(ap, device); |
| 526 | return ata_pio_devchk(ap, device); |
| 527 | } |
| 528 | |
| 529 | /** |
| 530 | * ata_dev_classify - determine device type based on ATA-spec signature |
| 531 | * @tf: ATA taskfile register set for device to be identified |
| 532 | * |
| 533 | * Determine from taskfile register contents whether a device is |
| 534 | * ATA or ATAPI, as per "Signature and persistence" section |
| 535 | * of ATA/PI spec (volume 1, sect 5.14). |
| 536 | * |
| 537 | * LOCKING: |
| 538 | * None. |
| 539 | * |
| 540 | * RETURNS: |
| 541 | * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN |
| 542 | * the event of failure. |
| 543 | */ |
| 544 | |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 545 | unsigned int ata_dev_classify(const struct ata_taskfile *tf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | { |
| 547 | /* Apple's open source Darwin code hints that some devices only |
| 548 | * put a proper signature into the LBA mid/high registers, |
| 549 | * So, we only check those. It's sufficient for uniqueness. |
| 550 | */ |
| 551 | |
| 552 | if (((tf->lbam == 0) && (tf->lbah == 0)) || |
| 553 | ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) { |
| 554 | DPRINTK("found ATA device by sig\n"); |
| 555 | return ATA_DEV_ATA; |
| 556 | } |
| 557 | |
| 558 | if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) || |
| 559 | ((tf->lbam == 0x69) && (tf->lbah == 0x96))) { |
| 560 | DPRINTK("found ATAPI device by sig\n"); |
| 561 | return ATA_DEV_ATAPI; |
| 562 | } |
| 563 | |
| 564 | DPRINTK("unknown device\n"); |
| 565 | return ATA_DEV_UNKNOWN; |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * ata_dev_try_classify - Parse returned ATA device signature |
| 570 | * @ap: ATA channel to examine |
| 571 | * @device: Device to examine (starting at zero) |
Tejun Heo | b4dc762 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 572 | * @r_err: Value of error register on completion |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | * |
| 574 | * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs, |
| 575 | * an ATA/ATAPI-defined set of values is placed in the ATA |
| 576 | * shadow registers, indicating the results of device detection |
| 577 | * and diagnostics. |
| 578 | * |
| 579 | * Select the ATA device, and read the values from the ATA shadow |
| 580 | * registers. Then parse according to the Error register value, |
| 581 | * and the spec-defined values examined by ata_dev_classify(). |
| 582 | * |
| 583 | * LOCKING: |
| 584 | * caller. |
Tejun Heo | b4dc762 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 585 | * |
| 586 | * RETURNS: |
| 587 | * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 588 | */ |
| 589 | |
Tejun Heo | b4dc762 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 590 | static unsigned int |
| 591 | ata_dev_try_classify(struct ata_port *ap, unsigned int device, u8 *r_err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | struct ata_taskfile tf; |
| 594 | unsigned int class; |
| 595 | u8 err; |
| 596 | |
| 597 | ap->ops->dev_select(ap, device); |
| 598 | |
| 599 | memset(&tf, 0, sizeof(tf)); |
| 600 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 601 | ap->ops->tf_read(ap, &tf); |
Jeff Garzik | 0169e28 | 2005-10-29 21:25:10 -0400 | [diff] [blame] | 602 | err = tf.feature; |
Tejun Heo | b4dc762 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 603 | if (r_err) |
| 604 | *r_err = err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | |
| 606 | /* see if device passed diags */ |
| 607 | if (err == 1) |
| 608 | /* do nothing */ ; |
| 609 | else if ((device == 0) && (err == 0x81)) |
| 610 | /* do nothing */ ; |
| 611 | else |
Tejun Heo | b4dc762 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 612 | return ATA_DEV_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
Tejun Heo | b4dc762 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 614 | /* determine if device is ATA or ATAPI */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | class = ata_dev_classify(&tf); |
Tejun Heo | b4dc762 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | if (class == ATA_DEV_UNKNOWN) |
Tejun Heo | b4dc762 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 618 | return ATA_DEV_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0)) |
Tejun Heo | b4dc762 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 620 | return ATA_DEV_NONE; |
| 621 | return class; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | } |
| 623 | |
| 624 | /** |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 625 | * ata_id_string - Convert IDENTIFY DEVICE page into string |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | * @id: IDENTIFY DEVICE results we will examine |
| 627 | * @s: string into which data is output |
| 628 | * @ofs: offset into identify device page |
| 629 | * @len: length of string to return. must be an even number. |
| 630 | * |
| 631 | * The strings in the IDENTIFY DEVICE page are broken up into |
| 632 | * 16-bit chunks. Run through the string, and output each |
| 633 | * 8-bit chunk linearly, regardless of platform. |
| 634 | * |
| 635 | * LOCKING: |
| 636 | * caller. |
| 637 | */ |
| 638 | |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 639 | void ata_id_string(const u16 *id, unsigned char *s, |
| 640 | unsigned int ofs, unsigned int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | { |
| 642 | unsigned int c; |
| 643 | |
| 644 | while (len > 0) { |
| 645 | c = id[ofs] >> 8; |
| 646 | *s = c; |
| 647 | s++; |
| 648 | |
| 649 | c = id[ofs] & 0xff; |
| 650 | *s = c; |
| 651 | s++; |
| 652 | |
| 653 | ofs++; |
| 654 | len -= 2; |
| 655 | } |
| 656 | } |
| 657 | |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 658 | /** |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 659 | * ata_id_c_string - Convert IDENTIFY DEVICE page into C string |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 660 | * @id: IDENTIFY DEVICE results we will examine |
| 661 | * @s: string into which data is output |
| 662 | * @ofs: offset into identify device page |
| 663 | * @len: length of string to return. must be an odd number. |
| 664 | * |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 665 | * This function is identical to ata_id_string except that it |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 666 | * trims trailing spaces and terminates the resulting string with |
| 667 | * null. @len must be actual maximum length (even number) + 1. |
| 668 | * |
| 669 | * LOCKING: |
| 670 | * caller. |
| 671 | */ |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 672 | void ata_id_c_string(const u16 *id, unsigned char *s, |
| 673 | unsigned int ofs, unsigned int len) |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 674 | { |
| 675 | unsigned char *p; |
| 676 | |
| 677 | WARN_ON(!(len & 1)); |
| 678 | |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 679 | ata_id_string(id, s, ofs, len - 1); |
Tejun Heo | 0e949ff | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 680 | |
| 681 | p = s + strnlen(s, len - 1); |
| 682 | while (p > s && p[-1] == ' ') |
| 683 | p--; |
| 684 | *p = '\0'; |
| 685 | } |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 686 | |
Tejun Heo | 2940740 | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 687 | static u64 ata_id_n_sectors(const u16 *id) |
| 688 | { |
| 689 | if (ata_id_has_lba(id)) { |
| 690 | if (ata_id_has_lba48(id)) |
| 691 | return ata_id_u64(id, 100); |
| 692 | else |
| 693 | return ata_id_u32(id, 60); |
| 694 | } else { |
| 695 | if (ata_id_current_chs_valid(id)) |
| 696 | return ata_id_u32(id, 57); |
| 697 | else |
| 698 | return id[1] * id[3] * id[6]; |
| 699 | } |
| 700 | } |
| 701 | |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 702 | /** |
| 703 | * ata_noop_dev_select - Select device 0/1 on ATA bus |
| 704 | * @ap: ATA channel to manipulate |
| 705 | * @device: ATA device (numbered from zero) to select |
| 706 | * |
| 707 | * This function performs no actual function. |
| 708 | * |
| 709 | * May be used as the dev_select() entry in ata_port_operations. |
| 710 | * |
| 711 | * LOCKING: |
| 712 | * caller. |
| 713 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 714 | void ata_noop_dev_select (struct ata_port *ap, unsigned int device) |
| 715 | { |
| 716 | } |
| 717 | |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 718 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | /** |
| 720 | * ata_std_dev_select - Select device 0/1 on ATA bus |
| 721 | * @ap: ATA channel to manipulate |
| 722 | * @device: ATA device (numbered from zero) to select |
| 723 | * |
| 724 | * Use the method defined in the ATA specification to |
| 725 | * make either device 0, or device 1, active on the |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 726 | * ATA channel. Works with both PIO and MMIO. |
| 727 | * |
| 728 | * May be used as the dev_select() entry in ata_port_operations. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | * |
| 730 | * LOCKING: |
| 731 | * caller. |
| 732 | */ |
| 733 | |
| 734 | void ata_std_dev_select (struct ata_port *ap, unsigned int device) |
| 735 | { |
| 736 | u8 tmp; |
| 737 | |
| 738 | if (device == 0) |
| 739 | tmp = ATA_DEVICE_OBS; |
| 740 | else |
| 741 | tmp = ATA_DEVICE_OBS | ATA_DEV1; |
| 742 | |
| 743 | if (ap->flags & ATA_FLAG_MMIO) { |
| 744 | writeb(tmp, (void __iomem *) ap->ioaddr.device_addr); |
| 745 | } else { |
| 746 | outb(tmp, ap->ioaddr.device_addr); |
| 747 | } |
| 748 | ata_pause(ap); /* needed; also flushes, for mmio */ |
| 749 | } |
| 750 | |
| 751 | /** |
| 752 | * ata_dev_select - Select device 0/1 on ATA bus |
| 753 | * @ap: ATA channel to manipulate |
| 754 | * @device: ATA device (numbered from zero) to select |
| 755 | * @wait: non-zero to wait for Status register BSY bit to clear |
| 756 | * @can_sleep: non-zero if context allows sleeping |
| 757 | * |
| 758 | * Use the method defined in the ATA specification to |
| 759 | * make either device 0, or device 1, active on the |
| 760 | * ATA channel. |
| 761 | * |
| 762 | * This is a high-level version of ata_std_dev_select(), |
| 763 | * which additionally provides the services of inserting |
| 764 | * the proper pauses and status polling, where needed. |
| 765 | * |
| 766 | * LOCKING: |
| 767 | * caller. |
| 768 | */ |
| 769 | |
| 770 | void ata_dev_select(struct ata_port *ap, unsigned int device, |
| 771 | unsigned int wait, unsigned int can_sleep) |
| 772 | { |
| 773 | VPRINTK("ENTER, ata%u: device %u, wait %u\n", |
| 774 | ap->id, device, wait); |
| 775 | |
| 776 | if (wait) |
| 777 | ata_wait_idle(ap); |
| 778 | |
| 779 | ap->ops->dev_select(ap, device); |
| 780 | |
| 781 | if (wait) { |
| 782 | if (can_sleep && ap->device[device].class == ATA_DEV_ATAPI) |
| 783 | msleep(150); |
| 784 | ata_wait_idle(ap); |
| 785 | } |
| 786 | } |
| 787 | |
| 788 | /** |
| 789 | * ata_dump_id - IDENTIFY DEVICE info debugging output |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 790 | * @id: IDENTIFY DEVICE page to dump |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 791 | * |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 792 | * Dump selected 16-bit words from the given IDENTIFY DEVICE |
| 793 | * page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 794 | * |
| 795 | * LOCKING: |
| 796 | * caller. |
| 797 | */ |
| 798 | |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 799 | static inline void ata_dump_id(const u16 *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | { |
| 801 | DPRINTK("49==0x%04x " |
| 802 | "53==0x%04x " |
| 803 | "63==0x%04x " |
| 804 | "64==0x%04x " |
| 805 | "75==0x%04x \n", |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 806 | id[49], |
| 807 | id[53], |
| 808 | id[63], |
| 809 | id[64], |
| 810 | id[75]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 811 | DPRINTK("80==0x%04x " |
| 812 | "81==0x%04x " |
| 813 | "82==0x%04x " |
| 814 | "83==0x%04x " |
| 815 | "84==0x%04x \n", |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 816 | id[80], |
| 817 | id[81], |
| 818 | id[82], |
| 819 | id[83], |
| 820 | id[84]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | DPRINTK("88==0x%04x " |
| 822 | "93==0x%04x\n", |
Tejun Heo | 0bd3300 | 2006-02-12 22:47:05 +0900 | [diff] [blame] | 823 | id[88], |
| 824 | id[93]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | } |
| 826 | |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 827 | /** |
| 828 | * ata_id_xfermask - Compute xfermask from the given IDENTIFY data |
| 829 | * @id: IDENTIFY data to compute xfer mask from |
| 830 | * |
| 831 | * Compute the xfermask for this device. This is not as trivial |
| 832 | * as it seems if we must consider early devices correctly. |
| 833 | * |
| 834 | * FIXME: pre IDE drive timing (do we care ?). |
| 835 | * |
| 836 | * LOCKING: |
| 837 | * None. |
| 838 | * |
| 839 | * RETURNS: |
| 840 | * Computed xfermask |
| 841 | */ |
| 842 | static unsigned int ata_id_xfermask(const u16 *id) |
| 843 | { |
| 844 | unsigned int pio_mask, mwdma_mask, udma_mask; |
| 845 | |
| 846 | /* Usual case. Word 53 indicates word 64 is valid */ |
| 847 | if (id[ATA_ID_FIELD_VALID] & (1 << 1)) { |
| 848 | pio_mask = id[ATA_ID_PIO_MODES] & 0x03; |
| 849 | pio_mask <<= 3; |
| 850 | pio_mask |= 0x7; |
| 851 | } else { |
| 852 | /* If word 64 isn't valid then Word 51 high byte holds |
| 853 | * the PIO timing number for the maximum. Turn it into |
| 854 | * a mask. |
| 855 | */ |
| 856 | pio_mask = (2 << (id[ATA_ID_OLD_PIO_MODES] & 0xFF)) - 1 ; |
| 857 | |
| 858 | /* But wait.. there's more. Design your standards by |
| 859 | * committee and you too can get a free iordy field to |
| 860 | * process. However its the speeds not the modes that |
| 861 | * are supported... Note drivers using the timing API |
| 862 | * will get this right anyway |
| 863 | */ |
| 864 | } |
| 865 | |
| 866 | mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07; |
Tejun Heo | fb21f0d | 2006-03-12 12:34:35 +0900 | [diff] [blame] | 867 | |
| 868 | udma_mask = 0; |
| 869 | if (id[ATA_ID_FIELD_VALID] & (1 << 2)) |
| 870 | udma_mask = id[ATA_ID_UDMA_MODES] & 0xff; |
Tejun Heo | cb95d56 | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 871 | |
| 872 | return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask); |
| 873 | } |
| 874 | |
Tejun Heo | 86e45b6 | 2006-03-05 15:29:09 +0900 | [diff] [blame] | 875 | /** |
| 876 | * ata_port_queue_task - Queue port_task |
| 877 | * @ap: The ata_port to queue port_task for |
| 878 | * |
| 879 | * Schedule @fn(@data) for execution after @delay jiffies using |
| 880 | * port_task. There is one port_task per port and it's the |
| 881 | * user(low level driver)'s responsibility to make sure that only |
| 882 | * one task is active at any given time. |
| 883 | * |
| 884 | * libata core layer takes care of synchronization between |
| 885 | * port_task and EH. ata_port_queue_task() may be ignored for EH |
| 886 | * synchronization. |
| 887 | * |
| 888 | * LOCKING: |
| 889 | * Inherited from caller. |
| 890 | */ |
| 891 | void ata_port_queue_task(struct ata_port *ap, void (*fn)(void *), void *data, |
| 892 | unsigned long delay) |
| 893 | { |
| 894 | int rc; |
| 895 | |
Tejun Heo | 2e755f6 | 2006-03-05 15:29:09 +0900 | [diff] [blame] | 896 | if (ap->flags & ATA_FLAG_FLUSH_PORT_TASK) |
Tejun Heo | 86e45b6 | 2006-03-05 15:29:09 +0900 | [diff] [blame] | 897 | return; |
| 898 | |
| 899 | PREPARE_WORK(&ap->port_task, fn, data); |
| 900 | |
| 901 | if (!delay) |
| 902 | rc = queue_work(ata_wq, &ap->port_task); |
| 903 | else |
| 904 | rc = queue_delayed_work(ata_wq, &ap->port_task, delay); |
| 905 | |
| 906 | /* rc == 0 means that another user is using port task */ |
| 907 | WARN_ON(rc == 0); |
| 908 | } |
| 909 | |
| 910 | /** |
| 911 | * ata_port_flush_task - Flush port_task |
| 912 | * @ap: The ata_port to flush port_task for |
| 913 | * |
| 914 | * After this function completes, port_task is guranteed not to |
| 915 | * be running or scheduled. |
| 916 | * |
| 917 | * LOCKING: |
| 918 | * Kernel thread context (may sleep) |
| 919 | */ |
| 920 | void ata_port_flush_task(struct ata_port *ap) |
| 921 | { |
| 922 | unsigned long flags; |
| 923 | |
| 924 | DPRINTK("ENTER\n"); |
| 925 | |
| 926 | spin_lock_irqsave(&ap->host_set->lock, flags); |
Tejun Heo | 2e755f6 | 2006-03-05 15:29:09 +0900 | [diff] [blame] | 927 | ap->flags |= ATA_FLAG_FLUSH_PORT_TASK; |
Tejun Heo | 86e45b6 | 2006-03-05 15:29:09 +0900 | [diff] [blame] | 928 | spin_unlock_irqrestore(&ap->host_set->lock, flags); |
| 929 | |
| 930 | DPRINTK("flush #1\n"); |
| 931 | flush_workqueue(ata_wq); |
| 932 | |
| 933 | /* |
| 934 | * At this point, if a task is running, it's guaranteed to see |
| 935 | * the FLUSH flag; thus, it will never queue pio tasks again. |
| 936 | * Cancel and flush. |
| 937 | */ |
| 938 | if (!cancel_delayed_work(&ap->port_task)) { |
| 939 | DPRINTK("flush #2\n"); |
| 940 | flush_workqueue(ata_wq); |
| 941 | } |
| 942 | |
| 943 | spin_lock_irqsave(&ap->host_set->lock, flags); |
Tejun Heo | 2e755f6 | 2006-03-05 15:29:09 +0900 | [diff] [blame] | 944 | ap->flags &= ~ATA_FLAG_FLUSH_PORT_TASK; |
Tejun Heo | 86e45b6 | 2006-03-05 15:29:09 +0900 | [diff] [blame] | 945 | spin_unlock_irqrestore(&ap->host_set->lock, flags); |
| 946 | |
| 947 | DPRINTK("EXIT\n"); |
| 948 | } |
| 949 | |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 950 | void ata_qc_complete_internal(struct ata_queued_cmd *qc) |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 951 | { |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 952 | struct completion *waiting = qc->private_data; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 953 | |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 954 | qc->ap->ops->tf_read(qc->ap, &qc->tf); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 955 | complete(waiting); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | /** |
| 959 | * ata_exec_internal - execute libata internal command |
| 960 | * @ap: Port to which the command is sent |
| 961 | * @dev: Device to which the command is sent |
| 962 | * @tf: Taskfile registers for the command and the result |
Tejun Heo | d69cf37 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 963 | * @cdb: CDB for packet command |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 964 | * @dma_dir: Data tranfer direction of the command |
| 965 | * @buf: Data buffer of the command |
| 966 | * @buflen: Length of data buffer |
| 967 | * |
| 968 | * Executes libata internal command with timeout. @tf contains |
| 969 | * command on entry and result on return. Timeout and error |
| 970 | * conditions are reported via return value. No recovery action |
| 971 | * is taken after a command times out. It's caller's duty to |
| 972 | * clean up after timeout. |
| 973 | * |
| 974 | * LOCKING: |
| 975 | * None. Should be called with kernel context, might sleep. |
| 976 | */ |
| 977 | |
Tejun Heo | 1ad8e7f | 2006-04-02 18:51:53 +0900 | [diff] [blame^] | 978 | unsigned ata_exec_internal(struct ata_port *ap, struct ata_device *dev, |
| 979 | struct ata_taskfile *tf, const u8 *cdb, |
| 980 | int dma_dir, void *buf, unsigned int buflen) |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 981 | { |
| 982 | u8 command = tf->command; |
| 983 | struct ata_queued_cmd *qc; |
| 984 | DECLARE_COMPLETION(wait); |
| 985 | unsigned long flags; |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 986 | unsigned int err_mask; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 987 | |
| 988 | spin_lock_irqsave(&ap->host_set->lock, flags); |
| 989 | |
| 990 | qc = ata_qc_new_init(ap, dev); |
| 991 | BUG_ON(qc == NULL); |
| 992 | |
| 993 | qc->tf = *tf; |
Tejun Heo | d69cf37 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 994 | if (cdb) |
| 995 | memcpy(qc->cdb, cdb, ATAPI_CDB_LEN); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 996 | qc->dma_dir = dma_dir; |
| 997 | if (dma_dir != DMA_NONE) { |
| 998 | ata_sg_init_one(qc, buf, buflen); |
| 999 | qc->nsect = buflen / ATA_SECT_SIZE; |
| 1000 | } |
| 1001 | |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1002 | qc->private_data = &wait; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1003 | qc->complete_fn = ata_qc_complete_internal; |
| 1004 | |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 1005 | ata_qc_issue(qc); |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1006 | |
| 1007 | spin_unlock_irqrestore(&ap->host_set->lock, flags); |
| 1008 | |
| 1009 | if (!wait_for_completion_timeout(&wait, ATA_TMOUT_INTERNAL)) { |
Albert Lee | 41ade50 | 2006-03-14 11:19:04 +0800 | [diff] [blame] | 1010 | ata_port_flush_task(ap); |
| 1011 | |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1012 | spin_lock_irqsave(&ap->host_set->lock, flags); |
| 1013 | |
| 1014 | /* We're racing with irq here. If we lose, the |
| 1015 | * following test prevents us from completing the qc |
| 1016 | * again. If completion irq occurs after here but |
| 1017 | * before the caller cleans up, it will result in a |
| 1018 | * spurious interrupt. We can live with that. |
| 1019 | */ |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1020 | if (qc->flags & ATA_QCFLAG_ACTIVE) { |
Tejun Heo | 11a56d2 | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1021 | qc->err_mask = AC_ERR_TIMEOUT; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1022 | ata_qc_complete(qc); |
| 1023 | printk(KERN_WARNING "ata%u: qc timeout (cmd 0x%x)\n", |
| 1024 | ap->id, command); |
| 1025 | } |
| 1026 | |
| 1027 | spin_unlock_irqrestore(&ap->host_set->lock, flags); |
| 1028 | } |
| 1029 | |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1030 | *tf = qc->tf; |
| 1031 | err_mask = qc->err_mask; |
| 1032 | |
| 1033 | ata_qc_free(qc); |
| 1034 | |
Tejun Heo | 1f7dd3e9 | 2006-03-24 15:25:30 +0900 | [diff] [blame] | 1035 | /* XXX - Some LLDDs (sata_mv) disable port on command failure. |
| 1036 | * Until those drivers are fixed, we detect the condition |
| 1037 | * here, fail the command with AC_ERR_SYSTEM and reenable the |
| 1038 | * port. |
| 1039 | * |
| 1040 | * Note that this doesn't change any behavior as internal |
| 1041 | * command failure results in disabling the device in the |
| 1042 | * higher layer for LLDDs without new reset/EH callbacks. |
| 1043 | * |
| 1044 | * Kill the following code as soon as those drivers are fixed. |
| 1045 | */ |
Tejun Heo | 198e0fe | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 1046 | if (ap->flags & ATA_FLAG_DISABLED) { |
Tejun Heo | 1f7dd3e9 | 2006-03-24 15:25:30 +0900 | [diff] [blame] | 1047 | err_mask |= AC_ERR_SYSTEM; |
| 1048 | ata_port_probe(ap); |
| 1049 | } |
| 1050 | |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1051 | return err_mask; |
Tejun Heo | a2a7a66 | 2005-12-13 14:48:31 +0900 | [diff] [blame] | 1052 | } |
| 1053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | /** |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1055 | * ata_pio_need_iordy - check if iordy needed |
| 1056 | * @adev: ATA device |
| 1057 | * |
| 1058 | * Check if the current speed of the device requires IORDY. Used |
| 1059 | * by various controllers for chip configuration. |
| 1060 | */ |
| 1061 | |
| 1062 | unsigned int ata_pio_need_iordy(const struct ata_device *adev) |
| 1063 | { |
| 1064 | int pio; |
| 1065 | int speed = adev->pio_mode - XFER_PIO_0; |
| 1066 | |
| 1067 | if (speed < 2) |
| 1068 | return 0; |
| 1069 | if (speed > 2) |
| 1070 | return 1; |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 1071 | |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 1072 | /* If we have no drive specific rule, then PIO 2 is non IORDY */ |
| 1073 | |
| 1074 | if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */ |
| 1075 | pio = adev->id[ATA_ID_EIDE_PIO]; |
| 1076 | /* Is the speed faster than the drive allows non IORDY ? */ |
| 1077 | if (pio) { |
| 1078 | /* This is cycle times not frequency - watch the logic! */ |
| 1079 | if (pio > 240) /* PIO2 is 240nS per cycle */ |
| 1080 | return 1; |
| 1081 | return 0; |
| 1082 | } |
| 1083 | } |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
| 1087 | /** |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1088 | * ata_dev_read_id - Read ID data from the specified device |
| 1089 | * @ap: port on which target device resides |
| 1090 | * @dev: target device |
| 1091 | * @p_class: pointer to class of the target device (may be changed) |
| 1092 | * @post_reset: is this read ID post-reset? |
Tejun Heo | d9572b1 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 1093 | * @p_id: read IDENTIFY page (newly allocated) |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1094 | * |
| 1095 | * Read ID data from the specified device. ATA_CMD_ID_ATA is |
| 1096 | * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI |
Tejun Heo | aec5c3c | 2006-03-25 01:33:34 +0900 | [diff] [blame] | 1097 | * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS |
| 1098 | * for pre-ATA4 drives. |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1099 | * |
| 1100 | * LOCKING: |
| 1101 | * Kernel thread context (may sleep) |
| 1102 | * |
| 1103 | * RETURNS: |
| 1104 | * 0 on success, -errno otherwise. |
| 1105 | */ |
| 1106 | static int ata_dev_read_id(struct ata_port *ap, struct ata_device *dev, |
Tejun Heo | d9572b1 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 1107 | unsigned int *p_class, int post_reset, u16 **p_id) |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1108 | { |
| 1109 | unsigned int class = *p_class; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1110 | struct ata_taskfile tf; |
| 1111 | unsigned int err_mask = 0; |
Tejun Heo | d9572b1 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 1112 | u16 *id; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1113 | const char *reason; |
| 1114 | int rc; |
| 1115 | |
| 1116 | DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno); |
| 1117 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1118 | ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */ |
| 1119 | |
Tejun Heo | d9572b1 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 1120 | id = kmalloc(sizeof(id[0]) * ATA_ID_WORDS, GFP_KERNEL); |
| 1121 | if (id == NULL) { |
| 1122 | rc = -ENOMEM; |
| 1123 | reason = "out of memory"; |
| 1124 | goto err_out; |
| 1125 | } |
| 1126 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1127 | retry: |
| 1128 | ata_tf_init(ap, &tf, dev->devno); |
| 1129 | |
| 1130 | switch (class) { |
| 1131 | case ATA_DEV_ATA: |
| 1132 | tf.command = ATA_CMD_ID_ATA; |
| 1133 | break; |
| 1134 | case ATA_DEV_ATAPI: |
| 1135 | tf.command = ATA_CMD_ID_ATAPI; |
| 1136 | break; |
| 1137 | default: |
| 1138 | rc = -ENODEV; |
| 1139 | reason = "unsupported class"; |
| 1140 | goto err_out; |
| 1141 | } |
| 1142 | |
| 1143 | tf.protocol = ATA_PROT_PIO; |
| 1144 | |
Tejun Heo | d69cf37 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 1145 | err_mask = ata_exec_internal(ap, dev, &tf, NULL, DMA_FROM_DEVICE, |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1146 | id, sizeof(id[0]) * ATA_ID_WORDS); |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1147 | if (err_mask) { |
| 1148 | rc = -EIO; |
| 1149 | reason = "I/O error"; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1150 | goto err_out; |
| 1151 | } |
| 1152 | |
| 1153 | swap_buf_le16(id, ATA_ID_WORDS); |
| 1154 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1155 | /* sanity check */ |
Alan Cox | 692785e | 2006-03-27 18:49:19 +0100 | [diff] [blame] | 1156 | if ((class == ATA_DEV_ATA) != (ata_id_is_ata(id) | ata_id_is_cfa(id))) { |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1157 | rc = -EINVAL; |
| 1158 | reason = "device reports illegal type"; |
| 1159 | goto err_out; |
| 1160 | } |
| 1161 | |
| 1162 | if (post_reset && class == ATA_DEV_ATA) { |
| 1163 | /* |
| 1164 | * The exact sequence expected by certain pre-ATA4 drives is: |
| 1165 | * SRST RESET |
| 1166 | * IDENTIFY |
| 1167 | * INITIALIZE DEVICE PARAMETERS |
| 1168 | * anything else.. |
| 1169 | * Some drives were very specific about that exact sequence. |
| 1170 | */ |
| 1171 | if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) { |
Albert Lee | 00b6f5e | 2006-03-27 16:39:18 +0800 | [diff] [blame] | 1172 | err_mask = ata_dev_init_params(ap, dev, id[3], id[6]); |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1173 | if (err_mask) { |
| 1174 | rc = -EIO; |
| 1175 | reason = "INIT_DEV_PARAMS failed"; |
| 1176 | goto err_out; |
| 1177 | } |
| 1178 | |
| 1179 | /* current CHS translation info (id[53-58]) might be |
| 1180 | * changed. reread the identify device info. |
| 1181 | */ |
| 1182 | post_reset = 0; |
| 1183 | goto retry; |
| 1184 | } |
| 1185 | } |
| 1186 | |
| 1187 | *p_class = class; |
Tejun Heo | d9572b1 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 1188 | *p_id = id; |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1189 | return 0; |
| 1190 | |
| 1191 | err_out: |
| 1192 | printk(KERN_WARNING "ata%u: dev %u failed to IDENTIFY (%s)\n", |
| 1193 | ap->id, dev->devno, reason); |
Tejun Heo | d9572b1 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 1194 | kfree(id); |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1195 | return rc; |
| 1196 | } |
| 1197 | |
Tejun Heo | 4b2f3ed | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 1198 | static inline u8 ata_dev_knobble(const struct ata_port *ap, |
| 1199 | struct ata_device *dev) |
| 1200 | { |
| 1201 | return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id))); |
| 1202 | } |
| 1203 | |
Tejun Heo | 49016ac | 2006-02-21 02:12:11 +0900 | [diff] [blame] | 1204 | /** |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 1205 | * ata_dev_configure - Configure the specified ATA/ATAPI device |
| 1206 | * @ap: Port on which target device resides |
| 1207 | * @dev: Target device to configure |
Tejun Heo | 4c2d721 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 1208 | * @print_info: Enable device info printout |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | * |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 1210 | * Configure @dev according to @dev->id. Generic and low-level |
| 1211 | * driver specific fixups are also applied. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | * |
| 1213 | * LOCKING: |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 1214 | * Kernel thread context (may sleep) |
| 1215 | * |
| 1216 | * RETURNS: |
| 1217 | * 0 on success, -errno otherwise |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1218 | */ |
Tejun Heo | 4c2d721 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 1219 | static int ata_dev_configure(struct ata_port *ap, struct ata_device *dev, |
| 1220 | int print_info) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | { |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 1222 | const u16 *id = dev->id; |
Tejun Heo | ff8854b | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1223 | unsigned int xfer_mask; |
Tejun Heo | 6e7846e | 2006-02-12 23:32:58 +0900 | [diff] [blame] | 1224 | int i, rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | |
Tejun Heo | e1211e3 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 1226 | if (!ata_dev_enabled(dev)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | DPRINTK("ENTER/EXIT (host %u, dev %u) -- nodev\n", |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 1228 | ap->id, dev->devno); |
| 1229 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 1232 | DPRINTK("ENTER, host %u, dev %u\n", ap->id, dev->devno); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1233 | |
Tejun Heo | c39f5eb | 2006-03-13 19:51:19 +0900 | [diff] [blame] | 1234 | /* print device capabilities */ |
| 1235 | if (print_info) |
| 1236 | printk(KERN_DEBUG "ata%u: dev %u cfg 49:%04x 82:%04x 83:%04x " |
| 1237 | "84:%04x 85:%04x 86:%04x 87:%04x 88:%04x\n", |
| 1238 | ap->id, dev->devno, id[49], id[82], id[83], |
| 1239 | id[84], id[85], id[86], id[87], id[88]); |
| 1240 | |
Tejun Heo | 208a993 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 1241 | /* initialize to-be-configured parameters */ |
Tejun Heo | ea1dd4e | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 1242 | dev->flags &= ~ATA_DFLAG_CFG_MASK; |
Tejun Heo | 208a993 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 1243 | dev->max_sectors = 0; |
| 1244 | dev->cdb_len = 0; |
| 1245 | dev->n_sectors = 0; |
| 1246 | dev->cylinders = 0; |
| 1247 | dev->heads = 0; |
| 1248 | dev->sectors = 0; |
| 1249 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | /* |
| 1251 | * common ATA, ATAPI feature tests |
| 1252 | */ |
| 1253 | |
Tejun Heo | ff8854b | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1254 | /* find max transfer mode; for printk only */ |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 1255 | xfer_mask = ata_id_xfermask(id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 1257 | ata_dump_id(id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | |
| 1259 | /* ATA-specific feature tests */ |
| 1260 | if (dev->class == ATA_DEV_ATA) { |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 1261 | dev->n_sectors = ata_id_n_sectors(id); |
Tejun Heo | 2940740 | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 1262 | |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 1263 | if (ata_id_has_lba(id)) { |
Tejun Heo | 4c2d721 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 1264 | const char *lba_desc; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1265 | |
Tejun Heo | 4c2d721 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 1266 | lba_desc = "LBA"; |
| 1267 | dev->flags |= ATA_DFLAG_LBA; |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 1268 | if (ata_id_has_lba48(id)) { |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1269 | dev->flags |= ATA_DFLAG_LBA48; |
Tejun Heo | 4c2d721 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 1270 | lba_desc = "LBA48"; |
| 1271 | } |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1272 | |
| 1273 | /* print device info to dmesg */ |
Tejun Heo | 4c2d721 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 1274 | if (print_info) |
| 1275 | printk(KERN_INFO "ata%u: dev %u ATA-%d, " |
| 1276 | "max %s, %Lu sectors: %s\n", |
| 1277 | ap->id, dev->devno, |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 1278 | ata_id_major_version(id), |
Tejun Heo | ff8854b | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1279 | ata_mode_string(xfer_mask), |
Tejun Heo | 4c2d721 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 1280 | (unsigned long long)dev->n_sectors, |
| 1281 | lba_desc); |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 1282 | } else { |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1283 | /* CHS */ |
| 1284 | |
| 1285 | /* Default translation */ |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 1286 | dev->cylinders = id[1]; |
| 1287 | dev->heads = id[3]; |
| 1288 | dev->sectors = id[6]; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1289 | |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 1290 | if (ata_id_current_chs_valid(id)) { |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1291 | /* Current CHS translation is valid. */ |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 1292 | dev->cylinders = id[54]; |
| 1293 | dev->heads = id[55]; |
| 1294 | dev->sectors = id[56]; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1295 | } |
| 1296 | |
| 1297 | /* print device info to dmesg */ |
Tejun Heo | 4c2d721 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 1298 | if (print_info) |
| 1299 | printk(KERN_INFO "ata%u: dev %u ATA-%d, " |
| 1300 | "max %s, %Lu sectors: CHS %u/%u/%u\n", |
| 1301 | ap->id, dev->devno, |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 1302 | ata_id_major_version(id), |
Tejun Heo | ff8854b | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1303 | ata_mode_string(xfer_mask), |
Tejun Heo | 4c2d721 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 1304 | (unsigned long long)dev->n_sectors, |
| 1305 | dev->cylinders, dev->heads, dev->sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | } |
| 1307 | |
Tejun Heo | 6e7846e | 2006-02-12 23:32:58 +0900 | [diff] [blame] | 1308 | dev->cdb_len = 16; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | } |
| 1310 | |
| 1311 | /* ATAPI-specific feature tests */ |
Jeff Garzik | 2c13b7c | 2005-11-14 14:14:16 -0500 | [diff] [blame] | 1312 | else if (dev->class == ATA_DEV_ATAPI) { |
Tejun Heo | 1148c3a | 2006-03-13 19:48:04 +0900 | [diff] [blame] | 1313 | rc = atapi_cdb_len(id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | if ((rc < 12) || (rc > ATAPI_CDB_LEN)) { |
| 1315 | printk(KERN_WARNING "ata%u: unsupported CDB len\n", ap->id); |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 1316 | rc = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1317 | goto err_out_nosup; |
| 1318 | } |
Tejun Heo | 6e7846e | 2006-02-12 23:32:58 +0900 | [diff] [blame] | 1319 | dev->cdb_len = (unsigned int) rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | |
| 1321 | /* print device info to dmesg */ |
Tejun Heo | 4c2d721 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 1322 | if (print_info) |
| 1323 | printk(KERN_INFO "ata%u: dev %u ATAPI, max %s\n", |
Tejun Heo | ff8854b | 2006-03-06 04:31:56 +0900 | [diff] [blame] | 1324 | ap->id, dev->devno, ata_mode_string(xfer_mask)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1325 | } |
| 1326 | |
Tejun Heo | 6e7846e | 2006-02-12 23:32:58 +0900 | [diff] [blame] | 1327 | ap->host->max_cmd_len = 0; |
| 1328 | for (i = 0; i < ATA_MAX_DEVICES; i++) |
| 1329 | ap->host->max_cmd_len = max_t(unsigned int, |
| 1330 | ap->host->max_cmd_len, |
| 1331 | ap->device[i].cdb_len); |
| 1332 | |
Tejun Heo | 4b2f3ed | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 1333 | /* limit bridge transfers to udma5, 200 sectors */ |
| 1334 | if (ata_dev_knobble(ap, dev)) { |
Tejun Heo | 4c2d721 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 1335 | if (print_info) |
| 1336 | printk(KERN_INFO "ata%u(%u): applying bridge limits\n", |
| 1337 | ap->id, dev->devno); |
Tejun Heo | 5a52913 | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 1338 | dev->udma_mask &= ATA_UDMA5; |
Tejun Heo | 4b2f3ed | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 1339 | dev->max_sectors = ATA_MAX_SECTORS; |
| 1340 | } |
| 1341 | |
| 1342 | if (ap->ops->dev_config) |
| 1343 | ap->ops->dev_config(ap, dev); |
| 1344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | DPRINTK("EXIT, drv_stat = 0x%x\n", ata_chk_status(ap)); |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 1346 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1347 | |
| 1348 | err_out_nosup: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1349 | DPRINTK("EXIT, err\n"); |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 1350 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | /** |
| 1354 | * ata_bus_probe - Reset and probe ATA bus |
| 1355 | * @ap: Bus to probe |
| 1356 | * |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 1357 | * Master ATA bus probing function. Initiates a hardware-dependent |
| 1358 | * bus reset, then attempts to identify any devices found on |
| 1359 | * the bus. |
| 1360 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1361 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 1362 | * PCI/etc. bus probe sem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | * |
| 1364 | * RETURNS: |
Tejun Heo | 96072e6 | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 1365 | * Zero on success, negative errno otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | */ |
| 1367 | |
| 1368 | static int ata_bus_probe(struct ata_port *ap) |
| 1369 | { |
Tejun Heo | 28ca5c5 | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 1370 | unsigned int classes[ATA_MAX_DEVICES]; |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 1371 | int tries[ATA_MAX_DEVICES]; |
| 1372 | int i, rc, down_xfermask; |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 1373 | struct ata_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | |
Tejun Heo | 28ca5c5 | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 1375 | ata_port_probe(ap); |
| 1376 | |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 1377 | for (i = 0; i < ATA_MAX_DEVICES; i++) |
| 1378 | tries[i] = ATA_PROBE_MAX_TRIES; |
| 1379 | |
| 1380 | retry: |
| 1381 | down_xfermask = 0; |
| 1382 | |
Tejun Heo | 2044470 | 2006-03-13 01:57:01 +0900 | [diff] [blame] | 1383 | /* reset and determine device classes */ |
| 1384 | for (i = 0; i < ATA_MAX_DEVICES; i++) |
| 1385 | classes[i] = ATA_DEV_UNKNOWN; |
Tejun Heo | 2061a47 | 2006-03-12 00:57:39 +0900 | [diff] [blame] | 1386 | |
Tejun Heo | 2044470 | 2006-03-13 01:57:01 +0900 | [diff] [blame] | 1387 | if (ap->ops->probe_reset) { |
Tejun Heo | c19ba8a | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 1388 | rc = ap->ops->probe_reset(ap, classes); |
Tejun Heo | 28ca5c5 | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 1389 | if (rc) { |
| 1390 | printk("ata%u: reset failed (errno=%d)\n", ap->id, rc); |
| 1391 | return rc; |
Tejun Heo | c19ba8a | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 1392 | } |
Tejun Heo | 28ca5c5 | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 1393 | } else { |
Tejun Heo | c19ba8a | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 1394 | ap->ops->phy_reset(ap); |
| 1395 | |
Tejun Heo | 198e0fe | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 1396 | if (!(ap->flags & ATA_FLAG_DISABLED)) |
Tejun Heo | 2044470 | 2006-03-13 01:57:01 +0900 | [diff] [blame] | 1397 | for (i = 0; i < ATA_MAX_DEVICES; i++) |
Tejun Heo | 28ca5c5 | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 1398 | classes[i] = ap->device[i].class; |
Tejun Heo | 2044470 | 2006-03-13 01:57:01 +0900 | [diff] [blame] | 1399 | |
Tejun Heo | 28ca5c5 | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 1400 | ata_port_probe(ap); |
| 1401 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1402 | |
Tejun Heo | 2044470 | 2006-03-13 01:57:01 +0900 | [diff] [blame] | 1403 | for (i = 0; i < ATA_MAX_DEVICES; i++) |
| 1404 | if (classes[i] == ATA_DEV_UNKNOWN) |
| 1405 | classes[i] = ATA_DEV_NONE; |
| 1406 | |
Tejun Heo | 28ca5c5 | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 1407 | /* read IDENTIFY page and configure devices */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1408 | for (i = 0; i < ATA_MAX_DEVICES; i++) { |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 1409 | dev = &ap->device[i]; |
Tejun Heo | 28ca5c5 | 2006-03-01 16:09:36 +0900 | [diff] [blame] | 1410 | dev->class = classes[i]; |
| 1411 | |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 1412 | if (!tries[i]) { |
| 1413 | ata_down_xfermask_limit(ap, dev, 1); |
| 1414 | ata_dev_disable(ap, dev); |
| 1415 | } |
| 1416 | |
Tejun Heo | e1211e3 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 1417 | if (!ata_dev_enabled(dev)) |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 1418 | continue; |
| 1419 | |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 1420 | kfree(dev->id); |
| 1421 | dev->id = NULL; |
| 1422 | rc = ata_dev_read_id(ap, dev, &dev->class, 1, &dev->id); |
| 1423 | if (rc) |
| 1424 | goto fail; |
Tejun Heo | ffeae41 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 1425 | |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 1426 | rc = ata_dev_configure(ap, dev, 1); |
| 1427 | if (rc) |
| 1428 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 1431 | /* configure transfer mode */ |
| 1432 | if (ap->ops->set_mode) { |
| 1433 | /* FIXME: make ->set_mode handle no device case and |
| 1434 | * return error code and failing device on failure as |
| 1435 | * ata_set_mode() does. |
| 1436 | */ |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 1437 | for (i = 0; i < ATA_MAX_DEVICES; i++) |
| 1438 | if (ata_dev_enabled(&ap->device[i])) { |
| 1439 | ap->ops->set_mode(ap); |
| 1440 | break; |
| 1441 | } |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 1442 | rc = 0; |
| 1443 | } else { |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 1444 | rc = ata_set_mode(ap, &dev); |
| 1445 | if (rc) { |
| 1446 | down_xfermask = 1; |
| 1447 | goto fail; |
| 1448 | } |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 1449 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1450 | |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 1451 | for (i = 0; i < ATA_MAX_DEVICES; i++) |
| 1452 | if (ata_dev_enabled(&ap->device[i])) |
| 1453 | return 0; |
Alan Cox | e35a9e0 | 2006-03-27 18:46:37 +0100 | [diff] [blame] | 1454 | |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 1455 | /* no device present, disable port */ |
| 1456 | ata_port_disable(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | ap->ops->port_disable(ap); |
Tejun Heo | 96072e6 | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 1458 | return -ENODEV; |
Tejun Heo | 14d2bac | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 1459 | |
| 1460 | fail: |
| 1461 | switch (rc) { |
| 1462 | case -EINVAL: |
| 1463 | case -ENODEV: |
| 1464 | tries[dev->devno] = 0; |
| 1465 | break; |
| 1466 | case -EIO: |
| 1467 | ata_down_sata_spd_limit(ap); |
| 1468 | /* fall through */ |
| 1469 | default: |
| 1470 | tries[dev->devno]--; |
| 1471 | if (down_xfermask && |
| 1472 | ata_down_xfermask_limit(ap, dev, tries[dev->devno] == 1)) |
| 1473 | tries[dev->devno] = 0; |
| 1474 | } |
| 1475 | |
| 1476 | goto retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | /** |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 1480 | * ata_port_probe - Mark port as enabled |
| 1481 | * @ap: Port for which we indicate enablement |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | * |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 1483 | * Modify @ap data structure such that the system |
| 1484 | * thinks that the entire port is enabled. |
| 1485 | * |
| 1486 | * LOCKING: host_set lock, or some other form of |
| 1487 | * serialization. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | */ |
| 1489 | |
| 1490 | void ata_port_probe(struct ata_port *ap) |
| 1491 | { |
Tejun Heo | 198e0fe | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 1492 | ap->flags &= ~ATA_FLAG_DISABLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | /** |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 1496 | * sata_print_link_status - Print SATA link status |
| 1497 | * @ap: SATA port to printk link status about |
| 1498 | * |
| 1499 | * This function prints link speed and status of a SATA link. |
| 1500 | * |
| 1501 | * LOCKING: |
| 1502 | * None. |
| 1503 | */ |
| 1504 | static void sata_print_link_status(struct ata_port *ap) |
| 1505 | { |
| 1506 | u32 sstatus, tmp; |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 1507 | |
| 1508 | if (!ap->ops->scr_read) |
| 1509 | return; |
| 1510 | |
| 1511 | sstatus = scr_read(ap, SCR_STATUS); |
| 1512 | |
| 1513 | if (sata_dev_present(ap)) { |
| 1514 | tmp = (sstatus >> 4) & 0xf; |
Tejun Heo | 4c360c8 | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 1515 | printk(KERN_INFO "ata%u: SATA link up %s (SStatus %X)\n", |
| 1516 | ap->id, sata_spd_string(tmp), sstatus); |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 1517 | } else { |
| 1518 | printk(KERN_INFO "ata%u: SATA link down (SStatus %X)\n", |
| 1519 | ap->id, sstatus); |
| 1520 | } |
| 1521 | } |
| 1522 | |
| 1523 | /** |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 1524 | * __sata_phy_reset - Wake/reset a low-level SATA PHY |
| 1525 | * @ap: SATA port associated with target SATA PHY. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | * |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 1527 | * This function issues commands to standard SATA Sxxx |
| 1528 | * PHY registers, to wake up the phy (and device), and |
| 1529 | * clear any reset condition. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | * |
| 1531 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 1532 | * PCI/etc. bus probe sem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | * |
| 1534 | */ |
| 1535 | void __sata_phy_reset(struct ata_port *ap) |
| 1536 | { |
| 1537 | u32 sstatus; |
| 1538 | unsigned long timeout = jiffies + (HZ * 5); |
| 1539 | |
| 1540 | if (ap->flags & ATA_FLAG_SATA_RESET) { |
Brett Russ | cdcca89e | 2005-03-28 15:10:27 -0500 | [diff] [blame] | 1541 | /* issue phy wake/reset */ |
| 1542 | scr_write_flush(ap, SCR_CONTROL, 0x301); |
Tejun Heo | 62ba284 | 2005-06-26 23:27:19 +0900 | [diff] [blame] | 1543 | /* Couldn't find anything in SATA I/II specs, but |
| 1544 | * AHCI-1.1 10.4.2 says at least 1 ms. */ |
| 1545 | mdelay(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | } |
Brett Russ | cdcca89e | 2005-03-28 15:10:27 -0500 | [diff] [blame] | 1547 | scr_write_flush(ap, SCR_CONTROL, 0x300); /* phy wake/clear reset */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | |
| 1549 | /* wait for phy to become ready, if necessary */ |
| 1550 | do { |
| 1551 | msleep(200); |
| 1552 | sstatus = scr_read(ap, SCR_STATUS); |
| 1553 | if ((sstatus & 0xf) != 1) |
| 1554 | break; |
| 1555 | } while (time_before(jiffies, timeout)); |
| 1556 | |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 1557 | /* print link status */ |
| 1558 | sata_print_link_status(ap); |
Jeff Garzik | 656563e | 2005-11-20 03:36:45 -0500 | [diff] [blame] | 1559 | |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 1560 | /* TODO: phy layer with polling, timeouts, etc. */ |
| 1561 | if (sata_dev_present(ap)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | ata_port_probe(ap); |
Tejun Heo | 3be680b | 2005-12-19 22:35:02 +0900 | [diff] [blame] | 1563 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | ata_port_disable(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | |
Tejun Heo | 198e0fe | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 1566 | if (ap->flags & ATA_FLAG_DISABLED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | return; |
| 1568 | |
| 1569 | if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) { |
| 1570 | ata_port_disable(ap); |
| 1571 | return; |
| 1572 | } |
| 1573 | |
| 1574 | ap->cbl = ATA_CBL_SATA; |
| 1575 | } |
| 1576 | |
| 1577 | /** |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 1578 | * sata_phy_reset - Reset SATA bus. |
| 1579 | * @ap: SATA port associated with target SATA PHY. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | * |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 1581 | * This function resets the SATA bus, and then probes |
| 1582 | * the bus for devices. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | * |
| 1584 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 1585 | * PCI/etc. bus probe sem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1586 | * |
| 1587 | */ |
| 1588 | void sata_phy_reset(struct ata_port *ap) |
| 1589 | { |
| 1590 | __sata_phy_reset(ap); |
Tejun Heo | 198e0fe | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 1591 | if (ap->flags & ATA_FLAG_DISABLED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1592 | return; |
| 1593 | ata_bus_reset(ap); |
| 1594 | } |
| 1595 | |
| 1596 | /** |
Alan Cox | ebdfca6 | 2006-03-23 15:38:34 +0000 | [diff] [blame] | 1597 | * ata_dev_pair - return other device on cable |
| 1598 | * @ap: port |
| 1599 | * @adev: device |
| 1600 | * |
| 1601 | * Obtain the other device on the same cable, or if none is |
| 1602 | * present NULL is returned |
| 1603 | */ |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 1604 | |
Alan Cox | ebdfca6 | 2006-03-23 15:38:34 +0000 | [diff] [blame] | 1605 | struct ata_device *ata_dev_pair(struct ata_port *ap, struct ata_device *adev) |
| 1606 | { |
| 1607 | struct ata_device *pair = &ap->device[1 - adev->devno]; |
Tejun Heo | e1211e3 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 1608 | if (!ata_dev_enabled(pair)) |
Alan Cox | ebdfca6 | 2006-03-23 15:38:34 +0000 | [diff] [blame] | 1609 | return NULL; |
| 1610 | return pair; |
| 1611 | } |
| 1612 | |
| 1613 | /** |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 1614 | * ata_port_disable - Disable port. |
| 1615 | * @ap: Port to be disabled. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1616 | * |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 1617 | * Modify @ap data structure such that the system |
| 1618 | * thinks that the entire port is disabled, and should |
| 1619 | * never attempt to probe or communicate with devices |
| 1620 | * on this port. |
| 1621 | * |
| 1622 | * LOCKING: host_set lock, or some other form of |
| 1623 | * serialization. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1624 | */ |
| 1625 | |
| 1626 | void ata_port_disable(struct ata_port *ap) |
| 1627 | { |
| 1628 | ap->device[0].class = ATA_DEV_NONE; |
| 1629 | ap->device[1].class = ATA_DEV_NONE; |
Tejun Heo | 198e0fe | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 1630 | ap->flags |= ATA_FLAG_DISABLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | } |
| 1632 | |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 1633 | /** |
| 1634 | * ata_down_sata_spd_limit - adjust SATA spd limit downward |
| 1635 | * @ap: Port to adjust SATA spd limit for |
| 1636 | * |
| 1637 | * Adjust SATA spd limit of @ap downward. Note that this |
| 1638 | * function only adjusts the limit. The change must be applied |
| 1639 | * using ata_set_sata_spd(). |
| 1640 | * |
| 1641 | * LOCKING: |
| 1642 | * Inherited from caller. |
| 1643 | * |
| 1644 | * RETURNS: |
| 1645 | * 0 on success, negative errno on failure |
| 1646 | */ |
Tejun Heo | 1ad8e7f | 2006-04-02 18:51:53 +0900 | [diff] [blame^] | 1647 | int ata_down_sata_spd_limit(struct ata_port *ap) |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 1648 | { |
| 1649 | u32 spd, mask; |
| 1650 | int highbit; |
| 1651 | |
| 1652 | if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read) |
| 1653 | return -EOPNOTSUPP; |
| 1654 | |
| 1655 | mask = ap->sata_spd_limit; |
| 1656 | if (mask <= 1) |
| 1657 | return -EINVAL; |
| 1658 | highbit = fls(mask) - 1; |
| 1659 | mask &= ~(1 << highbit); |
| 1660 | |
| 1661 | spd = (scr_read(ap, SCR_STATUS) >> 4) & 0xf; |
| 1662 | if (spd <= 1) |
| 1663 | return -EINVAL; |
| 1664 | spd--; |
| 1665 | mask &= (1 << spd) - 1; |
| 1666 | if (!mask) |
| 1667 | return -EINVAL; |
| 1668 | |
| 1669 | ap->sata_spd_limit = mask; |
| 1670 | |
| 1671 | printk(KERN_WARNING "ata%u: limiting SATA link speed to %s\n", |
| 1672 | ap->id, sata_spd_string(fls(mask))); |
| 1673 | |
| 1674 | return 0; |
| 1675 | } |
| 1676 | |
| 1677 | static int __ata_set_sata_spd_needed(struct ata_port *ap, u32 *scontrol) |
| 1678 | { |
| 1679 | u32 spd, limit; |
| 1680 | |
| 1681 | if (ap->sata_spd_limit == UINT_MAX) |
| 1682 | limit = 0; |
| 1683 | else |
| 1684 | limit = fls(ap->sata_spd_limit); |
| 1685 | |
| 1686 | spd = (*scontrol >> 4) & 0xf; |
| 1687 | *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4); |
| 1688 | |
| 1689 | return spd != limit; |
| 1690 | } |
| 1691 | |
| 1692 | /** |
| 1693 | * ata_set_sata_spd_needed - is SATA spd configuration needed |
| 1694 | * @ap: Port in question |
| 1695 | * |
| 1696 | * Test whether the spd limit in SControl matches |
| 1697 | * @ap->sata_spd_limit. This function is used to determine |
| 1698 | * whether hardreset is necessary to apply SATA spd |
| 1699 | * configuration. |
| 1700 | * |
| 1701 | * LOCKING: |
| 1702 | * Inherited from caller. |
| 1703 | * |
| 1704 | * RETURNS: |
| 1705 | * 1 if SATA spd configuration is needed, 0 otherwise. |
| 1706 | */ |
Tejun Heo | 1ad8e7f | 2006-04-02 18:51:53 +0900 | [diff] [blame^] | 1707 | int ata_set_sata_spd_needed(struct ata_port *ap) |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 1708 | { |
| 1709 | u32 scontrol; |
| 1710 | |
| 1711 | if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read) |
| 1712 | return 0; |
| 1713 | |
| 1714 | scontrol = scr_read(ap, SCR_CONTROL); |
| 1715 | |
| 1716 | return __ata_set_sata_spd_needed(ap, &scontrol); |
| 1717 | } |
| 1718 | |
| 1719 | /** |
| 1720 | * ata_set_sata_spd - set SATA spd according to spd limit |
| 1721 | * @ap: Port to set SATA spd for |
| 1722 | * |
| 1723 | * Set SATA spd of @ap according to sata_spd_limit. |
| 1724 | * |
| 1725 | * LOCKING: |
| 1726 | * Inherited from caller. |
| 1727 | * |
| 1728 | * RETURNS: |
| 1729 | * 0 if spd doesn't need to be changed, 1 if spd has been |
| 1730 | * changed. -EOPNOTSUPP if SCR registers are inaccessible. |
| 1731 | */ |
| 1732 | static int ata_set_sata_spd(struct ata_port *ap) |
| 1733 | { |
| 1734 | u32 scontrol; |
| 1735 | |
| 1736 | if (ap->cbl != ATA_CBL_SATA || !ap->ops->scr_read) |
| 1737 | return -EOPNOTSUPP; |
| 1738 | |
| 1739 | scontrol = scr_read(ap, SCR_CONTROL); |
| 1740 | if (!__ata_set_sata_spd_needed(ap, &scontrol)) |
| 1741 | return 0; |
| 1742 | |
| 1743 | scr_write(ap, SCR_CONTROL, scontrol); |
| 1744 | return 1; |
| 1745 | } |
| 1746 | |
Alan Cox | 452503f | 2005-10-21 19:01:32 -0400 | [diff] [blame] | 1747 | /* |
| 1748 | * This mode timing computation functionality is ported over from |
| 1749 | * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik |
| 1750 | */ |
| 1751 | /* |
| 1752 | * PIO 0-5, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds). |
| 1753 | * These were taken from ATA/ATAPI-6 standard, rev 0a, except |
| 1754 | * for PIO 5, which is a nonstandard extension and UDMA6, which |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 1755 | * is currently supported only by Maxtor drives. |
Alan Cox | 452503f | 2005-10-21 19:01:32 -0400 | [diff] [blame] | 1756 | */ |
| 1757 | |
| 1758 | static const struct ata_timing ata_timing[] = { |
| 1759 | |
| 1760 | { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 }, |
| 1761 | { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 }, |
| 1762 | { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 }, |
| 1763 | { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 }, |
| 1764 | |
| 1765 | { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 }, |
| 1766 | { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 }, |
| 1767 | { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 }, |
| 1768 | |
| 1769 | /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */ |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 1770 | |
Alan Cox | 452503f | 2005-10-21 19:01:32 -0400 | [diff] [blame] | 1771 | { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 }, |
| 1772 | { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 }, |
| 1773 | { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 }, |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 1774 | |
Alan Cox | 452503f | 2005-10-21 19:01:32 -0400 | [diff] [blame] | 1775 | { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 }, |
| 1776 | { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 }, |
| 1777 | { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 }, |
| 1778 | |
| 1779 | /* { XFER_PIO_5, 20, 50, 30, 100, 50, 30, 100, 0 }, */ |
| 1780 | { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 }, |
| 1781 | { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 }, |
| 1782 | |
| 1783 | { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 }, |
| 1784 | { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 }, |
| 1785 | { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 }, |
| 1786 | |
| 1787 | /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */ |
| 1788 | |
| 1789 | { 0xFF } |
| 1790 | }; |
| 1791 | |
| 1792 | #define ENOUGH(v,unit) (((v)-1)/(unit)+1) |
| 1793 | #define EZ(v,unit) ((v)?ENOUGH(v,unit):0) |
| 1794 | |
| 1795 | static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT) |
| 1796 | { |
| 1797 | q->setup = EZ(t->setup * 1000, T); |
| 1798 | q->act8b = EZ(t->act8b * 1000, T); |
| 1799 | q->rec8b = EZ(t->rec8b * 1000, T); |
| 1800 | q->cyc8b = EZ(t->cyc8b * 1000, T); |
| 1801 | q->active = EZ(t->active * 1000, T); |
| 1802 | q->recover = EZ(t->recover * 1000, T); |
| 1803 | q->cycle = EZ(t->cycle * 1000, T); |
| 1804 | q->udma = EZ(t->udma * 1000, UT); |
| 1805 | } |
| 1806 | |
| 1807 | void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b, |
| 1808 | struct ata_timing *m, unsigned int what) |
| 1809 | { |
| 1810 | if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup); |
| 1811 | if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b); |
| 1812 | if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b); |
| 1813 | if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b); |
| 1814 | if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active); |
| 1815 | if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover); |
| 1816 | if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle); |
| 1817 | if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma); |
| 1818 | } |
| 1819 | |
| 1820 | static const struct ata_timing* ata_timing_find_mode(unsigned short speed) |
| 1821 | { |
| 1822 | const struct ata_timing *t; |
| 1823 | |
| 1824 | for (t = ata_timing; t->mode != speed; t++) |
Alan Cox | 9119075 | 2005-10-26 12:17:46 -0400 | [diff] [blame] | 1825 | if (t->mode == 0xFF) |
Alan Cox | 452503f | 2005-10-21 19:01:32 -0400 | [diff] [blame] | 1826 | return NULL; |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 1827 | return t; |
Alan Cox | 452503f | 2005-10-21 19:01:32 -0400 | [diff] [blame] | 1828 | } |
| 1829 | |
| 1830 | int ata_timing_compute(struct ata_device *adev, unsigned short speed, |
| 1831 | struct ata_timing *t, int T, int UT) |
| 1832 | { |
| 1833 | const struct ata_timing *s; |
| 1834 | struct ata_timing p; |
| 1835 | |
| 1836 | /* |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 1837 | * Find the mode. |
Albert Lee | 75b1f2f | 2005-11-16 17:06:18 +0800 | [diff] [blame] | 1838 | */ |
Alan Cox | 452503f | 2005-10-21 19:01:32 -0400 | [diff] [blame] | 1839 | |
| 1840 | if (!(s = ata_timing_find_mode(speed))) |
| 1841 | return -EINVAL; |
| 1842 | |
Albert Lee | 75b1f2f | 2005-11-16 17:06:18 +0800 | [diff] [blame] | 1843 | memcpy(t, s, sizeof(*s)); |
| 1844 | |
Alan Cox | 452503f | 2005-10-21 19:01:32 -0400 | [diff] [blame] | 1845 | /* |
| 1846 | * If the drive is an EIDE drive, it can tell us it needs extended |
| 1847 | * PIO/MW_DMA cycle timing. |
| 1848 | */ |
| 1849 | |
| 1850 | if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */ |
| 1851 | memset(&p, 0, sizeof(p)); |
| 1852 | if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) { |
| 1853 | if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO]; |
| 1854 | else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY]; |
| 1855 | } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) { |
| 1856 | p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN]; |
| 1857 | } |
| 1858 | ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B); |
| 1859 | } |
| 1860 | |
| 1861 | /* |
| 1862 | * Convert the timing to bus clock counts. |
| 1863 | */ |
| 1864 | |
Albert Lee | 75b1f2f | 2005-11-16 17:06:18 +0800 | [diff] [blame] | 1865 | ata_timing_quantize(t, t, T, UT); |
Alan Cox | 452503f | 2005-10-21 19:01:32 -0400 | [diff] [blame] | 1866 | |
| 1867 | /* |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 1868 | * Even in DMA/UDMA modes we still use PIO access for IDENTIFY, |
| 1869 | * S.M.A.R.T * and some other commands. We have to ensure that the |
| 1870 | * DMA cycle timing is slower/equal than the fastest PIO timing. |
Alan Cox | 452503f | 2005-10-21 19:01:32 -0400 | [diff] [blame] | 1871 | */ |
| 1872 | |
| 1873 | if (speed > XFER_PIO_4) { |
| 1874 | ata_timing_compute(adev, adev->pio_mode, &p, T, UT); |
| 1875 | ata_timing_merge(&p, t, t, ATA_TIMING_ALL); |
| 1876 | } |
| 1877 | |
| 1878 | /* |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 1879 | * Lengthen active & recovery time so that cycle time is correct. |
Alan Cox | 452503f | 2005-10-21 19:01:32 -0400 | [diff] [blame] | 1880 | */ |
| 1881 | |
| 1882 | if (t->act8b + t->rec8b < t->cyc8b) { |
| 1883 | t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2; |
| 1884 | t->rec8b = t->cyc8b - t->act8b; |
| 1885 | } |
| 1886 | |
| 1887 | if (t->active + t->recover < t->cycle) { |
| 1888 | t->active += (t->cycle - (t->active + t->recover)) / 2; |
| 1889 | t->recover = t->cycle - t->active; |
| 1890 | } |
| 1891 | |
| 1892 | return 0; |
| 1893 | } |
| 1894 | |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 1895 | /** |
| 1896 | * ata_down_xfermask_limit - adjust dev xfer masks downward |
| 1897 | * @ap: Port associated with device @dev |
| 1898 | * @dev: Device to adjust xfer masks |
| 1899 | * @force_pio0: Force PIO0 |
| 1900 | * |
| 1901 | * Adjust xfer masks of @dev downward. Note that this function |
| 1902 | * does not apply the change. Invoking ata_set_mode() afterwards |
| 1903 | * will apply the limit. |
| 1904 | * |
| 1905 | * LOCKING: |
| 1906 | * Inherited from caller. |
| 1907 | * |
| 1908 | * RETURNS: |
| 1909 | * 0 on success, negative errno on failure |
| 1910 | */ |
Tejun Heo | 1ad8e7f | 2006-04-02 18:51:53 +0900 | [diff] [blame^] | 1911 | int ata_down_xfermask_limit(struct ata_port *ap, struct ata_device *dev, |
| 1912 | int force_pio0) |
Tejun Heo | cf176e1 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 1913 | { |
| 1914 | unsigned long xfer_mask; |
| 1915 | int highbit; |
| 1916 | |
| 1917 | xfer_mask = ata_pack_xfermask(dev->pio_mask, dev->mwdma_mask, |
| 1918 | dev->udma_mask); |
| 1919 | |
| 1920 | if (!xfer_mask) |
| 1921 | goto fail; |
| 1922 | /* don't gear down to MWDMA from UDMA, go directly to PIO */ |
| 1923 | if (xfer_mask & ATA_MASK_UDMA) |
| 1924 | xfer_mask &= ~ATA_MASK_MWDMA; |
| 1925 | |
| 1926 | highbit = fls(xfer_mask) - 1; |
| 1927 | xfer_mask &= ~(1 << highbit); |
| 1928 | if (force_pio0) |
| 1929 | xfer_mask &= 1 << ATA_SHIFT_PIO; |
| 1930 | if (!xfer_mask) |
| 1931 | goto fail; |
| 1932 | |
| 1933 | ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask, |
| 1934 | &dev->udma_mask); |
| 1935 | |
| 1936 | printk(KERN_WARNING "ata%u: dev %u limiting speed to %s\n", |
| 1937 | ap->id, dev->devno, ata_mode_string(xfer_mask)); |
| 1938 | |
| 1939 | return 0; |
| 1940 | |
| 1941 | fail: |
| 1942 | return -EINVAL; |
| 1943 | } |
| 1944 | |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 1945 | static int ata_dev_set_mode(struct ata_port *ap, struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1946 | { |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 1947 | unsigned int err_mask; |
| 1948 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | |
Tejun Heo | e838460 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 1950 | dev->flags &= ~ATA_DFLAG_PIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1951 | if (dev->xfer_shift == ATA_SHIFT_PIO) |
| 1952 | dev->flags |= ATA_DFLAG_PIO; |
| 1953 | |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 1954 | err_mask = ata_dev_set_xfermode(ap, dev); |
| 1955 | if (err_mask) { |
| 1956 | printk(KERN_ERR |
| 1957 | "ata%u: failed to set xfermode (err_mask=0x%x)\n", |
| 1958 | ap->id, err_mask); |
| 1959 | return -EIO; |
| 1960 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 1962 | rc = ata_dev_revalidate(ap, dev, 0); |
Tejun Heo | 5eb45c0 | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 1963 | if (rc) |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 1964 | return rc; |
Tejun Heo | 48a8a14 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 1965 | |
Tejun Heo | 23e71c3 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 1966 | DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n", |
| 1967 | dev->xfer_shift, (int)dev->xfer_mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | |
| 1969 | printk(KERN_INFO "ata%u: dev %u configured for %s\n", |
Tejun Heo | 23e71c3 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 1970 | ap->id, dev->devno, |
| 1971 | ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode))); |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 1972 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | } |
| 1974 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1975 | /** |
| 1976 | * ata_set_mode - Program timings and issue SET FEATURES - XFER |
| 1977 | * @ap: port on which timings will be programmed |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 1978 | * @r_failed_dev: out paramter for failed device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1979 | * |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 1980 | * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If |
| 1981 | * ata_set_mode() fails, pointer to the failing device is |
| 1982 | * returned in @r_failed_dev. |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 1983 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 1985 | * PCI/etc. bus probe sem. |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 1986 | * |
| 1987 | * RETURNS: |
| 1988 | * 0 on success, negative errno otherwise |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1989 | */ |
Tejun Heo | 1ad8e7f | 2006-04-02 18:51:53 +0900 | [diff] [blame^] | 1990 | int ata_set_mode(struct ata_port *ap, struct ata_device **r_failed_dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1991 | { |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 1992 | struct ata_device *dev; |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 1993 | int i, rc = 0, used_dma = 0, found = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 1995 | /* step 1: calculate xfer_mask */ |
| 1996 | for (i = 0; i < ATA_MAX_DEVICES; i++) { |
Tejun Heo | acf356b | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 1997 | unsigned int pio_mask, dma_mask; |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 1998 | |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 1999 | dev = &ap->device[i]; |
| 2000 | |
Tejun Heo | e1211e3 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2001 | if (!ata_dev_enabled(dev)) |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 2002 | continue; |
| 2003 | |
Tejun Heo | acf356b | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 2004 | ata_dev_xfermask(ap, dev); |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 2005 | |
Tejun Heo | acf356b | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 2006 | pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0); |
| 2007 | dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask); |
| 2008 | dev->pio_mode = ata_xfer_mask2mode(pio_mask); |
| 2009 | dev->dma_mode = ata_xfer_mask2mode(dma_mask); |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 2010 | |
Tejun Heo | 4f65977 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2011 | found = 1; |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 2012 | if (dev->dma_mode) |
| 2013 | used_dma = 1; |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 2014 | } |
Tejun Heo | 4f65977 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2015 | if (!found) |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2016 | goto out; |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 2017 | |
| 2018 | /* step 2: always set host PIO timings */ |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2019 | for (i = 0; i < ATA_MAX_DEVICES; i++) { |
| 2020 | dev = &ap->device[i]; |
| 2021 | if (!ata_dev_enabled(dev)) |
| 2022 | continue; |
| 2023 | |
| 2024 | if (!dev->pio_mode) { |
| 2025 | printk(KERN_WARNING "ata%u: dev %u no PIO support\n", |
| 2026 | ap->id, dev->devno); |
| 2027 | rc = -EINVAL; |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2028 | goto out; |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2029 | } |
| 2030 | |
| 2031 | dev->xfer_mode = dev->pio_mode; |
| 2032 | dev->xfer_shift = ATA_SHIFT_PIO; |
| 2033 | if (ap->ops->set_piomode) |
| 2034 | ap->ops->set_piomode(ap, dev); |
| 2035 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 2037 | /* step 3: set host DMA timings */ |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2038 | for (i = 0; i < ATA_MAX_DEVICES; i++) { |
| 2039 | dev = &ap->device[i]; |
| 2040 | |
| 2041 | if (!ata_dev_enabled(dev) || !dev->dma_mode) |
| 2042 | continue; |
| 2043 | |
| 2044 | dev->xfer_mode = dev->dma_mode; |
| 2045 | dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode); |
| 2046 | if (ap->ops->set_dmamode) |
| 2047 | ap->ops->set_dmamode(ap, dev); |
| 2048 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2049 | |
| 2050 | /* step 4: update devices' xfer mode */ |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 2051 | for (i = 0; i < ATA_MAX_DEVICES; i++) { |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2052 | dev = &ap->device[i]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2053 | |
Tejun Heo | e1211e3 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2054 | if (!ata_dev_enabled(dev)) |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 2055 | continue; |
| 2056 | |
Tejun Heo | 5bbc53f | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 2057 | rc = ata_dev_set_mode(ap, dev); |
| 2058 | if (rc) |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2059 | goto out; |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 2060 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2061 | |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2062 | /* Record simplex status. If we selected DMA then the other |
| 2063 | * host channels are not permitted to do so. |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 2064 | */ |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 2065 | if (used_dma && (ap->host_set->flags & ATA_HOST_SIMPLEX)) |
| 2066 | ap->host_set->simplex_claimed = 1; |
| 2067 | |
Tejun Heo | e8e0619 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2068 | /* step5: chip specific finalisation */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2069 | if (ap->ops->post_set_mode) |
| 2070 | ap->ops->post_set_mode(ap); |
| 2071 | |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2072 | out: |
| 2073 | if (rc) |
| 2074 | *r_failed_dev = dev; |
| 2075 | return rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | } |
| 2077 | |
| 2078 | /** |
Jeff Garzik | 1fdffbc | 2006-02-09 05:15:27 -0500 | [diff] [blame] | 2079 | * ata_tf_to_host - issue ATA taskfile to host controller |
| 2080 | * @ap: port to which command is being issued |
| 2081 | * @tf: ATA taskfile register set |
| 2082 | * |
| 2083 | * Issues ATA taskfile register set to ATA host controller, |
| 2084 | * with proper synchronization with interrupt handler and |
| 2085 | * other threads. |
| 2086 | * |
| 2087 | * LOCKING: |
| 2088 | * spin_lock_irqsave(host_set lock) |
| 2089 | */ |
| 2090 | |
| 2091 | static inline void ata_tf_to_host(struct ata_port *ap, |
| 2092 | const struct ata_taskfile *tf) |
| 2093 | { |
| 2094 | ap->ops->tf_load(ap, tf); |
| 2095 | ap->ops->exec_command(ap, tf); |
| 2096 | } |
| 2097 | |
| 2098 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | * ata_busy_sleep - sleep until BSY clears, or timeout |
| 2100 | * @ap: port containing status register to be polled |
| 2101 | * @tmout_pat: impatience timeout |
| 2102 | * @tmout: overall timeout |
| 2103 | * |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 2104 | * Sleep until ATA Status register bit BSY clears, |
| 2105 | * or a timeout occurs. |
| 2106 | * |
| 2107 | * LOCKING: None. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | */ |
| 2109 | |
Tejun Heo | 6f8b995 | 2006-01-24 17:05:21 +0900 | [diff] [blame] | 2110 | unsigned int ata_busy_sleep (struct ata_port *ap, |
| 2111 | unsigned long tmout_pat, unsigned long tmout) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | { |
| 2113 | unsigned long timer_start, timeout; |
| 2114 | u8 status; |
| 2115 | |
| 2116 | status = ata_busy_wait(ap, ATA_BUSY, 300); |
| 2117 | timer_start = jiffies; |
| 2118 | timeout = timer_start + tmout_pat; |
| 2119 | while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) { |
| 2120 | msleep(50); |
| 2121 | status = ata_busy_wait(ap, ATA_BUSY, 3); |
| 2122 | } |
| 2123 | |
| 2124 | if (status & ATA_BUSY) |
| 2125 | printk(KERN_WARNING "ata%u is slow to respond, " |
| 2126 | "please be patient\n", ap->id); |
| 2127 | |
| 2128 | timeout = timer_start + tmout; |
| 2129 | while ((status & ATA_BUSY) && (time_before(jiffies, timeout))) { |
| 2130 | msleep(50); |
| 2131 | status = ata_chk_status(ap); |
| 2132 | } |
| 2133 | |
| 2134 | if (status & ATA_BUSY) { |
| 2135 | printk(KERN_ERR "ata%u failed to respond (%lu secs)\n", |
| 2136 | ap->id, tmout / HZ); |
| 2137 | return 1; |
| 2138 | } |
| 2139 | |
| 2140 | return 0; |
| 2141 | } |
| 2142 | |
| 2143 | static void ata_bus_post_reset(struct ata_port *ap, unsigned int devmask) |
| 2144 | { |
| 2145 | struct ata_ioports *ioaddr = &ap->ioaddr; |
| 2146 | unsigned int dev0 = devmask & (1 << 0); |
| 2147 | unsigned int dev1 = devmask & (1 << 1); |
| 2148 | unsigned long timeout; |
| 2149 | |
| 2150 | /* if device 0 was found in ata_devchk, wait for its |
| 2151 | * BSY bit to clear |
| 2152 | */ |
| 2153 | if (dev0) |
| 2154 | ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT); |
| 2155 | |
| 2156 | /* if device 1 was found in ata_devchk, wait for |
| 2157 | * register access, then wait for BSY to clear |
| 2158 | */ |
| 2159 | timeout = jiffies + ATA_TMOUT_BOOT; |
| 2160 | while (dev1) { |
| 2161 | u8 nsect, lbal; |
| 2162 | |
| 2163 | ap->ops->dev_select(ap, 1); |
| 2164 | if (ap->flags & ATA_FLAG_MMIO) { |
| 2165 | nsect = readb((void __iomem *) ioaddr->nsect_addr); |
| 2166 | lbal = readb((void __iomem *) ioaddr->lbal_addr); |
| 2167 | } else { |
| 2168 | nsect = inb(ioaddr->nsect_addr); |
| 2169 | lbal = inb(ioaddr->lbal_addr); |
| 2170 | } |
| 2171 | if ((nsect == 1) && (lbal == 1)) |
| 2172 | break; |
| 2173 | if (time_after(jiffies, timeout)) { |
| 2174 | dev1 = 0; |
| 2175 | break; |
| 2176 | } |
| 2177 | msleep(50); /* give drive a breather */ |
| 2178 | } |
| 2179 | if (dev1) |
| 2180 | ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT); |
| 2181 | |
| 2182 | /* is all this really necessary? */ |
| 2183 | ap->ops->dev_select(ap, 0); |
| 2184 | if (dev1) |
| 2185 | ap->ops->dev_select(ap, 1); |
| 2186 | if (dev0) |
| 2187 | ap->ops->dev_select(ap, 0); |
| 2188 | } |
| 2189 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | static unsigned int ata_bus_softreset(struct ata_port *ap, |
| 2191 | unsigned int devmask) |
| 2192 | { |
| 2193 | struct ata_ioports *ioaddr = &ap->ioaddr; |
| 2194 | |
| 2195 | DPRINTK("ata%u: bus reset via SRST\n", ap->id); |
| 2196 | |
| 2197 | /* software reset. causes dev0 to be selected */ |
| 2198 | if (ap->flags & ATA_FLAG_MMIO) { |
| 2199 | writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr); |
| 2200 | udelay(20); /* FIXME: flush */ |
| 2201 | writeb(ap->ctl | ATA_SRST, (void __iomem *) ioaddr->ctl_addr); |
| 2202 | udelay(20); /* FIXME: flush */ |
| 2203 | writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr); |
| 2204 | } else { |
| 2205 | outb(ap->ctl, ioaddr->ctl_addr); |
| 2206 | udelay(10); |
| 2207 | outb(ap->ctl | ATA_SRST, ioaddr->ctl_addr); |
| 2208 | udelay(10); |
| 2209 | outb(ap->ctl, ioaddr->ctl_addr); |
| 2210 | } |
| 2211 | |
| 2212 | /* spec mandates ">= 2ms" before checking status. |
| 2213 | * We wait 150ms, because that was the magic delay used for |
| 2214 | * ATAPI devices in Hale Landis's ATADRVR, for the period of time |
| 2215 | * between when the ATA command register is written, and then |
| 2216 | * status is checked. Because waiting for "a while" before |
| 2217 | * checking status is fine, post SRST, we perform this magic |
| 2218 | * delay here as well. |
Alan Cox | 09c7ad7 | 2006-03-22 15:52:40 +0000 | [diff] [blame] | 2219 | * |
| 2220 | * Old drivers/ide uses the 2mS rule and then waits for ready |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2221 | */ |
| 2222 | msleep(150); |
| 2223 | |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 2224 | /* Before we perform post reset processing we want to see if |
Tejun Heo | 298a41c | 2006-03-25 02:58:13 +0900 | [diff] [blame] | 2225 | * the bus shows 0xFF because the odd clown forgets the D7 |
| 2226 | * pulldown resistor. |
| 2227 | */ |
Alan Cox | 09c7ad7 | 2006-03-22 15:52:40 +0000 | [diff] [blame] | 2228 | if (ata_check_status(ap) == 0xFF) |
Tejun Heo | 298a41c | 2006-03-25 02:58:13 +0900 | [diff] [blame] | 2229 | return AC_ERR_OTHER; |
Alan Cox | 09c7ad7 | 2006-03-22 15:52:40 +0000 | [diff] [blame] | 2230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | ata_bus_post_reset(ap, devmask); |
| 2232 | |
| 2233 | return 0; |
| 2234 | } |
| 2235 | |
| 2236 | /** |
| 2237 | * ata_bus_reset - reset host port and associated ATA channel |
| 2238 | * @ap: port to reset |
| 2239 | * |
| 2240 | * This is typically the first time we actually start issuing |
| 2241 | * commands to the ATA channel. We wait for BSY to clear, then |
| 2242 | * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its |
| 2243 | * result. Determine what devices, if any, are on the channel |
| 2244 | * by looking at the device 0/1 error register. Look at the signature |
| 2245 | * stored in each device's taskfile registers, to determine if |
| 2246 | * the device is ATA or ATAPI. |
| 2247 | * |
| 2248 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 2249 | * PCI/etc. bus probe sem. |
| 2250 | * Obtains host_set lock. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2251 | * |
| 2252 | * SIDE EFFECTS: |
Tejun Heo | 198e0fe | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 2253 | * Sets ATA_FLAG_DISABLED if bus reset fails. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | */ |
| 2255 | |
| 2256 | void ata_bus_reset(struct ata_port *ap) |
| 2257 | { |
| 2258 | struct ata_ioports *ioaddr = &ap->ioaddr; |
| 2259 | unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS; |
| 2260 | u8 err; |
Tejun Heo | aec5c3c | 2006-03-25 01:33:34 +0900 | [diff] [blame] | 2261 | unsigned int dev0, dev1 = 0, devmask = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | |
| 2263 | DPRINTK("ENTER, host %u, port %u\n", ap->id, ap->port_no); |
| 2264 | |
| 2265 | /* determine if device 0/1 are present */ |
| 2266 | if (ap->flags & ATA_FLAG_SATA_RESET) |
| 2267 | dev0 = 1; |
| 2268 | else { |
| 2269 | dev0 = ata_devchk(ap, 0); |
| 2270 | if (slave_possible) |
| 2271 | dev1 = ata_devchk(ap, 1); |
| 2272 | } |
| 2273 | |
| 2274 | if (dev0) |
| 2275 | devmask |= (1 << 0); |
| 2276 | if (dev1) |
| 2277 | devmask |= (1 << 1); |
| 2278 | |
| 2279 | /* select device 0 again */ |
| 2280 | ap->ops->dev_select(ap, 0); |
| 2281 | |
| 2282 | /* issue bus reset */ |
| 2283 | if (ap->flags & ATA_FLAG_SRST) |
Tejun Heo | aec5c3c | 2006-03-25 01:33:34 +0900 | [diff] [blame] | 2284 | if (ata_bus_softreset(ap, devmask)) |
| 2285 | goto err_out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2286 | |
| 2287 | /* |
| 2288 | * determine by signature whether we have ATA or ATAPI devices |
| 2289 | */ |
Tejun Heo | b4dc762 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2290 | ap->device[0].class = ata_dev_try_classify(ap, 0, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2291 | if ((slave_possible) && (err != 0x81)) |
Tejun Heo | b4dc762 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2292 | ap->device[1].class = ata_dev_try_classify(ap, 1, &err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2293 | |
| 2294 | /* re-enable interrupts */ |
| 2295 | if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */ |
| 2296 | ata_irq_on(ap); |
| 2297 | |
| 2298 | /* is double-select really necessary? */ |
| 2299 | if (ap->device[1].class != ATA_DEV_NONE) |
| 2300 | ap->ops->dev_select(ap, 1); |
| 2301 | if (ap->device[0].class != ATA_DEV_NONE) |
| 2302 | ap->ops->dev_select(ap, 0); |
| 2303 | |
| 2304 | /* if no devices were detected, disable this port */ |
| 2305 | if ((ap->device[0].class == ATA_DEV_NONE) && |
| 2306 | (ap->device[1].class == ATA_DEV_NONE)) |
| 2307 | goto err_out; |
| 2308 | |
| 2309 | if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) { |
| 2310 | /* set up device control for ATA_FLAG_SATA_RESET */ |
| 2311 | if (ap->flags & ATA_FLAG_MMIO) |
| 2312 | writeb(ap->ctl, (void __iomem *) ioaddr->ctl_addr); |
| 2313 | else |
| 2314 | outb(ap->ctl, ioaddr->ctl_addr); |
| 2315 | } |
| 2316 | |
| 2317 | DPRINTK("EXIT\n"); |
| 2318 | return; |
| 2319 | |
| 2320 | err_out: |
| 2321 | printk(KERN_ERR "ata%u: disabling port\n", ap->id); |
| 2322 | ap->ops->port_disable(ap); |
| 2323 | |
| 2324 | DPRINTK("EXIT\n"); |
| 2325 | } |
| 2326 | |
Tejun Heo | 7a7921e | 2006-02-02 18:20:00 +0900 | [diff] [blame] | 2327 | static int sata_phy_resume(struct ata_port *ap) |
| 2328 | { |
| 2329 | unsigned long timeout = jiffies + (HZ * 5); |
Tejun Heo | 852ee16 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2330 | u32 scontrol, sstatus; |
Tejun Heo | 7a7921e | 2006-02-02 18:20:00 +0900 | [diff] [blame] | 2331 | |
Tejun Heo | 852ee16 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2332 | scontrol = scr_read(ap, SCR_CONTROL); |
| 2333 | scontrol = (scontrol & 0x0f0) | 0x300; |
| 2334 | scr_write_flush(ap, SCR_CONTROL, scontrol); |
Tejun Heo | 7a7921e | 2006-02-02 18:20:00 +0900 | [diff] [blame] | 2335 | |
| 2336 | /* Wait for phy to become ready, if necessary. */ |
| 2337 | do { |
| 2338 | msleep(200); |
| 2339 | sstatus = scr_read(ap, SCR_STATUS); |
| 2340 | if ((sstatus & 0xf) != 1) |
| 2341 | return 0; |
| 2342 | } while (time_before(jiffies, timeout)); |
| 2343 | |
| 2344 | return -1; |
| 2345 | } |
| 2346 | |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2347 | /** |
Tejun Heo | 8a19ac8 | 2006-02-02 18:20:00 +0900 | [diff] [blame] | 2348 | * ata_std_probeinit - initialize probing |
| 2349 | * @ap: port to be probed |
| 2350 | * |
| 2351 | * @ap is about to be probed. Initialize it. This function is |
| 2352 | * to be used as standard callback for ata_drive_probe_reset(). |
Tejun Heo | 3a39746 | 2006-02-10 23:58:48 +0900 | [diff] [blame] | 2353 | * |
| 2354 | * NOTE!!! Do not use this function as probeinit if a low level |
| 2355 | * driver implements only hardreset. Just pass NULL as probeinit |
| 2356 | * in that case. Using this function is probably okay but doing |
| 2357 | * so makes reset sequence different from the original |
| 2358 | * ->phy_reset implementation and Jeff nervous. :-P |
Tejun Heo | 8a19ac8 | 2006-02-02 18:20:00 +0900 | [diff] [blame] | 2359 | */ |
Alan Cox | 17efc5f | 2006-03-27 19:01:32 +0100 | [diff] [blame] | 2360 | void ata_std_probeinit(struct ata_port *ap) |
Tejun Heo | 8a19ac8 | 2006-02-02 18:20:00 +0900 | [diff] [blame] | 2361 | { |
Alan Cox | 17efc5f | 2006-03-27 19:01:32 +0100 | [diff] [blame] | 2362 | if ((ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read) { |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 2363 | u32 spd; |
| 2364 | |
Tejun Heo | 8a19ac8 | 2006-02-02 18:20:00 +0900 | [diff] [blame] | 2365 | sata_phy_resume(ap); |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 2366 | |
| 2367 | spd = (scr_read(ap, SCR_CONTROL) & 0xf0) >> 4; |
| 2368 | if (spd) |
| 2369 | ap->sata_spd_limit &= (1 << spd) - 1; |
| 2370 | |
Tejun Heo | 3a39746 | 2006-02-10 23:58:48 +0900 | [diff] [blame] | 2371 | if (sata_dev_present(ap)) |
| 2372 | ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT); |
| 2373 | } |
Tejun Heo | 8a19ac8 | 2006-02-02 18:20:00 +0900 | [diff] [blame] | 2374 | } |
| 2375 | |
| 2376 | /** |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2377 | * ata_std_softreset - reset host port via ATA SRST |
| 2378 | * @ap: port to reset |
| 2379 | * @verbose: fail verbosely |
| 2380 | * @classes: resulting classes of attached devices |
| 2381 | * |
| 2382 | * Reset host port using ATA SRST. This function is to be used |
| 2383 | * as standard callback for ata_drive_*_reset() functions. |
| 2384 | * |
| 2385 | * LOCKING: |
| 2386 | * Kernel thread context (may sleep) |
| 2387 | * |
| 2388 | * RETURNS: |
| 2389 | * 0 on success, -errno otherwise. |
| 2390 | */ |
| 2391 | int ata_std_softreset(struct ata_port *ap, int verbose, unsigned int *classes) |
| 2392 | { |
| 2393 | unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS; |
| 2394 | unsigned int devmask = 0, err_mask; |
| 2395 | u8 err; |
| 2396 | |
| 2397 | DPRINTK("ENTER\n"); |
| 2398 | |
Tejun Heo | 3a39746 | 2006-02-10 23:58:48 +0900 | [diff] [blame] | 2399 | if (ap->ops->scr_read && !sata_dev_present(ap)) { |
| 2400 | classes[0] = ATA_DEV_NONE; |
| 2401 | goto out; |
| 2402 | } |
| 2403 | |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2404 | /* determine if device 0/1 are present */ |
| 2405 | if (ata_devchk(ap, 0)) |
| 2406 | devmask |= (1 << 0); |
| 2407 | if (slave_possible && ata_devchk(ap, 1)) |
| 2408 | devmask |= (1 << 1); |
| 2409 | |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2410 | /* select device 0 again */ |
| 2411 | ap->ops->dev_select(ap, 0); |
| 2412 | |
| 2413 | /* issue bus reset */ |
| 2414 | DPRINTK("about to softreset, devmask=%x\n", devmask); |
| 2415 | err_mask = ata_bus_softreset(ap, devmask); |
| 2416 | if (err_mask) { |
| 2417 | if (verbose) |
| 2418 | printk(KERN_ERR "ata%u: SRST failed (err_mask=0x%x)\n", |
| 2419 | ap->id, err_mask); |
| 2420 | else |
| 2421 | DPRINTK("EXIT, softreset failed (err_mask=0x%x)\n", |
| 2422 | err_mask); |
| 2423 | return -EIO; |
| 2424 | } |
| 2425 | |
| 2426 | /* determine by signature whether we have ATA or ATAPI devices */ |
| 2427 | classes[0] = ata_dev_try_classify(ap, 0, &err); |
| 2428 | if (slave_possible && err != 0x81) |
| 2429 | classes[1] = ata_dev_try_classify(ap, 1, &err); |
| 2430 | |
Tejun Heo | 3a39746 | 2006-02-10 23:58:48 +0900 | [diff] [blame] | 2431 | out: |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2432 | DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]); |
| 2433 | return 0; |
| 2434 | } |
| 2435 | |
| 2436 | /** |
| 2437 | * sata_std_hardreset - reset host port via SATA phy reset |
| 2438 | * @ap: port to reset |
| 2439 | * @verbose: fail verbosely |
| 2440 | * @class: resulting class of attached device |
| 2441 | * |
| 2442 | * SATA phy-reset host port using DET bits of SControl register. |
| 2443 | * This function is to be used as standard callback for |
| 2444 | * ata_drive_*_reset(). |
| 2445 | * |
| 2446 | * LOCKING: |
| 2447 | * Kernel thread context (may sleep) |
| 2448 | * |
| 2449 | * RETURNS: |
| 2450 | * 0 on success, -errno otherwise. |
| 2451 | */ |
| 2452 | int sata_std_hardreset(struct ata_port *ap, int verbose, unsigned int *class) |
| 2453 | { |
Tejun Heo | 852ee16 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2454 | u32 scontrol; |
| 2455 | |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2456 | DPRINTK("ENTER\n"); |
| 2457 | |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 2458 | if (ata_set_sata_spd_needed(ap)) { |
| 2459 | /* SATA spec says nothing about how to reconfigure |
| 2460 | * spd. To be on the safe side, turn off phy during |
| 2461 | * reconfiguration. This works for at least ICH7 AHCI |
| 2462 | * and Sil3124. |
| 2463 | */ |
| 2464 | scontrol = scr_read(ap, SCR_CONTROL); |
| 2465 | scontrol = (scontrol & 0x0f0) | 0x302; |
| 2466 | scr_write_flush(ap, SCR_CONTROL, scontrol); |
| 2467 | |
| 2468 | ata_set_sata_spd(ap); |
| 2469 | } |
| 2470 | |
| 2471 | /* issue phy wake/reset */ |
Tejun Heo | 852ee16 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2472 | scontrol = scr_read(ap, SCR_CONTROL); |
| 2473 | scontrol = (scontrol & 0x0f0) | 0x301; |
| 2474 | scr_write_flush(ap, SCR_CONTROL, scontrol); |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2475 | |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 2476 | /* Couldn't find anything in SATA I/II specs, but AHCI-1.1 |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2477 | * 10.4.2 says at least 1 ms. |
| 2478 | */ |
| 2479 | msleep(1); |
| 2480 | |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 2481 | /* bring phy back */ |
Tejun Heo | 7a7921e | 2006-02-02 18:20:00 +0900 | [diff] [blame] | 2482 | sata_phy_resume(ap); |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2483 | |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2484 | /* TODO: phy layer with polling, timeouts, etc. */ |
| 2485 | if (!sata_dev_present(ap)) { |
| 2486 | *class = ATA_DEV_NONE; |
| 2487 | DPRINTK("EXIT, link offline\n"); |
| 2488 | return 0; |
| 2489 | } |
| 2490 | |
| 2491 | if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) { |
| 2492 | if (verbose) |
| 2493 | printk(KERN_ERR "ata%u: COMRESET failed " |
| 2494 | "(device not ready)\n", ap->id); |
| 2495 | else |
| 2496 | DPRINTK("EXIT, device not ready\n"); |
| 2497 | return -EIO; |
| 2498 | } |
| 2499 | |
Tejun Heo | 3a39746 | 2006-02-10 23:58:48 +0900 | [diff] [blame] | 2500 | ap->ops->dev_select(ap, 0); /* probably unnecessary */ |
| 2501 | |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2502 | *class = ata_dev_try_classify(ap, 0, NULL); |
| 2503 | |
| 2504 | DPRINTK("EXIT, class=%u\n", *class); |
| 2505 | return 0; |
| 2506 | } |
| 2507 | |
| 2508 | /** |
| 2509 | * ata_std_postreset - standard postreset callback |
| 2510 | * @ap: the target ata_port |
| 2511 | * @classes: classes of attached devices |
| 2512 | * |
| 2513 | * This function is invoked after a successful reset. Note that |
| 2514 | * the device might have been reset more than once using |
| 2515 | * different reset methods before postreset is invoked. |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2516 | * |
| 2517 | * This function is to be used as standard callback for |
| 2518 | * ata_drive_*_reset(). |
| 2519 | * |
| 2520 | * LOCKING: |
| 2521 | * Kernel thread context (may sleep) |
| 2522 | */ |
| 2523 | void ata_std_postreset(struct ata_port *ap, unsigned int *classes) |
| 2524 | { |
| 2525 | DPRINTK("ENTER\n"); |
| 2526 | |
Tejun Heo | 56497bd | 2006-02-15 15:01:42 +0900 | [diff] [blame] | 2527 | /* set cable type if it isn't already set */ |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2528 | if (ap->cbl == ATA_CBL_NONE && ap->flags & ATA_FLAG_SATA) |
| 2529 | ap->cbl = ATA_CBL_SATA; |
| 2530 | |
| 2531 | /* print link status */ |
| 2532 | if (ap->cbl == ATA_CBL_SATA) |
| 2533 | sata_print_link_status(ap); |
| 2534 | |
Tejun Heo | 3a39746 | 2006-02-10 23:58:48 +0900 | [diff] [blame] | 2535 | /* re-enable interrupts */ |
| 2536 | if (ap->ioaddr.ctl_addr) /* FIXME: hack. create a hook instead */ |
| 2537 | ata_irq_on(ap); |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2538 | |
| 2539 | /* is double-select really necessary? */ |
| 2540 | if (classes[0] != ATA_DEV_NONE) |
| 2541 | ap->ops->dev_select(ap, 1); |
| 2542 | if (classes[1] != ATA_DEV_NONE) |
| 2543 | ap->ops->dev_select(ap, 0); |
| 2544 | |
Tejun Heo | 3a39746 | 2006-02-10 23:58:48 +0900 | [diff] [blame] | 2545 | /* bail out if no device is present */ |
| 2546 | if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) { |
| 2547 | DPRINTK("EXIT, no device\n"); |
| 2548 | return; |
| 2549 | } |
| 2550 | |
| 2551 | /* set up device control */ |
| 2552 | if (ap->ioaddr.ctl_addr) { |
| 2553 | if (ap->flags & ATA_FLAG_MMIO) |
| 2554 | writeb(ap->ctl, (void __iomem *) ap->ioaddr.ctl_addr); |
| 2555 | else |
| 2556 | outb(ap->ctl, ap->ioaddr.ctl_addr); |
| 2557 | } |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2558 | |
| 2559 | DPRINTK("EXIT\n"); |
| 2560 | } |
| 2561 | |
| 2562 | /** |
| 2563 | * ata_std_probe_reset - standard probe reset method |
| 2564 | * @ap: prot to perform probe-reset |
| 2565 | * @classes: resulting classes of attached devices |
| 2566 | * |
| 2567 | * The stock off-the-shelf ->probe_reset method. |
| 2568 | * |
| 2569 | * LOCKING: |
| 2570 | * Kernel thread context (may sleep) |
| 2571 | * |
| 2572 | * RETURNS: |
| 2573 | * 0 on success, -errno otherwise. |
| 2574 | */ |
| 2575 | int ata_std_probe_reset(struct ata_port *ap, unsigned int *classes) |
| 2576 | { |
| 2577 | ata_reset_fn_t hardreset; |
| 2578 | |
| 2579 | hardreset = NULL; |
Tejun Heo | b911fc3 | 2006-02-02 18:20:00 +0900 | [diff] [blame] | 2580 | if (ap->flags & ATA_FLAG_SATA && ap->ops->scr_read) |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2581 | hardreset = sata_std_hardreset; |
| 2582 | |
Tejun Heo | 8a19ac8 | 2006-02-02 18:20:00 +0900 | [diff] [blame] | 2583 | return ata_drive_probe_reset(ap, ata_std_probeinit, |
Tejun Heo | 7944ea9 | 2006-02-02 18:20:00 +0900 | [diff] [blame] | 2584 | ata_std_softreset, hardreset, |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2585 | ata_std_postreset, classes); |
| 2586 | } |
| 2587 | |
Tejun Heo | 1ad8e7f | 2006-04-02 18:51:53 +0900 | [diff] [blame^] | 2588 | int ata_do_reset(struct ata_port *ap, |
| 2589 | ata_reset_fn_t reset, ata_postreset_fn_t postreset, |
| 2590 | int verbose, unsigned int *classes) |
Tejun Heo | a62c0fc | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2591 | { |
| 2592 | int i, rc; |
| 2593 | |
| 2594 | for (i = 0; i < ATA_MAX_DEVICES; i++) |
| 2595 | classes[i] = ATA_DEV_UNKNOWN; |
| 2596 | |
Tejun Heo | 9974e7c | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 2597 | rc = reset(ap, verbose, classes); |
Tejun Heo | a62c0fc | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2598 | if (rc) |
| 2599 | return rc; |
| 2600 | |
| 2601 | /* If any class isn't ATA_DEV_UNKNOWN, consider classification |
| 2602 | * is complete and convert all ATA_DEV_UNKNOWN to |
| 2603 | * ATA_DEV_NONE. |
| 2604 | */ |
| 2605 | for (i = 0; i < ATA_MAX_DEVICES; i++) |
| 2606 | if (classes[i] != ATA_DEV_UNKNOWN) |
| 2607 | break; |
| 2608 | |
| 2609 | if (i < ATA_MAX_DEVICES) |
| 2610 | for (i = 0; i < ATA_MAX_DEVICES; i++) |
| 2611 | if (classes[i] == ATA_DEV_UNKNOWN) |
| 2612 | classes[i] = ATA_DEV_NONE; |
| 2613 | |
| 2614 | if (postreset) |
| 2615 | postreset(ap, classes); |
| 2616 | |
Tejun Heo | 9974e7c | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 2617 | return 0; |
Tejun Heo | a62c0fc | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2618 | } |
| 2619 | |
| 2620 | /** |
| 2621 | * ata_drive_probe_reset - Perform probe reset with given methods |
| 2622 | * @ap: port to reset |
Tejun Heo | 7944ea9 | 2006-02-02 18:20:00 +0900 | [diff] [blame] | 2623 | * @probeinit: probeinit method (can be NULL) |
Tejun Heo | a62c0fc | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2624 | * @softreset: softreset method (can be NULL) |
| 2625 | * @hardreset: hardreset method (can be NULL) |
| 2626 | * @postreset: postreset method (can be NULL) |
| 2627 | * @classes: resulting classes of attached devices |
| 2628 | * |
| 2629 | * Reset the specified port and classify attached devices using |
| 2630 | * given methods. This function prefers softreset but tries all |
| 2631 | * possible reset sequences to reset and classify devices. This |
| 2632 | * function is intended to be used for constructing ->probe_reset |
| 2633 | * callback by low level drivers. |
| 2634 | * |
| 2635 | * Reset methods should follow the following rules. |
| 2636 | * |
| 2637 | * - Return 0 on sucess, -errno on failure. |
| 2638 | * - If classification is supported, fill classes[] with |
| 2639 | * recognized class codes. |
| 2640 | * - If classification is not supported, leave classes[] alone. |
| 2641 | * - If verbose is non-zero, print error message on failure; |
| 2642 | * otherwise, shut up. |
| 2643 | * |
| 2644 | * LOCKING: |
| 2645 | * Kernel thread context (may sleep) |
| 2646 | * |
| 2647 | * RETURNS: |
| 2648 | * 0 on success, -EINVAL if no reset method is avaliable, -ENODEV |
| 2649 | * if classification fails, and any error code from reset |
| 2650 | * methods. |
| 2651 | */ |
Tejun Heo | 7944ea9 | 2006-02-02 18:20:00 +0900 | [diff] [blame] | 2652 | int ata_drive_probe_reset(struct ata_port *ap, ata_probeinit_fn_t probeinit, |
Tejun Heo | a62c0fc | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2653 | ata_reset_fn_t softreset, ata_reset_fn_t hardreset, |
| 2654 | ata_postreset_fn_t postreset, unsigned int *classes) |
| 2655 | { |
| 2656 | int rc = -EINVAL; |
| 2657 | |
Tejun Heo | 7944ea9 | 2006-02-02 18:20:00 +0900 | [diff] [blame] | 2658 | if (probeinit) |
| 2659 | probeinit(ap); |
| 2660 | |
Tejun Heo | 90dac02 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2661 | if (softreset && !ata_set_sata_spd_needed(ap)) { |
Tejun Heo | 9974e7c | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 2662 | rc = ata_do_reset(ap, softreset, postreset, 0, classes); |
| 2663 | if (rc == 0 && classes[0] != ATA_DEV_UNKNOWN) |
| 2664 | goto done; |
Tejun Heo | edbabd8 | 2006-04-02 20:55:02 +0900 | [diff] [blame] | 2665 | printk(KERN_INFO "ata%u: softreset failed, will try " |
| 2666 | "hardreset in 5 secs\n", ap->id); |
| 2667 | ssleep(5); |
Tejun Heo | a62c0fc | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2668 | } |
| 2669 | |
| 2670 | if (!hardreset) |
Tejun Heo | 9974e7c | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 2671 | goto done; |
Tejun Heo | a62c0fc | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2672 | |
Tejun Heo | 90dac02 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2673 | while (1) { |
| 2674 | rc = ata_do_reset(ap, hardreset, postreset, 0, classes); |
| 2675 | if (rc == 0) { |
| 2676 | if (classes[0] != ATA_DEV_UNKNOWN) |
| 2677 | goto done; |
| 2678 | break; |
| 2679 | } |
| 2680 | |
| 2681 | if (ata_down_sata_spd_limit(ap)) |
| 2682 | goto done; |
Tejun Heo | edbabd8 | 2006-04-02 20:55:02 +0900 | [diff] [blame] | 2683 | |
| 2684 | printk(KERN_INFO "ata%u: hardreset failed, will retry " |
| 2685 | "in 5 secs\n", ap->id); |
| 2686 | ssleep(5); |
Tejun Heo | 90dac02 | 2006-04-02 17:54:46 +0900 | [diff] [blame] | 2687 | } |
Tejun Heo | a62c0fc | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2688 | |
Tejun Heo | edbabd8 | 2006-04-02 20:55:02 +0900 | [diff] [blame] | 2689 | if (softreset) { |
| 2690 | printk(KERN_INFO "ata%u: hardreset succeeded without " |
| 2691 | "classification, will retry softreset in 5 secs\n", |
| 2692 | ap->id); |
| 2693 | ssleep(5); |
| 2694 | |
Tejun Heo | 9974e7c | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 2695 | rc = ata_do_reset(ap, softreset, postreset, 0, classes); |
Tejun Heo | edbabd8 | 2006-04-02 20:55:02 +0900 | [diff] [blame] | 2696 | } |
Tejun Heo | a62c0fc | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2697 | |
Tejun Heo | 9974e7c | 2006-04-01 01:38:17 +0900 | [diff] [blame] | 2698 | done: |
| 2699 | if (rc == 0 && classes[0] == ATA_DEV_UNKNOWN) |
| 2700 | rc = -ENODEV; |
Tejun Heo | a62c0fc | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 2701 | return rc; |
| 2702 | } |
| 2703 | |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 2704 | /** |
| 2705 | * ata_dev_same_device - Determine whether new ID matches configured device |
| 2706 | * @ap: port on which the device to compare against resides |
| 2707 | * @dev: device to compare against |
| 2708 | * @new_class: class of the new device |
| 2709 | * @new_id: IDENTIFY page of the new device |
| 2710 | * |
| 2711 | * Compare @new_class and @new_id against @dev and determine |
| 2712 | * whether @dev is the device indicated by @new_class and |
| 2713 | * @new_id. |
| 2714 | * |
| 2715 | * LOCKING: |
| 2716 | * None. |
| 2717 | * |
| 2718 | * RETURNS: |
| 2719 | * 1 if @dev matches @new_class and @new_id, 0 otherwise. |
| 2720 | */ |
| 2721 | static int ata_dev_same_device(struct ata_port *ap, struct ata_device *dev, |
| 2722 | unsigned int new_class, const u16 *new_id) |
| 2723 | { |
| 2724 | const u16 *old_id = dev->id; |
| 2725 | unsigned char model[2][41], serial[2][21]; |
| 2726 | u64 new_n_sectors; |
| 2727 | |
| 2728 | if (dev->class != new_class) { |
| 2729 | printk(KERN_INFO |
| 2730 | "ata%u: dev %u class mismatch %d != %d\n", |
| 2731 | ap->id, dev->devno, dev->class, new_class); |
| 2732 | return 0; |
| 2733 | } |
| 2734 | |
| 2735 | ata_id_c_string(old_id, model[0], ATA_ID_PROD_OFS, sizeof(model[0])); |
| 2736 | ata_id_c_string(new_id, model[1], ATA_ID_PROD_OFS, sizeof(model[1])); |
| 2737 | ata_id_c_string(old_id, serial[0], ATA_ID_SERNO_OFS, sizeof(serial[0])); |
| 2738 | ata_id_c_string(new_id, serial[1], ATA_ID_SERNO_OFS, sizeof(serial[1])); |
| 2739 | new_n_sectors = ata_id_n_sectors(new_id); |
| 2740 | |
| 2741 | if (strcmp(model[0], model[1])) { |
| 2742 | printk(KERN_INFO |
| 2743 | "ata%u: dev %u model number mismatch '%s' != '%s'\n", |
| 2744 | ap->id, dev->devno, model[0], model[1]); |
| 2745 | return 0; |
| 2746 | } |
| 2747 | |
| 2748 | if (strcmp(serial[0], serial[1])) { |
| 2749 | printk(KERN_INFO |
| 2750 | "ata%u: dev %u serial number mismatch '%s' != '%s'\n", |
| 2751 | ap->id, dev->devno, serial[0], serial[1]); |
| 2752 | return 0; |
| 2753 | } |
| 2754 | |
| 2755 | if (dev->class == ATA_DEV_ATA && dev->n_sectors != new_n_sectors) { |
| 2756 | printk(KERN_INFO |
| 2757 | "ata%u: dev %u n_sectors mismatch %llu != %llu\n", |
| 2758 | ap->id, dev->devno, (unsigned long long)dev->n_sectors, |
| 2759 | (unsigned long long)new_n_sectors); |
| 2760 | return 0; |
| 2761 | } |
| 2762 | |
| 2763 | return 1; |
| 2764 | } |
| 2765 | |
| 2766 | /** |
| 2767 | * ata_dev_revalidate - Revalidate ATA device |
| 2768 | * @ap: port on which the device to revalidate resides |
| 2769 | * @dev: device to revalidate |
| 2770 | * @post_reset: is this revalidation after reset? |
| 2771 | * |
| 2772 | * Re-read IDENTIFY page and make sure @dev is still attached to |
| 2773 | * the port. |
| 2774 | * |
| 2775 | * LOCKING: |
| 2776 | * Kernel thread context (may sleep) |
| 2777 | * |
| 2778 | * RETURNS: |
| 2779 | * 0 on success, negative errno otherwise |
| 2780 | */ |
| 2781 | int ata_dev_revalidate(struct ata_port *ap, struct ata_device *dev, |
| 2782 | int post_reset) |
| 2783 | { |
Tejun Heo | 5eb45c0 | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 2784 | unsigned int class = dev->class; |
| 2785 | u16 *id = NULL; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 2786 | int rc; |
| 2787 | |
Tejun Heo | 5eb45c0 | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 2788 | if (!ata_dev_enabled(dev)) { |
| 2789 | rc = -ENODEV; |
| 2790 | goto fail; |
| 2791 | } |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 2792 | |
| 2793 | /* allocate & read ID data */ |
| 2794 | rc = ata_dev_read_id(ap, dev, &class, post_reset, &id); |
| 2795 | if (rc) |
| 2796 | goto fail; |
| 2797 | |
| 2798 | /* is the device still there? */ |
| 2799 | if (!ata_dev_same_device(ap, dev, class, id)) { |
| 2800 | rc = -ENODEV; |
| 2801 | goto fail; |
| 2802 | } |
| 2803 | |
| 2804 | kfree(dev->id); |
| 2805 | dev->id = id; |
| 2806 | |
| 2807 | /* configure device according to the new ID */ |
Tejun Heo | 5eb45c0 | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 2808 | rc = ata_dev_configure(ap, dev, 0); |
| 2809 | if (rc == 0) |
| 2810 | return 0; |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 2811 | |
| 2812 | fail: |
| 2813 | printk(KERN_ERR "ata%u: dev %u revalidation failed (errno=%d)\n", |
| 2814 | ap->id, dev->devno, rc); |
| 2815 | kfree(id); |
| 2816 | return rc; |
| 2817 | } |
| 2818 | |
Arjan van de Ven | 98ac62d | 2005-11-28 10:06:23 +0100 | [diff] [blame] | 2819 | static const char * const ata_dma_blacklist [] = { |
Alan Cox | f4b15fe | 2006-03-22 15:54:04 +0000 | [diff] [blame] | 2820 | "WDC AC11000H", NULL, |
| 2821 | "WDC AC22100H", NULL, |
| 2822 | "WDC AC32500H", NULL, |
| 2823 | "WDC AC33100H", NULL, |
| 2824 | "WDC AC31600H", NULL, |
| 2825 | "WDC AC32100H", "24.09P07", |
| 2826 | "WDC AC23200L", "21.10N21", |
| 2827 | "Compaq CRD-8241B", NULL, |
| 2828 | "CRD-8400B", NULL, |
| 2829 | "CRD-8480B", NULL, |
| 2830 | "CRD-8482B", NULL, |
| 2831 | "CRD-84", NULL, |
| 2832 | "SanDisk SDP3B", NULL, |
| 2833 | "SanDisk SDP3B-64", NULL, |
| 2834 | "SANYO CD-ROM CRD", NULL, |
| 2835 | "HITACHI CDR-8", NULL, |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 2836 | "HITACHI CDR-8335", NULL, |
Alan Cox | f4b15fe | 2006-03-22 15:54:04 +0000 | [diff] [blame] | 2837 | "HITACHI CDR-8435", NULL, |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 2838 | "Toshiba CD-ROM XM-6202B", NULL, |
| 2839 | "TOSHIBA CD-ROM XM-1702BC", NULL, |
| 2840 | "CD-532E-A", NULL, |
| 2841 | "E-IDE CD-ROM CR-840", NULL, |
| 2842 | "CD-ROM Drive/F5A", NULL, |
| 2843 | "WPI CDD-820", NULL, |
Alan Cox | f4b15fe | 2006-03-22 15:54:04 +0000 | [diff] [blame] | 2844 | "SAMSUNG CD-ROM SC-148C", NULL, |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 2845 | "SAMSUNG CD-ROM SC", NULL, |
Alan Cox | f4b15fe | 2006-03-22 15:54:04 +0000 | [diff] [blame] | 2846 | "SanDisk SDP3B-64", NULL, |
| 2847 | "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL, |
| 2848 | "_NEC DV5800A", NULL, |
| 2849 | "SAMSUNG CD-ROM SN-124", "N001" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2850 | }; |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 2851 | |
Alan Cox | f4b15fe | 2006-03-22 15:54:04 +0000 | [diff] [blame] | 2852 | static int ata_strim(char *s, size_t len) |
| 2853 | { |
| 2854 | len = strnlen(s, len); |
| 2855 | |
| 2856 | /* ATAPI specifies that empty space is blank-filled; remove blanks */ |
| 2857 | while ((len > 0) && (s[len - 1] == ' ')) { |
| 2858 | len--; |
| 2859 | s[len] = 0; |
| 2860 | } |
| 2861 | return len; |
| 2862 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2863 | |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 2864 | static int ata_dma_blacklisted(const struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2865 | { |
Alan Cox | f4b15fe | 2006-03-22 15:54:04 +0000 | [diff] [blame] | 2866 | unsigned char model_num[40]; |
| 2867 | unsigned char model_rev[16]; |
| 2868 | unsigned int nlen, rlen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2869 | int i; |
| 2870 | |
Alan Cox | f4b15fe | 2006-03-22 15:54:04 +0000 | [diff] [blame] | 2871 | ata_id_string(dev->id, model_num, ATA_ID_PROD_OFS, |
| 2872 | sizeof(model_num)); |
| 2873 | ata_id_string(dev->id, model_rev, ATA_ID_FW_REV_OFS, |
| 2874 | sizeof(model_rev)); |
| 2875 | nlen = ata_strim(model_num, sizeof(model_num)); |
| 2876 | rlen = ata_strim(model_rev, sizeof(model_rev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2877 | |
Alan Cox | f4b15fe | 2006-03-22 15:54:04 +0000 | [diff] [blame] | 2878 | for (i = 0; i < ARRAY_SIZE(ata_dma_blacklist); i += 2) { |
| 2879 | if (!strncmp(ata_dma_blacklist[i], model_num, nlen)) { |
| 2880 | if (ata_dma_blacklist[i+1] == NULL) |
| 2881 | return 1; |
| 2882 | if (!strncmp(ata_dma_blacklist[i], model_rev, rlen)) |
| 2883 | return 1; |
| 2884 | } |
| 2885 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2886 | return 0; |
| 2887 | } |
| 2888 | |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 2889 | /** |
| 2890 | * ata_dev_xfermask - Compute supported xfermask of the given device |
| 2891 | * @ap: Port on which the device to compute xfermask for resides |
| 2892 | * @dev: Device to compute xfermask for |
| 2893 | * |
Tejun Heo | acf356b | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 2894 | * Compute supported xfermask of @dev and store it in |
| 2895 | * dev->*_mask. This function is responsible for applying all |
| 2896 | * known limits including host controller limits, device |
| 2897 | * blacklist, etc... |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 2898 | * |
Tejun Heo | 600511e | 2006-03-25 11:14:07 +0900 | [diff] [blame] | 2899 | * FIXME: The current implementation limits all transfer modes to |
| 2900 | * the fastest of the lowested device on the port. This is not |
Tejun Heo | 05c8e0a | 2006-03-25 14:28:57 +0900 | [diff] [blame] | 2901 | * required on most controllers. |
Tejun Heo | 600511e | 2006-03-25 11:14:07 +0900 | [diff] [blame] | 2902 | * |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 2903 | * LOCKING: |
| 2904 | * None. |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 2905 | */ |
Tejun Heo | acf356b | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 2906 | static void ata_dev_xfermask(struct ata_port *ap, struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2907 | { |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 2908 | struct ata_host_set *hs = ap->host_set; |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 2909 | unsigned long xfer_mask; |
| 2910 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2911 | |
Tejun Heo | 565083e | 2006-04-02 17:54:47 +0900 | [diff] [blame] | 2912 | xfer_mask = ata_pack_xfermask(ap->pio_mask, |
| 2913 | ap->mwdma_mask, ap->udma_mask); |
| 2914 | |
| 2915 | /* Apply cable rule here. Don't apply it early because when |
| 2916 | * we handle hot plug the cable type can itself change. |
| 2917 | */ |
| 2918 | if (ap->cbl == ATA_CBL_PATA40) |
| 2919 | xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 2921 | /* FIXME: Use port-wide xfermask for now */ |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 2922 | for (i = 0; i < ATA_MAX_DEVICES; i++) { |
| 2923 | struct ata_device *d = &ap->device[i]; |
Tejun Heo | 565083e | 2006-04-02 17:54:47 +0900 | [diff] [blame] | 2924 | |
| 2925 | if (ata_dev_absent(d)) |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 2926 | continue; |
Tejun Heo | 565083e | 2006-04-02 17:54:47 +0900 | [diff] [blame] | 2927 | |
| 2928 | if (ata_dev_disabled(d)) { |
| 2929 | /* to avoid violating device selection timing */ |
| 2930 | xfer_mask &= ata_pack_xfermask(d->pio_mask, |
| 2931 | UINT_MAX, UINT_MAX); |
| 2932 | continue; |
| 2933 | } |
| 2934 | |
| 2935 | xfer_mask &= ata_pack_xfermask(d->pio_mask, |
| 2936 | d->mwdma_mask, d->udma_mask); |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 2937 | xfer_mask &= ata_id_xfermask(d->id); |
| 2938 | if (ata_dma_blacklisted(d)) |
| 2939 | xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2940 | } |
| 2941 | |
Tejun Heo | a6d5a51 | 2006-03-06 04:31:57 +0900 | [diff] [blame] | 2942 | if (ata_dma_blacklisted(dev)) |
| 2943 | printk(KERN_WARNING "ata%u: dev %u is on DMA blacklist, " |
| 2944 | "disabling DMA\n", ap->id, dev->devno); |
| 2945 | |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 2946 | if (hs->flags & ATA_HOST_SIMPLEX) { |
| 2947 | if (hs->simplex_claimed) |
| 2948 | xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA); |
| 2949 | } |
Tejun Heo | 565083e | 2006-04-02 17:54:47 +0900 | [diff] [blame] | 2950 | |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 2951 | if (ap->ops->mode_filter) |
| 2952 | xfer_mask = ap->ops->mode_filter(ap, dev, xfer_mask); |
| 2953 | |
Tejun Heo | 565083e | 2006-04-02 17:54:47 +0900 | [diff] [blame] | 2954 | ata_unpack_xfermask(xfer_mask, &dev->pio_mask, |
| 2955 | &dev->mwdma_mask, &dev->udma_mask); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2956 | } |
| 2957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2958 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2959 | * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command |
| 2960 | * @ap: Port associated with device @dev |
| 2961 | * @dev: Device to which command will be sent |
| 2962 | * |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 2963 | * Issue SET FEATURES - XFER MODE command to device @dev |
| 2964 | * on port @ap. |
| 2965 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2966 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 2967 | * PCI/etc. bus probe sem. |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 2968 | * |
| 2969 | * RETURNS: |
| 2970 | * 0 on success, AC_ERR_* mask otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2971 | */ |
| 2972 | |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 2973 | static unsigned int ata_dev_set_xfermode(struct ata_port *ap, |
| 2974 | struct ata_device *dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | { |
Tejun Heo | a012370 | 2005-12-13 14:49:31 +0900 | [diff] [blame] | 2976 | struct ata_taskfile tf; |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 2977 | unsigned int err_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2978 | |
| 2979 | /* set up set-features taskfile */ |
| 2980 | DPRINTK("set features - xfer mode\n"); |
| 2981 | |
Tejun Heo | a012370 | 2005-12-13 14:49:31 +0900 | [diff] [blame] | 2982 | ata_tf_init(ap, &tf, dev->devno); |
| 2983 | tf.command = ATA_CMD_SET_FEATURES; |
| 2984 | tf.feature = SETFEATURES_XFER; |
| 2985 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 2986 | tf.protocol = ATA_PROT_NODATA; |
| 2987 | tf.nsect = dev->xfer_mode; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2988 | |
Tejun Heo | d69cf37 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 2989 | err_mask = ata_exec_internal(ap, dev, &tf, NULL, DMA_NONE, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | |
Tejun Heo | 83206a2 | 2006-03-24 15:25:31 +0900 | [diff] [blame] | 2991 | DPRINTK("EXIT, err_mask=%x\n", err_mask); |
| 2992 | return err_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | } |
| 2994 | |
| 2995 | /** |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 2996 | * ata_dev_init_params - Issue INIT DEV PARAMS command |
| 2997 | * @ap: Port associated with device @dev |
| 2998 | * @dev: Device to which command will be sent |
| 2999 | * |
| 3000 | * LOCKING: |
Tejun Heo | 6aff8f1 | 2006-02-15 18:24:09 +0900 | [diff] [blame] | 3001 | * Kernel thread context (may sleep) |
| 3002 | * |
| 3003 | * RETURNS: |
| 3004 | * 0 on success, AC_ERR_* mask otherwise. |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 3005 | */ |
| 3006 | |
Tejun Heo | 6aff8f1 | 2006-02-15 18:24:09 +0900 | [diff] [blame] | 3007 | static unsigned int ata_dev_init_params(struct ata_port *ap, |
Albert Lee | 00b6f5e | 2006-03-27 16:39:18 +0800 | [diff] [blame] | 3008 | struct ata_device *dev, |
| 3009 | u16 heads, |
| 3010 | u16 sectors) |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 3011 | { |
Tejun Heo | a012370 | 2005-12-13 14:49:31 +0900 | [diff] [blame] | 3012 | struct ata_taskfile tf; |
Tejun Heo | 6aff8f1 | 2006-02-15 18:24:09 +0900 | [diff] [blame] | 3013 | unsigned int err_mask; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 3014 | |
| 3015 | /* Number of sectors per track 1-255. Number of heads 1-16 */ |
| 3016 | if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16) |
Albert Lee | 00b6f5e | 2006-03-27 16:39:18 +0800 | [diff] [blame] | 3017 | return AC_ERR_INVALID; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 3018 | |
| 3019 | /* set up init dev params taskfile */ |
| 3020 | DPRINTK("init dev params \n"); |
| 3021 | |
Tejun Heo | a012370 | 2005-12-13 14:49:31 +0900 | [diff] [blame] | 3022 | ata_tf_init(ap, &tf, dev->devno); |
| 3023 | tf.command = ATA_CMD_INIT_DEV_PARAMS; |
| 3024 | tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 3025 | tf.protocol = ATA_PROT_NODATA; |
| 3026 | tf.nsect = sectors; |
| 3027 | tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */ |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 3028 | |
Tejun Heo | d69cf37 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3029 | err_mask = ata_exec_internal(ap, dev, &tf, NULL, DMA_NONE, NULL, 0); |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 3030 | |
Tejun Heo | 6aff8f1 | 2006-02-15 18:24:09 +0900 | [diff] [blame] | 3031 | DPRINTK("EXIT, err_mask=%x\n", err_mask); |
| 3032 | return err_mask; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 3033 | } |
| 3034 | |
| 3035 | /** |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 3036 | * ata_sg_clean - Unmap DMA memory associated with command |
| 3037 | * @qc: Command containing DMA memory to be released |
| 3038 | * |
| 3039 | * Unmap all mapped DMA memory associated with this command. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3040 | * |
| 3041 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 3042 | * spin_lock_irqsave(host_set lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3043 | */ |
| 3044 | |
| 3045 | static void ata_sg_clean(struct ata_queued_cmd *qc) |
| 3046 | { |
| 3047 | struct ata_port *ap = qc->ap; |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3048 | struct scatterlist *sg = qc->__sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3049 | int dir = qc->dma_dir; |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3050 | void *pad_buf = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3051 | |
Tejun Heo | a4631474 | 2006-02-11 19:11:13 +0900 | [diff] [blame] | 3052 | WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP)); |
| 3053 | WARN_ON(sg == NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3054 | |
| 3055 | if (qc->flags & ATA_QCFLAG_SINGLE) |
Jeff Garzik | f131883 | 2006-02-20 16:55:56 -0500 | [diff] [blame] | 3056 | WARN_ON(qc->n_elem > 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | |
Jeff Garzik | 2c13b7c | 2005-11-14 14:14:16 -0500 | [diff] [blame] | 3058 | VPRINTK("unmapping %u sg elements\n", qc->n_elem); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3059 | |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3060 | /* if we padded the buffer out to 32-bit bound, and data |
| 3061 | * xfer direction is from-device, we must copy from the |
| 3062 | * pad buffer back into the supplied buffer |
| 3063 | */ |
| 3064 | if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE)) |
| 3065 | pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ); |
| 3066 | |
| 3067 | if (qc->flags & ATA_QCFLAG_SG) { |
Jeff Garzik | e1410f2 | 2005-11-14 14:06:26 -0500 | [diff] [blame] | 3068 | if (qc->n_elem) |
Brian King | 2f1f610 | 2006-03-23 17:30:15 -0600 | [diff] [blame] | 3069 | dma_unmap_sg(ap->dev, sg, qc->n_elem, dir); |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3070 | /* restore last sg */ |
| 3071 | sg[qc->orig_n_elem - 1].length += qc->pad_len; |
| 3072 | if (pad_buf) { |
| 3073 | struct scatterlist *psg = &qc->pad_sgent; |
| 3074 | void *addr = kmap_atomic(psg->page, KM_IRQ0); |
| 3075 | memcpy(addr + psg->offset, pad_buf, qc->pad_len); |
Mark Lord | dfa1598 | 2005-12-12 23:19:28 -0500 | [diff] [blame] | 3076 | kunmap_atomic(addr, KM_IRQ0); |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3077 | } |
| 3078 | } else { |
Tejun Heo | 2e242fa | 2006-02-20 23:48:38 +0900 | [diff] [blame] | 3079 | if (qc->n_elem) |
Brian King | 2f1f610 | 2006-03-23 17:30:15 -0600 | [diff] [blame] | 3080 | dma_unmap_single(ap->dev, |
Jeff Garzik | e1410f2 | 2005-11-14 14:06:26 -0500 | [diff] [blame] | 3081 | sg_dma_address(&sg[0]), sg_dma_len(&sg[0]), |
| 3082 | dir); |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3083 | /* restore sg */ |
| 3084 | sg->length += qc->pad_len; |
| 3085 | if (pad_buf) |
| 3086 | memcpy(qc->buf_virt + sg->length - qc->pad_len, |
| 3087 | pad_buf, qc->pad_len); |
| 3088 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3089 | |
| 3090 | qc->flags &= ~ATA_QCFLAG_DMAMAP; |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3091 | qc->__sg = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3092 | } |
| 3093 | |
| 3094 | /** |
| 3095 | * ata_fill_sg - Fill PCI IDE PRD table |
| 3096 | * @qc: Metadata associated with taskfile to be transferred |
| 3097 | * |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 3098 | * Fill PCI IDE PRD (scatter-gather) table with segments |
| 3099 | * associated with the current disk command. |
| 3100 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3101 | * LOCKING: |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 3102 | * spin_lock_irqsave(host_set lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3103 | * |
| 3104 | */ |
| 3105 | static void ata_fill_sg(struct ata_queued_cmd *qc) |
| 3106 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3107 | struct ata_port *ap = qc->ap; |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3108 | struct scatterlist *sg; |
| 3109 | unsigned int idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3110 | |
Tejun Heo | a4631474 | 2006-02-11 19:11:13 +0900 | [diff] [blame] | 3111 | WARN_ON(qc->__sg == NULL); |
Jeff Garzik | f131883 | 2006-02-20 16:55:56 -0500 | [diff] [blame] | 3112 | WARN_ON(qc->n_elem == 0 && qc->pad_len == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3113 | |
| 3114 | idx = 0; |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3115 | ata_for_each_sg(sg, qc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3116 | u32 addr, offset; |
| 3117 | u32 sg_len, len; |
| 3118 | |
| 3119 | /* determine if physical DMA addr spans 64K boundary. |
| 3120 | * Note h/w doesn't support 64-bit, so we unconditionally |
| 3121 | * truncate dma_addr_t to u32. |
| 3122 | */ |
| 3123 | addr = (u32) sg_dma_address(sg); |
| 3124 | sg_len = sg_dma_len(sg); |
| 3125 | |
| 3126 | while (sg_len) { |
| 3127 | offset = addr & 0xffff; |
| 3128 | len = sg_len; |
| 3129 | if ((offset + sg_len) > 0x10000) |
| 3130 | len = 0x10000 - offset; |
| 3131 | |
| 3132 | ap->prd[idx].addr = cpu_to_le32(addr); |
| 3133 | ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff); |
| 3134 | VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len); |
| 3135 | |
| 3136 | idx++; |
| 3137 | sg_len -= len; |
| 3138 | addr += len; |
| 3139 | } |
| 3140 | } |
| 3141 | |
| 3142 | if (idx) |
| 3143 | ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT); |
| 3144 | } |
| 3145 | /** |
| 3146 | * ata_check_atapi_dma - Check whether ATAPI DMA can be supported |
| 3147 | * @qc: Metadata associated with taskfile to check |
| 3148 | * |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 3149 | * Allow low-level driver to filter ATA PACKET commands, returning |
| 3150 | * a status indicating whether or not it is OK to use DMA for the |
| 3151 | * supplied PACKET command. |
| 3152 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3153 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 3154 | * spin_lock_irqsave(host_set lock) |
| 3155 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3156 | * RETURNS: 0 when ATAPI DMA can be used |
| 3157 | * nonzero otherwise |
| 3158 | */ |
| 3159 | int ata_check_atapi_dma(struct ata_queued_cmd *qc) |
| 3160 | { |
| 3161 | struct ata_port *ap = qc->ap; |
| 3162 | int rc = 0; /* Assume ATAPI DMA is OK by default */ |
| 3163 | |
| 3164 | if (ap->ops->check_atapi_dma) |
| 3165 | rc = ap->ops->check_atapi_dma(qc); |
| 3166 | |
| 3167 | return rc; |
| 3168 | } |
| 3169 | /** |
| 3170 | * ata_qc_prep - Prepare taskfile for submission |
| 3171 | * @qc: Metadata associated with taskfile to be prepared |
| 3172 | * |
Jeff Garzik | 780a87f | 2005-05-30 15:41:05 -0400 | [diff] [blame] | 3173 | * Prepare ATA taskfile for submission. |
| 3174 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3175 | * LOCKING: |
| 3176 | * spin_lock_irqsave(host_set lock) |
| 3177 | */ |
| 3178 | void ata_qc_prep(struct ata_queued_cmd *qc) |
| 3179 | { |
| 3180 | if (!(qc->flags & ATA_QCFLAG_DMAMAP)) |
| 3181 | return; |
| 3182 | |
| 3183 | ata_fill_sg(qc); |
| 3184 | } |
| 3185 | |
Brian King | e46834c | 2006-03-17 17:04:03 -0600 | [diff] [blame] | 3186 | void ata_noop_qc_prep(struct ata_queued_cmd *qc) { } |
| 3187 | |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 3188 | /** |
| 3189 | * ata_sg_init_one - Associate command with memory buffer |
| 3190 | * @qc: Command to be associated |
| 3191 | * @buf: Memory buffer |
| 3192 | * @buflen: Length of memory buffer, in bytes. |
| 3193 | * |
| 3194 | * Initialize the data-related elements of queued_cmd @qc |
| 3195 | * to point to a single memory buffer, @buf of byte length @buflen. |
| 3196 | * |
| 3197 | * LOCKING: |
| 3198 | * spin_lock_irqsave(host_set lock) |
| 3199 | */ |
| 3200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3201 | void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen) |
| 3202 | { |
| 3203 | struct scatterlist *sg; |
| 3204 | |
| 3205 | qc->flags |= ATA_QCFLAG_SINGLE; |
| 3206 | |
| 3207 | memset(&qc->sgent, 0, sizeof(qc->sgent)); |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3208 | qc->__sg = &qc->sgent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3209 | qc->n_elem = 1; |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3210 | qc->orig_n_elem = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3211 | qc->buf_virt = buf; |
| 3212 | |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3213 | sg = qc->__sg; |
Jeff Garzik | f0612bb | 2005-10-30 01:58:18 -0500 | [diff] [blame] | 3214 | sg_init_one(sg, buf, buflen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3215 | } |
| 3216 | |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 3217 | /** |
| 3218 | * ata_sg_init - Associate command with scatter-gather table. |
| 3219 | * @qc: Command to be associated |
| 3220 | * @sg: Scatter-gather table. |
| 3221 | * @n_elem: Number of elements in s/g table. |
| 3222 | * |
| 3223 | * Initialize the data-related elements of queued_cmd @qc |
| 3224 | * to point to a scatter-gather table @sg, containing @n_elem |
| 3225 | * elements. |
| 3226 | * |
| 3227 | * LOCKING: |
| 3228 | * spin_lock_irqsave(host_set lock) |
| 3229 | */ |
| 3230 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3231 | void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg, |
| 3232 | unsigned int n_elem) |
| 3233 | { |
| 3234 | qc->flags |= ATA_QCFLAG_SG; |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3235 | qc->__sg = sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3236 | qc->n_elem = n_elem; |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3237 | qc->orig_n_elem = n_elem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3238 | } |
| 3239 | |
| 3240 | /** |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 3241 | * ata_sg_setup_one - DMA-map the memory buffer associated with a command. |
| 3242 | * @qc: Command with memory buffer to be mapped. |
| 3243 | * |
| 3244 | * DMA-map the memory buffer associated with queued_cmd @qc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3245 | * |
| 3246 | * LOCKING: |
| 3247 | * spin_lock_irqsave(host_set lock) |
| 3248 | * |
| 3249 | * RETURNS: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 3250 | * Zero on success, negative on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3251 | */ |
| 3252 | |
| 3253 | static int ata_sg_setup_one(struct ata_queued_cmd *qc) |
| 3254 | { |
| 3255 | struct ata_port *ap = qc->ap; |
| 3256 | int dir = qc->dma_dir; |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3257 | struct scatterlist *sg = qc->__sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3258 | dma_addr_t dma_address; |
Tejun Heo | 2e242fa | 2006-02-20 23:48:38 +0900 | [diff] [blame] | 3259 | int trim_sg = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3260 | |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3261 | /* we must lengthen transfers to end on a 32-bit boundary */ |
| 3262 | qc->pad_len = sg->length & 3; |
| 3263 | if (qc->pad_len) { |
| 3264 | void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ); |
| 3265 | struct scatterlist *psg = &qc->pad_sgent; |
| 3266 | |
Tejun Heo | a4631474 | 2006-02-11 19:11:13 +0900 | [diff] [blame] | 3267 | WARN_ON(qc->dev->class != ATA_DEV_ATAPI); |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3268 | |
| 3269 | memset(pad_buf, 0, ATA_DMA_PAD_SZ); |
| 3270 | |
| 3271 | if (qc->tf.flags & ATA_TFLAG_WRITE) |
| 3272 | memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len, |
| 3273 | qc->pad_len); |
| 3274 | |
| 3275 | sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ); |
| 3276 | sg_dma_len(psg) = ATA_DMA_PAD_SZ; |
| 3277 | /* trim sg */ |
| 3278 | sg->length -= qc->pad_len; |
Tejun Heo | 2e242fa | 2006-02-20 23:48:38 +0900 | [diff] [blame] | 3279 | if (sg->length == 0) |
| 3280 | trim_sg = 1; |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3281 | |
| 3282 | DPRINTK("padding done, sg->length=%u pad_len=%u\n", |
| 3283 | sg->length, qc->pad_len); |
| 3284 | } |
| 3285 | |
Tejun Heo | 2e242fa | 2006-02-20 23:48:38 +0900 | [diff] [blame] | 3286 | if (trim_sg) { |
| 3287 | qc->n_elem--; |
Jeff Garzik | e1410f2 | 2005-11-14 14:06:26 -0500 | [diff] [blame] | 3288 | goto skip_map; |
| 3289 | } |
| 3290 | |
Brian King | 2f1f610 | 2006-03-23 17:30:15 -0600 | [diff] [blame] | 3291 | dma_address = dma_map_single(ap->dev, qc->buf_virt, |
Albert Lee | 32529e0 | 2005-05-26 03:49:42 -0400 | [diff] [blame] | 3292 | sg->length, dir); |
Tejun Heo | 537a95d | 2005-11-05 14:29:01 -0500 | [diff] [blame] | 3293 | if (dma_mapping_error(dma_address)) { |
| 3294 | /* restore sg */ |
| 3295 | sg->length += qc->pad_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3296 | return -1; |
Tejun Heo | 537a95d | 2005-11-05 14:29:01 -0500 | [diff] [blame] | 3297 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3298 | |
| 3299 | sg_dma_address(sg) = dma_address; |
Albert Lee | 32529e0 | 2005-05-26 03:49:42 -0400 | [diff] [blame] | 3300 | sg_dma_len(sg) = sg->length; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3301 | |
Tejun Heo | 2e242fa | 2006-02-20 23:48:38 +0900 | [diff] [blame] | 3302 | skip_map: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3303 | DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg), |
| 3304 | qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read"); |
| 3305 | |
| 3306 | return 0; |
| 3307 | } |
| 3308 | |
| 3309 | /** |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 3310 | * ata_sg_setup - DMA-map the scatter-gather table associated with a command. |
| 3311 | * @qc: Command with scatter-gather table to be mapped. |
| 3312 | * |
| 3313 | * DMA-map the scatter-gather table associated with queued_cmd @qc. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3314 | * |
| 3315 | * LOCKING: |
| 3316 | * spin_lock_irqsave(host_set lock) |
| 3317 | * |
| 3318 | * RETURNS: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 3319 | * Zero on success, negative on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3320 | * |
| 3321 | */ |
| 3322 | |
| 3323 | static int ata_sg_setup(struct ata_queued_cmd *qc) |
| 3324 | { |
| 3325 | struct ata_port *ap = qc->ap; |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3326 | struct scatterlist *sg = qc->__sg; |
| 3327 | struct scatterlist *lsg = &sg[qc->n_elem - 1]; |
Jeff Garzik | e1410f2 | 2005-11-14 14:06:26 -0500 | [diff] [blame] | 3328 | int n_elem, pre_n_elem, dir, trim_sg = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3329 | |
| 3330 | VPRINTK("ENTER, ata%u\n", ap->id); |
Tejun Heo | a4631474 | 2006-02-11 19:11:13 +0900 | [diff] [blame] | 3331 | WARN_ON(!(qc->flags & ATA_QCFLAG_SG)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3332 | |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3333 | /* we must lengthen transfers to end on a 32-bit boundary */ |
| 3334 | qc->pad_len = lsg->length & 3; |
| 3335 | if (qc->pad_len) { |
| 3336 | void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ); |
| 3337 | struct scatterlist *psg = &qc->pad_sgent; |
| 3338 | unsigned int offset; |
| 3339 | |
Tejun Heo | a4631474 | 2006-02-11 19:11:13 +0900 | [diff] [blame] | 3340 | WARN_ON(qc->dev->class != ATA_DEV_ATAPI); |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3341 | |
| 3342 | memset(pad_buf, 0, ATA_DMA_PAD_SZ); |
| 3343 | |
| 3344 | /* |
| 3345 | * psg->page/offset are used to copy to-be-written |
| 3346 | * data in this function or read data in ata_sg_clean. |
| 3347 | */ |
| 3348 | offset = lsg->offset + lsg->length - qc->pad_len; |
| 3349 | psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT); |
| 3350 | psg->offset = offset_in_page(offset); |
| 3351 | |
| 3352 | if (qc->tf.flags & ATA_TFLAG_WRITE) { |
| 3353 | void *addr = kmap_atomic(psg->page, KM_IRQ0); |
| 3354 | memcpy(pad_buf, addr + psg->offset, qc->pad_len); |
Mark Lord | dfa1598 | 2005-12-12 23:19:28 -0500 | [diff] [blame] | 3355 | kunmap_atomic(addr, KM_IRQ0); |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3356 | } |
| 3357 | |
| 3358 | sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ); |
| 3359 | sg_dma_len(psg) = ATA_DMA_PAD_SZ; |
| 3360 | /* trim last sg */ |
| 3361 | lsg->length -= qc->pad_len; |
Jeff Garzik | e1410f2 | 2005-11-14 14:06:26 -0500 | [diff] [blame] | 3362 | if (lsg->length == 0) |
| 3363 | trim_sg = 1; |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3364 | |
| 3365 | DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n", |
| 3366 | qc->n_elem - 1, lsg->length, qc->pad_len); |
| 3367 | } |
| 3368 | |
Jeff Garzik | e1410f2 | 2005-11-14 14:06:26 -0500 | [diff] [blame] | 3369 | pre_n_elem = qc->n_elem; |
| 3370 | if (trim_sg && pre_n_elem) |
| 3371 | pre_n_elem--; |
| 3372 | |
| 3373 | if (!pre_n_elem) { |
| 3374 | n_elem = 0; |
| 3375 | goto skip_map; |
| 3376 | } |
| 3377 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3378 | dir = qc->dma_dir; |
Brian King | 2f1f610 | 2006-03-23 17:30:15 -0600 | [diff] [blame] | 3379 | n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir); |
Tejun Heo | 537a95d | 2005-11-05 14:29:01 -0500 | [diff] [blame] | 3380 | if (n_elem < 1) { |
| 3381 | /* restore last sg */ |
| 3382 | lsg->length += qc->pad_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3383 | return -1; |
Tejun Heo | 537a95d | 2005-11-05 14:29:01 -0500 | [diff] [blame] | 3384 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3385 | |
| 3386 | DPRINTK("%d sg elements mapped\n", n_elem); |
| 3387 | |
Jeff Garzik | e1410f2 | 2005-11-14 14:06:26 -0500 | [diff] [blame] | 3388 | skip_map: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3389 | qc->n_elem = n_elem; |
| 3390 | |
| 3391 | return 0; |
| 3392 | } |
| 3393 | |
| 3394 | /** |
Tejun Heo | 40e8c82 | 2005-08-22 17:12:45 +0900 | [diff] [blame] | 3395 | * ata_poll_qc_complete - turn irq back on and finish qc |
| 3396 | * @qc: Command to complete |
Randy Dunlap | 8e8b77d | 2005-11-01 21:29:27 -0800 | [diff] [blame] | 3397 | * @err_mask: ATA status register content |
Tejun Heo | 40e8c82 | 2005-08-22 17:12:45 +0900 | [diff] [blame] | 3398 | * |
| 3399 | * LOCKING: |
| 3400 | * None. (grabs host lock) |
| 3401 | */ |
| 3402 | |
Albert Lee | a22e2eb | 2005-12-05 15:38:02 +0800 | [diff] [blame] | 3403 | void ata_poll_qc_complete(struct ata_queued_cmd *qc) |
Tejun Heo | 40e8c82 | 2005-08-22 17:12:45 +0900 | [diff] [blame] | 3404 | { |
| 3405 | struct ata_port *ap = qc->ap; |
Jeff Garzik | b8f6153 | 2005-08-25 22:01:20 -0400 | [diff] [blame] | 3406 | unsigned long flags; |
Tejun Heo | 40e8c82 | 2005-08-22 17:12:45 +0900 | [diff] [blame] | 3407 | |
Jeff Garzik | b8f6153 | 2005-08-25 22:01:20 -0400 | [diff] [blame] | 3408 | spin_lock_irqsave(&ap->host_set->lock, flags); |
Tejun Heo | 40e8c82 | 2005-08-22 17:12:45 +0900 | [diff] [blame] | 3409 | ap->flags &= ~ATA_FLAG_NOINTR; |
| 3410 | ata_irq_on(ap); |
Albert Lee | a22e2eb | 2005-12-05 15:38:02 +0800 | [diff] [blame] | 3411 | ata_qc_complete(qc); |
Jeff Garzik | b8f6153 | 2005-08-25 22:01:20 -0400 | [diff] [blame] | 3412 | spin_unlock_irqrestore(&ap->host_set->lock, flags); |
Tejun Heo | 40e8c82 | 2005-08-22 17:12:45 +0900 | [diff] [blame] | 3413 | } |
| 3414 | |
| 3415 | /** |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 3416 | * ata_pio_poll - poll using PIO, depending on current state |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3417 | * @qc: qc in progress |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3418 | * |
| 3419 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 3420 | * None. (executing in kernel thread context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3421 | * |
| 3422 | * RETURNS: |
Randy Dunlap | 6f0ef4f | 2005-10-25 01:44:30 -0400 | [diff] [blame] | 3423 | * timeout value to use |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3424 | */ |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3425 | static unsigned long ata_pio_poll(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3426 | { |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3427 | struct ata_port *ap = qc->ap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3428 | u8 status; |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3429 | unsigned int poll_state = HSM_ST_UNKNOWN; |
| 3430 | unsigned int reg_state = HSM_ST_UNKNOWN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3431 | |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3432 | switch (ap->hsm_task_state) { |
| 3433 | case HSM_ST: |
| 3434 | case HSM_ST_POLL: |
| 3435 | poll_state = HSM_ST_POLL; |
| 3436 | reg_state = HSM_ST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3437 | break; |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3438 | case HSM_ST_LAST: |
| 3439 | case HSM_ST_LAST_POLL: |
| 3440 | poll_state = HSM_ST_LAST_POLL; |
| 3441 | reg_state = HSM_ST_LAST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3442 | break; |
| 3443 | default: |
| 3444 | BUG(); |
| 3445 | break; |
| 3446 | } |
| 3447 | |
| 3448 | status = ata_chk_status(ap); |
| 3449 | if (status & ATA_BUSY) { |
| 3450 | if (time_after(jiffies, ap->pio_task_timeout)) { |
Tejun Heo | 11a56d2 | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 3451 | qc->err_mask |= AC_ERR_TIMEOUT; |
Albert Lee | 7c39833 | 2005-11-09 13:03:30 +0800 | [diff] [blame] | 3452 | ap->hsm_task_state = HSM_ST_TMOUT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | return 0; |
| 3454 | } |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3455 | ap->hsm_task_state = poll_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3456 | return ATA_SHORT_PAUSE; |
| 3457 | } |
| 3458 | |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3459 | ap->hsm_task_state = reg_state; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3460 | return 0; |
| 3461 | } |
| 3462 | |
| 3463 | /** |
Randy Dunlap | 6f0ef4f | 2005-10-25 01:44:30 -0400 | [diff] [blame] | 3464 | * ata_pio_complete - check if drive is busy or idle |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3465 | * @qc: qc to complete |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3466 | * |
| 3467 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 3468 | * None. (executing in kernel thread context) |
Jeff Garzik | 7fb6ec2 | 2005-09-16 06:01:48 -0400 | [diff] [blame] | 3469 | * |
| 3470 | * RETURNS: |
| 3471 | * Non-zero if qc completed, zero otherwise. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3472 | */ |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3473 | static int ata_pio_complete(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3474 | { |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3475 | struct ata_port *ap = qc->ap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3476 | u8 drv_stat; |
| 3477 | |
| 3478 | /* |
Alan Cox | 31433ea | 2005-08-26 15:56:47 +0100 | [diff] [blame] | 3479 | * This is purely heuristic. This is a fast path. Sometimes when |
| 3480 | * we enter, BSY will be cleared in a chk-status or two. If not, |
| 3481 | * the drive is probably seeking or something. Snooze for a couple |
| 3482 | * msecs, then chk-status again. If still busy, fall back to |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3483 | * HSM_ST_POLL state. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3484 | */ |
Albert Lee | fe79e68 | 2005-12-06 11:34:59 +0800 | [diff] [blame] | 3485 | drv_stat = ata_busy_wait(ap, ATA_BUSY, 10); |
| 3486 | if (drv_stat & ATA_BUSY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3487 | msleep(2); |
Albert Lee | fe79e68 | 2005-12-06 11:34:59 +0800 | [diff] [blame] | 3488 | drv_stat = ata_busy_wait(ap, ATA_BUSY, 10); |
| 3489 | if (drv_stat & ATA_BUSY) { |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3490 | ap->hsm_task_state = HSM_ST_LAST_POLL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3491 | ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO; |
Jeff Garzik | 7fb6ec2 | 2005-09-16 06:01:48 -0400 | [diff] [blame] | 3492 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3493 | } |
| 3494 | } |
| 3495 | |
| 3496 | drv_stat = ata_wait_idle(ap); |
| 3497 | if (!ata_ok(drv_stat)) { |
Albert Lee | 1c84898 | 2005-12-05 15:40:15 +0800 | [diff] [blame] | 3498 | qc->err_mask |= __ac_err_mask(drv_stat); |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3499 | ap->hsm_task_state = HSM_ST_ERR; |
Jeff Garzik | 7fb6ec2 | 2005-09-16 06:01:48 -0400 | [diff] [blame] | 3500 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3501 | } |
| 3502 | |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3503 | ap->hsm_task_state = HSM_ST_IDLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3504 | |
Tejun Heo | a4631474 | 2006-02-11 19:11:13 +0900 | [diff] [blame] | 3505 | WARN_ON(qc->err_mask); |
Albert Lee | a22e2eb | 2005-12-05 15:38:02 +0800 | [diff] [blame] | 3506 | ata_poll_qc_complete(qc); |
Jeff Garzik | 7fb6ec2 | 2005-09-16 06:01:48 -0400 | [diff] [blame] | 3507 | |
| 3508 | /* another command may start at this point */ |
| 3509 | |
| 3510 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3511 | } |
| 3512 | |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 3513 | |
| 3514 | /** |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 3515 | * swap_buf_le16 - swap halves of 16-bit words in place |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 3516 | * @buf: Buffer to swap |
| 3517 | * @buf_words: Number of 16-bit words in buffer. |
| 3518 | * |
| 3519 | * Swap halves of 16-bit words if needed to convert from |
| 3520 | * little-endian byte order to native cpu byte order, or |
| 3521 | * vice-versa. |
| 3522 | * |
| 3523 | * LOCKING: |
Randy Dunlap | 6f0ef4f | 2005-10-25 01:44:30 -0400 | [diff] [blame] | 3524 | * Inherited from caller. |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 3525 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3526 | void swap_buf_le16(u16 *buf, unsigned int buf_words) |
| 3527 | { |
| 3528 | #ifdef __BIG_ENDIAN |
| 3529 | unsigned int i; |
| 3530 | |
| 3531 | for (i = 0; i < buf_words; i++) |
| 3532 | buf[i] = le16_to_cpu(buf[i]); |
| 3533 | #endif /* __BIG_ENDIAN */ |
| 3534 | } |
| 3535 | |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 3536 | /** |
| 3537 | * ata_mmio_data_xfer - Transfer data by MMIO |
| 3538 | * @ap: port to read/write |
| 3539 | * @buf: data buffer |
| 3540 | * @buflen: buffer length |
Jeff Garzik | 344baba | 2005-09-07 01:15:17 -0400 | [diff] [blame] | 3541 | * @write_data: read/write |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 3542 | * |
| 3543 | * Transfer data from/to the device data register by MMIO. |
| 3544 | * |
| 3545 | * LOCKING: |
| 3546 | * Inherited from caller. |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 3547 | */ |
| 3548 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3549 | static void ata_mmio_data_xfer(struct ata_port *ap, unsigned char *buf, |
| 3550 | unsigned int buflen, int write_data) |
| 3551 | { |
| 3552 | unsigned int i; |
| 3553 | unsigned int words = buflen >> 1; |
| 3554 | u16 *buf16 = (u16 *) buf; |
| 3555 | void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr; |
| 3556 | |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 3557 | /* Transfer multiple of 2 bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3558 | if (write_data) { |
| 3559 | for (i = 0; i < words; i++) |
| 3560 | writew(le16_to_cpu(buf16[i]), mmio); |
| 3561 | } else { |
| 3562 | for (i = 0; i < words; i++) |
| 3563 | buf16[i] = cpu_to_le16(readw(mmio)); |
| 3564 | } |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 3565 | |
| 3566 | /* Transfer trailing 1 byte, if any. */ |
| 3567 | if (unlikely(buflen & 0x01)) { |
| 3568 | u16 align_buf[1] = { 0 }; |
| 3569 | unsigned char *trailing_buf = buf + buflen - 1; |
| 3570 | |
| 3571 | if (write_data) { |
| 3572 | memcpy(align_buf, trailing_buf, 1); |
| 3573 | writew(le16_to_cpu(align_buf[0]), mmio); |
| 3574 | } else { |
| 3575 | align_buf[0] = cpu_to_le16(readw(mmio)); |
| 3576 | memcpy(trailing_buf, align_buf, 1); |
| 3577 | } |
| 3578 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3579 | } |
| 3580 | |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 3581 | /** |
| 3582 | * ata_pio_data_xfer - Transfer data by PIO |
| 3583 | * @ap: port to read/write |
| 3584 | * @buf: data buffer |
| 3585 | * @buflen: buffer length |
Jeff Garzik | 344baba | 2005-09-07 01:15:17 -0400 | [diff] [blame] | 3586 | * @write_data: read/write |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 3587 | * |
| 3588 | * Transfer data from/to the device data register by PIO. |
| 3589 | * |
| 3590 | * LOCKING: |
| 3591 | * Inherited from caller. |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 3592 | */ |
| 3593 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3594 | static void ata_pio_data_xfer(struct ata_port *ap, unsigned char *buf, |
| 3595 | unsigned int buflen, int write_data) |
| 3596 | { |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 3597 | unsigned int words = buflen >> 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3598 | |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 3599 | /* Transfer multiple of 2 bytes */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3600 | if (write_data) |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 3601 | outsw(ap->ioaddr.data_addr, buf, words); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3602 | else |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 3603 | insw(ap->ioaddr.data_addr, buf, words); |
| 3604 | |
| 3605 | /* Transfer trailing 1 byte, if any. */ |
| 3606 | if (unlikely(buflen & 0x01)) { |
| 3607 | u16 align_buf[1] = { 0 }; |
| 3608 | unsigned char *trailing_buf = buf + buflen - 1; |
| 3609 | |
| 3610 | if (write_data) { |
| 3611 | memcpy(align_buf, trailing_buf, 1); |
| 3612 | outw(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr); |
| 3613 | } else { |
| 3614 | align_buf[0] = cpu_to_le16(inw(ap->ioaddr.data_addr)); |
| 3615 | memcpy(trailing_buf, align_buf, 1); |
| 3616 | } |
| 3617 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3618 | } |
| 3619 | |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 3620 | /** |
| 3621 | * ata_data_xfer - Transfer data from/to the data register. |
| 3622 | * @ap: port to read/write |
| 3623 | * @buf: data buffer |
| 3624 | * @buflen: buffer length |
| 3625 | * @do_write: read/write |
| 3626 | * |
| 3627 | * Transfer data from/to the device data register. |
| 3628 | * |
| 3629 | * LOCKING: |
| 3630 | * Inherited from caller. |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 3631 | */ |
| 3632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3633 | static void ata_data_xfer(struct ata_port *ap, unsigned char *buf, |
| 3634 | unsigned int buflen, int do_write) |
| 3635 | { |
Alan Cox | a1bd9e6 | 2006-01-17 20:53:50 +0000 | [diff] [blame] | 3636 | /* Make the crap hardware pay the costs not the good stuff */ |
| 3637 | if (unlikely(ap->flags & ATA_FLAG_IRQ_MASK)) { |
| 3638 | unsigned long flags; |
| 3639 | local_irq_save(flags); |
| 3640 | if (ap->flags & ATA_FLAG_MMIO) |
| 3641 | ata_mmio_data_xfer(ap, buf, buflen, do_write); |
| 3642 | else |
| 3643 | ata_pio_data_xfer(ap, buf, buflen, do_write); |
| 3644 | local_irq_restore(flags); |
| 3645 | } else { |
| 3646 | if (ap->flags & ATA_FLAG_MMIO) |
| 3647 | ata_mmio_data_xfer(ap, buf, buflen, do_write); |
| 3648 | else |
| 3649 | ata_pio_data_xfer(ap, buf, buflen, do_write); |
| 3650 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3651 | } |
| 3652 | |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 3653 | /** |
| 3654 | * ata_pio_sector - Transfer ATA_SECT_SIZE (512 bytes) of data. |
| 3655 | * @qc: Command on going |
| 3656 | * |
| 3657 | * Transfer ATA_SECT_SIZE of data from/to the ATA device. |
| 3658 | * |
| 3659 | * LOCKING: |
| 3660 | * Inherited from caller. |
| 3661 | */ |
| 3662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3663 | static void ata_pio_sector(struct ata_queued_cmd *qc) |
| 3664 | { |
| 3665 | int do_write = (qc->tf.flags & ATA_TFLAG_WRITE); |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3666 | struct scatterlist *sg = qc->__sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3667 | struct ata_port *ap = qc->ap; |
| 3668 | struct page *page; |
| 3669 | unsigned int offset; |
| 3670 | unsigned char *buf; |
| 3671 | |
| 3672 | if (qc->cursect == (qc->nsect - 1)) |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3673 | ap->hsm_task_state = HSM_ST_LAST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3674 | |
| 3675 | page = sg[qc->cursg].page; |
| 3676 | offset = sg[qc->cursg].offset + qc->cursg_ofs * ATA_SECT_SIZE; |
| 3677 | |
| 3678 | /* get the current page and offset */ |
| 3679 | page = nth_page(page, (offset >> PAGE_SHIFT)); |
| 3680 | offset %= PAGE_SIZE; |
| 3681 | |
| 3682 | buf = kmap(page) + offset; |
| 3683 | |
| 3684 | qc->cursect++; |
| 3685 | qc->cursg_ofs++; |
| 3686 | |
Albert Lee | 32529e0 | 2005-05-26 03:49:42 -0400 | [diff] [blame] | 3687 | if ((qc->cursg_ofs * ATA_SECT_SIZE) == (&sg[qc->cursg])->length) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3688 | qc->cursg++; |
| 3689 | qc->cursg_ofs = 0; |
| 3690 | } |
| 3691 | |
| 3692 | DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read"); |
| 3693 | |
| 3694 | /* do the actual data transfer */ |
| 3695 | do_write = (qc->tf.flags & ATA_TFLAG_WRITE); |
| 3696 | ata_data_xfer(ap, buf, ATA_SECT_SIZE, do_write); |
| 3697 | |
| 3698 | kunmap(page); |
| 3699 | } |
| 3700 | |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 3701 | /** |
| 3702 | * __atapi_pio_bytes - Transfer data from/to the ATAPI device. |
| 3703 | * @qc: Command on going |
| 3704 | * @bytes: number of bytes |
| 3705 | * |
| 3706 | * Transfer Transfer data from/to the ATAPI device. |
| 3707 | * |
| 3708 | * LOCKING: |
| 3709 | * Inherited from caller. |
| 3710 | * |
| 3711 | */ |
| 3712 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3713 | static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes) |
| 3714 | { |
| 3715 | int do_write = (qc->tf.flags & ATA_TFLAG_WRITE); |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3716 | struct scatterlist *sg = qc->__sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3717 | struct ata_port *ap = qc->ap; |
| 3718 | struct page *page; |
| 3719 | unsigned char *buf; |
| 3720 | unsigned int offset, count; |
| 3721 | |
Albert Lee | 563a6e1 | 2005-08-12 14:17:50 +0800 | [diff] [blame] | 3722 | if (qc->curbytes + bytes >= qc->nbytes) |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3723 | ap->hsm_task_state = HSM_ST_LAST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3724 | |
| 3725 | next_sg: |
Albert Lee | 563a6e1 | 2005-08-12 14:17:50 +0800 | [diff] [blame] | 3726 | if (unlikely(qc->cursg >= qc->n_elem)) { |
Jeff Garzik | 7fb6ec2 | 2005-09-16 06:01:48 -0400 | [diff] [blame] | 3727 | /* |
Albert Lee | 563a6e1 | 2005-08-12 14:17:50 +0800 | [diff] [blame] | 3728 | * The end of qc->sg is reached and the device expects |
| 3729 | * more data to transfer. In order not to overrun qc->sg |
| 3730 | * and fulfill length specified in the byte count register, |
| 3731 | * - for read case, discard trailing data from the device |
| 3732 | * - for write case, padding zero data to the device |
| 3733 | */ |
| 3734 | u16 pad_buf[1] = { 0 }; |
| 3735 | unsigned int words = bytes >> 1; |
| 3736 | unsigned int i; |
| 3737 | |
| 3738 | if (words) /* warning if bytes > 1 */ |
Jeff Garzik | 7fb6ec2 | 2005-09-16 06:01:48 -0400 | [diff] [blame] | 3739 | printk(KERN_WARNING "ata%u: %u bytes trailing data\n", |
Albert Lee | 563a6e1 | 2005-08-12 14:17:50 +0800 | [diff] [blame] | 3740 | ap->id, bytes); |
| 3741 | |
| 3742 | for (i = 0; i < words; i++) |
| 3743 | ata_data_xfer(ap, (unsigned char*)pad_buf, 2, do_write); |
| 3744 | |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3745 | ap->hsm_task_state = HSM_ST_LAST; |
Albert Lee | 563a6e1 | 2005-08-12 14:17:50 +0800 | [diff] [blame] | 3746 | return; |
| 3747 | } |
| 3748 | |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 3749 | sg = &qc->__sg[qc->cursg]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3750 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3751 | page = sg->page; |
| 3752 | offset = sg->offset + qc->cursg_ofs; |
| 3753 | |
| 3754 | /* get the current page and offset */ |
| 3755 | page = nth_page(page, (offset >> PAGE_SHIFT)); |
| 3756 | offset %= PAGE_SIZE; |
| 3757 | |
Albert Lee | 6952df0 | 2005-06-06 15:56:03 +0800 | [diff] [blame] | 3758 | /* don't overrun current sg */ |
Albert Lee | 32529e0 | 2005-05-26 03:49:42 -0400 | [diff] [blame] | 3759 | count = min(sg->length - qc->cursg_ofs, bytes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3760 | |
| 3761 | /* don't cross page boundaries */ |
| 3762 | count = min(count, (unsigned int)PAGE_SIZE - offset); |
| 3763 | |
| 3764 | buf = kmap(page) + offset; |
| 3765 | |
| 3766 | bytes -= count; |
| 3767 | qc->curbytes += count; |
| 3768 | qc->cursg_ofs += count; |
| 3769 | |
Albert Lee | 32529e0 | 2005-05-26 03:49:42 -0400 | [diff] [blame] | 3770 | if (qc->cursg_ofs == sg->length) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3771 | qc->cursg++; |
| 3772 | qc->cursg_ofs = 0; |
| 3773 | } |
| 3774 | |
| 3775 | DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read"); |
| 3776 | |
| 3777 | /* do the actual data transfer */ |
| 3778 | ata_data_xfer(ap, buf, count, do_write); |
| 3779 | |
| 3780 | kunmap(page); |
| 3781 | |
Albert Lee | 563a6e1 | 2005-08-12 14:17:50 +0800 | [diff] [blame] | 3782 | if (bytes) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3783 | goto next_sg; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3784 | } |
| 3785 | |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 3786 | /** |
| 3787 | * atapi_pio_bytes - Transfer data from/to the ATAPI device. |
| 3788 | * @qc: Command on going |
| 3789 | * |
| 3790 | * Transfer Transfer data from/to the ATAPI device. |
| 3791 | * |
| 3792 | * LOCKING: |
| 3793 | * Inherited from caller. |
Albert Lee | 6ae4cfb | 2005-08-12 14:15:34 +0800 | [diff] [blame] | 3794 | */ |
| 3795 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3796 | static void atapi_pio_bytes(struct ata_queued_cmd *qc) |
| 3797 | { |
| 3798 | struct ata_port *ap = qc->ap; |
| 3799 | struct ata_device *dev = qc->dev; |
| 3800 | unsigned int ireason, bc_lo, bc_hi, bytes; |
| 3801 | int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0; |
| 3802 | |
| 3803 | ap->ops->tf_read(ap, &qc->tf); |
| 3804 | ireason = qc->tf.nsect; |
| 3805 | bc_lo = qc->tf.lbam; |
| 3806 | bc_hi = qc->tf.lbah; |
| 3807 | bytes = (bc_hi << 8) | bc_lo; |
| 3808 | |
| 3809 | /* shall be cleared to zero, indicating xfer of data */ |
| 3810 | if (ireason & (1 << 0)) |
| 3811 | goto err_out; |
| 3812 | |
| 3813 | /* make sure transfer direction matches expected */ |
| 3814 | i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0; |
| 3815 | if (do_write != i_write) |
| 3816 | goto err_out; |
| 3817 | |
| 3818 | __atapi_pio_bytes(qc, bytes); |
| 3819 | |
| 3820 | return; |
| 3821 | |
| 3822 | err_out: |
| 3823 | printk(KERN_INFO "ata%u: dev %u: ATAPI check failed\n", |
| 3824 | ap->id, dev->devno); |
Tejun Heo | 11a56d2 | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 3825 | qc->err_mask |= AC_ERR_HSM; |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3826 | ap->hsm_task_state = HSM_ST_ERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3827 | } |
| 3828 | |
| 3829 | /** |
Randy Dunlap | 6f0ef4f | 2005-10-25 01:44:30 -0400 | [diff] [blame] | 3830 | * ata_pio_block - start PIO on a block |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3831 | * @qc: qc to transfer block for |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3832 | * |
| 3833 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 3834 | * None. (executing in kernel thread context) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3835 | */ |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3836 | static void ata_pio_block(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3837 | { |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3838 | struct ata_port *ap = qc->ap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3839 | u8 status; |
| 3840 | |
| 3841 | /* |
Randy Dunlap | 6f0ef4f | 2005-10-25 01:44:30 -0400 | [diff] [blame] | 3842 | * This is purely heuristic. This is a fast path. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3843 | * Sometimes when we enter, BSY will be cleared in |
| 3844 | * a chk-status or two. If not, the drive is probably seeking |
| 3845 | * or something. Snooze for a couple msecs, then |
| 3846 | * chk-status again. If still busy, fall back to |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3847 | * HSM_ST_POLL state. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3848 | */ |
| 3849 | status = ata_busy_wait(ap, ATA_BUSY, 5); |
| 3850 | if (status & ATA_BUSY) { |
| 3851 | msleep(2); |
| 3852 | status = ata_busy_wait(ap, ATA_BUSY, 10); |
| 3853 | if (status & ATA_BUSY) { |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3854 | ap->hsm_task_state = HSM_ST_POLL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3855 | ap->pio_task_timeout = jiffies + ATA_TMOUT_PIO; |
| 3856 | return; |
| 3857 | } |
| 3858 | } |
| 3859 | |
Albert Lee | fe79e68 | 2005-12-06 11:34:59 +0800 | [diff] [blame] | 3860 | /* check error */ |
| 3861 | if (status & (ATA_ERR | ATA_DF)) { |
| 3862 | qc->err_mask |= AC_ERR_DEV; |
| 3863 | ap->hsm_task_state = HSM_ST_ERR; |
| 3864 | return; |
| 3865 | } |
| 3866 | |
| 3867 | /* transfer data if any */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3868 | if (is_atapi_taskfile(&qc->tf)) { |
Albert Lee | fe79e68 | 2005-12-06 11:34:59 +0800 | [diff] [blame] | 3869 | /* DRQ=0 means no more data to transfer */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3870 | if ((status & ATA_DRQ) == 0) { |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3871 | ap->hsm_task_state = HSM_ST_LAST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3872 | return; |
| 3873 | } |
| 3874 | |
| 3875 | atapi_pio_bytes(qc); |
| 3876 | } else { |
| 3877 | /* handle BSY=0, DRQ=0 as error */ |
| 3878 | if ((status & ATA_DRQ) == 0) { |
Tejun Heo | 11a56d2 | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 3879 | qc->err_mask |= AC_ERR_HSM; |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3880 | ap->hsm_task_state = HSM_ST_ERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3881 | return; |
| 3882 | } |
| 3883 | |
| 3884 | ata_pio_sector(qc); |
| 3885 | } |
| 3886 | } |
| 3887 | |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3888 | static void ata_pio_error(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3889 | { |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3890 | struct ata_port *ap = qc->ap; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3891 | |
Albert Lee | 0565c26 | 2006-02-13 18:55:25 +0800 | [diff] [blame] | 3892 | if (qc->tf.command != ATA_CMD_PACKET) |
Tejun Heo | d63cb4a | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 3893 | printk(KERN_WARNING "ata%u: dev %u PIO error\n", |
| 3894 | ap->id, qc->dev->devno); |
Albert Lee | 0565c26 | 2006-02-13 18:55:25 +0800 | [diff] [blame] | 3895 | |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 3896 | /* make sure qc->err_mask is available to |
Albert Lee | 1c84898 | 2005-12-05 15:40:15 +0800 | [diff] [blame] | 3897 | * know what's wrong and recover |
| 3898 | */ |
Tejun Heo | a4631474 | 2006-02-11 19:11:13 +0900 | [diff] [blame] | 3899 | WARN_ON(qc->err_mask == 0); |
Albert Lee | 1c84898 | 2005-12-05 15:40:15 +0800 | [diff] [blame] | 3900 | |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3901 | ap->hsm_task_state = HSM_ST_IDLE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3902 | |
Albert Lee | a22e2eb | 2005-12-05 15:38:02 +0800 | [diff] [blame] | 3903 | ata_poll_qc_complete(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3904 | } |
| 3905 | |
| 3906 | static void ata_pio_task(void *_data) |
| 3907 | { |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3908 | struct ata_queued_cmd *qc = _data; |
| 3909 | struct ata_port *ap = qc->ap; |
Jeff Garzik | 7fb6ec2 | 2005-09-16 06:01:48 -0400 | [diff] [blame] | 3910 | unsigned long timeout; |
| 3911 | int qc_completed; |
| 3912 | |
| 3913 | fsm_start: |
| 3914 | timeout = 0; |
| 3915 | qc_completed = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3916 | |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3917 | switch (ap->hsm_task_state) { |
| 3918 | case HSM_ST_IDLE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3919 | return; |
| 3920 | |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3921 | case HSM_ST: |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3922 | ata_pio_block(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3923 | break; |
| 3924 | |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3925 | case HSM_ST_LAST: |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3926 | qc_completed = ata_pio_complete(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3927 | break; |
| 3928 | |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3929 | case HSM_ST_POLL: |
| 3930 | case HSM_ST_LAST_POLL: |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3931 | timeout = ata_pio_poll(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3932 | break; |
| 3933 | |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 3934 | case HSM_ST_TMOUT: |
| 3935 | case HSM_ST_ERR: |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3936 | ata_pio_error(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3937 | return; |
| 3938 | } |
| 3939 | |
| 3940 | if (timeout) |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3941 | ata_port_queue_task(ap, ata_pio_task, qc, timeout); |
Jeff Garzik | 7fb6ec2 | 2005-09-16 06:01:48 -0400 | [diff] [blame] | 3942 | else if (!qc_completed) |
| 3943 | goto fsm_start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | } |
| 3945 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3946 | /** |
Tejun Heo | 8061f5f | 2006-03-05 15:29:09 +0900 | [diff] [blame] | 3947 | * atapi_packet_task - Write CDB bytes to hardware |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3948 | * @_data: qc in progress |
Tejun Heo | 8061f5f | 2006-03-05 15:29:09 +0900 | [diff] [blame] | 3949 | * |
| 3950 | * When device has indicated its readiness to accept |
| 3951 | * a CDB, this function is called. Send the CDB. |
| 3952 | * If DMA is to be performed, exit immediately. |
| 3953 | * Otherwise, we are in polling mode, so poll |
| 3954 | * status under operation succeeds or fails. |
| 3955 | * |
| 3956 | * LOCKING: |
| 3957 | * Kernel thread context (may sleep) |
| 3958 | */ |
Tejun Heo | 8061f5f | 2006-03-05 15:29:09 +0900 | [diff] [blame] | 3959 | static void atapi_packet_task(void *_data) |
| 3960 | { |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 3961 | struct ata_queued_cmd *qc = _data; |
| 3962 | struct ata_port *ap = qc->ap; |
Tejun Heo | 8061f5f | 2006-03-05 15:29:09 +0900 | [diff] [blame] | 3963 | u8 status; |
| 3964 | |
Tejun Heo | 8061f5f | 2006-03-05 15:29:09 +0900 | [diff] [blame] | 3965 | /* sleep-wait for BSY to clear */ |
| 3966 | DPRINTK("busy wait\n"); |
| 3967 | if (ata_busy_sleep(ap, ATA_TMOUT_CDB_QUICK, ATA_TMOUT_CDB)) { |
| 3968 | qc->err_mask |= AC_ERR_TIMEOUT; |
| 3969 | goto err_out; |
| 3970 | } |
| 3971 | |
| 3972 | /* make sure DRQ is set */ |
| 3973 | status = ata_chk_status(ap); |
| 3974 | if ((status & (ATA_BUSY | ATA_DRQ)) != ATA_DRQ) { |
| 3975 | qc->err_mask |= AC_ERR_HSM; |
| 3976 | goto err_out; |
| 3977 | } |
| 3978 | |
| 3979 | /* send SCSI cdb */ |
| 3980 | DPRINTK("send cdb\n"); |
| 3981 | WARN_ON(qc->dev->cdb_len < 12); |
| 3982 | |
| 3983 | if (qc->tf.protocol == ATA_PROT_ATAPI_DMA || |
| 3984 | qc->tf.protocol == ATA_PROT_ATAPI_NODATA) { |
| 3985 | unsigned long flags; |
| 3986 | |
| 3987 | /* Once we're done issuing command and kicking bmdma, |
| 3988 | * irq handler takes over. To not lose irq, we need |
| 3989 | * to clear NOINTR flag before sending cdb, but |
| 3990 | * interrupt handler shouldn't be invoked before we're |
| 3991 | * finished. Hence, the following locking. |
| 3992 | */ |
| 3993 | spin_lock_irqsave(&ap->host_set->lock, flags); |
| 3994 | ap->flags &= ~ATA_FLAG_NOINTR; |
| 3995 | ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1); |
| 3996 | if (qc->tf.protocol == ATA_PROT_ATAPI_DMA) |
| 3997 | ap->ops->bmdma_start(qc); /* initiate bmdma */ |
| 3998 | spin_unlock_irqrestore(&ap->host_set->lock, flags); |
| 3999 | } else { |
| 4000 | ata_data_xfer(ap, qc->cdb, qc->dev->cdb_len, 1); |
| 4001 | |
| 4002 | /* PIO commands are handled by polling */ |
| 4003 | ap->hsm_task_state = HSM_ST; |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 4004 | ata_port_queue_task(ap, ata_pio_task, qc, 0); |
Tejun Heo | 8061f5f | 2006-03-05 15:29:09 +0900 | [diff] [blame] | 4005 | } |
| 4006 | |
| 4007 | return; |
| 4008 | |
| 4009 | err_out: |
| 4010 | ata_poll_qc_complete(qc); |
| 4011 | } |
| 4012 | |
| 4013 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4014 | * ata_qc_new - Request an available ATA command, for queueing |
| 4015 | * @ap: Port associated with device @dev |
| 4016 | * @dev: Device from whom we request an available command structure |
| 4017 | * |
| 4018 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4019 | * None. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4020 | */ |
| 4021 | |
| 4022 | static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap) |
| 4023 | { |
| 4024 | struct ata_queued_cmd *qc = NULL; |
| 4025 | unsigned int i; |
| 4026 | |
| 4027 | for (i = 0; i < ATA_MAX_QUEUE; i++) |
| 4028 | if (!test_and_set_bit(i, &ap->qactive)) { |
| 4029 | qc = ata_qc_from_tag(ap, i); |
| 4030 | break; |
| 4031 | } |
| 4032 | |
| 4033 | if (qc) |
| 4034 | qc->tag = i; |
| 4035 | |
| 4036 | return qc; |
| 4037 | } |
| 4038 | |
| 4039 | /** |
| 4040 | * ata_qc_new_init - Request an available ATA command, and initialize it |
| 4041 | * @ap: Port associated with device @dev |
| 4042 | * @dev: Device from whom we request an available command structure |
| 4043 | * |
| 4044 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4045 | * None. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4046 | */ |
| 4047 | |
| 4048 | struct ata_queued_cmd *ata_qc_new_init(struct ata_port *ap, |
| 4049 | struct ata_device *dev) |
| 4050 | { |
| 4051 | struct ata_queued_cmd *qc; |
| 4052 | |
| 4053 | qc = ata_qc_new(ap); |
| 4054 | if (qc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4055 | qc->scsicmd = NULL; |
| 4056 | qc->ap = ap; |
| 4057 | qc->dev = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4058 | |
Jeff Garzik | 2c13b7c | 2005-11-14 14:14:16 -0500 | [diff] [blame] | 4059 | ata_qc_reinit(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4060 | } |
| 4061 | |
| 4062 | return qc; |
| 4063 | } |
| 4064 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4065 | /** |
| 4066 | * ata_qc_free - free unused ata_queued_cmd |
| 4067 | * @qc: Command to complete |
| 4068 | * |
| 4069 | * Designed to free unused ata_queued_cmd object |
| 4070 | * in case something prevents using it. |
| 4071 | * |
| 4072 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4073 | * spin_lock_irqsave(host_set lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4074 | */ |
| 4075 | void ata_qc_free(struct ata_queued_cmd *qc) |
| 4076 | { |
Tejun Heo | 4ba946e | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 4077 | struct ata_port *ap = qc->ap; |
| 4078 | unsigned int tag; |
| 4079 | |
Tejun Heo | a4631474 | 2006-02-11 19:11:13 +0900 | [diff] [blame] | 4080 | WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4081 | |
Tejun Heo | 4ba946e | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 4082 | qc->flags = 0; |
| 4083 | tag = qc->tag; |
| 4084 | if (likely(ata_tag_valid(tag))) { |
| 4085 | if (tag == ap->active_tag) |
| 4086 | ap->active_tag = ATA_TAG_POISON; |
| 4087 | qc->tag = ATA_TAG_POISON; |
| 4088 | clear_bit(tag, &ap->qactive); |
| 4089 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4090 | } |
| 4091 | |
Tejun Heo | 7601442 | 2006-02-11 15:13:49 +0900 | [diff] [blame] | 4092 | void __ata_qc_complete(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4093 | { |
Tejun Heo | a4631474 | 2006-02-11 19:11:13 +0900 | [diff] [blame] | 4094 | WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */ |
| 4095 | WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4096 | |
| 4097 | if (likely(qc->flags & ATA_QCFLAG_DMAMAP)) |
| 4098 | ata_sg_clean(qc); |
| 4099 | |
Albert Lee | 3f3791d | 2005-08-16 14:25:38 +0800 | [diff] [blame] | 4100 | /* atapi: mark qc as inactive to prevent the interrupt handler |
| 4101 | * from completing the command twice later, before the error handler |
| 4102 | * is called. (when rc != 0 and atapi request sense is needed) |
| 4103 | */ |
| 4104 | qc->flags &= ~ATA_QCFLAG_ACTIVE; |
| 4105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4106 | /* call completion callback */ |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 4107 | qc->complete_fn(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4108 | } |
| 4109 | |
| 4110 | static inline int ata_should_dma_map(struct ata_queued_cmd *qc) |
| 4111 | { |
| 4112 | struct ata_port *ap = qc->ap; |
| 4113 | |
| 4114 | switch (qc->tf.protocol) { |
| 4115 | case ATA_PROT_DMA: |
| 4116 | case ATA_PROT_ATAPI_DMA: |
| 4117 | return 1; |
| 4118 | |
| 4119 | case ATA_PROT_ATAPI: |
| 4120 | case ATA_PROT_PIO: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4121 | if (ap->flags & ATA_FLAG_PIO_DMA) |
| 4122 | return 1; |
| 4123 | |
| 4124 | /* fall through */ |
| 4125 | |
| 4126 | default: |
| 4127 | return 0; |
| 4128 | } |
| 4129 | |
| 4130 | /* never reached */ |
| 4131 | } |
| 4132 | |
| 4133 | /** |
| 4134 | * ata_qc_issue - issue taskfile to device |
| 4135 | * @qc: command to issue to device |
| 4136 | * |
| 4137 | * Prepare an ATA command to submission to device. |
| 4138 | * This includes mapping the data into a DMA-able |
| 4139 | * area, filling in the S/G table, and finally |
| 4140 | * writing the taskfile to hardware, starting the command. |
| 4141 | * |
| 4142 | * LOCKING: |
| 4143 | * spin_lock_irqsave(host_set lock) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4144 | */ |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 4145 | void ata_qc_issue(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4146 | { |
| 4147 | struct ata_port *ap = qc->ap; |
| 4148 | |
Tejun Heo | e4a70e7 | 2006-03-31 20:36:47 +0900 | [diff] [blame] | 4149 | qc->ap->active_tag = qc->tag; |
| 4150 | qc->flags |= ATA_QCFLAG_ACTIVE; |
| 4151 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4152 | if (ata_should_dma_map(qc)) { |
| 4153 | if (qc->flags & ATA_QCFLAG_SG) { |
| 4154 | if (ata_sg_setup(qc)) |
Tejun Heo | 8e436af | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 4155 | goto sg_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4156 | } else if (qc->flags & ATA_QCFLAG_SINGLE) { |
| 4157 | if (ata_sg_setup_one(qc)) |
Tejun Heo | 8e436af | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 4158 | goto sg_err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4159 | } |
| 4160 | } else { |
| 4161 | qc->flags &= ~ATA_QCFLAG_DMAMAP; |
| 4162 | } |
| 4163 | |
| 4164 | ap->ops->qc_prep(qc); |
| 4165 | |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 4166 | qc->err_mask |= ap->ops->qc_issue(qc); |
| 4167 | if (unlikely(qc->err_mask)) |
| 4168 | goto err; |
| 4169 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4170 | |
Tejun Heo | 8e436af | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 4171 | sg_err: |
| 4172 | qc->flags &= ~ATA_QCFLAG_DMAMAP; |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 4173 | qc->err_mask |= AC_ERR_SYSTEM; |
| 4174 | err: |
| 4175 | ata_qc_complete(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4176 | } |
| 4177 | |
| 4178 | /** |
| 4179 | * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner |
| 4180 | * @qc: command to issue to device |
| 4181 | * |
| 4182 | * Using various libata functions and hooks, this function |
| 4183 | * starts an ATA command. ATA commands are grouped into |
| 4184 | * classes called "protocols", and issuing each type of protocol |
| 4185 | * is slightly different. |
| 4186 | * |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 4187 | * May be used as the qc_issue() entry in ata_port_operations. |
| 4188 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4189 | * LOCKING: |
| 4190 | * spin_lock_irqsave(host_set lock) |
| 4191 | * |
| 4192 | * RETURNS: |
Tejun Heo | 9a3d9eb | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 4193 | * Zero on success, AC_ERR_* mask on failure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4194 | */ |
| 4195 | |
Tejun Heo | 9a3d9eb | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 4196 | unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4197 | { |
| 4198 | struct ata_port *ap = qc->ap; |
| 4199 | |
| 4200 | ata_dev_select(ap, qc->dev->devno, 1, 0); |
| 4201 | |
| 4202 | switch (qc->tf.protocol) { |
| 4203 | case ATA_PROT_NODATA: |
Jeff Garzik | e533825 | 2005-10-30 21:37:17 -0500 | [diff] [blame] | 4204 | ata_tf_to_host(ap, &qc->tf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4205 | break; |
| 4206 | |
| 4207 | case ATA_PROT_DMA: |
| 4208 | ap->ops->tf_load(ap, &qc->tf); /* load tf registers */ |
| 4209 | ap->ops->bmdma_setup(qc); /* set up bmdma */ |
| 4210 | ap->ops->bmdma_start(qc); /* initiate bmdma */ |
| 4211 | break; |
| 4212 | |
| 4213 | case ATA_PROT_PIO: /* load tf registers, initiate polling pio */ |
| 4214 | ata_qc_set_polling(qc); |
Jeff Garzik | e533825 | 2005-10-30 21:37:17 -0500 | [diff] [blame] | 4215 | ata_tf_to_host(ap, &qc->tf); |
Albert Lee | 14be71f | 2005-09-27 17:36:35 +0800 | [diff] [blame] | 4216 | ap->hsm_task_state = HSM_ST; |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 4217 | ata_port_queue_task(ap, ata_pio_task, qc, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4218 | break; |
| 4219 | |
| 4220 | case ATA_PROT_ATAPI: |
| 4221 | ata_qc_set_polling(qc); |
Jeff Garzik | e533825 | 2005-10-30 21:37:17 -0500 | [diff] [blame] | 4222 | ata_tf_to_host(ap, &qc->tf); |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 4223 | ata_port_queue_task(ap, atapi_packet_task, qc, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4224 | break; |
| 4225 | |
| 4226 | case ATA_PROT_ATAPI_NODATA: |
Tejun Heo | c138950 | 2005-08-22 14:59:24 +0900 | [diff] [blame] | 4227 | ap->flags |= ATA_FLAG_NOINTR; |
Jeff Garzik | e533825 | 2005-10-30 21:37:17 -0500 | [diff] [blame] | 4228 | ata_tf_to_host(ap, &qc->tf); |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 4229 | ata_port_queue_task(ap, atapi_packet_task, qc, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4230 | break; |
| 4231 | |
| 4232 | case ATA_PROT_ATAPI_DMA: |
Tejun Heo | c138950 | 2005-08-22 14:59:24 +0900 | [diff] [blame] | 4233 | ap->flags |= ATA_FLAG_NOINTR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4234 | ap->ops->tf_load(ap, &qc->tf); /* load tf registers */ |
| 4235 | ap->ops->bmdma_setup(qc); /* set up bmdma */ |
Tejun Heo | c91af2c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 4236 | ata_port_queue_task(ap, atapi_packet_task, qc, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4237 | break; |
| 4238 | |
| 4239 | default: |
| 4240 | WARN_ON(1); |
Tejun Heo | 9a3d9eb | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 4241 | return AC_ERR_SYSTEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4242 | } |
| 4243 | |
| 4244 | return 0; |
| 4245 | } |
| 4246 | |
| 4247 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4248 | * ata_host_intr - Handle host interrupt for given (port, task) |
| 4249 | * @ap: Port on which interrupt arrived (possibly...) |
| 4250 | * @qc: Taskfile currently active in engine |
| 4251 | * |
| 4252 | * Handle host interrupt for given queued command. Currently, |
| 4253 | * only DMA interrupts are handled. All other commands are |
| 4254 | * handled via polling with interrupts disabled (nIEN bit). |
| 4255 | * |
| 4256 | * LOCKING: |
| 4257 | * spin_lock_irqsave(host_set lock) |
| 4258 | * |
| 4259 | * RETURNS: |
| 4260 | * One if interrupt was handled, zero if not (shared irq). |
| 4261 | */ |
| 4262 | |
| 4263 | inline unsigned int ata_host_intr (struct ata_port *ap, |
| 4264 | struct ata_queued_cmd *qc) |
| 4265 | { |
| 4266 | u8 status, host_stat; |
| 4267 | |
| 4268 | switch (qc->tf.protocol) { |
| 4269 | |
| 4270 | case ATA_PROT_DMA: |
| 4271 | case ATA_PROT_ATAPI_DMA: |
| 4272 | case ATA_PROT_ATAPI: |
| 4273 | /* check status of DMA engine */ |
| 4274 | host_stat = ap->ops->bmdma_status(ap); |
| 4275 | VPRINTK("ata%u: host_stat 0x%X\n", ap->id, host_stat); |
| 4276 | |
| 4277 | /* if it's not our irq... */ |
| 4278 | if (!(host_stat & ATA_DMA_INTR)) |
| 4279 | goto idle_irq; |
| 4280 | |
| 4281 | /* before we do anything else, clear DMA-Start bit */ |
Alan Cox | b73fc89 | 2005-08-26 16:03:19 +0100 | [diff] [blame] | 4282 | ap->ops->bmdma_stop(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4283 | |
| 4284 | /* fall through */ |
| 4285 | |
| 4286 | case ATA_PROT_ATAPI_NODATA: |
| 4287 | case ATA_PROT_NODATA: |
| 4288 | /* check altstatus */ |
| 4289 | status = ata_altstatus(ap); |
| 4290 | if (status & ATA_BUSY) |
| 4291 | goto idle_irq; |
| 4292 | |
| 4293 | /* check main status, clearing INTRQ */ |
| 4294 | status = ata_chk_status(ap); |
| 4295 | if (unlikely(status & ATA_BUSY)) |
| 4296 | goto idle_irq; |
| 4297 | DPRINTK("ata%u: protocol %d (dev_stat 0x%X)\n", |
| 4298 | ap->id, qc->tf.protocol, status); |
| 4299 | |
| 4300 | /* ack bmdma irq events */ |
| 4301 | ap->ops->irq_clear(ap); |
| 4302 | |
| 4303 | /* complete taskfile transaction */ |
Albert Lee | a22e2eb | 2005-12-05 15:38:02 +0800 | [diff] [blame] | 4304 | qc->err_mask |= ac_err_mask(status); |
| 4305 | ata_qc_complete(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | break; |
| 4307 | |
| 4308 | default: |
| 4309 | goto idle_irq; |
| 4310 | } |
| 4311 | |
| 4312 | return 1; /* irq handled */ |
| 4313 | |
| 4314 | idle_irq: |
| 4315 | ap->stats.idle_irq++; |
| 4316 | |
| 4317 | #ifdef ATA_IRQ_TRAP |
| 4318 | if ((ap->stats.idle_irq % 1000) == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4319 | ata_irq_ack(ap, 0); /* debug trap */ |
| 4320 | printk(KERN_WARNING "ata%d: irq trap\n", ap->id); |
Alan Cox | 23cfce8 | 2006-03-21 16:06:53 +0000 | [diff] [blame] | 4321 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4322 | } |
| 4323 | #endif |
| 4324 | return 0; /* irq not handled */ |
| 4325 | } |
| 4326 | |
| 4327 | /** |
| 4328 | * ata_interrupt - Default ATA host interrupt handler |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4329 | * @irq: irq line (unused) |
| 4330 | * @dev_instance: pointer to our ata_host_set information structure |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4331 | * @regs: unused |
| 4332 | * |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4333 | * Default interrupt handler for PCI IDE devices. Calls |
| 4334 | * ata_host_intr() for each port that is not disabled. |
| 4335 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4336 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4337 | * Obtains host_set lock during operation. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4338 | * |
| 4339 | * RETURNS: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4340 | * IRQ_NONE or IRQ_HANDLED. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4341 | */ |
| 4342 | |
| 4343 | irqreturn_t ata_interrupt (int irq, void *dev_instance, struct pt_regs *regs) |
| 4344 | { |
| 4345 | struct ata_host_set *host_set = dev_instance; |
| 4346 | unsigned int i; |
| 4347 | unsigned int handled = 0; |
| 4348 | unsigned long flags; |
| 4349 | |
| 4350 | /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */ |
| 4351 | spin_lock_irqsave(&host_set->lock, flags); |
| 4352 | |
| 4353 | for (i = 0; i < host_set->n_ports; i++) { |
| 4354 | struct ata_port *ap; |
| 4355 | |
| 4356 | ap = host_set->ports[i]; |
Tejun Heo | c138950 | 2005-08-22 14:59:24 +0900 | [diff] [blame] | 4357 | if (ap && |
Tejun Heo | 198e0fe | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 4358 | !(ap->flags & (ATA_FLAG_DISABLED | ATA_FLAG_NOINTR))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4359 | struct ata_queued_cmd *qc; |
| 4360 | |
| 4361 | qc = ata_qc_from_tag(ap, ap->active_tag); |
Albert Lee | 21b1ed7 | 2005-04-29 17:34:59 +0800 | [diff] [blame] | 4362 | if (qc && (!(qc->tf.ctl & ATA_NIEN)) && |
| 4363 | (qc->flags & ATA_QCFLAG_ACTIVE)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4364 | handled |= ata_host_intr(ap, qc); |
| 4365 | } |
| 4366 | } |
| 4367 | |
| 4368 | spin_unlock_irqrestore(&host_set->lock, flags); |
| 4369 | |
| 4370 | return IRQ_RETVAL(handled); |
| 4371 | } |
| 4372 | |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 4373 | |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 4374 | /* |
| 4375 | * Execute a 'simple' command, that only consists of the opcode 'cmd' itself, |
| 4376 | * without filling any other registers |
| 4377 | */ |
| 4378 | static int ata_do_simple_cmd(struct ata_port *ap, struct ata_device *dev, |
| 4379 | u8 cmd) |
| 4380 | { |
| 4381 | struct ata_taskfile tf; |
| 4382 | int err; |
| 4383 | |
| 4384 | ata_tf_init(ap, &tf, dev->devno); |
| 4385 | |
| 4386 | tf.command = cmd; |
| 4387 | tf.flags |= ATA_TFLAG_DEVICE; |
| 4388 | tf.protocol = ATA_PROT_NODATA; |
| 4389 | |
Tejun Heo | d69cf37 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 4390 | err = ata_exec_internal(ap, dev, &tf, NULL, DMA_NONE, NULL, 0); |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 4391 | if (err) |
| 4392 | printk(KERN_ERR "%s: ata command failed: %d\n", |
| 4393 | __FUNCTION__, err); |
| 4394 | |
| 4395 | return err; |
| 4396 | } |
| 4397 | |
| 4398 | static int ata_flush_cache(struct ata_port *ap, struct ata_device *dev) |
| 4399 | { |
| 4400 | u8 cmd; |
| 4401 | |
| 4402 | if (!ata_try_flush_cache(dev)) |
| 4403 | return 0; |
| 4404 | |
| 4405 | if (ata_id_has_flush_ext(dev->id)) |
| 4406 | cmd = ATA_CMD_FLUSH_EXT; |
| 4407 | else |
| 4408 | cmd = ATA_CMD_FLUSH; |
| 4409 | |
| 4410 | return ata_do_simple_cmd(ap, dev, cmd); |
| 4411 | } |
| 4412 | |
| 4413 | static int ata_standby_drive(struct ata_port *ap, struct ata_device *dev) |
| 4414 | { |
| 4415 | return ata_do_simple_cmd(ap, dev, ATA_CMD_STANDBYNOW1); |
| 4416 | } |
| 4417 | |
| 4418 | static int ata_start_drive(struct ata_port *ap, struct ata_device *dev) |
| 4419 | { |
| 4420 | return ata_do_simple_cmd(ap, dev, ATA_CMD_IDLEIMMEDIATE); |
| 4421 | } |
| 4422 | |
| 4423 | /** |
| 4424 | * ata_device_resume - wakeup a previously suspended devices |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 4425 | * @ap: port the device is connected to |
| 4426 | * @dev: the device to resume |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 4427 | * |
| 4428 | * Kick the drive back into action, by sending it an idle immediate |
| 4429 | * command and making sure its transfer mode matches between drive |
| 4430 | * and host. |
| 4431 | * |
| 4432 | */ |
| 4433 | int ata_device_resume(struct ata_port *ap, struct ata_device *dev) |
| 4434 | { |
| 4435 | if (ap->flags & ATA_FLAG_SUSPENDED) { |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 4436 | struct ata_device *failed_dev; |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 4437 | ap->flags &= ~ATA_FLAG_SUSPENDED; |
Tejun Heo | e82cbdb | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 4438 | while (ata_set_mode(ap, &failed_dev)) |
| 4439 | ata_dev_disable(ap, failed_dev); |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 4440 | } |
Tejun Heo | e1211e3 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 4441 | if (!ata_dev_enabled(dev)) |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 4442 | return 0; |
| 4443 | if (dev->class == ATA_DEV_ATA) |
| 4444 | ata_start_drive(ap, dev); |
| 4445 | |
| 4446 | return 0; |
| 4447 | } |
| 4448 | |
| 4449 | /** |
| 4450 | * ata_device_suspend - prepare a device for suspend |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 4451 | * @ap: port the device is connected to |
| 4452 | * @dev: the device to suspend |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 4453 | * |
| 4454 | * Flush the cache on the drive, if appropriate, then issue a |
| 4455 | * standbynow command. |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 4456 | */ |
Nigel Cunningham | 082776e | 2006-03-23 23:22:16 +1000 | [diff] [blame] | 4457 | int ata_device_suspend(struct ata_port *ap, struct ata_device *dev, pm_message_t state) |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 4458 | { |
Tejun Heo | e1211e3 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 4459 | if (!ata_dev_enabled(dev)) |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 4460 | return 0; |
| 4461 | if (dev->class == ATA_DEV_ATA) |
| 4462 | ata_flush_cache(ap, dev); |
| 4463 | |
Nigel Cunningham | 082776e | 2006-03-23 23:22:16 +1000 | [diff] [blame] | 4464 | if (state.event != PM_EVENT_FREEZE) |
| 4465 | ata_standby_drive(ap, dev); |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 4466 | ap->flags |= ATA_FLAG_SUSPENDED; |
| 4467 | return 0; |
| 4468 | } |
| 4469 | |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 4470 | /** |
| 4471 | * ata_port_start - Set port up for dma. |
| 4472 | * @ap: Port to initialize |
| 4473 | * |
| 4474 | * Called just after data structures for each port are |
| 4475 | * initialized. Allocates space for PRD table. |
| 4476 | * |
| 4477 | * May be used as the port_start() entry in ata_port_operations. |
| 4478 | * |
| 4479 | * LOCKING: |
| 4480 | * Inherited from caller. |
| 4481 | */ |
| 4482 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4483 | int ata_port_start (struct ata_port *ap) |
| 4484 | { |
Brian King | 2f1f610 | 2006-03-23 17:30:15 -0600 | [diff] [blame] | 4485 | struct device *dev = ap->dev; |
Jeff Garzik | 6037d6b | 2005-11-04 22:08:00 -0500 | [diff] [blame] | 4486 | int rc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4487 | |
| 4488 | ap->prd = dma_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma, GFP_KERNEL); |
| 4489 | if (!ap->prd) |
| 4490 | return -ENOMEM; |
| 4491 | |
Jeff Garzik | 6037d6b | 2005-11-04 22:08:00 -0500 | [diff] [blame] | 4492 | rc = ata_pad_alloc(ap, dev); |
| 4493 | if (rc) { |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 4494 | dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma); |
Jeff Garzik | 6037d6b | 2005-11-04 22:08:00 -0500 | [diff] [blame] | 4495 | return rc; |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 4496 | } |
| 4497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4498 | DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd, (unsigned long long) ap->prd_dma); |
| 4499 | |
| 4500 | return 0; |
| 4501 | } |
| 4502 | |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 4503 | |
| 4504 | /** |
| 4505 | * ata_port_stop - Undo ata_port_start() |
| 4506 | * @ap: Port to shut down |
| 4507 | * |
| 4508 | * Frees the PRD table. |
| 4509 | * |
| 4510 | * May be used as the port_stop() entry in ata_port_operations. |
| 4511 | * |
| 4512 | * LOCKING: |
Randy Dunlap | 6f0ef4f | 2005-10-25 01:44:30 -0400 | [diff] [blame] | 4513 | * Inherited from caller. |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 4514 | */ |
| 4515 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4516 | void ata_port_stop (struct ata_port *ap) |
| 4517 | { |
Brian King | 2f1f610 | 2006-03-23 17:30:15 -0600 | [diff] [blame] | 4518 | struct device *dev = ap->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4519 | |
| 4520 | dma_free_coherent(dev, ATA_PRD_TBL_SZ, ap->prd, ap->prd_dma); |
Jeff Garzik | 6037d6b | 2005-11-04 22:08:00 -0500 | [diff] [blame] | 4521 | ata_pad_free(ap, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4522 | } |
| 4523 | |
Jeff Garzik | aa8f0dc | 2005-05-26 21:54:27 -0400 | [diff] [blame] | 4524 | void ata_host_stop (struct ata_host_set *host_set) |
| 4525 | { |
| 4526 | if (host_set->mmio_base) |
| 4527 | iounmap(host_set->mmio_base); |
| 4528 | } |
| 4529 | |
| 4530 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4531 | /** |
| 4532 | * ata_host_remove - Unregister SCSI host structure with upper layers |
| 4533 | * @ap: Port to unregister |
| 4534 | * @do_unregister: 1 if we fully unregister, 0 to just stop the port |
| 4535 | * |
| 4536 | * LOCKING: |
Randy Dunlap | 6f0ef4f | 2005-10-25 01:44:30 -0400 | [diff] [blame] | 4537 | * Inherited from caller. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4538 | */ |
| 4539 | |
| 4540 | static void ata_host_remove(struct ata_port *ap, unsigned int do_unregister) |
| 4541 | { |
| 4542 | struct Scsi_Host *sh = ap->host; |
| 4543 | |
| 4544 | DPRINTK("ENTER\n"); |
| 4545 | |
| 4546 | if (do_unregister) |
| 4547 | scsi_remove_host(sh); |
| 4548 | |
| 4549 | ap->ops->port_stop(ap); |
| 4550 | } |
| 4551 | |
| 4552 | /** |
| 4553 | * ata_host_init - Initialize an ata_port structure |
| 4554 | * @ap: Structure to initialize |
| 4555 | * @host: associated SCSI mid-layer structure |
| 4556 | * @host_set: Collection of hosts to which @ap belongs |
| 4557 | * @ent: Probe information provided by low-level driver |
| 4558 | * @port_no: Port number associated with this ata_port |
| 4559 | * |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4560 | * Initialize a new ata_port structure, and its associated |
| 4561 | * scsi_host. |
| 4562 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4563 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4564 | * Inherited from caller. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4565 | */ |
| 4566 | |
| 4567 | static void ata_host_init(struct ata_port *ap, struct Scsi_Host *host, |
| 4568 | struct ata_host_set *host_set, |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 4569 | const struct ata_probe_ent *ent, unsigned int port_no) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4570 | { |
| 4571 | unsigned int i; |
| 4572 | |
| 4573 | host->max_id = 16; |
| 4574 | host->max_lun = 1; |
| 4575 | host->max_channel = 1; |
| 4576 | host->unique_id = ata_unique_id++; |
| 4577 | host->max_cmd_len = 12; |
Christoph Hellwig | 1241319 | 2005-06-11 01:05:01 +0200 | [diff] [blame] | 4578 | |
Tejun Heo | 198e0fe | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 4579 | ap->flags = ATA_FLAG_DISABLED; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4580 | ap->id = host->unique_id; |
| 4581 | ap->host = host; |
| 4582 | ap->ctl = ATA_DEVCTL_OBS; |
| 4583 | ap->host_set = host_set; |
Brian King | 2f1f610 | 2006-03-23 17:30:15 -0600 | [diff] [blame] | 4584 | ap->dev = ent->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4585 | ap->port_no = port_no; |
| 4586 | ap->hard_port_no = |
| 4587 | ent->legacy_mode ? ent->hard_port_no : port_no; |
| 4588 | ap->pio_mask = ent->pio_mask; |
| 4589 | ap->mwdma_mask = ent->mwdma_mask; |
| 4590 | ap->udma_mask = ent->udma_mask; |
| 4591 | ap->flags |= ent->host_flags; |
| 4592 | ap->ops = ent->port_ops; |
| 4593 | ap->cbl = ATA_CBL_NONE; |
Tejun Heo | 1c3fae4 | 2006-04-02 20:53:28 +0900 | [diff] [blame] | 4594 | ap->sata_spd_limit = UINT_MAX; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4595 | ap->active_tag = ATA_TAG_POISON; |
| 4596 | ap->last_ctl = 0xFF; |
| 4597 | |
Tejun Heo | 86e45b6 | 2006-03-05 15:29:09 +0900 | [diff] [blame] | 4598 | INIT_WORK(&ap->port_task, NULL, NULL); |
Tejun Heo | a72ec4c | 2006-01-23 13:09:37 +0900 | [diff] [blame] | 4599 | INIT_LIST_HEAD(&ap->eh_done_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4600 | |
Tejun Heo | acf356b | 2006-03-24 14:07:50 +0900 | [diff] [blame] | 4601 | for (i = 0; i < ATA_MAX_DEVICES; i++) { |
| 4602 | struct ata_device *dev = &ap->device[i]; |
| 4603 | dev->devno = i; |
| 4604 | dev->pio_mask = UINT_MAX; |
| 4605 | dev->mwdma_mask = UINT_MAX; |
| 4606 | dev->udma_mask = UINT_MAX; |
| 4607 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4608 | |
| 4609 | #ifdef ATA_IRQ_TRAP |
| 4610 | ap->stats.unhandled_irq = 1; |
| 4611 | ap->stats.idle_irq = 1; |
| 4612 | #endif |
| 4613 | |
| 4614 | memcpy(&ap->ioaddr, &ent->port[port_no], sizeof(struct ata_ioports)); |
| 4615 | } |
| 4616 | |
| 4617 | /** |
| 4618 | * ata_host_add - Attach low-level ATA driver to system |
| 4619 | * @ent: Information provided by low-level driver |
| 4620 | * @host_set: Collections of ports to which we add |
| 4621 | * @port_no: Port number associated with this host |
| 4622 | * |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4623 | * Attach low-level ATA driver to system. |
| 4624 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4625 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4626 | * PCI/etc. bus probe sem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4627 | * |
| 4628 | * RETURNS: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4629 | * New ata_port on success, for NULL on error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4630 | */ |
| 4631 | |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 4632 | static struct ata_port * ata_host_add(const struct ata_probe_ent *ent, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4633 | struct ata_host_set *host_set, |
| 4634 | unsigned int port_no) |
| 4635 | { |
| 4636 | struct Scsi_Host *host; |
| 4637 | struct ata_port *ap; |
| 4638 | int rc; |
| 4639 | |
| 4640 | DPRINTK("ENTER\n"); |
Tejun Heo | aec5c3c | 2006-03-25 01:33:34 +0900 | [diff] [blame] | 4641 | |
| 4642 | if (!ent->port_ops->probe_reset && |
| 4643 | !(ent->host_flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST))) { |
| 4644 | printk(KERN_ERR "ata%u: no reset mechanism available\n", |
| 4645 | port_no); |
| 4646 | return NULL; |
| 4647 | } |
| 4648 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4649 | host = scsi_host_alloc(ent->sht, sizeof(struct ata_port)); |
| 4650 | if (!host) |
| 4651 | return NULL; |
| 4652 | |
Tejun Heo | 30afc84 | 2006-03-18 18:40:14 +0900 | [diff] [blame] | 4653 | host->transportt = &ata_scsi_transport_template; |
| 4654 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4655 | ap = (struct ata_port *) &host->hostdata[0]; |
| 4656 | |
| 4657 | ata_host_init(ap, host, host_set, ent, port_no); |
| 4658 | |
| 4659 | rc = ap->ops->port_start(ap); |
| 4660 | if (rc) |
| 4661 | goto err_out; |
| 4662 | |
| 4663 | return ap; |
| 4664 | |
| 4665 | err_out: |
| 4666 | scsi_host_put(host); |
| 4667 | return NULL; |
| 4668 | } |
| 4669 | |
| 4670 | /** |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4671 | * ata_device_add - Register hardware device with ATA and SCSI layers |
| 4672 | * @ent: Probe information describing hardware device to be registered |
| 4673 | * |
| 4674 | * This function processes the information provided in the probe |
| 4675 | * information struct @ent, allocates the necessary ATA and SCSI |
| 4676 | * host information structures, initializes them, and registers |
| 4677 | * everything with requisite kernel subsystems. |
| 4678 | * |
| 4679 | * This function requests irqs, probes the ATA bus, and probes |
| 4680 | * the SCSI bus. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4681 | * |
| 4682 | * LOCKING: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4683 | * PCI/etc. bus probe sem. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4684 | * |
| 4685 | * RETURNS: |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 4686 | * Number of ports registered. Zero on error (no ports registered). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4687 | */ |
| 4688 | |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 4689 | int ata_device_add(const struct ata_probe_ent *ent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4690 | { |
| 4691 | unsigned int count = 0, i; |
| 4692 | struct device *dev = ent->dev; |
| 4693 | struct ata_host_set *host_set; |
| 4694 | |
| 4695 | DPRINTK("ENTER\n"); |
| 4696 | /* alloc a container for our list of ATA ports (buses) */ |
Randy Dunlap | 57f3bda | 2005-10-28 20:37:23 -0700 | [diff] [blame] | 4697 | host_set = kzalloc(sizeof(struct ata_host_set) + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4698 | (ent->n_ports * sizeof(void *)), GFP_KERNEL); |
| 4699 | if (!host_set) |
| 4700 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4701 | spin_lock_init(&host_set->lock); |
| 4702 | |
| 4703 | host_set->dev = dev; |
| 4704 | host_set->n_ports = ent->n_ports; |
| 4705 | host_set->irq = ent->irq; |
| 4706 | host_set->mmio_base = ent->mmio_base; |
| 4707 | host_set->private_data = ent->private_data; |
| 4708 | host_set->ops = ent->port_ops; |
Alan Cox | 5444a6f | 2006-03-27 18:58:20 +0100 | [diff] [blame] | 4709 | host_set->flags = ent->host_set_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4710 | |
| 4711 | /* register each port bound to this device */ |
| 4712 | for (i = 0; i < ent->n_ports; i++) { |
| 4713 | struct ata_port *ap; |
| 4714 | unsigned long xfer_mode_mask; |
| 4715 | |
| 4716 | ap = ata_host_add(ent, host_set, i); |
| 4717 | if (!ap) |
| 4718 | goto err_out; |
| 4719 | |
| 4720 | host_set->ports[i] = ap; |
| 4721 | xfer_mode_mask =(ap->udma_mask << ATA_SHIFT_UDMA) | |
| 4722 | (ap->mwdma_mask << ATA_SHIFT_MWDMA) | |
| 4723 | (ap->pio_mask << ATA_SHIFT_PIO); |
| 4724 | |
| 4725 | /* print per-port info to dmesg */ |
| 4726 | printk(KERN_INFO "ata%u: %cATA max %s cmd 0x%lX ctl 0x%lX " |
| 4727 | "bmdma 0x%lX irq %lu\n", |
| 4728 | ap->id, |
| 4729 | ap->flags & ATA_FLAG_SATA ? 'S' : 'P', |
| 4730 | ata_mode_string(xfer_mode_mask), |
| 4731 | ap->ioaddr.cmd_addr, |
| 4732 | ap->ioaddr.ctl_addr, |
| 4733 | ap->ioaddr.bmdma_addr, |
| 4734 | ent->irq); |
| 4735 | |
| 4736 | ata_chk_status(ap); |
| 4737 | host_set->ops->irq_clear(ap); |
| 4738 | count++; |
| 4739 | } |
| 4740 | |
Randy Dunlap | 57f3bda | 2005-10-28 20:37:23 -0700 | [diff] [blame] | 4741 | if (!count) |
| 4742 | goto err_free_ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4743 | |
| 4744 | /* obtain irq, that is shared between channels */ |
| 4745 | if (request_irq(ent->irq, ent->port_ops->irq_handler, ent->irq_flags, |
| 4746 | DRV_NAME, host_set)) |
| 4747 | goto err_out; |
| 4748 | |
| 4749 | /* perform each probe synchronously */ |
| 4750 | DPRINTK("probe begin\n"); |
| 4751 | for (i = 0; i < count; i++) { |
| 4752 | struct ata_port *ap; |
| 4753 | int rc; |
| 4754 | |
| 4755 | ap = host_set->ports[i]; |
| 4756 | |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 4757 | DPRINTK("ata%u: bus probe begin\n", ap->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4758 | rc = ata_bus_probe(ap); |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 4759 | DPRINTK("ata%u: bus probe end\n", ap->id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4760 | |
| 4761 | if (rc) { |
| 4762 | /* FIXME: do something useful here? |
| 4763 | * Current libata behavior will |
| 4764 | * tear down everything when |
| 4765 | * the module is removed |
| 4766 | * or the h/w is unplugged. |
| 4767 | */ |
| 4768 | } |
| 4769 | |
| 4770 | rc = scsi_add_host(ap->host, dev); |
| 4771 | if (rc) { |
| 4772 | printk(KERN_ERR "ata%u: scsi_add_host failed\n", |
| 4773 | ap->id); |
| 4774 | /* FIXME: do something useful here */ |
| 4775 | /* FIXME: handle unconditional calls to |
| 4776 | * scsi_scan_host and ata_host_remove, below, |
| 4777 | * at the very least |
| 4778 | */ |
| 4779 | } |
| 4780 | } |
| 4781 | |
| 4782 | /* probes are done, now scan each port's disk(s) */ |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 4783 | DPRINTK("host probe begin\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4784 | for (i = 0; i < count; i++) { |
| 4785 | struct ata_port *ap = host_set->ports[i]; |
| 4786 | |
Jeff Garzik | 644dd0c | 2005-10-03 15:55:19 -0400 | [diff] [blame] | 4787 | ata_scsi_scan_host(ap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4788 | } |
| 4789 | |
| 4790 | dev_set_drvdata(dev, host_set); |
| 4791 | |
| 4792 | VPRINTK("EXIT, returning %u\n", ent->n_ports); |
| 4793 | return ent->n_ports; /* success */ |
| 4794 | |
| 4795 | err_out: |
| 4796 | for (i = 0; i < count; i++) { |
| 4797 | ata_host_remove(host_set->ports[i], 1); |
| 4798 | scsi_host_put(host_set->ports[i]->host); |
| 4799 | } |
Randy Dunlap | 57f3bda | 2005-10-28 20:37:23 -0700 | [diff] [blame] | 4800 | err_free_ret: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | kfree(host_set); |
| 4802 | VPRINTK("EXIT, returning 0\n"); |
| 4803 | return 0; |
| 4804 | } |
| 4805 | |
| 4806 | /** |
Alan Cox | 17b1445 | 2005-09-15 15:44:00 +0100 | [diff] [blame] | 4807 | * ata_host_set_remove - PCI layer callback for device removal |
| 4808 | * @host_set: ATA host set that was removed |
| 4809 | * |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 4810 | * Unregister all objects associated with this host set. Free those |
Alan Cox | 17b1445 | 2005-09-15 15:44:00 +0100 | [diff] [blame] | 4811 | * objects. |
| 4812 | * |
| 4813 | * LOCKING: |
| 4814 | * Inherited from calling layer (may sleep). |
| 4815 | */ |
| 4816 | |
Alan Cox | 17b1445 | 2005-09-15 15:44:00 +0100 | [diff] [blame] | 4817 | void ata_host_set_remove(struct ata_host_set *host_set) |
| 4818 | { |
| 4819 | struct ata_port *ap; |
| 4820 | unsigned int i; |
| 4821 | |
| 4822 | for (i = 0; i < host_set->n_ports; i++) { |
| 4823 | ap = host_set->ports[i]; |
| 4824 | scsi_remove_host(ap->host); |
| 4825 | } |
| 4826 | |
| 4827 | free_irq(host_set->irq, host_set); |
| 4828 | |
| 4829 | for (i = 0; i < host_set->n_ports; i++) { |
| 4830 | ap = host_set->ports[i]; |
| 4831 | |
| 4832 | ata_scsi_release(ap->host); |
| 4833 | |
| 4834 | if ((ap->flags & ATA_FLAG_NO_LEGACY) == 0) { |
| 4835 | struct ata_ioports *ioaddr = &ap->ioaddr; |
| 4836 | |
| 4837 | if (ioaddr->cmd_addr == 0x1f0) |
| 4838 | release_region(0x1f0, 8); |
| 4839 | else if (ioaddr->cmd_addr == 0x170) |
| 4840 | release_region(0x170, 8); |
| 4841 | } |
| 4842 | |
| 4843 | scsi_host_put(ap->host); |
| 4844 | } |
| 4845 | |
| 4846 | if (host_set->ops->host_stop) |
| 4847 | host_set->ops->host_stop(host_set); |
| 4848 | |
| 4849 | kfree(host_set); |
| 4850 | } |
| 4851 | |
| 4852 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4853 | * ata_scsi_release - SCSI layer callback hook for host unload |
| 4854 | * @host: libata host to be unloaded |
| 4855 | * |
| 4856 | * Performs all duties necessary to shut down a libata port... |
| 4857 | * Kill port kthread, disable port, and release resources. |
| 4858 | * |
| 4859 | * LOCKING: |
| 4860 | * Inherited from SCSI layer. |
| 4861 | * |
| 4862 | * RETURNS: |
| 4863 | * One. |
| 4864 | */ |
| 4865 | |
| 4866 | int ata_scsi_release(struct Scsi_Host *host) |
| 4867 | { |
| 4868 | struct ata_port *ap = (struct ata_port *) &host->hostdata[0]; |
Tejun Heo | d9572b1 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 4869 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4870 | |
| 4871 | DPRINTK("ENTER\n"); |
| 4872 | |
| 4873 | ap->ops->port_disable(ap); |
| 4874 | ata_host_remove(ap, 0); |
Tejun Heo | d9572b1 | 2006-03-01 16:09:35 +0900 | [diff] [blame] | 4875 | for (i = 0; i < ATA_MAX_DEVICES; i++) |
| 4876 | kfree(ap->device[i].id); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4877 | |
| 4878 | DPRINTK("EXIT\n"); |
| 4879 | return 1; |
| 4880 | } |
| 4881 | |
| 4882 | /** |
| 4883 | * ata_std_ports - initialize ioaddr with standard port offsets. |
| 4884 | * @ioaddr: IO address structure to be initialized |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 4885 | * |
| 4886 | * Utility function which initializes data_addr, error_addr, |
| 4887 | * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr, |
| 4888 | * device_addr, status_addr, and command_addr to standard offsets |
| 4889 | * relative to cmd_addr. |
| 4890 | * |
| 4891 | * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4892 | */ |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 4893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4894 | void ata_std_ports(struct ata_ioports *ioaddr) |
| 4895 | { |
| 4896 | ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA; |
| 4897 | ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR; |
| 4898 | ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE; |
| 4899 | ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT; |
| 4900 | ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL; |
| 4901 | ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM; |
| 4902 | ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH; |
| 4903 | ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE; |
| 4904 | ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS; |
| 4905 | ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD; |
| 4906 | } |
| 4907 | |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 4908 | |
Jeff Garzik | 374b187 | 2005-08-30 05:42:52 -0400 | [diff] [blame] | 4909 | #ifdef CONFIG_PCI |
| 4910 | |
| 4911 | void ata_pci_host_stop (struct ata_host_set *host_set) |
| 4912 | { |
| 4913 | struct pci_dev *pdev = to_pci_dev(host_set->dev); |
| 4914 | |
| 4915 | pci_iounmap(pdev, host_set->mmio_base); |
| 4916 | } |
| 4917 | |
Edward Falk | 0baab86 | 2005-06-02 18:17:13 -0400 | [diff] [blame] | 4918 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4919 | * ata_pci_remove_one - PCI layer callback for device removal |
| 4920 | * @pdev: PCI device that was removed |
| 4921 | * |
| 4922 | * PCI layer indicates to libata via this hook that |
Randy Dunlap | 6f0ef4f | 2005-10-25 01:44:30 -0400 | [diff] [blame] | 4923 | * hot-unplug or module unload event has occurred. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4924 | * Handle this by unregistering all objects associated |
| 4925 | * with this PCI device. Free those objects. Then finally |
| 4926 | * release PCI resources and disable device. |
| 4927 | * |
| 4928 | * LOCKING: |
| 4929 | * Inherited from PCI layer (may sleep). |
| 4930 | */ |
| 4931 | |
| 4932 | void ata_pci_remove_one (struct pci_dev *pdev) |
| 4933 | { |
| 4934 | struct device *dev = pci_dev_to_dev(pdev); |
| 4935 | struct ata_host_set *host_set = dev_get_drvdata(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4936 | |
Alan Cox | 17b1445 | 2005-09-15 15:44:00 +0100 | [diff] [blame] | 4937 | ata_host_set_remove(host_set); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4938 | pci_release_regions(pdev); |
| 4939 | pci_disable_device(pdev); |
| 4940 | dev_set_drvdata(dev, NULL); |
| 4941 | } |
| 4942 | |
| 4943 | /* move to PCI subsystem */ |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 4944 | int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4945 | { |
| 4946 | unsigned long tmp = 0; |
| 4947 | |
| 4948 | switch (bits->width) { |
| 4949 | case 1: { |
| 4950 | u8 tmp8 = 0; |
| 4951 | pci_read_config_byte(pdev, bits->reg, &tmp8); |
| 4952 | tmp = tmp8; |
| 4953 | break; |
| 4954 | } |
| 4955 | case 2: { |
| 4956 | u16 tmp16 = 0; |
| 4957 | pci_read_config_word(pdev, bits->reg, &tmp16); |
| 4958 | tmp = tmp16; |
| 4959 | break; |
| 4960 | } |
| 4961 | case 4: { |
| 4962 | u32 tmp32 = 0; |
| 4963 | pci_read_config_dword(pdev, bits->reg, &tmp32); |
| 4964 | tmp = tmp32; |
| 4965 | break; |
| 4966 | } |
| 4967 | |
| 4968 | default: |
| 4969 | return -EINVAL; |
| 4970 | } |
| 4971 | |
| 4972 | tmp &= bits->mask; |
| 4973 | |
| 4974 | return (tmp == bits->val) ? 1 : 0; |
| 4975 | } |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 4976 | |
| 4977 | int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t state) |
| 4978 | { |
| 4979 | pci_save_state(pdev); |
| 4980 | pci_disable_device(pdev); |
| 4981 | pci_set_power_state(pdev, PCI_D3hot); |
| 4982 | return 0; |
| 4983 | } |
| 4984 | |
| 4985 | int ata_pci_device_resume(struct pci_dev *pdev) |
| 4986 | { |
| 4987 | pci_set_power_state(pdev, PCI_D0); |
| 4988 | pci_restore_state(pdev); |
| 4989 | pci_enable_device(pdev); |
| 4990 | pci_set_master(pdev); |
| 4991 | return 0; |
| 4992 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4993 | #endif /* CONFIG_PCI */ |
| 4994 | |
| 4995 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4996 | static int __init ata_init(void) |
| 4997 | { |
| 4998 | ata_wq = create_workqueue("ata"); |
| 4999 | if (!ata_wq) |
| 5000 | return -ENOMEM; |
| 5001 | |
| 5002 | printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n"); |
| 5003 | return 0; |
| 5004 | } |
| 5005 | |
| 5006 | static void __exit ata_exit(void) |
| 5007 | { |
| 5008 | destroy_workqueue(ata_wq); |
| 5009 | } |
| 5010 | |
| 5011 | module_init(ata_init); |
| 5012 | module_exit(ata_exit); |
| 5013 | |
Jeff Garzik | 67846b3 | 2005-10-05 02:58:32 -0400 | [diff] [blame] | 5014 | static unsigned long ratelimit_time; |
| 5015 | static spinlock_t ata_ratelimit_lock = SPIN_LOCK_UNLOCKED; |
| 5016 | |
| 5017 | int ata_ratelimit(void) |
| 5018 | { |
| 5019 | int rc; |
| 5020 | unsigned long flags; |
| 5021 | |
| 5022 | spin_lock_irqsave(&ata_ratelimit_lock, flags); |
| 5023 | |
| 5024 | if (time_after(jiffies, ratelimit_time)) { |
| 5025 | rc = 1; |
| 5026 | ratelimit_time = jiffies + (HZ/5); |
| 5027 | } else |
| 5028 | rc = 0; |
| 5029 | |
| 5030 | spin_unlock_irqrestore(&ata_ratelimit_lock, flags); |
| 5031 | |
| 5032 | return rc; |
| 5033 | } |
| 5034 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5035 | /* |
| 5036 | * libata is essentially a library of internal helper functions for |
| 5037 | * low-level ATA host controller drivers. As such, the API/ABI is |
| 5038 | * likely to change as new drivers are added and updated. |
| 5039 | * Do not depend on ABI/API stability. |
| 5040 | */ |
| 5041 | |
| 5042 | EXPORT_SYMBOL_GPL(ata_std_bios_param); |
| 5043 | EXPORT_SYMBOL_GPL(ata_std_ports); |
| 5044 | EXPORT_SYMBOL_GPL(ata_device_add); |
Alan Cox | 17b1445 | 2005-09-15 15:44:00 +0100 | [diff] [blame] | 5045 | EXPORT_SYMBOL_GPL(ata_host_set_remove); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5046 | EXPORT_SYMBOL_GPL(ata_sg_init); |
| 5047 | EXPORT_SYMBOL_GPL(ata_sg_init_one); |
Tejun Heo | 7601442 | 2006-02-11 15:13:49 +0900 | [diff] [blame] | 5048 | EXPORT_SYMBOL_GPL(__ata_qc_complete); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5049 | EXPORT_SYMBOL_GPL(ata_qc_issue_prot); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5050 | EXPORT_SYMBOL_GPL(ata_tf_load); |
| 5051 | EXPORT_SYMBOL_GPL(ata_tf_read); |
| 5052 | EXPORT_SYMBOL_GPL(ata_noop_dev_select); |
| 5053 | EXPORT_SYMBOL_GPL(ata_std_dev_select); |
| 5054 | EXPORT_SYMBOL_GPL(ata_tf_to_fis); |
| 5055 | EXPORT_SYMBOL_GPL(ata_tf_from_fis); |
| 5056 | EXPORT_SYMBOL_GPL(ata_check_status); |
| 5057 | EXPORT_SYMBOL_GPL(ata_altstatus); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5058 | EXPORT_SYMBOL_GPL(ata_exec_command); |
| 5059 | EXPORT_SYMBOL_GPL(ata_port_start); |
| 5060 | EXPORT_SYMBOL_GPL(ata_port_stop); |
Jeff Garzik | aa8f0dc | 2005-05-26 21:54:27 -0400 | [diff] [blame] | 5061 | EXPORT_SYMBOL_GPL(ata_host_stop); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5062 | EXPORT_SYMBOL_GPL(ata_interrupt); |
| 5063 | EXPORT_SYMBOL_GPL(ata_qc_prep); |
Brian King | e46834c | 2006-03-17 17:04:03 -0600 | [diff] [blame] | 5064 | EXPORT_SYMBOL_GPL(ata_noop_qc_prep); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5065 | EXPORT_SYMBOL_GPL(ata_bmdma_setup); |
| 5066 | EXPORT_SYMBOL_GPL(ata_bmdma_start); |
| 5067 | EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear); |
| 5068 | EXPORT_SYMBOL_GPL(ata_bmdma_status); |
| 5069 | EXPORT_SYMBOL_GPL(ata_bmdma_stop); |
| 5070 | EXPORT_SYMBOL_GPL(ata_port_probe); |
| 5071 | EXPORT_SYMBOL_GPL(sata_phy_reset); |
| 5072 | EXPORT_SYMBOL_GPL(__sata_phy_reset); |
| 5073 | EXPORT_SYMBOL_GPL(ata_bus_reset); |
Tejun Heo | 8a19ac8 | 2006-02-02 18:20:00 +0900 | [diff] [blame] | 5074 | EXPORT_SYMBOL_GPL(ata_std_probeinit); |
Tejun Heo | c2bd580 | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 5075 | EXPORT_SYMBOL_GPL(ata_std_softreset); |
| 5076 | EXPORT_SYMBOL_GPL(sata_std_hardreset); |
| 5077 | EXPORT_SYMBOL_GPL(ata_std_postreset); |
| 5078 | EXPORT_SYMBOL_GPL(ata_std_probe_reset); |
Tejun Heo | a62c0fc | 2006-01-24 17:05:22 +0900 | [diff] [blame] | 5079 | EXPORT_SYMBOL_GPL(ata_drive_probe_reset); |
Tejun Heo | 623a312 | 2006-03-05 17:55:58 +0900 | [diff] [blame] | 5080 | EXPORT_SYMBOL_GPL(ata_dev_revalidate); |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 5081 | EXPORT_SYMBOL_GPL(ata_dev_classify); |
| 5082 | EXPORT_SYMBOL_GPL(ata_dev_pair); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5083 | EXPORT_SYMBOL_GPL(ata_port_disable); |
Jeff Garzik | 67846b3 | 2005-10-05 02:58:32 -0400 | [diff] [blame] | 5084 | EXPORT_SYMBOL_GPL(ata_ratelimit); |
Tejun Heo | 6f8b995 | 2006-01-24 17:05:21 +0900 | [diff] [blame] | 5085 | EXPORT_SYMBOL_GPL(ata_busy_sleep); |
Tejun Heo | 86e45b6 | 2006-03-05 15:29:09 +0900 | [diff] [blame] | 5086 | EXPORT_SYMBOL_GPL(ata_port_queue_task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5087 | EXPORT_SYMBOL_GPL(ata_scsi_ioctl); |
| 5088 | EXPORT_SYMBOL_GPL(ata_scsi_queuecmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5089 | EXPORT_SYMBOL_GPL(ata_scsi_slave_config); |
| 5090 | EXPORT_SYMBOL_GPL(ata_scsi_release); |
| 5091 | EXPORT_SYMBOL_GPL(ata_host_intr); |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 5092 | EXPORT_SYMBOL_GPL(ata_id_string); |
| 5093 | EXPORT_SYMBOL_GPL(ata_id_c_string); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5094 | EXPORT_SYMBOL_GPL(ata_scsi_simulate); |
| 5095 | |
Alan Cox | 1bc4ccf | 2006-01-09 17:18:14 +0000 | [diff] [blame] | 5096 | EXPORT_SYMBOL_GPL(ata_pio_need_iordy); |
Alan Cox | 452503f | 2005-10-21 19:01:32 -0400 | [diff] [blame] | 5097 | EXPORT_SYMBOL_GPL(ata_timing_compute); |
| 5098 | EXPORT_SYMBOL_GPL(ata_timing_merge); |
| 5099 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5100 | #ifdef CONFIG_PCI |
| 5101 | EXPORT_SYMBOL_GPL(pci_test_config_bits); |
Jeff Garzik | 374b187 | 2005-08-30 05:42:52 -0400 | [diff] [blame] | 5102 | EXPORT_SYMBOL_GPL(ata_pci_host_stop); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5103 | EXPORT_SYMBOL_GPL(ata_pci_init_native_mode); |
| 5104 | EXPORT_SYMBOL_GPL(ata_pci_init_one); |
| 5105 | EXPORT_SYMBOL_GPL(ata_pci_remove_one); |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 5106 | EXPORT_SYMBOL_GPL(ata_pci_device_suspend); |
| 5107 | EXPORT_SYMBOL_GPL(ata_pci_device_resume); |
Alan Cox | 67951ad | 2006-03-22 15:55:54 +0000 | [diff] [blame] | 5108 | EXPORT_SYMBOL_GPL(ata_pci_default_filter); |
| 5109 | EXPORT_SYMBOL_GPL(ata_pci_clear_simplex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5110 | #endif /* CONFIG_PCI */ |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 5111 | |
| 5112 | EXPORT_SYMBOL_GPL(ata_device_suspend); |
| 5113 | EXPORT_SYMBOL_GPL(ata_device_resume); |
| 5114 | EXPORT_SYMBOL_GPL(ata_scsi_device_suspend); |
| 5115 | EXPORT_SYMBOL_GPL(ata_scsi_device_resume); |
Tejun Heo | ece1d63 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 5116 | |
| 5117 | EXPORT_SYMBOL_GPL(ata_scsi_error); |
| 5118 | EXPORT_SYMBOL_GPL(ata_eng_timeout); |
| 5119 | EXPORT_SYMBOL_GPL(ata_eh_qc_complete); |
| 5120 | EXPORT_SYMBOL_GPL(ata_eh_qc_retry); |