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 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_busy_sleep - sleep until BSY clears, or timeout
260 * @ap: port containing status register to be polled
261 * @tmout_pat: impatience timeout
262 * @tmout: overall timeout
264 * Sleep until ATA Status register bit BSY clears,
265 * or a timeout occurs.
268 * Kernel thread context (may sleep).
271 * 0 on success, -errno otherwise.
273 int ata_sff_busy_sleep(struct ata_port
*ap
,
274 unsigned long tmout_pat
, unsigned long tmout
)
276 unsigned long timer_start
, timeout
;
279 status
= ata_sff_busy_wait(ap
, ATA_BUSY
, 300);
280 timer_start
= jiffies
;
281 timeout
= timer_start
+ tmout_pat
;
282 while (status
!= 0xff && (status
& ATA_BUSY
) &&
283 time_before(jiffies
, timeout
)) {
285 status
= ata_sff_busy_wait(ap
, ATA_BUSY
, 3);
288 if (status
!= 0xff && (status
& ATA_BUSY
))
289 ata_port_printk(ap
, KERN_WARNING
,
290 "port is slow to respond, please be patient "
291 "(Status 0x%x)\n", status
);
293 timeout
= timer_start
+ tmout
;
294 while (status
!= 0xff && (status
& ATA_BUSY
) &&
295 time_before(jiffies
, timeout
)) {
297 status
= ap
->ops
->sff_check_status(ap
);
303 if (status
& ATA_BUSY
) {
304 ata_port_printk(ap
, KERN_ERR
, "port failed to respond "
305 "(%lu secs, Status 0x%x)\n",
313 static int ata_sff_check_ready(struct ata_link
*link
)
315 u8 status
= link
->ap
->ops
->sff_check_status(link
->ap
);
317 return ata_check_ready(status
);
321 * ata_sff_wait_ready - sleep until BSY clears, or timeout
322 * @link: SFF link to wait ready status for
323 * @deadline: deadline jiffies for the operation
325 * Sleep until ATA Status register bit BSY clears, or timeout
329 * Kernel thread context (may sleep).
332 * 0 on success, -errno otherwise.
334 int ata_sff_wait_ready(struct ata_link
*link
, unsigned long deadline
)
336 return ata_wait_ready(link
, deadline
, ata_sff_check_ready
);
340 * ata_sff_dev_select - Select device 0/1 on ATA bus
341 * @ap: ATA channel to manipulate
342 * @device: ATA device (numbered from zero) to select
344 * Use the method defined in the ATA specification to
345 * make either device 0, or device 1, active on the
346 * ATA channel. Works with both PIO and MMIO.
348 * May be used as the dev_select() entry in ata_port_operations.
353 void ata_sff_dev_select(struct ata_port
*ap
, unsigned int device
)
358 tmp
= ATA_DEVICE_OBS
;
360 tmp
= ATA_DEVICE_OBS
| ATA_DEV1
;
362 iowrite8(tmp
, ap
->ioaddr
.device_addr
);
363 ata_sff_pause(ap
); /* needed; also flushes, for mmio */
367 * ata_dev_select - Select device 0/1 on ATA bus
368 * @ap: ATA channel to manipulate
369 * @device: ATA device (numbered from zero) to select
370 * @wait: non-zero to wait for Status register BSY bit to clear
371 * @can_sleep: non-zero if context allows sleeping
373 * Use the method defined in the ATA specification to
374 * make either device 0, or device 1, active on the
377 * This is a high-level version of ata_sff_dev_select(), which
378 * additionally provides the services of inserting the proper
379 * pauses and status polling, where needed.
384 void ata_dev_select(struct ata_port
*ap
, unsigned int device
,
385 unsigned int wait
, unsigned int can_sleep
)
387 if (ata_msg_probe(ap
))
388 ata_port_printk(ap
, KERN_INFO
, "ata_dev_select: ENTER, "
389 "device %u, wait %u\n", device
, wait
);
394 ap
->ops
->sff_dev_select(ap
, device
);
397 if (can_sleep
&& ap
->link
.device
[device
].class == ATA_DEV_ATAPI
)
404 * ata_sff_irq_on - Enable interrupts on a port.
405 * @ap: Port on which interrupts are enabled.
407 * Enable interrupts on a legacy IDE device using MMIO or PIO,
408 * wait for idle, clear any pending interrupts.
411 * Inherited from caller.
413 u8
ata_sff_irq_on(struct ata_port
*ap
)
415 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
418 ap
->ctl
&= ~ATA_NIEN
;
419 ap
->last_ctl
= ap
->ctl
;
421 if (ioaddr
->ctl_addr
)
422 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
423 tmp
= ata_wait_idle(ap
);
425 ap
->ops
->sff_irq_clear(ap
);
431 * ata_sff_irq_clear - Clear PCI IDE BMDMA interrupt.
432 * @ap: Port associated with this ATA transaction.
434 * Clear interrupt and error flags in DMA status register.
436 * May be used as the irq_clear() entry in ata_port_operations.
439 * spin_lock_irqsave(host lock)
441 void ata_sff_irq_clear(struct ata_port
*ap
)
443 void __iomem
*mmio
= ap
->ioaddr
.bmdma_addr
;
448 iowrite8(ioread8(mmio
+ ATA_DMA_STATUS
), mmio
+ ATA_DMA_STATUS
);
452 * ata_sff_tf_load - send taskfile registers to host controller
453 * @ap: Port to which output is sent
454 * @tf: ATA taskfile register set
456 * Outputs ATA taskfile to standard ATA host controller.
459 * Inherited from caller.
461 void ata_sff_tf_load(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
463 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
464 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
466 if (tf
->ctl
!= ap
->last_ctl
) {
467 if (ioaddr
->ctl_addr
)
468 iowrite8(tf
->ctl
, ioaddr
->ctl_addr
);
469 ap
->last_ctl
= tf
->ctl
;
473 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
474 WARN_ON(!ioaddr
->ctl_addr
);
475 iowrite8(tf
->hob_feature
, ioaddr
->feature_addr
);
476 iowrite8(tf
->hob_nsect
, ioaddr
->nsect_addr
);
477 iowrite8(tf
->hob_lbal
, ioaddr
->lbal_addr
);
478 iowrite8(tf
->hob_lbam
, ioaddr
->lbam_addr
);
479 iowrite8(tf
->hob_lbah
, ioaddr
->lbah_addr
);
480 VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
489 iowrite8(tf
->feature
, ioaddr
->feature_addr
);
490 iowrite8(tf
->nsect
, ioaddr
->nsect_addr
);
491 iowrite8(tf
->lbal
, ioaddr
->lbal_addr
);
492 iowrite8(tf
->lbam
, ioaddr
->lbam_addr
);
493 iowrite8(tf
->lbah
, ioaddr
->lbah_addr
);
494 VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
502 if (tf
->flags
& ATA_TFLAG_DEVICE
) {
503 iowrite8(tf
->device
, ioaddr
->device_addr
);
504 VPRINTK("device 0x%X\n", tf
->device
);
511 * ata_sff_tf_read - input device's ATA taskfile shadow registers
512 * @ap: Port from which input is read
513 * @tf: ATA taskfile register set for storing input
515 * Reads ATA taskfile registers for currently-selected device
516 * into @tf. Assumes the device has a fully SFF compliant task file
517 * layout and behaviour. If you device does not (eg has a different
518 * status method) then you will need to provide a replacement tf_read
521 * Inherited from caller.
523 void ata_sff_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
)
525 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
527 tf
->command
= ata_sff_check_status(ap
);
528 tf
->feature
= ioread8(ioaddr
->error_addr
);
529 tf
->nsect
= ioread8(ioaddr
->nsect_addr
);
530 tf
->lbal
= ioread8(ioaddr
->lbal_addr
);
531 tf
->lbam
= ioread8(ioaddr
->lbam_addr
);
532 tf
->lbah
= ioread8(ioaddr
->lbah_addr
);
533 tf
->device
= ioread8(ioaddr
->device_addr
);
535 if (tf
->flags
& ATA_TFLAG_LBA48
) {
536 if (likely(ioaddr
->ctl_addr
)) {
537 iowrite8(tf
->ctl
| ATA_HOB
, ioaddr
->ctl_addr
);
538 tf
->hob_feature
= ioread8(ioaddr
->error_addr
);
539 tf
->hob_nsect
= ioread8(ioaddr
->nsect_addr
);
540 tf
->hob_lbal
= ioread8(ioaddr
->lbal_addr
);
541 tf
->hob_lbam
= ioread8(ioaddr
->lbam_addr
);
542 tf
->hob_lbah
= ioread8(ioaddr
->lbah_addr
);
543 iowrite8(tf
->ctl
, ioaddr
->ctl_addr
);
544 ap
->last_ctl
= tf
->ctl
;
551 * ata_sff_exec_command - issue ATA command to host controller
552 * @ap: port to which command is being issued
553 * @tf: ATA taskfile register set
555 * Issues ATA command, with proper synchronization with interrupt
556 * handler / other threads.
559 * spin_lock_irqsave(host lock)
561 void ata_sff_exec_command(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
563 DPRINTK("ata%u: cmd 0x%X\n", ap
->print_id
, tf
->command
);
565 iowrite8(tf
->command
, ap
->ioaddr
.command_addr
);
570 * ata_tf_to_host - issue ATA taskfile to host controller
571 * @ap: port to which command is being issued
572 * @tf: ATA taskfile register set
574 * Issues ATA taskfile register set to ATA host controller,
575 * with proper synchronization with interrupt handler and
579 * spin_lock_irqsave(host lock)
581 static inline void ata_tf_to_host(struct ata_port
*ap
,
582 const struct ata_taskfile
*tf
)
584 ap
->ops
->sff_tf_load(ap
, tf
);
585 ap
->ops
->sff_exec_command(ap
, tf
);
589 * ata_sff_data_xfer - Transfer data by PIO
590 * @dev: device to target
592 * @buflen: buffer length
595 * Transfer data from/to the device data register by PIO.
598 * Inherited from caller.
603 unsigned int ata_sff_data_xfer(struct ata_device
*dev
, unsigned char *buf
,
604 unsigned int buflen
, int rw
)
606 struct ata_port
*ap
= dev
->link
->ap
;
607 void __iomem
*data_addr
= ap
->ioaddr
.data_addr
;
608 unsigned int words
= buflen
>> 1;
610 /* Transfer multiple of 2 bytes */
612 ioread16_rep(data_addr
, buf
, words
);
614 iowrite16_rep(data_addr
, buf
, words
);
616 /* Transfer trailing 1 byte, if any. */
617 if (unlikely(buflen
& 0x01)) {
618 __le16 align_buf
[1] = { 0 };
619 unsigned char *trailing_buf
= buf
+ buflen
- 1;
622 align_buf
[0] = cpu_to_le16(ioread16(data_addr
));
623 memcpy(trailing_buf
, align_buf
, 1);
625 memcpy(align_buf
, trailing_buf
, 1);
626 iowrite16(le16_to_cpu(align_buf
[0]), data_addr
);
635 * ata_sff_data_xfer_noirq - Transfer data by PIO
636 * @dev: device to target
638 * @buflen: buffer length
641 * Transfer data from/to the device data register by PIO. Do the
642 * transfer with interrupts disabled.
645 * Inherited from caller.
650 unsigned int ata_sff_data_xfer_noirq(struct ata_device
*dev
, unsigned char *buf
,
651 unsigned int buflen
, int rw
)
654 unsigned int consumed
;
656 local_irq_save(flags
);
657 consumed
= ata_sff_data_xfer(dev
, buf
, buflen
, rw
);
658 local_irq_restore(flags
);
664 * ata_pio_sector - Transfer a sector of data.
665 * @qc: Command on going
667 * Transfer qc->sect_size bytes of data from/to the ATA device.
670 * Inherited from caller.
672 static void ata_pio_sector(struct ata_queued_cmd
*qc
)
674 int do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
675 struct ata_port
*ap
= qc
->ap
;
680 if (qc
->curbytes
== qc
->nbytes
- qc
->sect_size
)
681 ap
->hsm_task_state
= HSM_ST_LAST
;
683 page
= sg_page(qc
->cursg
);
684 offset
= qc
->cursg
->offset
+ qc
->cursg_ofs
;
686 /* get the current page and offset */
687 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
690 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
692 if (PageHighMem(page
)) {
695 /* FIXME: use a bounce buffer */
696 local_irq_save(flags
);
697 buf
= kmap_atomic(page
, KM_IRQ0
);
699 /* do the actual data transfer */
700 ap
->ops
->sff_data_xfer(qc
->dev
, buf
+ offset
, qc
->sect_size
,
703 kunmap_atomic(buf
, KM_IRQ0
);
704 local_irq_restore(flags
);
706 buf
= page_address(page
);
707 ap
->ops
->sff_data_xfer(qc
->dev
, buf
+ offset
, qc
->sect_size
,
711 qc
->curbytes
+= qc
->sect_size
;
712 qc
->cursg_ofs
+= qc
->sect_size
;
714 if (qc
->cursg_ofs
== qc
->cursg
->length
) {
715 qc
->cursg
= sg_next(qc
->cursg
);
721 * ata_pio_sectors - Transfer one or many sectors.
722 * @qc: Command on going
724 * Transfer one or many sectors of data from/to the
725 * ATA device for the DRQ request.
728 * Inherited from caller.
730 static void ata_pio_sectors(struct ata_queued_cmd
*qc
)
732 if (is_multi_taskfile(&qc
->tf
)) {
733 /* READ/WRITE MULTIPLE */
736 WARN_ON(qc
->dev
->multi_count
== 0);
738 nsect
= min((qc
->nbytes
- qc
->curbytes
) / qc
->sect_size
,
739 qc
->dev
->multi_count
);
745 ata_sff_altstatus(qc
->ap
); /* flush */
749 * atapi_send_cdb - Write CDB bytes to hardware
750 * @ap: Port to which ATAPI device is attached.
751 * @qc: Taskfile currently active
753 * When device has indicated its readiness to accept
754 * a CDB, this function is called. Send the CDB.
759 static void atapi_send_cdb(struct ata_port
*ap
, struct ata_queued_cmd
*qc
)
762 DPRINTK("send cdb\n");
763 WARN_ON(qc
->dev
->cdb_len
< 12);
765 ap
->ops
->sff_data_xfer(qc
->dev
, qc
->cdb
, qc
->dev
->cdb_len
, 1);
766 ata_sff_altstatus(ap
); /* flush */
768 switch (qc
->tf
.protocol
) {
770 ap
->hsm_task_state
= HSM_ST
;
772 case ATAPI_PROT_NODATA
:
773 ap
->hsm_task_state
= HSM_ST_LAST
;
776 ap
->hsm_task_state
= HSM_ST_LAST
;
778 ap
->ops
->bmdma_start(qc
);
784 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
785 * @qc: Command on going
786 * @bytes: number of bytes
788 * Transfer Transfer data from/to the ATAPI device.
791 * Inherited from caller.
794 static int __atapi_pio_bytes(struct ata_queued_cmd
*qc
, unsigned int bytes
)
796 int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
) ? WRITE
: READ
;
797 struct ata_port
*ap
= qc
->ap
;
798 struct ata_device
*dev
= qc
->dev
;
799 struct ata_eh_info
*ehi
= &dev
->link
->eh_info
;
800 struct scatterlist
*sg
;
803 unsigned int offset
, count
, consumed
;
808 ata_ehi_push_desc(ehi
, "unexpected or too much trailing data "
809 "buf=%u cur=%u bytes=%u",
810 qc
->nbytes
, qc
->curbytes
, bytes
);
815 offset
= sg
->offset
+ qc
->cursg_ofs
;
817 /* get the current page and offset */
818 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
821 /* don't overrun current sg */
822 count
= min(sg
->length
- qc
->cursg_ofs
, bytes
);
824 /* don't cross page boundaries */
825 count
= min(count
, (unsigned int)PAGE_SIZE
- offset
);
827 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
829 if (PageHighMem(page
)) {
832 /* FIXME: use bounce buffer */
833 local_irq_save(flags
);
834 buf
= kmap_atomic(page
, KM_IRQ0
);
836 /* do the actual data transfer */
837 consumed
= ap
->ops
->sff_data_xfer(dev
, buf
+ offset
, count
, rw
);
839 kunmap_atomic(buf
, KM_IRQ0
);
840 local_irq_restore(flags
);
842 buf
= page_address(page
);
843 consumed
= ap
->ops
->sff_data_xfer(dev
, buf
+ offset
, count
, rw
);
846 bytes
-= min(bytes
, consumed
);
847 qc
->curbytes
+= count
;
848 qc
->cursg_ofs
+= count
;
850 if (qc
->cursg_ofs
== sg
->length
) {
851 qc
->cursg
= sg_next(qc
->cursg
);
855 /* consumed can be larger than count only for the last transfer */
856 WARN_ON(qc
->cursg
&& count
!= consumed
);
864 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
865 * @qc: Command on going
867 * Transfer Transfer data from/to the ATAPI device.
870 * Inherited from caller.
872 static void atapi_pio_bytes(struct ata_queued_cmd
*qc
)
874 struct ata_port
*ap
= qc
->ap
;
875 struct ata_device
*dev
= qc
->dev
;
876 struct ata_eh_info
*ehi
= &dev
->link
->eh_info
;
877 unsigned int ireason
, bc_lo
, bc_hi
, bytes
;
878 int i_write
, do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
) ? 1 : 0;
880 /* Abuse qc->result_tf for temp storage of intermediate TF
881 * here to save some kernel stack usage.
882 * For normal completion, qc->result_tf is not relevant. For
883 * error, qc->result_tf is later overwritten by ata_qc_complete().
884 * So, the correctness of qc->result_tf is not affected.
886 ap
->ops
->sff_tf_read(ap
, &qc
->result_tf
);
887 ireason
= qc
->result_tf
.nsect
;
888 bc_lo
= qc
->result_tf
.lbam
;
889 bc_hi
= qc
->result_tf
.lbah
;
890 bytes
= (bc_hi
<< 8) | bc_lo
;
892 /* shall be cleared to zero, indicating xfer of data */
893 if (unlikely(ireason
& (1 << 0)))
896 /* make sure transfer direction matches expected */
897 i_write
= ((ireason
& (1 << 1)) == 0) ? 1 : 0;
898 if (unlikely(do_write
!= i_write
))
901 if (unlikely(!bytes
))
904 VPRINTK("ata%u: xfering %d bytes\n", ap
->print_id
, bytes
);
906 if (unlikely(__atapi_pio_bytes(qc
, bytes
)))
908 ata_sff_altstatus(ap
); /* flush */
913 ata_ehi_push_desc(ehi
, "ATAPI check failed (ireason=0x%x bytes=%u)",
916 qc
->err_mask
|= AC_ERR_HSM
;
917 ap
->hsm_task_state
= HSM_ST_ERR
;
921 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
922 * @ap: the target ata_port
926 * 1 if ok in workqueue, 0 otherwise.
928 static inline int ata_hsm_ok_in_wq(struct ata_port
*ap
, struct ata_queued_cmd
*qc
)
930 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
933 if (ap
->hsm_task_state
== HSM_ST_FIRST
) {
934 if (qc
->tf
.protocol
== ATA_PROT_PIO
&&
935 (qc
->tf
.flags
& ATA_TFLAG_WRITE
))
938 if (ata_is_atapi(qc
->tf
.protocol
) &&
939 !(qc
->dev
->flags
& ATA_DFLAG_CDB_INTR
))
947 * ata_hsm_qc_complete - finish a qc running on standard HSM
948 * @qc: Command to complete
949 * @in_wq: 1 if called from workqueue, 0 otherwise
951 * Finish @qc which is running on standard HSM.
954 * If @in_wq is zero, spin_lock_irqsave(host lock).
955 * Otherwise, none on entry and grabs host lock.
957 static void ata_hsm_qc_complete(struct ata_queued_cmd
*qc
, int in_wq
)
959 struct ata_port
*ap
= qc
->ap
;
962 if (ap
->ops
->error_handler
) {
964 spin_lock_irqsave(ap
->lock
, flags
);
966 /* EH might have kicked in while host lock is
969 qc
= ata_qc_from_tag(ap
, qc
->tag
);
971 if (likely(!(qc
->err_mask
& AC_ERR_HSM
))) {
972 ap
->ops
->sff_irq_on(ap
);
978 spin_unlock_irqrestore(ap
->lock
, flags
);
980 if (likely(!(qc
->err_mask
& AC_ERR_HSM
)))
987 spin_lock_irqsave(ap
->lock
, flags
);
988 ap
->ops
->sff_irq_on(ap
);
990 spin_unlock_irqrestore(ap
->lock
, flags
);
997 * ata_sff_hsm_move - move the HSM to the next state.
998 * @ap: the target ata_port
1000 * @status: current device status
1001 * @in_wq: 1 if called from workqueue, 0 otherwise
1004 * 1 when poll next status needed, 0 otherwise.
1006 int ata_sff_hsm_move(struct ata_port
*ap
, struct ata_queued_cmd
*qc
,
1007 u8 status
, int in_wq
)
1009 unsigned long flags
= 0;
1012 WARN_ON((qc
->flags
& ATA_QCFLAG_ACTIVE
) == 0);
1014 /* Make sure ata_sff_qc_issue() does not throw things
1015 * like DMA polling into the workqueue. Notice that
1016 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
1018 WARN_ON(in_wq
!= ata_hsm_ok_in_wq(ap
, qc
));
1021 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
1022 ap
->print_id
, qc
->tf
.protocol
, ap
->hsm_task_state
, status
);
1024 switch (ap
->hsm_task_state
) {
1026 /* Send first data block or PACKET CDB */
1028 /* If polling, we will stay in the work queue after
1029 * sending the data. Otherwise, interrupt handler
1030 * takes over after sending the data.
1032 poll_next
= (qc
->tf
.flags
& ATA_TFLAG_POLLING
);
1034 /* check device status */
1035 if (unlikely((status
& ATA_DRQ
) == 0)) {
1036 /* handle BSY=0, DRQ=0 as error */
1037 if (likely(status
& (ATA_ERR
| ATA_DF
)))
1038 /* device stops HSM for abort/error */
1039 qc
->err_mask
|= AC_ERR_DEV
;
1041 /* HSM violation. Let EH handle this */
1042 qc
->err_mask
|= AC_ERR_HSM
;
1044 ap
->hsm_task_state
= HSM_ST_ERR
;
1048 /* Device should not ask for data transfer (DRQ=1)
1049 * when it finds something wrong.
1050 * We ignore DRQ here and stop the HSM by
1051 * changing hsm_task_state to HSM_ST_ERR and
1052 * let the EH abort the command or reset the device.
1054 if (unlikely(status
& (ATA_ERR
| ATA_DF
))) {
1055 /* Some ATAPI tape drives forget to clear the ERR bit
1056 * when doing the next command (mostly request sense).
1057 * We ignore ERR here to workaround and proceed sending
1060 if (!(qc
->dev
->horkage
& ATA_HORKAGE_STUCK_ERR
)) {
1061 ata_port_printk(ap
, KERN_WARNING
,
1062 "DRQ=1 with device error, "
1063 "dev_stat 0x%X\n", status
);
1064 qc
->err_mask
|= AC_ERR_HSM
;
1065 ap
->hsm_task_state
= HSM_ST_ERR
;
1070 /* Send the CDB (atapi) or the first data block (ata pio out).
1071 * During the state transition, interrupt handler shouldn't
1072 * be invoked before the data transfer is complete and
1073 * hsm_task_state is changed. Hence, the following locking.
1076 spin_lock_irqsave(ap
->lock
, flags
);
1078 if (qc
->tf
.protocol
== ATA_PROT_PIO
) {
1079 /* PIO data out protocol.
1080 * send first data block.
1083 /* ata_pio_sectors() might change the state
1084 * to HSM_ST_LAST. so, the state is changed here
1085 * before ata_pio_sectors().
1087 ap
->hsm_task_state
= HSM_ST
;
1088 ata_pio_sectors(qc
);
1091 atapi_send_cdb(ap
, qc
);
1094 spin_unlock_irqrestore(ap
->lock
, flags
);
1096 /* if polling, ata_pio_task() handles the rest.
1097 * otherwise, interrupt handler takes over from here.
1102 /* complete command or read/write the data register */
1103 if (qc
->tf
.protocol
== ATAPI_PROT_PIO
) {
1104 /* ATAPI PIO protocol */
1105 if ((status
& ATA_DRQ
) == 0) {
1106 /* No more data to transfer or device error.
1107 * Device error will be tagged in HSM_ST_LAST.
1109 ap
->hsm_task_state
= HSM_ST_LAST
;
1113 /* Device should not ask for data transfer (DRQ=1)
1114 * when it finds something wrong.
1115 * We ignore DRQ here and stop the HSM by
1116 * changing hsm_task_state to HSM_ST_ERR and
1117 * let the EH abort the command or reset the device.
1119 if (unlikely(status
& (ATA_ERR
| ATA_DF
))) {
1120 ata_port_printk(ap
, KERN_WARNING
, "DRQ=1 with "
1121 "device error, dev_stat 0x%X\n",
1123 qc
->err_mask
|= AC_ERR_HSM
;
1124 ap
->hsm_task_state
= HSM_ST_ERR
;
1128 atapi_pio_bytes(qc
);
1130 if (unlikely(ap
->hsm_task_state
== HSM_ST_ERR
))
1131 /* bad ireason reported by device */
1135 /* ATA PIO protocol */
1136 if (unlikely((status
& ATA_DRQ
) == 0)) {
1137 /* handle BSY=0, DRQ=0 as error */
1138 if (likely(status
& (ATA_ERR
| ATA_DF
)))
1139 /* device stops HSM for abort/error */
1140 qc
->err_mask
|= AC_ERR_DEV
;
1142 /* HSM violation. Let EH handle this.
1143 * Phantom devices also trigger this
1144 * condition. Mark hint.
1146 qc
->err_mask
|= AC_ERR_HSM
|
1149 ap
->hsm_task_state
= HSM_ST_ERR
;
1153 /* For PIO reads, some devices may ask for
1154 * data transfer (DRQ=1) alone with ERR=1.
1155 * We respect DRQ here and transfer one
1156 * block of junk data before changing the
1157 * hsm_task_state to HSM_ST_ERR.
1159 * For PIO writes, ERR=1 DRQ=1 doesn't make
1160 * sense since the data block has been
1161 * transferred to the device.
1163 if (unlikely(status
& (ATA_ERR
| ATA_DF
))) {
1164 /* data might be corrputed */
1165 qc
->err_mask
|= AC_ERR_DEV
;
1167 if (!(qc
->tf
.flags
& ATA_TFLAG_WRITE
)) {
1168 ata_pio_sectors(qc
);
1169 status
= ata_wait_idle(ap
);
1172 if (status
& (ATA_BUSY
| ATA_DRQ
))
1173 qc
->err_mask
|= AC_ERR_HSM
;
1175 /* ata_pio_sectors() might change the
1176 * state to HSM_ST_LAST. so, the state
1177 * is changed after ata_pio_sectors().
1179 ap
->hsm_task_state
= HSM_ST_ERR
;
1183 ata_pio_sectors(qc
);
1185 if (ap
->hsm_task_state
== HSM_ST_LAST
&&
1186 (!(qc
->tf
.flags
& ATA_TFLAG_WRITE
))) {
1188 status
= ata_wait_idle(ap
);
1197 if (unlikely(!ata_ok(status
))) {
1198 qc
->err_mask
|= __ac_err_mask(status
);
1199 ap
->hsm_task_state
= HSM_ST_ERR
;
1203 /* no more data to transfer */
1204 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
1205 ap
->print_id
, qc
->dev
->devno
, status
);
1207 WARN_ON(qc
->err_mask
& (AC_ERR_DEV
| AC_ERR_HSM
));
1209 ap
->hsm_task_state
= HSM_ST_IDLE
;
1211 /* complete taskfile transaction */
1212 ata_hsm_qc_complete(qc
, in_wq
);
1218 /* make sure qc->err_mask is available to
1219 * know what's wrong and recover
1221 WARN_ON(!(qc
->err_mask
& (AC_ERR_DEV
| AC_ERR_HSM
)));
1223 ap
->hsm_task_state
= HSM_ST_IDLE
;
1225 /* complete taskfile transaction */
1226 ata_hsm_qc_complete(qc
, in_wq
);
1238 void ata_pio_task(struct work_struct
*work
)
1240 struct ata_port
*ap
=
1241 container_of(work
, struct ata_port
, port_task
.work
);
1242 struct ata_queued_cmd
*qc
= ap
->port_task_data
;
1247 WARN_ON(ap
->hsm_task_state
== HSM_ST_IDLE
);
1250 * This is purely heuristic. This is a fast path.
1251 * Sometimes when we enter, BSY will be cleared in
1252 * a chk-status or two. If not, the drive is probably seeking
1253 * or something. Snooze for a couple msecs, then
1254 * chk-status again. If still busy, queue delayed work.
1256 status
= ata_sff_busy_wait(ap
, ATA_BUSY
, 5);
1257 if (status
& ATA_BUSY
) {
1259 status
= ata_sff_busy_wait(ap
, ATA_BUSY
, 10);
1260 if (status
& ATA_BUSY
) {
1261 ata_pio_queue_task(ap
, qc
, ATA_SHORT_PAUSE
);
1267 poll_next
= ata_sff_hsm_move(ap
, qc
, status
, 1);
1269 /* another command or interrupt handler
1270 * may be running at this point.
1277 * ata_sff_qc_issue - issue taskfile to device in proto-dependent manner
1278 * @qc: command to issue to device
1280 * Using various libata functions and hooks, this function
1281 * starts an ATA command. ATA commands are grouped into
1282 * classes called "protocols", and issuing each type of protocol
1283 * is slightly different.
1285 * May be used as the qc_issue() entry in ata_port_operations.
1288 * spin_lock_irqsave(host lock)
1291 * Zero on success, AC_ERR_* mask on failure
1293 unsigned int ata_sff_qc_issue(struct ata_queued_cmd
*qc
)
1295 struct ata_port
*ap
= qc
->ap
;
1297 /* Use polling pio if the LLD doesn't handle
1298 * interrupt driven pio and atapi CDB interrupt.
1300 if (ap
->flags
& ATA_FLAG_PIO_POLLING
) {
1301 switch (qc
->tf
.protocol
) {
1303 case ATA_PROT_NODATA
:
1304 case ATAPI_PROT_PIO
:
1305 case ATAPI_PROT_NODATA
:
1306 qc
->tf
.flags
|= ATA_TFLAG_POLLING
;
1308 case ATAPI_PROT_DMA
:
1309 if (qc
->dev
->flags
& ATA_DFLAG_CDB_INTR
)
1310 /* see ata_dma_blacklisted() */
1318 /* select the device */
1319 ata_dev_select(ap
, qc
->dev
->devno
, 1, 0);
1321 /* start the command */
1322 switch (qc
->tf
.protocol
) {
1323 case ATA_PROT_NODATA
:
1324 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1325 ata_qc_set_polling(qc
);
1327 ata_tf_to_host(ap
, &qc
->tf
);
1328 ap
->hsm_task_state
= HSM_ST_LAST
;
1330 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1331 ata_pio_queue_task(ap
, qc
, 0);
1336 WARN_ON(qc
->tf
.flags
& ATA_TFLAG_POLLING
);
1338 ap
->ops
->sff_tf_load(ap
, &qc
->tf
); /* load tf registers */
1339 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
1340 ap
->ops
->bmdma_start(qc
); /* initiate bmdma */
1341 ap
->hsm_task_state
= HSM_ST_LAST
;
1345 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1346 ata_qc_set_polling(qc
);
1348 ata_tf_to_host(ap
, &qc
->tf
);
1350 if (qc
->tf
.flags
& ATA_TFLAG_WRITE
) {
1351 /* PIO data out protocol */
1352 ap
->hsm_task_state
= HSM_ST_FIRST
;
1353 ata_pio_queue_task(ap
, qc
, 0);
1355 /* always send first data block using
1356 * the ata_pio_task() codepath.
1359 /* PIO data in protocol */
1360 ap
->hsm_task_state
= HSM_ST
;
1362 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1363 ata_pio_queue_task(ap
, qc
, 0);
1365 /* if polling, ata_pio_task() handles the rest.
1366 * otherwise, interrupt handler takes over from here.
1372 case ATAPI_PROT_PIO
:
1373 case ATAPI_PROT_NODATA
:
1374 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1375 ata_qc_set_polling(qc
);
1377 ata_tf_to_host(ap
, &qc
->tf
);
1379 ap
->hsm_task_state
= HSM_ST_FIRST
;
1381 /* send cdb by polling if no cdb interrupt */
1382 if ((!(qc
->dev
->flags
& ATA_DFLAG_CDB_INTR
)) ||
1383 (qc
->tf
.flags
& ATA_TFLAG_POLLING
))
1384 ata_pio_queue_task(ap
, qc
, 0);
1387 case ATAPI_PROT_DMA
:
1388 WARN_ON(qc
->tf
.flags
& ATA_TFLAG_POLLING
);
1390 ap
->ops
->sff_tf_load(ap
, &qc
->tf
); /* load tf registers */
1391 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
1392 ap
->hsm_task_state
= HSM_ST_FIRST
;
1394 /* send cdb by polling if no cdb interrupt */
1395 if (!(qc
->dev
->flags
& ATA_DFLAG_CDB_INTR
))
1396 ata_pio_queue_task(ap
, qc
, 0);
1401 return AC_ERR_SYSTEM
;
1408 * ata_sff_qc_fill_rtf - fill result TF using ->sff_tf_read
1409 * @qc: qc to fill result TF for
1411 * @qc is finished and result TF needs to be filled. Fill it
1412 * using ->sff_tf_read.
1415 * spin_lock_irqsave(host lock)
1418 * true indicating that result TF is successfully filled.
1420 bool ata_sff_qc_fill_rtf(struct ata_queued_cmd
*qc
)
1422 qc
->ap
->ops
->sff_tf_read(qc
->ap
, &qc
->result_tf
);
1427 * ata_sff_host_intr - Handle host interrupt for given (port, task)
1428 * @ap: Port on which interrupt arrived (possibly...)
1429 * @qc: Taskfile currently active in engine
1431 * Handle host interrupt for given queued command. Currently,
1432 * only DMA interrupts are handled. All other commands are
1433 * handled via polling with interrupts disabled (nIEN bit).
1436 * spin_lock_irqsave(host lock)
1439 * One if interrupt was handled, zero if not (shared irq).
1441 inline unsigned int ata_sff_host_intr(struct ata_port
*ap
,
1442 struct ata_queued_cmd
*qc
)
1444 struct ata_eh_info
*ehi
= &ap
->link
.eh_info
;
1445 u8 status
, host_stat
= 0;
1447 VPRINTK("ata%u: protocol %d task_state %d\n",
1448 ap
->print_id
, qc
->tf
.protocol
, ap
->hsm_task_state
);
1450 /* Check whether we are expecting interrupt in this state */
1451 switch (ap
->hsm_task_state
) {
1453 /* Some pre-ATAPI-4 devices assert INTRQ
1454 * at this state when ready to receive CDB.
1457 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
1458 * The flag was turned on only for atapi devices. No
1459 * need to check ata_is_atapi(qc->tf.protocol) again.
1461 if (!(qc
->dev
->flags
& ATA_DFLAG_CDB_INTR
))
1465 if (qc
->tf
.protocol
== ATA_PROT_DMA
||
1466 qc
->tf
.protocol
== ATAPI_PROT_DMA
) {
1467 /* check status of DMA engine */
1468 host_stat
= ap
->ops
->bmdma_status(ap
);
1469 VPRINTK("ata%u: host_stat 0x%X\n",
1470 ap
->print_id
, host_stat
);
1472 /* if it's not our irq... */
1473 if (!(host_stat
& ATA_DMA_INTR
))
1476 /* before we do anything else, clear DMA-Start bit */
1477 ap
->ops
->bmdma_stop(qc
);
1479 if (unlikely(host_stat
& ATA_DMA_ERR
)) {
1480 /* error when transfering data to/from memory */
1481 qc
->err_mask
|= AC_ERR_HOST_BUS
;
1482 ap
->hsm_task_state
= HSM_ST_ERR
;
1492 /* check altstatus */
1493 status
= ata_sff_altstatus(ap
);
1494 if (status
& ATA_BUSY
)
1497 /* check main status, clearing INTRQ */
1498 status
= ap
->ops
->sff_check_status(ap
);
1499 if (unlikely(status
& ATA_BUSY
))
1502 /* ack bmdma irq events */
1503 ap
->ops
->sff_irq_clear(ap
);
1505 ata_sff_hsm_move(ap
, qc
, status
, 0);
1507 if (unlikely(qc
->err_mask
) && (qc
->tf
.protocol
== ATA_PROT_DMA
||
1508 qc
->tf
.protocol
== ATAPI_PROT_DMA
))
1509 ata_ehi_push_desc(ehi
, "BMDMA stat 0x%x", host_stat
);
1511 return 1; /* irq handled */
1514 ap
->stats
.idle_irq
++;
1517 if ((ap
->stats
.idle_irq
% 1000) == 0) {
1518 ap
->ops
->sff_check_status(ap
);
1519 ap
->ops
->sff_irq_clear(ap
);
1520 ata_port_printk(ap
, KERN_WARNING
, "irq trap\n");
1524 return 0; /* irq not handled */
1528 * ata_sff_interrupt - Default ATA host interrupt handler
1529 * @irq: irq line (unused)
1530 * @dev_instance: pointer to our ata_host information structure
1532 * Default interrupt handler for PCI IDE devices. Calls
1533 * ata_sff_host_intr() for each port that is not disabled.
1536 * Obtains host lock during operation.
1539 * IRQ_NONE or IRQ_HANDLED.
1541 irqreturn_t
ata_sff_interrupt(int irq
, void *dev_instance
)
1543 struct ata_host
*host
= dev_instance
;
1545 unsigned int handled
= 0;
1546 unsigned long flags
;
1548 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
1549 spin_lock_irqsave(&host
->lock
, flags
);
1551 for (i
= 0; i
< host
->n_ports
; i
++) {
1552 struct ata_port
*ap
;
1554 ap
= host
->ports
[i
];
1556 !(ap
->flags
& ATA_FLAG_DISABLED
)) {
1557 struct ata_queued_cmd
*qc
;
1559 qc
= ata_qc_from_tag(ap
, ap
->link
.active_tag
);
1560 if (qc
&& (!(qc
->tf
.flags
& ATA_TFLAG_POLLING
)) &&
1561 (qc
->flags
& ATA_QCFLAG_ACTIVE
))
1562 handled
|= ata_sff_host_intr(ap
, qc
);
1566 spin_unlock_irqrestore(&host
->lock
, flags
);
1568 return IRQ_RETVAL(handled
);
1572 * ata_sff_freeze - Freeze SFF controller port
1573 * @ap: port to freeze
1575 * Freeze BMDMA controller port.
1578 * Inherited from caller.
1580 void ata_sff_freeze(struct ata_port
*ap
)
1582 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1584 ap
->ctl
|= ATA_NIEN
;
1585 ap
->last_ctl
= ap
->ctl
;
1587 if (ioaddr
->ctl_addr
)
1588 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
1590 /* Under certain circumstances, some controllers raise IRQ on
1591 * ATA_NIEN manipulation. Also, many controllers fail to mask
1592 * previously pending IRQ on ATA_NIEN assertion. Clear it.
1594 ap
->ops
->sff_check_status(ap
);
1596 ap
->ops
->sff_irq_clear(ap
);
1600 * ata_sff_thaw - Thaw SFF controller port
1603 * Thaw SFF controller port.
1606 * Inherited from caller.
1608 void ata_sff_thaw(struct ata_port
*ap
)
1610 /* clear & re-enable interrupts */
1611 ap
->ops
->sff_check_status(ap
);
1612 ap
->ops
->sff_irq_clear(ap
);
1613 ap
->ops
->sff_irq_on(ap
);
1617 * ata_sff_prereset - prepare SFF link for reset
1618 * @link: SFF link to be reset
1619 * @deadline: deadline jiffies for the operation
1621 * SFF link @link is about to be reset. Initialize it. It first
1622 * calls ata_std_prereset() and wait for !BSY if the port is
1626 * Kernel thread context (may sleep)
1629 * 0 on success, -errno otherwise.
1631 int ata_sff_prereset(struct ata_link
*link
, unsigned long deadline
)
1633 struct ata_eh_context
*ehc
= &link
->eh_context
;
1636 rc
= ata_std_prereset(link
, deadline
);
1640 /* if we're about to do hardreset, nothing more to do */
1641 if (ehc
->i
.action
& ATA_EH_HARDRESET
)
1644 /* wait for !BSY if we don't know that no device is attached */
1645 if (!ata_link_offline(link
)) {
1646 rc
= ata_sff_wait_ready(link
, deadline
);
1647 if (rc
&& rc
!= -ENODEV
) {
1648 ata_link_printk(link
, KERN_WARNING
, "device not ready "
1649 "(errno=%d), forcing hardreset\n", rc
);
1650 ehc
->i
.action
|= ATA_EH_HARDRESET
;
1658 * ata_devchk - PATA device presence detection
1659 * @ap: ATA channel to examine
1660 * @device: Device to examine (starting at zero)
1662 * This technique was originally described in
1663 * Hale Landis's ATADRVR (www.ata-atapi.com), and
1664 * later found its way into the ATA/ATAPI spec.
1666 * Write a pattern to the ATA shadow registers,
1667 * and if a device is present, it will respond by
1668 * correctly storing and echoing back the
1669 * ATA shadow register contents.
1674 static unsigned int ata_devchk(struct ata_port
*ap
, unsigned int device
)
1676 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1679 ap
->ops
->sff_dev_select(ap
, device
);
1681 iowrite8(0x55, ioaddr
->nsect_addr
);
1682 iowrite8(0xaa, ioaddr
->lbal_addr
);
1684 iowrite8(0xaa, ioaddr
->nsect_addr
);
1685 iowrite8(0x55, ioaddr
->lbal_addr
);
1687 iowrite8(0x55, ioaddr
->nsect_addr
);
1688 iowrite8(0xaa, ioaddr
->lbal_addr
);
1690 nsect
= ioread8(ioaddr
->nsect_addr
);
1691 lbal
= ioread8(ioaddr
->lbal_addr
);
1693 if ((nsect
== 0x55) && (lbal
== 0xaa))
1694 return 1; /* we found a device */
1696 return 0; /* nothing found */
1700 * ata_sff_dev_classify - Parse returned ATA device signature
1701 * @dev: ATA device to classify (starting at zero)
1702 * @present: device seems present
1703 * @r_err: Value of error register on completion
1705 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
1706 * an ATA/ATAPI-defined set of values is placed in the ATA
1707 * shadow registers, indicating the results of device detection
1710 * Select the ATA device, and read the values from the ATA shadow
1711 * registers. Then parse according to the Error register value,
1712 * and the spec-defined values examined by ata_dev_classify().
1718 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
1720 unsigned int ata_sff_dev_classify(struct ata_device
*dev
, int present
,
1723 struct ata_port
*ap
= dev
->link
->ap
;
1724 struct ata_taskfile tf
;
1728 ap
->ops
->sff_dev_select(ap
, dev
->devno
);
1730 memset(&tf
, 0, sizeof(tf
));
1732 ap
->ops
->sff_tf_read(ap
, &tf
);
1737 /* see if device passed diags: continue and warn later */
1739 /* diagnostic fail : do nothing _YET_ */
1740 dev
->horkage
|= ATA_HORKAGE_DIAGNOSTIC
;
1743 else if ((dev
->devno
== 0) && (err
== 0x81))
1746 return ATA_DEV_NONE
;
1748 /* determine if device is ATA or ATAPI */
1749 class = ata_dev_classify(&tf
);
1751 if (class == ATA_DEV_UNKNOWN
) {
1752 /* If the device failed diagnostic, it's likely to
1753 * have reported incorrect device signature too.
1754 * Assume ATA device if the device seems present but
1755 * device signature is invalid with diagnostic
1758 if (present
&& (dev
->horkage
& ATA_HORKAGE_DIAGNOSTIC
))
1759 class = ATA_DEV_ATA
;
1761 class = ATA_DEV_NONE
;
1762 } else if ((class == ATA_DEV_ATA
) &&
1763 (ap
->ops
->sff_check_status(ap
) == 0))
1764 class = ATA_DEV_NONE
;
1770 * ata_sff_wait_after_reset - wait for devices to become ready after reset
1771 * @link: SFF link which is just reset
1772 * @devmask: mask of present devices
1773 * @deadline: deadline jiffies for the operation
1775 * Wait devices attached to SFF @link to become ready after
1776 * reset. It contains preceding 150ms wait to avoid accessing TF
1777 * status register too early.
1780 * Kernel thread context (may sleep).
1783 * 0 on success, -ENODEV if some or all of devices in @devmask
1784 * don't seem to exist. -errno on other errors.
1786 int ata_sff_wait_after_reset(struct ata_link
*link
, unsigned int devmask
,
1787 unsigned long deadline
)
1789 struct ata_port
*ap
= link
->ap
;
1790 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1791 unsigned int dev0
= devmask
& (1 << 0);
1792 unsigned int dev1
= devmask
& (1 << 1);
1795 msleep(ATA_WAIT_AFTER_RESET_MSECS
);
1797 /* always check readiness of the master device */
1798 rc
= ata_sff_wait_ready(link
, deadline
);
1799 /* -ENODEV means the odd clown forgot the D7 pulldown resistor
1800 * and TF status is 0xff, bail out on it too.
1805 /* if device 1 was found in ata_devchk, wait for register
1806 * access briefly, then wait for BSY to clear.
1811 ap
->ops
->sff_dev_select(ap
, 1);
1813 /* Wait for register access. Some ATAPI devices fail
1814 * to set nsect/lbal after reset, so don't waste too
1815 * much time on it. We're gonna wait for !BSY anyway.
1817 for (i
= 0; i
< 2; i
++) {
1820 nsect
= ioread8(ioaddr
->nsect_addr
);
1821 lbal
= ioread8(ioaddr
->lbal_addr
);
1822 if ((nsect
== 1) && (lbal
== 1))
1824 msleep(50); /* give drive a breather */
1827 rc
= ata_sff_wait_ready(link
, deadline
);
1835 /* is all this really necessary? */
1836 ap
->ops
->sff_dev_select(ap
, 0);
1838 ap
->ops
->sff_dev_select(ap
, 1);
1840 ap
->ops
->sff_dev_select(ap
, 0);
1845 static int ata_bus_softreset(struct ata_port
*ap
, unsigned int devmask
,
1846 unsigned long deadline
)
1848 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1850 DPRINTK("ata%u: bus reset via SRST\n", ap
->print_id
);
1852 /* software reset. causes dev0 to be selected */
1853 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
1854 udelay(20); /* FIXME: flush */
1855 iowrite8(ap
->ctl
| ATA_SRST
, ioaddr
->ctl_addr
);
1856 udelay(20); /* FIXME: flush */
1857 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
1859 /* wait the port to become ready */
1860 return ata_sff_wait_after_reset(&ap
->link
, devmask
, deadline
);
1864 * ata_sff_softreset - reset host port via ATA SRST
1865 * @link: ATA link to reset
1866 * @classes: resulting classes of attached devices
1867 * @deadline: deadline jiffies for the operation
1869 * Reset host port using ATA SRST.
1872 * Kernel thread context (may sleep)
1875 * 0 on success, -errno otherwise.
1877 int ata_sff_softreset(struct ata_link
*link
, unsigned int *classes
,
1878 unsigned long deadline
)
1880 struct ata_port
*ap
= link
->ap
;
1881 unsigned int slave_possible
= ap
->flags
& ATA_FLAG_SLAVE_POSS
;
1882 unsigned int devmask
= 0;
1888 /* determine if device 0/1 are present */
1889 if (ata_devchk(ap
, 0))
1890 devmask
|= (1 << 0);
1891 if (slave_possible
&& ata_devchk(ap
, 1))
1892 devmask
|= (1 << 1);
1894 /* select device 0 again */
1895 ap
->ops
->sff_dev_select(ap
, 0);
1897 /* issue bus reset */
1898 DPRINTK("about to softreset, devmask=%x\n", devmask
);
1899 rc
= ata_bus_softreset(ap
, devmask
, deadline
);
1900 /* if link is occupied, -ENODEV too is an error */
1901 if (rc
&& (rc
!= -ENODEV
|| sata_scr_valid(link
))) {
1902 ata_link_printk(link
, KERN_ERR
, "SRST failed (errno=%d)\n", rc
);
1906 /* determine by signature whether we have ATA or ATAPI devices */
1907 classes
[0] = ata_sff_dev_classify(&link
->device
[0],
1908 devmask
& (1 << 0), &err
);
1909 if (slave_possible
&& err
!= 0x81)
1910 classes
[1] = ata_sff_dev_classify(&link
->device
[1],
1911 devmask
& (1 << 1), &err
);
1913 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes
[0], classes
[1]);
1918 * sata_sff_hardreset - reset host port via SATA phy reset
1919 * @link: link to reset
1920 * @class: resulting class of attached device
1921 * @deadline: deadline jiffies for the operation
1923 * SATA phy-reset host port using DET bits of SControl register,
1924 * wait for !BSY and classify the attached device.
1927 * Kernel thread context (may sleep)
1930 * 0 on success, -errno otherwise.
1932 int sata_sff_hardreset(struct ata_link
*link
, unsigned int *class,
1933 unsigned long deadline
)
1935 struct ata_eh_context
*ehc
= &link
->eh_context
;
1936 const unsigned long *timing
= sata_ehc_deb_timing(ehc
);
1940 rc
= sata_link_hardreset(link
, timing
, deadline
, &online
,
1941 ata_sff_check_ready
);
1943 *class = ata_sff_dev_classify(link
->device
, 1, NULL
);
1945 DPRINTK("EXIT, class=%u\n", *class);
1950 * ata_sff_postreset - SFF postreset callback
1951 * @link: the target SFF ata_link
1952 * @classes: classes of attached devices
1954 * This function is invoked after a successful reset. It first
1955 * calls ata_std_postreset() and performs SFF specific postreset
1959 * Kernel thread context (may sleep)
1961 void ata_sff_postreset(struct ata_link
*link
, unsigned int *classes
)
1963 struct ata_port
*ap
= link
->ap
;
1965 ata_std_postreset(link
, classes
);
1967 /* is double-select really necessary? */
1968 if (classes
[0] != ATA_DEV_NONE
)
1969 ap
->ops
->sff_dev_select(ap
, 1);
1970 if (classes
[1] != ATA_DEV_NONE
)
1971 ap
->ops
->sff_dev_select(ap
, 0);
1973 /* bail out if no device is present */
1974 if (classes
[0] == ATA_DEV_NONE
&& classes
[1] == ATA_DEV_NONE
) {
1975 DPRINTK("EXIT, no device\n");
1979 /* set up device control */
1980 if (ap
->ioaddr
.ctl_addr
)
1981 iowrite8(ap
->ctl
, ap
->ioaddr
.ctl_addr
);
1985 * ata_sff_error_handler - Stock error handler for BMDMA controller
1986 * @ap: port to handle error for
1988 * Stock error handler for SFF controller. It can handle both
1989 * PATA and SATA controllers. Many controllers should be able to
1990 * use this EH as-is or with some added handling before and
1994 * Kernel thread context (may sleep)
1996 void ata_sff_error_handler(struct ata_port
*ap
)
1998 ata_reset_fn_t softreset
= ap
->ops
->softreset
;
1999 ata_reset_fn_t hardreset
= ap
->ops
->hardreset
;
2000 struct ata_queued_cmd
*qc
;
2001 unsigned long flags
;
2004 qc
= __ata_qc_from_tag(ap
, ap
->link
.active_tag
);
2005 if (qc
&& !(qc
->flags
& ATA_QCFLAG_FAILED
))
2008 /* reset PIO HSM and stop DMA engine */
2009 spin_lock_irqsave(ap
->lock
, flags
);
2011 ap
->hsm_task_state
= HSM_ST_IDLE
;
2013 if (ap
->ioaddr
.bmdma_addr
&&
2014 qc
&& (qc
->tf
.protocol
== ATA_PROT_DMA
||
2015 qc
->tf
.protocol
== ATAPI_PROT_DMA
)) {
2018 host_stat
= ap
->ops
->bmdma_status(ap
);
2020 /* BMDMA controllers indicate host bus error by
2021 * setting DMA_ERR bit and timing out. As it wasn't
2022 * really a timeout event, adjust error mask and
2023 * cancel frozen state.
2025 if (qc
->err_mask
== AC_ERR_TIMEOUT
&& (host_stat
& ATA_DMA_ERR
)) {
2026 qc
->err_mask
= AC_ERR_HOST_BUS
;
2030 ap
->ops
->bmdma_stop(qc
);
2033 ata_sff_altstatus(ap
);
2034 ap
->ops
->sff_check_status(ap
);
2035 ap
->ops
->sff_irq_clear(ap
);
2037 spin_unlock_irqrestore(ap
->lock
, flags
);
2040 ata_eh_thaw_port(ap
);
2042 /* PIO and DMA engines have been stopped, perform recovery */
2044 /* Ignore ata_sff_softreset if ctl isn't accessible and
2045 * built-in hardresets if SCR access isn't available.
2047 if (softreset
== ata_sff_softreset
&& !ap
->ioaddr
.ctl_addr
)
2049 if (ata_is_builtin_hardreset(hardreset
) && !sata_scr_valid(&ap
->link
))
2052 ata_do_eh(ap
, ap
->ops
->prereset
, softreset
, hardreset
,
2053 ap
->ops
->postreset
);
2057 * ata_sff_post_internal_cmd - Stock post_internal_cmd for SFF controller
2058 * @qc: internal command to clean up
2061 * Kernel thread context (may sleep)
2063 void ata_sff_post_internal_cmd(struct ata_queued_cmd
*qc
)
2065 if (qc
->ap
->ioaddr
.bmdma_addr
)
2070 * ata_sff_port_start - Set port up for dma.
2071 * @ap: Port to initialize
2073 * Called just after data structures for each port are
2074 * initialized. Allocates space for PRD table if the device
2075 * is DMA capable SFF.
2077 * May be used as the port_start() entry in ata_port_operations.
2080 * Inherited from caller.
2082 int ata_sff_port_start(struct ata_port
*ap
)
2084 if (ap
->ioaddr
.bmdma_addr
)
2085 return ata_port_start(ap
);
2090 * ata_sff_std_ports - initialize ioaddr with standard port offsets.
2091 * @ioaddr: IO address structure to be initialized
2093 * Utility function which initializes data_addr, error_addr,
2094 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
2095 * device_addr, status_addr, and command_addr to standard offsets
2096 * relative to cmd_addr.
2098 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
2100 void ata_sff_std_ports(struct ata_ioports
*ioaddr
)
2102 ioaddr
->data_addr
= ioaddr
->cmd_addr
+ ATA_REG_DATA
;
2103 ioaddr
->error_addr
= ioaddr
->cmd_addr
+ ATA_REG_ERR
;
2104 ioaddr
->feature_addr
= ioaddr
->cmd_addr
+ ATA_REG_FEATURE
;
2105 ioaddr
->nsect_addr
= ioaddr
->cmd_addr
+ ATA_REG_NSECT
;
2106 ioaddr
->lbal_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAL
;
2107 ioaddr
->lbam_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAM
;
2108 ioaddr
->lbah_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAH
;
2109 ioaddr
->device_addr
= ioaddr
->cmd_addr
+ ATA_REG_DEVICE
;
2110 ioaddr
->status_addr
= ioaddr
->cmd_addr
+ ATA_REG_STATUS
;
2111 ioaddr
->command_addr
= ioaddr
->cmd_addr
+ ATA_REG_CMD
;
2114 unsigned long ata_bmdma_mode_filter(struct ata_device
*adev
,
2115 unsigned long xfer_mask
)
2117 /* Filter out DMA modes if the device has been configured by
2118 the BIOS as PIO only */
2120 if (adev
->link
->ap
->ioaddr
.bmdma_addr
== NULL
)
2121 xfer_mask
&= ~(ATA_MASK_MWDMA
| ATA_MASK_UDMA
);
2126 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
2127 * @qc: Info associated with this ATA transaction.
2130 * spin_lock_irqsave(host lock)
2132 void ata_bmdma_setup(struct ata_queued_cmd
*qc
)
2134 struct ata_port
*ap
= qc
->ap
;
2135 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
2138 /* load PRD table addr. */
2139 mb(); /* make sure PRD table writes are visible to controller */
2140 iowrite32(ap
->prd_dma
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_TABLE_OFS
);
2142 /* specify data direction, triple-check start bit is clear */
2143 dmactl
= ioread8(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
2144 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
2146 dmactl
|= ATA_DMA_WR
;
2147 iowrite8(dmactl
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
2149 /* issue r/w command */
2150 ap
->ops
->sff_exec_command(ap
, &qc
->tf
);
2154 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
2155 * @qc: Info associated with this ATA transaction.
2158 * spin_lock_irqsave(host lock)
2160 void ata_bmdma_start(struct ata_queued_cmd
*qc
)
2162 struct ata_port
*ap
= qc
->ap
;
2165 /* start host DMA transaction */
2166 dmactl
= ioread8(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
2167 iowrite8(dmactl
| ATA_DMA_START
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
2169 /* Strictly, one may wish to issue an ioread8() here, to
2170 * flush the mmio write. However, control also passes
2171 * to the hardware at this point, and it will interrupt
2172 * us when we are to resume control. So, in effect,
2173 * we don't care when the mmio write flushes.
2174 * Further, a read of the DMA status register _immediately_
2175 * following the write may not be what certain flaky hardware
2176 * is expected, so I think it is best to not add a readb()
2177 * without first all the MMIO ATA cards/mobos.
2178 * Or maybe I'm just being paranoid.
2180 * FIXME: The posting of this write means I/O starts are
2181 * unneccessarily delayed for MMIO
2186 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
2187 * @qc: Command we are ending DMA for
2189 * Clears the ATA_DMA_START flag in the dma control register
2191 * May be used as the bmdma_stop() entry in ata_port_operations.
2194 * spin_lock_irqsave(host lock)
2196 void ata_bmdma_stop(struct ata_queued_cmd
*qc
)
2198 struct ata_port
*ap
= qc
->ap
;
2199 void __iomem
*mmio
= ap
->ioaddr
.bmdma_addr
;
2201 /* clear start/stop bit */
2202 iowrite8(ioread8(mmio
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
2203 mmio
+ ATA_DMA_CMD
);
2205 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
2206 ata_sff_altstatus(ap
); /* dummy read */
2210 * ata_bmdma_status - Read PCI IDE BMDMA status
2211 * @ap: Port associated with this ATA transaction.
2213 * Read and return BMDMA status register.
2215 * May be used as the bmdma_status() entry in ata_port_operations.
2218 * spin_lock_irqsave(host lock)
2220 u8
ata_bmdma_status(struct ata_port
*ap
)
2222 return ioread8(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
);
2226 * ata_bus_reset - reset host port and associated ATA channel
2227 * @ap: port to reset
2229 * This is typically the first time we actually start issuing
2230 * commands to the ATA channel. We wait for BSY to clear, then
2231 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
2232 * result. Determine what devices, if any, are on the channel
2233 * by looking at the device 0/1 error register. Look at the signature
2234 * stored in each device's taskfile registers, to determine if
2235 * the device is ATA or ATAPI.
2238 * PCI/etc. bus probe sem.
2239 * Obtains host lock.
2242 * Sets ATA_FLAG_DISABLED if bus reset fails.
2245 * This function is only for drivers which still use old EH and
2246 * will be removed soon.
2248 void ata_bus_reset(struct ata_port
*ap
)
2250 struct ata_device
*device
= ap
->link
.device
;
2251 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
2252 unsigned int slave_possible
= ap
->flags
& ATA_FLAG_SLAVE_POSS
;
2254 unsigned int dev0
, dev1
= 0, devmask
= 0;
2257 DPRINTK("ENTER, host %u, port %u\n", ap
->print_id
, ap
->port_no
);
2259 /* determine if device 0/1 are present */
2260 if (ap
->flags
& ATA_FLAG_SATA_RESET
)
2263 dev0
= ata_devchk(ap
, 0);
2265 dev1
= ata_devchk(ap
, 1);
2269 devmask
|= (1 << 0);
2271 devmask
|= (1 << 1);
2273 /* select device 0 again */
2274 ap
->ops
->sff_dev_select(ap
, 0);
2276 /* issue bus reset */
2277 if (ap
->flags
& ATA_FLAG_SRST
) {
2278 rc
= ata_bus_softreset(ap
, devmask
, jiffies
+ 40 * HZ
);
2279 if (rc
&& rc
!= -ENODEV
)
2284 * determine by signature whether we have ATA or ATAPI devices
2286 device
[0].class = ata_sff_dev_classify(&device
[0], dev0
, &err
);
2287 if ((slave_possible
) && (err
!= 0x81))
2288 device
[1].class = ata_sff_dev_classify(&device
[1], dev1
, &err
);
2290 /* is double-select really necessary? */
2291 if (device
[1].class != ATA_DEV_NONE
)
2292 ap
->ops
->sff_dev_select(ap
, 1);
2293 if (device
[0].class != ATA_DEV_NONE
)
2294 ap
->ops
->sff_dev_select(ap
, 0);
2296 /* if no devices were detected, disable this port */
2297 if ((device
[0].class == ATA_DEV_NONE
) &&
2298 (device
[1].class == ATA_DEV_NONE
))
2301 if (ap
->flags
& (ATA_FLAG_SATA_RESET
| ATA_FLAG_SRST
)) {
2302 /* set up device control for ATA_FLAG_SATA_RESET */
2303 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
2310 ata_port_printk(ap
, KERN_ERR
, "disabling port\n");
2311 ata_port_disable(ap
);
2319 * ata_pci_bmdma_clear_simplex - attempt to kick device out of simplex
2322 * Some PCI ATA devices report simplex mode but in fact can be told to
2323 * enter non simplex mode. This implements the necessary logic to
2324 * perform the task on such devices. Calling it on other devices will
2325 * have -undefined- behaviour.
2327 int ata_pci_bmdma_clear_simplex(struct pci_dev
*pdev
)
2329 unsigned long bmdma
= pci_resource_start(pdev
, 4);
2335 simplex
= inb(bmdma
+ 0x02);
2336 outb(simplex
& 0x60, bmdma
+ 0x02);
2337 simplex
= inb(bmdma
+ 0x02);
2344 * ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
2345 * @host: target ATA host
2347 * Acquire PCI BMDMA resources and initialize @host accordingly.
2350 * Inherited from calling layer (may sleep).
2353 * 0 on success, -errno otherwise.
2355 int ata_pci_bmdma_init(struct ata_host
*host
)
2357 struct device
*gdev
= host
->dev
;
2358 struct pci_dev
*pdev
= to_pci_dev(gdev
);
2361 /* No BAR4 allocation: No DMA */
2362 if (pci_resource_start(pdev
, 4) == 0)
2365 /* TODO: If we get no DMA mask we should fall back to PIO */
2366 rc
= pci_set_dma_mask(pdev
, ATA_DMA_MASK
);
2369 rc
= pci_set_consistent_dma_mask(pdev
, ATA_DMA_MASK
);
2373 /* request and iomap DMA region */
2374 rc
= pcim_iomap_regions(pdev
, 1 << 4, dev_driver_string(gdev
));
2376 dev_printk(KERN_ERR
, gdev
, "failed to request/iomap BAR4\n");
2379 host
->iomap
= pcim_iomap_table(pdev
);
2381 for (i
= 0; i
< 2; i
++) {
2382 struct ata_port
*ap
= host
->ports
[i
];
2383 void __iomem
*bmdma
= host
->iomap
[4] + 8 * i
;
2385 if (ata_port_is_dummy(ap
))
2388 ap
->ioaddr
.bmdma_addr
= bmdma
;
2389 if ((!(ap
->flags
& ATA_FLAG_IGN_SIMPLEX
)) &&
2390 (ioread8(bmdma
+ 2) & 0x80))
2391 host
->flags
|= ATA_HOST_SIMPLEX
;
2393 ata_port_desc(ap
, "bmdma 0x%llx",
2394 (unsigned long long)pci_resource_start(pdev
, 4) + 8 * i
);
2400 static int ata_resources_present(struct pci_dev
*pdev
, int port
)
2404 /* Check the PCI resources for this channel are enabled */
2406 for (i
= 0; i
< 2; i
++) {
2407 if (pci_resource_start(pdev
, port
+ i
) == 0 ||
2408 pci_resource_len(pdev
, port
+ i
) == 0)
2415 * ata_pci_sff_init_host - acquire native PCI ATA resources and init host
2416 * @host: target ATA host
2418 * Acquire native PCI ATA resources for @host and initialize the
2419 * first two ports of @host accordingly. Ports marked dummy are
2420 * skipped and allocation failure makes the port dummy.
2422 * Note that native PCI resources are valid even for legacy hosts
2423 * as we fix up pdev resources array early in boot, so this
2424 * function can be used for both native and legacy SFF hosts.
2427 * Inherited from calling layer (may sleep).
2430 * 0 if at least one port is initialized, -ENODEV if no port is
2433 int ata_pci_sff_init_host(struct ata_host
*host
)
2435 struct device
*gdev
= host
->dev
;
2436 struct pci_dev
*pdev
= to_pci_dev(gdev
);
2437 unsigned int mask
= 0;
2440 /* request, iomap BARs and init port addresses accordingly */
2441 for (i
= 0; i
< 2; i
++) {
2442 struct ata_port
*ap
= host
->ports
[i
];
2444 void __iomem
* const *iomap
;
2446 if (ata_port_is_dummy(ap
))
2449 /* Discard disabled ports. Some controllers show
2450 * their unused channels this way. Disabled ports are
2453 if (!ata_resources_present(pdev
, i
)) {
2454 ap
->ops
= &ata_dummy_port_ops
;
2458 rc
= pcim_iomap_regions(pdev
, 0x3 << base
,
2459 dev_driver_string(gdev
));
2461 dev_printk(KERN_WARNING
, gdev
,
2462 "failed to request/iomap BARs for port %d "
2463 "(errno=%d)\n", i
, rc
);
2465 pcim_pin_device(pdev
);
2466 ap
->ops
= &ata_dummy_port_ops
;
2469 host
->iomap
= iomap
= pcim_iomap_table(pdev
);
2471 ap
->ioaddr
.cmd_addr
= iomap
[base
];
2472 ap
->ioaddr
.altstatus_addr
=
2473 ap
->ioaddr
.ctl_addr
= (void __iomem
*)
2474 ((unsigned long)iomap
[base
+ 1] | ATA_PCI_CTL_OFS
);
2475 ata_sff_std_ports(&ap
->ioaddr
);
2477 ata_port_desc(ap
, "cmd 0x%llx ctl 0x%llx",
2478 (unsigned long long)pci_resource_start(pdev
, base
),
2479 (unsigned long long)pci_resource_start(pdev
, base
+ 1));
2485 dev_printk(KERN_ERR
, gdev
, "no available native port\n");
2493 * ata_pci_sff_prepare_host - helper to prepare native PCI ATA host
2494 * @pdev: target PCI device
2495 * @ppi: array of port_info, must be enough for two ports
2496 * @r_host: out argument for the initialized ATA host
2498 * Helper to allocate ATA host for @pdev, acquire all native PCI
2499 * resources and initialize it accordingly in one go.
2502 * Inherited from calling layer (may sleep).
2505 * 0 on success, -errno otherwise.
2507 int ata_pci_sff_prepare_host(struct pci_dev
*pdev
,
2508 const struct ata_port_info
* const * ppi
,
2509 struct ata_host
**r_host
)
2511 struct ata_host
*host
;
2514 if (!devres_open_group(&pdev
->dev
, NULL
, GFP_KERNEL
))
2517 host
= ata_host_alloc_pinfo(&pdev
->dev
, ppi
, 2);
2519 dev_printk(KERN_ERR
, &pdev
->dev
,
2520 "failed to allocate ATA host\n");
2525 rc
= ata_pci_sff_init_host(host
);
2529 /* init DMA related stuff */
2530 rc
= ata_pci_bmdma_init(host
);
2534 devres_remove_group(&pdev
->dev
, NULL
);
2539 /* This is necessary because PCI and iomap resources are
2540 * merged and releasing the top group won't release the
2541 * acquired resources if some of those have been acquired
2542 * before entering this function.
2544 pcim_iounmap_regions(pdev
, 0xf);
2546 devres_release_group(&pdev
->dev
, NULL
);
2551 * ata_pci_sff_activate_host - start SFF host, request IRQ and register it
2552 * @host: target SFF ATA host
2553 * @irq_handler: irq_handler used when requesting IRQ(s)
2554 * @sht: scsi_host_template to use when registering the host
2556 * This is the counterpart of ata_host_activate() for SFF ATA
2557 * hosts. This separate helper is necessary because SFF hosts
2558 * use two separate interrupts in legacy mode.
2561 * Inherited from calling layer (may sleep).
2564 * 0 on success, -errno otherwise.
2566 int ata_pci_sff_activate_host(struct ata_host
*host
,
2567 irq_handler_t irq_handler
,
2568 struct scsi_host_template
*sht
)
2570 struct device
*dev
= host
->dev
;
2571 struct pci_dev
*pdev
= to_pci_dev(dev
);
2572 const char *drv_name
= dev_driver_string(host
->dev
);
2573 int legacy_mode
= 0, rc
;
2575 rc
= ata_host_start(host
);
2579 if ((pdev
->class >> 8) == PCI_CLASS_STORAGE_IDE
) {
2582 /* TODO: What if one channel is in native mode ... */
2583 pci_read_config_byte(pdev
, PCI_CLASS_PROG
, &tmp8
);
2584 mask
= (1 << 2) | (1 << 0);
2585 if ((tmp8
& mask
) != mask
)
2587 #if defined(CONFIG_NO_ATA_LEGACY)
2588 /* Some platforms with PCI limits cannot address compat
2589 port space. In that case we punt if their firmware has
2590 left a device in compatibility mode */
2592 printk(KERN_ERR
"ata: Compatibility mode ATA is not supported on this platform, skipping.\n");
2598 if (!devres_open_group(dev
, NULL
, GFP_KERNEL
))
2601 if (!legacy_mode
&& pdev
->irq
) {
2602 rc
= devm_request_irq(dev
, pdev
->irq
, irq_handler
,
2603 IRQF_SHARED
, drv_name
, host
);
2607 ata_port_desc(host
->ports
[0], "irq %d", pdev
->irq
);
2608 ata_port_desc(host
->ports
[1], "irq %d", pdev
->irq
);
2609 } else if (legacy_mode
) {
2610 if (!ata_port_is_dummy(host
->ports
[0])) {
2611 rc
= devm_request_irq(dev
, ATA_PRIMARY_IRQ(pdev
),
2612 irq_handler
, IRQF_SHARED
,
2617 ata_port_desc(host
->ports
[0], "irq %d",
2618 ATA_PRIMARY_IRQ(pdev
));
2621 if (!ata_port_is_dummy(host
->ports
[1])) {
2622 rc
= devm_request_irq(dev
, ATA_SECONDARY_IRQ(pdev
),
2623 irq_handler
, IRQF_SHARED
,
2628 ata_port_desc(host
->ports
[1], "irq %d",
2629 ATA_SECONDARY_IRQ(pdev
));
2633 rc
= ata_host_register(host
, sht
);
2636 devres_remove_group(dev
, NULL
);
2638 devres_release_group(dev
, NULL
);
2644 * ata_pci_sff_init_one - Initialize/register PCI IDE host controller
2645 * @pdev: Controller to be initialized
2646 * @ppi: array of port_info, must be enough for two ports
2647 * @sht: scsi_host_template to use when registering the host
2648 * @host_priv: host private_data
2650 * This is a helper function which can be called from a driver's
2651 * xxx_init_one() probe function if the hardware uses traditional
2652 * IDE taskfile registers.
2654 * This function calls pci_enable_device(), reserves its register
2655 * regions, sets the dma mask, enables bus master mode, and calls
2659 * Nobody makes a single channel controller that appears solely as
2660 * the secondary legacy port on PCI.
2663 * Inherited from PCI layer (may sleep).
2666 * Zero on success, negative on errno-based value on error.
2668 int ata_pci_sff_init_one(struct pci_dev
*pdev
,
2669 const struct ata_port_info
* const * ppi
,
2670 struct scsi_host_template
*sht
, void *host_priv
)
2672 struct device
*dev
= &pdev
->dev
;
2673 const struct ata_port_info
*pi
= NULL
;
2674 struct ata_host
*host
= NULL
;
2679 /* look up the first valid port_info */
2680 for (i
= 0; i
< 2 && ppi
[i
]; i
++) {
2681 if (ppi
[i
]->port_ops
!= &ata_dummy_port_ops
) {
2688 dev_printk(KERN_ERR
, &pdev
->dev
,
2689 "no valid port_info specified\n");
2693 if (!devres_open_group(dev
, NULL
, GFP_KERNEL
))
2696 rc
= pcim_enable_device(pdev
);
2700 /* prepare and activate SFF host */
2701 rc
= ata_pci_sff_prepare_host(pdev
, ppi
, &host
);
2704 host
->private_data
= host_priv
;
2706 pci_set_master(pdev
);
2707 rc
= ata_pci_sff_activate_host(host
, ata_sff_interrupt
, sht
);
2710 devres_remove_group(&pdev
->dev
, NULL
);
2712 devres_release_group(&pdev
->dev
, NULL
);
2717 #endif /* CONFIG_PCI */
2719 EXPORT_SYMBOL_GPL(ata_sff_port_ops
);
2720 EXPORT_SYMBOL_GPL(ata_bmdma_port_ops
);
2721 EXPORT_SYMBOL_GPL(ata_sff_qc_prep
);
2722 EXPORT_SYMBOL_GPL(ata_sff_dumb_qc_prep
);
2723 EXPORT_SYMBOL_GPL(ata_sff_dev_select
);
2724 EXPORT_SYMBOL_GPL(ata_sff_check_status
);
2725 EXPORT_SYMBOL_GPL(ata_sff_altstatus
);
2726 EXPORT_SYMBOL_GPL(ata_sff_busy_sleep
);
2727 EXPORT_SYMBOL_GPL(ata_sff_wait_ready
);
2728 EXPORT_SYMBOL_GPL(ata_sff_tf_load
);
2729 EXPORT_SYMBOL_GPL(ata_sff_tf_read
);
2730 EXPORT_SYMBOL_GPL(ata_sff_exec_command
);
2731 EXPORT_SYMBOL_GPL(ata_sff_data_xfer
);
2732 EXPORT_SYMBOL_GPL(ata_sff_data_xfer_noirq
);
2733 EXPORT_SYMBOL_GPL(ata_sff_irq_on
);
2734 EXPORT_SYMBOL_GPL(ata_sff_irq_clear
);
2735 EXPORT_SYMBOL_GPL(ata_sff_hsm_move
);
2736 EXPORT_SYMBOL_GPL(ata_sff_qc_issue
);
2737 EXPORT_SYMBOL_GPL(ata_sff_qc_fill_rtf
);
2738 EXPORT_SYMBOL_GPL(ata_sff_host_intr
);
2739 EXPORT_SYMBOL_GPL(ata_sff_interrupt
);
2740 EXPORT_SYMBOL_GPL(ata_sff_freeze
);
2741 EXPORT_SYMBOL_GPL(ata_sff_thaw
);
2742 EXPORT_SYMBOL_GPL(ata_sff_prereset
);
2743 EXPORT_SYMBOL_GPL(ata_sff_dev_classify
);
2744 EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset
);
2745 EXPORT_SYMBOL_GPL(ata_sff_softreset
);
2746 EXPORT_SYMBOL_GPL(sata_sff_hardreset
);
2747 EXPORT_SYMBOL_GPL(ata_sff_postreset
);
2748 EXPORT_SYMBOL_GPL(ata_sff_error_handler
);
2749 EXPORT_SYMBOL_GPL(ata_sff_post_internal_cmd
);
2750 EXPORT_SYMBOL_GPL(ata_sff_port_start
);
2751 EXPORT_SYMBOL_GPL(ata_sff_std_ports
);
2752 EXPORT_SYMBOL_GPL(ata_bmdma_mode_filter
);
2753 EXPORT_SYMBOL_GPL(ata_bmdma_setup
);
2754 EXPORT_SYMBOL_GPL(ata_bmdma_start
);
2755 EXPORT_SYMBOL_GPL(ata_bmdma_stop
);
2756 EXPORT_SYMBOL_GPL(ata_bmdma_status
);
2757 EXPORT_SYMBOL_GPL(ata_bus_reset
);
2759 EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex
);
2760 EXPORT_SYMBOL_GPL(ata_pci_bmdma_init
);
2761 EXPORT_SYMBOL_GPL(ata_pci_sff_init_host
);
2762 EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host
);
2763 EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host
);
2764 EXPORT_SYMBOL_GPL(ata_pci_sff_init_one
);
2765 #endif /* CONFIG_PCI */