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