Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / ata / libata-core.c
blob785f5daec7ecb39fa9be932dc6b78c882b499b4e
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/
33 * Standards documents from:
34 * http://www.t13.org (ATA standards, PCI DMA IDE spec)
35 * http://www.t10.org (SCSI MMC - for ATAPI MMC)
36 * http://www.sata-io.org (SATA)
37 * http://www.compactflash.org (CF)
38 * http://www.qic.org (QIC157 - Tape and DSC)
39 * http://www.ce-ata.org (CE-ATA: not supported)
43 #include <linux/kernel.h>
44 #include <linux/module.h>
45 #include <linux/pci.h>
46 #include <linux/init.h>
47 #include <linux/list.h>
48 #include <linux/mm.h>
49 #include <linux/highmem.h>
50 #include <linux/spinlock.h>
51 #include <linux/blkdev.h>
52 #include <linux/delay.h>
53 #include <linux/timer.h>
54 #include <linux/interrupt.h>
55 #include <linux/completion.h>
56 #include <linux/suspend.h>
57 #include <linux/workqueue.h>
58 #include <linux/jiffies.h>
59 #include <linux/scatterlist.h>
60 #include <linux/io.h>
61 #include <scsi/scsi.h>
62 #include <scsi/scsi_cmnd.h>
63 #include <scsi/scsi_host.h>
64 #include <linux/libata.h>
65 #include <asm/semaphore.h>
66 #include <asm/byteorder.h>
67 #include <linux/cdrom.h>
69 #include "libata.h"
72 /* debounce timing parameters in msecs { interval, duration, timeout } */
73 const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
74 const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
75 const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
77 static unsigned int ata_dev_init_params(struct ata_device *dev,
78 u16 heads, u16 sectors);
79 static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
80 static unsigned int ata_dev_set_feature(struct ata_device *dev,
81 u8 enable, u8 feature);
82 static void ata_dev_xfermask(struct ata_device *dev);
83 static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
85 unsigned int ata_print_id = 1;
86 static struct workqueue_struct *ata_wq;
88 struct workqueue_struct *ata_aux_wq;
90 <<<<<<< HEAD:drivers/ata/libata-core.c
91 =======
92 struct ata_force_param {
93 const char *name;
94 unsigned int cbl;
95 int spd_limit;
96 unsigned long xfer_mask;
97 unsigned int horkage_on;
98 unsigned int horkage_off;
101 struct ata_force_ent {
102 int port;
103 int device;
104 struct ata_force_param param;
107 static struct ata_force_ent *ata_force_tbl;
108 static int ata_force_tbl_size;
110 static char ata_force_param_buf[PAGE_SIZE] __initdata;
111 /* param_buf is thrown away after initialization, disallow read */
112 module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
113 MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
115 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
116 int atapi_enabled = 1;
117 module_param(atapi_enabled, int, 0444);
118 MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
120 <<<<<<< HEAD:drivers/ata/libata-core.c
121 int atapi_dmadir = 0;
122 =======
123 static int atapi_dmadir = 0;
124 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
125 module_param(atapi_dmadir, int, 0444);
126 MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
128 int atapi_passthru16 = 1;
129 module_param(atapi_passthru16, int, 0444);
130 MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices; on by default (0=off, 1=on)");
132 int libata_fua = 0;
133 module_param_named(fua, libata_fua, int, 0444);
134 MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
136 static int ata_ignore_hpa;
137 module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
138 MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
140 static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
141 module_param_named(dma, libata_dma_mask, int, 0444);
142 MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
144 static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
145 module_param(ata_probe_timeout, int, 0444);
146 MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
148 int libata_noacpi = 0;
149 module_param_named(noacpi, libata_noacpi, int, 0444);
150 MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in probe/suspend/resume when set");
152 int libata_allow_tpm = 0;
153 module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
154 MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands");
156 MODULE_AUTHOR("Jeff Garzik");
157 MODULE_DESCRIPTION("Library module for ATA devices");
158 MODULE_LICENSE("GPL");
159 MODULE_VERSION(DRV_VERSION);
163 <<<<<<< HEAD:drivers/ata/libata-core.c
164 =======
165 * ata_force_cbl - force cable type according to libata.force
166 * @ap: ATA port of interest
168 * Force cable type according to libata.force and whine about it.
169 * The last entry which has matching port number is used, so it
170 * can be specified as part of device force parameters. For
171 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
172 * same effect.
174 * LOCKING:
175 * EH context.
177 void ata_force_cbl(struct ata_port *ap)
179 int i;
181 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
182 const struct ata_force_ent *fe = &ata_force_tbl[i];
184 if (fe->port != -1 && fe->port != ap->print_id)
185 continue;
187 if (fe->param.cbl == ATA_CBL_NONE)
188 continue;
190 ap->cbl = fe->param.cbl;
191 ata_port_printk(ap, KERN_NOTICE,
192 "FORCE: cable set to %s\n", fe->param.name);
193 return;
198 * ata_force_spd_limit - force SATA spd limit according to libata.force
199 * @link: ATA link of interest
201 * Force SATA spd limit according to libata.force and whine about
202 * it. When only the port part is specified (e.g. 1:), the limit
203 * applies to all links connected to both the host link and all
204 * fan-out ports connected via PMP. If the device part is
205 * specified as 0 (e.g. 1.00:), it specifies the first fan-out
206 * link not the host link. Device number 15 always points to the
207 * host link whether PMP is attached or not.
209 * LOCKING:
210 * EH context.
212 static void ata_force_spd_limit(struct ata_link *link)
214 int linkno, i;
216 if (ata_is_host_link(link))
217 linkno = 15;
218 else
219 linkno = link->pmp;
221 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
222 const struct ata_force_ent *fe = &ata_force_tbl[i];
224 if (fe->port != -1 && fe->port != link->ap->print_id)
225 continue;
227 if (fe->device != -1 && fe->device != linkno)
228 continue;
230 if (!fe->param.spd_limit)
231 continue;
233 link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
234 ata_link_printk(link, KERN_NOTICE,
235 "FORCE: PHY spd limit set to %s\n", fe->param.name);
236 return;
241 * ata_force_xfermask - force xfermask according to libata.force
242 * @dev: ATA device of interest
244 * Force xfer_mask according to libata.force and whine about it.
245 * For consistency with link selection, device number 15 selects
246 * the first device connected to the host link.
248 * LOCKING:
249 * EH context.
251 static void ata_force_xfermask(struct ata_device *dev)
253 int devno = dev->link->pmp + dev->devno;
254 int alt_devno = devno;
255 int i;
257 /* allow n.15 for the first device attached to host port */
258 if (ata_is_host_link(dev->link) && devno == 0)
259 alt_devno = 15;
261 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
262 const struct ata_force_ent *fe = &ata_force_tbl[i];
263 unsigned long pio_mask, mwdma_mask, udma_mask;
265 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
266 continue;
268 if (fe->device != -1 && fe->device != devno &&
269 fe->device != alt_devno)
270 continue;
272 if (!fe->param.xfer_mask)
273 continue;
275 ata_unpack_xfermask(fe->param.xfer_mask,
276 &pio_mask, &mwdma_mask, &udma_mask);
277 if (udma_mask)
278 dev->udma_mask = udma_mask;
279 else if (mwdma_mask) {
280 dev->udma_mask = 0;
281 dev->mwdma_mask = mwdma_mask;
282 } else {
283 dev->udma_mask = 0;
284 dev->mwdma_mask = 0;
285 dev->pio_mask = pio_mask;
288 ata_dev_printk(dev, KERN_NOTICE,
289 "FORCE: xfer_mask set to %s\n", fe->param.name);
290 return;
295 * ata_force_horkage - force horkage according to libata.force
296 * @dev: ATA device of interest
298 * Force horkage according to libata.force and whine about it.
299 * For consistency with link selection, device number 15 selects
300 * the first device connected to the host link.
302 * LOCKING:
303 * EH context.
305 static void ata_force_horkage(struct ata_device *dev)
307 int devno = dev->link->pmp + dev->devno;
308 int alt_devno = devno;
309 int i;
311 /* allow n.15 for the first device attached to host port */
312 if (ata_is_host_link(dev->link) && devno == 0)
313 alt_devno = 15;
315 for (i = 0; i < ata_force_tbl_size; i++) {
316 const struct ata_force_ent *fe = &ata_force_tbl[i];
318 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
319 continue;
321 if (fe->device != -1 && fe->device != devno &&
322 fe->device != alt_devno)
323 continue;
325 if (!(~dev->horkage & fe->param.horkage_on) &&
326 !(dev->horkage & fe->param.horkage_off))
327 continue;
329 dev->horkage |= fe->param.horkage_on;
330 dev->horkage &= ~fe->param.horkage_off;
332 ata_dev_printk(dev, KERN_NOTICE,
333 "FORCE: horkage modified (%s)\n", fe->param.name);
338 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
339 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
340 * @tf: Taskfile to convert
341 * @pmp: Port multiplier port
342 * @is_cmd: This FIS is for command
343 * @fis: Buffer into which data will output
345 * Converts a standard ATA taskfile to a Serial ATA
346 * FIS structure (Register - Host to Device).
348 * LOCKING:
349 * Inherited from caller.
351 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
353 fis[0] = 0x27; /* Register - Host to Device FIS */
354 fis[1] = pmp & 0xf; /* Port multiplier number*/
355 if (is_cmd)
356 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
358 fis[2] = tf->command;
359 fis[3] = tf->feature;
361 fis[4] = tf->lbal;
362 fis[5] = tf->lbam;
363 fis[6] = tf->lbah;
364 fis[7] = tf->device;
366 fis[8] = tf->hob_lbal;
367 fis[9] = tf->hob_lbam;
368 fis[10] = tf->hob_lbah;
369 fis[11] = tf->hob_feature;
371 fis[12] = tf->nsect;
372 fis[13] = tf->hob_nsect;
373 fis[14] = 0;
374 fis[15] = tf->ctl;
376 fis[16] = 0;
377 fis[17] = 0;
378 fis[18] = 0;
379 fis[19] = 0;
383 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
384 * @fis: Buffer from which data will be input
385 * @tf: Taskfile to output
387 * Converts a serial ATA FIS structure to a standard ATA taskfile.
389 * LOCKING:
390 * Inherited from caller.
393 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
395 tf->command = fis[2]; /* status */
396 tf->feature = fis[3]; /* error */
398 tf->lbal = fis[4];
399 tf->lbam = fis[5];
400 tf->lbah = fis[6];
401 tf->device = fis[7];
403 tf->hob_lbal = fis[8];
404 tf->hob_lbam = fis[9];
405 tf->hob_lbah = fis[10];
407 tf->nsect = fis[12];
408 tf->hob_nsect = fis[13];
411 static const u8 ata_rw_cmds[] = {
412 /* pio multi */
413 ATA_CMD_READ_MULTI,
414 ATA_CMD_WRITE_MULTI,
415 ATA_CMD_READ_MULTI_EXT,
416 ATA_CMD_WRITE_MULTI_EXT,
420 ATA_CMD_WRITE_MULTI_FUA_EXT,
421 /* pio */
422 ATA_CMD_PIO_READ,
423 ATA_CMD_PIO_WRITE,
424 ATA_CMD_PIO_READ_EXT,
425 ATA_CMD_PIO_WRITE_EXT,
430 /* dma */
431 ATA_CMD_READ,
432 ATA_CMD_WRITE,
433 ATA_CMD_READ_EXT,
434 ATA_CMD_WRITE_EXT,
438 ATA_CMD_WRITE_FUA_EXT
442 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
443 * @tf: command to examine and configure
444 * @dev: device tf belongs to
446 * Examine the device configuration and tf->flags to calculate
447 * the proper read/write commands and protocol to use.
449 * LOCKING:
450 * caller.
452 static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
454 u8 cmd;
456 int index, fua, lba48, write;
458 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
459 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
460 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
462 if (dev->flags & ATA_DFLAG_PIO) {
463 tf->protocol = ATA_PROT_PIO;
464 index = dev->multi_count ? 0 : 8;
465 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
466 /* Unable to use DMA due to host limitation */
467 tf->protocol = ATA_PROT_PIO;
468 index = dev->multi_count ? 0 : 8;
469 } else {
470 tf->protocol = ATA_PROT_DMA;
471 index = 16;
474 cmd = ata_rw_cmds[index + fua + lba48 + write];
475 if (cmd) {
476 tf->command = cmd;
477 return 0;
479 return -1;
483 * ata_tf_read_block - Read block address from ATA taskfile
484 * @tf: ATA taskfile of interest
485 * @dev: ATA device @tf belongs to
487 * LOCKING:
488 * None.
490 * Read block address from @tf. This function can handle all
491 * three address formats - LBA, LBA48 and CHS. tf->protocol and
492 * flags select the address format to use.
494 * RETURNS:
495 * Block address read from @tf.
497 u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
499 u64 block = 0;
501 if (tf->flags & ATA_TFLAG_LBA) {
502 if (tf->flags & ATA_TFLAG_LBA48) {
503 block |= (u64)tf->hob_lbah << 40;
504 block |= (u64)tf->hob_lbam << 32;
505 block |= tf->hob_lbal << 24;
506 } else
507 block |= (tf->device & 0xf) << 24;
509 block |= tf->lbah << 16;
510 block |= tf->lbam << 8;
511 block |= tf->lbal;
512 } else {
513 u32 cyl, head, sect;
515 cyl = tf->lbam | (tf->lbah << 8);
516 head = tf->device & 0xf;
517 sect = tf->lbal;
519 block = (cyl * dev->heads + head) * dev->sectors + sect;
522 return block;
526 * ata_build_rw_tf - Build ATA taskfile for given read/write request
527 * @tf: Target ATA taskfile
528 * @dev: ATA device @tf belongs to
529 * @block: Block address
530 * @n_block: Number of blocks
531 * @tf_flags: RW/FUA etc...
532 * @tag: tag
534 * LOCKING:
535 * None.
537 * Build ATA taskfile @tf for read/write request described by
538 * @block, @n_block, @tf_flags and @tag on @dev.
540 * RETURNS:
542 * 0 on success, -ERANGE if the request is too large for @dev,
543 * -EINVAL if the request is invalid.
545 int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
546 u64 block, u32 n_block, unsigned int tf_flags,
547 unsigned int tag)
549 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
550 tf->flags |= tf_flags;
552 if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
553 /* yay, NCQ */
554 if (!lba_48_ok(block, n_block))
555 return -ERANGE;
557 tf->protocol = ATA_PROT_NCQ;
558 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
560 if (tf->flags & ATA_TFLAG_WRITE)
561 tf->command = ATA_CMD_FPDMA_WRITE;
562 else
563 tf->command = ATA_CMD_FPDMA_READ;
565 tf->nsect = tag << 3;
566 tf->hob_feature = (n_block >> 8) & 0xff;
567 tf->feature = n_block & 0xff;
569 tf->hob_lbah = (block >> 40) & 0xff;
570 tf->hob_lbam = (block >> 32) & 0xff;
571 tf->hob_lbal = (block >> 24) & 0xff;
572 tf->lbah = (block >> 16) & 0xff;
573 tf->lbam = (block >> 8) & 0xff;
574 tf->lbal = block & 0xff;
576 tf->device = 1 << 6;
577 if (tf->flags & ATA_TFLAG_FUA)
578 tf->device |= 1 << 7;
579 } else if (dev->flags & ATA_DFLAG_LBA) {
580 tf->flags |= ATA_TFLAG_LBA;
582 if (lba_28_ok(block, n_block)) {
583 /* use LBA28 */
584 tf->device |= (block >> 24) & 0xf;
585 } else if (lba_48_ok(block, n_block)) {
586 if (!(dev->flags & ATA_DFLAG_LBA48))
587 return -ERANGE;
589 /* use LBA48 */
590 tf->flags |= ATA_TFLAG_LBA48;
592 tf->hob_nsect = (n_block >> 8) & 0xff;
594 tf->hob_lbah = (block >> 40) & 0xff;
595 tf->hob_lbam = (block >> 32) & 0xff;
596 tf->hob_lbal = (block >> 24) & 0xff;
597 } else
598 /* request too large even for LBA48 */
599 return -ERANGE;
601 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
602 return -EINVAL;
604 tf->nsect = n_block & 0xff;
606 tf->lbah = (block >> 16) & 0xff;
607 tf->lbam = (block >> 8) & 0xff;
608 tf->lbal = block & 0xff;
610 tf->device |= ATA_LBA;
611 } else {
612 /* CHS */
613 u32 sect, head, cyl, track;
615 /* The request -may- be too large for CHS addressing. */
616 if (!lba_28_ok(block, n_block))
617 return -ERANGE;
619 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
620 return -EINVAL;
622 /* Convert LBA to CHS */
623 track = (u32)block / dev->sectors;
624 cyl = track / dev->heads;
625 head = track % dev->heads;
626 sect = (u32)block % dev->sectors + 1;
628 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
629 (u32)block, track, cyl, head, sect);
631 /* Check whether the converted CHS can fit.
632 Cylinder: 0-65535
633 Head: 0-15
634 Sector: 1-255*/
635 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
636 return -ERANGE;
638 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
639 tf->lbal = sect;
640 tf->lbam = cyl;
641 tf->lbah = cyl >> 8;
642 tf->device |= head;
645 return 0;
649 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
650 * @pio_mask: pio_mask
651 * @mwdma_mask: mwdma_mask
652 * @udma_mask: udma_mask
654 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
655 * unsigned int xfer_mask.
657 * LOCKING:
658 * None.
660 * RETURNS:
661 * Packed xfer_mask.
663 unsigned long ata_pack_xfermask(unsigned long pio_mask,
664 unsigned long mwdma_mask,
665 unsigned long udma_mask)
667 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
668 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
669 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
673 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
674 * @xfer_mask: xfer_mask to unpack
675 * @pio_mask: resulting pio_mask
676 * @mwdma_mask: resulting mwdma_mask
677 * @udma_mask: resulting udma_mask
679 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
680 * Any NULL distination masks will be ignored.
682 void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
683 unsigned long *mwdma_mask, unsigned long *udma_mask)
685 if (pio_mask)
686 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
687 if (mwdma_mask)
688 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
689 if (udma_mask)
690 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
693 static const struct ata_xfer_ent {
694 int shift, bits;
695 u8 base;
696 } ata_xfer_tbl[] = {
697 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
698 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
699 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
700 { -1, },
704 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
705 * @xfer_mask: xfer_mask of interest
707 * Return matching XFER_* value for @xfer_mask. Only the highest
708 * bit of @xfer_mask is considered.
710 * LOCKING:
711 * None.
713 * RETURNS:
714 * Matching XFER_* value, 0xff if no match found.
716 u8 ata_xfer_mask2mode(unsigned long xfer_mask)
718 int highbit = fls(xfer_mask) - 1;
719 const struct ata_xfer_ent *ent;
721 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
722 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
723 return ent->base + highbit - ent->shift;
724 return 0xff;
728 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
729 * @xfer_mode: XFER_* of interest
731 * Return matching xfer_mask for @xfer_mode.
733 * LOCKING:
734 * None.
736 * RETURNS:
737 * Matching xfer_mask, 0 if no match found.
739 unsigned long ata_xfer_mode2mask(u8 xfer_mode)
741 const struct ata_xfer_ent *ent;
743 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
744 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
745 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
746 & ~((1 << ent->shift) - 1);
747 return 0;
751 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
752 * @xfer_mode: XFER_* of interest
754 * Return matching xfer_shift for @xfer_mode.
756 * LOCKING:
757 * None.
759 * RETURNS:
760 * Matching xfer_shift, -1 if no match found.
762 int ata_xfer_mode2shift(unsigned long xfer_mode)
764 const struct ata_xfer_ent *ent;
766 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
767 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
768 return ent->shift;
769 return -1;
773 * ata_mode_string - convert xfer_mask to string
774 * @xfer_mask: mask of bits supported; only highest bit counts.
776 * Determine string which represents the highest speed
777 * (highest bit in @modemask).
779 * LOCKING:
780 * None.
782 * RETURNS:
783 * Constant C string representing highest speed listed in
784 * @mode_mask, or the constant C string "<n/a>".
786 const char *ata_mode_string(unsigned long xfer_mask)
788 static const char * const xfer_mode_str[] = {
789 "PIO0",
790 "PIO1",
791 "PIO2",
792 "PIO3",
793 "PIO4",
794 "PIO5",
795 "PIO6",
796 "MWDMA0",
797 "MWDMA1",
798 "MWDMA2",
799 "MWDMA3",
800 "MWDMA4",
801 "UDMA/16",
802 "UDMA/25",
803 "UDMA/33",
804 "UDMA/44",
805 "UDMA/66",
806 "UDMA/100",
807 "UDMA/133",
808 "UDMA7",
810 int highbit;
812 highbit = fls(xfer_mask) - 1;
813 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
814 return xfer_mode_str[highbit];
815 return "<n/a>";
818 static const char *sata_spd_string(unsigned int spd)
820 static const char * const spd_str[] = {
821 "1.5 Gbps",
822 "3.0 Gbps",
825 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
826 return "<unknown>";
827 return spd_str[spd - 1];
830 void ata_dev_disable(struct ata_device *dev)
832 if (ata_dev_enabled(dev)) {
833 if (ata_msg_drv(dev->link->ap))
834 ata_dev_printk(dev, KERN_WARNING, "disabled\n");
835 ata_acpi_on_disable(dev);
836 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
837 ATA_DNXFER_QUIET);
838 dev->class++;
842 static int ata_dev_set_dipm(struct ata_device *dev, enum link_pm policy)
844 struct ata_link *link = dev->link;
845 struct ata_port *ap = link->ap;
846 u32 scontrol;
847 unsigned int err_mask;
848 int rc;
851 * disallow DIPM for drivers which haven't set
852 * ATA_FLAG_IPM. This is because when DIPM is enabled,
853 * phy ready will be set in the interrupt status on
854 * state changes, which will cause some drivers to
855 * think there are errors - additionally drivers will
856 * need to disable hot plug.
858 if (!(ap->flags & ATA_FLAG_IPM) || !ata_dev_enabled(dev)) {
859 ap->pm_policy = NOT_AVAILABLE;
860 return -EINVAL;
864 * For DIPM, we will only enable it for the
865 * min_power setting.
867 * Why? Because Disks are too stupid to know that
868 * If the host rejects a request to go to SLUMBER
869 * they should retry at PARTIAL, and instead it
870 * just would give up. So, for medium_power to
871 * work at all, we need to only allow HIPM.
873 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
874 if (rc)
875 return rc;
877 switch (policy) {
878 case MIN_POWER:
879 /* no restrictions on IPM transitions */
880 scontrol &= ~(0x3 << 8);
881 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
882 if (rc)
883 return rc;
885 /* enable DIPM */
886 if (dev->flags & ATA_DFLAG_DIPM)
887 err_mask = ata_dev_set_feature(dev,
888 SETFEATURES_SATA_ENABLE, SATA_DIPM);
889 break;
890 case MEDIUM_POWER:
891 /* allow IPM to PARTIAL */
892 scontrol &= ~(0x1 << 8);
893 scontrol |= (0x2 << 8);
894 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
895 if (rc)
896 return rc;
899 * we don't have to disable DIPM since IPM flags
900 * disallow transitions to SLUMBER, which effectively
901 * disable DIPM if it does not support PARTIAL
903 break;
904 case NOT_AVAILABLE:
905 case MAX_PERFORMANCE:
906 /* disable all IPM transitions */
907 scontrol |= (0x3 << 8);
908 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
909 if (rc)
910 return rc;
913 * we don't have to disable DIPM since IPM flags
914 * disallow all transitions which effectively
915 * disable DIPM anyway.
917 break;
920 /* FIXME: handle SET FEATURES failure */
921 (void) err_mask;
923 return 0;
927 * ata_dev_enable_pm - enable SATA interface power management
928 * @dev: device to enable power management
929 * @policy: the link power management policy
931 * Enable SATA Interface power management. This will enable
932 * Device Interface Power Management (DIPM) for min_power
933 * policy, and then call driver specific callbacks for
934 * enabling Host Initiated Power management.
936 * Locking: Caller.
937 * Returns: -EINVAL if IPM is not supported, 0 otherwise.
939 void ata_dev_enable_pm(struct ata_device *dev, enum link_pm policy)
941 int rc = 0;
942 struct ata_port *ap = dev->link->ap;
944 /* set HIPM first, then DIPM */
945 if (ap->ops->enable_pm)
946 rc = ap->ops->enable_pm(ap, policy);
947 if (rc)
948 goto enable_pm_out;
949 rc = ata_dev_set_dipm(dev, policy);
951 enable_pm_out:
952 if (rc)
953 ap->pm_policy = MAX_PERFORMANCE;
954 else
955 ap->pm_policy = policy;
956 return /* rc */; /* hopefully we can use 'rc' eventually */
959 #ifdef CONFIG_PM
961 * ata_dev_disable_pm - disable SATA interface power management
962 * @dev: device to disable power management
964 * Disable SATA Interface power management. This will disable
965 * Device Interface Power Management (DIPM) without changing
966 * policy, call driver specific callbacks for disabling Host
967 * Initiated Power management.
969 * Locking: Caller.
970 * Returns: void
972 static void ata_dev_disable_pm(struct ata_device *dev)
974 struct ata_port *ap = dev->link->ap;
976 ata_dev_set_dipm(dev, MAX_PERFORMANCE);
977 if (ap->ops->disable_pm)
978 ap->ops->disable_pm(ap);
980 #endif /* CONFIG_PM */
982 void ata_lpm_schedule(struct ata_port *ap, enum link_pm policy)
984 ap->pm_policy = policy;
985 ap->link.eh_info.action |= ATA_EHI_LPM;
986 ap->link.eh_info.flags |= ATA_EHI_NO_AUTOPSY;
987 ata_port_schedule_eh(ap);
990 #ifdef CONFIG_PM
991 static void ata_lpm_enable(struct ata_host *host)
993 struct ata_link *link;
994 struct ata_port *ap;
995 struct ata_device *dev;
996 int i;
998 for (i = 0; i < host->n_ports; i++) {
999 ap = host->ports[i];
1000 ata_port_for_each_link(link, ap) {
1001 ata_link_for_each_dev(dev, link)
1002 ata_dev_disable_pm(dev);
1007 static void ata_lpm_disable(struct ata_host *host)
1009 int i;
1011 for (i = 0; i < host->n_ports; i++) {
1012 struct ata_port *ap = host->ports[i];
1013 ata_lpm_schedule(ap, ap->pm_policy);
1016 #endif /* CONFIG_PM */
1020 * ata_devchk - PATA device presence detection
1021 * @ap: ATA channel to examine
1022 * @device: Device to examine (starting at zero)
1024 * This technique was originally described in
1025 * Hale Landis's ATADRVR (www.ata-atapi.com), and
1026 * later found its way into the ATA/ATAPI spec.
1028 * Write a pattern to the ATA shadow registers,
1029 * and if a device is present, it will respond by
1030 * correctly storing and echoing back the
1031 * ATA shadow register contents.
1033 * LOCKING:
1034 * caller.
1037 static unsigned int ata_devchk(struct ata_port *ap, unsigned int device)
1039 struct ata_ioports *ioaddr = &ap->ioaddr;
1040 u8 nsect, lbal;
1042 ap->ops->dev_select(ap, device);
1044 iowrite8(0x55, ioaddr->nsect_addr);
1045 iowrite8(0xaa, ioaddr->lbal_addr);
1047 iowrite8(0xaa, ioaddr->nsect_addr);
1048 iowrite8(0x55, ioaddr->lbal_addr);
1050 iowrite8(0x55, ioaddr->nsect_addr);
1051 iowrite8(0xaa, ioaddr->lbal_addr);
1053 nsect = ioread8(ioaddr->nsect_addr);
1054 lbal = ioread8(ioaddr->lbal_addr);
1056 if ((nsect == 0x55) && (lbal == 0xaa))
1057 return 1; /* we found a device */
1059 return 0; /* nothing found */
1063 * ata_dev_classify - determine device type based on ATA-spec signature
1064 * @tf: ATA taskfile register set for device to be identified
1066 * Determine from taskfile register contents whether a device is
1067 * ATA or ATAPI, as per "Signature and persistence" section
1068 * of ATA/PI spec (volume 1, sect 5.14).
1070 * LOCKING:
1071 * None.
1073 * RETURNS:
1074 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
1075 * %ATA_DEV_UNKNOWN the event of failure.
1077 unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1079 /* Apple's open source Darwin code hints that some devices only
1080 * put a proper signature into the LBA mid/high registers,
1081 * So, we only check those. It's sufficient for uniqueness.
1083 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1084 * signatures for ATA and ATAPI devices attached on SerialATA,
1085 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
1086 * spec has never mentioned about using different signatures
1087 * for ATA/ATAPI devices. Then, Serial ATA II: Port
1088 * Multiplier specification began to use 0x69/0x96 to identify
1089 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1090 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1091 * 0x69/0x96 shortly and described them as reserved for
1092 * SerialATA.
1094 * We follow the current spec and consider that 0x69/0x96
1095 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
1097 if ((tf->lbam == 0) && (tf->lbah == 0)) {
1098 DPRINTK("found ATA device by sig\n");
1099 return ATA_DEV_ATA;
1102 if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
1103 DPRINTK("found ATAPI device by sig\n");
1104 return ATA_DEV_ATAPI;
1107 if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1108 DPRINTK("found PMP device by sig\n");
1109 return ATA_DEV_PMP;
1112 if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
1113 printk(KERN_INFO "ata: SEMB device ignored\n");
1114 return ATA_DEV_SEMB_UNSUP; /* not yet */
1117 DPRINTK("unknown device\n");
1118 return ATA_DEV_UNKNOWN;
1122 * ata_dev_try_classify - Parse returned ATA device signature
1123 * @dev: ATA device to classify (starting at zero)
1124 * @present: device seems present
1125 * @r_err: Value of error register on completion
1127 * After an event -- SRST, E.D.D., or SATA COMRESET -- occurs,
1128 * an ATA/ATAPI-defined set of values is placed in the ATA
1129 * shadow registers, indicating the results of device detection
1130 * and diagnostics.
1132 * Select the ATA device, and read the values from the ATA shadow
1133 * registers. Then parse according to the Error register value,
1134 * and the spec-defined values examined by ata_dev_classify().
1136 * LOCKING:
1137 * caller.
1139 * RETURNS:
1140 * Device type - %ATA_DEV_ATA, %ATA_DEV_ATAPI or %ATA_DEV_NONE.
1142 unsigned int ata_dev_try_classify(struct ata_device *dev, int present,
1143 u8 *r_err)
1145 struct ata_port *ap = dev->link->ap;
1146 struct ata_taskfile tf;
1147 unsigned int class;
1148 u8 err;
1150 ap->ops->dev_select(ap, dev->devno);
1152 memset(&tf, 0, sizeof(tf));
1154 ap->ops->tf_read(ap, &tf);
1155 err = tf.feature;
1156 if (r_err)
1157 *r_err = err;
1159 /* see if device passed diags: continue and warn later */
1160 if (err == 0)
1161 /* diagnostic fail : do nothing _YET_ */
1162 dev->horkage |= ATA_HORKAGE_DIAGNOSTIC;
1163 else if (err == 1)
1164 /* do nothing */ ;
1165 else if ((dev->devno == 0) && (err == 0x81))
1166 /* do nothing */ ;
1167 else
1168 return ATA_DEV_NONE;
1170 /* determine if device is ATA or ATAPI */
1171 class = ata_dev_classify(&tf);
1173 if (class == ATA_DEV_UNKNOWN) {
1174 /* If the device failed diagnostic, it's likely to
1175 * have reported incorrect device signature too.
1176 * Assume ATA device if the device seems present but
1177 * device signature is invalid with diagnostic
1178 * failure.
1180 if (present && (dev->horkage & ATA_HORKAGE_DIAGNOSTIC))
1181 class = ATA_DEV_ATA;
1182 else
1183 class = ATA_DEV_NONE;
1184 } else if ((class == ATA_DEV_ATA) && (ata_chk_status(ap) == 0))
1185 class = ATA_DEV_NONE;
1187 return class;
1191 * ata_id_string - Convert IDENTIFY DEVICE page into string
1192 * @id: IDENTIFY DEVICE results we will examine
1193 * @s: string into which data is output
1194 * @ofs: offset into identify device page
1195 * @len: length of string to return. must be an even number.
1197 * The strings in the IDENTIFY DEVICE page are broken up into
1198 * 16-bit chunks. Run through the string, and output each
1199 * 8-bit chunk linearly, regardless of platform.
1201 * LOCKING:
1202 * caller.
1205 void ata_id_string(const u16 *id, unsigned char *s,
1206 unsigned int ofs, unsigned int len)
1208 unsigned int c;
1210 while (len > 0) {
1211 c = id[ofs] >> 8;
1212 *s = c;
1213 s++;
1215 c = id[ofs] & 0xff;
1216 *s = c;
1217 s++;
1219 ofs++;
1220 len -= 2;
1225 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1226 * @id: IDENTIFY DEVICE results we will examine
1227 * @s: string into which data is output
1228 * @ofs: offset into identify device page
1229 * @len: length of string to return. must be an odd number.
1231 * This function is identical to ata_id_string except that it
1232 * trims trailing spaces and terminates the resulting string with
1233 * null. @len must be actual maximum length (even number) + 1.
1235 * LOCKING:
1236 * caller.
1238 void ata_id_c_string(const u16 *id, unsigned char *s,
1239 unsigned int ofs, unsigned int len)
1241 unsigned char *p;
1243 WARN_ON(!(len & 1));
1245 ata_id_string(id, s, ofs, len - 1);
1247 p = s + strnlen(s, len - 1);
1248 while (p > s && p[-1] == ' ')
1249 p--;
1250 *p = '\0';
1253 static u64 ata_id_n_sectors(const u16 *id)
1255 if (ata_id_has_lba(id)) {
1256 if (ata_id_has_lba48(id))
1257 return ata_id_u64(id, 100);
1258 else
1259 return ata_id_u32(id, 60);
1260 } else {
1261 if (ata_id_current_chs_valid(id))
1262 return ata_id_u32(id, 57);
1263 else
1264 return id[1] * id[3] * id[6];
1268 static u64 ata_tf_to_lba48(struct ata_taskfile *tf)
1270 u64 sectors = 0;
1272 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1273 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1274 sectors |= (tf->hob_lbal & 0xff) << 24;
1275 sectors |= (tf->lbah & 0xff) << 16;
1276 sectors |= (tf->lbam & 0xff) << 8;
1277 sectors |= (tf->lbal & 0xff);
1279 return ++sectors;
1282 static u64 ata_tf_to_lba(struct ata_taskfile *tf)
1284 u64 sectors = 0;
1286 sectors |= (tf->device & 0x0f) << 24;
1287 sectors |= (tf->lbah & 0xff) << 16;
1288 sectors |= (tf->lbam & 0xff) << 8;
1289 sectors |= (tf->lbal & 0xff);
1291 return ++sectors;
1295 * ata_read_native_max_address - Read native max address
1296 * @dev: target device
1297 * @max_sectors: out parameter for the result native max address
1299 * Perform an LBA48 or LBA28 native size query upon the device in
1300 * question.
1302 * RETURNS:
1303 * 0 on success, -EACCES if command is aborted by the drive.
1304 * -EIO on other errors.
1306 static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
1308 unsigned int err_mask;
1309 struct ata_taskfile tf;
1310 int lba48 = ata_id_has_lba48(dev->id);
1312 ata_tf_init(dev, &tf);
1314 /* always clear all address registers */
1315 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1317 if (lba48) {
1318 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1319 tf.flags |= ATA_TFLAG_LBA48;
1320 } else
1321 tf.command = ATA_CMD_READ_NATIVE_MAX;
1323 tf.protocol |= ATA_PROT_NODATA;
1324 tf.device |= ATA_LBA;
1326 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1327 if (err_mask) {
1328 ata_dev_printk(dev, KERN_WARNING, "failed to read native "
1329 "max address (err_mask=0x%x)\n", err_mask);
1330 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1331 return -EACCES;
1332 return -EIO;
1335 if (lba48)
1336 *max_sectors = ata_tf_to_lba48(&tf);
1337 else
1338 *max_sectors = ata_tf_to_lba(&tf);
1339 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
1340 (*max_sectors)--;
1341 return 0;
1345 * ata_set_max_sectors - Set max sectors
1346 * @dev: target device
1347 * @new_sectors: new max sectors value to set for the device
1349 * Set max sectors of @dev to @new_sectors.
1351 * RETURNS:
1352 * 0 on success, -EACCES if command is aborted or denied (due to
1353 * previous non-volatile SET_MAX) by the drive. -EIO on other
1354 * errors.
1356 static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
1358 unsigned int err_mask;
1359 struct ata_taskfile tf;
1360 int lba48 = ata_id_has_lba48(dev->id);
1362 new_sectors--;
1364 ata_tf_init(dev, &tf);
1366 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1368 if (lba48) {
1369 tf.command = ATA_CMD_SET_MAX_EXT;
1370 tf.flags |= ATA_TFLAG_LBA48;
1372 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1373 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1374 tf.hob_lbah = (new_sectors >> 40) & 0xff;
1375 } else {
1376 tf.command = ATA_CMD_SET_MAX;
1378 tf.device |= (new_sectors >> 24) & 0xf;
1381 tf.protocol |= ATA_PROT_NODATA;
1382 tf.device |= ATA_LBA;
1384 tf.lbal = (new_sectors >> 0) & 0xff;
1385 tf.lbam = (new_sectors >> 8) & 0xff;
1386 tf.lbah = (new_sectors >> 16) & 0xff;
1388 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1389 if (err_mask) {
1390 ata_dev_printk(dev, KERN_WARNING, "failed to set "
1391 "max address (err_mask=0x%x)\n", err_mask);
1392 if (err_mask == AC_ERR_DEV &&
1393 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1394 return -EACCES;
1395 return -EIO;
1398 return 0;
1402 * ata_hpa_resize - Resize a device with an HPA set
1403 * @dev: Device to resize
1405 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1406 * it if required to the full size of the media. The caller must check
1407 * the drive has the HPA feature set enabled.
1409 * RETURNS:
1410 * 0 on success, -errno on failure.
1412 static int ata_hpa_resize(struct ata_device *dev)
1414 struct ata_eh_context *ehc = &dev->link->eh_context;
1415 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
1416 u64 sectors = ata_id_n_sectors(dev->id);
1417 u64 native_sectors;
1418 int rc;
1420 /* do we need to do it? */
1421 if (dev->class != ATA_DEV_ATA ||
1422 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1423 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1424 return 0;
1426 /* read native max address */
1427 rc = ata_read_native_max_address(dev, &native_sectors);
1428 if (rc) {
1429 /* If HPA isn't going to be unlocked, skip HPA
1430 * resizing from the next try.
1432 if (!ata_ignore_hpa) {
1433 ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
1434 "broken, will skip HPA handling\n");
1435 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1437 /* we can continue if device aborted the command */
1438 if (rc == -EACCES)
1439 rc = 0;
1442 return rc;
1445 /* nothing to do? */
1446 if (native_sectors <= sectors || !ata_ignore_hpa) {
1447 if (!print_info || native_sectors == sectors)
1448 return 0;
1450 if (native_sectors > sectors)
1451 ata_dev_printk(dev, KERN_INFO,
1452 "HPA detected: current %llu, native %llu\n",
1453 (unsigned long long)sectors,
1454 (unsigned long long)native_sectors);
1455 else if (native_sectors < sectors)
1456 ata_dev_printk(dev, KERN_WARNING,
1457 "native sectors (%llu) is smaller than "
1458 "sectors (%llu)\n",
1459 (unsigned long long)native_sectors,
1460 (unsigned long long)sectors);
1461 return 0;
1464 /* let's unlock HPA */
1465 rc = ata_set_max_sectors(dev, native_sectors);
1466 if (rc == -EACCES) {
1467 /* if device aborted the command, skip HPA resizing */
1468 ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
1469 "(%llu -> %llu), skipping HPA handling\n",
1470 (unsigned long long)sectors,
1471 (unsigned long long)native_sectors);
1472 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1473 return 0;
1474 } else if (rc)
1475 return rc;
1477 /* re-read IDENTIFY data */
1478 rc = ata_dev_reread_id(dev, 0);
1479 if (rc) {
1480 ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
1481 "data after HPA resizing\n");
1482 return rc;
1485 if (print_info) {
1486 u64 new_sectors = ata_id_n_sectors(dev->id);
1487 ata_dev_printk(dev, KERN_INFO,
1488 "HPA unlocked: %llu -> %llu, native %llu\n",
1489 (unsigned long long)sectors,
1490 (unsigned long long)new_sectors,
1491 (unsigned long long)native_sectors);
1494 return 0;
1498 * ata_noop_dev_select - Select device 0/1 on ATA bus
1499 * @ap: ATA channel to manipulate
1500 * @device: ATA device (numbered from zero) to select
1502 * This function performs no actual function.
1504 * May be used as the dev_select() entry in ata_port_operations.
1506 * LOCKING:
1507 * caller.
1509 void ata_noop_dev_select(struct ata_port *ap, unsigned int device)
1515 * ata_std_dev_select - Select device 0/1 on ATA bus
1516 * @ap: ATA channel to manipulate
1517 * @device: ATA device (numbered from zero) to select
1519 * Use the method defined in the ATA specification to
1520 * make either device 0, or device 1, active on the
1521 * ATA channel. Works with both PIO and MMIO.
1523 * May be used as the dev_select() entry in ata_port_operations.
1525 * LOCKING:
1526 * caller.
1529 void ata_std_dev_select(struct ata_port *ap, unsigned int device)
1531 u8 tmp;
1533 if (device == 0)
1534 tmp = ATA_DEVICE_OBS;
1535 else
1536 tmp = ATA_DEVICE_OBS | ATA_DEV1;
1538 iowrite8(tmp, ap->ioaddr.device_addr);
1539 ata_pause(ap); /* needed; also flushes, for mmio */
1543 * ata_dev_select - Select device 0/1 on ATA bus
1544 * @ap: ATA channel to manipulate
1545 * @device: ATA device (numbered from zero) to select
1546 * @wait: non-zero to wait for Status register BSY bit to clear
1547 * @can_sleep: non-zero if context allows sleeping
1549 * Use the method defined in the ATA specification to
1550 * make either device 0, or device 1, active on the
1551 * ATA channel.
1553 * This is a high-level version of ata_std_dev_select(),
1554 * which additionally provides the services of inserting
1555 * the proper pauses and status polling, where needed.
1557 * LOCKING:
1558 * caller.
1561 void ata_dev_select(struct ata_port *ap, unsigned int device,
1562 unsigned int wait, unsigned int can_sleep)
1564 if (ata_msg_probe(ap))
1565 ata_port_printk(ap, KERN_INFO, "ata_dev_select: ENTER, "
1566 "device %u, wait %u\n", device, wait);
1568 if (wait)
1569 ata_wait_idle(ap);
1571 ap->ops->dev_select(ap, device);
1573 if (wait) {
1574 if (can_sleep && ap->link.device[device].class == ATA_DEV_ATAPI)
1575 msleep(150);
1576 ata_wait_idle(ap);
1581 * ata_dump_id - IDENTIFY DEVICE info debugging output
1582 * @id: IDENTIFY DEVICE page to dump
1584 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1585 * page.
1587 * LOCKING:
1588 * caller.
1591 static inline void ata_dump_id(const u16 *id)
1593 DPRINTK("49==0x%04x "
1594 "53==0x%04x "
1595 "63==0x%04x "
1596 "64==0x%04x "
1597 "75==0x%04x \n",
1598 id[49],
1599 id[53],
1600 id[63],
1601 id[64],
1602 id[75]);
1603 DPRINTK("80==0x%04x "
1604 "81==0x%04x "
1605 "82==0x%04x "
1606 "83==0x%04x "
1607 "84==0x%04x \n",
1608 id[80],
1609 id[81],
1610 id[82],
1611 id[83],
1612 id[84]);
1613 DPRINTK("88==0x%04x "
1614 "93==0x%04x\n",
1615 id[88],
1616 id[93]);
1620 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1621 * @id: IDENTIFY data to compute xfer mask from
1623 * Compute the xfermask for this device. This is not as trivial
1624 * as it seems if we must consider early devices correctly.
1626 * FIXME: pre IDE drive timing (do we care ?).
1628 * LOCKING:
1629 * None.
1631 * RETURNS:
1632 * Computed xfermask
1634 unsigned long ata_id_xfermask(const u16 *id)
1636 unsigned long pio_mask, mwdma_mask, udma_mask;
1638 /* Usual case. Word 53 indicates word 64 is valid */
1639 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1640 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1641 pio_mask <<= 3;
1642 pio_mask |= 0x7;
1643 } else {
1644 /* If word 64 isn't valid then Word 51 high byte holds
1645 * the PIO timing number for the maximum. Turn it into
1646 * a mask.
1648 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
1649 if (mode < 5) /* Valid PIO range */
1650 pio_mask = (2 << mode) - 1;
1651 else
1652 pio_mask = 1;
1654 /* But wait.. there's more. Design your standards by
1655 * committee and you too can get a free iordy field to
1656 * process. However its the speeds not the modes that
1657 * are supported... Note drivers using the timing API
1658 * will get this right anyway
1662 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1664 if (ata_id_is_cfa(id)) {
1666 * Process compact flash extended modes
1668 int pio = id[163] & 0x7;
1669 int dma = (id[163] >> 3) & 7;
1671 if (pio)
1672 pio_mask |= (1 << 5);
1673 if (pio > 1)
1674 pio_mask |= (1 << 6);
1675 if (dma)
1676 mwdma_mask |= (1 << 3);
1677 if (dma > 1)
1678 mwdma_mask |= (1 << 4);
1681 udma_mask = 0;
1682 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1683 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1685 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1689 * ata_pio_queue_task - Queue port_task
1690 * @ap: The ata_port to queue port_task for
1691 * @fn: workqueue function to be scheduled
1692 * @data: data for @fn to use
1693 * @delay: delay time for workqueue function
1695 * Schedule @fn(@data) for execution after @delay jiffies using
1696 * port_task. There is one port_task per port and it's the
1697 * user(low level driver)'s responsibility to make sure that only
1698 * one task is active at any given time.
1700 * libata core layer takes care of synchronization between
1701 * port_task and EH. ata_pio_queue_task() may be ignored for EH
1702 * synchronization.
1704 * LOCKING:
1705 * Inherited from caller.
1707 static void ata_pio_queue_task(struct ata_port *ap, void *data,
1708 unsigned long delay)
1710 ap->port_task_data = data;
1712 /* may fail if ata_port_flush_task() in progress */
1713 queue_delayed_work(ata_wq, &ap->port_task, delay);
1717 * ata_port_flush_task - Flush port_task
1718 * @ap: The ata_port to flush port_task for
1720 * After this function completes, port_task is guranteed not to
1721 * be running or scheduled.
1723 * LOCKING:
1724 * Kernel thread context (may sleep)
1726 void ata_port_flush_task(struct ata_port *ap)
1728 DPRINTK("ENTER\n");
1730 cancel_rearming_delayed_work(&ap->port_task);
1732 if (ata_msg_ctl(ap))
1733 <<<<<<< HEAD:drivers/ata/libata-core.c
1734 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __FUNCTION__);
1735 =======
1736 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __func__);
1737 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
1740 static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1742 struct completion *waiting = qc->private_data;
1744 complete(waiting);
1748 * ata_exec_internal_sg - execute libata internal command
1749 * @dev: Device to which the command is sent
1750 * @tf: Taskfile registers for the command and the result
1751 * @cdb: CDB for packet command
1752 * @dma_dir: Data tranfer direction of the command
1753 * @sgl: sg list for the data buffer of the command
1754 * @n_elem: Number of sg entries
1755 * @timeout: Timeout in msecs (0 for default)
1757 * Executes libata internal command with timeout. @tf contains
1758 * command on entry and result on return. Timeout and error
1759 * conditions are reported via return value. No recovery action
1760 * is taken after a command times out. It's caller's duty to
1761 * clean up after timeout.
1763 * LOCKING:
1764 * None. Should be called with kernel context, might sleep.
1766 * RETURNS:
1767 * Zero on success, AC_ERR_* mask on failure
1769 unsigned ata_exec_internal_sg(struct ata_device *dev,
1770 struct ata_taskfile *tf, const u8 *cdb,
1771 int dma_dir, struct scatterlist *sgl,
1772 unsigned int n_elem, unsigned long timeout)
1774 struct ata_link *link = dev->link;
1775 struct ata_port *ap = link->ap;
1776 u8 command = tf->command;
1777 struct ata_queued_cmd *qc;
1778 unsigned int tag, preempted_tag;
1779 u32 preempted_sactive, preempted_qc_active;
1780 int preempted_nr_active_links;
1781 DECLARE_COMPLETION_ONSTACK(wait);
1782 unsigned long flags;
1783 unsigned int err_mask;
1784 int rc;
1786 spin_lock_irqsave(ap->lock, flags);
1788 /* no internal command while frozen */
1789 if (ap->pflags & ATA_PFLAG_FROZEN) {
1790 spin_unlock_irqrestore(ap->lock, flags);
1791 return AC_ERR_SYSTEM;
1794 /* initialize internal qc */
1796 /* XXX: Tag 0 is used for drivers with legacy EH as some
1797 * drivers choke if any other tag is given. This breaks
1798 * ata_tag_internal() test for those drivers. Don't use new
1799 * EH stuff without converting to it.
1801 if (ap->ops->error_handler)
1802 tag = ATA_TAG_INTERNAL;
1803 else
1804 tag = 0;
1806 if (test_and_set_bit(tag, &ap->qc_allocated))
1807 BUG();
1808 qc = __ata_qc_from_tag(ap, tag);
1810 qc->tag = tag;
1811 qc->scsicmd = NULL;
1812 qc->ap = ap;
1813 qc->dev = dev;
1814 ata_qc_reinit(qc);
1816 preempted_tag = link->active_tag;
1817 preempted_sactive = link->sactive;
1818 preempted_qc_active = ap->qc_active;
1819 preempted_nr_active_links = ap->nr_active_links;
1820 link->active_tag = ATA_TAG_POISON;
1821 link->sactive = 0;
1822 ap->qc_active = 0;
1823 ap->nr_active_links = 0;
1825 /* prepare & issue qc */
1826 qc->tf = *tf;
1827 if (cdb)
1828 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1829 qc->flags |= ATA_QCFLAG_RESULT_TF;
1830 qc->dma_dir = dma_dir;
1831 if (dma_dir != DMA_NONE) {
1832 unsigned int i, buflen = 0;
1833 struct scatterlist *sg;
1835 for_each_sg(sgl, sg, n_elem, i)
1836 buflen += sg->length;
1838 ata_sg_init(qc, sgl, n_elem);
1839 qc->nbytes = buflen;
1842 qc->private_data = &wait;
1843 qc->complete_fn = ata_qc_complete_internal;
1845 ata_qc_issue(qc);
1847 spin_unlock_irqrestore(ap->lock, flags);
1849 if (!timeout)
1850 timeout = ata_probe_timeout * 1000 / HZ;
1852 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1854 ata_port_flush_task(ap);
1856 if (!rc) {
1857 spin_lock_irqsave(ap->lock, flags);
1859 /* We're racing with irq here. If we lose, the
1860 * following test prevents us from completing the qc
1861 * twice. If we win, the port is frozen and will be
1862 * cleaned up by ->post_internal_cmd().
1864 if (qc->flags & ATA_QCFLAG_ACTIVE) {
1865 qc->err_mask |= AC_ERR_TIMEOUT;
1867 if (ap->ops->error_handler)
1868 ata_port_freeze(ap);
1869 else
1870 ata_qc_complete(qc);
1872 if (ata_msg_warn(ap))
1873 ata_dev_printk(dev, KERN_WARNING,
1874 "qc timeout (cmd 0x%x)\n", command);
1877 spin_unlock_irqrestore(ap->lock, flags);
1880 /* do post_internal_cmd */
1881 if (ap->ops->post_internal_cmd)
1882 ap->ops->post_internal_cmd(qc);
1884 /* perform minimal error analysis */
1885 if (qc->flags & ATA_QCFLAG_FAILED) {
1886 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1887 qc->err_mask |= AC_ERR_DEV;
1889 if (!qc->err_mask)
1890 qc->err_mask |= AC_ERR_OTHER;
1892 if (qc->err_mask & ~AC_ERR_OTHER)
1893 qc->err_mask &= ~AC_ERR_OTHER;
1896 /* finish up */
1897 spin_lock_irqsave(ap->lock, flags);
1899 *tf = qc->result_tf;
1900 err_mask = qc->err_mask;
1902 ata_qc_free(qc);
1903 link->active_tag = preempted_tag;
1904 link->sactive = preempted_sactive;
1905 ap->qc_active = preempted_qc_active;
1906 ap->nr_active_links = preempted_nr_active_links;
1908 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1909 * Until those drivers are fixed, we detect the condition
1910 * here, fail the command with AC_ERR_SYSTEM and reenable the
1911 * port.
1913 * Note that this doesn't change any behavior as internal
1914 * command failure results in disabling the device in the
1915 * higher layer for LLDDs without new reset/EH callbacks.
1917 * Kill the following code as soon as those drivers are fixed.
1919 if (ap->flags & ATA_FLAG_DISABLED) {
1920 err_mask |= AC_ERR_SYSTEM;
1921 ata_port_probe(ap);
1924 spin_unlock_irqrestore(ap->lock, flags);
1926 return err_mask;
1930 * ata_exec_internal - execute libata internal command
1931 * @dev: Device to which the command is sent
1932 * @tf: Taskfile registers for the command and the result
1933 * @cdb: CDB for packet command
1934 * @dma_dir: Data tranfer direction of the command
1935 * @buf: Data buffer of the command
1936 * @buflen: Length of data buffer
1937 * @timeout: Timeout in msecs (0 for default)
1939 * Wrapper around ata_exec_internal_sg() which takes simple
1940 * buffer instead of sg list.
1942 * LOCKING:
1943 * None. Should be called with kernel context, might sleep.
1945 * RETURNS:
1946 * Zero on success, AC_ERR_* mask on failure
1948 unsigned ata_exec_internal(struct ata_device *dev,
1949 struct ata_taskfile *tf, const u8 *cdb,
1950 int dma_dir, void *buf, unsigned int buflen,
1951 unsigned long timeout)
1953 struct scatterlist *psg = NULL, sg;
1954 unsigned int n_elem = 0;
1956 if (dma_dir != DMA_NONE) {
1957 WARN_ON(!buf);
1958 sg_init_one(&sg, buf, buflen);
1959 psg = &sg;
1960 n_elem++;
1963 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1964 timeout);
1968 * ata_do_simple_cmd - execute simple internal command
1969 * @dev: Device to which the command is sent
1970 * @cmd: Opcode to execute
1972 * Execute a 'simple' command, that only consists of the opcode
1973 * 'cmd' itself, without filling any other registers
1975 * LOCKING:
1976 * Kernel thread context (may sleep).
1978 * RETURNS:
1979 * Zero on success, AC_ERR_* mask on failure
1981 unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
1983 struct ata_taskfile tf;
1985 ata_tf_init(dev, &tf);
1987 tf.command = cmd;
1988 tf.flags |= ATA_TFLAG_DEVICE;
1989 tf.protocol = ATA_PROT_NODATA;
1991 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1995 * ata_pio_need_iordy - check if iordy needed
1996 * @adev: ATA device
1998 * Check if the current speed of the device requires IORDY. Used
1999 * by various controllers for chip configuration.
2002 unsigned int ata_pio_need_iordy(const struct ata_device *adev)
2004 /* Controller doesn't support IORDY. Probably a pointless check
2005 as the caller should know this */
2006 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
2007 return 0;
2008 /* PIO3 and higher it is mandatory */
2009 if (adev->pio_mode > XFER_PIO_2)
2010 return 1;
2011 /* We turn it on when possible */
2012 if (ata_id_has_iordy(adev->id))
2013 return 1;
2014 return 0;
2018 * ata_pio_mask_no_iordy - Return the non IORDY mask
2019 * @adev: ATA device
2021 * Compute the highest mode possible if we are not using iordy. Return
2022 * -1 if no iordy mode is available.
2025 static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
2027 /* If we have no drive specific rule, then PIO 2 is non IORDY */
2028 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
2029 u16 pio = adev->id[ATA_ID_EIDE_PIO];
2030 /* Is the speed faster than the drive allows non IORDY ? */
2031 if (pio) {
2032 /* This is cycle times not frequency - watch the logic! */
2033 if (pio > 240) /* PIO2 is 240nS per cycle */
2034 return 3 << ATA_SHIFT_PIO;
2035 return 7 << ATA_SHIFT_PIO;
2038 return 3 << ATA_SHIFT_PIO;
2042 * ata_dev_read_id - Read ID data from the specified device
2043 * @dev: target device
2044 * @p_class: pointer to class of the target device (may be changed)
2045 * @flags: ATA_READID_* flags
2046 * @id: buffer to read IDENTIFY data into
2048 * Read ID data from the specified device. ATA_CMD_ID_ATA is
2049 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
2050 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
2051 * for pre-ATA4 drives.
2053 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
2054 * now we abort if we hit that case.
2056 * LOCKING:
2057 * Kernel thread context (may sleep)
2059 * RETURNS:
2060 * 0 on success, -errno otherwise.
2062 int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
2063 unsigned int flags, u16 *id)
2065 struct ata_port *ap = dev->link->ap;
2066 unsigned int class = *p_class;
2067 struct ata_taskfile tf;
2068 unsigned int err_mask = 0;
2069 const char *reason;
2070 int may_fallback = 1, tried_spinup = 0;
2071 int rc;
2073 if (ata_msg_ctl(ap))
2074 <<<<<<< HEAD:drivers/ata/libata-core.c
2075 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
2076 =======
2077 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
2078 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
2080 ata_dev_select(ap, dev->devno, 1, 1); /* select device 0/1 */
2081 retry:
2082 ata_tf_init(dev, &tf);
2084 switch (class) {
2085 case ATA_DEV_ATA:
2086 tf.command = ATA_CMD_ID_ATA;
2087 break;
2088 case ATA_DEV_ATAPI:
2089 tf.command = ATA_CMD_ID_ATAPI;
2090 break;
2091 default:
2092 rc = -ENODEV;
2093 reason = "unsupported class";
2094 goto err_out;
2097 tf.protocol = ATA_PROT_PIO;
2099 /* Some devices choke if TF registers contain garbage. Make
2100 * sure those are properly initialized.
2102 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2104 /* Device presence detection is unreliable on some
2105 * controllers. Always poll IDENTIFY if available.
2107 tf.flags |= ATA_TFLAG_POLLING;
2109 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
2110 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
2111 if (err_mask) {
2112 if (err_mask & AC_ERR_NODEV_HINT) {
2113 DPRINTK("ata%u.%d: NODEV after polling detection\n",
2114 ap->print_id, dev->devno);
2115 return -ENOENT;
2118 /* Device or controller might have reported the wrong
2119 * device class. Give a shot at the other IDENTIFY if
2120 * the current one is aborted by the device.
2122 if (may_fallback &&
2123 (err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
2124 may_fallback = 0;
2126 if (class == ATA_DEV_ATA)
2127 class = ATA_DEV_ATAPI;
2128 else
2129 class = ATA_DEV_ATA;
2130 goto retry;
2133 rc = -EIO;
2134 reason = "I/O error";
2135 goto err_out;
2138 /* Falling back doesn't make sense if ID data was read
2139 * successfully at least once.
2141 may_fallback = 0;
2143 swap_buf_le16(id, ATA_ID_WORDS);
2145 /* sanity check */
2146 rc = -EINVAL;
2147 reason = "device reports invalid type";
2149 if (class == ATA_DEV_ATA) {
2150 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
2151 goto err_out;
2152 } else {
2153 if (ata_id_is_ata(id))
2154 goto err_out;
2157 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
2158 tried_spinup = 1;
2160 * Drive powered-up in standby mode, and requires a specific
2161 * SET_FEATURES spin-up subcommand before it will accept
2162 * anything other than the original IDENTIFY command.
2164 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
2165 if (err_mask && id[2] != 0x738c) {
2166 rc = -EIO;
2167 reason = "SPINUP failed";
2168 goto err_out;
2171 * If the drive initially returned incomplete IDENTIFY info,
2172 * we now must reissue the IDENTIFY command.
2174 if (id[2] == 0x37c8)
2175 goto retry;
2178 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
2180 * The exact sequence expected by certain pre-ATA4 drives is:
2181 * SRST RESET
2182 * IDENTIFY (optional in early ATA)
2183 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2184 * anything else..
2185 * Some drives were very specific about that exact sequence.
2187 * Note that ATA4 says lba is mandatory so the second check
2188 * shoud never trigger.
2190 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2191 err_mask = ata_dev_init_params(dev, id[3], id[6]);
2192 if (err_mask) {
2193 rc = -EIO;
2194 reason = "INIT_DEV_PARAMS failed";
2195 goto err_out;
2198 /* current CHS translation info (id[53-58]) might be
2199 * changed. reread the identify device info.
2201 flags &= ~ATA_READID_POSTRESET;
2202 goto retry;
2206 *p_class = class;
2208 return 0;
2210 err_out:
2211 if (ata_msg_warn(ap))
2212 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
2213 "(%s, err_mask=0x%x)\n", reason, err_mask);
2214 return rc;
2217 static inline u8 ata_dev_knobble(struct ata_device *dev)
2219 struct ata_port *ap = dev->link->ap;
2220 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2223 static void ata_dev_config_ncq(struct ata_device *dev,
2224 char *desc, size_t desc_sz)
2226 struct ata_port *ap = dev->link->ap;
2227 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2229 if (!ata_id_has_ncq(dev->id)) {
2230 desc[0] = '\0';
2231 return;
2233 if (dev->horkage & ATA_HORKAGE_NONCQ) {
2234 snprintf(desc, desc_sz, "NCQ (not used)");
2235 return;
2237 if (ap->flags & ATA_FLAG_NCQ) {
2238 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
2239 dev->flags |= ATA_DFLAG_NCQ;
2242 if (hdepth >= ddepth)
2243 snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
2244 else
2245 snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
2249 * ata_dev_configure - Configure the specified ATA/ATAPI device
2250 * @dev: Target device to configure
2252 * Configure @dev according to @dev->id. Generic and low-level
2253 * driver specific fixups are also applied.
2255 * LOCKING:
2256 * Kernel thread context (may sleep)
2258 * RETURNS:
2259 * 0 on success, -errno otherwise
2261 int ata_dev_configure(struct ata_device *dev)
2263 struct ata_port *ap = dev->link->ap;
2264 struct ata_eh_context *ehc = &dev->link->eh_context;
2265 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2266 const u16 *id = dev->id;
2267 unsigned long xfer_mask;
2268 char revbuf[7]; /* XYZ-99\0 */
2269 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2270 char modelbuf[ATA_ID_PROD_LEN+1];
2271 int rc;
2273 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
2274 ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
2275 <<<<<<< HEAD:drivers/ata/libata-core.c
2276 __FUNCTION__);
2277 =======
2278 __func__);
2279 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
2280 return 0;
2283 if (ata_msg_probe(ap))
2284 <<<<<<< HEAD:drivers/ata/libata-core.c
2285 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __FUNCTION__);
2286 =======
2287 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
2288 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
2290 /* set horkage */
2291 dev->horkage |= ata_dev_blacklisted(dev);
2292 <<<<<<< HEAD:drivers/ata/libata-core.c
2293 =======
2294 ata_force_horkage(dev);
2295 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
2297 /* let ACPI work its magic */
2298 rc = ata_acpi_on_devcfg(dev);
2299 if (rc)
2300 return rc;
2302 /* massage HPA, do it early as it might change IDENTIFY data */
2303 rc = ata_hpa_resize(dev);
2304 if (rc)
2305 return rc;
2307 /* print device capabilities */
2308 if (ata_msg_probe(ap))
2309 ata_dev_printk(dev, KERN_DEBUG,
2310 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2311 "85:%04x 86:%04x 87:%04x 88:%04x\n",
2312 <<<<<<< HEAD:drivers/ata/libata-core.c
2313 __FUNCTION__,
2314 =======
2315 __func__,
2316 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
2317 id[49], id[82], id[83], id[84],
2318 id[85], id[86], id[87], id[88]);
2320 /* initialize to-be-configured parameters */
2321 dev->flags &= ~ATA_DFLAG_CFG_MASK;
2322 dev->max_sectors = 0;
2323 dev->cdb_len = 0;
2324 dev->n_sectors = 0;
2325 dev->cylinders = 0;
2326 dev->heads = 0;
2327 dev->sectors = 0;
2330 * common ATA, ATAPI feature tests
2333 /* find max transfer mode; for printk only */
2334 xfer_mask = ata_id_xfermask(id);
2336 if (ata_msg_probe(ap))
2337 ata_dump_id(id);
2339 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2340 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2341 sizeof(fwrevbuf));
2343 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2344 sizeof(modelbuf));
2346 /* ATA-specific feature tests */
2347 if (dev->class == ATA_DEV_ATA) {
2348 if (ata_id_is_cfa(id)) {
2349 if (id[162] & 1) /* CPRM may make this media unusable */
2350 ata_dev_printk(dev, KERN_WARNING,
2351 "supports DRM functions and may "
2352 "not be fully accessable.\n");
2353 snprintf(revbuf, 7, "CFA");
2354 } else {
2355 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2356 /* Warn the user if the device has TPM extensions */
2357 if (ata_id_has_tpm(id))
2358 ata_dev_printk(dev, KERN_WARNING,
2359 "supports DRM functions and may "
2360 "not be fully accessable.\n");
2363 dev->n_sectors = ata_id_n_sectors(id);
2365 if (dev->id[59] & 0x100)
2366 dev->multi_count = dev->id[59] & 0xff;
2368 if (ata_id_has_lba(id)) {
2369 const char *lba_desc;
2370 char ncq_desc[20];
2372 lba_desc = "LBA";
2373 dev->flags |= ATA_DFLAG_LBA;
2374 if (ata_id_has_lba48(id)) {
2375 dev->flags |= ATA_DFLAG_LBA48;
2376 lba_desc = "LBA48";
2378 if (dev->n_sectors >= (1UL << 28) &&
2379 ata_id_has_flush_ext(id))
2380 dev->flags |= ATA_DFLAG_FLUSH_EXT;
2383 /* config NCQ */
2384 ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2386 /* print device info to dmesg */
2387 if (ata_msg_drv(ap) && print_info) {
2388 ata_dev_printk(dev, KERN_INFO,
2389 "%s: %s, %s, max %s\n",
2390 revbuf, modelbuf, fwrevbuf,
2391 ata_mode_string(xfer_mask));
2392 ata_dev_printk(dev, KERN_INFO,
2393 "%Lu sectors, multi %u: %s %s\n",
2394 (unsigned long long)dev->n_sectors,
2395 dev->multi_count, lba_desc, ncq_desc);
2397 } else {
2398 /* CHS */
2400 /* Default translation */
2401 dev->cylinders = id[1];
2402 dev->heads = id[3];
2403 dev->sectors = id[6];
2405 if (ata_id_current_chs_valid(id)) {
2406 /* Current CHS translation is valid. */
2407 dev->cylinders = id[54];
2408 dev->heads = id[55];
2409 dev->sectors = id[56];
2412 /* print device info to dmesg */
2413 if (ata_msg_drv(ap) && print_info) {
2414 ata_dev_printk(dev, KERN_INFO,
2415 "%s: %s, %s, max %s\n",
2416 revbuf, modelbuf, fwrevbuf,
2417 ata_mode_string(xfer_mask));
2418 ata_dev_printk(dev, KERN_INFO,
2419 "%Lu sectors, multi %u, CHS %u/%u/%u\n",
2420 (unsigned long long)dev->n_sectors,
2421 dev->multi_count, dev->cylinders,
2422 dev->heads, dev->sectors);
2426 dev->cdb_len = 16;
2429 /* ATAPI-specific feature tests */
2430 else if (dev->class == ATA_DEV_ATAPI) {
2431 const char *cdb_intr_string = "";
2432 const char *atapi_an_string = "";
2433 <<<<<<< HEAD:drivers/ata/libata-core.c
2434 =======
2435 const char *dma_dir_string = "";
2436 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
2437 u32 sntf;
2439 rc = atapi_cdb_len(id);
2440 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2441 if (ata_msg_warn(ap))
2442 ata_dev_printk(dev, KERN_WARNING,
2443 "unsupported CDB len\n");
2444 rc = -EINVAL;
2445 goto err_out_nosup;
2447 dev->cdb_len = (unsigned int) rc;
2449 /* Enable ATAPI AN if both the host and device have
2450 * the support. If PMP is attached, SNTF is required
2451 * to enable ATAPI AN to discern between PHY status
2452 * changed notifications and ATAPI ANs.
2454 if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2455 (!ap->nr_pmp_links ||
2456 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
2457 unsigned int err_mask;
2459 /* issue SET feature command to turn this on */
2460 err_mask = ata_dev_set_feature(dev,
2461 SETFEATURES_SATA_ENABLE, SATA_AN);
2462 if (err_mask)
2463 ata_dev_printk(dev, KERN_ERR,
2464 "failed to enable ATAPI AN "
2465 "(err_mask=0x%x)\n", err_mask);
2466 else {
2467 dev->flags |= ATA_DFLAG_AN;
2468 atapi_an_string = ", ATAPI AN";
2472 if (ata_id_cdb_intr(dev->id)) {
2473 dev->flags |= ATA_DFLAG_CDB_INTR;
2474 cdb_intr_string = ", CDB intr";
2477 <<<<<<< HEAD:drivers/ata/libata-core.c
2478 =======
2479 if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
2480 dev->flags |= ATA_DFLAG_DMADIR;
2481 dma_dir_string = ", DMADIR";
2484 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
2485 /* print device info to dmesg */
2486 if (ata_msg_drv(ap) && print_info)
2487 ata_dev_printk(dev, KERN_INFO,
2488 <<<<<<< HEAD:drivers/ata/libata-core.c
2489 "ATAPI: %s, %s, max %s%s%s\n",
2490 =======
2491 "ATAPI: %s, %s, max %s%s%s%s\n",
2492 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
2493 modelbuf, fwrevbuf,
2494 ata_mode_string(xfer_mask),
2495 <<<<<<< HEAD:drivers/ata/libata-core.c
2496 cdb_intr_string, atapi_an_string);
2497 =======
2498 cdb_intr_string, atapi_an_string,
2499 dma_dir_string);
2500 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
2503 /* determine max_sectors */
2504 dev->max_sectors = ATA_MAX_SECTORS;
2505 if (dev->flags & ATA_DFLAG_LBA48)
2506 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2508 if (!(dev->horkage & ATA_HORKAGE_IPM)) {
2509 if (ata_id_has_hipm(dev->id))
2510 dev->flags |= ATA_DFLAG_HIPM;
2511 if (ata_id_has_dipm(dev->id))
2512 dev->flags |= ATA_DFLAG_DIPM;
2515 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2516 200 sectors */
2517 if (ata_dev_knobble(dev)) {
2518 if (ata_msg_drv(ap) && print_info)
2519 ata_dev_printk(dev, KERN_INFO,
2520 "applying bridge limits\n");
2521 dev->udma_mask &= ATA_UDMA5;
2522 dev->max_sectors = ATA_MAX_SECTORS;
2525 if ((dev->class == ATA_DEV_ATAPI) &&
2526 (atapi_command_packet_set(id) == TYPE_TAPE)) {
2527 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2528 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2531 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
2532 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2533 dev->max_sectors);
2535 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_IPM) {
2536 dev->horkage |= ATA_HORKAGE_IPM;
2538 /* reset link pm_policy for this port to no pm */
2539 ap->pm_policy = MAX_PERFORMANCE;
2542 if (ap->ops->dev_config)
2543 ap->ops->dev_config(dev);
2545 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2546 /* Let the user know. We don't want to disallow opens for
2547 rescue purposes, or in case the vendor is just a blithering
2548 idiot. Do this after the dev_config call as some controllers
2549 with buggy firmware may want to avoid reporting false device
2550 bugs */
2552 if (print_info) {
2553 ata_dev_printk(dev, KERN_WARNING,
2554 "Drive reports diagnostics failure. This may indicate a drive\n");
2555 ata_dev_printk(dev, KERN_WARNING,
2556 "fault or invalid emulation. Contact drive vendor for information.\n");
2560 if (ata_msg_probe(ap))
2561 ata_dev_printk(dev, KERN_DEBUG, "%s: EXIT, drv_stat = 0x%x\n",
2562 <<<<<<< HEAD:drivers/ata/libata-core.c
2563 __FUNCTION__, ata_chk_status(ap));
2564 =======
2565 __func__, ata_chk_status(ap));
2566 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
2567 return 0;
2569 err_out_nosup:
2570 if (ata_msg_probe(ap))
2571 ata_dev_printk(dev, KERN_DEBUG,
2572 <<<<<<< HEAD:drivers/ata/libata-core.c
2573 "%s: EXIT, err\n", __FUNCTION__);
2574 =======
2575 "%s: EXIT, err\n", __func__);
2576 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
2577 return rc;
2581 * ata_cable_40wire - return 40 wire cable type
2582 * @ap: port
2584 * Helper method for drivers which want to hardwire 40 wire cable
2585 * detection.
2588 int ata_cable_40wire(struct ata_port *ap)
2590 return ATA_CBL_PATA40;
2594 * ata_cable_80wire - return 80 wire cable type
2595 * @ap: port
2597 * Helper method for drivers which want to hardwire 80 wire cable
2598 * detection.
2601 int ata_cable_80wire(struct ata_port *ap)
2603 return ATA_CBL_PATA80;
2607 * ata_cable_unknown - return unknown PATA cable.
2608 * @ap: port
2610 * Helper method for drivers which have no PATA cable detection.
2613 int ata_cable_unknown(struct ata_port *ap)
2615 return ATA_CBL_PATA_UNK;
2619 * ata_cable_ignore - return ignored PATA cable.
2620 * @ap: port
2622 * Helper method for drivers which don't use cable type to limit
2623 * transfer mode.
2625 int ata_cable_ignore(struct ata_port *ap)
2627 return ATA_CBL_PATA_IGN;
2631 * ata_cable_sata - return SATA cable type
2632 * @ap: port
2634 * Helper method for drivers which have SATA cables
2637 int ata_cable_sata(struct ata_port *ap)
2639 return ATA_CBL_SATA;
2643 * ata_bus_probe - Reset and probe ATA bus
2644 * @ap: Bus to probe
2646 * Master ATA bus probing function. Initiates a hardware-dependent
2647 * bus reset, then attempts to identify any devices found on
2648 * the bus.
2650 * LOCKING:
2651 * PCI/etc. bus probe sem.
2653 * RETURNS:
2654 * Zero on success, negative errno otherwise.
2657 int ata_bus_probe(struct ata_port *ap)
2659 unsigned int classes[ATA_MAX_DEVICES];
2660 int tries[ATA_MAX_DEVICES];
2661 int rc;
2662 struct ata_device *dev;
2664 ata_port_probe(ap);
2666 ata_link_for_each_dev(dev, &ap->link)
2667 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2669 retry:
2670 ata_link_for_each_dev(dev, &ap->link) {
2671 /* If we issue an SRST then an ATA drive (not ATAPI)
2672 * may change configuration and be in PIO0 timing. If
2673 * we do a hard reset (or are coming from power on)
2674 * this is true for ATA or ATAPI. Until we've set a
2675 * suitable controller mode we should not touch the
2676 * bus as we may be talking too fast.
2678 dev->pio_mode = XFER_PIO_0;
2680 /* If the controller has a pio mode setup function
2681 * then use it to set the chipset to rights. Don't
2682 * touch the DMA setup as that will be dealt with when
2683 * configuring devices.
2685 if (ap->ops->set_piomode)
2686 ap->ops->set_piomode(ap, dev);
2689 /* reset and determine device classes */
2690 ap->ops->phy_reset(ap);
2692 ata_link_for_each_dev(dev, &ap->link) {
2693 if (!(ap->flags & ATA_FLAG_DISABLED) &&
2694 dev->class != ATA_DEV_UNKNOWN)
2695 classes[dev->devno] = dev->class;
2696 else
2697 classes[dev->devno] = ATA_DEV_NONE;
2699 dev->class = ATA_DEV_UNKNOWN;
2702 ata_port_probe(ap);
2704 /* read IDENTIFY page and configure devices. We have to do the identify
2705 specific sequence bass-ackwards so that PDIAG- is released by
2706 the slave device */
2708 ata_link_for_each_dev(dev, &ap->link) {
2709 if (tries[dev->devno])
2710 dev->class = classes[dev->devno];
2712 if (!ata_dev_enabled(dev))
2713 continue;
2715 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2716 dev->id);
2717 if (rc)
2718 goto fail;
2721 /* Now ask for the cable type as PDIAG- should have been released */
2722 if (ap->ops->cable_detect)
2723 ap->cbl = ap->ops->cable_detect(ap);
2725 /* We may have SATA bridge glue hiding here irrespective of the
2726 reported cable types and sensed types */
2727 ata_link_for_each_dev(dev, &ap->link) {
2728 if (!ata_dev_enabled(dev))
2729 continue;
2730 /* SATA drives indicate we have a bridge. We don't know which
2731 end of the link the bridge is which is a problem */
2732 if (ata_id_is_sata(dev->id))
2733 ap->cbl = ATA_CBL_SATA;
2736 /* After the identify sequence we can now set up the devices. We do
2737 this in the normal order so that the user doesn't get confused */
2739 ata_link_for_each_dev(dev, &ap->link) {
2740 if (!ata_dev_enabled(dev))
2741 continue;
2743 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2744 rc = ata_dev_configure(dev);
2745 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2746 if (rc)
2747 goto fail;
2750 /* configure transfer mode */
2751 rc = ata_set_mode(&ap->link, &dev);
2752 if (rc)
2753 goto fail;
2755 ata_link_for_each_dev(dev, &ap->link)
2756 if (ata_dev_enabled(dev))
2757 return 0;
2759 /* no device present, disable port */
2760 ata_port_disable(ap);
2761 return -ENODEV;
2763 fail:
2764 tries[dev->devno]--;
2766 switch (rc) {
2767 case -EINVAL:
2768 /* eeek, something went very wrong, give up */
2769 tries[dev->devno] = 0;
2770 break;
2772 case -ENODEV:
2773 /* give it just one more chance */
2774 tries[dev->devno] = min(tries[dev->devno], 1);
2775 case -EIO:
2776 if (tries[dev->devno] == 1) {
2777 /* This is the last chance, better to slow
2778 * down than lose it.
2780 sata_down_spd_limit(&ap->link);
2781 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2785 if (!tries[dev->devno])
2786 ata_dev_disable(dev);
2788 goto retry;
2792 * ata_port_probe - Mark port as enabled
2793 * @ap: Port for which we indicate enablement
2795 * Modify @ap data structure such that the system
2796 * thinks that the entire port is enabled.
2798 * LOCKING: host lock, or some other form of
2799 * serialization.
2802 void ata_port_probe(struct ata_port *ap)
2804 ap->flags &= ~ATA_FLAG_DISABLED;
2808 * sata_print_link_status - Print SATA link status
2809 * @link: SATA link to printk link status about
2811 * This function prints link speed and status of a SATA link.
2813 * LOCKING:
2814 * None.
2816 void sata_print_link_status(struct ata_link *link)
2818 u32 sstatus, scontrol, tmp;
2820 if (sata_scr_read(link, SCR_STATUS, &sstatus))
2821 return;
2822 sata_scr_read(link, SCR_CONTROL, &scontrol);
2824 if (ata_link_online(link)) {
2825 tmp = (sstatus >> 4) & 0xf;
2826 ata_link_printk(link, KERN_INFO,
2827 "SATA link up %s (SStatus %X SControl %X)\n",
2828 sata_spd_string(tmp), sstatus, scontrol);
2829 } else {
2830 ata_link_printk(link, KERN_INFO,
2831 "SATA link down (SStatus %X SControl %X)\n",
2832 sstatus, scontrol);
2837 * ata_dev_pair - return other device on cable
2838 * @adev: device
2840 * Obtain the other device on the same cable, or if none is
2841 * present NULL is returned
2844 struct ata_device *ata_dev_pair(struct ata_device *adev)
2846 struct ata_link *link = adev->link;
2847 struct ata_device *pair = &link->device[1 - adev->devno];
2848 if (!ata_dev_enabled(pair))
2849 return NULL;
2850 return pair;
2854 * ata_port_disable - Disable port.
2855 * @ap: Port to be disabled.
2857 * Modify @ap data structure such that the system
2858 * thinks that the entire port is disabled, and should
2859 * never attempt to probe or communicate with devices
2860 * on this port.
2862 * LOCKING: host lock, or some other form of
2863 * serialization.
2866 void ata_port_disable(struct ata_port *ap)
2868 ap->link.device[0].class = ATA_DEV_NONE;
2869 ap->link.device[1].class = ATA_DEV_NONE;
2870 ap->flags |= ATA_FLAG_DISABLED;
2874 * sata_down_spd_limit - adjust SATA spd limit downward
2875 * @link: Link to adjust SATA spd limit for
2877 * Adjust SATA spd limit of @link downward. Note that this
2878 * function only adjusts the limit. The change must be applied
2879 * using sata_set_spd().
2881 * LOCKING:
2882 * Inherited from caller.
2884 * RETURNS:
2885 * 0 on success, negative errno on failure
2887 int sata_down_spd_limit(struct ata_link *link)
2889 u32 sstatus, spd, mask;
2890 int rc, highbit;
2892 if (!sata_scr_valid(link))
2893 return -EOPNOTSUPP;
2895 /* If SCR can be read, use it to determine the current SPD.
2896 * If not, use cached value in link->sata_spd.
2898 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
2899 if (rc == 0)
2900 spd = (sstatus >> 4) & 0xf;
2901 else
2902 spd = link->sata_spd;
2904 mask = link->sata_spd_limit;
2905 if (mask <= 1)
2906 return -EINVAL;
2908 /* unconditionally mask off the highest bit */
2909 highbit = fls(mask) - 1;
2910 mask &= ~(1 << highbit);
2912 /* Mask off all speeds higher than or equal to the current
2913 * one. Force 1.5Gbps if current SPD is not available.
2915 if (spd > 1)
2916 mask &= (1 << (spd - 1)) - 1;
2917 else
2918 mask &= 1;
2920 /* were we already at the bottom? */
2921 if (!mask)
2922 return -EINVAL;
2924 link->sata_spd_limit = mask;
2926 ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
2927 sata_spd_string(fls(mask)));
2929 return 0;
2932 static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
2934 struct ata_link *host_link = &link->ap->link;
2935 u32 limit, target, spd;
2937 limit = link->sata_spd_limit;
2939 /* Don't configure downstream link faster than upstream link.
2940 * It doesn't speed up anything and some PMPs choke on such
2941 * configuration.
2943 if (!ata_is_host_link(link) && host_link->sata_spd)
2944 limit &= (1 << host_link->sata_spd) - 1;
2946 if (limit == UINT_MAX)
2947 target = 0;
2948 else
2949 target = fls(limit);
2951 spd = (*scontrol >> 4) & 0xf;
2952 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
2954 return spd != target;
2958 * sata_set_spd_needed - is SATA spd configuration needed
2959 * @link: Link in question
2961 * Test whether the spd limit in SControl matches
2962 * @link->sata_spd_limit. This function is used to determine
2963 * whether hardreset is necessary to apply SATA spd
2964 * configuration.
2966 * LOCKING:
2967 * Inherited from caller.
2969 * RETURNS:
2970 * 1 if SATA spd configuration is needed, 0 otherwise.
2972 int sata_set_spd_needed(struct ata_link *link)
2974 u32 scontrol;
2976 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
2977 return 1;
2979 return __sata_set_spd_needed(link, &scontrol);
2983 * sata_set_spd - set SATA spd according to spd limit
2984 * @link: Link to set SATA spd for
2986 * Set SATA spd of @link according to sata_spd_limit.
2988 * LOCKING:
2989 * Inherited from caller.
2991 * RETURNS:
2992 * 0 if spd doesn't need to be changed, 1 if spd has been
2993 * changed. Negative errno if SCR registers are inaccessible.
2995 int sata_set_spd(struct ata_link *link)
2997 u32 scontrol;
2998 int rc;
3000 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3001 return rc;
3003 if (!__sata_set_spd_needed(link, &scontrol))
3004 return 0;
3006 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3007 return rc;
3009 return 1;
3013 * This mode timing computation functionality is ported over from
3014 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
3017 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
3018 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
3019 * for UDMA6, which is currently supported only by Maxtor drives.
3021 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
3024 static const struct ata_timing ata_timing[] = {
3025 /* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
3026 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
3027 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
3028 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
3029 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
3030 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
3031 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
3032 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
3034 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
3035 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
3036 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
3038 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
3039 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
3040 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
3041 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
3042 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
3044 /* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
3045 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
3046 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
3047 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
3048 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
3049 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
3050 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
3051 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
3053 { 0xFF }
3056 #define ENOUGH(v, unit) (((v)-1)/(unit)+1)
3057 #define EZ(v, unit) ((v)?ENOUGH(v, unit):0)
3059 static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
3061 q->setup = EZ(t->setup * 1000, T);
3062 q->act8b = EZ(t->act8b * 1000, T);
3063 q->rec8b = EZ(t->rec8b * 1000, T);
3064 q->cyc8b = EZ(t->cyc8b * 1000, T);
3065 q->active = EZ(t->active * 1000, T);
3066 q->recover = EZ(t->recover * 1000, T);
3067 q->cycle = EZ(t->cycle * 1000, T);
3068 q->udma = EZ(t->udma * 1000, UT);
3071 void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
3072 struct ata_timing *m, unsigned int what)
3074 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
3075 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
3076 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
3077 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
3078 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
3079 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
3080 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
3081 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
3084 const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
3086 const struct ata_timing *t = ata_timing;
3088 while (xfer_mode > t->mode)
3089 t++;
3091 if (xfer_mode == t->mode)
3092 return t;
3093 return NULL;
3096 int ata_timing_compute(struct ata_device *adev, unsigned short speed,
3097 struct ata_timing *t, int T, int UT)
3099 const struct ata_timing *s;
3100 struct ata_timing p;
3103 * Find the mode.
3106 if (!(s = ata_timing_find_mode(speed)))
3107 return -EINVAL;
3109 memcpy(t, s, sizeof(*s));
3112 * If the drive is an EIDE drive, it can tell us it needs extended
3113 * PIO/MW_DMA cycle timing.
3116 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
3117 memset(&p, 0, sizeof(p));
3118 if (speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
3119 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
3120 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
3121 } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
3122 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
3124 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
3128 * Convert the timing to bus clock counts.
3131 ata_timing_quantize(t, t, T, UT);
3134 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3135 * S.M.A.R.T * and some other commands. We have to ensure that the
3136 * DMA cycle timing is slower/equal than the fastest PIO timing.
3139 if (speed > XFER_PIO_6) {
3140 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3141 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3145 * Lengthen active & recovery time so that cycle time is correct.
3148 if (t->act8b + t->rec8b < t->cyc8b) {
3149 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3150 t->rec8b = t->cyc8b - t->act8b;
3153 if (t->active + t->recover < t->cycle) {
3154 t->active += (t->cycle - (t->active + t->recover)) / 2;
3155 t->recover = t->cycle - t->active;
3158 /* In a few cases quantisation may produce enough errors to
3159 leave t->cycle too low for the sum of active and recovery
3160 if so we must correct this */
3161 if (t->active + t->recover > t->cycle)
3162 t->cycle = t->active + t->recover;
3164 return 0;
3168 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3169 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3170 * @cycle: cycle duration in ns
3172 * Return matching xfer mode for @cycle. The returned mode is of
3173 * the transfer type specified by @xfer_shift. If @cycle is too
3174 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3175 * than the fastest known mode, the fasted mode is returned.
3177 * LOCKING:
3178 * None.
3180 * RETURNS:
3181 * Matching xfer_mode, 0xff if no match found.
3183 u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3185 u8 base_mode = 0xff, last_mode = 0xff;
3186 const struct ata_xfer_ent *ent;
3187 const struct ata_timing *t;
3189 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3190 if (ent->shift == xfer_shift)
3191 base_mode = ent->base;
3193 for (t = ata_timing_find_mode(base_mode);
3194 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3195 unsigned short this_cycle;
3197 switch (xfer_shift) {
3198 case ATA_SHIFT_PIO:
3199 case ATA_SHIFT_MWDMA:
3200 this_cycle = t->cycle;
3201 break;
3202 case ATA_SHIFT_UDMA:
3203 this_cycle = t->udma;
3204 break;
3205 default:
3206 return 0xff;
3209 if (cycle > this_cycle)
3210 break;
3212 last_mode = t->mode;
3215 return last_mode;
3219 * ata_down_xfermask_limit - adjust dev xfer masks downward
3220 * @dev: Device to adjust xfer masks
3221 * @sel: ATA_DNXFER_* selector
3223 * Adjust xfer masks of @dev downward. Note that this function
3224 * does not apply the change. Invoking ata_set_mode() afterwards
3225 * will apply the limit.
3227 * LOCKING:
3228 * Inherited from caller.
3230 * RETURNS:
3231 * 0 on success, negative errno on failure
3233 int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3235 char buf[32];
3236 unsigned long orig_mask, xfer_mask;
3237 unsigned long pio_mask, mwdma_mask, udma_mask;
3238 int quiet, highbit;
3240 quiet = !!(sel & ATA_DNXFER_QUIET);
3241 sel &= ~ATA_DNXFER_QUIET;
3243 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3244 dev->mwdma_mask,
3245 dev->udma_mask);
3246 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3248 switch (sel) {
3249 case ATA_DNXFER_PIO:
3250 highbit = fls(pio_mask) - 1;
3251 pio_mask &= ~(1 << highbit);
3252 break;
3254 case ATA_DNXFER_DMA:
3255 if (udma_mask) {
3256 highbit = fls(udma_mask) - 1;
3257 udma_mask &= ~(1 << highbit);
3258 if (!udma_mask)
3259 return -ENOENT;
3260 } else if (mwdma_mask) {
3261 highbit = fls(mwdma_mask) - 1;
3262 mwdma_mask &= ~(1 << highbit);
3263 if (!mwdma_mask)
3264 return -ENOENT;
3266 break;
3268 case ATA_DNXFER_40C:
3269 udma_mask &= ATA_UDMA_MASK_40C;
3270 break;
3272 case ATA_DNXFER_FORCE_PIO0:
3273 pio_mask &= 1;
3274 case ATA_DNXFER_FORCE_PIO:
3275 mwdma_mask = 0;
3276 udma_mask = 0;
3277 break;
3279 default:
3280 BUG();
3283 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3285 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3286 return -ENOENT;
3288 if (!quiet) {
3289 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3290 snprintf(buf, sizeof(buf), "%s:%s",
3291 ata_mode_string(xfer_mask),
3292 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3293 else
3294 snprintf(buf, sizeof(buf), "%s",
3295 ata_mode_string(xfer_mask));
3297 ata_dev_printk(dev, KERN_WARNING,
3298 "limiting speed to %s\n", buf);
3301 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3302 &dev->udma_mask);
3304 return 0;
3307 static int ata_dev_set_mode(struct ata_device *dev)
3309 struct ata_eh_context *ehc = &dev->link->eh_context;
3310 const char *dev_err_whine = "";
3311 int ign_dev_err = 0;
3312 unsigned int err_mask;
3313 int rc;
3315 dev->flags &= ~ATA_DFLAG_PIO;
3316 if (dev->xfer_shift == ATA_SHIFT_PIO)
3317 dev->flags |= ATA_DFLAG_PIO;
3319 err_mask = ata_dev_set_xfermode(dev);
3321 if (err_mask & ~AC_ERR_DEV)
3322 goto fail;
3324 /* revalidate */
3325 ehc->i.flags |= ATA_EHI_POST_SETMODE;
3326 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3327 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3328 if (rc)
3329 return rc;
3331 /* Old CFA may refuse this command, which is just fine */
3332 if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
3333 ign_dev_err = 1;
3335 /* Some very old devices and some bad newer ones fail any kind of
3336 SET_XFERMODE request but support PIO0-2 timings and no IORDY */
3337 if (dev->xfer_shift == ATA_SHIFT_PIO && !ata_id_has_iordy(dev->id) &&
3338 dev->pio_mode <= XFER_PIO_2)
3339 ign_dev_err = 1;
3341 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3342 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
3343 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3344 dev->dma_mode == XFER_MW_DMA_0 &&
3345 (dev->id[63] >> 8) & 1)
3346 ign_dev_err = 1;
3348 /* if the device is actually configured correctly, ignore dev err */
3349 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3350 ign_dev_err = 1;
3352 if (err_mask & AC_ERR_DEV) {
3353 if (!ign_dev_err)
3354 goto fail;
3355 else
3356 dev_err_whine = " (device error ignored)";
3359 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3360 dev->xfer_shift, (int)dev->xfer_mode);
3362 ata_dev_printk(dev, KERN_INFO, "configured for %s%s\n",
3363 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3364 dev_err_whine);
3366 return 0;
3368 fail:
3369 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
3370 "(err_mask=0x%x)\n", err_mask);
3371 return -EIO;
3375 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
3376 * @link: link on which timings will be programmed
3377 * @r_failed_dev: out parameter for failed device
3379 * Standard implementation of the function used to tune and set
3380 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3381 * ata_dev_set_mode() fails, pointer to the failing device is
3382 * returned in @r_failed_dev.
3384 * LOCKING:
3385 * PCI/etc. bus probe sem.
3387 * RETURNS:
3388 * 0 on success, negative errno otherwise
3391 int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3393 struct ata_port *ap = link->ap;
3394 struct ata_device *dev;
3395 int rc = 0, used_dma = 0, found = 0;
3397 /* step 1: calculate xfer_mask */
3398 ata_link_for_each_dev(dev, link) {
3399 unsigned long pio_mask, dma_mask;
3400 unsigned int mode_mask;
3402 if (!ata_dev_enabled(dev))
3403 continue;
3405 mode_mask = ATA_DMA_MASK_ATA;
3406 if (dev->class == ATA_DEV_ATAPI)
3407 mode_mask = ATA_DMA_MASK_ATAPI;
3408 else if (ata_id_is_cfa(dev->id))
3409 mode_mask = ATA_DMA_MASK_CFA;
3411 ata_dev_xfermask(dev);
3412 <<<<<<< HEAD:drivers/ata/libata-core.c
3413 =======
3414 ata_force_xfermask(dev);
3415 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
3417 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3418 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3420 if (libata_dma_mask & mode_mask)
3421 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3422 else
3423 dma_mask = 0;
3425 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3426 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3428 found = 1;
3429 if (dev->dma_mode != 0xff)
3430 used_dma = 1;
3432 if (!found)
3433 goto out;
3435 /* step 2: always set host PIO timings */
3436 ata_link_for_each_dev(dev, link) {
3437 if (!ata_dev_enabled(dev))
3438 continue;
3440 if (dev->pio_mode == 0xff) {
3441 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
3442 rc = -EINVAL;
3443 goto out;
3446 dev->xfer_mode = dev->pio_mode;
3447 dev->xfer_shift = ATA_SHIFT_PIO;
3448 if (ap->ops->set_piomode)
3449 ap->ops->set_piomode(ap, dev);
3452 /* step 3: set host DMA timings */
3453 ata_link_for_each_dev(dev, link) {
3454 if (!ata_dev_enabled(dev) || dev->dma_mode == 0xff)
3455 continue;
3457 dev->xfer_mode = dev->dma_mode;
3458 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3459 if (ap->ops->set_dmamode)
3460 ap->ops->set_dmamode(ap, dev);
3463 /* step 4: update devices' xfer mode */
3464 ata_link_for_each_dev(dev, link) {
3465 /* don't update suspended devices' xfer mode */
3466 if (!ata_dev_enabled(dev))
3467 continue;
3469 rc = ata_dev_set_mode(dev);
3470 if (rc)
3471 goto out;
3474 /* Record simplex status. If we selected DMA then the other
3475 * host channels are not permitted to do so.
3477 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3478 ap->host->simplex_claimed = ap;
3480 out:
3481 if (rc)
3482 *r_failed_dev = dev;
3483 return rc;
3487 * ata_tf_to_host - issue ATA taskfile to host controller
3488 * @ap: port to which command is being issued
3489 * @tf: ATA taskfile register set
3491 * Issues ATA taskfile register set to ATA host controller,
3492 * with proper synchronization with interrupt handler and
3493 * other threads.
3495 * LOCKING:
3496 * spin_lock_irqsave(host lock)
3499 static inline void ata_tf_to_host(struct ata_port *ap,
3500 const struct ata_taskfile *tf)
3502 ap->ops->tf_load(ap, tf);
3503 ap->ops->exec_command(ap, tf);
3507 * ata_busy_sleep - sleep until BSY clears, or timeout
3508 * @ap: port containing status register to be polled
3509 * @tmout_pat: impatience timeout
3510 * @tmout: overall timeout
3512 * Sleep until ATA Status register bit BSY clears,
3513 * or a timeout occurs.
3515 * LOCKING:
3516 * Kernel thread context (may sleep).
3518 * RETURNS:
3519 * 0 on success, -errno otherwise.
3521 int ata_busy_sleep(struct ata_port *ap,
3522 unsigned long tmout_pat, unsigned long tmout)
3524 unsigned long timer_start, timeout;
3525 u8 status;
3527 status = ata_busy_wait(ap, ATA_BUSY, 300);
3528 timer_start = jiffies;
3529 timeout = timer_start + tmout_pat;
3530 while (status != 0xff && (status & ATA_BUSY) &&
3531 time_before(jiffies, timeout)) {
3532 msleep(50);
3533 status = ata_busy_wait(ap, ATA_BUSY, 3);
3536 if (status != 0xff && (status & ATA_BUSY))
3537 ata_port_printk(ap, KERN_WARNING,
3538 "port is slow to respond, please be patient "
3539 "(Status 0x%x)\n", status);
3541 timeout = timer_start + tmout;
3542 while (status != 0xff && (status & ATA_BUSY) &&
3543 time_before(jiffies, timeout)) {
3544 msleep(50);
3545 status = ata_chk_status(ap);
3548 if (status == 0xff)
3549 return -ENODEV;
3551 if (status & ATA_BUSY) {
3552 ata_port_printk(ap, KERN_ERR, "port failed to respond "
3553 "(%lu secs, Status 0x%x)\n",
3554 tmout / HZ, status);
3555 return -EBUSY;
3558 return 0;
3562 * ata_wait_after_reset - wait before checking status after reset
3563 * @ap: port containing status register to be polled
3564 * @deadline: deadline jiffies for the operation
3566 * After reset, we need to pause a while before reading status.
3567 * Also, certain combination of controller and device report 0xff
3568 * for some duration (e.g. until SATA PHY is up and running)
3569 * which is interpreted as empty port in ATA world. This
3570 * function also waits for such devices to get out of 0xff
3571 * status.
3573 * LOCKING:
3574 * Kernel thread context (may sleep).
3576 void ata_wait_after_reset(struct ata_port *ap, unsigned long deadline)
3578 unsigned long until = jiffies + ATA_TMOUT_FF_WAIT;
3580 if (time_before(until, deadline))
3581 deadline = until;
3583 /* Spec mandates ">= 2ms" before checking status. We wait
3584 * 150ms, because that was the magic delay used for ATAPI
3585 * devices in Hale Landis's ATADRVR, for the period of time
3586 * between when the ATA command register is written, and then
3587 * status is checked. Because waiting for "a while" before
3588 * checking status is fine, post SRST, we perform this magic
3589 * delay here as well.
3591 * Old drivers/ide uses the 2mS rule and then waits for ready.
3593 msleep(150);
3595 /* Wait for 0xff to clear. Some SATA devices take a long time
3596 * to clear 0xff after reset. For example, HHD424020F7SV00
3597 * iVDR needs >= 800ms while. Quantum GoVault needs even more
3598 * than that.
3600 * Note that some PATA controllers (pata_ali) explode if
3601 * status register is read more than once when there's no
3602 * device attached.
3604 if (ap->flags & ATA_FLAG_SATA) {
3605 while (1) {
3606 u8 status = ata_chk_status(ap);
3608 if (status != 0xff || time_after(jiffies, deadline))
3609 return;
3611 msleep(50);
3617 * ata_wait_ready - sleep until BSY clears, or timeout
3618 * @ap: port containing status register to be polled
3619 * @deadline: deadline jiffies for the operation
3621 * Sleep until ATA Status register bit BSY clears, or timeout
3622 * occurs.
3624 * LOCKING:
3625 * Kernel thread context (may sleep).
3627 * RETURNS:
3628 * 0 on success, -errno otherwise.
3630 int ata_wait_ready(struct ata_port *ap, unsigned long deadline)
3632 unsigned long start = jiffies;
3633 int warned = 0;
3635 while (1) {
3636 u8 status = ata_chk_status(ap);
3637 unsigned long now = jiffies;
3639 if (!(status & ATA_BUSY))
3640 return 0;
3641 if (!ata_link_online(&ap->link) && status == 0xff)
3642 return -ENODEV;
3643 if (time_after(now, deadline))
3644 return -EBUSY;
3646 if (!warned && time_after(now, start + 5 * HZ) &&
3647 (deadline - now > 3 * HZ)) {
3648 ata_port_printk(ap, KERN_WARNING,
3649 "port is slow to respond, please be patient "
3650 "(Status 0x%x)\n", status);
3651 warned = 1;
3654 msleep(50);
3658 static int ata_bus_post_reset(struct ata_port *ap, unsigned int devmask,
3659 unsigned long deadline)
3661 struct ata_ioports *ioaddr = &ap->ioaddr;
3662 unsigned int dev0 = devmask & (1 << 0);
3663 unsigned int dev1 = devmask & (1 << 1);
3664 int rc, ret = 0;
3666 /* if device 0 was found in ata_devchk, wait for its
3667 * BSY bit to clear
3669 if (dev0) {
3670 rc = ata_wait_ready(ap, deadline);
3671 if (rc) {
3672 if (rc != -ENODEV)
3673 return rc;
3674 ret = rc;
3678 /* if device 1 was found in ata_devchk, wait for register
3679 * access briefly, then wait for BSY to clear.
3681 if (dev1) {
3682 int i;
3684 ap->ops->dev_select(ap, 1);
3686 /* Wait for register access. Some ATAPI devices fail
3687 * to set nsect/lbal after reset, so don't waste too
3688 * much time on it. We're gonna wait for !BSY anyway.
3690 for (i = 0; i < 2; i++) {
3691 u8 nsect, lbal;
3693 nsect = ioread8(ioaddr->nsect_addr);
3694 lbal = ioread8(ioaddr->lbal_addr);
3695 if ((nsect == 1) && (lbal == 1))
3696 break;
3697 msleep(50); /* give drive a breather */
3700 rc = ata_wait_ready(ap, deadline);
3701 if (rc) {
3702 if (rc != -ENODEV)
3703 return rc;
3704 ret = rc;
3708 /* is all this really necessary? */
3709 ap->ops->dev_select(ap, 0);
3710 if (dev1)
3711 ap->ops->dev_select(ap, 1);
3712 if (dev0)
3713 ap->ops->dev_select(ap, 0);
3715 return ret;
3718 static int ata_bus_softreset(struct ata_port *ap, unsigned int devmask,
3719 unsigned long deadline)
3721 struct ata_ioports *ioaddr = &ap->ioaddr;
3723 DPRINTK("ata%u: bus reset via SRST\n", ap->print_id);
3725 /* software reset. causes dev0 to be selected */
3726 iowrite8(ap->ctl, ioaddr->ctl_addr);
3727 udelay(20); /* FIXME: flush */
3728 iowrite8(ap->ctl | ATA_SRST, ioaddr->ctl_addr);
3729 udelay(20); /* FIXME: flush */
3730 iowrite8(ap->ctl, ioaddr->ctl_addr);
3732 /* wait a while before checking status */
3733 ata_wait_after_reset(ap, deadline);
3735 /* Before we perform post reset processing we want to see if
3736 * the bus shows 0xFF because the odd clown forgets the D7
3737 * pulldown resistor.
3739 if (ata_chk_status(ap) == 0xFF)
3740 return -ENODEV;
3742 return ata_bus_post_reset(ap, devmask, deadline);
3746 * ata_bus_reset - reset host port and associated ATA channel
3747 * @ap: port to reset
3749 * This is typically the first time we actually start issuing
3750 * commands to the ATA channel. We wait for BSY to clear, then
3751 * issue EXECUTE DEVICE DIAGNOSTIC command, polling for its
3752 * result. Determine what devices, if any, are on the channel
3753 * by looking at the device 0/1 error register. Look at the signature
3754 * stored in each device's taskfile registers, to determine if
3755 * the device is ATA or ATAPI.
3757 * LOCKING:
3758 * PCI/etc. bus probe sem.
3759 * Obtains host lock.
3761 * SIDE EFFECTS:
3762 * Sets ATA_FLAG_DISABLED if bus reset fails.
3765 void ata_bus_reset(struct ata_port *ap)
3767 struct ata_device *device = ap->link.device;
3768 struct ata_ioports *ioaddr = &ap->ioaddr;
3769 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
3770 u8 err;
3771 unsigned int dev0, dev1 = 0, devmask = 0;
3772 int rc;
3774 DPRINTK("ENTER, host %u, port %u\n", ap->print_id, ap->port_no);
3776 /* determine if device 0/1 are present */
3777 if (ap->flags & ATA_FLAG_SATA_RESET)
3778 dev0 = 1;
3779 else {
3780 dev0 = ata_devchk(ap, 0);
3781 if (slave_possible)
3782 dev1 = ata_devchk(ap, 1);
3785 if (dev0)
3786 devmask |= (1 << 0);
3787 if (dev1)
3788 devmask |= (1 << 1);
3790 /* select device 0 again */
3791 ap->ops->dev_select(ap, 0);
3793 /* issue bus reset */
3794 if (ap->flags & ATA_FLAG_SRST) {
3795 rc = ata_bus_softreset(ap, devmask, jiffies + 40 * HZ);
3796 if (rc && rc != -ENODEV)
3797 goto err_out;
3801 * determine by signature whether we have ATA or ATAPI devices
3803 device[0].class = ata_dev_try_classify(&device[0], dev0, &err);
3804 if ((slave_possible) && (err != 0x81))
3805 device[1].class = ata_dev_try_classify(&device[1], dev1, &err);
3807 /* is double-select really necessary? */
3808 if (device[1].class != ATA_DEV_NONE)
3809 ap->ops->dev_select(ap, 1);
3810 if (device[0].class != ATA_DEV_NONE)
3811 ap->ops->dev_select(ap, 0);
3813 /* if no devices were detected, disable this port */
3814 if ((device[0].class == ATA_DEV_NONE) &&
3815 (device[1].class == ATA_DEV_NONE))
3816 goto err_out;
3818 if (ap->flags & (ATA_FLAG_SATA_RESET | ATA_FLAG_SRST)) {
3819 /* set up device control for ATA_FLAG_SATA_RESET */
3820 iowrite8(ap->ctl, ioaddr->ctl_addr);
3823 DPRINTK("EXIT\n");
3824 return;
3826 err_out:
3827 ata_port_printk(ap, KERN_ERR, "disabling port\n");
3828 ata_port_disable(ap);
3830 DPRINTK("EXIT\n");
3834 * sata_link_debounce - debounce SATA phy status
3835 * @link: ATA link to debounce SATA phy status for
3836 * @params: timing parameters { interval, duratinon, timeout } in msec
3837 * @deadline: deadline jiffies for the operation
3839 * Make sure SStatus of @link reaches stable state, determined by
3840 * holding the same value where DET is not 1 for @duration polled
3841 * every @interval, before @timeout. Timeout constraints the
3842 * beginning of the stable state. Because DET gets stuck at 1 on
3843 * some controllers after hot unplugging, this functions waits
3844 * until timeout then returns 0 if DET is stable at 1.
3846 * @timeout is further limited by @deadline. The sooner of the
3847 * two is used.
3849 * LOCKING:
3850 * Kernel thread context (may sleep)
3852 * RETURNS:
3853 * 0 on success, -errno on failure.
3855 int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3856 unsigned long deadline)
3858 unsigned long interval_msec = params[0];
3859 unsigned long duration = msecs_to_jiffies(params[1]);
3860 unsigned long last_jiffies, t;
3861 u32 last, cur;
3862 int rc;
3864 t = jiffies + msecs_to_jiffies(params[2]);
3865 if (time_before(t, deadline))
3866 deadline = t;
3868 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3869 return rc;
3870 cur &= 0xf;
3872 last = cur;
3873 last_jiffies = jiffies;
3875 while (1) {
3876 msleep(interval_msec);
3877 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3878 return rc;
3879 cur &= 0xf;
3881 /* DET stable? */
3882 if (cur == last) {
3883 if (cur == 1 && time_before(jiffies, deadline))
3884 continue;
3885 if (time_after(jiffies, last_jiffies + duration))
3886 return 0;
3887 continue;
3890 /* unstable, start over */
3891 last = cur;
3892 last_jiffies = jiffies;
3894 /* Check deadline. If debouncing failed, return
3895 * -EPIPE to tell upper layer to lower link speed.
3897 if (time_after(jiffies, deadline))
3898 return -EPIPE;
3903 * sata_link_resume - resume SATA link
3904 * @link: ATA link to resume SATA
3905 * @params: timing parameters { interval, duratinon, timeout } in msec
3906 * @deadline: deadline jiffies for the operation
3908 * Resume SATA phy @link and debounce it.
3910 * LOCKING:
3911 * Kernel thread context (may sleep)
3913 * RETURNS:
3914 * 0 on success, -errno on failure.
3916 int sata_link_resume(struct ata_link *link, const unsigned long *params,
3917 unsigned long deadline)
3919 u32 scontrol;
3920 int rc;
3922 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3923 return rc;
3925 scontrol = (scontrol & 0x0f0) | 0x300;
3927 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3928 return rc;
3930 /* Some PHYs react badly if SStatus is pounded immediately
3931 * after resuming. Delay 200ms before debouncing.
3933 msleep(200);
3935 return sata_link_debounce(link, params, deadline);
3939 * ata_std_prereset - prepare for reset
3940 * @link: ATA link to be reset
3941 * @deadline: deadline jiffies for the operation
3943 * @link is about to be reset. Initialize it. Failure from
3944 * prereset makes libata abort whole reset sequence and give up
3945 * that port, so prereset should be best-effort. It does its
3946 * best to prepare for reset sequence but if things go wrong, it
3947 * should just whine, not fail.
3949 * LOCKING:
3950 * Kernel thread context (may sleep)
3952 * RETURNS:
3953 * 0 on success, -errno otherwise.
3955 int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3957 struct ata_port *ap = link->ap;
3958 struct ata_eh_context *ehc = &link->eh_context;
3959 const unsigned long *timing = sata_ehc_deb_timing(ehc);
3960 int rc;
3962 /* handle link resume */
3963 if ((ehc->i.flags & ATA_EHI_RESUME_LINK) &&
3964 (link->flags & ATA_LFLAG_HRST_TO_RESUME))
3965 ehc->i.action |= ATA_EH_HARDRESET;
3967 /* Some PMPs don't work with only SRST, force hardreset if PMP
3968 * is supported.
3970 if (ap->flags & ATA_FLAG_PMP)
3971 ehc->i.action |= ATA_EH_HARDRESET;
3973 /* if we're about to do hardreset, nothing more to do */
3974 if (ehc->i.action & ATA_EH_HARDRESET)
3975 return 0;
3977 /* if SATA, resume link */
3978 if (ap->flags & ATA_FLAG_SATA) {
3979 rc = sata_link_resume(link, timing, deadline);
3980 /* whine about phy resume failure but proceed */
3981 if (rc && rc != -EOPNOTSUPP)
3982 ata_link_printk(link, KERN_WARNING, "failed to resume "
3983 "link for reset (errno=%d)\n", rc);
3986 /* Wait for !BSY if the controller can wait for the first D2H
3987 * Reg FIS and we don't know that no device is attached.
3989 if (!(link->flags & ATA_LFLAG_SKIP_D2H_BSY) && !ata_link_offline(link)) {
3990 rc = ata_wait_ready(ap, deadline);
3991 if (rc && rc != -ENODEV) {
3992 ata_link_printk(link, KERN_WARNING, "device not ready "
3993 "(errno=%d), forcing hardreset\n", rc);
3994 ehc->i.action |= ATA_EH_HARDRESET;
3998 return 0;
4002 * ata_std_softreset - reset host port via ATA SRST
4003 * @link: ATA link to reset
4004 * @classes: resulting classes of attached devices
4005 * @deadline: deadline jiffies for the operation
4007 * Reset host port using ATA SRST.
4009 * LOCKING:
4010 * Kernel thread context (may sleep)
4012 * RETURNS:
4013 * 0 on success, -errno otherwise.
4015 int ata_std_softreset(struct ata_link *link, unsigned int *classes,
4016 unsigned long deadline)
4018 struct ata_port *ap = link->ap;
4019 unsigned int slave_possible = ap->flags & ATA_FLAG_SLAVE_POSS;
4020 unsigned int devmask = 0;
4021 int rc;
4022 u8 err;
4024 DPRINTK("ENTER\n");
4026 if (ata_link_offline(link)) {
4027 classes[0] = ATA_DEV_NONE;
4028 goto out;
4031 /* determine if device 0/1 are present */
4032 if (ata_devchk(ap, 0))
4033 devmask |= (1 << 0);
4034 if (slave_possible && ata_devchk(ap, 1))
4035 devmask |= (1 << 1);
4037 /* select device 0 again */
4038 ap->ops->dev_select(ap, 0);
4040 /* issue bus reset */
4041 DPRINTK("about to softreset, devmask=%x\n", devmask);
4042 rc = ata_bus_softreset(ap, devmask, deadline);
4043 /* if link is occupied, -ENODEV too is an error */
4044 if (rc && (rc != -ENODEV || sata_scr_valid(link))) {
4045 ata_link_printk(link, KERN_ERR, "SRST failed (errno=%d)\n", rc);
4046 return rc;
4049 /* determine by signature whether we have ATA or ATAPI devices */
4050 classes[0] = ata_dev_try_classify(&link->device[0],
4051 devmask & (1 << 0), &err);
4052 if (slave_possible && err != 0x81)
4053 classes[1] = ata_dev_try_classify(&link->device[1],
4054 devmask & (1 << 1), &err);
4056 out:
4057 DPRINTK("EXIT, classes[0]=%u [1]=%u\n", classes[0], classes[1]);
4058 return 0;
4062 * sata_link_hardreset - reset link via SATA phy reset
4063 * @link: link to reset
4064 * @timing: timing parameters { interval, duratinon, timeout } in msec
4065 * @deadline: deadline jiffies for the operation
4067 * SATA phy-reset @link using DET bits of SControl register.
4069 * LOCKING:
4070 * Kernel thread context (may sleep)
4072 * RETURNS:
4073 * 0 on success, -errno otherwise.
4075 int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
4076 unsigned long deadline)
4078 u32 scontrol;
4079 int rc;
4081 DPRINTK("ENTER\n");
4083 if (sata_set_spd_needed(link)) {
4084 /* SATA spec says nothing about how to reconfigure
4085 * spd. To be on the safe side, turn off phy during
4086 * reconfiguration. This works for at least ICH7 AHCI
4087 * and Sil3124.
4089 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
4090 goto out;
4092 scontrol = (scontrol & 0x0f0) | 0x304;
4094 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
4095 goto out;
4097 sata_set_spd(link);
4100 /* issue phy wake/reset */
4101 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
4102 goto out;
4104 scontrol = (scontrol & 0x0f0) | 0x301;
4106 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
4107 goto out;
4109 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
4110 * 10.4.2 says at least 1 ms.
4112 msleep(1);
4114 /* bring link back */
4115 rc = sata_link_resume(link, timing, deadline);
4116 out:
4117 DPRINTK("EXIT, rc=%d\n", rc);
4118 return rc;
4122 * sata_std_hardreset - reset host port via SATA phy reset
4123 * @link: link to reset
4124 * @class: resulting class of attached device
4125 * @deadline: deadline jiffies for the operation
4127 * SATA phy-reset host port using DET bits of SControl register,
4128 * wait for !BSY and classify the attached device.
4130 * LOCKING:
4131 * Kernel thread context (may sleep)
4133 * RETURNS:
4134 * 0 on success, -errno otherwise.
4136 int sata_std_hardreset(struct ata_link *link, unsigned int *class,
4137 unsigned long deadline)
4139 struct ata_port *ap = link->ap;
4140 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
4141 int rc;
4143 DPRINTK("ENTER\n");
4145 /* do hardreset */
4146 rc = sata_link_hardreset(link, timing, deadline);
4147 if (rc) {
4148 ata_link_printk(link, KERN_ERR,
4149 "COMRESET failed (errno=%d)\n", rc);
4150 return rc;
4153 /* TODO: phy layer with polling, timeouts, etc. */
4154 if (ata_link_offline(link)) {
4155 *class = ATA_DEV_NONE;
4156 DPRINTK("EXIT, link offline\n");
4157 return 0;
4160 /* wait a while before checking status */
4161 ata_wait_after_reset(ap, deadline);
4163 /* If PMP is supported, we have to do follow-up SRST. Note
4164 * that some PMPs don't send D2H Reg FIS after hardreset at
4165 * all if the first port is empty. Wait for it just for a
4166 * second and request follow-up SRST.
4168 if (ap->flags & ATA_FLAG_PMP) {
4169 ata_wait_ready(ap, jiffies + HZ);
4170 return -EAGAIN;
4173 rc = ata_wait_ready(ap, deadline);
4174 /* link occupied, -ENODEV too is an error */
4175 if (rc) {
4176 ata_link_printk(link, KERN_ERR,
4177 "COMRESET failed (errno=%d)\n", rc);
4178 return rc;
4181 ap->ops->dev_select(ap, 0); /* probably unnecessary */
4183 *class = ata_dev_try_classify(link->device, 1, NULL);
4185 DPRINTK("EXIT, class=%u\n", *class);
4186 return 0;
4190 * ata_std_postreset - standard postreset callback
4191 * @link: the target ata_link
4192 * @classes: classes of attached devices
4194 * This function is invoked after a successful reset. Note that
4195 * the device might have been reset more than once using
4196 * different reset methods before postreset is invoked.
4198 * LOCKING:
4199 * Kernel thread context (may sleep)
4201 void ata_std_postreset(struct ata_link *link, unsigned int *classes)
4203 struct ata_port *ap = link->ap;
4204 u32 serror;
4206 DPRINTK("ENTER\n");
4208 /* print link status */
4209 sata_print_link_status(link);
4211 /* clear SError */
4212 if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
4213 sata_scr_write(link, SCR_ERROR, serror);
4214 link->eh_info.serror = 0;
4216 /* is double-select really necessary? */
4217 if (classes[0] != ATA_DEV_NONE)
4218 ap->ops->dev_select(ap, 1);
4219 if (classes[1] != ATA_DEV_NONE)
4220 ap->ops->dev_select(ap, 0);
4222 /* bail out if no device is present */
4223 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
4224 DPRINTK("EXIT, no device\n");
4225 return;
4228 /* set up device control */
4229 if (ap->ioaddr.ctl_addr)
4230 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
4232 DPRINTK("EXIT\n");
4236 * ata_dev_same_device - Determine whether new ID matches configured device
4237 * @dev: device to compare against
4238 * @new_class: class of the new device
4239 * @new_id: IDENTIFY page of the new device
4241 * Compare @new_class and @new_id against @dev and determine
4242 * whether @dev is the device indicated by @new_class and
4243 * @new_id.
4245 * LOCKING:
4246 * None.
4248 * RETURNS:
4249 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
4251 static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
4252 const u16 *new_id)
4254 const u16 *old_id = dev->id;
4255 unsigned char model[2][ATA_ID_PROD_LEN + 1];
4256 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
4258 if (dev->class != new_class) {
4259 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
4260 dev->class, new_class);
4261 return 0;
4264 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
4265 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
4266 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
4267 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
4269 if (strcmp(model[0], model[1])) {
4270 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
4271 "'%s' != '%s'\n", model[0], model[1]);
4272 return 0;
4275 if (strcmp(serial[0], serial[1])) {
4276 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
4277 "'%s' != '%s'\n", serial[0], serial[1]);
4278 return 0;
4281 return 1;
4285 * ata_dev_reread_id - Re-read IDENTIFY data
4286 * @dev: target ATA device
4287 * @readid_flags: read ID flags
4289 * Re-read IDENTIFY page and make sure @dev is still attached to
4290 * the port.
4292 * LOCKING:
4293 * Kernel thread context (may sleep)
4295 * RETURNS:
4296 * 0 on success, negative errno otherwise
4298 int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
4300 unsigned int class = dev->class;
4301 u16 *id = (void *)dev->link->ap->sector_buf;
4302 int rc;
4304 /* read ID data */
4305 rc = ata_dev_read_id(dev, &class, readid_flags, id);
4306 if (rc)
4307 return rc;
4309 /* is the device still there? */
4310 if (!ata_dev_same_device(dev, class, id))
4311 return -ENODEV;
4313 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
4314 return 0;
4318 * ata_dev_revalidate - Revalidate ATA device
4319 * @dev: device to revalidate
4320 * @new_class: new class code
4321 * @readid_flags: read ID flags
4323 * Re-read IDENTIFY page, make sure @dev is still attached to the
4324 * port and reconfigure it according to the new IDENTIFY page.
4326 * LOCKING:
4327 * Kernel thread context (may sleep)
4329 * RETURNS:
4330 * 0 on success, negative errno otherwise
4332 int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4333 unsigned int readid_flags)
4335 u64 n_sectors = dev->n_sectors;
4336 int rc;
4338 if (!ata_dev_enabled(dev))
4339 return -ENODEV;
4341 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4342 if (ata_class_enabled(new_class) &&
4343 new_class != ATA_DEV_ATA && new_class != ATA_DEV_ATAPI) {
4344 ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
4345 dev->class, new_class);
4346 rc = -ENODEV;
4347 goto fail;
4350 /* re-read ID */
4351 rc = ata_dev_reread_id(dev, readid_flags);
4352 if (rc)
4353 goto fail;
4355 /* configure device according to the new ID */
4356 rc = ata_dev_configure(dev);
4357 if (rc)
4358 goto fail;
4360 /* verify n_sectors hasn't changed */
4361 if (dev->class == ATA_DEV_ATA && n_sectors &&
4362 dev->n_sectors != n_sectors) {
4363 ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
4364 "%llu != %llu\n",
4365 (unsigned long long)n_sectors,
4366 (unsigned long long)dev->n_sectors);
4368 /* restore original n_sectors */
4369 dev->n_sectors = n_sectors;
4371 rc = -ENODEV;
4372 goto fail;
4375 return 0;
4377 fail:
4378 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
4379 return rc;
4382 struct ata_blacklist_entry {
4383 const char *model_num;
4384 const char *model_rev;
4385 unsigned long horkage;
4388 static const struct ata_blacklist_entry ata_device_blacklist [] = {
4389 /* Devices with DMA related problems under Linux */
4390 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
4391 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
4392 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
4393 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
4394 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
4395 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
4396 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
4397 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
4398 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
4399 { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
4400 { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
4401 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
4402 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
4403 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
4404 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
4405 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
4406 { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
4407 { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
4408 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
4409 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
4410 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
4411 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
4412 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
4413 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
4414 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
4415 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
4416 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
4417 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
4418 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
4419 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
4420 /* Odd clown on sil3726/4726 PMPs */
4421 { "Config Disk", NULL, ATA_HORKAGE_NODMA |
4422 ATA_HORKAGE_SKIP_PM },
4424 /* Weird ATAPI devices */
4425 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
4427 /* Devices we expect to fail diagnostics */
4429 /* Devices where NCQ should be avoided */
4430 /* NCQ is slow */
4431 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
4432 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
4433 /* http://thread.gmane.org/gmane.linux.ide/14907 */
4434 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
4435 /* NCQ is broken */
4436 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
4437 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
4438 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
4439 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
4441 /* Blacklist entries taken from Silicon Image 3124/3132
4442 Windows driver .inf file - also several Linux problem reports */
4443 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
4444 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
4445 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
4447 /* devices which puke on READ_NATIVE_MAX */
4448 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
4449 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
4450 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
4451 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
4453 /* Devices which report 1 sector over size HPA */
4454 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
4455 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
4456 <<<<<<< HEAD:drivers/ata/libata-core.c
4457 =======
4458 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
4459 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
4461 /* Devices which get the IVB wrong */
4462 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4463 { "TSSTcorp CDDVDW SH-S202J", "SB00", ATA_HORKAGE_IVB, },
4464 { "TSSTcorp CDDVDW SH-S202J", "SB01", ATA_HORKAGE_IVB, },
4465 { "TSSTcorp CDDVDW SH-S202N", "SB00", ATA_HORKAGE_IVB, },
4466 { "TSSTcorp CDDVDW SH-S202N", "SB01", ATA_HORKAGE_IVB, },
4468 /* End Marker */
4472 static int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
4474 const char *p;
4475 int len;
4478 * check for trailing wildcard: *\0
4480 p = strchr(patt, wildchar);
4481 if (p && ((*(p + 1)) == 0))
4482 len = p - patt;
4483 else {
4484 len = strlen(name);
4485 if (!len) {
4486 if (!*patt)
4487 return 0;
4488 return -1;
4492 return strncmp(patt, name, len);
4495 static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4497 unsigned char model_num[ATA_ID_PROD_LEN + 1];
4498 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
4499 const struct ata_blacklist_entry *ad = ata_device_blacklist;
4501 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
4502 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4504 while (ad->model_num) {
4505 if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
4506 if (ad->model_rev == NULL)
4507 return ad->horkage;
4508 if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
4509 return ad->horkage;
4511 ad++;
4513 return 0;
4516 static int ata_dma_blacklisted(const struct ata_device *dev)
4518 /* We don't support polling DMA.
4519 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
4520 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
4522 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
4523 (dev->flags & ATA_DFLAG_CDB_INTR))
4524 return 1;
4525 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
4529 * ata_is_40wire - check drive side detection
4530 * @dev: device
4532 * Perform drive side detection decoding, allowing for device vendors
4533 * who can't follow the documentation.
4536 static int ata_is_40wire(struct ata_device *dev)
4538 if (dev->horkage & ATA_HORKAGE_IVB)
4539 return ata_drive_40wire_relaxed(dev->id);
4540 return ata_drive_40wire(dev->id);
4544 * ata_dev_xfermask - Compute supported xfermask of the given device
4545 * @dev: Device to compute xfermask for
4547 * Compute supported xfermask of @dev and store it in
4548 * dev->*_mask. This function is responsible for applying all
4549 * known limits including host controller limits, device
4550 * blacklist, etc...
4552 * LOCKING:
4553 * None.
4555 static void ata_dev_xfermask(struct ata_device *dev)
4557 struct ata_link *link = dev->link;
4558 struct ata_port *ap = link->ap;
4559 struct ata_host *host = ap->host;
4560 unsigned long xfer_mask;
4562 /* controller modes available */
4563 xfer_mask = ata_pack_xfermask(ap->pio_mask,
4564 ap->mwdma_mask, ap->udma_mask);
4566 /* drive modes available */
4567 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4568 dev->mwdma_mask, dev->udma_mask);
4569 xfer_mask &= ata_id_xfermask(dev->id);
4572 * CFA Advanced TrueIDE timings are not allowed on a shared
4573 * cable
4575 if (ata_dev_pair(dev)) {
4576 /* No PIO5 or PIO6 */
4577 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4578 /* No MWDMA3 or MWDMA 4 */
4579 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4582 if (ata_dma_blacklisted(dev)) {
4583 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4584 ata_dev_printk(dev, KERN_WARNING,
4585 "device is on DMA blacklist, disabling DMA\n");
4588 if ((host->flags & ATA_HOST_SIMPLEX) &&
4589 host->simplex_claimed && host->simplex_claimed != ap) {
4590 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4591 ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
4592 "other device, disabling DMA\n");
4595 if (ap->flags & ATA_FLAG_NO_IORDY)
4596 xfer_mask &= ata_pio_mask_no_iordy(dev);
4598 if (ap->ops->mode_filter)
4599 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4601 /* Apply cable rule here. Don't apply it early because when
4602 * we handle hot plug the cable type can itself change.
4603 * Check this last so that we know if the transfer rate was
4604 * solely limited by the cable.
4605 * Unknown or 80 wire cables reported host side are checked
4606 * drive side as well. Cases where we know a 40wire cable
4607 * is used safely for 80 are not checked here.
4609 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
4610 /* UDMA/44 or higher would be available */
4611 if ((ap->cbl == ATA_CBL_PATA40) ||
4612 (ata_is_40wire(dev) &&
4613 (ap->cbl == ATA_CBL_PATA_UNK ||
4614 ap->cbl == ATA_CBL_PATA80))) {
4615 ata_dev_printk(dev, KERN_WARNING,
4616 "limited to UDMA/33 due to 40-wire cable\n");
4617 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
4620 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4621 &dev->mwdma_mask, &dev->udma_mask);
4625 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4626 * @dev: Device to which command will be sent
4628 * Issue SET FEATURES - XFER MODE command to device @dev
4629 * on port @ap.
4631 * LOCKING:
4632 * PCI/etc. bus probe sem.
4634 * RETURNS:
4635 * 0 on success, AC_ERR_* mask otherwise.
4638 static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4640 struct ata_taskfile tf;
4641 unsigned int err_mask;
4643 /* set up set-features taskfile */
4644 DPRINTK("set features - xfer mode\n");
4646 /* Some controllers and ATAPI devices show flaky interrupt
4647 * behavior after setting xfer mode. Use polling instead.
4649 ata_tf_init(dev, &tf);
4650 tf.command = ATA_CMD_SET_FEATURES;
4651 tf.feature = SETFEATURES_XFER;
4652 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4653 tf.protocol = ATA_PROT_NODATA;
4654 /* If we are using IORDY we must send the mode setting command */
4655 if (ata_pio_need_iordy(dev))
4656 tf.nsect = dev->xfer_mode;
4657 /* If the device has IORDY and the controller does not - turn it off */
4658 else if (ata_id_has_iordy(dev->id))
4659 tf.nsect = 0x01;
4660 else /* In the ancient relic department - skip all of this */
4661 return 0;
4663 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4665 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4666 return err_mask;
4669 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
4670 * @dev: Device to which command will be sent
4671 * @enable: Whether to enable or disable the feature
4672 * @feature: The sector count represents the feature to set
4674 * Issue SET FEATURES - SATA FEATURES command to device @dev
4675 * on port @ap with sector count
4677 * LOCKING:
4678 * PCI/etc. bus probe sem.
4680 * RETURNS:
4681 * 0 on success, AC_ERR_* mask otherwise.
4683 static unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable,
4684 u8 feature)
4686 struct ata_taskfile tf;
4687 unsigned int err_mask;
4689 /* set up set-features taskfile */
4690 DPRINTK("set features - SATA features\n");
4692 ata_tf_init(dev, &tf);
4693 tf.command = ATA_CMD_SET_FEATURES;
4694 tf.feature = enable;
4695 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4696 tf.protocol = ATA_PROT_NODATA;
4697 tf.nsect = feature;
4699 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4701 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4702 return err_mask;
4706 * ata_dev_init_params - Issue INIT DEV PARAMS command
4707 * @dev: Device to which command will be sent
4708 * @heads: Number of heads (taskfile parameter)
4709 * @sectors: Number of sectors (taskfile parameter)
4711 * LOCKING:
4712 * Kernel thread context (may sleep)
4714 * RETURNS:
4715 * 0 on success, AC_ERR_* mask otherwise.
4717 static unsigned int ata_dev_init_params(struct ata_device *dev,
4718 u16 heads, u16 sectors)
4720 struct ata_taskfile tf;
4721 unsigned int err_mask;
4723 /* Number of sectors per track 1-255. Number of heads 1-16 */
4724 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4725 return AC_ERR_INVALID;
4727 /* set up init dev params taskfile */
4728 DPRINTK("init dev params \n");
4730 ata_tf_init(dev, &tf);
4731 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4732 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4733 tf.protocol = ATA_PROT_NODATA;
4734 tf.nsect = sectors;
4735 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4737 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4738 /* A clean abort indicates an original or just out of spec drive
4739 and we should continue as we issue the setup based on the
4740 drive reported working geometry */
4741 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4742 err_mask = 0;
4744 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4745 return err_mask;
4749 * ata_sg_clean - Unmap DMA memory associated with command
4750 * @qc: Command containing DMA memory to be released
4752 * Unmap all mapped DMA memory associated with this command.
4754 * LOCKING:
4755 * spin_lock_irqsave(host lock)
4757 void ata_sg_clean(struct ata_queued_cmd *qc)
4759 struct ata_port *ap = qc->ap;
4760 struct scatterlist *sg = qc->sg;
4761 int dir = qc->dma_dir;
4762 <<<<<<< HEAD:drivers/ata/libata-core.c
4763 void *pad_buf = NULL;
4764 =======
4765 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
4767 WARN_ON(sg == NULL);
4769 <<<<<<< HEAD:drivers/ata/libata-core.c
4770 VPRINTK("unmapping %u sg elements\n", qc->mapped_n_elem);
4771 =======
4772 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4773 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
4775 <<<<<<< HEAD:drivers/ata/libata-core.c
4776 /* if we padded the buffer out to 32-bit bound, and data
4777 * xfer direction is from-device, we must copy from the
4778 * pad buffer back into the supplied buffer
4780 if (qc->pad_len && !(qc->tf.flags & ATA_TFLAG_WRITE))
4781 pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
4783 if (qc->mapped_n_elem)
4784 dma_unmap_sg(ap->dev, sg, qc->mapped_n_elem, dir);
4785 /* restore last sg */
4786 if (qc->last_sg)
4787 *qc->last_sg = qc->saved_last_sg;
4788 if (pad_buf) {
4789 struct scatterlist *psg = &qc->extra_sg[1];
4790 void *addr = kmap_atomic(sg_page(psg), KM_IRQ0);
4791 memcpy(addr + psg->offset, pad_buf, qc->pad_len);
4792 kunmap_atomic(addr, KM_IRQ0);
4794 =======
4795 if (qc->n_elem)
4796 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
4797 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
4799 qc->flags &= ~ATA_QCFLAG_DMAMAP;
4800 qc->sg = NULL;
4804 * ata_fill_sg - Fill PCI IDE PRD table
4805 * @qc: Metadata associated with taskfile to be transferred
4807 * Fill PCI IDE PRD (scatter-gather) table with segments
4808 * associated with the current disk command.
4810 * LOCKING:
4811 * spin_lock_irqsave(host lock)
4814 static void ata_fill_sg(struct ata_queued_cmd *qc)
4816 struct ata_port *ap = qc->ap;
4817 struct scatterlist *sg;
4818 unsigned int si, pi;
4820 pi = 0;
4821 for_each_sg(qc->sg, sg, qc->n_elem, si) {
4822 u32 addr, offset;
4823 u32 sg_len, len;
4825 /* determine if physical DMA addr spans 64K boundary.
4826 * Note h/w doesn't support 64-bit, so we unconditionally
4827 * truncate dma_addr_t to u32.
4829 addr = (u32) sg_dma_address(sg);
4830 sg_len = sg_dma_len(sg);
4832 while (sg_len) {
4833 offset = addr & 0xffff;
4834 len = sg_len;
4835 if ((offset + sg_len) > 0x10000)
4836 len = 0x10000 - offset;
4838 ap->prd[pi].addr = cpu_to_le32(addr);
4839 ap->prd[pi].flags_len = cpu_to_le32(len & 0xffff);
4840 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
4842 pi++;
4843 sg_len -= len;
4844 addr += len;
4848 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4852 * ata_fill_sg_dumb - Fill PCI IDE PRD table
4853 * @qc: Metadata associated with taskfile to be transferred
4855 * Fill PCI IDE PRD (scatter-gather) table with segments
4856 * associated with the current disk command. Perform the fill
4857 * so that we avoid writing any length 64K records for
4858 * controllers that don't follow the spec.
4860 * LOCKING:
4861 * spin_lock_irqsave(host lock)
4864 static void ata_fill_sg_dumb(struct ata_queued_cmd *qc)
4866 struct ata_port *ap = qc->ap;
4867 struct scatterlist *sg;
4868 unsigned int si, pi;
4870 pi = 0;
4871 for_each_sg(qc->sg, sg, qc->n_elem, si) {
4872 u32 addr, offset;
4873 u32 sg_len, len, blen;
4875 /* determine if physical DMA addr spans 64K boundary.
4876 * Note h/w doesn't support 64-bit, so we unconditionally
4877 * truncate dma_addr_t to u32.
4879 addr = (u32) sg_dma_address(sg);
4880 sg_len = sg_dma_len(sg);
4882 while (sg_len) {
4883 offset = addr & 0xffff;
4884 len = sg_len;
4885 if ((offset + sg_len) > 0x10000)
4886 len = 0x10000 - offset;
4888 blen = len & 0xffff;
4889 ap->prd[pi].addr = cpu_to_le32(addr);
4890 if (blen == 0) {
4891 /* Some PATA chipsets like the CS5530 can't
4892 cope with 0x0000 meaning 64K as the spec says */
4893 ap->prd[pi].flags_len = cpu_to_le32(0x8000);
4894 blen = 0x8000;
4895 ap->prd[++pi].addr = cpu_to_le32(addr + 0x8000);
4897 ap->prd[pi].flags_len = cpu_to_le32(blen);
4898 VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", pi, addr, len);
4900 pi++;
4901 sg_len -= len;
4902 addr += len;
4906 ap->prd[pi - 1].flags_len |= cpu_to_le32(ATA_PRD_EOT);
4910 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
4911 * @qc: Metadata associated with taskfile to check
4913 * Allow low-level driver to filter ATA PACKET commands, returning
4914 * a status indicating whether or not it is OK to use DMA for the
4915 * supplied PACKET command.
4917 * LOCKING:
4918 * spin_lock_irqsave(host lock)
4920 * RETURNS: 0 when ATAPI DMA can be used
4921 * nonzero otherwise
4923 int ata_check_atapi_dma(struct ata_queued_cmd *qc)
4925 struct ata_port *ap = qc->ap;
4927 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4928 * few ATAPI devices choke on such DMA requests.
4930 if (unlikely(qc->nbytes & 15))
4931 return 1;
4933 if (ap->ops->check_atapi_dma)
4934 return ap->ops->check_atapi_dma(qc);
4936 return 0;
4940 <<<<<<< HEAD:drivers/ata/libata-core.c
4941 * atapi_qc_may_overflow - Check whether data transfer may overflow
4942 * @qc: ATA command in question
4944 * ATAPI commands which transfer variable length data to host
4945 * might overflow due to application error or hardare bug. This
4946 * function checks whether overflow should be drained and ignored
4947 * for @qc.
4949 * LOCKING:
4950 * None.
4952 * RETURNS:
4953 * 1 if @qc may overflow; otherwise, 0.
4955 static int atapi_qc_may_overflow(struct ata_queued_cmd *qc)
4957 if (qc->tf.protocol != ATAPI_PROT_PIO &&
4958 qc->tf.protocol != ATAPI_PROT_DMA)
4959 return 0;
4961 if (qc->tf.flags & ATA_TFLAG_WRITE)
4962 return 0;
4964 switch (qc->cdb[0]) {
4965 case READ_10:
4966 case READ_12:
4967 case WRITE_10:
4968 case WRITE_12:
4969 case GPCMD_READ_CD:
4970 case GPCMD_READ_CD_MSF:
4971 return 0;
4974 return 1;
4978 =======
4979 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
4980 * ata_std_qc_defer - Check whether a qc needs to be deferred
4981 * @qc: ATA command in question
4983 * Non-NCQ commands cannot run with any other command, NCQ or
4984 * not. As upper layer only knows the queue depth, we are
4985 * responsible for maintaining exclusion. This function checks
4986 * whether a new command @qc can be issued.
4988 * LOCKING:
4989 * spin_lock_irqsave(host lock)
4991 * RETURNS:
4992 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4994 int ata_std_qc_defer(struct ata_queued_cmd *qc)
4996 struct ata_link *link = qc->dev->link;
4998 if (qc->tf.protocol == ATA_PROT_NCQ) {
4999 if (!ata_tag_valid(link->active_tag))
5000 return 0;
5001 } else {
5002 if (!ata_tag_valid(link->active_tag) && !link->sactive)
5003 return 0;
5006 return ATA_DEFER_LINK;
5010 * ata_qc_prep - Prepare taskfile for submission
5011 * @qc: Metadata associated with taskfile to be prepared
5013 * Prepare ATA taskfile for submission.
5015 * LOCKING:
5016 * spin_lock_irqsave(host lock)
5018 void ata_qc_prep(struct ata_queued_cmd *qc)
5020 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
5021 return;
5023 ata_fill_sg(qc);
5027 * ata_dumb_qc_prep - Prepare taskfile for submission
5028 * @qc: Metadata associated with taskfile to be prepared
5030 * Prepare ATA taskfile for submission.
5032 * LOCKING:
5033 * spin_lock_irqsave(host lock)
5035 void ata_dumb_qc_prep(struct ata_queued_cmd *qc)
5037 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
5038 return;
5040 ata_fill_sg_dumb(qc);
5043 void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
5046 * ata_sg_init - Associate command with scatter-gather table.
5047 * @qc: Command to be associated
5048 * @sg: Scatter-gather table.
5049 * @n_elem: Number of elements in s/g table.
5051 * Initialize the data-related elements of queued_cmd @qc
5052 * to point to a scatter-gather table @sg, containing @n_elem
5053 * elements.
5055 * LOCKING:
5056 * spin_lock_irqsave(host lock)
5058 void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
5059 unsigned int n_elem)
5061 qc->sg = sg;
5062 qc->n_elem = n_elem;
5063 qc->cursg = qc->sg;
5066 <<<<<<< HEAD:drivers/ata/libata-core.c
5067 static unsigned int ata_sg_setup_extra(struct ata_queued_cmd *qc,
5068 unsigned int *n_elem_extra,
5069 unsigned int *nbytes_extra)
5071 struct ata_port *ap = qc->ap;
5072 unsigned int n_elem = qc->n_elem;
5073 struct scatterlist *lsg, *copy_lsg = NULL, *tsg = NULL, *esg = NULL;
5075 *n_elem_extra = 0;
5076 *nbytes_extra = 0;
5078 /* needs padding? */
5079 qc->pad_len = qc->nbytes & 3;
5081 if (likely(!qc->pad_len))
5082 return n_elem;
5084 /* locate last sg and save it */
5085 lsg = sg_last(qc->sg, n_elem);
5086 qc->last_sg = lsg;
5087 qc->saved_last_sg = *lsg;
5089 sg_init_table(qc->extra_sg, ARRAY_SIZE(qc->extra_sg));
5091 if (qc->pad_len) {
5092 struct scatterlist *psg = &qc->extra_sg[1];
5093 void *pad_buf = ap->pad + (qc->tag * ATA_DMA_PAD_SZ);
5094 unsigned int offset;
5096 WARN_ON(qc->dev->class != ATA_DEV_ATAPI);
5098 memset(pad_buf, 0, ATA_DMA_PAD_SZ);
5100 /* psg->page/offset are used to copy to-be-written
5101 * data in this function or read data in ata_sg_clean.
5103 offset = lsg->offset + lsg->length - qc->pad_len;
5104 sg_set_page(psg, nth_page(sg_page(lsg), offset >> PAGE_SHIFT),
5105 qc->pad_len, offset_in_page(offset));
5107 if (qc->tf.flags & ATA_TFLAG_WRITE) {
5108 void *addr = kmap_atomic(sg_page(psg), KM_IRQ0);
5109 memcpy(pad_buf, addr + psg->offset, qc->pad_len);
5110 kunmap_atomic(addr, KM_IRQ0);
5113 sg_dma_address(psg) = ap->pad_dma + (qc->tag * ATA_DMA_PAD_SZ);
5114 sg_dma_len(psg) = ATA_DMA_PAD_SZ;
5116 /* Trim the last sg entry and chain the original and
5117 * padding sg lists.
5119 * Because chaining consumes one sg entry, one extra
5120 * sg entry is allocated and the last sg entry is
5121 * copied to it if the length isn't zero after padded
5122 * amount is removed.
5124 * If the last sg entry is completely replaced by
5125 * padding sg entry, the first sg entry is skipped
5126 * while chaining.
5128 lsg->length -= qc->pad_len;
5129 if (lsg->length) {
5130 copy_lsg = &qc->extra_sg[0];
5131 tsg = &qc->extra_sg[0];
5132 } else {
5133 n_elem--;
5134 tsg = &qc->extra_sg[1];
5137 esg = &qc->extra_sg[1];
5139 (*n_elem_extra)++;
5140 (*nbytes_extra) += 4 - qc->pad_len;
5143 if (copy_lsg)
5144 sg_set_page(copy_lsg, sg_page(lsg), lsg->length, lsg->offset);
5146 sg_chain(lsg, 1, tsg);
5147 sg_mark_end(esg);
5149 /* sglist can't start with chaining sg entry, fast forward */
5150 if (qc->sg == lsg) {
5151 qc->sg = tsg;
5152 qc->cursg = tsg;
5155 return n_elem;
5158 =======
5159 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5161 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
5162 * @qc: Command with scatter-gather table to be mapped.
5164 * DMA-map the scatter-gather table associated with queued_cmd @qc.
5166 * LOCKING:
5167 * spin_lock_irqsave(host lock)
5169 * RETURNS:
5170 * Zero on success, negative on error.
5173 static int ata_sg_setup(struct ata_queued_cmd *qc)
5175 struct ata_port *ap = qc->ap;
5176 <<<<<<< HEAD:drivers/ata/libata-core.c
5177 unsigned int n_elem, n_elem_extra, nbytes_extra;
5178 =======
5179 unsigned int n_elem;
5180 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5182 VPRINTK("ENTER, ata%u\n", ap->print_id);
5184 <<<<<<< HEAD:drivers/ata/libata-core.c
5185 n_elem = ata_sg_setup_extra(qc, &n_elem_extra, &nbytes_extra);
5186 =======
5187 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
5188 if (n_elem < 1)
5189 return -1;
5190 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5192 <<<<<<< HEAD:drivers/ata/libata-core.c
5193 if (n_elem) {
5194 n_elem = dma_map_sg(ap->dev, qc->sg, n_elem, qc->dma_dir);
5195 if (n_elem < 1) {
5196 /* restore last sg */
5197 if (qc->last_sg)
5198 *qc->last_sg = qc->saved_last_sg;
5199 return -1;
5201 DPRINTK("%d sg elements mapped\n", n_elem);
5203 =======
5204 DPRINTK("%d sg elements mapped\n", n_elem);
5205 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5207 <<<<<<< HEAD:drivers/ata/libata-core.c
5208 qc->n_elem = qc->mapped_n_elem = n_elem;
5209 qc->n_elem += n_elem_extra;
5210 qc->nbytes += nbytes_extra;
5211 =======
5212 qc->n_elem = n_elem;
5213 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5214 qc->flags |= ATA_QCFLAG_DMAMAP;
5216 return 0;
5220 * swap_buf_le16 - swap halves of 16-bit words in place
5221 * @buf: Buffer to swap
5222 * @buf_words: Number of 16-bit words in buffer.
5224 * Swap halves of 16-bit words if needed to convert from
5225 * little-endian byte order to native cpu byte order, or
5226 * vice-versa.
5228 * LOCKING:
5229 * Inherited from caller.
5231 void swap_buf_le16(u16 *buf, unsigned int buf_words)
5233 #ifdef __BIG_ENDIAN
5234 unsigned int i;
5236 for (i = 0; i < buf_words; i++)
5237 buf[i] = le16_to_cpu(buf[i]);
5238 #endif /* __BIG_ENDIAN */
5242 * ata_data_xfer - Transfer data by PIO
5243 * @dev: device to target
5244 * @buf: data buffer
5245 * @buflen: buffer length
5246 * @rw: read/write
5248 * Transfer data from/to the device data register by PIO.
5250 * LOCKING:
5251 * Inherited from caller.
5253 * RETURNS:
5254 * Bytes consumed.
5256 unsigned int ata_data_xfer(struct ata_device *dev, unsigned char *buf,
5257 unsigned int buflen, int rw)
5259 struct ata_port *ap = dev->link->ap;
5260 void __iomem *data_addr = ap->ioaddr.data_addr;
5261 unsigned int words = buflen >> 1;
5263 /* Transfer multiple of 2 bytes */
5264 if (rw == READ)
5265 ioread16_rep(data_addr, buf, words);
5266 else
5267 iowrite16_rep(data_addr, buf, words);
5269 /* Transfer trailing 1 byte, if any. */
5270 if (unlikely(buflen & 0x01)) {
5271 __le16 align_buf[1] = { 0 };
5272 unsigned char *trailing_buf = buf + buflen - 1;
5274 if (rw == READ) {
5275 align_buf[0] = cpu_to_le16(ioread16(data_addr));
5276 memcpy(trailing_buf, align_buf, 1);
5277 } else {
5278 memcpy(align_buf, trailing_buf, 1);
5279 iowrite16(le16_to_cpu(align_buf[0]), data_addr);
5281 words++;
5284 return words << 1;
5288 * ata_data_xfer_noirq - Transfer data by PIO
5289 * @dev: device to target
5290 * @buf: data buffer
5291 * @buflen: buffer length
5292 * @rw: read/write
5294 * Transfer data from/to the device data register by PIO. Do the
5295 * transfer with interrupts disabled.
5297 * LOCKING:
5298 * Inherited from caller.
5300 * RETURNS:
5301 * Bytes consumed.
5303 unsigned int ata_data_xfer_noirq(struct ata_device *dev, unsigned char *buf,
5304 unsigned int buflen, int rw)
5306 unsigned long flags;
5307 unsigned int consumed;
5309 local_irq_save(flags);
5310 consumed = ata_data_xfer(dev, buf, buflen, rw);
5311 local_irq_restore(flags);
5313 return consumed;
5318 * ata_pio_sector - Transfer a sector of data.
5319 * @qc: Command on going
5321 * Transfer qc->sect_size bytes of data from/to the ATA device.
5323 * LOCKING:
5324 * Inherited from caller.
5327 static void ata_pio_sector(struct ata_queued_cmd *qc)
5329 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
5330 struct ata_port *ap = qc->ap;
5331 struct page *page;
5332 unsigned int offset;
5333 unsigned char *buf;
5335 if (qc->curbytes == qc->nbytes - qc->sect_size)
5336 ap->hsm_task_state = HSM_ST_LAST;
5338 page = sg_page(qc->cursg);
5339 offset = qc->cursg->offset + qc->cursg_ofs;
5341 /* get the current page and offset */
5342 page = nth_page(page, (offset >> PAGE_SHIFT));
5343 offset %= PAGE_SIZE;
5345 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
5347 if (PageHighMem(page)) {
5348 unsigned long flags;
5350 /* FIXME: use a bounce buffer */
5351 local_irq_save(flags);
5352 buf = kmap_atomic(page, KM_IRQ0);
5354 /* do the actual data transfer */
5355 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
5357 kunmap_atomic(buf, KM_IRQ0);
5358 local_irq_restore(flags);
5359 } else {
5360 buf = page_address(page);
5361 ap->ops->data_xfer(qc->dev, buf + offset, qc->sect_size, do_write);
5364 qc->curbytes += qc->sect_size;
5365 qc->cursg_ofs += qc->sect_size;
5367 if (qc->cursg_ofs == qc->cursg->length) {
5368 qc->cursg = sg_next(qc->cursg);
5369 qc->cursg_ofs = 0;
5374 * ata_pio_sectors - Transfer one or many sectors.
5375 * @qc: Command on going
5377 * Transfer one or many sectors of data from/to the
5378 * ATA device for the DRQ request.
5380 * LOCKING:
5381 * Inherited from caller.
5384 static void ata_pio_sectors(struct ata_queued_cmd *qc)
5386 if (is_multi_taskfile(&qc->tf)) {
5387 /* READ/WRITE MULTIPLE */
5388 unsigned int nsect;
5390 WARN_ON(qc->dev->multi_count == 0);
5392 nsect = min((qc->nbytes - qc->curbytes) / qc->sect_size,
5393 qc->dev->multi_count);
5394 while (nsect--)
5395 ata_pio_sector(qc);
5396 } else
5397 ata_pio_sector(qc);
5399 ata_altstatus(qc->ap); /* flush */
5403 * atapi_send_cdb - Write CDB bytes to hardware
5404 * @ap: Port to which ATAPI device is attached.
5405 * @qc: Taskfile currently active
5407 * When device has indicated its readiness to accept
5408 * a CDB, this function is called. Send the CDB.
5410 * LOCKING:
5411 * caller.
5414 static void atapi_send_cdb(struct ata_port *ap, struct ata_queued_cmd *qc)
5416 /* send SCSI cdb */
5417 DPRINTK("send cdb\n");
5418 WARN_ON(qc->dev->cdb_len < 12);
5420 ap->ops->data_xfer(qc->dev, qc->cdb, qc->dev->cdb_len, 1);
5421 ata_altstatus(ap); /* flush */
5423 switch (qc->tf.protocol) {
5424 case ATAPI_PROT_PIO:
5425 ap->hsm_task_state = HSM_ST;
5426 break;
5427 case ATAPI_PROT_NODATA:
5428 ap->hsm_task_state = HSM_ST_LAST;
5429 break;
5430 case ATAPI_PROT_DMA:
5431 ap->hsm_task_state = HSM_ST_LAST;
5432 /* initiate bmdma */
5433 ap->ops->bmdma_start(qc);
5434 break;
5439 * __atapi_pio_bytes - Transfer data from/to the ATAPI device.
5440 * @qc: Command on going
5441 * @bytes: number of bytes
5443 * Transfer Transfer data from/to the ATAPI device.
5445 * LOCKING:
5446 * Inherited from caller.
5449 static int __atapi_pio_bytes(struct ata_queued_cmd *qc, unsigned int bytes)
5451 <<<<<<< HEAD:drivers/ata/libata-core.c
5452 int do_write = (qc->tf.flags & ATA_TFLAG_WRITE);
5453 =======
5454 int rw = (qc->tf.flags & ATA_TFLAG_WRITE) ? WRITE : READ;
5455 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5456 struct ata_port *ap = qc->ap;
5457 <<<<<<< HEAD:drivers/ata/libata-core.c
5458 struct ata_eh_info *ehi = &qc->dev->link->eh_info;
5459 =======
5460 struct ata_device *dev = qc->dev;
5461 struct ata_eh_info *ehi = &dev->link->eh_info;
5462 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5463 struct scatterlist *sg;
5464 struct page *page;
5465 unsigned char *buf;
5466 <<<<<<< HEAD:drivers/ata/libata-core.c
5467 unsigned int offset, count;
5468 =======
5469 unsigned int offset, count, consumed;
5470 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5472 next_sg:
5473 sg = qc->cursg;
5474 if (unlikely(!sg)) {
5475 <<<<<<< HEAD:drivers/ata/libata-core.c
5477 * The end of qc->sg is reached and the device expects
5478 * more data to transfer. In order not to overrun qc->sg
5479 * and fulfill length specified in the byte count register,
5480 * - for read case, discard trailing data from the device
5481 * - for write case, padding zero data to the device
5483 u16 pad_buf[1] = { 0 };
5484 unsigned int i;
5486 if (bytes > qc->curbytes - qc->nbytes + ATAPI_MAX_DRAIN) {
5487 ata_ehi_push_desc(ehi, "too much trailing data "
5488 "buf=%u cur=%u bytes=%u",
5489 qc->nbytes, qc->curbytes, bytes);
5490 return -1;
5493 /* overflow is exptected for misc ATAPI commands */
5494 if (bytes && !atapi_qc_may_overflow(qc))
5495 ata_dev_printk(qc->dev, KERN_WARNING, "ATAPI %u bytes "
5496 "trailing data (cdb=%02x nbytes=%u)\n",
5497 bytes, qc->cdb[0], qc->nbytes);
5499 for (i = 0; i < (bytes + 1) / 2; i++)
5500 ap->ops->data_xfer(qc->dev, (unsigned char *)pad_buf, 2, do_write);
5502 qc->curbytes += bytes;
5504 return 0;
5505 =======
5506 ata_ehi_push_desc(ehi, "unexpected or too much trailing data "
5507 "buf=%u cur=%u bytes=%u",
5508 qc->nbytes, qc->curbytes, bytes);
5509 return -1;
5510 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5513 page = sg_page(sg);
5514 offset = sg->offset + qc->cursg_ofs;
5516 /* get the current page and offset */
5517 page = nth_page(page, (offset >> PAGE_SHIFT));
5518 offset %= PAGE_SIZE;
5520 /* don't overrun current sg */
5521 count = min(sg->length - qc->cursg_ofs, bytes);
5523 /* don't cross page boundaries */
5524 count = min(count, (unsigned int)PAGE_SIZE - offset);
5526 DPRINTK("data %s\n", qc->tf.flags & ATA_TFLAG_WRITE ? "write" : "read");
5528 if (PageHighMem(page)) {
5529 unsigned long flags;
5531 /* FIXME: use bounce buffer */
5532 local_irq_save(flags);
5533 buf = kmap_atomic(page, KM_IRQ0);
5535 /* do the actual data transfer */
5536 <<<<<<< HEAD:drivers/ata/libata-core.c
5537 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
5538 =======
5539 consumed = ap->ops->data_xfer(dev, buf + offset, count, rw);
5540 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5542 kunmap_atomic(buf, KM_IRQ0);
5543 local_irq_restore(flags);
5544 } else {
5545 buf = page_address(page);
5546 <<<<<<< HEAD:drivers/ata/libata-core.c
5547 ap->ops->data_xfer(qc->dev, buf + offset, count, do_write);
5548 =======
5549 consumed = ap->ops->data_xfer(dev, buf + offset, count, rw);
5550 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5553 <<<<<<< HEAD:drivers/ata/libata-core.c
5554 bytes -= count;
5555 if ((count & 1) && bytes)
5556 bytes--;
5557 =======
5558 bytes -= min(bytes, consumed);
5559 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5560 qc->curbytes += count;
5561 qc->cursg_ofs += count;
5563 if (qc->cursg_ofs == sg->length) {
5564 qc->cursg = sg_next(qc->cursg);
5565 qc->cursg_ofs = 0;
5568 <<<<<<< HEAD:drivers/ata/libata-core.c
5569 =======
5570 /* consumed can be larger than count only for the last transfer */
5571 WARN_ON(qc->cursg && count != consumed);
5573 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5574 if (bytes)
5575 goto next_sg;
5576 <<<<<<< HEAD:drivers/ata/libata-core.c
5578 =======
5579 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5580 return 0;
5584 * atapi_pio_bytes - Transfer data from/to the ATAPI device.
5585 * @qc: Command on going
5587 * Transfer Transfer data from/to the ATAPI device.
5589 * LOCKING:
5590 * Inherited from caller.
5593 static void atapi_pio_bytes(struct ata_queued_cmd *qc)
5595 struct ata_port *ap = qc->ap;
5596 struct ata_device *dev = qc->dev;
5597 <<<<<<< HEAD:drivers/ata/libata-core.c
5598 =======
5599 struct ata_eh_info *ehi = &dev->link->eh_info;
5600 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5601 unsigned int ireason, bc_lo, bc_hi, bytes;
5602 int i_write, do_write = (qc->tf.flags & ATA_TFLAG_WRITE) ? 1 : 0;
5604 /* Abuse qc->result_tf for temp storage of intermediate TF
5605 * here to save some kernel stack usage.
5606 * For normal completion, qc->result_tf is not relevant. For
5607 * error, qc->result_tf is later overwritten by ata_qc_complete().
5608 * So, the correctness of qc->result_tf is not affected.
5610 ap->ops->tf_read(ap, &qc->result_tf);
5611 ireason = qc->result_tf.nsect;
5612 bc_lo = qc->result_tf.lbam;
5613 bc_hi = qc->result_tf.lbah;
5614 bytes = (bc_hi << 8) | bc_lo;
5616 /* shall be cleared to zero, indicating xfer of data */
5617 if (unlikely(ireason & (1 << 0)))
5618 <<<<<<< HEAD:drivers/ata/libata-core.c
5619 goto err_out;
5620 =======
5621 goto atapi_check;
5622 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5624 /* make sure transfer direction matches expected */
5625 i_write = ((ireason & (1 << 1)) == 0) ? 1 : 0;
5626 if (unlikely(do_write != i_write))
5627 <<<<<<< HEAD:drivers/ata/libata-core.c
5628 goto err_out;
5629 =======
5630 goto atapi_check;
5631 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5633 if (unlikely(!bytes))
5634 <<<<<<< HEAD:drivers/ata/libata-core.c
5635 goto err_out;
5636 =======
5637 goto atapi_check;
5638 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5640 VPRINTK("ata%u: xfering %d bytes\n", ap->print_id, bytes);
5642 <<<<<<< HEAD:drivers/ata/libata-core.c
5643 if (__atapi_pio_bytes(qc, bytes))
5644 =======
5645 if (unlikely(__atapi_pio_bytes(qc, bytes)))
5646 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5647 goto err_out;
5648 ata_altstatus(ap); /* flush */
5650 return;
5652 <<<<<<< HEAD:drivers/ata/libata-core.c
5653 err_out:
5654 ata_dev_printk(dev, KERN_INFO, "ATAPI check failed\n");
5655 =======
5656 atapi_check:
5657 ata_ehi_push_desc(ehi, "ATAPI check failed (ireason=0x%x bytes=%u)",
5658 ireason, bytes);
5659 err_out:
5660 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
5661 qc->err_mask |= AC_ERR_HSM;
5662 ap->hsm_task_state = HSM_ST_ERR;
5666 * ata_hsm_ok_in_wq - Check if the qc can be handled in the workqueue.
5667 * @ap: the target ata_port
5668 * @qc: qc on going
5670 * RETURNS:
5671 * 1 if ok in workqueue, 0 otherwise.
5674 static inline int ata_hsm_ok_in_wq(struct ata_port *ap, struct ata_queued_cmd *qc)
5676 if (qc->tf.flags & ATA_TFLAG_POLLING)
5677 return 1;
5679 if (ap->hsm_task_state == HSM_ST_FIRST) {
5680 if (qc->tf.protocol == ATA_PROT_PIO &&
5681 (qc->tf.flags & ATA_TFLAG_WRITE))
5682 return 1;
5684 if (ata_is_atapi(qc->tf.protocol) &&
5685 !(qc->dev->flags & ATA_DFLAG_CDB_INTR))
5686 return 1;
5689 return 0;
5693 * ata_hsm_qc_complete - finish a qc running on standard HSM
5694 * @qc: Command to complete
5695 * @in_wq: 1 if called from workqueue, 0 otherwise
5697 * Finish @qc which is running on standard HSM.
5699 * LOCKING:
5700 * If @in_wq is zero, spin_lock_irqsave(host lock).
5701 * Otherwise, none on entry and grabs host lock.
5703 static void ata_hsm_qc_complete(struct ata_queued_cmd *qc, int in_wq)
5705 struct ata_port *ap = qc->ap;
5706 unsigned long flags;
5708 if (ap->ops->error_handler) {
5709 if (in_wq) {
5710 spin_lock_irqsave(ap->lock, flags);
5712 /* EH might have kicked in while host lock is
5713 * released.
5715 qc = ata_qc_from_tag(ap, qc->tag);
5716 if (qc) {
5717 if (likely(!(qc->err_mask & AC_ERR_HSM))) {
5718 ap->ops->irq_on(ap);
5719 ata_qc_complete(qc);
5720 } else
5721 ata_port_freeze(ap);
5724 spin_unlock_irqrestore(ap->lock, flags);
5725 } else {
5726 if (likely(!(qc->err_mask & AC_ERR_HSM)))
5727 ata_qc_complete(qc);
5728 else
5729 ata_port_freeze(ap);
5731 } else {
5732 if (in_wq) {
5733 spin_lock_irqsave(ap->lock, flags);
5734 ap->ops->irq_on(ap);
5735 ata_qc_complete(qc);
5736 spin_unlock_irqrestore(ap->lock, flags);
5737 } else
5738 ata_qc_complete(qc);
5743 * ata_hsm_move - move the HSM to the next state.
5744 * @ap: the target ata_port
5745 * @qc: qc on going
5746 * @status: current device status
5747 * @in_wq: 1 if called from workqueue, 0 otherwise
5749 * RETURNS:
5750 * 1 when poll next status needed, 0 otherwise.
5752 int ata_hsm_move(struct ata_port *ap, struct ata_queued_cmd *qc,
5753 u8 status, int in_wq)
5755 unsigned long flags = 0;
5756 int poll_next;
5758 WARN_ON((qc->flags & ATA_QCFLAG_ACTIVE) == 0);
5760 /* Make sure ata_qc_issue_prot() does not throw things
5761 * like DMA polling into the workqueue. Notice that
5762 * in_wq is not equivalent to (qc->tf.flags & ATA_TFLAG_POLLING).
5764 WARN_ON(in_wq != ata_hsm_ok_in_wq(ap, qc));
5766 fsm_start:
5767 DPRINTK("ata%u: protocol %d task_state %d (dev_stat 0x%X)\n",
5768 ap->print_id, qc->tf.protocol, ap->hsm_task_state, status);
5770 switch (ap->hsm_task_state) {
5771 case HSM_ST_FIRST:
5772 /* Send first data block or PACKET CDB */
5774 /* If polling, we will stay in the work queue after
5775 * sending the data. Otherwise, interrupt handler
5776 * takes over after sending the data.
5778 poll_next = (qc->tf.flags & ATA_TFLAG_POLLING);
5780 /* check device status */
5781 if (unlikely((status & ATA_DRQ) == 0)) {
5782 /* handle BSY=0, DRQ=0 as error */
5783 if (likely(status & (ATA_ERR | ATA_DF)))
5784 /* device stops HSM for abort/error */
5785 qc->err_mask |= AC_ERR_DEV;
5786 else
5787 /* HSM violation. Let EH handle this */
5788 qc->err_mask |= AC_ERR_HSM;
5790 ap->hsm_task_state = HSM_ST_ERR;
5791 goto fsm_start;
5794 /* Device should not ask for data transfer (DRQ=1)
5795 * when it finds something wrong.
5796 * We ignore DRQ here and stop the HSM by
5797 * changing hsm_task_state to HSM_ST_ERR and
5798 * let the EH abort the command or reset the device.
5800 if (unlikely(status & (ATA_ERR | ATA_DF))) {
5801 /* Some ATAPI tape drives forget to clear the ERR bit
5802 * when doing the next command (mostly request sense).
5803 * We ignore ERR here to workaround and proceed sending
5804 * the CDB.
5806 if (!(qc->dev->horkage & ATA_HORKAGE_STUCK_ERR)) {
5807 ata_port_printk(ap, KERN_WARNING,
5808 "DRQ=1 with device error, "
5809 "dev_stat 0x%X\n", status);
5810 qc->err_mask |= AC_ERR_HSM;
5811 ap->hsm_task_state = HSM_ST_ERR;
5812 goto fsm_start;
5816 /* Send the CDB (atapi) or the first data block (ata pio out).
5817 * During the state transition, interrupt handler shouldn't
5818 * be invoked before the data transfer is complete and
5819 * hsm_task_state is changed. Hence, the following locking.
5821 if (in_wq)
5822 spin_lock_irqsave(ap->lock, flags);
5824 if (qc->tf.protocol == ATA_PROT_PIO) {
5825 /* PIO data out protocol.
5826 * send first data block.
5829 /* ata_pio_sectors() might change the state
5830 * to HSM_ST_LAST. so, the state is changed here
5831 * before ata_pio_sectors().
5833 ap->hsm_task_state = HSM_ST;
5834 ata_pio_sectors(qc);
5835 } else
5836 /* send CDB */
5837 atapi_send_cdb(ap, qc);
5839 if (in_wq)
5840 spin_unlock_irqrestore(ap->lock, flags);
5842 /* if polling, ata_pio_task() handles the rest.
5843 * otherwise, interrupt handler takes over from here.
5845 break;
5847 case HSM_ST:
5848 /* complete command or read/write the data register */
5849 if (qc->tf.protocol == ATAPI_PROT_PIO) {
5850 /* ATAPI PIO protocol */
5851 if ((status & ATA_DRQ) == 0) {
5852 /* No more data to transfer or device error.
5853 * Device error will be tagged in HSM_ST_LAST.
5855 ap->hsm_task_state = HSM_ST_LAST;
5856 goto fsm_start;
5859 /* Device should not ask for data transfer (DRQ=1)
5860 * when it finds something wrong.
5861 * We ignore DRQ here and stop the HSM by
5862 * changing hsm_task_state to HSM_ST_ERR and
5863 * let the EH abort the command or reset the device.
5865 if (unlikely(status & (ATA_ERR | ATA_DF))) {
5866 ata_port_printk(ap, KERN_WARNING, "DRQ=1 with "
5867 "device error, dev_stat 0x%X\n",
5868 status);
5869 qc->err_mask |= AC_ERR_HSM;
5870 ap->hsm_task_state = HSM_ST_ERR;
5871 goto fsm_start;
5874 atapi_pio_bytes(qc);
5876 if (unlikely(ap->hsm_task_state == HSM_ST_ERR))
5877 /* bad ireason reported by device */
5878 goto fsm_start;
5880 } else {
5881 /* ATA PIO protocol */
5882 if (unlikely((status & ATA_DRQ) == 0)) {
5883 /* handle BSY=0, DRQ=0 as error */
5884 if (likely(status & (ATA_ERR | ATA_DF)))
5885 /* device stops HSM for abort/error */
5886 qc->err_mask |= AC_ERR_DEV;
5887 else
5888 /* HSM violation. Let EH handle this.
5889 * Phantom devices also trigger this
5890 * condition. Mark hint.
5892 qc->err_mask |= AC_ERR_HSM |
5893 AC_ERR_NODEV_HINT;
5895 ap->hsm_task_state = HSM_ST_ERR;
5896 goto fsm_start;
5899 /* For PIO reads, some devices may ask for
5900 * data transfer (DRQ=1) alone with ERR=1.
5901 * We respect DRQ here and transfer one
5902 * block of junk data before changing the
5903 * hsm_task_state to HSM_ST_ERR.
5905 * For PIO writes, ERR=1 DRQ=1 doesn't make
5906 * sense since the data block has been
5907 * transferred to the device.
5909 if (unlikely(status & (ATA_ERR | ATA_DF))) {
5910 /* data might be corrputed */
5911 qc->err_mask |= AC_ERR_DEV;
5913 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
5914 ata_pio_sectors(qc);
5915 status = ata_wait_idle(ap);
5918 if (status & (ATA_BUSY | ATA_DRQ))
5919 qc->err_mask |= AC_ERR_HSM;
5921 /* ata_pio_sectors() might change the
5922 * state to HSM_ST_LAST. so, the state
5923 * is changed after ata_pio_sectors().
5925 ap->hsm_task_state = HSM_ST_ERR;
5926 goto fsm_start;
5929 ata_pio_sectors(qc);
5931 if (ap->hsm_task_state == HSM_ST_LAST &&
5932 (!(qc->tf.flags & ATA_TFLAG_WRITE))) {
5933 /* all data read */
5934 status = ata_wait_idle(ap);
5935 goto fsm_start;
5939 poll_next = 1;
5940 break;
5942 case HSM_ST_LAST:
5943 if (unlikely(!ata_ok(status))) {
5944 qc->err_mask |= __ac_err_mask(status);
5945 ap->hsm_task_state = HSM_ST_ERR;
5946 goto fsm_start;
5949 /* no more data to transfer */
5950 DPRINTK("ata%u: dev %u command complete, drv_stat 0x%x\n",
5951 ap->print_id, qc->dev->devno, status);
5953 WARN_ON(qc->err_mask);
5955 ap->hsm_task_state = HSM_ST_IDLE;
5957 /* complete taskfile transaction */
5958 ata_hsm_qc_complete(qc, in_wq);
5960 poll_next = 0;
5961 break;
5963 case HSM_ST_ERR:
5964 /* make sure qc->err_mask is available to
5965 * know what's wrong and recover
5967 WARN_ON(qc->err_mask == 0);
5969 ap->hsm_task_state = HSM_ST_IDLE;
5971 /* complete taskfile transaction */
5972 ata_hsm_qc_complete(qc, in_wq);
5974 poll_next = 0;
5975 break;
5976 default:
5977 poll_next = 0;
5978 BUG();
5981 return poll_next;
5984 static void ata_pio_task(struct work_struct *work)
5986 struct ata_port *ap =
5987 container_of(work, struct ata_port, port_task.work);
5988 struct ata_queued_cmd *qc = ap->port_task_data;
5989 u8 status;
5990 int poll_next;
5992 fsm_start:
5993 WARN_ON(ap->hsm_task_state == HSM_ST_IDLE);
5996 * This is purely heuristic. This is a fast path.
5997 * Sometimes when we enter, BSY will be cleared in
5998 * a chk-status or two. If not, the drive is probably seeking
5999 * or something. Snooze for a couple msecs, then
6000 * chk-status again. If still busy, queue delayed work.
6002 status = ata_busy_wait(ap, ATA_BUSY, 5);
6003 if (status & ATA_BUSY) {
6004 msleep(2);
6005 status = ata_busy_wait(ap, ATA_BUSY, 10);
6006 if (status & ATA_BUSY) {
6007 ata_pio_queue_task(ap, qc, ATA_SHORT_PAUSE);
6008 return;
6012 /* move the HSM */
6013 poll_next = ata_hsm_move(ap, qc, status, 1);
6015 /* another command or interrupt handler
6016 * may be running at this point.
6018 if (poll_next)
6019 goto fsm_start;
6023 * ata_qc_new - Request an available ATA command, for queueing
6024 * @ap: Port associated with device @dev
6025 * @dev: Device from whom we request an available command structure
6027 * LOCKING:
6028 * None.
6031 static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
6033 struct ata_queued_cmd *qc = NULL;
6034 unsigned int i;
6036 /* no command while frozen */
6037 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
6038 return NULL;
6040 /* the last tag is reserved for internal command. */
6041 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
6042 if (!test_and_set_bit(i, &ap->qc_allocated)) {
6043 qc = __ata_qc_from_tag(ap, i);
6044 break;
6047 if (qc)
6048 qc->tag = i;
6050 return qc;
6054 * ata_qc_new_init - Request an available ATA command, and initialize it
6055 * @dev: Device from whom we request an available command structure
6057 * LOCKING:
6058 * None.
6061 struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
6063 struct ata_port *ap = dev->link->ap;
6064 struct ata_queued_cmd *qc;
6066 qc = ata_qc_new(ap);
6067 if (qc) {
6068 qc->scsicmd = NULL;
6069 qc->ap = ap;
6070 qc->dev = dev;
6072 ata_qc_reinit(qc);
6075 return qc;
6079 * ata_qc_free - free unused ata_queued_cmd
6080 * @qc: Command to complete
6082 * Designed to free unused ata_queued_cmd object
6083 * in case something prevents using it.
6085 * LOCKING:
6086 * spin_lock_irqsave(host lock)
6088 void ata_qc_free(struct ata_queued_cmd *qc)
6090 struct ata_port *ap = qc->ap;
6091 unsigned int tag;
6093 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
6095 qc->flags = 0;
6096 tag = qc->tag;
6097 if (likely(ata_tag_valid(tag))) {
6098 qc->tag = ATA_TAG_POISON;
6099 clear_bit(tag, &ap->qc_allocated);
6103 void __ata_qc_complete(struct ata_queued_cmd *qc)
6105 struct ata_port *ap = qc->ap;
6106 struct ata_link *link = qc->dev->link;
6108 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
6109 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
6111 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
6112 ata_sg_clean(qc);
6114 /* command should be marked inactive atomically with qc completion */
6115 if (qc->tf.protocol == ATA_PROT_NCQ) {
6116 link->sactive &= ~(1 << qc->tag);
6117 if (!link->sactive)
6118 ap->nr_active_links--;
6119 } else {
6120 link->active_tag = ATA_TAG_POISON;
6121 ap->nr_active_links--;
6124 /* clear exclusive status */
6125 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
6126 ap->excl_link == link))
6127 ap->excl_link = NULL;
6129 /* atapi: mark qc as inactive to prevent the interrupt handler
6130 * from completing the command twice later, before the error handler
6131 * is called. (when rc != 0 and atapi request sense is needed)
6133 qc->flags &= ~ATA_QCFLAG_ACTIVE;
6134 ap->qc_active &= ~(1 << qc->tag);
6136 /* call completion callback */
6137 qc->complete_fn(qc);
6140 static void fill_result_tf(struct ata_queued_cmd *qc)
6142 struct ata_port *ap = qc->ap;
6144 qc->result_tf.flags = qc->tf.flags;
6145 ap->ops->tf_read(ap, &qc->result_tf);
6148 static void ata_verify_xfer(struct ata_queued_cmd *qc)
6150 struct ata_device *dev = qc->dev;
6152 if (ata_tag_internal(qc->tag))
6153 return;
6155 if (ata_is_nodata(qc->tf.protocol))
6156 return;
6158 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
6159 return;
6161 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
6165 * ata_qc_complete - Complete an active ATA command
6166 * @qc: Command to complete
6167 * @err_mask: ATA Status register contents
6169 * Indicate to the mid and upper layers that an ATA
6170 * command has completed, with either an ok or not-ok status.
6172 * LOCKING:
6173 * spin_lock_irqsave(host lock)
6175 void ata_qc_complete(struct ata_queued_cmd *qc)
6177 struct ata_port *ap = qc->ap;
6179 /* XXX: New EH and old EH use different mechanisms to
6180 * synchronize EH with regular execution path.
6182 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
6183 * Normal execution path is responsible for not accessing a
6184 * failed qc. libata core enforces the rule by returning NULL
6185 * from ata_qc_from_tag() for failed qcs.
6187 * Old EH depends on ata_qc_complete() nullifying completion
6188 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
6189 * not synchronize with interrupt handler. Only PIO task is
6190 * taken care of.
6192 if (ap->ops->error_handler) {
6193 struct ata_device *dev = qc->dev;
6194 struct ata_eh_info *ehi = &dev->link->eh_info;
6196 WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
6198 if (unlikely(qc->err_mask))
6199 qc->flags |= ATA_QCFLAG_FAILED;
6201 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
6202 if (!ata_tag_internal(qc->tag)) {
6203 /* always fill result TF for failed qc */
6204 fill_result_tf(qc);
6205 ata_qc_schedule_eh(qc);
6206 return;
6210 /* read result TF if requested */
6211 if (qc->flags & ATA_QCFLAG_RESULT_TF)
6212 fill_result_tf(qc);
6214 /* Some commands need post-processing after successful
6215 * completion.
6217 switch (qc->tf.command) {
6218 case ATA_CMD_SET_FEATURES:
6219 if (qc->tf.feature != SETFEATURES_WC_ON &&
6220 qc->tf.feature != SETFEATURES_WC_OFF)
6221 break;
6222 /* fall through */
6223 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
6224 case ATA_CMD_SET_MULTI: /* multi_count changed */
6225 /* revalidate device */
6226 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
6227 ata_port_schedule_eh(ap);
6228 break;
6230 case ATA_CMD_SLEEP:
6231 dev->flags |= ATA_DFLAG_SLEEPING;
6232 break;
6235 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
6236 ata_verify_xfer(qc);
6238 __ata_qc_complete(qc);
6239 } else {
6240 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
6241 return;
6243 /* read result TF if failed or requested */
6244 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
6245 fill_result_tf(qc);
6247 __ata_qc_complete(qc);
6252 * ata_qc_complete_multiple - Complete multiple qcs successfully
6253 * @ap: port in question
6254 * @qc_active: new qc_active mask
6255 * @finish_qc: LLDD callback invoked before completing a qc
6257 * Complete in-flight commands. This functions is meant to be
6258 * called from low-level driver's interrupt routine to complete
6259 * requests normally. ap->qc_active and @qc_active is compared
6260 * and commands are completed accordingly.
6262 * LOCKING:
6263 * spin_lock_irqsave(host lock)
6265 * RETURNS:
6266 * Number of completed commands on success, -errno otherwise.
6268 int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
6269 void (*finish_qc)(struct ata_queued_cmd *))
6271 int nr_done = 0;
6272 u32 done_mask;
6273 int i;
6275 done_mask = ap->qc_active ^ qc_active;
6277 if (unlikely(done_mask & qc_active)) {
6278 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
6279 "(%08x->%08x)\n", ap->qc_active, qc_active);
6280 return -EINVAL;
6283 for (i = 0; i < ATA_MAX_QUEUE; i++) {
6284 struct ata_queued_cmd *qc;
6286 if (!(done_mask & (1 << i)))
6287 continue;
6289 if ((qc = ata_qc_from_tag(ap, i))) {
6290 if (finish_qc)
6291 finish_qc(qc);
6292 ata_qc_complete(qc);
6293 nr_done++;
6297 return nr_done;
6301 * ata_qc_issue - issue taskfile to device
6302 * @qc: command to issue to device
6304 * Prepare an ATA command to submission to device.
6305 * This includes mapping the data into a DMA-able
6306 * area, filling in the S/G table, and finally
6307 * writing the taskfile to hardware, starting the command.
6309 * LOCKING:
6310 * spin_lock_irqsave(host lock)
6312 void ata_qc_issue(struct ata_queued_cmd *qc)
6314 struct ata_port *ap = qc->ap;
6315 struct ata_link *link = qc->dev->link;
6316 u8 prot = qc->tf.protocol;
6318 /* Make sure only one non-NCQ command is outstanding. The
6319 * check is skipped for old EH because it reuses active qc to
6320 * request ATAPI sense.
6322 WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
6324 if (ata_is_ncq(prot)) {
6325 WARN_ON(link->sactive & (1 << qc->tag));
6327 if (!link->sactive)
6328 ap->nr_active_links++;
6329 link->sactive |= 1 << qc->tag;
6330 } else {
6331 WARN_ON(link->sactive);
6333 ap->nr_active_links++;
6334 link->active_tag = qc->tag;
6337 qc->flags |= ATA_QCFLAG_ACTIVE;
6338 ap->qc_active |= 1 << qc->tag;
6340 /* We guarantee to LLDs that they will have at least one
6341 * non-zero sg if the command is a data command.
6343 BUG_ON(ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes));
6345 <<<<<<< HEAD:drivers/ata/libata-core.c
6346 /* ata_sg_setup() may update nbytes */
6347 qc->raw_nbytes = qc->nbytes;
6349 =======
6350 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
6351 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
6352 (ap->flags & ATA_FLAG_PIO_DMA)))
6353 if (ata_sg_setup(qc))
6354 goto sg_err;
6356 /* if device is sleeping, schedule softreset and abort the link */
6357 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
6358 link->eh_info.action |= ATA_EH_SOFTRESET;
6359 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
6360 ata_link_abort(link);
6361 return;
6364 ap->ops->qc_prep(qc);
6366 qc->err_mask |= ap->ops->qc_issue(qc);
6367 if (unlikely(qc->err_mask))
6368 goto err;
6369 return;
6371 sg_err:
6372 qc->err_mask |= AC_ERR_SYSTEM;
6373 err:
6374 ata_qc_complete(qc);
6378 * ata_qc_issue_prot - issue taskfile to device in proto-dependent manner
6379 * @qc: command to issue to device
6381 * Using various libata functions and hooks, this function
6382 * starts an ATA command. ATA commands are grouped into
6383 * classes called "protocols", and issuing each type of protocol
6384 * is slightly different.
6386 * May be used as the qc_issue() entry in ata_port_operations.
6388 * LOCKING:
6389 * spin_lock_irqsave(host lock)
6391 * RETURNS:
6392 * Zero on success, AC_ERR_* mask on failure
6395 unsigned int ata_qc_issue_prot(struct ata_queued_cmd *qc)
6397 struct ata_port *ap = qc->ap;
6399 /* Use polling pio if the LLD doesn't handle
6400 * interrupt driven pio and atapi CDB interrupt.
6402 if (ap->flags & ATA_FLAG_PIO_POLLING) {
6403 switch (qc->tf.protocol) {
6404 case ATA_PROT_PIO:
6405 case ATA_PROT_NODATA:
6406 case ATAPI_PROT_PIO:
6407 case ATAPI_PROT_NODATA:
6408 qc->tf.flags |= ATA_TFLAG_POLLING;
6409 break;
6410 case ATAPI_PROT_DMA:
6411 if (qc->dev->flags & ATA_DFLAG_CDB_INTR)
6412 /* see ata_dma_blacklisted() */
6413 BUG();
6414 break;
6415 default:
6416 break;
6420 /* select the device */
6421 ata_dev_select(ap, qc->dev->devno, 1, 0);
6423 /* start the command */
6424 switch (qc->tf.protocol) {
6425 case ATA_PROT_NODATA:
6426 if (qc->tf.flags & ATA_TFLAG_POLLING)
6427 ata_qc_set_polling(qc);
6429 ata_tf_to_host(ap, &qc->tf);
6430 ap->hsm_task_state = HSM_ST_LAST;
6432 if (qc->tf.flags & ATA_TFLAG_POLLING)
6433 ata_pio_queue_task(ap, qc, 0);
6435 break;
6437 case ATA_PROT_DMA:
6438 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
6440 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
6441 ap->ops->bmdma_setup(qc); /* set up bmdma */
6442 ap->ops->bmdma_start(qc); /* initiate bmdma */
6443 ap->hsm_task_state = HSM_ST_LAST;
6444 break;
6446 case ATA_PROT_PIO:
6447 if (qc->tf.flags & ATA_TFLAG_POLLING)
6448 ata_qc_set_polling(qc);
6450 ata_tf_to_host(ap, &qc->tf);
6452 if (qc->tf.flags & ATA_TFLAG_WRITE) {
6453 /* PIO data out protocol */
6454 ap->hsm_task_state = HSM_ST_FIRST;
6455 ata_pio_queue_task(ap, qc, 0);
6457 /* always send first data block using
6458 * the ata_pio_task() codepath.
6460 } else {
6461 /* PIO data in protocol */
6462 ap->hsm_task_state = HSM_ST;
6464 if (qc->tf.flags & ATA_TFLAG_POLLING)
6465 ata_pio_queue_task(ap, qc, 0);
6467 /* if polling, ata_pio_task() handles the rest.
6468 * otherwise, interrupt handler takes over from here.
6472 break;
6474 case ATAPI_PROT_PIO:
6475 case ATAPI_PROT_NODATA:
6476 if (qc->tf.flags & ATA_TFLAG_POLLING)
6477 ata_qc_set_polling(qc);
6479 ata_tf_to_host(ap, &qc->tf);
6481 ap->hsm_task_state = HSM_ST_FIRST;
6483 /* send cdb by polling if no cdb interrupt */
6484 if ((!(qc->dev->flags & ATA_DFLAG_CDB_INTR)) ||
6485 (qc->tf.flags & ATA_TFLAG_POLLING))
6486 ata_pio_queue_task(ap, qc, 0);
6487 break;
6489 case ATAPI_PROT_DMA:
6490 WARN_ON(qc->tf.flags & ATA_TFLAG_POLLING);
6492 ap->ops->tf_load(ap, &qc->tf); /* load tf registers */
6493 ap->ops->bmdma_setup(qc); /* set up bmdma */
6494 ap->hsm_task_state = HSM_ST_FIRST;
6496 /* send cdb by polling if no cdb interrupt */
6497 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
6498 ata_pio_queue_task(ap, qc, 0);
6499 break;
6501 default:
6502 WARN_ON(1);
6503 return AC_ERR_SYSTEM;
6506 return 0;
6510 * ata_host_intr - Handle host interrupt for given (port, task)
6511 * @ap: Port on which interrupt arrived (possibly...)
6512 * @qc: Taskfile currently active in engine
6514 * Handle host interrupt for given queued command. Currently,
6515 * only DMA interrupts are handled. All other commands are
6516 * handled via polling with interrupts disabled (nIEN bit).
6518 * LOCKING:
6519 * spin_lock_irqsave(host lock)
6521 * RETURNS:
6522 * One if interrupt was handled, zero if not (shared irq).
6525 inline unsigned int ata_host_intr(struct ata_port *ap,
6526 struct ata_queued_cmd *qc)
6528 struct ata_eh_info *ehi = &ap->link.eh_info;
6529 u8 status, host_stat = 0;
6531 VPRINTK("ata%u: protocol %d task_state %d\n",
6532 ap->print_id, qc->tf.protocol, ap->hsm_task_state);
6534 /* Check whether we are expecting interrupt in this state */
6535 switch (ap->hsm_task_state) {
6536 case HSM_ST_FIRST:
6537 /* Some pre-ATAPI-4 devices assert INTRQ
6538 * at this state when ready to receive CDB.
6541 /* Check the ATA_DFLAG_CDB_INTR flag is enough here.
6542 * The flag was turned on only for atapi devices. No
6543 * need to check ata_is_atapi(qc->tf.protocol) again.
6545 if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
6546 goto idle_irq;
6547 break;
6548 case HSM_ST_LAST:
6549 if (qc->tf.protocol == ATA_PROT_DMA ||
6550 qc->tf.protocol == ATAPI_PROT_DMA) {
6551 /* check status of DMA engine */
6552 host_stat = ap->ops->bmdma_status(ap);
6553 VPRINTK("ata%u: host_stat 0x%X\n",
6554 ap->print_id, host_stat);
6556 /* if it's not our irq... */
6557 if (!(host_stat & ATA_DMA_INTR))
6558 goto idle_irq;
6560 /* before we do anything else, clear DMA-Start bit */
6561 ap->ops->bmdma_stop(qc);
6563 if (unlikely(host_stat & ATA_DMA_ERR)) {
6564 /* error when transfering data to/from memory */
6565 qc->err_mask |= AC_ERR_HOST_BUS;
6566 ap->hsm_task_state = HSM_ST_ERR;
6569 break;
6570 case HSM_ST:
6571 break;
6572 default:
6573 goto idle_irq;
6576 /* check altstatus */
6577 status = ata_altstatus(ap);
6578 if (status & ATA_BUSY)
6579 goto idle_irq;
6581 /* check main status, clearing INTRQ */
6582 status = ata_chk_status(ap);
6583 if (unlikely(status & ATA_BUSY))
6584 goto idle_irq;
6586 /* ack bmdma irq events */
6587 ap->ops->irq_clear(ap);
6589 ata_hsm_move(ap, qc, status, 0);
6591 if (unlikely(qc->err_mask) && (qc->tf.protocol == ATA_PROT_DMA ||
6592 qc->tf.protocol == ATAPI_PROT_DMA))
6593 ata_ehi_push_desc(ehi, "BMDMA stat 0x%x", host_stat);
6595 return 1; /* irq handled */
6597 idle_irq:
6598 ap->stats.idle_irq++;
6600 #ifdef ATA_IRQ_TRAP
6601 if ((ap->stats.idle_irq % 1000) == 0) {
6602 ata_chk_status(ap);
6603 ap->ops->irq_clear(ap);
6604 ata_port_printk(ap, KERN_WARNING, "irq trap\n");
6605 return 1;
6607 #endif
6608 return 0; /* irq not handled */
6612 * ata_interrupt - Default ATA host interrupt handler
6613 * @irq: irq line (unused)
6614 * @dev_instance: pointer to our ata_host information structure
6616 * Default interrupt handler for PCI IDE devices. Calls
6617 * ata_host_intr() for each port that is not disabled.
6619 * LOCKING:
6620 * Obtains host lock during operation.
6622 * RETURNS:
6623 * IRQ_NONE or IRQ_HANDLED.
6626 irqreturn_t ata_interrupt(int irq, void *dev_instance)
6628 struct ata_host *host = dev_instance;
6629 unsigned int i;
6630 unsigned int handled = 0;
6631 unsigned long flags;
6633 /* TODO: make _irqsave conditional on x86 PCI IDE legacy mode */
6634 spin_lock_irqsave(&host->lock, flags);
6636 for (i = 0; i < host->n_ports; i++) {
6637 struct ata_port *ap;
6639 ap = host->ports[i];
6640 if (ap &&
6641 !(ap->flags & ATA_FLAG_DISABLED)) {
6642 struct ata_queued_cmd *qc;
6644 qc = ata_qc_from_tag(ap, ap->link.active_tag);
6645 if (qc && (!(qc->tf.flags & ATA_TFLAG_POLLING)) &&
6646 (qc->flags & ATA_QCFLAG_ACTIVE))
6647 handled |= ata_host_intr(ap, qc);
6651 spin_unlock_irqrestore(&host->lock, flags);
6653 return IRQ_RETVAL(handled);
6657 * sata_scr_valid - test whether SCRs are accessible
6658 * @link: ATA link to test SCR accessibility for
6660 * Test whether SCRs are accessible for @link.
6662 * LOCKING:
6663 * None.
6665 * RETURNS:
6666 * 1 if SCRs are accessible, 0 otherwise.
6668 int sata_scr_valid(struct ata_link *link)
6670 struct ata_port *ap = link->ap;
6672 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
6676 * sata_scr_read - read SCR register of the specified port
6677 * @link: ATA link to read SCR for
6678 * @reg: SCR to read
6679 * @val: Place to store read value
6681 * Read SCR register @reg of @link into *@val. This function is
6682 * guaranteed to succeed if @link is ap->link, the cable type of
6683 * the port is SATA and the port implements ->scr_read.
6685 * LOCKING:
6686 * None if @link is ap->link. Kernel thread context otherwise.
6688 * RETURNS:
6689 * 0 on success, negative errno on failure.
6691 int sata_scr_read(struct ata_link *link, int reg, u32 *val)
6693 if (ata_is_host_link(link)) {
6694 struct ata_port *ap = link->ap;
6696 if (sata_scr_valid(link))
6697 return ap->ops->scr_read(ap, reg, val);
6698 return -EOPNOTSUPP;
6701 return sata_pmp_scr_read(link, reg, val);
6705 * sata_scr_write - write SCR register of the specified port
6706 * @link: ATA link to write SCR for
6707 * @reg: SCR to write
6708 * @val: value to write
6710 * Write @val to SCR register @reg of @link. This function is
6711 * guaranteed to succeed if @link is ap->link, the cable type of
6712 * the port is SATA and the port implements ->scr_read.
6714 * LOCKING:
6715 * None if @link is ap->link. Kernel thread context otherwise.
6717 * RETURNS:
6718 * 0 on success, negative errno on failure.
6720 int sata_scr_write(struct ata_link *link, int reg, u32 val)
6722 if (ata_is_host_link(link)) {
6723 struct ata_port *ap = link->ap;
6725 if (sata_scr_valid(link))
6726 return ap->ops->scr_write(ap, reg, val);
6727 return -EOPNOTSUPP;
6730 return sata_pmp_scr_write(link, reg, val);
6734 * sata_scr_write_flush - write SCR register of the specified port and flush
6735 * @link: ATA link to write SCR for
6736 * @reg: SCR to write
6737 * @val: value to write
6739 * This function is identical to sata_scr_write() except that this
6740 * function performs flush after writing to the register.
6742 * LOCKING:
6743 * None if @link is ap->link. Kernel thread context otherwise.
6745 * RETURNS:
6746 * 0 on success, negative errno on failure.
6748 int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
6750 if (ata_is_host_link(link)) {
6751 struct ata_port *ap = link->ap;
6752 int rc;
6754 if (sata_scr_valid(link)) {
6755 rc = ap->ops->scr_write(ap, reg, val);
6756 if (rc == 0)
6757 rc = ap->ops->scr_read(ap, reg, &val);
6758 return rc;
6760 return -EOPNOTSUPP;
6763 return sata_pmp_scr_write(link, reg, val);
6767 * ata_link_online - test whether the given link is online
6768 * @link: ATA link to test
6770 * Test whether @link is online. Note that this function returns
6771 * 0 if online status of @link cannot be obtained, so
6772 * ata_link_online(link) != !ata_link_offline(link).
6774 * LOCKING:
6775 * None.
6777 * RETURNS:
6778 * 1 if the port online status is available and online.
6780 int ata_link_online(struct ata_link *link)
6782 u32 sstatus;
6784 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
6785 (sstatus & 0xf) == 0x3)
6786 return 1;
6787 return 0;
6791 * ata_link_offline - test whether the given link is offline
6792 * @link: ATA link to test
6794 * Test whether @link is offline. Note that this function
6795 * returns 0 if offline status of @link cannot be obtained, so
6796 * ata_link_online(link) != !ata_link_offline(link).
6798 * LOCKING:
6799 * None.
6801 * RETURNS:
6802 * 1 if the port offline status is available and offline.
6804 int ata_link_offline(struct ata_link *link)
6806 u32 sstatus;
6808 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
6809 (sstatus & 0xf) != 0x3)
6810 return 1;
6811 return 0;
6814 int ata_flush_cache(struct ata_device *dev)
6816 unsigned int err_mask;
6817 u8 cmd;
6819 if (!ata_try_flush_cache(dev))
6820 return 0;
6822 if (dev->flags & ATA_DFLAG_FLUSH_EXT)
6823 cmd = ATA_CMD_FLUSH_EXT;
6824 else
6825 cmd = ATA_CMD_FLUSH;
6827 /* This is wrong. On a failed flush we get back the LBA of the lost
6828 sector and we should (assuming it wasn't aborted as unknown) issue
6829 a further flush command to continue the writeback until it
6830 does not error */
6831 err_mask = ata_do_simple_cmd(dev, cmd);
6832 if (err_mask) {
6833 ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
6834 return -EIO;
6837 return 0;
6840 #ifdef CONFIG_PM
6841 static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
6842 unsigned int action, unsigned int ehi_flags,
6843 int wait)
6845 unsigned long flags;
6846 int i, rc;
6848 for (i = 0; i < host->n_ports; i++) {
6849 struct ata_port *ap = host->ports[i];
6850 struct ata_link *link;
6852 /* Previous resume operation might still be in
6853 * progress. Wait for PM_PENDING to clear.
6855 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
6856 ata_port_wait_eh(ap);
6857 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
6860 /* request PM ops to EH */
6861 spin_lock_irqsave(ap->lock, flags);
6863 ap->pm_mesg = mesg;
6864 if (wait) {
6865 rc = 0;
6866 ap->pm_result = &rc;
6869 ap->pflags |= ATA_PFLAG_PM_PENDING;
6870 __ata_port_for_each_link(link, ap) {
6871 link->eh_info.action |= action;
6872 link->eh_info.flags |= ehi_flags;
6875 ata_port_schedule_eh(ap);
6877 spin_unlock_irqrestore(ap->lock, flags);
6879 /* wait and check result */
6880 if (wait) {
6881 ata_port_wait_eh(ap);
6882 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
6883 if (rc)
6884 return rc;
6888 return 0;
6892 * ata_host_suspend - suspend host
6893 * @host: host to suspend
6894 * @mesg: PM message
6896 * Suspend @host. Actual operation is performed by EH. This
6897 * function requests EH to perform PM operations and waits for EH
6898 * to finish.
6900 * LOCKING:
6901 * Kernel thread context (may sleep).
6903 * RETURNS:
6904 * 0 on success, -errno on failure.
6906 int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
6908 int rc;
6911 * disable link pm on all ports before requesting
6912 * any pm activity
6914 ata_lpm_enable(host);
6916 rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
6917 if (rc == 0)
6918 host->dev->power.power_state = mesg;
6919 return rc;
6923 * ata_host_resume - resume host
6924 * @host: host to resume
6926 * Resume @host. Actual operation is performed by EH. This
6927 * function requests EH to perform PM operations and returns.
6928 * Note that all resume operations are performed parallely.
6930 * LOCKING:
6931 * Kernel thread context (may sleep).
6933 void ata_host_resume(struct ata_host *host)
6935 ata_host_request_pm(host, PMSG_ON, ATA_EH_SOFTRESET,
6936 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
6937 host->dev->power.power_state = PMSG_ON;
6939 /* reenable link pm */
6940 ata_lpm_disable(host);
6942 #endif
6945 * ata_port_start - Set port up for dma.
6946 * @ap: Port to initialize
6948 * Called just after data structures for each port are
6949 * initialized. Allocates space for PRD table.
6951 * May be used as the port_start() entry in ata_port_operations.
6953 * LOCKING:
6954 * Inherited from caller.
6956 int ata_port_start(struct ata_port *ap)
6958 struct device *dev = ap->dev;
6959 <<<<<<< HEAD:drivers/ata/libata-core.c
6960 int rc;
6961 =======
6962 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
6964 ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
6965 GFP_KERNEL);
6966 if (!ap->prd)
6967 return -ENOMEM;
6969 <<<<<<< HEAD:drivers/ata/libata-core.c
6970 rc = ata_pad_alloc(ap, dev);
6971 if (rc)
6972 return rc;
6974 DPRINTK("prd alloc, virt %p, dma %llx\n", ap->prd,
6975 (unsigned long long)ap->prd_dma);
6976 =======
6977 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
6978 return 0;
6982 * ata_dev_init - Initialize an ata_device structure
6983 * @dev: Device structure to initialize
6985 * Initialize @dev in preparation for probing.
6987 * LOCKING:
6988 * Inherited from caller.
6990 void ata_dev_init(struct ata_device *dev)
6992 struct ata_link *link = dev->link;
6993 struct ata_port *ap = link->ap;
6994 unsigned long flags;
6996 /* SATA spd limit is bound to the first device */
6997 link->sata_spd_limit = link->hw_sata_spd_limit;
6998 link->sata_spd = 0;
7000 /* High bits of dev->flags are used to record warm plug
7001 * requests which occur asynchronously. Synchronize using
7002 * host lock.
7004 spin_lock_irqsave(ap->lock, flags);
7005 dev->flags &= ~ATA_DFLAG_INIT_MASK;
7006 dev->horkage = 0;
7007 spin_unlock_irqrestore(ap->lock, flags);
7009 memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
7010 sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
7011 dev->pio_mask = UINT_MAX;
7012 dev->mwdma_mask = UINT_MAX;
7013 dev->udma_mask = UINT_MAX;
7017 * ata_link_init - Initialize an ata_link structure
7018 * @ap: ATA port link is attached to
7019 * @link: Link structure to initialize
7020 * @pmp: Port multiplier port number
7022 * Initialize @link.
7024 * LOCKING:
7025 * Kernel thread context (may sleep)
7027 void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
7029 int i;
7031 /* clear everything except for devices */
7032 memset(link, 0, offsetof(struct ata_link, device[0]));
7034 link->ap = ap;
7035 link->pmp = pmp;
7036 link->active_tag = ATA_TAG_POISON;
7037 link->hw_sata_spd_limit = UINT_MAX;
7039 /* can't use iterator, ap isn't initialized yet */
7040 for (i = 0; i < ATA_MAX_DEVICES; i++) {
7041 struct ata_device *dev = &link->device[i];
7043 dev->link = link;
7044 dev->devno = dev - link->device;
7045 ata_dev_init(dev);
7050 * sata_link_init_spd - Initialize link->sata_spd_limit
7051 * @link: Link to configure sata_spd_limit for
7053 * Initialize @link->[hw_]sata_spd_limit to the currently
7054 * configured value.
7056 * LOCKING:
7057 * Kernel thread context (may sleep).
7059 * RETURNS:
7060 * 0 on success, -errno on failure.
7062 int sata_link_init_spd(struct ata_link *link)
7064 <<<<<<< HEAD:drivers/ata/libata-core.c
7065 u32 scontrol, spd;
7066 =======
7067 u32 scontrol;
7068 u8 spd;
7069 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
7070 int rc;
7072 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
7073 if (rc)
7074 return rc;
7076 spd = (scontrol >> 4) & 0xf;
7077 if (spd)
7078 link->hw_sata_spd_limit &= (1 << spd) - 1;
7080 <<<<<<< HEAD:drivers/ata/libata-core.c
7081 =======
7082 ata_force_spd_limit(link);
7084 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
7085 link->sata_spd_limit = link->hw_sata_spd_limit;
7087 return 0;
7091 * ata_port_alloc - allocate and initialize basic ATA port resources
7092 * @host: ATA host this allocated port belongs to
7094 * Allocate and initialize basic ATA port resources.
7096 * RETURNS:
7097 * Allocate ATA port on success, NULL on failure.
7099 * LOCKING:
7100 * Inherited from calling layer (may sleep).
7102 struct ata_port *ata_port_alloc(struct ata_host *host)
7104 struct ata_port *ap;
7106 DPRINTK("ENTER\n");
7108 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
7109 if (!ap)
7110 return NULL;
7112 ap->pflags |= ATA_PFLAG_INITIALIZING;
7113 ap->lock = &host->lock;
7114 ap->flags = ATA_FLAG_DISABLED;
7115 ap->print_id = -1;
7116 ap->ctl = ATA_DEVCTL_OBS;
7117 ap->host = host;
7118 ap->dev = host->dev;
7119 ap->last_ctl = 0xFF;
7121 #if defined(ATA_VERBOSE_DEBUG)
7122 /* turn on all debugging levels */
7123 ap->msg_enable = 0x00FF;
7124 #elif defined(ATA_DEBUG)
7125 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
7126 #else
7127 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
7128 #endif
7130 INIT_DELAYED_WORK(&ap->port_task, ata_pio_task);
7131 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
7132 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
7133 INIT_LIST_HEAD(&ap->eh_done_q);
7134 init_waitqueue_head(&ap->eh_wait_q);
7135 init_timer_deferrable(&ap->fastdrain_timer);
7136 ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
7137 ap->fastdrain_timer.data = (unsigned long)ap;
7139 ap->cbl = ATA_CBL_NONE;
7141 ata_link_init(ap, &ap->link, 0);
7143 #ifdef ATA_IRQ_TRAP
7144 ap->stats.unhandled_irq = 1;
7145 ap->stats.idle_irq = 1;
7146 #endif
7147 return ap;
7150 static void ata_host_release(struct device *gendev, void *res)
7152 struct ata_host *host = dev_get_drvdata(gendev);
7153 int i;
7155 for (i = 0; i < host->n_ports; i++) {
7156 struct ata_port *ap = host->ports[i];
7158 if (!ap)
7159 continue;
7161 if (ap->scsi_host)
7162 scsi_host_put(ap->scsi_host);
7164 kfree(ap->pmp_link);
7165 kfree(ap);
7166 host->ports[i] = NULL;
7169 dev_set_drvdata(gendev, NULL);
7173 * ata_host_alloc - allocate and init basic ATA host resources
7174 * @dev: generic device this host is associated with
7175 * @max_ports: maximum number of ATA ports associated with this host
7177 * Allocate and initialize basic ATA host resources. LLD calls
7178 * this function to allocate a host, initializes it fully and
7179 * attaches it using ata_host_register().
7181 * @max_ports ports are allocated and host->n_ports is
7182 * initialized to @max_ports. The caller is allowed to decrease
7183 * host->n_ports before calling ata_host_register(). The unused
7184 * ports will be automatically freed on registration.
7186 * RETURNS:
7187 * Allocate ATA host on success, NULL on failure.
7189 * LOCKING:
7190 * Inherited from calling layer (may sleep).
7192 struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
7194 struct ata_host *host;
7195 size_t sz;
7196 int i;
7198 DPRINTK("ENTER\n");
7200 if (!devres_open_group(dev, NULL, GFP_KERNEL))
7201 return NULL;
7203 /* alloc a container for our list of ATA ports (buses) */
7204 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
7205 /* alloc a container for our list of ATA ports (buses) */
7206 host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
7207 if (!host)
7208 goto err_out;
7210 devres_add(dev, host);
7211 dev_set_drvdata(dev, host);
7213 spin_lock_init(&host->lock);
7214 host->dev = dev;
7215 host->n_ports = max_ports;
7217 /* allocate ports bound to this host */
7218 for (i = 0; i < max_ports; i++) {
7219 struct ata_port *ap;
7221 ap = ata_port_alloc(host);
7222 if (!ap)
7223 goto err_out;
7225 ap->port_no = i;
7226 host->ports[i] = ap;
7229 devres_remove_group(dev, NULL);
7230 return host;
7232 err_out:
7233 devres_release_group(dev, NULL);
7234 return NULL;
7238 * ata_host_alloc_pinfo - alloc host and init with port_info array
7239 * @dev: generic device this host is associated with
7240 * @ppi: array of ATA port_info to initialize host with
7241 * @n_ports: number of ATA ports attached to this host
7243 * Allocate ATA host and initialize with info from @ppi. If NULL
7244 * terminated, @ppi may contain fewer entries than @n_ports. The
7245 * last entry will be used for the remaining ports.
7247 * RETURNS:
7248 * Allocate ATA host on success, NULL on failure.
7250 * LOCKING:
7251 * Inherited from calling layer (may sleep).
7253 struct ata_host *ata_host_alloc_pinfo(struct device *dev,
7254 const struct ata_port_info * const * ppi,
7255 int n_ports)
7257 const struct ata_port_info *pi;
7258 struct ata_host *host;
7259 int i, j;
7261 host = ata_host_alloc(dev, n_ports);
7262 if (!host)
7263 return NULL;
7265 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
7266 struct ata_port *ap = host->ports[i];
7268 if (ppi[j])
7269 pi = ppi[j++];
7271 ap->pio_mask = pi->pio_mask;
7272 ap->mwdma_mask = pi->mwdma_mask;
7273 ap->udma_mask = pi->udma_mask;
7274 ap->flags |= pi->flags;
7275 ap->link.flags |= pi->link_flags;
7276 ap->ops = pi->port_ops;
7278 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
7279 host->ops = pi->port_ops;
7280 if (!host->private_data && pi->private_data)
7281 host->private_data = pi->private_data;
7284 return host;
7287 static void ata_host_stop(struct device *gendev, void *res)
7289 struct ata_host *host = dev_get_drvdata(gendev);
7290 int i;
7292 WARN_ON(!(host->flags & ATA_HOST_STARTED));
7294 for (i = 0; i < host->n_ports; i++) {
7295 struct ata_port *ap = host->ports[i];
7297 if (ap->ops->port_stop)
7298 ap->ops->port_stop(ap);
7301 if (host->ops->host_stop)
7302 host->ops->host_stop(host);
7306 * ata_host_start - start and freeze ports of an ATA host
7307 * @host: ATA host to start ports for
7309 * Start and then freeze ports of @host. Started status is
7310 * recorded in host->flags, so this function can be called
7311 * multiple times. Ports are guaranteed to get started only
7312 * once. If host->ops isn't initialized yet, its set to the
7313 * first non-dummy port ops.
7315 * LOCKING:
7316 * Inherited from calling layer (may sleep).
7318 * RETURNS:
7319 * 0 if all ports are started successfully, -errno otherwise.
7321 int ata_host_start(struct ata_host *host)
7323 int have_stop = 0;
7324 void *start_dr = NULL;
7325 int i, rc;
7327 if (host->flags & ATA_HOST_STARTED)
7328 return 0;
7330 for (i = 0; i < host->n_ports; i++) {
7331 struct ata_port *ap = host->ports[i];
7333 if (!host->ops && !ata_port_is_dummy(ap))
7334 host->ops = ap->ops;
7336 if (ap->ops->port_stop)
7337 have_stop = 1;
7340 if (host->ops->host_stop)
7341 have_stop = 1;
7343 if (have_stop) {
7344 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
7345 if (!start_dr)
7346 return -ENOMEM;
7349 for (i = 0; i < host->n_ports; i++) {
7350 struct ata_port *ap = host->ports[i];
7352 if (ap->ops->port_start) {
7353 rc = ap->ops->port_start(ap);
7354 if (rc) {
7355 if (rc != -ENODEV)
7356 dev_printk(KERN_ERR, host->dev,
7357 "failed to start port %d "
7358 "(errno=%d)\n", i, rc);
7359 goto err_out;
7362 ata_eh_freeze_port(ap);
7365 if (start_dr)
7366 devres_add(host->dev, start_dr);
7367 host->flags |= ATA_HOST_STARTED;
7368 return 0;
7370 err_out:
7371 while (--i >= 0) {
7372 struct ata_port *ap = host->ports[i];
7374 if (ap->ops->port_stop)
7375 ap->ops->port_stop(ap);
7377 devres_free(start_dr);
7378 return rc;
7382 * ata_sas_host_init - Initialize a host struct
7383 * @host: host to initialize
7384 * @dev: device host is attached to
7385 * @flags: host flags
7386 * @ops: port_ops
7388 * LOCKING:
7389 * PCI/etc. bus probe sem.
7392 /* KILLME - the only user left is ipr */
7393 void ata_host_init(struct ata_host *host, struct device *dev,
7394 unsigned long flags, const struct ata_port_operations *ops)
7396 spin_lock_init(&host->lock);
7397 host->dev = dev;
7398 host->flags = flags;
7399 host->ops = ops;
7403 * ata_host_register - register initialized ATA host
7404 * @host: ATA host to register
7405 * @sht: template for SCSI host
7407 * Register initialized ATA host. @host is allocated using
7408 * ata_host_alloc() and fully initialized by LLD. This function
7409 * starts ports, registers @host with ATA and SCSI layers and
7410 * probe registered devices.
7412 * LOCKING:
7413 * Inherited from calling layer (may sleep).
7415 * RETURNS:
7416 * 0 on success, -errno otherwise.
7418 int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
7420 int i, rc;
7422 /* host must have been started */
7423 if (!(host->flags & ATA_HOST_STARTED)) {
7424 dev_printk(KERN_ERR, host->dev,
7425 "BUG: trying to register unstarted host\n");
7426 WARN_ON(1);
7427 return -EINVAL;
7430 /* Blow away unused ports. This happens when LLD can't
7431 * determine the exact number of ports to allocate at
7432 * allocation time.
7434 for (i = host->n_ports; host->ports[i]; i++)
7435 kfree(host->ports[i]);
7437 /* give ports names and add SCSI hosts */
7438 for (i = 0; i < host->n_ports; i++)
7439 host->ports[i]->print_id = ata_print_id++;
7441 rc = ata_scsi_add_hosts(host, sht);
7442 if (rc)
7443 return rc;
7445 /* associate with ACPI nodes */
7446 ata_acpi_associate(host);
7448 /* set cable, sata_spd_limit and report */
7449 for (i = 0; i < host->n_ports; i++) {
7450 struct ata_port *ap = host->ports[i];
7451 unsigned long xfer_mask;
7453 /* set SATA cable type if still unset */
7454 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
7455 ap->cbl = ATA_CBL_SATA;
7457 /* init sata_spd_limit to the current value */
7458 sata_link_init_spd(&ap->link);
7460 /* print per-port info to dmesg */
7461 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
7462 ap->udma_mask);
7464 if (!ata_port_is_dummy(ap)) {
7465 ata_port_printk(ap, KERN_INFO,
7466 "%cATA max %s %s\n",
7467 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
7468 ata_mode_string(xfer_mask),
7469 ap->link.eh_info.desc);
7470 ata_ehi_clear_desc(&ap->link.eh_info);
7471 } else
7472 ata_port_printk(ap, KERN_INFO, "DUMMY\n");
7475 /* perform each probe synchronously */
7476 DPRINTK("probe begin\n");
7477 for (i = 0; i < host->n_ports; i++) {
7478 struct ata_port *ap = host->ports[i];
7480 /* probe */
7481 if (ap->ops->error_handler) {
7482 struct ata_eh_info *ehi = &ap->link.eh_info;
7483 unsigned long flags;
7485 ata_port_probe(ap);
7487 /* kick EH for boot probing */
7488 spin_lock_irqsave(ap->lock, flags);
7490 ehi->probe_mask =
7491 (1 << ata_link_max_devices(&ap->link)) - 1;
7492 ehi->action |= ATA_EH_SOFTRESET;
7493 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
7495 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
7496 ap->pflags |= ATA_PFLAG_LOADING;
7497 ata_port_schedule_eh(ap);
7499 spin_unlock_irqrestore(ap->lock, flags);
7501 /* wait for EH to finish */
7502 ata_port_wait_eh(ap);
7503 } else {
7504 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
7505 rc = ata_bus_probe(ap);
7506 DPRINTK("ata%u: bus probe end\n", ap->print_id);
7508 if (rc) {
7509 /* FIXME: do something useful here?
7510 * Current libata behavior will
7511 * tear down everything when
7512 * the module is removed
7513 * or the h/w is unplugged.
7519 /* probes are done, now scan each port's disk(s) */
7520 DPRINTK("host probe begin\n");
7521 for (i = 0; i < host->n_ports; i++) {
7522 struct ata_port *ap = host->ports[i];
7524 ata_scsi_scan_host(ap, 1);
7525 ata_lpm_schedule(ap, ap->pm_policy);
7528 return 0;
7532 * ata_host_activate - start host, request IRQ and register it
7533 * @host: target ATA host
7534 * @irq: IRQ to request
7535 * @irq_handler: irq_handler used when requesting IRQ
7536 * @irq_flags: irq_flags used when requesting IRQ
7537 * @sht: scsi_host_template to use when registering the host
7539 * After allocating an ATA host and initializing it, most libata
7540 * LLDs perform three steps to activate the host - start host,
7541 * request IRQ and register it. This helper takes necessasry
7542 * arguments and performs the three steps in one go.
7544 * An invalid IRQ skips the IRQ registration and expects the host to
7545 * have set polling mode on the port. In this case, @irq_handler
7546 * should be NULL.
7548 * LOCKING:
7549 * Inherited from calling layer (may sleep).
7551 * RETURNS:
7552 * 0 on success, -errno otherwise.
7554 int ata_host_activate(struct ata_host *host, int irq,
7555 irq_handler_t irq_handler, unsigned long irq_flags,
7556 struct scsi_host_template *sht)
7558 int i, rc;
7560 rc = ata_host_start(host);
7561 if (rc)
7562 return rc;
7564 /* Special case for polling mode */
7565 if (!irq) {
7566 WARN_ON(irq_handler);
7567 return ata_host_register(host, sht);
7570 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
7571 dev_driver_string(host->dev), host);
7572 if (rc)
7573 return rc;
7575 for (i = 0; i < host->n_ports; i++)
7576 ata_port_desc(host->ports[i], "irq %d", irq);
7578 rc = ata_host_register(host, sht);
7579 /* if failed, just free the IRQ and leave ports alone */
7580 if (rc)
7581 devm_free_irq(host->dev, irq, host);
7583 return rc;
7587 * ata_port_detach - Detach ATA port in prepration of device removal
7588 * @ap: ATA port to be detached
7590 * Detach all ATA devices and the associated SCSI devices of @ap;
7591 * then, remove the associated SCSI host. @ap is guaranteed to
7592 * be quiescent on return from this function.
7594 * LOCKING:
7595 * Kernel thread context (may sleep).
7597 static void ata_port_detach(struct ata_port *ap)
7599 unsigned long flags;
7600 struct ata_link *link;
7601 struct ata_device *dev;
7603 if (!ap->ops->error_handler)
7604 goto skip_eh;
7606 /* tell EH we're leaving & flush EH */
7607 spin_lock_irqsave(ap->lock, flags);
7608 ap->pflags |= ATA_PFLAG_UNLOADING;
7609 spin_unlock_irqrestore(ap->lock, flags);
7611 ata_port_wait_eh(ap);
7613 /* EH is now guaranteed to see UNLOADING - EH context belongs
7614 * to us. Disable all existing devices.
7616 ata_port_for_each_link(link, ap) {
7617 ata_link_for_each_dev(dev, link)
7618 ata_dev_disable(dev);
7621 /* Final freeze & EH. All in-flight commands are aborted. EH
7622 * will be skipped and retrials will be terminated with bad
7623 * target.
7625 spin_lock_irqsave(ap->lock, flags);
7626 ata_port_freeze(ap); /* won't be thawed */
7627 spin_unlock_irqrestore(ap->lock, flags);
7629 ata_port_wait_eh(ap);
7630 cancel_rearming_delayed_work(&ap->hotplug_task);
7632 skip_eh:
7633 /* remove the associated SCSI host */
7634 scsi_remove_host(ap->scsi_host);
7638 * ata_host_detach - Detach all ports of an ATA host
7639 * @host: Host to detach
7641 * Detach all ports of @host.
7643 * LOCKING:
7644 * Kernel thread context (may sleep).
7646 void ata_host_detach(struct ata_host *host)
7648 int i;
7650 for (i = 0; i < host->n_ports; i++)
7651 ata_port_detach(host->ports[i]);
7653 /* the host is dead now, dissociate ACPI */
7654 ata_acpi_dissociate(host);
7658 * ata_std_ports - initialize ioaddr with standard port offsets.
7659 * @ioaddr: IO address structure to be initialized
7661 * Utility function which initializes data_addr, error_addr,
7662 * feature_addr, nsect_addr, lbal_addr, lbam_addr, lbah_addr,
7663 * device_addr, status_addr, and command_addr to standard offsets
7664 * relative to cmd_addr.
7666 * Does not set ctl_addr, altstatus_addr, bmdma_addr, or scr_addr.
7669 void ata_std_ports(struct ata_ioports *ioaddr)
7671 ioaddr->data_addr = ioaddr->cmd_addr + ATA_REG_DATA;
7672 ioaddr->error_addr = ioaddr->cmd_addr + ATA_REG_ERR;
7673 ioaddr->feature_addr = ioaddr->cmd_addr + ATA_REG_FEATURE;
7674 ioaddr->nsect_addr = ioaddr->cmd_addr + ATA_REG_NSECT;
7675 ioaddr->lbal_addr = ioaddr->cmd_addr + ATA_REG_LBAL;
7676 ioaddr->lbam_addr = ioaddr->cmd_addr + ATA_REG_LBAM;
7677 ioaddr->lbah_addr = ioaddr->cmd_addr + ATA_REG_LBAH;
7678 ioaddr->device_addr = ioaddr->cmd_addr + ATA_REG_DEVICE;
7679 ioaddr->status_addr = ioaddr->cmd_addr + ATA_REG_STATUS;
7680 ioaddr->command_addr = ioaddr->cmd_addr + ATA_REG_CMD;
7684 #ifdef CONFIG_PCI
7687 * ata_pci_remove_one - PCI layer callback for device removal
7688 * @pdev: PCI device that was removed
7690 * PCI layer indicates to libata via this hook that hot-unplug or
7691 * module unload event has occurred. Detach all ports. Resource
7692 * release is handled via devres.
7694 * LOCKING:
7695 * Inherited from PCI layer (may sleep).
7697 void ata_pci_remove_one(struct pci_dev *pdev)
7699 struct device *dev = &pdev->dev;
7700 struct ata_host *host = dev_get_drvdata(dev);
7702 ata_host_detach(host);
7705 /* move to PCI subsystem */
7706 int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
7708 unsigned long tmp = 0;
7710 switch (bits->width) {
7711 case 1: {
7712 u8 tmp8 = 0;
7713 pci_read_config_byte(pdev, bits->reg, &tmp8);
7714 tmp = tmp8;
7715 break;
7717 case 2: {
7718 u16 tmp16 = 0;
7719 pci_read_config_word(pdev, bits->reg, &tmp16);
7720 tmp = tmp16;
7721 break;
7723 case 4: {
7724 u32 tmp32 = 0;
7725 pci_read_config_dword(pdev, bits->reg, &tmp32);
7726 tmp = tmp32;
7727 break;
7730 default:
7731 return -EINVAL;
7734 tmp &= bits->mask;
7736 return (tmp == bits->val) ? 1 : 0;
7739 #ifdef CONFIG_PM
7740 void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
7742 pci_save_state(pdev);
7743 pci_disable_device(pdev);
7745 <<<<<<< HEAD:drivers/ata/libata-core.c
7746 if (mesg.event == PM_EVENT_SUSPEND)
7747 =======
7748 if (mesg.event & PM_EVENT_SLEEP)
7749 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
7750 pci_set_power_state(pdev, PCI_D3hot);
7753 int ata_pci_device_do_resume(struct pci_dev *pdev)
7755 int rc;
7757 pci_set_power_state(pdev, PCI_D0);
7758 pci_restore_state(pdev);
7760 rc = pcim_enable_device(pdev);
7761 if (rc) {
7762 dev_printk(KERN_ERR, &pdev->dev,
7763 "failed to enable device after resume (%d)\n", rc);
7764 return rc;
7767 pci_set_master(pdev);
7768 return 0;
7771 int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
7773 struct ata_host *host = dev_get_drvdata(&pdev->dev);
7774 int rc = 0;
7776 rc = ata_host_suspend(host, mesg);
7777 if (rc)
7778 return rc;
7780 ata_pci_device_do_suspend(pdev, mesg);
7782 return 0;
7785 int ata_pci_device_resume(struct pci_dev *pdev)
7787 struct ata_host *host = dev_get_drvdata(&pdev->dev);
7788 int rc;
7790 rc = ata_pci_device_do_resume(pdev);
7791 if (rc == 0)
7792 ata_host_resume(host);
7793 return rc;
7795 #endif /* CONFIG_PM */
7797 #endif /* CONFIG_PCI */
7799 <<<<<<< HEAD:drivers/ata/libata-core.c
7800 =======
7801 static int __init ata_parse_force_one(char **cur,
7802 struct ata_force_ent *force_ent,
7803 const char **reason)
7805 /* FIXME: Currently, there's no way to tag init const data and
7806 * using __initdata causes build failure on some versions of
7807 * gcc. Once __initdataconst is implemented, add const to the
7808 * following structure.
7810 static struct ata_force_param force_tbl[] __initdata = {
7811 { "40c", .cbl = ATA_CBL_PATA40 },
7812 { "80c", .cbl = ATA_CBL_PATA80 },
7813 { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
7814 { "unk", .cbl = ATA_CBL_PATA_UNK },
7815 { "ign", .cbl = ATA_CBL_PATA_IGN },
7816 { "sata", .cbl = ATA_CBL_SATA },
7817 { "1.5Gbps", .spd_limit = 1 },
7818 { "3.0Gbps", .spd_limit = 2 },
7819 { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
7820 { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
7821 { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
7822 { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
7823 { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
7824 { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
7825 { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
7826 { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
7827 { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
7828 { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
7829 { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
7830 { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
7831 { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
7832 { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
7833 { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
7834 { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
7835 { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
7836 { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
7837 { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
7838 { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
7839 { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
7840 { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
7841 { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
7842 { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
7843 { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
7844 { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
7845 { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
7846 { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
7847 { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
7848 { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
7849 { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
7850 { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
7851 { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
7852 { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
7853 { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
7854 { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
7856 char *start = *cur, *p = *cur;
7857 char *id, *val, *endp;
7858 const struct ata_force_param *match_fp = NULL;
7859 int nr_matches = 0, i;
7861 /* find where this param ends and update *cur */
7862 while (*p != '\0' && *p != ',')
7863 p++;
7865 if (*p == '\0')
7866 *cur = p;
7867 else
7868 *cur = p + 1;
7870 *p = '\0';
7872 /* parse */
7873 p = strchr(start, ':');
7874 if (!p) {
7875 val = strstrip(start);
7876 goto parse_val;
7878 *p = '\0';
7880 id = strstrip(start);
7881 val = strstrip(p + 1);
7883 /* parse id */
7884 p = strchr(id, '.');
7885 if (p) {
7886 *p++ = '\0';
7887 force_ent->device = simple_strtoul(p, &endp, 10);
7888 if (p == endp || *endp != '\0') {
7889 *reason = "invalid device";
7890 return -EINVAL;
7894 force_ent->port = simple_strtoul(id, &endp, 10);
7895 if (p == endp || *endp != '\0') {
7896 *reason = "invalid port/link";
7897 return -EINVAL;
7900 parse_val:
7901 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
7902 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
7903 const struct ata_force_param *fp = &force_tbl[i];
7905 if (strncasecmp(val, fp->name, strlen(val)))
7906 continue;
7908 nr_matches++;
7909 match_fp = fp;
7911 if (strcasecmp(val, fp->name) == 0) {
7912 nr_matches = 1;
7913 break;
7917 if (!nr_matches) {
7918 *reason = "unknown value";
7919 return -EINVAL;
7921 if (nr_matches > 1) {
7922 *reason = "ambigious value";
7923 return -EINVAL;
7926 force_ent->param = *match_fp;
7928 return 0;
7931 static void __init ata_parse_force_param(void)
7933 int idx = 0, size = 1;
7934 int last_port = -1, last_device = -1;
7935 char *p, *cur, *next;
7937 /* calculate maximum number of params and allocate force_tbl */
7938 for (p = ata_force_param_buf; *p; p++)
7939 if (*p == ',')
7940 size++;
7942 ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
7943 if (!ata_force_tbl) {
7944 printk(KERN_WARNING "ata: failed to extend force table, "
7945 "libata.force ignored\n");
7946 return;
7949 /* parse and populate the table */
7950 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
7951 const char *reason = "";
7952 struct ata_force_ent te = { .port = -1, .device = -1 };
7954 next = cur;
7955 if (ata_parse_force_one(&next, &te, &reason)) {
7956 printk(KERN_WARNING "ata: failed to parse force "
7957 "parameter \"%s\" (%s)\n",
7958 cur, reason);
7959 continue;
7962 if (te.port == -1) {
7963 te.port = last_port;
7964 te.device = last_device;
7967 ata_force_tbl[idx++] = te;
7969 last_port = te.port;
7970 last_device = te.device;
7973 ata_force_tbl_size = idx;
7975 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
7977 static int __init ata_init(void)
7979 ata_probe_timeout *= HZ;
7980 <<<<<<< HEAD:drivers/ata/libata-core.c
7981 =======
7983 ata_parse_force_param();
7985 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
7986 ata_wq = create_workqueue("ata");
7987 if (!ata_wq)
7988 return -ENOMEM;
7990 ata_aux_wq = create_singlethread_workqueue("ata_aux");
7991 if (!ata_aux_wq) {
7992 destroy_workqueue(ata_wq);
7993 return -ENOMEM;
7996 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
7997 return 0;
8000 static void __exit ata_exit(void)
8002 <<<<<<< HEAD:drivers/ata/libata-core.c
8003 =======
8004 kfree(ata_force_tbl);
8005 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-core.c
8006 destroy_workqueue(ata_wq);
8007 destroy_workqueue(ata_aux_wq);
8010 subsys_initcall(ata_init);
8011 module_exit(ata_exit);
8013 static unsigned long ratelimit_time;
8014 static DEFINE_SPINLOCK(ata_ratelimit_lock);
8016 int ata_ratelimit(void)
8018 int rc;
8019 unsigned long flags;
8021 spin_lock_irqsave(&ata_ratelimit_lock, flags);
8023 if (time_after(jiffies, ratelimit_time)) {
8024 rc = 1;
8025 ratelimit_time = jiffies + (HZ/5);
8026 } else
8027 rc = 0;
8029 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
8031 return rc;
8035 * ata_wait_register - wait until register value changes
8036 * @reg: IO-mapped register
8037 * @mask: Mask to apply to read register value
8038 * @val: Wait condition
8039 * @interval_msec: polling interval in milliseconds
8040 * @timeout_msec: timeout in milliseconds
8042 * Waiting for some bits of register to change is a common
8043 * operation for ATA controllers. This function reads 32bit LE
8044 * IO-mapped register @reg and tests for the following condition.
8046 * (*@reg & mask) != val
8048 * If the condition is met, it returns; otherwise, the process is
8049 * repeated after @interval_msec until timeout.
8051 * LOCKING:
8052 * Kernel thread context (may sleep)
8054 * RETURNS:
8055 * The final register value.
8057 u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
8058 unsigned long interval_msec,
8059 unsigned long timeout_msec)
8061 unsigned long timeout;
8062 u32 tmp;
8064 tmp = ioread32(reg);
8066 /* Calculate timeout _after_ the first read to make sure
8067 * preceding writes reach the controller before starting to
8068 * eat away the timeout.
8070 timeout = jiffies + (timeout_msec * HZ) / 1000;
8072 while ((tmp & mask) == val && time_before(jiffies, timeout)) {
8073 msleep(interval_msec);
8074 tmp = ioread32(reg);
8077 return tmp;
8081 * Dummy port_ops
8083 static void ata_dummy_noret(struct ata_port *ap) { }
8084 static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
8085 static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
8087 static u8 ata_dummy_check_status(struct ata_port *ap)
8089 return ATA_DRDY;
8092 static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
8094 return AC_ERR_SYSTEM;
8097 const struct ata_port_operations ata_dummy_port_ops = {
8098 .check_status = ata_dummy_check_status,
8099 .check_altstatus = ata_dummy_check_status,
8100 .dev_select = ata_noop_dev_select,
8101 .qc_prep = ata_noop_qc_prep,
8102 .qc_issue = ata_dummy_qc_issue,
8103 .freeze = ata_dummy_noret,
8104 .thaw = ata_dummy_noret,
8105 .error_handler = ata_dummy_noret,
8106 .post_internal_cmd = ata_dummy_qc_noret,
8107 .irq_clear = ata_dummy_noret,
8108 .port_start = ata_dummy_ret0,
8109 .port_stop = ata_dummy_noret,
8112 const struct ata_port_info ata_dummy_port_info = {
8113 .port_ops = &ata_dummy_port_ops,
8117 * libata is essentially a library of internal helper functions for
8118 * low-level ATA host controller drivers. As such, the API/ABI is
8119 * likely to change as new drivers are added and updated.
8120 * Do not depend on ABI/API stability.
8122 EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
8123 EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
8124 EXPORT_SYMBOL_GPL(sata_deb_timing_long);
8125 EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
8126 EXPORT_SYMBOL_GPL(ata_dummy_port_info);
8127 EXPORT_SYMBOL_GPL(ata_std_bios_param);
8128 EXPORT_SYMBOL_GPL(ata_std_ports);
8129 EXPORT_SYMBOL_GPL(ata_host_init);
8130 EXPORT_SYMBOL_GPL(ata_host_alloc);
8131 EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
8132 EXPORT_SYMBOL_GPL(ata_host_start);
8133 EXPORT_SYMBOL_GPL(ata_host_register);
8134 EXPORT_SYMBOL_GPL(ata_host_activate);
8135 EXPORT_SYMBOL_GPL(ata_host_detach);
8136 EXPORT_SYMBOL_GPL(ata_sg_init);
8137 EXPORT_SYMBOL_GPL(ata_hsm_move);
8138 EXPORT_SYMBOL_GPL(ata_qc_complete);
8139 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
8140 EXPORT_SYMBOL_GPL(ata_qc_issue_prot);
8141 EXPORT_SYMBOL_GPL(ata_tf_load);
8142 EXPORT_SYMBOL_GPL(ata_tf_read);
8143 EXPORT_SYMBOL_GPL(ata_noop_dev_select);
8144 EXPORT_SYMBOL_GPL(ata_std_dev_select);
8145 EXPORT_SYMBOL_GPL(sata_print_link_status);
8146 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
8147 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
8148 EXPORT_SYMBOL_GPL(ata_pack_xfermask);
8149 EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
8150 EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
8151 EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
8152 EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
8153 EXPORT_SYMBOL_GPL(ata_mode_string);
8154 EXPORT_SYMBOL_GPL(ata_id_xfermask);
8155 EXPORT_SYMBOL_GPL(ata_check_status);
8156 EXPORT_SYMBOL_GPL(ata_altstatus);
8157 EXPORT_SYMBOL_GPL(ata_exec_command);
8158 EXPORT_SYMBOL_GPL(ata_port_start);
8159 EXPORT_SYMBOL_GPL(ata_sff_port_start);
8160 EXPORT_SYMBOL_GPL(ata_interrupt);
8161 EXPORT_SYMBOL_GPL(ata_do_set_mode);
8162 EXPORT_SYMBOL_GPL(ata_data_xfer);
8163 EXPORT_SYMBOL_GPL(ata_data_xfer_noirq);
8164 EXPORT_SYMBOL_GPL(ata_std_qc_defer);
8165 EXPORT_SYMBOL_GPL(ata_qc_prep);
8166 EXPORT_SYMBOL_GPL(ata_dumb_qc_prep);
8167 EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
8168 EXPORT_SYMBOL_GPL(ata_bmdma_setup);
8169 EXPORT_SYMBOL_GPL(ata_bmdma_start);
8170 EXPORT_SYMBOL_GPL(ata_bmdma_irq_clear);
8171 EXPORT_SYMBOL_GPL(ata_bmdma_status);
8172 EXPORT_SYMBOL_GPL(ata_bmdma_stop);
8173 EXPORT_SYMBOL_GPL(ata_bmdma_freeze);
8174 EXPORT_SYMBOL_GPL(ata_bmdma_thaw);
8175 EXPORT_SYMBOL_GPL(ata_bmdma_drive_eh);
8176 EXPORT_SYMBOL_GPL(ata_bmdma_error_handler);
8177 EXPORT_SYMBOL_GPL(ata_bmdma_post_internal_cmd);
8178 EXPORT_SYMBOL_GPL(ata_port_probe);
8179 EXPORT_SYMBOL_GPL(ata_dev_disable);
8180 EXPORT_SYMBOL_GPL(sata_set_spd);
8181 EXPORT_SYMBOL_GPL(sata_link_debounce);
8182 EXPORT_SYMBOL_GPL(sata_link_resume);
8183 EXPORT_SYMBOL_GPL(ata_bus_reset);
8184 EXPORT_SYMBOL_GPL(ata_std_prereset);
8185 EXPORT_SYMBOL_GPL(ata_std_softreset);
8186 EXPORT_SYMBOL_GPL(sata_link_hardreset);
8187 EXPORT_SYMBOL_GPL(sata_std_hardreset);
8188 EXPORT_SYMBOL_GPL(ata_std_postreset);
8189 EXPORT_SYMBOL_GPL(ata_dev_classify);
8190 EXPORT_SYMBOL_GPL(ata_dev_pair);
8191 EXPORT_SYMBOL_GPL(ata_port_disable);
8192 EXPORT_SYMBOL_GPL(ata_ratelimit);
8193 EXPORT_SYMBOL_GPL(ata_wait_register);
8194 EXPORT_SYMBOL_GPL(ata_busy_sleep);
8195 EXPORT_SYMBOL_GPL(ata_wait_after_reset);
8196 EXPORT_SYMBOL_GPL(ata_wait_ready);
8197 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
8198 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
8199 EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
8200 EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
8201 EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
8202 EXPORT_SYMBOL_GPL(ata_host_intr);
8203 EXPORT_SYMBOL_GPL(sata_scr_valid);
8204 EXPORT_SYMBOL_GPL(sata_scr_read);
8205 EXPORT_SYMBOL_GPL(sata_scr_write);
8206 EXPORT_SYMBOL_GPL(sata_scr_write_flush);
8207 EXPORT_SYMBOL_GPL(ata_link_online);
8208 EXPORT_SYMBOL_GPL(ata_link_offline);
8209 #ifdef CONFIG_PM
8210 EXPORT_SYMBOL_GPL(ata_host_suspend);
8211 EXPORT_SYMBOL_GPL(ata_host_resume);
8212 #endif /* CONFIG_PM */
8213 EXPORT_SYMBOL_GPL(ata_id_string);
8214 EXPORT_SYMBOL_GPL(ata_id_c_string);
8215 EXPORT_SYMBOL_GPL(ata_scsi_simulate);
8217 EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
8218 EXPORT_SYMBOL_GPL(ata_timing_find_mode);
8219 EXPORT_SYMBOL_GPL(ata_timing_compute);
8220 EXPORT_SYMBOL_GPL(ata_timing_merge);
8221 EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
8223 #ifdef CONFIG_PCI
8224 EXPORT_SYMBOL_GPL(pci_test_config_bits);
8225 EXPORT_SYMBOL_GPL(ata_pci_init_sff_host);
8226 EXPORT_SYMBOL_GPL(ata_pci_init_bmdma);
8227 EXPORT_SYMBOL_GPL(ata_pci_prepare_sff_host);
8228 EXPORT_SYMBOL_GPL(ata_pci_activate_sff_host);
8229 EXPORT_SYMBOL_GPL(ata_pci_init_one);
8230 EXPORT_SYMBOL_GPL(ata_pci_remove_one);
8231 #ifdef CONFIG_PM
8232 EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
8233 EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
8234 EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
8235 EXPORT_SYMBOL_GPL(ata_pci_device_resume);
8236 #endif /* CONFIG_PM */
8237 EXPORT_SYMBOL_GPL(ata_pci_default_filter);
8238 EXPORT_SYMBOL_GPL(ata_pci_clear_simplex);
8239 #endif /* CONFIG_PCI */
8241 EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch);
8242 EXPORT_SYMBOL_GPL(sata_pmp_std_prereset);
8243 EXPORT_SYMBOL_GPL(sata_pmp_std_hardreset);
8244 EXPORT_SYMBOL_GPL(sata_pmp_std_postreset);
8245 EXPORT_SYMBOL_GPL(sata_pmp_do_eh);
8247 EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
8248 EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
8249 EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
8250 EXPORT_SYMBOL_GPL(ata_port_desc);
8251 #ifdef CONFIG_PCI
8252 EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
8253 #endif /* CONFIG_PCI */
8254 EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
8255 EXPORT_SYMBOL_GPL(ata_link_abort);
8256 EXPORT_SYMBOL_GPL(ata_port_abort);
8257 EXPORT_SYMBOL_GPL(ata_port_freeze);
8258 EXPORT_SYMBOL_GPL(sata_async_notification);
8259 EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
8260 EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
8261 EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
8262 EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
8263 EXPORT_SYMBOL_GPL(ata_do_eh);
8264 EXPORT_SYMBOL_GPL(ata_irq_on);
8265 EXPORT_SYMBOL_GPL(ata_dev_try_classify);
8267 EXPORT_SYMBOL_GPL(ata_cable_40wire);
8268 EXPORT_SYMBOL_GPL(ata_cable_80wire);
8269 EXPORT_SYMBOL_GPL(ata_cable_unknown);
8270 EXPORT_SYMBOL_GPL(ata_cable_ignore);
8271 EXPORT_SYMBOL_GPL(ata_cable_sata);