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