2 * libata-core.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 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/
33 * Standards documents from:
34 * http://www.t13.org (ATA standards, PCI DMA IDE spec)
35 * http://www.t10.org (SCSI MMC - for ATAPI MMC)
36 * http://www.sata-io.org (SATA)
37 * http://www.compactflash.org (CF)
38 * http://www.qic.org (QIC157 - Tape and DSC)
39 * http://www.ce-ata.org (CE-ATA: not supported)
43 #include <linux/kernel.h>
44 #include <linux/module.h>
45 #include <linux/pci.h>
46 #include <linux/init.h>
47 #include <linux/list.h>
49 #include <linux/highmem.h>
50 #include <linux/spinlock.h>
51 #include <linux/blkdev.h>
52 #include <linux/delay.h>
53 #include <linux/timer.h>
54 #include <linux/interrupt.h>
55 #include <linux/completion.h>
56 #include <linux/suspend.h>
57 #include <linux/workqueue.h>
58 #include <linux/jiffies.h>
59 #include <linux/scatterlist.h>
61 #include <scsi/scsi.h>
62 #include <scsi/scsi_cmnd.h>
63 #include <scsi/scsi_host.h>
64 #include <linux/libata.h>
65 #include <asm/semaphore.h>
66 #include <asm/byteorder.h>
67 #include <linux/cdrom.h>
72 /* debounce timing parameters in msecs { interval, duration, timeout } */
73 const unsigned long sata_deb_timing_normal
[] = { 5, 100, 2000 };
74 const unsigned long sata_deb_timing_hotplug
[] = { 25, 500, 2000 };
75 const unsigned long sata_deb_timing_long
[] = { 100, 2000, 5000 };
77 const struct ata_port_operations ata_base_port_ops
= {
78 .irq_clear
= ata_noop_irq_clear
,
81 const struct ata_port_operations sata_port_ops
= {
82 .inherits
= &ata_base_port_ops
,
84 .qc_defer
= ata_std_qc_defer
,
85 .dev_select
= ata_noop_dev_select
,
88 const struct ata_port_operations sata_pmp_port_ops
= {
89 .inherits
= &sata_port_ops
,
92 const struct ata_port_operations ata_sff_port_ops
= {
93 .inherits
= &ata_base_port_ops
,
95 .qc_prep
= ata_qc_prep
,
96 .qc_issue
= ata_qc_issue_prot
,
98 .freeze
= ata_bmdma_freeze
,
99 .thaw
= ata_bmdma_thaw
,
100 .error_handler
= ata_bmdma_error_handler
,
101 .post_internal_cmd
= ata_bmdma_post_internal_cmd
,
103 .dev_select
= ata_std_dev_select
,
104 .check_status
= ata_check_status
,
105 .tf_load
= ata_tf_load
,
106 .tf_read
= ata_tf_read
,
107 .exec_command
= ata_exec_command
,
108 .data_xfer
= ata_data_xfer
,
109 .irq_on
= ata_irq_on
,
111 .port_start
= ata_sff_port_start
,
112 .irq_handler
= ata_interrupt
,
115 const struct ata_port_operations ata_bmdma_port_ops
= {
116 .inherits
= &ata_sff_port_ops
,
118 .mode_filter
= ata_pci_default_filter
,
120 .bmdma_setup
= ata_bmdma_setup
,
121 .bmdma_start
= ata_bmdma_start
,
122 .bmdma_stop
= ata_bmdma_stop
,
123 .bmdma_status
= ata_bmdma_status
,
124 .irq_clear
= ata_bmdma_irq_clear
,
127 static unsigned int ata_dev_init_params(struct ata_device
*dev
,
128 u16 heads
, u16 sectors
);
129 static unsigned int ata_dev_set_xfermode(struct ata_device
*dev
);
130 static unsigned int ata_dev_set_feature(struct ata_device
*dev
,
131 u8 enable
, u8 feature
);
132 static void ata_dev_xfermask(struct ata_device
*dev
);
133 static unsigned long ata_dev_blacklisted(const struct ata_device
*dev
);
135 unsigned int ata_print_id
= 1;
136 static struct workqueue_struct
*ata_wq
;
138 struct workqueue_struct
*ata_aux_wq
;
140 struct ata_force_param
{
144 unsigned long xfer_mask
;
145 unsigned int horkage_on
;
146 unsigned int horkage_off
;
149 struct ata_force_ent
{
152 struct ata_force_param param
;
155 static struct ata_force_ent
*ata_force_tbl
;
156 static int ata_force_tbl_size
;
158 static char ata_force_param_buf
[PAGE_SIZE
] __initdata
;
159 /* param_buf is thrown away after initialization, disallow read */
160 module_param_string(force
, ata_force_param_buf
, sizeof(ata_force_param_buf
), 0);
161 MODULE_PARM_DESC(force
, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
163 int atapi_enabled
= 1;
164 module_param(atapi_enabled
, int, 0444);
165 MODULE_PARM_DESC(atapi_enabled
, "Enable discovery of ATAPI devices (0=off, 1=on)");
167 static int atapi_dmadir
= 0;
168 module_param(atapi_dmadir
, int, 0444);
169 MODULE_PARM_DESC(atapi_dmadir
, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
171 int atapi_passthru16
= 1;
172 module_param(atapi_passthru16
, int, 0444);
173 MODULE_PARM_DESC(atapi_passthru16
, "Enable ATA_16 passthru for ATAPI devices; on by default (0=off, 1=on)");
176 module_param_named(fua
, libata_fua
, int, 0444);
177 MODULE_PARM_DESC(fua
, "FUA support (0=off, 1=on)");
179 static int ata_ignore_hpa
;
180 module_param_named(ignore_hpa
, ata_ignore_hpa
, int, 0644);
181 MODULE_PARM_DESC(ignore_hpa
, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
183 static int libata_dma_mask
= ATA_DMA_MASK_ATA
|ATA_DMA_MASK_ATAPI
|ATA_DMA_MASK_CFA
;
184 module_param_named(dma
, libata_dma_mask
, int, 0444);
185 MODULE_PARM_DESC(dma
, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
187 static int ata_probe_timeout
= ATA_TMOUT_INTERNAL
/ HZ
;
188 module_param(ata_probe_timeout
, int, 0444);
189 MODULE_PARM_DESC(ata_probe_timeout
, "Set ATA probing timeout (seconds)");
191 int libata_noacpi
= 0;
192 module_param_named(noacpi
, libata_noacpi
, int, 0444);
193 MODULE_PARM_DESC(noacpi
, "Disables the use of ACPI in probe/suspend/resume when set");
195 int libata_allow_tpm
= 0;
196 module_param_named(allow_tpm
, libata_allow_tpm
, int, 0444);
197 MODULE_PARM_DESC(allow_tpm
, "Permit the use of TPM commands");
199 MODULE_AUTHOR("Jeff Garzik");
200 MODULE_DESCRIPTION("Library module for ATA devices");
201 MODULE_LICENSE("GPL");
202 MODULE_VERSION(DRV_VERSION
);
206 * ata_force_cbl - force cable type according to libata.force
207 * @ap: ATA port of interest
209 * Force cable type according to libata.force and whine about it.
210 * The last entry which has matching port number is used, so it
211 * can be specified as part of device force parameters. For
212 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
218 void ata_force_cbl(struct ata_port
*ap
)
222 for (i
= ata_force_tbl_size
- 1; i
>= 0; i
--) {
223 const struct ata_force_ent
*fe
= &ata_force_tbl
[i
];
225 if (fe
->port
!= -1 && fe
->port
!= ap
->print_id
)
228 if (fe
->param
.cbl
== ATA_CBL_NONE
)
231 ap
->cbl
= fe
->param
.cbl
;
232 ata_port_printk(ap
, KERN_NOTICE
,
233 "FORCE: cable set to %s\n", fe
->param
.name
);
239 * ata_force_spd_limit - force SATA spd limit according to libata.force
240 * @link: ATA link of interest
242 * Force SATA spd limit according to libata.force and whine about
243 * it. When only the port part is specified (e.g. 1:), the limit
244 * applies to all links connected to both the host link and all
245 * fan-out ports connected via PMP. If the device part is
246 * specified as 0 (e.g. 1.00:), it specifies the first fan-out
247 * link not the host link. Device number 15 always points to the
248 * host link whether PMP is attached or not.
253 static void ata_force_spd_limit(struct ata_link
*link
)
257 if (ata_is_host_link(link
))
262 for (i
= ata_force_tbl_size
- 1; i
>= 0; i
--) {
263 const struct ata_force_ent
*fe
= &ata_force_tbl
[i
];
265 if (fe
->port
!= -1 && fe
->port
!= link
->ap
->print_id
)
268 if (fe
->device
!= -1 && fe
->device
!= linkno
)
271 if (!fe
->param
.spd_limit
)
274 link
->hw_sata_spd_limit
= (1 << fe
->param
.spd_limit
) - 1;
275 ata_link_printk(link
, KERN_NOTICE
,
276 "FORCE: PHY spd limit set to %s\n", fe
->param
.name
);
282 * ata_force_xfermask - force xfermask according to libata.force
283 * @dev: ATA device of interest
285 * Force xfer_mask according to libata.force and whine about it.
286 * For consistency with link selection, device number 15 selects
287 * the first device connected to the host link.
292 static void ata_force_xfermask(struct ata_device
*dev
)
294 int devno
= dev
->link
->pmp
+ dev
->devno
;
295 int alt_devno
= devno
;
298 /* allow n.15 for the first device attached to host port */
299 if (ata_is_host_link(dev
->link
) && devno
== 0)
302 for (i
= ata_force_tbl_size
- 1; i
>= 0; i
--) {
303 const struct ata_force_ent
*fe
= &ata_force_tbl
[i
];
304 unsigned long pio_mask
, mwdma_mask
, udma_mask
;
306 if (fe
->port
!= -1 && fe
->port
!= dev
->link
->ap
->print_id
)
309 if (fe
->device
!= -1 && fe
->device
!= devno
&&
310 fe
->device
!= alt_devno
)
313 if (!fe
->param
.xfer_mask
)
316 ata_unpack_xfermask(fe
->param
.xfer_mask
,
317 &pio_mask
, &mwdma_mask
, &udma_mask
);
319 dev
->udma_mask
= udma_mask
;
320 else if (mwdma_mask
) {
322 dev
->mwdma_mask
= mwdma_mask
;
326 dev
->pio_mask
= pio_mask
;
329 ata_dev_printk(dev
, KERN_NOTICE
,
330 "FORCE: xfer_mask set to %s\n", fe
->param
.name
);
336 * ata_force_horkage - force horkage according to libata.force
337 * @dev: ATA device of interest
339 * Force horkage according to libata.force and whine about it.
340 * For consistency with link selection, device number 15 selects
341 * the first device connected to the host link.
346 static void ata_force_horkage(struct ata_device
*dev
)
348 int devno
= dev
->link
->pmp
+ dev
->devno
;
349 int alt_devno
= devno
;
352 /* allow n.15 for the first device attached to host port */
353 if (ata_is_host_link(dev
->link
) && devno
== 0)
356 for (i
= 0; i
< ata_force_tbl_size
; i
++) {
357 const struct ata_force_ent
*fe
= &ata_force_tbl
[i
];
359 if (fe
->port
!= -1 && fe
->port
!= dev
->link
->ap
->print_id
)
362 if (fe
->device
!= -1 && fe
->device
!= devno
&&
363 fe
->device
!= alt_devno
)
366 if (!(~dev
->horkage
& fe
->param
.horkage_on
) &&
367 !(dev
->horkage
& fe
->param
.horkage_off
))
370 dev
->horkage
|= fe
->param
.horkage_on
;
371 dev
->horkage
&= ~fe
->param
.horkage_off
;
373 ata_dev_printk(dev
, KERN_NOTICE
,
374 "FORCE: horkage modified (%s)\n", fe
->param
.name
);
379 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
380 * @opcode: SCSI opcode
382 * Determine ATAPI command type from @opcode.
388 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
390 int atapi_cmd_type(u8 opcode
)
399 case GPCMD_WRITE_AND_VERIFY_10
:
403 case GPCMD_READ_CD_MSF
:
404 return ATAPI_READ_CD
;
408 if (atapi_passthru16
)
409 return ATAPI_PASS_THRU
;
417 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
418 * @tf: Taskfile to convert
419 * @pmp: Port multiplier port
420 * @is_cmd: This FIS is for command
421 * @fis: Buffer into which data will output
423 * Converts a standard ATA taskfile to a Serial ATA
424 * FIS structure (Register - Host to Device).
427 * Inherited from caller.
429 void ata_tf_to_fis(const struct ata_taskfile
*tf
, u8 pmp
, int is_cmd
, u8
*fis
)
431 fis
[0] = 0x27; /* Register - Host to Device FIS */
432 fis
[1] = pmp
& 0xf; /* Port multiplier number*/
434 fis
[1] |= (1 << 7); /* bit 7 indicates Command FIS */
436 fis
[2] = tf
->command
;
437 fis
[3] = tf
->feature
;
444 fis
[8] = tf
->hob_lbal
;
445 fis
[9] = tf
->hob_lbam
;
446 fis
[10] = tf
->hob_lbah
;
447 fis
[11] = tf
->hob_feature
;
450 fis
[13] = tf
->hob_nsect
;
461 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
462 * @fis: Buffer from which data will be input
463 * @tf: Taskfile to output
465 * Converts a serial ATA FIS structure to a standard ATA taskfile.
468 * Inherited from caller.
471 void ata_tf_from_fis(const u8
*fis
, struct ata_taskfile
*tf
)
473 tf
->command
= fis
[2]; /* status */
474 tf
->feature
= fis
[3]; /* error */
481 tf
->hob_lbal
= fis
[8];
482 tf
->hob_lbam
= fis
[9];
483 tf
->hob_lbah
= fis
[10];
486 tf
->hob_nsect
= fis
[13];
489 static const u8 ata_rw_cmds
[] = {
493 ATA_CMD_READ_MULTI_EXT
,
494 ATA_CMD_WRITE_MULTI_EXT
,
498 ATA_CMD_WRITE_MULTI_FUA_EXT
,
502 ATA_CMD_PIO_READ_EXT
,
503 ATA_CMD_PIO_WRITE_EXT
,
516 ATA_CMD_WRITE_FUA_EXT
520 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
521 * @tf: command to examine and configure
522 * @dev: device tf belongs to
524 * Examine the device configuration and tf->flags to calculate
525 * the proper read/write commands and protocol to use.
530 static int ata_rwcmd_protocol(struct ata_taskfile
*tf
, struct ata_device
*dev
)
534 int index
, fua
, lba48
, write
;
536 fua
= (tf
->flags
& ATA_TFLAG_FUA
) ? 4 : 0;
537 lba48
= (tf
->flags
& ATA_TFLAG_LBA48
) ? 2 : 0;
538 write
= (tf
->flags
& ATA_TFLAG_WRITE
) ? 1 : 0;
540 if (dev
->flags
& ATA_DFLAG_PIO
) {
541 tf
->protocol
= ATA_PROT_PIO
;
542 index
= dev
->multi_count
? 0 : 8;
543 } else if (lba48
&& (dev
->link
->ap
->flags
& ATA_FLAG_PIO_LBA48
)) {
544 /* Unable to use DMA due to host limitation */
545 tf
->protocol
= ATA_PROT_PIO
;
546 index
= dev
->multi_count
? 0 : 8;
548 tf
->protocol
= ATA_PROT_DMA
;
552 cmd
= ata_rw_cmds
[index
+ fua
+ lba48
+ write
];
561 * ata_tf_read_block - Read block address from ATA taskfile
562 * @tf: ATA taskfile of interest
563 * @dev: ATA device @tf belongs to
568 * Read block address from @tf. This function can handle all
569 * three address formats - LBA, LBA48 and CHS. tf->protocol and
570 * flags select the address format to use.
573 * Block address read from @tf.
575 u64
ata_tf_read_block(struct ata_taskfile
*tf
, struct ata_device
*dev
)
579 if (tf
->flags
& ATA_TFLAG_LBA
) {
580 if (tf
->flags
& ATA_TFLAG_LBA48
) {
581 block
|= (u64
)tf
->hob_lbah
<< 40;
582 block
|= (u64
)tf
->hob_lbam
<< 32;
583 block
|= tf
->hob_lbal
<< 24;
585 block
|= (tf
->device
& 0xf) << 24;
587 block
|= tf
->lbah
<< 16;
588 block
|= tf
->lbam
<< 8;
593 cyl
= tf
->lbam
| (tf
->lbah
<< 8);
594 head
= tf
->device
& 0xf;
597 block
= (cyl
* dev
->heads
+ head
) * dev
->sectors
+ sect
;
604 * ata_build_rw_tf - Build ATA taskfile for given read/write request
605 * @tf: Target ATA taskfile
606 * @dev: ATA device @tf belongs to
607 * @block: Block address
608 * @n_block: Number of blocks
609 * @tf_flags: RW/FUA etc...
615 * Build ATA taskfile @tf for read/write request described by
616 * @block, @n_block, @tf_flags and @tag on @dev.
620 * 0 on success, -ERANGE if the request is too large for @dev,
621 * -EINVAL if the request is invalid.
623 int ata_build_rw_tf(struct ata_taskfile
*tf
, struct ata_device
*dev
,
624 u64 block
, u32 n_block
, unsigned int tf_flags
,
627 tf
->flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
628 tf
->flags
|= tf_flags
;
630 if (ata_ncq_enabled(dev
) && likely(tag
!= ATA_TAG_INTERNAL
)) {
632 if (!lba_48_ok(block
, n_block
))
635 tf
->protocol
= ATA_PROT_NCQ
;
636 tf
->flags
|= ATA_TFLAG_LBA
| ATA_TFLAG_LBA48
;
638 if (tf
->flags
& ATA_TFLAG_WRITE
)
639 tf
->command
= ATA_CMD_FPDMA_WRITE
;
641 tf
->command
= ATA_CMD_FPDMA_READ
;
643 tf
->nsect
= tag
<< 3;
644 tf
->hob_feature
= (n_block
>> 8) & 0xff;
645 tf
->feature
= n_block
& 0xff;
647 tf
->hob_lbah
= (block
>> 40) & 0xff;
648 tf
->hob_lbam
= (block
>> 32) & 0xff;
649 tf
->hob_lbal
= (block
>> 24) & 0xff;
650 tf
->lbah
= (block
>> 16) & 0xff;
651 tf
->lbam
= (block
>> 8) & 0xff;
652 tf
->lbal
= block
& 0xff;
655 if (tf
->flags
& ATA_TFLAG_FUA
)
656 tf
->device
|= 1 << 7;
657 } else if (dev
->flags
& ATA_DFLAG_LBA
) {
658 tf
->flags
|= ATA_TFLAG_LBA
;
660 if (lba_28_ok(block
, n_block
)) {
662 tf
->device
|= (block
>> 24) & 0xf;
663 } else if (lba_48_ok(block
, n_block
)) {
664 if (!(dev
->flags
& ATA_DFLAG_LBA48
))
668 tf
->flags
|= ATA_TFLAG_LBA48
;
670 tf
->hob_nsect
= (n_block
>> 8) & 0xff;
672 tf
->hob_lbah
= (block
>> 40) & 0xff;
673 tf
->hob_lbam
= (block
>> 32) & 0xff;
674 tf
->hob_lbal
= (block
>> 24) & 0xff;
676 /* request too large even for LBA48 */
679 if (unlikely(ata_rwcmd_protocol(tf
, dev
) < 0))
682 tf
->nsect
= n_block
& 0xff;
684 tf
->lbah
= (block
>> 16) & 0xff;
685 tf
->lbam
= (block
>> 8) & 0xff;
686 tf
->lbal
= block
& 0xff;
688 tf
->device
|= ATA_LBA
;
691 u32 sect
, head
, cyl
, track
;
693 /* The request -may- be too large for CHS addressing. */
694 if (!lba_28_ok(block
, n_block
))
697 if (unlikely(ata_rwcmd_protocol(tf
, dev
) < 0))
700 /* Convert LBA to CHS */
701 track
= (u32
)block
/ dev
->sectors
;
702 cyl
= track
/ dev
->heads
;
703 head
= track
% dev
->heads
;
704 sect
= (u32
)block
% dev
->sectors
+ 1;
706 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
707 (u32
)block
, track
, cyl
, head
, sect
);
709 /* Check whether the converted CHS can fit.
713 if ((cyl
>> 16) || (head
>> 4) || (sect
>> 8) || (!sect
))
716 tf
->nsect
= n_block
& 0xff; /* Sector count 0 means 256 sectors */
727 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
728 * @pio_mask: pio_mask
729 * @mwdma_mask: mwdma_mask
730 * @udma_mask: udma_mask
732 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
733 * unsigned int xfer_mask.
741 unsigned long ata_pack_xfermask(unsigned long pio_mask
,
742 unsigned long mwdma_mask
,
743 unsigned long udma_mask
)
745 return ((pio_mask
<< ATA_SHIFT_PIO
) & ATA_MASK_PIO
) |
746 ((mwdma_mask
<< ATA_SHIFT_MWDMA
) & ATA_MASK_MWDMA
) |
747 ((udma_mask
<< ATA_SHIFT_UDMA
) & ATA_MASK_UDMA
);
751 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
752 * @xfer_mask: xfer_mask to unpack
753 * @pio_mask: resulting pio_mask
754 * @mwdma_mask: resulting mwdma_mask
755 * @udma_mask: resulting udma_mask
757 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
758 * Any NULL distination masks will be ignored.
760 void ata_unpack_xfermask(unsigned long xfer_mask
, unsigned long *pio_mask
,
761 unsigned long *mwdma_mask
, unsigned long *udma_mask
)
764 *pio_mask
= (xfer_mask
& ATA_MASK_PIO
) >> ATA_SHIFT_PIO
;
766 *mwdma_mask
= (xfer_mask
& ATA_MASK_MWDMA
) >> ATA_SHIFT_MWDMA
;
768 *udma_mask
= (xfer_mask
& ATA_MASK_UDMA
) >> ATA_SHIFT_UDMA
;
771 static const struct ata_xfer_ent
{
775 { ATA_SHIFT_PIO
, ATA_NR_PIO_MODES
, XFER_PIO_0
},
776 { ATA_SHIFT_MWDMA
, ATA_NR_MWDMA_MODES
, XFER_MW_DMA_0
},
777 { ATA_SHIFT_UDMA
, ATA_NR_UDMA_MODES
, XFER_UDMA_0
},
782 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
783 * @xfer_mask: xfer_mask of interest
785 * Return matching XFER_* value for @xfer_mask. Only the highest
786 * bit of @xfer_mask is considered.
792 * Matching XFER_* value, 0xff if no match found.
794 u8
ata_xfer_mask2mode(unsigned long xfer_mask
)
796 int highbit
= fls(xfer_mask
) - 1;
797 const struct ata_xfer_ent
*ent
;
799 for (ent
= ata_xfer_tbl
; ent
->shift
>= 0; ent
++)
800 if (highbit
>= ent
->shift
&& highbit
< ent
->shift
+ ent
->bits
)
801 return ent
->base
+ highbit
- ent
->shift
;
806 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
807 * @xfer_mode: XFER_* of interest
809 * Return matching xfer_mask for @xfer_mode.
815 * Matching xfer_mask, 0 if no match found.
817 unsigned long ata_xfer_mode2mask(u8 xfer_mode
)
819 const struct ata_xfer_ent
*ent
;
821 for (ent
= ata_xfer_tbl
; ent
->shift
>= 0; ent
++)
822 if (xfer_mode
>= ent
->base
&& xfer_mode
< ent
->base
+ ent
->bits
)
823 return ((2 << (ent
->shift
+ xfer_mode
- ent
->base
)) - 1)
824 & ~((1 << ent
->shift
) - 1);
829 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
830 * @xfer_mode: XFER_* of interest
832 * Return matching xfer_shift for @xfer_mode.
838 * Matching xfer_shift, -1 if no match found.
840 int ata_xfer_mode2shift(unsigned long xfer_mode
)
842 const struct ata_xfer_ent
*ent
;
844 for (ent
= ata_xfer_tbl
; ent
->shift
>= 0; ent
++)
845 if (xfer_mode
>= ent
->base
&& xfer_mode
< ent
->base
+ ent
->bits
)
851 * ata_mode_string - convert xfer_mask to string
852 * @xfer_mask: mask of bits supported; only highest bit counts.
854 * Determine string which represents the highest speed
855 * (highest bit in @modemask).
861 * Constant C string representing highest speed listed in
862 * @mode_mask, or the constant C string "<n/a>".
864 const char *ata_mode_string(unsigned long xfer_mask
)
866 static const char * const xfer_mode_str
[] = {
890 highbit
= fls(xfer_mask
) - 1;
891 if (highbit
>= 0 && highbit
< ARRAY_SIZE(xfer_mode_str
))
892 return xfer_mode_str
[highbit
];
896 static const char *sata_spd_string(unsigned int spd
)
898 static const char * const spd_str
[] = {
903 if (spd
== 0 || (spd
- 1) >= ARRAY_SIZE(spd_str
))
905 return spd_str
[spd
- 1];
908 void ata_dev_disable(struct ata_device
*dev
)
910 if (ata_dev_enabled(dev
)) {
911 if (ata_msg_drv(dev
->link
->ap
))
912 ata_dev_printk(dev
, KERN_WARNING
, "disabled\n");
913 ata_acpi_on_disable(dev
);
914 ata_down_xfermask_limit(dev
, ATA_DNXFER_FORCE_PIO0
|
920 static int ata_dev_set_dipm(struct ata_device
*dev
, enum link_pm policy
)
922 struct ata_link
*link
= dev
->link
;
923 struct ata_port
*ap
= link
->ap
;
925 unsigned int err_mask
;
929 * disallow DIPM for drivers which haven't set
930 * ATA_FLAG_IPM. This is because when DIPM is enabled,
931 * phy ready will be set in the interrupt status on
932 * state changes, which will cause some drivers to
933 * think there are errors - additionally drivers will
934 * need to disable hot plug.
936 if (!(ap
->flags
& ATA_FLAG_IPM
) || !ata_dev_enabled(dev
)) {
937 ap
->pm_policy
= NOT_AVAILABLE
;
942 * For DIPM, we will only enable it for the
945 * Why? Because Disks are too stupid to know that
946 * If the host rejects a request to go to SLUMBER
947 * they should retry at PARTIAL, and instead it
948 * just would give up. So, for medium_power to
949 * work at all, we need to only allow HIPM.
951 rc
= sata_scr_read(link
, SCR_CONTROL
, &scontrol
);
957 /* no restrictions on IPM transitions */
958 scontrol
&= ~(0x3 << 8);
959 rc
= sata_scr_write(link
, SCR_CONTROL
, scontrol
);
964 if (dev
->flags
& ATA_DFLAG_DIPM
)
965 err_mask
= ata_dev_set_feature(dev
,
966 SETFEATURES_SATA_ENABLE
, SATA_DIPM
);
969 /* allow IPM to PARTIAL */
970 scontrol
&= ~(0x1 << 8);
971 scontrol
|= (0x2 << 8);
972 rc
= sata_scr_write(link
, SCR_CONTROL
, scontrol
);
977 * we don't have to disable DIPM since IPM flags
978 * disallow transitions to SLUMBER, which effectively
979 * disable DIPM if it does not support PARTIAL
983 case MAX_PERFORMANCE
:
984 /* disable all IPM transitions */
985 scontrol
|= (0x3 << 8);
986 rc
= sata_scr_write(link
, SCR_CONTROL
, scontrol
);
991 * we don't have to disable DIPM since IPM flags
992 * disallow all transitions which effectively
993 * disable DIPM anyway.
998 /* FIXME: handle SET FEATURES failure */
1005 * ata_dev_enable_pm - enable SATA interface power management
1006 * @dev: device to enable power management
1007 * @policy: the link power management policy
1009 * Enable SATA Interface power management. This will enable
1010 * Device Interface Power Management (DIPM) for min_power
1011 * policy, and then call driver specific callbacks for
1012 * enabling Host Initiated Power management.
1015 * Returns: -EINVAL if IPM is not supported, 0 otherwise.
1017 void ata_dev_enable_pm(struct ata_device
*dev
, enum link_pm policy
)
1020 struct ata_port
*ap
= dev
->link
->ap
;
1022 /* set HIPM first, then DIPM */
1023 if (ap
->ops
->enable_pm
)
1024 rc
= ap
->ops
->enable_pm(ap
, policy
);
1027 rc
= ata_dev_set_dipm(dev
, policy
);
1031 ap
->pm_policy
= MAX_PERFORMANCE
;
1033 ap
->pm_policy
= policy
;
1034 return /* rc */; /* hopefully we can use 'rc' eventually */
1039 * ata_dev_disable_pm - disable SATA interface power management
1040 * @dev: device to disable power management
1042 * Disable SATA Interface power management. This will disable
1043 * Device Interface Power Management (DIPM) without changing
1044 * policy, call driver specific callbacks for disabling Host
1045 * Initiated Power management.
1050 static void ata_dev_disable_pm(struct ata_device
*dev
)
1052 struct ata_port
*ap
= dev
->link
->ap
;
1054 ata_dev_set_dipm(dev
, MAX_PERFORMANCE
);
1055 if (ap
->ops
->disable_pm
)
1056 ap
->ops
->disable_pm(ap
);
1058 #endif /* CONFIG_PM */
1060 void ata_lpm_schedule(struct ata_port
*ap
, enum link_pm policy
)
1062 ap
->pm_policy
= policy
;
1063 ap
->link
.eh_info
.action
|= ATA_EH_LPM
;
1064 ap
->link
.eh_info
.flags
|= ATA_EHI_NO_AUTOPSY
;
1065 ata_port_schedule_eh(ap
);
1069 static void ata_lpm_enable(struct ata_host
*host
)
1071 struct ata_link
*link
;
1072 struct ata_port
*ap
;
1073 struct ata_device
*dev
;
1076 for (i
= 0; i
< host
->n_ports
; i
++) {
1077 ap
= host
->ports
[i
];
1078 ata_port_for_each_link(link
, ap
) {
1079 ata_link_for_each_dev(dev
, link
)
1080 ata_dev_disable_pm(dev
);
1085 static void ata_lpm_disable(struct ata_host
*host
)
1089 for (i
= 0; i
< host
->n_ports
; i
++) {
1090 struct ata_port
*ap
= host
->ports
[i
];
1091 ata_lpm_schedule(ap
, ap
->pm_policy
);
1094 #endif /* CONFIG_PM */
1098 * ata_devchk - PATA device presence detection
1099 * @ap: ATA channel to examine
1100 * @device: Device to examine (starting at zero)
1102 * This technique was originally described in
1103 * Hale Landis's ATADRVR (www.ata-atapi.com), and
1104 * later found its way into the ATA/ATAPI spec.
1106 * Write a pattern to the ATA shadow registers,
1107 * and if a device is present, it will respond by
1108 * correctly storing and echoing back the
1109 * ATA shadow register contents.
1115 static unsigned int ata_devchk(struct ata_port
*ap
, unsigned int device
)
1117 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
1120 ap
->ops
->dev_select(ap
, device
);
1122 iowrite8(0x55, ioaddr
->nsect_addr
);
1123 iowrite8(0xaa, ioaddr
->lbal_addr
);
1125 iowrite8(0xaa, ioaddr
->nsect_addr
);
1126 iowrite8(0x55, ioaddr
->lbal_addr
);
1128 iowrite8(0x55, ioaddr
->nsect_addr
);
1129 iowrite8(0xaa, ioaddr
->lbal_addr
);
1131 nsect
= ioread8(ioaddr
->nsect_addr
);
1132 lbal
= ioread8(ioaddr
->lbal_addr
);
1134 if ((nsect
== 0x55) && (lbal
== 0xaa))
1135 return 1; /* we found a device */
1137 return 0; /* nothing found */
1141 * ata_dev_classify - determine device type based on ATA-spec signature
1142 * @tf: ATA taskfile register set for device to be identified
1144 * Determine from taskfile register contents whether a device is
1145 * ATA or ATAPI, as per "Signature and persistence" section
1146 * of ATA/PI spec (volume 1, sect 5.14).
1152 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
1153 * %ATA_DEV_UNKNOWN the event of failure.
1155 unsigned int ata_dev_classify(const struct ata_taskfile
*tf
)
1157 /* Apple's open source Darwin code hints that some devices only
1158 * put a proper signature into the LBA mid/high registers,
1159 * So, we only check those. It's sufficient for uniqueness.
1161 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1162 * signatures for ATA and ATAPI devices attached on SerialATA,
1163 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
1164 * spec has never mentioned about using different signatures
1165 * for ATA/ATAPI devices. Then, Serial ATA II: Port
1166 * Multiplier specification began to use 0x69/0x96 to identify
1167 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1168 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1169 * 0x69/0x96 shortly and described them as reserved for
1172 * We follow the current spec and consider that 0x69/0x96
1173 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
1175 if ((tf
->lbam
== 0) && (tf
->lbah
== 0)) {
1176 DPRINTK("found ATA device by sig\n");
1180 if ((tf
->lbam
== 0x14) && (tf
->lbah
== 0xeb)) {
1181 DPRINTK("found ATAPI device by sig\n");
1182 return ATA_DEV_ATAPI
;
1185 if ((tf
->lbam
== 0x69) && (tf
->lbah
== 0x96)) {
1186 DPRINTK("found PMP device by sig\n");
1190 if ((tf
->lbam
== 0x3c) && (tf
->lbah
== 0xc3)) {
1191 printk(KERN_INFO
"ata: SEMB device ignored\n");
1192 return ATA_DEV_SEMB_UNSUP
; /* not yet */
1195 DPRINTK("unknown device\n");
1196 return ATA_DEV_UNKNOWN
;
1200 * ata_dev_try_classify - Parse returned ATA device signature
1201 * @dev: ATA device to classify (starting at zero)
1202 * @present: device seems present
1203 * @r_err: Value of error register on completion
1205 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
1206 * an ATA/ATAPI-defined set of values is placed in the ATA
1207 * shadow registers, indicating the results of device detection
1210 * Select the ATA device, and read the values from the ATA shadow
1211 * registers. Then parse according to the Error register value,
1212 * and the spec-defined values examined by ata_dev_classify().
1218 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
1220 unsigned int ata_dev_try_classify(struct ata_device
*dev
, int present
,
1223 struct ata_port
*ap
= dev
->link
->ap
;
1224 struct ata_taskfile tf
;
1228 ap
->ops
->dev_select(ap
, dev
->devno
);
1230 memset(&tf
, 0, sizeof(tf
));
1232 ap
->ops
->tf_read(ap
, &tf
);
1237 /* see if device passed diags: continue and warn later */
1239 /* diagnostic fail : do nothing _YET_ */
1240 dev
->horkage
|= ATA_HORKAGE_DIAGNOSTIC
;
1243 else if ((dev
->devno
== 0) && (err
== 0x81))
1246 return ATA_DEV_NONE
;
1248 /* determine if device is ATA or ATAPI */
1249 class = ata_dev_classify(&tf
);
1251 if (class == ATA_DEV_UNKNOWN
) {
1252 /* If the device failed diagnostic, it's likely to
1253 * have reported incorrect device signature too.
1254 * Assume ATA device if the device seems present but
1255 * device signature is invalid with diagnostic
1258 if (present
&& (dev
->horkage
& ATA_HORKAGE_DIAGNOSTIC
))
1259 class = ATA_DEV_ATA
;
1261 class = ATA_DEV_NONE
;
1262 } else if ((class == ATA_DEV_ATA
) && (ata_chk_status(ap
) == 0))
1263 class = ATA_DEV_NONE
;
1269 * ata_id_string - Convert IDENTIFY DEVICE page into string
1270 * @id: IDENTIFY DEVICE results we will examine
1271 * @s: string into which data is output
1272 * @ofs: offset into identify device page
1273 * @len: length of string to return. must be an even number.
1275 * The strings in the IDENTIFY DEVICE page are broken up into
1276 * 16-bit chunks. Run through the string, and output each
1277 * 8-bit chunk linearly, regardless of platform.
1283 void ata_id_string(const u16
*id
, unsigned char *s
,
1284 unsigned int ofs
, unsigned int len
)
1303 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1304 * @id: IDENTIFY DEVICE results we will examine
1305 * @s: string into which data is output
1306 * @ofs: offset into identify device page
1307 * @len: length of string to return. must be an odd number.
1309 * This function is identical to ata_id_string except that it
1310 * trims trailing spaces and terminates the resulting string with
1311 * null. @len must be actual maximum length (even number) + 1.
1316 void ata_id_c_string(const u16
*id
, unsigned char *s
,
1317 unsigned int ofs
, unsigned int len
)
1321 WARN_ON(!(len
& 1));
1323 ata_id_string(id
, s
, ofs
, len
- 1);
1325 p
= s
+ strnlen(s
, len
- 1);
1326 while (p
> s
&& p
[-1] == ' ')
1331 static u64
ata_id_n_sectors(const u16
*id
)
1333 if (ata_id_has_lba(id
)) {
1334 if (ata_id_has_lba48(id
))
1335 return ata_id_u64(id
, 100);
1337 return ata_id_u32(id
, 60);
1339 if (ata_id_current_chs_valid(id
))
1340 return ata_id_u32(id
, 57);
1342 return id
[1] * id
[3] * id
[6];
1346 static u64
ata_tf_to_lba48(struct ata_taskfile
*tf
)
1350 sectors
|= ((u64
)(tf
->hob_lbah
& 0xff)) << 40;
1351 sectors
|= ((u64
)(tf
->hob_lbam
& 0xff)) << 32;
1352 sectors
|= (tf
->hob_lbal
& 0xff) << 24;
1353 sectors
|= (tf
->lbah
& 0xff) << 16;
1354 sectors
|= (tf
->lbam
& 0xff) << 8;
1355 sectors
|= (tf
->lbal
& 0xff);
1360 static u64
ata_tf_to_lba(struct ata_taskfile
*tf
)
1364 sectors
|= (tf
->device
& 0x0f) << 24;
1365 sectors
|= (tf
->lbah
& 0xff) << 16;
1366 sectors
|= (tf
->lbam
& 0xff) << 8;
1367 sectors
|= (tf
->lbal
& 0xff);
1373 * ata_read_native_max_address - Read native max address
1374 * @dev: target device
1375 * @max_sectors: out parameter for the result native max address
1377 * Perform an LBA48 or LBA28 native size query upon the device in
1381 * 0 on success, -EACCES if command is aborted by the drive.
1382 * -EIO on other errors.
1384 static int ata_read_native_max_address(struct ata_device
*dev
, u64
*max_sectors
)
1386 unsigned int err_mask
;
1387 struct ata_taskfile tf
;
1388 int lba48
= ata_id_has_lba48(dev
->id
);
1390 ata_tf_init(dev
, &tf
);
1392 /* always clear all address registers */
1393 tf
.flags
|= ATA_TFLAG_DEVICE
| ATA_TFLAG_ISADDR
;
1396 tf
.command
= ATA_CMD_READ_NATIVE_MAX_EXT
;
1397 tf
.flags
|= ATA_TFLAG_LBA48
;
1399 tf
.command
= ATA_CMD_READ_NATIVE_MAX
;
1401 tf
.protocol
|= ATA_PROT_NODATA
;
1402 tf
.device
|= ATA_LBA
;
1404 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_NONE
, NULL
, 0, 0);
1406 ata_dev_printk(dev
, KERN_WARNING
, "failed to read native "
1407 "max address (err_mask=0x%x)\n", err_mask
);
1408 if (err_mask
== AC_ERR_DEV
&& (tf
.feature
& ATA_ABORTED
))
1414 *max_sectors
= ata_tf_to_lba48(&tf
);
1416 *max_sectors
= ata_tf_to_lba(&tf
);
1417 if (dev
->horkage
& ATA_HORKAGE_HPA_SIZE
)
1423 * ata_set_max_sectors - Set max sectors
1424 * @dev: target device
1425 * @new_sectors: new max sectors value to set for the device
1427 * Set max sectors of @dev to @new_sectors.
1430 * 0 on success, -EACCES if command is aborted or denied (due to
1431 * previous non-volatile SET_MAX) by the drive. -EIO on other
1434 static int ata_set_max_sectors(struct ata_device
*dev
, u64 new_sectors
)
1436 unsigned int err_mask
;
1437 struct ata_taskfile tf
;
1438 int lba48
= ata_id_has_lba48(dev
->id
);
1442 ata_tf_init(dev
, &tf
);
1444 tf
.flags
|= ATA_TFLAG_DEVICE
| ATA_TFLAG_ISADDR
;
1447 tf
.command
= ATA_CMD_SET_MAX_EXT
;
1448 tf
.flags
|= ATA_TFLAG_LBA48
;
1450 tf
.hob_lbal
= (new_sectors
>> 24) & 0xff;
1451 tf
.hob_lbam
= (new_sectors
>> 32) & 0xff;
1452 tf
.hob_lbah
= (new_sectors
>> 40) & 0xff;
1454 tf
.command
= ATA_CMD_SET_MAX
;
1456 tf
.device
|= (new_sectors
>> 24) & 0xf;
1459 tf
.protocol
|= ATA_PROT_NODATA
;
1460 tf
.device
|= ATA_LBA
;
1462 tf
.lbal
= (new_sectors
>> 0) & 0xff;
1463 tf
.lbam
= (new_sectors
>> 8) & 0xff;
1464 tf
.lbah
= (new_sectors
>> 16) & 0xff;
1466 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_NONE
, NULL
, 0, 0);
1468 ata_dev_printk(dev
, KERN_WARNING
, "failed to set "
1469 "max address (err_mask=0x%x)\n", err_mask
);
1470 if (err_mask
== AC_ERR_DEV
&&
1471 (tf
.feature
& (ATA_ABORTED
| ATA_IDNF
)))
1480 * ata_hpa_resize - Resize a device with an HPA set
1481 * @dev: Device to resize
1483 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1484 * it if required to the full size of the media. The caller must check
1485 * the drive has the HPA feature set enabled.
1488 * 0 on success, -errno on failure.
1490 static int ata_hpa_resize(struct ata_device
*dev
)
1492 struct ata_eh_context
*ehc
= &dev
->link
->eh_context
;
1493 int print_info
= ehc
->i
.flags
& ATA_EHI_PRINTINFO
;
1494 u64 sectors
= ata_id_n_sectors(dev
->id
);
1498 /* do we need to do it? */
1499 if (dev
->class != ATA_DEV_ATA
||
1500 !ata_id_has_lba(dev
->id
) || !ata_id_hpa_enabled(dev
->id
) ||
1501 (dev
->horkage
& ATA_HORKAGE_BROKEN_HPA
))
1504 /* read native max address */
1505 rc
= ata_read_native_max_address(dev
, &native_sectors
);
1507 /* If device aborted the command or HPA isn't going to
1508 * be unlocked, skip HPA resizing.
1510 if (rc
== -EACCES
|| !ata_ignore_hpa
) {
1511 ata_dev_printk(dev
, KERN_WARNING
, "HPA support seems "
1512 "broken, skipping HPA handling\n");
1513 dev
->horkage
|= ATA_HORKAGE_BROKEN_HPA
;
1515 /* we can continue if device aborted the command */
1523 /* nothing to do? */
1524 if (native_sectors
<= sectors
|| !ata_ignore_hpa
) {
1525 if (!print_info
|| native_sectors
== sectors
)
1528 if (native_sectors
> sectors
)
1529 ata_dev_printk(dev
, KERN_INFO
,
1530 "HPA detected: current %llu, native %llu\n",
1531 (unsigned long long)sectors
,
1532 (unsigned long long)native_sectors
);
1533 else if (native_sectors
< sectors
)
1534 ata_dev_printk(dev
, KERN_WARNING
,
1535 "native sectors (%llu) is smaller than "
1537 (unsigned long long)native_sectors
,
1538 (unsigned long long)sectors
);
1542 /* let's unlock HPA */
1543 rc
= ata_set_max_sectors(dev
, native_sectors
);
1544 if (rc
== -EACCES
) {
1545 /* if device aborted the command, skip HPA resizing */
1546 ata_dev_printk(dev
, KERN_WARNING
, "device aborted resize "
1547 "(%llu -> %llu), skipping HPA handling\n",
1548 (unsigned long long)sectors
,
1549 (unsigned long long)native_sectors
);
1550 dev
->horkage
|= ATA_HORKAGE_BROKEN_HPA
;
1555 /* re-read IDENTIFY data */
1556 rc
= ata_dev_reread_id(dev
, 0);
1558 ata_dev_printk(dev
, KERN_ERR
, "failed to re-read IDENTIFY "
1559 "data after HPA resizing\n");
1564 u64 new_sectors
= ata_id_n_sectors(dev
->id
);
1565 ata_dev_printk(dev
, KERN_INFO
,
1566 "HPA unlocked: %llu -> %llu, native %llu\n",
1567 (unsigned long long)sectors
,
1568 (unsigned long long)new_sectors
,
1569 (unsigned long long)native_sectors
);
1576 * ata_noop_dev_select - Select device 0/1 on ATA bus
1577 * @ap: ATA channel to manipulate
1578 * @device: ATA device (numbered from zero) to select
1580 * This function performs no actual function.
1582 * May be used as the dev_select() entry in ata_port_operations.
1587 void ata_noop_dev_select(struct ata_port
*ap
, unsigned int device
)
1593 * ata_std_dev_select - Select device 0/1 on ATA bus
1594 * @ap: ATA channel to manipulate
1595 * @device: ATA device (numbered from zero) to select
1597 * Use the method defined in the ATA specification to
1598 * make either device 0, or device 1, active on the
1599 * ATA channel. Works with both PIO and MMIO.
1601 * May be used as the dev_select() entry in ata_port_operations.
1607 void ata_std_dev_select(struct ata_port
*ap
, unsigned int device
)
1612 tmp
= ATA_DEVICE_OBS
;
1614 tmp
= ATA_DEVICE_OBS
| ATA_DEV1
;
1616 iowrite8(tmp
, ap
->ioaddr
.device_addr
);
1617 ata_pause(ap
); /* needed; also flushes, for mmio */
1621 * ata_dev_select - Select device 0/1 on ATA bus
1622 * @ap: ATA channel to manipulate
1623 * @device: ATA device (numbered from zero) to select
1624 * @wait: non-zero to wait for Status register BSY bit to clear
1625 * @can_sleep: non-zero if context allows sleeping
1627 * Use the method defined in the ATA specification to
1628 * make either device 0, or device 1, active on the
1631 * This is a high-level version of ata_std_dev_select(),
1632 * which additionally provides the services of inserting
1633 * the proper pauses and status polling, where needed.
1639 void ata_dev_select(struct ata_port
*ap
, unsigned int device
,
1640 unsigned int wait
, unsigned int can_sleep
)
1642 if (ata_msg_probe(ap
))
1643 ata_port_printk(ap
, KERN_INFO
, "ata_dev_select: ENTER, "
1644 "device %u, wait %u\n", device
, wait
);
1649 ap
->ops
->dev_select(ap
, device
);
1652 if (can_sleep
&& ap
->link
.device
[device
].class == ATA_DEV_ATAPI
)
1659 * ata_dump_id - IDENTIFY DEVICE info debugging output
1660 * @id: IDENTIFY DEVICE page to dump
1662 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1669 static inline void ata_dump_id(const u16
*id
)
1671 DPRINTK("49==0x%04x "
1681 DPRINTK("80==0x%04x "
1691 DPRINTK("88==0x%04x "
1698 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1699 * @id: IDENTIFY data to compute xfer mask from
1701 * Compute the xfermask for this device. This is not as trivial
1702 * as it seems if we must consider early devices correctly.
1704 * FIXME: pre IDE drive timing (do we care ?).
1712 unsigned long ata_id_xfermask(const u16
*id
)
1714 unsigned long pio_mask
, mwdma_mask
, udma_mask
;
1716 /* Usual case. Word 53 indicates word 64 is valid */
1717 if (id
[ATA_ID_FIELD_VALID
] & (1 << 1)) {
1718 pio_mask
= id
[ATA_ID_PIO_MODES
] & 0x03;
1722 /* If word 64 isn't valid then Word 51 high byte holds
1723 * the PIO timing number for the maximum. Turn it into
1726 u8 mode
= (id
[ATA_ID_OLD_PIO_MODES
] >> 8) & 0xFF;
1727 if (mode
< 5) /* Valid PIO range */
1728 pio_mask
= (2 << mode
) - 1;
1732 /* But wait.. there's more. Design your standards by
1733 * committee and you too can get a free iordy field to
1734 * process. However its the speeds not the modes that
1735 * are supported... Note drivers using the timing API
1736 * will get this right anyway
1740 mwdma_mask
= id
[ATA_ID_MWDMA_MODES
] & 0x07;
1742 if (ata_id_is_cfa(id
)) {
1744 * Process compact flash extended modes
1746 int pio
= id
[163] & 0x7;
1747 int dma
= (id
[163] >> 3) & 7;
1750 pio_mask
|= (1 << 5);
1752 pio_mask
|= (1 << 6);
1754 mwdma_mask
|= (1 << 3);
1756 mwdma_mask
|= (1 << 4);
1760 if (id
[ATA_ID_FIELD_VALID
] & (1 << 2))
1761 udma_mask
= id
[ATA_ID_UDMA_MODES
] & 0xff;
1763 return ata_pack_xfermask(pio_mask
, mwdma_mask
, udma_mask
);
1767 * ata_pio_queue_task - Queue port_task
1768 * @ap: The ata_port to queue port_task for
1769 * @fn: workqueue function to be scheduled
1770 * @data: data for @fn to use
1771 * @delay: delay time for workqueue function
1773 * Schedule @fn(@data) for execution after @delay jiffies using
1774 * port_task. There is one port_task per port and it's the
1775 * user(low level driver)'s responsibility to make sure that only
1776 * one task is active at any given time.
1778 * libata core layer takes care of synchronization between
1779 * port_task and EH. ata_pio_queue_task() may be ignored for EH
1783 * Inherited from caller.
1785 static void ata_pio_queue_task(struct ata_port
*ap
, void *data
,
1786 unsigned long delay
)
1788 ap
->port_task_data
= data
;
1790 /* may fail if ata_port_flush_task() in progress */
1791 queue_delayed_work(ata_wq
, &ap
->port_task
, delay
);
1795 * ata_port_flush_task - Flush port_task
1796 * @ap: The ata_port to flush port_task for
1798 * After this function completes, port_task is guranteed not to
1799 * be running or scheduled.
1802 * Kernel thread context (may sleep)
1804 void ata_port_flush_task(struct ata_port
*ap
)
1808 cancel_rearming_delayed_work(&ap
->port_task
);
1810 if (ata_msg_ctl(ap
))
1811 ata_port_printk(ap
, KERN_DEBUG
, "%s: EXIT\n", __func__
);
1814 static void ata_qc_complete_internal(struct ata_queued_cmd
*qc
)
1816 struct completion
*waiting
= qc
->private_data
;
1822 * ata_exec_internal_sg - execute libata internal command
1823 * @dev: Device to which the command is sent
1824 * @tf: Taskfile registers for the command and the result
1825 * @cdb: CDB for packet command
1826 * @dma_dir: Data tranfer direction of the command
1827 * @sgl: sg list for the data buffer of the command
1828 * @n_elem: Number of sg entries
1829 * @timeout: Timeout in msecs (0 for default)
1831 * Executes libata internal command with timeout. @tf contains
1832 * command on entry and result on return. Timeout and error
1833 * conditions are reported via return value. No recovery action
1834 * is taken after a command times out. It's caller's duty to
1835 * clean up after timeout.
1838 * None. Should be called with kernel context, might sleep.
1841 * Zero on success, AC_ERR_* mask on failure
1843 unsigned ata_exec_internal_sg(struct ata_device
*dev
,
1844 struct ata_taskfile
*tf
, const u8
*cdb
,
1845 int dma_dir
, struct scatterlist
*sgl
,
1846 unsigned int n_elem
, unsigned long timeout
)
1848 struct ata_link
*link
= dev
->link
;
1849 struct ata_port
*ap
= link
->ap
;
1850 u8 command
= tf
->command
;
1851 struct ata_queued_cmd
*qc
;
1852 unsigned int tag
, preempted_tag
;
1853 u32 preempted_sactive
, preempted_qc_active
;
1854 int preempted_nr_active_links
;
1855 DECLARE_COMPLETION_ONSTACK(wait
);
1856 unsigned long flags
;
1857 unsigned int err_mask
;
1860 spin_lock_irqsave(ap
->lock
, flags
);
1862 /* no internal command while frozen */
1863 if (ap
->pflags
& ATA_PFLAG_FROZEN
) {
1864 spin_unlock_irqrestore(ap
->lock
, flags
);
1865 return AC_ERR_SYSTEM
;
1868 /* initialize internal qc */
1870 /* XXX: Tag 0 is used for drivers with legacy EH as some
1871 * drivers choke if any other tag is given. This breaks
1872 * ata_tag_internal() test for those drivers. Don't use new
1873 * EH stuff without converting to it.
1875 if (ap
->ops
->error_handler
)
1876 tag
= ATA_TAG_INTERNAL
;
1880 if (test_and_set_bit(tag
, &ap
->qc_allocated
))
1882 qc
= __ata_qc_from_tag(ap
, tag
);
1890 preempted_tag
= link
->active_tag
;
1891 preempted_sactive
= link
->sactive
;
1892 preempted_qc_active
= ap
->qc_active
;
1893 preempted_nr_active_links
= ap
->nr_active_links
;
1894 link
->active_tag
= ATA_TAG_POISON
;
1897 ap
->nr_active_links
= 0;
1899 /* prepare & issue qc */
1902 memcpy(qc
->cdb
, cdb
, ATAPI_CDB_LEN
);
1903 qc
->flags
|= ATA_QCFLAG_RESULT_TF
;
1904 qc
->dma_dir
= dma_dir
;
1905 if (dma_dir
!= DMA_NONE
) {
1906 unsigned int i
, buflen
= 0;
1907 struct scatterlist
*sg
;
1909 for_each_sg(sgl
, sg
, n_elem
, i
)
1910 buflen
+= sg
->length
;
1912 ata_sg_init(qc
, sgl
, n_elem
);
1913 qc
->nbytes
= buflen
;
1916 qc
->private_data
= &wait
;
1917 qc
->complete_fn
= ata_qc_complete_internal
;
1921 spin_unlock_irqrestore(ap
->lock
, flags
);
1924 timeout
= ata_probe_timeout
* 1000 / HZ
;
1926 rc
= wait_for_completion_timeout(&wait
, msecs_to_jiffies(timeout
));
1928 ata_port_flush_task(ap
);
1931 spin_lock_irqsave(ap
->lock
, flags
);
1933 /* We're racing with irq here. If we lose, the
1934 * following test prevents us from completing the qc
1935 * twice. If we win, the port is frozen and will be
1936 * cleaned up by ->post_internal_cmd().
1938 if (qc
->flags
& ATA_QCFLAG_ACTIVE
) {
1939 qc
->err_mask
|= AC_ERR_TIMEOUT
;
1941 if (ap
->ops
->error_handler
)
1942 ata_port_freeze(ap
);
1944 ata_qc_complete(qc
);
1946 if (ata_msg_warn(ap
))
1947 ata_dev_printk(dev
, KERN_WARNING
,
1948 "qc timeout (cmd 0x%x)\n", command
);
1951 spin_unlock_irqrestore(ap
->lock
, flags
);
1954 /* do post_internal_cmd */
1955 if (ap
->ops
->post_internal_cmd
)
1956 ap
->ops
->post_internal_cmd(qc
);
1958 /* perform minimal error analysis */
1959 if (qc
->flags
& ATA_QCFLAG_FAILED
) {
1960 if (qc
->result_tf
.command
& (ATA_ERR
| ATA_DF
))
1961 qc
->err_mask
|= AC_ERR_DEV
;
1964 qc
->err_mask
|= AC_ERR_OTHER
;
1966 if (qc
->err_mask
& ~AC_ERR_OTHER
)
1967 qc
->err_mask
&= ~AC_ERR_OTHER
;
1971 spin_lock_irqsave(ap
->lock
, flags
);
1973 *tf
= qc
->result_tf
;
1974 err_mask
= qc
->err_mask
;
1977 link
->active_tag
= preempted_tag
;
1978 link
->sactive
= preempted_sactive
;
1979 ap
->qc_active
= preempted_qc_active
;
1980 ap
->nr_active_links
= preempted_nr_active_links
;
1982 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1983 * Until those drivers are fixed, we detect the condition
1984 * here, fail the command with AC_ERR_SYSTEM and reenable the
1987 * Note that this doesn't change any behavior as internal
1988 * command failure results in disabling the device in the
1989 * higher layer for LLDDs without new reset/EH callbacks.
1991 * Kill the following code as soon as those drivers are fixed.
1993 if (ap
->flags
& ATA_FLAG_DISABLED
) {
1994 err_mask
|= AC_ERR_SYSTEM
;
1998 spin_unlock_irqrestore(ap
->lock
, flags
);
2004 * ata_exec_internal - execute libata internal command
2005 * @dev: Device to which the command is sent
2006 * @tf: Taskfile registers for the command and the result
2007 * @cdb: CDB for packet command
2008 * @dma_dir: Data tranfer direction of the command
2009 * @buf: Data buffer of the command
2010 * @buflen: Length of data buffer
2011 * @timeout: Timeout in msecs (0 for default)
2013 * Wrapper around ata_exec_internal_sg() which takes simple
2014 * buffer instead of sg list.
2017 * None. Should be called with kernel context, might sleep.
2020 * Zero on success, AC_ERR_* mask on failure
2022 unsigned ata_exec_internal(struct ata_device
*dev
,
2023 struct ata_taskfile
*tf
, const u8
*cdb
,
2024 int dma_dir
, void *buf
, unsigned int buflen
,
2025 unsigned long timeout
)
2027 struct scatterlist
*psg
= NULL
, sg
;
2028 unsigned int n_elem
= 0;
2030 if (dma_dir
!= DMA_NONE
) {
2032 sg_init_one(&sg
, buf
, buflen
);
2037 return ata_exec_internal_sg(dev
, tf
, cdb
, dma_dir
, psg
, n_elem
,
2042 * ata_do_simple_cmd - execute simple internal command
2043 * @dev: Device to which the command is sent
2044 * @cmd: Opcode to execute
2046 * Execute a 'simple' command, that only consists of the opcode
2047 * 'cmd' itself, without filling any other registers
2050 * Kernel thread context (may sleep).
2053 * Zero on success, AC_ERR_* mask on failure
2055 unsigned int ata_do_simple_cmd(struct ata_device
*dev
, u8 cmd
)
2057 struct ata_taskfile tf
;
2059 ata_tf_init(dev
, &tf
);
2062 tf
.flags
|= ATA_TFLAG_DEVICE
;
2063 tf
.protocol
= ATA_PROT_NODATA
;
2065 return ata_exec_internal(dev
, &tf
, NULL
, DMA_NONE
, NULL
, 0, 0);
2069 * ata_pio_need_iordy - check if iordy needed
2072 * Check if the current speed of the device requires IORDY. Used
2073 * by various controllers for chip configuration.
2076 unsigned int ata_pio_need_iordy(const struct ata_device
*adev
)
2078 /* Controller doesn't support IORDY. Probably a pointless check
2079 as the caller should know this */
2080 if (adev
->link
->ap
->flags
& ATA_FLAG_NO_IORDY
)
2082 /* PIO3 and higher it is mandatory */
2083 if (adev
->pio_mode
> XFER_PIO_2
)
2085 /* We turn it on when possible */
2086 if (ata_id_has_iordy(adev
->id
))
2092 * ata_pio_mask_no_iordy - Return the non IORDY mask
2095 * Compute the highest mode possible if we are not using iordy. Return
2096 * -1 if no iordy mode is available.
2099 static u32
ata_pio_mask_no_iordy(const struct ata_device
*adev
)
2101 /* If we have no drive specific rule, then PIO 2 is non IORDY */
2102 if (adev
->id
[ATA_ID_FIELD_VALID
] & 2) { /* EIDE */
2103 u16 pio
= adev
->id
[ATA_ID_EIDE_PIO
];
2104 /* Is the speed faster than the drive allows non IORDY ? */
2106 /* This is cycle times not frequency - watch the logic! */
2107 if (pio
> 240) /* PIO2 is 240nS per cycle */
2108 return 3 << ATA_SHIFT_PIO
;
2109 return 7 << ATA_SHIFT_PIO
;
2112 return 3 << ATA_SHIFT_PIO
;
2116 * ata_dev_read_id - Read ID data from the specified device
2117 * @dev: target device
2118 * @p_class: pointer to class of the target device (may be changed)
2119 * @flags: ATA_READID_* flags
2120 * @id: buffer to read IDENTIFY data into
2122 * Read ID data from the specified device. ATA_CMD_ID_ATA is
2123 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
2124 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
2125 * for pre-ATA4 drives.
2127 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
2128 * now we abort if we hit that case.
2131 * Kernel thread context (may sleep)
2134 * 0 on success, -errno otherwise.
2136 int ata_dev_read_id(struct ata_device
*dev
, unsigned int *p_class
,
2137 unsigned int flags
, u16
*id
)
2139 struct ata_port
*ap
= dev
->link
->ap
;
2140 unsigned int class = *p_class
;
2141 struct ata_taskfile tf
;
2142 unsigned int err_mask
= 0;
2144 int may_fallback
= 1, tried_spinup
= 0;
2147 if (ata_msg_ctl(ap
))
2148 ata_dev_printk(dev
, KERN_DEBUG
, "%s: ENTER\n", __func__
);
2150 ata_dev_select(ap
, dev
->devno
, 1, 1); /* select device 0/1 */
2152 ata_tf_init(dev
, &tf
);
2156 tf
.command
= ATA_CMD_ID_ATA
;
2159 tf
.command
= ATA_CMD_ID_ATAPI
;
2163 reason
= "unsupported class";
2167 tf
.protocol
= ATA_PROT_PIO
;
2169 /* Some devices choke if TF registers contain garbage. Make
2170 * sure those are properly initialized.
2172 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
2174 /* Device presence detection is unreliable on some
2175 * controllers. Always poll IDENTIFY if available.
2177 tf
.flags
|= ATA_TFLAG_POLLING
;
2179 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_FROM_DEVICE
,
2180 id
, sizeof(id
[0]) * ATA_ID_WORDS
, 0);
2182 if (err_mask
& AC_ERR_NODEV_HINT
) {
2183 ata_dev_printk(dev
, KERN_DEBUG
,
2184 "NODEV after polling detection\n");
2188 if ((err_mask
== AC_ERR_DEV
) && (tf
.feature
& ATA_ABORTED
)) {
2189 /* Device or controller might have reported
2190 * the wrong device class. Give a shot at the
2191 * other IDENTIFY if the current one is
2192 * aborted by the device.
2197 if (class == ATA_DEV_ATA
)
2198 class = ATA_DEV_ATAPI
;
2200 class = ATA_DEV_ATA
;
2204 /* Control reaches here iff the device aborted
2205 * both flavors of IDENTIFYs which happens
2206 * sometimes with phantom devices.
2208 ata_dev_printk(dev
, KERN_DEBUG
,
2209 "both IDENTIFYs aborted, assuming NODEV\n");
2214 reason
= "I/O error";
2218 /* Falling back doesn't make sense if ID data was read
2219 * successfully at least once.
2223 swap_buf_le16(id
, ATA_ID_WORDS
);
2227 reason
= "device reports invalid type";
2229 if (class == ATA_DEV_ATA
) {
2230 if (!ata_id_is_ata(id
) && !ata_id_is_cfa(id
))
2233 if (ata_id_is_ata(id
))
2237 if (!tried_spinup
&& (id
[2] == 0x37c8 || id
[2] == 0x738c)) {
2240 * Drive powered-up in standby mode, and requires a specific
2241 * SET_FEATURES spin-up subcommand before it will accept
2242 * anything other than the original IDENTIFY command.
2244 err_mask
= ata_dev_set_feature(dev
, SETFEATURES_SPINUP
, 0);
2245 if (err_mask
&& id
[2] != 0x738c) {
2247 reason
= "SPINUP failed";
2251 * If the drive initially returned incomplete IDENTIFY info,
2252 * we now must reissue the IDENTIFY command.
2254 if (id
[2] == 0x37c8)
2258 if ((flags
& ATA_READID_POSTRESET
) && class == ATA_DEV_ATA
) {
2260 * The exact sequence expected by certain pre-ATA4 drives is:
2262 * IDENTIFY (optional in early ATA)
2263 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2265 * Some drives were very specific about that exact sequence.
2267 * Note that ATA4 says lba is mandatory so the second check
2268 * shoud never trigger.
2270 if (ata_id_major_version(id
) < 4 || !ata_id_has_lba(id
)) {
2271 err_mask
= ata_dev_init_params(dev
, id
[3], id
[6]);
2274 reason
= "INIT_DEV_PARAMS failed";
2278 /* current CHS translation info (id[53-58]) might be
2279 * changed. reread the identify device info.
2281 flags
&= ~ATA_READID_POSTRESET
;
2291 if (ata_msg_warn(ap
))
2292 ata_dev_printk(dev
, KERN_WARNING
, "failed to IDENTIFY "
2293 "(%s, err_mask=0x%x)\n", reason
, err_mask
);
2297 static inline u8
ata_dev_knobble(struct ata_device
*dev
)
2299 struct ata_port
*ap
= dev
->link
->ap
;
2300 return ((ap
->cbl
== ATA_CBL_SATA
) && (!ata_id_is_sata(dev
->id
)));
2303 static void ata_dev_config_ncq(struct ata_device
*dev
,
2304 char *desc
, size_t desc_sz
)
2306 struct ata_port
*ap
= dev
->link
->ap
;
2307 int hdepth
= 0, ddepth
= ata_id_queue_depth(dev
->id
);
2309 if (!ata_id_has_ncq(dev
->id
)) {
2313 if (dev
->horkage
& ATA_HORKAGE_NONCQ
) {
2314 snprintf(desc
, desc_sz
, "NCQ (not used)");
2317 if (ap
->flags
& ATA_FLAG_NCQ
) {
2318 hdepth
= min(ap
->scsi_host
->can_queue
, ATA_MAX_QUEUE
- 1);
2319 dev
->flags
|= ATA_DFLAG_NCQ
;
2322 if (hdepth
>= ddepth
)
2323 snprintf(desc
, desc_sz
, "NCQ (depth %d)", ddepth
);
2325 snprintf(desc
, desc_sz
, "NCQ (depth %d/%d)", hdepth
, ddepth
);
2329 * ata_dev_configure - Configure the specified ATA/ATAPI device
2330 * @dev: Target device to configure
2332 * Configure @dev according to @dev->id. Generic and low-level
2333 * driver specific fixups are also applied.
2336 * Kernel thread context (may sleep)
2339 * 0 on success, -errno otherwise
2341 int ata_dev_configure(struct ata_device
*dev
)
2343 struct ata_port
*ap
= dev
->link
->ap
;
2344 struct ata_eh_context
*ehc
= &dev
->link
->eh_context
;
2345 int print_info
= ehc
->i
.flags
& ATA_EHI_PRINTINFO
;
2346 const u16
*id
= dev
->id
;
2347 unsigned long xfer_mask
;
2348 char revbuf
[7]; /* XYZ-99\0 */
2349 char fwrevbuf
[ATA_ID_FW_REV_LEN
+1];
2350 char modelbuf
[ATA_ID_PROD_LEN
+1];
2353 if (!ata_dev_enabled(dev
) && ata_msg_info(ap
)) {
2354 ata_dev_printk(dev
, KERN_INFO
, "%s: ENTER/EXIT -- nodev\n",
2359 if (ata_msg_probe(ap
))
2360 ata_dev_printk(dev
, KERN_DEBUG
, "%s: ENTER\n", __func__
);
2363 dev
->horkage
|= ata_dev_blacklisted(dev
);
2364 ata_force_horkage(dev
);
2366 /* let ACPI work its magic */
2367 rc
= ata_acpi_on_devcfg(dev
);
2371 /* massage HPA, do it early as it might change IDENTIFY data */
2372 rc
= ata_hpa_resize(dev
);
2376 /* print device capabilities */
2377 if (ata_msg_probe(ap
))
2378 ata_dev_printk(dev
, KERN_DEBUG
,
2379 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2380 "85:%04x 86:%04x 87:%04x 88:%04x\n",
2382 id
[49], id
[82], id
[83], id
[84],
2383 id
[85], id
[86], id
[87], id
[88]);
2385 /* initialize to-be-configured parameters */
2386 dev
->flags
&= ~ATA_DFLAG_CFG_MASK
;
2387 dev
->max_sectors
= 0;
2395 * common ATA, ATAPI feature tests
2398 /* find max transfer mode; for printk only */
2399 xfer_mask
= ata_id_xfermask(id
);
2401 if (ata_msg_probe(ap
))
2404 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2405 ata_id_c_string(dev
->id
, fwrevbuf
, ATA_ID_FW_REV
,
2408 ata_id_c_string(dev
->id
, modelbuf
, ATA_ID_PROD
,
2411 /* ATA-specific feature tests */
2412 if (dev
->class == ATA_DEV_ATA
) {
2413 if (ata_id_is_cfa(id
)) {
2414 if (id
[162] & 1) /* CPRM may make this media unusable */
2415 ata_dev_printk(dev
, KERN_WARNING
,
2416 "supports DRM functions and may "
2417 "not be fully accessable.\n");
2418 snprintf(revbuf
, 7, "CFA");
2420 snprintf(revbuf
, 7, "ATA-%d", ata_id_major_version(id
));
2421 /* Warn the user if the device has TPM extensions */
2422 if (ata_id_has_tpm(id
))
2423 ata_dev_printk(dev
, KERN_WARNING
,
2424 "supports DRM functions and may "
2425 "not be fully accessable.\n");
2428 dev
->n_sectors
= ata_id_n_sectors(id
);
2430 if (dev
->id
[59] & 0x100)
2431 dev
->multi_count
= dev
->id
[59] & 0xff;
2433 if (ata_id_has_lba(id
)) {
2434 const char *lba_desc
;
2438 dev
->flags
|= ATA_DFLAG_LBA
;
2439 if (ata_id_has_lba48(id
)) {
2440 dev
->flags
|= ATA_DFLAG_LBA48
;
2443 if (dev
->n_sectors
>= (1UL << 28) &&
2444 ata_id_has_flush_ext(id
))
2445 dev
->flags
|= ATA_DFLAG_FLUSH_EXT
;
2449 ata_dev_config_ncq(dev
, ncq_desc
, sizeof(ncq_desc
));
2451 /* print device info to dmesg */
2452 if (ata_msg_drv(ap
) && print_info
) {
2453 ata_dev_printk(dev
, KERN_INFO
,
2454 "%s: %s, %s, max %s\n",
2455 revbuf
, modelbuf
, fwrevbuf
,
2456 ata_mode_string(xfer_mask
));
2457 ata_dev_printk(dev
, KERN_INFO
,
2458 "%Lu sectors, multi %u: %s %s\n",
2459 (unsigned long long)dev
->n_sectors
,
2460 dev
->multi_count
, lba_desc
, ncq_desc
);
2465 /* Default translation */
2466 dev
->cylinders
= id
[1];
2468 dev
->sectors
= id
[6];
2470 if (ata_id_current_chs_valid(id
)) {
2471 /* Current CHS translation is valid. */
2472 dev
->cylinders
= id
[54];
2473 dev
->heads
= id
[55];
2474 dev
->sectors
= id
[56];
2477 /* print device info to dmesg */
2478 if (ata_msg_drv(ap
) && print_info
) {
2479 ata_dev_printk(dev
, KERN_INFO
,
2480 "%s: %s, %s, max %s\n",
2481 revbuf
, modelbuf
, fwrevbuf
,
2482 ata_mode_string(xfer_mask
));
2483 ata_dev_printk(dev
, KERN_INFO
,
2484 "%Lu sectors, multi %u, CHS %u/%u/%u\n",
2485 (unsigned long long)dev
->n_sectors
,
2486 dev
->multi_count
, dev
->cylinders
,
2487 dev
->heads
, dev
->sectors
);
2494 /* ATAPI-specific feature tests */
2495 else if (dev
->class == ATA_DEV_ATAPI
) {
2496 const char *cdb_intr_string
= "";
2497 const char *atapi_an_string
= "";
2498 const char *dma_dir_string
= "";
2501 rc
= atapi_cdb_len(id
);
2502 if ((rc
< 12) || (rc
> ATAPI_CDB_LEN
)) {
2503 if (ata_msg_warn(ap
))
2504 ata_dev_printk(dev
, KERN_WARNING
,
2505 "unsupported CDB len\n");
2509 dev
->cdb_len
= (unsigned int) rc
;
2511 /* Enable ATAPI AN if both the host and device have
2512 * the support. If PMP is attached, SNTF is required
2513 * to enable ATAPI AN to discern between PHY status
2514 * changed notifications and ATAPI ANs.
2516 if ((ap
->flags
& ATA_FLAG_AN
) && ata_id_has_atapi_AN(id
) &&
2517 (!ap
->nr_pmp_links
||
2518 sata_scr_read(&ap
->link
, SCR_NOTIFICATION
, &sntf
) == 0)) {
2519 unsigned int err_mask
;
2521 /* issue SET feature command to turn this on */
2522 err_mask
= ata_dev_set_feature(dev
,
2523 SETFEATURES_SATA_ENABLE
, SATA_AN
);
2525 ata_dev_printk(dev
, KERN_ERR
,
2526 "failed to enable ATAPI AN "
2527 "(err_mask=0x%x)\n", err_mask
);
2529 dev
->flags
|= ATA_DFLAG_AN
;
2530 atapi_an_string
= ", ATAPI AN";
2534 if (ata_id_cdb_intr(dev
->id
)) {
2535 dev
->flags
|= ATA_DFLAG_CDB_INTR
;
2536 cdb_intr_string
= ", CDB intr";
2539 if (atapi_dmadir
|| atapi_id_dmadir(dev
->id
)) {
2540 dev
->flags
|= ATA_DFLAG_DMADIR
;
2541 dma_dir_string
= ", DMADIR";
2544 /* print device info to dmesg */
2545 if (ata_msg_drv(ap
) && print_info
)
2546 ata_dev_printk(dev
, KERN_INFO
,
2547 "ATAPI: %s, %s, max %s%s%s%s\n",
2549 ata_mode_string(xfer_mask
),
2550 cdb_intr_string
, atapi_an_string
,
2554 /* determine max_sectors */
2555 dev
->max_sectors
= ATA_MAX_SECTORS
;
2556 if (dev
->flags
& ATA_DFLAG_LBA48
)
2557 dev
->max_sectors
= ATA_MAX_SECTORS_LBA48
;
2559 if (!(dev
->horkage
& ATA_HORKAGE_IPM
)) {
2560 if (ata_id_has_hipm(dev
->id
))
2561 dev
->flags
|= ATA_DFLAG_HIPM
;
2562 if (ata_id_has_dipm(dev
->id
))
2563 dev
->flags
|= ATA_DFLAG_DIPM
;
2566 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2568 if (ata_dev_knobble(dev
)) {
2569 if (ata_msg_drv(ap
) && print_info
)
2570 ata_dev_printk(dev
, KERN_INFO
,
2571 "applying bridge limits\n");
2572 dev
->udma_mask
&= ATA_UDMA5
;
2573 dev
->max_sectors
= ATA_MAX_SECTORS
;
2576 if ((dev
->class == ATA_DEV_ATAPI
) &&
2577 (atapi_command_packet_set(id
) == TYPE_TAPE
)) {
2578 dev
->max_sectors
= ATA_MAX_SECTORS_TAPE
;
2579 dev
->horkage
|= ATA_HORKAGE_STUCK_ERR
;
2582 if (dev
->horkage
& ATA_HORKAGE_MAX_SEC_128
)
2583 dev
->max_sectors
= min_t(unsigned int, ATA_MAX_SECTORS_128
,
2586 if (ata_dev_blacklisted(dev
) & ATA_HORKAGE_IPM
) {
2587 dev
->horkage
|= ATA_HORKAGE_IPM
;
2589 /* reset link pm_policy for this port to no pm */
2590 ap
->pm_policy
= MAX_PERFORMANCE
;
2593 if (ap
->ops
->dev_config
)
2594 ap
->ops
->dev_config(dev
);
2596 if (dev
->horkage
& ATA_HORKAGE_DIAGNOSTIC
) {
2597 /* Let the user know. We don't want to disallow opens for
2598 rescue purposes, or in case the vendor is just a blithering
2599 idiot. Do this after the dev_config call as some controllers
2600 with buggy firmware may want to avoid reporting false device
2604 ata_dev_printk(dev
, KERN_WARNING
,
2605 "Drive reports diagnostics failure. This may indicate a drive\n");
2606 ata_dev_printk(dev
, KERN_WARNING
,
2607 "fault or invalid emulation. Contact drive vendor for information.\n");
2611 if (ata_msg_probe(ap
))
2612 ata_dev_printk(dev
, KERN_DEBUG
, "%s: EXIT, drv_stat = 0x%x\n",
2613 __func__
, ata_chk_status(ap
));
2617 if (ata_msg_probe(ap
))
2618 ata_dev_printk(dev
, KERN_DEBUG
,
2619 "%s: EXIT, err\n", __func__
);
2624 * ata_cable_40wire - return 40 wire cable type
2627 * Helper method for drivers which want to hardwire 40 wire cable
2631 int ata_cable_40wire(struct ata_port
*ap
)
2633 return ATA_CBL_PATA40
;
2637 * ata_cable_80wire - return 80 wire cable type
2640 * Helper method for drivers which want to hardwire 80 wire cable
2644 int ata_cable_80wire(struct ata_port
*ap
)
2646 return ATA_CBL_PATA80
;
2650 * ata_cable_unknown - return unknown PATA cable.
2653 * Helper method for drivers which have no PATA cable detection.
2656 int ata_cable_unknown(struct ata_port
*ap
)
2658 return ATA_CBL_PATA_UNK
;
2662 * ata_cable_ignore - return ignored PATA cable.
2665 * Helper method for drivers which don't use cable type to limit
2668 int ata_cable_ignore(struct ata_port
*ap
)
2670 return ATA_CBL_PATA_IGN
;
2674 * ata_cable_sata - return SATA cable type
2677 * Helper method for drivers which have SATA cables
2680 int ata_cable_sata(struct ata_port
*ap
)
2682 return ATA_CBL_SATA
;
2686 * ata_bus_probe - Reset and probe ATA bus
2689 * Master ATA bus probing function. Initiates a hardware-dependent
2690 * bus reset, then attempts to identify any devices found on
2694 * PCI/etc. bus probe sem.
2697 * Zero on success, negative errno otherwise.
2700 int ata_bus_probe(struct ata_port
*ap
)
2702 unsigned int classes
[ATA_MAX_DEVICES
];
2703 int tries
[ATA_MAX_DEVICES
];
2705 struct ata_device
*dev
;
2709 ata_link_for_each_dev(dev
, &ap
->link
)
2710 tries
[dev
->devno
] = ATA_PROBE_MAX_TRIES
;
2713 ata_link_for_each_dev(dev
, &ap
->link
) {
2714 /* If we issue an SRST then an ATA drive (not ATAPI)
2715 * may change configuration and be in PIO0 timing. If
2716 * we do a hard reset (or are coming from power on)
2717 * this is true for ATA or ATAPI. Until we've set a
2718 * suitable controller mode we should not touch the
2719 * bus as we may be talking too fast.
2721 dev
->pio_mode
= XFER_PIO_0
;
2723 /* If the controller has a pio mode setup function
2724 * then use it to set the chipset to rights. Don't
2725 * touch the DMA setup as that will be dealt with when
2726 * configuring devices.
2728 if (ap
->ops
->set_piomode
)
2729 ap
->ops
->set_piomode(ap
, dev
);
2732 /* reset and determine device classes */
2733 ap
->ops
->phy_reset(ap
);
2735 ata_link_for_each_dev(dev
, &ap
->link
) {
2736 if (!(ap
->flags
& ATA_FLAG_DISABLED
) &&
2737 dev
->class != ATA_DEV_UNKNOWN
)
2738 classes
[dev
->devno
] = dev
->class;
2740 classes
[dev
->devno
] = ATA_DEV_NONE
;
2742 dev
->class = ATA_DEV_UNKNOWN
;
2747 /* read IDENTIFY page and configure devices. We have to do the identify
2748 specific sequence bass-ackwards so that PDIAG- is released by
2751 ata_link_for_each_dev_reverse(dev
, &ap
->link
) {
2752 if (tries
[dev
->devno
])
2753 dev
->class = classes
[dev
->devno
];
2755 if (!ata_dev_enabled(dev
))
2758 rc
= ata_dev_read_id(dev
, &dev
->class, ATA_READID_POSTRESET
,
2764 /* Now ask for the cable type as PDIAG- should have been released */
2765 if (ap
->ops
->cable_detect
)
2766 ap
->cbl
= ap
->ops
->cable_detect(ap
);
2768 /* We may have SATA bridge glue hiding here irrespective of the
2769 reported cable types and sensed types */
2770 ata_link_for_each_dev(dev
, &ap
->link
) {
2771 if (!ata_dev_enabled(dev
))
2773 /* SATA drives indicate we have a bridge. We don't know which
2774 end of the link the bridge is which is a problem */
2775 if (ata_id_is_sata(dev
->id
))
2776 ap
->cbl
= ATA_CBL_SATA
;
2779 /* After the identify sequence we can now set up the devices. We do
2780 this in the normal order so that the user doesn't get confused */
2782 ata_link_for_each_dev(dev
, &ap
->link
) {
2783 if (!ata_dev_enabled(dev
))
2786 ap
->link
.eh_context
.i
.flags
|= ATA_EHI_PRINTINFO
;
2787 rc
= ata_dev_configure(dev
);
2788 ap
->link
.eh_context
.i
.flags
&= ~ATA_EHI_PRINTINFO
;
2793 /* configure transfer mode */
2794 rc
= ata_set_mode(&ap
->link
, &dev
);
2798 ata_link_for_each_dev(dev
, &ap
->link
)
2799 if (ata_dev_enabled(dev
))
2802 /* no device present, disable port */
2803 ata_port_disable(ap
);
2807 tries
[dev
->devno
]--;
2811 /* eeek, something went very wrong, give up */
2812 tries
[dev
->devno
] = 0;
2816 /* give it just one more chance */
2817 tries
[dev
->devno
] = min(tries
[dev
->devno
], 1);
2819 if (tries
[dev
->devno
] == 1) {
2820 /* This is the last chance, better to slow
2821 * down than lose it.
2823 sata_down_spd_limit(&ap
->link
);
2824 ata_down_xfermask_limit(dev
, ATA_DNXFER_PIO
);
2828 if (!tries
[dev
->devno
])
2829 ata_dev_disable(dev
);
2835 * ata_port_probe - Mark port as enabled
2836 * @ap: Port for which we indicate enablement
2838 * Modify @ap data structure such that the system
2839 * thinks that the entire port is enabled.
2841 * LOCKING: host lock, or some other form of
2845 void ata_port_probe(struct ata_port
*ap
)
2847 ap
->flags
&= ~ATA_FLAG_DISABLED
;
2851 * sata_print_link_status - Print SATA link status
2852 * @link: SATA link to printk link status about
2854 * This function prints link speed and status of a SATA link.
2859 void sata_print_link_status(struct ata_link
*link
)
2861 u32 sstatus
, scontrol
, tmp
;
2863 if (sata_scr_read(link
, SCR_STATUS
, &sstatus
))
2865 sata_scr_read(link
, SCR_CONTROL
, &scontrol
);
2867 if (ata_link_online(link
)) {
2868 tmp
= (sstatus
>> 4) & 0xf;
2869 ata_link_printk(link
, KERN_INFO
,
2870 "SATA link up %s (SStatus %X SControl %X)\n",
2871 sata_spd_string(tmp
), sstatus
, scontrol
);
2873 ata_link_printk(link
, KERN_INFO
,
2874 "SATA link down (SStatus %X SControl %X)\n",
2880 * ata_dev_pair - return other device on cable
2883 * Obtain the other device on the same cable, or if none is
2884 * present NULL is returned
2887 struct ata_device
*ata_dev_pair(struct ata_device
*adev
)
2889 struct ata_link
*link
= adev
->link
;
2890 struct ata_device
*pair
= &link
->device
[1 - adev
->devno
];
2891 if (!ata_dev_enabled(pair
))
2897 * ata_port_disable - Disable port.
2898 * @ap: Port to be disabled.
2900 * Modify @ap data structure such that the system
2901 * thinks that the entire port is disabled, and should
2902 * never attempt to probe or communicate with devices
2905 * LOCKING: host lock, or some other form of
2909 void ata_port_disable(struct ata_port
*ap
)
2911 ap
->link
.device
[0].class = ATA_DEV_NONE
;
2912 ap
->link
.device
[1].class = ATA_DEV_NONE
;
2913 ap
->flags
|= ATA_FLAG_DISABLED
;
2917 * sata_down_spd_limit - adjust SATA spd limit downward
2918 * @link: Link to adjust SATA spd limit for
2920 * Adjust SATA spd limit of @link downward. Note that this
2921 * function only adjusts the limit. The change must be applied
2922 * using sata_set_spd().
2925 * Inherited from caller.
2928 * 0 on success, negative errno on failure
2930 int sata_down_spd_limit(struct ata_link
*link
)
2932 u32 sstatus
, spd
, mask
;
2935 if (!sata_scr_valid(link
))
2938 /* If SCR can be read, use it to determine the current SPD.
2939 * If not, use cached value in link->sata_spd.
2941 rc
= sata_scr_read(link
, SCR_STATUS
, &sstatus
);
2943 spd
= (sstatus
>> 4) & 0xf;
2945 spd
= link
->sata_spd
;
2947 mask
= link
->sata_spd_limit
;
2951 /* unconditionally mask off the highest bit */
2952 highbit
= fls(mask
) - 1;
2953 mask
&= ~(1 << highbit
);
2955 /* Mask off all speeds higher than or equal to the current
2956 * one. Force 1.5Gbps if current SPD is not available.
2959 mask
&= (1 << (spd
- 1)) - 1;
2963 /* were we already at the bottom? */
2967 link
->sata_spd_limit
= mask
;
2969 ata_link_printk(link
, KERN_WARNING
, "limiting SATA link speed to %s\n",
2970 sata_spd_string(fls(mask
)));
2975 static int __sata_set_spd_needed(struct ata_link
*link
, u32
*scontrol
)
2977 struct ata_link
*host_link
= &link
->ap
->link
;
2978 u32 limit
, target
, spd
;
2980 limit
= link
->sata_spd_limit
;
2982 /* Don't configure downstream link faster than upstream link.
2983 * It doesn't speed up anything and some PMPs choke on such
2986 if (!ata_is_host_link(link
) && host_link
->sata_spd
)
2987 limit
&= (1 << host_link
->sata_spd
) - 1;
2989 if (limit
== UINT_MAX
)
2992 target
= fls(limit
);
2994 spd
= (*scontrol
>> 4) & 0xf;
2995 *scontrol
= (*scontrol
& ~0xf0) | ((target
& 0xf) << 4);
2997 return spd
!= target
;
3001 * sata_set_spd_needed - is SATA spd configuration needed
3002 * @link: Link in question
3004 * Test whether the spd limit in SControl matches
3005 * @link->sata_spd_limit. This function is used to determine
3006 * whether hardreset is necessary to apply SATA spd
3010 * Inherited from caller.
3013 * 1 if SATA spd configuration is needed, 0 otherwise.
3015 int sata_set_spd_needed(struct ata_link
*link
)
3019 if (sata_scr_read(link
, SCR_CONTROL
, &scontrol
))
3022 return __sata_set_spd_needed(link
, &scontrol
);
3026 * sata_set_spd - set SATA spd according to spd limit
3027 * @link: Link to set SATA spd for
3029 * Set SATA spd of @link according to sata_spd_limit.
3032 * Inherited from caller.
3035 * 0 if spd doesn't need to be changed, 1 if spd has been
3036 * changed. Negative errno if SCR registers are inaccessible.
3038 int sata_set_spd(struct ata_link
*link
)
3043 if ((rc
= sata_scr_read(link
, SCR_CONTROL
, &scontrol
)))
3046 if (!__sata_set_spd_needed(link
, &scontrol
))
3049 if ((rc
= sata_scr_write(link
, SCR_CONTROL
, scontrol
)))
3056 * This mode timing computation functionality is ported over from
3057 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
3060 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
3061 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
3062 * for UDMA6, which is currently supported only by Maxtor drives.
3064 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
3067 static const struct ata_timing ata_timing
[] = {
3068 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
3069 { XFER_PIO_0
, 70, 290, 240, 600, 165, 150, 600, 0 },
3070 { XFER_PIO_1
, 50, 290, 93, 383, 125, 100, 383, 0 },
3071 { XFER_PIO_2
, 30, 290, 40, 330, 100, 90, 240, 0 },
3072 { XFER_PIO_3
, 30, 80, 70, 180, 80, 70, 180, 0 },
3073 { XFER_PIO_4
, 25, 70, 25, 120, 70, 25, 120, 0 },
3074 { XFER_PIO_5
, 15, 65, 25, 100, 65, 25, 100, 0 },
3075 { XFER_PIO_6
, 10, 55, 20, 80, 55, 20, 80, 0 },
3077 { XFER_SW_DMA_0
, 120, 0, 0, 0, 480, 480, 960, 0 },
3078 { XFER_SW_DMA_1
, 90, 0, 0, 0, 240, 240, 480, 0 },
3079 { XFER_SW_DMA_2
, 60, 0, 0, 0, 120, 120, 240, 0 },
3081 { XFER_MW_DMA_0
, 60, 0, 0, 0, 215, 215, 480, 0 },
3082 { XFER_MW_DMA_1
, 45, 0, 0, 0, 80, 50, 150, 0 },
3083 { XFER_MW_DMA_2
, 25, 0, 0, 0, 70, 25, 120, 0 },
3084 { XFER_MW_DMA_3
, 25, 0, 0, 0, 65, 25, 100, 0 },
3085 { XFER_MW_DMA_4
, 25, 0, 0, 0, 55, 20, 80, 0 },
3087 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
3088 { XFER_UDMA_0
, 0, 0, 0, 0, 0, 0, 0, 120 },
3089 { XFER_UDMA_1
, 0, 0, 0, 0, 0, 0, 0, 80 },
3090 { XFER_UDMA_2
, 0, 0, 0, 0, 0, 0, 0, 60 },
3091 { XFER_UDMA_3
, 0, 0, 0, 0, 0, 0, 0, 45 },
3092 { XFER_UDMA_4
, 0, 0, 0, 0, 0, 0, 0, 30 },
3093 { XFER_UDMA_5
, 0, 0, 0, 0, 0, 0, 0, 20 },
3094 { XFER_UDMA_6
, 0, 0, 0, 0, 0, 0, 0, 15 },
3099 #define ENOUGH(v, unit) (((v)-1)/(unit)+1)
3100 #define EZ(v, unit) ((v)?ENOUGH(v, unit):0)
3102 static void ata_timing_quantize(const struct ata_timing
*t
, struct ata_timing
*q
, int T
, int UT
)
3104 q
->setup
= EZ(t
->setup
* 1000, T
);
3105 q
->act8b
= EZ(t
->act8b
* 1000, T
);
3106 q
->rec8b
= EZ(t
->rec8b
* 1000, T
);
3107 q
->cyc8b
= EZ(t
->cyc8b
* 1000, T
);
3108 q
->active
= EZ(t
->active
* 1000, T
);
3109 q
->recover
= EZ(t
->recover
* 1000, T
);
3110 q
->cycle
= EZ(t
->cycle
* 1000, T
);
3111 q
->udma
= EZ(t
->udma
* 1000, UT
);
3114 void ata_timing_merge(const struct ata_timing
*a
, const struct ata_timing
*b
,
3115 struct ata_timing
*m
, unsigned int what
)
3117 if (what
& ATA_TIMING_SETUP
) m
->setup
= max(a
->setup
, b
->setup
);
3118 if (what
& ATA_TIMING_ACT8B
) m
->act8b
= max(a
->act8b
, b
->act8b
);
3119 if (what
& ATA_TIMING_REC8B
) m
->rec8b
= max(a
->rec8b
, b
->rec8b
);
3120 if (what
& ATA_TIMING_CYC8B
) m
->cyc8b
= max(a
->cyc8b
, b
->cyc8b
);
3121 if (what
& ATA_TIMING_ACTIVE
) m
->active
= max(a
->active
, b
->active
);
3122 if (what
& ATA_TIMING_RECOVER
) m
->recover
= max(a
->recover
, b
->recover
);
3123 if (what
& ATA_TIMING_CYCLE
) m
->cycle
= max(a
->cycle
, b
->cycle
);
3124 if (what
& ATA_TIMING_UDMA
) m
->udma
= max(a
->udma
, b
->udma
);
3127 const struct ata_timing
*ata_timing_find_mode(u8 xfer_mode
)
3129 const struct ata_timing
*t
= ata_timing
;
3131 while (xfer_mode
> t
->mode
)
3134 if (xfer_mode
== t
->mode
)
3139 int ata_timing_compute(struct ata_device
*adev
, unsigned short speed
,
3140 struct ata_timing
*t
, int T
, int UT
)
3142 const struct ata_timing
*s
;
3143 struct ata_timing p
;
3149 if (!(s
= ata_timing_find_mode(speed
)))
3152 memcpy(t
, s
, sizeof(*s
));
3155 * If the drive is an EIDE drive, it can tell us it needs extended
3156 * PIO/MW_DMA cycle timing.
3159 if (adev
->id
[ATA_ID_FIELD_VALID
] & 2) { /* EIDE drive */
3160 memset(&p
, 0, sizeof(p
));
3161 if (speed
>= XFER_PIO_0
&& speed
<= XFER_SW_DMA_0
) {
3162 if (speed
<= XFER_PIO_2
) p
.cycle
= p
.cyc8b
= adev
->id
[ATA_ID_EIDE_PIO
];
3163 else p
.cycle
= p
.cyc8b
= adev
->id
[ATA_ID_EIDE_PIO_IORDY
];
3164 } else if (speed
>= XFER_MW_DMA_0
&& speed
<= XFER_MW_DMA_2
) {
3165 p
.cycle
= adev
->id
[ATA_ID_EIDE_DMA_MIN
];
3167 ata_timing_merge(&p
, t
, t
, ATA_TIMING_CYCLE
| ATA_TIMING_CYC8B
);
3171 * Convert the timing to bus clock counts.
3174 ata_timing_quantize(t
, t
, T
, UT
);
3177 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3178 * S.M.A.R.T * and some other commands. We have to ensure that the
3179 * DMA cycle timing is slower/equal than the fastest PIO timing.
3182 if (speed
> XFER_PIO_6
) {
3183 ata_timing_compute(adev
, adev
->pio_mode
, &p
, T
, UT
);
3184 ata_timing_merge(&p
, t
, t
, ATA_TIMING_ALL
);
3188 * Lengthen active & recovery time so that cycle time is correct.
3191 if (t
->act8b
+ t
->rec8b
< t
->cyc8b
) {
3192 t
->act8b
+= (t
->cyc8b
- (t
->act8b
+ t
->rec8b
)) / 2;
3193 t
->rec8b
= t
->cyc8b
- t
->act8b
;
3196 if (t
->active
+ t
->recover
< t
->cycle
) {
3197 t
->active
+= (t
->cycle
- (t
->active
+ t
->recover
)) / 2;
3198 t
->recover
= t
->cycle
- t
->active
;
3201 /* In a few cases quantisation may produce enough errors to
3202 leave t->cycle too low for the sum of active and recovery
3203 if so we must correct this */
3204 if (t
->active
+ t
->recover
> t
->cycle
)
3205 t
->cycle
= t
->active
+ t
->recover
;
3211 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3212 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3213 * @cycle: cycle duration in ns
3215 * Return matching xfer mode for @cycle. The returned mode is of
3216 * the transfer type specified by @xfer_shift. If @cycle is too
3217 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3218 * than the fastest known mode, the fasted mode is returned.
3224 * Matching xfer_mode, 0xff if no match found.
3226 u8
ata_timing_cycle2mode(unsigned int xfer_shift
, int cycle
)
3228 u8 base_mode
= 0xff, last_mode
= 0xff;
3229 const struct ata_xfer_ent
*ent
;
3230 const struct ata_timing
*t
;
3232 for (ent
= ata_xfer_tbl
; ent
->shift
>= 0; ent
++)
3233 if (ent
->shift
== xfer_shift
)
3234 base_mode
= ent
->base
;
3236 for (t
= ata_timing_find_mode(base_mode
);
3237 t
&& ata_xfer_mode2shift(t
->mode
) == xfer_shift
; t
++) {
3238 unsigned short this_cycle
;
3240 switch (xfer_shift
) {
3242 case ATA_SHIFT_MWDMA
:
3243 this_cycle
= t
->cycle
;
3245 case ATA_SHIFT_UDMA
:
3246 this_cycle
= t
->udma
;
3252 if (cycle
> this_cycle
)
3255 last_mode
= t
->mode
;
3262 * ata_down_xfermask_limit - adjust dev xfer masks downward
3263 * @dev: Device to adjust xfer masks
3264 * @sel: ATA_DNXFER_* selector
3266 * Adjust xfer masks of @dev downward. Note that this function
3267 * does not apply the change. Invoking ata_set_mode() afterwards
3268 * will apply the limit.
3271 * Inherited from caller.
3274 * 0 on success, negative errno on failure
3276 int ata_down_xfermask_limit(struct ata_device
*dev
, unsigned int sel
)
3279 unsigned long orig_mask
, xfer_mask
;
3280 unsigned long pio_mask
, mwdma_mask
, udma_mask
;
3283 quiet
= !!(sel
& ATA_DNXFER_QUIET
);
3284 sel
&= ~ATA_DNXFER_QUIET
;
3286 xfer_mask
= orig_mask
= ata_pack_xfermask(dev
->pio_mask
,
3289 ata_unpack_xfermask(xfer_mask
, &pio_mask
, &mwdma_mask
, &udma_mask
);
3292 case ATA_DNXFER_PIO
:
3293 highbit
= fls(pio_mask
) - 1;
3294 pio_mask
&= ~(1 << highbit
);
3297 case ATA_DNXFER_DMA
:
3299 highbit
= fls(udma_mask
) - 1;
3300 udma_mask
&= ~(1 << highbit
);
3303 } else if (mwdma_mask
) {
3304 highbit
= fls(mwdma_mask
) - 1;
3305 mwdma_mask
&= ~(1 << highbit
);
3311 case ATA_DNXFER_40C
:
3312 udma_mask
&= ATA_UDMA_MASK_40C
;
3315 case ATA_DNXFER_FORCE_PIO0
:
3317 case ATA_DNXFER_FORCE_PIO
:
3326 xfer_mask
&= ata_pack_xfermask(pio_mask
, mwdma_mask
, udma_mask
);
3328 if (!(xfer_mask
& ATA_MASK_PIO
) || xfer_mask
== orig_mask
)
3332 if (xfer_mask
& (ATA_MASK_MWDMA
| ATA_MASK_UDMA
))
3333 snprintf(buf
, sizeof(buf
), "%s:%s",
3334 ata_mode_string(xfer_mask
),
3335 ata_mode_string(xfer_mask
& ATA_MASK_PIO
));
3337 snprintf(buf
, sizeof(buf
), "%s",
3338 ata_mode_string(xfer_mask
));
3340 ata_dev_printk(dev
, KERN_WARNING
,
3341 "limiting speed to %s\n", buf
);
3344 ata_unpack_xfermask(xfer_mask
, &dev
->pio_mask
, &dev
->mwdma_mask
,
3350 static int ata_dev_set_mode(struct ata_device
*dev
)
3352 struct ata_eh_context
*ehc
= &dev
->link
->eh_context
;
3353 const char *dev_err_whine
= "";
3354 int ign_dev_err
= 0;
3355 unsigned int err_mask
;
3358 dev
->flags
&= ~ATA_DFLAG_PIO
;
3359 if (dev
->xfer_shift
== ATA_SHIFT_PIO
)
3360 dev
->flags
|= ATA_DFLAG_PIO
;
3362 err_mask
= ata_dev_set_xfermode(dev
);
3364 if (err_mask
& ~AC_ERR_DEV
)
3368 ehc
->i
.flags
|= ATA_EHI_POST_SETMODE
;
3369 rc
= ata_dev_revalidate(dev
, ATA_DEV_UNKNOWN
, 0);
3370 ehc
->i
.flags
&= ~ATA_EHI_POST_SETMODE
;
3374 /* Old CFA may refuse this command, which is just fine */
3375 if (dev
->xfer_shift
== ATA_SHIFT_PIO
&& ata_id_is_cfa(dev
->id
))
3378 /* Some very old devices and some bad newer ones fail any kind of
3379 SET_XFERMODE request but support PIO0-2 timings and no IORDY */
3380 if (dev
->xfer_shift
== ATA_SHIFT_PIO
&& !ata_id_has_iordy(dev
->id
) &&
3381 dev
->pio_mode
<= XFER_PIO_2
)
3384 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3385 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3386 if (dev
->xfer_shift
== ATA_SHIFT_MWDMA
&&
3387 dev
->dma_mode
== XFER_MW_DMA_0
&&
3388 (dev
->id
[63] >> 8) & 1)
3391 /* if the device is actually configured correctly, ignore dev err */
3392 if (dev
->xfer_mode
== ata_xfer_mask2mode(ata_id_xfermask(dev
->id
)))
3395 if (err_mask
& AC_ERR_DEV
) {
3399 dev_err_whine
= " (device error ignored)";
3402 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3403 dev
->xfer_shift
, (int)dev
->xfer_mode
);
3405 ata_dev_printk(dev
, KERN_INFO
, "configured for %s%s\n",
3406 ata_mode_string(ata_xfer_mode2mask(dev
->xfer_mode
)),
3412 ata_dev_printk(dev
, KERN_ERR
, "failed to set xfermode "
3413 "(err_mask=0x%x)\n", err_mask
);
3418 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
3419 * @link: link on which timings will be programmed
3420 * @r_failed_dev: out parameter for failed device
3422 * Standard implementation of the function used to tune and set
3423 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3424 * ata_dev_set_mode() fails, pointer to the failing device is
3425 * returned in @r_failed_dev.
3428 * PCI/etc. bus probe sem.
3431 * 0 on success, negative errno otherwise
3434 int ata_do_set_mode(struct ata_link
*link
, struct ata_device
**r_failed_dev
)
3436 struct ata_port
*ap
= link
->ap
;
3437 struct ata_device
*dev
;
3438 int rc
= 0, used_dma
= 0, found
= 0;
3440 /* step 1: calculate xfer_mask */
3441 ata_link_for_each_dev(dev
, link
) {
3442 unsigned long pio_mask
, dma_mask
;
3443 unsigned int mode_mask
;
3445 if (!ata_dev_enabled(dev
))
3448 mode_mask
= ATA_DMA_MASK_ATA
;
3449 if (dev
->class == ATA_DEV_ATAPI
)
3450 mode_mask
= ATA_DMA_MASK_ATAPI
;
3451 else if (ata_id_is_cfa(dev
->id
))
3452 mode_mask
= ATA_DMA_MASK_CFA
;
3454 ata_dev_xfermask(dev
);
3455 ata_force_xfermask(dev
);
3457 pio_mask
= ata_pack_xfermask(dev
->pio_mask
, 0, 0);
3458 dma_mask
= ata_pack_xfermask(0, dev
->mwdma_mask
, dev
->udma_mask
);
3460 if (libata_dma_mask
& mode_mask
)
3461 dma_mask
= ata_pack_xfermask(0, dev
->mwdma_mask
, dev
->udma_mask
);
3465 dev
->pio_mode
= ata_xfer_mask2mode(pio_mask
);
3466 dev
->dma_mode
= ata_xfer_mask2mode(dma_mask
);
3469 if (dev
->dma_mode
!= 0xff)
3475 /* step 2: always set host PIO timings */
3476 ata_link_for_each_dev(dev
, link
) {
3477 if (!ata_dev_enabled(dev
))
3480 if (dev
->pio_mode
== 0xff) {
3481 ata_dev_printk(dev
, KERN_WARNING
, "no PIO support\n");
3486 dev
->xfer_mode
= dev
->pio_mode
;
3487 dev
->xfer_shift
= ATA_SHIFT_PIO
;
3488 if (ap
->ops
->set_piomode
)
3489 ap
->ops
->set_piomode(ap
, dev
);
3492 /* step 3: set host DMA timings */
3493 ata_link_for_each_dev(dev
, link
) {
3494 if (!ata_dev_enabled(dev
) || dev
->dma_mode
== 0xff)
3497 dev
->xfer_mode
= dev
->dma_mode
;
3498 dev
->xfer_shift
= ata_xfer_mode2shift(dev
->dma_mode
);
3499 if (ap
->ops
->set_dmamode
)
3500 ap
->ops
->set_dmamode(ap
, dev
);
3503 /* step 4: update devices' xfer mode */
3504 ata_link_for_each_dev(dev
, link
) {
3505 /* don't update suspended devices' xfer mode */
3506 if (!ata_dev_enabled(dev
))
3509 rc
= ata_dev_set_mode(dev
);
3514 /* Record simplex status. If we selected DMA then the other
3515 * host channels are not permitted to do so.
3517 if (used_dma
&& (ap
->host
->flags
& ATA_HOST_SIMPLEX
))
3518 ap
->host
->simplex_claimed
= ap
;
3522 *r_failed_dev
= dev
;
3527 * ata_tf_to_host - issue ATA taskfile to host controller
3528 * @ap: port to which command is being issued
3529 * @tf: ATA taskfile register set
3531 * Issues ATA taskfile register set to ATA host controller,
3532 * with proper synchronization with interrupt handler and
3536 * spin_lock_irqsave(host lock)
3539 static inline void ata_tf_to_host(struct ata_port
*ap
,
3540 const struct ata_taskfile
*tf
)
3542 ap
->ops
->tf_load(ap
, tf
);
3543 ap
->ops
->exec_command(ap
, tf
);
3547 * ata_busy_sleep - sleep until BSY clears, or timeout
3548 * @ap: port containing status register to be polled
3549 * @tmout_pat: impatience timeout
3550 * @tmout: overall timeout
3552 * Sleep until ATA Status register bit BSY clears,
3553 * or a timeout occurs.
3556 * Kernel thread context (may sleep).
3559 * 0 on success, -errno otherwise.
3561 int ata_busy_sleep(struct ata_port
*ap
,
3562 unsigned long tmout_pat
, unsigned long tmout
)
3564 unsigned long timer_start
, timeout
;
3567 status
= ata_busy_wait(ap
, ATA_BUSY
, 300);
3568 timer_start
= jiffies
;
3569 timeout
= timer_start
+ tmout_pat
;
3570 while (status
!= 0xff && (status
& ATA_BUSY
) &&
3571 time_before(jiffies
, timeout
)) {
3573 status
= ata_busy_wait(ap
, ATA_BUSY
, 3);
3576 if (status
!= 0xff && (status
& ATA_BUSY
))
3577 ata_port_printk(ap
, KERN_WARNING
,
3578 "port is slow to respond, please be patient "
3579 "(Status 0x%x)\n", status
);
3581 timeout
= timer_start
+ tmout
;
3582 while (status
!= 0xff && (status
& ATA_BUSY
) &&
3583 time_before(jiffies
, timeout
)) {
3585 status
= ata_chk_status(ap
);
3591 if (status
& ATA_BUSY
) {
3592 ata_port_printk(ap
, KERN_ERR
, "port failed to respond "
3593 "(%lu secs, Status 0x%x)\n",
3594 tmout
/ HZ
, status
);
3602 * ata_wait_after_reset - wait before checking status after reset
3603 * @ap: port containing status register to be polled
3604 * @deadline: deadline jiffies for the operation
3606 * After reset, we need to pause a while before reading status.
3607 * Also, certain combination of controller and device report 0xff
3608 * for some duration (e.g. until SATA PHY is up and running)
3609 * which is interpreted as empty port in ATA world. This
3610 * function also waits for such devices to get out of 0xff
3614 * Kernel thread context (may sleep).
3616 void ata_wait_after_reset(struct ata_port
*ap
, unsigned long deadline
)
3618 unsigned long until
= jiffies
+ ATA_TMOUT_FF_WAIT
;
3620 if (time_before(until
, deadline
))
3623 /* Spec mandates ">= 2ms" before checking status. We wait
3624 * 150ms, because that was the magic delay used for ATAPI
3625 * devices in Hale Landis's ATADRVR, for the period of time
3626 * between when the ATA command register is written, and then
3627 * status is checked. Because waiting for "a while" before
3628 * checking status is fine, post SRST, we perform this magic
3629 * delay here as well.
3631 * Old drivers/ide uses the 2mS rule and then waits for ready.
3635 /* Wait for 0xff to clear. Some SATA devices take a long time
3636 * to clear 0xff after reset. For example, HHD424020F7SV00
3637 * iVDR needs >= 800ms while. Quantum GoVault needs even more
3640 * Note that some PATA controllers (pata_ali) explode if
3641 * status register is read more than once when there's no
3644 if (ap
->flags
& ATA_FLAG_SATA
) {
3646 u8 status
= ata_chk_status(ap
);
3648 if (status
!= 0xff || time_after(jiffies
, deadline
))
3657 * ata_wait_ready - sleep until BSY clears, or timeout
3658 * @ap: port containing status register to be polled
3659 * @deadline: deadline jiffies for the operation
3661 * Sleep until ATA Status register bit BSY clears, or timeout
3665 * Kernel thread context (may sleep).
3668 * 0 on success, -errno otherwise.
3670 int ata_wait_ready(struct ata_port
*ap
, unsigned long deadline
)
3672 unsigned long start
= jiffies
;
3676 u8 status
= ata_chk_status(ap
);
3677 unsigned long now
= jiffies
;
3679 if (!(status
& ATA_BUSY
))
3681 if (!ata_link_online(&ap
->link
) && status
== 0xff)
3683 if (time_after(now
, deadline
))
3686 if (!warned
&& time_after(now
, start
+ 5 * HZ
) &&
3687 (deadline
- now
> 3 * HZ
)) {
3688 ata_port_printk(ap
, KERN_WARNING
,
3689 "port is slow to respond, please be patient "
3690 "(Status 0x%x)\n", status
);
3698 static int ata_bus_post_reset(struct ata_port
*ap
, unsigned int devmask
,
3699 unsigned long deadline
)
3701 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
3702 unsigned int dev0
= devmask
& (1 << 0);
3703 unsigned int dev1
= devmask
& (1 << 1);
3706 /* if device 0 was found in ata_devchk, wait for its
3710 rc
= ata_wait_ready(ap
, deadline
);
3718 /* if device 1 was found in ata_devchk, wait for register
3719 * access briefly, then wait for BSY to clear.
3724 ap
->ops
->dev_select(ap
, 1);
3726 /* Wait for register access. Some ATAPI devices fail
3727 * to set nsect/lbal after reset, so don't waste too
3728 * much time on it. We're gonna wait for !BSY anyway.
3730 for (i
= 0; i
< 2; i
++) {
3733 nsect
= ioread8(ioaddr
->nsect_addr
);
3734 lbal
= ioread8(ioaddr
->lbal_addr
);
3735 if ((nsect
== 1) && (lbal
== 1))
3737 msleep(50); /* give drive a breather */
3740 rc
= ata_wait_ready(ap
, deadline
);
3748 /* is all this really necessary? */
3749 ap
->ops
->dev_select(ap
, 0);
3751 ap
->ops
->dev_select(ap
, 1);
3753 ap
->ops
->dev_select(ap
, 0);
3758 static int ata_bus_softreset(struct ata_port
*ap
, unsigned int devmask
,
3759 unsigned long deadline
)
3761 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
3763 DPRINTK("ata%u: bus reset via SRST\n", ap
->print_id
);
3765 /* software reset. causes dev0 to be selected */
3766 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
3767 udelay(20); /* FIXME: flush */
3768 iowrite8(ap
->ctl
| ATA_SRST
, ioaddr
->ctl_addr
);
3769 udelay(20); /* FIXME: flush */
3770 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
3772 /* wait a while before checking status */
3773 ata_wait_after_reset(ap
, deadline
);
3775 /* Before we perform post reset processing we want to see if
3776 * the bus shows 0xFF because the odd clown forgets the D7
3777 * pulldown resistor.
3779 if (ata_chk_status(ap
) == 0xFF)
3782 return ata_bus_post_reset(ap
, devmask
, deadline
);
3786 * ata_bus_reset - reset host port and associated ATA channel
3787 * @ap: port to reset
3789 * This is typically the first time we actually start issuing
3790 * commands to the ATA channel. We wait for BSY to clear, then
3791 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
3792 * result. Determine what devices, if any, are on the channel
3793 * by looking at the device 0/1 error register. Look at the signature
3794 * stored in each device's taskfile registers, to determine if
3795 * the device is ATA or ATAPI.
3798 * PCI/etc. bus probe sem.
3799 * Obtains host lock.
3802 * Sets ATA_FLAG_DISABLED if bus reset fails.
3805 void ata_bus_reset(struct ata_port
*ap
)
3807 struct ata_device
*device
= ap
->link
.device
;
3808 struct ata_ioports
*ioaddr
= &ap
->ioaddr
;
3809 unsigned int slave_possible
= ap
->flags
& ATA_FLAG_SLAVE_POSS
;
3811 unsigned int dev0
, dev1
= 0, devmask
= 0;
3814 DPRINTK("ENTER, host %u, port %u\n", ap
->print_id
, ap
->port_no
);
3816 /* determine if device 0/1 are present */
3817 if (ap
->flags
& ATA_FLAG_SATA_RESET
)
3820 dev0
= ata_devchk(ap
, 0);
3822 dev1
= ata_devchk(ap
, 1);
3826 devmask
|= (1 << 0);
3828 devmask
|= (1 << 1);
3830 /* select device 0 again */
3831 ap
->ops
->dev_select(ap
, 0);
3833 /* issue bus reset */
3834 if (ap
->flags
& ATA_FLAG_SRST
) {
3835 rc
= ata_bus_softreset(ap
, devmask
, jiffies
+ 40 * HZ
);
3836 if (rc
&& rc
!= -ENODEV
)
3841 * determine by signature whether we have ATA or ATAPI devices
3843 device
[0].class = ata_dev_try_classify(&device
[0], dev0
, &err
);
3844 if ((slave_possible
) && (err
!= 0x81))
3845 device
[1].class = ata_dev_try_classify(&device
[1], dev1
, &err
);
3847 /* is double-select really necessary? */
3848 if (device
[1].class != ATA_DEV_NONE
)
3849 ap
->ops
->dev_select(ap
, 1);
3850 if (device
[0].class != ATA_DEV_NONE
)
3851 ap
->ops
->dev_select(ap
, 0);
3853 /* if no devices were detected, disable this port */
3854 if ((device
[0].class == ATA_DEV_NONE
) &&
3855 (device
[1].class == ATA_DEV_NONE
))
3858 if (ap
->flags
& (ATA_FLAG_SATA_RESET
| ATA_FLAG_SRST
)) {
3859 /* set up device control for ATA_FLAG_SATA_RESET */
3860 iowrite8(ap
->ctl
, ioaddr
->ctl_addr
);
3867 ata_port_printk(ap
, KERN_ERR
, "disabling port\n");
3868 ata_port_disable(ap
);
3874 * sata_link_debounce - debounce SATA phy status
3875 * @link: ATA link to debounce SATA phy status for
3876 * @params: timing parameters { interval, duratinon, timeout } in msec
3877 * @deadline: deadline jiffies for the operation
3879 * Make sure SStatus of @link reaches stable state, determined by
3880 * holding the same value where DET is not 1 for @duration polled
3881 * every @interval, before @timeout. Timeout constraints the
3882 * beginning of the stable state. Because DET gets stuck at 1 on
3883 * some controllers after hot unplugging, this functions waits
3884 * until timeout then returns 0 if DET is stable at 1.
3886 * @timeout is further limited by @deadline. The sooner of the
3890 * Kernel thread context (may sleep)
3893 * 0 on success, -errno on failure.
3895 int sata_link_debounce(struct ata_link
*link
, const unsigned long *params
,
3896 unsigned long deadline
)
3898 unsigned long interval_msec
= params
[0];
3899 unsigned long duration
= msecs_to_jiffies(params
[1]);
3900 unsigned long last_jiffies
, t
;
3904 t
= jiffies
+ msecs_to_jiffies(params
[2]);
3905 if (time_before(t
, deadline
))
3908 if ((rc
= sata_scr_read(link
, SCR_STATUS
, &cur
)))
3913 last_jiffies
= jiffies
;
3916 msleep(interval_msec
);
3917 if ((rc
= sata_scr_read(link
, SCR_STATUS
, &cur
)))
3923 if (cur
== 1 && time_before(jiffies
, deadline
))
3925 if (time_after(jiffies
, last_jiffies
+ duration
))
3930 /* unstable, start over */
3932 last_jiffies
= jiffies
;
3934 /* Check deadline. If debouncing failed, return
3935 * -EPIPE to tell upper layer to lower link speed.
3937 if (time_after(jiffies
, deadline
))
3943 * sata_link_resume - resume SATA link
3944 * @link: ATA link to resume SATA
3945 * @params: timing parameters { interval, duratinon, timeout } in msec
3946 * @deadline: deadline jiffies for the operation
3948 * Resume SATA phy @link and debounce it.
3951 * Kernel thread context (may sleep)
3954 * 0 on success, -errno on failure.
3956 int sata_link_resume(struct ata_link
*link
, const unsigned long *params
,
3957 unsigned long deadline
)
3962 if ((rc
= sata_scr_read(link
, SCR_CONTROL
, &scontrol
)))
3965 scontrol
= (scontrol
& 0x0f0) | 0x300;
3967 if ((rc
= sata_scr_write(link
, SCR_CONTROL
, scontrol
)))
3970 /* Some PHYs react badly if SStatus is pounded immediately
3971 * after resuming. Delay 200ms before debouncing.
3975 return sata_link_debounce(link
, params
, deadline
);
3979 * ata_std_prereset - prepare for reset
3980 * @link: ATA link to be reset
3981 * @deadline: deadline jiffies for the operation
3983 * @link is about to be reset. Initialize it. Failure from
3984 * prereset makes libata abort whole reset sequence and give up
3985 * that port, so prereset should be best-effort. It does its
3986 * best to prepare for reset sequence but if things go wrong, it
3987 * should just whine, not fail.
3990 * Kernel thread context (may sleep)
3993 * 0 on success, -errno otherwise.
3995 int ata_std_prereset(struct ata_link
*link
, unsigned long deadline
)
3997 struct ata_port
*ap
= link
->ap
;
3998 struct ata_eh_context
*ehc
= &link
->eh_context
;
3999 const unsigned long *timing
= sata_ehc_deb_timing(ehc
);
4002 /* if we're about to do hardreset, nothing more to do */
4003 if (ehc
->i
.action
& ATA_EH_HARDRESET
)
4006 /* if SATA, resume link */
4007 if (ap
->flags
& ATA_FLAG_SATA
) {
4008 rc
= sata_link_resume(link
, timing
, deadline
);
4009 /* whine about phy resume failure but proceed */
4010 if (rc
&& rc
!= -EOPNOTSUPP
)
4011 ata_link_printk(link
, KERN_WARNING
, "failed to resume "
4012 "link for reset (errno=%d)\n", rc
);
4015 /* wait for !BSY if we don't know that no device is attached */
4016 if (!ata_link_offline(link
)) {
4017 rc
= ata_wait_ready(ap
, deadline
);
4018 if (rc
&& rc
!= -ENODEV
) {
4019 ata_link_printk(link
, KERN_WARNING
, "device not ready "
4020 "(errno=%d), forcing hardreset\n", rc
);
4021 ehc
->i
.action
|= ATA_EH_HARDRESET
;
4029 * ata_std_softreset - reset host port via ATA SRST
4030 * @link: ATA link to reset
4031 * @classes: resulting classes of attached devices
4032 * @deadline: deadline jiffies for the operation
4034 * Reset host port using ATA SRST.
4037 * Kernel thread context (may sleep)
4040 * 0 on success, -errno otherwise.
4042 int ata_std_softreset(struct ata_link
*link
, unsigned int *classes
,
4043 unsigned long deadline
)
4045 struct ata_port
*ap
= link
->ap
;
4046 unsigned int slave_possible
= ap
->flags
& ATA_FLAG_SLAVE_POSS
;
4047 unsigned int devmask
= 0;
4053 if (ata_link_offline(link
)) {
4054 classes
[0] = ATA_DEV_NONE
;
4058 /* determine if device 0/1 are present */
4059 if (ata_devchk(ap
, 0))
4060 devmask
|= (1 << 0);
4061 if (slave_possible
&& ata_devchk(ap
, 1))
4062 devmask
|= (1 << 1);
4064 /* select device 0 again */
4065 ap
->ops
->dev_select(ap
, 0);
4067 /* issue bus reset */
4068 DPRINTK("about to softreset, devmask=%x\n", devmask
);
4069 rc
= ata_bus_softreset(ap
, devmask
, deadline
);
4070 /* if link is occupied, -ENODEV too is an error */
4071 if (rc
&& (rc
!= -ENODEV
|| sata_scr_valid(link
))) {
4072 ata_link_printk(link
, KERN_ERR
, "SRST failed (errno=%d)\n", rc
);
4076 /* determine by signature whether we have ATA or ATAPI devices */
4077 classes
[0] = ata_dev_try_classify(&link
->device
[0],
4078 devmask
& (1 << 0), &err
);
4079 if (slave_possible
&& err
!= 0x81)
4080 classes
[1] = ata_dev_try_classify(&link
->device
[1],
4081 devmask
& (1 << 1), &err
);
4084 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes
[0], classes
[1]);
4089 * sata_link_hardreset - reset link via SATA phy reset
4090 * @link: link to reset
4091 * @timing: timing parameters { interval, duratinon, timeout } in msec
4092 * @deadline: deadline jiffies for the operation
4094 * SATA phy-reset @link using DET bits of SControl register.
4097 * Kernel thread context (may sleep)
4100 * 0 on success, -errno otherwise.
4102 int sata_link_hardreset(struct ata_link
*link
, const unsigned long *timing
,
4103 unsigned long deadline
)
4110 if (sata_set_spd_needed(link
)) {
4111 /* SATA spec says nothing about how to reconfigure
4112 * spd. To be on the safe side, turn off phy during
4113 * reconfiguration. This works for at least ICH7 AHCI
4116 if ((rc
= sata_scr_read(link
, SCR_CONTROL
, &scontrol
)))
4119 scontrol
= (scontrol
& 0x0f0) | 0x304;
4121 if ((rc
= sata_scr_write(link
, SCR_CONTROL
, scontrol
)))
4127 /* issue phy wake/reset */
4128 if ((rc
= sata_scr_read(link
, SCR_CONTROL
, &scontrol
)))
4131 scontrol
= (scontrol
& 0x0f0) | 0x301;
4133 if ((rc
= sata_scr_write_flush(link
, SCR_CONTROL
, scontrol
)))
4136 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
4137 * 10.4.2 says at least 1 ms.
4141 /* bring link back */
4142 rc
= sata_link_resume(link
, timing
, deadline
);
4144 DPRINTK("EXIT, rc=%d\n", rc
);
4149 * sata_std_hardreset - reset host port via SATA phy reset
4150 * @link: link to reset
4151 * @class: resulting class of attached device
4152 * @deadline: deadline jiffies for the operation
4154 * SATA phy-reset host port using DET bits of SControl register,
4155 * wait for !BSY and classify the attached device.
4158 * Kernel thread context (may sleep)
4161 * 0 on success, -errno otherwise.
4163 int sata_std_hardreset(struct ata_link
*link
, unsigned int *class,
4164 unsigned long deadline
)
4166 struct ata_port
*ap
= link
->ap
;
4167 const unsigned long *timing
= sata_ehc_deb_timing(&link
->eh_context
);
4173 rc
= sata_link_hardreset(link
, timing
, deadline
);
4175 ata_link_printk(link
, KERN_ERR
,
4176 "COMRESET failed (errno=%d)\n", rc
);
4180 /* TODO: phy layer with polling, timeouts, etc. */
4181 if (ata_link_offline(link
)) {
4182 *class = ATA_DEV_NONE
;
4183 DPRINTK("EXIT, link offline\n");
4187 /* wait a while before checking status */
4188 ata_wait_after_reset(ap
, deadline
);
4190 /* If PMP is supported, we have to do follow-up SRST. Note
4191 * that some PMPs don't send D2H Reg FIS after hardreset at
4192 * all if the first port is empty. Wait for it just for a
4193 * second and request follow-up SRST.
4195 if (ap
->flags
& ATA_FLAG_PMP
) {
4196 ata_wait_ready(ap
, jiffies
+ HZ
);
4200 rc
= ata_wait_ready(ap
, deadline
);
4201 /* link occupied, -ENODEV too is an error */
4203 ata_link_printk(link
, KERN_ERR
,
4204 "COMRESET failed (errno=%d)\n", rc
);
4208 ap
->ops
->dev_select(ap
, 0); /* probably unnecessary */
4210 *class = ata_dev_try_classify(link
->device
, 1, NULL
);
4212 DPRINTK("EXIT, class=%u\n", *class);
4217 * ata_std_postreset - standard postreset callback
4218 * @link: the target ata_link
4219 * @classes: classes of attached devices
4221 * This function is invoked after a successful reset. Note that
4222 * the device might have been reset more than once using
4223 * different reset methods before postreset is invoked.
4226 * Kernel thread context (may sleep)
4228 void ata_std_postreset(struct ata_link
*link
, unsigned int *classes
)
4230 struct ata_port
*ap
= link
->ap
;
4235 /* print link status */
4236 sata_print_link_status(link
);
4239 if (sata_scr_read(link
, SCR_ERROR
, &serror
) == 0)
4240 sata_scr_write(link
, SCR_ERROR
, serror
);
4241 link
->eh_info
.serror
= 0;
4243 /* is double-select really necessary? */
4244 if (classes
[0] != ATA_DEV_NONE
)
4245 ap
->ops
->dev_select(ap
, 1);
4246 if (classes
[1] != ATA_DEV_NONE
)
4247 ap
->ops
->dev_select(ap
, 0);
4249 /* bail out if no device is present */
4250 if (classes
[0] == ATA_DEV_NONE
&& classes
[1] == ATA_DEV_NONE
) {
4251 DPRINTK("EXIT, no device\n");
4255 /* set up device control */
4256 if (ap
->ioaddr
.ctl_addr
)
4257 iowrite8(ap
->ctl
, ap
->ioaddr
.ctl_addr
);
4263 * ata_dev_same_device - Determine whether new ID matches configured device
4264 * @dev: device to compare against
4265 * @new_class: class of the new device
4266 * @new_id: IDENTIFY page of the new device
4268 * Compare @new_class and @new_id against @dev and determine
4269 * whether @dev is the device indicated by @new_class and
4276 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
4278 static int ata_dev_same_device(struct ata_device
*dev
, unsigned int new_class
,
4281 const u16
*old_id
= dev
->id
;
4282 unsigned char model
[2][ATA_ID_PROD_LEN
+ 1];
4283 unsigned char serial
[2][ATA_ID_SERNO_LEN
+ 1];
4285 if (dev
->class != new_class
) {
4286 ata_dev_printk(dev
, KERN_INFO
, "class mismatch %d != %d\n",
4287 dev
->class, new_class
);
4291 ata_id_c_string(old_id
, model
[0], ATA_ID_PROD
, sizeof(model
[0]));
4292 ata_id_c_string(new_id
, model
[1], ATA_ID_PROD
, sizeof(model
[1]));
4293 ata_id_c_string(old_id
, serial
[0], ATA_ID_SERNO
, sizeof(serial
[0]));
4294 ata_id_c_string(new_id
, serial
[1], ATA_ID_SERNO
, sizeof(serial
[1]));
4296 if (strcmp(model
[0], model
[1])) {
4297 ata_dev_printk(dev
, KERN_INFO
, "model number mismatch "
4298 "'%s' != '%s'\n", model
[0], model
[1]);
4302 if (strcmp(serial
[0], serial
[1])) {
4303 ata_dev_printk(dev
, KERN_INFO
, "serial number mismatch "
4304 "'%s' != '%s'\n", serial
[0], serial
[1]);
4312 * ata_dev_reread_id - Re-read IDENTIFY data
4313 * @dev: target ATA device
4314 * @readid_flags: read ID flags
4316 * Re-read IDENTIFY page and make sure @dev is still attached to
4320 * Kernel thread context (may sleep)
4323 * 0 on success, negative errno otherwise
4325 int ata_dev_reread_id(struct ata_device
*dev
, unsigned int readid_flags
)
4327 unsigned int class = dev
->class;
4328 u16
*id
= (void *)dev
->link
->ap
->sector_buf
;
4332 rc
= ata_dev_read_id(dev
, &class, readid_flags
, id
);
4336 /* is the device still there? */
4337 if (!ata_dev_same_device(dev
, class, id
))
4340 memcpy(dev
->id
, id
, sizeof(id
[0]) * ATA_ID_WORDS
);
4345 * ata_dev_revalidate - Revalidate ATA device
4346 * @dev: device to revalidate
4347 * @new_class: new class code
4348 * @readid_flags: read ID flags
4350 * Re-read IDENTIFY page, make sure @dev is still attached to the
4351 * port and reconfigure it according to the new IDENTIFY page.
4354 * Kernel thread context (may sleep)
4357 * 0 on success, negative errno otherwise
4359 int ata_dev_revalidate(struct ata_device
*dev
, unsigned int new_class
,
4360 unsigned int readid_flags
)
4362 u64 n_sectors
= dev
->n_sectors
;
4365 if (!ata_dev_enabled(dev
))
4368 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4369 if (ata_class_enabled(new_class
) &&
4370 new_class
!= ATA_DEV_ATA
&& new_class
!= ATA_DEV_ATAPI
) {
4371 ata_dev_printk(dev
, KERN_INFO
, "class mismatch %u != %u\n",
4372 dev
->class, new_class
);
4378 rc
= ata_dev_reread_id(dev
, readid_flags
);
4382 /* configure device according to the new ID */
4383 rc
= ata_dev_configure(dev
);
4387 /* verify n_sectors hasn't changed */
4388 if (dev
->class == ATA_DEV_ATA
&& n_sectors
&&
4389 dev
->n_sectors
!= n_sectors
) {
4390 ata_dev_printk(dev
, KERN_INFO
, "n_sectors mismatch "
4392 (unsigned long long)n_sectors
,
4393 (unsigned long long)dev
->n_sectors
);
4395 /* restore original n_sectors */
4396 dev
->n_sectors
= n_sectors
;
4405 ata_dev_printk(dev
, KERN_ERR
, "revalidation failed (errno=%d)\n", rc
);
4409 struct ata_blacklist_entry
{
4410 const char *model_num
;
4411 const char *model_rev
;
4412 unsigned long horkage
;
4415 static const struct ata_blacklist_entry ata_device_blacklist
[] = {
4416 /* Devices with DMA related problems under Linux */
4417 { "WDC AC11000H", NULL
, ATA_HORKAGE_NODMA
},
4418 { "WDC AC22100H", NULL
, ATA_HORKAGE_NODMA
},
4419 { "WDC AC32500H", NULL
, ATA_HORKAGE_NODMA
},
4420 { "WDC AC33100H", NULL
, ATA_HORKAGE_NODMA
},
4421 { "WDC AC31600H", NULL
, ATA_HORKAGE_NODMA
},
4422 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA
},
4423 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA
},
4424 { "Compaq CRD-8241B", NULL
, ATA_HORKAGE_NODMA
},
4425 { "CRD-8400B", NULL
, ATA_HORKAGE_NODMA
},
4426 { "CRD-8480B", NULL
, ATA_HORKAGE_NODMA
},
4427 { "CRD-8482B", NULL
, ATA_HORKAGE_NODMA
},
4428 { "CRD-84", NULL
, ATA_HORKAGE_NODMA
},
4429 { "SanDisk SDP3B", NULL
, ATA_HORKAGE_NODMA
},
4430 { "SanDisk SDP3B-64", NULL
, ATA_HORKAGE_NODMA
},
4431 { "SANYO CD-ROM CRD", NULL
, ATA_HORKAGE_NODMA
},
4432 { "HITACHI CDR-8", NULL
, ATA_HORKAGE_NODMA
},
4433 { "HITACHI CDR-8335", NULL
, ATA_HORKAGE_NODMA
},
4434 { "HITACHI CDR-8435", NULL
, ATA_HORKAGE_NODMA
},
4435 { "Toshiba CD-ROM XM-6202B", NULL
, ATA_HORKAGE_NODMA
},
4436 { "TOSHIBA CD-ROM XM-1702BC", NULL
, ATA_HORKAGE_NODMA
},
4437 { "CD-532E-A", NULL
, ATA_HORKAGE_NODMA
},
4438 { "E-IDE CD-ROM CR-840",NULL
, ATA_HORKAGE_NODMA
},
4439 { "CD-ROM Drive/F5A", NULL
, ATA_HORKAGE_NODMA
},
4440 { "WPI CDD-820", NULL
, ATA_HORKAGE_NODMA
},
4441 { "SAMSUNG CD-ROM SC-148C", NULL
, ATA_HORKAGE_NODMA
},
4442 { "SAMSUNG CD-ROM SC", NULL
, ATA_HORKAGE_NODMA
},
4443 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL
,ATA_HORKAGE_NODMA
},
4444 { "_NEC DV5800A", NULL
, ATA_HORKAGE_NODMA
},
4445 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA
},
4446 { "Seagate STT20000A", NULL
, ATA_HORKAGE_NODMA
},
4447 /* Odd clown on sil3726/4726 PMPs */
4448 { "Config Disk", NULL
, ATA_HORKAGE_NODMA
|
4449 ATA_HORKAGE_SKIP_PM
},
4451 /* Weird ATAPI devices */
4452 { "TORiSAN DVD-ROM DRD-N216", NULL
, ATA_HORKAGE_MAX_SEC_128
},
4454 /* Devices we expect to fail diagnostics */
4456 /* Devices where NCQ should be avoided */
4458 { "WDC WD740ADFD-00", NULL
, ATA_HORKAGE_NONCQ
},
4459 { "WDC WD740ADFD-00NLR1", NULL
, ATA_HORKAGE_NONCQ
, },
4460 /* http://thread.gmane.org/gmane.linux.ide/14907 */
4461 { "FUJITSU MHT2060BH", NULL
, ATA_HORKAGE_NONCQ
},
4463 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ
},
4464 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ
},
4465 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ
},
4466 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ
},
4468 /* Blacklist entries taken from Silicon Image 3124/3132
4469 Windows driver .inf file - also several Linux problem reports */
4470 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ
, },
4471 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ
, },
4472 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ
, },
4474 /* devices which puke on READ_NATIVE_MAX */
4475 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA
, },
4476 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA
},
4477 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA
},
4478 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA
},
4480 /* Devices which report 1 sector over size HPA */
4481 { "ST340823A", NULL
, ATA_HORKAGE_HPA_SIZE
, },
4482 { "ST320413A", NULL
, ATA_HORKAGE_HPA_SIZE
, },
4483 { "ST310211A", NULL
, ATA_HORKAGE_HPA_SIZE
, },
4485 /* Devices which get the IVB wrong */
4486 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB
, },
4487 { "TSSTcorp CDDVDW SH-S202J", "SB00", ATA_HORKAGE_IVB
, },
4488 { "TSSTcorp CDDVDW SH-S202J", "SB01", ATA_HORKAGE_IVB
, },
4489 { "TSSTcorp CDDVDW SH-S202N", "SB00", ATA_HORKAGE_IVB
, },
4490 { "TSSTcorp CDDVDW SH-S202N", "SB01", ATA_HORKAGE_IVB
, },
4496 static int strn_pattern_cmp(const char *patt
, const char *name
, int wildchar
)
4502 * check for trailing wildcard: *\0
4504 p
= strchr(patt
, wildchar
);
4505 if (p
&& ((*(p
+ 1)) == 0))
4516 return strncmp(patt
, name
, len
);
4519 static unsigned long ata_dev_blacklisted(const struct ata_device
*dev
)
4521 unsigned char model_num
[ATA_ID_PROD_LEN
+ 1];
4522 unsigned char model_rev
[ATA_ID_FW_REV_LEN
+ 1];
4523 const struct ata_blacklist_entry
*ad
= ata_device_blacklist
;
4525 ata_id_c_string(dev
->id
, model_num
, ATA_ID_PROD
, sizeof(model_num
));
4526 ata_id_c_string(dev
->id
, model_rev
, ATA_ID_FW_REV
, sizeof(model_rev
));
4528 while (ad
->model_num
) {
4529 if (!strn_pattern_cmp(ad
->model_num
, model_num
, '*')) {
4530 if (ad
->model_rev
== NULL
)
4532 if (!strn_pattern_cmp(ad
->model_rev
, model_rev
, '*'))
4540 static int ata_dma_blacklisted(const struct ata_device
*dev
)
4542 /* We don't support polling DMA.
4543 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
4544 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
4546 if ((dev
->link
->ap
->flags
& ATA_FLAG_PIO_POLLING
) &&
4547 (dev
->flags
& ATA_DFLAG_CDB_INTR
))
4549 return (dev
->horkage
& ATA_HORKAGE_NODMA
) ? 1 : 0;
4553 * ata_is_40wire - check drive side detection
4556 * Perform drive side detection decoding, allowing for device vendors
4557 * who can't follow the documentation.
4560 static int ata_is_40wire(struct ata_device
*dev
)
4562 if (dev
->horkage
& ATA_HORKAGE_IVB
)
4563 return ata_drive_40wire_relaxed(dev
->id
);
4564 return ata_drive_40wire(dev
->id
);
4568 * ata_dev_xfermask - Compute supported xfermask of the given device
4569 * @dev: Device to compute xfermask for
4571 * Compute supported xfermask of @dev and store it in
4572 * dev->*_mask. This function is responsible for applying all
4573 * known limits including host controller limits, device
4579 static void ata_dev_xfermask(struct ata_device
*dev
)
4581 struct ata_link
*link
= dev
->link
;
4582 struct ata_port
*ap
= link
->ap
;
4583 struct ata_host
*host
= ap
->host
;
4584 unsigned long xfer_mask
;
4586 /* controller modes available */
4587 xfer_mask
= ata_pack_xfermask(ap
->pio_mask
,
4588 ap
->mwdma_mask
, ap
->udma_mask
);
4590 /* drive modes available */
4591 xfer_mask
&= ata_pack_xfermask(dev
->pio_mask
,
4592 dev
->mwdma_mask
, dev
->udma_mask
);
4593 xfer_mask
&= ata_id_xfermask(dev
->id
);
4596 * CFA Advanced TrueIDE timings are not allowed on a shared
4599 if (ata_dev_pair(dev
)) {
4600 /* No PIO5 or PIO6 */
4601 xfer_mask
&= ~(0x03 << (ATA_SHIFT_PIO
+ 5));
4602 /* No MWDMA3 or MWDMA 4 */
4603 xfer_mask
&= ~(0x03 << (ATA_SHIFT_MWDMA
+ 3));
4606 if (ata_dma_blacklisted(dev
)) {
4607 xfer_mask
&= ~(ATA_MASK_MWDMA
| ATA_MASK_UDMA
);
4608 ata_dev_printk(dev
, KERN_WARNING
,
4609 "device is on DMA blacklist, disabling DMA\n");
4612 if ((host
->flags
& ATA_HOST_SIMPLEX
) &&
4613 host
->simplex_claimed
&& host
->simplex_claimed
!= ap
) {
4614 xfer_mask
&= ~(ATA_MASK_MWDMA
| ATA_MASK_UDMA
);
4615 ata_dev_printk(dev
, KERN_WARNING
, "simplex DMA is claimed by "
4616 "other device, disabling DMA\n");
4619 if (ap
->flags
& ATA_FLAG_NO_IORDY
)
4620 xfer_mask
&= ata_pio_mask_no_iordy(dev
);
4622 if (ap
->ops
->mode_filter
)
4623 xfer_mask
= ap
->ops
->mode_filter(dev
, xfer_mask
);
4625 /* Apply cable rule here. Don't apply it early because when
4626 * we handle hot plug the cable type can itself change.
4627 * Check this last so that we know if the transfer rate was
4628 * solely limited by the cable.
4629 * Unknown or 80 wire cables reported host side are checked
4630 * drive side as well. Cases where we know a 40wire cable
4631 * is used safely for 80 are not checked here.
4633 if (xfer_mask
& (0xF8 << ATA_SHIFT_UDMA
))
4634 /* UDMA/44 or higher would be available */
4635 if ((ap
->cbl
== ATA_CBL_PATA40
) ||
4636 (ata_is_40wire(dev
) &&
4637 (ap
->cbl
== ATA_CBL_PATA_UNK
||
4638 ap
->cbl
== ATA_CBL_PATA80
))) {
4639 ata_dev_printk(dev
, KERN_WARNING
,
4640 "limited to UDMA/33 due to 40-wire cable\n");
4641 xfer_mask
&= ~(0xF8 << ATA_SHIFT_UDMA
);
4644 ata_unpack_xfermask(xfer_mask
, &dev
->pio_mask
,
4645 &dev
->mwdma_mask
, &dev
->udma_mask
);
4649 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4650 * @dev: Device to which command will be sent
4652 * Issue SET FEATURES - XFER MODE command to device @dev
4656 * PCI/etc. bus probe sem.
4659 * 0 on success, AC_ERR_* mask otherwise.
4662 static unsigned int ata_dev_set_xfermode(struct ata_device
*dev
)
4664 struct ata_taskfile tf
;
4665 unsigned int err_mask
;
4667 /* set up set-features taskfile */
4668 DPRINTK("set features - xfer mode\n");
4670 /* Some controllers and ATAPI devices show flaky interrupt
4671 * behavior after setting xfer mode. Use polling instead.
4673 ata_tf_init(dev
, &tf
);
4674 tf
.command
= ATA_CMD_SET_FEATURES
;
4675 tf
.feature
= SETFEATURES_XFER
;
4676 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
| ATA_TFLAG_POLLING
;
4677 tf
.protocol
= ATA_PROT_NODATA
;
4678 /* If we are using IORDY we must send the mode setting command */
4679 if (ata_pio_need_iordy(dev
))
4680 tf
.nsect
= dev
->xfer_mode
;
4681 /* If the device has IORDY and the controller does not - turn it off */
4682 else if (ata_id_has_iordy(dev
->id
))
4684 else /* In the ancient relic department - skip all of this */
4687 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_NONE
, NULL
, 0, 0);
4689 DPRINTK("EXIT, err_mask=%x\n", err_mask
);
4693 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
4694 * @dev: Device to which command will be sent
4695 * @enable: Whether to enable or disable the feature
4696 * @feature: The sector count represents the feature to set
4698 * Issue SET FEATURES - SATA FEATURES command to device @dev
4699 * on port @ap with sector count
4702 * PCI/etc. bus probe sem.
4705 * 0 on success, AC_ERR_* mask otherwise.
4707 static unsigned int ata_dev_set_feature(struct ata_device
*dev
, u8 enable
,
4710 struct ata_taskfile tf
;
4711 unsigned int err_mask
;
4713 /* set up set-features taskfile */
4714 DPRINTK("set features - SATA features\n");
4716 ata_tf_init(dev
, &tf
);
4717 tf
.command
= ATA_CMD_SET_FEATURES
;
4718 tf
.feature
= enable
;
4719 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
4720 tf
.protocol
= ATA_PROT_NODATA
;
4723 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_NONE
, NULL
, 0, 0);
4725 DPRINTK("EXIT, err_mask=%x\n", err_mask
);
4730 * ata_dev_init_params - Issue INIT DEV PARAMS command
4731 * @dev: Device to which command will be sent
4732 * @heads: Number of heads (taskfile parameter)
4733 * @sectors: Number of sectors (taskfile parameter)
4736 * Kernel thread context (may sleep)
4739 * 0 on success, AC_ERR_* mask otherwise.
4741 static unsigned int ata_dev_init_params(struct ata_device
*dev
,
4742 u16 heads
, u16 sectors
)
4744 struct ata_taskfile tf
;
4745 unsigned int err_mask
;
4747 /* Number of sectors per track 1-255. Number of heads 1-16 */
4748 if (sectors
< 1 || sectors
> 255 || heads
< 1 || heads
> 16)
4749 return AC_ERR_INVALID
;
4751 /* set up init dev params taskfile */
4752 DPRINTK("init dev params \n");
4754 ata_tf_init(dev
, &tf
);
4755 tf
.command
= ATA_CMD_INIT_DEV_PARAMS
;
4756 tf
.flags
|= ATA_TFLAG_ISADDR
| ATA_TFLAG_DEVICE
;
4757 tf
.protocol
= ATA_PROT_NODATA
;
4759 tf
.device
|= (heads
- 1) & 0x0f; /* max head = num. of heads - 1 */
4761 err_mask
= ata_exec_internal(dev
, &tf
, NULL
, DMA_NONE
, NULL
, 0, 0);
4762 /* A clean abort indicates an original or just out of spec drive
4763 and we should continue as we issue the setup based on the
4764 drive reported working geometry */
4765 if (err_mask
== AC_ERR_DEV
&& (tf
.feature
& ATA_ABORTED
))
4768 DPRINTK("EXIT, err_mask=%x\n", err_mask
);
4773 * ata_sg_clean - Unmap DMA memory associated with command
4774 * @qc: Command containing DMA memory to be released
4776 * Unmap all mapped DMA memory associated with this command.
4779 * spin_lock_irqsave(host lock)
4781 void ata_sg_clean(struct ata_queued_cmd
*qc
)
4783 struct ata_port
*ap
= qc
->ap
;
4784 struct scatterlist
*sg
= qc
->sg
;
4785 int dir
= qc
->dma_dir
;
4787 WARN_ON(sg
== NULL
);
4789 VPRINTK("unmapping %u sg elements\n", qc
->n_elem
);
4792 dma_unmap_sg(ap
->dev
, sg
, qc
->n_elem
, dir
);
4794 qc
->flags
&= ~ATA_QCFLAG_DMAMAP
;
4799 * ata_fill_sg - Fill PCI IDE PRD table
4800 * @qc: Metadata associated with taskfile to be transferred
4802 * Fill PCI IDE PRD (scatter-gather) table with segments
4803 * associated with the current disk command.
4806 * spin_lock_irqsave(host lock)
4809 static void ata_fill_sg(struct ata_queued_cmd
*qc
)
4811 struct ata_port
*ap
= qc
->ap
;
4812 struct scatterlist
*sg
;
4813 unsigned int si
, pi
;
4816 for_each_sg(qc
->sg
, sg
, qc
->n_elem
, si
) {
4820 /* determine if physical DMA addr spans 64K boundary.
4821 * Note h/w doesn't support 64-bit, so we unconditionally
4822 * truncate dma_addr_t to u32.
4824 addr
= (u32
) sg_dma_address(sg
);
4825 sg_len
= sg_dma_len(sg
);
4828 offset
= addr
& 0xffff;
4830 if ((offset
+ sg_len
) > 0x10000)
4831 len
= 0x10000 - offset
;
4833 ap
->prd
[pi
].addr
= cpu_to_le32(addr
);
4834 ap
->prd
[pi
].flags_len
= cpu_to_le32(len
& 0xffff);
4835 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi
, addr
, len
);
4843 ap
->prd
[pi
- 1].flags_len
|= cpu_to_le32(ATA_PRD_EOT
);
4847 * ata_fill_sg_dumb - Fill PCI IDE PRD table
4848 * @qc: Metadata associated with taskfile to be transferred
4850 * Fill PCI IDE PRD (scatter-gather) table with segments
4851 * associated with the current disk command. Perform the fill
4852 * so that we avoid writing any length 64K records for
4853 * controllers that don't follow the spec.
4856 * spin_lock_irqsave(host lock)
4859 static void ata_fill_sg_dumb(struct ata_queued_cmd
*qc
)
4861 struct ata_port
*ap
= qc
->ap
;
4862 struct scatterlist
*sg
;
4863 unsigned int si
, pi
;
4866 for_each_sg(qc
->sg
, sg
, qc
->n_elem
, si
) {
4868 u32 sg_len
, len
, blen
;
4870 /* determine if physical DMA addr spans 64K boundary.
4871 * Note h/w doesn't support 64-bit, so we unconditionally
4872 * truncate dma_addr_t to u32.
4874 addr
= (u32
) sg_dma_address(sg
);
4875 sg_len
= sg_dma_len(sg
);
4878 offset
= addr
& 0xffff;
4880 if ((offset
+ sg_len
) > 0x10000)
4881 len
= 0x10000 - offset
;
4883 blen
= len
& 0xffff;
4884 ap
->prd
[pi
].addr
= cpu_to_le32(addr
);
4886 /* Some PATA chipsets like the CS5530 can't
4887 cope with 0x0000 meaning 64K as the spec says */
4888 ap
->prd
[pi
].flags_len
= cpu_to_le32(0x8000);
4890 ap
->prd
[++pi
].addr
= cpu_to_le32(addr
+ 0x8000);
4892 ap
->prd
[pi
].flags_len
= cpu_to_le32(blen
);
4893 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi
, addr
, len
);
4901 ap
->prd
[pi
- 1].flags_len
|= cpu_to_le32(ATA_PRD_EOT
);
4905 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
4906 * @qc: Metadata associated with taskfile to check
4908 * Allow low-level driver to filter ATA PACKET commands, returning
4909 * a status indicating whether or not it is OK to use DMA for the
4910 * supplied PACKET command.
4913 * spin_lock_irqsave(host lock)
4915 * RETURNS: 0 when ATAPI DMA can be used
4918 int ata_check_atapi_dma(struct ata_queued_cmd
*qc
)
4920 struct ata_port
*ap
= qc
->ap
;
4922 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4923 * few ATAPI devices choke on such DMA requests.
4925 if (unlikely(qc
->nbytes
& 15))
4928 if (ap
->ops
->check_atapi_dma
)
4929 return ap
->ops
->check_atapi_dma(qc
);
4935 * ata_std_qc_defer - Check whether a qc needs to be deferred
4936 * @qc: ATA command in question
4938 * Non-NCQ commands cannot run with any other command, NCQ or
4939 * not. As upper layer only knows the queue depth, we are
4940 * responsible for maintaining exclusion. This function checks
4941 * whether a new command @qc can be issued.
4944 * spin_lock_irqsave(host lock)
4947 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4949 int ata_std_qc_defer(struct ata_queued_cmd
*qc
)
4951 struct ata_link
*link
= qc
->dev
->link
;
4953 if (qc
->tf
.protocol
== ATA_PROT_NCQ
) {
4954 if (!ata_tag_valid(link
->active_tag
))
4957 if (!ata_tag_valid(link
->active_tag
) && !link
->sactive
)
4961 return ATA_DEFER_LINK
;
4965 * ata_qc_prep - Prepare taskfile for submission
4966 * @qc: Metadata associated with taskfile to be prepared
4968 * Prepare ATA taskfile for submission.
4971 * spin_lock_irqsave(host lock)
4973 void ata_qc_prep(struct ata_queued_cmd
*qc
)
4975 if (!(qc
->flags
& ATA_QCFLAG_DMAMAP
))
4982 * ata_dumb_qc_prep - Prepare taskfile for submission
4983 * @qc: Metadata associated with taskfile to be prepared
4985 * Prepare ATA taskfile for submission.
4988 * spin_lock_irqsave(host lock)
4990 void ata_dumb_qc_prep(struct ata_queued_cmd
*qc
)
4992 if (!(qc
->flags
& ATA_QCFLAG_DMAMAP
))
4995 ata_fill_sg_dumb(qc
);
4998 void ata_noop_qc_prep(struct ata_queued_cmd
*qc
) { }
5001 * ata_sg_init - Associate command with scatter-gather table.
5002 * @qc: Command to be associated
5003 * @sg: Scatter-gather table.
5004 * @n_elem: Number of elements in s/g table.
5006 * Initialize the data-related elements of queued_cmd @qc
5007 * to point to a scatter-gather table @sg, containing @n_elem
5011 * spin_lock_irqsave(host lock)
5013 void ata_sg_init(struct ata_queued_cmd
*qc
, struct scatterlist
*sg
,
5014 unsigned int n_elem
)
5017 qc
->n_elem
= n_elem
;
5022 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
5023 * @qc: Command with scatter-gather table to be mapped.
5025 * DMA-map the scatter-gather table associated with queued_cmd @qc.
5028 * spin_lock_irqsave(host lock)
5031 * Zero on success, negative on error.
5034 static int ata_sg_setup(struct ata_queued_cmd
*qc
)
5036 struct ata_port
*ap
= qc
->ap
;
5037 unsigned int n_elem
;
5039 VPRINTK("ENTER, ata%u\n", ap
->print_id
);
5041 n_elem
= dma_map_sg(ap
->dev
, qc
->sg
, qc
->n_elem
, qc
->dma_dir
);
5045 DPRINTK("%d sg elements mapped\n", n_elem
);
5047 qc
->n_elem
= n_elem
;
5048 qc
->flags
|= ATA_QCFLAG_DMAMAP
;
5054 * swap_buf_le16 - swap halves of 16-bit words in place
5055 * @buf: Buffer to swap
5056 * @buf_words: Number of 16-bit words in buffer.
5058 * Swap halves of 16-bit words if needed to convert from
5059 * little-endian byte order to native cpu byte order, or
5063 * Inherited from caller.
5065 void swap_buf_le16(u16
*buf
, unsigned int buf_words
)
5070 for (i
= 0; i
< buf_words
; i
++)
5071 buf
[i
] = le16_to_cpu(buf
[i
]);
5072 #endif /* __BIG_ENDIAN */
5076 * ata_data_xfer - Transfer data by PIO
5077 * @dev: device to target
5079 * @buflen: buffer length
5082 * Transfer data from/to the device data register by PIO.
5085 * Inherited from caller.
5090 unsigned int ata_data_xfer(struct ata_device
*dev
, unsigned char *buf
,
5091 unsigned int buflen
, int rw
)
5093 struct ata_port
*ap
= dev
->link
->ap
;
5094 void __iomem
*data_addr
= ap
->ioaddr
.data_addr
;
5095 unsigned int words
= buflen
>> 1;
5097 /* Transfer multiple of 2 bytes */
5099 ioread16_rep(data_addr
, buf
, words
);
5101 iowrite16_rep(data_addr
, buf
, words
);
5103 /* Transfer trailing 1 byte, if any. */
5104 if (unlikely(buflen
& 0x01)) {
5105 __le16 align_buf
[1] = { 0 };
5106 unsigned char *trailing_buf
= buf
+ buflen
- 1;
5109 align_buf
[0] = cpu_to_le16(ioread16(data_addr
));
5110 memcpy(trailing_buf
, align_buf
, 1);
5112 memcpy(align_buf
, trailing_buf
, 1);
5113 iowrite16(le16_to_cpu(align_buf
[0]), data_addr
);
5122 * ata_data_xfer_noirq - Transfer data by PIO
5123 * @dev: device to target
5125 * @buflen: buffer length
5128 * Transfer data from/to the device data register by PIO. Do the
5129 * transfer with interrupts disabled.
5132 * Inherited from caller.
5137 unsigned int ata_data_xfer_noirq(struct ata_device
*dev
, unsigned char *buf
,
5138 unsigned int buflen
, int rw
)
5140 unsigned long flags
;
5141 unsigned int consumed
;
5143 local_irq_save(flags
);
5144 consumed
= ata_data_xfer(dev
, buf
, buflen
, rw
);
5145 local_irq_restore(flags
);
5152 * ata_pio_sector - Transfer a sector of data.
5153 * @qc: Command on going
5155 * Transfer qc->sect_size bytes of data from/to the ATA device.
5158 * Inherited from caller.
5161 static void ata_pio_sector(struct ata_queued_cmd
*qc
)
5163 int do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
);
5164 struct ata_port
*ap
= qc
->ap
;
5166 unsigned int offset
;
5169 if (qc
->curbytes
== qc
->nbytes
- qc
->sect_size
)
5170 ap
->hsm_task_state
= HSM_ST_LAST
;
5172 page
= sg_page(qc
->cursg
);
5173 offset
= qc
->cursg
->offset
+ qc
->cursg_ofs
;
5175 /* get the current page and offset */
5176 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
5177 offset
%= PAGE_SIZE
;
5179 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
5181 if (PageHighMem(page
)) {
5182 unsigned long flags
;
5184 /* FIXME: use a bounce buffer */
5185 local_irq_save(flags
);
5186 buf
= kmap_atomic(page
, KM_IRQ0
);
5188 /* do the actual data transfer */
5189 ap
->ops
->data_xfer(qc
->dev
, buf
+ offset
, qc
->sect_size
, do_write
);
5191 kunmap_atomic(buf
, KM_IRQ0
);
5192 local_irq_restore(flags
);
5194 buf
= page_address(page
);
5195 ap
->ops
->data_xfer(qc
->dev
, buf
+ offset
, qc
->sect_size
, do_write
);
5198 qc
->curbytes
+= qc
->sect_size
;
5199 qc
->cursg_ofs
+= qc
->sect_size
;
5201 if (qc
->cursg_ofs
== qc
->cursg
->length
) {
5202 qc
->cursg
= sg_next(qc
->cursg
);
5208 * ata_pio_sectors - Transfer one or many sectors.
5209 * @qc: Command on going
5211 * Transfer one or many sectors of data from/to the
5212 * ATA device for the DRQ request.
5215 * Inherited from caller.
5218 static void ata_pio_sectors(struct ata_queued_cmd
*qc
)
5220 if (is_multi_taskfile(&qc
->tf
)) {
5221 /* READ/WRITE MULTIPLE */
5224 WARN_ON(qc
->dev
->multi_count
== 0);
5226 nsect
= min((qc
->nbytes
- qc
->curbytes
) / qc
->sect_size
,
5227 qc
->dev
->multi_count
);
5233 ata_altstatus(qc
->ap
); /* flush */
5237 * atapi_send_cdb - Write CDB bytes to hardware
5238 * @ap: Port to which ATAPI device is attached.
5239 * @qc: Taskfile currently active
5241 * When device has indicated its readiness to accept
5242 * a CDB, this function is called. Send the CDB.
5248 static void atapi_send_cdb(struct ata_port
*ap
, struct ata_queued_cmd
*qc
)
5251 DPRINTK("send cdb\n");
5252 WARN_ON(qc
->dev
->cdb_len
< 12);
5254 ap
->ops
->data_xfer(qc
->dev
, qc
->cdb
, qc
->dev
->cdb_len
, 1);
5255 ata_altstatus(ap
); /* flush */
5257 switch (qc
->tf
.protocol
) {
5258 case ATAPI_PROT_PIO
:
5259 ap
->hsm_task_state
= HSM_ST
;
5261 case ATAPI_PROT_NODATA
:
5262 ap
->hsm_task_state
= HSM_ST_LAST
;
5264 case ATAPI_PROT_DMA
:
5265 ap
->hsm_task_state
= HSM_ST_LAST
;
5266 /* initiate bmdma */
5267 ap
->ops
->bmdma_start(qc
);
5273 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
5274 * @qc: Command on going
5275 * @bytes: number of bytes
5277 * Transfer Transfer data from/to the ATAPI device.
5280 * Inherited from caller.
5283 static int __atapi_pio_bytes(struct ata_queued_cmd
*qc
, unsigned int bytes
)
5285 int rw
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
) ? WRITE
: READ
;
5286 struct ata_port
*ap
= qc
->ap
;
5287 struct ata_device
*dev
= qc
->dev
;
5288 struct ata_eh_info
*ehi
= &dev
->link
->eh_info
;
5289 struct scatterlist
*sg
;
5292 unsigned int offset
, count
, consumed
;
5296 if (unlikely(!sg
)) {
5297 ata_ehi_push_desc(ehi
, "unexpected or too much trailing data "
5298 "buf=%u cur=%u bytes=%u",
5299 qc
->nbytes
, qc
->curbytes
, bytes
);
5304 offset
= sg
->offset
+ qc
->cursg_ofs
;
5306 /* get the current page and offset */
5307 page
= nth_page(page
, (offset
>> PAGE_SHIFT
));
5308 offset
%= PAGE_SIZE
;
5310 /* don't overrun current sg */
5311 count
= min(sg
->length
- qc
->cursg_ofs
, bytes
);
5313 /* don't cross page boundaries */
5314 count
= min(count
, (unsigned int)PAGE_SIZE
- offset
);
5316 DPRINTK("data %s\n", qc
->tf
.flags
& ATA_TFLAG_WRITE
? "write" : "read");
5318 if (PageHighMem(page
)) {
5319 unsigned long flags
;
5321 /* FIXME: use bounce buffer */
5322 local_irq_save(flags
);
5323 buf
= kmap_atomic(page
, KM_IRQ0
);
5325 /* do the actual data transfer */
5326 consumed
= ap
->ops
->data_xfer(dev
, buf
+ offset
, count
, rw
);
5328 kunmap_atomic(buf
, KM_IRQ0
);
5329 local_irq_restore(flags
);
5331 buf
= page_address(page
);
5332 consumed
= ap
->ops
->data_xfer(dev
, buf
+ offset
, count
, rw
);
5335 bytes
-= min(bytes
, consumed
);
5336 qc
->curbytes
+= count
;
5337 qc
->cursg_ofs
+= count
;
5339 if (qc
->cursg_ofs
== sg
->length
) {
5340 qc
->cursg
= sg_next(qc
->cursg
);
5344 /* consumed can be larger than count only for the last transfer */
5345 WARN_ON(qc
->cursg
&& count
!= consumed
);
5353 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
5354 * @qc: Command on going
5356 * Transfer Transfer data from/to the ATAPI device.
5359 * Inherited from caller.
5362 static void atapi_pio_bytes(struct ata_queued_cmd
*qc
)
5364 struct ata_port
*ap
= qc
->ap
;
5365 struct ata_device
*dev
= qc
->dev
;
5366 struct ata_eh_info
*ehi
= &dev
->link
->eh_info
;
5367 unsigned int ireason
, bc_lo
, bc_hi
, bytes
;
5368 int i_write
, do_write
= (qc
->tf
.flags
& ATA_TFLAG_WRITE
) ? 1 : 0;
5370 /* Abuse qc->result_tf for temp storage of intermediate TF
5371 * here to save some kernel stack usage.
5372 * For normal completion, qc->result_tf is not relevant. For
5373 * error, qc->result_tf is later overwritten by ata_qc_complete().
5374 * So, the correctness of qc->result_tf is not affected.
5376 ap
->ops
->tf_read(ap
, &qc
->result_tf
);
5377 ireason
= qc
->result_tf
.nsect
;
5378 bc_lo
= qc
->result_tf
.lbam
;
5379 bc_hi
= qc
->result_tf
.lbah
;
5380 bytes
= (bc_hi
<< 8) | bc_lo
;
5382 /* shall be cleared to zero, indicating xfer of data */
5383 if (unlikely(ireason
& (1 << 0)))
5386 /* make sure transfer direction matches expected */
5387 i_write
= ((ireason
& (1 << 1)) == 0) ? 1 : 0;
5388 if (unlikely(do_write
!= i_write
))
5391 if (unlikely(!bytes
))
5394 VPRINTK("ata%u: xfering %d bytes\n", ap
->print_id
, bytes
);
5396 if (unlikely(__atapi_pio_bytes(qc
, bytes
)))
5398 ata_altstatus(ap
); /* flush */
5403 ata_ehi_push_desc(ehi
, "ATAPI check failed (ireason=0x%x bytes=%u)",
5406 qc
->err_mask
|= AC_ERR_HSM
;
5407 ap
->hsm_task_state
= HSM_ST_ERR
;
5411 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
5412 * @ap: the target ata_port
5416 * 1 if ok in workqueue, 0 otherwise.
5419 static inline int ata_hsm_ok_in_wq(struct ata_port
*ap
, struct ata_queued_cmd
*qc
)
5421 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
5424 if (ap
->hsm_task_state
== HSM_ST_FIRST
) {
5425 if (qc
->tf
.protocol
== ATA_PROT_PIO
&&
5426 (qc
->tf
.flags
& ATA_TFLAG_WRITE
))
5429 if (ata_is_atapi(qc
->tf
.protocol
) &&
5430 !(qc
->dev
->flags
& ATA_DFLAG_CDB_INTR
))
5438 * ata_hsm_qc_complete - finish a qc running on standard HSM
5439 * @qc: Command to complete
5440 * @in_wq: 1 if called from workqueue, 0 otherwise
5442 * Finish @qc which is running on standard HSM.
5445 * If @in_wq is zero, spin_lock_irqsave(host lock).
5446 * Otherwise, none on entry and grabs host lock.
5448 static void ata_hsm_qc_complete(struct ata_queued_cmd
*qc
, int in_wq
)
5450 struct ata_port
*ap
= qc
->ap
;
5451 unsigned long flags
;
5453 if (ap
->ops
->error_handler
) {
5455 spin_lock_irqsave(ap
->lock
, flags
);
5457 /* EH might have kicked in while host lock is
5460 qc
= ata_qc_from_tag(ap
, qc
->tag
);
5462 if (likely(!(qc
->err_mask
& AC_ERR_HSM
))) {
5463 ap
->ops
->irq_on(ap
);
5464 ata_qc_complete(qc
);
5466 ata_port_freeze(ap
);
5469 spin_unlock_irqrestore(ap
->lock
, flags
);
5471 if (likely(!(qc
->err_mask
& AC_ERR_HSM
)))
5472 ata_qc_complete(qc
);
5474 ata_port_freeze(ap
);
5478 spin_lock_irqsave(ap
->lock
, flags
);
5479 ap
->ops
->irq_on(ap
);
5480 ata_qc_complete(qc
);
5481 spin_unlock_irqrestore(ap
->lock
, flags
);
5483 ata_qc_complete(qc
);
5488 * ata_hsm_move - move the HSM to the next state.
5489 * @ap: the target ata_port
5491 * @status: current device status
5492 * @in_wq: 1 if called from workqueue, 0 otherwise
5495 * 1 when poll next status needed, 0 otherwise.
5497 int ata_hsm_move(struct ata_port
*ap
, struct ata_queued_cmd
*qc
,
5498 u8 status
, int in_wq
)
5500 unsigned long flags
= 0;
5503 WARN_ON((qc
->flags
& ATA_QCFLAG_ACTIVE
) == 0);
5505 /* Make sure ata_qc_issue_prot() does not throw things
5506 * like DMA polling into the workqueue. Notice that
5507 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
5509 WARN_ON(in_wq
!= ata_hsm_ok_in_wq(ap
, qc
));
5512 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
5513 ap
->print_id
, qc
->tf
.protocol
, ap
->hsm_task_state
, status
);
5515 switch (ap
->hsm_task_state
) {
5517 /* Send first data block or PACKET CDB */
5519 /* If polling, we will stay in the work queue after
5520 * sending the data. Otherwise, interrupt handler
5521 * takes over after sending the data.
5523 poll_next
= (qc
->tf
.flags
& ATA_TFLAG_POLLING
);
5525 /* check device status */
5526 if (unlikely((status
& ATA_DRQ
) == 0)) {
5527 /* handle BSY=0, DRQ=0 as error */
5528 if (likely(status
& (ATA_ERR
| ATA_DF
)))
5529 /* device stops HSM for abort/error */
5530 qc
->err_mask
|= AC_ERR_DEV
;
5532 /* HSM violation. Let EH handle this */
5533 qc
->err_mask
|= AC_ERR_HSM
;
5535 ap
->hsm_task_state
= HSM_ST_ERR
;
5539 /* Device should not ask for data transfer (DRQ=1)
5540 * when it finds something wrong.
5541 * We ignore DRQ here and stop the HSM by
5542 * changing hsm_task_state to HSM_ST_ERR and
5543 * let the EH abort the command or reset the device.
5545 if (unlikely(status
& (ATA_ERR
| ATA_DF
))) {
5546 /* Some ATAPI tape drives forget to clear the ERR bit
5547 * when doing the next command (mostly request sense).
5548 * We ignore ERR here to workaround and proceed sending
5551 if (!(qc
->dev
->horkage
& ATA_HORKAGE_STUCK_ERR
)) {
5552 ata_port_printk(ap
, KERN_WARNING
,
5553 "DRQ=1 with device error, "
5554 "dev_stat 0x%X\n", status
);
5555 qc
->err_mask
|= AC_ERR_HSM
;
5556 ap
->hsm_task_state
= HSM_ST_ERR
;
5561 /* Send the CDB (atapi) or the first data block (ata pio out).
5562 * During the state transition, interrupt handler shouldn't
5563 * be invoked before the data transfer is complete and
5564 * hsm_task_state is changed. Hence, the following locking.
5567 spin_lock_irqsave(ap
->lock
, flags
);
5569 if (qc
->tf
.protocol
== ATA_PROT_PIO
) {
5570 /* PIO data out protocol.
5571 * send first data block.
5574 /* ata_pio_sectors() might change the state
5575 * to HSM_ST_LAST. so, the state is changed here
5576 * before ata_pio_sectors().
5578 ap
->hsm_task_state
= HSM_ST
;
5579 ata_pio_sectors(qc
);
5582 atapi_send_cdb(ap
, qc
);
5585 spin_unlock_irqrestore(ap
->lock
, flags
);
5587 /* if polling, ata_pio_task() handles the rest.
5588 * otherwise, interrupt handler takes over from here.
5593 /* complete command or read/write the data register */
5594 if (qc
->tf
.protocol
== ATAPI_PROT_PIO
) {
5595 /* ATAPI PIO protocol */
5596 if ((status
& ATA_DRQ
) == 0) {
5597 /* No more data to transfer or device error.
5598 * Device error will be tagged in HSM_ST_LAST.
5600 ap
->hsm_task_state
= HSM_ST_LAST
;
5604 /* Device should not ask for data transfer (DRQ=1)
5605 * when it finds something wrong.
5606 * We ignore DRQ here and stop the HSM by
5607 * changing hsm_task_state to HSM_ST_ERR and
5608 * let the EH abort the command or reset the device.
5610 if (unlikely(status
& (ATA_ERR
| ATA_DF
))) {
5611 ata_port_printk(ap
, KERN_WARNING
, "DRQ=1 with "
5612 "device error, dev_stat 0x%X\n",
5614 qc
->err_mask
|= AC_ERR_HSM
;
5615 ap
->hsm_task_state
= HSM_ST_ERR
;
5619 atapi_pio_bytes(qc
);
5621 if (unlikely(ap
->hsm_task_state
== HSM_ST_ERR
))
5622 /* bad ireason reported by device */
5626 /* ATA PIO protocol */
5627 if (unlikely((status
& ATA_DRQ
) == 0)) {
5628 /* handle BSY=0, DRQ=0 as error */
5629 if (likely(status
& (ATA_ERR
| ATA_DF
)))
5630 /* device stops HSM for abort/error */
5631 qc
->err_mask
|= AC_ERR_DEV
;
5633 /* HSM violation. Let EH handle this.
5634 * Phantom devices also trigger this
5635 * condition. Mark hint.
5637 qc
->err_mask
|= AC_ERR_HSM
|
5640 ap
->hsm_task_state
= HSM_ST_ERR
;
5644 /* For PIO reads, some devices may ask for
5645 * data transfer (DRQ=1) alone with ERR=1.
5646 * We respect DRQ here and transfer one
5647 * block of junk data before changing the
5648 * hsm_task_state to HSM_ST_ERR.
5650 * For PIO writes, ERR=1 DRQ=1 doesn't make
5651 * sense since the data block has been
5652 * transferred to the device.
5654 if (unlikely(status
& (ATA_ERR
| ATA_DF
))) {
5655 /* data might be corrputed */
5656 qc
->err_mask
|= AC_ERR_DEV
;
5658 if (!(qc
->tf
.flags
& ATA_TFLAG_WRITE
)) {
5659 ata_pio_sectors(qc
);
5660 status
= ata_wait_idle(ap
);
5663 if (status
& (ATA_BUSY
| ATA_DRQ
))
5664 qc
->err_mask
|= AC_ERR_HSM
;
5666 /* ata_pio_sectors() might change the
5667 * state to HSM_ST_LAST. so, the state
5668 * is changed after ata_pio_sectors().
5670 ap
->hsm_task_state
= HSM_ST_ERR
;
5674 ata_pio_sectors(qc
);
5676 if (ap
->hsm_task_state
== HSM_ST_LAST
&&
5677 (!(qc
->tf
.flags
& ATA_TFLAG_WRITE
))) {
5679 status
= ata_wait_idle(ap
);
5688 if (unlikely(!ata_ok(status
))) {
5689 qc
->err_mask
|= __ac_err_mask(status
);
5690 ap
->hsm_task_state
= HSM_ST_ERR
;
5694 /* no more data to transfer */
5695 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
5696 ap
->print_id
, qc
->dev
->devno
, status
);
5698 WARN_ON(qc
->err_mask
);
5700 ap
->hsm_task_state
= HSM_ST_IDLE
;
5702 /* complete taskfile transaction */
5703 ata_hsm_qc_complete(qc
, in_wq
);
5709 /* make sure qc->err_mask is available to
5710 * know what's wrong and recover
5712 WARN_ON(qc
->err_mask
== 0);
5714 ap
->hsm_task_state
= HSM_ST_IDLE
;
5716 /* complete taskfile transaction */
5717 ata_hsm_qc_complete(qc
, in_wq
);
5729 static void ata_pio_task(struct work_struct
*work
)
5731 struct ata_port
*ap
=
5732 container_of(work
, struct ata_port
, port_task
.work
);
5733 struct ata_queued_cmd
*qc
= ap
->port_task_data
;
5738 WARN_ON(ap
->hsm_task_state
== HSM_ST_IDLE
);
5741 * This is purely heuristic. This is a fast path.
5742 * Sometimes when we enter, BSY will be cleared in
5743 * a chk-status or two. If not, the drive is probably seeking
5744 * or something. Snooze for a couple msecs, then
5745 * chk-status again. If still busy, queue delayed work.
5747 status
= ata_busy_wait(ap
, ATA_BUSY
, 5);
5748 if (status
& ATA_BUSY
) {
5750 status
= ata_busy_wait(ap
, ATA_BUSY
, 10);
5751 if (status
& ATA_BUSY
) {
5752 ata_pio_queue_task(ap
, qc
, ATA_SHORT_PAUSE
);
5758 poll_next
= ata_hsm_move(ap
, qc
, status
, 1);
5760 /* another command or interrupt handler
5761 * may be running at this point.
5768 * ata_qc_new - Request an available ATA command, for queueing
5769 * @ap: Port associated with device @dev
5770 * @dev: Device from whom we request an available command structure
5776 static struct ata_queued_cmd
*ata_qc_new(struct ata_port
*ap
)
5778 struct ata_queued_cmd
*qc
= NULL
;
5781 /* no command while frozen */
5782 if (unlikely(ap
->pflags
& ATA_PFLAG_FROZEN
))
5785 /* the last tag is reserved for internal command. */
5786 for (i
= 0; i
< ATA_MAX_QUEUE
- 1; i
++)
5787 if (!test_and_set_bit(i
, &ap
->qc_allocated
)) {
5788 qc
= __ata_qc_from_tag(ap
, i
);
5799 * ata_qc_new_init - Request an available ATA command, and initialize it
5800 * @dev: Device from whom we request an available command structure
5806 struct ata_queued_cmd
*ata_qc_new_init(struct ata_device
*dev
)
5808 struct ata_port
*ap
= dev
->link
->ap
;
5809 struct ata_queued_cmd
*qc
;
5811 qc
= ata_qc_new(ap
);
5824 * ata_qc_free - free unused ata_queued_cmd
5825 * @qc: Command to complete
5827 * Designed to free unused ata_queued_cmd object
5828 * in case something prevents using it.
5831 * spin_lock_irqsave(host lock)
5833 void ata_qc_free(struct ata_queued_cmd
*qc
)
5835 struct ata_port
*ap
= qc
->ap
;
5838 WARN_ON(qc
== NULL
); /* ata_qc_from_tag _might_ return NULL */
5842 if (likely(ata_tag_valid(tag
))) {
5843 qc
->tag
= ATA_TAG_POISON
;
5844 clear_bit(tag
, &ap
->qc_allocated
);
5848 void __ata_qc_complete(struct ata_queued_cmd
*qc
)
5850 struct ata_port
*ap
= qc
->ap
;
5851 struct ata_link
*link
= qc
->dev
->link
;
5853 WARN_ON(qc
== NULL
); /* ata_qc_from_tag _might_ return NULL */
5854 WARN_ON(!(qc
->flags
& ATA_QCFLAG_ACTIVE
));
5856 if (likely(qc
->flags
& ATA_QCFLAG_DMAMAP
))
5859 /* command should be marked inactive atomically with qc completion */
5860 if (qc
->tf
.protocol
== ATA_PROT_NCQ
) {
5861 link
->sactive
&= ~(1 << qc
->tag
);
5863 ap
->nr_active_links
--;
5865 link
->active_tag
= ATA_TAG_POISON
;
5866 ap
->nr_active_links
--;
5869 /* clear exclusive status */
5870 if (unlikely(qc
->flags
& ATA_QCFLAG_CLEAR_EXCL
&&
5871 ap
->excl_link
== link
))
5872 ap
->excl_link
= NULL
;
5874 /* atapi: mark qc as inactive to prevent the interrupt handler
5875 * from completing the command twice later, before the error handler
5876 * is called. (when rc != 0 and atapi request sense is needed)
5878 qc
->flags
&= ~ATA_QCFLAG_ACTIVE
;
5879 ap
->qc_active
&= ~(1 << qc
->tag
);
5881 /* call completion callback */
5882 qc
->complete_fn(qc
);
5885 static void fill_result_tf(struct ata_queued_cmd
*qc
)
5887 struct ata_port
*ap
= qc
->ap
;
5889 qc
->result_tf
.flags
= qc
->tf
.flags
;
5890 ap
->ops
->tf_read(ap
, &qc
->result_tf
);
5893 static void ata_verify_xfer(struct ata_queued_cmd
*qc
)
5895 struct ata_device
*dev
= qc
->dev
;
5897 if (ata_tag_internal(qc
->tag
))
5900 if (ata_is_nodata(qc
->tf
.protocol
))
5903 if ((dev
->mwdma_mask
|| dev
->udma_mask
) && ata_is_pio(qc
->tf
.protocol
))
5906 dev
->flags
&= ~ATA_DFLAG_DUBIOUS_XFER
;
5910 * ata_qc_complete - Complete an active ATA command
5911 * @qc: Command to complete
5912 * @err_mask: ATA Status register contents
5914 * Indicate to the mid and upper layers that an ATA
5915 * command has completed, with either an ok or not-ok status.
5918 * spin_lock_irqsave(host lock)
5920 void ata_qc_complete(struct ata_queued_cmd
*qc
)
5922 struct ata_port
*ap
= qc
->ap
;
5924 /* XXX: New EH and old EH use different mechanisms to
5925 * synchronize EH with regular execution path.
5927 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5928 * Normal execution path is responsible for not accessing a
5929 * failed qc. libata core enforces the rule by returning NULL
5930 * from ata_qc_from_tag() for failed qcs.
5932 * Old EH depends on ata_qc_complete() nullifying completion
5933 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
5934 * not synchronize with interrupt handler. Only PIO task is
5937 if (ap
->ops
->error_handler
) {
5938 struct ata_device
*dev
= qc
->dev
;
5939 struct ata_eh_info
*ehi
= &dev
->link
->eh_info
;
5941 WARN_ON(ap
->pflags
& ATA_PFLAG_FROZEN
);
5943 if (unlikely(qc
->err_mask
))
5944 qc
->flags
|= ATA_QCFLAG_FAILED
;
5946 if (unlikely(qc
->flags
& ATA_QCFLAG_FAILED
)) {
5947 if (!ata_tag_internal(qc
->tag
)) {
5948 /* always fill result TF for failed qc */
5950 ata_qc_schedule_eh(qc
);
5955 /* read result TF if requested */
5956 if (qc
->flags
& ATA_QCFLAG_RESULT_TF
)
5959 /* Some commands need post-processing after successful
5962 switch (qc
->tf
.command
) {
5963 case ATA_CMD_SET_FEATURES
:
5964 if (qc
->tf
.feature
!= SETFEATURES_WC_ON
&&
5965 qc
->tf
.feature
!= SETFEATURES_WC_OFF
)
5968 case ATA_CMD_INIT_DEV_PARAMS
: /* CHS translation changed */
5969 case ATA_CMD_SET_MULTI
: /* multi_count changed */
5970 /* revalidate device */
5971 ehi
->dev_action
[dev
->devno
] |= ATA_EH_REVALIDATE
;
5972 ata_port_schedule_eh(ap
);
5976 dev
->flags
|= ATA_DFLAG_SLEEPING
;
5980 if (unlikely(dev
->flags
& ATA_DFLAG_DUBIOUS_XFER
))
5981 ata_verify_xfer(qc
);
5983 __ata_qc_complete(qc
);
5985 if (qc
->flags
& ATA_QCFLAG_EH_SCHEDULED
)
5988 /* read result TF if failed or requested */
5989 if (qc
->err_mask
|| qc
->flags
& ATA_QCFLAG_RESULT_TF
)
5992 __ata_qc_complete(qc
);
5997 * ata_qc_complete_multiple - Complete multiple qcs successfully
5998 * @ap: port in question
5999 * @qc_active: new qc_active mask
6000 * @finish_qc: LLDD callback invoked before completing a qc
6002 * Complete in-flight commands. This functions is meant to be
6003 * called from low-level driver's interrupt routine to complete
6004 * requests normally. ap->qc_active and @qc_active is compared
6005 * and commands are completed accordingly.
6008 * spin_lock_irqsave(host lock)
6011 * Number of completed commands on success, -errno otherwise.
6013 int ata_qc_complete_multiple(struct ata_port
*ap
, u32 qc_active
,
6014 void (*finish_qc
)(struct ata_queued_cmd
*))
6020 done_mask
= ap
->qc_active
^ qc_active
;
6022 if (unlikely(done_mask
& qc_active
)) {
6023 ata_port_printk(ap
, KERN_ERR
, "illegal qc_active transition "
6024 "(%08x->%08x)\n", ap
->qc_active
, qc_active
);
6028 for (i
= 0; i
< ATA_MAX_QUEUE
; i
++) {
6029 struct ata_queued_cmd
*qc
;
6031 if (!(done_mask
& (1 << i
)))
6034 if ((qc
= ata_qc_from_tag(ap
, i
))) {
6037 ata_qc_complete(qc
);
6046 * ata_qc_issue - issue taskfile to device
6047 * @qc: command to issue to device
6049 * Prepare an ATA command to submission to device.
6050 * This includes mapping the data into a DMA-able
6051 * area, filling in the S/G table, and finally
6052 * writing the taskfile to hardware, starting the command.
6055 * spin_lock_irqsave(host lock)
6057 void ata_qc_issue(struct ata_queued_cmd
*qc
)
6059 struct ata_port
*ap
= qc
->ap
;
6060 struct ata_link
*link
= qc
->dev
->link
;
6061 u8 prot
= qc
->tf
.protocol
;
6063 /* Make sure only one non-NCQ command is outstanding. The
6064 * check is skipped for old EH because it reuses active qc to
6065 * request ATAPI sense.
6067 WARN_ON(ap
->ops
->error_handler
&& ata_tag_valid(link
->active_tag
));
6069 if (ata_is_ncq(prot
)) {
6070 WARN_ON(link
->sactive
& (1 << qc
->tag
));
6073 ap
->nr_active_links
++;
6074 link
->sactive
|= 1 << qc
->tag
;
6076 WARN_ON(link
->sactive
);
6078 ap
->nr_active_links
++;
6079 link
->active_tag
= qc
->tag
;
6082 qc
->flags
|= ATA_QCFLAG_ACTIVE
;
6083 ap
->qc_active
|= 1 << qc
->tag
;
6085 /* We guarantee to LLDs that they will have at least one
6086 * non-zero sg if the command is a data command.
6088 BUG_ON(ata_is_data(prot
) && (!qc
->sg
|| !qc
->n_elem
|| !qc
->nbytes
));
6090 if (ata_is_dma(prot
) || (ata_is_pio(prot
) &&
6091 (ap
->flags
& ATA_FLAG_PIO_DMA
)))
6092 if (ata_sg_setup(qc
))
6095 /* if device is sleeping, schedule reset and abort the link */
6096 if (unlikely(qc
->dev
->flags
& ATA_DFLAG_SLEEPING
)) {
6097 link
->eh_info
.action
|= ATA_EH_RESET
;
6098 ata_ehi_push_desc(&link
->eh_info
, "waking up from sleep");
6099 ata_link_abort(link
);
6103 ap
->ops
->qc_prep(qc
);
6105 qc
->err_mask
|= ap
->ops
->qc_issue(qc
);
6106 if (unlikely(qc
->err_mask
))
6111 qc
->err_mask
|= AC_ERR_SYSTEM
;
6113 ata_qc_complete(qc
);
6117 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
6118 * @qc: command to issue to device
6120 * Using various libata functions and hooks, this function
6121 * starts an ATA command. ATA commands are grouped into
6122 * classes called "protocols", and issuing each type of protocol
6123 * is slightly different.
6125 * May be used as the qc_issue() entry in ata_port_operations.
6128 * spin_lock_irqsave(host lock)
6131 * Zero on success, AC_ERR_* mask on failure
6134 unsigned int ata_qc_issue_prot(struct ata_queued_cmd
*qc
)
6136 struct ata_port
*ap
= qc
->ap
;
6138 /* Use polling pio if the LLD doesn't handle
6139 * interrupt driven pio and atapi CDB interrupt.
6141 if (ap
->flags
& ATA_FLAG_PIO_POLLING
) {
6142 switch (qc
->tf
.protocol
) {
6144 case ATA_PROT_NODATA
:
6145 case ATAPI_PROT_PIO
:
6146 case ATAPI_PROT_NODATA
:
6147 qc
->tf
.flags
|= ATA_TFLAG_POLLING
;
6149 case ATAPI_PROT_DMA
:
6150 if (qc
->dev
->flags
& ATA_DFLAG_CDB_INTR
)
6151 /* see ata_dma_blacklisted() */
6159 /* select the device */
6160 ata_dev_select(ap
, qc
->dev
->devno
, 1, 0);
6162 /* start the command */
6163 switch (qc
->tf
.protocol
) {
6164 case ATA_PROT_NODATA
:
6165 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
6166 ata_qc_set_polling(qc
);
6168 ata_tf_to_host(ap
, &qc
->tf
);
6169 ap
->hsm_task_state
= HSM_ST_LAST
;
6171 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
6172 ata_pio_queue_task(ap
, qc
, 0);
6177 WARN_ON(qc
->tf
.flags
& ATA_TFLAG_POLLING
);
6179 ap
->ops
->tf_load(ap
, &qc
->tf
); /* load tf registers */
6180 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
6181 ap
->ops
->bmdma_start(qc
); /* initiate bmdma */
6182 ap
->hsm_task_state
= HSM_ST_LAST
;
6186 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
6187 ata_qc_set_polling(qc
);
6189 ata_tf_to_host(ap
, &qc
->tf
);
6191 if (qc
->tf
.flags
& ATA_TFLAG_WRITE
) {
6192 /* PIO data out protocol */
6193 ap
->hsm_task_state
= HSM_ST_FIRST
;
6194 ata_pio_queue_task(ap
, qc
, 0);
6196 /* always send first data block using
6197 * the ata_pio_task() codepath.
6200 /* PIO data in protocol */
6201 ap
->hsm_task_state
= HSM_ST
;
6203 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
6204 ata_pio_queue_task(ap
, qc
, 0);
6206 /* if polling, ata_pio_task() handles the rest.
6207 * otherwise, interrupt handler takes over from here.
6213 case ATAPI_PROT_PIO
:
6214 case ATAPI_PROT_NODATA
:
6215 if (qc
->tf
.flags
& ATA_TFLAG_POLLING
)
6216 ata_qc_set_polling(qc
);
6218 ata_tf_to_host(ap
, &qc
->tf
);
6220 ap
->hsm_task_state
= HSM_ST_FIRST
;
6222 /* send cdb by polling if no cdb interrupt */
6223 if ((!(qc
->dev
->flags
& ATA_DFLAG_CDB_INTR
)) ||
6224 (qc
->tf
.flags
& ATA_TFLAG_POLLING
))
6225 ata_pio_queue_task(ap
, qc
, 0);
6228 case ATAPI_PROT_DMA
:
6229 WARN_ON(qc
->tf
.flags
& ATA_TFLAG_POLLING
);
6231 ap
->ops
->tf_load(ap
, &qc
->tf
); /* load tf registers */
6232 ap
->ops
->bmdma_setup(qc
); /* set up bmdma */
6233 ap
->hsm_task_state
= HSM_ST_FIRST
;
6235 /* send cdb by polling if no cdb interrupt */
6236 if (!(qc
->dev
->flags
& ATA_DFLAG_CDB_INTR
))
6237 ata_pio_queue_task(ap
, qc
, 0);
6242 return AC_ERR_SYSTEM
;
6249 * ata_host_intr - Handle host interrupt for given (port, task)
6250 * @ap: Port on which interrupt arrived (possibly...)
6251 * @qc: Taskfile currently active in engine
6253 * Handle host interrupt for given queued command. Currently,
6254 * only DMA interrupts are handled. All other commands are
6255 * handled via polling with interrupts disabled (nIEN bit).
6258 * spin_lock_irqsave(host lock)
6261 * One if interrupt was handled, zero if not (shared irq).
6264 inline unsigned int ata_host_intr(struct ata_port
*ap
,
6265 struct ata_queued_cmd
*qc
)
6267 struct ata_eh_info
*ehi
= &ap
->link
.eh_info
;
6268 u8 status
, host_stat
= 0;
6270 VPRINTK("ata%u: protocol %d task_state %d\n",
6271 ap
->print_id
, qc
->tf
.protocol
, ap
->hsm_task_state
);
6273 /* Check whether we are expecting interrupt in this state */
6274 switch (ap
->hsm_task_state
) {
6276 /* Some pre-ATAPI-4 devices assert INTRQ
6277 * at this state when ready to receive CDB.
6280 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
6281 * The flag was turned on only for atapi devices. No
6282 * need to check ata_is_atapi(qc->tf.protocol) again.
6284 if (!(qc
->dev
->flags
& ATA_DFLAG_CDB_INTR
))
6288 if (qc
->tf
.protocol
== ATA_PROT_DMA
||
6289 qc
->tf
.protocol
== ATAPI_PROT_DMA
) {
6290 /* check status of DMA engine */
6291 host_stat
= ap
->ops
->bmdma_status(ap
);
6292 VPRINTK("ata%u: host_stat 0x%X\n",
6293 ap
->print_id
, host_stat
);
6295 /* if it's not our irq... */
6296 if (!(host_stat
& ATA_DMA_INTR
))
6299 /* before we do anything else, clear DMA-Start bit */
6300 ap
->ops
->bmdma_stop(qc
);
6302 if (unlikely(host_stat
& ATA_DMA_ERR
)) {
6303 /* error when transfering data to/from memory */
6304 qc
->err_mask
|= AC_ERR_HOST_BUS
;
6305 ap
->hsm_task_state
= HSM_ST_ERR
;
6315 /* check altstatus */
6316 status
= ata_altstatus(ap
);
6317 if (status
& ATA_BUSY
)
6320 /* check main status, clearing INTRQ */
6321 status
= ata_chk_status(ap
);
6322 if (unlikely(status
& ATA_BUSY
))
6325 /* ack bmdma irq events */
6326 ap
->ops
->irq_clear(ap
);
6328 ata_hsm_move(ap
, qc
, status
, 0);
6330 if (unlikely(qc
->err_mask
) && (qc
->tf
.protocol
== ATA_PROT_DMA
||
6331 qc
->tf
.protocol
== ATAPI_PROT_DMA
))
6332 ata_ehi_push_desc(ehi
, "BMDMA stat 0x%x", host_stat
);
6334 return 1; /* irq handled */
6337 ap
->stats
.idle_irq
++;
6340 if ((ap
->stats
.idle_irq
% 1000) == 0) {
6342 ap
->ops
->irq_clear(ap
);
6343 ata_port_printk(ap
, KERN_WARNING
, "irq trap\n");
6347 return 0; /* irq not handled */
6351 * ata_interrupt - Default ATA host interrupt handler
6352 * @irq: irq line (unused)
6353 * @dev_instance: pointer to our ata_host information structure
6355 * Default interrupt handler for PCI IDE devices. Calls
6356 * ata_host_intr() for each port that is not disabled.
6359 * Obtains host lock during operation.
6362 * IRQ_NONE or IRQ_HANDLED.
6365 irqreturn_t
ata_interrupt(int irq
, void *dev_instance
)
6367 struct ata_host
*host
= dev_instance
;
6369 unsigned int handled
= 0;
6370 unsigned long flags
;
6372 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
6373 spin_lock_irqsave(&host
->lock
, flags
);
6375 for (i
= 0; i
< host
->n_ports
; i
++) {
6376 struct ata_port
*ap
;
6378 ap
= host
->ports
[i
];
6380 !(ap
->flags
& ATA_FLAG_DISABLED
)) {
6381 struct ata_queued_cmd
*qc
;
6383 qc
= ata_qc_from_tag(ap
, ap
->link
.active_tag
);
6384 if (qc
&& (!(qc
->tf
.flags
& ATA_TFLAG_POLLING
)) &&
6385 (qc
->flags
& ATA_QCFLAG_ACTIVE
))
6386 handled
|= ata_host_intr(ap
, qc
);
6390 spin_unlock_irqrestore(&host
->lock
, flags
);
6392 return IRQ_RETVAL(handled
);
6396 * sata_scr_valid - test whether SCRs are accessible
6397 * @link: ATA link to test SCR accessibility for
6399 * Test whether SCRs are accessible for @link.
6405 * 1 if SCRs are accessible, 0 otherwise.
6407 int sata_scr_valid(struct ata_link
*link
)
6409 struct ata_port
*ap
= link
->ap
;
6411 return (ap
->flags
& ATA_FLAG_SATA
) && ap
->ops
->scr_read
;
6415 * sata_scr_read - read SCR register of the specified port
6416 * @link: ATA link to read SCR for
6418 * @val: Place to store read value
6420 * Read SCR register @reg of @link into *@val. This function is
6421 * guaranteed to succeed if @link is ap->link, the cable type of
6422 * the port is SATA and the port implements ->scr_read.
6425 * None if @link is ap->link. Kernel thread context otherwise.
6428 * 0 on success, negative errno on failure.
6430 int sata_scr_read(struct ata_link
*link
, int reg
, u32
*val
)
6432 if (ata_is_host_link(link
)) {
6433 struct ata_port
*ap
= link
->ap
;
6435 if (sata_scr_valid(link
))
6436 return ap
->ops
->scr_read(ap
, reg
, val
);
6440 return sata_pmp_scr_read(link
, reg
, val
);
6444 * sata_scr_write - write SCR register of the specified port
6445 * @link: ATA link to write SCR for
6446 * @reg: SCR to write
6447 * @val: value to write
6449 * Write @val to SCR register @reg of @link. This function is
6450 * guaranteed to succeed if @link is ap->link, the cable type of
6451 * the port is SATA and the port implements ->scr_read.
6454 * None if @link is ap->link. Kernel thread context otherwise.
6457 * 0 on success, negative errno on failure.
6459 int sata_scr_write(struct ata_link
*link
, int reg
, u32 val
)
6461 if (ata_is_host_link(link
)) {
6462 struct ata_port
*ap
= link
->ap
;
6464 if (sata_scr_valid(link
))
6465 return ap
->ops
->scr_write(ap
, reg
, val
);
6469 return sata_pmp_scr_write(link
, reg
, val
);
6473 * sata_scr_write_flush - write SCR register of the specified port and flush
6474 * @link: ATA link to write SCR for
6475 * @reg: SCR to write
6476 * @val: value to write
6478 * This function is identical to sata_scr_write() except that this
6479 * function performs flush after writing to the register.
6482 * None if @link is ap->link. Kernel thread context otherwise.
6485 * 0 on success, negative errno on failure.
6487 int sata_scr_write_flush(struct ata_link
*link
, int reg
, u32 val
)
6489 if (ata_is_host_link(link
)) {
6490 struct ata_port
*ap
= link
->ap
;
6493 if (sata_scr_valid(link
)) {
6494 rc
= ap
->ops
->scr_write(ap
, reg
, val
);
6496 rc
= ap
->ops
->scr_read(ap
, reg
, &val
);
6502 return sata_pmp_scr_write(link
, reg
, val
);
6506 * ata_link_online - test whether the given link is online
6507 * @link: ATA link to test
6509 * Test whether @link is online. Note that this function returns
6510 * 0 if online status of @link cannot be obtained, so
6511 * ata_link_online(link) != !ata_link_offline(link).
6517 * 1 if the port online status is available and online.
6519 int ata_link_online(struct ata_link
*link
)
6523 if (sata_scr_read(link
, SCR_STATUS
, &sstatus
) == 0 &&
6524 (sstatus
& 0xf) == 0x3)
6530 * ata_link_offline - test whether the given link is offline
6531 * @link: ATA link to test
6533 * Test whether @link is offline. Note that this function
6534 * returns 0 if offline status of @link cannot be obtained, so
6535 * ata_link_online(link) != !ata_link_offline(link).
6541 * 1 if the port offline status is available and offline.
6543 int ata_link_offline(struct ata_link
*link
)
6547 if (sata_scr_read(link
, SCR_STATUS
, &sstatus
) == 0 &&
6548 (sstatus
& 0xf) != 0x3)
6553 int ata_flush_cache(struct ata_device
*dev
)
6555 unsigned int err_mask
;
6558 if (!ata_try_flush_cache(dev
))
6561 if (dev
->flags
& ATA_DFLAG_FLUSH_EXT
)
6562 cmd
= ATA_CMD_FLUSH_EXT
;
6564 cmd
= ATA_CMD_FLUSH
;
6566 /* This is wrong. On a failed flush we get back the LBA of the lost
6567 sector and we should (assuming it wasn't aborted as unknown) issue
6568 a further flush command to continue the writeback until it
6570 err_mask
= ata_do_simple_cmd(dev
, cmd
);
6572 ata_dev_printk(dev
, KERN_ERR
, "failed to flush cache\n");
6580 static int ata_host_request_pm(struct ata_host
*host
, pm_message_t mesg
,
6581 unsigned int action
, unsigned int ehi_flags
,
6584 unsigned long flags
;
6587 for (i
= 0; i
< host
->n_ports
; i
++) {
6588 struct ata_port
*ap
= host
->ports
[i
];
6589 struct ata_link
*link
;
6591 /* Previous resume operation might still be in
6592 * progress. Wait for PM_PENDING to clear.
6594 if (ap
->pflags
& ATA_PFLAG_PM_PENDING
) {
6595 ata_port_wait_eh(ap
);
6596 WARN_ON(ap
->pflags
& ATA_PFLAG_PM_PENDING
);
6599 /* request PM ops to EH */
6600 spin_lock_irqsave(ap
->lock
, flags
);
6605 ap
->pm_result
= &rc
;
6608 ap
->pflags
|= ATA_PFLAG_PM_PENDING
;
6609 __ata_port_for_each_link(link
, ap
) {
6610 link
->eh_info
.action
|= action
;
6611 link
->eh_info
.flags
|= ehi_flags
;
6614 ata_port_schedule_eh(ap
);
6616 spin_unlock_irqrestore(ap
->lock
, flags
);
6618 /* wait and check result */
6620 ata_port_wait_eh(ap
);
6621 WARN_ON(ap
->pflags
& ATA_PFLAG_PM_PENDING
);
6631 * ata_host_suspend - suspend host
6632 * @host: host to suspend
6635 * Suspend @host. Actual operation is performed by EH. This
6636 * function requests EH to perform PM operations and waits for EH
6640 * Kernel thread context (may sleep).
6643 * 0 on success, -errno on failure.
6645 int ata_host_suspend(struct ata_host
*host
, pm_message_t mesg
)
6650 * disable link pm on all ports before requesting
6653 ata_lpm_enable(host
);
6655 rc
= ata_host_request_pm(host
, mesg
, 0, ATA_EHI_QUIET
, 1);
6657 host
->dev
->power
.power_state
= mesg
;
6662 * ata_host_resume - resume host
6663 * @host: host to resume
6665 * Resume @host. Actual operation is performed by EH. This
6666 * function requests EH to perform PM operations and returns.
6667 * Note that all resume operations are performed parallely.
6670 * Kernel thread context (may sleep).
6672 void ata_host_resume(struct ata_host
*host
)
6674 ata_host_request_pm(host
, PMSG_ON
, ATA_EH_RESET
,
6675 ATA_EHI_NO_AUTOPSY
| ATA_EHI_QUIET
, 0);
6676 host
->dev
->power
.power_state
= PMSG_ON
;
6678 /* reenable link pm */
6679 ata_lpm_disable(host
);
6684 * ata_port_start - Set port up for dma.
6685 * @ap: Port to initialize
6687 * Called just after data structures for each port are
6688 * initialized. Allocates space for PRD table.
6690 * May be used as the port_start() entry in ata_port_operations.
6693 * Inherited from caller.
6695 int ata_port_start(struct ata_port
*ap
)
6697 struct device
*dev
= ap
->dev
;
6699 ap
->prd
= dmam_alloc_coherent(dev
, ATA_PRD_TBL_SZ
, &ap
->prd_dma
,
6708 * ata_dev_init - Initialize an ata_device structure
6709 * @dev: Device structure to initialize
6711 * Initialize @dev in preparation for probing.
6714 * Inherited from caller.
6716 void ata_dev_init(struct ata_device
*dev
)
6718 struct ata_link
*link
= dev
->link
;
6719 struct ata_port
*ap
= link
->ap
;
6720 unsigned long flags
;
6722 /* SATA spd limit is bound to the first device */
6723 link
->sata_spd_limit
= link
->hw_sata_spd_limit
;
6726 /* High bits of dev->flags are used to record warm plug
6727 * requests which occur asynchronously. Synchronize using
6730 spin_lock_irqsave(ap
->lock
, flags
);
6731 dev
->flags
&= ~ATA_DFLAG_INIT_MASK
;
6733 spin_unlock_irqrestore(ap
->lock
, flags
);
6735 memset((void *)dev
+ ATA_DEVICE_CLEAR_OFFSET
, 0,
6736 sizeof(*dev
) - ATA_DEVICE_CLEAR_OFFSET
);
6737 dev
->pio_mask
= UINT_MAX
;
6738 dev
->mwdma_mask
= UINT_MAX
;
6739 dev
->udma_mask
= UINT_MAX
;
6743 * ata_link_init - Initialize an ata_link structure
6744 * @ap: ATA port link is attached to
6745 * @link: Link structure to initialize
6746 * @pmp: Port multiplier port number
6751 * Kernel thread context (may sleep)
6753 void ata_link_init(struct ata_port
*ap
, struct ata_link
*link
, int pmp
)
6757 /* clear everything except for devices */
6758 memset(link
, 0, offsetof(struct ata_link
, device
[0]));
6762 link
->active_tag
= ATA_TAG_POISON
;
6763 link
->hw_sata_spd_limit
= UINT_MAX
;
6765 /* can't use iterator, ap isn't initialized yet */
6766 for (i
= 0; i
< ATA_MAX_DEVICES
; i
++) {
6767 struct ata_device
*dev
= &link
->device
[i
];
6770 dev
->devno
= dev
- link
->device
;
6776 * sata_link_init_spd - Initialize link->sata_spd_limit
6777 * @link: Link to configure sata_spd_limit for
6779 * Initialize @link->[hw_]sata_spd_limit to the currently
6783 * Kernel thread context (may sleep).
6786 * 0 on success, -errno on failure.
6788 int sata_link_init_spd(struct ata_link
*link
)
6794 rc
= sata_scr_read(link
, SCR_CONTROL
, &scontrol
);
6798 spd
= (scontrol
>> 4) & 0xf;
6800 link
->hw_sata_spd_limit
&= (1 << spd
) - 1;
6802 ata_force_spd_limit(link
);
6804 link
->sata_spd_limit
= link
->hw_sata_spd_limit
;
6810 * ata_port_alloc - allocate and initialize basic ATA port resources
6811 * @host: ATA host this allocated port belongs to
6813 * Allocate and initialize basic ATA port resources.
6816 * Allocate ATA port on success, NULL on failure.
6819 * Inherited from calling layer (may sleep).
6821 struct ata_port
*ata_port_alloc(struct ata_host
*host
)
6823 struct ata_port
*ap
;
6827 ap
= kzalloc(sizeof(*ap
), GFP_KERNEL
);
6831 ap
->pflags
|= ATA_PFLAG_INITIALIZING
;
6832 ap
->lock
= &host
->lock
;
6833 ap
->flags
= ATA_FLAG_DISABLED
;
6835 ap
->ctl
= ATA_DEVCTL_OBS
;
6837 ap
->dev
= host
->dev
;
6838 ap
->last_ctl
= 0xFF;
6840 #if defined(ATA_VERBOSE_DEBUG)
6841 /* turn on all debugging levels */
6842 ap
->msg_enable
= 0x00FF;
6843 #elif defined(ATA_DEBUG)
6844 ap
->msg_enable
= ATA_MSG_DRV
| ATA_MSG_INFO
| ATA_MSG_CTL
| ATA_MSG_WARN
| ATA_MSG_ERR
;
6846 ap
->msg_enable
= ATA_MSG_DRV
| ATA_MSG_ERR
| ATA_MSG_WARN
;
6849 INIT_DELAYED_WORK(&ap
->port_task
, ata_pio_task
);
6850 INIT_DELAYED_WORK(&ap
->hotplug_task
, ata_scsi_hotplug
);
6851 INIT_WORK(&ap
->scsi_rescan_task
, ata_scsi_dev_rescan
);
6852 INIT_LIST_HEAD(&ap
->eh_done_q
);
6853 init_waitqueue_head(&ap
->eh_wait_q
);
6854 init_timer_deferrable(&ap
->fastdrain_timer
);
6855 ap
->fastdrain_timer
.function
= ata_eh_fastdrain_timerfn
;
6856 ap
->fastdrain_timer
.data
= (unsigned long)ap
;
6858 ap
->cbl
= ATA_CBL_NONE
;
6860 ata_link_init(ap
, &ap
->link
, 0);
6863 ap
->stats
.unhandled_irq
= 1;
6864 ap
->stats
.idle_irq
= 1;
6869 static void ata_host_release(struct device
*gendev
, void *res
)
6871 struct ata_host
*host
= dev_get_drvdata(gendev
);
6874 for (i
= 0; i
< host
->n_ports
; i
++) {
6875 struct ata_port
*ap
= host
->ports
[i
];
6881 scsi_host_put(ap
->scsi_host
);
6883 kfree(ap
->pmp_link
);
6885 host
->ports
[i
] = NULL
;
6888 dev_set_drvdata(gendev
, NULL
);
6892 * ata_host_alloc - allocate and init basic ATA host resources
6893 * @dev: generic device this host is associated with
6894 * @max_ports: maximum number of ATA ports associated with this host
6896 * Allocate and initialize basic ATA host resources. LLD calls
6897 * this function to allocate a host, initializes it fully and
6898 * attaches it using ata_host_register().
6900 * @max_ports ports are allocated and host->n_ports is
6901 * initialized to @max_ports. The caller is allowed to decrease
6902 * host->n_ports before calling ata_host_register(). The unused
6903 * ports will be automatically freed on registration.
6906 * Allocate ATA host on success, NULL on failure.
6909 * Inherited from calling layer (may sleep).
6911 struct ata_host
*ata_host_alloc(struct device
*dev
, int max_ports
)
6913 struct ata_host
*host
;
6919 if (!devres_open_group(dev
, NULL
, GFP_KERNEL
))
6922 /* alloc a container for our list of ATA ports (buses) */
6923 sz
= sizeof(struct ata_host
) + (max_ports
+ 1) * sizeof(void *);
6924 /* alloc a container for our list of ATA ports (buses) */
6925 host
= devres_alloc(ata_host_release
, sz
, GFP_KERNEL
);
6929 devres_add(dev
, host
);
6930 dev_set_drvdata(dev
, host
);
6932 spin_lock_init(&host
->lock
);
6934 host
->n_ports
= max_ports
;
6936 /* allocate ports bound to this host */
6937 for (i
= 0; i
< max_ports
; i
++) {
6938 struct ata_port
*ap
;
6940 ap
= ata_port_alloc(host
);
6945 host
->ports
[i
] = ap
;
6948 devres_remove_group(dev
, NULL
);
6952 devres_release_group(dev
, NULL
);
6957 * ata_host_alloc_pinfo - alloc host and init with port_info array
6958 * @dev: generic device this host is associated with
6959 * @ppi: array of ATA port_info to initialize host with
6960 * @n_ports: number of ATA ports attached to this host
6962 * Allocate ATA host and initialize with info from @ppi. If NULL
6963 * terminated, @ppi may contain fewer entries than @n_ports. The
6964 * last entry will be used for the remaining ports.
6967 * Allocate ATA host on success, NULL on failure.
6970 * Inherited from calling layer (may sleep).
6972 struct ata_host
*ata_host_alloc_pinfo(struct device
*dev
,
6973 const struct ata_port_info
* const * ppi
,
6976 const struct ata_port_info
*pi
;
6977 struct ata_host
*host
;
6980 host
= ata_host_alloc(dev
, n_ports
);
6984 for (i
= 0, j
= 0, pi
= NULL
; i
< host
->n_ports
; i
++) {
6985 struct ata_port
*ap
= host
->ports
[i
];
6990 ap
->pio_mask
= pi
->pio_mask
;
6991 ap
->mwdma_mask
= pi
->mwdma_mask
;
6992 ap
->udma_mask
= pi
->udma_mask
;
6993 ap
->flags
|= pi
->flags
;
6994 ap
->link
.flags
|= pi
->link_flags
;
6995 ap
->ops
= pi
->port_ops
;
6997 if (!host
->ops
&& (pi
->port_ops
!= &ata_dummy_port_ops
))
6998 host
->ops
= pi
->port_ops
;
6999 if (!host
->private_data
&& pi
->private_data
)
7000 host
->private_data
= pi
->private_data
;
7006 static void ata_host_stop(struct device
*gendev
, void *res
)
7008 struct ata_host
*host
= dev_get_drvdata(gendev
);
7011 WARN_ON(!(host
->flags
& ATA_HOST_STARTED
));
7013 for (i
= 0; i
< host
->n_ports
; i
++) {
7014 struct ata_port
*ap
= host
->ports
[i
];
7016 if (ap
->ops
->port_stop
)
7017 ap
->ops
->port_stop(ap
);
7020 if (host
->ops
->host_stop
)
7021 host
->ops
->host_stop(host
);
7025 * ata_finalize_port_ops - finalize ata_port_operations
7026 * @ops: ata_port_operations to finalize
7028 * An ata_port_operations can inherit from another ops and that
7029 * ops can again inherit from another. This can go on as many
7030 * times as necessary as long as there is no loop in the
7031 * inheritance chain.
7033 * Ops tables are finalized when the host is started. NULL or
7034 * unspecified entries are inherited from the closet ancestor
7035 * which has the method and the entry is populated with it.
7036 * After finalization, the ops table directly points to all the
7037 * methods and ->inherits is no longer necessary and cleared.
7039 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
7044 static void ata_finalize_port_ops(struct ata_port_operations
*ops
)
7046 static spinlock_t lock
= SPIN_LOCK_UNLOCKED
;
7047 const struct ata_port_operations
*cur
;
7048 void **begin
= (void **)ops
;
7049 void **end
= (void **)&ops
->inherits
;
7052 if (!ops
|| !ops
->inherits
)
7057 for (cur
= ops
->inherits
; cur
; cur
= cur
->inherits
) {
7058 void **inherit
= (void **)cur
;
7060 for (pp
= begin
; pp
< end
; pp
++, inherit
++)
7065 for (pp
= begin
; pp
< end
; pp
++)
7069 ops
->inherits
= NULL
;
7075 * ata_host_start - start and freeze ports of an ATA host
7076 * @host: ATA host to start ports for
7078 * Start and then freeze ports of @host. Started status is
7079 * recorded in host->flags, so this function can be called
7080 * multiple times. Ports are guaranteed to get started only
7081 * once. If host->ops isn't initialized yet, its set to the
7082 * first non-dummy port ops.
7085 * Inherited from calling layer (may sleep).
7088 * 0 if all ports are started successfully, -errno otherwise.
7090 int ata_host_start(struct ata_host
*host
)
7093 void *start_dr
= NULL
;
7096 if (host
->flags
& ATA_HOST_STARTED
)
7099 ata_finalize_port_ops(host
->ops
);
7101 for (i
= 0; i
< host
->n_ports
; i
++) {
7102 struct ata_port
*ap
= host
->ports
[i
];
7104 ata_finalize_port_ops(ap
->ops
);
7106 if (!host
->ops
&& !ata_port_is_dummy(ap
))
7107 host
->ops
= ap
->ops
;
7109 if (ap
->ops
->port_stop
)
7113 if (host
->ops
->host_stop
)
7117 start_dr
= devres_alloc(ata_host_stop
, 0, GFP_KERNEL
);
7122 for (i
= 0; i
< host
->n_ports
; i
++) {
7123 struct ata_port
*ap
= host
->ports
[i
];
7125 if (ap
->ops
->port_start
) {
7126 rc
= ap
->ops
->port_start(ap
);
7129 dev_printk(KERN_ERR
, host
->dev
,
7130 "failed to start port %d "
7131 "(errno=%d)\n", i
, rc
);
7135 ata_eh_freeze_port(ap
);
7139 devres_add(host
->dev
, start_dr
);
7140 host
->flags
|= ATA_HOST_STARTED
;
7145 struct ata_port
*ap
= host
->ports
[i
];
7147 if (ap
->ops
->port_stop
)
7148 ap
->ops
->port_stop(ap
);
7150 devres_free(start_dr
);
7155 * ata_sas_host_init - Initialize a host struct
7156 * @host: host to initialize
7157 * @dev: device host is attached to
7158 * @flags: host flags
7162 * PCI/etc. bus probe sem.
7165 /* KILLME - the only user left is ipr */
7166 void ata_host_init(struct ata_host
*host
, struct device
*dev
,
7167 unsigned long flags
, struct ata_port_operations
*ops
)
7169 spin_lock_init(&host
->lock
);
7171 host
->flags
= flags
;
7176 * ata_host_register - register initialized ATA host
7177 * @host: ATA host to register
7178 * @sht: template for SCSI host
7180 * Register initialized ATA host. @host is allocated using
7181 * ata_host_alloc() and fully initialized by LLD. This function
7182 * starts ports, registers @host with ATA and SCSI layers and
7183 * probe registered devices.
7186 * Inherited from calling layer (may sleep).
7189 * 0 on success, -errno otherwise.
7191 int ata_host_register(struct ata_host
*host
, struct scsi_host_template
*sht
)
7195 /* host must have been started */
7196 if (!(host
->flags
& ATA_HOST_STARTED
)) {
7197 dev_printk(KERN_ERR
, host
->dev
,
7198 "BUG: trying to register unstarted host\n");
7203 /* Blow away unused ports. This happens when LLD can't
7204 * determine the exact number of ports to allocate at
7207 for (i
= host
->n_ports
; host
->ports
[i
]; i
++)
7208 kfree(host
->ports
[i
]);
7210 /* give ports names and add SCSI hosts */
7211 for (i
= 0; i
< host
->n_ports
; i
++)
7212 host
->ports
[i
]->print_id
= ata_print_id
++;
7214 rc
= ata_scsi_add_hosts(host
, sht
);
7218 /* associate with ACPI nodes */
7219 ata_acpi_associate(host
);
7221 /* set cable, sata_spd_limit and report */
7222 for (i
= 0; i
< host
->n_ports
; i
++) {
7223 struct ata_port
*ap
= host
->ports
[i
];
7224 unsigned long xfer_mask
;
7226 /* set SATA cable type if still unset */
7227 if (ap
->cbl
== ATA_CBL_NONE
&& (ap
->flags
& ATA_FLAG_SATA
))
7228 ap
->cbl
= ATA_CBL_SATA
;
7230 /* init sata_spd_limit to the current value */
7231 sata_link_init_spd(&ap
->link
);
7233 /* print per-port info to dmesg */
7234 xfer_mask
= ata_pack_xfermask(ap
->pio_mask
, ap
->mwdma_mask
,
7237 if (!ata_port_is_dummy(ap
)) {
7238 ata_port_printk(ap
, KERN_INFO
,
7239 "%cATA max %s %s\n",
7240 (ap
->flags
& ATA_FLAG_SATA
) ? 'S' : 'P',
7241 ata_mode_string(xfer_mask
),
7242 ap
->link
.eh_info
.desc
);
7243 ata_ehi_clear_desc(&ap
->link
.eh_info
);
7245 ata_port_printk(ap
, KERN_INFO
, "DUMMY\n");
7248 /* perform each probe synchronously */
7249 DPRINTK("probe begin\n");
7250 for (i
= 0; i
< host
->n_ports
; i
++) {
7251 struct ata_port
*ap
= host
->ports
[i
];
7254 if (ap
->ops
->error_handler
) {
7255 struct ata_eh_info
*ehi
= &ap
->link
.eh_info
;
7256 unsigned long flags
;
7260 /* kick EH for boot probing */
7261 spin_lock_irqsave(ap
->lock
, flags
);
7263 ehi
->probe_mask
|= ATA_ALL_DEVICES
;
7264 ehi
->action
|= ATA_EH_RESET
;
7265 ehi
->flags
|= ATA_EHI_NO_AUTOPSY
| ATA_EHI_QUIET
;
7267 ap
->pflags
&= ~ATA_PFLAG_INITIALIZING
;
7268 ap
->pflags
|= ATA_PFLAG_LOADING
;
7269 ata_port_schedule_eh(ap
);
7271 spin_unlock_irqrestore(ap
->lock
, flags
);
7273 /* wait for EH to finish */
7274 ata_port_wait_eh(ap
);
7276 DPRINTK("ata%u: bus probe begin\n", ap
->print_id
);
7277 rc
= ata_bus_probe(ap
);
7278 DPRINTK("ata%u: bus probe end\n", ap
->print_id
);
7281 /* FIXME: do something useful here?
7282 * Current libata behavior will
7283 * tear down everything when
7284 * the module is removed
7285 * or the h/w is unplugged.
7291 /* probes are done, now scan each port's disk(s) */
7292 DPRINTK("host probe begin\n");
7293 for (i
= 0; i
< host
->n_ports
; i
++) {
7294 struct ata_port
*ap
= host
->ports
[i
];
7296 ata_scsi_scan_host(ap
, 1);
7297 ata_lpm_schedule(ap
, ap
->pm_policy
);
7304 * ata_host_activate - start host, request IRQ and register it
7305 * @host: target ATA host
7306 * @irq: IRQ to request
7307 * @irq_handler: irq_handler used when requesting IRQ
7308 * @irq_flags: irq_flags used when requesting IRQ
7309 * @sht: scsi_host_template to use when registering the host
7311 * After allocating an ATA host and initializing it, most libata
7312 * LLDs perform three steps to activate the host - start host,
7313 * request IRQ and register it. This helper takes necessasry
7314 * arguments and performs the three steps in one go.
7316 * An invalid IRQ skips the IRQ registration and expects the host to
7317 * have set polling mode on the port. In this case, @irq_handler
7321 * Inherited from calling layer (may sleep).
7324 * 0 on success, -errno otherwise.
7326 int ata_host_activate(struct ata_host
*host
, int irq
,
7327 irq_handler_t irq_handler
, unsigned long irq_flags
,
7328 struct scsi_host_template
*sht
)
7332 rc
= ata_host_start(host
);
7336 /* Special case for polling mode */
7338 WARN_ON(irq_handler
);
7339 return ata_host_register(host
, sht
);
7342 rc
= devm_request_irq(host
->dev
, irq
, irq_handler
, irq_flags
,
7343 dev_driver_string(host
->dev
), host
);
7347 for (i
= 0; i
< host
->n_ports
; i
++)
7348 ata_port_desc(host
->ports
[i
], "irq %d", irq
);
7350 rc
= ata_host_register(host
, sht
);
7351 /* if failed, just free the IRQ and leave ports alone */
7353 devm_free_irq(host
->dev
, irq
, host
);
7359 * ata_port_detach - Detach ATA port in prepration of device removal
7360 * @ap: ATA port to be detached
7362 * Detach all ATA devices and the associated SCSI devices of @ap;
7363 * then, remove the associated SCSI host. @ap is guaranteed to
7364 * be quiescent on return from this function.
7367 * Kernel thread context (may sleep).
7369 static void ata_port_detach(struct ata_port
*ap
)
7371 unsigned long flags
;
7372 struct ata_link
*link
;
7373 struct ata_device
*dev
;
7375 if (!ap
->ops
->error_handler
)
7378 /* tell EH we're leaving & flush EH */
7379 spin_lock_irqsave(ap
->lock
, flags
);
7380 ap
->pflags
|= ATA_PFLAG_UNLOADING
;
7381 spin_unlock_irqrestore(ap
->lock
, flags
);
7383 ata_port_wait_eh(ap
);
7385 /* EH is now guaranteed to see UNLOADING - EH context belongs
7386 * to us. Disable all existing devices.
7388 ata_port_for_each_link(link
, ap
) {
7389 ata_link_for_each_dev(dev
, link
)
7390 ata_dev_disable(dev
);
7393 /* Final freeze & EH. All in-flight commands are aborted. EH
7394 * will be skipped and retrials will be terminated with bad
7397 spin_lock_irqsave(ap
->lock
, flags
);
7398 ata_port_freeze(ap
); /* won't be thawed */
7399 spin_unlock_irqrestore(ap
->lock
, flags
);
7401 ata_port_wait_eh(ap
);
7402 cancel_rearming_delayed_work(&ap
->hotplug_task
);
7405 /* remove the associated SCSI host */
7406 scsi_remove_host(ap
->scsi_host
);
7410 * ata_host_detach - Detach all ports of an ATA host
7411 * @host: Host to detach
7413 * Detach all ports of @host.
7416 * Kernel thread context (may sleep).
7418 void ata_host_detach(struct ata_host
*host
)
7422 for (i
= 0; i
< host
->n_ports
; i
++)
7423 ata_port_detach(host
->ports
[i
]);
7425 /* the host is dead now, dissociate ACPI */
7426 ata_acpi_dissociate(host
);
7430 * ata_std_ports - initialize ioaddr with standard port offsets.
7431 * @ioaddr: IO address structure to be initialized
7433 * Utility function which initializes data_addr, error_addr,
7434 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
7435 * device_addr, status_addr, and command_addr to standard offsets
7436 * relative to cmd_addr.
7438 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
7441 void ata_std_ports(struct ata_ioports
*ioaddr
)
7443 ioaddr
->data_addr
= ioaddr
->cmd_addr
+ ATA_REG_DATA
;
7444 ioaddr
->error_addr
= ioaddr
->cmd_addr
+ ATA_REG_ERR
;
7445 ioaddr
->feature_addr
= ioaddr
->cmd_addr
+ ATA_REG_FEATURE
;
7446 ioaddr
->nsect_addr
= ioaddr
->cmd_addr
+ ATA_REG_NSECT
;
7447 ioaddr
->lbal_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAL
;
7448 ioaddr
->lbam_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAM
;
7449 ioaddr
->lbah_addr
= ioaddr
->cmd_addr
+ ATA_REG_LBAH
;
7450 ioaddr
->device_addr
= ioaddr
->cmd_addr
+ ATA_REG_DEVICE
;
7451 ioaddr
->status_addr
= ioaddr
->cmd_addr
+ ATA_REG_STATUS
;
7452 ioaddr
->command_addr
= ioaddr
->cmd_addr
+ ATA_REG_CMD
;
7459 * ata_pci_remove_one - PCI layer callback for device removal
7460 * @pdev: PCI device that was removed
7462 * PCI layer indicates to libata via this hook that hot-unplug or
7463 * module unload event has occurred. Detach all ports. Resource
7464 * release is handled via devres.
7467 * Inherited from PCI layer (may sleep).
7469 void ata_pci_remove_one(struct pci_dev
*pdev
)
7471 struct device
*dev
= &pdev
->dev
;
7472 struct ata_host
*host
= dev_get_drvdata(dev
);
7474 ata_host_detach(host
);
7477 /* move to PCI subsystem */
7478 int pci_test_config_bits(struct pci_dev
*pdev
, const struct pci_bits
*bits
)
7480 unsigned long tmp
= 0;
7482 switch (bits
->width
) {
7485 pci_read_config_byte(pdev
, bits
->reg
, &tmp8
);
7491 pci_read_config_word(pdev
, bits
->reg
, &tmp16
);
7497 pci_read_config_dword(pdev
, bits
->reg
, &tmp32
);
7508 return (tmp
== bits
->val
) ? 1 : 0;
7512 void ata_pci_device_do_suspend(struct pci_dev
*pdev
, pm_message_t mesg
)
7514 pci_save_state(pdev
);
7515 pci_disable_device(pdev
);
7517 if (mesg
.event
& PM_EVENT_SLEEP
)
7518 pci_set_power_state(pdev
, PCI_D3hot
);
7521 int ata_pci_device_do_resume(struct pci_dev
*pdev
)
7525 pci_set_power_state(pdev
, PCI_D0
);
7526 pci_restore_state(pdev
);
7528 rc
= pcim_enable_device(pdev
);
7530 dev_printk(KERN_ERR
, &pdev
->dev
,
7531 "failed to enable device after resume (%d)\n", rc
);
7535 pci_set_master(pdev
);
7539 int ata_pci_device_suspend(struct pci_dev
*pdev
, pm_message_t mesg
)
7541 struct ata_host
*host
= dev_get_drvdata(&pdev
->dev
);
7544 rc
= ata_host_suspend(host
, mesg
);
7548 ata_pci_device_do_suspend(pdev
, mesg
);
7553 int ata_pci_device_resume(struct pci_dev
*pdev
)
7555 struct ata_host
*host
= dev_get_drvdata(&pdev
->dev
);
7558 rc
= ata_pci_device_do_resume(pdev
);
7560 ata_host_resume(host
);
7563 #endif /* CONFIG_PM */
7565 #endif /* CONFIG_PCI */
7567 static int __init
ata_parse_force_one(char **cur
,
7568 struct ata_force_ent
*force_ent
,
7569 const char **reason
)
7571 /* FIXME: Currently, there's no way to tag init const data and
7572 * using __initdata causes build failure on some versions of
7573 * gcc. Once __initdataconst is implemented, add const to the
7574 * following structure.
7576 static struct ata_force_param force_tbl
[] __initdata
= {
7577 { "40c", .cbl
= ATA_CBL_PATA40
},
7578 { "80c", .cbl
= ATA_CBL_PATA80
},
7579 { "short40c", .cbl
= ATA_CBL_PATA40_SHORT
},
7580 { "unk", .cbl
= ATA_CBL_PATA_UNK
},
7581 { "ign", .cbl
= ATA_CBL_PATA_IGN
},
7582 { "sata", .cbl
= ATA_CBL_SATA
},
7583 { "1.5Gbps", .spd_limit
= 1 },
7584 { "3.0Gbps", .spd_limit
= 2 },
7585 { "noncq", .horkage_on
= ATA_HORKAGE_NONCQ
},
7586 { "ncq", .horkage_off
= ATA_HORKAGE_NONCQ
},
7587 { "pio0", .xfer_mask
= 1 << (ATA_SHIFT_PIO
+ 0) },
7588 { "pio1", .xfer_mask
= 1 << (ATA_SHIFT_PIO
+ 1) },
7589 { "pio2", .xfer_mask
= 1 << (ATA_SHIFT_PIO
+ 2) },
7590 { "pio3", .xfer_mask
= 1 << (ATA_SHIFT_PIO
+ 3) },
7591 { "pio4", .xfer_mask
= 1 << (ATA_SHIFT_PIO
+ 4) },
7592 { "pio5", .xfer_mask
= 1 << (ATA_SHIFT_PIO
+ 5) },
7593 { "pio6", .xfer_mask
= 1 << (ATA_SHIFT_PIO
+ 6) },
7594 { "mwdma0", .xfer_mask
= 1 << (ATA_SHIFT_MWDMA
+ 0) },
7595 { "mwdma1", .xfer_mask
= 1 << (ATA_SHIFT_MWDMA
+ 1) },
7596 { "mwdma2", .xfer_mask
= 1 << (ATA_SHIFT_MWDMA
+ 2) },
7597 { "mwdma3", .xfer_mask
= 1 << (ATA_SHIFT_MWDMA
+ 3) },
7598 { "mwdma4", .xfer_mask
= 1 << (ATA_SHIFT_MWDMA
+ 4) },
7599 { "udma0", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 0) },
7600 { "udma16", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 0) },
7601 { "udma/16", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 0) },
7602 { "udma1", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 1) },
7603 { "udma25", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 1) },
7604 { "udma/25", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 1) },
7605 { "udma2", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 2) },
7606 { "udma33", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 2) },
7607 { "udma/33", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 2) },
7608 { "udma3", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 3) },
7609 { "udma44", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 3) },
7610 { "udma/44", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 3) },
7611 { "udma4", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 4) },
7612 { "udma66", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 4) },
7613 { "udma/66", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 4) },
7614 { "udma5", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 5) },
7615 { "udma100", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 5) },
7616 { "udma/100", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 5) },
7617 { "udma6", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 6) },
7618 { "udma133", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 6) },
7619 { "udma/133", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 6) },
7620 { "udma7", .xfer_mask
= 1 << (ATA_SHIFT_UDMA
+ 7) },
7622 char *start
= *cur
, *p
= *cur
;
7623 char *id
, *val
, *endp
;
7624 const struct ata_force_param
*match_fp
= NULL
;
7625 int nr_matches
= 0, i
;
7627 /* find where this param ends and update *cur */
7628 while (*p
!= '\0' && *p
!= ',')
7639 p
= strchr(start
, ':');
7641 val
= strstrip(start
);
7646 id
= strstrip(start
);
7647 val
= strstrip(p
+ 1);
7650 p
= strchr(id
, '.');
7653 force_ent
->device
= simple_strtoul(p
, &endp
, 10);
7654 if (p
== endp
|| *endp
!= '\0') {
7655 *reason
= "invalid device";
7660 force_ent
->port
= simple_strtoul(id
, &endp
, 10);
7661 if (p
== endp
|| *endp
!= '\0') {
7662 *reason
= "invalid port/link";
7667 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
7668 for (i
= 0; i
< ARRAY_SIZE(force_tbl
); i
++) {
7669 const struct ata_force_param
*fp
= &force_tbl
[i
];
7671 if (strncasecmp(val
, fp
->name
, strlen(val
)))
7677 if (strcasecmp(val
, fp
->name
) == 0) {
7684 *reason
= "unknown value";
7687 if (nr_matches
> 1) {
7688 *reason
= "ambigious value";
7692 force_ent
->param
= *match_fp
;
7697 static void __init
ata_parse_force_param(void)
7699 int idx
= 0, size
= 1;
7700 int last_port
= -1, last_device
= -1;
7701 char *p
, *cur
, *next
;
7703 /* calculate maximum number of params and allocate force_tbl */
7704 for (p
= ata_force_param_buf
; *p
; p
++)
7708 ata_force_tbl
= kzalloc(sizeof(ata_force_tbl
[0]) * size
, GFP_KERNEL
);
7709 if (!ata_force_tbl
) {
7710 printk(KERN_WARNING
"ata: failed to extend force table, "
7711 "libata.force ignored\n");
7715 /* parse and populate the table */
7716 for (cur
= ata_force_param_buf
; *cur
!= '\0'; cur
= next
) {
7717 const char *reason
= "";
7718 struct ata_force_ent te
= { .port
= -1, .device
= -1 };
7721 if (ata_parse_force_one(&next
, &te
, &reason
)) {
7722 printk(KERN_WARNING
"ata: failed to parse force "
7723 "parameter \"%s\" (%s)\n",
7728 if (te
.port
== -1) {
7729 te
.port
= last_port
;
7730 te
.device
= last_device
;
7733 ata_force_tbl
[idx
++] = te
;
7735 last_port
= te
.port
;
7736 last_device
= te
.device
;
7739 ata_force_tbl_size
= idx
;
7742 static int __init
ata_init(void)
7744 ata_probe_timeout
*= HZ
;
7746 ata_parse_force_param();
7748 ata_wq
= create_workqueue("ata");
7752 ata_aux_wq
= create_singlethread_workqueue("ata_aux");
7754 destroy_workqueue(ata_wq
);
7758 printk(KERN_DEBUG
"libata version " DRV_VERSION
" loaded.\n");
7762 static void __exit
ata_exit(void)
7764 kfree(ata_force_tbl
);
7765 destroy_workqueue(ata_wq
);
7766 destroy_workqueue(ata_aux_wq
);
7769 subsys_initcall(ata_init
);
7770 module_exit(ata_exit
);
7772 static unsigned long ratelimit_time
;
7773 static DEFINE_SPINLOCK(ata_ratelimit_lock
);
7775 int ata_ratelimit(void)
7778 unsigned long flags
;
7780 spin_lock_irqsave(&ata_ratelimit_lock
, flags
);
7782 if (time_after(jiffies
, ratelimit_time
)) {
7784 ratelimit_time
= jiffies
+ (HZ
/5);
7788 spin_unlock_irqrestore(&ata_ratelimit_lock
, flags
);
7794 * ata_wait_register - wait until register value changes
7795 * @reg: IO-mapped register
7796 * @mask: Mask to apply to read register value
7797 * @val: Wait condition
7798 * @interval_msec: polling interval in milliseconds
7799 * @timeout_msec: timeout in milliseconds
7801 * Waiting for some bits of register to change is a common
7802 * operation for ATA controllers. This function reads 32bit LE
7803 * IO-mapped register @reg and tests for the following condition.
7805 * (*@reg & mask) != val
7807 * If the condition is met, it returns; otherwise, the process is
7808 * repeated after @interval_msec until timeout.
7811 * Kernel thread context (may sleep)
7814 * The final register value.
7816 u32
ata_wait_register(void __iomem
*reg
, u32 mask
, u32 val
,
7817 unsigned long interval_msec
,
7818 unsigned long timeout_msec
)
7820 unsigned long timeout
;
7823 tmp
= ioread32(reg
);
7825 /* Calculate timeout _after_ the first read to make sure
7826 * preceding writes reach the controller before starting to
7827 * eat away the timeout.
7829 timeout
= jiffies
+ (timeout_msec
* HZ
) / 1000;
7831 while ((tmp
& mask
) == val
&& time_before(jiffies
, timeout
)) {
7832 msleep(interval_msec
);
7833 tmp
= ioread32(reg
);
7842 static void ata_dummy_noret(struct ata_port
*ap
) { }
7843 static int ata_dummy_ret0(struct ata_port
*ap
) { return 0; }
7844 static void ata_dummy_qc_noret(struct ata_queued_cmd
*qc
) { }
7846 static u8
ata_dummy_check_status(struct ata_port
*ap
)
7851 static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd
*qc
)
7853 return AC_ERR_SYSTEM
;
7856 struct ata_port_operations ata_dummy_port_ops
= {
7857 .check_status
= ata_dummy_check_status
,
7858 .check_altstatus
= ata_dummy_check_status
,
7859 .dev_select
= ata_noop_dev_select
,
7860 .qc_prep
= ata_noop_qc_prep
,
7861 .qc_issue
= ata_dummy_qc_issue
,
7862 .freeze
= ata_dummy_noret
,
7863 .thaw
= ata_dummy_noret
,
7864 .error_handler
= ata_dummy_noret
,
7865 .post_internal_cmd
= ata_dummy_qc_noret
,
7866 .irq_clear
= ata_dummy_noret
,
7867 .port_start
= ata_dummy_ret0
,
7868 .port_stop
= ata_dummy_noret
,
7871 const struct ata_port_info ata_dummy_port_info
= {
7872 .port_ops
= &ata_dummy_port_ops
,
7876 * libata is essentially a library of internal helper functions for
7877 * low-level ATA host controller drivers. As such, the API/ABI is
7878 * likely to change as new drivers are added and updated.
7879 * Do not depend on ABI/API stability.
7881 EXPORT_SYMBOL_GPL(sata_deb_timing_normal
);
7882 EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug
);
7883 EXPORT_SYMBOL_GPL(sata_deb_timing_long
);
7884 EXPORT_SYMBOL_GPL(ata_base_port_ops
);
7885 EXPORT_SYMBOL_GPL(sata_port_ops
);
7886 EXPORT_SYMBOL_GPL(sata_pmp_port_ops
);
7887 EXPORT_SYMBOL_GPL(ata_sff_port_ops
);
7888 EXPORT_SYMBOL_GPL(ata_bmdma_port_ops
);
7889 EXPORT_SYMBOL_GPL(ata_dummy_port_ops
);
7890 EXPORT_SYMBOL_GPL(ata_dummy_port_info
);
7891 EXPORT_SYMBOL_GPL(ata_std_bios_param
);
7892 EXPORT_SYMBOL_GPL(ata_std_ports
);
7893 EXPORT_SYMBOL_GPL(ata_host_init
);
7894 EXPORT_SYMBOL_GPL(ata_host_alloc
);
7895 EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo
);
7896 EXPORT_SYMBOL_GPL(ata_host_start
);
7897 EXPORT_SYMBOL_GPL(ata_host_register
);
7898 EXPORT_SYMBOL_GPL(ata_host_activate
);
7899 EXPORT_SYMBOL_GPL(ata_host_detach
);
7900 EXPORT_SYMBOL_GPL(ata_sg_init
);
7901 EXPORT_SYMBOL_GPL(ata_hsm_move
);
7902 EXPORT_SYMBOL_GPL(ata_qc_complete
);
7903 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple
);
7904 EXPORT_SYMBOL_GPL(ata_qc_issue_prot
);
7905 EXPORT_SYMBOL_GPL(ata_tf_load
);
7906 EXPORT_SYMBOL_GPL(ata_tf_read
);
7907 EXPORT_SYMBOL_GPL(ata_noop_dev_select
);
7908 EXPORT_SYMBOL_GPL(ata_std_dev_select
);
7909 EXPORT_SYMBOL_GPL(sata_print_link_status
);
7910 EXPORT_SYMBOL_GPL(atapi_cmd_type
);
7911 EXPORT_SYMBOL_GPL(ata_tf_to_fis
);
7912 EXPORT_SYMBOL_GPL(ata_tf_from_fis
);
7913 EXPORT_SYMBOL_GPL(ata_pack_xfermask
);
7914 EXPORT_SYMBOL_GPL(ata_unpack_xfermask
);
7915 EXPORT_SYMBOL_GPL(ata_xfer_mask2mode
);
7916 EXPORT_SYMBOL_GPL(ata_xfer_mode2mask
);
7917 EXPORT_SYMBOL_GPL(ata_xfer_mode2shift
);
7918 EXPORT_SYMBOL_GPL(ata_mode_string
);
7919 EXPORT_SYMBOL_GPL(ata_id_xfermask
);
7920 EXPORT_SYMBOL_GPL(ata_check_status
);
7921 EXPORT_SYMBOL_GPL(ata_altstatus
);
7922 EXPORT_SYMBOL_GPL(ata_exec_command
);
7923 EXPORT_SYMBOL_GPL(ata_port_start
);
7924 EXPORT_SYMBOL_GPL(ata_sff_port_start
);
7925 EXPORT_SYMBOL_GPL(ata_interrupt
);
7926 EXPORT_SYMBOL_GPL(ata_do_set_mode
);
7927 EXPORT_SYMBOL_GPL(ata_data_xfer
);
7928 EXPORT_SYMBOL_GPL(ata_data_xfer_noirq
);
7929 EXPORT_SYMBOL_GPL(ata_std_qc_defer
);
7930 EXPORT_SYMBOL_GPL(ata_qc_prep
);
7931 EXPORT_SYMBOL_GPL(ata_dumb_qc_prep
);
7932 EXPORT_SYMBOL_GPL(ata_noop_qc_prep
);
7933 EXPORT_SYMBOL_GPL(ata_bmdma_setup
);
7934 EXPORT_SYMBOL_GPL(ata_bmdma_start
);
7935 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear
);
7936 EXPORT_SYMBOL_GPL(ata_noop_irq_clear
);
7937 EXPORT_SYMBOL_GPL(ata_bmdma_status
);
7938 EXPORT_SYMBOL_GPL(ata_bmdma_stop
);
7939 EXPORT_SYMBOL_GPL(ata_bmdma_freeze
);
7940 EXPORT_SYMBOL_GPL(ata_bmdma_thaw
);
7941 EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh
);
7942 EXPORT_SYMBOL_GPL(ata_bmdma_error_handler
);
7943 EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd
);
7944 EXPORT_SYMBOL_GPL(ata_port_probe
);
7945 EXPORT_SYMBOL_GPL(ata_dev_disable
);
7946 EXPORT_SYMBOL_GPL(sata_set_spd
);
7947 EXPORT_SYMBOL_GPL(sata_link_debounce
);
7948 EXPORT_SYMBOL_GPL(sata_link_resume
);
7949 EXPORT_SYMBOL_GPL(ata_bus_reset
);
7950 EXPORT_SYMBOL_GPL(ata_std_prereset
);
7951 EXPORT_SYMBOL_GPL(ata_std_softreset
);
7952 EXPORT_SYMBOL_GPL(sata_link_hardreset
);
7953 EXPORT_SYMBOL_GPL(sata_std_hardreset
);
7954 EXPORT_SYMBOL_GPL(ata_std_postreset
);
7955 EXPORT_SYMBOL_GPL(ata_dev_classify
);
7956 EXPORT_SYMBOL_GPL(ata_dev_pair
);
7957 EXPORT_SYMBOL_GPL(ata_port_disable
);
7958 EXPORT_SYMBOL_GPL(ata_ratelimit
);
7959 EXPORT_SYMBOL_GPL(ata_wait_register
);
7960 EXPORT_SYMBOL_GPL(ata_busy_sleep
);
7961 EXPORT_SYMBOL_GPL(ata_wait_after_reset
);
7962 EXPORT_SYMBOL_GPL(ata_wait_ready
);
7963 EXPORT_SYMBOL_GPL(ata_scsi_ioctl
);
7964 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd
);
7965 EXPORT_SYMBOL_GPL(ata_scsi_slave_config
);
7966 EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy
);
7967 EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth
);
7968 EXPORT_SYMBOL_GPL(ata_host_intr
);
7969 EXPORT_SYMBOL_GPL(sata_scr_valid
);
7970 EXPORT_SYMBOL_GPL(sata_scr_read
);
7971 EXPORT_SYMBOL_GPL(sata_scr_write
);
7972 EXPORT_SYMBOL_GPL(sata_scr_write_flush
);
7973 EXPORT_SYMBOL_GPL(ata_link_online
);
7974 EXPORT_SYMBOL_GPL(ata_link_offline
);
7976 EXPORT_SYMBOL_GPL(ata_host_suspend
);
7977 EXPORT_SYMBOL_GPL(ata_host_resume
);
7978 #endif /* CONFIG_PM */
7979 EXPORT_SYMBOL_GPL(ata_id_string
);
7980 EXPORT_SYMBOL_GPL(ata_id_c_string
);
7981 EXPORT_SYMBOL_GPL(ata_scsi_simulate
);
7983 EXPORT_SYMBOL_GPL(ata_pio_need_iordy
);
7984 EXPORT_SYMBOL_GPL(ata_timing_find_mode
);
7985 EXPORT_SYMBOL_GPL(ata_timing_compute
);
7986 EXPORT_SYMBOL_GPL(ata_timing_merge
);
7987 EXPORT_SYMBOL_GPL(ata_timing_cycle2mode
);
7990 EXPORT_SYMBOL_GPL(pci_test_config_bits
);
7991 EXPORT_SYMBOL_GPL(ata_pci_init_sff_host
);
7992 EXPORT_SYMBOL_GPL(ata_pci_init_bmdma
);
7993 EXPORT_SYMBOL_GPL(ata_pci_prepare_sff_host
);
7994 EXPORT_SYMBOL_GPL(ata_pci_activate_sff_host
);
7995 EXPORT_SYMBOL_GPL(ata_pci_init_one
);
7996 EXPORT_SYMBOL_GPL(ata_pci_remove_one
);
7998 EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend
);
7999 EXPORT_SYMBOL_GPL(ata_pci_device_do_resume
);
8000 EXPORT_SYMBOL_GPL(ata_pci_device_suspend
);
8001 EXPORT_SYMBOL_GPL(ata_pci_device_resume
);
8002 #endif /* CONFIG_PM */
8003 EXPORT_SYMBOL_GPL(ata_pci_default_filter
);
8004 EXPORT_SYMBOL_GPL(ata_pci_clear_simplex
);
8005 #endif /* CONFIG_PCI */
8007 EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch
);
8008 EXPORT_SYMBOL_GPL(sata_pmp_std_prereset
);
8009 EXPORT_SYMBOL_GPL(sata_pmp_std_hardreset
);
8010 EXPORT_SYMBOL_GPL(sata_pmp_std_postreset
);
8011 EXPORT_SYMBOL_GPL(sata_pmp_do_eh
);
8013 EXPORT_SYMBOL_GPL(__ata_ehi_push_desc
);
8014 EXPORT_SYMBOL_GPL(ata_ehi_push_desc
);
8015 EXPORT_SYMBOL_GPL(ata_ehi_clear_desc
);
8016 EXPORT_SYMBOL_GPL(ata_port_desc
);
8018 EXPORT_SYMBOL_GPL(ata_port_pbar_desc
);
8019 #endif /* CONFIG_PCI */
8020 EXPORT_SYMBOL_GPL(ata_port_schedule_eh
);
8021 EXPORT_SYMBOL_GPL(ata_link_abort
);
8022 EXPORT_SYMBOL_GPL(ata_port_abort
);
8023 EXPORT_SYMBOL_GPL(ata_port_freeze
);
8024 EXPORT_SYMBOL_GPL(sata_async_notification
);
8025 EXPORT_SYMBOL_GPL(ata_eh_freeze_port
);
8026 EXPORT_SYMBOL_GPL(ata_eh_thaw_port
);
8027 EXPORT_SYMBOL_GPL(ata_eh_qc_complete
);
8028 EXPORT_SYMBOL_GPL(ata_eh_qc_retry
);
8029 EXPORT_SYMBOL_GPL(ata_do_eh
);
8030 EXPORT_SYMBOL_GPL(ata_irq_on
);
8031 EXPORT_SYMBOL_GPL(ata_dev_try_classify
);
8033 EXPORT_SYMBOL_GPL(ata_cable_40wire
);
8034 EXPORT_SYMBOL_GPL(ata_cable_80wire
);
8035 EXPORT_SYMBOL_GPL(ata_cable_unknown
);
8036 EXPORT_SYMBOL_GPL(ata_cable_ignore
);
8037 EXPORT_SYMBOL_GPL(ata_cable_sata
);