1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * libata-sff.c - helper library for PCI IDE BMDMA
5 * Copyright 2003-2006 Red Hat, Inc. All rights reserved.
6 * Copyright 2003-2006 Jeff Garzik
8 * libata documentation is available via 'make {ps|pdf}docs',
9 * as Documentation/driver-api/libata.rst
11 * Hardware documentation available from http://www.t13.org/ and
12 * http://www.sata-io.org/
15 #include <linux/kernel.h>
16 #include <linux/gfp.h>
17 #include <linux/pci.h>
18 #include <linux/module.h>
19 #include <linux/libata.h>
20 #include <linux/highmem.h>
21 #include <trace/events/libata.h>
24 static struct workqueue_struct
*ata_sff_wq
;
26 const struct ata_port_operations ata_sff_port_ops
= {
27 .inherits
= &ata_base_port_ops
,
29 .qc_issue
= ata_sff_qc_issue
,
30 .qc_fill_rtf
= ata_sff_qc_fill_rtf
,
32 .freeze
= ata_sff_freeze
,
34 .prereset
= ata_sff_prereset
,
35 .softreset
= ata_sff_softreset
,
36 .hardreset
= sata_sff_hardreset
,
37 .postreset
= ata_sff_postreset
,
38 .error_handler
= ata_sff_error_handler
,
40 .sff_dev_select
= ata_sff_dev_select
,
41 .sff_check_status
= ata_sff_check_status
,
42 .sff_tf_load
= ata_sff_tf_load
,
43 .sff_tf_read
= ata_sff_tf_read
,
44 .sff_exec_command
= ata_sff_exec_command
,
45 .sff_data_xfer
= ata_sff_data_xfer
,
46 .sff_drain_fifo
= ata_sff_drain_fifo
,
48 .lost_interrupt
= ata_sff_lost_interrupt
,
50 EXPORT_SYMBOL_GPL(ata_sff_port_ops
);
53 * ata_sff_check_status - Read device status reg & clear interrupt
54 * @ap: port where the device is
56 * Reads ATA taskfile status register for currently-selected device
57 * and return its value. This also clears pending interrupts
61 * Inherited from caller.
63 u8
ata_sff_check_status(struct ata_port
*ap
)
65 return ioread8(ap
->ioaddr
.status_addr
);
67 EXPORT_SYMBOL_GPL(ata_sff_check_status
);
70 * ata_sff_altstatus - Read device alternate status reg
71 * @ap: port where the device is
72 * @status: pointer to a status value
74 * Reads ATA alternate status register for currently-selected device
75 * and return its value.
78 * true if the register exists, false if not.
81 * Inherited from caller.
83 static bool ata_sff_altstatus(struct ata_port
*ap
, u8
*status
)
87 if (ap
->ops
->sff_check_altstatus
) {
88 tmp
= ap
->ops
->sff_check_altstatus(ap
);
91 if (ap
->ioaddr
.altstatus_addr
) {
92 tmp
= ioread8(ap
->ioaddr
.altstatus_addr
);
104 * ata_sff_irq_status - Check if the device is busy
105 * @ap: port where the device is
107 * Determine if the port is currently busy. Uses altstatus
108 * if available in order to avoid clearing shared IRQ status
109 * when finding an IRQ source. Non ctl capable devices don't
110 * share interrupt lines fortunately for us.
113 * Inherited from caller.
115 static u8
ata_sff_irq_status(struct ata_port
*ap
)
119 /* Not us: We are busy */
120 if (ata_sff_altstatus(ap
, &status
) && (status
& ATA_BUSY
))
122 /* Clear INTRQ latch */
123 status
= ap
->ops
->sff_check_status(ap
);
128 * ata_sff_sync - Flush writes
129 * @ap: Port to wait for.
132 * If we have an mmio device with no ctl and no altstatus
133 * method this will fail. No such devices are known to exist.
136 * Inherited from caller.
139 static void ata_sff_sync(struct ata_port
*ap
)
141 ata_sff_altstatus(ap
, NULL
);
145 * ata_sff_pause - Flush writes and wait 400nS
146 * @ap: Port to pause for.
149 * If we have an mmio device with no ctl and no altstatus
150 * method this will fail. No such devices are known to exist.
153 * Inherited from caller.
156 void ata_sff_pause(struct ata_port
*ap
)
161 EXPORT_SYMBOL_GPL(ata_sff_pause
);
164 * ata_sff_dma_pause - Pause before commencing DMA
165 * @ap: Port to pause for.
167 * Perform I/O fencing and ensure sufficient cycle delays occur
168 * for the HDMA1:0 transition
171 void ata_sff_dma_pause(struct ata_port
*ap
)
174 * An altstatus read will cause the needed delay without
175 * messing up the IRQ status
177 if (ata_sff_altstatus(ap
, NULL
))
179 /* There are no DMA controllers without ctl. BUG here to ensure
180 we never violate the HDMA1:0 transition timing and risk
184 EXPORT_SYMBOL_GPL(ata_sff_dma_pause
);
186 static int ata_sff_check_ready(struct ata_link
*link
)
188 u8 status
= link
->ap
->ops
->sff_check_status(link
->ap
);
190 return ata_check_ready(status
);
194 * ata_sff_wait_ready - sleep until BSY clears, or timeout
195 * @link: SFF link to wait ready status for
196 * @deadline: deadline jiffies for the operation
198 * Sleep until ATA Status register bit BSY clears, or timeout
202 * Kernel thread context (may sleep).
205 * 0 on success, -errno otherwise.
207 int ata_sff_wait_ready(struct ata_link
*link
, unsigned long deadline
)
209 return ata_wait_ready(link
, deadline
, ata_sff_check_ready
);
211 EXPORT_SYMBOL_GPL(ata_sff_wait_ready
);
214 * ata_sff_set_devctl - Write device control reg
215 * @ap: port where the device is
216 * @ctl: value to write
218 * Writes ATA device control register.
221 * true if the register exists, false if not.
224 * Inherited from caller.
226 static bool ata_sff_set_devctl(struct ata_port
*ap
, u8 ctl
)
228 if (ap
->ops
->sff_set_devctl
) {
229 ap
->ops
->sff_set_devctl(ap
, ctl
);
232 if (ap
->ioaddr
.ctl_addr
) {
233 iowrite8(ctl
, ap
->ioaddr
.ctl_addr
);
241 * ata_sff_dev_select - Select device 0/1 on ATA bus
242 * @ap: ATA channel to manipulate
243 * @device: ATA device (numbered from zero) to select
245 * Use the method defined in the ATA specification to
246 * make either device 0, or device 1, active on the
247 * ATA channel. Works with both PIO and MMIO.
249 * May be used as the dev_select() entry in ata_port_operations.
254 void ata_sff_dev_select(struct ata_port
*ap
, unsigned int device
)
259 tmp
= ATA_DEVICE_OBS
;
261 tmp
= ATA_DEVICE_OBS
| ATA_DEV1
;
263 iowrite8(tmp
, ap
->ioaddr
.device_addr
);
264 ata_sff_pause(ap
); /* needed; also flushes, for mmio */
266 EXPORT_SYMBOL_GPL(ata_sff_dev_select
);
269 * ata_dev_select - Select device 0/1 on ATA bus
270 * @ap: ATA channel to manipulate
271 * @device: ATA device (numbered from zero) to select
272 * @wait: non-zero to wait for Status register BSY bit to clear
273 * @can_sleep: non-zero if context allows sleeping
275 * Use the method defined in the ATA specification to
276 * make either device 0, or device 1, active on the
279 * This is a high-level version of ata_sff_dev_select(), which
280 * additionally provides the services of inserting the proper
281 * pauses and status polling, where needed.
286 static void ata_dev_select(struct ata_port
*ap
, unsigned int device
,
287 unsigned int wait
, unsigned int can_sleep
)
292 ap
->ops
->sff_dev_select(ap
, device
);
295 if (can_sleep
&& ap
->link
.device
[device
].class == ATA_DEV_ATAPI
)
302 * ata_sff_irq_on - Enable interrupts on a port.
303 * @ap: Port on which interrupts are enabled.
305 * Enable interrupts on a legacy IDE device using MMIO or PIO,
306 * wait for idle, clear any pending interrupts.
308 * Note: may NOT be used as the sff_irq_on() entry in
309 * ata_port_operations.
312 * Inherited from caller.
314 void ata_sff_irq_on(struct ata_port
*ap
)
316 if (ap
->ops
->sff_irq_on
) {
317 ap
->ops
->sff_irq_on(ap
);
321 ap
->ctl
&= ~ATA_NIEN
;
322 ap
->last_ctl
= ap
->ctl
;
324 ata_sff_set_devctl(ap
, ap
->ctl
);
327 if (ap
->ops
->sff_irq_clear
)
328 ap
->ops
->sff_irq_clear(ap
);
330 EXPORT_SYMBOL_GPL(ata_sff_irq_on
);
333 * ata_sff_tf_load - send taskfile registers to host controller
334 * @ap: Port to which output is sent
335 * @tf: ATA taskfile register set
337 * Outputs ATA taskfile to standard ATA host controller.
340 * Inherited from caller.
342 void ata_sff_tf_load(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
344 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
345 unsigned int is_addr
= tf
->flags
& ATA_TFLAG_ISADDR
;
347 if (tf
->ctl
!= ap
->last_ctl
) {
348 if (ioaddr
->ctl_addr
)
349 iowrite8(tf
->ctl
, ioaddr
->ctl_addr
);
350 ap
->last_ctl
= tf
->ctl
;
354 if (is_addr
&& (tf
->flags
& ATA_TFLAG_LBA48
)) {
355 WARN_ON_ONCE(!ioaddr
->ctl_addr
);
356 iowrite8(tf
->hob_feature
, ioaddr
->feature_addr
);
357 iowrite8(tf
->hob_nsect
, ioaddr
->nsect_addr
);
358 iowrite8(tf
->hob_lbal
, ioaddr
->lbal_addr
);
359 iowrite8(tf
->hob_lbam
, ioaddr
->lbam_addr
);
360 iowrite8(tf
->hob_lbah
, ioaddr
->lbah_addr
);
364 iowrite8(tf
->feature
, ioaddr
->feature_addr
);
365 iowrite8(tf
->nsect
, ioaddr
->nsect_addr
);
366 iowrite8(tf
->lbal
, ioaddr
->lbal_addr
);
367 iowrite8(tf
->lbam
, ioaddr
->lbam_addr
);
368 iowrite8(tf
->lbah
, ioaddr
->lbah_addr
);
371 if (tf
->flags
& ATA_TFLAG_DEVICE
)
372 iowrite8(tf
->device
, ioaddr
->device_addr
);
376 EXPORT_SYMBOL_GPL(ata_sff_tf_load
);
379 * ata_sff_tf_read - input device's ATA taskfile shadow registers
380 * @ap: Port from which input is read
381 * @tf: ATA taskfile register set for storing input
383 * Reads ATA taskfile registers for currently-selected device
384 * into @tf. Assumes the device has a fully SFF compliant task file
385 * layout and behaviour. If you device does not (eg has a different
386 * status method) then you will need to provide a replacement tf_read
389 * Inherited from caller.
391 void ata_sff_tf_read(struct ata_port
*ap
, struct ata_taskfile
*tf
)
393 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
395 tf
->status
= ata_sff_check_status(ap
);
396 tf
->error
= ioread8(ioaddr
->error_addr
);
397 tf
->nsect
= ioread8(ioaddr
->nsect_addr
);
398 tf
->lbal
= ioread8(ioaddr
->lbal_addr
);
399 tf
->lbam
= ioread8(ioaddr
->lbam_addr
);
400 tf
->lbah
= ioread8(ioaddr
->lbah_addr
);
401 tf
->device
= ioread8(ioaddr
->device_addr
);
403 if (tf
->flags
& ATA_TFLAG_LBA48
) {
404 if (likely(ioaddr
->ctl_addr
)) {
405 iowrite8(tf
->ctl
| ATA_HOB
, ioaddr
->ctl_addr
);
406 tf
->hob_feature
= ioread8(ioaddr
->error_addr
);
407 tf
->hob_nsect
= ioread8(ioaddr
->nsect_addr
);
408 tf
->hob_lbal
= ioread8(ioaddr
->lbal_addr
);
409 tf
->hob_lbam
= ioread8(ioaddr
->lbam_addr
);
410 tf
->hob_lbah
= ioread8(ioaddr
->lbah_addr
);
411 iowrite8(tf
->ctl
, ioaddr
->ctl_addr
);
412 ap
->last_ctl
= tf
->ctl
;
417 EXPORT_SYMBOL_GPL(ata_sff_tf_read
);
420 * ata_sff_exec_command - issue ATA command to host controller
421 * @ap: port to which command is being issued
422 * @tf: ATA taskfile register set
424 * Issues ATA command, with proper synchronization with interrupt
425 * handler / other threads.
428 * spin_lock_irqsave(host lock)
430 void ata_sff_exec_command(struct ata_port
*ap
, const struct ata_taskfile
*tf
)
432 iowrite8(tf
->command
, ap
->ioaddr
.command_addr
);
435 EXPORT_SYMBOL_GPL(ata_sff_exec_command
);
438 * ata_tf_to_host - issue ATA taskfile to host controller
439 * @ap: port to which command is being issued
440 * @tf: ATA taskfile register set
441 * @tag: tag of the associated command
443 * Issues ATA taskfile register set to ATA host controller,
444 * with proper synchronization with interrupt handler and
448 * spin_lock_irqsave(host lock)
450 static inline void ata_tf_to_host(struct ata_port
*ap
,
451 const struct ata_taskfile
*tf
,
454 trace_ata_tf_load(ap
, tf
);
455 ap
->ops
->sff_tf_load(ap
, tf
);
456 trace_ata_exec_command(ap
, tf
, tag
);
457 ap
->ops
->sff_exec_command(ap
, tf
);
461 * ata_sff_data_xfer - Transfer data by PIO
462 * @qc: queued command
464 * @buflen: buffer length
467 * Transfer data from/to the device data register by PIO.
470 * Inherited from caller.
475 unsigned int ata_sff_data_xfer(struct ata_queued_cmd
*qc
, unsigned char *buf
,
476 unsigned int buflen
, int rw
)
478 struct ata_port
*ap
= qc
->dev
->link
->ap
;
479 void __iomem
*data_addr
= ap
->ioaddr
.data_addr
;
480 unsigned int words
= buflen
>> 1;
482 /* Transfer multiple of 2 bytes */
484 ioread16_rep(data_addr
, buf
, words
);
486 iowrite16_rep(data_addr
, buf
, words
);
488 /* Transfer trailing byte, if any. */
489 if (unlikely(buflen
& 0x01)) {
490 unsigned char pad
[2] = { };
492 /* Point buf to the tail of buffer */
496 * Use io*16_rep() accessors here as well to avoid pointlessly
497 * swapping bytes to and from on the big endian machines...
500 ioread16_rep(data_addr
, pad
, 1);
504 iowrite16_rep(data_addr
, pad
, 1);
511 EXPORT_SYMBOL_GPL(ata_sff_data_xfer
);
514 * ata_sff_data_xfer32 - Transfer data by PIO
515 * @qc: queued command
517 * @buflen: buffer length
520 * Transfer data from/to the device data register by PIO using 32bit
524 * Inherited from caller.
530 unsigned int ata_sff_data_xfer32(struct ata_queued_cmd
*qc
, unsigned char *buf
,
531 unsigned int buflen
, int rw
)
533 struct ata_device
*dev
= qc
->dev
;
534 struct ata_port
*ap
= dev
->link
->ap
;
535 void __iomem
*data_addr
= ap
->ioaddr
.data_addr
;
536 unsigned int words
= buflen
>> 2;
537 int slop
= buflen
& 3;
539 if (!(ap
->pflags
& ATA_PFLAG_PIO32
))
540 return ata_sff_data_xfer(qc
, buf
, buflen
, rw
);
542 /* Transfer multiple of 4 bytes */
544 ioread32_rep(data_addr
, buf
, words
);
546 iowrite32_rep(data_addr
, buf
, words
);
548 /* Transfer trailing bytes, if any */
549 if (unlikely(slop
)) {
550 unsigned char pad
[4] = { };
552 /* Point buf to the tail of buffer */
553 buf
+= buflen
- slop
;
556 * Use io*_rep() accessors here as well to avoid pointlessly
557 * swapping bytes to and from on the big endian machines...
561 ioread16_rep(data_addr
, pad
, 1);
563 ioread32_rep(data_addr
, pad
, 1);
564 memcpy(buf
, pad
, slop
);
566 memcpy(pad
, buf
, slop
);
568 iowrite16_rep(data_addr
, pad
, 1);
570 iowrite32_rep(data_addr
, pad
, 1);
573 return (buflen
+ 1) & ~1;
575 EXPORT_SYMBOL_GPL(ata_sff_data_xfer32
);
577 static void ata_pio_xfer(struct ata_queued_cmd
*qc
, struct page
*page
,
578 unsigned int offset
, size_t xfer_size
)
580 bool do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
583 buf
= kmap_atomic(page
);
584 qc
->ap
->ops
->sff_data_xfer(qc
, buf
+ offset
, xfer_size
, do_write
);
587 if (!do_write
&& !PageSlab(page
))
588 flush_dcache_page(page
);
592 * ata_pio_sector - Transfer a sector of data.
593 * @qc: Command on going
595 * Transfer qc->sect_size bytes of data from/to the ATA device.
598 * Inherited from caller.
600 static void ata_pio_sector(struct ata_queued_cmd
*qc
)
602 struct ata_port
*ap
= qc
->ap
;
607 qc
->curbytes
= qc
->nbytes
;
610 if (qc
->curbytes
== qc
->nbytes
- qc
->sect_size
)
611 ap
->hsm_task_state
= HSM_ST_LAST
;
613 page
= sg_page(qc
->cursg
);
614 offset
= qc
->cursg
->offset
+ qc
->cursg_ofs
;
616 /* get the current page and offset */
617 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
620 trace_ata_sff_pio_transfer_data(qc
, offset
, qc
->sect_size
);
623 * Split the transfer when it splits a page boundary. Note that the
624 * split still has to be dword aligned like all ATA data transfers.
626 WARN_ON_ONCE(offset
% 4);
627 if (offset
+ qc
->sect_size
> PAGE_SIZE
) {
628 unsigned int split_len
= PAGE_SIZE
- offset
;
630 ata_pio_xfer(qc
, page
, offset
, split_len
);
631 ata_pio_xfer(qc
, nth_page(page
, 1), 0,
632 qc
->sect_size
- split_len
);
634 ata_pio_xfer(qc
, page
, offset
, qc
->sect_size
);
637 qc
->curbytes
+= qc
->sect_size
;
638 qc
->cursg_ofs
+= qc
->sect_size
;
640 if (qc
->cursg_ofs
== qc
->cursg
->length
) {
641 qc
->cursg
= sg_next(qc
->cursg
);
643 ap
->hsm_task_state
= HSM_ST_LAST
;
649 * ata_pio_sectors - Transfer one or many sectors.
650 * @qc: Command on going
652 * Transfer one or many sectors of data from/to the
653 * ATA device for the DRQ request.
656 * Inherited from caller.
658 static void ata_pio_sectors(struct ata_queued_cmd
*qc
)
660 if (is_multi_taskfile(&qc
->tf
)) {
661 /* READ/WRITE MULTIPLE */
664 WARN_ON_ONCE(qc
->dev
->multi_count
== 0);
666 nsect
= min((qc
->nbytes
- qc
->curbytes
) / qc
->sect_size
,
667 qc
->dev
->multi_count
);
673 ata_sff_sync(qc
->ap
); /* flush */
677 * atapi_send_cdb - Write CDB bytes to hardware
678 * @ap: Port to which ATAPI device is attached.
679 * @qc: Taskfile currently active
681 * When device has indicated its readiness to accept
682 * a CDB, this function is called. Send the CDB.
687 static void atapi_send_cdb(struct ata_port
*ap
, struct ata_queued_cmd
*qc
)
690 trace_atapi_send_cdb(qc
, 0, qc
->dev
->cdb_len
);
691 WARN_ON_ONCE(qc
->dev
->cdb_len
< 12);
693 ap
->ops
->sff_data_xfer(qc
, qc
->cdb
, qc
->dev
->cdb_len
, 1);
695 /* FIXME: If the CDB is for DMA do we need to do the transition delay
696 or is bmdma_start guaranteed to do it ? */
697 switch (qc
->tf
.protocol
) {
699 ap
->hsm_task_state
= HSM_ST
;
701 case ATAPI_PROT_NODATA
:
702 ap
->hsm_task_state
= HSM_ST_LAST
;
704 #ifdef CONFIG_ATA_BMDMA
706 ap
->hsm_task_state
= HSM_ST_LAST
;
708 trace_ata_bmdma_start(ap
, &qc
->tf
, qc
->tag
);
709 ap
->ops
->bmdma_start(qc
);
711 #endif /* CONFIG_ATA_BMDMA */
718 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
719 * @qc: Command on going
720 * @bytes: number of bytes
722 * Transfer data from/to the ATAPI device.
725 * Inherited from caller.
728 static int __atapi_pio_bytes(struct ata_queued_cmd
*qc
, unsigned int bytes
)
730 int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
) ? WRITE
: READ
;
731 struct ata_port
*ap
= qc
->ap
;
732 struct ata_device
*dev
= qc
->dev
;
733 struct ata_eh_info
*ehi
= &dev
->link
->eh_info
;
734 struct scatterlist
*sg
;
737 unsigned int offset
, count
, consumed
;
742 ata_ehi_push_desc(ehi
, "unexpected or too much trailing data "
743 "buf=%u cur=%u bytes=%u",
744 qc
->nbytes
, qc
->curbytes
, bytes
);
749 offset
= sg
->offset
+ qc
->cursg_ofs
;
751 /* get the current page and offset */
752 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
755 /* don't overrun current sg */
756 count
= min(sg
->length
- qc
->cursg_ofs
, bytes
);
758 /* don't cross page boundaries */
759 count
= min(count
, (unsigned int)PAGE_SIZE
- offset
);
761 trace_atapi_pio_transfer_data(qc
, offset
, count
);
763 /* do the actual data transfer */
764 buf
= kmap_atomic(page
);
765 consumed
= ap
->ops
->sff_data_xfer(qc
, buf
+ offset
, count
, rw
);
768 bytes
-= min(bytes
, consumed
);
769 qc
->curbytes
+= count
;
770 qc
->cursg_ofs
+= count
;
772 if (qc
->cursg_ofs
== sg
->length
) {
773 qc
->cursg
= sg_next(qc
->cursg
);
778 * There used to be a WARN_ON_ONCE(qc->cursg && count != consumed);
779 * Unfortunately __atapi_pio_bytes doesn't know enough to do the WARN
780 * check correctly as it doesn't know if it is the last request being
781 * made. Somebody should implement a proper sanity check.
789 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
790 * @qc: Command on going
792 * Transfer Transfer data from/to the ATAPI device.
795 * Inherited from caller.
797 static void atapi_pio_bytes(struct ata_queued_cmd
*qc
)
799 struct ata_port
*ap
= qc
->ap
;
800 struct ata_device
*dev
= qc
->dev
;
801 struct ata_eh_info
*ehi
= &dev
->link
->eh_info
;
802 unsigned int ireason
, bc_lo
, bc_hi
, bytes
;
803 int i_write
, do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
) ? 1 : 0;
805 /* Abuse qc->result_tf for temp storage of intermediate TF
806 * here to save some kernel stack usage.
807 * For normal completion, qc->result_tf is not relevant. For
808 * error, qc->result_tf is later overwritten by ata_qc_complete().
809 * So, the correctness of qc->result_tf is not affected.
811 ap
->ops
->sff_tf_read(ap
, &qc
->result_tf
);
812 ireason
= qc
->result_tf
.nsect
;
813 bc_lo
= qc
->result_tf
.lbam
;
814 bc_hi
= qc
->result_tf
.lbah
;
815 bytes
= (bc_hi
<< 8) | bc_lo
;
817 /* shall be cleared to zero, indicating xfer of data */
818 if (unlikely(ireason
& ATAPI_COD
))
821 /* make sure transfer direction matches expected */
822 i_write
= ((ireason
& ATAPI_IO
) == 0) ? 1 : 0;
823 if (unlikely(do_write
!= i_write
))
826 if (unlikely(!bytes
))
829 if (unlikely(__atapi_pio_bytes(qc
, bytes
)))
831 ata_sff_sync(ap
); /* flush */
836 ata_ehi_push_desc(ehi
, "ATAPI check failed (ireason=0x%x bytes=%u)",
839 qc
->err_mask
|= AC_ERR_HSM
;
840 ap
->hsm_task_state
= HSM_ST_ERR
;
844 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
845 * @ap: the target ata_port
849 * 1 if ok in workqueue, 0 otherwise.
851 static inline int ata_hsm_ok_in_wq(struct ata_port
*ap
,
852 struct ata_queued_cmd
*qc
)
854 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
857 if (ap
->hsm_task_state
== HSM_ST_FIRST
) {
858 if (qc
->tf
.protocol
== ATA_PROT_PIO
&&
859 (qc
->tf
.flags
& ATA_TFLAG_WRITE
))
862 if (ata_is_atapi(qc
->tf
.protocol
) &&
863 !(qc
->dev
->flags
& ATA_DFLAG_CDB_INTR
))
871 * ata_hsm_qc_complete - finish a qc running on standard HSM
872 * @qc: Command to complete
873 * @in_wq: 1 if called from workqueue, 0 otherwise
875 * Finish @qc which is running on standard HSM.
878 * If @in_wq is zero, spin_lock_irqsave(host lock).
879 * Otherwise, none on entry and grabs host lock.
881 static void ata_hsm_qc_complete(struct ata_queued_cmd
*qc
, int in_wq
)
883 struct ata_port
*ap
= qc
->ap
;
886 /* EH might have kicked in while host lock is released. */
887 qc
= ata_qc_from_tag(ap
, qc
->tag
);
889 if (likely(!(qc
->err_mask
& AC_ERR_HSM
))) {
896 if (likely(!(qc
->err_mask
& AC_ERR_HSM
)))
904 * ata_sff_hsm_move - move the HSM to the next state.
905 * @ap: the target ata_port
907 * @status: current device status
908 * @in_wq: 1 if called from workqueue, 0 otherwise
911 * 1 when poll next status needed, 0 otherwise.
913 int ata_sff_hsm_move(struct ata_port
*ap
, struct ata_queued_cmd
*qc
,
914 u8 status
, int in_wq
)
916 struct ata_link
*link
= qc
->dev
->link
;
917 struct ata_eh_info
*ehi
= &link
->eh_info
;
920 lockdep_assert_held(ap
->lock
);
922 WARN_ON_ONCE((qc
->flags
& ATA_QCFLAG_ACTIVE
) == 0);
924 /* Make sure ata_sff_qc_issue() does not throw things
925 * like DMA polling into the workqueue. Notice that
926 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
928 WARN_ON_ONCE(in_wq
!= ata_hsm_ok_in_wq(ap
, qc
));
931 trace_ata_sff_hsm_state(qc
, status
);
933 switch (ap
->hsm_task_state
) {
935 /* Send first data block or PACKET CDB */
937 /* If polling, we will stay in the work queue after
938 * sending the data. Otherwise, interrupt handler
939 * takes over after sending the data.
941 poll_next
= (qc
->tf
.flags
& ATA_TFLAG_POLLING
);
943 /* check device status */
944 if (unlikely((status
& ATA_DRQ
) == 0)) {
945 /* handle BSY=0, DRQ=0 as error */
946 if (likely(status
& (ATA_ERR
| ATA_DF
)))
947 /* device stops HSM for abort/error */
948 qc
->err_mask
|= AC_ERR_DEV
;
950 /* HSM violation. Let EH handle this */
951 ata_ehi_push_desc(ehi
,
952 "ST_FIRST: !(DRQ|ERR|DF)");
953 qc
->err_mask
|= AC_ERR_HSM
;
956 ap
->hsm_task_state
= HSM_ST_ERR
;
960 /* Device should not ask for data transfer (DRQ=1)
961 * when it finds something wrong.
962 * We ignore DRQ here and stop the HSM by
963 * changing hsm_task_state to HSM_ST_ERR and
964 * let the EH abort the command or reset the device.
966 if (unlikely(status
& (ATA_ERR
| ATA_DF
))) {
967 /* Some ATAPI tape drives forget to clear the ERR bit
968 * when doing the next command (mostly request sense).
969 * We ignore ERR here to workaround and proceed sending
972 if (!(qc
->dev
->quirks
& ATA_QUIRK_STUCK_ERR
)) {
973 ata_ehi_push_desc(ehi
, "ST_FIRST: "
974 "DRQ=1 with device error, "
975 "dev_stat 0x%X", status
);
976 qc
->err_mask
|= AC_ERR_HSM
;
977 ap
->hsm_task_state
= HSM_ST_ERR
;
982 if (qc
->tf
.protocol
== ATA_PROT_PIO
) {
983 /* PIO data out protocol.
984 * send first data block.
987 /* ata_pio_sectors() might change the state
988 * to HSM_ST_LAST. so, the state is changed here
989 * before ata_pio_sectors().
991 ap
->hsm_task_state
= HSM_ST
;
995 atapi_send_cdb(ap
, qc
);
997 /* if polling, ata_sff_pio_task() handles the rest.
998 * otherwise, interrupt handler takes over from here.
1003 /* complete command or read/write the data register */
1004 if (qc
->tf
.protocol
== ATAPI_PROT_PIO
) {
1005 /* ATAPI PIO protocol */
1006 if ((status
& ATA_DRQ
) == 0) {
1007 /* No more data to transfer or device error.
1008 * Device error will be tagged in HSM_ST_LAST.
1010 ap
->hsm_task_state
= HSM_ST_LAST
;
1014 /* Device should not ask for data transfer (DRQ=1)
1015 * when it finds something wrong.
1016 * We ignore DRQ here and stop the HSM by
1017 * changing hsm_task_state to HSM_ST_ERR and
1018 * let the EH abort the command or reset the device.
1020 if (unlikely(status
& (ATA_ERR
| ATA_DF
))) {
1021 ata_ehi_push_desc(ehi
, "ST-ATAPI: "
1022 "DRQ=1 with device error, "
1023 "dev_stat 0x%X", status
);
1024 qc
->err_mask
|= AC_ERR_HSM
;
1025 ap
->hsm_task_state
= HSM_ST_ERR
;
1029 atapi_pio_bytes(qc
);
1031 if (unlikely(ap
->hsm_task_state
== HSM_ST_ERR
))
1032 /* bad ireason reported by device */
1036 /* ATA PIO protocol */
1037 if (unlikely((status
& ATA_DRQ
) == 0)) {
1038 /* handle BSY=0, DRQ=0 as error */
1039 if (likely(status
& (ATA_ERR
| ATA_DF
))) {
1040 /* device stops HSM for abort/error */
1041 qc
->err_mask
|= AC_ERR_DEV
;
1043 /* If diagnostic failed and this is
1044 * IDENTIFY, it's likely a phantom
1045 * device. Mark hint.
1047 if (qc
->dev
->quirks
&
1048 ATA_QUIRK_DIAGNOSTIC
)
1052 /* HSM violation. Let EH handle this.
1053 * Phantom devices also trigger this
1054 * condition. Mark hint.
1056 ata_ehi_push_desc(ehi
, "ST-ATA: "
1057 "DRQ=0 without device error, "
1058 "dev_stat 0x%X", status
);
1059 qc
->err_mask
|= AC_ERR_HSM
|
1063 ap
->hsm_task_state
= HSM_ST_ERR
;
1067 /* For PIO reads, some devices may ask for
1068 * data transfer (DRQ=1) alone with ERR=1.
1069 * We respect DRQ here and transfer one
1070 * block of junk data before changing the
1071 * hsm_task_state to HSM_ST_ERR.
1073 * For PIO writes, ERR=1 DRQ=1 doesn't make
1074 * sense since the data block has been
1075 * transferred to the device.
1077 if (unlikely(status
& (ATA_ERR
| ATA_DF
))) {
1078 /* data might be corrputed */
1079 qc
->err_mask
|= AC_ERR_DEV
;
1081 if (!(qc
->tf
.flags
& ATA_TFLAG_WRITE
)) {
1082 ata_pio_sectors(qc
);
1083 status
= ata_wait_idle(ap
);
1086 if (status
& (ATA_BUSY
| ATA_DRQ
)) {
1087 ata_ehi_push_desc(ehi
, "ST-ATA: "
1088 "BUSY|DRQ persists on ERR|DF, "
1089 "dev_stat 0x%X", status
);
1090 qc
->err_mask
|= AC_ERR_HSM
;
1093 /* There are oddball controllers with
1094 * status register stuck at 0x7f and
1095 * lbal/m/h at zero which makes it
1096 * pass all other presence detection
1097 * mechanisms we have. Set NODEV_HINT
1098 * for it. Kernel bz#7241.
1101 qc
->err_mask
|= AC_ERR_NODEV_HINT
;
1103 /* ata_pio_sectors() might change the
1104 * state to HSM_ST_LAST. so, the state
1105 * is changed after ata_pio_sectors().
1107 ap
->hsm_task_state
= HSM_ST_ERR
;
1111 ata_pio_sectors(qc
);
1113 if (ap
->hsm_task_state
== HSM_ST_LAST
&&
1114 (!(qc
->tf
.flags
& ATA_TFLAG_WRITE
))) {
1116 status
= ata_wait_idle(ap
);
1125 if (unlikely(!ata_ok(status
))) {
1126 qc
->err_mask
|= __ac_err_mask(status
);
1127 ap
->hsm_task_state
= HSM_ST_ERR
;
1131 /* no more data to transfer */
1132 trace_ata_sff_hsm_command_complete(qc
, status
);
1134 WARN_ON_ONCE(qc
->err_mask
& (AC_ERR_DEV
| AC_ERR_HSM
));
1136 ap
->hsm_task_state
= HSM_ST_IDLE
;
1138 /* complete taskfile transaction */
1139 ata_hsm_qc_complete(qc
, in_wq
);
1145 ap
->hsm_task_state
= HSM_ST_IDLE
;
1147 /* complete taskfile transaction */
1148 ata_hsm_qc_complete(qc
, in_wq
);
1154 WARN(true, "ata%d: SFF host state machine in invalid state %d",
1155 ap
->print_id
, ap
->hsm_task_state
);
1160 EXPORT_SYMBOL_GPL(ata_sff_hsm_move
);
1162 void ata_sff_queue_work(struct work_struct
*work
)
1164 queue_work(ata_sff_wq
, work
);
1166 EXPORT_SYMBOL_GPL(ata_sff_queue_work
);
1168 void ata_sff_queue_delayed_work(struct delayed_work
*dwork
, unsigned long delay
)
1170 queue_delayed_work(ata_sff_wq
, dwork
, delay
);
1172 EXPORT_SYMBOL_GPL(ata_sff_queue_delayed_work
);
1174 void ata_sff_queue_pio_task(struct ata_link
*link
, unsigned long delay
)
1176 struct ata_port
*ap
= link
->ap
;
1178 WARN_ON((ap
->sff_pio_task_link
!= NULL
) &&
1179 (ap
->sff_pio_task_link
!= link
));
1180 ap
->sff_pio_task_link
= link
;
1182 /* may fail if ata_sff_flush_pio_task() in progress */
1183 ata_sff_queue_delayed_work(&ap
->sff_pio_task
, msecs_to_jiffies(delay
));
1185 EXPORT_SYMBOL_GPL(ata_sff_queue_pio_task
);
1187 void ata_sff_flush_pio_task(struct ata_port
*ap
)
1189 trace_ata_sff_flush_pio_task(ap
);
1191 cancel_delayed_work_sync(&ap
->sff_pio_task
);
1194 * We wanna reset the HSM state to IDLE. If we do so without
1195 * grabbing the port lock, critical sections protected by it which
1196 * expect the HSM state to stay stable may get surprised. For
1197 * example, we may set IDLE in between the time
1198 * __ata_sff_port_intr() checks for HSM_ST_IDLE and before it calls
1199 * ata_sff_hsm_move() causing ata_sff_hsm_move() to BUG().
1201 spin_lock_irq(ap
->lock
);
1202 ap
->hsm_task_state
= HSM_ST_IDLE
;
1203 spin_unlock_irq(ap
->lock
);
1205 ap
->sff_pio_task_link
= NULL
;
1208 static void ata_sff_pio_task(struct work_struct
*work
)
1210 struct ata_port
*ap
=
1211 container_of(work
, struct ata_port
, sff_pio_task
.work
);
1212 struct ata_link
*link
= ap
->sff_pio_task_link
;
1213 struct ata_queued_cmd
*qc
;
1217 spin_lock_irq(ap
->lock
);
1219 BUG_ON(ap
->sff_pio_task_link
== NULL
);
1220 /* qc can be NULL if timeout occurred */
1221 qc
= ata_qc_from_tag(ap
, link
->active_tag
);
1223 ap
->sff_pio_task_link
= NULL
;
1228 WARN_ON_ONCE(ap
->hsm_task_state
== HSM_ST_IDLE
);
1231 * This is purely heuristic. This is a fast path.
1232 * Sometimes when we enter, BSY will be cleared in
1233 * a chk-status or two. If not, the drive is probably seeking
1234 * or something. Snooze for a couple msecs, then
1235 * chk-status again. If still busy, queue delayed work.
1237 status
= ata_sff_busy_wait(ap
, ATA_BUSY
, 5);
1238 if (status
& ATA_BUSY
) {
1239 spin_unlock_irq(ap
->lock
);
1241 spin_lock_irq(ap
->lock
);
1243 status
= ata_sff_busy_wait(ap
, ATA_BUSY
, 10);
1244 if (status
& ATA_BUSY
) {
1245 ata_sff_queue_pio_task(link
, ATA_SHORT_PAUSE
);
1251 * hsm_move() may trigger another command to be processed.
1252 * clean the link beforehand.
1254 ap
->sff_pio_task_link
= NULL
;
1256 poll_next
= ata_sff_hsm_move(ap
, qc
, status
, 1);
1258 /* another command or interrupt handler
1259 * may be running at this point.
1264 spin_unlock_irq(ap
->lock
);
1268 * ata_sff_qc_issue - issue taskfile to a SFF controller
1269 * @qc: command to issue to device
1271 * This function issues a PIO or NODATA command to a SFF
1275 * spin_lock_irqsave(host lock)
1278 * Zero on success, AC_ERR_* mask on failure
1280 unsigned int ata_sff_qc_issue(struct ata_queued_cmd
*qc
)
1282 struct ata_port
*ap
= qc
->ap
;
1283 struct ata_link
*link
= qc
->dev
->link
;
1285 /* Use polling pio if the LLD doesn't handle
1286 * interrupt driven pio and atapi CDB interrupt.
1288 if (ap
->flags
& ATA_FLAG_PIO_POLLING
)
1289 qc
->tf
.flags
|= ATA_TFLAG_POLLING
;
1291 /* select the device */
1292 ata_dev_select(ap
, qc
->dev
->devno
, 1, 0);
1294 /* start the command */
1295 switch (qc
->tf
.protocol
) {
1296 case ATA_PROT_NODATA
:
1297 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1298 ata_qc_set_polling(qc
);
1300 ata_tf_to_host(ap
, &qc
->tf
, qc
->tag
);
1301 ap
->hsm_task_state
= HSM_ST_LAST
;
1303 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1304 ata_sff_queue_pio_task(link
, 0);
1309 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1310 ata_qc_set_polling(qc
);
1312 ata_tf_to_host(ap
, &qc
->tf
, qc
->tag
);
1314 if (qc
->tf
.flags
& ATA_TFLAG_WRITE
) {
1315 /* PIO data out protocol */
1316 ap
->hsm_task_state
= HSM_ST_FIRST
;
1317 ata_sff_queue_pio_task(link
, 0);
1319 /* always send first data block using the
1320 * ata_sff_pio_task() codepath.
1323 /* PIO data in protocol */
1324 ap
->hsm_task_state
= HSM_ST
;
1326 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1327 ata_sff_queue_pio_task(link
, 0);
1329 /* if polling, ata_sff_pio_task() handles the
1330 * rest. otherwise, interrupt handler takes
1337 case ATAPI_PROT_PIO
:
1338 case ATAPI_PROT_NODATA
:
1339 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1340 ata_qc_set_polling(qc
);
1342 ata_tf_to_host(ap
, &qc
->tf
, qc
->tag
);
1344 ap
->hsm_task_state
= HSM_ST_FIRST
;
1346 /* send cdb by polling if no cdb interrupt */
1347 if ((!(qc
->dev
->flags
& ATA_DFLAG_CDB_INTR
)) ||
1348 (qc
->tf
.flags
& ATA_TFLAG_POLLING
))
1349 ata_sff_queue_pio_task(link
, 0);
1353 return AC_ERR_SYSTEM
;
1358 EXPORT_SYMBOL_GPL(ata_sff_qc_issue
);
1361 * ata_sff_qc_fill_rtf - fill result TF using ->sff_tf_read
1362 * @qc: qc to fill result TF for
1364 * @qc is finished and result TF needs to be filled. Fill it
1365 * using ->sff_tf_read.
1368 * spin_lock_irqsave(host lock)
1370 void ata_sff_qc_fill_rtf(struct ata_queued_cmd
*qc
)
1372 qc
->ap
->ops
->sff_tf_read(qc
->ap
, &qc
->result_tf
);
1374 EXPORT_SYMBOL_GPL(ata_sff_qc_fill_rtf
);
1376 static unsigned int ata_sff_idle_irq(struct ata_port
*ap
)
1378 ap
->stats
.idle_irq
++;
1381 if ((ap
->stats
.idle_irq
% 1000) == 0) {
1382 ap
->ops
->sff_check_status(ap
);
1383 if (ap
->ops
->sff_irq_clear
)
1384 ap
->ops
->sff_irq_clear(ap
);
1385 ata_port_warn(ap
, "irq trap\n");
1389 return 0; /* irq not handled */
1392 static unsigned int __ata_sff_port_intr(struct ata_port
*ap
,
1393 struct ata_queued_cmd
*qc
,
1398 trace_ata_sff_port_intr(qc
, hsmv_on_idle
);
1400 /* Check whether we are expecting interrupt in this state */
1401 switch (ap
->hsm_task_state
) {
1403 /* Some pre-ATAPI-4 devices assert INTRQ
1404 * at this state when ready to receive CDB.
1407 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
1408 * The flag was turned on only for atapi devices. No
1409 * need to check ata_is_atapi(qc->tf.protocol) again.
1411 if (!(qc
->dev
->flags
& ATA_DFLAG_CDB_INTR
))
1412 return ata_sff_idle_irq(ap
);
1415 return ata_sff_idle_irq(ap
);
1420 /* check main status, clearing INTRQ if needed */
1421 status
= ata_sff_irq_status(ap
);
1422 if (status
& ATA_BUSY
) {
1424 /* BMDMA engine is already stopped, we're screwed */
1425 qc
->err_mask
|= AC_ERR_HSM
;
1426 ap
->hsm_task_state
= HSM_ST_ERR
;
1428 return ata_sff_idle_irq(ap
);
1431 /* clear irq events */
1432 if (ap
->ops
->sff_irq_clear
)
1433 ap
->ops
->sff_irq_clear(ap
);
1435 ata_sff_hsm_move(ap
, qc
, status
, 0);
1437 return 1; /* irq handled */
1441 * ata_sff_port_intr - Handle SFF port interrupt
1442 * @ap: Port on which interrupt arrived (possibly...)
1443 * @qc: Taskfile currently active in engine
1445 * Handle port interrupt for given queued command.
1448 * spin_lock_irqsave(host lock)
1451 * One if interrupt was handled, zero if not (shared irq).
1453 unsigned int ata_sff_port_intr(struct ata_port
*ap
, struct ata_queued_cmd
*qc
)
1455 return __ata_sff_port_intr(ap
, qc
, false);
1457 EXPORT_SYMBOL_GPL(ata_sff_port_intr
);
1459 static inline irqreturn_t
__ata_sff_interrupt(int irq
, void *dev_instance
,
1460 unsigned int (*port_intr
)(struct ata_port
*, struct ata_queued_cmd
*))
1462 struct ata_host
*host
= dev_instance
;
1463 bool retried
= false;
1465 unsigned int handled
, idle
, polling
;
1466 unsigned long flags
;
1468 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
1469 spin_lock_irqsave(&host
->lock
, flags
);
1472 handled
= idle
= polling
= 0;
1473 for (i
= 0; i
< host
->n_ports
; i
++) {
1474 struct ata_port
*ap
= host
->ports
[i
];
1475 struct ata_queued_cmd
*qc
;
1477 qc
= ata_qc_from_tag(ap
, ap
->link
.active_tag
);
1479 if (!(qc
->tf
.flags
& ATA_TFLAG_POLLING
))
1480 handled
|= port_intr(ap
, qc
);
1488 * If no port was expecting IRQ but the controller is actually
1489 * asserting IRQ line, nobody cared will ensue. Check IRQ
1490 * pending status if available and clear spurious IRQ.
1492 if (!handled
&& !retried
) {
1495 for (i
= 0; i
< host
->n_ports
; i
++) {
1496 struct ata_port
*ap
= host
->ports
[i
];
1498 if (polling
& (1 << i
))
1501 if (!ap
->ops
->sff_irq_check
||
1502 !ap
->ops
->sff_irq_check(ap
))
1505 if (idle
& (1 << i
)) {
1506 ap
->ops
->sff_check_status(ap
);
1507 if (ap
->ops
->sff_irq_clear
)
1508 ap
->ops
->sff_irq_clear(ap
);
1510 /* clear INTRQ and check if BUSY cleared */
1511 if (!(ap
->ops
->sff_check_status(ap
) & ATA_BUSY
))
1514 * With command in flight, we can't do
1515 * sff_irq_clear() w/o racing with completion.
1526 spin_unlock_irqrestore(&host
->lock
, flags
);
1528 return IRQ_RETVAL(handled
);
1532 * ata_sff_interrupt - Default SFF ATA host interrupt handler
1533 * @irq: irq line (unused)
1534 * @dev_instance: pointer to our ata_host information structure
1536 * Default interrupt handler for PCI IDE devices. Calls
1537 * ata_sff_port_intr() for each port that is not disabled.
1540 * Obtains host lock during operation.
1543 * IRQ_NONE or IRQ_HANDLED.
1545 irqreturn_t
ata_sff_interrupt(int irq
, void *dev_instance
)
1547 return __ata_sff_interrupt(irq
, dev_instance
, ata_sff_port_intr
);
1549 EXPORT_SYMBOL_GPL(ata_sff_interrupt
);
1552 * ata_sff_lost_interrupt - Check for an apparent lost interrupt
1553 * @ap: port that appears to have timed out
1555 * Called from the libata error handlers when the core code suspects
1556 * an interrupt has been lost. If it has complete anything we can and
1557 * then return. Interface must support altstatus for this faster
1558 * recovery to occur.
1561 * Caller holds host lock
1564 void ata_sff_lost_interrupt(struct ata_port
*ap
)
1567 struct ata_queued_cmd
*qc
;
1569 /* Only one outstanding command per SFF channel */
1570 qc
= ata_qc_from_tag(ap
, ap
->link
.active_tag
);
1571 /* We cannot lose an interrupt on a non-existent or polled command */
1572 if (!qc
|| qc
->tf
.flags
& ATA_TFLAG_POLLING
)
1574 /* See if the controller thinks it is still busy - if so the command
1575 isn't a lost IRQ but is still in progress */
1576 if (WARN_ON_ONCE(!ata_sff_altstatus(ap
, &status
)))
1578 if (status
& ATA_BUSY
)
1581 /* There was a command running, we are no longer busy and we have
1583 ata_port_warn(ap
, "lost interrupt (Status 0x%x)\n", status
);
1584 /* Run the host interrupt logic as if the interrupt had not been
1586 ata_sff_port_intr(ap
, qc
);
1588 EXPORT_SYMBOL_GPL(ata_sff_lost_interrupt
);
1591 * ata_sff_freeze - Freeze SFF controller port
1592 * @ap: port to freeze
1594 * Freeze SFF controller port.
1597 * Inherited from caller.
1599 void ata_sff_freeze(struct ata_port
*ap
)
1601 ap
->ctl
|= ATA_NIEN
;
1602 ap
->last_ctl
= ap
->ctl
;
1604 ata_sff_set_devctl(ap
, ap
->ctl
);
1606 /* Under certain circumstances, some controllers raise IRQ on
1607 * ATA_NIEN manipulation. Also, many controllers fail to mask
1608 * previously pending IRQ on ATA_NIEN assertion. Clear it.
1610 ap
->ops
->sff_check_status(ap
);
1612 if (ap
->ops
->sff_irq_clear
)
1613 ap
->ops
->sff_irq_clear(ap
);
1615 EXPORT_SYMBOL_GPL(ata_sff_freeze
);
1618 * ata_sff_thaw - Thaw SFF controller port
1621 * Thaw SFF controller port.
1624 * Inherited from caller.
1626 void ata_sff_thaw(struct ata_port
*ap
)
1628 /* clear & re-enable interrupts */
1629 ap
->ops
->sff_check_status(ap
);
1630 if (ap
->ops
->sff_irq_clear
)
1631 ap
->ops
->sff_irq_clear(ap
);
1634 EXPORT_SYMBOL_GPL(ata_sff_thaw
);
1637 * ata_sff_prereset - prepare SFF link for reset
1638 * @link: SFF link to be reset
1639 * @deadline: deadline jiffies for the operation
1641 * SFF link @link is about to be reset. Initialize it. It first
1642 * calls ata_std_prereset() and wait for !BSY if the port is
1646 * Kernel thread context (may sleep)
1651 int ata_sff_prereset(struct ata_link
*link
, unsigned long deadline
)
1653 struct ata_eh_context
*ehc
= &link
->eh_context
;
1656 /* The standard prereset is best-effort and always returns 0 */
1657 ata_std_prereset(link
, deadline
);
1659 /* if we're about to do hardreset, nothing more to do */
1660 if (ehc
->i
.action
& ATA_EH_HARDRESET
)
1663 /* wait for !BSY if we don't know that no device is attached */
1664 if (!ata_link_offline(link
)) {
1665 rc
= ata_sff_wait_ready(link
, deadline
);
1666 if (rc
&& rc
!= -ENODEV
) {
1668 "device not ready (errno=%d), forcing hardreset\n",
1670 ehc
->i
.action
|= ATA_EH_HARDRESET
;
1676 EXPORT_SYMBOL_GPL(ata_sff_prereset
);
1679 * ata_devchk - PATA device presence detection
1680 * @ap: ATA channel to examine
1681 * @device: Device to examine (starting at zero)
1683 * This technique was originally described in
1684 * Hale Landis's ATADRVR (www.ata-atapi.com), and
1685 * later found its way into the ATA/ATAPI spec.
1687 * Write a pattern to the ATA shadow registers,
1688 * and if a device is present, it will respond by
1689 * correctly storing and echoing back the
1690 * ATA shadow register contents.
1693 * true if device is present, false if not.
1698 static bool ata_devchk(struct ata_port
*ap
, unsigned int device
)
1700 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1703 ap
->ops
->sff_dev_select(ap
, device
);
1705 iowrite8(0x55, ioaddr
->nsect_addr
);
1706 iowrite8(0xaa, ioaddr
->lbal_addr
);
1708 iowrite8(0xaa, ioaddr
->nsect_addr
);
1709 iowrite8(0x55, ioaddr
->lbal_addr
);
1711 iowrite8(0x55, ioaddr
->nsect_addr
);
1712 iowrite8(0xaa, ioaddr
->lbal_addr
);
1714 nsect
= ioread8(ioaddr
->nsect_addr
);
1715 lbal
= ioread8(ioaddr
->lbal_addr
);
1717 if ((nsect
== 0x55) && (lbal
== 0xaa))
1718 return true; /* we found a device */
1720 return false; /* nothing found */
1724 * ata_sff_dev_classify - Parse returned ATA device signature
1725 * @dev: ATA device to classify (starting at zero)
1726 * @present: device seems present
1727 * @r_err: Value of error register on completion
1729 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
1730 * an ATA/ATAPI-defined set of values is placed in the ATA
1731 * shadow registers, indicating the results of device detection
1734 * Select the ATA device, and read the values from the ATA shadow
1735 * registers. Then parse according to the Error register value,
1736 * and the spec-defined values examined by ata_dev_classify().
1742 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
1744 unsigned int ata_sff_dev_classify(struct ata_device
*dev
, int present
,
1747 struct ata_port
*ap
= dev
->link
->ap
;
1748 struct ata_taskfile tf
;
1752 ap
->ops
->sff_dev_select(ap
, dev
->devno
);
1754 memset(&tf
, 0, sizeof(tf
));
1756 ap
->ops
->sff_tf_read(ap
, &tf
);
1761 /* see if device passed diags: continue and warn later */
1763 /* diagnostic fail : do nothing _YET_ */
1764 dev
->quirks
|= ATA_QUIRK_DIAGNOSTIC
;
1767 else if ((dev
->devno
== 0) && (err
== 0x81))
1770 return ATA_DEV_NONE
;
1772 /* determine if device is ATA or ATAPI */
1773 class = ata_port_classify(ap
, &tf
);
1775 case ATA_DEV_UNKNOWN
:
1777 * If the device failed diagnostic, it's likely to
1778 * have reported incorrect device signature too.
1779 * Assume ATA device if the device seems present but
1780 * device signature is invalid with diagnostic
1783 if (present
&& (dev
->quirks
& ATA_QUIRK_DIAGNOSTIC
))
1784 class = ATA_DEV_ATA
;
1786 class = ATA_DEV_NONE
;
1789 if (ap
->ops
->sff_check_status(ap
) == 0)
1790 class = ATA_DEV_NONE
;
1795 EXPORT_SYMBOL_GPL(ata_sff_dev_classify
);
1798 * ata_sff_wait_after_reset - wait for devices to become ready after reset
1799 * @link: SFF link which is just reset
1800 * @devmask: mask of present devices
1801 * @deadline: deadline jiffies for the operation
1803 * Wait devices attached to SFF @link to become ready after
1804 * reset. It contains preceding 150ms wait to avoid accessing TF
1805 * status register too early.
1808 * Kernel thread context (may sleep).
1811 * 0 on success, -ENODEV if some or all of devices in @devmask
1812 * don't seem to exist. -errno on other errors.
1814 int ata_sff_wait_after_reset(struct ata_link
*link
, unsigned int devmask
,
1815 unsigned long deadline
)
1817 struct ata_port
*ap
= link
->ap
;
1818 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1819 unsigned int dev0
= devmask
& (1 << 0);
1820 unsigned int dev1
= devmask
& (1 << 1);
1823 ata_msleep(ap
, ATA_WAIT_AFTER_RESET
);
1825 /* always check readiness of the master device */
1826 rc
= ata_sff_wait_ready(link
, deadline
);
1827 /* -ENODEV means the odd clown forgot the D7 pulldown resistor
1828 * and TF status is 0xff, bail out on it too.
1833 /* if device 1 was found in ata_devchk, wait for register
1834 * access briefly, then wait for BSY to clear.
1839 ap
->ops
->sff_dev_select(ap
, 1);
1841 /* Wait for register access. Some ATAPI devices fail
1842 * to set nsect/lbal after reset, so don't waste too
1843 * much time on it. We're gonna wait for !BSY anyway.
1845 for (i
= 0; i
< 2; i
++) {
1848 nsect
= ioread8(ioaddr
->nsect_addr
);
1849 lbal
= ioread8(ioaddr
->lbal_addr
);
1850 if ((nsect
== 1) && (lbal
== 1))
1852 ata_msleep(ap
, 50); /* give drive a breather */
1855 rc
= ata_sff_wait_ready(link
, deadline
);
1863 /* is all this really necessary? */
1864 ap
->ops
->sff_dev_select(ap
, 0);
1866 ap
->ops
->sff_dev_select(ap
, 1);
1868 ap
->ops
->sff_dev_select(ap
, 0);
1872 EXPORT_SYMBOL_GPL(ata_sff_wait_after_reset
);
1874 static int ata_bus_softreset(struct ata_port
*ap
, unsigned int devmask
,
1875 unsigned long deadline
)
1877 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1879 if (ap
->ioaddr
.ctl_addr
) {
1880 /* software reset. causes dev0 to be selected */
1881 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
1882 udelay(20); /* FIXME: flush */
1883 iowrite8(ap
->ctl
| ATA_SRST
, ioaddr
->ctl_addr
);
1884 udelay(20); /* FIXME: flush */
1885 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
1886 ap
->last_ctl
= ap
->ctl
;
1889 /* wait the port to become ready */
1890 return ata_sff_wait_after_reset(&ap
->link
, devmask
, deadline
);
1894 * ata_sff_softreset - reset host port via ATA SRST
1895 * @link: ATA link to reset
1896 * @classes: resulting classes of attached devices
1897 * @deadline: deadline jiffies for the operation
1899 * Reset host port using ATA SRST.
1902 * Kernel thread context (may sleep)
1905 * 0 on success, -errno otherwise.
1907 int ata_sff_softreset(struct ata_link
*link
, unsigned int *classes
,
1908 unsigned long deadline
)
1910 struct ata_port
*ap
= link
->ap
;
1911 unsigned int slave_possible
= ap
->flags
& ATA_FLAG_SLAVE_POSS
;
1912 unsigned int devmask
= 0;
1916 /* determine if device 0/1 are present */
1917 if (ata_devchk(ap
, 0))
1918 devmask
|= (1 << 0);
1919 if (slave_possible
&& ata_devchk(ap
, 1))
1920 devmask
|= (1 << 1);
1922 /* select device 0 again */
1923 ap
->ops
->sff_dev_select(ap
, 0);
1925 /* issue bus reset */
1926 rc
= ata_bus_softreset(ap
, devmask
, deadline
);
1927 /* if link is occupied, -ENODEV too is an error */
1928 if (rc
&& (rc
!= -ENODEV
|| sata_scr_valid(link
))) {
1929 ata_link_err(link
, "SRST failed (errno=%d)\n", rc
);
1933 /* determine by signature whether we have ATA or ATAPI devices */
1934 classes
[0] = ata_sff_dev_classify(&link
->device
[0],
1935 devmask
& (1 << 0), &err
);
1936 if (slave_possible
&& err
!= 0x81)
1937 classes
[1] = ata_sff_dev_classify(&link
->device
[1],
1938 devmask
& (1 << 1), &err
);
1942 EXPORT_SYMBOL_GPL(ata_sff_softreset
);
1945 * sata_sff_hardreset - reset host port via SATA phy reset
1946 * @link: link to reset
1947 * @class: resulting class of attached device
1948 * @deadline: deadline jiffies for the operation
1950 * SATA phy-reset host port using DET bits of SControl register,
1951 * wait for !BSY and classify the attached device.
1954 * Kernel thread context (may sleep)
1957 * 0 on success, -errno otherwise.
1959 int sata_sff_hardreset(struct ata_link
*link
, unsigned int *class,
1960 unsigned long deadline
)
1962 struct ata_eh_context
*ehc
= &link
->eh_context
;
1963 const unsigned int *timing
= sata_ehc_deb_timing(ehc
);
1967 rc
= sata_link_hardreset(link
, timing
, deadline
, &online
,
1968 ata_sff_check_ready
);
1970 *class = ata_sff_dev_classify(link
->device
, 1, NULL
);
1974 EXPORT_SYMBOL_GPL(sata_sff_hardreset
);
1977 * ata_sff_postreset - SFF postreset callback
1978 * @link: the target SFF ata_link
1979 * @classes: classes of attached devices
1981 * This function is invoked after a successful reset. It first
1982 * calls ata_std_postreset() and performs SFF specific postreset
1986 * Kernel thread context (may sleep)
1988 void ata_sff_postreset(struct ata_link
*link
, unsigned int *classes
)
1990 struct ata_port
*ap
= link
->ap
;
1992 ata_std_postreset(link
, classes
);
1994 /* is double-select really necessary? */
1995 if (classes
[0] != ATA_DEV_NONE
)
1996 ap
->ops
->sff_dev_select(ap
, 1);
1997 if (classes
[1] != ATA_DEV_NONE
)
1998 ap
->ops
->sff_dev_select(ap
, 0);
2000 /* bail out if no device is present */
2001 if (classes
[0] == ATA_DEV_NONE
&& classes
[1] == ATA_DEV_NONE
)
2004 /* set up device control */
2005 if (ata_sff_set_devctl(ap
, ap
->ctl
))
2006 ap
->last_ctl
= ap
->ctl
;
2008 EXPORT_SYMBOL_GPL(ata_sff_postreset
);
2011 * ata_sff_drain_fifo - Stock FIFO drain logic for SFF controllers
2014 * Drain the FIFO and device of any stuck data following a command
2015 * failing to complete. In some cases this is necessary before a
2016 * reset will recover the device.
2020 void ata_sff_drain_fifo(struct ata_queued_cmd
*qc
)
2023 struct ata_port
*ap
;
2025 /* We only need to flush incoming data when a command was running */
2026 if (qc
== NULL
|| qc
->dma_dir
== DMA_TO_DEVICE
)
2030 /* Drain up to 64K of data before we give up this recovery method */
2031 for (count
= 0; (ap
->ops
->sff_check_status(ap
) & ATA_DRQ
)
2032 && count
< 65536; count
+= 2)
2033 ioread16(ap
->ioaddr
.data_addr
);
2036 ata_port_dbg(ap
, "drained %d bytes to clear DRQ\n", count
);
2039 EXPORT_SYMBOL_GPL(ata_sff_drain_fifo
);
2042 * ata_sff_error_handler - Stock error handler for SFF controller
2043 * @ap: port to handle error for
2045 * Stock error handler for SFF controller. It can handle both
2046 * PATA and SATA controllers. Many controllers should be able to
2047 * use this EH as-is or with some added handling before and
2051 * Kernel thread context (may sleep)
2053 void ata_sff_error_handler(struct ata_port
*ap
)
2055 ata_reset_fn_t softreset
= ap
->ops
->softreset
;
2056 ata_reset_fn_t hardreset
= ap
->ops
->hardreset
;
2057 struct ata_queued_cmd
*qc
;
2058 unsigned long flags
;
2060 qc
= __ata_qc_from_tag(ap
, ap
->link
.active_tag
);
2061 if (qc
&& !(qc
->flags
& ATA_QCFLAG_EH
))
2064 spin_lock_irqsave(ap
->lock
, flags
);
2067 * We *MUST* do FIFO draining before we issue a reset as
2068 * several devices helpfully clear their internal state and
2069 * will lock solid if we touch the data port post reset. Pass
2070 * qc in case anyone wants to do different PIO/DMA recovery or
2071 * has per command fixups
2073 if (ap
->ops
->sff_drain_fifo
)
2074 ap
->ops
->sff_drain_fifo(qc
);
2076 spin_unlock_irqrestore(ap
->lock
, flags
);
2078 /* ignore built-in hardresets if SCR access is not available */
2079 if ((hardreset
== sata_std_hardreset
||
2080 hardreset
== sata_sff_hardreset
) && !sata_scr_valid(&ap
->link
))
2083 ata_do_eh(ap
, ap
->ops
->prereset
, softreset
, hardreset
,
2084 ap
->ops
->postreset
);
2086 EXPORT_SYMBOL_GPL(ata_sff_error_handler
);
2089 * ata_sff_std_ports - initialize ioaddr with standard port offsets.
2090 * @ioaddr: IO address structure to be initialized
2092 * Utility function which initializes data_addr, error_addr,
2093 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
2094 * device_addr, status_addr, and command_addr to standard offsets
2095 * relative to cmd_addr.
2097 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
2099 void ata_sff_std_ports(struct ata_ioports
*ioaddr
)
2101 ioaddr
->data_addr
= ioaddr
->cmd_addr
+ ATA_REG_DATA
;
2102 ioaddr
->error_addr
= ioaddr
->cmd_addr
+ ATA_REG_ERR
;
2103 ioaddr
->feature_addr
= ioaddr
->cmd_addr
+ ATA_REG_FEATURE
;
2104 ioaddr
->nsect_addr
= ioaddr
->cmd_addr
+ ATA_REG_NSECT
;
2105 ioaddr
->lbal_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAL
;
2106 ioaddr
->lbam_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAM
;
2107 ioaddr
->lbah_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAH
;
2108 ioaddr
->device_addr
= ioaddr
->cmd_addr
+ ATA_REG_DEVICE
;
2109 ioaddr
->status_addr
= ioaddr
->cmd_addr
+ ATA_REG_STATUS
;
2110 ioaddr
->command_addr
= ioaddr
->cmd_addr
+ ATA_REG_CMD
;
2112 EXPORT_SYMBOL_GPL(ata_sff_std_ports
);
2116 static bool ata_resources_present(struct pci_dev
*pdev
, int port
)
2120 /* Check the PCI resources for this channel are enabled */
2122 for (i
= 0; i
< 2; i
++) {
2123 if (pci_resource_start(pdev
, port
+ i
) == 0 ||
2124 pci_resource_len(pdev
, port
+ i
) == 0)
2131 * ata_pci_sff_init_host - acquire native PCI ATA resources and init host
2132 * @host: target ATA host
2134 * Acquire native PCI ATA resources for @host and initialize the
2135 * first two ports of @host accordingly. Ports marked dummy are
2136 * skipped and allocation failure makes the port dummy.
2138 * Note that native PCI resources are valid even for legacy hosts
2139 * as we fix up pdev resources array early in boot, so this
2140 * function can be used for both native and legacy SFF hosts.
2143 * Inherited from calling layer (may sleep).
2146 * 0 if at least one port is initialized, -ENODEV if no port is
2149 int ata_pci_sff_init_host(struct ata_host
*host
)
2151 struct device
*gdev
= host
->dev
;
2152 struct pci_dev
*pdev
= to_pci_dev(gdev
);
2153 unsigned int mask
= 0;
2156 /* request, iomap BARs and init port addresses accordingly */
2157 for (i
= 0; i
< 2; i
++) {
2158 struct ata_port
*ap
= host
->ports
[i
];
2160 void __iomem
* const *iomap
;
2162 if (ata_port_is_dummy(ap
))
2165 /* Discard disabled ports. Some controllers show
2166 * their unused channels this way. Disabled ports are
2169 if (!ata_resources_present(pdev
, i
)) {
2170 ap
->ops
= &ata_dummy_port_ops
;
2174 rc
= pcim_iomap_regions(pdev
, 0x3 << base
,
2175 dev_driver_string(gdev
));
2178 "failed to request/iomap BARs for port %d (errno=%d)\n",
2181 pcim_pin_device(pdev
);
2182 ap
->ops
= &ata_dummy_port_ops
;
2185 host
->iomap
= iomap
= pcim_iomap_table(pdev
);
2187 ap
->ioaddr
.cmd_addr
= iomap
[base
];
2188 ap
->ioaddr
.altstatus_addr
=
2189 ap
->ioaddr
.ctl_addr
= (void __iomem
*)
2190 ((unsigned long)iomap
[base
+ 1] | ATA_PCI_CTL_OFS
);
2191 ata_sff_std_ports(&ap
->ioaddr
);
2193 ata_port_desc(ap
, "cmd 0x%llx ctl 0x%llx",
2194 (unsigned long long)pci_resource_start(pdev
, base
),
2195 (unsigned long long)pci_resource_start(pdev
, base
+ 1));
2201 dev_err(gdev
, "no available native port\n");
2207 EXPORT_SYMBOL_GPL(ata_pci_sff_init_host
);
2210 * ata_pci_sff_prepare_host - helper to prepare PCI PIO-only SFF ATA host
2211 * @pdev: target PCI device
2212 * @ppi: array of port_info, must be enough for two ports
2213 * @r_host: out argument for the initialized ATA host
2215 * Helper to allocate PIO-only SFF ATA host for @pdev, acquire
2216 * all PCI resources and initialize it accordingly in one go.
2219 * Inherited from calling layer (may sleep).
2222 * 0 on success, -errno otherwise.
2224 int ata_pci_sff_prepare_host(struct pci_dev
*pdev
,
2225 const struct ata_port_info
* const *ppi
,
2226 struct ata_host
**r_host
)
2228 struct ata_host
*host
;
2231 if (!devres_open_group(&pdev
->dev
, NULL
, GFP_KERNEL
))
2234 host
= ata_host_alloc_pinfo(&pdev
->dev
, ppi
, 2);
2236 dev_err(&pdev
->dev
, "failed to allocate ATA host\n");
2241 rc
= ata_pci_sff_init_host(host
);
2245 devres_remove_group(&pdev
->dev
, NULL
);
2250 devres_release_group(&pdev
->dev
, NULL
);
2253 EXPORT_SYMBOL_GPL(ata_pci_sff_prepare_host
);
2256 * ata_pci_sff_activate_host - start SFF host, request IRQ and register it
2257 * @host: target SFF ATA host
2258 * @irq_handler: irq_handler used when requesting IRQ(s)
2259 * @sht: scsi_host_template to use when registering the host
2261 * This is the counterpart of ata_host_activate() for SFF ATA
2262 * hosts. This separate helper is necessary because SFF hosts
2263 * use two separate interrupts in legacy mode.
2266 * Inherited from calling layer (may sleep).
2269 * 0 on success, -errno otherwise.
2271 int ata_pci_sff_activate_host(struct ata_host
*host
,
2272 irq_handler_t irq_handler
,
2273 const struct scsi_host_template
*sht
)
2275 struct device
*dev
= host
->dev
;
2276 struct pci_dev
*pdev
= to_pci_dev(dev
);
2277 const char *drv_name
= dev_driver_string(host
->dev
);
2278 int legacy_mode
= 0, rc
;
2280 rc
= ata_host_start(host
);
2284 if ((pdev
->class >> 8) == PCI_CLASS_STORAGE_IDE
) {
2288 * ATA spec says we should use legacy mode when one
2289 * port is in legacy mode, but disabled ports on some
2290 * PCI hosts appear as fixed legacy ports, e.g SB600/700
2291 * on which the secondary port is not wired, so
2292 * ignore ports that are marked as 'dummy' during
2295 pci_read_config_byte(pdev
, PCI_CLASS_PROG
, &tmp8
);
2296 if (!ata_port_is_dummy(host
->ports
[0]))
2298 if (!ata_port_is_dummy(host
->ports
[1]))
2300 if ((tmp8
& mask
) != mask
)
2304 if (!devres_open_group(dev
, NULL
, GFP_KERNEL
))
2307 if (!legacy_mode
&& pdev
->irq
) {
2310 rc
= devm_request_irq(dev
, pdev
->irq
, irq_handler
,
2311 IRQF_SHARED
, drv_name
, host
);
2315 for (i
= 0; i
< 2; i
++) {
2316 if (ata_port_is_dummy(host
->ports
[i
]))
2318 ata_port_desc_misc(host
->ports
[i
], pdev
->irq
);
2320 } else if (legacy_mode
) {
2321 if (!ata_port_is_dummy(host
->ports
[0])) {
2322 rc
= devm_request_irq(dev
, ATA_PRIMARY_IRQ(pdev
),
2323 irq_handler
, IRQF_SHARED
,
2328 ata_port_desc_misc(host
->ports
[0],
2329 ATA_PRIMARY_IRQ(pdev
));
2332 if (!ata_port_is_dummy(host
->ports
[1])) {
2333 rc
= devm_request_irq(dev
, ATA_SECONDARY_IRQ(pdev
),
2334 irq_handler
, IRQF_SHARED
,
2339 ata_port_desc_misc(host
->ports
[1],
2340 ATA_SECONDARY_IRQ(pdev
));
2344 rc
= ata_host_register(host
, sht
);
2347 devres_remove_group(dev
, NULL
);
2349 devres_release_group(dev
, NULL
);
2353 EXPORT_SYMBOL_GPL(ata_pci_sff_activate_host
);
2355 static const struct ata_port_info
*ata_sff_find_valid_pi(
2356 const struct ata_port_info
* const *ppi
)
2360 /* look up the first valid port_info */
2361 for (i
= 0; i
< 2 && ppi
[i
]; i
++)
2362 if (ppi
[i
]->port_ops
!= &ata_dummy_port_ops
)
2368 static int ata_pci_init_one(struct pci_dev
*pdev
,
2369 const struct ata_port_info
* const *ppi
,
2370 const struct scsi_host_template
*sht
, void *host_priv
,
2371 int hflags
, bool bmdma
)
2373 struct device
*dev
= &pdev
->dev
;
2374 const struct ata_port_info
*pi
;
2375 struct ata_host
*host
= NULL
;
2378 pi
= ata_sff_find_valid_pi(ppi
);
2380 dev_err(&pdev
->dev
, "no valid port_info specified\n");
2384 if (!devres_open_group(dev
, NULL
, GFP_KERNEL
))
2387 rc
= pcim_enable_device(pdev
);
2391 #ifdef CONFIG_ATA_BMDMA
2393 /* prepare and activate BMDMA host */
2394 rc
= ata_pci_bmdma_prepare_host(pdev
, ppi
, &host
);
2397 /* prepare and activate SFF host */
2398 rc
= ata_pci_sff_prepare_host(pdev
, ppi
, &host
);
2401 host
->private_data
= host_priv
;
2402 host
->flags
|= hflags
;
2404 #ifdef CONFIG_ATA_BMDMA
2406 pci_set_master(pdev
);
2407 rc
= ata_pci_sff_activate_host(host
, ata_bmdma_interrupt
, sht
);
2410 rc
= ata_pci_sff_activate_host(host
, ata_sff_interrupt
, sht
);
2413 devres_remove_group(&pdev
->dev
, NULL
);
2415 devres_release_group(&pdev
->dev
, NULL
);
2421 * ata_pci_sff_init_one - Initialize/register PIO-only PCI IDE controller
2422 * @pdev: Controller to be initialized
2423 * @ppi: array of port_info, must be enough for two ports
2424 * @sht: scsi_host_template to use when registering the host
2425 * @host_priv: host private_data
2426 * @hflag: host flags
2428 * This is a helper function which can be called from a driver's
2429 * xxx_init_one() probe function if the hardware uses traditional
2430 * IDE taskfile registers and is PIO only.
2433 * Nobody makes a single channel controller that appears solely as
2434 * the secondary legacy port on PCI.
2437 * Inherited from PCI layer (may sleep).
2440 * Zero on success, negative on errno-based value on error.
2442 int ata_pci_sff_init_one(struct pci_dev
*pdev
,
2443 const struct ata_port_info
* const *ppi
,
2444 const struct scsi_host_template
*sht
, void *host_priv
, int hflag
)
2446 return ata_pci_init_one(pdev
, ppi
, sht
, host_priv
, hflag
, 0);
2448 EXPORT_SYMBOL_GPL(ata_pci_sff_init_one
);
2450 #endif /* CONFIG_PCI */
2456 #ifdef CONFIG_ATA_BMDMA
2458 const struct ata_port_operations ata_bmdma_port_ops
= {
2459 .inherits
= &ata_sff_port_ops
,
2461 .error_handler
= ata_bmdma_error_handler
,
2462 .post_internal_cmd
= ata_bmdma_post_internal_cmd
,
2464 .qc_prep
= ata_bmdma_qc_prep
,
2465 .qc_issue
= ata_bmdma_qc_issue
,
2467 .sff_irq_clear
= ata_bmdma_irq_clear
,
2468 .bmdma_setup
= ata_bmdma_setup
,
2469 .bmdma_start
= ata_bmdma_start
,
2470 .bmdma_stop
= ata_bmdma_stop
,
2471 .bmdma_status
= ata_bmdma_status
,
2473 .port_start
= ata_bmdma_port_start
,
2475 EXPORT_SYMBOL_GPL(ata_bmdma_port_ops
);
2477 const struct ata_port_operations ata_bmdma32_port_ops
= {
2478 .inherits
= &ata_bmdma_port_ops
,
2480 .sff_data_xfer
= ata_sff_data_xfer32
,
2481 .port_start
= ata_bmdma_port_start32
,
2483 EXPORT_SYMBOL_GPL(ata_bmdma32_port_ops
);
2486 * ata_bmdma_fill_sg - Fill PCI IDE PRD table
2487 * @qc: Metadata associated with taskfile to be transferred
2489 * Fill PCI IDE PRD (scatter-gather) table with segments
2490 * associated with the current disk command.
2493 * spin_lock_irqsave(host lock)
2496 static void ata_bmdma_fill_sg(struct ata_queued_cmd
*qc
)
2498 struct ata_port
*ap
= qc
->ap
;
2499 struct ata_bmdma_prd
*prd
= ap
->bmdma_prd
;
2500 struct scatterlist
*sg
;
2501 unsigned int si
, pi
;
2504 for_each_sg(qc
->sg
, sg
, qc
->n_elem
, si
) {
2508 /* determine if physical DMA addr spans 64K boundary.
2509 * Note h/w doesn't support 64-bit, so we unconditionally
2510 * truncate dma_addr_t to u32.
2512 addr
= (u32
) sg_dma_address(sg
);
2513 sg_len
= sg_dma_len(sg
);
2516 offset
= addr
& 0xffff;
2518 if ((offset
+ sg_len
) > 0x10000)
2519 len
= 0x10000 - offset
;
2521 prd
[pi
].addr
= cpu_to_le32(addr
);
2522 prd
[pi
].flags_len
= cpu_to_le32(len
& 0xffff);
2530 prd
[pi
- 1].flags_len
|= cpu_to_le32(ATA_PRD_EOT
);
2534 * ata_bmdma_fill_sg_dumb - Fill PCI IDE PRD table
2535 * @qc: Metadata associated with taskfile to be transferred
2537 * Fill PCI IDE PRD (scatter-gather) table with segments
2538 * associated with the current disk command. Perform the fill
2539 * so that we avoid writing any length 64K records for
2540 * controllers that don't follow the spec.
2543 * spin_lock_irqsave(host lock)
2546 static void ata_bmdma_fill_sg_dumb(struct ata_queued_cmd
*qc
)
2548 struct ata_port
*ap
= qc
->ap
;
2549 struct ata_bmdma_prd
*prd
= ap
->bmdma_prd
;
2550 struct scatterlist
*sg
;
2551 unsigned int si
, pi
;
2554 for_each_sg(qc
->sg
, sg
, qc
->n_elem
, si
) {
2556 u32 sg_len
, len
, blen
;
2558 /* determine if physical DMA addr spans 64K boundary.
2559 * Note h/w doesn't support 64-bit, so we unconditionally
2560 * truncate dma_addr_t to u32.
2562 addr
= (u32
) sg_dma_address(sg
);
2563 sg_len
= sg_dma_len(sg
);
2566 offset
= addr
& 0xffff;
2568 if ((offset
+ sg_len
) > 0x10000)
2569 len
= 0x10000 - offset
;
2571 blen
= len
& 0xffff;
2572 prd
[pi
].addr
= cpu_to_le32(addr
);
2574 /* Some PATA chipsets like the CS5530 can't
2575 cope with 0x0000 meaning 64K as the spec
2577 prd
[pi
].flags_len
= cpu_to_le32(0x8000);
2579 prd
[++pi
].addr
= cpu_to_le32(addr
+ 0x8000);
2581 prd
[pi
].flags_len
= cpu_to_le32(blen
);
2589 prd
[pi
- 1].flags_len
|= cpu_to_le32(ATA_PRD_EOT
);
2593 * ata_bmdma_qc_prep - Prepare taskfile for submission
2594 * @qc: Metadata associated with taskfile to be prepared
2596 * Prepare ATA taskfile for submission.
2599 * spin_lock_irqsave(host lock)
2601 enum ata_completion_errors
ata_bmdma_qc_prep(struct ata_queued_cmd
*qc
)
2603 if (!(qc
->flags
& ATA_QCFLAG_DMAMAP
))
2606 ata_bmdma_fill_sg(qc
);
2610 EXPORT_SYMBOL_GPL(ata_bmdma_qc_prep
);
2613 * ata_bmdma_dumb_qc_prep - Prepare taskfile for submission
2614 * @qc: Metadata associated with taskfile to be prepared
2616 * Prepare ATA taskfile for submission.
2619 * spin_lock_irqsave(host lock)
2621 enum ata_completion_errors
ata_bmdma_dumb_qc_prep(struct ata_queued_cmd
*qc
)
2623 if (!(qc
->flags
& ATA_QCFLAG_DMAMAP
))
2626 ata_bmdma_fill_sg_dumb(qc
);
2630 EXPORT_SYMBOL_GPL(ata_bmdma_dumb_qc_prep
);
2633 * ata_bmdma_qc_issue - issue taskfile to a BMDMA controller
2634 * @qc: command to issue to device
2636 * This function issues a PIO, NODATA or DMA command to a
2637 * SFF/BMDMA controller. PIO and NODATA are handled by
2638 * ata_sff_qc_issue().
2641 * spin_lock_irqsave(host lock)
2644 * Zero on success, AC_ERR_* mask on failure
2646 unsigned int ata_bmdma_qc_issue(struct ata_queued_cmd
*qc
)
2648 struct ata_port
*ap
= qc
->ap
;
2649 struct ata_link
*link
= qc
->dev
->link
;
2651 /* defer PIO handling to sff_qc_issue */
2652 if (!ata_is_dma(qc
->tf
.protocol
))
2653 return ata_sff_qc_issue(qc
);
2655 /* select the device */
2656 ata_dev_select(ap
, qc
->dev
->devno
, 1, 0);
2658 /* start the command */
2659 switch (qc
->tf
.protocol
) {
2661 WARN_ON_ONCE(qc
->tf
.flags
& ATA_TFLAG_POLLING
);
2663 trace_ata_tf_load(ap
, &qc
->tf
);
2664 ap
->ops
->sff_tf_load(ap
, &qc
->tf
); /* load tf registers */
2665 trace_ata_bmdma_setup(ap
, &qc
->tf
, qc
->tag
);
2666 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
2667 trace_ata_bmdma_start(ap
, &qc
->tf
, qc
->tag
);
2668 ap
->ops
->bmdma_start(qc
); /* initiate bmdma */
2669 ap
->hsm_task_state
= HSM_ST_LAST
;
2672 case ATAPI_PROT_DMA
:
2673 WARN_ON_ONCE(qc
->tf
.flags
& ATA_TFLAG_POLLING
);
2675 trace_ata_tf_load(ap
, &qc
->tf
);
2676 ap
->ops
->sff_tf_load(ap
, &qc
->tf
); /* load tf registers */
2677 trace_ata_bmdma_setup(ap
, &qc
->tf
, qc
->tag
);
2678 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
2679 ap
->hsm_task_state
= HSM_ST_FIRST
;
2681 /* send cdb by polling if no cdb interrupt */
2682 if (!(qc
->dev
->flags
& ATA_DFLAG_CDB_INTR
))
2683 ata_sff_queue_pio_task(link
, 0);
2688 return AC_ERR_SYSTEM
;
2693 EXPORT_SYMBOL_GPL(ata_bmdma_qc_issue
);
2696 * ata_bmdma_port_intr - Handle BMDMA port interrupt
2697 * @ap: Port on which interrupt arrived (possibly...)
2698 * @qc: Taskfile currently active in engine
2700 * Handle port interrupt for given queued command.
2703 * spin_lock_irqsave(host lock)
2706 * One if interrupt was handled, zero if not (shared irq).
2708 unsigned int ata_bmdma_port_intr(struct ata_port
*ap
, struct ata_queued_cmd
*qc
)
2710 struct ata_eh_info
*ehi
= &ap
->link
.eh_info
;
2712 bool bmdma_stopped
= false;
2713 unsigned int handled
;
2715 if (ap
->hsm_task_state
== HSM_ST_LAST
&& ata_is_dma(qc
->tf
.protocol
)) {
2716 /* check status of DMA engine */
2717 host_stat
= ap
->ops
->bmdma_status(ap
);
2718 trace_ata_bmdma_status(ap
, host_stat
);
2720 /* if it's not our irq... */
2721 if (!(host_stat
& ATA_DMA_INTR
))
2722 return ata_sff_idle_irq(ap
);
2724 /* before we do anything else, clear DMA-Start bit */
2725 trace_ata_bmdma_stop(ap
, &qc
->tf
, qc
->tag
);
2726 ap
->ops
->bmdma_stop(qc
);
2727 bmdma_stopped
= true;
2729 if (unlikely(host_stat
& ATA_DMA_ERR
)) {
2730 /* error when transferring data to/from memory */
2731 qc
->err_mask
|= AC_ERR_HOST_BUS
;
2732 ap
->hsm_task_state
= HSM_ST_ERR
;
2736 handled
= __ata_sff_port_intr(ap
, qc
, bmdma_stopped
);
2738 if (unlikely(qc
->err_mask
) && ata_is_dma(qc
->tf
.protocol
))
2739 ata_ehi_push_desc(ehi
, "BMDMA stat 0x%x", host_stat
);
2743 EXPORT_SYMBOL_GPL(ata_bmdma_port_intr
);
2746 * ata_bmdma_interrupt - Default BMDMA ATA host interrupt handler
2747 * @irq: irq line (unused)
2748 * @dev_instance: pointer to our ata_host information structure
2750 * Default interrupt handler for PCI IDE devices. Calls
2751 * ata_bmdma_port_intr() for each port that is not disabled.
2754 * Obtains host lock during operation.
2757 * IRQ_NONE or IRQ_HANDLED.
2759 irqreturn_t
ata_bmdma_interrupt(int irq
, void *dev_instance
)
2761 return __ata_sff_interrupt(irq
, dev_instance
, ata_bmdma_port_intr
);
2763 EXPORT_SYMBOL_GPL(ata_bmdma_interrupt
);
2766 * ata_bmdma_error_handler - Stock error handler for BMDMA controller
2767 * @ap: port to handle error for
2769 * Stock error handler for BMDMA controller. It can handle both
2770 * PATA and SATA controllers. Most BMDMA controllers should be
2771 * able to use this EH as-is or with some added handling before
2775 * Kernel thread context (may sleep)
2777 void ata_bmdma_error_handler(struct ata_port
*ap
)
2779 struct ata_queued_cmd
*qc
;
2780 unsigned long flags
;
2783 qc
= __ata_qc_from_tag(ap
, ap
->link
.active_tag
);
2784 if (qc
&& !(qc
->flags
& ATA_QCFLAG_EH
))
2787 /* reset PIO HSM and stop DMA engine */
2788 spin_lock_irqsave(ap
->lock
, flags
);
2790 if (qc
&& ata_is_dma(qc
->tf
.protocol
)) {
2793 host_stat
= ap
->ops
->bmdma_status(ap
);
2794 trace_ata_bmdma_status(ap
, host_stat
);
2796 /* BMDMA controllers indicate host bus error by
2797 * setting DMA_ERR bit and timing out. As it wasn't
2798 * really a timeout event, adjust error mask and
2799 * cancel frozen state.
2801 if (qc
->err_mask
== AC_ERR_TIMEOUT
&& (host_stat
& ATA_DMA_ERR
)) {
2802 qc
->err_mask
= AC_ERR_HOST_BUS
;
2806 trace_ata_bmdma_stop(ap
, &qc
->tf
, qc
->tag
);
2807 ap
->ops
->bmdma_stop(qc
);
2809 /* if we're gonna thaw, make sure IRQ is clear */
2811 ap
->ops
->sff_check_status(ap
);
2812 if (ap
->ops
->sff_irq_clear
)
2813 ap
->ops
->sff_irq_clear(ap
);
2817 spin_unlock_irqrestore(ap
->lock
, flags
);
2820 ata_eh_thaw_port(ap
);
2822 ata_sff_error_handler(ap
);
2824 EXPORT_SYMBOL_GPL(ata_bmdma_error_handler
);
2827 * ata_bmdma_post_internal_cmd - Stock post_internal_cmd for BMDMA
2828 * @qc: internal command to clean up
2831 * Kernel thread context (may sleep)
2833 void ata_bmdma_post_internal_cmd(struct ata_queued_cmd
*qc
)
2835 struct ata_port
*ap
= qc
->ap
;
2836 unsigned long flags
;
2838 if (ata_is_dma(qc
->tf
.protocol
)) {
2839 spin_lock_irqsave(ap
->lock
, flags
);
2840 trace_ata_bmdma_stop(ap
, &qc
->tf
, qc
->tag
);
2841 ap
->ops
->bmdma_stop(qc
);
2842 spin_unlock_irqrestore(ap
->lock
, flags
);
2845 EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd
);
2848 * ata_bmdma_irq_clear - Clear PCI IDE BMDMA interrupt.
2849 * @ap: Port associated with this ATA transaction.
2851 * Clear interrupt and error flags in DMA status register.
2853 * May be used as the irq_clear() entry in ata_port_operations.
2856 * spin_lock_irqsave(host lock)
2858 void ata_bmdma_irq_clear(struct ata_port
*ap
)
2860 void __iomem
*mmio
= ap
->ioaddr
.bmdma_addr
;
2865 iowrite8(ioread8(mmio
+ ATA_DMA_STATUS
), mmio
+ ATA_DMA_STATUS
);
2867 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear
);
2870 * ata_bmdma_setup - Set up PCI IDE BMDMA transaction
2871 * @qc: Info associated with this ATA transaction.
2874 * spin_lock_irqsave(host lock)
2876 void ata_bmdma_setup(struct ata_queued_cmd
*qc
)
2878 struct ata_port
*ap
= qc
->ap
;
2879 unsigned int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
2882 /* load PRD table addr. */
2883 mb(); /* make sure PRD table writes are visible to controller */
2884 iowrite32(ap
->bmdma_prd_dma
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_TABLE_OFS
);
2886 /* specify data direction, triple-check start bit is clear */
2887 dmactl
= ioread8(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
2888 dmactl
&= ~(ATA_DMA_WR
| ATA_DMA_START
);
2890 dmactl
|= ATA_DMA_WR
;
2891 iowrite8(dmactl
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
2893 /* issue r/w command */
2894 ap
->ops
->sff_exec_command(ap
, &qc
->tf
);
2896 EXPORT_SYMBOL_GPL(ata_bmdma_setup
);
2899 * ata_bmdma_start - Start a PCI IDE BMDMA transaction
2900 * @qc: Info associated with this ATA transaction.
2903 * spin_lock_irqsave(host lock)
2905 void ata_bmdma_start(struct ata_queued_cmd
*qc
)
2907 struct ata_port
*ap
= qc
->ap
;
2910 /* start host DMA transaction */
2911 dmactl
= ioread8(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
2912 iowrite8(dmactl
| ATA_DMA_START
, ap
->ioaddr
.bmdma_addr
+ ATA_DMA_CMD
);
2914 /* Strictly, one may wish to issue an ioread8() here, to
2915 * flush the mmio write. However, control also passes
2916 * to the hardware at this point, and it will interrupt
2917 * us when we are to resume control. So, in effect,
2918 * we don't care when the mmio write flushes.
2919 * Further, a read of the DMA status register _immediately_
2920 * following the write may not be what certain flaky hardware
2921 * is expected, so I think it is best to not add a readb()
2922 * without first all the MMIO ATA cards/mobos.
2923 * Or maybe I'm just being paranoid.
2925 * FIXME: The posting of this write means I/O starts are
2926 * unnecessarily delayed for MMIO
2929 EXPORT_SYMBOL_GPL(ata_bmdma_start
);
2932 * ata_bmdma_stop - Stop PCI IDE BMDMA transfer
2933 * @qc: Command we are ending DMA for
2935 * Clears the ATA_DMA_START flag in the dma control register
2937 * May be used as the bmdma_stop() entry in ata_port_operations.
2940 * spin_lock_irqsave(host lock)
2942 void ata_bmdma_stop(struct ata_queued_cmd
*qc
)
2944 struct ata_port
*ap
= qc
->ap
;
2945 void __iomem
*mmio
= ap
->ioaddr
.bmdma_addr
;
2947 /* clear start/stop bit */
2948 iowrite8(ioread8(mmio
+ ATA_DMA_CMD
) & ~ATA_DMA_START
,
2949 mmio
+ ATA_DMA_CMD
);
2951 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
2952 ata_sff_dma_pause(ap
);
2954 EXPORT_SYMBOL_GPL(ata_bmdma_stop
);
2957 * ata_bmdma_status - Read PCI IDE BMDMA status
2958 * @ap: Port associated with this ATA transaction.
2960 * Read and return BMDMA status register.
2962 * May be used as the bmdma_status() entry in ata_port_operations.
2965 * spin_lock_irqsave(host lock)
2967 u8
ata_bmdma_status(struct ata_port
*ap
)
2969 return ioread8(ap
->ioaddr
.bmdma_addr
+ ATA_DMA_STATUS
);
2971 EXPORT_SYMBOL_GPL(ata_bmdma_status
);
2975 * ata_bmdma_port_start - Set port up for bmdma.
2976 * @ap: Port to initialize
2978 * Called just after data structures for each port are
2979 * initialized. Allocates space for PRD table.
2981 * May be used as the port_start() entry in ata_port_operations.
2984 * Inherited from caller.
2986 int ata_bmdma_port_start(struct ata_port
*ap
)
2988 if (ap
->mwdma_mask
|| ap
->udma_mask
) {
2990 dmam_alloc_coherent(ap
->host
->dev
, ATA_PRD_TBL_SZ
,
2991 &ap
->bmdma_prd_dma
, GFP_KERNEL
);
2998 EXPORT_SYMBOL_GPL(ata_bmdma_port_start
);
3001 * ata_bmdma_port_start32 - Set port up for dma.
3002 * @ap: Port to initialize
3004 * Called just after data structures for each port are
3005 * initialized. Enables 32bit PIO and allocates space for PRD
3008 * May be used as the port_start() entry in ata_port_operations for
3009 * devices that are capable of 32bit PIO.
3012 * Inherited from caller.
3014 int ata_bmdma_port_start32(struct ata_port
*ap
)
3016 ap
->pflags
|= ATA_PFLAG_PIO32
| ATA_PFLAG_PIO32CHANGE
;
3017 return ata_bmdma_port_start(ap
);
3019 EXPORT_SYMBOL_GPL(ata_bmdma_port_start32
);
3024 * ata_pci_bmdma_clear_simplex - attempt to kick device out of simplex
3027 * Some PCI ATA devices report simplex mode but in fact can be told to
3028 * enter non simplex mode. This implements the necessary logic to
3029 * perform the task on such devices. Calling it on other devices will
3030 * have -undefined- behaviour.
3032 int ata_pci_bmdma_clear_simplex(struct pci_dev
*pdev
)
3034 #ifdef CONFIG_HAS_IOPORT
3035 unsigned long bmdma
= pci_resource_start(pdev
, 4);
3041 simplex
= inb(bmdma
+ 0x02);
3042 outb(simplex
& 0x60, bmdma
+ 0x02);
3043 simplex
= inb(bmdma
+ 0x02);
3049 #endif /* CONFIG_HAS_IOPORT */
3051 EXPORT_SYMBOL_GPL(ata_pci_bmdma_clear_simplex
);
3053 static void ata_bmdma_nodma(struct ata_host
*host
, const char *reason
)
3057 dev_err(host
->dev
, "BMDMA: %s, falling back to PIO\n", reason
);
3059 for (i
= 0; i
< 2; i
++) {
3060 host
->ports
[i
]->mwdma_mask
= 0;
3061 host
->ports
[i
]->udma_mask
= 0;
3066 * ata_pci_bmdma_init - acquire PCI BMDMA resources and init ATA host
3067 * @host: target ATA host
3069 * Acquire PCI BMDMA resources and initialize @host accordingly.
3072 * Inherited from calling layer (may sleep).
3074 void ata_pci_bmdma_init(struct ata_host
*host
)
3076 struct device
*gdev
= host
->dev
;
3077 struct pci_dev
*pdev
= to_pci_dev(gdev
);
3080 /* No BAR4 allocation: No DMA */
3081 if (pci_resource_start(pdev
, 4) == 0) {
3082 ata_bmdma_nodma(host
, "BAR4 is zero");
3087 * Some controllers require BMDMA region to be initialized
3088 * even if DMA is not in use to clear IRQ status via
3089 * ->sff_irq_clear method. Try to initialize bmdma_addr
3090 * regardless of dma masks.
3092 rc
= dma_set_mask_and_coherent(&pdev
->dev
, ATA_DMA_MASK
);
3094 ata_bmdma_nodma(host
, "failed to set dma mask");
3096 /* request and iomap DMA region */
3097 rc
= pcim_iomap_regions(pdev
, 1 << 4, dev_driver_string(gdev
));
3099 ata_bmdma_nodma(host
, "failed to request/iomap BAR4");
3102 host
->iomap
= pcim_iomap_table(pdev
);
3104 for (i
= 0; i
< 2; i
++) {
3105 struct ata_port
*ap
= host
->ports
[i
];
3106 void __iomem
*bmdma
= host
->iomap
[4] + 8 * i
;
3108 if (ata_port_is_dummy(ap
))
3111 ap
->ioaddr
.bmdma_addr
= bmdma
;
3112 if ((!(ap
->flags
& ATA_FLAG_IGN_SIMPLEX
)) &&
3113 (ioread8(bmdma
+ 2) & 0x80))
3114 host
->flags
|= ATA_HOST_SIMPLEX
;
3116 ata_port_desc(ap
, "bmdma 0x%llx",
3117 (unsigned long long)pci_resource_start(pdev
, 4) + 8 * i
);
3120 EXPORT_SYMBOL_GPL(ata_pci_bmdma_init
);
3123 * ata_pci_bmdma_prepare_host - helper to prepare PCI BMDMA ATA host
3124 * @pdev: target PCI device
3125 * @ppi: array of port_info, must be enough for two ports
3126 * @r_host: out argument for the initialized ATA host
3128 * Helper to allocate BMDMA ATA host for @pdev, acquire all PCI
3129 * resources and initialize it accordingly in one go.
3132 * Inherited from calling layer (may sleep).
3135 * 0 on success, -errno otherwise.
3137 int ata_pci_bmdma_prepare_host(struct pci_dev
*pdev
,
3138 const struct ata_port_info
* const * ppi
,
3139 struct ata_host
**r_host
)
3143 rc
= ata_pci_sff_prepare_host(pdev
, ppi
, r_host
);
3147 ata_pci_bmdma_init(*r_host
);
3150 EXPORT_SYMBOL_GPL(ata_pci_bmdma_prepare_host
);
3153 * ata_pci_bmdma_init_one - Initialize/register BMDMA PCI IDE controller
3154 * @pdev: Controller to be initialized
3155 * @ppi: array of port_info, must be enough for two ports
3156 * @sht: scsi_host_template to use when registering the host
3157 * @host_priv: host private_data
3158 * @hflags: host flags
3160 * This function is similar to ata_pci_sff_init_one() but also
3161 * takes care of BMDMA initialization.
3164 * Inherited from PCI layer (may sleep).
3167 * Zero on success, negative on errno-based value on error.
3169 int ata_pci_bmdma_init_one(struct pci_dev
*pdev
,
3170 const struct ata_port_info
* const * ppi
,
3171 const struct scsi_host_template
*sht
, void *host_priv
,
3174 return ata_pci_init_one(pdev
, ppi
, sht
, host_priv
, hflags
, 1);
3176 EXPORT_SYMBOL_GPL(ata_pci_bmdma_init_one
);
3178 #endif /* CONFIG_PCI */
3179 #endif /* CONFIG_ATA_BMDMA */
3182 * ata_sff_port_init - Initialize SFF/BMDMA ATA port
3183 * @ap: Port to initialize
3185 * Called on port allocation to initialize SFF/BMDMA specific
3191 void ata_sff_port_init(struct ata_port
*ap
)
3193 INIT_DELAYED_WORK(&ap
->sff_pio_task
, ata_sff_pio_task
);
3194 ap
->ctl
= ATA_DEVCTL_OBS
;
3195 ap
->last_ctl
= 0xFF;
3198 int __init
ata_sff_init(void)
3200 ata_sff_wq
= alloc_workqueue("ata_sff", WQ_MEM_RECLAIM
, WQ_MAX_ACTIVE
);
3207 void ata_sff_exit(void)
3209 destroy_workqueue(ata_sff_wq
);