2 * libata-sff.c - helper library for PCI IDE BMDMA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2006 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2006 Jeff Garzik
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)
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.
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.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/kernel.h>
36 #include <linux/pci.h>
37 #include <linux/libata.h>
38 #include <linux/highmem.h>
42 const struct ata_port_operations ata_sff_port_ops
= {
43 .inherits
= &ata_base_port_ops
,
45 .qc_prep
= ata_sff_qc_prep
,
46 .qc_issue
= ata_sff_qc_issue
,
47 .qc_fill_rtf
= ata_sff_qc_fill_rtf
,
49 .freeze
= ata_sff_freeze
,
51 .prereset
= ata_sff_prereset
,
52 .softreset
= ata_sff_softreset
,
53 .hardreset
= sata_sff_hardreset
,
54 .postreset
= ata_sff_postreset
,
55 .error_handler
= ata_sff_error_handler
,
56 .post_internal_cmd
= ata_sff_post_internal_cmd
,
58 .sff_dev_select
= ata_sff_dev_select
,
59 .sff_check_status
= ata_sff_check_status
,
60 .sff_tf_load
= ata_sff_tf_load
,
61 .sff_tf_read
= ata_sff_tf_read
,
62 .sff_exec_command
= ata_sff_exec_command
,
63 .sff_data_xfer
= ata_sff_data_xfer
,
64 .sff_irq_on
= ata_sff_irq_on
,
65 .sff_irq_clear
= ata_sff_irq_clear
,
67 .port_start
= ata_sff_port_start
,
70 const struct ata_port_operations ata_bmdma_port_ops
= {
71 .inherits
= &ata_sff_port_ops
,
73 .mode_filter
= ata_bmdma_mode_filter
,
75 .bmdma_setup
= ata_bmdma_setup
,
76 .bmdma_start
= ata_bmdma_start
,
77 .bmdma_stop
= ata_bmdma_stop
,
78 .bmdma_status
= ata_bmdma_status
,
82 * ata_fill_sg - Fill PCI IDE PRD table
83 * @qc: Metadata associated with taskfile to be transferred
85 * Fill PCI IDE PRD (scatter-gather) table with segments
86 * associated with the current disk command.
89 * spin_lock_irqsave(host lock)
92 static void ata_fill_sg(struct ata_queued_cmd
*qc
)
94 struct ata_port
*ap
= qc
->ap
;
95 struct scatterlist
*sg
;
99 for_each_sg(qc
->sg
, sg
, qc
->n_elem
, si
) {
103 /* determine if physical DMA addr spans 64K boundary.
104 * Note h/w doesn't support 64-bit, so we unconditionally
105 * truncate dma_addr_t to u32.
107 addr
= (u32
) sg_dma_address(sg
);
108 sg_len
= sg_dma_len(sg
);
111 offset
= addr
& 0xffff;
113 if ((offset
+ sg_len
) > 0x10000)
114 len
= 0x10000 - offset
;
116 ap
->prd
[pi
].addr
= cpu_to_le32(addr
);
117 ap
->prd
[pi
].flags_len
= cpu_to_le32(len
& 0xffff);
118 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi
, addr
, len
);
126 ap
->prd
[pi
- 1].flags_len
|= cpu_to_le32(ATA_PRD_EOT
);
130 * ata_fill_sg_dumb - Fill PCI IDE PRD table
131 * @qc: Metadata associated with taskfile to be transferred
133 * Fill PCI IDE PRD (scatter-gather) table with segments
134 * associated with the current disk command. Perform the fill
135 * so that we avoid writing any length 64K records for
136 * controllers that don't follow the spec.
139 * spin_lock_irqsave(host lock)
142 static void ata_fill_sg_dumb(struct ata_queued_cmd
*qc
)
144 struct ata_port
*ap
= qc
->ap
;
145 struct scatterlist
*sg
;
149 for_each_sg(qc
->sg
, sg
, qc
->n_elem
, si
) {
151 u32 sg_len
, len
, blen
;
153 /* determine if physical DMA addr spans 64K boundary.
154 * Note h/w doesn't support 64-bit, so we unconditionally
155 * truncate dma_addr_t to u32.
157 addr
= (u32
) sg_dma_address(sg
);
158 sg_len
= sg_dma_len(sg
);
161 offset
= addr
& 0xffff;
163 if ((offset
+ sg_len
) > 0x10000)
164 len
= 0x10000 - offset
;
167 ap
->prd
[pi
].addr
= cpu_to_le32(addr
);
169 /* Some PATA chipsets like the CS5530 can't
170 cope with 0x0000 meaning 64K as the spec says */
171 ap
->prd
[pi
].flags_len
= cpu_to_le32(0x8000);
173 ap
->prd
[++pi
].addr
= cpu_to_le32(addr
+ 0x8000);
175 ap
->prd
[pi
].flags_len
= cpu_to_le32(blen
);
176 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi
, addr
, len
);
184 ap
->prd
[pi
- 1].flags_len
|= cpu_to_le32(ATA_PRD_EOT
);
188 * ata_sff_qc_prep - Prepare taskfile for submission
189 * @qc: Metadata associated with taskfile to be prepared
191 * Prepare ATA taskfile for submission.
194 * spin_lock_irqsave(host lock)
196 void ata_sff_qc_prep(struct ata_queued_cmd
*qc
)
198 if (!(qc
->flags
& ATA_QCFLAG_DMAMAP
))
205 * ata_sff_dumb_qc_prep - Prepare taskfile for submission
206 * @qc: Metadata associated with taskfile to be prepared
208 * Prepare ATA taskfile for submission.
211 * spin_lock_irqsave(host lock)
213 void ata_sff_dumb_qc_prep(struct ata_queued_cmd
*qc
)
215 if (!(qc
->flags
& ATA_QCFLAG_DMAMAP
))
218 ata_fill_sg_dumb(qc
);
222 * ata_sff_check_status - Read device status reg & clear interrupt
223 * @ap: port where the device is
225 * Reads ATA taskfile status register for currently-selected device
226 * and return its value. This also clears pending interrupts
230 * Inherited from caller.
232 u8
ata_sff_check_status(struct ata_port
*ap
)
234 return ioread8(ap
->ioaddr
.status_addr
);
238 * ata_sff_altstatus - Read device alternate status reg
239 * @ap: port where the device is
241 * Reads ATA taskfile alternate status register for
242 * currently-selected device and return its value.
244 * Note: may NOT be used as the check_altstatus() entry in
245 * ata_port_operations.
248 * Inherited from caller.
250 static u8
ata_sff_altstatus(struct ata_port
*ap
)
252 if (ap
->ops
->sff_check_altstatus
)
253 return ap
->ops
->sff_check_altstatus(ap
);
255 return ioread8(ap
->ioaddr
.altstatus_addr
);
259 * ata_sff_irq_status - Check if the device is busy
260 * @ap: port where the device is
262 * Determine if the port is currently busy. Uses altstatus
263 * if available in order to avoid clearing shared IRQ status
264 * when finding an IRQ source. Non ctl capable devices don't
265 * share interrupt lines fortunately for us.
268 * Inherited from caller.
270 static u8
ata_sff_irq_status(struct ata_port
*ap
)
274 if (ap
->ops
->sff_check_altstatus
|| ap
->ioaddr
.altstatus_addr
) {
275 status
= ata_sff_altstatus(ap
);
276 /* Not us: We are busy */
277 if (status
& ATA_BUSY
)
280 /* Clear INTRQ latch */
281 status
= ap
->ops
->sff_check_status(ap
);
286 * ata_sff_sync - Flush writes
287 * @ap: Port to wait for.
290 * If we have an mmio device with no ctl and no altstatus
291 * method this will fail. No such devices are known to exist.
294 * Inherited from caller.
297 static void ata_sff_sync(struct ata_port
*ap
)
299 if (ap
->ops
->sff_check_altstatus
)
300 ap
->ops
->sff_check_altstatus(ap
);
301 else if (ap
->ioaddr
.altstatus_addr
)
302 ioread8(ap
->ioaddr
.altstatus_addr
);
306 * ata_sff_pause - Flush writes and wait 400nS
307 * @ap: Port to pause for.
310 * If we have an mmio device with no ctl and no altstatus
311 * method this will fail. No such devices are known to exist.
314 * Inherited from caller.
317 void ata_sff_pause(struct ata_port
*ap
)
324 * ata_sff_dma_pause - Pause before commencing DMA
325 * @ap: Port to pause for.
327 * Perform I/O fencing and ensure sufficient cycle delays occur
328 * for the HDMA1:0 transition
331 void ata_sff_dma_pause(struct ata_port
*ap
)
333 if (ap
->ops
->sff_check_altstatus
|| ap
->ioaddr
.altstatus_addr
) {
334 /* An altstatus read will cause the needed delay without
335 messing up the IRQ status */
336 ata_sff_altstatus(ap
);
339 /* There are no DMA controllers without ctl. BUG here to ensure
340 we never violate the HDMA1:0 transition timing and risk
346 * ata_sff_busy_sleep - sleep until BSY clears, or timeout
347 * @ap: port containing status register to be polled
348 * @tmout_pat: impatience timeout
349 * @tmout: overall timeout
351 * Sleep until ATA Status register bit BSY clears,
352 * or a timeout occurs.
355 * Kernel thread context (may sleep).
358 * 0 on success, -errno otherwise.
360 int ata_sff_busy_sleep(struct ata_port
*ap
,
361 unsigned long tmout_pat
, unsigned long tmout
)
363 unsigned long timer_start
, timeout
;
366 status
= ata_sff_busy_wait(ap
, ATA_BUSY
, 300);
367 timer_start
= jiffies
;
368 timeout
= timer_start
+ tmout_pat
;
369 while (status
!= 0xff && (status
& ATA_BUSY
) &&
370 time_before(jiffies
, timeout
)) {
372 status
= ata_sff_busy_wait(ap
, ATA_BUSY
, 3);
375 if (status
!= 0xff && (status
& ATA_BUSY
))
376 ata_port_printk(ap
, KERN_WARNING
,
377 "port is slow to respond, please be patient "
378 "(Status 0x%x)\n", status
);
380 timeout
= timer_start
+ tmout
;
381 while (status
!= 0xff && (status
& ATA_BUSY
) &&
382 time_before(jiffies
, timeout
)) {
384 status
= ap
->ops
->sff_check_status(ap
);
390 if (status
& ATA_BUSY
) {
391 ata_port_printk(ap
, KERN_ERR
, "port failed to respond "
392 "(%lu secs, Status 0x%x)\n",
400 static int ata_sff_check_ready(struct ata_link
*link
)
402 u8 status
= link
->ap
->ops
->sff_check_status(link
->ap
);
404 return ata_check_ready(status
);
408 * ata_sff_wait_ready - sleep until BSY clears, or timeout
409 * @link: SFF link to wait ready status for
410 * @deadline: deadline jiffies for the operation
412 * Sleep until ATA Status register bit BSY clears, or timeout
416 * Kernel thread context (may sleep).
419 * 0 on success, -errno otherwise.
421 int ata_sff_wait_ready(struct ata_link
*link
, unsigned long deadline
)
423 return ata_wait_ready(link
, deadline
, ata_sff_check_ready
);
427 * ata_sff_dev_select - Select device 0/1 on ATA bus
428 * @ap: ATA channel to manipulate
429 * @device: ATA device (numbered from zero) to select
431 * Use the method defined in the ATA specification to
432 * make either device 0, or device 1, active on the
433 * ATA channel. Works with both PIO and MMIO.
435 * May be used as the dev_select() entry in ata_port_operations.
440 void ata_sff_dev_select(struct ata_port
*ap
, unsigned int device
)
445 tmp
= ATA_DEVICE_OBS
;
447 tmp
= ATA_DEVICE_OBS
| ATA_DEV1
;
449 iowrite8(tmp
, ap
->ioaddr
.device_addr
);
450 ata_sff_pause(ap
); /* needed; also flushes, for mmio */
454 * ata_dev_select - Select device 0/1 on ATA bus
455 * @ap: ATA channel to manipulate
456 * @device: ATA device (numbered from zero) to select
457 * @wait: non-zero to wait for Status register BSY bit to clear
458 * @can_sleep: non-zero if context allows sleeping
460 * Use the method defined in the ATA specification to
461 * make either device 0, or device 1, active on the
464 * This is a high-level version of ata_sff_dev_select(), which
465 * additionally provides the services of inserting the proper
466 * pauses and status polling, where needed.
471 void ata_dev_select(struct ata_port
*ap
, unsigned int device
,
472 unsigned int wait
, unsigned int can_sleep
)
474 if (ata_msg_probe(ap
))
475 ata_port_printk(ap
, KERN_INFO
, "ata_dev_select: ENTER, "
476 "device %u, wait %u\n", device
, wait
);
481 ap
->ops
->sff_dev_select(ap
, device
);
484 if (can_sleep
&& ap
->link
.device
[device
].class == ATA_DEV_ATAPI
)
491 * ata_sff_irq_on - Enable interrupts on a port.
492 * @ap: Port on which interrupts are enabled.
494 * Enable interrupts on a legacy IDE device using MMIO or PIO,
495 * wait for idle, clear any pending interrupts.
498 * Inherited from caller.
500 u8
ata_sff_irq_on(struct ata_port
*ap
)
502 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
505 ap
->ctl
&= ~ATA_NIEN
;
506 ap
->last_ctl
= ap
->ctl
;
508 if (ioaddr
->ctl_addr
)
509 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
510 tmp
= ata_wait_idle(ap
);
512 ap
->ops
->sff_irq_clear(ap
);
518 * ata_sff_irq_clear - Clear PCI IDE BMDMA interrupt.
519 * @ap: Port associated with this ATA transaction.
521 * Clear interrupt and error flags in DMA status register.
523 * May be used as the irq_clear() entry in ata_port_operations.
526 * spin_lock_irqsave(host lock)
528 void ata_sff_irq_clear(struct ata_port
*ap
)
530 void __iomem
*mmio
= ap
->ioaddr
.bmdma_addr
;
535 iowrite8(ioread8(mmio
+ ATA_DMA_STATUS
), mmio
+ ATA_DMA_STATUS
);
539 * ata_sff_tf_load - send taskfile registers to host controller
540 * @ap: Port to which output is sent
541 * @tf: ATA taskfile register set
543 * Outputs ATA taskfile to standard ATA host controller.
546 * Inherited from caller.
548 void ata_sff_tf_load(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
550 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
551 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
553 if (tf
->ctl
!= ap
->last_ctl
) {
554 if (ioaddr
->ctl_addr
)
555 iowrite8(tf
->ctl
, ioaddr
->ctl_addr
);
556 ap
->last_ctl
= tf
->ctl
;
560 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
561 WARN_ON(!ioaddr
->ctl_addr
);
562 iowrite8(tf
->hob_feature
, ioaddr
->feature_addr
);
563 iowrite8(tf
->hob_nsect
, ioaddr
->nsect_addr
);
564 iowrite8(tf
->hob_lbal
, ioaddr
->lbal_addr
);
565 iowrite8(tf
->hob_lbam
, ioaddr
->lbam_addr
);
566 iowrite8(tf
->hob_lbah
, ioaddr
->lbah_addr
);
567 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
576 iowrite8(tf
->feature
, ioaddr
->feature_addr
);
577 iowrite8(tf
->nsect
, ioaddr
->nsect_addr
);
578 iowrite8(tf
->lbal
, ioaddr
->lbal_addr
);
579 iowrite8(tf
->lbam
, ioaddr
->lbam_addr
);
580 iowrite8(tf
->lbah
, ioaddr
->lbah_addr
);
581 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
589 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
590 iowrite8(tf
->device
, ioaddr
->device_addr
);
591 VPRINTK("device 0x%X\n", tf
->device
);
598 * ata_sff_tf_read - input device's ATA taskfile shadow registers
599 * @ap: Port from which input is read
600 * @tf: ATA taskfile register set for storing input
602 * Reads ATA taskfile registers for currently-selected device
603 * into @tf. Assumes the device has a fully SFF compliant task file
604 * layout and behaviour. If you device does not (eg has a different
605 * status method) then you will need to provide a replacement tf_read
608 * Inherited from caller.
610 void ata_sff_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
)
612 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
614 tf
->command
= ata_sff_check_status(ap
);
615 tf
->feature
= ioread8(ioaddr
->error_addr
);
616 tf
->nsect
= ioread8(ioaddr
->nsect_addr
);
617 tf
->lbal
= ioread8(ioaddr
->lbal_addr
);
618 tf
->lbam
= ioread8(ioaddr
->lbam_addr
);
619 tf
->lbah
= ioread8(ioaddr
->lbah_addr
);
620 tf
->device
= ioread8(ioaddr
->device_addr
);
622 if (tf
->flags
& ATA_TFLAG_LBA48
) {
623 if (likely(ioaddr
->ctl_addr
)) {
624 iowrite8(tf
->ctl
| ATA_HOB
, ioaddr
->ctl_addr
);
625 tf
->hob_feature
= ioread8(ioaddr
->error_addr
);
626 tf
->hob_nsect
= ioread8(ioaddr
->nsect_addr
);
627 tf
->hob_lbal
= ioread8(ioaddr
->lbal_addr
);
628 tf
->hob_lbam
= ioread8(ioaddr
->lbam_addr
);
629 tf
->hob_lbah
= ioread8(ioaddr
->lbah_addr
);
630 iowrite8(tf
->ctl
, ioaddr
->ctl_addr
);
631 ap
->last_ctl
= tf
->ctl
;
638 * ata_sff_exec_command - issue ATA command to host controller
639 * @ap: port to which command is being issued
640 * @tf: ATA taskfile register set
642 * Issues ATA command, with proper synchronization with interrupt
643 * handler / other threads.
646 * spin_lock_irqsave(host lock)
648 void ata_sff_exec_command(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
650 DPRINTK("ata%u: cmd 0x%X\n", ap
->print_id
, tf
->command
);
652 iowrite8(tf
->command
, ap
->ioaddr
.command_addr
);
657 * ata_tf_to_host - issue ATA taskfile to host controller
658 * @ap: port to which command is being issued
659 * @tf: ATA taskfile register set
661 * Issues ATA taskfile register set to ATA host controller,
662 * with proper synchronization with interrupt handler and
666 * spin_lock_irqsave(host lock)
668 static inline void ata_tf_to_host(struct ata_port
*ap
,
669 const struct ata_taskfile
*tf
)
671 ap
->ops
->sff_tf_load(ap
, tf
);
672 ap
->ops
->sff_exec_command(ap
, tf
);
676 * ata_sff_data_xfer - Transfer data by PIO
677 * @dev: device to target
679 * @buflen: buffer length
682 * Transfer data from/to the device data register by PIO.
685 * Inherited from caller.
690 unsigned int ata_sff_data_xfer(struct ata_device
*dev
, unsigned char *buf
,
691 unsigned int buflen
, int rw
)
693 struct ata_port
*ap
= dev
->link
->ap
;
694 void __iomem
*data_addr
= ap
->ioaddr
.data_addr
;
695 unsigned int words
= buflen
>> 1;
697 /* Transfer multiple of 2 bytes */
699 ioread16_rep(data_addr
, buf
, words
);
701 iowrite16_rep(data_addr
, buf
, words
);
703 /* Transfer trailing 1 byte, if any. */
704 if (unlikely(buflen
& 0x01)) {
705 __le16 align_buf
[1] = { 0 };
706 unsigned char *trailing_buf
= buf
+ buflen
- 1;
709 align_buf
[0] = cpu_to_le16(ioread16(data_addr
));
710 memcpy(trailing_buf
, align_buf
, 1);
712 memcpy(align_buf
, trailing_buf
, 1);
713 iowrite16(le16_to_cpu(align_buf
[0]), data_addr
);
722 * ata_sff_data_xfer_noirq - Transfer data by PIO
723 * @dev: device to target
725 * @buflen: buffer length
728 * Transfer data from/to the device data register by PIO. Do the
729 * transfer with interrupts disabled.
732 * Inherited from caller.
737 unsigned int ata_sff_data_xfer_noirq(struct ata_device
*dev
, unsigned char *buf
,
738 unsigned int buflen
, int rw
)
741 unsigned int consumed
;
743 local_irq_save(flags
);
744 consumed
= ata_sff_data_xfer(dev
, buf
, buflen
, rw
);
745 local_irq_restore(flags
);
751 * ata_pio_sector - Transfer a sector of data.
752 * @qc: Command on going
754 * Transfer qc->sect_size bytes of data from/to the ATA device.
757 * Inherited from caller.
759 static void ata_pio_sector(struct ata_queued_cmd
*qc
)
761 int do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
762 struct ata_port
*ap
= qc
->ap
;
767 if (qc
->curbytes
== qc
->nbytes
- qc
->sect_size
)
768 ap
->hsm_task_state
= HSM_ST_LAST
;
770 page
= sg_page(qc
->cursg
);
771 offset
= qc
->cursg
->offset
+ qc
->cursg_ofs
;
773 /* get the current page and offset */
774 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
777 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
779 if (PageHighMem(page
)) {
782 /* FIXME: use a bounce buffer */
783 local_irq_save(flags
);
784 buf
= kmap_atomic(page
, KM_IRQ0
);
786 /* do the actual data transfer */
787 ap
->ops
->sff_data_xfer(qc
->dev
, buf
+ offset
, qc
->sect_size
,
790 kunmap_atomic(buf
, KM_IRQ0
);
791 local_irq_restore(flags
);
793 buf
= page_address(page
);
794 ap
->ops
->sff_data_xfer(qc
->dev
, buf
+ offset
, qc
->sect_size
,
798 qc
->curbytes
+= qc
->sect_size
;
799 qc
->cursg_ofs
+= qc
->sect_size
;
801 if (qc
->cursg_ofs
== qc
->cursg
->length
) {
802 qc
->cursg
= sg_next(qc
->cursg
);
808 * ata_pio_sectors - Transfer one or many sectors.
809 * @qc: Command on going
811 * Transfer one or many sectors of data from/to the
812 * ATA device for the DRQ request.
815 * Inherited from caller.
817 static void ata_pio_sectors(struct ata_queued_cmd
*qc
)
819 if (is_multi_taskfile(&qc
->tf
)) {
820 /* READ/WRITE MULTIPLE */
823 WARN_ON(qc
->dev
->multi_count
== 0);
825 nsect
= min((qc
->nbytes
- qc
->curbytes
) / qc
->sect_size
,
826 qc
->dev
->multi_count
);
832 ata_sff_sync(qc
->ap
); /* flush */
836 * atapi_send_cdb - Write CDB bytes to hardware
837 * @ap: Port to which ATAPI device is attached.
838 * @qc: Taskfile currently active
840 * When device has indicated its readiness to accept
841 * a CDB, this function is called. Send the CDB.
846 static void atapi_send_cdb(struct ata_port
*ap
, struct ata_queued_cmd
*qc
)
849 DPRINTK("send cdb\n");
850 WARN_ON(qc
->dev
->cdb_len
< 12);
852 ap
->ops
->sff_data_xfer(qc
->dev
, qc
->cdb
, qc
->dev
->cdb_len
, 1);
854 /* FIXME: If the CDB is for DMA do we need to do the transition delay
855 or is bmdma_start guaranteed to do it ? */
856 switch (qc
->tf
.protocol
) {
858 ap
->hsm_task_state
= HSM_ST
;
860 case ATAPI_PROT_NODATA
:
861 ap
->hsm_task_state
= HSM_ST_LAST
;
864 ap
->hsm_task_state
= HSM_ST_LAST
;
866 ap
->ops
->bmdma_start(qc
);
872 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
873 * @qc: Command on going
874 * @bytes: number of bytes
876 * Transfer Transfer data from/to the ATAPI device.
879 * Inherited from caller.
882 static int __atapi_pio_bytes(struct ata_queued_cmd
*qc
, unsigned int bytes
)
884 int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
) ? WRITE
: READ
;
885 struct ata_port
*ap
= qc
->ap
;
886 struct ata_device
*dev
= qc
->dev
;
887 struct ata_eh_info
*ehi
= &dev
->link
->eh_info
;
888 struct scatterlist
*sg
;
891 unsigned int offset
, count
, consumed
;
896 ata_ehi_push_desc(ehi
, "unexpected or too much trailing data "
897 "buf=%u cur=%u bytes=%u",
898 qc
->nbytes
, qc
->curbytes
, bytes
);
903 offset
= sg
->offset
+ qc
->cursg_ofs
;
905 /* get the current page and offset */
906 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
909 /* don't overrun current sg */
910 count
= min(sg
->length
- qc
->cursg_ofs
, bytes
);
912 /* don't cross page boundaries */
913 count
= min(count
, (unsigned int)PAGE_SIZE
- offset
);
915 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
917 if (PageHighMem(page
)) {
920 /* FIXME: use bounce buffer */
921 local_irq_save(flags
);
922 buf
= kmap_atomic(page
, KM_IRQ0
);
924 /* do the actual data transfer */
925 consumed
= ap
->ops
->sff_data_xfer(dev
, buf
+ offset
, count
, rw
);
927 kunmap_atomic(buf
, KM_IRQ0
);
928 local_irq_restore(flags
);
930 buf
= page_address(page
);
931 consumed
= ap
->ops
->sff_data_xfer(dev
, buf
+ offset
, count
, rw
);
934 bytes
-= min(bytes
, consumed
);
935 qc
->curbytes
+= count
;
936 qc
->cursg_ofs
+= count
;
938 if (qc
->cursg_ofs
== sg
->length
) {
939 qc
->cursg
= sg_next(qc
->cursg
);
943 /* consumed can be larger than count only for the last transfer */
944 WARN_ON(qc
->cursg
&& count
!= consumed
);
952 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
953 * @qc: Command on going
955 * Transfer Transfer data from/to the ATAPI device.
958 * Inherited from caller.
960 static void atapi_pio_bytes(struct ata_queued_cmd
*qc
)
962 struct ata_port
*ap
= qc
->ap
;
963 struct ata_device
*dev
= qc
->dev
;
964 struct ata_eh_info
*ehi
= &dev
->link
->eh_info
;
965 unsigned int ireason
, bc_lo
, bc_hi
, bytes
;
966 int i_write
, do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
) ? 1 : 0;
968 /* Abuse qc->result_tf for temp storage of intermediate TF
969 * here to save some kernel stack usage.
970 * For normal completion, qc->result_tf is not relevant. For
971 * error, qc->result_tf is later overwritten by ata_qc_complete().
972 * So, the correctness of qc->result_tf is not affected.
974 ap
->ops
->sff_tf_read(ap
, &qc
->result_tf
);
975 ireason
= qc
->result_tf
.nsect
;
976 bc_lo
= qc
->result_tf
.lbam
;
977 bc_hi
= qc
->result_tf
.lbah
;
978 bytes
= (bc_hi
<< 8) | bc_lo
;
980 /* shall be cleared to zero, indicating xfer of data */
981 if (unlikely(ireason
& (1 << 0)))
984 /* make sure transfer direction matches expected */
985 i_write
= ((ireason
& (1 << 1)) == 0) ? 1 : 0;
986 if (unlikely(do_write
!= i_write
))
989 if (unlikely(!bytes
))
992 VPRINTK("ata%u: xfering %d bytes\n", ap
->print_id
, bytes
);
994 if (unlikely(__atapi_pio_bytes(qc
, bytes
)))
996 ata_sff_sync(ap
); /* flush */
1001 ata_ehi_push_desc(ehi
, "ATAPI check failed (ireason=0x%x bytes=%u)",
1004 qc
->err_mask
|= AC_ERR_HSM
;
1005 ap
->hsm_task_state
= HSM_ST_ERR
;
1009 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
1010 * @ap: the target ata_port
1014 * 1 if ok in workqueue, 0 otherwise.
1016 static inline int ata_hsm_ok_in_wq(struct ata_port
*ap
, struct ata_queued_cmd
*qc
)
1018 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1021 if (ap
->hsm_task_state
== HSM_ST_FIRST
) {
1022 if (qc
->tf
.protocol
== ATA_PROT_PIO
&&
1023 (qc
->tf
.flags
& ATA_TFLAG_WRITE
))
1026 if (ata_is_atapi(qc
->tf
.protocol
) &&
1027 !(qc
->dev
->flags
& ATA_DFLAG_CDB_INTR
))
1035 * ata_hsm_qc_complete - finish a qc running on standard HSM
1036 * @qc: Command to complete
1037 * @in_wq: 1 if called from workqueue, 0 otherwise
1039 * Finish @qc which is running on standard HSM.
1042 * If @in_wq is zero, spin_lock_irqsave(host lock).
1043 * Otherwise, none on entry and grabs host lock.
1045 static void ata_hsm_qc_complete(struct ata_queued_cmd
*qc
, int in_wq
)
1047 struct ata_port
*ap
= qc
->ap
;
1048 unsigned long flags
;
1050 if (ap
->ops
->error_handler
) {
1052 spin_lock_irqsave(ap
->lock
, flags
);
1054 /* EH might have kicked in while host lock is
1057 qc
= ata_qc_from_tag(ap
, qc
->tag
);
1059 if (likely(!(qc
->err_mask
& AC_ERR_HSM
))) {
1060 ap
->ops
->sff_irq_on(ap
);
1061 ata_qc_complete(qc
);
1063 ata_port_freeze(ap
);
1066 spin_unlock_irqrestore(ap
->lock
, flags
);
1068 if (likely(!(qc
->err_mask
& AC_ERR_HSM
)))
1069 ata_qc_complete(qc
);
1071 ata_port_freeze(ap
);
1075 spin_lock_irqsave(ap
->lock
, flags
);
1076 ap
->ops
->sff_irq_on(ap
);
1077 ata_qc_complete(qc
);
1078 spin_unlock_irqrestore(ap
->lock
, flags
);
1080 ata_qc_complete(qc
);
1085 * ata_sff_hsm_move - move the HSM to the next state.
1086 * @ap: the target ata_port
1088 * @status: current device status
1089 * @in_wq: 1 if called from workqueue, 0 otherwise
1092 * 1 when poll next status needed, 0 otherwise.
1094 int ata_sff_hsm_move(struct ata_port
*ap
, struct ata_queued_cmd
*qc
,
1095 u8 status
, int in_wq
)
1097 unsigned long flags
= 0;
1100 WARN_ON((qc
->flags
& ATA_QCFLAG_ACTIVE
) == 0);
1102 /* Make sure ata_sff_qc_issue() does not throw things
1103 * like DMA polling into the workqueue. Notice that
1104 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
1106 WARN_ON(in_wq
!= ata_hsm_ok_in_wq(ap
, qc
));
1109 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
1110 ap
->print_id
, qc
->tf
.protocol
, ap
->hsm_task_state
, status
);
1112 switch (ap
->hsm_task_state
) {
1114 /* Send first data block or PACKET CDB */
1116 /* If polling, we will stay in the work queue after
1117 * sending the data. Otherwise, interrupt handler
1118 * takes over after sending the data.
1120 poll_next
= (qc
->tf
.flags
& ATA_TFLAG_POLLING
);
1122 /* check device status */
1123 if (unlikely((status
& ATA_DRQ
) == 0)) {
1124 /* handle BSY=0, DRQ=0 as error */
1125 if (likely(status
& (ATA_ERR
| ATA_DF
)))
1126 /* device stops HSM for abort/error */
1127 qc
->err_mask
|= AC_ERR_DEV
;
1129 /* HSM violation. Let EH handle this */
1130 qc
->err_mask
|= AC_ERR_HSM
;
1132 ap
->hsm_task_state
= HSM_ST_ERR
;
1136 /* Device should not ask for data transfer (DRQ=1)
1137 * when it finds something wrong.
1138 * We ignore DRQ here and stop the HSM by
1139 * changing hsm_task_state to HSM_ST_ERR and
1140 * let the EH abort the command or reset the device.
1142 if (unlikely(status
& (ATA_ERR
| ATA_DF
))) {
1143 /* Some ATAPI tape drives forget to clear the ERR bit
1144 * when doing the next command (mostly request sense).
1145 * We ignore ERR here to workaround and proceed sending
1148 if (!(qc
->dev
->horkage
& ATA_HORKAGE_STUCK_ERR
)) {
1149 ata_port_printk(ap
, KERN_WARNING
,
1150 "DRQ=1 with device error, "
1151 "dev_stat 0x%X\n", status
);
1152 qc
->err_mask
|= AC_ERR_HSM
;
1153 ap
->hsm_task_state
= HSM_ST_ERR
;
1158 /* Send the CDB (atapi) or the first data block (ata pio out).
1159 * During the state transition, interrupt handler shouldn't
1160 * be invoked before the data transfer is complete and
1161 * hsm_task_state is changed. Hence, the following locking.
1164 spin_lock_irqsave(ap
->lock
, flags
);
1166 if (qc
->tf
.protocol
== ATA_PROT_PIO
) {
1167 /* PIO data out protocol.
1168 * send first data block.
1171 /* ata_pio_sectors() might change the state
1172 * to HSM_ST_LAST. so, the state is changed here
1173 * before ata_pio_sectors().
1175 ap
->hsm_task_state
= HSM_ST
;
1176 ata_pio_sectors(qc
);
1179 atapi_send_cdb(ap
, qc
);
1182 spin_unlock_irqrestore(ap
->lock
, flags
);
1184 /* if polling, ata_pio_task() handles the rest.
1185 * otherwise, interrupt handler takes over from here.
1190 /* complete command or read/write the data register */
1191 if (qc
->tf
.protocol
== ATAPI_PROT_PIO
) {
1192 /* ATAPI PIO protocol */
1193 if ((status
& ATA_DRQ
) == 0) {
1194 /* No more data to transfer or device error.
1195 * Device error will be tagged in HSM_ST_LAST.
1197 ap
->hsm_task_state
= HSM_ST_LAST
;
1201 /* Device should not ask for data transfer (DRQ=1)
1202 * when it finds something wrong.
1203 * We ignore DRQ here and stop the HSM by
1204 * changing hsm_task_state to HSM_ST_ERR and
1205 * let the EH abort the command or reset the device.
1207 if (unlikely(status
& (ATA_ERR
| ATA_DF
))) {
1208 ata_port_printk(ap
, KERN_WARNING
, "DRQ=1 with "
1209 "device error, dev_stat 0x%X\n",
1211 qc
->err_mask
|= AC_ERR_HSM
;
1212 ap
->hsm_task_state
= HSM_ST_ERR
;
1216 atapi_pio_bytes(qc
);
1218 if (unlikely(ap
->hsm_task_state
== HSM_ST_ERR
))
1219 /* bad ireason reported by device */
1223 /* ATA PIO protocol */
1224 if (unlikely((status
& ATA_DRQ
) == 0)) {
1225 /* handle BSY=0, DRQ=0 as error */
1226 if (likely(status
& (ATA_ERR
| ATA_DF
)))
1227 /* device stops HSM for abort/error */
1228 qc
->err_mask
|= AC_ERR_DEV
;
1230 /* HSM violation. Let EH handle this.
1231 * Phantom devices also trigger this
1232 * condition. Mark hint.
1234 qc
->err_mask
|= AC_ERR_HSM
|
1237 ap
->hsm_task_state
= HSM_ST_ERR
;
1241 /* For PIO reads, some devices may ask for
1242 * data transfer (DRQ=1) alone with ERR=1.
1243 * We respect DRQ here and transfer one
1244 * block of junk data before changing the
1245 * hsm_task_state to HSM_ST_ERR.
1247 * For PIO writes, ERR=1 DRQ=1 doesn't make
1248 * sense since the data block has been
1249 * transferred to the device.
1251 if (unlikely(status
& (ATA_ERR
| ATA_DF
))) {
1252 /* data might be corrputed */
1253 qc
->err_mask
|= AC_ERR_DEV
;
1255 if (!(qc
->tf
.flags
& ATA_TFLAG_WRITE
)) {
1256 ata_pio_sectors(qc
);
1257 status
= ata_wait_idle(ap
);
1260 if (status
& (ATA_BUSY
| ATA_DRQ
))
1261 qc
->err_mask
|= AC_ERR_HSM
;
1263 /* ata_pio_sectors() might change the
1264 * state to HSM_ST_LAST. so, the state
1265 * is changed after ata_pio_sectors().
1267 ap
->hsm_task_state
= HSM_ST_ERR
;
1271 ata_pio_sectors(qc
);
1273 if (ap
->hsm_task_state
== HSM_ST_LAST
&&
1274 (!(qc
->tf
.flags
& ATA_TFLAG_WRITE
))) {
1276 status
= ata_wait_idle(ap
);
1285 if (unlikely(!ata_ok(status
))) {
1286 qc
->err_mask
|= __ac_err_mask(status
);
1287 ap
->hsm_task_state
= HSM_ST_ERR
;
1291 /* no more data to transfer */
1292 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
1293 ap
->print_id
, qc
->dev
->devno
, status
);
1295 WARN_ON(qc
->err_mask
& (AC_ERR_DEV
| AC_ERR_HSM
));
1297 ap
->hsm_task_state
= HSM_ST_IDLE
;
1299 /* complete taskfile transaction */
1300 ata_hsm_qc_complete(qc
, in_wq
);
1306 /* make sure qc->err_mask is available to
1307 * know what's wrong and recover
1309 WARN_ON(!(qc
->err_mask
& (AC_ERR_DEV
| AC_ERR_HSM
)));
1311 ap
->hsm_task_state
= HSM_ST_IDLE
;
1313 /* complete taskfile transaction */
1314 ata_hsm_qc_complete(qc
, in_wq
);
1326 void ata_pio_task(struct work_struct
*work
)
1328 struct ata_port
*ap
=
1329 container_of(work
, struct ata_port
, port_task
.work
);
1330 struct ata_queued_cmd
*qc
= ap
->port_task_data
;
1335 WARN_ON(ap
->hsm_task_state
== HSM_ST_IDLE
);
1338 * This is purely heuristic. This is a fast path.
1339 * Sometimes when we enter, BSY will be cleared in
1340 * a chk-status or two. If not, the drive is probably seeking
1341 * or something. Snooze for a couple msecs, then
1342 * chk-status again. If still busy, queue delayed work.
1344 status
= ata_sff_busy_wait(ap
, ATA_BUSY
, 5);
1345 if (status
& ATA_BUSY
) {
1347 status
= ata_sff_busy_wait(ap
, ATA_BUSY
, 10);
1348 if (status
& ATA_BUSY
) {
1349 ata_pio_queue_task(ap
, qc
, ATA_SHORT_PAUSE
);
1355 poll_next
= ata_sff_hsm_move(ap
, qc
, status
, 1);
1357 /* another command or interrupt handler
1358 * may be running at this point.
1365 * ata_sff_qc_issue - issue taskfile to device in proto-dependent manner
1366 * @qc: command to issue to device
1368 * Using various libata functions and hooks, this function
1369 * starts an ATA command. ATA commands are grouped into
1370 * classes called "protocols", and issuing each type of protocol
1371 * is slightly different.
1373 * May be used as the qc_issue() entry in ata_port_operations.
1376 * spin_lock_irqsave(host lock)
1379 * Zero on success, AC_ERR_* mask on failure
1381 unsigned int ata_sff_qc_issue(struct ata_queued_cmd
*qc
)
1383 struct ata_port
*ap
= qc
->ap
;
1385 /* Use polling pio if the LLD doesn't handle
1386 * interrupt driven pio and atapi CDB interrupt.
1388 if (ap
->flags
& ATA_FLAG_PIO_POLLING
) {
1389 switch (qc
->tf
.protocol
) {
1391 case ATA_PROT_NODATA
:
1392 case ATAPI_PROT_PIO
:
1393 case ATAPI_PROT_NODATA
:
1394 qc
->tf
.flags
|= ATA_TFLAG_POLLING
;
1396 case ATAPI_PROT_DMA
:
1397 if (qc
->dev
->flags
& ATA_DFLAG_CDB_INTR
)
1398 /* see ata_dma_blacklisted() */
1406 /* select the device */
1407 ata_dev_select(ap
, qc
->dev
->devno
, 1, 0);
1409 /* start the command */
1410 switch (qc
->tf
.protocol
) {
1411 case ATA_PROT_NODATA
:
1412 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1413 ata_qc_set_polling(qc
);
1415 ata_tf_to_host(ap
, &qc
->tf
);
1416 ap
->hsm_task_state
= HSM_ST_LAST
;
1418 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1419 ata_pio_queue_task(ap
, qc
, 0);
1424 WARN_ON(qc
->tf
.flags
& ATA_TFLAG_POLLING
);
1426 ap
->ops
->sff_tf_load(ap
, &qc
->tf
); /* load tf registers */
1427 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
1428 ap
->ops
->bmdma_start(qc
); /* initiate bmdma */
1429 ap
->hsm_task_state
= HSM_ST_LAST
;
1433 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1434 ata_qc_set_polling(qc
);
1436 ata_tf_to_host(ap
, &qc
->tf
);
1438 if (qc
->tf
.flags
& ATA_TFLAG_WRITE
) {
1439 /* PIO data out protocol */
1440 ap
->hsm_task_state
= HSM_ST_FIRST
;
1441 ata_pio_queue_task(ap
, qc
, 0);
1443 /* always send first data block using
1444 * the ata_pio_task() codepath.
1447 /* PIO data in protocol */
1448 ap
->hsm_task_state
= HSM_ST
;
1450 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1451 ata_pio_queue_task(ap
, qc
, 0);
1453 /* if polling, ata_pio_task() handles the rest.
1454 * otherwise, interrupt handler takes over from here.
1460 case ATAPI_PROT_PIO
:
1461 case ATAPI_PROT_NODATA
:
1462 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1463 ata_qc_set_polling(qc
);
1465 ata_tf_to_host(ap
, &qc
->tf
);
1467 ap
->hsm_task_state
= HSM_ST_FIRST
;
1469 /* send cdb by polling if no cdb interrupt */
1470 if ((!(qc
->dev
->flags
& ATA_DFLAG_CDB_INTR
)) ||
1471 (qc
->tf
.flags
& ATA_TFLAG_POLLING
))
1472 ata_pio_queue_task(ap
, qc
, 0);
1475 case ATAPI_PROT_DMA
:
1476 WARN_ON(qc
->tf
.flags
& ATA_TFLAG_POLLING
);
1478 ap
->ops
->sff_tf_load(ap
, &qc
->tf
); /* load tf registers */
1479 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
1480 ap
->hsm_task_state
= HSM_ST_FIRST
;
1482 /* send cdb by polling if no cdb interrupt */
1483 if (!(qc
->dev
->flags
& ATA_DFLAG_CDB_INTR
))
1484 ata_pio_queue_task(ap
, qc
, 0);
1489 return AC_ERR_SYSTEM
;
1496 * ata_sff_qc_fill_rtf - fill result TF using ->sff_tf_read
1497 * @qc: qc to fill result TF for
1499 * @qc is finished and result TF needs to be filled. Fill it
1500 * using ->sff_tf_read.
1503 * spin_lock_irqsave(host lock)
1506 * true indicating that result TF is successfully filled.
1508 bool ata_sff_qc_fill_rtf(struct ata_queued_cmd
*qc
)
1510 qc
->ap
->ops
->sff_tf_read(qc
->ap
, &qc
->result_tf
);
1515 * ata_sff_host_intr - Handle host interrupt for given (port, task)
1516 * @ap: Port on which interrupt arrived (possibly...)
1517 * @qc: Taskfile currently active in engine
1519 * Handle host interrupt for given queued command. Currently,
1520 * only DMA interrupts are handled. All other commands are
1521 * handled via polling with interrupts disabled (nIEN bit).
1524 * spin_lock_irqsave(host lock)
1527 * One if interrupt was handled, zero if not (shared irq).
1529 inline unsigned int ata_sff_host_intr(struct ata_port
*ap
,
1530 struct ata_queued_cmd
*qc
)
1532 struct ata_eh_info
*ehi
= &ap
->link
.eh_info
;
1533 u8 status
, host_stat
= 0;
1535 VPRINTK("ata%u: protocol %d task_state %d\n",
1536 ap
->print_id
, qc
->tf
.protocol
, ap
->hsm_task_state
);
1538 /* Check whether we are expecting interrupt in this state */
1539 switch (ap
->hsm_task_state
) {
1541 /* Some pre-ATAPI-4 devices assert INTRQ
1542 * at this state when ready to receive CDB.
1545 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
1546 * The flag was turned on only for atapi devices. No
1547 * need to check ata_is_atapi(qc->tf.protocol) again.
1549 if (!(qc
->dev
->flags
& ATA_DFLAG_CDB_INTR
))
1553 if (qc
->tf
.protocol
== ATA_PROT_DMA
||
1554 qc
->tf
.protocol
== ATAPI_PROT_DMA
) {
1555 /* check status of DMA engine */
1556 host_stat
= ap
->ops
->bmdma_status(ap
);
1557 VPRINTK("ata%u: host_stat 0x%X\n",
1558 ap
->print_id
, host_stat
);
1560 /* if it's not our irq... */
1561 if (!(host_stat
& ATA_DMA_INTR
))
1564 /* before we do anything else, clear DMA-Start bit */
1565 ap
->ops
->bmdma_stop(qc
);
1567 if (unlikely(host_stat
& ATA_DMA_ERR
)) {
1568 /* error when transfering data to/from memory */
1569 qc
->err_mask
|= AC_ERR_HOST_BUS
;
1570 ap
->hsm_task_state
= HSM_ST_ERR
;
1581 /* check main status, clearing INTRQ if needed */
1582 status
= ata_sff_irq_status(ap
);
1583 if (status
& ATA_BUSY
)
1586 /* ack bmdma irq events */
1587 ap
->ops
->sff_irq_clear(ap
);
1589 ata_sff_hsm_move(ap
, qc
, status
, 0);
1591 if (unlikely(qc
->err_mask
) && (qc
->tf
.protocol
== ATA_PROT_DMA
||
1592 qc
->tf
.protocol
== ATAPI_PROT_DMA
))
1593 ata_ehi_push_desc(ehi
, "BMDMA stat 0x%x", host_stat
);
1595 return 1; /* irq handled */
1598 ap
->stats
.idle_irq
++;
1601 if ((ap
->stats
.idle_irq
% 1000) == 0) {
1602 ap
->ops
->sff_check_status(ap
);
1603 ap
->ops
->sff_irq_clear(ap
);
1604 ata_port_printk(ap
, KERN_WARNING
, "irq trap\n");
1608 return 0; /* irq not handled */
1612 * ata_sff_interrupt - Default ATA host interrupt handler
1613 * @irq: irq line (unused)
1614 * @dev_instance: pointer to our ata_host information structure
1616 * Default interrupt handler for PCI IDE devices. Calls
1617 * ata_sff_host_intr() for each port that is not disabled.
1620 * Obtains host lock during operation.
1623 * IRQ_NONE or IRQ_HANDLED.
1625 irqreturn_t
ata_sff_interrupt(int irq
, void *dev_instance
)
1627 struct ata_host
*host
= dev_instance
;
1629 unsigned int handled
= 0;
1630 unsigned long flags
;
1632 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
1633 spin_lock_irqsave(&host
->lock
, flags
);
1635 for (i
= 0; i
< host
->n_ports
; i
++) {
1636 struct ata_port
*ap
;
1638 ap
= host
->ports
[i
];
1640 !(ap
->flags
& ATA_FLAG_DISABLED
)) {
1641 struct ata_queued_cmd
*qc
;
1643 qc
= ata_qc_from_tag(ap
, ap
->link
.active_tag
);
1644 if (qc
&& (!(qc
->tf
.flags
& ATA_TFLAG_POLLING
)) &&
1645 (qc
->flags
& ATA_QCFLAG_ACTIVE
))
1646 handled
|= ata_sff_host_intr(ap
, qc
);
1650 spin_unlock_irqrestore(&host
->lock
, flags
);
1652 return IRQ_RETVAL(handled
);
1656 * ata_sff_freeze - Freeze SFF controller port
1657 * @ap: port to freeze
1659 * Freeze BMDMA controller port.
1662 * Inherited from caller.
1664 void ata_sff_freeze(struct ata_port
*ap
)
1666 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1668 ap
->ctl
|= ATA_NIEN
;
1669 ap
->last_ctl
= ap
->ctl
;
1671 if (ioaddr
->ctl_addr
)
1672 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
1674 /* Under certain circumstances, some controllers raise IRQ on
1675 * ATA_NIEN manipulation. Also, many controllers fail to mask
1676 * previously pending IRQ on ATA_NIEN assertion. Clear it.
1678 ap
->ops
->sff_check_status(ap
);
1680 ap
->ops
->sff_irq_clear(ap
);
1684 * ata_sff_thaw - Thaw SFF controller port
1687 * Thaw SFF controller port.
1690 * Inherited from caller.
1692 void ata_sff_thaw(struct ata_port
*ap
)
1694 /* clear & re-enable interrupts */
1695 ap
->ops
->sff_check_status(ap
);
1696 ap
->ops
->sff_irq_clear(ap
);
1697 ap
->ops
->sff_irq_on(ap
);
1701 * ata_sff_prereset - prepare SFF link for reset
1702 * @link: SFF link to be reset
1703 * @deadline: deadline jiffies for the operation
1705 * SFF link @link is about to be reset. Initialize it. It first
1706 * calls ata_std_prereset() and wait for !BSY if the port is
1710 * Kernel thread context (may sleep)
1713 * 0 on success, -errno otherwise.
1715 int ata_sff_prereset(struct ata_link
*link
, unsigned long deadline
)
1717 struct ata_eh_context
*ehc
= &link
->eh_context
;
1720 rc
= ata_std_prereset(link
, deadline
);
1724 /* if we're about to do hardreset, nothing more to do */
1725 if (ehc
->i
.action
& ATA_EH_HARDRESET
)
1728 /* wait for !BSY if we don't know that no device is attached */
1729 if (!ata_link_offline(link
)) {
1730 rc
= ata_sff_wait_ready(link
, deadline
);
1731 if (rc
&& rc
!= -ENODEV
) {
1732 ata_link_printk(link
, KERN_WARNING
, "device not ready "
1733 "(errno=%d), forcing hardreset\n", rc
);
1734 ehc
->i
.action
|= ATA_EH_HARDRESET
;
1742 * ata_devchk - PATA device presence detection
1743 * @ap: ATA channel to examine
1744 * @device: Device to examine (starting at zero)
1746 * This technique was originally described in
1747 * Hale Landis's ATADRVR (www.ata-atapi.com), and
1748 * later found its way into the ATA/ATAPI spec.
1750 * Write a pattern to the ATA shadow registers,
1751 * and if a device is present, it will respond by
1752 * correctly storing and echoing back the
1753 * ATA shadow register contents.
1758 static unsigned int ata_devchk(struct ata_port
*ap
, unsigned int device
)
1760 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1763 ap
->ops
->sff_dev_select(ap
, device
);
1765 iowrite8(0x55, ioaddr
->nsect_addr
);
1766 iowrite8(0xaa, ioaddr
->lbal_addr
);
1768 iowrite8(0xaa, ioaddr
->nsect_addr
);
1769 iowrite8(0x55, ioaddr
->lbal_addr
);
1771 iowrite8(0x55, ioaddr
->nsect_addr
);
1772 iowrite8(0xaa, ioaddr
->lbal_addr
);
1774 nsect
= ioread8(ioaddr
->nsect_addr
);
1775 lbal
= ioread8(ioaddr
->lbal_addr
);
1777 if ((nsect
== 0x55) && (lbal
== 0xaa))
1778 return 1; /* we found a device */
1780 return 0; /* nothing found */
1784 * ata_sff_dev_classify - Parse returned ATA device signature
1785 * @dev: ATA device to classify (starting at zero)
1786 * @present: device seems present
1787 * @r_err: Value of error register on completion
1789 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
1790 * an ATA/ATAPI-defined set of values is placed in the ATA
1791 * shadow registers, indicating the results of device detection
1794 * Select the ATA device, and read the values from the ATA shadow
1795 * registers. Then parse according to the Error register value,
1796 * and the spec-defined values examined by ata_dev_classify().
1802 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
1804 unsigned int ata_sff_dev_classify(struct ata_device
*dev
, int present
,
1807 struct ata_port
*ap
= dev
->link
->ap
;
1808 struct ata_taskfile tf
;
1812 ap
->ops
->sff_dev_select(ap
, dev
->devno
);
1814 memset(&tf
, 0, sizeof(tf
));
1816 ap
->ops
->sff_tf_read(ap
, &tf
);
1821 /* see if device passed diags: continue and warn later */
1823 /* diagnostic fail : do nothing _YET_ */
1824 dev
->horkage
|= ATA_HORKAGE_DIAGNOSTIC
;
1827 else if ((dev
->devno
== 0) && (err
== 0x81))
1830 return ATA_DEV_NONE
;
1832 /* determine if device is ATA or ATAPI */
1833 class = ata_dev_classify(&tf
);
1835 if (class == ATA_DEV_UNKNOWN
) {
1836 /* If the device failed diagnostic, it's likely to
1837 * have reported incorrect device signature too.
1838 * Assume ATA device if the device seems present but
1839 * device signature is invalid with diagnostic
1842 if (present
&& (dev
->horkage
& ATA_HORKAGE_DIAGNOSTIC
))
1843 class = ATA_DEV_ATA
;
1845 class = ATA_DEV_NONE
;
1846 } else if ((class == ATA_DEV_ATA
) &&
1847 (ap
->ops
->sff_check_status(ap
) == 0))
1848 class = ATA_DEV_NONE
;
1854 * ata_sff_wait_after_reset - wait for devices to become ready after reset
1855 * @link: SFF link which is just reset
1856 * @devmask: mask of present devices
1857 * @deadline: deadline jiffies for the operation
1859 * Wait devices attached to SFF @link to become ready after
1860 * reset. It contains preceding 150ms wait to avoid accessing TF
1861 * status register too early.
1864 * Kernel thread context (may sleep).
1867 * 0 on success, -ENODEV if some or all of devices in @devmask
1868 * don't seem to exist. -errno on other errors.
1870 int ata_sff_wait_after_reset(struct ata_link
*link
, unsigned int devmask
,
1871 unsigned long deadline
)
1873 struct ata_port
*ap
= link
->ap
;
1874 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1875 unsigned int dev0
= devmask
& (1 << 0);
1876 unsigned int dev1
= devmask
& (1 << 1);
1879 msleep(ATA_WAIT_AFTER_RESET_MSECS
);
1881 /* always check readiness of the master device */
1882 rc
= ata_sff_wait_ready(link
, deadline
);
1883 /* -ENODEV means the odd clown forgot the D7 pulldown resistor
1884 * and TF status is 0xff, bail out on it too.
1889 /* if device 1 was found in ata_devchk, wait for register
1890 * access briefly, then wait for BSY to clear.
1895 ap
->ops
->sff_dev_select(ap
, 1);
1897 /* Wait for register access. Some ATAPI devices fail
1898 * to set nsect/lbal after reset, so don't waste too
1899 * much time on it. We're gonna wait for !BSY anyway.
1901 for (i
= 0; i
< 2; i
++) {
1904 nsect
= ioread8(ioaddr
->nsect_addr
);
1905 lbal
= ioread8(ioaddr
->lbal_addr
);
1906 if ((nsect
== 1) && (lbal
== 1))
1908 msleep(50); /* give drive a breather */
1911 rc
= ata_sff_wait_ready(link
, deadline
);
1919 /* is all this really necessary? */
1920 ap
->ops
->sff_dev_select(ap
, 0);
1922 ap
->ops
->sff_dev_select(ap
, 1);
1924 ap
->ops
->sff_dev_select(ap
, 0);
1929 static int ata_bus_softreset(struct ata_port
*ap
, unsigned int devmask
,
1930 unsigned long deadline
)
1932 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1934 DPRINTK("ata%u: bus reset via SRST\n", ap
->print_id
);
1936 /* software reset. causes dev0 to be selected */
1937 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
1938 udelay(20); /* FIXME: flush */
1939 iowrite8(ap
->ctl
| ATA_SRST
, ioaddr
->ctl_addr
);
1940 udelay(20); /* FIXME: flush */
1941 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
1943 /* wait the port to become ready */
1944 return ata_sff_wait_after_reset(&ap
->link
, devmask
, deadline
);
1948 * ata_sff_softreset - reset host port via ATA SRST
1949 * @link: ATA link to reset
1950 * @classes: resulting classes of attached devices
1951 * @deadline: deadline jiffies for the operation
1953 * Reset host port using ATA SRST.
1956 * Kernel thread context (may sleep)
1959 * 0 on success, -errno otherwise.
1961 int ata_sff_softreset(struct ata_link
*link
, unsigned int *classes
,
1962 unsigned long deadline
)
1964 struct ata_port
*ap
= link
->ap
;
1965 unsigned int slave_possible
= ap
->flags
& ATA_FLAG_SLAVE_POSS
;
1966 unsigned int devmask
= 0;
1972 /* determine if device 0/1 are present */
1973 if (ata_devchk(ap
, 0))
1974 devmask
|= (1 << 0);
1975 if (slave_possible
&& ata_devchk(ap
, 1))
1976 devmask
|= (1 << 1);
1978 /* select device 0 again */
1979 ap
->ops
->sff_dev_select(ap
, 0);
1981 /* issue bus reset */
1982 DPRINTK("about to softreset, devmask=%x\n", devmask
);
1983 rc
= ata_bus_softreset(ap
, devmask
, deadline
);
1984 /* if link is occupied, -ENODEV too is an error */
1985 if (rc
&& (rc
!= -ENODEV
|| sata_scr_valid(link
))) {
1986 ata_link_printk(link
, KERN_ERR
, "SRST failed (errno=%d)\n", rc
);
1990 /* determine by signature whether we have ATA or ATAPI devices */
1991 classes
[0] = ata_sff_dev_classify(&link
->device
[0],
1992 devmask
& (1 << 0), &err
);
1993 if (slave_possible
&& err
!= 0x81)
1994 classes
[1] = ata_sff_dev_classify(&link
->device
[1],
1995 devmask
& (1 << 1), &err
);
1997 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes
[0], classes
[1]);
2002 * sata_sff_hardreset - reset host port via SATA phy reset
2003 * @link: link to reset
2004 * @class: resulting class of attached device
2005 * @deadline: deadline jiffies for the operation
2007 * SATA phy-reset host port using DET bits of SControl register,
2008 * wait for !BSY and classify the attached device.
2011 * Kernel thread context (may sleep)
2014 * 0 on success, -errno otherwise.
2016 int sata_sff_hardreset(struct ata_link
*link
, unsigned int *class,
2017 unsigned long deadline
)
2019 struct ata_eh_context
*ehc
= &link
->eh_context
;
2020 const unsigned long *timing
= sata_ehc_deb_timing(ehc
);
2024 rc
= sata_link_hardreset(link
, timing
, deadline
, &online
,
2025 ata_sff_check_ready
);
2027 *class = ata_sff_dev_classify(link
->device
, 1, NULL
);
2029 DPRINTK("EXIT, class=%u\n", *class);
2034 * ata_sff_postreset - SFF postreset callback
2035 * @link: the target SFF ata_link
2036 * @classes: classes of attached devices
2038 * This function is invoked after a successful reset. It first
2039 * calls ata_std_postreset() and performs SFF specific postreset
2043 * Kernel thread context (may sleep)
2045 void ata_sff_postreset(struct ata_link
*link
, unsigned int *classes
)
2047 struct ata_port
*ap
= link
->ap
;
2049 ata_std_postreset(link
, classes
);
2051 /* is double-select really necessary? */
2052 if (classes
[0] != ATA_DEV_NONE
)
2053 ap
->ops
->sff_dev_select(ap
, 1);
2054 if (classes
[1] != ATA_DEV_NONE
)
2055 ap
->ops
->sff_dev_select(ap
, 0);
2057 /* bail out if no device is present */
2058 if (classes
[0] == ATA_DEV_NONE
&& classes
[1] == ATA_DEV_NONE
) {
2059 DPRINTK("EXIT, no device\n");
2063 /* set up device control */
2064 if (ap
->ioaddr
.ctl_addr
)
2065 iowrite8(ap
->ctl
, ap
->ioaddr
.ctl_addr
);
2069 * ata_sff_error_handler - Stock error handler for BMDMA controller
2070 * @ap: port to handle error for
2072 * Stock error handler for SFF controller. It can handle both
2073 * PATA and SATA controllers. Many controllers should be able to
2074 * use this EH as-is or with some added handling before and
2078 * Kernel thread context (may sleep)
2080 void ata_sff_error_handler(struct ata_port
*ap
)
2082 ata_reset_fn_t softreset
= ap
->ops
->softreset
;
2083 ata_reset_fn_t hardreset
= ap
->ops
->hardreset
;
2084 struct ata_queued_cmd
*qc
;
2085 unsigned long flags
;
2088 qc
= __ata_qc_from_tag(ap
, ap
->link
.active_tag
);
2089 if (qc
&& !(qc
->flags
& ATA_QCFLAG_FAILED
))
2092 /* reset PIO HSM and stop DMA engine */
2093 spin_lock_irqsave(ap
->lock
, flags
);
2095 ap
->hsm_task_state
= HSM_ST_IDLE
;
2097 if (ap
->ioaddr
.bmdma_addr
&&
2098 qc
&& (qc
->tf
.protocol
== ATA_PROT_DMA
||
2099 qc
->tf
.protocol
== ATAPI_PROT_DMA
)) {
2102 host_stat
= ap
->ops
->bmdma_status(ap
);
2104 /* BMDMA controllers indicate host bus error by
2105 * setting DMA_ERR bit and timing out. As it wasn't
2106 * really a timeout event, adjust error mask and
2107 * cancel frozen state.
2109 if (qc
->err_mask
== AC_ERR_TIMEOUT
&& (host_stat
& ATA_DMA_ERR
)) {
2110 qc
->err_mask
= AC_ERR_HOST_BUS
;
2114 ap
->ops
->bmdma_stop(qc
);
2117 ata_sff_sync(ap
); /* FIXME: We don't need this */
2118 ap
->ops
->sff_check_status(ap
);
2119 ap
->ops
->sff_irq_clear(ap
);
2121 spin_unlock_irqrestore(ap
->lock
, flags
);
2124 ata_eh_thaw_port(ap
);
2126 /* PIO and DMA engines have been stopped, perform recovery */
2128 /* Ignore ata_sff_softreset if ctl isn't accessible and
2129 * built-in hardresets if SCR access isn't available.
2131 if (softreset
== ata_sff_softreset
&& !ap
->ioaddr
.ctl_addr
)
2133 if (ata_is_builtin_hardreset(hardreset
) && !sata_scr_valid(&ap
->link
))
2136 ata_do_eh(ap
, ap
->ops
->prereset
, softreset
, hardreset
,
2137 ap
->ops
->postreset
);
2141 * ata_sff_post_internal_cmd - Stock post_internal_cmd for SFF controller
2142 * @qc: internal command to clean up
2145 * Kernel thread context (may sleep)
2147 void ata_sff_post_internal_cmd(struct ata_queued_cmd
*qc
)
2149 if (qc
->ap
->ioaddr
.bmdma_addr
)
2154 * ata_sff_port_start - Set port up for dma.
2155 * @ap: Port to initialize
2157 * Called just after data structures for each port are
2158 * initialized. Allocates space for PRD table if the device
2159 * is DMA capable SFF.
2161 * May be used as the port_start() entry in ata_port_operations.
2164 * Inherited from caller.
2166 int ata_sff_port_start(struct ata_port
*ap
)
2168 if (ap
->ioaddr
.bmdma_addr
)
2169 return ata_port_start(ap
);
2174 * ata_sff_std_ports - initialize ioaddr with standard port offsets.
2175 * @ioaddr: IO address structure to be initialized
2177 * Utility function which initializes data_addr, error_addr,
2178 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
2179 * device_addr, status_addr, and command_addr to standard offsets
2180 * relative to cmd_addr.
2182 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
2184 void ata_sff_std_ports(struct ata_ioports
*ioaddr
)
2186 ioaddr
->data_addr
= ioaddr
->cmd_addr
+ ATA_REG_DATA
;
2187 ioaddr
->error_addr
= ioaddr
->cmd_addr
+ ATA_REG_ERR
;
2188 ioaddr
->feature_addr
= ioaddr
->cmd_addr
+ ATA_REG_FEATURE
;
2189 ioaddr
->nsect_addr
= ioaddr
->cmd_addr
+ ATA_REG_NSECT
;
2190 ioaddr
->lbal_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAL
;
2191 ioaddr
->lbam_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAM
;
2192 ioaddr
->lbah_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAH
;
2193 ioaddr
->device_addr
= ioaddr
->cmd_addr
+ ATA_REG_DEVICE
;
2194 ioaddr
->status_addr
= ioaddr
->cmd_addr
+ ATA_REG_STATUS
;
2195 ioaddr
->command_addr
= ioaddr
->cmd_addr
+ ATA_REG_CMD
;
2198 unsigned long ata_bmdma_mode_filter(struct ata_device
*adev
,
2199 unsigned long xfer_mask
)
2201 /* Filter out DMA modes if the device has been configured by
2202 the BIOS as PIO only */
2204 if (adev
->link
->ap
->ioaddr
.bmdma_addr
== NULL
)
2205 xfer_mask
&= ~(ATA_MASK_MWDMA
| ATA_MASK_UDMA
);
2210 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
2211 * @qc: Info associated with this ATA transaction.
2214 * spin_lock_irqsave(host lock)
2216 void ata_bmdma_setup(struct ata_queued_cmd
*qc
)
2218 struct ata_port
*ap
= qc
->ap
;
2219 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
2222 /* load PRD table addr. */
2223 mb(); /* make sure PRD table writes are visible to controller */
2224 iowrite32(ap
->prd_dma
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_TABLE_OFS
);
2226 /* specify data direction, triple-check start bit is clear */
2227 dmactl
= ioread8(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
2228 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
2230 dmactl
|= ATA_DMA_WR
;
2231 iowrite8(dmactl
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
2233 /* issue r/w command */
2234 ap
->ops
->sff_exec_command(ap
, &qc
->tf
);
2238 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
2239 * @qc: Info associated with this ATA transaction.
2242 * spin_lock_irqsave(host lock)
2244 void ata_bmdma_start(struct ata_queued_cmd
*qc
)
2246 struct ata_port
*ap
= qc
->ap
;
2249 /* start host DMA transaction */
2250 dmactl
= ioread8(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
2251 iowrite8(dmactl
| ATA_DMA_START
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
2253 /* Strictly, one may wish to issue an ioread8() here, to
2254 * flush the mmio write. However, control also passes
2255 * to the hardware at this point, and it will interrupt
2256 * us when we are to resume control. So, in effect,
2257 * we don't care when the mmio write flushes.
2258 * Further, a read of the DMA status register _immediately_
2259 * following the write may not be what certain flaky hardware
2260 * is expected, so I think it is best to not add a readb()
2261 * without first all the MMIO ATA cards/mobos.
2262 * Or maybe I'm just being paranoid.
2264 * FIXME: The posting of this write means I/O starts are
2265 * unneccessarily delayed for MMIO
2270 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
2271 * @qc: Command we are ending DMA for
2273 * Clears the ATA_DMA_START flag in the dma control register
2275 * May be used as the bmdma_stop() entry in ata_port_operations.
2278 * spin_lock_irqsave(host lock)
2280 void ata_bmdma_stop(struct ata_queued_cmd
*qc
)
2282 struct ata_port
*ap
= qc
->ap
;
2283 void __iomem
*mmio
= ap
->ioaddr
.bmdma_addr
;
2285 /* clear start/stop bit */
2286 iowrite8(ioread8(mmio
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
2287 mmio
+ ATA_DMA_CMD
);
2289 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
2290 ata_sff_dma_pause(ap
);
2294 * ata_bmdma_status - Read PCI IDE BMDMA status
2295 * @ap: Port associated with this ATA transaction.
2297 * Read and return BMDMA status register.
2299 * May be used as the bmdma_status() entry in ata_port_operations.
2302 * spin_lock_irqsave(host lock)
2304 u8
ata_bmdma_status(struct ata_port
*ap
)
2306 return ioread8(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
);
2310 * ata_bus_reset - reset host port and associated ATA channel
2311 * @ap: port to reset
2313 * This is typically the first time we actually start issuing
2314 * commands to the ATA channel. We wait for BSY to clear, then
2315 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2316 * result. Determine what devices, if any, are on the channel
2317 * by looking at the device 0/1 error register. Look at the signature
2318 * stored in each device's taskfile registers, to determine if
2319 * the device is ATA or ATAPI.
2322 * PCI/etc. bus probe sem.
2323 * Obtains host lock.
2326 * Sets ATA_FLAG_DISABLED if bus reset fails.
2329 * This function is only for drivers which still use old EH and
2330 * will be removed soon.
2332 void ata_bus_reset(struct ata_port
*ap
)
2334 struct ata_device
*device
= ap
->link
.device
;
2335 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
2336 unsigned int slave_possible
= ap
->flags
& ATA_FLAG_SLAVE_POSS
;
2338 unsigned int dev0
, dev1
= 0, devmask
= 0;
2341 DPRINTK("ENTER, host %u, port %u\n", ap
->print_id
, ap
->port_no
);
2343 /* determine if device 0/1 are present */
2344 if (ap
->flags
& ATA_FLAG_SATA_RESET
)
2347 dev0
= ata_devchk(ap
, 0);
2349 dev1
= ata_devchk(ap
, 1);
2353 devmask
|= (1 << 0);
2355 devmask
|= (1 << 1);
2357 /* select device 0 again */
2358 ap
->ops
->sff_dev_select(ap
, 0);
2360 /* issue bus reset */
2361 if (ap
->flags
& ATA_FLAG_SRST
) {
2362 rc
= ata_bus_softreset(ap
, devmask
, jiffies
+ 40 * HZ
);
2363 if (rc
&& rc
!= -ENODEV
)
2368 * determine by signature whether we have ATA or ATAPI devices
2370 device
[0].class = ata_sff_dev_classify(&device
[0], dev0
, &err
);
2371 if ((slave_possible
) && (err
!= 0x81))
2372 device
[1].class = ata_sff_dev_classify(&device
[1], dev1
, &err
);
2374 /* is double-select really necessary? */
2375 if (device
[1].class != ATA_DEV_NONE
)
2376 ap
->ops
->sff_dev_select(ap
, 1);
2377 if (device
[0].class != ATA_DEV_NONE
)
2378 ap
->ops
->sff_dev_select(ap
, 0);
2380 /* if no devices were detected, disable this port */
2381 if ((device
[0].class == ATA_DEV_NONE
) &&
2382 (device
[1].class == ATA_DEV_NONE
))
2385 if (ap
->flags
& (ATA_FLAG_SATA_RESET
| ATA_FLAG_SRST
)) {
2386 /* set up device control for ATA_FLAG_SATA_RESET */
2387 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
2394 ata_port_printk(ap
, KERN_ERR
, "disabling port\n");
2395 ata_port_disable(ap
);
2403 * ata_pci_bmdma_clear_simplex - attempt to kick device out of simplex
2406 * Some PCI ATA devices report simplex mode but in fact can be told to
2407 * enter non simplex mode. This implements the necessary logic to
2408 * perform the task on such devices. Calling it on other devices will
2409 * have -undefined- behaviour.
2411 int ata_pci_bmdma_clear_simplex(struct pci_dev
*pdev
)
2413 unsigned long bmdma
= pci_resource_start(pdev
, 4);
2419 simplex
= inb(bmdma
+ 0x02);
2420 outb(simplex
& 0x60, bmdma
+ 0x02);
2421 simplex
= inb(bmdma
+ 0x02);
2428 * ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
2429 * @host: target ATA host
2431 * Acquire PCI BMDMA resources and initialize @host accordingly.
2434 * Inherited from calling layer (may sleep).
2437 * 0 on success, -errno otherwise.
2439 int ata_pci_bmdma_init(struct ata_host
*host
)
2441 struct device
*gdev
= host
->dev
;
2442 struct pci_dev
*pdev
= to_pci_dev(gdev
);
2445 /* No BAR4 allocation: No DMA */
2446 if (pci_resource_start(pdev
, 4) == 0)
2449 /* TODO: If we get no DMA mask we should fall back to PIO */
2450 rc
= pci_set_dma_mask(pdev
, ATA_DMA_MASK
);
2453 rc
= pci_set_consistent_dma_mask(pdev
, ATA_DMA_MASK
);
2457 /* request and iomap DMA region */
2458 rc
= pcim_iomap_regions(pdev
, 1 << 4, dev_driver_string(gdev
));
2460 dev_printk(KERN_ERR
, gdev
, "failed to request/iomap BAR4\n");
2463 host
->iomap
= pcim_iomap_table(pdev
);
2465 for (i
= 0; i
< 2; i
++) {
2466 struct ata_port
*ap
= host
->ports
[i
];
2467 void __iomem
*bmdma
= host
->iomap
[4] + 8 * i
;
2469 if (ata_port_is_dummy(ap
))
2472 ap
->ioaddr
.bmdma_addr
= bmdma
;
2473 if ((!(ap
->flags
& ATA_FLAG_IGN_SIMPLEX
)) &&
2474 (ioread8(bmdma
+ 2) & 0x80))
2475 host
->flags
|= ATA_HOST_SIMPLEX
;
2477 ata_port_desc(ap
, "bmdma 0x%llx",
2478 (unsigned long long)pci_resource_start(pdev
, 4) + 8 * i
);
2484 static int ata_resources_present(struct pci_dev
*pdev
, int port
)
2488 /* Check the PCI resources for this channel are enabled */
2490 for (i
= 0; i
< 2; i
++) {
2491 if (pci_resource_start(pdev
, port
+ i
) == 0 ||
2492 pci_resource_len(pdev
, port
+ i
) == 0)
2499 * ata_pci_sff_init_host - acquire native PCI ATA resources and init host
2500 * @host: target ATA host
2502 * Acquire native PCI ATA resources for @host and initialize the
2503 * first two ports of @host accordingly. Ports marked dummy are
2504 * skipped and allocation failure makes the port dummy.
2506 * Note that native PCI resources are valid even for legacy hosts
2507 * as we fix up pdev resources array early in boot, so this
2508 * function can be used for both native and legacy SFF hosts.
2511 * Inherited from calling layer (may sleep).
2514 * 0 if at least one port is initialized, -ENODEV if no port is
2517 int ata_pci_sff_init_host(struct ata_host
*host
)
2519 struct device
*gdev
= host
->dev
;
2520 struct pci_dev
*pdev
= to_pci_dev(gdev
);
2521 unsigned int mask
= 0;
2524 /* request, iomap BARs and init port addresses accordingly */
2525 for (i
= 0; i
< 2; i
++) {
2526 struct ata_port
*ap
= host
->ports
[i
];
2528 void __iomem
* const *iomap
;
2530 if (ata_port_is_dummy(ap
))
2533 /* Discard disabled ports. Some controllers show
2534 * their unused channels this way. Disabled ports are
2537 if (!ata_resources_present(pdev
, i
)) {
2538 ap
->ops
= &ata_dummy_port_ops
;
2542 rc
= pcim_iomap_regions(pdev
, 0x3 << base
,
2543 dev_driver_string(gdev
));
2545 dev_printk(KERN_WARNING
, gdev
,
2546 "failed to request/iomap BARs for port %d "
2547 "(errno=%d)\n", i
, rc
);
2549 pcim_pin_device(pdev
);
2550 ap
->ops
= &ata_dummy_port_ops
;
2553 host
->iomap
= iomap
= pcim_iomap_table(pdev
);
2555 ap
->ioaddr
.cmd_addr
= iomap
[base
];
2556 ap
->ioaddr
.altstatus_addr
=
2557 ap
->ioaddr
.ctl_addr
= (void __iomem
*)
2558 ((unsigned long)iomap
[base
+ 1] | ATA_PCI_CTL_OFS
);
2559 ata_sff_std_ports(&ap
->ioaddr
);
2561 ata_port_desc(ap
, "cmd 0x%llx ctl 0x%llx",
2562 (unsigned long long)pci_resource_start(pdev
, base
),
2563 (unsigned long long)pci_resource_start(pdev
, base
+ 1));
2569 dev_printk(KERN_ERR
, gdev
, "no available native port\n");
2577 * ata_pci_sff_prepare_host - helper to prepare native PCI ATA host
2578 * @pdev: target PCI device
2579 * @ppi: array of port_info, must be enough for two ports
2580 * @r_host: out argument for the initialized ATA host
2582 * Helper to allocate ATA host for @pdev, acquire all native PCI
2583 * resources and initialize it accordingly in one go.
2586 * Inherited from calling layer (may sleep).
2589 * 0 on success, -errno otherwise.
2591 int ata_pci_sff_prepare_host(struct pci_dev
*pdev
,
2592 const struct ata_port_info
* const * ppi
,
2593 struct ata_host
**r_host
)
2595 struct ata_host
*host
;
2598 if (!devres_open_group(&pdev
->dev
, NULL
, GFP_KERNEL
))
2601 host
= ata_host_alloc_pinfo(&pdev
->dev
, ppi
, 2);
2603 dev_printk(KERN_ERR
, &pdev
->dev
,
2604 "failed to allocate ATA host\n");
2609 rc
= ata_pci_sff_init_host(host
);
2613 /* init DMA related stuff */
2614 rc
= ata_pci_bmdma_init(host
);
2618 devres_remove_group(&pdev
->dev
, NULL
);
2623 /* This is necessary because PCI and iomap resources are
2624 * merged and releasing the top group won't release the
2625 * acquired resources if some of those have been acquired
2626 * before entering this function.
2628 pcim_iounmap_regions(pdev
, 0xf);
2630 devres_release_group(&pdev
->dev
, NULL
);
2635 * ata_pci_sff_activate_host - start SFF host, request IRQ and register it
2636 * @host: target SFF ATA host
2637 * @irq_handler: irq_handler used when requesting IRQ(s)
2638 * @sht: scsi_host_template to use when registering the host
2640 * This is the counterpart of ata_host_activate() for SFF ATA
2641 * hosts. This separate helper is necessary because SFF hosts
2642 * use two separate interrupts in legacy mode.
2645 * Inherited from calling layer (may sleep).
2648 * 0 on success, -errno otherwise.
2650 int ata_pci_sff_activate_host(struct ata_host
*host
,
2651 irq_handler_t irq_handler
,
2652 struct scsi_host_template
*sht
)
2654 struct device
*dev
= host
->dev
;
2655 struct pci_dev
*pdev
= to_pci_dev(dev
);
2656 const char *drv_name
= dev_driver_string(host
->dev
);
2657 int legacy_mode
= 0, rc
;
2659 rc
= ata_host_start(host
);
2663 if ((pdev
->class >> 8) == PCI_CLASS_STORAGE_IDE
) {
2666 /* TODO: What if one channel is in native mode ... */
2667 pci_read_config_byte(pdev
, PCI_CLASS_PROG
, &tmp8
);
2668 mask
= (1 << 2) | (1 << 0);
2669 if ((tmp8
& mask
) != mask
)
2671 #if defined(CONFIG_NO_ATA_LEGACY)
2672 /* Some platforms with PCI limits cannot address compat
2673 port space. In that case we punt if their firmware has
2674 left a device in compatibility mode */
2676 printk(KERN_ERR
"ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
2682 if (!devres_open_group(dev
, NULL
, GFP_KERNEL
))
2685 if (!legacy_mode
&& pdev
->irq
) {
2686 rc
= devm_request_irq(dev
, pdev
->irq
, irq_handler
,
2687 IRQF_SHARED
, drv_name
, host
);
2691 ata_port_desc(host
->ports
[0], "irq %d", pdev
->irq
);
2692 ata_port_desc(host
->ports
[1], "irq %d", pdev
->irq
);
2693 } else if (legacy_mode
) {
2694 if (!ata_port_is_dummy(host
->ports
[0])) {
2695 rc
= devm_request_irq(dev
, ATA_PRIMARY_IRQ(pdev
),
2696 irq_handler
, IRQF_SHARED
,
2701 ata_port_desc(host
->ports
[0], "irq %d",
2702 ATA_PRIMARY_IRQ(pdev
));
2705 if (!ata_port_is_dummy(host
->ports
[1])) {
2706 rc
= devm_request_irq(dev
, ATA_SECONDARY_IRQ(pdev
),
2707 irq_handler
, IRQF_SHARED
,
2712 ata_port_desc(host
->ports
[1], "irq %d",
2713 ATA_SECONDARY_IRQ(pdev
));
2717 rc
= ata_host_register(host
, sht
);
2720 devres_remove_group(dev
, NULL
);
2722 devres_release_group(dev
, NULL
);
2728 * ata_pci_sff_init_one - Initialize/register PCI IDE host controller
2729 * @pdev: Controller to be initialized
2730 * @ppi: array of port_info, must be enough for two ports
2731 * @sht: scsi_host_template to use when registering the host
2732 * @host_priv: host private_data
2734 * This is a helper function which can be called from a driver's
2735 * xxx_init_one() probe function if the hardware uses traditional
2736 * IDE taskfile registers.
2738 * This function calls pci_enable_device(), reserves its register
2739 * regions, sets the dma mask, enables bus master mode, and calls
2743 * Nobody makes a single channel controller that appears solely as
2744 * the secondary legacy port on PCI.
2747 * Inherited from PCI layer (may sleep).
2750 * Zero on success, negative on errno-based value on error.
2752 int ata_pci_sff_init_one(struct pci_dev
*pdev
,
2753 const struct ata_port_info
* const * ppi
,
2754 struct scsi_host_template
*sht
, void *host_priv
)
2756 struct device
*dev
= &pdev
->dev
;
2757 const struct ata_port_info
*pi
= NULL
;
2758 struct ata_host
*host
= NULL
;
2763 /* look up the first valid port_info */
2764 for (i
= 0; i
< 2 && ppi
[i
]; i
++) {
2765 if (ppi
[i
]->port_ops
!= &ata_dummy_port_ops
) {
2772 dev_printk(KERN_ERR
, &pdev
->dev
,
2773 "no valid port_info specified\n");
2777 if (!devres_open_group(dev
, NULL
, GFP_KERNEL
))
2780 rc
= pcim_enable_device(pdev
);
2784 /* prepare and activate SFF host */
2785 rc
= ata_pci_sff_prepare_host(pdev
, ppi
, &host
);
2788 host
->private_data
= host_priv
;
2790 pci_set_master(pdev
);
2791 rc
= ata_pci_sff_activate_host(host
, ata_sff_interrupt
, sht
);
2794 devres_remove_group(&pdev
->dev
, NULL
);
2796 devres_release_group(&pdev
->dev
, NULL
);
2801 #endif /* CONFIG_PCI */
2803 EXPORT_SYMBOL_GPL(ata_sff_port_ops
);
2804 EXPORT_SYMBOL_GPL(ata_bmdma_port_ops
);
2805 EXPORT_SYMBOL_GPL(ata_sff_qc_prep
);
2806 EXPORT_SYMBOL_GPL(ata_sff_dumb_qc_prep
);
2807 EXPORT_SYMBOL_GPL(ata_sff_dev_select
);
2808 EXPORT_SYMBOL_GPL(ata_sff_check_status
);
2809 EXPORT_SYMBOL_GPL(ata_sff_dma_pause
);
2810 EXPORT_SYMBOL_GPL(ata_sff_pause
);
2811 EXPORT_SYMBOL_GPL(ata_sff_busy_sleep
);
2812 EXPORT_SYMBOL_GPL(ata_sff_wait_ready
);
2813 EXPORT_SYMBOL_GPL(ata_sff_tf_load
);
2814 EXPORT_SYMBOL_GPL(ata_sff_tf_read
);
2815 EXPORT_SYMBOL_GPL(ata_sff_exec_command
);
2816 EXPORT_SYMBOL_GPL(ata_sff_data_xfer
);
2817 EXPORT_SYMBOL_GPL(ata_sff_data_xfer_noirq
);
2818 EXPORT_SYMBOL_GPL(ata_sff_irq_on
);
2819 EXPORT_SYMBOL_GPL(ata_sff_irq_clear
);
2820 EXPORT_SYMBOL_GPL(ata_sff_hsm_move
);
2821 EXPORT_SYMBOL_GPL(ata_sff_qc_issue
);
2822 EXPORT_SYMBOL_GPL(ata_sff_qc_fill_rtf
);
2823 EXPORT_SYMBOL_GPL(ata_sff_host_intr
);
2824 EXPORT_SYMBOL_GPL(ata_sff_interrupt
);
2825 EXPORT_SYMBOL_GPL(ata_sff_freeze
);
2826 EXPORT_SYMBOL_GPL(ata_sff_thaw
);
2827 EXPORT_SYMBOL_GPL(ata_sff_prereset
);
2828 EXPORT_SYMBOL_GPL(ata_sff_dev_classify
);
2829 EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset
);
2830 EXPORT_SYMBOL_GPL(ata_sff_softreset
);
2831 EXPORT_SYMBOL_GPL(sata_sff_hardreset
);
2832 EXPORT_SYMBOL_GPL(ata_sff_postreset
);
2833 EXPORT_SYMBOL_GPL(ata_sff_error_handler
);
2834 EXPORT_SYMBOL_GPL(ata_sff_post_internal_cmd
);
2835 EXPORT_SYMBOL_GPL(ata_sff_port_start
);
2836 EXPORT_SYMBOL_GPL(ata_sff_std_ports
);
2837 EXPORT_SYMBOL_GPL(ata_bmdma_mode_filter
);
2838 EXPORT_SYMBOL_GPL(ata_bmdma_setup
);
2839 EXPORT_SYMBOL_GPL(ata_bmdma_start
);
2840 EXPORT_SYMBOL_GPL(ata_bmdma_stop
);
2841 EXPORT_SYMBOL_GPL(ata_bmdma_status
);
2842 EXPORT_SYMBOL_GPL(ata_bus_reset
);
2844 EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex
);
2845 EXPORT_SYMBOL_GPL(ata_pci_bmdma_init
);
2846 EXPORT_SYMBOL_GPL(ata_pci_sff_init_host
);
2847 EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host
);
2848 EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host
);
2849 EXPORT_SYMBOL_GPL(ata_pci_sff_init_one
);
2850 #endif /* CONFIG_PCI */