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