libata-pmp-prep: implement sata_async_notification()
[linux-2.6.git] / drivers / ata / libata-core.c
blob8b08e7bdd24d59727bf3d7e3a94266a27e22e0d5
1 /*
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
6 * on emails.
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)
15 * any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/pci.h>
38 #include <linux/init.h>
39 #include <linux/list.h>
40 #include <linux/mm.h>
41 #include <linux/highmem.h>
42 #include <linux/spinlock.h>
43 #include <linux/blkdev.h>
44 #include <linux/delay.h>
45 #include <linux/timer.h>
46 #include <linux/interrupt.h>
47 #include <linux/completion.h>
48 #include <linux/suspend.h>
49 #include <linux/workqueue.h>
50 #include <linux/jiffies.h>
51 #include <linux/scatterlist.h>
52 #include <scsi/scsi.h>
53 #include <scsi/scsi_cmnd.h>
54 #include <scsi/scsi_host.h>
55 #include <linux/libata.h>
56 #include <asm/io.h>
57 #include <asm/semaphore.h>
58 #include <asm/byteorder.h>
60 #include "libata.h"
63 /* debounce timing parameters in msecs { interval, duration, timeout } */
64 const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
65 const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
66 const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
68 static unsigned int ata_dev_init_params(struct ata_device *dev,
69 u16 heads, u16 sectors);
70 static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
71 static unsigned int ata_dev_set_AN(struct ata_device *dev, u8 enable);
72 static void ata_dev_xfermask(struct ata_device *dev);
73 static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
75 unsigned int ata_print_id = 1;
76 static struct workqueue_struct *ata_wq;
78 struct workqueue_struct *ata_aux_wq;
80 int atapi_enabled = 1;
81 module_param(atapi_enabled, int, 0444);
82 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
84 int atapi_dmadir = 0;
85 module_param(atapi_dmadir, int, 0444);
86 MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
88 int atapi_passthru16 = 1;
89 module_param(atapi_passthru16, int, 0444);
90 MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices; on by default (0=off, 1=on)");
92 int libata_fua = 0;
93 module_param_named(fua, libata_fua, int, 0444);
94 MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
96 static int ata_ignore_hpa = 0;
97 module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
98 MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
100 static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
101 module_param(ata_probe_timeout, int, 0444);
102 MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
104 int libata_noacpi = 1;
105 module_param_named(noacpi, libata_noacpi, int, 0444);
106 MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in suspend/resume when set");
108 MODULE_AUTHOR("Jeff Garzik");
109 MODULE_DESCRIPTION("Library module for ATA devices");
110 MODULE_LICENSE("GPL");
111 MODULE_VERSION(DRV_VERSION);
115 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
116 * @tf: Taskfile to convert
117 * @pmp: Port multiplier port
118 * @is_cmd: This FIS is for command
119 * @fis: Buffer into which data will output
121 * Converts a standard ATA taskfile to a Serial ATA
122 * FIS structure (Register - Host to Device).
124 * LOCKING:
125 * Inherited from caller.
127 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
129 fis[0] = 0x27; /* Register - Host to Device FIS */
130 fis[1] = pmp & 0xf; /* Port multiplier number*/
131 if (is_cmd)
132 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
134 fis[2] = tf->command;
135 fis[3] = tf->feature;
137 fis[4] = tf->lbal;
138 fis[5] = tf->lbam;
139 fis[6] = tf->lbah;
140 fis[7] = tf->device;
142 fis[8] = tf->hob_lbal;
143 fis[9] = tf->hob_lbam;
144 fis[10] = tf->hob_lbah;
145 fis[11] = tf->hob_feature;
147 fis[12] = tf->nsect;
148 fis[13] = tf->hob_nsect;
149 fis[14] = 0;
150 fis[15] = tf->ctl;
152 fis[16] = 0;
153 fis[17] = 0;
154 fis[18] = 0;
155 fis[19] = 0;
159 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
160 * @fis: Buffer from which data will be input
161 * @tf: Taskfile to output
163 * Converts a serial ATA FIS structure to a standard ATA taskfile.
165 * LOCKING:
166 * Inherited from caller.
169 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
171 tf->command = fis[2]; /* status */
172 tf->feature = fis[3]; /* error */
174 tf->lbal = fis[4];
175 tf->lbam = fis[5];
176 tf->lbah = fis[6];
177 tf->device = fis[7];
179 tf->hob_lbal = fis[8];
180 tf->hob_lbam = fis[9];
181 tf->hob_lbah = fis[10];
183 tf->nsect = fis[12];
184 tf->hob_nsect = fis[13];
187 static const u8 ata_rw_cmds[] = {
188 /* pio multi */
189 ATA_CMD_READ_MULTI,
190 ATA_CMD_WRITE_MULTI,
191 ATA_CMD_READ_MULTI_EXT,
192 ATA_CMD_WRITE_MULTI_EXT,
196 ATA_CMD_WRITE_MULTI_FUA_EXT,
197 /* pio */
198 ATA_CMD_PIO_READ,
199 ATA_CMD_PIO_WRITE,
200 ATA_CMD_PIO_READ_EXT,
201 ATA_CMD_PIO_WRITE_EXT,
206 /* dma */
207 ATA_CMD_READ,
208 ATA_CMD_WRITE,
209 ATA_CMD_READ_EXT,
210 ATA_CMD_WRITE_EXT,
214 ATA_CMD_WRITE_FUA_EXT
218 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
219 * @tf: command to examine and configure
220 * @dev: device tf belongs to
222 * Examine the device configuration and tf->flags to calculate
223 * the proper read/write commands and protocol to use.
225 * LOCKING:
226 * caller.
228 static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
230 u8 cmd;
232 int index, fua, lba48, write;
234 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
235 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
236 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
238 if (dev->flags & ATA_DFLAG_PIO) {
239 tf->protocol = ATA_PROT_PIO;
240 index = dev->multi_count ? 0 : 8;
241 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
242 /* Unable to use DMA due to host limitation */
243 tf->protocol = ATA_PROT_PIO;
244 index = dev->multi_count ? 0 : 8;
245 } else {
246 tf->protocol = ATA_PROT_DMA;
247 index = 16;
250 cmd = ata_rw_cmds[index + fua + lba48 + write];
251 if (cmd) {
252 tf->command = cmd;
253 return 0;
255 return -1;
259 * ata_tf_read_block - Read block address from ATA taskfile
260 * @tf: ATA taskfile of interest
261 * @dev: ATA device @tf belongs to
263 * LOCKING:
264 * None.
266 * Read block address from @tf. This function can handle all
267 * three address formats - LBA, LBA48 and CHS. tf->protocol and
268 * flags select the address format to use.
270 * RETURNS:
271 * Block address read from @tf.
273 u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
275 u64 block = 0;
277 if (tf->flags & ATA_TFLAG_LBA) {
278 if (tf->flags & ATA_TFLAG_LBA48) {
279 block |= (u64)tf->hob_lbah << 40;
280 block |= (u64)tf->hob_lbam << 32;
281 block |= tf->hob_lbal << 24;
282 } else
283 block |= (tf->device & 0xf) << 24;
285 block |= tf->lbah << 16;
286 block |= tf->lbam << 8;
287 block |= tf->lbal;
288 } else {
289 u32 cyl, head, sect;
291 cyl = tf->lbam | (tf->lbah << 8);
292 head = tf->device & 0xf;
293 sect = tf->lbal;
295 block = (cyl * dev->heads + head) * dev->sectors + sect;
298 return block;
302 * ata_build_rw_tf - Build ATA taskfile for given read/write request
303 * @tf: Target ATA taskfile
304 * @dev: ATA device @tf belongs to
305 * @block: Block address
306 * @n_block: Number of blocks
307 * @tf_flags: RW/FUA etc...
308 * @tag: tag
310 * LOCKING:
311 * None.
313 * Build ATA taskfile @tf for read/write request described by
314 * @block, @n_block, @tf_flags and @tag on @dev.
316 * RETURNS:
318 * 0 on success, -ERANGE if the request is too large for @dev,
319 * -EINVAL if the request is invalid.
321 int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
322 u64 block, u32 n_block, unsigned int tf_flags,
323 unsigned int tag)
325 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
326 tf->flags |= tf_flags;
328 if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
329 /* yay, NCQ */
330 if (!lba_48_ok(block, n_block))
331 return -ERANGE;
333 tf->protocol = ATA_PROT_NCQ;
334 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
336 if (tf->flags & ATA_TFLAG_WRITE)
337 tf->command = ATA_CMD_FPDMA_WRITE;
338 else
339 tf->command = ATA_CMD_FPDMA_READ;
341 tf->nsect = tag << 3;
342 tf->hob_feature = (n_block >> 8) & 0xff;
343 tf->feature = n_block & 0xff;
345 tf->hob_lbah = (block >> 40) & 0xff;
346 tf->hob_lbam = (block >> 32) & 0xff;
347 tf->hob_lbal = (block >> 24) & 0xff;
348 tf->lbah = (block >> 16) & 0xff;
349 tf->lbam = (block >> 8) & 0xff;
350 tf->lbal = block & 0xff;
352 tf->device = 1 << 6;
353 if (tf->flags & ATA_TFLAG_FUA)
354 tf->device |= 1 << 7;
355 } else if (dev->flags & ATA_DFLAG_LBA) {
356 tf->flags |= ATA_TFLAG_LBA;
358 if (lba_28_ok(block, n_block)) {
359 /* use LBA28 */
360 tf->device |= (block >> 24) & 0xf;
361 } else if (lba_48_ok(block, n_block)) {
362 if (!(dev->flags & ATA_DFLAG_LBA48))
363 return -ERANGE;
365 /* use LBA48 */
366 tf->flags |= ATA_TFLAG_LBA48;
368 tf->hob_nsect = (n_block >> 8) & 0xff;
370 tf->hob_lbah = (block >> 40) & 0xff;
371 tf->hob_lbam = (block >> 32) & 0xff;
372 tf->hob_lbal = (block >> 24) & 0xff;
373 } else
374 /* request too large even for LBA48 */
375 return -ERANGE;
377 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
378 return -EINVAL;
380 tf->nsect = n_block & 0xff;
382 tf->lbah = (block >> 16) & 0xff;
383 tf->lbam = (block >> 8) & 0xff;
384 tf->lbal = block & 0xff;
386 tf->device |= ATA_LBA;
387 } else {
388 /* CHS */
389 u32 sect, head, cyl, track;
391 /* The request -may- be too large for CHS addressing. */
392 if (!lba_28_ok(block, n_block))
393 return -ERANGE;
395 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
396 return -EINVAL;
398 /* Convert LBA to CHS */
399 track = (u32)block / dev->sectors;
400 cyl = track / dev->heads;
401 head = track % dev->heads;
402 sect = (u32)block % dev->sectors + 1;
404 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
405 (u32)block, track, cyl, head, sect);
407 /* Check whether the converted CHS can fit.
408 Cylinder: 0-65535
409 Head: 0-15
410 Sector: 1-255*/
411 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
412 return -ERANGE;
414 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
415 tf->lbal = sect;
416 tf->lbam = cyl;
417 tf->lbah = cyl >> 8;
418 tf->device |= head;
421 return 0;
425 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
426 * @pio_mask: pio_mask
427 * @mwdma_mask: mwdma_mask
428 * @udma_mask: udma_mask
430 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
431 * unsigned int xfer_mask.
433 * LOCKING:
434 * None.
436 * RETURNS:
437 * Packed xfer_mask.
439 static unsigned int ata_pack_xfermask(unsigned int pio_mask,
440 unsigned int mwdma_mask,
441 unsigned int udma_mask)
443 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
444 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
445 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
449 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
450 * @xfer_mask: xfer_mask to unpack
451 * @pio_mask: resulting pio_mask
452 * @mwdma_mask: resulting mwdma_mask
453 * @udma_mask: resulting udma_mask
455 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
456 * Any NULL distination masks will be ignored.
458 static void ata_unpack_xfermask(unsigned int xfer_mask,
459 unsigned int *pio_mask,
460 unsigned int *mwdma_mask,
461 unsigned int *udma_mask)
463 if (pio_mask)
464 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
465 if (mwdma_mask)
466 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
467 if (udma_mask)
468 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
471 static const struct ata_xfer_ent {
472 int shift, bits;
473 u8 base;
474 } ata_xfer_tbl[] = {
475 { ATA_SHIFT_PIO, ATA_BITS_PIO, XFER_PIO_0 },
476 { ATA_SHIFT_MWDMA, ATA_BITS_MWDMA, XFER_MW_DMA_0 },
477 { ATA_SHIFT_UDMA, ATA_BITS_UDMA, XFER_UDMA_0 },
478 { -1, },
482 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
483 * @xfer_mask: xfer_mask of interest
485 * Return matching XFER_* value for @xfer_mask. Only the highest
486 * bit of @xfer_mask is considered.
488 * LOCKING:
489 * None.
491 * RETURNS:
492 * Matching XFER_* value, 0 if no match found.
494 static u8 ata_xfer_mask2mode(unsigned int xfer_mask)
496 int highbit = fls(xfer_mask) - 1;
497 const struct ata_xfer_ent *ent;
499 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
500 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
501 return ent->base + highbit - ent->shift;
502 return 0;
506 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
507 * @xfer_mode: XFER_* of interest
509 * Return matching xfer_mask for @xfer_mode.
511 * LOCKING:
512 * None.
514 * RETURNS:
515 * Matching xfer_mask, 0 if no match found.
517 static unsigned int ata_xfer_mode2mask(u8 xfer_mode)
519 const struct ata_xfer_ent *ent;
521 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
522 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
523 return 1 << (ent->shift + xfer_mode - ent->base);
524 return 0;
528 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
529 * @xfer_mode: XFER_* of interest
531 * Return matching xfer_shift for @xfer_mode.
533 * LOCKING:
534 * None.
536 * RETURNS:
537 * Matching xfer_shift, -1 if no match found.
539 static int ata_xfer_mode2shift(unsigned int xfer_mode)
541 const struct ata_xfer_ent *ent;
543 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
544 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
545 return ent->shift;
546 return -1;
550 * ata_mode_string - convert xfer_mask to string
551 * @xfer_mask: mask of bits supported; only highest bit counts.
553 * Determine string which represents the highest speed
554 * (highest bit in @modemask).
556 * LOCKING:
557 * None.
559 * RETURNS:
560 * Constant C string representing highest speed listed in
561 * @mode_mask, or the constant C string "<n/a>".
563 static const char *ata_mode_string(unsigned int xfer_mask)
565 static const char * const xfer_mode_str[] = {
566 "PIO0",
567 "PIO1",
568 "PIO2",
569 "PIO3",
570 "PIO4",
571 "PIO5",
572 "PIO6",
573 "MWDMA0",
574 "MWDMA1",
575 "MWDMA2",
576 "MWDMA3",
577 "MWDMA4",
578 "UDMA/16",
579 "UDMA/25",
580 "UDMA/33",
581 "UDMA/44",
582 "UDMA/66",
583 "UDMA/100",
584 "UDMA/133",
585 "UDMA7",
587 int highbit;
589 highbit = fls(xfer_mask) - 1;
590 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
591 return xfer_mode_str[highbit];
592 return "<n/a>";
595 static const char *sata_spd_string(unsigned int spd)
597 static const char * const spd_str[] = {
598 "1.5 Gbps",
599 "3.0 Gbps",
602 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
603 return "<unknown>";
604 return spd_str[spd - 1];
607 void ata_dev_disable(struct ata_device *dev)
609 if (ata_dev_enabled(dev)) {
610 if (ata_msg_drv(dev->link->ap))
611 ata_dev_printk(dev, KERN_WARNING, "disabled\n");
612 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
613 ATA_DNXFER_QUIET);
614 dev->class++;
619 * ata_devchk - PATA device presence detection
620 * @ap: ATA channel to examine
621 * @device: Device to examine (starting at zero)
623 * This technique was originally described in
624 * Hale Landis's ATADRVR (www.ata-atapi.com), and
625 * later found its way into the ATA/ATAPI spec.
627 * Write a pattern to the ATA shadow registers,
628 * and if a device is present, it will respond by
629 * correctly storing and echoing back the
630 * ATA shadow register contents.
632 * LOCKING:
633 * caller.
636 static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
638 struct ata_ioports *ioaddr = &ap->ioaddr;
639 u8 nsect, lbal;
641 ap->ops->dev_select(ap, device);
643 iowrite8(0x55, ioaddr->nsect_addr);
644 iowrite8(0xaa, ioaddr->lbal_addr);
646 iowrite8(0xaa, ioaddr->nsect_addr);
647 iowrite8(0x55, ioaddr->lbal_addr);
649 iowrite8(0x55, ioaddr->nsect_addr);
650 iowrite8(0xaa, ioaddr->lbal_addr);
652 nsect = ioread8(ioaddr->nsect_addr);
653 lbal = ioread8(ioaddr->lbal_addr);
655 if ((nsect == 0x55) && (lbal == 0xaa))
656 return 1; /* we found a device */
658 return 0; /* nothing found */
662 * ata_dev_classify - determine device type based on ATA-spec signature
663 * @tf: ATA taskfile register set for device to be identified
665 * Determine from taskfile register contents whether a device is
666 * ATA or ATAPI, as per "Signature and persistence" section
667 * of ATA/PI spec (volume 1, sect 5.14).
669 * LOCKING:
670 * None.
672 * RETURNS:
673 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, or %ATA_DEV_UNKNOWN
674 * the event of failure.
677 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
679 /* Apple's open source Darwin code hints that some devices only
680 * put a proper signature into the LBA mid/high registers,
681 * So, we only check those. It's sufficient for uniqueness.
684 if (((tf->lbam == 0) && (tf->lbah == 0)) ||
685 ((tf->lbam == 0x3c) && (tf->lbah == 0xc3))) {
686 DPRINTK("found ATA device by sig\n");
687 return ATA_DEV_ATA;
690 if (((tf->lbam == 0x14) && (tf->lbah == 0xeb)) ||
691 ((tf->lbam == 0x69) && (tf->lbah == 0x96))) {
692 DPRINTK("found ATAPI device by sig\n");
693 return ATA_DEV_ATAPI;
696 DPRINTK("unknown device\n");
697 return ATA_DEV_UNKNOWN;
701 * ata_dev_try_classify - Parse returned ATA device signature
702 * @dev: ATA device to classify (starting at zero)
703 * @present: device seems present
704 * @r_err: Value of error register on completion
706 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
707 * an ATA/ATAPI-defined set of values is placed in the ATA
708 * shadow registers, indicating the results of device detection
709 * and diagnostics.
711 * Select the ATA device, and read the values from the ATA shadow
712 * registers. Then parse according to the Error register value,
713 * and the spec-defined values examined by ata_dev_classify().
715 * LOCKING:
716 * caller.
718 * RETURNS:
719 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
721 unsigned int ata_dev_try_classify(struct ata_device *dev, int present,
722 u8 *r_err)
724 struct ata_port *ap = dev->link->ap;
725 struct ata_taskfile tf;
726 unsigned int class;
727 u8 err;
729 ap->ops->dev_select(ap, dev->devno);
731 memset(&tf, 0, sizeof(tf));
733 ap->ops->tf_read(ap, &tf);
734 err = tf.feature;
735 if (r_err)
736 *r_err = err;
738 /* see if device passed diags: if master then continue and warn later */
739 if (err == 0 && dev->devno == 0)
740 /* diagnostic fail : do nothing _YET_ */
741 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
742 else if (err == 1)
743 /* do nothing */ ;
744 else if ((dev->devno == 0) && (err == 0x81))
745 /* do nothing */ ;
746 else
747 return ATA_DEV_NONE;
749 /* determine if device is ATA or ATAPI */
750 class = ata_dev_classify(&tf);
752 if (class == ATA_DEV_UNKNOWN) {
753 /* If the device failed diagnostic, it's likely to
754 * have reported incorrect device signature too.
755 * Assume ATA device if the device seems present but
756 * device signature is invalid with diagnostic
757 * failure.
759 if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
760 class = ATA_DEV_ATA;
761 else
762 class = ATA_DEV_NONE;
763 } else if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
764 class = ATA_DEV_NONE;
766 return class;
770 * ata_id_string - Convert IDENTIFY DEVICE page into string
771 * @id: IDENTIFY DEVICE results we will examine
772 * @s: string into which data is output
773 * @ofs: offset into identify device page
774 * @len: length of string to return. must be an even number.
776 * The strings in the IDENTIFY DEVICE page are broken up into
777 * 16-bit chunks. Run through the string, and output each
778 * 8-bit chunk linearly, regardless of platform.
780 * LOCKING:
781 * caller.
784 void ata_id_string(const u16 *id, unsigned char *s,
785 unsigned int ofs, unsigned int len)
787 unsigned int c;
789 while (len > 0) {
790 c = id[ofs] >> 8;
791 *s = c;
792 s++;
794 c = id[ofs] & 0xff;
795 *s = c;
796 s++;
798 ofs++;
799 len -= 2;
804 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
805 * @id: IDENTIFY DEVICE results we will examine
806 * @s: string into which data is output
807 * @ofs: offset into identify device page
808 * @len: length of string to return. must be an odd number.
810 * This function is identical to ata_id_string except that it
811 * trims trailing spaces and terminates the resulting string with
812 * null. @len must be actual maximum length (even number) + 1.
814 * LOCKING:
815 * caller.
817 void ata_id_c_string(const u16 *id, unsigned char *s,
818 unsigned int ofs, unsigned int len)
820 unsigned char *p;
822 WARN_ON(!(len & 1));
824 ata_id_string(id, s, ofs, len - 1);
826 p = s + strnlen(s, len - 1);
827 while (p > s && p[-1] == ' ')
828 p--;
829 *p = '\0';
832 static u64 ata_id_n_sectors(const u16 *id)
834 if (ata_id_has_lba(id)) {
835 if (ata_id_has_lba48(id))
836 return ata_id_u64(id, 100);
837 else
838 return ata_id_u32(id, 60);
839 } else {
840 if (ata_id_current_chs_valid(id))
841 return ata_id_u32(id, 57);
842 else
843 return id[1] * id[3] * id[6];
847 static u64 ata_tf_to_lba48(struct ata_taskfile *tf)
849 u64 sectors = 0;
851 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
852 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
853 sectors |= (tf->hob_lbal & 0xff) << 24;
854 sectors |= (tf->lbah & 0xff) << 16;
855 sectors |= (tf->lbam & 0xff) << 8;
856 sectors |= (tf->lbal & 0xff);
858 return ++sectors;
861 static u64 ata_tf_to_lba(struct ata_taskfile *tf)
863 u64 sectors = 0;
865 sectors |= (tf->device & 0x0f) << 24;
866 sectors |= (tf->lbah & 0xff) << 16;
867 sectors |= (tf->lbam & 0xff) << 8;
868 sectors |= (tf->lbal & 0xff);
870 return ++sectors;
874 * ata_read_native_max_address - Read native max address
875 * @dev: target device
876 * @max_sectors: out parameter for the result native max address
878 * Perform an LBA48 or LBA28 native size query upon the device in
879 * question.
881 * RETURNS:
882 * 0 on success, -EACCES if command is aborted by the drive.
883 * -EIO on other errors.
885 static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
887 unsigned int err_mask;
888 struct ata_taskfile tf;
889 int lba48 = ata_id_has_lba48(dev->id);
891 ata_tf_init(dev, &tf);
893 /* always clear all address registers */
894 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
896 if (lba48) {
897 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
898 tf.flags |= ATA_TFLAG_LBA48;
899 } else
900 tf.command = ATA_CMD_READ_NATIVE_MAX;
902 tf.protocol |= ATA_PROT_NODATA;
903 tf.device |= ATA_LBA;
905 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
906 if (err_mask) {
907 ata_dev_printk(dev, KERN_WARNING, "failed to read native "
908 "max address (err_mask=0x%x)\n", err_mask);
909 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
910 return -EACCES;
911 return -EIO;
914 if (lba48)
915 *max_sectors = ata_tf_to_lba48(&tf);
916 else
917 *max_sectors = ata_tf_to_lba(&tf);
919 return 0;
923 * ata_set_max_sectors - Set max sectors
924 * @dev: target device
925 * @new_sectors: new max sectors value to set for the device
927 * Set max sectors of @dev to @new_sectors.
929 * RETURNS:
930 * 0 on success, -EACCES if command is aborted or denied (due to
931 * previous non-volatile SET_MAX) by the drive. -EIO on other
932 * errors.
934 static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
936 unsigned int err_mask;
937 struct ata_taskfile tf;
938 int lba48 = ata_id_has_lba48(dev->id);
940 new_sectors--;
942 ata_tf_init(dev, &tf);
944 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
946 if (lba48) {
947 tf.command = ATA_CMD_SET_MAX_EXT;
948 tf.flags |= ATA_TFLAG_LBA48;
950 tf.hob_lbal = (new_sectors >> 24) & 0xff;
951 tf.hob_lbam = (new_sectors >> 32) & 0xff;
952 tf.hob_lbah = (new_sectors >> 40) & 0xff;
953 } else
954 tf.command = ATA_CMD_SET_MAX;
956 tf.protocol |= ATA_PROT_NODATA;
957 tf.device |= ATA_LBA;
959 tf.lbal = (new_sectors >> 0) & 0xff;
960 tf.lbam = (new_sectors >> 8) & 0xff;
961 tf.lbah = (new_sectors >> 16) & 0xff;
963 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
964 if (err_mask) {
965 ata_dev_printk(dev, KERN_WARNING, "failed to set "
966 "max address (err_mask=0x%x)\n", err_mask);
967 if (err_mask == AC_ERR_DEV &&
968 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
969 return -EACCES;
970 return -EIO;
973 return 0;
977 * ata_hpa_resize - Resize a device with an HPA set
978 * @dev: Device to resize
980 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
981 * it if required to the full size of the media. The caller must check
982 * the drive has the HPA feature set enabled.
984 * RETURNS:
985 * 0 on success, -errno on failure.
987 static int ata_hpa_resize(struct ata_device *dev)
989 struct ata_eh_context *ehc = &dev->link->eh_context;
990 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
991 u64 sectors = ata_id_n_sectors(dev->id);
992 u64 native_sectors;
993 int rc;
995 /* do we need to do it? */
996 if (dev->class != ATA_DEV_ATA ||
997 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
998 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
999 return 0;
1001 /* read native max address */
1002 rc = ata_read_native_max_address(dev, &native_sectors);
1003 if (rc) {
1004 /* If HPA isn't going to be unlocked, skip HPA
1005 * resizing from the next try.
1007 if (!ata_ignore_hpa) {
1008 ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
1009 "broken, will skip HPA handling\n");
1010 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1012 /* we can continue if device aborted the command */
1013 if (rc == -EACCES)
1014 rc = 0;
1017 return rc;
1020 /* nothing to do? */
1021 if (native_sectors <= sectors || !ata_ignore_hpa) {
1022 if (!print_info || native_sectors == sectors)
1023 return 0;
1025 if (native_sectors > sectors)
1026 ata_dev_printk(dev, KERN_INFO,
1027 "HPA detected: current %llu, native %llu\n",
1028 (unsigned long long)sectors,
1029 (unsigned long long)native_sectors);
1030 else if (native_sectors < sectors)
1031 ata_dev_printk(dev, KERN_WARNING,
1032 "native sectors (%llu) is smaller than "
1033 "sectors (%llu)\n",
1034 (unsigned long long)native_sectors,
1035 (unsigned long long)sectors);
1036 return 0;
1039 /* let's unlock HPA */
1040 rc = ata_set_max_sectors(dev, native_sectors);
1041 if (rc == -EACCES) {
1042 /* if device aborted the command, skip HPA resizing */
1043 ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
1044 "(%llu -> %llu), skipping HPA handling\n",
1045 (unsigned long long)sectors,
1046 (unsigned long long)native_sectors);
1047 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1048 return 0;
1049 } else if (rc)
1050 return rc;
1052 /* re-read IDENTIFY data */
1053 rc = ata_dev_reread_id(dev, 0);
1054 if (rc) {
1055 ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
1056 "data after HPA resizing\n");
1057 return rc;
1060 if (print_info) {
1061 u64 new_sectors = ata_id_n_sectors(dev->id);
1062 ata_dev_printk(dev, KERN_INFO,
1063 "HPA unlocked: %llu -> %llu, native %llu\n",
1064 (unsigned long long)sectors,
1065 (unsigned long long)new_sectors,
1066 (unsigned long long)native_sectors);
1069 return 0;
1073 * ata_id_to_dma_mode - Identify DMA mode from id block
1074 * @dev: device to identify
1075 * @unknown: mode to assume if we cannot tell
1077 * Set up the timing values for the device based upon the identify
1078 * reported values for the DMA mode. This function is used by drivers
1079 * which rely upon firmware configured modes, but wish to report the
1080 * mode correctly when possible.
1082 * In addition we emit similarly formatted messages to the default
1083 * ata_dev_set_mode handler, in order to provide consistency of
1084 * presentation.
1087 void ata_id_to_dma_mode(struct ata_device *dev, u8 unknown)
1089 unsigned int mask;
1090 u8 mode;
1092 /* Pack the DMA modes */
1093 mask = ((dev->id[63] >> 8) << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA;
1094 if (dev->id[53] & 0x04)
1095 mask |= ((dev->id[88] >> 8) << ATA_SHIFT_UDMA) & ATA_MASK_UDMA;
1097 /* Select the mode in use */
1098 mode = ata_xfer_mask2mode(mask);
1100 if (mode != 0) {
1101 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
1102 ata_mode_string(mask));
1103 } else {
1104 /* SWDMA perhaps ? */
1105 mode = unknown;
1106 ata_dev_printk(dev, KERN_INFO, "configured for DMA\n");
1109 /* Configure the device reporting */
1110 dev->xfer_mode = mode;
1111 dev->xfer_shift = ata_xfer_mode2shift(mode);
1115 * ata_noop_dev_select - Select device 0/1 on ATA bus
1116 * @ap: ATA channel to manipulate
1117 * @device: ATA device (numbered from zero) to select
1119 * This function performs no actual function.
1121 * May be used as the dev_select() entry in ata_port_operations.
1123 * LOCKING:
1124 * caller.
1126 void ata_noop_dev_select (struct ata_port *ap, unsigned int device)
1132 * ata_std_dev_select - Select device 0/1 on ATA bus
1133 * @ap: ATA channel to manipulate
1134 * @device: ATA device (numbered from zero) to select
1136 * Use the method defined in the ATA specification to
1137 * make either device 0, or device 1, active on the
1138 * ATA channel. Works with both PIO and MMIO.
1140 * May be used as the dev_select() entry in ata_port_operations.
1142 * LOCKING:
1143 * caller.
1146 void ata_std_dev_select (struct ata_port *ap, unsigned int device)
1148 u8 tmp;
1150 if (device == 0)
1151 tmp = ATA_DEVICE_OBS;
1152 else
1153 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1155 iowrite8(tmp, ap->ioaddr.device_addr);
1156 ata_pause(ap); /* needed; also flushes, for mmio */
1160 * ata_dev_select - Select device 0/1 on ATA bus
1161 * @ap: ATA channel to manipulate
1162 * @device: ATA device (numbered from zero) to select
1163 * @wait: non-zero to wait for Status register BSY bit to clear
1164 * @can_sleep: non-zero if context allows sleeping
1166 * Use the method defined in the ATA specification to
1167 * make either device 0, or device 1, active on the
1168 * ATA channel.
1170 * This is a high-level version of ata_std_dev_select(),
1171 * which additionally provides the services of inserting
1172 * the proper pauses and status polling, where needed.
1174 * LOCKING:
1175 * caller.
1178 void ata_dev_select(struct ata_port *ap, unsigned int device,
1179 unsigned int wait, unsigned int can_sleep)
1181 if (ata_msg_probe(ap))
1182 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
1183 "device %u, wait %u\n", device, wait);
1185 if (wait)
1186 ata_wait_idle(ap);
1188 ap->ops->dev_select(ap, device);
1190 if (wait) {
1191 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
1192 msleep(150);
1193 ata_wait_idle(ap);
1198 * ata_dump_id - IDENTIFY DEVICE info debugging output
1199 * @id: IDENTIFY DEVICE page to dump
1201 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1202 * page.
1204 * LOCKING:
1205 * caller.
1208 static inline void ata_dump_id(const u16 *id)
1210 DPRINTK("49==0x%04x "
1211 "53==0x%04x "
1212 "63==0x%04x "
1213 "64==0x%04x "
1214 "75==0x%04x \n",
1215 id[49],
1216 id[53],
1217 id[63],
1218 id[64],
1219 id[75]);
1220 DPRINTK("80==0x%04x "
1221 "81==0x%04x "
1222 "82==0x%04x "
1223 "83==0x%04x "
1224 "84==0x%04x \n",
1225 id[80],
1226 id[81],
1227 id[82],
1228 id[83],
1229 id[84]);
1230 DPRINTK("88==0x%04x "
1231 "93==0x%04x\n",
1232 id[88],
1233 id[93]);
1237 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1238 * @id: IDENTIFY data to compute xfer mask from
1240 * Compute the xfermask for this device. This is not as trivial
1241 * as it seems if we must consider early devices correctly.
1243 * FIXME: pre IDE drive timing (do we care ?).
1245 * LOCKING:
1246 * None.
1248 * RETURNS:
1249 * Computed xfermask
1251 static unsigned int ata_id_xfermask(const u16 *id)
1253 unsigned int pio_mask, mwdma_mask, udma_mask;
1255 /* Usual case. Word 53 indicates word 64 is valid */
1256 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1257 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1258 pio_mask <<= 3;
1259 pio_mask |= 0x7;
1260 } else {
1261 /* If word 64 isn't valid then Word 51 high byte holds
1262 * the PIO timing number for the maximum. Turn it into
1263 * a mask.
1265 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
1266 if (mode < 5) /* Valid PIO range */
1267 pio_mask = (2 << mode) - 1;
1268 else
1269 pio_mask = 1;
1271 /* But wait.. there's more. Design your standards by
1272 * committee and you too can get a free iordy field to
1273 * process. However its the speeds not the modes that
1274 * are supported... Note drivers using the timing API
1275 * will get this right anyway
1279 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1281 if (ata_id_is_cfa(id)) {
1283 * Process compact flash extended modes
1285 int pio = id[163] & 0x7;
1286 int dma = (id[163] >> 3) & 7;
1288 if (pio)
1289 pio_mask |= (1 << 5);
1290 if (pio > 1)
1291 pio_mask |= (1 << 6);
1292 if (dma)
1293 mwdma_mask |= (1 << 3);
1294 if (dma > 1)
1295 mwdma_mask |= (1 << 4);
1298 udma_mask = 0;
1299 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1300 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1302 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1306 * ata_port_queue_task - Queue port_task
1307 * @ap: The ata_port to queue port_task for
1308 * @fn: workqueue function to be scheduled
1309 * @data: data for @fn to use
1310 * @delay: delay time for workqueue function
1312 * Schedule @fn(@data) for execution after @delay jiffies using
1313 * port_task. There is one port_task per port and it's the
1314 * user(low level driver)'s responsibility to make sure that only
1315 * one task is active at any given time.
1317 * libata core layer takes care of synchronization between
1318 * port_task and EH. ata_port_queue_task() may be ignored for EH
1319 * synchronization.
1321 * LOCKING:
1322 * Inherited from caller.
1324 void ata_port_queue_task(struct ata_port *ap, work_func_t fn, void *data,
1325 unsigned long delay)
1327 PREPARE_DELAYED_WORK(&ap->port_task, fn);
1328 ap->port_task_data = data;
1330 /* may fail if ata_port_flush_task() in progress */
1331 queue_delayed_work(ata_wq, &ap->port_task, delay);
1335 * ata_port_flush_task - Flush port_task
1336 * @ap: The ata_port to flush port_task for
1338 * After this function completes, port_task is guranteed not to
1339 * be running or scheduled.
1341 * LOCKING:
1342 * Kernel thread context (may sleep)
1344 void ata_port_flush_task(struct ata_port *ap)
1346 DPRINTK("ENTER\n");
1348 cancel_rearming_delayed_work(&ap->port_task);
1350 if (ata_msg_ctl(ap))
1351 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __FUNCTION__);
1354 static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1356 struct completion *waiting = qc->private_data;
1358 complete(waiting);
1362 * ata_exec_internal_sg - execute libata internal command
1363 * @dev: Device to which the command is sent
1364 * @tf: Taskfile registers for the command and the result
1365 * @cdb: CDB for packet command
1366 * @dma_dir: Data tranfer direction of the command
1367 * @sg: sg list for the data buffer of the command
1368 * @n_elem: Number of sg entries
1370 * Executes libata internal command with timeout. @tf contains
1371 * command on entry and result on return. Timeout and error
1372 * conditions are reported via return value. No recovery action
1373 * is taken after a command times out. It's caller's duty to
1374 * clean up after timeout.
1376 * LOCKING:
1377 * None. Should be called with kernel context, might sleep.
1379 * RETURNS:
1380 * Zero on success, AC_ERR_* mask on failure
1382 unsigned ata_exec_internal_sg(struct ata_device *dev,
1383 struct ata_taskfile *tf, const u8 *cdb,
1384 int dma_dir, struct scatterlist *sg,
1385 unsigned int n_elem)
1387 struct ata_link *link = dev->link;
1388 struct ata_port *ap = link->ap;
1389 u8 command = tf->command;
1390 struct ata_queued_cmd *qc;
1391 unsigned int tag, preempted_tag;
1392 u32 preempted_sactive, preempted_qc_active;
1393 int preempted_nr_active_links;
1394 DECLARE_COMPLETION_ONSTACK(wait);
1395 unsigned long flags;
1396 unsigned int err_mask;
1397 int rc;
1399 spin_lock_irqsave(ap->lock, flags);
1401 /* no internal command while frozen */
1402 if (ap->pflags & ATA_PFLAG_FROZEN) {
1403 spin_unlock_irqrestore(ap->lock, flags);
1404 return AC_ERR_SYSTEM;
1407 /* initialize internal qc */
1409 /* XXX: Tag 0 is used for drivers with legacy EH as some
1410 * drivers choke if any other tag is given. This breaks
1411 * ata_tag_internal() test for those drivers. Don't use new
1412 * EH stuff without converting to it.
1414 if (ap->ops->error_handler)
1415 tag = ATA_TAG_INTERNAL;
1416 else
1417 tag = 0;
1419 if (test_and_set_bit(tag, &ap->qc_allocated))
1420 BUG();
1421 qc = __ata_qc_from_tag(ap, tag);
1423 qc->tag = tag;
1424 qc->scsicmd = NULL;
1425 qc->ap = ap;
1426 qc->dev = dev;
1427 ata_qc_reinit(qc);
1429 preempted_tag = link->active_tag;
1430 preempted_sactive = link->sactive;
1431 preempted_qc_active = ap->qc_active;
1432 preempted_nr_active_links = ap->nr_active_links;
1433 link->active_tag = ATA_TAG_POISON;
1434 link->sactive = 0;
1435 ap->qc_active = 0;
1436 ap->nr_active_links = 0;
1438 /* prepare & issue qc */
1439 qc->tf = *tf;
1440 if (cdb)
1441 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1442 qc->flags |= ATA_QCFLAG_RESULT_TF;
1443 qc->dma_dir = dma_dir;
1444 if (dma_dir != DMA_NONE) {
1445 unsigned int i, buflen = 0;
1447 for (i = 0; i < n_elem; i++)
1448 buflen += sg[i].length;
1450 ata_sg_init(qc, sg, n_elem);
1451 qc->nbytes = buflen;
1454 qc->private_data = &wait;
1455 qc->complete_fn = ata_qc_complete_internal;
1457 ata_qc_issue(qc);
1459 spin_unlock_irqrestore(ap->lock, flags);
1461 rc = wait_for_completion_timeout(&wait, ata_probe_timeout);
1463 ata_port_flush_task(ap);
1465 if (!rc) {
1466 spin_lock_irqsave(ap->lock, flags);
1468 /* We're racing with irq here. If we lose, the
1469 * following test prevents us from completing the qc
1470 * twice. If we win, the port is frozen and will be
1471 * cleaned up by ->post_internal_cmd().
1473 if (qc->flags & ATA_QCFLAG_ACTIVE) {
1474 qc->err_mask |= AC_ERR_TIMEOUT;
1476 if (ap->ops->error_handler)
1477 ata_port_freeze(ap);
1478 else
1479 ata_qc_complete(qc);
1481 if (ata_msg_warn(ap))
1482 ata_dev_printk(dev, KERN_WARNING,
1483 "qc timeout (cmd 0x%x)\n", command);
1486 spin_unlock_irqrestore(ap->lock, flags);
1489 /* do post_internal_cmd */
1490 if (ap->ops->post_internal_cmd)
1491 ap->ops->post_internal_cmd(qc);
1493 /* perform minimal error analysis */
1494 if (qc->flags & ATA_QCFLAG_FAILED) {
1495 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1496 qc->err_mask |= AC_ERR_DEV;
1498 if (!qc->err_mask)
1499 qc->err_mask |= AC_ERR_OTHER;
1501 if (qc->err_mask & ~AC_ERR_OTHER)
1502 qc->err_mask &= ~AC_ERR_OTHER;
1505 /* finish up */
1506 spin_lock_irqsave(ap->lock, flags);
1508 *tf = qc->result_tf;
1509 err_mask = qc->err_mask;
1511 ata_qc_free(qc);
1512 link->active_tag = preempted_tag;
1513 link->sactive = preempted_sactive;
1514 ap->qc_active = preempted_qc_active;
1515 ap->nr_active_links = preempted_nr_active_links;
1517 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1518 * Until those drivers are fixed, we detect the condition
1519 * here, fail the command with AC_ERR_SYSTEM and reenable the
1520 * port.
1522 * Note that this doesn't change any behavior as internal
1523 * command failure results in disabling the device in the
1524 * higher layer for LLDDs without new reset/EH callbacks.
1526 * Kill the following code as soon as those drivers are fixed.
1528 if (ap->flags & ATA_FLAG_DISABLED) {
1529 err_mask |= AC_ERR_SYSTEM;
1530 ata_port_probe(ap);
1533 spin_unlock_irqrestore(ap->lock, flags);
1535 return err_mask;
1539 * ata_exec_internal - execute libata internal command
1540 * @dev: Device to which the command is sent
1541 * @tf: Taskfile registers for the command and the result
1542 * @cdb: CDB for packet command
1543 * @dma_dir: Data tranfer direction of the command
1544 * @buf: Data buffer of the command
1545 * @buflen: Length of data buffer
1547 * Wrapper around ata_exec_internal_sg() which takes simple
1548 * buffer instead of sg list.
1550 * LOCKING:
1551 * None. Should be called with kernel context, might sleep.
1553 * RETURNS:
1554 * Zero on success, AC_ERR_* mask on failure
1556 unsigned ata_exec_internal(struct ata_device *dev,
1557 struct ata_taskfile *tf, const u8 *cdb,
1558 int dma_dir, void *buf, unsigned int buflen)
1560 struct scatterlist *psg = NULL, sg;
1561 unsigned int n_elem = 0;
1563 if (dma_dir != DMA_NONE) {
1564 WARN_ON(!buf);
1565 sg_init_one(&sg, buf, buflen);
1566 psg = &sg;
1567 n_elem++;
1570 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem);
1574 * ata_do_simple_cmd - execute simple internal command
1575 * @dev: Device to which the command is sent
1576 * @cmd: Opcode to execute
1578 * Execute a 'simple' command, that only consists of the opcode
1579 * 'cmd' itself, without filling any other registers
1581 * LOCKING:
1582 * Kernel thread context (may sleep).
1584 * RETURNS:
1585 * Zero on success, AC_ERR_* mask on failure
1587 unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
1589 struct ata_taskfile tf;
1591 ata_tf_init(dev, &tf);
1593 tf.command = cmd;
1594 tf.flags |= ATA_TFLAG_DEVICE;
1595 tf.protocol = ATA_PROT_NODATA;
1597 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
1601 * ata_pio_need_iordy - check if iordy needed
1602 * @adev: ATA device
1604 * Check if the current speed of the device requires IORDY. Used
1605 * by various controllers for chip configuration.
1608 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1610 /* Controller doesn't support IORDY. Probably a pointless check
1611 as the caller should know this */
1612 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1613 return 0;
1614 /* PIO3 and higher it is mandatory */
1615 if (adev->pio_mode > XFER_PIO_2)
1616 return 1;
1617 /* We turn it on when possible */
1618 if (ata_id_has_iordy(adev->id))
1619 return 1;
1620 return 0;
1624 * ata_pio_mask_no_iordy - Return the non IORDY mask
1625 * @adev: ATA device
1627 * Compute the highest mode possible if we are not using iordy. Return
1628 * -1 if no iordy mode is available.
1631 static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1633 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1634 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
1635 u16 pio = adev->id[ATA_ID_EIDE_PIO];
1636 /* Is the speed faster than the drive allows non IORDY ? */
1637 if (pio) {
1638 /* This is cycle times not frequency - watch the logic! */
1639 if (pio > 240) /* PIO2 is 240nS per cycle */
1640 return 3 << ATA_SHIFT_PIO;
1641 return 7 << ATA_SHIFT_PIO;
1644 return 3 << ATA_SHIFT_PIO;
1648 * ata_dev_read_id - Read ID data from the specified device
1649 * @dev: target device
1650 * @p_class: pointer to class of the target device (may be changed)
1651 * @flags: ATA_READID_* flags
1652 * @id: buffer to read IDENTIFY data into
1654 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1655 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1656 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1657 * for pre-ATA4 drives.
1659 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
1660 * now we abort if we hit that case.
1662 * LOCKING:
1663 * Kernel thread context (may sleep)
1665 * RETURNS:
1666 * 0 on success, -errno otherwise.
1668 int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1669 unsigned int flags, u16 *id)
1671 struct ata_port *ap = dev->link->ap;
1672 unsigned int class = *p_class;
1673 struct ata_taskfile tf;
1674 unsigned int err_mask = 0;
1675 const char *reason;
1676 int may_fallback = 1, tried_spinup = 0;
1677 int rc;
1679 if (ata_msg_ctl(ap))
1680 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
1682 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
1683 retry:
1684 ata_tf_init(dev, &tf);
1686 switch (class) {
1687 case ATA_DEV_ATA:
1688 tf.command = ATA_CMD_ID_ATA;
1689 break;
1690 case ATA_DEV_ATAPI:
1691 tf.command = ATA_CMD_ID_ATAPI;
1692 break;
1693 default:
1694 rc = -ENODEV;
1695 reason = "unsupported class";
1696 goto err_out;
1699 tf.protocol = ATA_PROT_PIO;
1701 /* Some devices choke if TF registers contain garbage. Make
1702 * sure those are properly initialized.
1704 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1706 /* Device presence detection is unreliable on some
1707 * controllers. Always poll IDENTIFY if available.
1709 tf.flags |= ATA_TFLAG_POLLING;
1711 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
1712 id, sizeof(id[0]) * ATA_ID_WORDS);
1713 if (err_mask) {
1714 if (err_mask & AC_ERR_NODEV_HINT) {
1715 DPRINTK("ata%u.%d: NODEV after polling detection\n",
1716 ap->print_id, dev->devno);
1717 return -ENOENT;
1720 /* Device or controller might have reported the wrong
1721 * device class. Give a shot at the other IDENTIFY if
1722 * the current one is aborted by the device.
1724 if (may_fallback &&
1725 (err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1726 may_fallback = 0;
1728 if (class == ATA_DEV_ATA)
1729 class = ATA_DEV_ATAPI;
1730 else
1731 class = ATA_DEV_ATA;
1732 goto retry;
1735 rc = -EIO;
1736 reason = "I/O error";
1737 goto err_out;
1740 /* Falling back doesn't make sense if ID data was read
1741 * successfully at least once.
1743 may_fallback = 0;
1745 swap_buf_le16(id, ATA_ID_WORDS);
1747 /* sanity check */
1748 rc = -EINVAL;
1749 reason = "device reports invalid type";
1751 if (class == ATA_DEV_ATA) {
1752 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
1753 goto err_out;
1754 } else {
1755 if (ata_id_is_ata(id))
1756 goto err_out;
1759 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1760 tried_spinup = 1;
1762 * Drive powered-up in standby mode, and requires a specific
1763 * SET_FEATURES spin-up subcommand before it will accept
1764 * anything other than the original IDENTIFY command.
1766 ata_tf_init(dev, &tf);
1767 tf.command = ATA_CMD_SET_FEATURES;
1768 tf.feature = SETFEATURES_SPINUP;
1769 tf.protocol = ATA_PROT_NODATA;
1770 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1771 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
1772 if (err_mask && id[2] != 0x738c) {
1773 rc = -EIO;
1774 reason = "SPINUP failed";
1775 goto err_out;
1778 * If the drive initially returned incomplete IDENTIFY info,
1779 * we now must reissue the IDENTIFY command.
1781 if (id[2] == 0x37c8)
1782 goto retry;
1785 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
1787 * The exact sequence expected by certain pre-ATA4 drives is:
1788 * SRST RESET
1789 * IDENTIFY (optional in early ATA)
1790 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
1791 * anything else..
1792 * Some drives were very specific about that exact sequence.
1794 * Note that ATA4 says lba is mandatory so the second check
1795 * shoud never trigger.
1797 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1798 err_mask = ata_dev_init_params(dev, id[3], id[6]);
1799 if (err_mask) {
1800 rc = -EIO;
1801 reason = "INIT_DEV_PARAMS failed";
1802 goto err_out;
1805 /* current CHS translation info (id[53-58]) might be
1806 * changed. reread the identify device info.
1808 flags &= ~ATA_READID_POSTRESET;
1809 goto retry;
1813 *p_class = class;
1815 return 0;
1817 err_out:
1818 if (ata_msg_warn(ap))
1819 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
1820 "(%s, err_mask=0x%x)\n", reason, err_mask);
1821 return rc;
1824 static inline u8 ata_dev_knobble(struct ata_device *dev)
1826 struct ata_port *ap = dev->link->ap;
1827 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
1830 static void ata_dev_config_ncq(struct ata_device *dev,
1831 char *desc, size_t desc_sz)
1833 struct ata_port *ap = dev->link->ap;
1834 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
1836 if (!ata_id_has_ncq(dev->id)) {
1837 desc[0] = '\0';
1838 return;
1840 if (dev->horkage & ATA_HORKAGE_NONCQ) {
1841 snprintf(desc, desc_sz, "NCQ (not used)");
1842 return;
1844 if (ap->flags & ATA_FLAG_NCQ) {
1845 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
1846 dev->flags |= ATA_DFLAG_NCQ;
1849 if (hdepth >= ddepth)
1850 snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
1851 else
1852 snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
1856 * ata_dev_configure - Configure the specified ATA/ATAPI device
1857 * @dev: Target device to configure
1859 * Configure @dev according to @dev->id. Generic and low-level
1860 * driver specific fixups are also applied.
1862 * LOCKING:
1863 * Kernel thread context (may sleep)
1865 * RETURNS:
1866 * 0 on success, -errno otherwise
1868 int ata_dev_configure(struct ata_device *dev)
1870 struct ata_port *ap = dev->link->ap;
1871 struct ata_eh_context *ehc = &dev->link->eh_context;
1872 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
1873 const u16 *id = dev->id;
1874 unsigned int xfer_mask;
1875 char revbuf[7]; /* XYZ-99\0 */
1876 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
1877 char modelbuf[ATA_ID_PROD_LEN+1];
1878 int rc;
1880 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
1881 ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
1882 __FUNCTION__);
1883 return 0;
1886 if (ata_msg_probe(ap))
1887 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
1889 /* set horkage */
1890 dev->horkage |= ata_dev_blacklisted(dev);
1892 /* let ACPI work its magic */
1893 rc = ata_acpi_on_devcfg(dev);
1894 if (rc)
1895 return rc;
1897 /* massage HPA, do it early as it might change IDENTIFY data */
1898 rc = ata_hpa_resize(dev);
1899 if (rc)
1900 return rc;
1902 /* print device capabilities */
1903 if (ata_msg_probe(ap))
1904 ata_dev_printk(dev, KERN_DEBUG,
1905 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
1906 "85:%04x 86:%04x 87:%04x 88:%04x\n",
1907 __FUNCTION__,
1908 id[49], id[82], id[83], id[84],
1909 id[85], id[86], id[87], id[88]);
1911 /* initialize to-be-configured parameters */
1912 dev->flags &= ~ATA_DFLAG_CFG_MASK;
1913 dev->max_sectors = 0;
1914 dev->cdb_len = 0;
1915 dev->n_sectors = 0;
1916 dev->cylinders = 0;
1917 dev->heads = 0;
1918 dev->sectors = 0;
1921 * common ATA, ATAPI feature tests
1924 /* find max transfer mode; for printk only */
1925 xfer_mask = ata_id_xfermask(id);
1927 if (ata_msg_probe(ap))
1928 ata_dump_id(id);
1930 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
1931 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
1932 sizeof(fwrevbuf));
1934 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
1935 sizeof(modelbuf));
1937 /* ATA-specific feature tests */
1938 if (dev->class == ATA_DEV_ATA) {
1939 if (ata_id_is_cfa(id)) {
1940 if (id[162] & 1) /* CPRM may make this media unusable */
1941 ata_dev_printk(dev, KERN_WARNING,
1942 "supports DRM functions and may "
1943 "not be fully accessable.\n");
1944 snprintf(revbuf, 7, "CFA");
1946 else
1947 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
1949 dev->n_sectors = ata_id_n_sectors(id);
1951 if (dev->id[59] & 0x100)
1952 dev->multi_count = dev->id[59] & 0xff;
1954 if (ata_id_has_lba(id)) {
1955 const char *lba_desc;
1956 char ncq_desc[20];
1958 lba_desc = "LBA";
1959 dev->flags |= ATA_DFLAG_LBA;
1960 if (ata_id_has_lba48(id)) {
1961 dev->flags |= ATA_DFLAG_LBA48;
1962 lba_desc = "LBA48";
1964 if (dev->n_sectors >= (1UL << 28) &&
1965 ata_id_has_flush_ext(id))
1966 dev->flags |= ATA_DFLAG_FLUSH_EXT;
1969 /* config NCQ */
1970 ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
1972 /* print device info to dmesg */
1973 if (ata_msg_drv(ap) && print_info) {
1974 ata_dev_printk(dev, KERN_INFO,
1975 "%s: %s, %s, max %s\n",
1976 revbuf, modelbuf, fwrevbuf,
1977 ata_mode_string(xfer_mask));
1978 ata_dev_printk(dev, KERN_INFO,
1979 "%Lu sectors, multi %u: %s %s\n",
1980 (unsigned long long)dev->n_sectors,
1981 dev->multi_count, lba_desc, ncq_desc);
1983 } else {
1984 /* CHS */
1986 /* Default translation */
1987 dev->cylinders = id[1];
1988 dev->heads = id[3];
1989 dev->sectors = id[6];
1991 if (ata_id_current_chs_valid(id)) {
1992 /* Current CHS translation is valid. */
1993 dev->cylinders = id[54];
1994 dev->heads = id[55];
1995 dev->sectors = id[56];
1998 /* print device info to dmesg */
1999 if (ata_msg_drv(ap) && print_info) {
2000 ata_dev_printk(dev, KERN_INFO,
2001 "%s: %s, %s, max %s\n",
2002 revbuf, modelbuf, fwrevbuf,
2003 ata_mode_string(xfer_mask));
2004 ata_dev_printk(dev, KERN_INFO,
2005 "%Lu sectors, multi %u, CHS %u/%u/%u\n",
2006 (unsigned long long)dev->n_sectors,
2007 dev->multi_count, dev->cylinders,
2008 dev->heads, dev->sectors);
2012 dev->cdb_len = 16;
2015 /* ATAPI-specific feature tests */
2016 else if (dev->class == ATA_DEV_ATAPI) {
2017 const char *cdb_intr_string = "";
2018 const char *atapi_an_string = "";
2019 u32 sntf;
2021 rc = atapi_cdb_len(id);
2022 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2023 if (ata_msg_warn(ap))
2024 ata_dev_printk(dev, KERN_WARNING,
2025 "unsupported CDB len\n");
2026 rc = -EINVAL;
2027 goto err_out_nosup;
2029 dev->cdb_len = (unsigned int) rc;
2031 /* Enable ATAPI AN if both the host and device have
2032 * the support. If PMP is attached, SNTF is required
2033 * to enable ATAPI AN to discern between PHY status
2034 * changed notifications and ATAPI ANs.
2036 if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2037 (!ap->nr_pmp_links ||
2038 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
2039 unsigned int err_mask;
2041 /* issue SET feature command to turn this on */
2042 err_mask = ata_dev_set_AN(dev, SETFEATURES_SATA_ENABLE);
2043 if (err_mask)
2044 ata_dev_printk(dev, KERN_ERR,
2045 "failed to enable ATAPI AN "
2046 "(err_mask=0x%x)\n", err_mask);
2047 else {
2048 dev->flags |= ATA_DFLAG_AN;
2049 atapi_an_string = ", ATAPI AN";
2053 if (ata_id_cdb_intr(dev->id)) {
2054 dev->flags |= ATA_DFLAG_CDB_INTR;
2055 cdb_intr_string = ", CDB intr";
2058 /* print device info to dmesg */
2059 if (ata_msg_drv(ap) && print_info)
2060 ata_dev_printk(dev, KERN_INFO,
2061 "ATAPI: %s, %s, max %s%s%s\n",
2062 modelbuf, fwrevbuf,
2063 ata_mode_string(xfer_mask),
2064 cdb_intr_string, atapi_an_string);
2067 /* determine max_sectors */
2068 dev->max_sectors = ATA_MAX_SECTORS;
2069 if (dev->flags & ATA_DFLAG_LBA48)
2070 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2072 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2073 /* Let the user know. We don't want to disallow opens for
2074 rescue purposes, or in case the vendor is just a blithering
2075 idiot */
2076 if (print_info) {
2077 ata_dev_printk(dev, KERN_WARNING,
2078 "Drive reports diagnostics failure. This may indicate a drive\n");
2079 ata_dev_printk(dev, KERN_WARNING,
2080 "fault or invalid emulation. Contact drive vendor for information.\n");
2084 /* limit bridge transfers to udma5, 200 sectors */
2085 if (ata_dev_knobble(dev)) {
2086 if (ata_msg_drv(ap) && print_info)
2087 ata_dev_printk(dev, KERN_INFO,
2088 "applying bridge limits\n");
2089 dev->udma_mask &= ATA_UDMA5;
2090 dev->max_sectors = ATA_MAX_SECTORS;
2093 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
2094 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2095 dev->max_sectors);
2097 if (ap->ops->dev_config)
2098 ap->ops->dev_config(dev);
2100 if (ata_msg_probe(ap))
2101 ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
2102 __FUNCTION__, ata_chk_status(ap));
2103 return 0;
2105 err_out_nosup:
2106 if (ata_msg_probe(ap))
2107 ata_dev_printk(dev, KERN_DEBUG,
2108 "%s: EXIT, err\n", __FUNCTION__);
2109 return rc;
2113 * ata_cable_40wire - return 40 wire cable type
2114 * @ap: port
2116 * Helper method for drivers which want to hardwire 40 wire cable
2117 * detection.
2120 int ata_cable_40wire(struct ata_port *ap)
2122 return ATA_CBL_PATA40;
2126 * ata_cable_80wire - return 80 wire cable type
2127 * @ap: port
2129 * Helper method for drivers which want to hardwire 80 wire cable
2130 * detection.
2133 int ata_cable_80wire(struct ata_port *ap)
2135 return ATA_CBL_PATA80;
2139 * ata_cable_unknown - return unknown PATA cable.
2140 * @ap: port
2142 * Helper method for drivers which have no PATA cable detection.
2145 int ata_cable_unknown(struct ata_port *ap)
2147 return ATA_CBL_PATA_UNK;
2151 * ata_cable_sata - return SATA cable type
2152 * @ap: port
2154 * Helper method for drivers which have SATA cables
2157 int ata_cable_sata(struct ata_port *ap)
2159 return ATA_CBL_SATA;
2163 * ata_bus_probe - Reset and probe ATA bus
2164 * @ap: Bus to probe
2166 * Master ATA bus probing function. Initiates a hardware-dependent
2167 * bus reset, then attempts to identify any devices found on
2168 * the bus.
2170 * LOCKING:
2171 * PCI/etc. bus probe sem.
2173 * RETURNS:
2174 * Zero on success, negative errno otherwise.
2177 int ata_bus_probe(struct ata_port *ap)
2179 unsigned int classes[ATA_MAX_DEVICES];
2180 int tries[ATA_MAX_DEVICES];
2181 int rc;
2182 struct ata_device *dev;
2184 ata_port_probe(ap);
2186 ata_link_for_each_dev(dev, &ap->link)
2187 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2189 retry:
2190 /* reset and determine device classes */
2191 ap->ops->phy_reset(ap);
2193 ata_link_for_each_dev(dev, &ap->link) {
2194 if (!(ap->flags & ATA_FLAG_DISABLED) &&
2195 dev->class != ATA_DEV_UNKNOWN)
2196 classes[dev->devno] = dev->class;
2197 else
2198 classes[dev->devno] = ATA_DEV_NONE;
2200 dev->class = ATA_DEV_UNKNOWN;
2203 ata_port_probe(ap);
2205 /* after the reset the device state is PIO 0 and the controller
2206 state is undefined. Record the mode */
2208 ata_link_for_each_dev(dev, &ap->link)
2209 dev->pio_mode = XFER_PIO_0;
2211 /* read IDENTIFY page and configure devices. We have to do the identify
2212 specific sequence bass-ackwards so that PDIAG- is released by
2213 the slave device */
2215 ata_link_for_each_dev(dev, &ap->link) {
2216 if (tries[dev->devno])
2217 dev->class = classes[dev->devno];
2219 if (!ata_dev_enabled(dev))
2220 continue;
2222 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2223 dev->id);
2224 if (rc)
2225 goto fail;
2228 /* Now ask for the cable type as PDIAG- should have been released */
2229 if (ap->ops->cable_detect)
2230 ap->cbl = ap->ops->cable_detect(ap);
2232 /* We may have SATA bridge glue hiding here irrespective of the
2233 reported cable types and sensed types */
2234 ata_link_for_each_dev(dev, &ap->link) {
2235 if (!ata_dev_enabled(dev))
2236 continue;
2237 /* SATA drives indicate we have a bridge. We don't know which
2238 end of the link the bridge is which is a problem */
2239 if (ata_id_is_sata(dev->id))
2240 ap->cbl = ATA_CBL_SATA;
2243 /* After the identify sequence we can now set up the devices. We do
2244 this in the normal order so that the user doesn't get confused */
2246 ata_link_for_each_dev(dev, &ap->link) {
2247 if (!ata_dev_enabled(dev))
2248 continue;
2250 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2251 rc = ata_dev_configure(dev);
2252 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2253 if (rc)
2254 goto fail;
2257 /* configure transfer mode */
2258 rc = ata_set_mode(&ap->link, &dev);
2259 if (rc)
2260 goto fail;
2262 ata_link_for_each_dev(dev, &ap->link)
2263 if (ata_dev_enabled(dev))
2264 return 0;
2266 /* no device present, disable port */
2267 ata_port_disable(ap);
2268 return -ENODEV;
2270 fail:
2271 tries[dev->devno]--;
2273 switch (rc) {
2274 case -EINVAL:
2275 /* eeek, something went very wrong, give up */
2276 tries[dev->devno] = 0;
2277 break;
2279 case -ENODEV:
2280 /* give it just one more chance */
2281 tries[dev->devno] = min(tries[dev->devno], 1);
2282 case -EIO:
2283 if (tries[dev->devno] == 1) {
2284 /* This is the last chance, better to slow
2285 * down than lose it.
2287 sata_down_spd_limit(&ap->link);
2288 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2292 if (!tries[dev->devno])
2293 ata_dev_disable(dev);
2295 goto retry;
2299 * ata_port_probe - Mark port as enabled
2300 * @ap: Port for which we indicate enablement
2302 * Modify @ap data structure such that the system
2303 * thinks that the entire port is enabled.
2305 * LOCKING: host lock, or some other form of
2306 * serialization.
2309 void ata_port_probe(struct ata_port *ap)
2311 ap->flags &= ~ATA_FLAG_DISABLED;
2315 * sata_print_link_status - Print SATA link status
2316 * @link: SATA link to printk link status about
2318 * This function prints link speed and status of a SATA link.
2320 * LOCKING:
2321 * None.
2323 void sata_print_link_status(struct ata_link *link)
2325 u32 sstatus, scontrol, tmp;
2327 if (sata_scr_read(link, SCR_STATUS, &sstatus))
2328 return;
2329 sata_scr_read(link, SCR_CONTROL, &scontrol);
2331 if (ata_link_online(link)) {
2332 tmp = (sstatus >> 4) & 0xf;
2333 ata_link_printk(link, KERN_INFO,
2334 "SATA link up %s (SStatus %X SControl %X)\n",
2335 sata_spd_string(tmp), sstatus, scontrol);
2336 } else {
2337 ata_link_printk(link, KERN_INFO,
2338 "SATA link down (SStatus %X SControl %X)\n",
2339 sstatus, scontrol);
2344 * __sata_phy_reset - Wake/reset a low-level SATA PHY
2345 * @ap: SATA port associated with target SATA PHY.
2347 * This function issues commands to standard SATA Sxxx
2348 * PHY registers, to wake up the phy (and device), and
2349 * clear any reset condition.
2351 * LOCKING:
2352 * PCI/etc. bus probe sem.
2355 void __sata_phy_reset(struct ata_port *ap)
2357 struct ata_link *link = &ap->link;
2358 unsigned long timeout = jiffies + (HZ * 5);
2359 u32 sstatus;
2361 if (ap->flags & ATA_FLAG_SATA_RESET) {
2362 /* issue phy wake/reset */
2363 sata_scr_write_flush(link, SCR_CONTROL, 0x301);
2364 /* Couldn't find anything in SATA I/II specs, but
2365 * AHCI-1.1 10.4.2 says at least 1 ms. */
2366 mdelay(1);
2368 /* phy wake/clear reset */
2369 sata_scr_write_flush(link, SCR_CONTROL, 0x300);
2371 /* wait for phy to become ready, if necessary */
2372 do {
2373 msleep(200);
2374 sata_scr_read(link, SCR_STATUS, &sstatus);
2375 if ((sstatus & 0xf) != 1)
2376 break;
2377 } while (time_before(jiffies, timeout));
2379 /* print link status */
2380 sata_print_link_status(link);
2382 /* TODO: phy layer with polling, timeouts, etc. */
2383 if (!ata_link_offline(link))
2384 ata_port_probe(ap);
2385 else
2386 ata_port_disable(ap);
2388 if (ap->flags & ATA_FLAG_DISABLED)
2389 return;
2391 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
2392 ata_port_disable(ap);
2393 return;
2396 ap->cbl = ATA_CBL_SATA;
2400 * sata_phy_reset - Reset SATA bus.
2401 * @ap: SATA port associated with target SATA PHY.
2403 * This function resets the SATA bus, and then probes
2404 * the bus for devices.
2406 * LOCKING:
2407 * PCI/etc. bus probe sem.
2410 void sata_phy_reset(struct ata_port *ap)
2412 __sata_phy_reset(ap);
2413 if (ap->flags & ATA_FLAG_DISABLED)
2414 return;
2415 ata_bus_reset(ap);
2419 * ata_dev_pair - return other device on cable
2420 * @adev: device
2422 * Obtain the other device on the same cable, or if none is
2423 * present NULL is returned
2426 struct ata_device *ata_dev_pair(struct ata_device *adev)
2428 struct ata_link *link = adev->link;
2429 struct ata_device *pair = &link->device[1 - adev->devno];
2430 if (!ata_dev_enabled(pair))
2431 return NULL;
2432 return pair;
2436 * ata_port_disable - Disable port.
2437 * @ap: Port to be disabled.
2439 * Modify @ap data structure such that the system
2440 * thinks that the entire port is disabled, and should
2441 * never attempt to probe or communicate with devices
2442 * on this port.
2444 * LOCKING: host lock, or some other form of
2445 * serialization.
2448 void ata_port_disable(struct ata_port *ap)
2450 ap->link.device[0].class = ATA_DEV_NONE;
2451 ap->link.device[1].class = ATA_DEV_NONE;
2452 ap->flags |= ATA_FLAG_DISABLED;
2456 * sata_down_spd_limit - adjust SATA spd limit downward
2457 * @link: Link to adjust SATA spd limit for
2459 * Adjust SATA spd limit of @link downward. Note that this
2460 * function only adjusts the limit. The change must be applied
2461 * using sata_set_spd().
2463 * LOCKING:
2464 * Inherited from caller.
2466 * RETURNS:
2467 * 0 on success, negative errno on failure
2469 int sata_down_spd_limit(struct ata_link *link)
2471 u32 sstatus, spd, mask;
2472 int rc, highbit;
2474 if (!sata_scr_valid(link))
2475 return -EOPNOTSUPP;
2477 /* If SCR can be read, use it to determine the current SPD.
2478 * If not, use cached value in link->sata_spd.
2480 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
2481 if (rc == 0)
2482 spd = (sstatus >> 4) & 0xf;
2483 else
2484 spd = link->sata_spd;
2486 mask = link->sata_spd_limit;
2487 if (mask <= 1)
2488 return -EINVAL;
2490 /* unconditionally mask off the highest bit */
2491 highbit = fls(mask) - 1;
2492 mask &= ~(1 << highbit);
2494 /* Mask off all speeds higher than or equal to the current
2495 * one. Force 1.5Gbps if current SPD is not available.
2497 if (spd > 1)
2498 mask &= (1 << (spd - 1)) - 1;
2499 else
2500 mask &= 1;
2502 /* were we already at the bottom? */
2503 if (!mask)
2504 return -EINVAL;
2506 link->sata_spd_limit = mask;
2508 ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
2509 sata_spd_string(fls(mask)));
2511 return 0;
2514 static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
2516 u32 spd, limit;
2518 if (link->sata_spd_limit == UINT_MAX)
2519 limit = 0;
2520 else
2521 limit = fls(link->sata_spd_limit);
2523 spd = (*scontrol >> 4) & 0xf;
2524 *scontrol = (*scontrol & ~0xf0) | ((limit & 0xf) << 4);
2526 return spd != limit;
2530 * sata_set_spd_needed - is SATA spd configuration needed
2531 * @link: Link in question
2533 * Test whether the spd limit in SControl matches
2534 * @link->sata_spd_limit. This function is used to determine
2535 * whether hardreset is necessary to apply SATA spd
2536 * configuration.
2538 * LOCKING:
2539 * Inherited from caller.
2541 * RETURNS:
2542 * 1 if SATA spd configuration is needed, 0 otherwise.
2544 int sata_set_spd_needed(struct ata_link *link)
2546 u32 scontrol;
2548 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
2549 return 0;
2551 return __sata_set_spd_needed(link, &scontrol);
2555 * sata_set_spd - set SATA spd according to spd limit
2556 * @link: Link to set SATA spd for
2558 * Set SATA spd of @link according to sata_spd_limit.
2560 * LOCKING:
2561 * Inherited from caller.
2563 * RETURNS:
2564 * 0 if spd doesn't need to be changed, 1 if spd has been
2565 * changed. Negative errno if SCR registers are inaccessible.
2567 int sata_set_spd(struct ata_link *link)
2569 u32 scontrol;
2570 int rc;
2572 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
2573 return rc;
2575 if (!__sata_set_spd_needed(link, &scontrol))
2576 return 0;
2578 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
2579 return rc;
2581 return 1;
2585 * This mode timing computation functionality is ported over from
2586 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
2589 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
2590 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
2591 * for UDMA6, which is currently supported only by Maxtor drives.
2593 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
2596 static const struct ata_timing ata_timing[] = {
2598 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
2599 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
2600 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
2601 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
2603 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
2604 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
2605 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
2606 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
2607 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
2609 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
2611 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
2612 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
2613 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
2615 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
2616 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
2617 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
2619 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
2620 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
2621 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
2622 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
2624 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
2625 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
2626 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
2628 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
2630 { 0xFF }
2633 #define ENOUGH(v,unit) (((v)-1)/(unit)+1)
2634 #define EZ(v,unit) ((v)?ENOUGH(v,unit):0)
2636 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
2638 q->setup = EZ(t->setup * 1000, T);
2639 q->act8b = EZ(t->act8b * 1000, T);
2640 q->rec8b = EZ(t->rec8b * 1000, T);
2641 q->cyc8b = EZ(t->cyc8b * 1000, T);
2642 q->active = EZ(t->active * 1000, T);
2643 q->recover = EZ(t->recover * 1000, T);
2644 q->cycle = EZ(t->cycle * 1000, T);
2645 q->udma = EZ(t->udma * 1000, UT);
2648 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2649 struct ata_timing *m, unsigned int what)
2651 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
2652 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
2653 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
2654 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
2655 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
2656 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
2657 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
2658 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
2661 static const struct ata_timing* ata_timing_find_mode(unsigned short speed)
2663 const struct ata_timing *t;
2665 for (t = ata_timing; t->mode != speed; t++)
2666 if (t->mode == 0xFF)
2667 return NULL;
2668 return t;
2671 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
2672 struct ata_timing *t, int T, int UT)
2674 const struct ata_timing *s;
2675 struct ata_timing p;
2678 * Find the mode.
2681 if (!(s = ata_timing_find_mode(speed)))
2682 return -EINVAL;
2684 memcpy(t, s, sizeof(*s));
2687 * If the drive is an EIDE drive, it can tell us it needs extended
2688 * PIO/MW_DMA cycle timing.
2691 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
2692 memset(&p, 0, sizeof(p));
2693 if(speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
2694 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
2695 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
2696 } else if(speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
2697 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
2699 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
2703 * Convert the timing to bus clock counts.
2706 ata_timing_quantize(t, t, T, UT);
2709 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
2710 * S.M.A.R.T * and some other commands. We have to ensure that the
2711 * DMA cycle timing is slower/equal than the fastest PIO timing.
2714 if (speed > XFER_PIO_6) {
2715 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
2716 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
2720 * Lengthen active & recovery time so that cycle time is correct.
2723 if (t->act8b + t->rec8b < t->cyc8b) {
2724 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
2725 t->rec8b = t->cyc8b - t->act8b;
2728 if (t->active + t->recover < t->cycle) {
2729 t->active += (t->cycle - (t->active + t->recover)) / 2;
2730 t->recover = t->cycle - t->active;
2733 /* In a few cases quantisation may produce enough errors to
2734 leave t->cycle too low for the sum of active and recovery
2735 if so we must correct this */
2736 if (t->active + t->recover > t->cycle)
2737 t->cycle = t->active + t->recover;
2739 return 0;
2743 * ata_down_xfermask_limit - adjust dev xfer masks downward
2744 * @dev: Device to adjust xfer masks
2745 * @sel: ATA_DNXFER_* selector
2747 * Adjust xfer masks of @dev downward. Note that this function
2748 * does not apply the change. Invoking ata_set_mode() afterwards
2749 * will apply the limit.
2751 * LOCKING:
2752 * Inherited from caller.
2754 * RETURNS:
2755 * 0 on success, negative errno on failure
2757 int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
2759 char buf[32];
2760 unsigned int orig_mask, xfer_mask;
2761 unsigned int pio_mask, mwdma_mask, udma_mask;
2762 int quiet, highbit;
2764 quiet = !!(sel & ATA_DNXFER_QUIET);
2765 sel &= ~ATA_DNXFER_QUIET;
2767 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
2768 dev->mwdma_mask,
2769 dev->udma_mask);
2770 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
2772 switch (sel) {
2773 case ATA_DNXFER_PIO:
2774 highbit = fls(pio_mask) - 1;
2775 pio_mask &= ~(1 << highbit);
2776 break;
2778 case ATA_DNXFER_DMA:
2779 if (udma_mask) {
2780 highbit = fls(udma_mask) - 1;
2781 udma_mask &= ~(1 << highbit);
2782 if (!udma_mask)
2783 return -ENOENT;
2784 } else if (mwdma_mask) {
2785 highbit = fls(mwdma_mask) - 1;
2786 mwdma_mask &= ~(1 << highbit);
2787 if (!mwdma_mask)
2788 return -ENOENT;
2790 break;
2792 case ATA_DNXFER_40C:
2793 udma_mask &= ATA_UDMA_MASK_40C;
2794 break;
2796 case ATA_DNXFER_FORCE_PIO0:
2797 pio_mask &= 1;
2798 case ATA_DNXFER_FORCE_PIO:
2799 mwdma_mask = 0;
2800 udma_mask = 0;
2801 break;
2803 default:
2804 BUG();
2807 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
2809 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
2810 return -ENOENT;
2812 if (!quiet) {
2813 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
2814 snprintf(buf, sizeof(buf), "%s:%s",
2815 ata_mode_string(xfer_mask),
2816 ata_mode_string(xfer_mask & ATA_MASK_PIO));
2817 else
2818 snprintf(buf, sizeof(buf), "%s",
2819 ata_mode_string(xfer_mask));
2821 ata_dev_printk(dev, KERN_WARNING,
2822 "limiting speed to %s\n", buf);
2825 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
2826 &dev->udma_mask);
2828 return 0;
2831 static int ata_dev_set_mode(struct ata_device *dev)
2833 struct ata_eh_context *ehc = &dev->link->eh_context;
2834 unsigned int err_mask;
2835 int rc;
2837 dev->flags &= ~ATA_DFLAG_PIO;
2838 if (dev->xfer_shift == ATA_SHIFT_PIO)
2839 dev->flags |= ATA_DFLAG_PIO;
2841 err_mask = ata_dev_set_xfermode(dev);
2842 /* Old CFA may refuse this command, which is just fine */
2843 if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
2844 err_mask &= ~AC_ERR_DEV;
2845 /* Some very old devices and some bad newer ones fail any kind of
2846 SET_XFERMODE request but support PIO0-2 timings and no IORDY */
2847 if (dev->xfer_shift == ATA_SHIFT_PIO && !ata_id_has_iordy(dev->id) &&
2848 dev->pio_mode <= XFER_PIO_2)
2849 err_mask &= ~AC_ERR_DEV;
2850 if (err_mask) {
2851 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
2852 "(err_mask=0x%x)\n", err_mask);
2853 return -EIO;
2856 ehc->i.flags |= ATA_EHI_POST_SETMODE;
2857 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
2858 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
2859 if (rc)
2860 return rc;
2862 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
2863 dev->xfer_shift, (int)dev->xfer_mode);
2865 ata_dev_printk(dev, KERN_INFO, "configured for %s\n",
2866 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)));
2867 return 0;
2871 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
2872 * @link: link on which timings will be programmed
2873 * @r_failed_dev: out paramter for failed device
2875 * Standard implementation of the function used to tune and set
2876 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2877 * ata_dev_set_mode() fails, pointer to the failing device is
2878 * returned in @r_failed_dev.
2880 * LOCKING:
2881 * PCI/etc. bus probe sem.
2883 * RETURNS:
2884 * 0 on success, negative errno otherwise
2887 int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
2889 struct ata_port *ap = link->ap;
2890 struct ata_device *dev;
2891 int rc = 0, used_dma = 0, found = 0;
2893 /* step 1: calculate xfer_mask */
2894 ata_link_for_each_dev(dev, link) {
2895 unsigned int pio_mask, dma_mask;
2897 if (!ata_dev_enabled(dev))
2898 continue;
2900 ata_dev_xfermask(dev);
2902 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
2903 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
2904 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
2905 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
2907 found = 1;
2908 if (dev->dma_mode)
2909 used_dma = 1;
2911 if (!found)
2912 goto out;
2914 /* step 2: always set host PIO timings */
2915 ata_link_for_each_dev(dev, link) {
2916 if (!ata_dev_enabled(dev))
2917 continue;
2919 if (!dev->pio_mode) {
2920 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
2921 rc = -EINVAL;
2922 goto out;
2925 dev->xfer_mode = dev->pio_mode;
2926 dev->xfer_shift = ATA_SHIFT_PIO;
2927 if (ap->ops->set_piomode)
2928 ap->ops->set_piomode(ap, dev);
2931 /* step 3: set host DMA timings */
2932 ata_link_for_each_dev(dev, link) {
2933 if (!ata_dev_enabled(dev) || !dev->dma_mode)
2934 continue;
2936 dev->xfer_mode = dev->dma_mode;
2937 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
2938 if (ap->ops->set_dmamode)
2939 ap->ops->set_dmamode(ap, dev);
2942 /* step 4: update devices' xfer mode */
2943 ata_link_for_each_dev(dev, link) {
2944 /* don't update suspended devices' xfer mode */
2945 if (!ata_dev_enabled(dev))
2946 continue;
2948 rc = ata_dev_set_mode(dev);
2949 if (rc)
2950 goto out;
2953 /* Record simplex status. If we selected DMA then the other
2954 * host channels are not permitted to do so.
2956 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
2957 ap->host->simplex_claimed = ap;
2959 out:
2960 if (rc)
2961 *r_failed_dev = dev;
2962 return rc;
2966 * ata_set_mode - Program timings and issue SET FEATURES - XFER
2967 * @link: link on which timings will be programmed
2968 * @r_failed_dev: out paramter for failed device
2970 * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
2971 * ata_set_mode() fails, pointer to the failing device is
2972 * returned in @r_failed_dev.
2974 * LOCKING:
2975 * PCI/etc. bus probe sem.
2977 * RETURNS:
2978 * 0 on success, negative errno otherwise
2980 int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
2982 struct ata_port *ap = link->ap;
2984 /* has private set_mode? */
2985 if (ap->ops->set_mode)
2986 return ap->ops->set_mode(link, r_failed_dev);
2987 return ata_do_set_mode(link, r_failed_dev);
2991 * ata_tf_to_host - issue ATA taskfile to host controller
2992 * @ap: port to which command is being issued
2993 * @tf: ATA taskfile register set
2995 * Issues ATA taskfile register set to ATA host controller,
2996 * with proper synchronization with interrupt handler and
2997 * other threads.
2999 * LOCKING:
3000 * spin_lock_irqsave(host lock)
3003 static inline void ata_tf_to_host(struct ata_port *ap,
3004 const struct ata_taskfile *tf)
3006 ap->ops->tf_load(ap, tf);
3007 ap->ops->exec_command(ap, tf);
3011 * ata_busy_sleep - sleep until BSY clears, or timeout
3012 * @ap: port containing status register to be polled
3013 * @tmout_pat: impatience timeout
3014 * @tmout: overall timeout
3016 * Sleep until ATA Status register bit BSY clears,
3017 * or a timeout occurs.
3019 * LOCKING:
3020 * Kernel thread context (may sleep).
3022 * RETURNS:
3023 * 0 on success, -errno otherwise.
3025 int ata_busy_sleep(struct ata_port *ap,
3026 unsigned long tmout_pat, unsigned long tmout)
3028 unsigned long timer_start, timeout;
3029 u8 status;
3031 status = ata_busy_wait(ap, ATA_BUSY, 300);
3032 timer_start = jiffies;
3033 timeout = timer_start + tmout_pat;
3034 while (status != 0xff && (status & ATA_BUSY) &&
3035 time_before(jiffies, timeout)) {
3036 msleep(50);
3037 status = ata_busy_wait(ap, ATA_BUSY, 3);
3040 if (status != 0xff && (status & ATA_BUSY))
3041 ata_port_printk(ap, KERN_WARNING,
3042 "port is slow to respond, please be patient "
3043 "(Status 0x%x)\n", status);
3045 timeout = timer_start + tmout;
3046 while (status != 0xff && (status & ATA_BUSY) &&
3047 time_before(jiffies, timeout)) {
3048 msleep(50);
3049 status = ata_chk_status(ap);
3052 if (status == 0xff)
3053 return -ENODEV;
3055 if (status & ATA_BUSY) {
3056 ata_port_printk(ap, KERN_ERR, "port failed to respond "
3057 "(%lu secs, Status 0x%x)\n",
3058 tmout / HZ, status);
3059 return -EBUSY;
3062 return 0;
3066 * ata_wait_ready - sleep until BSY clears, or timeout
3067 * @ap: port containing status register to be polled
3068 * @deadline: deadline jiffies for the operation
3070 * Sleep until ATA Status register bit BSY clears, or timeout
3071 * occurs.
3073 * LOCKING:
3074 * Kernel thread context (may sleep).
3076 * RETURNS:
3077 * 0 on success, -errno otherwise.
3079 int ata_wait_ready(struct ata_port *ap, unsigned long deadline)
3081 unsigned long start = jiffies;
3082 int warned = 0;
3084 while (1) {
3085 u8 status = ata_chk_status(ap);
3086 unsigned long now = jiffies;
3088 if (!(status & ATA_BUSY))
3089 return 0;
3090 if (!ata_link_online(&ap->link) && status == 0xff)
3091 return -ENODEV;
3092 if (time_after(now, deadline))
3093 return -EBUSY;
3095 if (!warned && time_after(now, start + 5 * HZ) &&
3096 (deadline - now > 3 * HZ)) {
3097 ata_port_printk(ap, KERN_WARNING,
3098 "port is slow to respond, please be patient "
3099 "(Status 0x%x)\n", status);
3100 warned = 1;
3103 msleep(50);
3107 static int ata_bus_post_reset(struct ata_port *ap, unsigned int devmask,
3108 unsigned long deadline)
3110 struct ata_ioports *ioaddr = &ap->ioaddr;
3111 unsigned int dev0 = devmask & (1 << 0);
3112 unsigned int dev1 = devmask & (1 << 1);
3113 int rc, ret = 0;
3115 /* if device 0 was found in ata_devchk, wait for its
3116 * BSY bit to clear
3118 if (dev0) {
3119 rc = ata_wait_ready(ap, deadline);
3120 if (rc) {
3121 if (rc != -ENODEV)
3122 return rc;
3123 ret = rc;
3127 /* if device 1 was found in ata_devchk, wait for register
3128 * access briefly, then wait for BSY to clear.
3130 if (dev1) {
3131 int i;
3133 ap->ops->dev_select(ap, 1);
3135 /* Wait for register access. Some ATAPI devices fail
3136 * to set nsect/lbal after reset, so don't waste too
3137 * much time on it. We're gonna wait for !BSY anyway.
3139 for (i = 0; i < 2; i++) {
3140 u8 nsect, lbal;
3142 nsect = ioread8(ioaddr->nsect_addr);
3143 lbal = ioread8(ioaddr->lbal_addr);
3144 if ((nsect == 1) && (lbal == 1))
3145 break;
3146 msleep(50); /* give drive a breather */
3149 rc = ata_wait_ready(ap, deadline);
3150 if (rc) {
3151 if (rc != -ENODEV)
3152 return rc;
3153 ret = rc;
3157 /* is all this really necessary? */
3158 ap->ops->dev_select(ap, 0);
3159 if (dev1)
3160 ap->ops->dev_select(ap, 1);
3161 if (dev0)
3162 ap->ops->dev_select(ap, 0);
3164 return ret;
3167 static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
3168 unsigned long deadline)
3170 struct ata_ioports *ioaddr = &ap->ioaddr;
3172 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
3174 /* software reset. causes dev0 to be selected */
3175 iowrite8(ap->ctl, ioaddr->ctl_addr);
3176 udelay(20); /* FIXME: flush */
3177 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
3178 udelay(20); /* FIXME: flush */
3179 iowrite8(ap->ctl, ioaddr->ctl_addr);
3181 /* spec mandates ">= 2ms" before checking status.
3182 * We wait 150ms, because that was the magic delay used for
3183 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
3184 * between when the ATA command register is written, and then
3185 * status is checked. Because waiting for "a while" before
3186 * checking status is fine, post SRST, we perform this magic
3187 * delay here as well.
3189 * Old drivers/ide uses the 2mS rule and then waits for ready
3191 msleep(150);
3193 /* Before we perform post reset processing we want to see if
3194 * the bus shows 0xFF because the odd clown forgets the D7
3195 * pulldown resistor.
3197 if (ata_check_status(ap) == 0xFF)
3198 return -ENODEV;
3200 return ata_bus_post_reset(ap, devmask, deadline);
3204 * ata_bus_reset - reset host port and associated ATA channel
3205 * @ap: port to reset
3207 * This is typically the first time we actually start issuing
3208 * commands to the ATA channel. We wait for BSY to clear, then
3209 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
3210 * result. Determine what devices, if any, are on the channel
3211 * by looking at the device 0/1 error register. Look at the signature
3212 * stored in each device's taskfile registers, to determine if
3213 * the device is ATA or ATAPI.
3215 * LOCKING:
3216 * PCI/etc. bus probe sem.
3217 * Obtains host lock.
3219 * SIDE EFFECTS:
3220 * Sets ATA_FLAG_DISABLED if bus reset fails.
3223 void ata_bus_reset(struct ata_port *ap)
3225 struct ata_device *device = ap->link.device;
3226 struct ata_ioports *ioaddr = &ap->ioaddr;
3227 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
3228 u8 err;
3229 unsigned int dev0, dev1 = 0, devmask = 0;
3230 int rc;
3232 DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
3234 /* determine if device 0/1 are present */
3235 if (ap->flags & ATA_FLAG_SATA_RESET)
3236 dev0 = 1;
3237 else {
3238 dev0 = ata_devchk(ap, 0);
3239 if (slave_possible)
3240 dev1 = ata_devchk(ap, 1);
3243 if (dev0)
3244 devmask |= (1 << 0);
3245 if (dev1)
3246 devmask |= (1 << 1);
3248 /* select device 0 again */
3249 ap->ops->dev_select(ap, 0);
3251 /* issue bus reset */
3252 if (ap->flags & ATA_FLAG_SRST) {
3253 rc = ata_bus_softreset(ap, devmask, jiffies + 40 * HZ);
3254 if (rc && rc != -ENODEV)
3255 goto err_out;
3259 * determine by signature whether we have ATA or ATAPI devices
3261 device[0].class = ata_dev_try_classify(&device[0], dev0, &err);
3262 if ((slave_possible) && (err != 0x81))
3263 device[1].class = ata_dev_try_classify(&device[1], dev1, &err);
3265 /* is double-select really necessary? */
3266 if (device[1].class != ATA_DEV_NONE)
3267 ap->ops->dev_select(ap, 1);
3268 if (device[0].class != ATA_DEV_NONE)
3269 ap->ops->dev_select(ap, 0);
3271 /* if no devices were detected, disable this port */
3272 if ((device[0].class == ATA_DEV_NONE) &&
3273 (device[1].class == ATA_DEV_NONE))
3274 goto err_out;
3276 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
3277 /* set up device control for ATA_FLAG_SATA_RESET */
3278 iowrite8(ap->ctl, ioaddr->ctl_addr);
3281 DPRINTK("EXIT\n");
3282 return;
3284 err_out:
3285 ata_port_printk(ap, KERN_ERR, "disabling port\n");
3286 ata_port_disable(ap);
3288 DPRINTK("EXIT\n");
3292 * sata_link_debounce - debounce SATA phy status
3293 * @link: ATA link to debounce SATA phy status for
3294 * @params: timing parameters { interval, duratinon, timeout } in msec
3295 * @deadline: deadline jiffies for the operation
3297 * Make sure SStatus of @link reaches stable state, determined by
3298 * holding the same value where DET is not 1 for @duration polled
3299 * every @interval, before @timeout. Timeout constraints the
3300 * beginning of the stable state. Because DET gets stuck at 1 on
3301 * some controllers after hot unplugging, this functions waits
3302 * until timeout then returns 0 if DET is stable at 1.
3304 * @timeout is further limited by @deadline. The sooner of the
3305 * two is used.
3307 * LOCKING:
3308 * Kernel thread context (may sleep)
3310 * RETURNS:
3311 * 0 on success, -errno on failure.
3313 int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3314 unsigned long deadline)
3316 unsigned long interval_msec = params[0];
3317 unsigned long duration = msecs_to_jiffies(params[1]);
3318 unsigned long last_jiffies, t;
3319 u32 last, cur;
3320 int rc;
3322 t = jiffies + msecs_to_jiffies(params[2]);
3323 if (time_before(t, deadline))
3324 deadline = t;
3326 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3327 return rc;
3328 cur &= 0xf;
3330 last = cur;
3331 last_jiffies = jiffies;
3333 while (1) {
3334 msleep(interval_msec);
3335 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3336 return rc;
3337 cur &= 0xf;
3339 /* DET stable? */
3340 if (cur == last) {
3341 if (cur == 1 && time_before(jiffies, deadline))
3342 continue;
3343 if (time_after(jiffies, last_jiffies + duration))
3344 return 0;
3345 continue;
3348 /* unstable, start over */
3349 last = cur;
3350 last_jiffies = jiffies;
3352 /* Check deadline. If debouncing failed, return
3353 * -EPIPE to tell upper layer to lower link speed.
3355 if (time_after(jiffies, deadline))
3356 return -EPIPE;
3361 * sata_link_resume - resume SATA link
3362 * @link: ATA link to resume SATA
3363 * @params: timing parameters { interval, duratinon, timeout } in msec
3364 * @deadline: deadline jiffies for the operation
3366 * Resume SATA phy @link and debounce it.
3368 * LOCKING:
3369 * Kernel thread context (may sleep)
3371 * RETURNS:
3372 * 0 on success, -errno on failure.
3374 int sata_link_resume(struct ata_link *link, const unsigned long *params,
3375 unsigned long deadline)
3377 u32 scontrol;
3378 int rc;
3380 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3381 return rc;
3383 scontrol = (scontrol & 0x0f0) | 0x300;
3385 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3386 return rc;
3388 /* Some PHYs react badly if SStatus is pounded immediately
3389 * after resuming. Delay 200ms before debouncing.
3391 msleep(200);
3393 return sata_link_debounce(link, params, deadline);
3397 * ata_std_prereset - prepare for reset
3398 * @link: ATA link to be reset
3399 * @deadline: deadline jiffies for the operation
3401 * @link is about to be reset. Initialize it. Failure from
3402 * prereset makes libata abort whole reset sequence and give up
3403 * that port, so prereset should be best-effort. It does its
3404 * best to prepare for reset sequence but if things go wrong, it
3405 * should just whine, not fail.
3407 * LOCKING:
3408 * Kernel thread context (may sleep)
3410 * RETURNS:
3411 * 0 on success, -errno otherwise.
3413 int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3415 struct ata_port *ap = link->ap;
3416 struct ata_eh_context *ehc = &link->eh_context;
3417 const unsigned long *timing = sata_ehc_deb_timing(ehc);
3418 int rc;
3420 /* handle link resume */
3421 if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
3422 (link->flags & ATA_LFLAG_HRST_TO_RESUME))
3423 ehc->i.action |= ATA_EH_HARDRESET;
3425 /* if we're about to do hardreset, nothing more to do */
3426 if (ehc->i.action & ATA_EH_HARDRESET)
3427 return 0;
3429 /* if SATA, resume link */
3430 if (ap->flags & ATA_FLAG_SATA) {
3431 rc = sata_link_resume(link, timing, deadline);
3432 /* whine about phy resume failure but proceed */
3433 if (rc && rc != -EOPNOTSUPP)
3434 ata_link_printk(link, KERN_WARNING, "failed to resume "
3435 "link for reset (errno=%d)\n", rc);
3438 /* Wait for !BSY if the controller can wait for the first D2H
3439 * Reg FIS and we don't know that no device is attached.
3441 if (!(link->flags & ATA_LFLAG_SKIP_D2H_BSY) && !ata_link_offline(link)) {
3442 rc = ata_wait_ready(ap, deadline);
3443 if (rc && rc != -ENODEV) {
3444 ata_link_printk(link, KERN_WARNING, "device not ready "
3445 "(errno=%d), forcing hardreset\n", rc);
3446 ehc->i.action |= ATA_EH_HARDRESET;
3450 return 0;
3454 * ata_std_softreset - reset host port via ATA SRST
3455 * @link: ATA link to reset
3456 * @classes: resulting classes of attached devices
3457 * @deadline: deadline jiffies for the operation
3459 * Reset host port using ATA SRST.
3461 * LOCKING:
3462 * Kernel thread context (may sleep)
3464 * RETURNS:
3465 * 0 on success, -errno otherwise.
3467 int ata_std_softreset(struct ata_link *link, unsigned int *classes,
3468 unsigned long deadline)
3470 struct ata_port *ap = link->ap;
3471 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
3472 unsigned int devmask = 0;
3473 int rc;
3474 u8 err;
3476 DPRINTK("ENTER\n");
3478 if (ata_link_offline(link)) {
3479 classes[0] = ATA_DEV_NONE;
3480 goto out;
3483 /* determine if device 0/1 are present */
3484 if (ata_devchk(ap, 0))
3485 devmask |= (1 << 0);
3486 if (slave_possible && ata_devchk(ap, 1))
3487 devmask |= (1 << 1);
3489 /* select device 0 again */
3490 ap->ops->dev_select(ap, 0);
3492 /* issue bus reset */
3493 DPRINTK("about to softreset, devmask=%x\n", devmask);
3494 rc = ata_bus_softreset(ap, devmask, deadline);
3495 /* if link is occupied, -ENODEV too is an error */
3496 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
3497 ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
3498 return rc;
3501 /* determine by signature whether we have ATA or ATAPI devices */
3502 classes[0] = ata_dev_try_classify(&link->device[0],
3503 devmask & (1 << 0), &err);
3504 if (slave_possible && err != 0x81)
3505 classes[1] = ata_dev_try_classify(&link->device[1],
3506 devmask & (1 << 1), &err);
3508 out:
3509 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
3510 return 0;
3514 * sata_link_hardreset - reset link via SATA phy reset
3515 * @link: link to reset
3516 * @timing: timing parameters { interval, duratinon, timeout } in msec
3517 * @deadline: deadline jiffies for the operation
3519 * SATA phy-reset @link using DET bits of SControl register.
3521 * LOCKING:
3522 * Kernel thread context (may sleep)
3524 * RETURNS:
3525 * 0 on success, -errno otherwise.
3527 int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
3528 unsigned long deadline)
3530 u32 scontrol;
3531 int rc;
3533 DPRINTK("ENTER\n");
3535 if (sata_set_spd_needed(link)) {
3536 /* SATA spec says nothing about how to reconfigure
3537 * spd. To be on the safe side, turn off phy during
3538 * reconfiguration. This works for at least ICH7 AHCI
3539 * and Sil3124.
3541 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3542 goto out;
3544 scontrol = (scontrol & 0x0f0) | 0x304;
3546 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3547 goto out;
3549 sata_set_spd(link);
3552 /* issue phy wake/reset */
3553 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3554 goto out;
3556 scontrol = (scontrol & 0x0f0) | 0x301;
3558 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
3559 goto out;
3561 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
3562 * 10.4.2 says at least 1 ms.
3564 msleep(1);
3566 /* bring link back */
3567 rc = sata_link_resume(link, timing, deadline);
3568 out:
3569 DPRINTK("EXIT, rc=%d\n", rc);
3570 return rc;
3574 * sata_std_hardreset - reset host port via SATA phy reset
3575 * @link: link to reset
3576 * @class: resulting class of attached device
3577 * @deadline: deadline jiffies for the operation
3579 * SATA phy-reset host port using DET bits of SControl register,
3580 * wait for !BSY and classify the attached device.
3582 * LOCKING:
3583 * Kernel thread context (may sleep)
3585 * RETURNS:
3586 * 0 on success, -errno otherwise.
3588 int sata_std_hardreset(struct ata_link *link, unsigned int *class,
3589 unsigned long deadline)
3591 struct ata_port *ap = link->ap;
3592 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
3593 int rc;
3595 DPRINTK("ENTER\n");
3597 /* do hardreset */
3598 rc = sata_link_hardreset(link, timing, deadline);
3599 if (rc) {
3600 ata_link_printk(link, KERN_ERR,
3601 "COMRESET failed (errno=%d)\n", rc);
3602 return rc;
3605 /* TODO: phy layer with polling, timeouts, etc. */
3606 if (ata_link_offline(link)) {
3607 *class = ATA_DEV_NONE;
3608 DPRINTK("EXIT, link offline\n");
3609 return 0;
3612 /* wait a while before checking status, see SRST for more info */
3613 msleep(150);
3615 rc = ata_wait_ready(ap, deadline);
3616 /* link occupied, -ENODEV too is an error */
3617 if (rc) {
3618 ata_link_printk(link, KERN_ERR,
3619 "COMRESET failed (errno=%d)\n", rc);
3620 return rc;
3623 ap->ops->dev_select(ap, 0); /* probably unnecessary */
3625 *class = ata_dev_try_classify(link->device, 1, NULL);
3627 DPRINTK("EXIT, class=%u\n", *class);
3628 return 0;
3632 * ata_std_postreset - standard postreset callback
3633 * @link: the target ata_link
3634 * @classes: classes of attached devices
3636 * This function is invoked after a successful reset. Note that
3637 * the device might have been reset more than once using
3638 * different reset methods before postreset is invoked.
3640 * LOCKING:
3641 * Kernel thread context (may sleep)
3643 void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3645 struct ata_port *ap = link->ap;
3646 u32 serror;
3648 DPRINTK("ENTER\n");
3650 /* print link status */
3651 sata_print_link_status(link);
3653 /* clear SError */
3654 if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
3655 sata_scr_write(link, SCR_ERROR, serror);
3657 /* is double-select really necessary? */
3658 if (classes[0] != ATA_DEV_NONE)
3659 ap->ops->dev_select(ap, 1);
3660 if (classes[1] != ATA_DEV_NONE)
3661 ap->ops->dev_select(ap, 0);
3663 /* bail out if no device is present */
3664 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
3665 DPRINTK("EXIT, no device\n");
3666 return;
3669 /* set up device control */
3670 if (ap->ioaddr.ctl_addr)
3671 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
3673 DPRINTK("EXIT\n");
3677 * ata_dev_same_device - Determine whether new ID matches configured device
3678 * @dev: device to compare against
3679 * @new_class: class of the new device
3680 * @new_id: IDENTIFY page of the new device
3682 * Compare @new_class and @new_id against @dev and determine
3683 * whether @dev is the device indicated by @new_class and
3684 * @new_id.
3686 * LOCKING:
3687 * None.
3689 * RETURNS:
3690 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3692 static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3693 const u16 *new_id)
3695 const u16 *old_id = dev->id;
3696 unsigned char model[2][ATA_ID_PROD_LEN + 1];
3697 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3699 if (dev->class != new_class) {
3700 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
3701 dev->class, new_class);
3702 return 0;
3705 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3706 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3707 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3708 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
3710 if (strcmp(model[0], model[1])) {
3711 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
3712 "'%s' != '%s'\n", model[0], model[1]);
3713 return 0;
3716 if (strcmp(serial[0], serial[1])) {
3717 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
3718 "'%s' != '%s'\n", serial[0], serial[1]);
3719 return 0;
3722 return 1;
3726 * ata_dev_reread_id - Re-read IDENTIFY data
3727 * @dev: target ATA device
3728 * @readid_flags: read ID flags
3730 * Re-read IDENTIFY page and make sure @dev is still attached to
3731 * the port.
3733 * LOCKING:
3734 * Kernel thread context (may sleep)
3736 * RETURNS:
3737 * 0 on success, negative errno otherwise
3739 int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
3741 unsigned int class = dev->class;
3742 u16 *id = (void *)dev->link->ap->sector_buf;
3743 int rc;
3745 /* read ID data */
3746 rc = ata_dev_read_id(dev, &class, readid_flags, id);
3747 if (rc)
3748 return rc;
3750 /* is the device still there? */
3751 if (!ata_dev_same_device(dev, class, id))
3752 return -ENODEV;
3754 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
3755 return 0;
3759 * ata_dev_revalidate - Revalidate ATA device
3760 * @dev: device to revalidate
3761 * @new_class: new class code
3762 * @readid_flags: read ID flags
3764 * Re-read IDENTIFY page, make sure @dev is still attached to the
3765 * port and reconfigure it according to the new IDENTIFY page.
3767 * LOCKING:
3768 * Kernel thread context (may sleep)
3770 * RETURNS:
3771 * 0 on success, negative errno otherwise
3773 int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3774 unsigned int readid_flags)
3776 u64 n_sectors = dev->n_sectors;
3777 int rc;
3779 if (!ata_dev_enabled(dev))
3780 return -ENODEV;
3782 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3783 if (ata_class_enabled(new_class) &&
3784 new_class != ATA_DEV_ATA && new_class != ATA_DEV_ATAPI) {
3785 ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
3786 dev->class, new_class);
3787 rc = -ENODEV;
3788 goto fail;
3791 /* re-read ID */
3792 rc = ata_dev_reread_id(dev, readid_flags);
3793 if (rc)
3794 goto fail;
3796 /* configure device according to the new ID */
3797 rc = ata_dev_configure(dev);
3798 if (rc)
3799 goto fail;
3801 /* verify n_sectors hasn't changed */
3802 if (dev->class == ATA_DEV_ATA && n_sectors &&
3803 dev->n_sectors != n_sectors) {
3804 ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
3805 "%llu != %llu\n",
3806 (unsigned long long)n_sectors,
3807 (unsigned long long)dev->n_sectors);
3809 /* restore original n_sectors */
3810 dev->n_sectors = n_sectors;
3812 rc = -ENODEV;
3813 goto fail;
3816 return 0;
3818 fail:
3819 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
3820 return rc;
3823 struct ata_blacklist_entry {
3824 const char *model_num;
3825 const char *model_rev;
3826 unsigned long horkage;
3829 static const struct ata_blacklist_entry ata_device_blacklist [] = {
3830 /* Devices with DMA related problems under Linux */
3831 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
3832 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
3833 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
3834 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
3835 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
3836 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
3837 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
3838 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
3839 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
3840 { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
3841 { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
3842 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
3843 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
3844 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3845 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
3846 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
3847 { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
3848 { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
3849 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
3850 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
3851 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
3852 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
3853 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
3854 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
3855 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
3856 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
3857 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
3858 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
3859 { "SAMSUNG CD-ROM SN-124","N001", ATA_HORKAGE_NODMA },
3860 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
3861 { "IOMEGA ZIP 250 ATAPI", NULL, ATA_HORKAGE_NODMA }, /* temporary fix */
3862 { "IOMEGA ZIP 250 ATAPI Floppy",
3863 NULL, ATA_HORKAGE_NODMA },
3865 /* Weird ATAPI devices */
3866 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
3868 /* Devices we expect to fail diagnostics */
3870 /* Devices where NCQ should be avoided */
3871 /* NCQ is slow */
3872 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
3873 /* http://thread.gmane.org/gmane.linux.ide/14907 */
3874 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
3875 /* NCQ is broken */
3876 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
3877 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
3878 { "HITACHI HDS7250SASUN500G 0621KTAWSD", "K2AOAJ0AHITACHI",
3879 ATA_HORKAGE_NONCQ },
3881 /* Blacklist entries taken from Silicon Image 3124/3132
3882 Windows driver .inf file - also several Linux problem reports */
3883 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
3884 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
3885 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
3886 /* Drives which do spurious command completion */
3887 { "HTS541680J9SA00", "SB2IC7EP", ATA_HORKAGE_NONCQ, },
3888 { "HTS541612J9SA00", "SBDIC7JP", ATA_HORKAGE_NONCQ, },
3889 { "Hitachi HTS541616J9SA00", "SB4OC70P", ATA_HORKAGE_NONCQ, },
3890 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
3891 { "FUJITSU MHV2080BH", "00840028", ATA_HORKAGE_NONCQ, },
3892 { "ST9160821AS", "3.CLF", ATA_HORKAGE_NONCQ, },
3893 { "ST3160812AS", "3.AD", ATA_HORKAGE_NONCQ, },
3894 { "SAMSUNG HD401LJ", "ZZ100-15", ATA_HORKAGE_NONCQ, },
3896 /* devices which puke on READ_NATIVE_MAX */
3897 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
3898 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
3899 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
3900 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
3902 /* End Marker */
3906 int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
3908 const char *p;
3909 int len;
3912 * check for trailing wildcard: *\0
3914 p = strchr(patt, wildchar);
3915 if (p && ((*(p + 1)) == 0))
3916 len = p - patt;
3917 else
3918 len = strlen(name);
3920 return strncmp(patt, name, len);
3923 static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
3925 unsigned char model_num[ATA_ID_PROD_LEN + 1];
3926 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
3927 const struct ata_blacklist_entry *ad = ata_device_blacklist;
3929 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
3930 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
3932 while (ad->model_num) {
3933 if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
3934 if (ad->model_rev == NULL)
3935 return ad->horkage;
3936 if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
3937 return ad->horkage;
3939 ad++;
3941 return 0;
3944 static int ata_dma_blacklisted(const struct ata_device *dev)
3946 /* We don't support polling DMA.
3947 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
3948 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
3950 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
3951 (dev->flags & ATA_DFLAG_CDB_INTR))
3952 return 1;
3953 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
3957 * ata_dev_xfermask - Compute supported xfermask of the given device
3958 * @dev: Device to compute xfermask for
3960 * Compute supported xfermask of @dev and store it in
3961 * dev->*_mask. This function is responsible for applying all
3962 * known limits including host controller limits, device
3963 * blacklist, etc...
3965 * LOCKING:
3966 * None.
3968 static void ata_dev_xfermask(struct ata_device *dev)
3970 struct ata_link *link = dev->link;
3971 struct ata_port *ap = link->ap;
3972 struct ata_host *host = ap->host;
3973 unsigned long xfer_mask;
3975 /* controller modes available */
3976 xfer_mask = ata_pack_xfermask(ap->pio_mask,
3977 ap->mwdma_mask, ap->udma_mask);
3979 /* drive modes available */
3980 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
3981 dev->mwdma_mask, dev->udma_mask);
3982 xfer_mask &= ata_id_xfermask(dev->id);
3985 * CFA Advanced TrueIDE timings are not allowed on a shared
3986 * cable
3988 if (ata_dev_pair(dev)) {
3989 /* No PIO5 or PIO6 */
3990 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
3991 /* No MWDMA3 or MWDMA 4 */
3992 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
3995 if (ata_dma_blacklisted(dev)) {
3996 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
3997 ata_dev_printk(dev, KERN_WARNING,
3998 "device is on DMA blacklist, disabling DMA\n");
4001 if ((host->flags & ATA_HOST_SIMPLEX) &&
4002 host->simplex_claimed && host->simplex_claimed != ap) {
4003 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4004 ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
4005 "other device, disabling DMA\n");
4008 if (ap->flags & ATA_FLAG_NO_IORDY)
4009 xfer_mask &= ata_pio_mask_no_iordy(dev);
4011 if (ap->ops->mode_filter)
4012 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4014 /* Apply cable rule here. Don't apply it early because when
4015 * we handle hot plug the cable type can itself change.
4016 * Check this last so that we know if the transfer rate was
4017 * solely limited by the cable.
4018 * Unknown or 80 wire cables reported host side are checked
4019 * drive side as well. Cases where we know a 40wire cable
4020 * is used safely for 80 are not checked here.
4022 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4023 /* UDMA/44 or higher would be available */
4024 if((ap->cbl == ATA_CBL_PATA40) ||
4025 (ata_drive_40wire(dev->id) &&
4026 (ap->cbl == ATA_CBL_PATA_UNK ||
4027 ap->cbl == ATA_CBL_PATA80))) {
4028 ata_dev_printk(dev, KERN_WARNING,
4029 "limited to UDMA/33 due to 40-wire cable\n");
4030 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4033 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4034 &dev->mwdma_mask, &dev->udma_mask);
4038 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4039 * @dev: Device to which command will be sent
4041 * Issue SET FEATURES - XFER MODE command to device @dev
4042 * on port @ap.
4044 * LOCKING:
4045 * PCI/etc. bus probe sem.
4047 * RETURNS:
4048 * 0 on success, AC_ERR_* mask otherwise.
4051 static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4053 struct ata_taskfile tf;
4054 unsigned int err_mask;
4056 /* set up set-features taskfile */
4057 DPRINTK("set features - xfer mode\n");
4059 /* Some controllers and ATAPI devices show flaky interrupt
4060 * behavior after setting xfer mode. Use polling instead.
4062 ata_tf_init(dev, &tf);
4063 tf.command = ATA_CMD_SET_FEATURES;
4064 tf.feature = SETFEATURES_XFER;
4065 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4066 tf.protocol = ATA_PROT_NODATA;
4067 tf.nsect = dev->xfer_mode;
4069 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
4071 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4072 return err_mask;
4076 * ata_dev_set_AN - Issue SET FEATURES - SATA FEATURES
4077 * @dev: Device to which command will be sent
4078 * @enable: Whether to enable or disable the feature
4080 * Issue SET FEATURES - SATA FEATURES command to device @dev
4081 * on port @ap with sector count set to indicate Asynchronous
4082 * Notification feature
4084 * LOCKING:
4085 * PCI/etc. bus probe sem.
4087 * RETURNS:
4088 * 0 on success, AC_ERR_* mask otherwise.
4090 static unsigned int ata_dev_set_AN(struct ata_device *dev, u8 enable)
4092 struct ata_taskfile tf;
4093 unsigned int err_mask;
4095 /* set up set-features taskfile */
4096 DPRINTK("set features - SATA features\n");
4098 ata_tf_init(dev, &tf);
4099 tf.command = ATA_CMD_SET_FEATURES;
4100 tf.feature = enable;
4101 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4102 tf.protocol = ATA_PROT_NODATA;
4103 tf.nsect = SATA_AN;
4105 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
4107 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4108 return err_mask;
4112 * ata_dev_init_params - Issue INIT DEV PARAMS command
4113 * @dev: Device to which command will be sent
4114 * @heads: Number of heads (taskfile parameter)
4115 * @sectors: Number of sectors (taskfile parameter)
4117 * LOCKING:
4118 * Kernel thread context (may sleep)
4120 * RETURNS:
4121 * 0 on success, AC_ERR_* mask otherwise.
4123 static unsigned int ata_dev_init_params(struct ata_device *dev,
4124 u16 heads, u16 sectors)
4126 struct ata_taskfile tf;
4127 unsigned int err_mask;
4129 /* Number of sectors per track 1-255. Number of heads 1-16 */
4130 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4131 return AC_ERR_INVALID;
4133 /* set up init dev params taskfile */
4134 DPRINTK("init dev params \n");
4136 ata_tf_init(dev, &tf);
4137 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4138 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4139 tf.protocol = ATA_PROT_NODATA;
4140 tf.nsect = sectors;
4141 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4143 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
4144 /* A clean abort indicates an original or just out of spec drive
4145 and we should continue as we issue the setup based on the
4146 drive reported working geometry */
4147 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4148 err_mask = 0;
4150 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4151 return err_mask;
4155 * ata_sg_clean - Unmap DMA memory associated with command
4156 * @qc: Command containing DMA memory to be released
4158 * Unmap all mapped DMA memory associated with this command.
4160 * LOCKING:
4161 * spin_lock_irqsave(host lock)
4163 void ata_sg_clean(struct ata_queued_cmd *qc)
4165 struct ata_port *ap = qc->ap;
4166 struct scatterlist *sg = qc->__sg;
4167 int dir = qc->dma_dir;
4168 void *pad_buf = NULL;
4170 WARN_ON(!(qc->flags & ATA_QCFLAG_DMAMAP));
4171 WARN_ON(sg == NULL);
4173 if (qc->flags & ATA_QCFLAG_SINGLE)
4174 WARN_ON(qc->n_elem > 1);
4176 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4178 /* if we padded the buffer out to 32-bit bound, and data
4179 * xfer direction is from-device, we must copy from the
4180 * pad buffer back into the supplied buffer
4182 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
4183 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4185 if (qc->flags & ATA_QCFLAG_SG) {
4186 if (qc->n_elem)
4187 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
4188 /* restore last sg */
4189 sg[qc->orig_n_elem - 1].length += qc->pad_len;
4190 if (pad_buf) {
4191 struct scatterlist *psg = &qc->pad_sgent;
4192 void *addr = kmap_atomic(psg->page, KM_IRQ0);
4193 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
4194 kunmap_atomic(addr, KM_IRQ0);
4196 } else {
4197 if (qc->n_elem)
4198 dma_unmap_single(ap->dev,
4199 sg_dma_address(&sg[0]), sg_dma_len(&sg[0]),
4200 dir);
4201 /* restore sg */
4202 sg->length += qc->pad_len;
4203 if (pad_buf)
4204 memcpy(qc->buf_virt + sg->length - qc->pad_len,
4205 pad_buf, qc->pad_len);
4208 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4209 qc->__sg = NULL;
4213 * ata_fill_sg - Fill PCI IDE PRD table
4214 * @qc: Metadata associated with taskfile to be transferred
4216 * Fill PCI IDE PRD (scatter-gather) table with segments
4217 * associated with the current disk command.
4219 * LOCKING:
4220 * spin_lock_irqsave(host lock)
4223 static void ata_fill_sg(struct ata_queued_cmd *qc)
4225 struct ata_port *ap = qc->ap;
4226 struct scatterlist *sg;
4227 unsigned int idx;
4229 WARN_ON(qc->__sg == NULL);
4230 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
4232 idx = 0;
4233 ata_for_each_sg(sg, qc) {
4234 u32 addr, offset;
4235 u32 sg_len, len;
4237 /* determine if physical DMA addr spans 64K boundary.
4238 * Note h/w doesn't support 64-bit, so we unconditionally
4239 * truncate dma_addr_t to u32.
4241 addr = (u32) sg_dma_address(sg);
4242 sg_len = sg_dma_len(sg);
4244 while (sg_len) {
4245 offset = addr & 0xffff;
4246 len = sg_len;
4247 if ((offset + sg_len) > 0x10000)
4248 len = 0x10000 - offset;
4250 ap->prd[idx].addr = cpu_to_le32(addr);
4251 ap->prd[idx].flags_len = cpu_to_le32(len & 0xffff);
4252 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
4254 idx++;
4255 sg_len -= len;
4256 addr += len;
4260 if (idx)
4261 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4265 * ata_fill_sg_dumb - Fill PCI IDE PRD table
4266 * @qc: Metadata associated with taskfile to be transferred
4268 * Fill PCI IDE PRD (scatter-gather) table with segments
4269 * associated with the current disk command. Perform the fill
4270 * so that we avoid writing any length 64K records for
4271 * controllers that don't follow the spec.
4273 * LOCKING:
4274 * spin_lock_irqsave(host lock)
4277 static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
4279 struct ata_port *ap = qc->ap;
4280 struct scatterlist *sg;
4281 unsigned int idx;
4283 WARN_ON(qc->__sg == NULL);
4284 WARN_ON(qc->n_elem == 0 && qc->pad_len == 0);
4286 idx = 0;
4287 ata_for_each_sg(sg, qc) {
4288 u32 addr, offset;
4289 u32 sg_len, len, blen;
4291 /* determine if physical DMA addr spans 64K boundary.
4292 * Note h/w doesn't support 64-bit, so we unconditionally
4293 * truncate dma_addr_t to u32.
4295 addr = (u32) sg_dma_address(sg);
4296 sg_len = sg_dma_len(sg);
4298 while (sg_len) {
4299 offset = addr & 0xffff;
4300 len = sg_len;
4301 if ((offset + sg_len) > 0x10000)
4302 len = 0x10000 - offset;
4304 blen = len & 0xffff;
4305 ap->prd[idx].addr = cpu_to_le32(addr);
4306 if (blen == 0) {
4307 /* Some PATA chipsets like the CS5530 can't
4308 cope with 0x0000 meaning 64K as the spec says */
4309 ap->prd[idx].flags_len = cpu_to_le32(0x8000);
4310 blen = 0x8000;
4311 ap->prd[++idx].addr = cpu_to_le32(addr + 0x8000);
4313 ap->prd[idx].flags_len = cpu_to_le32(blen);
4314 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", idx, addr, len);
4316 idx++;
4317 sg_len -= len;
4318 addr += len;
4322 if (idx)
4323 ap->prd[idx - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4327 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
4328 * @qc: Metadata associated with taskfile to check
4330 * Allow low-level driver to filter ATA PACKET commands, returning
4331 * a status indicating whether or not it is OK to use DMA for the
4332 * supplied PACKET command.
4334 * LOCKING:
4335 * spin_lock_irqsave(host lock)
4337 * RETURNS: 0 when ATAPI DMA can be used
4338 * nonzero otherwise
4340 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
4342 struct ata_port *ap = qc->ap;
4344 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4345 * few ATAPI devices choke on such DMA requests.
4347 if (unlikely(qc->nbytes & 15))
4348 return 1;
4350 if (ap->ops->check_atapi_dma)
4351 return ap->ops->check_atapi_dma(qc);
4353 return 0;
4357 * ata_std_qc_defer - Check whether a qc needs to be deferred
4358 * @qc: ATA command in question
4360 * Non-NCQ commands cannot run with any other command, NCQ or
4361 * not. As upper layer only knows the queue depth, we are
4362 * responsible for maintaining exclusion. This function checks
4363 * whether a new command @qc can be issued.
4365 * LOCKING:
4366 * spin_lock_irqsave(host lock)
4368 * RETURNS:
4369 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4371 int ata_std_qc_defer(struct ata_queued_cmd *qc)
4373 struct ata_link *link = qc->dev->link;
4375 if (qc->tf.protocol == ATA_PROT_NCQ) {
4376 if (!ata_tag_valid(link->active_tag))
4377 return 0;
4378 } else {
4379 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4380 return 0;
4383 return ATA_DEFER_LINK;
4387 * ata_qc_prep - Prepare taskfile for submission
4388 * @qc: Metadata associated with taskfile to be prepared
4390 * Prepare ATA taskfile for submission.
4392 * LOCKING:
4393 * spin_lock_irqsave(host lock)
4395 void ata_qc_prep(struct ata_queued_cmd *qc)
4397 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
4398 return;
4400 ata_fill_sg(qc);
4404 * ata_dumb_qc_prep - Prepare taskfile for submission
4405 * @qc: Metadata associated with taskfile to be prepared
4407 * Prepare ATA taskfile for submission.
4409 * LOCKING:
4410 * spin_lock_irqsave(host lock)
4412 void ata_dumb_qc_prep(struct ata_queued_cmd *qc)
4414 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
4415 return;
4417 ata_fill_sg_dumb(qc);
4420 void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4423 * ata_sg_init_one - Associate command with memory buffer
4424 * @qc: Command to be associated
4425 * @buf: Memory buffer
4426 * @buflen: Length of memory buffer, in bytes.
4428 * Initialize the data-related elements of queued_cmd @qc
4429 * to point to a single memory buffer, @buf of byte length @buflen.
4431 * LOCKING:
4432 * spin_lock_irqsave(host lock)
4435 void ata_sg_init_one(struct ata_queued_cmd *qc, void *buf, unsigned int buflen)
4437 qc->flags |= ATA_QCFLAG_SINGLE;
4439 qc->__sg = &qc->sgent;
4440 qc->n_elem = 1;
4441 qc->orig_n_elem = 1;
4442 qc->buf_virt = buf;
4443 qc->nbytes = buflen;
4445 sg_init_one(&qc->sgent, buf, buflen);
4449 * ata_sg_init - Associate command with scatter-gather table.
4450 * @qc: Command to be associated
4451 * @sg: Scatter-gather table.
4452 * @n_elem: Number of elements in s/g table.
4454 * Initialize the data-related elements of queued_cmd @qc
4455 * to point to a scatter-gather table @sg, containing @n_elem
4456 * elements.
4458 * LOCKING:
4459 * spin_lock_irqsave(host lock)
4462 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4463 unsigned int n_elem)
4465 qc->flags |= ATA_QCFLAG_SG;
4466 qc->__sg = sg;
4467 qc->n_elem = n_elem;
4468 qc->orig_n_elem = n_elem;
4472 * ata_sg_setup_one - DMA-map the memory buffer associated with a command.
4473 * @qc: Command with memory buffer to be mapped.
4475 * DMA-map the memory buffer associated with queued_cmd @qc.
4477 * LOCKING:
4478 * spin_lock_irqsave(host lock)
4480 * RETURNS:
4481 * Zero on success, negative on error.
4484 static int ata_sg_setup_one(struct ata_queued_cmd *qc)
4486 struct ata_port *ap = qc->ap;
4487 int dir = qc->dma_dir;
4488 struct scatterlist *sg = qc->__sg;
4489 dma_addr_t dma_address;
4490 int trim_sg = 0;
4492 /* we must lengthen transfers to end on a 32-bit boundary */
4493 qc->pad_len = sg->length & 3;
4494 if (qc->pad_len) {
4495 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4496 struct scatterlist *psg = &qc->pad_sgent;
4498 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
4500 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
4502 if (qc->tf.flags & ATA_TFLAG_WRITE)
4503 memcpy(pad_buf, qc->buf_virt + sg->length - qc->pad_len,
4504 qc->pad_len);
4506 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
4507 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
4508 /* trim sg */
4509 sg->length -= qc->pad_len;
4510 if (sg->length == 0)
4511 trim_sg = 1;
4513 DPRINTK("padding done, sg->length=%u pad_len=%u\n",
4514 sg->length, qc->pad_len);
4517 if (trim_sg) {
4518 qc->n_elem--;
4519 goto skip_map;
4522 dma_address = dma_map_single(ap->dev, qc->buf_virt,
4523 sg->length, dir);
4524 if (dma_mapping_error(dma_address)) {
4525 /* restore sg */
4526 sg->length += qc->pad_len;
4527 return -1;
4530 sg_dma_address(sg) = dma_address;
4531 sg_dma_len(sg) = sg->length;
4533 skip_map:
4534 DPRINTK("mapped buffer of %d bytes for %s\n", sg_dma_len(sg),
4535 qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4537 return 0;
4541 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4542 * @qc: Command with scatter-gather table to be mapped.
4544 * DMA-map the scatter-gather table associated with queued_cmd @qc.
4546 * LOCKING:
4547 * spin_lock_irqsave(host lock)
4549 * RETURNS:
4550 * Zero on success, negative on error.
4554 static int ata_sg_setup(struct ata_queued_cmd *qc)
4556 struct ata_port *ap = qc->ap;
4557 struct scatterlist *sg = qc->__sg;
4558 struct scatterlist *lsg = &sg[qc->n_elem - 1];
4559 int n_elem, pre_n_elem, dir, trim_sg = 0;
4561 VPRINTK("ENTER, ata%u\n", ap->print_id);
4562 WARN_ON(!(qc->flags & ATA_QCFLAG_SG));
4564 /* we must lengthen transfers to end on a 32-bit boundary */
4565 qc->pad_len = lsg->length & 3;
4566 if (qc->pad_len) {
4567 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4568 struct scatterlist *psg = &qc->pad_sgent;
4569 unsigned int offset;
4571 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
4573 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
4576 * psg->page/offset are used to copy to-be-written
4577 * data in this function or read data in ata_sg_clean.
4579 offset = lsg->offset + lsg->length - qc->pad_len;
4580 psg->page = nth_page(lsg->page, offset >> PAGE_SHIFT);
4581 psg->offset = offset_in_page(offset);
4583 if (qc->tf.flags & ATA_TFLAG_WRITE) {
4584 void *addr = kmap_atomic(psg->page, KM_IRQ0);
4585 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
4586 kunmap_atomic(addr, KM_IRQ0);
4589 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
4590 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
4591 /* trim last sg */
4592 lsg->length -= qc->pad_len;
4593 if (lsg->length == 0)
4594 trim_sg = 1;
4596 DPRINTK("padding done, sg[%d].length=%u pad_len=%u\n",
4597 qc->n_elem - 1, lsg->length, qc->pad_len);
4600 pre_n_elem = qc->n_elem;
4601 if (trim_sg && pre_n_elem)
4602 pre_n_elem--;
4604 if (!pre_n_elem) {
4605 n_elem = 0;
4606 goto skip_map;
4609 dir = qc->dma_dir;
4610 n_elem = dma_map_sg(ap->dev, sg, pre_n_elem, dir);
4611 if (n_elem < 1) {
4612 /* restore last sg */
4613 lsg->length += qc->pad_len;
4614 return -1;
4617 DPRINTK("%d sg elements mapped\n", n_elem);
4619 skip_map:
4620 qc->n_elem = n_elem;
4622 return 0;
4626 * swap_buf_le16 - swap halves of 16-bit words in place
4627 * @buf: Buffer to swap
4628 * @buf_words: Number of 16-bit words in buffer.
4630 * Swap halves of 16-bit words if needed to convert from
4631 * little-endian byte order to native cpu byte order, or
4632 * vice-versa.
4634 * LOCKING:
4635 * Inherited from caller.
4637 void swap_buf_le16(u16 *buf, unsigned int buf_words)
4639 #ifdef __BIG_ENDIAN
4640 unsigned int i;
4642 for (i = 0; i < buf_words; i++)
4643 buf[i] = le16_to_cpu(buf[i]);
4644 #endif /* __BIG_ENDIAN */
4648 * ata_data_xfer - Transfer data by PIO
4649 * @adev: device to target
4650 * @buf: data buffer
4651 * @buflen: buffer length
4652 * @write_data: read/write
4654 * Transfer data from/to the device data register by PIO.
4656 * LOCKING:
4657 * Inherited from caller.
4659 void ata_data_xfer(struct ata_device *adev, unsigned char *buf,
4660 unsigned int buflen, int write_data)
4662 struct ata_port *ap = adev->link->ap;
4663 unsigned int words = buflen >> 1;
4665 /* Transfer multiple of 2 bytes */
4666 if (write_data)
4667 iowrite16_rep(ap->ioaddr.data_addr, buf, words);
4668 else
4669 ioread16_rep(ap->ioaddr.data_addr, buf, words);
4671 /* Transfer trailing 1 byte, if any. */
4672 if (unlikely(buflen & 0x01)) {
4673 u16 align_buf[1] = { 0 };
4674 unsigned char *trailing_buf = buf + buflen - 1;
4676 if (write_data) {
4677 memcpy(align_buf, trailing_buf, 1);
4678 iowrite16(le16_to_cpu(align_buf[0]), ap->ioaddr.data_addr);
4679 } else {
4680 align_buf[0] = cpu_to_le16(ioread16(ap->ioaddr.data_addr));
4681 memcpy(trailing_buf, align_buf, 1);
4687 * ata_data_xfer_noirq - Transfer data by PIO
4688 * @adev: device to target
4689 * @buf: data buffer
4690 * @buflen: buffer length
4691 * @write_data: read/write
4693 * Transfer data from/to the device data register by PIO. Do the
4694 * transfer with interrupts disabled.
4696 * LOCKING:
4697 * Inherited from caller.
4699 void ata_data_xfer_noirq(struct ata_device *adev, unsigned char *buf,
4700 unsigned int buflen, int write_data)
4702 unsigned long flags;
4703 local_irq_save(flags);
4704 ata_data_xfer(adev, buf, buflen, write_data);
4705 local_irq_restore(flags);
4710 * ata_pio_sector - Transfer a sector of data.
4711 * @qc: Command on going
4713 * Transfer qc->sect_size bytes of data from/to the ATA device.
4715 * LOCKING:
4716 * Inherited from caller.
4719 static void ata_pio_sector(struct ata_queued_cmd *qc)
4721 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
4722 struct scatterlist *sg = qc->__sg;
4723 struct ata_port *ap = qc->ap;
4724 struct page *page;
4725 unsigned int offset;
4726 unsigned char *buf;
4728 if (qc->curbytes == qc->nbytes - qc->sect_size)
4729 ap->hsm_task_state = HSM_ST_LAST;
4731 page = sg[qc->cursg].page;
4732 offset = sg[qc->cursg].offset + qc->cursg_ofs;
4734 /* get the current page and offset */
4735 page = nth_page(page, (offset >> PAGE_SHIFT));
4736 offset %= PAGE_SIZE;
4738 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4740 if (PageHighMem(page)) {
4741 unsigned long flags;
4743 /* FIXME: use a bounce buffer */
4744 local_irq_save(flags);
4745 buf = kmap_atomic(page, KM_IRQ0);
4747 /* do the actual data transfer */
4748 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
4750 kunmap_atomic(buf, KM_IRQ0);
4751 local_irq_restore(flags);
4752 } else {
4753 buf = page_address(page);
4754 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
4757 qc->curbytes += qc->sect_size;
4758 qc->cursg_ofs += qc->sect_size;
4760 if (qc->cursg_ofs == (&sg[qc->cursg])->length) {
4761 qc->cursg++;
4762 qc->cursg_ofs = 0;
4767 * ata_pio_sectors - Transfer one or many sectors.
4768 * @qc: Command on going
4770 * Transfer one or many sectors of data from/to the
4771 * ATA device for the DRQ request.
4773 * LOCKING:
4774 * Inherited from caller.
4777 static void ata_pio_sectors(struct ata_queued_cmd *qc)
4779 if (is_multi_taskfile(&qc->tf)) {
4780 /* READ/WRITE MULTIPLE */
4781 unsigned int nsect;
4783 WARN_ON(qc->dev->multi_count == 0);
4785 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
4786 qc->dev->multi_count);
4787 while (nsect--)
4788 ata_pio_sector(qc);
4789 } else
4790 ata_pio_sector(qc);
4792 ata_altstatus(qc->ap); /* flush */
4796 * atapi_send_cdb - Write CDB bytes to hardware
4797 * @ap: Port to which ATAPI device is attached.
4798 * @qc: Taskfile currently active
4800 * When device has indicated its readiness to accept
4801 * a CDB, this function is called. Send the CDB.
4803 * LOCKING:
4804 * caller.
4807 static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
4809 /* send SCSI cdb */
4810 DPRINTK("send cdb\n");
4811 WARN_ON(qc->dev->cdb_len < 12);
4813 ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
4814 ata_altstatus(ap); /* flush */
4816 switch (qc->tf.protocol) {
4817 case ATA_PROT_ATAPI:
4818 ap->hsm_task_state = HSM_ST;
4819 break;
4820 case ATA_PROT_ATAPI_NODATA:
4821 ap->hsm_task_state = HSM_ST_LAST;
4822 break;
4823 case ATA_PROT_ATAPI_DMA:
4824 ap->hsm_task_state = HSM_ST_LAST;
4825 /* initiate bmdma */
4826 ap->ops->bmdma_start(qc);
4827 break;
4832 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
4833 * @qc: Command on going
4834 * @bytes: number of bytes
4836 * Transfer Transfer data from/to the ATAPI device.
4838 * LOCKING:
4839 * Inherited from caller.
4843 static void __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
4845 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
4846 struct scatterlist *sg = qc->__sg;
4847 struct ata_port *ap = qc->ap;
4848 struct page *page;
4849 unsigned char *buf;
4850 unsigned int offset, count;
4852 if (qc->curbytes + bytes >= qc->nbytes)
4853 ap->hsm_task_state = HSM_ST_LAST;
4855 next_sg:
4856 if (unlikely(qc->cursg >= qc->n_elem)) {
4858 * The end of qc->sg is reached and the device expects
4859 * more data to transfer. In order not to overrun qc->sg
4860 * and fulfill length specified in the byte count register,
4861 * - for read case, discard trailing data from the device
4862 * - for write case, padding zero data to the device
4864 u16 pad_buf[1] = { 0 };
4865 unsigned int words = bytes >> 1;
4866 unsigned int i;
4868 if (words) /* warning if bytes > 1 */
4869 ata_dev_printk(qc->dev, KERN_WARNING,
4870 "%u bytes trailing data\n", bytes);
4872 for (i = 0; i < words; i++)
4873 ap->ops->data_xfer(qc->dev, (unsigned char*)pad_buf, 2, do_write);
4875 ap->hsm_task_state = HSM_ST_LAST;
4876 return;
4879 sg = &qc->__sg[qc->cursg];
4881 page = sg->page;
4882 offset = sg->offset + qc->cursg_ofs;
4884 /* get the current page and offset */
4885 page = nth_page(page, (offset >> PAGE_SHIFT));
4886 offset %= PAGE_SIZE;
4888 /* don't overrun current sg */
4889 count = min(sg->length - qc->cursg_ofs, bytes);
4891 /* don't cross page boundaries */
4892 count = min(count, (unsigned int)PAGE_SIZE - offset);
4894 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
4896 if (PageHighMem(page)) {
4897 unsigned long flags;
4899 /* FIXME: use bounce buffer */
4900 local_irq_save(flags);
4901 buf = kmap_atomic(page, KM_IRQ0);
4903 /* do the actual data transfer */
4904 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
4906 kunmap_atomic(buf, KM_IRQ0);
4907 local_irq_restore(flags);
4908 } else {
4909 buf = page_address(page);
4910 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
4913 bytes -= count;
4914 qc->curbytes += count;
4915 qc->cursg_ofs += count;
4917 if (qc->cursg_ofs == sg->length) {
4918 qc->cursg++;
4919 qc->cursg_ofs = 0;
4922 if (bytes)
4923 goto next_sg;
4927 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
4928 * @qc: Command on going
4930 * Transfer Transfer data from/to the ATAPI device.
4932 * LOCKING:
4933 * Inherited from caller.
4936 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
4938 struct ata_port *ap = qc->ap;
4939 struct ata_device *dev = qc->dev;
4940 unsigned int ireason, bc_lo, bc_hi, bytes;
4941 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
4943 /* Abuse qc->result_tf for temp storage of intermediate TF
4944 * here to save some kernel stack usage.
4945 * For normal completion, qc->result_tf is not relevant. For
4946 * error, qc->result_tf is later overwritten by ata_qc_complete().
4947 * So, the correctness of qc->result_tf is not affected.
4949 ap->ops->tf_read(ap, &qc->result_tf);
4950 ireason = qc->result_tf.nsect;
4951 bc_lo = qc->result_tf.lbam;
4952 bc_hi = qc->result_tf.lbah;
4953 bytes = (bc_hi << 8) | bc_lo;
4955 /* shall be cleared to zero, indicating xfer of data */
4956 if (ireason & (1 << 0))
4957 goto err_out;
4959 /* make sure transfer direction matches expected */
4960 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
4961 if (do_write != i_write)
4962 goto err_out;
4964 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
4966 __atapi_pio_bytes(qc, bytes);
4967 ata_altstatus(ap); /* flush */
4969 return;
4971 err_out:
4972 ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
4973 qc->err_mask |= AC_ERR_HSM;
4974 ap->hsm_task_state = HSM_ST_ERR;
4978 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
4979 * @ap: the target ata_port
4980 * @qc: qc on going
4982 * RETURNS:
4983 * 1 if ok in workqueue, 0 otherwise.
4986 static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
4988 if (qc->tf.flags & ATA_TFLAG_POLLING)
4989 return 1;
4991 if (ap->hsm_task_state == HSM_ST_FIRST) {
4992 if (qc->tf.protocol == ATA_PROT_PIO &&
4993 (qc->tf.flags & ATA_TFLAG_WRITE))
4994 return 1;
4996 if (is_atapi_taskfile(&qc->tf) &&
4997 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
4998 return 1;
5001 return 0;
5005 * ata_hsm_qc_complete - finish a qc running on standard HSM
5006 * @qc: Command to complete
5007 * @in_wq: 1 if called from workqueue, 0 otherwise
5009 * Finish @qc which is running on standard HSM.
5011 * LOCKING:
5012 * If @in_wq is zero, spin_lock_irqsave(host lock).
5013 * Otherwise, none on entry and grabs host lock.
5015 static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
5017 struct ata_port *ap = qc->ap;
5018 unsigned long flags;
5020 if (ap->ops->error_handler) {
5021 if (in_wq) {
5022 spin_lock_irqsave(ap->lock, flags);
5024 /* EH might have kicked in while host lock is
5025 * released.
5027 qc = ata_qc_from_tag(ap, qc->tag);
5028 if (qc) {
5029 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
5030 ap->ops->irq_on(ap);
5031 ata_qc_complete(qc);
5032 } else
5033 ata_port_freeze(ap);
5036 spin_unlock_irqrestore(ap->lock, flags);
5037 } else {
5038 if (likely(!(qc->err_mask & AC_ERR_HSM)))
5039 ata_qc_complete(qc);
5040 else
5041 ata_port_freeze(ap);
5043 } else {
5044 if (in_wq) {
5045 spin_lock_irqsave(ap->lock, flags);
5046 ap->ops->irq_on(ap);
5047 ata_qc_complete(qc);
5048 spin_unlock_irqrestore(ap->lock, flags);
5049 } else
5050 ata_qc_complete(qc);
5055 * ata_hsm_move - move the HSM to the next state.
5056 * @ap: the target ata_port
5057 * @qc: qc on going
5058 * @status: current device status
5059 * @in_wq: 1 if called from workqueue, 0 otherwise
5061 * RETURNS:
5062 * 1 when poll next status needed, 0 otherwise.
5064 int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
5065 u8 status, int in_wq)
5067 unsigned long flags = 0;
5068 int poll_next;
5070 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
5072 /* Make sure ata_qc_issue_prot() does not throw things
5073 * like DMA polling into the workqueue. Notice that
5074 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
5076 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
5078 fsm_start:
5079 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
5080 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
5082 switch (ap->hsm_task_state) {
5083 case HSM_ST_FIRST:
5084 /* Send first data block or PACKET CDB */
5086 /* If polling, we will stay in the work queue after
5087 * sending the data. Otherwise, interrupt handler
5088 * takes over after sending the data.
5090 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
5092 /* check device status */
5093 if (unlikely((status & ATA_DRQ) == 0)) {
5094 /* handle BSY=0, DRQ=0 as error */
5095 if (likely(status & (ATA_ERR | ATA_DF)))
5096 /* device stops HSM for abort/error */
5097 qc->err_mask |= AC_ERR_DEV;
5098 else
5099 /* HSM violation. Let EH handle this */
5100 qc->err_mask |= AC_ERR_HSM;
5102 ap->hsm_task_state = HSM_ST_ERR;
5103 goto fsm_start;
5106 /* Device should not ask for data transfer (DRQ=1)
5107 * when it finds something wrong.
5108 * We ignore DRQ here and stop the HSM by
5109 * changing hsm_task_state to HSM_ST_ERR and
5110 * let the EH abort the command or reset the device.
5112 if (unlikely(status & (ATA_ERR | ATA_DF))) {
5113 ata_port_printk(ap, KERN_WARNING, "DRQ=1 with device "
5114 "error, dev_stat 0x%X\n", status);
5115 qc->err_mask |= AC_ERR_HSM;
5116 ap->hsm_task_state = HSM_ST_ERR;
5117 goto fsm_start;
5120 /* Send the CDB (atapi) or the first data block (ata pio out).
5121 * During the state transition, interrupt handler shouldn't
5122 * be invoked before the data transfer is complete and
5123 * hsm_task_state is changed. Hence, the following locking.
5125 if (in_wq)
5126 spin_lock_irqsave(ap->lock, flags);
5128 if (qc->tf.protocol == ATA_PROT_PIO) {
5129 /* PIO data out protocol.
5130 * send first data block.
5133 /* ata_pio_sectors() might change the state
5134 * to HSM_ST_LAST. so, the state is changed here
5135 * before ata_pio_sectors().
5137 ap->hsm_task_state = HSM_ST;
5138 ata_pio_sectors(qc);
5139 } else
5140 /* send CDB */
5141 atapi_send_cdb(ap, qc);
5143 if (in_wq)
5144 spin_unlock_irqrestore(ap->lock, flags);
5146 /* if polling, ata_pio_task() handles the rest.
5147 * otherwise, interrupt handler takes over from here.
5149 break;
5151 case HSM_ST:
5152 /* complete command or read/write the data register */
5153 if (qc->tf.protocol == ATA_PROT_ATAPI) {
5154 /* ATAPI PIO protocol */
5155 if ((status & ATA_DRQ) == 0) {
5156 /* No more data to transfer or device error.
5157 * Device error will be tagged in HSM_ST_LAST.
5159 ap->hsm_task_state = HSM_ST_LAST;
5160 goto fsm_start;
5163 /* Device should not ask for data transfer (DRQ=1)
5164 * when it finds something wrong.
5165 * We ignore DRQ here and stop the HSM by
5166 * changing hsm_task_state to HSM_ST_ERR and
5167 * let the EH abort the command or reset the device.
5169 if (unlikely(status & (ATA_ERR | ATA_DF))) {
5170 ata_port_printk(ap, KERN_WARNING, "DRQ=1 with "
5171 "device error, dev_stat 0x%X\n",
5172 status);
5173 qc->err_mask |= AC_ERR_HSM;
5174 ap->hsm_task_state = HSM_ST_ERR;
5175 goto fsm_start;
5178 atapi_pio_bytes(qc);
5180 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
5181 /* bad ireason reported by device */
5182 goto fsm_start;
5184 } else {
5185 /* ATA PIO protocol */
5186 if (unlikely((status & ATA_DRQ) == 0)) {
5187 /* handle BSY=0, DRQ=0 as error */
5188 if (likely(status & (ATA_ERR | ATA_DF)))
5189 /* device stops HSM for abort/error */
5190 qc->err_mask |= AC_ERR_DEV;
5191 else
5192 /* HSM violation. Let EH handle this.
5193 * Phantom devices also trigger this
5194 * condition. Mark hint.
5196 qc->err_mask |= AC_ERR_HSM |
5197 AC_ERR_NODEV_HINT;
5199 ap->hsm_task_state = HSM_ST_ERR;
5200 goto fsm_start;
5203 /* For PIO reads, some devices may ask for
5204 * data transfer (DRQ=1) alone with ERR=1.
5205 * We respect DRQ here and transfer one
5206 * block of junk data before changing the
5207 * hsm_task_state to HSM_ST_ERR.
5209 * For PIO writes, ERR=1 DRQ=1 doesn't make
5210 * sense since the data block has been
5211 * transferred to the device.
5213 if (unlikely(status & (ATA_ERR | ATA_DF))) {
5214 /* data might be corrputed */
5215 qc->err_mask |= AC_ERR_DEV;
5217 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
5218 ata_pio_sectors(qc);
5219 status = ata_wait_idle(ap);
5222 if (status & (ATA_BUSY | ATA_DRQ))
5223 qc->err_mask |= AC_ERR_HSM;
5225 /* ata_pio_sectors() might change the
5226 * state to HSM_ST_LAST. so, the state
5227 * is changed after ata_pio_sectors().
5229 ap->hsm_task_state = HSM_ST_ERR;
5230 goto fsm_start;
5233 ata_pio_sectors(qc);
5235 if (ap->hsm_task_state == HSM_ST_LAST &&
5236 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
5237 /* all data read */
5238 status = ata_wait_idle(ap);
5239 goto fsm_start;
5243 poll_next = 1;
5244 break;
5246 case HSM_ST_LAST:
5247 if (unlikely(!ata_ok(status))) {
5248 qc->err_mask |= __ac_err_mask(status);
5249 ap->hsm_task_state = HSM_ST_ERR;
5250 goto fsm_start;
5253 /* no more data to transfer */
5254 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
5255 ap->print_id, qc->dev->devno, status);
5257 WARN_ON(qc->err_mask);
5259 ap->hsm_task_state = HSM_ST_IDLE;
5261 /* complete taskfile transaction */
5262 ata_hsm_qc_complete(qc, in_wq);
5264 poll_next = 0;
5265 break;
5267 case HSM_ST_ERR:
5268 /* make sure qc->err_mask is available to
5269 * know what's wrong and recover
5271 WARN_ON(qc->err_mask == 0);
5273 ap->hsm_task_state = HSM_ST_IDLE;
5275 /* complete taskfile transaction */
5276 ata_hsm_qc_complete(qc, in_wq);
5278 poll_next = 0;
5279 break;
5280 default:
5281 poll_next = 0;
5282 BUG();
5285 return poll_next;
5288 static void ata_pio_task(struct work_struct *work)
5290 struct ata_port *ap =
5291 container_of(work, struct ata_port, port_task.work);
5292 struct ata_queued_cmd *qc = ap->port_task_data;
5293 u8 status;
5294 int poll_next;
5296 fsm_start:
5297 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
5300 * This is purely heuristic. This is a fast path.
5301 * Sometimes when we enter, BSY will be cleared in
5302 * a chk-status or two. If not, the drive is probably seeking
5303 * or something. Snooze for a couple msecs, then
5304 * chk-status again. If still busy, queue delayed work.
5306 status = ata_busy_wait(ap, ATA_BUSY, 5);
5307 if (status & ATA_BUSY) {
5308 msleep(2);
5309 status = ata_busy_wait(ap, ATA_BUSY, 10);
5310 if (status & ATA_BUSY) {
5311 ata_port_queue_task(ap, ata_pio_task, qc, ATA_SHORT_PAUSE);
5312 return;
5316 /* move the HSM */
5317 poll_next = ata_hsm_move(ap, qc, status, 1);
5319 /* another command or interrupt handler
5320 * may be running at this point.
5322 if (poll_next)
5323 goto fsm_start;
5327 * ata_qc_new - Request an available ATA command, for queueing
5328 * @ap: Port associated with device @dev
5329 * @dev: Device from whom we request an available command structure
5331 * LOCKING:
5332 * None.
5335 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
5337 struct ata_queued_cmd *qc = NULL;
5338 unsigned int i;
5340 /* no command while frozen */
5341 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
5342 return NULL;
5344 /* the last tag is reserved for internal command. */
5345 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
5346 if (!test_and_set_bit(i, &ap->qc_allocated)) {
5347 qc = __ata_qc_from_tag(ap, i);
5348 break;
5351 if (qc)
5352 qc->tag = i;
5354 return qc;
5358 * ata_qc_new_init - Request an available ATA command, and initialize it
5359 * @dev: Device from whom we request an available command structure
5361 * LOCKING:
5362 * None.
5365 struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
5367 struct ata_port *ap = dev->link->ap;
5368 struct ata_queued_cmd *qc;
5370 qc = ata_qc_new(ap);
5371 if (qc) {
5372 qc->scsicmd = NULL;
5373 qc->ap = ap;
5374 qc->dev = dev;
5376 ata_qc_reinit(qc);
5379 return qc;
5383 * ata_qc_free - free unused ata_queued_cmd
5384 * @qc: Command to complete
5386 * Designed to free unused ata_queued_cmd object
5387 * in case something prevents using it.
5389 * LOCKING:
5390 * spin_lock_irqsave(host lock)
5392 void ata_qc_free(struct ata_queued_cmd *qc)
5394 struct ata_port *ap = qc->ap;
5395 unsigned int tag;
5397 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5399 qc->flags = 0;
5400 tag = qc->tag;
5401 if (likely(ata_tag_valid(tag))) {
5402 qc->tag = ATA_TAG_POISON;
5403 clear_bit(tag, &ap->qc_allocated);
5407 void __ata_qc_complete(struct ata_queued_cmd *qc)
5409 struct ata_port *ap = qc->ap;
5410 struct ata_link *link = qc->dev->link;
5412 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5413 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
5415 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5416 ata_sg_clean(qc);
5418 /* command should be marked inactive atomically with qc completion */
5419 if (qc->tf.protocol == ATA_PROT_NCQ) {
5420 link->sactive &= ~(1 << qc->tag);
5421 if (!link->sactive)
5422 ap->nr_active_links--;
5423 } else {
5424 link->active_tag = ATA_TAG_POISON;
5425 ap->nr_active_links--;
5428 /* clear exclusive status */
5429 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
5430 ap->excl_link == link))
5431 ap->excl_link = NULL;
5433 /* atapi: mark qc as inactive to prevent the interrupt handler
5434 * from completing the command twice later, before the error handler
5435 * is called. (when rc != 0 and atapi request sense is needed)
5437 qc->flags &= ~ATA_QCFLAG_ACTIVE;
5438 ap->qc_active &= ~(1 << qc->tag);
5440 /* call completion callback */
5441 qc->complete_fn(qc);
5444 static void fill_result_tf(struct ata_queued_cmd *qc)
5446 struct ata_port *ap = qc->ap;
5448 qc->result_tf.flags = qc->tf.flags;
5449 ap->ops->tf_read(ap, &qc->result_tf);
5453 * ata_qc_complete - Complete an active ATA command
5454 * @qc: Command to complete
5455 * @err_mask: ATA Status register contents
5457 * Indicate to the mid and upper layers that an ATA
5458 * command has completed, with either an ok or not-ok status.
5460 * LOCKING:
5461 * spin_lock_irqsave(host lock)
5463 void ata_qc_complete(struct ata_queued_cmd *qc)
5465 struct ata_port *ap = qc->ap;
5467 /* XXX: New EH and old EH use different mechanisms to
5468 * synchronize EH with regular execution path.
5470 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5471 * Normal execution path is responsible for not accessing a
5472 * failed qc. libata core enforces the rule by returning NULL
5473 * from ata_qc_from_tag() for failed qcs.
5475 * Old EH depends on ata_qc_complete() nullifying completion
5476 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
5477 * not synchronize with interrupt handler. Only PIO task is
5478 * taken care of.
5480 if (ap->ops->error_handler) {
5481 WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
5483 if (unlikely(qc->err_mask))
5484 qc->flags |= ATA_QCFLAG_FAILED;
5486 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5487 if (!ata_tag_internal(qc->tag)) {
5488 /* always fill result TF for failed qc */
5489 fill_result_tf(qc);
5490 ata_qc_schedule_eh(qc);
5491 return;
5495 /* read result TF if requested */
5496 if (qc->flags & ATA_QCFLAG_RESULT_TF)
5497 fill_result_tf(qc);
5499 __ata_qc_complete(qc);
5500 } else {
5501 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5502 return;
5504 /* read result TF if failed or requested */
5505 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
5506 fill_result_tf(qc);
5508 __ata_qc_complete(qc);
5513 * ata_qc_complete_multiple - Complete multiple qcs successfully
5514 * @ap: port in question
5515 * @qc_active: new qc_active mask
5516 * @finish_qc: LLDD callback invoked before completing a qc
5518 * Complete in-flight commands. This functions is meant to be
5519 * called from low-level driver's interrupt routine to complete
5520 * requests normally. ap->qc_active and @qc_active is compared
5521 * and commands are completed accordingly.
5523 * LOCKING:
5524 * spin_lock_irqsave(host lock)
5526 * RETURNS:
5527 * Number of completed commands on success, -errno otherwise.
5529 int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
5530 void (*finish_qc)(struct ata_queued_cmd *))
5532 int nr_done = 0;
5533 u32 done_mask;
5534 int i;
5536 done_mask = ap->qc_active ^ qc_active;
5538 if (unlikely(done_mask & qc_active)) {
5539 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
5540 "(%08x->%08x)\n", ap->qc_active, qc_active);
5541 return -EINVAL;
5544 for (i = 0; i < ATA_MAX_QUEUE; i++) {
5545 struct ata_queued_cmd *qc;
5547 if (!(done_mask & (1 << i)))
5548 continue;
5550 if ((qc = ata_qc_from_tag(ap, i))) {
5551 if (finish_qc)
5552 finish_qc(qc);
5553 ata_qc_complete(qc);
5554 nr_done++;
5558 return nr_done;
5561 static inline int ata_should_dma_map(struct ata_queued_cmd *qc)
5563 struct ata_port *ap = qc->ap;
5565 switch (qc->tf.protocol) {
5566 case ATA_PROT_NCQ:
5567 case ATA_PROT_DMA:
5568 case ATA_PROT_ATAPI_DMA:
5569 return 1;
5571 case ATA_PROT_ATAPI:
5572 case ATA_PROT_PIO:
5573 if (ap->flags & ATA_FLAG_PIO_DMA)
5574 return 1;
5576 /* fall through */
5578 default:
5579 return 0;
5582 /* never reached */
5586 * ata_qc_issue - issue taskfile to device
5587 * @qc: command to issue to device
5589 * Prepare an ATA command to submission to device.
5590 * This includes mapping the data into a DMA-able
5591 * area, filling in the S/G table, and finally
5592 * writing the taskfile to hardware, starting the command.
5594 * LOCKING:
5595 * spin_lock_irqsave(host lock)
5597 void ata_qc_issue(struct ata_queued_cmd *qc)
5599 struct ata_port *ap = qc->ap;
5600 struct ata_link *link = qc->dev->link;
5602 /* Make sure only one non-NCQ command is outstanding. The
5603 * check is skipped for old EH because it reuses active qc to
5604 * request ATAPI sense.
5606 WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
5608 if (qc->tf.protocol == ATA_PROT_NCQ) {
5609 WARN_ON(link->sactive & (1 << qc->tag));
5611 if (!link->sactive)
5612 ap->nr_active_links++;
5613 link->sactive |= 1 << qc->tag;
5614 } else {
5615 WARN_ON(link->sactive);
5617 ap->nr_active_links++;
5618 link->active_tag = qc->tag;
5621 qc->flags |= ATA_QCFLAG_ACTIVE;
5622 ap->qc_active |= 1 << qc->tag;
5624 if (ata_should_dma_map(qc)) {
5625 if (qc->flags & ATA_QCFLAG_SG) {
5626 if (ata_sg_setup(qc))
5627 goto sg_err;
5628 } else if (qc->flags & ATA_QCFLAG_SINGLE) {
5629 if (ata_sg_setup_one(qc))
5630 goto sg_err;
5632 } else {
5633 qc->flags &= ~ATA_QCFLAG_DMAMAP;
5636 ap->ops->qc_prep(qc);
5638 qc->err_mask |= ap->ops->qc_issue(qc);
5639 if (unlikely(qc->err_mask))
5640 goto err;
5641 return;
5643 sg_err:
5644 qc->flags &= ~ATA_QCFLAG_DMAMAP;
5645 qc->err_mask |= AC_ERR_SYSTEM;
5646 err:
5647 ata_qc_complete(qc);
5651 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
5652 * @qc: command to issue to device
5654 * Using various libata functions and hooks, this function
5655 * starts an ATA command. ATA commands are grouped into
5656 * classes called "protocols", and issuing each type of protocol
5657 * is slightly different.
5659 * May be used as the qc_issue() entry in ata_port_operations.
5661 * LOCKING:
5662 * spin_lock_irqsave(host lock)
5664 * RETURNS:
5665 * Zero on success, AC_ERR_* mask on failure
5668 unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
5670 struct ata_port *ap = qc->ap;
5672 /* Use polling pio if the LLD doesn't handle
5673 * interrupt driven pio and atapi CDB interrupt.
5675 if (ap->flags & ATA_FLAG_PIO_POLLING) {
5676 switch (qc->tf.protocol) {
5677 case ATA_PROT_PIO:
5678 case ATA_PROT_NODATA:
5679 case ATA_PROT_ATAPI:
5680 case ATA_PROT_ATAPI_NODATA:
5681 qc->tf.flags |= ATA_TFLAG_POLLING;
5682 break;
5683 case ATA_PROT_ATAPI_DMA:
5684 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
5685 /* see ata_dma_blacklisted() */
5686 BUG();
5687 break;
5688 default:
5689 break;
5693 /* select the device */
5694 ata_dev_select(ap, qc->dev->devno, 1, 0);
5696 /* start the command */
5697 switch (qc->tf.protocol) {
5698 case ATA_PROT_NODATA:
5699 if (qc->tf.flags & ATA_TFLAG_POLLING)
5700 ata_qc_set_polling(qc);
5702 ata_tf_to_host(ap, &qc->tf);
5703 ap->hsm_task_state = HSM_ST_LAST;
5705 if (qc->tf.flags & ATA_TFLAG_POLLING)
5706 ata_port_queue_task(ap, ata_pio_task, qc, 0);
5708 break;
5710 case ATA_PROT_DMA:
5711 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
5713 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
5714 ap->ops->bmdma_setup(qc); /* set up bmdma */
5715 ap->ops->bmdma_start(qc); /* initiate bmdma */
5716 ap->hsm_task_state = HSM_ST_LAST;
5717 break;
5719 case ATA_PROT_PIO:
5720 if (qc->tf.flags & ATA_TFLAG_POLLING)
5721 ata_qc_set_polling(qc);
5723 ata_tf_to_host(ap, &qc->tf);
5725 if (qc->tf.flags & ATA_TFLAG_WRITE) {
5726 /* PIO data out protocol */
5727 ap->hsm_task_state = HSM_ST_FIRST;
5728 ata_port_queue_task(ap, ata_pio_task, qc, 0);
5730 /* always send first data block using
5731 * the ata_pio_task() codepath.
5733 } else {
5734 /* PIO data in protocol */
5735 ap->hsm_task_state = HSM_ST;
5737 if (qc->tf.flags & ATA_TFLAG_POLLING)
5738 ata_port_queue_task(ap, ata_pio_task, qc, 0);
5740 /* if polling, ata_pio_task() handles the rest.
5741 * otherwise, interrupt handler takes over from here.
5745 break;
5747 case ATA_PROT_ATAPI:
5748 case ATA_PROT_ATAPI_NODATA:
5749 if (qc->tf.flags & ATA_TFLAG_POLLING)
5750 ata_qc_set_polling(qc);
5752 ata_tf_to_host(ap, &qc->tf);
5754 ap->hsm_task_state = HSM_ST_FIRST;
5756 /* send cdb by polling if no cdb interrupt */
5757 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
5758 (qc->tf.flags & ATA_TFLAG_POLLING))
5759 ata_port_queue_task(ap, ata_pio_task, qc, 0);
5760 break;
5762 case ATA_PROT_ATAPI_DMA:
5763 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
5765 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
5766 ap->ops->bmdma_setup(qc); /* set up bmdma */
5767 ap->hsm_task_state = HSM_ST_FIRST;
5769 /* send cdb by polling if no cdb interrupt */
5770 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
5771 ata_port_queue_task(ap, ata_pio_task, qc, 0);
5772 break;
5774 default:
5775 WARN_ON(1);
5776 return AC_ERR_SYSTEM;
5779 return 0;
5783 * ata_host_intr - Handle host interrupt for given (port, task)
5784 * @ap: Port on which interrupt arrived (possibly...)
5785 * @qc: Taskfile currently active in engine
5787 * Handle host interrupt for given queued command. Currently,
5788 * only DMA interrupts are handled. All other commands are
5789 * handled via polling with interrupts disabled (nIEN bit).
5791 * LOCKING:
5792 * spin_lock_irqsave(host lock)
5794 * RETURNS:
5795 * One if interrupt was handled, zero if not (shared irq).
5798 inline unsigned int ata_host_intr (struct ata_port *ap,
5799 struct ata_queued_cmd *qc)
5801 struct ata_eh_info *ehi = &ap->link.eh_info;
5802 u8 status, host_stat = 0;
5804 VPRINTK("ata%u: protocol %d task_state %d\n",
5805 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
5807 /* Check whether we are expecting interrupt in this state */
5808 switch (ap->hsm_task_state) {
5809 case HSM_ST_FIRST:
5810 /* Some pre-ATAPI-4 devices assert INTRQ
5811 * at this state when ready to receive CDB.
5814 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
5815 * The flag was turned on only for atapi devices.
5816 * No need to check is_atapi_taskfile(&qc->tf) again.
5818 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
5819 goto idle_irq;
5820 break;
5821 case HSM_ST_LAST:
5822 if (qc->tf.protocol == ATA_PROT_DMA ||
5823 qc->tf.protocol == ATA_PROT_ATAPI_DMA) {
5824 /* check status of DMA engine */
5825 host_stat = ap->ops->bmdma_status(ap);
5826 VPRINTK("ata%u: host_stat 0x%X\n",
5827 ap->print_id, host_stat);
5829 /* if it's not our irq... */
5830 if (!(host_stat & ATA_DMA_INTR))
5831 goto idle_irq;
5833 /* before we do anything else, clear DMA-Start bit */
5834 ap->ops->bmdma_stop(qc);
5836 if (unlikely(host_stat & ATA_DMA_ERR)) {
5837 /* error when transfering data to/from memory */
5838 qc->err_mask |= AC_ERR_HOST_BUS;
5839 ap->hsm_task_state = HSM_ST_ERR;
5842 break;
5843 case HSM_ST:
5844 break;
5845 default:
5846 goto idle_irq;
5849 /* check altstatus */
5850 status = ata_altstatus(ap);
5851 if (status & ATA_BUSY)
5852 goto idle_irq;
5854 /* check main status, clearing INTRQ */
5855 status = ata_chk_status(ap);
5856 if (unlikely(status & ATA_BUSY))
5857 goto idle_irq;
5859 /* ack bmdma irq events */
5860 ap->ops->irq_clear(ap);
5862 ata_hsm_move(ap, qc, status, 0);
5864 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
5865 qc->tf.protocol == ATA_PROT_ATAPI_DMA))
5866 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
5868 return 1; /* irq handled */
5870 idle_irq:
5871 ap->stats.idle_irq++;
5873 #ifdef ATA_IRQ_TRAP
5874 if ((ap->stats.idle_irq % 1000) == 0) {
5875 ata_chk_status(ap);
5876 ap->ops->irq_clear(ap);
5877 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
5878 return 1;
5880 #endif
5881 return 0; /* irq not handled */
5885 * ata_interrupt - Default ATA host interrupt handler
5886 * @irq: irq line (unused)
5887 * @dev_instance: pointer to our ata_host information structure
5889 * Default interrupt handler for PCI IDE devices. Calls
5890 * ata_host_intr() for each port that is not disabled.
5892 * LOCKING:
5893 * Obtains host lock during operation.
5895 * RETURNS:
5896 * IRQ_NONE or IRQ_HANDLED.
5899 irqreturn_t ata_interrupt (int irq, void *dev_instance)
5901 struct ata_host *host = dev_instance;
5902 unsigned int i;
5903 unsigned int handled = 0;
5904 unsigned long flags;
5906 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
5907 spin_lock_irqsave(&host->lock, flags);
5909 for (i = 0; i < host->n_ports; i++) {
5910 struct ata_port *ap;
5912 ap = host->ports[i];
5913 if (ap &&
5914 !(ap->flags & ATA_FLAG_DISABLED)) {
5915 struct ata_queued_cmd *qc;
5917 qc = ata_qc_from_tag(ap, ap->link.active_tag);
5918 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
5919 (qc->flags & ATA_QCFLAG_ACTIVE))
5920 handled |= ata_host_intr(ap, qc);
5924 spin_unlock_irqrestore(&host->lock, flags);
5926 return IRQ_RETVAL(handled);
5930 * sata_scr_valid - test whether SCRs are accessible
5931 * @link: ATA link to test SCR accessibility for
5933 * Test whether SCRs are accessible for @link.
5935 * LOCKING:
5936 * None.
5938 * RETURNS:
5939 * 1 if SCRs are accessible, 0 otherwise.
5941 int sata_scr_valid(struct ata_link *link)
5943 struct ata_port *ap = link->ap;
5945 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
5949 * sata_scr_read - read SCR register of the specified port
5950 * @link: ATA link to read SCR for
5951 * @reg: SCR to read
5952 * @val: Place to store read value
5954 * Read SCR register @reg of @link into *@val. This function is
5955 * guaranteed to succeed if the cable type of the port is SATA
5956 * and the port implements ->scr_read.
5958 * LOCKING:
5959 * None.
5961 * RETURNS:
5962 * 0 on success, negative errno on failure.
5964 int sata_scr_read(struct ata_link *link, int reg, u32 *val)
5966 struct ata_port *ap = link->ap;
5968 if (sata_scr_valid(link))
5969 return ap->ops->scr_read(ap, reg, val);
5970 return -EOPNOTSUPP;
5974 * sata_scr_write - write SCR register of the specified port
5975 * @link: ATA link to write SCR for
5976 * @reg: SCR to write
5977 * @val: value to write
5979 * Write @val to SCR register @reg of @link. This function is
5980 * guaranteed to succeed if the cable type of the port is SATA
5981 * and the port implements ->scr_read.
5983 * LOCKING:
5984 * None.
5986 * RETURNS:
5987 * 0 on success, negative errno on failure.
5989 int sata_scr_write(struct ata_link *link, int reg, u32 val)
5991 struct ata_port *ap = link->ap;
5993 if (sata_scr_valid(link))
5994 return ap->ops->scr_write(ap, reg, val);
5995 return -EOPNOTSUPP;
5999 * sata_scr_write_flush - write SCR register of the specified port and flush
6000 * @link: ATA link to write SCR for
6001 * @reg: SCR to write
6002 * @val: value to write
6004 * This function is identical to sata_scr_write() except that this
6005 * function performs flush after writing to the register.
6007 * LOCKING:
6008 * None.
6010 * RETURNS:
6011 * 0 on success, negative errno on failure.
6013 int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
6015 struct ata_port *ap = link->ap;
6016 int rc;
6018 if (sata_scr_valid(link)) {
6019 rc = ap->ops->scr_write(ap, reg, val);
6020 if (rc == 0)
6021 rc = ap->ops->scr_read(ap, reg, &val);
6022 return rc;
6024 return -EOPNOTSUPP;
6028 * ata_link_online - test whether the given link is online
6029 * @link: ATA link to test
6031 * Test whether @link is online. Note that this function returns
6032 * 0 if online status of @link cannot be obtained, so
6033 * ata_link_online(link) != !ata_link_offline(link).
6035 * LOCKING:
6036 * None.
6038 * RETURNS:
6039 * 1 if the port online status is available and online.
6041 int ata_link_online(struct ata_link *link)
6043 u32 sstatus;
6045 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
6046 (sstatus & 0xf) == 0x3)
6047 return 1;
6048 return 0;
6052 * ata_link_offline - test whether the given link is offline
6053 * @link: ATA link to test
6055 * Test whether @link is offline. Note that this function
6056 * returns 0 if offline status of @link cannot be obtained, so
6057 * ata_link_online(link) != !ata_link_offline(link).
6059 * LOCKING:
6060 * None.
6062 * RETURNS:
6063 * 1 if the port offline status is available and offline.
6065 int ata_link_offline(struct ata_link *link)
6067 u32 sstatus;
6069 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
6070 (sstatus & 0xf) != 0x3)
6071 return 1;
6072 return 0;
6075 int ata_flush_cache(struct ata_device *dev)
6077 unsigned int err_mask;
6078 u8 cmd;
6080 if (!ata_try_flush_cache(dev))
6081 return 0;
6083 if (dev->flags & ATA_DFLAG_FLUSH_EXT)
6084 cmd = ATA_CMD_FLUSH_EXT;
6085 else
6086 cmd = ATA_CMD_FLUSH;
6088 /* This is wrong. On a failed flush we get back the LBA of the lost
6089 sector and we should (assuming it wasn't aborted as unknown) issue
6090 a further flush command to continue the writeback until it
6091 does not error */
6092 err_mask = ata_do_simple_cmd(dev, cmd);
6093 if (err_mask) {
6094 ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
6095 return -EIO;
6098 return 0;
6101 #ifdef CONFIG_PM
6102 static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
6103 unsigned int action, unsigned int ehi_flags,
6104 int wait)
6106 unsigned long flags;
6107 int i, rc;
6109 for (i = 0; i < host->n_ports; i++) {
6110 struct ata_port *ap = host->ports[i];
6111 struct ata_link *link;
6113 /* Previous resume operation might still be in
6114 * progress. Wait for PM_PENDING to clear.
6116 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
6117 ata_port_wait_eh(ap);
6118 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
6121 /* request PM ops to EH */
6122 spin_lock_irqsave(ap->lock, flags);
6124 ap->pm_mesg = mesg;
6125 if (wait) {
6126 rc = 0;
6127 ap->pm_result = &rc;
6130 ap->pflags |= ATA_PFLAG_PM_PENDING;
6131 __ata_port_for_each_link(link, ap) {
6132 link->eh_info.action |= action;
6133 link->eh_info.flags |= ehi_flags;
6136 ata_port_schedule_eh(ap);
6138 spin_unlock_irqrestore(ap->lock, flags);
6140 /* wait and check result */
6141 if (wait) {
6142 ata_port_wait_eh(ap);
6143 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
6144 if (rc)
6145 return rc;
6149 return 0;
6153 * ata_host_suspend - suspend host
6154 * @host: host to suspend
6155 * @mesg: PM message
6157 * Suspend @host. Actual operation is performed by EH. This
6158 * function requests EH to perform PM operations and waits for EH
6159 * to finish.
6161 * LOCKING:
6162 * Kernel thread context (may sleep).
6164 * RETURNS:
6165 * 0 on success, -errno on failure.
6167 int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
6169 int rc;
6171 rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
6172 if (rc == 0)
6173 host->dev->power.power_state = mesg;
6174 return rc;
6178 * ata_host_resume - resume host
6179 * @host: host to resume
6181 * Resume @host. Actual operation is performed by EH. This
6182 * function requests EH to perform PM operations and returns.
6183 * Note that all resume operations are performed parallely.
6185 * LOCKING:
6186 * Kernel thread context (may sleep).
6188 void ata_host_resume(struct ata_host *host)
6190 ata_host_request_pm(host, PMSG_ON, ATA_EH_SOFTRESET,
6191 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
6192 host->dev->power.power_state = PMSG_ON;
6194 #endif
6197 * ata_port_start - Set port up for dma.
6198 * @ap: Port to initialize
6200 * Called just after data structures for each port are
6201 * initialized. Allocates space for PRD table.
6203 * May be used as the port_start() entry in ata_port_operations.
6205 * LOCKING:
6206 * Inherited from caller.
6208 int ata_port_start(struct ata_port *ap)
6210 struct device *dev = ap->dev;
6211 int rc;
6213 ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
6214 GFP_KERNEL);
6215 if (!ap->prd)
6216 return -ENOMEM;
6218 rc = ata_pad_alloc(ap, dev);
6219 if (rc)
6220 return rc;
6222 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd,
6223 (unsigned long long)ap->prd_dma);
6224 return 0;
6228 * ata_dev_init - Initialize an ata_device structure
6229 * @dev: Device structure to initialize
6231 * Initialize @dev in preparation for probing.
6233 * LOCKING:
6234 * Inherited from caller.
6236 void ata_dev_init(struct ata_device *dev)
6238 struct ata_link *link = dev->link;
6239 struct ata_port *ap = link->ap;
6240 unsigned long flags;
6242 /* SATA spd limit is bound to the first device */
6243 link->sata_spd_limit = link->hw_sata_spd_limit;
6244 link->sata_spd = 0;
6246 /* High bits of dev->flags are used to record warm plug
6247 * requests which occur asynchronously. Synchronize using
6248 * host lock.
6250 spin_lock_irqsave(ap->lock, flags);
6251 dev->flags &= ~ATA_DFLAG_INIT_MASK;
6252 dev->horkage = 0;
6253 spin_unlock_irqrestore(ap->lock, flags);
6255 memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
6256 sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
6257 dev->pio_mask = UINT_MAX;
6258 dev->mwdma_mask = UINT_MAX;
6259 dev->udma_mask = UINT_MAX;
6263 * ata_link_init - Initialize an ata_link structure
6264 * @ap: ATA port link is attached to
6265 * @link: Link structure to initialize
6266 * @pmp: Port multiplier port number
6268 * Initialize @link.
6270 * LOCKING:
6271 * Kernel thread context (may sleep)
6273 void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
6275 int i;
6277 /* clear everything except for devices */
6278 memset(link, 0, offsetof(struct ata_link, device[0]));
6280 link->ap = ap;
6281 link->pmp = pmp;
6282 link->active_tag = ATA_TAG_POISON;
6283 link->hw_sata_spd_limit = UINT_MAX;
6285 /* can't use iterator, ap isn't initialized yet */
6286 for (i = 0; i < ATA_MAX_DEVICES; i++) {
6287 struct ata_device *dev = &link->device[i];
6289 dev->link = link;
6290 dev->devno = dev - link->device;
6291 ata_dev_init(dev);
6296 * sata_link_init_spd - Initialize link->sata_spd_limit
6297 * @link: Link to configure sata_spd_limit for
6299 * Initialize @link->[hw_]sata_spd_limit to the currently
6300 * configured value.
6302 * LOCKING:
6303 * Kernel thread context (may sleep).
6305 * RETURNS:
6306 * 0 on success, -errno on failure.
6308 int sata_link_init_spd(struct ata_link *link)
6310 u32 scontrol, spd;
6311 int rc;
6313 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
6314 if (rc)
6315 return rc;
6317 spd = (scontrol >> 4) & 0xf;
6318 if (spd)
6319 link->hw_sata_spd_limit &= (1 << spd) - 1;
6321 link->sata_spd_limit = link->hw_sata_spd_limit;
6323 return 0;
6327 * ata_port_alloc - allocate and initialize basic ATA port resources
6328 * @host: ATA host this allocated port belongs to
6330 * Allocate and initialize basic ATA port resources.
6332 * RETURNS:
6333 * Allocate ATA port on success, NULL on failure.
6335 * LOCKING:
6336 * Inherited from calling layer (may sleep).
6338 struct ata_port *ata_port_alloc(struct ata_host *host)
6340 struct ata_port *ap;
6342 DPRINTK("ENTER\n");
6344 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
6345 if (!ap)
6346 return NULL;
6348 ap->pflags |= ATA_PFLAG_INITIALIZING;
6349 ap->lock = &host->lock;
6350 ap->flags = ATA_FLAG_DISABLED;
6351 ap->print_id = -1;
6352 ap->ctl = ATA_DEVCTL_OBS;
6353 ap->host = host;
6354 ap->dev = host->dev;
6355 ap->last_ctl = 0xFF;
6357 #if defined(ATA_VERBOSE_DEBUG)
6358 /* turn on all debugging levels */
6359 ap->msg_enable = 0x00FF;
6360 #elif defined(ATA_DEBUG)
6361 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
6362 #else
6363 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
6364 #endif
6366 INIT_DELAYED_WORK(&ap->port_task, NULL);
6367 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
6368 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
6369 INIT_LIST_HEAD(&ap->eh_done_q);
6370 init_waitqueue_head(&ap->eh_wait_q);
6371 init_timer_deferrable(&ap->fastdrain_timer);
6372 ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
6373 ap->fastdrain_timer.data = (unsigned long)ap;
6375 ap->cbl = ATA_CBL_NONE;
6377 ata_link_init(ap, &ap->link, 0);
6379 #ifdef ATA_IRQ_TRAP
6380 ap->stats.unhandled_irq = 1;
6381 ap->stats.idle_irq = 1;
6382 #endif
6383 return ap;
6386 static void ata_host_release(struct device *gendev, void *res)
6388 struct ata_host *host = dev_get_drvdata(gendev);
6389 int i;
6391 for (i = 0; i < host->n_ports; i++) {
6392 struct ata_port *ap = host->ports[i];
6394 if (!ap)
6395 continue;
6397 if ((host->flags & ATA_HOST_STARTED) && ap->ops->port_stop)
6398 ap->ops->port_stop(ap);
6401 if ((host->flags & ATA_HOST_STARTED) && host->ops->host_stop)
6402 host->ops->host_stop(host);
6404 for (i = 0; i < host->n_ports; i++) {
6405 struct ata_port *ap = host->ports[i];
6407 if (!ap)
6408 continue;
6410 if (ap->scsi_host)
6411 scsi_host_put(ap->scsi_host);
6413 kfree(ap);
6414 host->ports[i] = NULL;
6417 dev_set_drvdata(gendev, NULL);
6421 * ata_host_alloc - allocate and init basic ATA host resources
6422 * @dev: generic device this host is associated with
6423 * @max_ports: maximum number of ATA ports associated with this host
6425 * Allocate and initialize basic ATA host resources. LLD calls
6426 * this function to allocate a host, initializes it fully and
6427 * attaches it using ata_host_register().
6429 * @max_ports ports are allocated and host->n_ports is
6430 * initialized to @max_ports. The caller is allowed to decrease
6431 * host->n_ports before calling ata_host_register(). The unused
6432 * ports will be automatically freed on registration.
6434 * RETURNS:
6435 * Allocate ATA host on success, NULL on failure.
6437 * LOCKING:
6438 * Inherited from calling layer (may sleep).
6440 struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
6442 struct ata_host *host;
6443 size_t sz;
6444 int i;
6446 DPRINTK("ENTER\n");
6448 if (!devres_open_group(dev, NULL, GFP_KERNEL))
6449 return NULL;
6451 /* alloc a container for our list of ATA ports (buses) */
6452 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
6453 /* alloc a container for our list of ATA ports (buses) */
6454 host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
6455 if (!host)
6456 goto err_out;
6458 devres_add(dev, host);
6459 dev_set_drvdata(dev, host);
6461 spin_lock_init(&host->lock);
6462 host->dev = dev;
6463 host->n_ports = max_ports;
6465 /* allocate ports bound to this host */
6466 for (i = 0; i < max_ports; i++) {
6467 struct ata_port *ap;
6469 ap = ata_port_alloc(host);
6470 if (!ap)
6471 goto err_out;
6473 ap->port_no = i;
6474 host->ports[i] = ap;
6477 devres_remove_group(dev, NULL);
6478 return host;
6480 err_out:
6481 devres_release_group(dev, NULL);
6482 return NULL;
6486 * ata_host_alloc_pinfo - alloc host and init with port_info array
6487 * @dev: generic device this host is associated with
6488 * @ppi: array of ATA port_info to initialize host with
6489 * @n_ports: number of ATA ports attached to this host
6491 * Allocate ATA host and initialize with info from @ppi. If NULL
6492 * terminated, @ppi may contain fewer entries than @n_ports. The
6493 * last entry will be used for the remaining ports.
6495 * RETURNS:
6496 * Allocate ATA host on success, NULL on failure.
6498 * LOCKING:
6499 * Inherited from calling layer (may sleep).
6501 struct ata_host *ata_host_alloc_pinfo(struct device *dev,
6502 const struct ata_port_info * const * ppi,
6503 int n_ports)
6505 const struct ata_port_info *pi;
6506 struct ata_host *host;
6507 int i, j;
6509 host = ata_host_alloc(dev, n_ports);
6510 if (!host)
6511 return NULL;
6513 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
6514 struct ata_port *ap = host->ports[i];
6516 if (ppi[j])
6517 pi = ppi[j++];
6519 ap->pio_mask = pi->pio_mask;
6520 ap->mwdma_mask = pi->mwdma_mask;
6521 ap->udma_mask = pi->udma_mask;
6522 ap->flags |= pi->flags;
6523 ap->link.flags |= pi->link_flags;
6524 ap->ops = pi->port_ops;
6526 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
6527 host->ops = pi->port_ops;
6528 if (!host->private_data && pi->private_data)
6529 host->private_data = pi->private_data;
6532 return host;
6536 * ata_host_start - start and freeze ports of an ATA host
6537 * @host: ATA host to start ports for
6539 * Start and then freeze ports of @host. Started status is
6540 * recorded in host->flags, so this function can be called
6541 * multiple times. Ports are guaranteed to get started only
6542 * once. If host->ops isn't initialized yet, its set to the
6543 * first non-dummy port ops.
6545 * LOCKING:
6546 * Inherited from calling layer (may sleep).
6548 * RETURNS:
6549 * 0 if all ports are started successfully, -errno otherwise.
6551 int ata_host_start(struct ata_host *host)
6553 int i, rc;
6555 if (host->flags & ATA_HOST_STARTED)
6556 return 0;
6558 for (i = 0; i < host->n_ports; i++) {
6559 struct ata_port *ap = host->ports[i];
6561 if (!host->ops && !ata_port_is_dummy(ap))
6562 host->ops = ap->ops;
6564 if (ap->ops->port_start) {
6565 rc = ap->ops->port_start(ap);
6566 if (rc) {
6567 ata_port_printk(ap, KERN_ERR, "failed to "
6568 "start port (errno=%d)\n", rc);
6569 goto err_out;
6573 ata_eh_freeze_port(ap);
6576 host->flags |= ATA_HOST_STARTED;
6577 return 0;
6579 err_out:
6580 while (--i >= 0) {
6581 struct ata_port *ap = host->ports[i];
6583 if (ap->ops->port_stop)
6584 ap->ops->port_stop(ap);
6586 return rc;
6590 * ata_sas_host_init - Initialize a host struct
6591 * @host: host to initialize
6592 * @dev: device host is attached to
6593 * @flags: host flags
6594 * @ops: port_ops
6596 * LOCKING:
6597 * PCI/etc. bus probe sem.
6600 /* KILLME - the only user left is ipr */
6601 void ata_host_init(struct ata_host *host, struct device *dev,
6602 unsigned long flags, const struct ata_port_operations *ops)
6604 spin_lock_init(&host->lock);
6605 host->dev = dev;
6606 host->flags = flags;
6607 host->ops = ops;
6611 * ata_host_register - register initialized ATA host
6612 * @host: ATA host to register
6613 * @sht: template for SCSI host
6615 * Register initialized ATA host. @host is allocated using
6616 * ata_host_alloc() and fully initialized by LLD. This function
6617 * starts ports, registers @host with ATA and SCSI layers and
6618 * probe registered devices.
6620 * LOCKING:
6621 * Inherited from calling layer (may sleep).
6623 * RETURNS:
6624 * 0 on success, -errno otherwise.
6626 int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
6628 int i, rc;
6630 /* host must have been started */
6631 if (!(host->flags & ATA_HOST_STARTED)) {
6632 dev_printk(KERN_ERR, host->dev,
6633 "BUG: trying to register unstarted host\n");
6634 WARN_ON(1);
6635 return -EINVAL;
6638 /* Blow away unused ports. This happens when LLD can't
6639 * determine the exact number of ports to allocate at
6640 * allocation time.
6642 for (i = host->n_ports; host->ports[i]; i++)
6643 kfree(host->ports[i]);
6645 /* give ports names and add SCSI hosts */
6646 for (i = 0; i < host->n_ports; i++)
6647 host->ports[i]->print_id = ata_print_id++;
6649 rc = ata_scsi_add_hosts(host, sht);
6650 if (rc)
6651 return rc;
6653 /* associate with ACPI nodes */
6654 ata_acpi_associate(host);
6656 /* set cable, sata_spd_limit and report */
6657 for (i = 0; i < host->n_ports; i++) {
6658 struct ata_port *ap = host->ports[i];
6659 unsigned long xfer_mask;
6661 /* set SATA cable type if still unset */
6662 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6663 ap->cbl = ATA_CBL_SATA;
6665 /* init sata_spd_limit to the current value */
6666 sata_link_init_spd(&ap->link);
6668 /* print per-port info to dmesg */
6669 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6670 ap->udma_mask);
6672 if (!ata_port_is_dummy(ap))
6673 ata_port_printk(ap, KERN_INFO,
6674 "%cATA max %s %s\n",
6675 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
6676 ata_mode_string(xfer_mask),
6677 ap->link.eh_info.desc);
6678 else
6679 ata_port_printk(ap, KERN_INFO, "DUMMY\n");
6682 /* perform each probe synchronously */
6683 DPRINTK("probe begin\n");
6684 for (i = 0; i < host->n_ports; i++) {
6685 struct ata_port *ap = host->ports[i];
6686 int rc;
6688 /* probe */
6689 if (ap->ops->error_handler) {
6690 struct ata_eh_info *ehi = &ap->link.eh_info;
6691 unsigned long flags;
6693 ata_port_probe(ap);
6695 /* kick EH for boot probing */
6696 spin_lock_irqsave(ap->lock, flags);
6698 ehi->probe_mask =
6699 (1 << ata_link_max_devices(&ap->link)) - 1;
6700 ehi->action |= ATA_EH_SOFTRESET;
6701 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
6703 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
6704 ap->pflags |= ATA_PFLAG_LOADING;
6705 ata_port_schedule_eh(ap);
6707 spin_unlock_irqrestore(ap->lock, flags);
6709 /* wait for EH to finish */
6710 ata_port_wait_eh(ap);
6711 } else {
6712 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
6713 rc = ata_bus_probe(ap);
6714 DPRINTK("ata%u: bus probe end\n", ap->print_id);
6716 if (rc) {
6717 /* FIXME: do something useful here?
6718 * Current libata behavior will
6719 * tear down everything when
6720 * the module is removed
6721 * or the h/w is unplugged.
6727 /* probes are done, now scan each port's disk(s) */
6728 DPRINTK("host probe begin\n");
6729 for (i = 0; i < host->n_ports; i++) {
6730 struct ata_port *ap = host->ports[i];
6732 ata_scsi_scan_host(ap, 1);
6735 return 0;
6739 * ata_host_activate - start host, request IRQ and register it
6740 * @host: target ATA host
6741 * @irq: IRQ to request
6742 * @irq_handler: irq_handler used when requesting IRQ
6743 * @irq_flags: irq_flags used when requesting IRQ
6744 * @sht: scsi_host_template to use when registering the host
6746 * After allocating an ATA host and initializing it, most libata
6747 * LLDs perform three steps to activate the host - start host,
6748 * request IRQ and register it. This helper takes necessasry
6749 * arguments and performs the three steps in one go.
6751 * LOCKING:
6752 * Inherited from calling layer (may sleep).
6754 * RETURNS:
6755 * 0 on success, -errno otherwise.
6757 int ata_host_activate(struct ata_host *host, int irq,
6758 irq_handler_t irq_handler, unsigned long irq_flags,
6759 struct scsi_host_template *sht)
6761 int i, rc;
6763 rc = ata_host_start(host);
6764 if (rc)
6765 return rc;
6767 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6768 dev_driver_string(host->dev), host);
6769 if (rc)
6770 return rc;
6772 for (i = 0; i < host->n_ports; i++)
6773 ata_port_desc(host->ports[i], "irq %d", irq);
6775 rc = ata_host_register(host, sht);
6776 /* if failed, just free the IRQ and leave ports alone */
6777 if (rc)
6778 devm_free_irq(host->dev, irq, host);
6780 return rc;
6784 * ata_port_detach - Detach ATA port in prepration of device removal
6785 * @ap: ATA port to be detached
6787 * Detach all ATA devices and the associated SCSI devices of @ap;
6788 * then, remove the associated SCSI host. @ap is guaranteed to
6789 * be quiescent on return from this function.
6791 * LOCKING:
6792 * Kernel thread context (may sleep).
6794 void ata_port_detach(struct ata_port *ap)
6796 unsigned long flags;
6797 struct ata_link *link;
6798 struct ata_device *dev;
6800 if (!ap->ops->error_handler)
6801 goto skip_eh;
6803 /* tell EH we're leaving & flush EH */
6804 spin_lock_irqsave(ap->lock, flags);
6805 ap->pflags |= ATA_PFLAG_UNLOADING;
6806 spin_unlock_irqrestore(ap->lock, flags);
6808 ata_port_wait_eh(ap);
6810 /* EH is now guaranteed to see UNLOADING, so no new device
6811 * will be attached. Disable all existing devices.
6813 spin_lock_irqsave(ap->lock, flags);
6815 ata_port_for_each_link(link, ap) {
6816 ata_link_for_each_dev(dev, link)
6817 ata_dev_disable(dev);
6820 spin_unlock_irqrestore(ap->lock, flags);
6822 /* Final freeze & EH. All in-flight commands are aborted. EH
6823 * will be skipped and retrials will be terminated with bad
6824 * target.
6826 spin_lock_irqsave(ap->lock, flags);
6827 ata_port_freeze(ap); /* won't be thawed */
6828 spin_unlock_irqrestore(ap->lock, flags);
6830 ata_port_wait_eh(ap);
6831 cancel_rearming_delayed_work(&ap->hotplug_task);
6833 skip_eh:
6834 /* remove the associated SCSI host */
6835 scsi_remove_host(ap->scsi_host);
6839 * ata_host_detach - Detach all ports of an ATA host
6840 * @host: Host to detach
6842 * Detach all ports of @host.
6844 * LOCKING:
6845 * Kernel thread context (may sleep).
6847 void ata_host_detach(struct ata_host *host)
6849 int i;
6851 for (i = 0; i < host->n_ports; i++)
6852 ata_port_detach(host->ports[i]);
6856 * ata_std_ports - initialize ioaddr with standard port offsets.
6857 * @ioaddr: IO address structure to be initialized
6859 * Utility function which initializes data_addr, error_addr,
6860 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
6861 * device_addr, status_addr, and command_addr to standard offsets
6862 * relative to cmd_addr.
6864 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
6867 void ata_std_ports(struct ata_ioports *ioaddr)
6869 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
6870 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
6871 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
6872 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
6873 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
6874 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
6875 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
6876 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
6877 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
6878 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
6882 #ifdef CONFIG_PCI
6885 * ata_pci_remove_one - PCI layer callback for device removal
6886 * @pdev: PCI device that was removed
6888 * PCI layer indicates to libata via this hook that hot-unplug or
6889 * module unload event has occurred. Detach all ports. Resource
6890 * release is handled via devres.
6892 * LOCKING:
6893 * Inherited from PCI layer (may sleep).
6895 void ata_pci_remove_one(struct pci_dev *pdev)
6897 struct device *dev = pci_dev_to_dev(pdev);
6898 struct ata_host *host = dev_get_drvdata(dev);
6900 ata_host_detach(host);
6903 /* move to PCI subsystem */
6904 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6906 unsigned long tmp = 0;
6908 switch (bits->width) {
6909 case 1: {
6910 u8 tmp8 = 0;
6911 pci_read_config_byte(pdev, bits->reg, &tmp8);
6912 tmp = tmp8;
6913 break;
6915 case 2: {
6916 u16 tmp16 = 0;
6917 pci_read_config_word(pdev, bits->reg, &tmp16);
6918 tmp = tmp16;
6919 break;
6921 case 4: {
6922 u32 tmp32 = 0;
6923 pci_read_config_dword(pdev, bits->reg, &tmp32);
6924 tmp = tmp32;
6925 break;
6928 default:
6929 return -EINVAL;
6932 tmp &= bits->mask;
6934 return (tmp == bits->val) ? 1 : 0;
6937 #ifdef CONFIG_PM
6938 void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6940 pci_save_state(pdev);
6941 pci_disable_device(pdev);
6943 if (mesg.event == PM_EVENT_SUSPEND)
6944 pci_set_power_state(pdev, PCI_D3hot);
6947 int ata_pci_device_do_resume(struct pci_dev *pdev)
6949 int rc;
6951 pci_set_power_state(pdev, PCI_D0);
6952 pci_restore_state(pdev);
6954 rc = pcim_enable_device(pdev);
6955 if (rc) {
6956 dev_printk(KERN_ERR, &pdev->dev,
6957 "failed to enable device after resume (%d)\n", rc);
6958 return rc;
6961 pci_set_master(pdev);
6962 return 0;
6965 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6967 struct ata_host *host = dev_get_drvdata(&pdev->dev);
6968 int rc = 0;
6970 rc = ata_host_suspend(host, mesg);
6971 if (rc)
6972 return rc;
6974 ata_pci_device_do_suspend(pdev, mesg);
6976 return 0;
6979 int ata_pci_device_resume(struct pci_dev *pdev)
6981 struct ata_host *host = dev_get_drvdata(&pdev->dev);
6982 int rc;
6984 rc = ata_pci_device_do_resume(pdev);
6985 if (rc == 0)
6986 ata_host_resume(host);
6987 return rc;
6989 #endif /* CONFIG_PM */
6991 #endif /* CONFIG_PCI */
6994 static int __init ata_init(void)
6996 ata_probe_timeout *= HZ;
6997 ata_wq = create_workqueue("ata");
6998 if (!ata_wq)
6999 return -ENOMEM;
7001 ata_aux_wq = create_singlethread_workqueue("ata_aux");
7002 if (!ata_aux_wq) {
7003 destroy_workqueue(ata_wq);
7004 return -ENOMEM;
7007 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
7008 return 0;
7011 static void __exit ata_exit(void)
7013 destroy_workqueue(ata_wq);
7014 destroy_workqueue(ata_aux_wq);
7017 subsys_initcall(ata_init);
7018 module_exit(ata_exit);
7020 static unsigned long ratelimit_time;
7021 static DEFINE_SPINLOCK(ata_ratelimit_lock);
7023 int ata_ratelimit(void)
7025 int rc;
7026 unsigned long flags;
7028 spin_lock_irqsave(&ata_ratelimit_lock, flags);
7030 if (time_after(jiffies, ratelimit_time)) {
7031 rc = 1;
7032 ratelimit_time = jiffies + (HZ/5);
7033 } else
7034 rc = 0;
7036 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
7038 return rc;
7042 * ata_wait_register - wait until register value changes
7043 * @reg: IO-mapped register
7044 * @mask: Mask to apply to read register value
7045 * @val: Wait condition
7046 * @interval_msec: polling interval in milliseconds
7047 * @timeout_msec: timeout in milliseconds
7049 * Waiting for some bits of register to change is a common
7050 * operation for ATA controllers. This function reads 32bit LE
7051 * IO-mapped register @reg and tests for the following condition.
7053 * (*@reg & mask) != val
7055 * If the condition is met, it returns; otherwise, the process is
7056 * repeated after @interval_msec until timeout.
7058 * LOCKING:
7059 * Kernel thread context (may sleep)
7061 * RETURNS:
7062 * The final register value.
7064 u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
7065 unsigned long interval_msec,
7066 unsigned long timeout_msec)
7068 unsigned long timeout;
7069 u32 tmp;
7071 tmp = ioread32(reg);
7073 /* Calculate timeout _after_ the first read to make sure
7074 * preceding writes reach the controller before starting to
7075 * eat away the timeout.
7077 timeout = jiffies + (timeout_msec * HZ) / 1000;
7079 while ((tmp & mask) == val && time_before(jiffies, timeout)) {
7080 msleep(interval_msec);
7081 tmp = ioread32(reg);
7084 return tmp;
7088 * Dummy port_ops
7090 static void ata_dummy_noret(struct ata_port *ap) { }
7091 static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
7092 static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
7094 static u8 ata_dummy_check_status(struct ata_port *ap)
7096 return ATA_DRDY;
7099 static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
7101 return AC_ERR_SYSTEM;
7104 const struct ata_port_operations ata_dummy_port_ops = {
7105 .check_status = ata_dummy_check_status,
7106 .check_altstatus = ata_dummy_check_status,
7107 .dev_select = ata_noop_dev_select,
7108 .qc_prep = ata_noop_qc_prep,
7109 .qc_issue = ata_dummy_qc_issue,
7110 .freeze = ata_dummy_noret,
7111 .thaw = ata_dummy_noret,
7112 .error_handler = ata_dummy_noret,
7113 .post_internal_cmd = ata_dummy_qc_noret,
7114 .irq_clear = ata_dummy_noret,
7115 .port_start = ata_dummy_ret0,
7116 .port_stop = ata_dummy_noret,
7119 const struct ata_port_info ata_dummy_port_info = {
7120 .port_ops = &ata_dummy_port_ops,
7124 * libata is essentially a library of internal helper functions for
7125 * low-level ATA host controller drivers. As such, the API/ABI is
7126 * likely to change as new drivers are added and updated.
7127 * Do not depend on ABI/API stability.
7130 EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
7131 EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
7132 EXPORT_SYMBOL_GPL(sata_deb_timing_long);
7133 EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
7134 EXPORT_SYMBOL_GPL(ata_dummy_port_info);
7135 EXPORT_SYMBOL_GPL(ata_std_bios_param);
7136 EXPORT_SYMBOL_GPL(ata_std_ports);
7137 EXPORT_SYMBOL_GPL(ata_host_init);
7138 EXPORT_SYMBOL_GPL(ata_host_alloc);
7139 EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
7140 EXPORT_SYMBOL_GPL(ata_host_start);
7141 EXPORT_SYMBOL_GPL(ata_host_register);
7142 EXPORT_SYMBOL_GPL(ata_host_activate);
7143 EXPORT_SYMBOL_GPL(ata_host_detach);
7144 EXPORT_SYMBOL_GPL(ata_sg_init);
7145 EXPORT_SYMBOL_GPL(ata_sg_init_one);
7146 EXPORT_SYMBOL_GPL(ata_hsm_move);
7147 EXPORT_SYMBOL_GPL(ata_qc_complete);
7148 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
7149 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
7150 EXPORT_SYMBOL_GPL(ata_tf_load);
7151 EXPORT_SYMBOL_GPL(ata_tf_read);
7152 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
7153 EXPORT_SYMBOL_GPL(ata_std_dev_select);
7154 EXPORT_SYMBOL_GPL(sata_print_link_status);
7155 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
7156 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
7157 EXPORT_SYMBOL_GPL(ata_check_status);
7158 EXPORT_SYMBOL_GPL(ata_altstatus);
7159 EXPORT_SYMBOL_GPL(ata_exec_command);
7160 EXPORT_SYMBOL_GPL(ata_port_start);
7161 EXPORT_SYMBOL_GPL(ata_sff_port_start);
7162 EXPORT_SYMBOL_GPL(ata_interrupt);
7163 EXPORT_SYMBOL_GPL(ata_do_set_mode);
7164 EXPORT_SYMBOL_GPL(ata_data_xfer);
7165 EXPORT_SYMBOL_GPL(ata_data_xfer_noirq);
7166 EXPORT_SYMBOL_GPL(ata_std_qc_defer);
7167 EXPORT_SYMBOL_GPL(ata_qc_prep);
7168 EXPORT_SYMBOL_GPL(ata_dumb_qc_prep);
7169 EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
7170 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
7171 EXPORT_SYMBOL_GPL(ata_bmdma_start);
7172 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
7173 EXPORT_SYMBOL_GPL(ata_bmdma_status);
7174 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
7175 EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
7176 EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
7177 EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
7178 EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
7179 EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
7180 EXPORT_SYMBOL_GPL(ata_port_probe);
7181 EXPORT_SYMBOL_GPL(ata_dev_disable);
7182 EXPORT_SYMBOL_GPL(sata_set_spd);
7183 EXPORT_SYMBOL_GPL(sata_link_debounce);
7184 EXPORT_SYMBOL_GPL(sata_link_resume);
7185 EXPORT_SYMBOL_GPL(sata_phy_reset);
7186 EXPORT_SYMBOL_GPL(__sata_phy_reset);
7187 EXPORT_SYMBOL_GPL(ata_bus_reset);
7188 EXPORT_SYMBOL_GPL(ata_std_prereset);
7189 EXPORT_SYMBOL_GPL(ata_std_softreset);
7190 EXPORT_SYMBOL_GPL(sata_link_hardreset);
7191 EXPORT_SYMBOL_GPL(sata_std_hardreset);
7192 EXPORT_SYMBOL_GPL(ata_std_postreset);
7193 EXPORT_SYMBOL_GPL(ata_dev_classify);
7194 EXPORT_SYMBOL_GPL(ata_dev_pair);
7195 EXPORT_SYMBOL_GPL(ata_port_disable);
7196 EXPORT_SYMBOL_GPL(ata_ratelimit);
7197 EXPORT_SYMBOL_GPL(ata_wait_register);
7198 EXPORT_SYMBOL_GPL(ata_busy_sleep);
7199 EXPORT_SYMBOL_GPL(ata_wait_ready);
7200 EXPORT_SYMBOL_GPL(ata_port_queue_task);
7201 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
7202 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
7203 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
7204 EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
7205 EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
7206 EXPORT_SYMBOL_GPL(ata_host_intr);
7207 EXPORT_SYMBOL_GPL(sata_scr_valid);
7208 EXPORT_SYMBOL_GPL(sata_scr_read);
7209 EXPORT_SYMBOL_GPL(sata_scr_write);
7210 EXPORT_SYMBOL_GPL(sata_scr_write_flush);
7211 EXPORT_SYMBOL_GPL(ata_link_online);
7212 EXPORT_SYMBOL_GPL(ata_link_offline);
7213 #ifdef CONFIG_PM
7214 EXPORT_SYMBOL_GPL(ata_host_suspend);
7215 EXPORT_SYMBOL_GPL(ata_host_resume);
7216 #endif /* CONFIG_PM */
7217 EXPORT_SYMBOL_GPL(ata_id_string);
7218 EXPORT_SYMBOL_GPL(ata_id_c_string);
7219 EXPORT_SYMBOL_GPL(ata_id_to_dma_mode);
7220 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
7222 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
7223 EXPORT_SYMBOL_GPL(ata_timing_compute);
7224 EXPORT_SYMBOL_GPL(ata_timing_merge);
7226 #ifdef CONFIG_PCI
7227 EXPORT_SYMBOL_GPL(pci_test_config_bits);
7228 EXPORT_SYMBOL_GPL(ata_pci_init_sff_host);
7229 EXPORT_SYMBOL_GPL(ata_pci_init_bmdma);
7230 EXPORT_SYMBOL_GPL(ata_pci_prepare_sff_host);
7231 EXPORT_SYMBOL_GPL(ata_pci_init_one);
7232 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
7233 #ifdef CONFIG_PM
7234 EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
7235 EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
7236 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
7237 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
7238 #endif /* CONFIG_PM */
7239 EXPORT_SYMBOL_GPL(ata_pci_default_filter);
7240 EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
7241 #endif /* CONFIG_PCI */
7243 EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
7244 EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
7245 EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
7246 EXPORT_SYMBOL_GPL(ata_port_desc);
7247 #ifdef CONFIG_PCI
7248 EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
7249 #endif /* CONFIG_PCI */
7250 EXPORT_SYMBOL_GPL(ata_eng_timeout);
7251 EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
7252 EXPORT_SYMBOL_GPL(ata_link_abort);
7253 EXPORT_SYMBOL_GPL(ata_port_abort);
7254 EXPORT_SYMBOL_GPL(ata_port_freeze);
7255 EXPORT_SYMBOL_GPL(sata_async_notification);
7256 EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
7257 EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
7258 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
7259 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
7260 EXPORT_SYMBOL_GPL(ata_do_eh);
7261 EXPORT_SYMBOL_GPL(ata_irq_on);
7262 EXPORT_SYMBOL_GPL(ata_dev_try_classify);
7264 EXPORT_SYMBOL_GPL(ata_cable_40wire);
7265 EXPORT_SYMBOL_GPL(ata_cable_80wire);
7266 EXPORT_SYMBOL_GPL(ata_cable_unknown);
7267 EXPORT_SYMBOL_GPL(ata_cable_sata);