1 /* 2 * libata-sff.c - helper library for PCI IDE BMDMA 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-2006 Red Hat, Inc. All rights reserved. 9 * Copyright 2003-2006 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 * 33 */ 34 35 #include <linux/kernel.h> 36 #include <linux/pci.h> 37 #include <linux/libata.h> 38 #include <linux/highmem.h> 39 40 #include "libata.h" 41 42 const struct ata_port_operations ata_sff_port_ops = { 43 .inherits = &ata_base_port_ops, 44 45 .qc_prep = ata_sff_qc_prep, 46 .qc_issue = ata_sff_qc_issue, 47 .qc_fill_rtf = ata_sff_qc_fill_rtf, 48 49 .freeze = ata_sff_freeze, 50 .thaw = ata_sff_thaw, 51 .prereset = ata_sff_prereset, 52 .softreset = ata_sff_softreset, 53 .hardreset = sata_sff_hardreset, 54 .postreset = ata_sff_postreset, 55 .drain_fifo = ata_sff_drain_fifo, 56 .error_handler = ata_sff_error_handler, 57 .post_internal_cmd = ata_sff_post_internal_cmd, 58 59 .sff_dev_select = ata_sff_dev_select, 60 .sff_check_status = ata_sff_check_status, 61 .sff_tf_load = ata_sff_tf_load, 62 .sff_tf_read = ata_sff_tf_read, 63 .sff_exec_command = ata_sff_exec_command, 64 .sff_data_xfer = ata_sff_data_xfer, 65 .sff_irq_on = ata_sff_irq_on, 66 .sff_irq_clear = ata_sff_irq_clear, 67 68 .lost_interrupt = ata_sff_lost_interrupt, 69 70 .port_start = ata_sff_port_start, 71 }; 72 EXPORT_SYMBOL_GPL(ata_sff_port_ops); 73 74 const struct ata_port_operations ata_bmdma_port_ops = { 75 .inherits = &ata_sff_port_ops, 76 77 .mode_filter = ata_bmdma_mode_filter, 78 79 .bmdma_setup = ata_bmdma_setup, 80 .bmdma_start = ata_bmdma_start, 81 .bmdma_stop = ata_bmdma_stop, 82 .bmdma_status = ata_bmdma_status, 83 }; 84 EXPORT_SYMBOL_GPL(ata_bmdma_port_ops); 85 86 const struct ata_port_operations ata_bmdma32_port_ops = { 87 .inherits = &ata_bmdma_port_ops, 88 89 .sff_data_xfer = ata_sff_data_xfer32, 90 .port_start = ata_sff_port_start32, 91 }; 92 EXPORT_SYMBOL_GPL(ata_bmdma32_port_ops); 93 94 /** 95 * ata_fill_sg - Fill PCI IDE PRD table 96 * @qc: Metadata associated with taskfile to be transferred 97 * 98 * Fill PCI IDE PRD (scatter-gather) table with segments 99 * associated with the current disk command. 100 * 101 * LOCKING: 102 * spin_lock_irqsave(host lock) 103 * 104 */ 105 static void ata_fill_sg(struct ata_queued_cmd *qc) 106 { 107 struct ata_port *ap = qc->ap; 108 struct scatterlist *sg; 109 unsigned int si, pi; 110 111 pi = 0; 112 for_each_sg(qc->sg, sg, qc->n_elem, si) { 113 u32 addr, offset; 114 u32 sg_len, len; 115 116 /* determine if physical DMA addr spans 64K boundary. 117 * Note h/w doesn't support 64-bit, so we unconditionally 118 * truncate dma_addr_t to u32. 119 */ 120 addr = (u32) sg_dma_address(sg); 121 sg_len = sg_dma_len(sg); 122 123 while (sg_len) { 124 offset = addr & 0xffff; 125 len = sg_len; 126 if ((offset + sg_len) > 0x10000) 127 len = 0x10000 - offset; 128 129 ap->prd[pi].addr = cpu_to_le32(addr); 130 ap->prd[pi].flags_len = cpu_to_le32(len & 0xffff); 131 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len); 132 133 pi++; 134 sg_len -= len; 135 addr += len; 136 } 137 } 138 139 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT); 140 } 141 142 /** 143 * ata_fill_sg_dumb - Fill PCI IDE PRD table 144 * @qc: Metadata associated with taskfile to be transferred 145 * 146 * Fill PCI IDE PRD (scatter-gather) table with segments 147 * associated with the current disk command. Perform the fill 148 * so that we avoid writing any length 64K records for 149 * controllers that don't follow the spec. 150 * 151 * LOCKING: 152 * spin_lock_irqsave(host lock) 153 * 154 */ 155 static void ata_fill_sg_dumb(struct ata_queued_cmd *qc) 156 { 157 struct ata_port *ap = qc->ap; 158 struct scatterlist *sg; 159 unsigned int si, pi; 160 161 pi = 0; 162 for_each_sg(qc->sg, sg, qc->n_elem, si) { 163 u32 addr, offset; 164 u32 sg_len, len, blen; 165 166 /* determine if physical DMA addr spans 64K boundary. 167 * Note h/w doesn't support 64-bit, so we unconditionally 168 * truncate dma_addr_t to u32. 169 */ 170 addr = (u32) sg_dma_address(sg); 171 sg_len = sg_dma_len(sg); 172 173 while (sg_len) { 174 offset = addr & 0xffff; 175 len = sg_len; 176 if ((offset + sg_len) > 0x10000) 177 len = 0x10000 - offset; 178 179 blen = len & 0xffff; 180 ap->prd[pi].addr = cpu_to_le32(addr); 181 if (blen == 0) { 182 /* Some PATA chipsets like the CS5530 can't 183 cope with 0x0000 meaning 64K as the spec 184 says */ 185 ap->prd[pi].flags_len = cpu_to_le32(0x8000); 186 blen = 0x8000; 187 ap->prd[++pi].addr = cpu_to_le32(addr + 0x8000); 188 } 189 ap->prd[pi].flags_len = cpu_to_le32(blen); 190 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len); 191 192 pi++; 193 sg_len -= len; 194 addr += len; 195 } 196 } 197 198 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT); 199 } 200 201 /** 202 * ata_sff_qc_prep - Prepare taskfile for submission 203 * @qc: Metadata associated with taskfile to be prepared 204 * 205 * Prepare ATA taskfile for submission. 206 * 207 * LOCKING: 208 * spin_lock_irqsave(host lock) 209 */ 210 void ata_sff_qc_prep(struct ata_queued_cmd *qc) 211 { 212 if (!(qc->flags & ATA_QCFLAG_DMAMAP)) 213 return; 214 215 ata_fill_sg(qc); 216 } 217 EXPORT_SYMBOL_GPL(ata_sff_qc_prep); 218 219 /** 220 * ata_sff_dumb_qc_prep - Prepare taskfile for submission 221 * @qc: Metadata associated with taskfile to be prepared 222 * 223 * Prepare ATA taskfile for submission. 224 * 225 * LOCKING: 226 * spin_lock_irqsave(host lock) 227 */ 228 void ata_sff_dumb_qc_prep(struct ata_queued_cmd *qc) 229 { 230 if (!(qc->flags & ATA_QCFLAG_DMAMAP)) 231 return; 232 233 ata_fill_sg_dumb(qc); 234 } 235 EXPORT_SYMBOL_GPL(ata_sff_dumb_qc_prep); 236 237 /** 238 * ata_sff_check_status - Read device status reg & clear interrupt 239 * @ap: port where the device is 240 * 241 * Reads ATA taskfile status register for currently-selected device 242 * and return its value. This also clears pending interrupts 243 * from this device 244 * 245 * LOCKING: 246 * Inherited from caller. 247 */ 248 u8 ata_sff_check_status(struct ata_port *ap) 249 { 250 return ioread8(ap->ioaddr.status_addr); 251 } 252 EXPORT_SYMBOL_GPL(ata_sff_check_status); 253 254 /** 255 * ata_sff_altstatus - Read device alternate status reg 256 * @ap: port where the device is 257 * 258 * Reads ATA taskfile alternate status register for 259 * currently-selected device and return its value. 260 * 261 * Note: may NOT be used as the check_altstatus() entry in 262 * ata_port_operations. 263 * 264 * LOCKING: 265 * Inherited from caller. 266 */ 267 static u8 ata_sff_altstatus(struct ata_port *ap) 268 { 269 if (ap->ops->sff_check_altstatus) 270 return ap->ops->sff_check_altstatus(ap); 271 272 return ioread8(ap->ioaddr.altstatus_addr); 273 } 274 275 /** 276 * ata_sff_irq_status - Check if the device is busy 277 * @ap: port where the device is 278 * 279 * Determine if the port is currently busy. Uses altstatus 280 * if available in order to avoid clearing shared IRQ status 281 * when finding an IRQ source. Non ctl capable devices don't 282 * share interrupt lines fortunately for us. 283 * 284 * LOCKING: 285 * Inherited from caller. 286 */ 287 static u8 ata_sff_irq_status(struct ata_port *ap) 288 { 289 u8 status; 290 291 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) { 292 status = ata_sff_altstatus(ap); 293 /* Not us: We are busy */ 294 if (status & ATA_BUSY) 295 return status; 296 } 297 /* Clear INTRQ latch */ 298 status = ap->ops->sff_check_status(ap); 299 return status; 300 } 301 302 /** 303 * ata_sff_sync - Flush writes 304 * @ap: Port to wait for. 305 * 306 * CAUTION: 307 * If we have an mmio device with no ctl and no altstatus 308 * method this will fail. No such devices are known to exist. 309 * 310 * LOCKING: 311 * Inherited from caller. 312 */ 313 314 static void ata_sff_sync(struct ata_port *ap) 315 { 316 if (ap->ops->sff_check_altstatus) 317 ap->ops->sff_check_altstatus(ap); 318 else if (ap->ioaddr.altstatus_addr) 319 ioread8(ap->ioaddr.altstatus_addr); 320 } 321 322 /** 323 * ata_sff_pause - Flush writes and wait 400nS 324 * @ap: Port to pause for. 325 * 326 * CAUTION: 327 * If we have an mmio device with no ctl and no altstatus 328 * method this will fail. No such devices are known to exist. 329 * 330 * LOCKING: 331 * Inherited from caller. 332 */ 333 334 void ata_sff_pause(struct ata_port *ap) 335 { 336 ata_sff_sync(ap); 337 ndelay(400); 338 } 339 EXPORT_SYMBOL_GPL(ata_sff_pause); 340 341 /** 342 * ata_sff_dma_pause - Pause before commencing DMA 343 * @ap: Port to pause for. 344 * 345 * Perform I/O fencing and ensure sufficient cycle delays occur 346 * for the HDMA1:0 transition 347 */ 348 349 void ata_sff_dma_pause(struct ata_port *ap) 350 { 351 if (ap->ops->sff_check_altstatus || ap->ioaddr.altstatus_addr) { 352 /* An altstatus read will cause the needed delay without 353 messing up the IRQ status */ 354 ata_sff_altstatus(ap); 355 return; 356 } 357 /* There are no DMA controllers without ctl. BUG here to ensure 358 we never violate the HDMA1:0 transition timing and risk 359 corruption. */ 360 BUG(); 361 } 362 EXPORT_SYMBOL_GPL(ata_sff_dma_pause); 363 364 /** 365 * ata_sff_busy_sleep - sleep until BSY clears, or timeout 366 * @ap: port containing status register to be polled 367 * @tmout_pat: impatience timeout in msecs 368 * @tmout: overall timeout in msecs 369 * 370 * Sleep until ATA Status register bit BSY clears, 371 * or a timeout occurs. 372 * 373 * LOCKING: 374 * Kernel thread context (may sleep). 375 * 376 * RETURNS: 377 * 0 on success, -errno otherwise. 378 */ 379 int ata_sff_busy_sleep(struct ata_port *ap, 380 unsigned long tmout_pat, unsigned long tmout) 381 { 382 unsigned long timer_start, timeout; 383 u8 status; 384 385 status = ata_sff_busy_wait(ap, ATA_BUSY, 300); 386 timer_start = jiffies; 387 timeout = ata_deadline(timer_start, tmout_pat); 388 while (status != 0xff && (status & ATA_BUSY) && 389 time_before(jiffies, timeout)) { 390 msleep(50); 391 status = ata_sff_busy_wait(ap, ATA_BUSY, 3); 392 } 393 394 if (status != 0xff && (status & ATA_BUSY)) 395 ata_port_printk(ap, KERN_WARNING, 396 "port is slow to respond, please be patient " 397 "(Status 0x%x)\n", status); 398 399 timeout = ata_deadline(timer_start, tmout); 400 while (status != 0xff && (status & ATA_BUSY) && 401 time_before(jiffies, timeout)) { 402 msleep(50); 403 status = ap->ops->sff_check_status(ap); 404 } 405 406 if (status == 0xff) 407 return -ENODEV; 408 409 if (status & ATA_BUSY) { 410 ata_port_printk(ap, KERN_ERR, "port failed to respond " 411 "(%lu secs, Status 0x%x)\n", 412 DIV_ROUND_UP(tmout, 1000), status); 413 return -EBUSY; 414 } 415 416 return 0; 417 } 418 EXPORT_SYMBOL_GPL(ata_sff_busy_sleep); 419 420 static int ata_sff_check_ready(struct ata_link *link) 421 { 422 u8 status = link->ap->ops->sff_check_status(link->ap); 423 424 return ata_check_ready(status); 425 } 426 427 /** 428 * ata_sff_wait_ready - sleep until BSY clears, or timeout 429 * @link: SFF link to wait ready status for 430 * @deadline: deadline jiffies for the operation 431 * 432 * Sleep until ATA Status register bit BSY clears, or timeout 433 * occurs. 434 * 435 * LOCKING: 436 * Kernel thread context (may sleep). 437 * 438 * RETURNS: 439 * 0 on success, -errno otherwise. 440 */ 441 int ata_sff_wait_ready(struct ata_link *link, unsigned long deadline) 442 { 443 return ata_wait_ready(link, deadline, ata_sff_check_ready); 444 } 445 EXPORT_SYMBOL_GPL(ata_sff_wait_ready); 446 447 /** 448 * ata_sff_dev_select - Select device 0/1 on ATA bus 449 * @ap: ATA channel to manipulate 450 * @device: ATA device (numbered from zero) to select 451 * 452 * Use the method defined in the ATA specification to 453 * make either device 0, or device 1, active on the 454 * ATA channel. Works with both PIO and MMIO. 455 * 456 * May be used as the dev_select() entry in ata_port_operations. 457 * 458 * LOCKING: 459 * caller. 460 */ 461 void ata_sff_dev_select(struct ata_port *ap, unsigned int device) 462 { 463 u8 tmp; 464 465 if (device == 0) 466 tmp = ATA_DEVICE_OBS; 467 else 468 tmp = ATA_DEVICE_OBS | ATA_DEV1; 469 470 iowrite8(tmp, ap->ioaddr.device_addr); 471 ata_sff_pause(ap); /* needed; also flushes, for mmio */ 472 } 473 EXPORT_SYMBOL_GPL(ata_sff_dev_select); 474 475 /** 476 * ata_dev_select - Select device 0/1 on ATA bus 477 * @ap: ATA channel to manipulate 478 * @device: ATA device (numbered from zero) to select 479 * @wait: non-zero to wait for Status register BSY bit to clear 480 * @can_sleep: non-zero if context allows sleeping 481 * 482 * Use the method defined in the ATA specification to 483 * make either device 0, or device 1, active on the 484 * ATA channel. 485 * 486 * This is a high-level version of ata_sff_dev_select(), which 487 * additionally provides the services of inserting the proper 488 * pauses and status polling, where needed. 489 * 490 * LOCKING: 491 * caller. 492 */ 493 void ata_dev_select(struct ata_port *ap, unsigned int device, 494 unsigned int wait, unsigned int can_sleep) 495 { 496 if (ata_msg_probe(ap)) 497 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, " 498 "device %u, wait %u\n", device, wait); 499 500 if (wait) 501 ata_wait_idle(ap); 502 503 ap->ops->sff_dev_select(ap, device); 504 505 if (wait) { 506 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI) 507 msleep(150); 508 ata_wait_idle(ap); 509 } 510 } 511 512 /** 513 * ata_sff_irq_on - Enable interrupts on a port. 514 * @ap: Port on which interrupts are enabled. 515 * 516 * Enable interrupts on a legacy IDE device using MMIO or PIO, 517 * wait for idle, clear any pending interrupts. 518 * 519 * LOCKING: 520 * Inherited from caller. 521 */ 522 u8 ata_sff_irq_on(struct ata_port *ap) 523 { 524 struct ata_ioports *ioaddr = &ap->ioaddr; 525 u8 tmp; 526 527 ap->ctl &= ~ATA_NIEN; 528 ap->last_ctl = ap->ctl; 529 530 if (ioaddr->ctl_addr) 531 iowrite8(ap->ctl, ioaddr->ctl_addr); 532 tmp = ata_wait_idle(ap); 533 534 ap->ops->sff_irq_clear(ap); 535 536 return tmp; 537 } 538 EXPORT_SYMBOL_GPL(ata_sff_irq_on); 539 540 /** 541 * ata_sff_irq_clear - Clear PCI IDE BMDMA interrupt. 542 * @ap: Port associated with this ATA transaction. 543 * 544 * Clear interrupt and error flags in DMA status register. 545 * 546 * May be used as the irq_clear() entry in ata_port_operations. 547 * 548 * LOCKING: 549 * spin_lock_irqsave(host lock) 550 */ 551 void ata_sff_irq_clear(struct ata_port *ap) 552 { 553 void __iomem *mmio = ap->ioaddr.bmdma_addr; 554 555 if (!mmio) 556 return; 557 558 iowrite8(ioread8(mmio + ATA_DMA_STATUS), mmio + ATA_DMA_STATUS); 559 } 560 EXPORT_SYMBOL_GPL(ata_sff_irq_clear); 561 562 /** 563 * ata_sff_tf_load - send taskfile registers to host controller 564 * @ap: Port to which output is sent 565 * @tf: ATA taskfile register set 566 * 567 * Outputs ATA taskfile to standard ATA host controller. 568 * 569 * LOCKING: 570 * Inherited from caller. 571 */ 572 void ata_sff_tf_load(struct ata_port *ap, const struct ata_taskfile *tf) 573 { 574 struct ata_ioports *ioaddr = &ap->ioaddr; 575 unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR; 576 577 if (tf->ctl != ap->last_ctl) { 578 if (ioaddr->ctl_addr) 579 iowrite8(tf->ctl, ioaddr->ctl_addr); 580 ap->last_ctl = tf->ctl; 581 ata_wait_idle(ap); 582 } 583 584 if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) { 585 WARN_ON_ONCE(!ioaddr->ctl_addr); 586 iowrite8(tf->hob_feature, ioaddr->feature_addr); 587 iowrite8(tf->hob_nsect, ioaddr->nsect_addr); 588 iowrite8(tf->hob_lbal, ioaddr->lbal_addr); 589 iowrite8(tf->hob_lbam, ioaddr->lbam_addr); 590 iowrite8(tf->hob_lbah, ioaddr->lbah_addr); 591 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n", 592 tf->hob_feature, 593 tf->hob_nsect, 594 tf->hob_lbal, 595 tf->hob_lbam, 596 tf->hob_lbah); 597 } 598 599 if (is_addr) { 600 iowrite8(tf->feature, ioaddr->feature_addr); 601 iowrite8(tf->nsect, ioaddr->nsect_addr); 602 iowrite8(tf->lbal, ioaddr->lbal_addr); 603 iowrite8(tf->lbam, ioaddr->lbam_addr); 604 iowrite8(tf->lbah, ioaddr->lbah_addr); 605 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n", 606 tf->feature, 607 tf->nsect, 608 tf->lbal, 609 tf->lbam, 610 tf->lbah); 611 } 612 613 if (tf->flags & ATA_TFLAG_DEVICE) { 614 iowrite8(tf->device, ioaddr->device_addr); 615 VPRINTK("device 0x%X\n", tf->device); 616 } 617 618 ata_wait_idle(ap); 619 } 620 EXPORT_SYMBOL_GPL(ata_sff_tf_load); 621 622 /** 623 * ata_sff_tf_read - input device's ATA taskfile shadow registers 624 * @ap: Port from which input is read 625 * @tf: ATA taskfile register set for storing input 626 * 627 * Reads ATA taskfile registers for currently-selected device 628 * into @tf. Assumes the device has a fully SFF compliant task file 629 * layout and behaviour. If you device does not (eg has a different 630 * status method) then you will need to provide a replacement tf_read 631 * 632 * LOCKING: 633 * Inherited from caller. 634 */ 635 void ata_sff_tf_read(struct ata_port *ap, struct ata_taskfile *tf) 636 { 637 struct ata_ioports *ioaddr = &ap->ioaddr; 638 639 tf->command = ata_sff_check_status(ap); 640 tf->feature = ioread8(ioaddr->error_addr); 641 tf->nsect = ioread8(ioaddr->nsect_addr); 642 tf->lbal = ioread8(ioaddr->lbal_addr); 643 tf->lbam = ioread8(ioaddr->lbam_addr); 644 tf->lbah = ioread8(ioaddr->lbah_addr); 645 tf->device = ioread8(ioaddr->device_addr); 646 647 if (tf->flags & ATA_TFLAG_LBA48) { 648 if (likely(ioaddr->ctl_addr)) { 649 iowrite8(tf->ctl | ATA_HOB, ioaddr->ctl_addr); 650 tf->hob_feature = ioread8(ioaddr->error_addr); 651 tf->hob_nsect = ioread8(ioaddr->nsect_addr); 652 tf->hob_lbal = ioread8(ioaddr->lbal_addr); 653 tf->hob_lbam = ioread8(ioaddr->lbam_addr); 654 tf->hob_lbah = ioread8(ioaddr->lbah_addr); 655 iowrite8(tf->ctl, ioaddr->ctl_addr); 656 ap->last_ctl = tf->ctl; 657 } else 658 WARN_ON_ONCE(1); 659 } 660 } 661 EXPORT_SYMBOL_GPL(ata_sff_tf_read); 662 663 /** 664 * ata_sff_exec_command - issue ATA command to host controller 665 * @ap: port to which command is being issued 666 * @tf: ATA taskfile register set 667 * 668 * Issues ATA command, with proper synchronization with interrupt 669 * handler / other threads. 670 * 671 * LOCKING: 672 * spin_lock_irqsave(host lock) 673 */ 674 void ata_sff_exec_command(struct ata_port *ap, const struct ata_taskfile *tf) 675 { 676 DPRINTK("ata%u: cmd 0x%X\n", ap->print_id, tf->command); 677 678 iowrite8(tf->command, ap->ioaddr.command_addr); 679 ata_sff_pause(ap); 680 } 681 EXPORT_SYMBOL_GPL(ata_sff_exec_command); 682 683 /** 684 * ata_tf_to_host - issue ATA taskfile to host controller 685 * @ap: port to which command is being issued 686 * @tf: ATA taskfile register set 687 * 688 * Issues ATA taskfile register set to ATA host controller, 689 * with proper synchronization with interrupt handler and 690 * other threads. 691 * 692 * LOCKING: 693 * spin_lock_irqsave(host lock) 694 */ 695 static inline void ata_tf_to_host(struct ata_port *ap, 696 const struct ata_taskfile *tf) 697 { 698 ap->ops->sff_tf_load(ap, tf); 699 ap->ops->sff_exec_command(ap, tf); 700 } 701 702 /** 703 * ata_sff_data_xfer - Transfer data by PIO 704 * @dev: device to target 705 * @buf: data buffer 706 * @buflen: buffer length 707 * @rw: read/write 708 * 709 * Transfer data from/to the device data register by PIO. 710 * 711 * LOCKING: 712 * Inherited from caller. 713 * 714 * RETURNS: 715 * Bytes consumed. 716 */ 717 unsigned int ata_sff_data_xfer(struct ata_device *dev, unsigned char *buf, 718 unsigned int buflen, int rw) 719 { 720 struct ata_port *ap = dev->link->ap; 721 void __iomem *data_addr = ap->ioaddr.data_addr; 722 unsigned int words = buflen >> 1; 723 724 /* Transfer multiple of 2 bytes */ 725 if (rw == READ) 726 ioread16_rep(data_addr, buf, words); 727 else 728 iowrite16_rep(data_addr, buf, words); 729 730 /* Transfer trailing 1 byte, if any. */ 731 if (unlikely(buflen & 0x01)) { 732 __le16 align_buf[1] = { 0 }; 733 unsigned char *trailing_buf = buf + buflen - 1; 734 735 if (rw == READ) { 736 align_buf[0] = cpu_to_le16(ioread16(data_addr)); 737 memcpy(trailing_buf, align_buf, 1); 738 } else { 739 memcpy(align_buf, trailing_buf, 1); 740 iowrite16(le16_to_cpu(align_buf[0]), data_addr); 741 } 742 words++; 743 } 744 745 return words << 1; 746 } 747 EXPORT_SYMBOL_GPL(ata_sff_data_xfer); 748 749 /** 750 * ata_sff_data_xfer32 - Transfer data by PIO 751 * @dev: device to target 752 * @buf: data buffer 753 * @buflen: buffer length 754 * @rw: read/write 755 * 756 * Transfer data from/to the device data register by PIO using 32bit 757 * I/O operations. 758 * 759 * LOCKING: 760 * Inherited from caller. 761 * 762 * RETURNS: 763 * Bytes consumed. 764 */ 765 766 unsigned int ata_sff_data_xfer32(struct ata_device *dev, unsigned char *buf, 767 unsigned int buflen, int rw) 768 { 769 struct ata_port *ap = dev->link->ap; 770 void __iomem *data_addr = ap->ioaddr.data_addr; 771 unsigned int words = buflen >> 2; 772 int slop = buflen & 3; 773 774 if (!(ap->pflags & ATA_PFLAG_PIO32)) 775 return ata_sff_data_xfer(dev, buf, buflen, rw); 776 777 /* Transfer multiple of 4 bytes */ 778 if (rw == READ) 779 ioread32_rep(data_addr, buf, words); 780 else 781 iowrite32_rep(data_addr, buf, words); 782 783 /* Transfer trailing bytes, if any */ 784 if (unlikely(slop)) { 785 unsigned char pad[4]; 786 787 /* Point buf to the tail of buffer */ 788 buf += buflen - slop; 789 790 /* 791 * Use io*_rep() accessors here as well to avoid pointlessly 792 * swapping bytes to and fro on the big endian machines... 793 */ 794 if (rw == READ) { 795 if (slop < 3) 796 ioread16_rep(data_addr, pad, 1); 797 else 798 ioread32_rep(data_addr, pad, 1); 799 memcpy(buf, pad, slop); 800 } else { 801 memcpy(pad, buf, slop); 802 if (slop < 3) 803 iowrite16_rep(data_addr, pad, 1); 804 else 805 iowrite32_rep(data_addr, pad, 1); 806 } 807 } 808 return (buflen + 1) & ~1; 809 } 810 EXPORT_SYMBOL_GPL(ata_sff_data_xfer32); 811 812 /** 813 * ata_sff_data_xfer_noirq - Transfer data by PIO 814 * @dev: device to target 815 * @buf: data buffer 816 * @buflen: buffer length 817 * @rw: read/write 818 * 819 * Transfer data from/to the device data register by PIO. Do the 820 * transfer with interrupts disabled. 821 * 822 * LOCKING: 823 * Inherited from caller. 824 * 825 * RETURNS: 826 * Bytes consumed. 827 */ 828 unsigned int ata_sff_data_xfer_noirq(struct ata_device *dev, unsigned char *buf, 829 unsigned int buflen, int rw) 830 { 831 unsigned long flags; 832 unsigned int consumed; 833 834 local_irq_save(flags); 835 consumed = ata_sff_data_xfer(dev, buf, buflen, rw); 836 local_irq_restore(flags); 837 838 return consumed; 839 } 840 EXPORT_SYMBOL_GPL(ata_sff_data_xfer_noirq); 841 842 /** 843 * ata_pio_sector - Transfer a sector of data. 844 * @qc: Command on going 845 * 846 * Transfer qc->sect_size bytes of data from/to the ATA device. 847 * 848 * LOCKING: 849 * Inherited from caller. 850 */ 851 static void ata_pio_sector(struct ata_queued_cmd *qc) 852 { 853 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE); 854 struct ata_port *ap = qc->ap; 855 struct page *page; 856 unsigned int offset; 857 unsigned char *buf; 858 859 if (qc->curbytes == qc->nbytes - qc->sect_size) 860 ap->hsm_task_state = HSM_ST_LAST; 861 862 page = sg_page(qc->cursg); 863 offset = qc->cursg->offset + qc->cursg_ofs; 864 865 /* get the current page and offset */ 866 page = nth_page(page, (offset >> PAGE_SHIFT)); 867 offset %= PAGE_SIZE; 868 869 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read"); 870 871 if (PageHighMem(page)) { 872 unsigned long flags; 873 874 /* FIXME: use a bounce buffer */ 875 local_irq_save(flags); 876 buf = kmap_atomic(page, KM_IRQ0); 877 878 /* do the actual data transfer */ 879 ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size, 880 do_write); 881 882 kunmap_atomic(buf, KM_IRQ0); 883 local_irq_restore(flags); 884 } else { 885 buf = page_address(page); 886 ap->ops->sff_data_xfer(qc->dev, buf + offset, qc->sect_size, 887 do_write); 888 } 889 890 qc->curbytes += qc->sect_size; 891 qc->cursg_ofs += qc->sect_size; 892 893 if (qc->cursg_ofs == qc->cursg->length) { 894 qc->cursg = sg_next(qc->cursg); 895 qc->cursg_ofs = 0; 896 } 897 } 898 899 /** 900 * ata_pio_sectors - Transfer one or many sectors. 901 * @qc: Command on going 902 * 903 * Transfer one or many sectors of data from/to the 904 * ATA device for the DRQ request. 905 * 906 * LOCKING: 907 * Inherited from caller. 908 */ 909 static void ata_pio_sectors(struct ata_queued_cmd *qc) 910 { 911 if (is_multi_taskfile(&qc->tf)) { 912 /* READ/WRITE MULTIPLE */ 913 unsigned int nsect; 914 915 WARN_ON_ONCE(qc->dev->multi_count == 0); 916 917 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size, 918 qc->dev->multi_count); 919 while (nsect--) 920 ata_pio_sector(qc); 921 } else 922 ata_pio_sector(qc); 923 924 ata_sff_sync(qc->ap); /* flush */ 925 } 926 927 /** 928 * atapi_send_cdb - Write CDB bytes to hardware 929 * @ap: Port to which ATAPI device is attached. 930 * @qc: Taskfile currently active 931 * 932 * When device has indicated its readiness to accept 933 * a CDB, this function is called. Send the CDB. 934 * 935 * LOCKING: 936 * caller. 937 */ 938 static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc) 939 { 940 /* send SCSI cdb */ 941 DPRINTK("send cdb\n"); 942 WARN_ON_ONCE(qc->dev->cdb_len < 12); 943 944 ap->ops->sff_data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1); 945 ata_sff_sync(ap); 946 /* FIXME: If the CDB is for DMA do we need to do the transition delay 947 or is bmdma_start guaranteed to do it ? */ 948 switch (qc->tf.protocol) { 949 case ATAPI_PROT_PIO: 950 ap->hsm_task_state = HSM_ST; 951 break; 952 case ATAPI_PROT_NODATA: 953 ap->hsm_task_state = HSM_ST_LAST; 954 break; 955 case ATAPI_PROT_DMA: 956 ap->hsm_task_state = HSM_ST_LAST; 957 /* initiate bmdma */ 958 ap->ops->bmdma_start(qc); 959 break; 960 } 961 } 962 963 /** 964 * __atapi_pio_bytes - Transfer data from/to the ATAPI device. 965 * @qc: Command on going 966 * @bytes: number of bytes 967 * 968 * Transfer Transfer data from/to the ATAPI device. 969 * 970 * LOCKING: 971 * Inherited from caller. 972 * 973 */ 974 static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes) 975 { 976 int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ; 977 struct ata_port *ap = qc->ap; 978 struct ata_device *dev = qc->dev; 979 struct ata_eh_info *ehi = &dev->link->eh_info; 980 struct scatterlist *sg; 981 struct page *page; 982 unsigned char *buf; 983 unsigned int offset, count, consumed; 984 985 next_sg: 986 sg = qc->cursg; 987 if (unlikely(!sg)) { 988 ata_ehi_push_desc(ehi, "unexpected or too much trailing data " 989 "buf=%u cur=%u bytes=%u", 990 qc->nbytes, qc->curbytes, bytes); 991 return -1; 992 } 993 994 page = sg_page(sg); 995 offset = sg->offset + qc->cursg_ofs; 996 997 /* get the current page and offset */ 998 page = nth_page(page, (offset >> PAGE_SHIFT)); 999 offset %= PAGE_SIZE; 1000 1001 /* don't overrun current sg */ 1002 count = min(sg->length - qc->cursg_ofs, bytes); 1003 1004 /* don't cross page boundaries */ 1005 count = min(count, (unsigned int)PAGE_SIZE - offset); 1006 1007 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read"); 1008 1009 if (PageHighMem(page)) { 1010 unsigned long flags; 1011 1012 /* FIXME: use bounce buffer */ 1013 local_irq_save(flags); 1014 buf = kmap_atomic(page, KM_IRQ0); 1015 1016 /* do the actual data transfer */ 1017 consumed = ap->ops->sff_data_xfer(dev, buf + offset, 1018 count, rw); 1019 1020 kunmap_atomic(buf, KM_IRQ0); 1021 local_irq_restore(flags); 1022 } else { 1023 buf = page_address(page); 1024 consumed = ap->ops->sff_data_xfer(dev, buf + offset, 1025 count, rw); 1026 } 1027 1028 bytes -= min(bytes, consumed); 1029 qc->curbytes += count; 1030 qc->cursg_ofs += count; 1031 1032 if (qc->cursg_ofs == sg->length) { 1033 qc->cursg = sg_next(qc->cursg); 1034 qc->cursg_ofs = 0; 1035 } 1036 1037 /* 1038 * There used to be a WARN_ON_ONCE(qc->cursg && count != consumed); 1039 * Unfortunately __atapi_pio_bytes doesn't know enough to do the WARN 1040 * check correctly as it doesn't know if it is the last request being 1041 * made. Somebody should implement a proper sanity check. 1042 */ 1043 if (bytes) 1044 goto next_sg; 1045 return 0; 1046 } 1047 1048 /** 1049 * atapi_pio_bytes - Transfer data from/to the ATAPI device. 1050 * @qc: Command on going 1051 * 1052 * Transfer Transfer data from/to the ATAPI device. 1053 * 1054 * LOCKING: 1055 * Inherited from caller. 1056 */ 1057 static void atapi_pio_bytes(struct ata_queued_cmd *qc) 1058 { 1059 struct ata_port *ap = qc->ap; 1060 struct ata_device *dev = qc->dev; 1061 struct ata_eh_info *ehi = &dev->link->eh_info; 1062 unsigned int ireason, bc_lo, bc_hi, bytes; 1063 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0; 1064 1065 /* Abuse qc->result_tf for temp storage of intermediate TF 1066 * here to save some kernel stack usage. 1067 * For normal completion, qc->result_tf is not relevant. For 1068 * error, qc->result_tf is later overwritten by ata_qc_complete(). 1069 * So, the correctness of qc->result_tf is not affected. 1070 */ 1071 ap->ops->sff_tf_read(ap, &qc->result_tf); 1072 ireason = qc->result_tf.nsect; 1073 bc_lo = qc->result_tf.lbam; 1074 bc_hi = qc->result_tf.lbah; 1075 bytes = (bc_hi << 8) | bc_lo; 1076 1077 /* shall be cleared to zero, indicating xfer of data */ 1078 if (unlikely(ireason & (1 << 0))) 1079 goto atapi_check; 1080 1081 /* make sure transfer direction matches expected */ 1082 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0; 1083 if (unlikely(do_write != i_write)) 1084 goto atapi_check; 1085 1086 if (unlikely(!bytes)) 1087 goto atapi_check; 1088 1089 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes); 1090 1091 if (unlikely(__atapi_pio_bytes(qc, bytes))) 1092 goto err_out; 1093 ata_sff_sync(ap); /* flush */ 1094 1095 return; 1096 1097 atapi_check: 1098 ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)", 1099 ireason, bytes); 1100 err_out: 1101 qc->err_mask |= AC_ERR_HSM; 1102 ap->hsm_task_state = HSM_ST_ERR; 1103 } 1104 1105 /** 1106 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue. 1107 * @ap: the target ata_port 1108 * @qc: qc on going 1109 * 1110 * RETURNS: 1111 * 1 if ok in workqueue, 0 otherwise. 1112 */ 1113 static inline int ata_hsm_ok_in_wq(struct ata_port *ap, 1114 struct ata_queued_cmd *qc) 1115 { 1116 if (qc->tf.flags & ATA_TFLAG_POLLING) 1117 return 1; 1118 1119 if (ap->hsm_task_state == HSM_ST_FIRST) { 1120 if (qc->tf.protocol == ATA_PROT_PIO && 1121 (qc->tf.flags & ATA_TFLAG_WRITE)) 1122 return 1; 1123 1124 if (ata_is_atapi(qc->tf.protocol) && 1125 !(qc->dev->flags & ATA_DFLAG_CDB_INTR)) 1126 return 1; 1127 } 1128 1129 return 0; 1130 } 1131 1132 /** 1133 * ata_hsm_qc_complete - finish a qc running on standard HSM 1134 * @qc: Command to complete 1135 * @in_wq: 1 if called from workqueue, 0 otherwise 1136 * 1137 * Finish @qc which is running on standard HSM. 1138 * 1139 * LOCKING: 1140 * If @in_wq is zero, spin_lock_irqsave(host lock). 1141 * Otherwise, none on entry and grabs host lock. 1142 */ 1143 static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq) 1144 { 1145 struct ata_port *ap = qc->ap; 1146 unsigned long flags; 1147 1148 if (ap->ops->error_handler) { 1149 if (in_wq) { 1150 spin_lock_irqsave(ap->lock, flags); 1151 1152 /* EH might have kicked in while host lock is 1153 * released. 1154 */ 1155 qc = ata_qc_from_tag(ap, qc->tag); 1156 if (qc) { 1157 if (likely(!(qc->err_mask & AC_ERR_HSM))) { 1158 ap->ops->sff_irq_on(ap); 1159 ata_qc_complete(qc); 1160 } else 1161 ata_port_freeze(ap); 1162 } 1163 1164 spin_unlock_irqrestore(ap->lock, flags); 1165 } else { 1166 if (likely(!(qc->err_mask & AC_ERR_HSM))) 1167 ata_qc_complete(qc); 1168 else 1169 ata_port_freeze(ap); 1170 } 1171 } else { 1172 if (in_wq) { 1173 spin_lock_irqsave(ap->lock, flags); 1174 ap->ops->sff_irq_on(ap); 1175 ata_qc_complete(qc); 1176 spin_unlock_irqrestore(ap->lock, flags); 1177 } else 1178 ata_qc_complete(qc); 1179 } 1180 } 1181 1182 /** 1183 * ata_sff_hsm_move - move the HSM to the next state. 1184 * @ap: the target ata_port 1185 * @qc: qc on going 1186 * @status: current device status 1187 * @in_wq: 1 if called from workqueue, 0 otherwise 1188 * 1189 * RETURNS: 1190 * 1 when poll next status needed, 0 otherwise. 1191 */ 1192 int ata_sff_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc, 1193 u8 status, int in_wq) 1194 { 1195 struct ata_eh_info *ehi = &ap->link.eh_info; 1196 unsigned long flags = 0; 1197 int poll_next; 1198 1199 WARN_ON_ONCE((qc->flags & ATA_QCFLAG_ACTIVE) == 0); 1200 1201 /* Make sure ata_sff_qc_issue() does not throw things 1202 * like DMA polling into the workqueue. Notice that 1203 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING). 1204 */ 1205 WARN_ON_ONCE(in_wq != ata_hsm_ok_in_wq(ap, qc)); 1206 1207 fsm_start: 1208 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n", 1209 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status); 1210 1211 switch (ap->hsm_task_state) { 1212 case HSM_ST_FIRST: 1213 /* Send first data block or PACKET CDB */ 1214 1215 /* If polling, we will stay in the work queue after 1216 * sending the data. Otherwise, interrupt handler 1217 * takes over after sending the data. 1218 */ 1219 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING); 1220 1221 /* check device status */ 1222 if (unlikely((status & ATA_DRQ) == 0)) { 1223 /* handle BSY=0, DRQ=0 as error */ 1224 if (likely(status & (ATA_ERR | ATA_DF))) 1225 /* device stops HSM for abort/error */ 1226 qc->err_mask |= AC_ERR_DEV; 1227 else { 1228 /* HSM violation. Let EH handle this */ 1229 ata_ehi_push_desc(ehi, 1230 "ST_FIRST: !(DRQ|ERR|DF)"); 1231 qc->err_mask |= AC_ERR_HSM; 1232 } 1233 1234 ap->hsm_task_state = HSM_ST_ERR; 1235 goto fsm_start; 1236 } 1237 1238 /* Device should not ask for data transfer (DRQ=1) 1239 * when it finds something wrong. 1240 * We ignore DRQ here and stop the HSM by 1241 * changing hsm_task_state to HSM_ST_ERR and 1242 * let the EH abort the command or reset the device. 1243 */ 1244 if (unlikely(status & (ATA_ERR | ATA_DF))) { 1245 /* Some ATAPI tape drives forget to clear the ERR bit 1246 * when doing the next command (mostly request sense). 1247 * We ignore ERR here to workaround and proceed sending 1248 * the CDB. 1249 */ 1250 if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) { 1251 ata_ehi_push_desc(ehi, "ST_FIRST: " 1252 "DRQ=1 with device error, " 1253 "dev_stat 0x%X", status); 1254 qc->err_mask |= AC_ERR_HSM; 1255 ap->hsm_task_state = HSM_ST_ERR; 1256 goto fsm_start; 1257 } 1258 } 1259 1260 /* Send the CDB (atapi) or the first data block (ata pio out). 1261 * During the state transition, interrupt handler shouldn't 1262 * be invoked before the data transfer is complete and 1263 * hsm_task_state is changed. Hence, the following locking. 1264 */ 1265 if (in_wq) 1266 spin_lock_irqsave(ap->lock, flags); 1267 1268 if (qc->tf.protocol == ATA_PROT_PIO) { 1269 /* PIO data out protocol. 1270 * send first data block. 1271 */ 1272 1273 /* ata_pio_sectors() might change the state 1274 * to HSM_ST_LAST. so, the state is changed here 1275 * before ata_pio_sectors(). 1276 */ 1277 ap->hsm_task_state = HSM_ST; 1278 ata_pio_sectors(qc); 1279 } else 1280 /* send CDB */ 1281 atapi_send_cdb(ap, qc); 1282 1283 if (in_wq) 1284 spin_unlock_irqrestore(ap->lock, flags); 1285 1286 /* if polling, ata_pio_task() handles the rest. 1287 * otherwise, interrupt handler takes over from here. 1288 */ 1289 break; 1290 1291 case HSM_ST: 1292 /* complete command or read/write the data register */ 1293 if (qc->tf.protocol == ATAPI_PROT_PIO) { 1294 /* ATAPI PIO protocol */ 1295 if ((status & ATA_DRQ) == 0) { 1296 /* No more data to transfer or device error. 1297 * Device error will be tagged in HSM_ST_LAST. 1298 */ 1299 ap->hsm_task_state = HSM_ST_LAST; 1300 goto fsm_start; 1301 } 1302 1303 /* Device should not ask for data transfer (DRQ=1) 1304 * when it finds something wrong. 1305 * We ignore DRQ here and stop the HSM by 1306 * changing hsm_task_state to HSM_ST_ERR and 1307 * let the EH abort the command or reset the device. 1308 */ 1309 if (unlikely(status & (ATA_ERR | ATA_DF))) { 1310 ata_ehi_push_desc(ehi, "ST-ATAPI: " 1311 "DRQ=1 with device error, " 1312 "dev_stat 0x%X", status); 1313 qc->err_mask |= AC_ERR_HSM; 1314 ap->hsm_task_state = HSM_ST_ERR; 1315 goto fsm_start; 1316 } 1317 1318 atapi_pio_bytes(qc); 1319 1320 if (unlikely(ap->hsm_task_state == HSM_ST_ERR)) 1321 /* bad ireason reported by device */ 1322 goto fsm_start; 1323 1324 } else { 1325 /* ATA PIO protocol */ 1326 if (unlikely((status & ATA_DRQ) == 0)) { 1327 /* handle BSY=0, DRQ=0 as error */ 1328 if (likely(status & (ATA_ERR | ATA_DF))) { 1329 /* device stops HSM for abort/error */ 1330 qc->err_mask |= AC_ERR_DEV; 1331 1332 /* If diagnostic failed and this is 1333 * IDENTIFY, it's likely a phantom 1334 * device. Mark hint. 1335 */ 1336 if (qc->dev->horkage & 1337 ATA_HORKAGE_DIAGNOSTIC) 1338 qc->err_mask |= 1339 AC_ERR_NODEV_HINT; 1340 } else { 1341 /* HSM violation. Let EH handle this. 1342 * Phantom devices also trigger this 1343 * condition. Mark hint. 1344 */ 1345 ata_ehi_push_desc(ehi, "ST-ATA: " 1346 "DRQ=0 without device error, " 1347 "dev_stat 0x%X", status); 1348 qc->err_mask |= AC_ERR_HSM | 1349 AC_ERR_NODEV_HINT; 1350 } 1351 1352 ap->hsm_task_state = HSM_ST_ERR; 1353 goto fsm_start; 1354 } 1355 1356 /* For PIO reads, some devices may ask for 1357 * data transfer (DRQ=1) alone with ERR=1. 1358 * We respect DRQ here and transfer one 1359 * block of junk data before changing the 1360 * hsm_task_state to HSM_ST_ERR. 1361 * 1362 * For PIO writes, ERR=1 DRQ=1 doesn't make 1363 * sense since the data block has been 1364 * transferred to the device. 1365 */ 1366 if (unlikely(status & (ATA_ERR | ATA_DF))) { 1367 /* data might be corrputed */ 1368 qc->err_mask |= AC_ERR_DEV; 1369 1370 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) { 1371 ata_pio_sectors(qc); 1372 status = ata_wait_idle(ap); 1373 } 1374 1375 if (status & (ATA_BUSY | ATA_DRQ)) { 1376 ata_ehi_push_desc(ehi, "ST-ATA: " 1377 "BUSY|DRQ persists on ERR|DF, " 1378 "dev_stat 0x%X", status); 1379 qc->err_mask |= AC_ERR_HSM; 1380 } 1381 1382 /* There are oddball controllers with 1383 * status register stuck at 0x7f and 1384 * lbal/m/h at zero which makes it 1385 * pass all other presence detection 1386 * mechanisms we have. Set NODEV_HINT 1387 * for it. Kernel bz#7241. 1388 */ 1389 if (status == 0x7f) 1390 qc->err_mask |= AC_ERR_NODEV_HINT; 1391 1392 /* ata_pio_sectors() might change the 1393 * state to HSM_ST_LAST. so, the state 1394 * is changed after ata_pio_sectors(). 1395 */ 1396 ap->hsm_task_state = HSM_ST_ERR; 1397 goto fsm_start; 1398 } 1399 1400 ata_pio_sectors(qc); 1401 1402 if (ap->hsm_task_state == HSM_ST_LAST && 1403 (!(qc->tf.flags & ATA_TFLAG_WRITE))) { 1404 /* all data read */ 1405 status = ata_wait_idle(ap); 1406 goto fsm_start; 1407 } 1408 } 1409 1410 poll_next = 1; 1411 break; 1412 1413 case HSM_ST_LAST: 1414 if (unlikely(!ata_ok(status))) { 1415 qc->err_mask |= __ac_err_mask(status); 1416 ap->hsm_task_state = HSM_ST_ERR; 1417 goto fsm_start; 1418 } 1419 1420 /* no more data to transfer */ 1421 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n", 1422 ap->print_id, qc->dev->devno, status); 1423 1424 WARN_ON_ONCE(qc->err_mask & (AC_ERR_DEV | AC_ERR_HSM)); 1425 1426 ap->hsm_task_state = HSM_ST_IDLE; 1427 1428 /* complete taskfile transaction */ 1429 ata_hsm_qc_complete(qc, in_wq); 1430 1431 poll_next = 0; 1432 break; 1433 1434 case HSM_ST_ERR: 1435 ap->hsm_task_state = HSM_ST_IDLE; 1436 1437 /* complete taskfile transaction */ 1438 ata_hsm_qc_complete(qc, in_wq); 1439 1440 poll_next = 0; 1441 break; 1442 default: 1443 poll_next = 0; 1444 BUG(); 1445 } 1446 1447 return poll_next; 1448 } 1449 EXPORT_SYMBOL_GPL(ata_sff_hsm_move); 1450 1451 void ata_pio_task(struct work_struct *work) 1452 { 1453 struct ata_port *ap = 1454 container_of(work, struct ata_port, port_task.work); 1455 struct ata_queued_cmd *qc = ap->port_task_data; 1456 u8 status; 1457 int poll_next; 1458 1459 fsm_start: 1460 WARN_ON_ONCE(ap->hsm_task_state == HSM_ST_IDLE); 1461 1462 /* 1463 * This is purely heuristic. This is a fast path. 1464 * Sometimes when we enter, BSY will be cleared in 1465 * a chk-status or two. If not, the drive is probably seeking 1466 * or something. Snooze for a couple msecs, then 1467 * chk-status again. If still busy, queue delayed work. 1468 */ 1469 status = ata_sff_busy_wait(ap, ATA_BUSY, 5); 1470 if (status & ATA_BUSY) { 1471 msleep(2); 1472 status = ata_sff_busy_wait(ap, ATA_BUSY, 10); 1473 if (status & ATA_BUSY) { 1474 ata_pio_queue_task(ap, qc, ATA_SHORT_PAUSE); 1475 return; 1476 } 1477 } 1478 1479 /* move the HSM */ 1480 poll_next = ata_sff_hsm_move(ap, qc, status, 1); 1481 1482 /* another command or interrupt handler 1483 * may be running at this point. 1484 */ 1485 if (poll_next) 1486 goto fsm_start; 1487 } 1488 1489 /** 1490 * ata_sff_qc_issue - issue taskfile to device in proto-dependent manner 1491 * @qc: command to issue to device 1492 * 1493 * Using various libata functions and hooks, this function 1494 * starts an ATA command. ATA commands are grouped into 1495 * classes called "protocols", and issuing each type of protocol 1496 * is slightly different. 1497 * 1498 * May be used as the qc_issue() entry in ata_port_operations. 1499 * 1500 * LOCKING: 1501 * spin_lock_irqsave(host lock) 1502 * 1503 * RETURNS: 1504 * Zero on success, AC_ERR_* mask on failure 1505 */ 1506 unsigned int ata_sff_qc_issue(struct ata_queued_cmd *qc) 1507 { 1508 struct ata_port *ap = qc->ap; 1509 1510 /* Use polling pio if the LLD doesn't handle 1511 * interrupt driven pio and atapi CDB interrupt. 1512 */ 1513 if (ap->flags & ATA_FLAG_PIO_POLLING) { 1514 switch (qc->tf.protocol) { 1515 case ATA_PROT_PIO: 1516 case ATA_PROT_NODATA: 1517 case ATAPI_PROT_PIO: 1518 case ATAPI_PROT_NODATA: 1519 qc->tf.flags |= ATA_TFLAG_POLLING; 1520 break; 1521 case ATAPI_PROT_DMA: 1522 if (qc->dev->flags & ATA_DFLAG_CDB_INTR) 1523 /* see ata_dma_blacklisted() */ 1524 BUG(); 1525 break; 1526 default: 1527 break; 1528 } 1529 } 1530 1531 /* select the device */ 1532 ata_dev_select(ap, qc->dev->devno, 1, 0); 1533 1534 /* start the command */ 1535 switch (qc->tf.protocol) { 1536 case ATA_PROT_NODATA: 1537 if (qc->tf.flags & ATA_TFLAG_POLLING) 1538 ata_qc_set_polling(qc); 1539 1540 ata_tf_to_host(ap, &qc->tf); 1541 ap->hsm_task_state = HSM_ST_LAST; 1542 1543 if (qc->tf.flags & ATA_TFLAG_POLLING) 1544 ata_pio_queue_task(ap, qc, 0); 1545 1546 break; 1547 1548 case ATA_PROT_DMA: 1549 WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING); 1550 1551 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */ 1552 ap->ops->bmdma_setup(qc); /* set up bmdma */ 1553 ap->ops->bmdma_start(qc); /* initiate bmdma */ 1554 ap->hsm_task_state = HSM_ST_LAST; 1555 break; 1556 1557 case ATA_PROT_PIO: 1558 if (qc->tf.flags & ATA_TFLAG_POLLING) 1559 ata_qc_set_polling(qc); 1560 1561 ata_tf_to_host(ap, &qc->tf); 1562 1563 if (qc->tf.flags & ATA_TFLAG_WRITE) { 1564 /* PIO data out protocol */ 1565 ap->hsm_task_state = HSM_ST_FIRST; 1566 ata_pio_queue_task(ap, qc, 0); 1567 1568 /* always send first data block using 1569 * the ata_pio_task() codepath. 1570 */ 1571 } else { 1572 /* PIO data in protocol */ 1573 ap->hsm_task_state = HSM_ST; 1574 1575 if (qc->tf.flags & ATA_TFLAG_POLLING) 1576 ata_pio_queue_task(ap, qc, 0); 1577 1578 /* if polling, ata_pio_task() handles the rest. 1579 * otherwise, interrupt handler takes over from here. 1580 */ 1581 } 1582 1583 break; 1584 1585 case ATAPI_PROT_PIO: 1586 case ATAPI_PROT_NODATA: 1587 if (qc->tf.flags & ATA_TFLAG_POLLING) 1588 ata_qc_set_polling(qc); 1589 1590 ata_tf_to_host(ap, &qc->tf); 1591 1592 ap->hsm_task_state = HSM_ST_FIRST; 1593 1594 /* send cdb by polling if no cdb interrupt */ 1595 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) || 1596 (qc->tf.flags & ATA_TFLAG_POLLING)) 1597 ata_pio_queue_task(ap, qc, 0); 1598 break; 1599 1600 case ATAPI_PROT_DMA: 1601 WARN_ON_ONCE(qc->tf.flags & ATA_TFLAG_POLLING); 1602 1603 ap->ops->sff_tf_load(ap, &qc->tf); /* load tf registers */ 1604 ap->ops->bmdma_setup(qc); /* set up bmdma */ 1605 ap->hsm_task_state = HSM_ST_FIRST; 1606 1607 /* send cdb by polling if no cdb interrupt */ 1608 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) 1609 ata_pio_queue_task(ap, qc, 0); 1610 break; 1611 1612 default: 1613 WARN_ON_ONCE(1); 1614 return AC_ERR_SYSTEM; 1615 } 1616 1617 return 0; 1618 } 1619 EXPORT_SYMBOL_GPL(ata_sff_qc_issue); 1620 1621 /** 1622 * ata_sff_qc_fill_rtf - fill result TF using ->sff_tf_read 1623 * @qc: qc to fill result TF for 1624 * 1625 * @qc is finished and result TF needs to be filled. Fill it 1626 * using ->sff_tf_read. 1627 * 1628 * LOCKING: 1629 * spin_lock_irqsave(host lock) 1630 * 1631 * RETURNS: 1632 * true indicating that result TF is successfully filled. 1633 */ 1634 bool ata_sff_qc_fill_rtf(struct ata_queued_cmd *qc) 1635 { 1636 qc->ap->ops->sff_tf_read(qc->ap, &qc->result_tf); 1637 return true; 1638 } 1639 EXPORT_SYMBOL_GPL(ata_sff_qc_fill_rtf); 1640 1641 /** 1642 * ata_sff_host_intr - Handle host interrupt for given (port, task) 1643 * @ap: Port on which interrupt arrived (possibly...) 1644 * @qc: Taskfile currently active in engine 1645 * 1646 * Handle host interrupt for given queued command. Currently, 1647 * only DMA interrupts are handled. All other commands are 1648 * handled via polling with interrupts disabled (nIEN bit). 1649 * 1650 * LOCKING: 1651 * spin_lock_irqsave(host lock) 1652 * 1653 * RETURNS: 1654 * One if interrupt was handled, zero if not (shared irq). 1655 */ 1656 unsigned int ata_sff_host_intr(struct ata_port *ap, 1657 struct ata_queued_cmd *qc) 1658 { 1659 struct ata_eh_info *ehi = &ap->link.eh_info; 1660 u8 status, host_stat = 0; 1661 1662 VPRINTK("ata%u: protocol %d task_state %d\n", 1663 ap->print_id, qc->tf.protocol, ap->hsm_task_state); 1664 1665 /* Check whether we are expecting interrupt in this state */ 1666 switch (ap->hsm_task_state) { 1667 case HSM_ST_FIRST: 1668 /* Some pre-ATAPI-4 devices assert INTRQ 1669 * at this state when ready to receive CDB. 1670 */ 1671 1672 /* Check the ATA_DFLAG_CDB_INTR flag is enough here. 1673 * The flag was turned on only for atapi devices. No 1674 * need to check ata_is_atapi(qc->tf.protocol) again. 1675 */ 1676 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) 1677 goto idle_irq; 1678 break; 1679 case HSM_ST_LAST: 1680 if (qc->tf.protocol == ATA_PROT_DMA || 1681 qc->tf.protocol == ATAPI_PROT_DMA) { 1682 /* check status of DMA engine */ 1683 host_stat = ap->ops->bmdma_status(ap); 1684 VPRINTK("ata%u: host_stat 0x%X\n", 1685 ap->print_id, host_stat); 1686 1687 /* if it's not our irq... */ 1688 if (!(host_stat & ATA_DMA_INTR)) 1689 goto idle_irq; 1690 1691 /* before we do anything else, clear DMA-Start bit */ 1692 ap->ops->bmdma_stop(qc); 1693 1694 if (unlikely(host_stat & ATA_DMA_ERR)) { 1695 /* error when transfering data to/from memory */ 1696 qc->err_mask |= AC_ERR_HOST_BUS; 1697 ap->hsm_task_state = HSM_ST_ERR; 1698 } 1699 } 1700 break; 1701 case HSM_ST: 1702 break; 1703 default: 1704 goto idle_irq; 1705 } 1706 1707 1708 /* check main status, clearing INTRQ if needed */ 1709 status = ata_sff_irq_status(ap); 1710 if (status & ATA_BUSY) 1711 goto idle_irq; 1712 1713 /* ack bmdma irq events */ 1714 ap->ops->sff_irq_clear(ap); 1715 1716 ata_sff_hsm_move(ap, qc, status, 0); 1717 1718 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA || 1719 qc->tf.protocol == ATAPI_PROT_DMA)) 1720 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat); 1721 1722 return 1; /* irq handled */ 1723 1724 idle_irq: 1725 ap->stats.idle_irq++; 1726 1727 #ifdef ATA_IRQ_TRAP 1728 if ((ap->stats.idle_irq % 1000) == 0) { 1729 ap->ops->sff_check_status(ap); 1730 ap->ops->sff_irq_clear(ap); 1731 ata_port_printk(ap, KERN_WARNING, "irq trap\n"); 1732 return 1; 1733 } 1734 #endif 1735 return 0; /* irq not handled */ 1736 } 1737 EXPORT_SYMBOL_GPL(ata_sff_host_intr); 1738 1739 /** 1740 * ata_sff_interrupt - Default ATA host interrupt handler 1741 * @irq: irq line (unused) 1742 * @dev_instance: pointer to our ata_host information structure 1743 * 1744 * Default interrupt handler for PCI IDE devices. Calls 1745 * ata_sff_host_intr() for each port that is not disabled. 1746 * 1747 * LOCKING: 1748 * Obtains host lock during operation. 1749 * 1750 * RETURNS: 1751 * IRQ_NONE or IRQ_HANDLED. 1752 */ 1753 irqreturn_t ata_sff_interrupt(int irq, void *dev_instance) 1754 { 1755 struct ata_host *host = dev_instance; 1756 unsigned int i; 1757 unsigned int handled = 0; 1758 unsigned long flags; 1759 1760 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */ 1761 spin_lock_irqsave(&host->lock, flags); 1762 1763 for (i = 0; i < host->n_ports; i++) { 1764 struct ata_port *ap; 1765 1766 ap = host->ports[i]; 1767 if (ap && 1768 !(ap->flags & ATA_FLAG_DISABLED)) { 1769 struct ata_queued_cmd *qc; 1770 1771 qc = ata_qc_from_tag(ap, ap->link.active_tag); 1772 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) && 1773 (qc->flags & ATA_QCFLAG_ACTIVE)) 1774 handled |= ata_sff_host_intr(ap, qc); 1775 } 1776 } 1777 1778 spin_unlock_irqrestore(&host->lock, flags); 1779 1780 return IRQ_RETVAL(handled); 1781 } 1782 EXPORT_SYMBOL_GPL(ata_sff_interrupt); 1783 1784 /** 1785 * ata_sff_lost_interrupt - Check for an apparent lost interrupt 1786 * @ap: port that appears to have timed out 1787 * 1788 * Called from the libata error handlers when the core code suspects 1789 * an interrupt has been lost. If it has complete anything we can and 1790 * then return. Interface must support altstatus for this faster 1791 * recovery to occur. 1792 * 1793 * Locking: 1794 * Caller holds host lock 1795 */ 1796 1797 void ata_sff_lost_interrupt(struct ata_port *ap) 1798 { 1799 u8 status; 1800 struct ata_queued_cmd *qc; 1801 1802 /* Only one outstanding command per SFF channel */ 1803 qc = ata_qc_from_tag(ap, ap->link.active_tag); 1804 /* Check we have a live one.. */ 1805 if (qc == NULL || !(qc->flags & ATA_QCFLAG_ACTIVE)) 1806 return; 1807 /* We cannot lose an interrupt on a polled command */ 1808 if (qc->tf.flags & ATA_TFLAG_POLLING) 1809 return; 1810 /* See if the controller thinks it is still busy - if so the command 1811 isn't a lost IRQ but is still in progress */ 1812 status = ata_sff_altstatus(ap); 1813 if (status & ATA_BUSY) 1814 return; 1815 1816 /* There was a command running, we are no longer busy and we have 1817 no interrupt. */ 1818 ata_port_printk(ap, KERN_WARNING, "lost interrupt (Status 0x%x)\n", 1819 status); 1820 /* Run the host interrupt logic as if the interrupt had not been 1821 lost */ 1822 ata_sff_host_intr(ap, qc); 1823 } 1824 EXPORT_SYMBOL_GPL(ata_sff_lost_interrupt); 1825 1826 /** 1827 * ata_sff_freeze - Freeze SFF controller port 1828 * @ap: port to freeze 1829 * 1830 * Freeze BMDMA controller port. 1831 * 1832 * LOCKING: 1833 * Inherited from caller. 1834 */ 1835 void ata_sff_freeze(struct ata_port *ap) 1836 { 1837 struct ata_ioports *ioaddr = &ap->ioaddr; 1838 1839 ap->ctl |= ATA_NIEN; 1840 ap->last_ctl = ap->ctl; 1841 1842 if (ioaddr->ctl_addr) 1843 iowrite8(ap->ctl, ioaddr->ctl_addr); 1844 1845 /* Under certain circumstances, some controllers raise IRQ on 1846 * ATA_NIEN manipulation. Also, many controllers fail to mask 1847 * previously pending IRQ on ATA_NIEN assertion. Clear it. 1848 */ 1849 ap->ops->sff_check_status(ap); 1850 1851 ap->ops->sff_irq_clear(ap); 1852 } 1853 EXPORT_SYMBOL_GPL(ata_sff_freeze); 1854 1855 /** 1856 * ata_sff_thaw - Thaw SFF controller port 1857 * @ap: port to thaw 1858 * 1859 * Thaw SFF controller port. 1860 * 1861 * LOCKING: 1862 * Inherited from caller. 1863 */ 1864 void ata_sff_thaw(struct ata_port *ap) 1865 { 1866 /* clear & re-enable interrupts */ 1867 ap->ops->sff_check_status(ap); 1868 ap->ops->sff_irq_clear(ap); 1869 ap->ops->sff_irq_on(ap); 1870 } 1871 EXPORT_SYMBOL_GPL(ata_sff_thaw); 1872 1873 /** 1874 * ata_sff_prereset - prepare SFF link for reset 1875 * @link: SFF link to be reset 1876 * @deadline: deadline jiffies for the operation 1877 * 1878 * SFF link @link is about to be reset. Initialize it. It first 1879 * calls ata_std_prereset() and wait for !BSY if the port is 1880 * being softreset. 1881 * 1882 * LOCKING: 1883 * Kernel thread context (may sleep) 1884 * 1885 * RETURNS: 1886 * 0 on success, -errno otherwise. 1887 */ 1888 int ata_sff_prereset(struct ata_link *link, unsigned long deadline) 1889 { 1890 struct ata_eh_context *ehc = &link->eh_context; 1891 int rc; 1892 1893 rc = ata_std_prereset(link, deadline); 1894 if (rc) 1895 return rc; 1896 1897 /* if we're about to do hardreset, nothing more to do */ 1898 if (ehc->i.action & ATA_EH_HARDRESET) 1899 return 0; 1900 1901 /* wait for !BSY if we don't know that no device is attached */ 1902 if (!ata_link_offline(link)) { 1903 rc = ata_sff_wait_ready(link, deadline); 1904 if (rc && rc != -ENODEV) { 1905 ata_link_printk(link, KERN_WARNING, "device not ready " 1906 "(errno=%d), forcing hardreset\n", rc); 1907 ehc->i.action |= ATA_EH_HARDRESET; 1908 } 1909 } 1910 1911 return 0; 1912 } 1913 EXPORT_SYMBOL_GPL(ata_sff_prereset); 1914 1915 /** 1916 * ata_devchk - PATA device presence detection 1917 * @ap: ATA channel to examine 1918 * @device: Device to examine (starting at zero) 1919 * 1920 * This technique was originally described in 1921 * Hale Landis's ATADRVR (www.ata-atapi.com), and 1922 * later found its way into the ATA/ATAPI spec. 1923 * 1924 * Write a pattern to the ATA shadow registers, 1925 * and if a device is present, it will respond by 1926 * correctly storing and echoing back the 1927 * ATA shadow register contents. 1928 * 1929 * LOCKING: 1930 * caller. 1931 */ 1932 static unsigned int ata_devchk(struct ata_port *ap, unsigned int device) 1933 { 1934 struct ata_ioports *ioaddr = &ap->ioaddr; 1935 u8 nsect, lbal; 1936 1937 ap->ops->sff_dev_select(ap, device); 1938 1939 iowrite8(0x55, ioaddr->nsect_addr); 1940 iowrite8(0xaa, ioaddr->lbal_addr); 1941 1942 iowrite8(0xaa, ioaddr->nsect_addr); 1943 iowrite8(0x55, ioaddr->lbal_addr); 1944 1945 iowrite8(0x55, ioaddr->nsect_addr); 1946 iowrite8(0xaa, ioaddr->lbal_addr); 1947 1948 nsect = ioread8(ioaddr->nsect_addr); 1949 lbal = ioread8(ioaddr->lbal_addr); 1950 1951 if ((nsect == 0x55) && (lbal == 0xaa)) 1952 return 1; /* we found a device */ 1953 1954 return 0; /* nothing found */ 1955 } 1956 1957 /** 1958 * ata_sff_dev_classify - Parse returned ATA device signature 1959 * @dev: ATA device to classify (starting at zero) 1960 * @present: device seems present 1961 * @r_err: Value of error register on completion 1962 * 1963 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs, 1964 * an ATA/ATAPI-defined set of values is placed in the ATA 1965 * shadow registers, indicating the results of device detection 1966 * and diagnostics. 1967 * 1968 * Select the ATA device, and read the values from the ATA shadow 1969 * registers. Then parse according to the Error register value, 1970 * and the spec-defined values examined by ata_dev_classify(). 1971 * 1972 * LOCKING: 1973 * caller. 1974 * 1975 * RETURNS: 1976 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE. 1977 */ 1978 unsigned int ata_sff_dev_classify(struct ata_device *dev, int present, 1979 u8 *r_err) 1980 { 1981 struct ata_port *ap = dev->link->ap; 1982 struct ata_taskfile tf; 1983 unsigned int class; 1984 u8 err; 1985 1986 ap->ops->sff_dev_select(ap, dev->devno); 1987 1988 memset(&tf, 0, sizeof(tf)); 1989 1990 ap->ops->sff_tf_read(ap, &tf); 1991 err = tf.feature; 1992 if (r_err) 1993 *r_err = err; 1994 1995 /* see if device passed diags: continue and warn later */ 1996 if (err == 0) 1997 /* diagnostic fail : do nothing _YET_ */ 1998 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC; 1999 else if (err == 1) 2000 /* do nothing */ ; 2001 else if ((dev->devno == 0) && (err == 0x81)) 2002 /* do nothing */ ; 2003 else 2004 return ATA_DEV_NONE; 2005 2006 /* determine if device is ATA or ATAPI */ 2007 class = ata_dev_classify(&tf); 2008 2009 if (class == ATA_DEV_UNKNOWN) { 2010 /* If the device failed diagnostic, it's likely to 2011 * have reported incorrect device signature too. 2012 * Assume ATA device if the device seems present but 2013 * device signature is invalid with diagnostic 2014 * failure. 2015 */ 2016 if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC)) 2017 class = ATA_DEV_ATA; 2018 else 2019 class = ATA_DEV_NONE; 2020 } else if ((class == ATA_DEV_ATA) && 2021 (ap->ops->sff_check_status(ap) == 0)) 2022 class = ATA_DEV_NONE; 2023 2024 return class; 2025 } 2026 EXPORT_SYMBOL_GPL(ata_sff_dev_classify); 2027 2028 /** 2029 * ata_sff_wait_after_reset - wait for devices to become ready after reset 2030 * @link: SFF link which is just reset 2031 * @devmask: mask of present devices 2032 * @deadline: deadline jiffies for the operation 2033 * 2034 * Wait devices attached to SFF @link to become ready after 2035 * reset. It contains preceding 150ms wait to avoid accessing TF 2036 * status register too early. 2037 * 2038 * LOCKING: 2039 * Kernel thread context (may sleep). 2040 * 2041 * RETURNS: 2042 * 0 on success, -ENODEV if some or all of devices in @devmask 2043 * don't seem to exist. -errno on other errors. 2044 */ 2045 int ata_sff_wait_after_reset(struct ata_link *link, unsigned int devmask, 2046 unsigned long deadline) 2047 { 2048 struct ata_port *ap = link->ap; 2049 struct ata_ioports *ioaddr = &ap->ioaddr; 2050 unsigned int dev0 = devmask & (1 << 0); 2051 unsigned int dev1 = devmask & (1 << 1); 2052 int rc, ret = 0; 2053 2054 msleep(ATA_WAIT_AFTER_RESET); 2055 2056 /* always check readiness of the master device */ 2057 rc = ata_sff_wait_ready(link, deadline); 2058 /* -ENODEV means the odd clown forgot the D7 pulldown resistor 2059 * and TF status is 0xff, bail out on it too. 2060 */ 2061 if (rc) 2062 return rc; 2063 2064 /* if device 1 was found in ata_devchk, wait for register 2065 * access briefly, then wait for BSY to clear. 2066 */ 2067 if (dev1) { 2068 int i; 2069 2070 ap->ops->sff_dev_select(ap, 1); 2071 2072 /* Wait for register access. Some ATAPI devices fail 2073 * to set nsect/lbal after reset, so don't waste too 2074 * much time on it. We're gonna wait for !BSY anyway. 2075 */ 2076 for (i = 0; i < 2; i++) { 2077 u8 nsect, lbal; 2078 2079 nsect = ioread8(ioaddr->nsect_addr); 2080 lbal = ioread8(ioaddr->lbal_addr); 2081 if ((nsect == 1) && (lbal == 1)) 2082 break; 2083 msleep(50); /* give drive a breather */ 2084 } 2085 2086 rc = ata_sff_wait_ready(link, deadline); 2087 if (rc) { 2088 if (rc != -ENODEV) 2089 return rc; 2090 ret = rc; 2091 } 2092 } 2093 2094 /* is all this really necessary? */ 2095 ap->ops->sff_dev_select(ap, 0); 2096 if (dev1) 2097 ap->ops->sff_dev_select(ap, 1); 2098 if (dev0) 2099 ap->ops->sff_dev_select(ap, 0); 2100 2101 return ret; 2102 } 2103 EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset); 2104 2105 static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask, 2106 unsigned long deadline) 2107 { 2108 struct ata_ioports *ioaddr = &ap->ioaddr; 2109 2110 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id); 2111 2112 /* software reset. causes dev0 to be selected */ 2113 iowrite8(ap->ctl, ioaddr->ctl_addr); 2114 udelay(20); /* FIXME: flush */ 2115 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr); 2116 udelay(20); /* FIXME: flush */ 2117 iowrite8(ap->ctl, ioaddr->ctl_addr); 2118 ap->last_ctl = ap->ctl; 2119 2120 /* wait the port to become ready */ 2121 return ata_sff_wait_after_reset(&ap->link, devmask, deadline); 2122 } 2123 2124 /** 2125 * ata_sff_softreset - reset host port via ATA SRST 2126 * @link: ATA link to reset 2127 * @classes: resulting classes of attached devices 2128 * @deadline: deadline jiffies for the operation 2129 * 2130 * Reset host port using ATA SRST. 2131 * 2132 * LOCKING: 2133 * Kernel thread context (may sleep) 2134 * 2135 * RETURNS: 2136 * 0 on success, -errno otherwise. 2137 */ 2138 int ata_sff_softreset(struct ata_link *link, unsigned int *classes, 2139 unsigned long deadline) 2140 { 2141 struct ata_port *ap = link->ap; 2142 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS; 2143 unsigned int devmask = 0; 2144 int rc; 2145 u8 err; 2146 2147 DPRINTK("ENTER\n"); 2148 2149 /* determine if device 0/1 are present */ 2150 if (ata_devchk(ap, 0)) 2151 devmask |= (1 << 0); 2152 if (slave_possible && ata_devchk(ap, 1)) 2153 devmask |= (1 << 1); 2154 2155 /* select device 0 again */ 2156 ap->ops->sff_dev_select(ap, 0); 2157 2158 /* issue bus reset */ 2159 DPRINTK("about to softreset, devmask=%x\n", devmask); 2160 rc = ata_bus_softreset(ap, devmask, deadline); 2161 /* if link is occupied, -ENODEV too is an error */ 2162 if (rc && (rc != -ENODEV || sata_scr_valid(link))) { 2163 ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc); 2164 return rc; 2165 } 2166 2167 /* determine by signature whether we have ATA or ATAPI devices */ 2168 classes[0] = ata_sff_dev_classify(&link->device[0], 2169 devmask & (1 << 0), &err); 2170 if (slave_possible && err != 0x81) 2171 classes[1] = ata_sff_dev_classify(&link->device[1], 2172 devmask & (1 << 1), &err); 2173 2174 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]); 2175 return 0; 2176 } 2177 EXPORT_SYMBOL_GPL(ata_sff_softreset); 2178 2179 /** 2180 * sata_sff_hardreset - reset host port via SATA phy reset 2181 * @link: link to reset 2182 * @class: resulting class of attached device 2183 * @deadline: deadline jiffies for the operation 2184 * 2185 * SATA phy-reset host port using DET bits of SControl register, 2186 * wait for !BSY and classify the attached device. 2187 * 2188 * LOCKING: 2189 * Kernel thread context (may sleep) 2190 * 2191 * RETURNS: 2192 * 0 on success, -errno otherwise. 2193 */ 2194 int sata_sff_hardreset(struct ata_link *link, unsigned int *class, 2195 unsigned long deadline) 2196 { 2197 struct ata_eh_context *ehc = &link->eh_context; 2198 const unsigned long *timing = sata_ehc_deb_timing(ehc); 2199 bool online; 2200 int rc; 2201 2202 rc = sata_link_hardreset(link, timing, deadline, &online, 2203 ata_sff_check_ready); 2204 if (online) 2205 *class = ata_sff_dev_classify(link->device, 1, NULL); 2206 2207 DPRINTK("EXIT, class=%u\n", *class); 2208 return rc; 2209 } 2210 EXPORT_SYMBOL_GPL(sata_sff_hardreset); 2211 2212 /** 2213 * ata_sff_postreset - SFF postreset callback 2214 * @link: the target SFF ata_link 2215 * @classes: classes of attached devices 2216 * 2217 * This function is invoked after a successful reset. It first 2218 * calls ata_std_postreset() and performs SFF specific postreset 2219 * processing. 2220 * 2221 * LOCKING: 2222 * Kernel thread context (may sleep) 2223 */ 2224 void ata_sff_postreset(struct ata_link *link, unsigned int *classes) 2225 { 2226 struct ata_port *ap = link->ap; 2227 2228 ata_std_postreset(link, classes); 2229 2230 /* is double-select really necessary? */ 2231 if (classes[0] != ATA_DEV_NONE) 2232 ap->ops->sff_dev_select(ap, 1); 2233 if (classes[1] != ATA_DEV_NONE) 2234 ap->ops->sff_dev_select(ap, 0); 2235 2236 /* bail out if no device is present */ 2237 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) { 2238 DPRINTK("EXIT, no device\n"); 2239 return; 2240 } 2241 2242 /* set up device control */ 2243 if (ap->ioaddr.ctl_addr) { 2244 iowrite8(ap->ctl, ap->ioaddr.ctl_addr); 2245 ap->last_ctl = ap->ctl; 2246 } 2247 } 2248 EXPORT_SYMBOL_GPL(ata_sff_postreset); 2249 2250 /** 2251 * ata_sff_drain_fifo - Stock FIFO drain logic for SFF controllers 2252 * @qc: command 2253 * 2254 * Drain the FIFO and device of any stuck data following a command 2255 * failing to complete. In some cases this is neccessary before a 2256 * reset will recover the device. 2257 * 2258 */ 2259 2260 void ata_sff_drain_fifo(struct ata_queued_cmd *qc) 2261 { 2262 int count; 2263 struct ata_port *ap; 2264 2265 /* We only need to flush incoming data when a command was running */ 2266 if (qc == NULL || qc->dma_dir == DMA_TO_DEVICE) 2267 return; 2268 2269 ap = qc->ap; 2270 /* Drain up to 64K of data before we give up this recovery method */ 2271 for (count = 0; (ap->ops->sff_check_status(ap) & ATA_DRQ) 2272 && count < 32768; count++) 2273 ioread16(ap->ioaddr.data_addr); 2274 2275 /* Can become DEBUG later */ 2276 if (count) 2277 ata_port_printk(ap, KERN_DEBUG, 2278 "drained %d bytes to clear DRQ.\n", count); 2279 2280 } 2281 EXPORT_SYMBOL_GPL(ata_sff_drain_fifo); 2282 2283 /** 2284 * ata_sff_error_handler - Stock error handler for BMDMA controller 2285 * @ap: port to handle error for 2286 * 2287 * Stock error handler for SFF controller. It can handle both 2288 * PATA and SATA controllers. Many controllers should be able to 2289 * use this EH as-is or with some added handling before and 2290 * after. 2291 * 2292 * LOCKING: 2293 * Kernel thread context (may sleep) 2294 */ 2295 void ata_sff_error_handler(struct ata_port *ap) 2296 { 2297 ata_reset_fn_t softreset = ap->ops->softreset; 2298 ata_reset_fn_t hardreset = ap->ops->hardreset; 2299 struct ata_queued_cmd *qc; 2300 unsigned long flags; 2301 int thaw = 0; 2302 2303 qc = __ata_qc_from_tag(ap, ap->link.active_tag); 2304 if (qc && !(qc->flags & ATA_QCFLAG_FAILED)) 2305 qc = NULL; 2306 2307 /* reset PIO HSM and stop DMA engine */ 2308 spin_lock_irqsave(ap->lock, flags); 2309 2310 ap->hsm_task_state = HSM_ST_IDLE; 2311 2312 if (ap->ioaddr.bmdma_addr && 2313 qc && (qc->tf.protocol == ATA_PROT_DMA || 2314 qc->tf.protocol == ATAPI_PROT_DMA)) { 2315 u8 host_stat; 2316 2317 host_stat = ap->ops->bmdma_status(ap); 2318 2319 /* BMDMA controllers indicate host bus error by 2320 * setting DMA_ERR bit and timing out. As it wasn't 2321 * really a timeout event, adjust error mask and 2322 * cancel frozen state. 2323 */ 2324 if (qc->err_mask == AC_ERR_TIMEOUT 2325 && (host_stat & ATA_DMA_ERR)) { 2326 qc->err_mask = AC_ERR_HOST_BUS; 2327 thaw = 1; 2328 } 2329 2330 ap->ops->bmdma_stop(qc); 2331 } 2332 2333 ata_sff_sync(ap); /* FIXME: We don't need this */ 2334 ap->ops->sff_check_status(ap); 2335 ap->ops->sff_irq_clear(ap); 2336 /* We *MUST* do FIFO draining before we issue a reset as several 2337 * devices helpfully clear their internal state and will lock solid 2338 * if we touch the data port post reset. Pass qc in case anyone wants 2339 * to do different PIO/DMA recovery or has per command fixups 2340 */ 2341 if (ap->ops->drain_fifo) 2342 ap->ops->drain_fifo(qc); 2343 2344 spin_unlock_irqrestore(ap->lock, flags); 2345 2346 if (thaw) 2347 ata_eh_thaw_port(ap); 2348 2349 /* PIO and DMA engines have been stopped, perform recovery */ 2350 2351 /* Ignore ata_sff_softreset if ctl isn't accessible and 2352 * built-in hardresets if SCR access isn't available. 2353 */ 2354 if (softreset == ata_sff_softreset && !ap->ioaddr.ctl_addr) 2355 softreset = NULL; 2356 if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link)) 2357 hardreset = NULL; 2358 2359 ata_do_eh(ap, ap->ops->prereset, softreset, hardreset, 2360 ap->ops->postreset); 2361 } 2362 EXPORT_SYMBOL_GPL(ata_sff_error_handler); 2363 2364 /** 2365 * ata_sff_post_internal_cmd - Stock post_internal_cmd for SFF controller 2366 * @qc: internal command to clean up 2367 * 2368 * LOCKING: 2369 * Kernel thread context (may sleep) 2370 */ 2371 void ata_sff_post_internal_cmd(struct ata_queued_cmd *qc) 2372 { 2373 struct ata_port *ap = qc->ap; 2374 unsigned long flags; 2375 2376 spin_lock_irqsave(ap->lock, flags); 2377 2378 ap->hsm_task_state = HSM_ST_IDLE; 2379 2380 if (ap->ioaddr.bmdma_addr) 2381 ata_bmdma_stop(qc); 2382 2383 spin_unlock_irqrestore(ap->lock, flags); 2384 } 2385 EXPORT_SYMBOL_GPL(ata_sff_post_internal_cmd); 2386 2387 /** 2388 * ata_sff_port_start - Set port up for dma. 2389 * @ap: Port to initialize 2390 * 2391 * Called just after data structures for each port are 2392 * initialized. Allocates space for PRD table if the device 2393 * is DMA capable SFF. 2394 * 2395 * May be used as the port_start() entry in ata_port_operations. 2396 * 2397 * LOCKING: 2398 * Inherited from caller. 2399 */ 2400 int ata_sff_port_start(struct ata_port *ap) 2401 { 2402 if (ap->ioaddr.bmdma_addr) 2403 return ata_port_start(ap); 2404 return 0; 2405 } 2406 EXPORT_SYMBOL_GPL(ata_sff_port_start); 2407 2408 /** 2409 * ata_sff_port_start32 - Set port up for dma. 2410 * @ap: Port to initialize 2411 * 2412 * Called just after data structures for each port are 2413 * initialized. Allocates space for PRD table if the device 2414 * is DMA capable SFF. 2415 * 2416 * May be used as the port_start() entry in ata_port_operations for 2417 * devices that are capable of 32bit PIO. 2418 * 2419 * LOCKING: 2420 * Inherited from caller. 2421 */ 2422 int ata_sff_port_start32(struct ata_port *ap) 2423 { 2424 ap->pflags |= ATA_PFLAG_PIO32 | ATA_PFLAG_PIO32CHANGE; 2425 if (ap->ioaddr.bmdma_addr) 2426 return ata_port_start(ap); 2427 return 0; 2428 } 2429 EXPORT_SYMBOL_GPL(ata_sff_port_start32); 2430 2431 /** 2432 * ata_sff_std_ports - initialize ioaddr with standard port offsets. 2433 * @ioaddr: IO address structure to be initialized 2434 * 2435 * Utility function which initializes data_addr, error_addr, 2436 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr, 2437 * device_addr, status_addr, and command_addr to standard offsets 2438 * relative to cmd_addr. 2439 * 2440 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr. 2441 */ 2442 void ata_sff_std_ports(struct ata_ioports *ioaddr) 2443 { 2444 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA; 2445 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR; 2446 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE; 2447 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT; 2448 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL; 2449 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM; 2450 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH; 2451 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE; 2452 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS; 2453 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD; 2454 } 2455 EXPORT_SYMBOL_GPL(ata_sff_std_ports); 2456 2457 unsigned long ata_bmdma_mode_filter(struct ata_device *adev, 2458 unsigned long xfer_mask) 2459 { 2460 /* Filter out DMA modes if the device has been configured by 2461 the BIOS as PIO only */ 2462 2463 if (adev->link->ap->ioaddr.bmdma_addr == NULL) 2464 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA); 2465 return xfer_mask; 2466 } 2467 EXPORT_SYMBOL_GPL(ata_bmdma_mode_filter); 2468 2469 /** 2470 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction 2471 * @qc: Info associated with this ATA transaction. 2472 * 2473 * LOCKING: 2474 * spin_lock_irqsave(host lock) 2475 */ 2476 void ata_bmdma_setup(struct ata_queued_cmd *qc) 2477 { 2478 struct ata_port *ap = qc->ap; 2479 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE); 2480 u8 dmactl; 2481 2482 /* load PRD table addr. */ 2483 mb(); /* make sure PRD table writes are visible to controller */ 2484 iowrite32(ap->prd_dma, ap->ioaddr.bmdma_addr + ATA_DMA_TABLE_OFS); 2485 2486 /* specify data direction, triple-check start bit is clear */ 2487 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD); 2488 dmactl &= ~(ATA_DMA_WR | ATA_DMA_START); 2489 if (!rw) 2490 dmactl |= ATA_DMA_WR; 2491 iowrite8(dmactl, ap->ioaddr.bmdma_addr + ATA_DMA_CMD); 2492 2493 /* issue r/w command */ 2494 ap->ops->sff_exec_command(ap, &qc->tf); 2495 } 2496 EXPORT_SYMBOL_GPL(ata_bmdma_setup); 2497 2498 /** 2499 * ata_bmdma_start - Start a PCI IDE BMDMA transaction 2500 * @qc: Info associated with this ATA transaction. 2501 * 2502 * LOCKING: 2503 * spin_lock_irqsave(host lock) 2504 */ 2505 void ata_bmdma_start(struct ata_queued_cmd *qc) 2506 { 2507 struct ata_port *ap = qc->ap; 2508 u8 dmactl; 2509 2510 /* start host DMA transaction */ 2511 dmactl = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_CMD); 2512 iowrite8(dmactl | ATA_DMA_START, ap->ioaddr.bmdma_addr + ATA_DMA_CMD); 2513 2514 /* Strictly, one may wish to issue an ioread8() here, to 2515 * flush the mmio write. However, control also passes 2516 * to the hardware at this point, and it will interrupt 2517 * us when we are to resume control. So, in effect, 2518 * we don't care when the mmio write flushes. 2519 * Further, a read of the DMA status register _immediately_ 2520 * following the write may not be what certain flaky hardware 2521 * is expected, so I think it is best to not add a readb() 2522 * without first all the MMIO ATA cards/mobos. 2523 * Or maybe I'm just being paranoid. 2524 * 2525 * FIXME: The posting of this write means I/O starts are 2526 * unneccessarily delayed for MMIO 2527 */ 2528 } 2529 EXPORT_SYMBOL_GPL(ata_bmdma_start); 2530 2531 /** 2532 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer 2533 * @qc: Command we are ending DMA for 2534 * 2535 * Clears the ATA_DMA_START flag in the dma control register 2536 * 2537 * May be used as the bmdma_stop() entry in ata_port_operations. 2538 * 2539 * LOCKING: 2540 * spin_lock_irqsave(host lock) 2541 */ 2542 void ata_bmdma_stop(struct ata_queued_cmd *qc) 2543 { 2544 struct ata_port *ap = qc->ap; 2545 void __iomem *mmio = ap->ioaddr.bmdma_addr; 2546 2547 /* clear start/stop bit */ 2548 iowrite8(ioread8(mmio + ATA_DMA_CMD) & ~ATA_DMA_START, 2549 mmio + ATA_DMA_CMD); 2550 2551 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */ 2552 ata_sff_dma_pause(ap); 2553 } 2554 EXPORT_SYMBOL_GPL(ata_bmdma_stop); 2555 2556 /** 2557 * ata_bmdma_status - Read PCI IDE BMDMA status 2558 * @ap: Port associated with this ATA transaction. 2559 * 2560 * Read and return BMDMA status register. 2561 * 2562 * May be used as the bmdma_status() entry in ata_port_operations. 2563 * 2564 * LOCKING: 2565 * spin_lock_irqsave(host lock) 2566 */ 2567 u8 ata_bmdma_status(struct ata_port *ap) 2568 { 2569 return ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS); 2570 } 2571 EXPORT_SYMBOL_GPL(ata_bmdma_status); 2572 2573 /** 2574 * ata_bus_reset - reset host port and associated ATA channel 2575 * @ap: port to reset 2576 * 2577 * This is typically the first time we actually start issuing 2578 * commands to the ATA channel. We wait for BSY to clear, then 2579 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its 2580 * result. Determine what devices, if any, are on the channel 2581 * by looking at the device 0/1 error register. Look at the signature 2582 * stored in each device's taskfile registers, to determine if 2583 * the device is ATA or ATAPI. 2584 * 2585 * LOCKING: 2586 * PCI/etc. bus probe sem. 2587 * Obtains host lock. 2588 * 2589 * SIDE EFFECTS: 2590 * Sets ATA_FLAG_DISABLED if bus reset fails. 2591 * 2592 * DEPRECATED: 2593 * This function is only for drivers which still use old EH and 2594 * will be removed soon. 2595 */ 2596 void ata_bus_reset(struct ata_port *ap) 2597 { 2598 struct ata_device *device = ap->link.device; 2599 struct ata_ioports *ioaddr = &ap->ioaddr; 2600 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS; 2601 u8 err; 2602 unsigned int dev0, dev1 = 0, devmask = 0; 2603 int rc; 2604 2605 DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no); 2606 2607 /* determine if device 0/1 are present */ 2608 if (ap->flags & ATA_FLAG_SATA_RESET) 2609 dev0 = 1; 2610 else { 2611 dev0 = ata_devchk(ap, 0); 2612 if (slave_possible) 2613 dev1 = ata_devchk(ap, 1); 2614 } 2615 2616 if (dev0) 2617 devmask |= (1 << 0); 2618 if (dev1) 2619 devmask |= (1 << 1); 2620 2621 /* select device 0 again */ 2622 ap->ops->sff_dev_select(ap, 0); 2623 2624 /* issue bus reset */ 2625 if (ap->flags & ATA_FLAG_SRST) { 2626 rc = ata_bus_softreset(ap, devmask, 2627 ata_deadline(jiffies, 40000)); 2628 if (rc && rc != -ENODEV) 2629 goto err_out; 2630 } 2631 2632 /* 2633 * determine by signature whether we have ATA or ATAPI devices 2634 */ 2635 device[0].class = ata_sff_dev_classify(&device[0], dev0, &err); 2636 if ((slave_possible) && (err != 0x81)) 2637 device[1].class = ata_sff_dev_classify(&device[1], dev1, &err); 2638 2639 /* is double-select really necessary? */ 2640 if (device[1].class != ATA_DEV_NONE) 2641 ap->ops->sff_dev_select(ap, 1); 2642 if (device[0].class != ATA_DEV_NONE) 2643 ap->ops->sff_dev_select(ap, 0); 2644 2645 /* if no devices were detected, disable this port */ 2646 if ((device[0].class == ATA_DEV_NONE) && 2647 (device[1].class == ATA_DEV_NONE)) 2648 goto err_out; 2649 2650 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) { 2651 /* set up device control for ATA_FLAG_SATA_RESET */ 2652 iowrite8(ap->ctl, ioaddr->ctl_addr); 2653 ap->last_ctl = ap->ctl; 2654 } 2655 2656 DPRINTK("EXIT\n"); 2657 return; 2658 2659 err_out: 2660 ata_port_printk(ap, KERN_ERR, "disabling port\n"); 2661 ata_port_disable(ap); 2662 2663 DPRINTK("EXIT\n"); 2664 } 2665 EXPORT_SYMBOL_GPL(ata_bus_reset); 2666 2667 #ifdef CONFIG_PCI 2668 2669 /** 2670 * ata_pci_bmdma_clear_simplex - attempt to kick device out of simplex 2671 * @pdev: PCI device 2672 * 2673 * Some PCI ATA devices report simplex mode but in fact can be told to 2674 * enter non simplex mode. This implements the necessary logic to 2675 * perform the task on such devices. Calling it on other devices will 2676 * have -undefined- behaviour. 2677 */ 2678 int ata_pci_bmdma_clear_simplex(struct pci_dev *pdev) 2679 { 2680 unsigned long bmdma = pci_resource_start(pdev, 4); 2681 u8 simplex; 2682 2683 if (bmdma == 0) 2684 return -ENOENT; 2685 2686 simplex = inb(bmdma + 0x02); 2687 outb(simplex & 0x60, bmdma + 0x02); 2688 simplex = inb(bmdma + 0x02); 2689 if (simplex & 0x80) 2690 return -EOPNOTSUPP; 2691 return 0; 2692 } 2693 EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex); 2694 2695 /** 2696 * ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host 2697 * @host: target ATA host 2698 * 2699 * Acquire PCI BMDMA resources and initialize @host accordingly. 2700 * 2701 * LOCKING: 2702 * Inherited from calling layer (may sleep). 2703 * 2704 * RETURNS: 2705 * 0 on success, -errno otherwise. 2706 */ 2707 int ata_pci_bmdma_init(struct ata_host *host) 2708 { 2709 struct device *gdev = host->dev; 2710 struct pci_dev *pdev = to_pci_dev(gdev); 2711 int i, rc; 2712 2713 /* No BAR4 allocation: No DMA */ 2714 if (pci_resource_start(pdev, 4) == 0) 2715 return 0; 2716 2717 /* TODO: If we get no DMA mask we should fall back to PIO */ 2718 rc = pci_set_dma_mask(pdev, ATA_DMA_MASK); 2719 if (rc) 2720 return rc; 2721 rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK); 2722 if (rc) 2723 return rc; 2724 2725 /* request and iomap DMA region */ 2726 rc = pcim_iomap_regions(pdev, 1 << 4, dev_driver_string(gdev)); 2727 if (rc) { 2728 dev_printk(KERN_ERR, gdev, "failed to request/iomap BAR4\n"); 2729 return -ENOMEM; 2730 } 2731 host->iomap = pcim_iomap_table(pdev); 2732 2733 for (i = 0; i < 2; i++) { 2734 struct ata_port *ap = host->ports[i]; 2735 void __iomem *bmdma = host->iomap[4] + 8 * i; 2736 2737 if (ata_port_is_dummy(ap)) 2738 continue; 2739 2740 ap->ioaddr.bmdma_addr = bmdma; 2741 if ((!(ap->flags & ATA_FLAG_IGN_SIMPLEX)) && 2742 (ioread8(bmdma + 2) & 0x80)) 2743 host->flags |= ATA_HOST_SIMPLEX; 2744 2745 ata_port_desc(ap, "bmdma 0x%llx", 2746 (unsigned long long)pci_resource_start(pdev, 4) + 8 * i); 2747 } 2748 2749 return 0; 2750 } 2751 EXPORT_SYMBOL_GPL(ata_pci_bmdma_init); 2752 2753 static int ata_resources_present(struct pci_dev *pdev, int port) 2754 { 2755 int i; 2756 2757 /* Check the PCI resources for this channel are enabled */ 2758 port = port * 2; 2759 for (i = 0; i < 2; i++) { 2760 if (pci_resource_start(pdev, port + i) == 0 || 2761 pci_resource_len(pdev, port + i) == 0) 2762 return 0; 2763 } 2764 return 1; 2765 } 2766 2767 /** 2768 * ata_pci_sff_init_host - acquire native PCI ATA resources and init host 2769 * @host: target ATA host 2770 * 2771 * Acquire native PCI ATA resources for @host and initialize the 2772 * first two ports of @host accordingly. Ports marked dummy are 2773 * skipped and allocation failure makes the port dummy. 2774 * 2775 * Note that native PCI resources are valid even for legacy hosts 2776 * as we fix up pdev resources array early in boot, so this 2777 * function can be used for both native and legacy SFF hosts. 2778 * 2779 * LOCKING: 2780 * Inherited from calling layer (may sleep). 2781 * 2782 * RETURNS: 2783 * 0 if at least one port is initialized, -ENODEV if no port is 2784 * available. 2785 */ 2786 int ata_pci_sff_init_host(struct ata_host *host) 2787 { 2788 struct device *gdev = host->dev; 2789 struct pci_dev *pdev = to_pci_dev(gdev); 2790 unsigned int mask = 0; 2791 int i, rc; 2792 2793 /* request, iomap BARs and init port addresses accordingly */ 2794 for (i = 0; i < 2; i++) { 2795 struct ata_port *ap = host->ports[i]; 2796 int base = i * 2; 2797 void __iomem * const *iomap; 2798 2799 if (ata_port_is_dummy(ap)) 2800 continue; 2801 2802 /* Discard disabled ports. Some controllers show 2803 * their unused channels this way. Disabled ports are 2804 * made dummy. 2805 */ 2806 if (!ata_resources_present(pdev, i)) { 2807 ap->ops = &ata_dummy_port_ops; 2808 continue; 2809 } 2810 2811 rc = pcim_iomap_regions(pdev, 0x3 << base, 2812 dev_driver_string(gdev)); 2813 if (rc) { 2814 dev_printk(KERN_WARNING, gdev, 2815 "failed to request/iomap BARs for port %d " 2816 "(errno=%d)\n", i, rc); 2817 if (rc == -EBUSY) 2818 pcim_pin_device(pdev); 2819 ap->ops = &ata_dummy_port_ops; 2820 continue; 2821 } 2822 host->iomap = iomap = pcim_iomap_table(pdev); 2823 2824 ap->ioaddr.cmd_addr = iomap[base]; 2825 ap->ioaddr.altstatus_addr = 2826 ap->ioaddr.ctl_addr = (void __iomem *) 2827 ((unsigned long)iomap[base + 1] | ATA_PCI_CTL_OFS); 2828 ata_sff_std_ports(&ap->ioaddr); 2829 2830 ata_port_desc(ap, "cmd 0x%llx ctl 0x%llx", 2831 (unsigned long long)pci_resource_start(pdev, base), 2832 (unsigned long long)pci_resource_start(pdev, base + 1)); 2833 2834 mask |= 1 << i; 2835 } 2836 2837 if (!mask) { 2838 dev_printk(KERN_ERR, gdev, "no available native port\n"); 2839 return -ENODEV; 2840 } 2841 2842 return 0; 2843 } 2844 EXPORT_SYMBOL_GPL(ata_pci_sff_init_host); 2845 2846 /** 2847 * ata_pci_sff_prepare_host - helper to prepare native PCI ATA host 2848 * @pdev: target PCI device 2849 * @ppi: array of port_info, must be enough for two ports 2850 * @r_host: out argument for the initialized ATA host 2851 * 2852 * Helper to allocate ATA host for @pdev, acquire all native PCI 2853 * resources and initialize it accordingly in one go. 2854 * 2855 * LOCKING: 2856 * Inherited from calling layer (may sleep). 2857 * 2858 * RETURNS: 2859 * 0 on success, -errno otherwise. 2860 */ 2861 int ata_pci_sff_prepare_host(struct pci_dev *pdev, 2862 const struct ata_port_info * const *ppi, 2863 struct ata_host **r_host) 2864 { 2865 struct ata_host *host; 2866 int rc; 2867 2868 if (!devres_open_group(&pdev->dev, NULL, GFP_KERNEL)) 2869 return -ENOMEM; 2870 2871 host = ata_host_alloc_pinfo(&pdev->dev, ppi, 2); 2872 if (!host) { 2873 dev_printk(KERN_ERR, &pdev->dev, 2874 "failed to allocate ATA host\n"); 2875 rc = -ENOMEM; 2876 goto err_out; 2877 } 2878 2879 rc = ata_pci_sff_init_host(host); 2880 if (rc) 2881 goto err_out; 2882 2883 /* init DMA related stuff */ 2884 rc = ata_pci_bmdma_init(host); 2885 if (rc) 2886 goto err_bmdma; 2887 2888 devres_remove_group(&pdev->dev, NULL); 2889 *r_host = host; 2890 return 0; 2891 2892 err_bmdma: 2893 /* This is necessary because PCI and iomap resources are 2894 * merged and releasing the top group won't release the 2895 * acquired resources if some of those have been acquired 2896 * before entering this function. 2897 */ 2898 pcim_iounmap_regions(pdev, 0xf); 2899 err_out: 2900 devres_release_group(&pdev->dev, NULL); 2901 return rc; 2902 } 2903 EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host); 2904 2905 /** 2906 * ata_pci_sff_activate_host - start SFF host, request IRQ and register it 2907 * @host: target SFF ATA host 2908 * @irq_handler: irq_handler used when requesting IRQ(s) 2909 * @sht: scsi_host_template to use when registering the host 2910 * 2911 * This is the counterpart of ata_host_activate() for SFF ATA 2912 * hosts. This separate helper is necessary because SFF hosts 2913 * use two separate interrupts in legacy mode. 2914 * 2915 * LOCKING: 2916 * Inherited from calling layer (may sleep). 2917 * 2918 * RETURNS: 2919 * 0 on success, -errno otherwise. 2920 */ 2921 int ata_pci_sff_activate_host(struct ata_host *host, 2922 irq_handler_t irq_handler, 2923 struct scsi_host_template *sht) 2924 { 2925 struct device *dev = host->dev; 2926 struct pci_dev *pdev = to_pci_dev(dev); 2927 const char *drv_name = dev_driver_string(host->dev); 2928 int legacy_mode = 0, rc; 2929 2930 rc = ata_host_start(host); 2931 if (rc) 2932 return rc; 2933 2934 if ((pdev->class >> 8) == PCI_CLASS_STORAGE_IDE) { 2935 u8 tmp8, mask; 2936 2937 /* TODO: What if one channel is in native mode ... */ 2938 pci_read_config_byte(pdev, PCI_CLASS_PROG, &tmp8); 2939 mask = (1 << 2) | (1 << 0); 2940 if ((tmp8 & mask) != mask) 2941 legacy_mode = 1; 2942 #if defined(CONFIG_NO_ATA_LEGACY) 2943 /* Some platforms with PCI limits cannot address compat 2944 port space. In that case we punt if their firmware has 2945 left a device in compatibility mode */ 2946 if (legacy_mode) { 2947 printk(KERN_ERR "ata: Compatibility mode ATA is not supported on this platform, skipping.\n"); 2948 return -EOPNOTSUPP; 2949 } 2950 #endif 2951 } 2952 2953 if (!devres_open_group(dev, NULL, GFP_KERNEL)) 2954 return -ENOMEM; 2955 2956 if (!legacy_mode && pdev->irq) { 2957 rc = devm_request_irq(dev, pdev->irq, irq_handler, 2958 IRQF_SHARED, drv_name, host); 2959 if (rc) 2960 goto out; 2961 2962 ata_port_desc(host->ports[0], "irq %d", pdev->irq); 2963 ata_port_desc(host->ports[1], "irq %d", pdev->irq); 2964 } else if (legacy_mode) { 2965 if (!ata_port_is_dummy(host->ports[0])) { 2966 rc = devm_request_irq(dev, ATA_PRIMARY_IRQ(pdev), 2967 irq_handler, IRQF_SHARED, 2968 drv_name, host); 2969 if (rc) 2970 goto out; 2971 2972 ata_port_desc(host->ports[0], "irq %d", 2973 ATA_PRIMARY_IRQ(pdev)); 2974 } 2975 2976 if (!ata_port_is_dummy(host->ports[1])) { 2977 rc = devm_request_irq(dev, ATA_SECONDARY_IRQ(pdev), 2978 irq_handler, IRQF_SHARED, 2979 drv_name, host); 2980 if (rc) 2981 goto out; 2982 2983 ata_port_desc(host->ports[1], "irq %d", 2984 ATA_SECONDARY_IRQ(pdev)); 2985 } 2986 } 2987 2988 rc = ata_host_register(host, sht); 2989 out: 2990 if (rc == 0) 2991 devres_remove_group(dev, NULL); 2992 else 2993 devres_release_group(dev, NULL); 2994 2995 return rc; 2996 } 2997 EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host); 2998 2999 /** 3000 * ata_pci_sff_init_one - Initialize/register PCI IDE host controller 3001 * @pdev: Controller to be initialized 3002 * @ppi: array of port_info, must be enough for two ports 3003 * @sht: scsi_host_template to use when registering the host 3004 * @host_priv: host private_data 3005 * 3006 * This is a helper function which can be called from a driver's 3007 * xxx_init_one() probe function if the hardware uses traditional 3008 * IDE taskfile registers. 3009 * 3010 * This function calls pci_enable_device(), reserves its register 3011 * regions, sets the dma mask, enables bus master mode, and calls 3012 * ata_device_add() 3013 * 3014 * ASSUMPTION: 3015 * Nobody makes a single channel controller that appears solely as 3016 * the secondary legacy port on PCI. 3017 * 3018 * LOCKING: 3019 * Inherited from PCI layer (may sleep). 3020 * 3021 * RETURNS: 3022 * Zero on success, negative on errno-based value on error. 3023 */ 3024 int ata_pci_sff_init_one(struct pci_dev *pdev, 3025 const struct ata_port_info * const *ppi, 3026 struct scsi_host_template *sht, void *host_priv) 3027 { 3028 struct device *dev = &pdev->dev; 3029 const struct ata_port_info *pi = NULL; 3030 struct ata_host *host = NULL; 3031 int i, rc; 3032 3033 DPRINTK("ENTER\n"); 3034 3035 /* look up the first valid port_info */ 3036 for (i = 0; i < 2 && ppi[i]; i++) { 3037 if (ppi[i]->port_ops != &ata_dummy_port_ops) { 3038 pi = ppi[i]; 3039 break; 3040 } 3041 } 3042 3043 if (!pi) { 3044 dev_printk(KERN_ERR, &pdev->dev, 3045 "no valid port_info specified\n"); 3046 return -EINVAL; 3047 } 3048 3049 if (!devres_open_group(dev, NULL, GFP_KERNEL)) 3050 return -ENOMEM; 3051 3052 rc = pcim_enable_device(pdev); 3053 if (rc) 3054 goto out; 3055 3056 /* prepare and activate SFF host */ 3057 rc = ata_pci_sff_prepare_host(pdev, ppi, &host); 3058 if (rc) 3059 goto out; 3060 host->private_data = host_priv; 3061 3062 pci_set_master(pdev); 3063 rc = ata_pci_sff_activate_host(host, ata_sff_interrupt, sht); 3064 out: 3065 if (rc == 0) 3066 devres_remove_group(&pdev->dev, NULL); 3067 else 3068 devres_release_group(&pdev->dev, NULL); 3069 3070 return rc; 3071 } 3072 EXPORT_SYMBOL_GPL(ata_pci_sff_init_one); 3073 3074 #endif /* CONFIG_PCI */ 3075