]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/ata/libata-core.c
ata: SWNCQ should be enabled by default
[mirror_ubuntu-bionic-kernel.git] / drivers / ata / libata-core.c
CommitLineData
1da177e4 1/*
af36d7f0
JG
2 * libata-core.c - helper library for ATA
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
10 *
11 *
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.
16 *
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.
21 *
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.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from http://www.t13.org/ and
31 * http://www.sata-io.org/
32 *
92c52c52
AC
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)
40 *
1da177e4
LT
41 */
42
1da177e4
LT
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>
1da177e4
LT
49#include <linux/spinlock.h>
50#include <linux/blkdev.h>
51#include <linux/delay.h>
52#include <linux/timer.h>
53#include <linux/interrupt.h>
54#include <linux/completion.h>
55#include <linux/suspend.h>
56#include <linux/workqueue.h>
67846b30 57#include <linux/jiffies.h>
378f058c 58#include <linux/scatterlist.h>
2dcb407e 59#include <linux/io.h>
1da177e4 60#include <scsi/scsi.h>
193515d5 61#include <scsi/scsi_cmnd.h>
1da177e4
LT
62#include <scsi/scsi_host.h>
63#include <linux/libata.h>
1da177e4
LT
64#include <asm/semaphore.h>
65#include <asm/byteorder.h>
140b5e59 66#include <linux/cdrom.h>
1da177e4
LT
67
68#include "libata.h"
69
fda0efc5 70
d7bb4cc7 71/* debounce timing parameters in msecs { interval, duration, timeout } */
e9c83914
TH
72const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
73const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
74const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
d7bb4cc7 75
029cfd6b
TH
76const struct ata_port_operations ata_base_port_ops = {
77 .irq_clear = ata_noop_irq_clear,
a1efdaba
TH
78 .prereset = ata_std_prereset,
79 .hardreset = sata_std_hardreset,
80 .postreset = ata_std_postreset,
81 .error_handler = ata_std_error_handler,
029cfd6b
TH
82};
83
84const struct ata_port_operations sata_port_ops = {
85 .inherits = &ata_base_port_ops,
86
87 .qc_defer = ata_std_qc_defer,
88 .dev_select = ata_noop_dev_select,
89};
90
91const struct ata_port_operations sata_pmp_port_ops = {
92 .inherits = &sata_port_ops,
a1efdaba
TH
93
94 .pmp_prereset = sata_pmp_std_prereset,
95 .pmp_hardreset = sata_pmp_std_hardreset,
96 .pmp_postreset = sata_pmp_std_postreset,
97 .error_handler = sata_pmp_error_handler,
029cfd6b
TH
98};
99
3373efd8
TH
100static unsigned int ata_dev_init_params(struct ata_device *dev,
101 u16 heads, u16 sectors);
102static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
218f3d30
JG
103static unsigned int ata_dev_set_feature(struct ata_device *dev,
104 u8 enable, u8 feature);
3373efd8 105static void ata_dev_xfermask(struct ata_device *dev);
75683fe7 106static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
1da177e4 107
f3187195 108unsigned int ata_print_id = 1;
1da177e4
LT
109static struct workqueue_struct *ata_wq;
110
453b07ac
TH
111struct workqueue_struct *ata_aux_wq;
112
33267325
TH
113struct ata_force_param {
114 const char *name;
115 unsigned int cbl;
116 int spd_limit;
117 unsigned long xfer_mask;
118 unsigned int horkage_on;
119 unsigned int horkage_off;
120};
121
122struct ata_force_ent {
123 int port;
124 int device;
125 struct ata_force_param param;
126};
127
128static struct ata_force_ent *ata_force_tbl;
129static int ata_force_tbl_size;
130
131static char ata_force_param_buf[PAGE_SIZE] __initdata;
7afb4222
TH
132/* param_buf is thrown away after initialization, disallow read */
133module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
33267325
TH
134MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
135
418dc1f5 136int atapi_enabled = 1;
1623c81e
JG
137module_param(atapi_enabled, int, 0444);
138MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
139
c5c61bda 140static int atapi_dmadir = 0;
95de719a
AL
141module_param(atapi_dmadir, int, 0444);
142MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
143
baf4fdfa
ML
144int atapi_passthru16 = 1;
145module_param(atapi_passthru16, int, 0444);
146MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices; on by default (0=off, 1=on)");
147
c3c013a2
JG
148int libata_fua = 0;
149module_param_named(fua, libata_fua, int, 0444);
150MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
151
2dcb407e 152static int ata_ignore_hpa;
1e999736
AC
153module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
154MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
155
b3a70601
AC
156static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
157module_param_named(dma, libata_dma_mask, int, 0444);
158MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
159
a8601e5f
AM
160static int ata_probe_timeout = ATA_TMOUT_INTERNAL / HZ;
161module_param(ata_probe_timeout, int, 0444);
162MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
163
6ebe9d86 164int libata_noacpi = 0;
d7d0dad6 165module_param_named(noacpi, libata_noacpi, int, 0444);
6ebe9d86 166MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in probe/suspend/resume when set");
11ef697b 167
ae8d4ee7
AC
168int libata_allow_tpm = 0;
169module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
170MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands");
171
1da177e4
LT
172MODULE_AUTHOR("Jeff Garzik");
173MODULE_DESCRIPTION("Library module for ATA devices");
174MODULE_LICENSE("GPL");
175MODULE_VERSION(DRV_VERSION);
176
0baab86b 177
33267325
TH
178/**
179 * ata_force_cbl - force cable type according to libata.force
4cdfa1b3 180 * @ap: ATA port of interest
33267325
TH
181 *
182 * Force cable type according to libata.force and whine about it.
183 * The last entry which has matching port number is used, so it
184 * can be specified as part of device force parameters. For
185 * example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
186 * same effect.
187 *
188 * LOCKING:
189 * EH context.
190 */
191void ata_force_cbl(struct ata_port *ap)
192{
193 int i;
194
195 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
196 const struct ata_force_ent *fe = &ata_force_tbl[i];
197
198 if (fe->port != -1 && fe->port != ap->print_id)
199 continue;
200
201 if (fe->param.cbl == ATA_CBL_NONE)
202 continue;
203
204 ap->cbl = fe->param.cbl;
205 ata_port_printk(ap, KERN_NOTICE,
206 "FORCE: cable set to %s\n", fe->param.name);
207 return;
208 }
209}
210
211/**
212 * ata_force_spd_limit - force SATA spd limit according to libata.force
213 * @link: ATA link of interest
214 *
215 * Force SATA spd limit according to libata.force and whine about
216 * it. When only the port part is specified (e.g. 1:), the limit
217 * applies to all links connected to both the host link and all
218 * fan-out ports connected via PMP. If the device part is
219 * specified as 0 (e.g. 1.00:), it specifies the first fan-out
220 * link not the host link. Device number 15 always points to the
221 * host link whether PMP is attached or not.
222 *
223 * LOCKING:
224 * EH context.
225 */
226static void ata_force_spd_limit(struct ata_link *link)
227{
228 int linkno, i;
229
230 if (ata_is_host_link(link))
231 linkno = 15;
232 else
233 linkno = link->pmp;
234
235 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
236 const struct ata_force_ent *fe = &ata_force_tbl[i];
237
238 if (fe->port != -1 && fe->port != link->ap->print_id)
239 continue;
240
241 if (fe->device != -1 && fe->device != linkno)
242 continue;
243
244 if (!fe->param.spd_limit)
245 continue;
246
247 link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
248 ata_link_printk(link, KERN_NOTICE,
249 "FORCE: PHY spd limit set to %s\n", fe->param.name);
250 return;
251 }
252}
253
254/**
255 * ata_force_xfermask - force xfermask according to libata.force
256 * @dev: ATA device of interest
257 *
258 * Force xfer_mask according to libata.force and whine about it.
259 * For consistency with link selection, device number 15 selects
260 * the first device connected to the host link.
261 *
262 * LOCKING:
263 * EH context.
264 */
265static void ata_force_xfermask(struct ata_device *dev)
266{
267 int devno = dev->link->pmp + dev->devno;
268 int alt_devno = devno;
269 int i;
270
271 /* allow n.15 for the first device attached to host port */
272 if (ata_is_host_link(dev->link) && devno == 0)
273 alt_devno = 15;
274
275 for (i = ata_force_tbl_size - 1; i >= 0; i--) {
276 const struct ata_force_ent *fe = &ata_force_tbl[i];
277 unsigned long pio_mask, mwdma_mask, udma_mask;
278
279 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
280 continue;
281
282 if (fe->device != -1 && fe->device != devno &&
283 fe->device != alt_devno)
284 continue;
285
286 if (!fe->param.xfer_mask)
287 continue;
288
289 ata_unpack_xfermask(fe->param.xfer_mask,
290 &pio_mask, &mwdma_mask, &udma_mask);
291 if (udma_mask)
292 dev->udma_mask = udma_mask;
293 else if (mwdma_mask) {
294 dev->udma_mask = 0;
295 dev->mwdma_mask = mwdma_mask;
296 } else {
297 dev->udma_mask = 0;
298 dev->mwdma_mask = 0;
299 dev->pio_mask = pio_mask;
300 }
301
302 ata_dev_printk(dev, KERN_NOTICE,
303 "FORCE: xfer_mask set to %s\n", fe->param.name);
304 return;
305 }
306}
307
308/**
309 * ata_force_horkage - force horkage according to libata.force
310 * @dev: ATA device of interest
311 *
312 * Force horkage according to libata.force and whine about it.
313 * For consistency with link selection, device number 15 selects
314 * the first device connected to the host link.
315 *
316 * LOCKING:
317 * EH context.
318 */
319static void ata_force_horkage(struct ata_device *dev)
320{
321 int devno = dev->link->pmp + dev->devno;
322 int alt_devno = devno;
323 int i;
324
325 /* allow n.15 for the first device attached to host port */
326 if (ata_is_host_link(dev->link) && devno == 0)
327 alt_devno = 15;
328
329 for (i = 0; i < ata_force_tbl_size; i++) {
330 const struct ata_force_ent *fe = &ata_force_tbl[i];
331
332 if (fe->port != -1 && fe->port != dev->link->ap->print_id)
333 continue;
334
335 if (fe->device != -1 && fe->device != devno &&
336 fe->device != alt_devno)
337 continue;
338
339 if (!(~dev->horkage & fe->param.horkage_on) &&
340 !(dev->horkage & fe->param.horkage_off))
341 continue;
342
343 dev->horkage |= fe->param.horkage_on;
344 dev->horkage &= ~fe->param.horkage_off;
345
346 ata_dev_printk(dev, KERN_NOTICE,
347 "FORCE: horkage modified (%s)\n", fe->param.name);
348 }
349}
350
436d34b3
TH
351/**
352 * atapi_cmd_type - Determine ATAPI command type from SCSI opcode
353 * @opcode: SCSI opcode
354 *
355 * Determine ATAPI command type from @opcode.
356 *
357 * LOCKING:
358 * None.
359 *
360 * RETURNS:
361 * ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
362 */
363int atapi_cmd_type(u8 opcode)
364{
365 switch (opcode) {
366 case GPCMD_READ_10:
367 case GPCMD_READ_12:
368 return ATAPI_READ;
369
370 case GPCMD_WRITE_10:
371 case GPCMD_WRITE_12:
372 case GPCMD_WRITE_AND_VERIFY_10:
373 return ATAPI_WRITE;
374
375 case GPCMD_READ_CD:
376 case GPCMD_READ_CD_MSF:
377 return ATAPI_READ_CD;
378
e52dcc48
TH
379 case ATA_16:
380 case ATA_12:
381 if (atapi_passthru16)
382 return ATAPI_PASS_THRU;
383 /* fall thru */
436d34b3
TH
384 default:
385 return ATAPI_MISC;
386 }
387}
388
624d5c51
TH
389/**
390 * ata_noop_irq_clear - Noop placeholder for irq_clear
391 * @ap: Port associated with this ATA transaction.
392 */
393void ata_noop_irq_clear(struct ata_port *ap)
394{
395}
396
1da177e4
LT
397/**
398 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
399 * @tf: Taskfile to convert
1da177e4 400 * @pmp: Port multiplier port
9977126c
TH
401 * @is_cmd: This FIS is for command
402 * @fis: Buffer into which data will output
1da177e4
LT
403 *
404 * Converts a standard ATA taskfile to a Serial ATA
405 * FIS structure (Register - Host to Device).
406 *
407 * LOCKING:
408 * Inherited from caller.
409 */
9977126c 410void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
1da177e4 411{
9977126c
TH
412 fis[0] = 0x27; /* Register - Host to Device FIS */
413 fis[1] = pmp & 0xf; /* Port multiplier number*/
414 if (is_cmd)
415 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
416
1da177e4
LT
417 fis[2] = tf->command;
418 fis[3] = tf->feature;
419
420 fis[4] = tf->lbal;
421 fis[5] = tf->lbam;
422 fis[6] = tf->lbah;
423 fis[7] = tf->device;
424
425 fis[8] = tf->hob_lbal;
426 fis[9] = tf->hob_lbam;
427 fis[10] = tf->hob_lbah;
428 fis[11] = tf->hob_feature;
429
430 fis[12] = tf->nsect;
431 fis[13] = tf->hob_nsect;
432 fis[14] = 0;
433 fis[15] = tf->ctl;
434
435 fis[16] = 0;
436 fis[17] = 0;
437 fis[18] = 0;
438 fis[19] = 0;
439}
440
441/**
442 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
443 * @fis: Buffer from which data will be input
444 * @tf: Taskfile to output
445 *
e12a1be6 446 * Converts a serial ATA FIS structure to a standard ATA taskfile.
1da177e4
LT
447 *
448 * LOCKING:
449 * Inherited from caller.
450 */
451
057ace5e 452void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
1da177e4
LT
453{
454 tf->command = fis[2]; /* status */
455 tf->feature = fis[3]; /* error */
456
457 tf->lbal = fis[4];
458 tf->lbam = fis[5];
459 tf->lbah = fis[6];
460 tf->device = fis[7];
461
462 tf->hob_lbal = fis[8];
463 tf->hob_lbam = fis[9];
464 tf->hob_lbah = fis[10];
465
466 tf->nsect = fis[12];
467 tf->hob_nsect = fis[13];
468}
469
8cbd6df1
AL
470static const u8 ata_rw_cmds[] = {
471 /* pio multi */
472 ATA_CMD_READ_MULTI,
473 ATA_CMD_WRITE_MULTI,
474 ATA_CMD_READ_MULTI_EXT,
475 ATA_CMD_WRITE_MULTI_EXT,
9a3dccc4
TH
476 0,
477 0,
478 0,
479 ATA_CMD_WRITE_MULTI_FUA_EXT,
8cbd6df1
AL
480 /* pio */
481 ATA_CMD_PIO_READ,
482 ATA_CMD_PIO_WRITE,
483 ATA_CMD_PIO_READ_EXT,
484 ATA_CMD_PIO_WRITE_EXT,
9a3dccc4
TH
485 0,
486 0,
487 0,
488 0,
8cbd6df1
AL
489 /* dma */
490 ATA_CMD_READ,
491 ATA_CMD_WRITE,
492 ATA_CMD_READ_EXT,
9a3dccc4
TH
493 ATA_CMD_WRITE_EXT,
494 0,
495 0,
496 0,
497 ATA_CMD_WRITE_FUA_EXT
8cbd6df1 498};
1da177e4
LT
499
500/**
8cbd6df1 501 * ata_rwcmd_protocol - set taskfile r/w commands and protocol
bd056d7e
TH
502 * @tf: command to examine and configure
503 * @dev: device tf belongs to
1da177e4 504 *
2e9edbf8 505 * Examine the device configuration and tf->flags to calculate
8cbd6df1 506 * the proper read/write commands and protocol to use.
1da177e4
LT
507 *
508 * LOCKING:
509 * caller.
510 */
bd056d7e 511static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
1da177e4 512{
9a3dccc4 513 u8 cmd;
1da177e4 514
9a3dccc4 515 int index, fua, lba48, write;
2e9edbf8 516
9a3dccc4 517 fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
8cbd6df1
AL
518 lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
519 write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
1da177e4 520
8cbd6df1
AL
521 if (dev->flags & ATA_DFLAG_PIO) {
522 tf->protocol = ATA_PROT_PIO;
9a3dccc4 523 index = dev->multi_count ? 0 : 8;
9af5c9c9 524 } else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
8d238e01
AC
525 /* Unable to use DMA due to host limitation */
526 tf->protocol = ATA_PROT_PIO;
0565c26d 527 index = dev->multi_count ? 0 : 8;
8cbd6df1
AL
528 } else {
529 tf->protocol = ATA_PROT_DMA;
9a3dccc4 530 index = 16;
8cbd6df1 531 }
1da177e4 532
9a3dccc4
TH
533 cmd = ata_rw_cmds[index + fua + lba48 + write];
534 if (cmd) {
535 tf->command = cmd;
536 return 0;
537 }
538 return -1;
1da177e4
LT
539}
540
35b649fe
TH
541/**
542 * ata_tf_read_block - Read block address from ATA taskfile
543 * @tf: ATA taskfile of interest
544 * @dev: ATA device @tf belongs to
545 *
546 * LOCKING:
547 * None.
548 *
549 * Read block address from @tf. This function can handle all
550 * three address formats - LBA, LBA48 and CHS. tf->protocol and
551 * flags select the address format to use.
552 *
553 * RETURNS:
554 * Block address read from @tf.
555 */
556u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
557{
558 u64 block = 0;
559
560 if (tf->flags & ATA_TFLAG_LBA) {
561 if (tf->flags & ATA_TFLAG_LBA48) {
562 block |= (u64)tf->hob_lbah << 40;
563 block |= (u64)tf->hob_lbam << 32;
564 block |= tf->hob_lbal << 24;
565 } else
566 block |= (tf->device & 0xf) << 24;
567
568 block |= tf->lbah << 16;
569 block |= tf->lbam << 8;
570 block |= tf->lbal;
571 } else {
572 u32 cyl, head, sect;
573
574 cyl = tf->lbam | (tf->lbah << 8);
575 head = tf->device & 0xf;
576 sect = tf->lbal;
577
578 block = (cyl * dev->heads + head) * dev->sectors + sect;
579 }
580
581 return block;
582}
583
bd056d7e
TH
584/**
585 * ata_build_rw_tf - Build ATA taskfile for given read/write request
586 * @tf: Target ATA taskfile
587 * @dev: ATA device @tf belongs to
588 * @block: Block address
589 * @n_block: Number of blocks
590 * @tf_flags: RW/FUA etc...
591 * @tag: tag
592 *
593 * LOCKING:
594 * None.
595 *
596 * Build ATA taskfile @tf for read/write request described by
597 * @block, @n_block, @tf_flags and @tag on @dev.
598 *
599 * RETURNS:
600 *
601 * 0 on success, -ERANGE if the request is too large for @dev,
602 * -EINVAL if the request is invalid.
603 */
604int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
605 u64 block, u32 n_block, unsigned int tf_flags,
606 unsigned int tag)
607{
608 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
609 tf->flags |= tf_flags;
610
6d1245bf 611 if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
bd056d7e
TH
612 /* yay, NCQ */
613 if (!lba_48_ok(block, n_block))
614 return -ERANGE;
615
616 tf->protocol = ATA_PROT_NCQ;
617 tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
618
619 if (tf->flags & ATA_TFLAG_WRITE)
620 tf->command = ATA_CMD_FPDMA_WRITE;
621 else
622 tf->command = ATA_CMD_FPDMA_READ;
623
624 tf->nsect = tag << 3;
625 tf->hob_feature = (n_block >> 8) & 0xff;
626 tf->feature = n_block & 0xff;
627
628 tf->hob_lbah = (block >> 40) & 0xff;
629 tf->hob_lbam = (block >> 32) & 0xff;
630 tf->hob_lbal = (block >> 24) & 0xff;
631 tf->lbah = (block >> 16) & 0xff;
632 tf->lbam = (block >> 8) & 0xff;
633 tf->lbal = block & 0xff;
634
635 tf->device = 1 << 6;
636 if (tf->flags & ATA_TFLAG_FUA)
637 tf->device |= 1 << 7;
638 } else if (dev->flags & ATA_DFLAG_LBA) {
639 tf->flags |= ATA_TFLAG_LBA;
640
641 if (lba_28_ok(block, n_block)) {
642 /* use LBA28 */
643 tf->device |= (block >> 24) & 0xf;
644 } else if (lba_48_ok(block, n_block)) {
645 if (!(dev->flags & ATA_DFLAG_LBA48))
646 return -ERANGE;
647
648 /* use LBA48 */
649 tf->flags |= ATA_TFLAG_LBA48;
650
651 tf->hob_nsect = (n_block >> 8) & 0xff;
652
653 tf->hob_lbah = (block >> 40) & 0xff;
654 tf->hob_lbam = (block >> 32) & 0xff;
655 tf->hob_lbal = (block >> 24) & 0xff;
656 } else
657 /* request too large even for LBA48 */
658 return -ERANGE;
659
660 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
661 return -EINVAL;
662
663 tf->nsect = n_block & 0xff;
664
665 tf->lbah = (block >> 16) & 0xff;
666 tf->lbam = (block >> 8) & 0xff;
667 tf->lbal = block & 0xff;
668
669 tf->device |= ATA_LBA;
670 } else {
671 /* CHS */
672 u32 sect, head, cyl, track;
673
674 /* The request -may- be too large for CHS addressing. */
675 if (!lba_28_ok(block, n_block))
676 return -ERANGE;
677
678 if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
679 return -EINVAL;
680
681 /* Convert LBA to CHS */
682 track = (u32)block / dev->sectors;
683 cyl = track / dev->heads;
684 head = track % dev->heads;
685 sect = (u32)block % dev->sectors + 1;
686
687 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
688 (u32)block, track, cyl, head, sect);
689
690 /* Check whether the converted CHS can fit.
691 Cylinder: 0-65535
692 Head: 0-15
693 Sector: 1-255*/
694 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
695 return -ERANGE;
696
697 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
698 tf->lbal = sect;
699 tf->lbam = cyl;
700 tf->lbah = cyl >> 8;
701 tf->device |= head;
702 }
703
704 return 0;
705}
706
cb95d562
TH
707/**
708 * ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
709 * @pio_mask: pio_mask
710 * @mwdma_mask: mwdma_mask
711 * @udma_mask: udma_mask
712 *
713 * Pack @pio_mask, @mwdma_mask and @udma_mask into a single
714 * unsigned int xfer_mask.
715 *
716 * LOCKING:
717 * None.
718 *
719 * RETURNS:
720 * Packed xfer_mask.
721 */
7dc951ae
TH
722unsigned long ata_pack_xfermask(unsigned long pio_mask,
723 unsigned long mwdma_mask,
724 unsigned long udma_mask)
cb95d562
TH
725{
726 return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
727 ((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
728 ((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
729}
730
c0489e4e
TH
731/**
732 * ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
733 * @xfer_mask: xfer_mask to unpack
734 * @pio_mask: resulting pio_mask
735 * @mwdma_mask: resulting mwdma_mask
736 * @udma_mask: resulting udma_mask
737 *
738 * Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
739 * Any NULL distination masks will be ignored.
740 */
7dc951ae
TH
741void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
742 unsigned long *mwdma_mask, unsigned long *udma_mask)
c0489e4e
TH
743{
744 if (pio_mask)
745 *pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
746 if (mwdma_mask)
747 *mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
748 if (udma_mask)
749 *udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
750}
751
cb95d562 752static const struct ata_xfer_ent {
be9a50c8 753 int shift, bits;
cb95d562
TH
754 u8 base;
755} ata_xfer_tbl[] = {
70cd071e
TH
756 { ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
757 { ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
758 { ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
cb95d562
TH
759 { -1, },
760};
761
762/**
763 * ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
764 * @xfer_mask: xfer_mask of interest
765 *
766 * Return matching XFER_* value for @xfer_mask. Only the highest
767 * bit of @xfer_mask is considered.
768 *
769 * LOCKING:
770 * None.
771 *
772 * RETURNS:
70cd071e 773 * Matching XFER_* value, 0xff if no match found.
cb95d562 774 */
7dc951ae 775u8 ata_xfer_mask2mode(unsigned long xfer_mask)
cb95d562
TH
776{
777 int highbit = fls(xfer_mask) - 1;
778 const struct ata_xfer_ent *ent;
779
780 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
781 if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
782 return ent->base + highbit - ent->shift;
70cd071e 783 return 0xff;
cb95d562
TH
784}
785
786/**
787 * ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
788 * @xfer_mode: XFER_* of interest
789 *
790 * Return matching xfer_mask for @xfer_mode.
791 *
792 * LOCKING:
793 * None.
794 *
795 * RETURNS:
796 * Matching xfer_mask, 0 if no match found.
797 */
7dc951ae 798unsigned long ata_xfer_mode2mask(u8 xfer_mode)
cb95d562
TH
799{
800 const struct ata_xfer_ent *ent;
801
802 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
803 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
70cd071e
TH
804 return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
805 & ~((1 << ent->shift) - 1);
cb95d562
TH
806 return 0;
807}
808
809/**
810 * ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
811 * @xfer_mode: XFER_* of interest
812 *
813 * Return matching xfer_shift for @xfer_mode.
814 *
815 * LOCKING:
816 * None.
817 *
818 * RETURNS:
819 * Matching xfer_shift, -1 if no match found.
820 */
7dc951ae 821int ata_xfer_mode2shift(unsigned long xfer_mode)
cb95d562
TH
822{
823 const struct ata_xfer_ent *ent;
824
825 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
826 if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
827 return ent->shift;
828 return -1;
829}
830
1da177e4 831/**
1da7b0d0
TH
832 * ata_mode_string - convert xfer_mask to string
833 * @xfer_mask: mask of bits supported; only highest bit counts.
1da177e4
LT
834 *
835 * Determine string which represents the highest speed
1da7b0d0 836 * (highest bit in @modemask).
1da177e4
LT
837 *
838 * LOCKING:
839 * None.
840 *
841 * RETURNS:
842 * Constant C string representing highest speed listed in
1da7b0d0 843 * @mode_mask, or the constant C string "<n/a>".
1da177e4 844 */
7dc951ae 845const char *ata_mode_string(unsigned long xfer_mask)
1da177e4 846{
75f554bc
TH
847 static const char * const xfer_mode_str[] = {
848 "PIO0",
849 "PIO1",
850 "PIO2",
851 "PIO3",
852 "PIO4",
b352e57d
AC
853 "PIO5",
854 "PIO6",
75f554bc
TH
855 "MWDMA0",
856 "MWDMA1",
857 "MWDMA2",
b352e57d
AC
858 "MWDMA3",
859 "MWDMA4",
75f554bc
TH
860 "UDMA/16",
861 "UDMA/25",
862 "UDMA/33",
863 "UDMA/44",
864 "UDMA/66",
865 "UDMA/100",
866 "UDMA/133",
867 "UDMA7",
868 };
1da7b0d0 869 int highbit;
1da177e4 870
1da7b0d0
TH
871 highbit = fls(xfer_mask) - 1;
872 if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
873 return xfer_mode_str[highbit];
1da177e4 874 return "<n/a>";
1da177e4
LT
875}
876
4c360c81
TH
877static const char *sata_spd_string(unsigned int spd)
878{
879 static const char * const spd_str[] = {
880 "1.5 Gbps",
881 "3.0 Gbps",
882 };
883
884 if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
885 return "<unknown>";
886 return spd_str[spd - 1];
887}
888
3373efd8 889void ata_dev_disable(struct ata_device *dev)
0b8efb0a 890{
09d7f9b0 891 if (ata_dev_enabled(dev)) {
9af5c9c9 892 if (ata_msg_drv(dev->link->ap))
09d7f9b0 893 ata_dev_printk(dev, KERN_WARNING, "disabled\n");
562f0c2d 894 ata_acpi_on_disable(dev);
4ae72a1e
TH
895 ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
896 ATA_DNXFER_QUIET);
0b8efb0a
TH
897 dev->class++;
898 }
899}
900
ca77329f
KCA
901static int ata_dev_set_dipm(struct ata_device *dev, enum link_pm policy)
902{
903 struct ata_link *link = dev->link;
904 struct ata_port *ap = link->ap;
905 u32 scontrol;
906 unsigned int err_mask;
907 int rc;
908
909 /*
910 * disallow DIPM for drivers which haven't set
911 * ATA_FLAG_IPM. This is because when DIPM is enabled,
912 * phy ready will be set in the interrupt status on
913 * state changes, which will cause some drivers to
914 * think there are errors - additionally drivers will
915 * need to disable hot plug.
916 */
917 if (!(ap->flags & ATA_FLAG_IPM) || !ata_dev_enabled(dev)) {
918 ap->pm_policy = NOT_AVAILABLE;
919 return -EINVAL;
920 }
921
922 /*
923 * For DIPM, we will only enable it for the
924 * min_power setting.
925 *
926 * Why? Because Disks are too stupid to know that
927 * If the host rejects a request to go to SLUMBER
928 * they should retry at PARTIAL, and instead it
929 * just would give up. So, for medium_power to
930 * work at all, we need to only allow HIPM.
931 */
932 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
933 if (rc)
934 return rc;
935
936 switch (policy) {
937 case MIN_POWER:
938 /* no restrictions on IPM transitions */
939 scontrol &= ~(0x3 << 8);
940 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
941 if (rc)
942 return rc;
943
944 /* enable DIPM */
945 if (dev->flags & ATA_DFLAG_DIPM)
946 err_mask = ata_dev_set_feature(dev,
947 SETFEATURES_SATA_ENABLE, SATA_DIPM);
948 break;
949 case MEDIUM_POWER:
950 /* allow IPM to PARTIAL */
951 scontrol &= ~(0x1 << 8);
952 scontrol |= (0x2 << 8);
953 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
954 if (rc)
955 return rc;
956
f5456b63
KCA
957 /*
958 * we don't have to disable DIPM since IPM flags
959 * disallow transitions to SLUMBER, which effectively
960 * disable DIPM if it does not support PARTIAL
961 */
ca77329f
KCA
962 break;
963 case NOT_AVAILABLE:
964 case MAX_PERFORMANCE:
965 /* disable all IPM transitions */
966 scontrol |= (0x3 << 8);
967 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
968 if (rc)
969 return rc;
970
f5456b63
KCA
971 /*
972 * we don't have to disable DIPM since IPM flags
973 * disallow all transitions which effectively
974 * disable DIPM anyway.
975 */
ca77329f
KCA
976 break;
977 }
978
979 /* FIXME: handle SET FEATURES failure */
980 (void) err_mask;
981
982 return 0;
983}
984
985/**
986 * ata_dev_enable_pm - enable SATA interface power management
48166fd9
SH
987 * @dev: device to enable power management
988 * @policy: the link power management policy
ca77329f
KCA
989 *
990 * Enable SATA Interface power management. This will enable
991 * Device Interface Power Management (DIPM) for min_power
992 * policy, and then call driver specific callbacks for
993 * enabling Host Initiated Power management.
994 *
995 * Locking: Caller.
996 * Returns: -EINVAL if IPM is not supported, 0 otherwise.
997 */
998void ata_dev_enable_pm(struct ata_device *dev, enum link_pm policy)
999{
1000 int rc = 0;
1001 struct ata_port *ap = dev->link->ap;
1002
1003 /* set HIPM first, then DIPM */
1004 if (ap->ops->enable_pm)
1005 rc = ap->ops->enable_pm(ap, policy);
1006 if (rc)
1007 goto enable_pm_out;
1008 rc = ata_dev_set_dipm(dev, policy);
1009
1010enable_pm_out:
1011 if (rc)
1012 ap->pm_policy = MAX_PERFORMANCE;
1013 else
1014 ap->pm_policy = policy;
1015 return /* rc */; /* hopefully we can use 'rc' eventually */
1016}
1017
1992a5ed 1018#ifdef CONFIG_PM
ca77329f
KCA
1019/**
1020 * ata_dev_disable_pm - disable SATA interface power management
48166fd9 1021 * @dev: device to disable power management
ca77329f
KCA
1022 *
1023 * Disable SATA Interface power management. This will disable
1024 * Device Interface Power Management (DIPM) without changing
1025 * policy, call driver specific callbacks for disabling Host
1026 * Initiated Power management.
1027 *
1028 * Locking: Caller.
1029 * Returns: void
1030 */
1031static void ata_dev_disable_pm(struct ata_device *dev)
1032{
1033 struct ata_port *ap = dev->link->ap;
1034
1035 ata_dev_set_dipm(dev, MAX_PERFORMANCE);
1036 if (ap->ops->disable_pm)
1037 ap->ops->disable_pm(ap);
1038}
1992a5ed 1039#endif /* CONFIG_PM */
ca77329f
KCA
1040
1041void ata_lpm_schedule(struct ata_port *ap, enum link_pm policy)
1042{
1043 ap->pm_policy = policy;
3ec25ebd 1044 ap->link.eh_info.action |= ATA_EH_LPM;
ca77329f
KCA
1045 ap->link.eh_info.flags |= ATA_EHI_NO_AUTOPSY;
1046 ata_port_schedule_eh(ap);
1047}
1048
1992a5ed 1049#ifdef CONFIG_PM
ca77329f
KCA
1050static void ata_lpm_enable(struct ata_host *host)
1051{
1052 struct ata_link *link;
1053 struct ata_port *ap;
1054 struct ata_device *dev;
1055 int i;
1056
1057 for (i = 0; i < host->n_ports; i++) {
1058 ap = host->ports[i];
1059 ata_port_for_each_link(link, ap) {
1060 ata_link_for_each_dev(dev, link)
1061 ata_dev_disable_pm(dev);
1062 }
1063 }
1064}
1065
1066static void ata_lpm_disable(struct ata_host *host)
1067{
1068 int i;
1069
1070 for (i = 0; i < host->n_ports; i++) {
1071 struct ata_port *ap = host->ports[i];
1072 ata_lpm_schedule(ap, ap->pm_policy);
1073 }
1074}
1992a5ed 1075#endif /* CONFIG_PM */
ca77329f 1076
1da177e4
LT
1077/**
1078 * ata_dev_classify - determine device type based on ATA-spec signature
1079 * @tf: ATA taskfile register set for device to be identified
1080 *
1081 * Determine from taskfile register contents whether a device is
1082 * ATA or ATAPI, as per "Signature and persistence" section
1083 * of ATA/PI spec (volume 1, sect 5.14).
1084 *
1085 * LOCKING:
1086 * None.
1087 *
1088 * RETURNS:
633273a3
TH
1089 * Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
1090 * %ATA_DEV_UNKNOWN the event of failure.
1da177e4 1091 */
057ace5e 1092unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1da177e4
LT
1093{
1094 /* Apple's open source Darwin code hints that some devices only
1095 * put a proper signature into the LBA mid/high registers,
1096 * So, we only check those. It's sufficient for uniqueness.
633273a3
TH
1097 *
1098 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1099 * signatures for ATA and ATAPI devices attached on SerialATA,
1100 * 0x3c/0xc3 and 0x69/0x96 respectively. However, SerialATA
1101 * spec has never mentioned about using different signatures
1102 * for ATA/ATAPI devices. Then, Serial ATA II: Port
1103 * Multiplier specification began to use 0x69/0x96 to identify
1104 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1105 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1106 * 0x69/0x96 shortly and described them as reserved for
1107 * SerialATA.
1108 *
1109 * We follow the current spec and consider that 0x69/0x96
1110 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
1da177e4 1111 */
633273a3 1112 if ((tf->lbam == 0) && (tf->lbah == 0)) {
1da177e4
LT
1113 DPRINTK("found ATA device by sig\n");
1114 return ATA_DEV_ATA;
1115 }
1116
633273a3 1117 if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
1da177e4
LT
1118 DPRINTK("found ATAPI device by sig\n");
1119 return ATA_DEV_ATAPI;
1120 }
1121
633273a3
TH
1122 if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1123 DPRINTK("found PMP device by sig\n");
1124 return ATA_DEV_PMP;
1125 }
1126
1127 if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
2dcb407e 1128 printk(KERN_INFO "ata: SEMB device ignored\n");
633273a3
TH
1129 return ATA_DEV_SEMB_UNSUP; /* not yet */
1130 }
1131
1da177e4
LT
1132 DPRINTK("unknown device\n");
1133 return ATA_DEV_UNKNOWN;
1134}
1135
1da177e4 1136/**
6a62a04d 1137 * ata_id_string - Convert IDENTIFY DEVICE page into string
1da177e4
LT
1138 * @id: IDENTIFY DEVICE results we will examine
1139 * @s: string into which data is output
1140 * @ofs: offset into identify device page
1141 * @len: length of string to return. must be an even number.
1142 *
1143 * The strings in the IDENTIFY DEVICE page are broken up into
1144 * 16-bit chunks. Run through the string, and output each
1145 * 8-bit chunk linearly, regardless of platform.
1146 *
1147 * LOCKING:
1148 * caller.
1149 */
1150
6a62a04d
TH
1151void ata_id_string(const u16 *id, unsigned char *s,
1152 unsigned int ofs, unsigned int len)
1da177e4
LT
1153{
1154 unsigned int c;
1155
1156 while (len > 0) {
1157 c = id[ofs] >> 8;
1158 *s = c;
1159 s++;
1160
1161 c = id[ofs] & 0xff;
1162 *s = c;
1163 s++;
1164
1165 ofs++;
1166 len -= 2;
1167 }
1168}
1169
0e949ff3 1170/**
6a62a04d 1171 * ata_id_c_string - Convert IDENTIFY DEVICE page into C string
0e949ff3
TH
1172 * @id: IDENTIFY DEVICE results we will examine
1173 * @s: string into which data is output
1174 * @ofs: offset into identify device page
1175 * @len: length of string to return. must be an odd number.
1176 *
6a62a04d 1177 * This function is identical to ata_id_string except that it
0e949ff3
TH
1178 * trims trailing spaces and terminates the resulting string with
1179 * null. @len must be actual maximum length (even number) + 1.
1180 *
1181 * LOCKING:
1182 * caller.
1183 */
6a62a04d
TH
1184void ata_id_c_string(const u16 *id, unsigned char *s,
1185 unsigned int ofs, unsigned int len)
0e949ff3
TH
1186{
1187 unsigned char *p;
1188
1189 WARN_ON(!(len & 1));
1190
6a62a04d 1191 ata_id_string(id, s, ofs, len - 1);
0e949ff3
TH
1192
1193 p = s + strnlen(s, len - 1);
1194 while (p > s && p[-1] == ' ')
1195 p--;
1196 *p = '\0';
1197}
0baab86b 1198
db6f8759
TH
1199static u64 ata_id_n_sectors(const u16 *id)
1200{
1201 if (ata_id_has_lba(id)) {
1202 if (ata_id_has_lba48(id))
1203 return ata_id_u64(id, 100);
1204 else
1205 return ata_id_u32(id, 60);
1206 } else {
1207 if (ata_id_current_chs_valid(id))
1208 return ata_id_u32(id, 57);
1209 else
1210 return id[1] * id[3] * id[6];
1211 }
1212}
1213
1e999736
AC
1214static u64 ata_tf_to_lba48(struct ata_taskfile *tf)
1215{
1216 u64 sectors = 0;
1217
1218 sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
1219 sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1220 sectors |= (tf->hob_lbal & 0xff) << 24;
1221 sectors |= (tf->lbah & 0xff) << 16;
1222 sectors |= (tf->lbam & 0xff) << 8;
1223 sectors |= (tf->lbal & 0xff);
1224
1225 return ++sectors;
1226}
1227
1228static u64 ata_tf_to_lba(struct ata_taskfile *tf)
1229{
1230 u64 sectors = 0;
1231
1232 sectors |= (tf->device & 0x0f) << 24;
1233 sectors |= (tf->lbah & 0xff) << 16;
1234 sectors |= (tf->lbam & 0xff) << 8;
1235 sectors |= (tf->lbal & 0xff);
1236
1237 return ++sectors;
1238}
1239
1240/**
c728a914
TH
1241 * ata_read_native_max_address - Read native max address
1242 * @dev: target device
1243 * @max_sectors: out parameter for the result native max address
1e999736 1244 *
c728a914
TH
1245 * Perform an LBA48 or LBA28 native size query upon the device in
1246 * question.
1e999736 1247 *
c728a914
TH
1248 * RETURNS:
1249 * 0 on success, -EACCES if command is aborted by the drive.
1250 * -EIO on other errors.
1e999736 1251 */
c728a914 1252static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
1e999736 1253{
c728a914 1254 unsigned int err_mask;
1e999736 1255 struct ata_taskfile tf;
c728a914 1256 int lba48 = ata_id_has_lba48(dev->id);
1e999736
AC
1257
1258 ata_tf_init(dev, &tf);
1259
c728a914 1260 /* always clear all address registers */
1e999736 1261 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1e999736 1262
c728a914
TH
1263 if (lba48) {
1264 tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1265 tf.flags |= ATA_TFLAG_LBA48;
1266 } else
1267 tf.command = ATA_CMD_READ_NATIVE_MAX;
1e999736 1268
1e999736 1269 tf.protocol |= ATA_PROT_NODATA;
c728a914
TH
1270 tf.device |= ATA_LBA;
1271
2b789108 1272 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
c728a914
TH
1273 if (err_mask) {
1274 ata_dev_printk(dev, KERN_WARNING, "failed to read native "
1275 "max address (err_mask=0x%x)\n", err_mask);
1276 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1277 return -EACCES;
1278 return -EIO;
1279 }
1e999736 1280
c728a914
TH
1281 if (lba48)
1282 *max_sectors = ata_tf_to_lba48(&tf);
1283 else
1284 *max_sectors = ata_tf_to_lba(&tf);
2dcb407e 1285 if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
93328e11 1286 (*max_sectors)--;
c728a914 1287 return 0;
1e999736
AC
1288}
1289
1290/**
c728a914
TH
1291 * ata_set_max_sectors - Set max sectors
1292 * @dev: target device
6b38d1d1 1293 * @new_sectors: new max sectors value to set for the device
1e999736 1294 *
c728a914
TH
1295 * Set max sectors of @dev to @new_sectors.
1296 *
1297 * RETURNS:
1298 * 0 on success, -EACCES if command is aborted or denied (due to
1299 * previous non-volatile SET_MAX) by the drive. -EIO on other
1300 * errors.
1e999736 1301 */
05027adc 1302static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
1e999736 1303{
c728a914 1304 unsigned int err_mask;
1e999736 1305 struct ata_taskfile tf;
c728a914 1306 int lba48 = ata_id_has_lba48(dev->id);
1e999736
AC
1307
1308 new_sectors--;
1309
1310 ata_tf_init(dev, &tf);
1311
1e999736 1312 tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
c728a914
TH
1313
1314 if (lba48) {
1315 tf.command = ATA_CMD_SET_MAX_EXT;
1316 tf.flags |= ATA_TFLAG_LBA48;
1317
1318 tf.hob_lbal = (new_sectors >> 24) & 0xff;
1319 tf.hob_lbam = (new_sectors >> 32) & 0xff;
1320 tf.hob_lbah = (new_sectors >> 40) & 0xff;
1e582ba4 1321 } else {
c728a914
TH
1322 tf.command = ATA_CMD_SET_MAX;
1323
1e582ba4
TH
1324 tf.device |= (new_sectors >> 24) & 0xf;
1325 }
1326
1e999736 1327 tf.protocol |= ATA_PROT_NODATA;
c728a914 1328 tf.device |= ATA_LBA;
1e999736
AC
1329
1330 tf.lbal = (new_sectors >> 0) & 0xff;
1331 tf.lbam = (new_sectors >> 8) & 0xff;
1332 tf.lbah = (new_sectors >> 16) & 0xff;
1e999736 1333
2b789108 1334 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
c728a914
TH
1335 if (err_mask) {
1336 ata_dev_printk(dev, KERN_WARNING, "failed to set "
1337 "max address (err_mask=0x%x)\n", err_mask);
1338 if (err_mask == AC_ERR_DEV &&
1339 (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1340 return -EACCES;
1341 return -EIO;
1342 }
1343
c728a914 1344 return 0;
1e999736
AC
1345}
1346
1347/**
1348 * ata_hpa_resize - Resize a device with an HPA set
1349 * @dev: Device to resize
1350 *
1351 * Read the size of an LBA28 or LBA48 disk with HPA features and resize
1352 * it if required to the full size of the media. The caller must check
1353 * the drive has the HPA feature set enabled.
05027adc
TH
1354 *
1355 * RETURNS:
1356 * 0 on success, -errno on failure.
1e999736 1357 */
05027adc 1358static int ata_hpa_resize(struct ata_device *dev)
1e999736 1359{
05027adc
TH
1360 struct ata_eh_context *ehc = &dev->link->eh_context;
1361 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
1362 u64 sectors = ata_id_n_sectors(dev->id);
1363 u64 native_sectors;
c728a914 1364 int rc;
a617c09f 1365
05027adc
TH
1366 /* do we need to do it? */
1367 if (dev->class != ATA_DEV_ATA ||
1368 !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
1369 (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
c728a914 1370 return 0;
1e999736 1371
05027adc
TH
1372 /* read native max address */
1373 rc = ata_read_native_max_address(dev, &native_sectors);
1374 if (rc) {
dda7aba1
TH
1375 /* If device aborted the command or HPA isn't going to
1376 * be unlocked, skip HPA resizing.
05027adc 1377 */
dda7aba1 1378 if (rc == -EACCES || !ata_ignore_hpa) {
05027adc 1379 ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
dda7aba1 1380 "broken, skipping HPA handling\n");
05027adc
TH
1381 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1382
1383 /* we can continue if device aborted the command */
1384 if (rc == -EACCES)
1385 rc = 0;
1e999736 1386 }
37301a55 1387
05027adc
TH
1388 return rc;
1389 }
1390
1391 /* nothing to do? */
1392 if (native_sectors <= sectors || !ata_ignore_hpa) {
1393 if (!print_info || native_sectors == sectors)
1394 return 0;
1395
1396 if (native_sectors > sectors)
1397 ata_dev_printk(dev, KERN_INFO,
1398 "HPA detected: current %llu, native %llu\n",
1399 (unsigned long long)sectors,
1400 (unsigned long long)native_sectors);
1401 else if (native_sectors < sectors)
1402 ata_dev_printk(dev, KERN_WARNING,
1403 "native sectors (%llu) is smaller than "
1404 "sectors (%llu)\n",
1405 (unsigned long long)native_sectors,
1406 (unsigned long long)sectors);
1407 return 0;
1408 }
1409
1410 /* let's unlock HPA */
1411 rc = ata_set_max_sectors(dev, native_sectors);
1412 if (rc == -EACCES) {
1413 /* if device aborted the command, skip HPA resizing */
1414 ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
1415 "(%llu -> %llu), skipping HPA handling\n",
1416 (unsigned long long)sectors,
1417 (unsigned long long)native_sectors);
1418 dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
1419 return 0;
1420 } else if (rc)
1421 return rc;
1422
1423 /* re-read IDENTIFY data */
1424 rc = ata_dev_reread_id(dev, 0);
1425 if (rc) {
1426 ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
1427 "data after HPA resizing\n");
1428 return rc;
1429 }
1430
1431 if (print_info) {
1432 u64 new_sectors = ata_id_n_sectors(dev->id);
1433 ata_dev_printk(dev, KERN_INFO,
1434 "HPA unlocked: %llu -> %llu, native %llu\n",
1435 (unsigned long long)sectors,
1436 (unsigned long long)new_sectors,
1437 (unsigned long long)native_sectors);
1438 }
1439
1440 return 0;
1e999736
AC
1441}
1442
0baab86b
EF
1443/**
1444 * ata_noop_dev_select - Select device 0/1 on ATA bus
1445 * @ap: ATA channel to manipulate
1446 * @device: ATA device (numbered from zero) to select
1447 *
1448 * This function performs no actual function.
1449 *
1450 * May be used as the dev_select() entry in ata_port_operations.
1451 *
1452 * LOCKING:
1453 * caller.
1454 */
2dcb407e 1455void ata_noop_dev_select(struct ata_port *ap, unsigned int device)
1da177e4
LT
1456{
1457}
1458
1da177e4
LT
1459/**
1460 * ata_dump_id - IDENTIFY DEVICE info debugging output
0bd3300a 1461 * @id: IDENTIFY DEVICE page to dump
1da177e4 1462 *
0bd3300a
TH
1463 * Dump selected 16-bit words from the given IDENTIFY DEVICE
1464 * page.
1da177e4
LT
1465 *
1466 * LOCKING:
1467 * caller.
1468 */
1469
0bd3300a 1470static inline void ata_dump_id(const u16 *id)
1da177e4
LT
1471{
1472 DPRINTK("49==0x%04x "
1473 "53==0x%04x "
1474 "63==0x%04x "
1475 "64==0x%04x "
1476 "75==0x%04x \n",
0bd3300a
TH
1477 id[49],
1478 id[53],
1479 id[63],
1480 id[64],
1481 id[75]);
1da177e4
LT
1482 DPRINTK("80==0x%04x "
1483 "81==0x%04x "
1484 "82==0x%04x "
1485 "83==0x%04x "
1486 "84==0x%04x \n",
0bd3300a
TH
1487 id[80],
1488 id[81],
1489 id[82],
1490 id[83],
1491 id[84]);
1da177e4
LT
1492 DPRINTK("88==0x%04x "
1493 "93==0x%04x\n",
0bd3300a
TH
1494 id[88],
1495 id[93]);
1da177e4
LT
1496}
1497
cb95d562
TH
1498/**
1499 * ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1500 * @id: IDENTIFY data to compute xfer mask from
1501 *
1502 * Compute the xfermask for this device. This is not as trivial
1503 * as it seems if we must consider early devices correctly.
1504 *
1505 * FIXME: pre IDE drive timing (do we care ?).
1506 *
1507 * LOCKING:
1508 * None.
1509 *
1510 * RETURNS:
1511 * Computed xfermask
1512 */
7dc951ae 1513unsigned long ata_id_xfermask(const u16 *id)
cb95d562 1514{
7dc951ae 1515 unsigned long pio_mask, mwdma_mask, udma_mask;
cb95d562
TH
1516
1517 /* Usual case. Word 53 indicates word 64 is valid */
1518 if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1519 pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1520 pio_mask <<= 3;
1521 pio_mask |= 0x7;
1522 } else {
1523 /* If word 64 isn't valid then Word 51 high byte holds
1524 * the PIO timing number for the maximum. Turn it into
1525 * a mask.
1526 */
7a0f1c8a 1527 u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
46767aeb 1528 if (mode < 5) /* Valid PIO range */
2dcb407e 1529 pio_mask = (2 << mode) - 1;
46767aeb
AC
1530 else
1531 pio_mask = 1;
cb95d562
TH
1532
1533 /* But wait.. there's more. Design your standards by
1534 * committee and you too can get a free iordy field to
1535 * process. However its the speeds not the modes that
1536 * are supported... Note drivers using the timing API
1537 * will get this right anyway
1538 */
1539 }
1540
1541 mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
fb21f0d0 1542
b352e57d
AC
1543 if (ata_id_is_cfa(id)) {
1544 /*
1545 * Process compact flash extended modes
1546 */
1547 int pio = id[163] & 0x7;
1548 int dma = (id[163] >> 3) & 7;
1549
1550 if (pio)
1551 pio_mask |= (1 << 5);
1552 if (pio > 1)
1553 pio_mask |= (1 << 6);
1554 if (dma)
1555 mwdma_mask |= (1 << 3);
1556 if (dma > 1)
1557 mwdma_mask |= (1 << 4);
1558 }
1559
fb21f0d0
TH
1560 udma_mask = 0;
1561 if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1562 udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
cb95d562
TH
1563
1564 return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1565}
1566
86e45b6b 1567/**
442eacc3 1568 * ata_pio_queue_task - Queue port_task
86e45b6b 1569 * @ap: The ata_port to queue port_task for
e2a7f77a 1570 * @fn: workqueue function to be scheduled
65f27f38 1571 * @data: data for @fn to use
e2a7f77a 1572 * @delay: delay time for workqueue function
86e45b6b
TH
1573 *
1574 * Schedule @fn(@data) for execution after @delay jiffies using
1575 * port_task. There is one port_task per port and it's the
1576 * user(low level driver)'s responsibility to make sure that only
1577 * one task is active at any given time.
1578 *
1579 * libata core layer takes care of synchronization between
442eacc3 1580 * port_task and EH. ata_pio_queue_task() may be ignored for EH
86e45b6b
TH
1581 * synchronization.
1582 *
1583 * LOCKING:
1584 * Inherited from caller.
1585 */
624d5c51 1586void ata_pio_queue_task(struct ata_port *ap, void *data, unsigned long delay)
86e45b6b 1587{
65f27f38 1588 ap->port_task_data = data;
86e45b6b 1589
45a66c1c
ON
1590 /* may fail if ata_port_flush_task() in progress */
1591 queue_delayed_work(ata_wq, &ap->port_task, delay);
86e45b6b
TH
1592}
1593
1594/**
1595 * ata_port_flush_task - Flush port_task
1596 * @ap: The ata_port to flush port_task for
1597 *
1598 * After this function completes, port_task is guranteed not to
1599 * be running or scheduled.
1600 *
1601 * LOCKING:
1602 * Kernel thread context (may sleep)
1603 */
1604void ata_port_flush_task(struct ata_port *ap)
1605{
86e45b6b
TH
1606 DPRINTK("ENTER\n");
1607
45a66c1c 1608 cancel_rearming_delayed_work(&ap->port_task);
86e45b6b 1609
0dd4b21f 1610 if (ata_msg_ctl(ap))
7f5e4e8d 1611 ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __func__);
86e45b6b
TH
1612}
1613
7102d230 1614static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
a2a7a662 1615{
77853bf2 1616 struct completion *waiting = qc->private_data;
a2a7a662 1617
a2a7a662 1618 complete(waiting);
a2a7a662
TH
1619}
1620
1621/**
2432697b 1622 * ata_exec_internal_sg - execute libata internal command
a2a7a662
TH
1623 * @dev: Device to which the command is sent
1624 * @tf: Taskfile registers for the command and the result
d69cf37d 1625 * @cdb: CDB for packet command
a2a7a662 1626 * @dma_dir: Data tranfer direction of the command
5c1ad8b3 1627 * @sgl: sg list for the data buffer of the command
2432697b 1628 * @n_elem: Number of sg entries
2b789108 1629 * @timeout: Timeout in msecs (0 for default)
a2a7a662
TH
1630 *
1631 * Executes libata internal command with timeout. @tf contains
1632 * command on entry and result on return. Timeout and error
1633 * conditions are reported via return value. No recovery action
1634 * is taken after a command times out. It's caller's duty to
1635 * clean up after timeout.
1636 *
1637 * LOCKING:
1638 * None. Should be called with kernel context, might sleep.
551e8889
TH
1639 *
1640 * RETURNS:
1641 * Zero on success, AC_ERR_* mask on failure
a2a7a662 1642 */
2432697b
TH
1643unsigned ata_exec_internal_sg(struct ata_device *dev,
1644 struct ata_taskfile *tf, const u8 *cdb,
87260216 1645 int dma_dir, struct scatterlist *sgl,
2b789108 1646 unsigned int n_elem, unsigned long timeout)
a2a7a662 1647{
9af5c9c9
TH
1648 struct ata_link *link = dev->link;
1649 struct ata_port *ap = link->ap;
a2a7a662
TH
1650 u8 command = tf->command;
1651 struct ata_queued_cmd *qc;
2ab7db1f 1652 unsigned int tag, preempted_tag;
dedaf2b0 1653 u32 preempted_sactive, preempted_qc_active;
da917d69 1654 int preempted_nr_active_links;
60be6b9a 1655 DECLARE_COMPLETION_ONSTACK(wait);
a2a7a662 1656 unsigned long flags;
77853bf2 1657 unsigned int err_mask;
d95a717f 1658 int rc;
a2a7a662 1659
ba6a1308 1660 spin_lock_irqsave(ap->lock, flags);
a2a7a662 1661
e3180499 1662 /* no internal command while frozen */
b51e9e5d 1663 if (ap->pflags & ATA_PFLAG_FROZEN) {
ba6a1308 1664 spin_unlock_irqrestore(ap->lock, flags);
e3180499
TH
1665 return AC_ERR_SYSTEM;
1666 }
1667
2ab7db1f 1668 /* initialize internal qc */
a2a7a662 1669
2ab7db1f
TH
1670 /* XXX: Tag 0 is used for drivers with legacy EH as some
1671 * drivers choke if any other tag is given. This breaks
1672 * ata_tag_internal() test for those drivers. Don't use new
1673 * EH stuff without converting to it.
1674 */
1675 if (ap->ops->error_handler)
1676 tag = ATA_TAG_INTERNAL;
1677 else
1678 tag = 0;
1679
6cec4a39 1680 if (test_and_set_bit(tag, &ap->qc_allocated))
2ab7db1f 1681 BUG();
f69499f4 1682 qc = __ata_qc_from_tag(ap, tag);
2ab7db1f
TH
1683
1684 qc->tag = tag;
1685 qc->scsicmd = NULL;
1686 qc->ap = ap;
1687 qc->dev = dev;
1688 ata_qc_reinit(qc);
1689
9af5c9c9
TH
1690 preempted_tag = link->active_tag;
1691 preempted_sactive = link->sactive;
dedaf2b0 1692 preempted_qc_active = ap->qc_active;
da917d69 1693 preempted_nr_active_links = ap->nr_active_links;
9af5c9c9
TH
1694 link->active_tag = ATA_TAG_POISON;
1695 link->sactive = 0;
dedaf2b0 1696 ap->qc_active = 0;
da917d69 1697 ap->nr_active_links = 0;
2ab7db1f
TH
1698
1699 /* prepare & issue qc */
a2a7a662 1700 qc->tf = *tf;
d69cf37d
TH
1701 if (cdb)
1702 memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
e61e0672 1703 qc->flags |= ATA_QCFLAG_RESULT_TF;
a2a7a662
TH
1704 qc->dma_dir = dma_dir;
1705 if (dma_dir != DMA_NONE) {
2432697b 1706 unsigned int i, buflen = 0;
87260216 1707 struct scatterlist *sg;
2432697b 1708
87260216
JA
1709 for_each_sg(sgl, sg, n_elem, i)
1710 buflen += sg->length;
2432697b 1711
87260216 1712 ata_sg_init(qc, sgl, n_elem);
49c80429 1713 qc->nbytes = buflen;
a2a7a662
TH
1714 }
1715
77853bf2 1716 qc->private_data = &wait;
a2a7a662
TH
1717 qc->complete_fn = ata_qc_complete_internal;
1718
8e0e694a 1719 ata_qc_issue(qc);
a2a7a662 1720
ba6a1308 1721 spin_unlock_irqrestore(ap->lock, flags);
a2a7a662 1722
2b789108
TH
1723 if (!timeout)
1724 timeout = ata_probe_timeout * 1000 / HZ;
1725
1726 rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
d95a717f
TH
1727
1728 ata_port_flush_task(ap);
41ade50c 1729
d95a717f 1730 if (!rc) {
ba6a1308 1731 spin_lock_irqsave(ap->lock, flags);
a2a7a662
TH
1732
1733 /* We're racing with irq here. If we lose, the
1734 * following test prevents us from completing the qc
d95a717f
TH
1735 * twice. If we win, the port is frozen and will be
1736 * cleaned up by ->post_internal_cmd().
a2a7a662 1737 */
77853bf2 1738 if (qc->flags & ATA_QCFLAG_ACTIVE) {
d95a717f
TH
1739 qc->err_mask |= AC_ERR_TIMEOUT;
1740
1741 if (ap->ops->error_handler)
1742 ata_port_freeze(ap);
1743 else
1744 ata_qc_complete(qc);
f15a1daf 1745
0dd4b21f
BP
1746 if (ata_msg_warn(ap))
1747 ata_dev_printk(dev, KERN_WARNING,
88574551 1748 "qc timeout (cmd 0x%x)\n", command);
a2a7a662
TH
1749 }
1750
ba6a1308 1751 spin_unlock_irqrestore(ap->lock, flags);
a2a7a662
TH
1752 }
1753
d95a717f
TH
1754 /* do post_internal_cmd */
1755 if (ap->ops->post_internal_cmd)
1756 ap->ops->post_internal_cmd(qc);
1757
a51d644a
TH
1758 /* perform minimal error analysis */
1759 if (qc->flags & ATA_QCFLAG_FAILED) {
1760 if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1761 qc->err_mask |= AC_ERR_DEV;
1762
1763 if (!qc->err_mask)
1764 qc->err_mask |= AC_ERR_OTHER;
1765
1766 if (qc->err_mask & ~AC_ERR_OTHER)
1767 qc->err_mask &= ~AC_ERR_OTHER;
d95a717f
TH
1768 }
1769
15869303 1770 /* finish up */
ba6a1308 1771 spin_lock_irqsave(ap->lock, flags);
15869303 1772
e61e0672 1773 *tf = qc->result_tf;
77853bf2
TH
1774 err_mask = qc->err_mask;
1775
1776 ata_qc_free(qc);
9af5c9c9
TH
1777 link->active_tag = preempted_tag;
1778 link->sactive = preempted_sactive;
dedaf2b0 1779 ap->qc_active = preempted_qc_active;
da917d69 1780 ap->nr_active_links = preempted_nr_active_links;
77853bf2 1781
1f7dd3e9
TH
1782 /* XXX - Some LLDDs (sata_mv) disable port on command failure.
1783 * Until those drivers are fixed, we detect the condition
1784 * here, fail the command with AC_ERR_SYSTEM and reenable the
1785 * port.
1786 *
1787 * Note that this doesn't change any behavior as internal
1788 * command failure results in disabling the device in the
1789 * higher layer for LLDDs without new reset/EH callbacks.
1790 *
1791 * Kill the following code as soon as those drivers are fixed.
1792 */
198e0fed 1793 if (ap->flags & ATA_FLAG_DISABLED) {
1f7dd3e9
TH
1794 err_mask |= AC_ERR_SYSTEM;
1795 ata_port_probe(ap);
1796 }
1797
ba6a1308 1798 spin_unlock_irqrestore(ap->lock, flags);
15869303 1799
77853bf2 1800 return err_mask;
a2a7a662
TH
1801}
1802
2432697b 1803/**
33480a0e 1804 * ata_exec_internal - execute libata internal command
2432697b
TH
1805 * @dev: Device to which the command is sent
1806 * @tf: Taskfile registers for the command and the result
1807 * @cdb: CDB for packet command
1808 * @dma_dir: Data tranfer direction of the command
1809 * @buf: Data buffer of the command
1810 * @buflen: Length of data buffer
2b789108 1811 * @timeout: Timeout in msecs (0 for default)
2432697b
TH
1812 *
1813 * Wrapper around ata_exec_internal_sg() which takes simple
1814 * buffer instead of sg list.
1815 *
1816 * LOCKING:
1817 * None. Should be called with kernel context, might sleep.
1818 *
1819 * RETURNS:
1820 * Zero on success, AC_ERR_* mask on failure
1821 */
1822unsigned ata_exec_internal(struct ata_device *dev,
1823 struct ata_taskfile *tf, const u8 *cdb,
2b789108
TH
1824 int dma_dir, void *buf, unsigned int buflen,
1825 unsigned long timeout)
2432697b 1826{
33480a0e
TH
1827 struct scatterlist *psg = NULL, sg;
1828 unsigned int n_elem = 0;
2432697b 1829
33480a0e
TH
1830 if (dma_dir != DMA_NONE) {
1831 WARN_ON(!buf);
1832 sg_init_one(&sg, buf, buflen);
1833 psg = &sg;
1834 n_elem++;
1835 }
2432697b 1836
2b789108
TH
1837 return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
1838 timeout);
2432697b
TH
1839}
1840
977e6b9f
TH
1841/**
1842 * ata_do_simple_cmd - execute simple internal command
1843 * @dev: Device to which the command is sent
1844 * @cmd: Opcode to execute
1845 *
1846 * Execute a 'simple' command, that only consists of the opcode
1847 * 'cmd' itself, without filling any other registers
1848 *
1849 * LOCKING:
1850 * Kernel thread context (may sleep).
1851 *
1852 * RETURNS:
1853 * Zero on success, AC_ERR_* mask on failure
e58eb583 1854 */
77b08fb5 1855unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
e58eb583
TH
1856{
1857 struct ata_taskfile tf;
e58eb583
TH
1858
1859 ata_tf_init(dev, &tf);
1860
1861 tf.command = cmd;
1862 tf.flags |= ATA_TFLAG_DEVICE;
1863 tf.protocol = ATA_PROT_NODATA;
1864
2b789108 1865 return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
e58eb583
TH
1866}
1867
1bc4ccff
AC
1868/**
1869 * ata_pio_need_iordy - check if iordy needed
1870 * @adev: ATA device
1871 *
1872 * Check if the current speed of the device requires IORDY. Used
1873 * by various controllers for chip configuration.
1874 */
a617c09f 1875
1bc4ccff
AC
1876unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1877{
432729f0
AC
1878 /* Controller doesn't support IORDY. Probably a pointless check
1879 as the caller should know this */
9af5c9c9 1880 if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1bc4ccff 1881 return 0;
432729f0
AC
1882 /* PIO3 and higher it is mandatory */
1883 if (adev->pio_mode > XFER_PIO_2)
1884 return 1;
1885 /* We turn it on when possible */
1886 if (ata_id_has_iordy(adev->id))
1bc4ccff 1887 return 1;
432729f0
AC
1888 return 0;
1889}
2e9edbf8 1890
432729f0
AC
1891/**
1892 * ata_pio_mask_no_iordy - Return the non IORDY mask
1893 * @adev: ATA device
1894 *
1895 * Compute the highest mode possible if we are not using iordy. Return
1896 * -1 if no iordy mode is available.
1897 */
a617c09f 1898
432729f0
AC
1899static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1900{
1bc4ccff 1901 /* If we have no drive specific rule, then PIO 2 is non IORDY */
1bc4ccff 1902 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE */
432729f0 1903 u16 pio = adev->id[ATA_ID_EIDE_PIO];
1bc4ccff
AC
1904 /* Is the speed faster than the drive allows non IORDY ? */
1905 if (pio) {
1906 /* This is cycle times not frequency - watch the logic! */
1907 if (pio > 240) /* PIO2 is 240nS per cycle */
432729f0
AC
1908 return 3 << ATA_SHIFT_PIO;
1909 return 7 << ATA_SHIFT_PIO;
1bc4ccff
AC
1910 }
1911 }
432729f0 1912 return 3 << ATA_SHIFT_PIO;
1bc4ccff
AC
1913}
1914
1da177e4 1915/**
49016aca 1916 * ata_dev_read_id - Read ID data from the specified device
49016aca
TH
1917 * @dev: target device
1918 * @p_class: pointer to class of the target device (may be changed)
bff04647 1919 * @flags: ATA_READID_* flags
fe635c7e 1920 * @id: buffer to read IDENTIFY data into
1da177e4 1921 *
49016aca
TH
1922 * Read ID data from the specified device. ATA_CMD_ID_ATA is
1923 * performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
aec5c3c1
TH
1924 * devices. This function also issues ATA_CMD_INIT_DEV_PARAMS
1925 * for pre-ATA4 drives.
1da177e4 1926 *
50a99018 1927 * FIXME: ATA_CMD_ID_ATA is optional for early drives and right
2dcb407e 1928 * now we abort if we hit that case.
50a99018 1929 *
1da177e4 1930 * LOCKING:
49016aca
TH
1931 * Kernel thread context (may sleep)
1932 *
1933 * RETURNS:
1934 * 0 on success, -errno otherwise.
1da177e4 1935 */
a9beec95 1936int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
bff04647 1937 unsigned int flags, u16 *id)
1da177e4 1938{
9af5c9c9 1939 struct ata_port *ap = dev->link->ap;
49016aca 1940 unsigned int class = *p_class;
a0123703 1941 struct ata_taskfile tf;
49016aca
TH
1942 unsigned int err_mask = 0;
1943 const char *reason;
54936f8b 1944 int may_fallback = 1, tried_spinup = 0;
49016aca 1945 int rc;
1da177e4 1946
0dd4b21f 1947 if (ata_msg_ctl(ap))
7f5e4e8d 1948 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
1da177e4 1949
49016aca 1950 retry:
3373efd8 1951 ata_tf_init(dev, &tf);
a0123703 1952
49016aca
TH
1953 switch (class) {
1954 case ATA_DEV_ATA:
a0123703 1955 tf.command = ATA_CMD_ID_ATA;
49016aca
TH
1956 break;
1957 case ATA_DEV_ATAPI:
a0123703 1958 tf.command = ATA_CMD_ID_ATAPI;
49016aca
TH
1959 break;
1960 default:
1961 rc = -ENODEV;
1962 reason = "unsupported class";
1963 goto err_out;
1da177e4
LT
1964 }
1965
a0123703 1966 tf.protocol = ATA_PROT_PIO;
81afe893
TH
1967
1968 /* Some devices choke if TF registers contain garbage. Make
1969 * sure those are properly initialized.
1970 */
1971 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1972
1973 /* Device presence detection is unreliable on some
1974 * controllers. Always poll IDENTIFY if available.
1975 */
1976 tf.flags |= ATA_TFLAG_POLLING;
1da177e4 1977
3373efd8 1978 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
2b789108 1979 id, sizeof(id[0]) * ATA_ID_WORDS, 0);
a0123703 1980 if (err_mask) {
800b3996 1981 if (err_mask & AC_ERR_NODEV_HINT) {
1ffc151f
TH
1982 ata_dev_printk(dev, KERN_DEBUG,
1983 "NODEV after polling detection\n");
55a8e2c8
TH
1984 return -ENOENT;
1985 }
1986
1ffc151f
TH
1987 if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
1988 /* Device or controller might have reported
1989 * the wrong device class. Give a shot at the
1990 * other IDENTIFY if the current one is
1991 * aborted by the device.
1992 */
1993 if (may_fallback) {
1994 may_fallback = 0;
1995
1996 if (class == ATA_DEV_ATA)
1997 class = ATA_DEV_ATAPI;
1998 else
1999 class = ATA_DEV_ATA;
2000 goto retry;
2001 }
2002
2003 /* Control reaches here iff the device aborted
2004 * both flavors of IDENTIFYs which happens
2005 * sometimes with phantom devices.
2006 */
2007 ata_dev_printk(dev, KERN_DEBUG,
2008 "both IDENTIFYs aborted, assuming NODEV\n");
2009 return -ENOENT;
54936f8b
TH
2010 }
2011
49016aca
TH
2012 rc = -EIO;
2013 reason = "I/O error";
1da177e4
LT
2014 goto err_out;
2015 }
2016
54936f8b
TH
2017 /* Falling back doesn't make sense if ID data was read
2018 * successfully at least once.
2019 */
2020 may_fallback = 0;
2021
49016aca 2022 swap_buf_le16(id, ATA_ID_WORDS);
1da177e4 2023
49016aca 2024 /* sanity check */
a4f5749b 2025 rc = -EINVAL;
6070068b 2026 reason = "device reports invalid type";
a4f5749b
TH
2027
2028 if (class == ATA_DEV_ATA) {
2029 if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
2030 goto err_out;
2031 } else {
2032 if (ata_id_is_ata(id))
2033 goto err_out;
49016aca
TH
2034 }
2035
169439c2
ML
2036 if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
2037 tried_spinup = 1;
2038 /*
2039 * Drive powered-up in standby mode, and requires a specific
2040 * SET_FEATURES spin-up subcommand before it will accept
2041 * anything other than the original IDENTIFY command.
2042 */
218f3d30 2043 err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
fb0582f9 2044 if (err_mask && id[2] != 0x738c) {
169439c2
ML
2045 rc = -EIO;
2046 reason = "SPINUP failed";
2047 goto err_out;
2048 }
2049 /*
2050 * If the drive initially returned incomplete IDENTIFY info,
2051 * we now must reissue the IDENTIFY command.
2052 */
2053 if (id[2] == 0x37c8)
2054 goto retry;
2055 }
2056
bff04647 2057 if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
49016aca
TH
2058 /*
2059 * The exact sequence expected by certain pre-ATA4 drives is:
2060 * SRST RESET
50a99018
AC
2061 * IDENTIFY (optional in early ATA)
2062 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
49016aca
TH
2063 * anything else..
2064 * Some drives were very specific about that exact sequence.
50a99018
AC
2065 *
2066 * Note that ATA4 says lba is mandatory so the second check
2067 * shoud never trigger.
49016aca
TH
2068 */
2069 if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
3373efd8 2070 err_mask = ata_dev_init_params(dev, id[3], id[6]);
49016aca
TH
2071 if (err_mask) {
2072 rc = -EIO;
2073 reason = "INIT_DEV_PARAMS failed";
2074 goto err_out;
2075 }
2076
2077 /* current CHS translation info (id[53-58]) might be
2078 * changed. reread the identify device info.
2079 */
bff04647 2080 flags &= ~ATA_READID_POSTRESET;
49016aca
TH
2081 goto retry;
2082 }
2083 }
2084
2085 *p_class = class;
fe635c7e 2086
49016aca
TH
2087 return 0;
2088
2089 err_out:
88574551 2090 if (ata_msg_warn(ap))
0dd4b21f 2091 ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
88574551 2092 "(%s, err_mask=0x%x)\n", reason, err_mask);
49016aca
TH
2093 return rc;
2094}
2095
3373efd8 2096static inline u8 ata_dev_knobble(struct ata_device *dev)
4b2f3ede 2097{
9af5c9c9
TH
2098 struct ata_port *ap = dev->link->ap;
2099 return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
4b2f3ede
TH
2100}
2101
a6e6ce8e
TH
2102static void ata_dev_config_ncq(struct ata_device *dev,
2103 char *desc, size_t desc_sz)
2104{
9af5c9c9 2105 struct ata_port *ap = dev->link->ap;
a6e6ce8e
TH
2106 int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2107
2108 if (!ata_id_has_ncq(dev->id)) {
2109 desc[0] = '\0';
2110 return;
2111 }
75683fe7 2112 if (dev->horkage & ATA_HORKAGE_NONCQ) {
6919a0a6
AC
2113 snprintf(desc, desc_sz, "NCQ (not used)");
2114 return;
2115 }
a6e6ce8e 2116 if (ap->flags & ATA_FLAG_NCQ) {
cca3974e 2117 hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
a6e6ce8e
TH
2118 dev->flags |= ATA_DFLAG_NCQ;
2119 }
2120
2121 if (hdepth >= ddepth)
2122 snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
2123 else
2124 snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
2125}
2126
49016aca 2127/**
ffeae418 2128 * ata_dev_configure - Configure the specified ATA/ATAPI device
ffeae418
TH
2129 * @dev: Target device to configure
2130 *
2131 * Configure @dev according to @dev->id. Generic and low-level
2132 * driver specific fixups are also applied.
49016aca
TH
2133 *
2134 * LOCKING:
ffeae418
TH
2135 * Kernel thread context (may sleep)
2136 *
2137 * RETURNS:
2138 * 0 on success, -errno otherwise
49016aca 2139 */
efdaedc4 2140int ata_dev_configure(struct ata_device *dev)
49016aca 2141{
9af5c9c9
TH
2142 struct ata_port *ap = dev->link->ap;
2143 struct ata_eh_context *ehc = &dev->link->eh_context;
6746544c 2144 int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
1148c3a7 2145 const u16 *id = dev->id;
7dc951ae 2146 unsigned long xfer_mask;
b352e57d 2147 char revbuf[7]; /* XYZ-99\0 */
3f64f565
EM
2148 char fwrevbuf[ATA_ID_FW_REV_LEN+1];
2149 char modelbuf[ATA_ID_PROD_LEN+1];
e6d902a3 2150 int rc;
49016aca 2151
0dd4b21f 2152 if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
44877b4e 2153 ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
7f5e4e8d 2154 __func__);
ffeae418 2155 return 0;
49016aca
TH
2156 }
2157
0dd4b21f 2158 if (ata_msg_probe(ap))
7f5e4e8d 2159 ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
1da177e4 2160
75683fe7
TH
2161 /* set horkage */
2162 dev->horkage |= ata_dev_blacklisted(dev);
33267325 2163 ata_force_horkage(dev);
75683fe7 2164
6746544c
TH
2165 /* let ACPI work its magic */
2166 rc = ata_acpi_on_devcfg(dev);
2167 if (rc)
2168 return rc;
08573a86 2169
05027adc
TH
2170 /* massage HPA, do it early as it might change IDENTIFY data */
2171 rc = ata_hpa_resize(dev);
2172 if (rc)
2173 return rc;
2174
c39f5ebe 2175 /* print device capabilities */
0dd4b21f 2176 if (ata_msg_probe(ap))
88574551
TH
2177 ata_dev_printk(dev, KERN_DEBUG,
2178 "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2179 "85:%04x 86:%04x 87:%04x 88:%04x\n",
7f5e4e8d 2180 __func__,
f15a1daf
TH
2181 id[49], id[82], id[83], id[84],
2182 id[85], id[86], id[87], id[88]);
c39f5ebe 2183
208a9933 2184 /* initialize to-be-configured parameters */
ea1dd4e1 2185 dev->flags &= ~ATA_DFLAG_CFG_MASK;
208a9933
TH
2186 dev->max_sectors = 0;
2187 dev->cdb_len = 0;
2188 dev->n_sectors = 0;
2189 dev->cylinders = 0;
2190 dev->heads = 0;
2191 dev->sectors = 0;
2192
1da177e4
LT
2193 /*
2194 * common ATA, ATAPI feature tests
2195 */
2196
ff8854b2 2197 /* find max transfer mode; for printk only */
1148c3a7 2198 xfer_mask = ata_id_xfermask(id);
1da177e4 2199
0dd4b21f
BP
2200 if (ata_msg_probe(ap))
2201 ata_dump_id(id);
1da177e4 2202
ef143d57
AL
2203 /* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2204 ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2205 sizeof(fwrevbuf));
2206
2207 ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2208 sizeof(modelbuf));
2209
1da177e4
LT
2210 /* ATA-specific feature tests */
2211 if (dev->class == ATA_DEV_ATA) {
b352e57d
AC
2212 if (ata_id_is_cfa(id)) {
2213 if (id[162] & 1) /* CPRM may make this media unusable */
44877b4e
TH
2214 ata_dev_printk(dev, KERN_WARNING,
2215 "supports DRM functions and may "
2216 "not be fully accessable.\n");
b352e57d 2217 snprintf(revbuf, 7, "CFA");
ae8d4ee7 2218 } else {
2dcb407e 2219 snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
ae8d4ee7
AC
2220 /* Warn the user if the device has TPM extensions */
2221 if (ata_id_has_tpm(id))
2222 ata_dev_printk(dev, KERN_WARNING,
2223 "supports DRM functions and may "
2224 "not be fully accessable.\n");
2225 }
b352e57d 2226
1148c3a7 2227 dev->n_sectors = ata_id_n_sectors(id);
2940740b 2228
3f64f565
EM
2229 if (dev->id[59] & 0x100)
2230 dev->multi_count = dev->id[59] & 0xff;
2231
1148c3a7 2232 if (ata_id_has_lba(id)) {
4c2d721a 2233 const char *lba_desc;
a6e6ce8e 2234 char ncq_desc[20];
8bf62ece 2235
4c2d721a
TH
2236 lba_desc = "LBA";
2237 dev->flags |= ATA_DFLAG_LBA;
1148c3a7 2238 if (ata_id_has_lba48(id)) {
8bf62ece 2239 dev->flags |= ATA_DFLAG_LBA48;
4c2d721a 2240 lba_desc = "LBA48";
6fc49adb
TH
2241
2242 if (dev->n_sectors >= (1UL << 28) &&
2243 ata_id_has_flush_ext(id))
2244 dev->flags |= ATA_DFLAG_FLUSH_EXT;
4c2d721a 2245 }
8bf62ece 2246
a6e6ce8e
TH
2247 /* config NCQ */
2248 ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2249
8bf62ece 2250 /* print device info to dmesg */
3f64f565
EM
2251 if (ata_msg_drv(ap) && print_info) {
2252 ata_dev_printk(dev, KERN_INFO,
2253 "%s: %s, %s, max %s\n",
2254 revbuf, modelbuf, fwrevbuf,
2255 ata_mode_string(xfer_mask));
2256 ata_dev_printk(dev, KERN_INFO,
2257 "%Lu sectors, multi %u: %s %s\n",
f15a1daf 2258 (unsigned long long)dev->n_sectors,
3f64f565
EM
2259 dev->multi_count, lba_desc, ncq_desc);
2260 }
ffeae418 2261 } else {
8bf62ece
AL
2262 /* CHS */
2263
2264 /* Default translation */
1148c3a7
TH
2265 dev->cylinders = id[1];
2266 dev->heads = id[3];
2267 dev->sectors = id[6];
8bf62ece 2268
1148c3a7 2269 if (ata_id_current_chs_valid(id)) {
8bf62ece 2270 /* Current CHS translation is valid. */
1148c3a7
TH
2271 dev->cylinders = id[54];
2272 dev->heads = id[55];
2273 dev->sectors = id[56];
8bf62ece
AL
2274 }
2275
2276 /* print device info to dmesg */
3f64f565 2277 if (ata_msg_drv(ap) && print_info) {
88574551 2278 ata_dev_printk(dev, KERN_INFO,
3f64f565
EM
2279 "%s: %s, %s, max %s\n",
2280 revbuf, modelbuf, fwrevbuf,
2281 ata_mode_string(xfer_mask));
a84471fe 2282 ata_dev_printk(dev, KERN_INFO,
3f64f565
EM
2283 "%Lu sectors, multi %u, CHS %u/%u/%u\n",
2284 (unsigned long long)dev->n_sectors,
2285 dev->multi_count, dev->cylinders,
2286 dev->heads, dev->sectors);
2287 }
07f6f7d0
AL
2288 }
2289
6e7846e9 2290 dev->cdb_len = 16;
1da177e4
LT
2291 }
2292
2293 /* ATAPI-specific feature tests */
2c13b7ce 2294 else if (dev->class == ATA_DEV_ATAPI) {
854c73a2
TH
2295 const char *cdb_intr_string = "";
2296 const char *atapi_an_string = "";
91163006 2297 const char *dma_dir_string = "";
7d77b247 2298 u32 sntf;
08a556db 2299
1148c3a7 2300 rc = atapi_cdb_len(id);
1da177e4 2301 if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
0dd4b21f 2302 if (ata_msg_warn(ap))
88574551
TH
2303 ata_dev_printk(dev, KERN_WARNING,
2304 "unsupported CDB len\n");
ffeae418 2305 rc = -EINVAL;
1da177e4
LT
2306 goto err_out_nosup;
2307 }
6e7846e9 2308 dev->cdb_len = (unsigned int) rc;
1da177e4 2309
7d77b247
TH
2310 /* Enable ATAPI AN if both the host and device have
2311 * the support. If PMP is attached, SNTF is required
2312 * to enable ATAPI AN to discern between PHY status
2313 * changed notifications and ATAPI ANs.
9f45cbd3 2314 */
7d77b247
TH
2315 if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2316 (!ap->nr_pmp_links ||
2317 sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
854c73a2
TH
2318 unsigned int err_mask;
2319
9f45cbd3 2320 /* issue SET feature command to turn this on */
218f3d30
JG
2321 err_mask = ata_dev_set_feature(dev,
2322 SETFEATURES_SATA_ENABLE, SATA_AN);
854c73a2 2323 if (err_mask)
9f45cbd3 2324 ata_dev_printk(dev, KERN_ERR,
854c73a2
TH
2325 "failed to enable ATAPI AN "
2326 "(err_mask=0x%x)\n", err_mask);
2327 else {
9f45cbd3 2328 dev->flags |= ATA_DFLAG_AN;
854c73a2
TH
2329 atapi_an_string = ", ATAPI AN";
2330 }
9f45cbd3
KCA
2331 }
2332
08a556db 2333 if (ata_id_cdb_intr(dev->id)) {
312f7da2 2334 dev->flags |= ATA_DFLAG_CDB_INTR;
08a556db
AL
2335 cdb_intr_string = ", CDB intr";
2336 }
312f7da2 2337
91163006
TH
2338 if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
2339 dev->flags |= ATA_DFLAG_DMADIR;
2340 dma_dir_string = ", DMADIR";
2341 }
2342
1da177e4 2343 /* print device info to dmesg */
5afc8142 2344 if (ata_msg_drv(ap) && print_info)
ef143d57 2345 ata_dev_printk(dev, KERN_INFO,
91163006 2346 "ATAPI: %s, %s, max %s%s%s%s\n",
ef143d57 2347 modelbuf, fwrevbuf,
12436c30 2348 ata_mode_string(xfer_mask),
91163006
TH
2349 cdb_intr_string, atapi_an_string,
2350 dma_dir_string);
1da177e4
LT
2351 }
2352
914ed354
TH
2353 /* determine max_sectors */
2354 dev->max_sectors = ATA_MAX_SECTORS;
2355 if (dev->flags & ATA_DFLAG_LBA48)
2356 dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2357
ca77329f
KCA
2358 if (!(dev->horkage & ATA_HORKAGE_IPM)) {
2359 if (ata_id_has_hipm(dev->id))
2360 dev->flags |= ATA_DFLAG_HIPM;
2361 if (ata_id_has_dipm(dev->id))
2362 dev->flags |= ATA_DFLAG_DIPM;
2363 }
2364
c5038fc0
AC
2365 /* Limit PATA drive on SATA cable bridge transfers to udma5,
2366 200 sectors */
3373efd8 2367 if (ata_dev_knobble(dev)) {
5afc8142 2368 if (ata_msg_drv(ap) && print_info)
f15a1daf
TH
2369 ata_dev_printk(dev, KERN_INFO,
2370 "applying bridge limits\n");
5a529139 2371 dev->udma_mask &= ATA_UDMA5;
4b2f3ede
TH
2372 dev->max_sectors = ATA_MAX_SECTORS;
2373 }
2374
f8d8e579 2375 if ((dev->class == ATA_DEV_ATAPI) &&
f442cd86 2376 (atapi_command_packet_set(id) == TYPE_TAPE)) {
f8d8e579 2377 dev->max_sectors = ATA_MAX_SECTORS_TAPE;
f442cd86
AL
2378 dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2379 }
f8d8e579 2380
75683fe7 2381 if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
03ec52de
TH
2382 dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
2383 dev->max_sectors);
18d6e9d5 2384
ca77329f
KCA
2385 if (ata_dev_blacklisted(dev) & ATA_HORKAGE_IPM) {
2386 dev->horkage |= ATA_HORKAGE_IPM;
2387
2388 /* reset link pm_policy for this port to no pm */
2389 ap->pm_policy = MAX_PERFORMANCE;
2390 }
2391
4b2f3ede 2392 if (ap->ops->dev_config)
cd0d3bbc 2393 ap->ops->dev_config(dev);
4b2f3ede 2394
c5038fc0
AC
2395 if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2396 /* Let the user know. We don't want to disallow opens for
2397 rescue purposes, or in case the vendor is just a blithering
2398 idiot. Do this after the dev_config call as some controllers
2399 with buggy firmware may want to avoid reporting false device
2400 bugs */
2401
2402 if (print_info) {
2403 ata_dev_printk(dev, KERN_WARNING,
2404"Drive reports diagnostics failure. This may indicate a drive\n");
2405 ata_dev_printk(dev, KERN_WARNING,
2406"fault or invalid emulation. Contact drive vendor for information.\n");
2407 }
2408 }
2409
ffeae418 2410 return 0;
1da177e4
LT
2411
2412err_out_nosup:
0dd4b21f 2413 if (ata_msg_probe(ap))
88574551 2414 ata_dev_printk(dev, KERN_DEBUG,
7f5e4e8d 2415 "%s: EXIT, err\n", __func__);
ffeae418 2416 return rc;
1da177e4
LT
2417}
2418
be0d18df 2419/**
2e41e8e6 2420 * ata_cable_40wire - return 40 wire cable type
be0d18df
AC
2421 * @ap: port
2422 *
2e41e8e6 2423 * Helper method for drivers which want to hardwire 40 wire cable
be0d18df
AC
2424 * detection.
2425 */
2426
2427int ata_cable_40wire(struct ata_port *ap)
2428{
2429 return ATA_CBL_PATA40;
2430}
2431
2432/**
2e41e8e6 2433 * ata_cable_80wire - return 80 wire cable type
be0d18df
AC
2434 * @ap: port
2435 *
2e41e8e6 2436 * Helper method for drivers which want to hardwire 80 wire cable
be0d18df
AC
2437 * detection.
2438 */
2439
2440int ata_cable_80wire(struct ata_port *ap)
2441{
2442 return ATA_CBL_PATA80;
2443}
2444
2445/**
2446 * ata_cable_unknown - return unknown PATA cable.
2447 * @ap: port
2448 *
2449 * Helper method for drivers which have no PATA cable detection.
2450 */
2451
2452int ata_cable_unknown(struct ata_port *ap)
2453{
2454 return ATA_CBL_PATA_UNK;
2455}
2456
c88f90c3
TH
2457/**
2458 * ata_cable_ignore - return ignored PATA cable.
2459 * @ap: port
2460 *
2461 * Helper method for drivers which don't use cable type to limit
2462 * transfer mode.
2463 */
2464int ata_cable_ignore(struct ata_port *ap)
2465{
2466 return ATA_CBL_PATA_IGN;
2467}
2468
be0d18df
AC
2469/**
2470 * ata_cable_sata - return SATA cable type
2471 * @ap: port
2472 *
2473 * Helper method for drivers which have SATA cables
2474 */
2475
2476int ata_cable_sata(struct ata_port *ap)
2477{
2478 return ATA_CBL_SATA;
2479}
2480
1da177e4
LT
2481/**
2482 * ata_bus_probe - Reset and probe ATA bus
2483 * @ap: Bus to probe
2484 *
0cba632b
JG
2485 * Master ATA bus probing function. Initiates a hardware-dependent
2486 * bus reset, then attempts to identify any devices found on
2487 * the bus.
2488 *
1da177e4 2489 * LOCKING:
0cba632b 2490 * PCI/etc. bus probe sem.
1da177e4
LT
2491 *
2492 * RETURNS:
96072e69 2493 * Zero on success, negative errno otherwise.
1da177e4
LT
2494 */
2495
80289167 2496int ata_bus_probe(struct ata_port *ap)
1da177e4 2497{
28ca5c57 2498 unsigned int classes[ATA_MAX_DEVICES];
14d2bac1 2499 int tries[ATA_MAX_DEVICES];
f58229f8 2500 int rc;
e82cbdb9 2501 struct ata_device *dev;
1da177e4 2502
28ca5c57 2503 ata_port_probe(ap);
c19ba8af 2504
f58229f8
TH
2505 ata_link_for_each_dev(dev, &ap->link)
2506 tries[dev->devno] = ATA_PROBE_MAX_TRIES;
14d2bac1
TH
2507
2508 retry:
cdeab114
TH
2509 ata_link_for_each_dev(dev, &ap->link) {
2510 /* If we issue an SRST then an ATA drive (not ATAPI)
2511 * may change configuration and be in PIO0 timing. If
2512 * we do a hard reset (or are coming from power on)
2513 * this is true for ATA or ATAPI. Until we've set a
2514 * suitable controller mode we should not touch the
2515 * bus as we may be talking too fast.
2516 */
2517 dev->pio_mode = XFER_PIO_0;
2518
2519 /* If the controller has a pio mode setup function
2520 * then use it to set the chipset to rights. Don't
2521 * touch the DMA setup as that will be dealt with when
2522 * configuring devices.
2523 */
2524 if (ap->ops->set_piomode)
2525 ap->ops->set_piomode(ap, dev);
2526 }
2527
2044470c 2528 /* reset and determine device classes */
52783c5d 2529 ap->ops->phy_reset(ap);
2061a47a 2530
f58229f8 2531 ata_link_for_each_dev(dev, &ap->link) {
52783c5d
TH
2532 if (!(ap->flags & ATA_FLAG_DISABLED) &&
2533 dev->class != ATA_DEV_UNKNOWN)
2534 classes[dev->devno] = dev->class;
2535 else
2536 classes[dev->devno] = ATA_DEV_NONE;
2044470c 2537
52783c5d 2538 dev->class = ATA_DEV_UNKNOWN;
28ca5c57 2539 }
1da177e4 2540
52783c5d 2541 ata_port_probe(ap);
2044470c 2542
f31f0cc2
JG
2543 /* read IDENTIFY page and configure devices. We have to do the identify
2544 specific sequence bass-ackwards so that PDIAG- is released by
2545 the slave device */
2546
a4ba7fe2 2547 ata_link_for_each_dev_reverse(dev, &ap->link) {
f58229f8
TH
2548 if (tries[dev->devno])
2549 dev->class = classes[dev->devno];
ffeae418 2550
14d2bac1 2551 if (!ata_dev_enabled(dev))
ffeae418 2552 continue;
ffeae418 2553
bff04647
TH
2554 rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2555 dev->id);
14d2bac1
TH
2556 if (rc)
2557 goto fail;
f31f0cc2
JG
2558 }
2559
be0d18df
AC
2560 /* Now ask for the cable type as PDIAG- should have been released */
2561 if (ap->ops->cable_detect)
2562 ap->cbl = ap->ops->cable_detect(ap);
2563
614fe29b
AC
2564 /* We may have SATA bridge glue hiding here irrespective of the
2565 reported cable types and sensed types */
2566 ata_link_for_each_dev(dev, &ap->link) {
2567 if (!ata_dev_enabled(dev))
2568 continue;
2569 /* SATA drives indicate we have a bridge. We don't know which
2570 end of the link the bridge is which is a problem */
2571 if (ata_id_is_sata(dev->id))
2572 ap->cbl = ATA_CBL_SATA;
2573 }
2574
f31f0cc2
JG
2575 /* After the identify sequence we can now set up the devices. We do
2576 this in the normal order so that the user doesn't get confused */
2577
f58229f8 2578 ata_link_for_each_dev(dev, &ap->link) {
f31f0cc2
JG
2579 if (!ata_dev_enabled(dev))
2580 continue;
14d2bac1 2581
9af5c9c9 2582 ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
efdaedc4 2583 rc = ata_dev_configure(dev);
9af5c9c9 2584 ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
14d2bac1
TH
2585 if (rc)
2586 goto fail;
1da177e4
LT
2587 }
2588
e82cbdb9 2589 /* configure transfer mode */
0260731f 2590 rc = ata_set_mode(&ap->link, &dev);
4ae72a1e 2591 if (rc)
51713d35 2592 goto fail;
1da177e4 2593
f58229f8
TH
2594 ata_link_for_each_dev(dev, &ap->link)
2595 if (ata_dev_enabled(dev))
e82cbdb9 2596 return 0;
1da177e4 2597
e82cbdb9
TH
2598 /* no device present, disable port */
2599 ata_port_disable(ap);
96072e69 2600 return -ENODEV;
14d2bac1
TH
2601
2602 fail:
4ae72a1e
TH
2603 tries[dev->devno]--;
2604
14d2bac1
TH
2605 switch (rc) {
2606 case -EINVAL:
4ae72a1e 2607 /* eeek, something went very wrong, give up */
14d2bac1
TH
2608 tries[dev->devno] = 0;
2609 break;
4ae72a1e
TH
2610
2611 case -ENODEV:
2612 /* give it just one more chance */
2613 tries[dev->devno] = min(tries[dev->devno], 1);
14d2bac1 2614 case -EIO:
4ae72a1e
TH
2615 if (tries[dev->devno] == 1) {
2616 /* This is the last chance, better to slow
2617 * down than lose it.
2618 */
936fd732 2619 sata_down_spd_limit(&ap->link);
4ae72a1e
TH
2620 ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
2621 }
14d2bac1
TH
2622 }
2623
4ae72a1e 2624 if (!tries[dev->devno])
3373efd8 2625 ata_dev_disable(dev);
ec573755 2626
14d2bac1 2627 goto retry;
1da177e4
LT
2628}
2629
2630/**
0cba632b
JG
2631 * ata_port_probe - Mark port as enabled
2632 * @ap: Port for which we indicate enablement
1da177e4 2633 *
0cba632b
JG
2634 * Modify @ap data structure such that the system
2635 * thinks that the entire port is enabled.
2636 *
cca3974e 2637 * LOCKING: host lock, or some other form of
0cba632b 2638 * serialization.
1da177e4
LT
2639 */
2640
2641void ata_port_probe(struct ata_port *ap)
2642{
198e0fed 2643 ap->flags &= ~ATA_FLAG_DISABLED;
1da177e4
LT
2644}
2645
3be680b7
TH
2646/**
2647 * sata_print_link_status - Print SATA link status
936fd732 2648 * @link: SATA link to printk link status about
3be680b7
TH
2649 *
2650 * This function prints link speed and status of a SATA link.
2651 *
2652 * LOCKING:
2653 * None.
2654 */
936fd732 2655void sata_print_link_status(struct ata_link *link)
3be680b7 2656{
6d5f9732 2657 u32 sstatus, scontrol, tmp;
3be680b7 2658
936fd732 2659 if (sata_scr_read(link, SCR_STATUS, &sstatus))
3be680b7 2660 return;
936fd732 2661 sata_scr_read(link, SCR_CONTROL, &scontrol);
3be680b7 2662
936fd732 2663 if (ata_link_online(link)) {
3be680b7 2664 tmp = (sstatus >> 4) & 0xf;
936fd732 2665 ata_link_printk(link, KERN_INFO,
f15a1daf
TH
2666 "SATA link up %s (SStatus %X SControl %X)\n",
2667 sata_spd_string(tmp), sstatus, scontrol);
3be680b7 2668 } else {
936fd732 2669 ata_link_printk(link, KERN_INFO,
f15a1daf
TH
2670 "SATA link down (SStatus %X SControl %X)\n",
2671 sstatus, scontrol);
3be680b7
TH
2672 }
2673}
2674
ebdfca6e
AC
2675/**
2676 * ata_dev_pair - return other device on cable
ebdfca6e
AC
2677 * @adev: device
2678 *
2679 * Obtain the other device on the same cable, or if none is
2680 * present NULL is returned
2681 */
2e9edbf8 2682
3373efd8 2683struct ata_device *ata_dev_pair(struct ata_device *adev)
ebdfca6e 2684{
9af5c9c9
TH
2685 struct ata_link *link = adev->link;
2686 struct ata_device *pair = &link->device[1 - adev->devno];
e1211e3f 2687 if (!ata_dev_enabled(pair))
ebdfca6e
AC
2688 return NULL;
2689 return pair;
2690}
2691
1da177e4 2692/**
780a87f7
JG
2693 * ata_port_disable - Disable port.
2694 * @ap: Port to be disabled.
1da177e4 2695 *
780a87f7
JG
2696 * Modify @ap data structure such that the system
2697 * thinks that the entire port is disabled, and should
2698 * never attempt to probe or communicate with devices
2699 * on this port.
2700 *
cca3974e 2701 * LOCKING: host lock, or some other form of
780a87f7 2702 * serialization.
1da177e4
LT
2703 */
2704
2705void ata_port_disable(struct ata_port *ap)
2706{
9af5c9c9
TH
2707 ap->link.device[0].class = ATA_DEV_NONE;
2708 ap->link.device[1].class = ATA_DEV_NONE;
198e0fed 2709 ap->flags |= ATA_FLAG_DISABLED;
1da177e4
LT
2710}
2711
1c3fae4d 2712/**
3c567b7d 2713 * sata_down_spd_limit - adjust SATA spd limit downward
936fd732 2714 * @link: Link to adjust SATA spd limit for
1c3fae4d 2715 *
936fd732 2716 * Adjust SATA spd limit of @link downward. Note that this
1c3fae4d 2717 * function only adjusts the limit. The change must be applied
3c567b7d 2718 * using sata_set_spd().
1c3fae4d
TH
2719 *
2720 * LOCKING:
2721 * Inherited from caller.
2722 *
2723 * RETURNS:
2724 * 0 on success, negative errno on failure
2725 */
936fd732 2726int sata_down_spd_limit(struct ata_link *link)
1c3fae4d 2727{
81952c54
TH
2728 u32 sstatus, spd, mask;
2729 int rc, highbit;
1c3fae4d 2730
936fd732 2731 if (!sata_scr_valid(link))
008a7896
TH
2732 return -EOPNOTSUPP;
2733
2734 /* If SCR can be read, use it to determine the current SPD.
936fd732 2735 * If not, use cached value in link->sata_spd.
008a7896 2736 */
936fd732 2737 rc = sata_scr_read(link, SCR_STATUS, &sstatus);
008a7896
TH
2738 if (rc == 0)
2739 spd = (sstatus >> 4) & 0xf;
2740 else
936fd732 2741 spd = link->sata_spd;
1c3fae4d 2742
936fd732 2743 mask = link->sata_spd_limit;
1c3fae4d
TH
2744 if (mask <= 1)
2745 return -EINVAL;
008a7896
TH
2746
2747 /* unconditionally mask off the highest bit */
1c3fae4d
TH
2748 highbit = fls(mask) - 1;
2749 mask &= ~(1 << highbit);
2750
008a7896
TH
2751 /* Mask off all speeds higher than or equal to the current
2752 * one. Force 1.5Gbps if current SPD is not available.
2753 */
2754 if (spd > 1)
2755 mask &= (1 << (spd - 1)) - 1;
2756 else
2757 mask &= 1;
2758
2759 /* were we already at the bottom? */
1c3fae4d
TH
2760 if (!mask)
2761 return -EINVAL;
2762
936fd732 2763 link->sata_spd_limit = mask;
1c3fae4d 2764
936fd732 2765 ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
f15a1daf 2766 sata_spd_string(fls(mask)));
1c3fae4d
TH
2767
2768 return 0;
2769}
2770
936fd732 2771static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
1c3fae4d 2772{
5270222f
TH
2773 struct ata_link *host_link = &link->ap->link;
2774 u32 limit, target, spd;
1c3fae4d 2775
5270222f
TH
2776 limit = link->sata_spd_limit;
2777
2778 /* Don't configure downstream link faster than upstream link.
2779 * It doesn't speed up anything and some PMPs choke on such
2780 * configuration.
2781 */
2782 if (!ata_is_host_link(link) && host_link->sata_spd)
2783 limit &= (1 << host_link->sata_spd) - 1;
2784
2785 if (limit == UINT_MAX)
2786 target = 0;
1c3fae4d 2787 else
5270222f 2788 target = fls(limit);
1c3fae4d
TH
2789
2790 spd = (*scontrol >> 4) & 0xf;
5270222f 2791 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
1c3fae4d 2792
5270222f 2793 return spd != target;
1c3fae4d
TH
2794}
2795
2796/**
3c567b7d 2797 * sata_set_spd_needed - is SATA spd configuration needed
936fd732 2798 * @link: Link in question
1c3fae4d
TH
2799 *
2800 * Test whether the spd limit in SControl matches
936fd732 2801 * @link->sata_spd_limit. This function is used to determine
1c3fae4d
TH
2802 * whether hardreset is necessary to apply SATA spd
2803 * configuration.
2804 *
2805 * LOCKING:
2806 * Inherited from caller.
2807 *
2808 * RETURNS:
2809 * 1 if SATA spd configuration is needed, 0 otherwise.
2810 */
936fd732 2811int sata_set_spd_needed(struct ata_link *link)
1c3fae4d
TH
2812{
2813 u32 scontrol;
2814
936fd732 2815 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
db64bcf3 2816 return 1;
1c3fae4d 2817
936fd732 2818 return __sata_set_spd_needed(link, &scontrol);
1c3fae4d
TH
2819}
2820
2821/**
3c567b7d 2822 * sata_set_spd - set SATA spd according to spd limit
936fd732 2823 * @link: Link to set SATA spd for
1c3fae4d 2824 *
936fd732 2825 * Set SATA spd of @link according to sata_spd_limit.
1c3fae4d
TH
2826 *
2827 * LOCKING:
2828 * Inherited from caller.
2829 *
2830 * RETURNS:
2831 * 0 if spd doesn't need to be changed, 1 if spd has been
81952c54 2832 * changed. Negative errno if SCR registers are inaccessible.
1c3fae4d 2833 */
936fd732 2834int sata_set_spd(struct ata_link *link)
1c3fae4d
TH
2835{
2836 u32 scontrol;
81952c54 2837 int rc;
1c3fae4d 2838
936fd732 2839 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
81952c54 2840 return rc;
1c3fae4d 2841
936fd732 2842 if (!__sata_set_spd_needed(link, &scontrol))
1c3fae4d
TH
2843 return 0;
2844
936fd732 2845 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
81952c54
TH
2846 return rc;
2847
1c3fae4d
TH
2848 return 1;
2849}
2850
452503f9
AC
2851/*
2852 * This mode timing computation functionality is ported over from
2853 * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
2854 */
2855/*
b352e57d 2856 * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
452503f9 2857 * These were taken from ATA/ATAPI-6 standard, rev 0a, except
b352e57d
AC
2858 * for UDMA6, which is currently supported only by Maxtor drives.
2859 *
2860 * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
452503f9
AC
2861 */
2862
2863static const struct ata_timing ata_timing[] = {
70cd071e
TH
2864/* { XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960, 0 }, */
2865 { XFER_PIO_0, 70, 290, 240, 600, 165, 150, 600, 0 },
2866 { XFER_PIO_1, 50, 290, 93, 383, 125, 100, 383, 0 },
2867 { XFER_PIO_2, 30, 290, 40, 330, 100, 90, 240, 0 },
2868 { XFER_PIO_3, 30, 80, 70, 180, 80, 70, 180, 0 },
2869 { XFER_PIO_4, 25, 70, 25, 120, 70, 25, 120, 0 },
2870 { XFER_PIO_5, 15, 65, 25, 100, 65, 25, 100, 0 },
2871 { XFER_PIO_6, 10, 55, 20, 80, 55, 20, 80, 0 },
452503f9 2872
70cd071e
TH
2873 { XFER_SW_DMA_0, 120, 0, 0, 0, 480, 480, 960, 0 },
2874 { XFER_SW_DMA_1, 90, 0, 0, 0, 240, 240, 480, 0 },
2875 { XFER_SW_DMA_2, 60, 0, 0, 0, 120, 120, 240, 0 },
452503f9 2876
70cd071e
TH
2877 { XFER_MW_DMA_0, 60, 0, 0, 0, 215, 215, 480, 0 },
2878 { XFER_MW_DMA_1, 45, 0, 0, 0, 80, 50, 150, 0 },
2879 { XFER_MW_DMA_2, 25, 0, 0, 0, 70, 25, 120, 0 },
b352e57d 2880 { XFER_MW_DMA_3, 25, 0, 0, 0, 65, 25, 100, 0 },
70cd071e 2881 { XFER_MW_DMA_4, 25, 0, 0, 0, 55, 20, 80, 0 },
452503f9
AC
2882
2883/* { XFER_UDMA_SLOW, 0, 0, 0, 0, 0, 0, 0, 150 }, */
70cd071e
TH
2884 { XFER_UDMA_0, 0, 0, 0, 0, 0, 0, 0, 120 },
2885 { XFER_UDMA_1, 0, 0, 0, 0, 0, 0, 0, 80 },
2886 { XFER_UDMA_2, 0, 0, 0, 0, 0, 0, 0, 60 },
2887 { XFER_UDMA_3, 0, 0, 0, 0, 0, 0, 0, 45 },
2888 { XFER_UDMA_4, 0, 0, 0, 0, 0, 0, 0, 30 },
2889 { XFER_UDMA_5, 0, 0, 0, 0, 0, 0, 0, 20 },
2890 { XFER_UDMA_6, 0, 0, 0, 0, 0, 0, 0, 15 },
452503f9
AC
2891
2892 { 0xFF }
2893};
2894
2dcb407e
JG
2895#define ENOUGH(v, unit) (((v)-1)/(unit)+1)
2896#define EZ(v, unit) ((v)?ENOUGH(v, unit):0)
452503f9
AC
2897
2898static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
2899{
2900 q->setup = EZ(t->setup * 1000, T);
2901 q->act8b = EZ(t->act8b * 1000, T);
2902 q->rec8b = EZ(t->rec8b * 1000, T);
2903 q->cyc8b = EZ(t->cyc8b * 1000, T);
2904 q->active = EZ(t->active * 1000, T);
2905 q->recover = EZ(t->recover * 1000, T);
2906 q->cycle = EZ(t->cycle * 1000, T);
2907 q->udma = EZ(t->udma * 1000, UT);
2908}
2909
2910void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2911 struct ata_timing *m, unsigned int what)
2912{
2913 if (what & ATA_TIMING_SETUP ) m->setup = max(a->setup, b->setup);
2914 if (what & ATA_TIMING_ACT8B ) m->act8b = max(a->act8b, b->act8b);
2915 if (what & ATA_TIMING_REC8B ) m->rec8b = max(a->rec8b, b->rec8b);
2916 if (what & ATA_TIMING_CYC8B ) m->cyc8b = max(a->cyc8b, b->cyc8b);
2917 if (what & ATA_TIMING_ACTIVE ) m->active = max(a->active, b->active);
2918 if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
2919 if (what & ATA_TIMING_CYCLE ) m->cycle = max(a->cycle, b->cycle);
2920 if (what & ATA_TIMING_UDMA ) m->udma = max(a->udma, b->udma);
2921}
2922
6357357c 2923const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
452503f9 2924{
70cd071e
TH
2925 const struct ata_timing *t = ata_timing;
2926
2927 while (xfer_mode > t->mode)
2928 t++;
452503f9 2929
70cd071e
TH
2930 if (xfer_mode == t->mode)
2931 return t;
2932 return NULL;
452503f9
AC
2933}
2934
2935int ata_timing_compute(struct ata_device *adev, unsigned short speed,
2936 struct ata_timing *t, int T, int UT)
2937{
2938 const struct ata_timing *s;
2939 struct ata_timing p;
2940
2941 /*
2e9edbf8 2942 * Find the mode.
75b1f2f8 2943 */
452503f9
AC
2944
2945 if (!(s = ata_timing_find_mode(speed)))
2946 return -EINVAL;
2947
75b1f2f8
AL
2948 memcpy(t, s, sizeof(*s));
2949
452503f9
AC
2950 /*
2951 * If the drive is an EIDE drive, it can tell us it needs extended
2952 * PIO/MW_DMA cycle timing.
2953 */
2954
2955 if (adev->id[ATA_ID_FIELD_VALID] & 2) { /* EIDE drive */
2956 memset(&p, 0, sizeof(p));
2dcb407e 2957 if (speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
452503f9
AC
2958 if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
2959 else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
2dcb407e 2960 } else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
452503f9
AC
2961 p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
2962 }
2963 ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
2964 }
2965
2966 /*
2967 * Convert the timing to bus clock counts.
2968 */
2969
75b1f2f8 2970 ata_timing_quantize(t, t, T, UT);
452503f9
AC
2971
2972 /*
c893a3ae
RD
2973 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
2974 * S.M.A.R.T * and some other commands. We have to ensure that the
2975 * DMA cycle timing is slower/equal than the fastest PIO timing.
452503f9
AC
2976 */
2977
fd3367af 2978 if (speed > XFER_PIO_6) {
452503f9
AC
2979 ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
2980 ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
2981 }
2982
2983 /*
c893a3ae 2984 * Lengthen active & recovery time so that cycle time is correct.
452503f9
AC
2985 */
2986
2987 if (t->act8b + t->rec8b < t->cyc8b) {
2988 t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
2989 t->rec8b = t->cyc8b - t->act8b;
2990 }
2991
2992 if (t->active + t->recover < t->cycle) {
2993 t->active += (t->cycle - (t->active + t->recover)) / 2;
2994 t->recover = t->cycle - t->active;
2995 }
a617c09f 2996
4f701d1e
AC
2997 /* In a few cases quantisation may produce enough errors to
2998 leave t->cycle too low for the sum of active and recovery
2999 if so we must correct this */
3000 if (t->active + t->recover > t->cycle)
3001 t->cycle = t->active + t->recover;
452503f9
AC
3002
3003 return 0;
3004}
3005
a0f79b92
TH
3006/**
3007 * ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3008 * @xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3009 * @cycle: cycle duration in ns
3010 *
3011 * Return matching xfer mode for @cycle. The returned mode is of
3012 * the transfer type specified by @xfer_shift. If @cycle is too
3013 * slow for @xfer_shift, 0xff is returned. If @cycle is faster
3014 * than the fastest known mode, the fasted mode is returned.
3015 *
3016 * LOCKING:
3017 * None.
3018 *
3019 * RETURNS:
3020 * Matching xfer_mode, 0xff if no match found.
3021 */
3022u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3023{
3024 u8 base_mode = 0xff, last_mode = 0xff;
3025 const struct ata_xfer_ent *ent;
3026 const struct ata_timing *t;
3027
3028 for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3029 if (ent->shift == xfer_shift)
3030 base_mode = ent->base;
3031
3032 for (t = ata_timing_find_mode(base_mode);
3033 t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3034 unsigned short this_cycle;
3035
3036 switch (xfer_shift) {
3037 case ATA_SHIFT_PIO:
3038 case ATA_SHIFT_MWDMA:
3039 this_cycle = t->cycle;
3040 break;
3041 case ATA_SHIFT_UDMA:
3042 this_cycle = t->udma;
3043 break;
3044 default:
3045 return 0xff;
3046 }
3047
3048 if (cycle > this_cycle)
3049 break;
3050
3051 last_mode = t->mode;
3052 }
3053
3054 return last_mode;
3055}
3056
cf176e1a
TH
3057/**
3058 * ata_down_xfermask_limit - adjust dev xfer masks downward
cf176e1a 3059 * @dev: Device to adjust xfer masks
458337db 3060 * @sel: ATA_DNXFER_* selector
cf176e1a
TH
3061 *
3062 * Adjust xfer masks of @dev downward. Note that this function
3063 * does not apply the change. Invoking ata_set_mode() afterwards
3064 * will apply the limit.
3065 *
3066 * LOCKING:
3067 * Inherited from caller.
3068 *
3069 * RETURNS:
3070 * 0 on success, negative errno on failure
3071 */
458337db 3072int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
cf176e1a 3073{
458337db 3074 char buf[32];
7dc951ae
TH
3075 unsigned long orig_mask, xfer_mask;
3076 unsigned long pio_mask, mwdma_mask, udma_mask;
458337db 3077 int quiet, highbit;
cf176e1a 3078
458337db
TH
3079 quiet = !!(sel & ATA_DNXFER_QUIET);
3080 sel &= ~ATA_DNXFER_QUIET;
cf176e1a 3081
458337db
TH
3082 xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3083 dev->mwdma_mask,
3084 dev->udma_mask);
3085 ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
cf176e1a 3086
458337db
TH
3087 switch (sel) {
3088 case ATA_DNXFER_PIO:
3089 highbit = fls(pio_mask) - 1;
3090 pio_mask &= ~(1 << highbit);
3091 break;
3092
3093 case ATA_DNXFER_DMA:
3094 if (udma_mask) {
3095 highbit = fls(udma_mask) - 1;
3096 udma_mask &= ~(1 << highbit);
3097 if (!udma_mask)
3098 return -ENOENT;
3099 } else if (mwdma_mask) {
3100 highbit = fls(mwdma_mask) - 1;
3101 mwdma_mask &= ~(1 << highbit);
3102 if (!mwdma_mask)
3103 return -ENOENT;
3104 }
3105 break;
3106
3107 case ATA_DNXFER_40C:
3108 udma_mask &= ATA_UDMA_MASK_40C;
3109 break;
3110
3111 case ATA_DNXFER_FORCE_PIO0:
3112 pio_mask &= 1;
3113 case ATA_DNXFER_FORCE_PIO:
3114 mwdma_mask = 0;
3115 udma_mask = 0;
3116 break;
3117
458337db
TH
3118 default:
3119 BUG();
3120 }
3121
3122 xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3123
3124 if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3125 return -ENOENT;
3126
3127 if (!quiet) {
3128 if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3129 snprintf(buf, sizeof(buf), "%s:%s",
3130 ata_mode_string(xfer_mask),
3131 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3132 else
3133 snprintf(buf, sizeof(buf), "%s",
3134 ata_mode_string(xfer_mask));
3135
3136 ata_dev_printk(dev, KERN_WARNING,
3137 "limiting speed to %s\n", buf);
3138 }
cf176e1a
TH
3139
3140 ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3141 &dev->udma_mask);
3142
cf176e1a 3143 return 0;
cf176e1a
TH
3144}
3145
3373efd8 3146static int ata_dev_set_mode(struct ata_device *dev)
1da177e4 3147{
9af5c9c9 3148 struct ata_eh_context *ehc = &dev->link->eh_context;
4055dee7
TH
3149 const char *dev_err_whine = "";
3150 int ign_dev_err = 0;
83206a29
TH
3151 unsigned int err_mask;
3152 int rc;
1da177e4 3153
e8384607 3154 dev->flags &= ~ATA_DFLAG_PIO;
1da177e4
LT
3155 if (dev->xfer_shift == ATA_SHIFT_PIO)
3156 dev->flags |= ATA_DFLAG_PIO;
3157
3373efd8 3158 err_mask = ata_dev_set_xfermode(dev);
2dcb407e 3159
4055dee7
TH
3160 if (err_mask & ~AC_ERR_DEV)
3161 goto fail;
3162
3163 /* revalidate */
3164 ehc->i.flags |= ATA_EHI_POST_SETMODE;
3165 rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3166 ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3167 if (rc)
3168 return rc;
3169
11750a40
AC
3170 /* Old CFA may refuse this command, which is just fine */
3171 if (dev->xfer_shift == ATA_SHIFT_PIO && ata_id_is_cfa(dev->id))
4055dee7 3172 ign_dev_err = 1;
2dcb407e 3173
0bc2a79a
AC
3174 /* Some very old devices and some bad newer ones fail any kind of
3175 SET_XFERMODE request but support PIO0-2 timings and no IORDY */
3176 if (dev->xfer_shift == ATA_SHIFT_PIO && !ata_id_has_iordy(dev->id) &&
3177 dev->pio_mode <= XFER_PIO_2)
4055dee7 3178 ign_dev_err = 1;
2dcb407e 3179
3acaf94b
AC
3180 /* Early MWDMA devices do DMA but don't allow DMA mode setting.
3181 Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
c5038fc0 3182 if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
3acaf94b
AC
3183 dev->dma_mode == XFER_MW_DMA_0 &&
3184 (dev->id[63] >> 8) & 1)
4055dee7 3185 ign_dev_err = 1;
3acaf94b 3186
4055dee7
TH
3187 /* if the device is actually configured correctly, ignore dev err */
3188 if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
3189 ign_dev_err = 1;
1da177e4 3190
4055dee7
TH
3191 if (err_mask & AC_ERR_DEV) {
3192 if (!ign_dev_err)
3193 goto fail;
3194 else
3195 dev_err_whine = " (device error ignored)";
3196 }
48a8a14f 3197
23e71c3d
TH
3198 DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3199 dev->xfer_shift, (int)dev->xfer_mode);
1da177e4 3200
4055dee7
TH
3201 ata_dev_printk(dev, KERN_INFO, "configured for %s%s\n",
3202 ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
3203 dev_err_whine);
3204
83206a29 3205 return 0;
4055dee7
TH
3206
3207 fail:
3208 ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
3209 "(err_mask=0x%x)\n", err_mask);
3210 return -EIO;
1da177e4
LT
3211}
3212
1da177e4 3213/**
04351821 3214 * ata_do_set_mode - Program timings and issue SET FEATURES - XFER
0260731f 3215 * @link: link on which timings will be programmed
1967b7ff 3216 * @r_failed_dev: out parameter for failed device
1da177e4 3217 *
04351821
AC
3218 * Standard implementation of the function used to tune and set
3219 * ATA device disk transfer mode (PIO3, UDMA6, etc.). If
3220 * ata_dev_set_mode() fails, pointer to the failing device is
e82cbdb9 3221 * returned in @r_failed_dev.
780a87f7 3222 *
1da177e4 3223 * LOCKING:
0cba632b 3224 * PCI/etc. bus probe sem.
e82cbdb9
TH
3225 *
3226 * RETURNS:
3227 * 0 on success, negative errno otherwise
1da177e4 3228 */
04351821 3229
0260731f 3230int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
1da177e4 3231{
0260731f 3232 struct ata_port *ap = link->ap;
e8e0619f 3233 struct ata_device *dev;
f58229f8 3234 int rc = 0, used_dma = 0, found = 0;
3adcebb2 3235
a6d5a51c 3236 /* step 1: calculate xfer_mask */
f58229f8 3237 ata_link_for_each_dev(dev, link) {
7dc951ae 3238 unsigned long pio_mask, dma_mask;
b3a70601 3239 unsigned int mode_mask;
a6d5a51c 3240
e1211e3f 3241 if (!ata_dev_enabled(dev))
a6d5a51c
TH
3242 continue;
3243
b3a70601
AC
3244 mode_mask = ATA_DMA_MASK_ATA;
3245 if (dev->class == ATA_DEV_ATAPI)
3246 mode_mask = ATA_DMA_MASK_ATAPI;
3247 else if (ata_id_is_cfa(dev->id))
3248 mode_mask = ATA_DMA_MASK_CFA;
3249
3373efd8 3250 ata_dev_xfermask(dev);
33267325 3251 ata_force_xfermask(dev);
1da177e4 3252
acf356b1
TH
3253 pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3254 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
b3a70601
AC
3255
3256 if (libata_dma_mask & mode_mask)
3257 dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3258 else
3259 dma_mask = 0;
3260
acf356b1
TH
3261 dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3262 dev->dma_mode = ata_xfer_mask2mode(dma_mask);
5444a6f4 3263
4f65977d 3264 found = 1;
70cd071e 3265 if (dev->dma_mode != 0xff)
5444a6f4 3266 used_dma = 1;
a6d5a51c 3267 }
4f65977d 3268 if (!found)
e82cbdb9 3269 goto out;
a6d5a51c
TH
3270
3271 /* step 2: always set host PIO timings */
f58229f8 3272 ata_link_for_each_dev(dev, link) {
e8e0619f
TH
3273 if (!ata_dev_enabled(dev))
3274 continue;
3275
70cd071e 3276 if (dev->pio_mode == 0xff) {
f15a1daf 3277 ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
e8e0619f 3278 rc = -EINVAL;
e82cbdb9 3279 goto out;
e8e0619f
TH
3280 }
3281
3282 dev->xfer_mode = dev->pio_mode;
3283 dev->xfer_shift = ATA_SHIFT_PIO;
3284 if (ap->ops->set_piomode)
3285 ap->ops->set_piomode(ap, dev);
3286 }
1da177e4 3287
a6d5a51c 3288 /* step 3: set host DMA timings */
f58229f8 3289 ata_link_for_each_dev(dev, link) {
70cd071e 3290 if (!ata_dev_enabled(dev) || dev->dma_mode == 0xff)
e8e0619f
TH
3291 continue;
3292
3293 dev->xfer_mode = dev->dma_mode;
3294 dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3295 if (ap->ops->set_dmamode)
3296 ap->ops->set_dmamode(ap, dev);
3297 }
1da177e4
LT
3298
3299 /* step 4: update devices' xfer mode */
f58229f8 3300 ata_link_for_each_dev(dev, link) {
18d90deb 3301 /* don't update suspended devices' xfer mode */
9666f400 3302 if (!ata_dev_enabled(dev))
83206a29
TH
3303 continue;
3304
3373efd8 3305 rc = ata_dev_set_mode(dev);
5bbc53f4 3306 if (rc)
e82cbdb9 3307 goto out;
83206a29 3308 }
1da177e4 3309
e8e0619f
TH
3310 /* Record simplex status. If we selected DMA then the other
3311 * host channels are not permitted to do so.
5444a6f4 3312 */
cca3974e 3313 if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
032af1ce 3314 ap->host->simplex_claimed = ap;
5444a6f4 3315
e82cbdb9
TH
3316 out:
3317 if (rc)
3318 *r_failed_dev = dev;
3319 return rc;
1da177e4
LT
3320}
3321
d7bb4cc7 3322/**
936fd732
TH
3323 * sata_link_debounce - debounce SATA phy status
3324 * @link: ATA link to debounce SATA phy status for
d7bb4cc7 3325 * @params: timing parameters { interval, duratinon, timeout } in msec
d4b2bab4 3326 * @deadline: deadline jiffies for the operation
d7bb4cc7 3327 *
936fd732 3328* Make sure SStatus of @link reaches stable state, determined by
d7bb4cc7
TH
3329 * holding the same value where DET is not 1 for @duration polled
3330 * every @interval, before @timeout. Timeout constraints the
d4b2bab4
TH
3331 * beginning of the stable state. Because DET gets stuck at 1 on
3332 * some controllers after hot unplugging, this functions waits
d7bb4cc7
TH
3333 * until timeout then returns 0 if DET is stable at 1.
3334 *
d4b2bab4
TH
3335 * @timeout is further limited by @deadline. The sooner of the
3336 * two is used.
3337 *
d7bb4cc7
TH
3338 * LOCKING:
3339 * Kernel thread context (may sleep)
3340 *
3341 * RETURNS:
3342 * 0 on success, -errno on failure.
3343 */
936fd732
TH
3344int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3345 unsigned long deadline)
7a7921e8 3346{
d7bb4cc7 3347 unsigned long interval_msec = params[0];
d4b2bab4
TH
3348 unsigned long duration = msecs_to_jiffies(params[1]);
3349 unsigned long last_jiffies, t;
d7bb4cc7
TH
3350 u32 last, cur;
3351 int rc;
3352
d4b2bab4
TH
3353 t = jiffies + msecs_to_jiffies(params[2]);
3354 if (time_before(t, deadline))
3355 deadline = t;
3356
936fd732 3357 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
d7bb4cc7
TH
3358 return rc;
3359 cur &= 0xf;
3360
3361 last = cur;
3362 last_jiffies = jiffies;
3363
3364 while (1) {
3365 msleep(interval_msec);
936fd732 3366 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
d7bb4cc7
TH
3367 return rc;
3368 cur &= 0xf;
3369
3370 /* DET stable? */
3371 if (cur == last) {
d4b2bab4 3372 if (cur == 1 && time_before(jiffies, deadline))
d7bb4cc7
TH
3373 continue;
3374 if (time_after(jiffies, last_jiffies + duration))
3375 return 0;
3376 continue;
3377 }
3378
3379 /* unstable, start over */
3380 last = cur;
3381 last_jiffies = jiffies;
3382
f1545154
TH
3383 /* Check deadline. If debouncing failed, return
3384 * -EPIPE to tell upper layer to lower link speed.
3385 */
d4b2bab4 3386 if (time_after(jiffies, deadline))
f1545154 3387 return -EPIPE;
d7bb4cc7
TH
3388 }
3389}
3390
3391/**
936fd732
TH
3392 * sata_link_resume - resume SATA link
3393 * @link: ATA link to resume SATA
d7bb4cc7 3394 * @params: timing parameters { interval, duratinon, timeout } in msec
d4b2bab4 3395 * @deadline: deadline jiffies for the operation
d7bb4cc7 3396 *
936fd732 3397 * Resume SATA phy @link and debounce it.
d7bb4cc7
TH
3398 *
3399 * LOCKING:
3400 * Kernel thread context (may sleep)
3401 *
3402 * RETURNS:
3403 * 0 on success, -errno on failure.
3404 */
936fd732
TH
3405int sata_link_resume(struct ata_link *link, const unsigned long *params,
3406 unsigned long deadline)
d7bb4cc7
TH
3407{
3408 u32 scontrol;
81952c54
TH
3409 int rc;
3410
936fd732 3411 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
81952c54 3412 return rc;
7a7921e8 3413
852ee16a 3414 scontrol = (scontrol & 0x0f0) | 0x300;
81952c54 3415
936fd732 3416 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
81952c54 3417 return rc;
7a7921e8 3418
d7bb4cc7
TH
3419 /* Some PHYs react badly if SStatus is pounded immediately
3420 * after resuming. Delay 200ms before debouncing.
3421 */
3422 msleep(200);
7a7921e8 3423
936fd732 3424 return sata_link_debounce(link, params, deadline);
7a7921e8
TH
3425}
3426
f5914a46
TH
3427/**
3428 * ata_std_prereset - prepare for reset
cc0680a5 3429 * @link: ATA link to be reset
d4b2bab4 3430 * @deadline: deadline jiffies for the operation
f5914a46 3431 *
cc0680a5 3432 * @link is about to be reset. Initialize it. Failure from
b8cffc6a
TH
3433 * prereset makes libata abort whole reset sequence and give up
3434 * that port, so prereset should be best-effort. It does its
3435 * best to prepare for reset sequence but if things go wrong, it
3436 * should just whine, not fail.
f5914a46
TH
3437 *
3438 * LOCKING:
3439 * Kernel thread context (may sleep)
3440 *
3441 * RETURNS:
3442 * 0 on success, -errno otherwise.
3443 */
cc0680a5 3444int ata_std_prereset(struct ata_link *link, unsigned long deadline)
f5914a46 3445{
cc0680a5 3446 struct ata_port *ap = link->ap;
936fd732 3447 struct ata_eh_context *ehc = &link->eh_context;
e9c83914 3448 const unsigned long *timing = sata_ehc_deb_timing(ehc);
f5914a46
TH
3449 int rc;
3450
f5914a46
TH
3451 /* if we're about to do hardreset, nothing more to do */
3452 if (ehc->i.action & ATA_EH_HARDRESET)
3453 return 0;
3454
936fd732 3455 /* if SATA, resume link */
a16abc0b 3456 if (ap->flags & ATA_FLAG_SATA) {
936fd732 3457 rc = sata_link_resume(link, timing, deadline);
b8cffc6a
TH
3458 /* whine about phy resume failure but proceed */
3459 if (rc && rc != -EOPNOTSUPP)
cc0680a5 3460 ata_link_printk(link, KERN_WARNING, "failed to resume "
f5914a46 3461 "link for reset (errno=%d)\n", rc);
f5914a46
TH
3462 }
3463
8cebf274
TH
3464 /* wait for !BSY if we don't know that no device is attached */
3465 if (!ata_link_offline(link)) {
b8cffc6a 3466 rc = ata_wait_ready(ap, deadline);
6dffaf61 3467 if (rc && rc != -ENODEV) {
cc0680a5 3468 ata_link_printk(link, KERN_WARNING, "device not ready "
b8cffc6a
TH
3469 "(errno=%d), forcing hardreset\n", rc);
3470 ehc->i.action |= ATA_EH_HARDRESET;
3471 }
3472 }
f5914a46
TH
3473
3474 return 0;
3475}
3476
c2bd5804 3477/**
624d5c51
TH
3478 * sata_link_hardreset - reset link via SATA phy reset
3479 * @link: link to reset
3480 * @timing: timing parameters { interval, duratinon, timeout } in msec
d4b2bab4 3481 * @deadline: deadline jiffies for the operation
c2bd5804 3482 *
624d5c51 3483 * SATA phy-reset @link using DET bits of SControl register.
c2bd5804
TH
3484 *
3485 * LOCKING:
3486 * Kernel thread context (may sleep)
3487 *
3488 * RETURNS:
3489 * 0 on success, -errno otherwise.
3490 */
624d5c51
TH
3491int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
3492 unsigned long deadline)
c2bd5804 3493{
624d5c51 3494 u32 scontrol;
81952c54 3495 int rc;
852ee16a 3496
c2bd5804
TH
3497 DPRINTK("ENTER\n");
3498
936fd732 3499 if (sata_set_spd_needed(link)) {
1c3fae4d
TH
3500 /* SATA spec says nothing about how to reconfigure
3501 * spd. To be on the safe side, turn off phy during
3502 * reconfiguration. This works for at least ICH7 AHCI
3503 * and Sil3124.
3504 */
936fd732 3505 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
b6103f6d 3506 goto out;
81952c54 3507
a34b6fc0 3508 scontrol = (scontrol & 0x0f0) | 0x304;
81952c54 3509
936fd732 3510 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
b6103f6d 3511 goto out;
1c3fae4d 3512
936fd732 3513 sata_set_spd(link);
1c3fae4d
TH
3514 }
3515
3516 /* issue phy wake/reset */
936fd732 3517 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
b6103f6d 3518 goto out;
81952c54 3519
852ee16a 3520 scontrol = (scontrol & 0x0f0) | 0x301;
81952c54 3521
936fd732 3522 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
b6103f6d 3523 goto out;
c2bd5804 3524
1c3fae4d 3525 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
c2bd5804
TH
3526 * 10.4.2 says at least 1 ms.
3527 */
3528 msleep(1);
3529
936fd732
TH
3530 /* bring link back */
3531 rc = sata_link_resume(link, timing, deadline);
b6103f6d
TH
3532 out:
3533 DPRINTK("EXIT, rc=%d\n", rc);
3534 return rc;
3535}
3536
c2bd5804
TH
3537/**
3538 * ata_std_postreset - standard postreset callback
cc0680a5 3539 * @link: the target ata_link
c2bd5804
TH
3540 * @classes: classes of attached devices
3541 *
3542 * This function is invoked after a successful reset. Note that
3543 * the device might have been reset more than once using
3544 * different reset methods before postreset is invoked.
c2bd5804 3545 *
c2bd5804
TH
3546 * LOCKING:
3547 * Kernel thread context (may sleep)
3548 */
cc0680a5 3549void ata_std_postreset(struct ata_link *link, unsigned int *classes)
c2bd5804 3550{
cc0680a5 3551 struct ata_port *ap = link->ap;
dc2b3515
TH
3552 u32 serror;
3553
c2bd5804
TH
3554 DPRINTK("ENTER\n");
3555
c2bd5804 3556 /* print link status */
936fd732 3557 sata_print_link_status(link);
c2bd5804 3558
dc2b3515 3559 /* clear SError */
936fd732
TH
3560 if (sata_scr_read(link, SCR_ERROR, &serror) == 0)
3561 sata_scr_write(link, SCR_ERROR, serror);
f7fe7ad4 3562 link->eh_info.serror = 0;
dc2b3515 3563
c2bd5804
TH
3564 /* is double-select really necessary? */
3565 if (classes[0] != ATA_DEV_NONE)
3566 ap->ops->dev_select(ap, 1);
3567 if (classes[1] != ATA_DEV_NONE)
3568 ap->ops->dev_select(ap, 0);
3569
3a39746a
TH
3570 /* bail out if no device is present */
3571 if (classes[0] == ATA_DEV_NONE && classes[1] == ATA_DEV_NONE) {
3572 DPRINTK("EXIT, no device\n");
3573 return;
3574 }
3575
3576 /* set up device control */
0d5ff566
TH
3577 if (ap->ioaddr.ctl_addr)
3578 iowrite8(ap->ctl, ap->ioaddr.ctl_addr);
c2bd5804
TH
3579
3580 DPRINTK("EXIT\n");
3581}
3582
623a3128
TH
3583/**
3584 * ata_dev_same_device - Determine whether new ID matches configured device
623a3128
TH
3585 * @dev: device to compare against
3586 * @new_class: class of the new device
3587 * @new_id: IDENTIFY page of the new device
3588 *
3589 * Compare @new_class and @new_id against @dev and determine
3590 * whether @dev is the device indicated by @new_class and
3591 * @new_id.
3592 *
3593 * LOCKING:
3594 * None.
3595 *
3596 * RETURNS:
3597 * 1 if @dev matches @new_class and @new_id, 0 otherwise.
3598 */
3373efd8
TH
3599static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3600 const u16 *new_id)
623a3128
TH
3601{
3602 const u16 *old_id = dev->id;
a0cf733b
TH
3603 unsigned char model[2][ATA_ID_PROD_LEN + 1];
3604 unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
623a3128
TH
3605
3606 if (dev->class != new_class) {
f15a1daf
TH
3607 ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
3608 dev->class, new_class);
623a3128
TH
3609 return 0;
3610 }
3611
a0cf733b
TH
3612 ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3613 ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3614 ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3615 ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
623a3128
TH
3616
3617 if (strcmp(model[0], model[1])) {
f15a1daf
TH
3618 ata_dev_printk(dev, KERN_INFO, "model number mismatch "
3619 "'%s' != '%s'\n", model[0], model[1]);
623a3128
TH
3620 return 0;
3621 }
3622
3623 if (strcmp(serial[0], serial[1])) {
f15a1daf
TH
3624 ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
3625 "'%s' != '%s'\n", serial[0], serial[1]);
623a3128
TH
3626 return 0;
3627 }
3628
623a3128
TH
3629 return 1;
3630}
3631
3632/**
fe30911b 3633 * ata_dev_reread_id - Re-read IDENTIFY data
3fae450c 3634 * @dev: target ATA device
bff04647 3635 * @readid_flags: read ID flags
623a3128
TH
3636 *
3637 * Re-read IDENTIFY page and make sure @dev is still attached to
3638 * the port.
3639 *
3640 * LOCKING:
3641 * Kernel thread context (may sleep)
3642 *
3643 * RETURNS:
3644 * 0 on success, negative errno otherwise
3645 */
fe30911b 3646int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
623a3128 3647{
5eb45c02 3648 unsigned int class = dev->class;
9af5c9c9 3649 u16 *id = (void *)dev->link->ap->sector_buf;
623a3128
TH
3650 int rc;
3651
fe635c7e 3652 /* read ID data */
bff04647 3653 rc = ata_dev_read_id(dev, &class, readid_flags, id);
623a3128 3654 if (rc)
fe30911b 3655 return rc;
623a3128
TH
3656
3657 /* is the device still there? */
fe30911b
TH
3658 if (!ata_dev_same_device(dev, class, id))
3659 return -ENODEV;
623a3128 3660
fe635c7e 3661 memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
fe30911b
TH
3662 return 0;
3663}
3664
3665/**
3666 * ata_dev_revalidate - Revalidate ATA device
3667 * @dev: device to revalidate
422c9daa 3668 * @new_class: new class code
fe30911b
TH
3669 * @readid_flags: read ID flags
3670 *
3671 * Re-read IDENTIFY page, make sure @dev is still attached to the
3672 * port and reconfigure it according to the new IDENTIFY page.
3673 *
3674 * LOCKING:
3675 * Kernel thread context (may sleep)
3676 *
3677 * RETURNS:
3678 * 0 on success, negative errno otherwise
3679 */
422c9daa
TH
3680int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3681 unsigned int readid_flags)
fe30911b 3682{
6ddcd3b0 3683 u64 n_sectors = dev->n_sectors;
fe30911b
TH
3684 int rc;
3685
3686 if (!ata_dev_enabled(dev))
3687 return -ENODEV;
3688
422c9daa
TH
3689 /* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3690 if (ata_class_enabled(new_class) &&
3691 new_class != ATA_DEV_ATA && new_class != ATA_DEV_ATAPI) {
3692 ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
3693 dev->class, new_class);
3694 rc = -ENODEV;
3695 goto fail;
3696 }
3697
fe30911b
TH
3698 /* re-read ID */
3699 rc = ata_dev_reread_id(dev, readid_flags);
3700 if (rc)
3701 goto fail;
623a3128
TH
3702
3703 /* configure device according to the new ID */
efdaedc4 3704 rc = ata_dev_configure(dev);
6ddcd3b0
TH
3705 if (rc)
3706 goto fail;
3707
3708 /* verify n_sectors hasn't changed */
b54eebd6
TH
3709 if (dev->class == ATA_DEV_ATA && n_sectors &&
3710 dev->n_sectors != n_sectors) {
6ddcd3b0
TH
3711 ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
3712 "%llu != %llu\n",
3713 (unsigned long long)n_sectors,
3714 (unsigned long long)dev->n_sectors);
8270bec4
TH
3715
3716 /* restore original n_sectors */
3717 dev->n_sectors = n_sectors;
3718
6ddcd3b0
TH
3719 rc = -ENODEV;
3720 goto fail;
3721 }
3722
3723 return 0;
623a3128
TH
3724
3725 fail:
f15a1daf 3726 ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
623a3128
TH
3727 return rc;
3728}
3729
6919a0a6
AC
3730struct ata_blacklist_entry {
3731 const char *model_num;
3732 const char *model_rev;
3733 unsigned long horkage;
3734};
3735
3736static const struct ata_blacklist_entry ata_device_blacklist [] = {
3737 /* Devices with DMA related problems under Linux */
3738 { "WDC AC11000H", NULL, ATA_HORKAGE_NODMA },
3739 { "WDC AC22100H", NULL, ATA_HORKAGE_NODMA },
3740 { "WDC AC32500H", NULL, ATA_HORKAGE_NODMA },
3741 { "WDC AC33100H", NULL, ATA_HORKAGE_NODMA },
3742 { "WDC AC31600H", NULL, ATA_HORKAGE_NODMA },
3743 { "WDC AC32100H", "24.09P07", ATA_HORKAGE_NODMA },
3744 { "WDC AC23200L", "21.10N21", ATA_HORKAGE_NODMA },
3745 { "Compaq CRD-8241B", NULL, ATA_HORKAGE_NODMA },
3746 { "CRD-8400B", NULL, ATA_HORKAGE_NODMA },
3747 { "CRD-8480B", NULL, ATA_HORKAGE_NODMA },
3748 { "CRD-8482B", NULL, ATA_HORKAGE_NODMA },
3749 { "CRD-84", NULL, ATA_HORKAGE_NODMA },
3750 { "SanDisk SDP3B", NULL, ATA_HORKAGE_NODMA },
3751 { "SanDisk SDP3B-64", NULL, ATA_HORKAGE_NODMA },
3752 { "SANYO CD-ROM CRD", NULL, ATA_HORKAGE_NODMA },
3753 { "HITACHI CDR-8", NULL, ATA_HORKAGE_NODMA },
3754 { "HITACHI CDR-8335", NULL, ATA_HORKAGE_NODMA },
3755 { "HITACHI CDR-8435", NULL, ATA_HORKAGE_NODMA },
3756 { "Toshiba CD-ROM XM-6202B", NULL, ATA_HORKAGE_NODMA },
3757 { "TOSHIBA CD-ROM XM-1702BC", NULL, ATA_HORKAGE_NODMA },
3758 { "CD-532E-A", NULL, ATA_HORKAGE_NODMA },
3759 { "E-IDE CD-ROM CR-840",NULL, ATA_HORKAGE_NODMA },
3760 { "CD-ROM Drive/F5A", NULL, ATA_HORKAGE_NODMA },
3761 { "WPI CDD-820", NULL, ATA_HORKAGE_NODMA },
3762 { "SAMSUNG CD-ROM SC-148C", NULL, ATA_HORKAGE_NODMA },
3763 { "SAMSUNG CD-ROM SC", NULL, ATA_HORKAGE_NODMA },
6919a0a6
AC
3764 { "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
3765 { "_NEC DV5800A", NULL, ATA_HORKAGE_NODMA },
2dcb407e 3766 { "SAMSUNG CD-ROM SN-124", "N001", ATA_HORKAGE_NODMA },
39f19886 3767 { "Seagate STT20000A", NULL, ATA_HORKAGE_NODMA },
3af9a77a
TH
3768 /* Odd clown on sil3726/4726 PMPs */
3769 { "Config Disk", NULL, ATA_HORKAGE_NODMA |
3770 ATA_HORKAGE_SKIP_PM },
6919a0a6 3771
18d6e9d5 3772 /* Weird ATAPI devices */
40a1d531 3773 { "TORiSAN DVD-ROM DRD-N216", NULL, ATA_HORKAGE_MAX_SEC_128 },
18d6e9d5 3774
6919a0a6
AC
3775 /* Devices we expect to fail diagnostics */
3776
3777 /* Devices where NCQ should be avoided */
3778 /* NCQ is slow */
2dcb407e 3779 { "WDC WD740ADFD-00", NULL, ATA_HORKAGE_NONCQ },
459ad688 3780 { "WDC WD740ADFD-00NLR1", NULL, ATA_HORKAGE_NONCQ, },
09125ea6
TH
3781 /* http://thread.gmane.org/gmane.linux.ide/14907 */
3782 { "FUJITSU MHT2060BH", NULL, ATA_HORKAGE_NONCQ },
7acfaf30 3783 /* NCQ is broken */
539cc7c7 3784 { "Maxtor *", "BANC*", ATA_HORKAGE_NONCQ },
0e3dbc01 3785 { "Maxtor 7V300F0", "VA111630", ATA_HORKAGE_NONCQ },
da6f0ec2 3786 { "ST380817AS", "3.42", ATA_HORKAGE_NONCQ },
e41bd3e8 3787 { "ST3160023AS", "3.42", ATA_HORKAGE_NONCQ },
539cc7c7 3788
36e337d0
RH
3789 /* Blacklist entries taken from Silicon Image 3124/3132
3790 Windows driver .inf file - also several Linux problem reports */
3791 { "HTS541060G9SA00", "MB3OC60D", ATA_HORKAGE_NONCQ, },
3792 { "HTS541080G9SA00", "MB4OC60D", ATA_HORKAGE_NONCQ, },
3793 { "HTS541010G9SA00", "MBZOC60D", ATA_HORKAGE_NONCQ, },
6919a0a6 3794
16c55b03
TH
3795 /* devices which puke on READ_NATIVE_MAX */
3796 { "HDS724040KLSA80", "KFAOA20N", ATA_HORKAGE_BROKEN_HPA, },
3797 { "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
3798 { "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
3799 { "MAXTOR 6L080L4", "A93.0500", ATA_HORKAGE_BROKEN_HPA },
6919a0a6 3800
93328e11
AC
3801 /* Devices which report 1 sector over size HPA */
3802 { "ST340823A", NULL, ATA_HORKAGE_HPA_SIZE, },
3803 { "ST320413A", NULL, ATA_HORKAGE_HPA_SIZE, },
b152fcd3 3804 { "ST310211A", NULL, ATA_HORKAGE_HPA_SIZE, },
93328e11 3805
6bbfd53d
AC
3806 /* Devices which get the IVB wrong */
3807 { "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
3808 { "TSSTcorp CDDVDW SH-S202J", "SB00", ATA_HORKAGE_IVB, },
e9f33406
PM
3809 { "TSSTcorp CDDVDW SH-S202J", "SB01", ATA_HORKAGE_IVB, },
3810 { "TSSTcorp CDDVDW SH-S202N", "SB00", ATA_HORKAGE_IVB, },
3811 { "TSSTcorp CDDVDW SH-S202N", "SB01", ATA_HORKAGE_IVB, },
6bbfd53d 3812
6919a0a6
AC
3813 /* End Marker */
3814 { }
1da177e4 3815};
2e9edbf8 3816
741b7763 3817static int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
539cc7c7
JG
3818{
3819 const char *p;
3820 int len;
3821
3822 /*
3823 * check for trailing wildcard: *\0
3824 */
3825 p = strchr(patt, wildchar);
3826 if (p && ((*(p + 1)) == 0))
3827 len = p - patt;
317b50b8 3828 else {
539cc7c7 3829 len = strlen(name);
317b50b8
AP
3830 if (!len) {
3831 if (!*patt)
3832 return 0;
3833 return -1;
3834 }
3835 }
539cc7c7
JG
3836
3837 return strncmp(patt, name, len);
3838}
3839
75683fe7 3840static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
1da177e4 3841{
8bfa79fc
TH
3842 unsigned char model_num[ATA_ID_PROD_LEN + 1];
3843 unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
6919a0a6 3844 const struct ata_blacklist_entry *ad = ata_device_blacklist;
3a778275 3845
8bfa79fc
TH
3846 ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
3847 ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
1da177e4 3848
6919a0a6 3849 while (ad->model_num) {
539cc7c7 3850 if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
6919a0a6
AC
3851 if (ad->model_rev == NULL)
3852 return ad->horkage;
539cc7c7 3853 if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
6919a0a6 3854 return ad->horkage;
f4b15fef 3855 }
6919a0a6 3856 ad++;
f4b15fef 3857 }
1da177e4
LT
3858 return 0;
3859}
3860
6919a0a6
AC
3861static int ata_dma_blacklisted(const struct ata_device *dev)
3862{
3863 /* We don't support polling DMA.
3864 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
3865 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
3866 */
9af5c9c9 3867 if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
6919a0a6
AC
3868 (dev->flags & ATA_DFLAG_CDB_INTR))
3869 return 1;
75683fe7 3870 return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
6919a0a6
AC
3871}
3872
6bbfd53d
AC
3873/**
3874 * ata_is_40wire - check drive side detection
3875 * @dev: device
3876 *
3877 * Perform drive side detection decoding, allowing for device vendors
3878 * who can't follow the documentation.
3879 */
3880
3881static int ata_is_40wire(struct ata_device *dev)
3882{
3883 if (dev->horkage & ATA_HORKAGE_IVB)
3884 return ata_drive_40wire_relaxed(dev->id);
3885 return ata_drive_40wire(dev->id);
3886}
3887
a6d5a51c
TH
3888/**
3889 * ata_dev_xfermask - Compute supported xfermask of the given device
a6d5a51c
TH
3890 * @dev: Device to compute xfermask for
3891 *
acf356b1
TH
3892 * Compute supported xfermask of @dev and store it in
3893 * dev->*_mask. This function is responsible for applying all
3894 * known limits including host controller limits, device
3895 * blacklist, etc...
a6d5a51c
TH
3896 *
3897 * LOCKING:
3898 * None.
a6d5a51c 3899 */
3373efd8 3900static void ata_dev_xfermask(struct ata_device *dev)
1da177e4 3901{
9af5c9c9
TH
3902 struct ata_link *link = dev->link;
3903 struct ata_port *ap = link->ap;
cca3974e 3904 struct ata_host *host = ap->host;
a6d5a51c 3905 unsigned long xfer_mask;
1da177e4 3906
37deecb5 3907 /* controller modes available */
565083e1
TH
3908 xfer_mask = ata_pack_xfermask(ap->pio_mask,
3909 ap->mwdma_mask, ap->udma_mask);
3910
8343f889 3911 /* drive modes available */
37deecb5
TH
3912 xfer_mask &= ata_pack_xfermask(dev->pio_mask,
3913 dev->mwdma_mask, dev->udma_mask);
3914 xfer_mask &= ata_id_xfermask(dev->id);
565083e1 3915
b352e57d
AC
3916 /*
3917 * CFA Advanced TrueIDE timings are not allowed on a shared
3918 * cable
3919 */
3920 if (ata_dev_pair(dev)) {
3921 /* No PIO5 or PIO6 */
3922 xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
3923 /* No MWDMA3 or MWDMA 4 */
3924 xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
3925 }
3926
37deecb5
TH
3927 if (ata_dma_blacklisted(dev)) {
3928 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
f15a1daf
TH
3929 ata_dev_printk(dev, KERN_WARNING,
3930 "device is on DMA blacklist, disabling DMA\n");
37deecb5 3931 }
a6d5a51c 3932
14d66ab7 3933 if ((host->flags & ATA_HOST_SIMPLEX) &&
2dcb407e 3934 host->simplex_claimed && host->simplex_claimed != ap) {
37deecb5
TH
3935 xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
3936 ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
3937 "other device, disabling DMA\n");
5444a6f4 3938 }
565083e1 3939
e424675f
JG
3940 if (ap->flags & ATA_FLAG_NO_IORDY)
3941 xfer_mask &= ata_pio_mask_no_iordy(dev);
3942
5444a6f4 3943 if (ap->ops->mode_filter)
a76b62ca 3944 xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
5444a6f4 3945
8343f889
RH
3946 /* Apply cable rule here. Don't apply it early because when
3947 * we handle hot plug the cable type can itself change.
3948 * Check this last so that we know if the transfer rate was
3949 * solely limited by the cable.
3950 * Unknown or 80 wire cables reported host side are checked
3951 * drive side as well. Cases where we know a 40wire cable
3952 * is used safely for 80 are not checked here.
3953 */
3954 if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
3955 /* UDMA/44 or higher would be available */
2dcb407e 3956 if ((ap->cbl == ATA_CBL_PATA40) ||
6bbfd53d 3957 (ata_is_40wire(dev) &&
2dcb407e
JG
3958 (ap->cbl == ATA_CBL_PATA_UNK ||
3959 ap->cbl == ATA_CBL_PATA80))) {
3960 ata_dev_printk(dev, KERN_WARNING,
8343f889
RH
3961 "limited to UDMA/33 due to 40-wire cable\n");
3962 xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
3963 }
3964
565083e1
TH
3965 ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
3966 &dev->mwdma_mask, &dev->udma_mask);
1da177e4
LT
3967}
3968
1da177e4
LT
3969/**
3970 * ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
1da177e4
LT
3971 * @dev: Device to which command will be sent
3972 *
780a87f7
JG
3973 * Issue SET FEATURES - XFER MODE command to device @dev
3974 * on port @ap.
3975 *
1da177e4 3976 * LOCKING:
0cba632b 3977 * PCI/etc. bus probe sem.
83206a29
TH
3978 *
3979 * RETURNS:
3980 * 0 on success, AC_ERR_* mask otherwise.
1da177e4
LT
3981 */
3982
3373efd8 3983static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
1da177e4 3984{
a0123703 3985 struct ata_taskfile tf;
83206a29 3986 unsigned int err_mask;
1da177e4
LT
3987
3988 /* set up set-features taskfile */
3989 DPRINTK("set features - xfer mode\n");
3990
464cf177
TH
3991 /* Some controllers and ATAPI devices show flaky interrupt
3992 * behavior after setting xfer mode. Use polling instead.
3993 */
3373efd8 3994 ata_tf_init(dev, &tf);
a0123703
TH
3995 tf.command = ATA_CMD_SET_FEATURES;
3996 tf.feature = SETFEATURES_XFER;
464cf177 3997 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
a0123703 3998 tf.protocol = ATA_PROT_NODATA;
b9f8ab2d 3999 /* If we are using IORDY we must send the mode setting command */
11b7becc
JG
4000 if (ata_pio_need_iordy(dev))
4001 tf.nsect = dev->xfer_mode;
b9f8ab2d
AC
4002 /* If the device has IORDY and the controller does not - turn it off */
4003 else if (ata_id_has_iordy(dev->id))
11b7becc 4004 tf.nsect = 0x01;
b9f8ab2d
AC
4005 else /* In the ancient relic department - skip all of this */
4006 return 0;
1da177e4 4007
2b789108 4008 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
9f45cbd3
KCA
4009
4010 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4011 return err_mask;
4012}
9f45cbd3 4013/**
218f3d30 4014 * ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
9f45cbd3
KCA
4015 * @dev: Device to which command will be sent
4016 * @enable: Whether to enable or disable the feature
218f3d30 4017 * @feature: The sector count represents the feature to set
9f45cbd3
KCA
4018 *
4019 * Issue SET FEATURES - SATA FEATURES command to device @dev
218f3d30 4020 * on port @ap with sector count
9f45cbd3
KCA
4021 *
4022 * LOCKING:
4023 * PCI/etc. bus probe sem.
4024 *
4025 * RETURNS:
4026 * 0 on success, AC_ERR_* mask otherwise.
4027 */
218f3d30
JG
4028static unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable,
4029 u8 feature)
9f45cbd3
KCA
4030{
4031 struct ata_taskfile tf;
4032 unsigned int err_mask;
4033
4034 /* set up set-features taskfile */
4035 DPRINTK("set features - SATA features\n");
4036
4037 ata_tf_init(dev, &tf);
4038 tf.command = ATA_CMD_SET_FEATURES;
4039 tf.feature = enable;
4040 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4041 tf.protocol = ATA_PROT_NODATA;
218f3d30 4042 tf.nsect = feature;
9f45cbd3 4043
2b789108 4044 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1da177e4 4045
83206a29
TH
4046 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4047 return err_mask;
1da177e4
LT
4048}
4049
8bf62ece
AL
4050/**
4051 * ata_dev_init_params - Issue INIT DEV PARAMS command
8bf62ece 4052 * @dev: Device to which command will be sent
e2a7f77a
RD
4053 * @heads: Number of heads (taskfile parameter)
4054 * @sectors: Number of sectors (taskfile parameter)
8bf62ece
AL
4055 *
4056 * LOCKING:
6aff8f1f
TH
4057 * Kernel thread context (may sleep)
4058 *
4059 * RETURNS:
4060 * 0 on success, AC_ERR_* mask otherwise.
8bf62ece 4061 */
3373efd8
TH
4062static unsigned int ata_dev_init_params(struct ata_device *dev,
4063 u16 heads, u16 sectors)
8bf62ece 4064{
a0123703 4065 struct ata_taskfile tf;
6aff8f1f 4066 unsigned int err_mask;
8bf62ece
AL
4067
4068 /* Number of sectors per track 1-255. Number of heads 1-16 */
4069 if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
00b6f5e9 4070 return AC_ERR_INVALID;
8bf62ece
AL
4071
4072 /* set up init dev params taskfile */
4073 DPRINTK("init dev params \n");
4074
3373efd8 4075 ata_tf_init(dev, &tf);
a0123703
TH
4076 tf.command = ATA_CMD_INIT_DEV_PARAMS;
4077 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4078 tf.protocol = ATA_PROT_NODATA;
4079 tf.nsect = sectors;
4080 tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
8bf62ece 4081
2b789108 4082 err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
18b2466c
AC
4083 /* A clean abort indicates an original or just out of spec drive
4084 and we should continue as we issue the setup based on the
4085 drive reported working geometry */
4086 if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
4087 err_mask = 0;
8bf62ece 4088
6aff8f1f
TH
4089 DPRINTK("EXIT, err_mask=%x\n", err_mask);
4090 return err_mask;
8bf62ece
AL
4091}
4092
1da177e4 4093/**
0cba632b
JG
4094 * ata_sg_clean - Unmap DMA memory associated with command
4095 * @qc: Command containing DMA memory to be released
4096 *
4097 * Unmap all mapped DMA memory associated with this command.
1da177e4
LT
4098 *
4099 * LOCKING:
cca3974e 4100 * spin_lock_irqsave(host lock)
1da177e4 4101 */
70e6ad0c 4102void ata_sg_clean(struct ata_queued_cmd *qc)
1da177e4
LT
4103{
4104 struct ata_port *ap = qc->ap;
ff2aeb1e 4105 struct scatterlist *sg = qc->sg;
1da177e4
LT
4106 int dir = qc->dma_dir;
4107
a4631474 4108 WARN_ON(sg == NULL);
1da177e4 4109
dde20207 4110 VPRINTK("unmapping %u sg elements\n", qc->n_elem);
1da177e4 4111
dde20207
JB
4112 if (qc->n_elem)
4113 dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
1da177e4
LT
4114
4115 qc->flags &= ~ATA_QCFLAG_DMAMAP;
ff2aeb1e 4116 qc->sg = NULL;
1da177e4
LT
4117}
4118
1da177e4
LT
4119/**
4120 * ata_check_atapi_dma - Check whether ATAPI DMA can be supported
4121 * @qc: Metadata associated with taskfile to check
4122 *
780a87f7
JG
4123 * Allow low-level driver to filter ATA PACKET commands, returning
4124 * a status indicating whether or not it is OK to use DMA for the
4125 * supplied PACKET command.
4126 *
1da177e4 4127 * LOCKING:
624d5c51
TH
4128 * spin_lock_irqsave(host lock)
4129 *
4130 * RETURNS: 0 when ATAPI DMA can be used
4131 * nonzero otherwise
4132 */
4133int ata_check_atapi_dma(struct ata_queued_cmd *qc)
4134{
4135 struct ata_port *ap = qc->ap;
71601958 4136
624d5c51
TH
4137 /* Don't allow DMA if it isn't multiple of 16 bytes. Quite a
4138 * few ATAPI devices choke on such DMA requests.
4139 */
4140 if (unlikely(qc->nbytes & 15))
4141 return 1;
e2cec771 4142
624d5c51
TH
4143 if (ap->ops->check_atapi_dma)
4144 return ap->ops->check_atapi_dma(qc);
e2cec771 4145
624d5c51
TH
4146 return 0;
4147}
1da177e4 4148
624d5c51
TH
4149/**
4150 * ata_std_qc_defer - Check whether a qc needs to be deferred
4151 * @qc: ATA command in question
4152 *
4153 * Non-NCQ commands cannot run with any other command, NCQ or
4154 * not. As upper layer only knows the queue depth, we are
4155 * responsible for maintaining exclusion. This function checks
4156 * whether a new command @qc can be issued.
4157 *
4158 * LOCKING:
4159 * spin_lock_irqsave(host lock)
4160 *
4161 * RETURNS:
4162 * ATA_DEFER_* if deferring is needed, 0 otherwise.
4163 */
4164int ata_std_qc_defer(struct ata_queued_cmd *qc)
4165{
4166 struct ata_link *link = qc->dev->link;
e2cec771 4167
624d5c51
TH
4168 if (qc->tf.protocol == ATA_PROT_NCQ) {
4169 if (!ata_tag_valid(link->active_tag))
4170 return 0;
4171 } else {
4172 if (!ata_tag_valid(link->active_tag) && !link->sactive)
4173 return 0;
4174 }
e2cec771 4175
624d5c51
TH
4176 return ATA_DEFER_LINK;
4177}
6912ccd5 4178
624d5c51 4179void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
1da177e4 4180
624d5c51
TH
4181/**
4182 * ata_sg_init - Associate command with scatter-gather table.
4183 * @qc: Command to be associated
4184 * @sg: Scatter-gather table.
4185 * @n_elem: Number of elements in s/g table.
4186 *
4187 * Initialize the data-related elements of queued_cmd @qc
4188 * to point to a scatter-gather table @sg, containing @n_elem
4189 * elements.
4190 *
4191 * LOCKING:
4192 * spin_lock_irqsave(host lock)
4193 */
4194void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4195 unsigned int n_elem)
4196{
4197 qc->sg = sg;
4198 qc->n_elem = n_elem;
4199 qc->cursg = qc->sg;
4200}
bb5cb290 4201
624d5c51
TH
4202/**
4203 * ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4204 * @qc: Command with scatter-gather table to be mapped.
4205 *
4206 * DMA-map the scatter-gather table associated with queued_cmd @qc.
4207 *
4208 * LOCKING:
4209 * spin_lock_irqsave(host lock)
4210 *
4211 * RETURNS:
4212 * Zero on success, negative on error.
4213 *
4214 */
4215static int ata_sg_setup(struct ata_queued_cmd *qc)
4216{
4217 struct ata_port *ap = qc->ap;
4218 unsigned int n_elem;
1da177e4 4219
624d5c51 4220 VPRINTK("ENTER, ata%u\n", ap->print_id);
e2cec771 4221
624d5c51
TH
4222 n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4223 if (n_elem < 1)
4224 return -1;
bb5cb290 4225
624d5c51 4226 DPRINTK("%d sg elements mapped\n", n_elem);
bb5cb290 4227
624d5c51
TH
4228 qc->n_elem = n_elem;
4229 qc->flags |= ATA_QCFLAG_DMAMAP;
1da177e4 4230
624d5c51 4231 return 0;
1da177e4
LT
4232}
4233
624d5c51
TH
4234/**
4235 * swap_buf_le16 - swap halves of 16-bit words in place
4236 * @buf: Buffer to swap
4237 * @buf_words: Number of 16-bit words in buffer.
4238 *
4239 * Swap halves of 16-bit words if needed to convert from
4240 * little-endian byte order to native cpu byte order, or
4241 * vice-versa.
4242 *
4243 * LOCKING:
4244 * Inherited from caller.
4245 */
4246void swap_buf_le16(u16 *buf, unsigned int buf_words)
8061f5f0 4247{
624d5c51
TH
4248#ifdef __BIG_ENDIAN
4249 unsigned int i;
8061f5f0 4250
624d5c51
TH
4251 for (i = 0; i < buf_words; i++)
4252 buf[i] = le16_to_cpu(buf[i]);
4253#endif /* __BIG_ENDIAN */
8061f5f0
TH
4254}
4255
1da177e4
LT
4256/**
4257 * ata_qc_new - Request an available ATA command, for queueing
4258 * @ap: Port associated with device @dev
4259 * @dev: Device from whom we request an available command structure
4260 *
4261 * LOCKING:
0cba632b 4262 * None.
1da177e4
LT
4263 */
4264
4265static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
4266{
4267 struct ata_queued_cmd *qc = NULL;
4268 unsigned int i;
4269
e3180499 4270 /* no command while frozen */
b51e9e5d 4271 if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
e3180499
TH
4272 return NULL;
4273
2ab7db1f
TH
4274 /* the last tag is reserved for internal command. */
4275 for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
6cec4a39 4276 if (!test_and_set_bit(i, &ap->qc_allocated)) {
f69499f4 4277 qc = __ata_qc_from_tag(ap, i);
1da177e4
LT
4278 break;
4279 }
4280
4281 if (qc)
4282 qc->tag = i;
4283
4284 return qc;
4285}
4286
4287/**
4288 * ata_qc_new_init - Request an available ATA command, and initialize it
1da177e4
LT
4289 * @dev: Device from whom we request an available command structure
4290 *
4291 * LOCKING:
0cba632b 4292 * None.
1da177e4
LT
4293 */
4294
3373efd8 4295struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
1da177e4 4296{
9af5c9c9 4297 struct ata_port *ap = dev->link->ap;
1da177e4
LT
4298 struct ata_queued_cmd *qc;
4299
4300 qc = ata_qc_new(ap);
4301 if (qc) {
1da177e4
LT
4302 qc->scsicmd = NULL;
4303 qc->ap = ap;
4304 qc->dev = dev;
1da177e4 4305
2c13b7ce 4306 ata_qc_reinit(qc);
1da177e4
LT
4307 }
4308
4309 return qc;
4310}
4311
1da177e4
LT
4312/**
4313 * ata_qc_free - free unused ata_queued_cmd
4314 * @qc: Command to complete
4315 *
4316 * Designed to free unused ata_queued_cmd object
4317 * in case something prevents using it.
4318 *
4319 * LOCKING:
cca3974e 4320 * spin_lock_irqsave(host lock)
1da177e4
LT
4321 */
4322void ata_qc_free(struct ata_queued_cmd *qc)
4323{
4ba946e9
TH
4324 struct ata_port *ap = qc->ap;
4325 unsigned int tag;
4326
a4631474 4327 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
1da177e4 4328
4ba946e9
TH
4329 qc->flags = 0;
4330 tag = qc->tag;
4331 if (likely(ata_tag_valid(tag))) {
4ba946e9 4332 qc->tag = ATA_TAG_POISON;
6cec4a39 4333 clear_bit(tag, &ap->qc_allocated);
4ba946e9 4334 }
1da177e4
LT
4335}
4336
76014427 4337void __ata_qc_complete(struct ata_queued_cmd *qc)
1da177e4 4338{
dedaf2b0 4339 struct ata_port *ap = qc->ap;
9af5c9c9 4340 struct ata_link *link = qc->dev->link;
dedaf2b0 4341
a4631474
TH
4342 WARN_ON(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4343 WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
1da177e4
LT
4344
4345 if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4346 ata_sg_clean(qc);
4347
7401abf2 4348 /* command should be marked inactive atomically with qc completion */
da917d69 4349 if (qc->tf.protocol == ATA_PROT_NCQ) {
9af5c9c9 4350 link->sactive &= ~(1 << qc->tag);
da917d69
TH
4351 if (!link->sactive)
4352 ap->nr_active_links--;
4353 } else {
9af5c9c9 4354 link->active_tag = ATA_TAG_POISON;
da917d69
TH
4355 ap->nr_active_links--;
4356 }
4357
4358 /* clear exclusive status */
4359 if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4360 ap->excl_link == link))
4361 ap->excl_link = NULL;
7401abf2 4362
3f3791d3
AL
4363 /* atapi: mark qc as inactive to prevent the interrupt handler
4364 * from completing the command twice later, before the error handler
4365 * is called. (when rc != 0 and atapi request sense is needed)
4366 */
4367 qc->flags &= ~ATA_QCFLAG_ACTIVE;
dedaf2b0 4368 ap->qc_active &= ~(1 << qc->tag);
3f3791d3 4369
1da177e4 4370 /* call completion callback */
77853bf2 4371 qc->complete_fn(qc);
1da177e4
LT
4372}
4373
39599a53
TH
4374static void fill_result_tf(struct ata_queued_cmd *qc)
4375{
4376 struct ata_port *ap = qc->ap;
4377
39599a53 4378 qc->result_tf.flags = qc->tf.flags;
4742d54f 4379 ap->ops->tf_read(ap, &qc->result_tf);
39599a53
TH
4380}
4381
00115e0f
TH
4382static void ata_verify_xfer(struct ata_queued_cmd *qc)
4383{
4384 struct ata_device *dev = qc->dev;
4385
4386 if (ata_tag_internal(qc->tag))
4387 return;
4388
4389 if (ata_is_nodata(qc->tf.protocol))
4390 return;
4391
4392 if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
4393 return;
4394
4395 dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
4396}
4397
f686bcb8
TH
4398/**
4399 * ata_qc_complete - Complete an active ATA command
4400 * @qc: Command to complete
4401 * @err_mask: ATA Status register contents
4402 *
4403 * Indicate to the mid and upper layers that an ATA
4404 * command has completed, with either an ok or not-ok status.
4405 *
4406 * LOCKING:
cca3974e 4407 * spin_lock_irqsave(host lock)
f686bcb8
TH
4408 */
4409void ata_qc_complete(struct ata_queued_cmd *qc)
4410{
4411 struct ata_port *ap = qc->ap;
4412
4413 /* XXX: New EH and old EH use different mechanisms to
4414 * synchronize EH with regular execution path.
4415 *
4416 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4417 * Normal execution path is responsible for not accessing a
4418 * failed qc. libata core enforces the rule by returning NULL
4419 * from ata_qc_from_tag() for failed qcs.
4420 *
4421 * Old EH depends on ata_qc_complete() nullifying completion
4422 * requests if ATA_QCFLAG_EH_SCHEDULED is set. Old EH does
4423 * not synchronize with interrupt handler. Only PIO task is
4424 * taken care of.
4425 */
4426 if (ap->ops->error_handler) {
4dbfa39b
TH
4427 struct ata_device *dev = qc->dev;
4428 struct ata_eh_info *ehi = &dev->link->eh_info;
4429
b51e9e5d 4430 WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
f686bcb8
TH
4431
4432 if (unlikely(qc->err_mask))
4433 qc->flags |= ATA_QCFLAG_FAILED;
4434
4435 if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4436 if (!ata_tag_internal(qc->tag)) {
4437 /* always fill result TF for failed qc */
39599a53 4438 fill_result_tf(qc);
f686bcb8
TH
4439 ata_qc_schedule_eh(qc);
4440 return;
4441 }
4442 }
4443
4444 /* read result TF if requested */
4445 if (qc->flags & ATA_QCFLAG_RESULT_TF)
39599a53 4446 fill_result_tf(qc);
f686bcb8 4447
4dbfa39b
TH
4448 /* Some commands need post-processing after successful
4449 * completion.
4450 */
4451 switch (qc->tf.command) {
4452 case ATA_CMD_SET_FEATURES:
4453 if (qc->tf.feature != SETFEATURES_WC_ON &&
4454 qc->tf.feature != SETFEATURES_WC_OFF)
4455 break;
4456 /* fall through */
4457 case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
4458 case ATA_CMD_SET_MULTI: /* multi_count changed */
4459 /* revalidate device */
4460 ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
4461 ata_port_schedule_eh(ap);
4462 break;
054a5fba
TH
4463
4464 case ATA_CMD_SLEEP:
4465 dev->flags |= ATA_DFLAG_SLEEPING;
4466 break;
4dbfa39b
TH
4467 }
4468
00115e0f
TH
4469 if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
4470 ata_verify_xfer(qc);
4471
f686bcb8
TH
4472 __ata_qc_complete(qc);
4473 } else {
4474 if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4475 return;
4476
4477 /* read result TF if failed or requested */
4478 if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
39599a53 4479 fill_result_tf(qc);
f686bcb8
TH
4480
4481 __ata_qc_complete(qc);
4482 }
4483}
4484
dedaf2b0
TH
4485/**
4486 * ata_qc_complete_multiple - Complete multiple qcs successfully
4487 * @ap: port in question
4488 * @qc_active: new qc_active mask
4489 * @finish_qc: LLDD callback invoked before completing a qc
4490 *
4491 * Complete in-flight commands. This functions is meant to be
4492 * called from low-level driver's interrupt routine to complete
4493 * requests normally. ap->qc_active and @qc_active is compared
4494 * and commands are completed accordingly.
4495 *
4496 * LOCKING:
cca3974e 4497 * spin_lock_irqsave(host lock)
dedaf2b0
TH
4498 *
4499 * RETURNS:
4500 * Number of completed commands on success, -errno otherwise.
4501 */
4502int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active,
4503 void (*finish_qc)(struct ata_queued_cmd *))
4504{
4505 int nr_done = 0;
4506 u32 done_mask;
4507 int i;
4508
4509 done_mask = ap->qc_active ^ qc_active;
4510
4511 if (unlikely(done_mask & qc_active)) {
4512 ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
4513 "(%08x->%08x)\n", ap->qc_active, qc_active);
4514 return -EINVAL;
4515 }
4516
4517 for (i = 0; i < ATA_MAX_QUEUE; i++) {
4518 struct ata_queued_cmd *qc;
4519
4520 if (!(done_mask & (1 << i)))
4521 continue;
4522
4523 if ((qc = ata_qc_from_tag(ap, i))) {
4524 if (finish_qc)
4525 finish_qc(qc);
4526 ata_qc_complete(qc);
4527 nr_done++;
4528 }
4529 }
4530
4531 return nr_done;
4532}
4533
1da177e4
LT
4534/**
4535 * ata_qc_issue - issue taskfile to device
4536 * @qc: command to issue to device
4537 *
4538 * Prepare an ATA command to submission to device.
4539 * This includes mapping the data into a DMA-able
4540 * area, filling in the S/G table, and finally
4541 * writing the taskfile to hardware, starting the command.
4542 *
4543 * LOCKING:
cca3974e 4544 * spin_lock_irqsave(host lock)
1da177e4 4545 */
8e0e694a 4546void ata_qc_issue(struct ata_queued_cmd *qc)
1da177e4
LT
4547{
4548 struct ata_port *ap = qc->ap;
9af5c9c9 4549 struct ata_link *link = qc->dev->link;
405e66b3 4550 u8 prot = qc->tf.protocol;
1da177e4 4551
dedaf2b0
TH
4552 /* Make sure only one non-NCQ command is outstanding. The
4553 * check is skipped for old EH because it reuses active qc to
4554 * request ATAPI sense.
4555 */
9af5c9c9 4556 WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
dedaf2b0 4557
1973a023 4558 if (ata_is_ncq(prot)) {
9af5c9c9 4559 WARN_ON(link->sactive & (1 << qc->tag));
da917d69
TH
4560
4561 if (!link->sactive)
4562 ap->nr_active_links++;
9af5c9c9 4563 link->sactive |= 1 << qc->tag;
dedaf2b0 4564 } else {
9af5c9c9 4565 WARN_ON(link->sactive);
da917d69
TH
4566
4567 ap->nr_active_links++;
9af5c9c9 4568 link->active_tag = qc->tag;
dedaf2b0
TH
4569 }
4570
e4a70e76 4571 qc->flags |= ATA_QCFLAG_ACTIVE;
dedaf2b0 4572 ap->qc_active |= 1 << qc->tag;
e4a70e76 4573
f92a2636
TH
4574 /* We guarantee to LLDs that they will have at least one
4575 * non-zero sg if the command is a data command.
4576 */
ff2aeb1e 4577 BUG_ON(ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes));
f92a2636 4578
405e66b3 4579 if (ata_is_dma(prot) || (ata_is_pio(prot) &&
f92a2636 4580 (ap->flags & ATA_FLAG_PIO_DMA)))
001102d7
TH
4581 if (ata_sg_setup(qc))
4582 goto sg_err;
1da177e4 4583
cf480626 4584 /* if device is sleeping, schedule reset and abort the link */
054a5fba 4585 if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
cf480626 4586 link->eh_info.action |= ATA_EH_RESET;
054a5fba
TH
4587 ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
4588 ata_link_abort(link);
4589 return;
4590 }
4591
1da177e4
LT
4592 ap->ops->qc_prep(qc);
4593
8e0e694a
TH
4594 qc->err_mask |= ap->ops->qc_issue(qc);
4595 if (unlikely(qc->err_mask))
4596 goto err;
4597 return;
1da177e4 4598
8e436af9 4599sg_err:
8e0e694a
TH
4600 qc->err_mask |= AC_ERR_SYSTEM;
4601err:
4602 ata_qc_complete(qc);
1da177e4
LT
4603}
4604
34bf2170
TH
4605/**
4606 * sata_scr_valid - test whether SCRs are accessible
936fd732 4607 * @link: ATA link to test SCR accessibility for
34bf2170 4608 *
936fd732 4609 * Test whether SCRs are accessible for @link.
34bf2170
TH
4610 *
4611 * LOCKING:
4612 * None.
4613 *
4614 * RETURNS:
4615 * 1 if SCRs are accessible, 0 otherwise.
4616 */
936fd732 4617int sata_scr_valid(struct ata_link *link)
34bf2170 4618{
936fd732
TH
4619 struct ata_port *ap = link->ap;
4620
a16abc0b 4621 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
34bf2170
TH
4622}
4623
4624/**
4625 * sata_scr_read - read SCR register of the specified port
936fd732 4626 * @link: ATA link to read SCR for
34bf2170
TH
4627 * @reg: SCR to read
4628 * @val: Place to store read value
4629 *
936fd732 4630 * Read SCR register @reg of @link into *@val. This function is
633273a3
TH
4631 * guaranteed to succeed if @link is ap->link, the cable type of
4632 * the port is SATA and the port implements ->scr_read.
34bf2170
TH
4633 *
4634 * LOCKING:
633273a3 4635 * None if @link is ap->link. Kernel thread context otherwise.
34bf2170
TH
4636 *
4637 * RETURNS:
4638 * 0 on success, negative errno on failure.
4639 */
936fd732 4640int sata_scr_read(struct ata_link *link, int reg, u32 *val)
34bf2170 4641{
633273a3
TH
4642 if (ata_is_host_link(link)) {
4643 struct ata_port *ap = link->ap;
936fd732 4644
633273a3
TH
4645 if (sata_scr_valid(link))
4646 return ap->ops->scr_read(ap, reg, val);
4647 return -EOPNOTSUPP;
4648 }
4649
4650 return sata_pmp_scr_read(link, reg, val);
34bf2170
TH
4651}
4652
4653/**
4654 * sata_scr_write - write SCR register of the specified port
936fd732 4655 * @link: ATA link to write SCR for
34bf2170
TH
4656 * @reg: SCR to write
4657 * @val: value to write
4658 *
936fd732 4659 * Write @val to SCR register @reg of @link. This function is
633273a3
TH
4660 * guaranteed to succeed if @link is ap->link, the cable type of
4661 * the port is SATA and the port implements ->scr_read.
34bf2170
TH
4662 *
4663 * LOCKING:
633273a3 4664 * None if @link is ap->link. Kernel thread context otherwise.
34bf2170
TH
4665 *
4666 * RETURNS:
4667 * 0 on success, negative errno on failure.
4668 */
936fd732 4669int sata_scr_write(struct ata_link *link, int reg, u32 val)
34bf2170 4670{
633273a3
TH
4671 if (ata_is_host_link(link)) {
4672 struct ata_port *ap = link->ap;
4673
4674 if (sata_scr_valid(link))
4675 return ap->ops->scr_write(ap, reg, val);
4676 return -EOPNOTSUPP;
4677 }
936fd732 4678
633273a3 4679 return sata_pmp_scr_write(link, reg, val);
34bf2170
TH
4680}
4681
4682/**
4683 * sata_scr_write_flush - write SCR register of the specified port and flush
936fd732 4684 * @link: ATA link to write SCR for
34bf2170
TH
4685 * @reg: SCR to write
4686 * @val: value to write
4687 *
4688 * This function is identical to sata_scr_write() except that this
4689 * function performs flush after writing to the register.
4690 *
4691 * LOCKING:
633273a3 4692 * None if @link is ap->link. Kernel thread context otherwise.
34bf2170
TH
4693 *
4694 * RETURNS:
4695 * 0 on success, negative errno on failure.
4696 */
936fd732 4697int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
34bf2170 4698{
633273a3
TH
4699 if (ata_is_host_link(link)) {
4700 struct ata_port *ap = link->ap;
4701 int rc;
da3dbb17 4702
633273a3
TH
4703 if (sata_scr_valid(link)) {
4704 rc = ap->ops->scr_write(ap, reg, val);
4705 if (rc == 0)
4706 rc = ap->ops->scr_read(ap, reg, &val);
4707 return rc;
4708 }
4709 return -EOPNOTSUPP;
34bf2170 4710 }
633273a3
TH
4711
4712 return sata_pmp_scr_write(link, reg, val);
34bf2170
TH
4713}
4714
4715/**
936fd732
TH
4716 * ata_link_online - test whether the given link is online
4717 * @link: ATA link to test
34bf2170 4718 *
936fd732
TH
4719 * Test whether @link is online. Note that this function returns
4720 * 0 if online status of @link cannot be obtained, so
4721 * ata_link_online(link) != !ata_link_offline(link).
34bf2170
TH
4722 *
4723 * LOCKING:
4724 * None.
4725 *
4726 * RETURNS:
4727 * 1 if the port online status is available and online.
4728 */
936fd732 4729int ata_link_online(struct ata_link *link)
34bf2170
TH
4730{
4731 u32 sstatus;
4732
936fd732
TH
4733 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
4734 (sstatus & 0xf) == 0x3)
34bf2170
TH
4735 return 1;
4736 return 0;
4737}
4738
4739/**
936fd732
TH
4740 * ata_link_offline - test whether the given link is offline
4741 * @link: ATA link to test
34bf2170 4742 *
936fd732
TH
4743 * Test whether @link is offline. Note that this function
4744 * returns 0 if offline status of @link cannot be obtained, so
4745 * ata_link_online(link) != !ata_link_offline(link).
34bf2170
TH
4746 *
4747 * LOCKING:
4748 * None.
4749 *
4750 * RETURNS:
4751 * 1 if the port offline status is available and offline.
4752 */
936fd732 4753int ata_link_offline(struct ata_link *link)
34bf2170
TH
4754{
4755 u32 sstatus;
4756
936fd732
TH
4757 if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
4758 (sstatus & 0xf) != 0x3)
34bf2170
TH
4759 return 1;
4760 return 0;
4761}
0baab86b 4762
77b08fb5 4763int ata_flush_cache(struct ata_device *dev)
9b847548 4764{
977e6b9f 4765 unsigned int err_mask;
9b847548
JA
4766 u8 cmd;
4767
4768 if (!ata_try_flush_cache(dev))
4769 return 0;
4770
6fc49adb 4771 if (dev->flags & ATA_DFLAG_FLUSH_EXT)
9b847548
JA
4772 cmd = ATA_CMD_FLUSH_EXT;
4773 else
4774 cmd = ATA_CMD_FLUSH;
4775
4f34337b
AC
4776 /* This is wrong. On a failed flush we get back the LBA of the lost
4777 sector and we should (assuming it wasn't aborted as unknown) issue
2dcb407e 4778 a further flush command to continue the writeback until it
4f34337b 4779 does not error */
977e6b9f
TH
4780 err_mask = ata_do_simple_cmd(dev, cmd);
4781 if (err_mask) {
4782 ata_dev_printk(dev, KERN_ERR, "failed to flush cache\n");
4783 return -EIO;
4784 }
4785
4786 return 0;
9b847548
JA
4787}
4788
6ffa01d8 4789#ifdef CONFIG_PM
cca3974e
JG
4790static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
4791 unsigned int action, unsigned int ehi_flags,
4792 int wait)
500530f6
TH
4793{
4794 unsigned long flags;
4795 int i, rc;
4796
cca3974e
JG
4797 for (i = 0; i < host->n_ports; i++) {
4798 struct ata_port *ap = host->ports[i];
e3667ebf 4799 struct ata_link *link;
500530f6
TH
4800
4801 /* Previous resume operation might still be in
4802 * progress. Wait for PM_PENDING to clear.
4803 */
4804 if (ap->pflags & ATA_PFLAG_PM_PENDING) {
4805 ata_port_wait_eh(ap);
4806 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
4807 }
4808
4809 /* request PM ops to EH */
4810 spin_lock_irqsave(ap->lock, flags);
4811
4812 ap->pm_mesg = mesg;
4813 if (wait) {
4814 rc = 0;
4815 ap->pm_result = &rc;
4816 }
4817
4818 ap->pflags |= ATA_PFLAG_PM_PENDING;
e3667ebf
TH
4819 __ata_port_for_each_link(link, ap) {
4820 link->eh_info.action |= action;
4821 link->eh_info.flags |= ehi_flags;
4822 }
500530f6
TH
4823
4824 ata_port_schedule_eh(ap);
4825
4826 spin_unlock_irqrestore(ap->lock, flags);
4827
4828 /* wait and check result */
4829 if (wait) {
4830 ata_port_wait_eh(ap);
4831 WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
4832 if (rc)
4833 return rc;
4834 }
4835 }
4836
4837 return 0;
4838}
4839
4840/**
cca3974e
JG
4841 * ata_host_suspend - suspend host
4842 * @host: host to suspend
500530f6
TH
4843 * @mesg: PM message
4844 *
cca3974e 4845 * Suspend @host. Actual operation is performed by EH. This
500530f6
TH
4846 * function requests EH to perform PM operations and waits for EH
4847 * to finish.
4848 *
4849 * LOCKING:
4850 * Kernel thread context (may sleep).
4851 *
4852 * RETURNS:
4853 * 0 on success, -errno on failure.
4854 */
cca3974e 4855int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
500530f6 4856{
9666f400 4857 int rc;
500530f6 4858
ca77329f
KCA
4859 /*
4860 * disable link pm on all ports before requesting
4861 * any pm activity
4862 */
4863 ata_lpm_enable(host);
4864
cca3974e 4865 rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
72ad6ec4
JG
4866 if (rc == 0)
4867 host->dev->power.power_state = mesg;
500530f6
TH
4868 return rc;
4869}
4870
4871/**
cca3974e
JG
4872 * ata_host_resume - resume host
4873 * @host: host to resume
500530f6 4874 *
cca3974e 4875 * Resume @host. Actual operation is performed by EH. This
500530f6
TH
4876 * function requests EH to perform PM operations and returns.
4877 * Note that all resume operations are performed parallely.
4878 *
4879 * LOCKING:
4880 * Kernel thread context (may sleep).
4881 */
cca3974e 4882void ata_host_resume(struct ata_host *host)
500530f6 4883{
cf480626 4884 ata_host_request_pm(host, PMSG_ON, ATA_EH_RESET,
cca3974e 4885 ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
72ad6ec4 4886 host->dev->power.power_state = PMSG_ON;
ca77329f
KCA
4887
4888 /* reenable link pm */
4889 ata_lpm_disable(host);
500530f6 4890}
6ffa01d8 4891#endif
500530f6 4892
c893a3ae
RD
4893/**
4894 * ata_port_start - Set port up for dma.
4895 * @ap: Port to initialize
4896 *
4897 * Called just after data structures for each port are
4898 * initialized. Allocates space for PRD table.
4899 *
4900 * May be used as the port_start() entry in ata_port_operations.
4901 *
4902 * LOCKING:
4903 * Inherited from caller.
4904 */
f0d36efd 4905int ata_port_start(struct ata_port *ap)
1da177e4 4906{
2f1f610b 4907 struct device *dev = ap->dev;
1da177e4 4908
f0d36efd
TH
4909 ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
4910 GFP_KERNEL);
1da177e4
LT
4911 if (!ap->prd)
4912 return -ENOMEM;
4913
1da177e4
LT
4914 return 0;
4915}
4916
3ef3b43d
TH
4917/**
4918 * ata_dev_init - Initialize an ata_device structure
4919 * @dev: Device structure to initialize
4920 *
4921 * Initialize @dev in preparation for probing.
4922 *
4923 * LOCKING:
4924 * Inherited from caller.
4925 */
4926void ata_dev_init(struct ata_device *dev)
4927{
9af5c9c9
TH
4928 struct ata_link *link = dev->link;
4929 struct ata_port *ap = link->ap;
72fa4b74
TH
4930 unsigned long flags;
4931
5a04bf4b 4932 /* SATA spd limit is bound to the first device */
9af5c9c9
TH
4933 link->sata_spd_limit = link->hw_sata_spd_limit;
4934 link->sata_spd = 0;
5a04bf4b 4935
72fa4b74
TH
4936 /* High bits of dev->flags are used to record warm plug
4937 * requests which occur asynchronously. Synchronize using
cca3974e 4938 * host lock.
72fa4b74 4939 */
ba6a1308 4940 spin_lock_irqsave(ap->lock, flags);
72fa4b74 4941 dev->flags &= ~ATA_DFLAG_INIT_MASK;
3dcc323f 4942 dev->horkage = 0;
ba6a1308 4943 spin_unlock_irqrestore(ap->lock, flags);
3ef3b43d 4944
72fa4b74
TH
4945 memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
4946 sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
3ef3b43d
TH
4947 dev->pio_mask = UINT_MAX;
4948 dev->mwdma_mask = UINT_MAX;
4949 dev->udma_mask = UINT_MAX;
4950}
4951
4fb37a25
TH
4952/**
4953 * ata_link_init - Initialize an ata_link structure
4954 * @ap: ATA port link is attached to
4955 * @link: Link structure to initialize
8989805d 4956 * @pmp: Port multiplier port number
4fb37a25
TH
4957 *
4958 * Initialize @link.
4959 *
4960 * LOCKING:
4961 * Kernel thread context (may sleep)
4962 */
fb7fd614 4963void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
4fb37a25
TH
4964{
4965 int i;
4966
4967 /* clear everything except for devices */
4968 memset(link, 0, offsetof(struct ata_link, device[0]));
4969
4970 link->ap = ap;
8989805d 4971 link->pmp = pmp;
4fb37a25
TH
4972 link->active_tag = ATA_TAG_POISON;
4973 link->hw_sata_spd_limit = UINT_MAX;
4974
4975 /* can't use iterator, ap isn't initialized yet */
4976 for (i = 0; i < ATA_MAX_DEVICES; i++) {
4977 struct ata_device *dev = &link->device[i];
4978
4979 dev->link = link;
4980 dev->devno = dev - link->device;
4981 ata_dev_init(dev);
4982 }
4983}
4984
4985/**
4986 * sata_link_init_spd - Initialize link->sata_spd_limit
4987 * @link: Link to configure sata_spd_limit for
4988 *
4989 * Initialize @link->[hw_]sata_spd_limit to the currently
4990 * configured value.
4991 *
4992 * LOCKING:
4993 * Kernel thread context (may sleep).
4994 *
4995 * RETURNS:
4996 * 0 on success, -errno on failure.
4997 */
fb7fd614 4998int sata_link_init_spd(struct ata_link *link)
4fb37a25 4999{
33267325
TH
5000 u32 scontrol;
5001 u8 spd;
4fb37a25
TH
5002 int rc;
5003
5004 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
5005 if (rc)
5006 return rc;
5007
5008 spd = (scontrol >> 4) & 0xf;
5009 if (spd)
5010 link->hw_sata_spd_limit &= (1 << spd) - 1;
5011
33267325
TH
5012 ata_force_spd_limit(link);
5013
4fb37a25
TH
5014 link->sata_spd_limit = link->hw_sata_spd_limit;
5015
5016 return 0;
5017}
5018
1da177e4 5019/**
f3187195
TH
5020 * ata_port_alloc - allocate and initialize basic ATA port resources
5021 * @host: ATA host this allocated port belongs to
1da177e4 5022 *
f3187195
TH
5023 * Allocate and initialize basic ATA port resources.
5024 *
5025 * RETURNS:
5026 * Allocate ATA port on success, NULL on failure.
0cba632b 5027 *
1da177e4 5028 * LOCKING:
f3187195 5029 * Inherited from calling layer (may sleep).
1da177e4 5030 */
f3187195 5031struct ata_port *ata_port_alloc(struct ata_host *host)
1da177e4 5032{
f3187195 5033 struct ata_port *ap;
1da177e4 5034
f3187195
TH
5035 DPRINTK("ENTER\n");
5036
5037 ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5038 if (!ap)
5039 return NULL;
5040
f4d6d004 5041 ap->pflags |= ATA_PFLAG_INITIALIZING;
cca3974e 5042 ap->lock = &host->lock;
198e0fed 5043 ap->flags = ATA_FLAG_DISABLED;
f3187195 5044 ap->print_id = -1;
1da177e4 5045 ap->ctl = ATA_DEVCTL_OBS;
cca3974e 5046 ap->host = host;
f3187195 5047 ap->dev = host->dev;
1da177e4 5048 ap->last_ctl = 0xFF;
bd5d825c
BP
5049
5050#if defined(ATA_VERBOSE_DEBUG)
5051 /* turn on all debugging levels */
5052 ap->msg_enable = 0x00FF;
5053#elif defined(ATA_DEBUG)
5054 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
88574551 5055#else
0dd4b21f 5056 ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
bd5d825c 5057#endif
1da177e4 5058
442eacc3 5059 INIT_DELAYED_WORK(&ap->port_task, ata_pio_task);
65f27f38
DH
5060 INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
5061 INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
a72ec4ce 5062 INIT_LIST_HEAD(&ap->eh_done_q);
c6cf9e99 5063 init_waitqueue_head(&ap->eh_wait_q);
5ddf24c5
TH
5064 init_timer_deferrable(&ap->fastdrain_timer);
5065 ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
5066 ap->fastdrain_timer.data = (unsigned long)ap;
1da177e4 5067
838df628 5068 ap->cbl = ATA_CBL_NONE;
838df628 5069
8989805d 5070 ata_link_init(ap, &ap->link, 0);
1da177e4
LT
5071
5072#ifdef ATA_IRQ_TRAP
5073 ap->stats.unhandled_irq = 1;
5074 ap->stats.idle_irq = 1;
5075#endif
1da177e4 5076 return ap;
1da177e4
LT
5077}
5078
f0d36efd
TH
5079static void ata_host_release(struct device *gendev, void *res)
5080{
5081 struct ata_host *host = dev_get_drvdata(gendev);
5082 int i;
5083
1aa506e4
TH
5084 for (i = 0; i < host->n_ports; i++) {
5085 struct ata_port *ap = host->ports[i];
5086
4911487a
TH
5087 if (!ap)
5088 continue;
5089
5090 if (ap->scsi_host)
1aa506e4
TH
5091 scsi_host_put(ap->scsi_host);
5092
633273a3 5093 kfree(ap->pmp_link);
4911487a 5094 kfree(ap);
1aa506e4
TH
5095 host->ports[i] = NULL;
5096 }
5097
1aa56cca 5098 dev_set_drvdata(gendev, NULL);
f0d36efd
TH
5099}
5100
f3187195
TH
5101/**
5102 * ata_host_alloc - allocate and init basic ATA host resources
5103 * @dev: generic device this host is associated with
5104 * @max_ports: maximum number of ATA ports associated with this host
5105 *
5106 * Allocate and initialize basic ATA host resources. LLD calls
5107 * this function to allocate a host, initializes it fully and
5108 * attaches it using ata_host_register().
5109 *
5110 * @max_ports ports are allocated and host->n_ports is
5111 * initialized to @max_ports. The caller is allowed to decrease
5112 * host->n_ports before calling ata_host_register(). The unused
5113 * ports will be automatically freed on registration.
5114 *
5115 * RETURNS:
5116 * Allocate ATA host on success, NULL on failure.
5117 *
5118 * LOCKING:
5119 * Inherited from calling layer (may sleep).
5120 */
5121struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5122{
5123 struct ata_host *host;
5124 size_t sz;
5125 int i;
5126
5127 DPRINTK("ENTER\n");
5128
5129 if (!devres_open_group(dev, NULL, GFP_KERNEL))
5130 return NULL;
5131
5132 /* alloc a container for our list of ATA ports (buses) */
5133 sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5134 /* alloc a container for our list of ATA ports (buses) */
5135 host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
5136 if (!host)
5137 goto err_out;
5138
5139 devres_add(dev, host);
5140 dev_set_drvdata(dev, host);
5141
5142 spin_lock_init(&host->lock);
5143 host->dev = dev;
5144 host->n_ports = max_ports;
5145
5146 /* allocate ports bound to this host */
5147 for (i = 0; i < max_ports; i++) {
5148 struct ata_port *ap;
5149
5150 ap = ata_port_alloc(host);
5151 if (!ap)
5152 goto err_out;
5153
5154 ap->port_no = i;
5155 host->ports[i] = ap;
5156 }
5157
5158 devres_remove_group(dev, NULL);
5159 return host;
5160
5161 err_out:
5162 devres_release_group(dev, NULL);
5163 return NULL;
5164}
5165
f5cda257
TH
5166/**
5167 * ata_host_alloc_pinfo - alloc host and init with port_info array
5168 * @dev: generic device this host is associated with
5169 * @ppi: array of ATA port_info to initialize host with
5170 * @n_ports: number of ATA ports attached to this host
5171 *
5172 * Allocate ATA host and initialize with info from @ppi. If NULL
5173 * terminated, @ppi may contain fewer entries than @n_ports. The
5174 * last entry will be used for the remaining ports.
5175 *
5176 * RETURNS:
5177 * Allocate ATA host on success, NULL on failure.
5178 *
5179 * LOCKING:
5180 * Inherited from calling layer (may sleep).
5181 */
5182struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5183 const struct ata_port_info * const * ppi,
5184 int n_ports)
5185{
5186 const struct ata_port_info *pi;
5187 struct ata_host *host;
5188 int i, j;
5189
5190 host = ata_host_alloc(dev, n_ports);
5191 if (!host)
5192 return NULL;
5193
5194 for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5195 struct ata_port *ap = host->ports[i];
5196
5197 if (ppi[j])
5198 pi = ppi[j++];
5199
5200 ap->pio_mask = pi->pio_mask;
5201 ap->mwdma_mask = pi->mwdma_mask;
5202 ap->udma_mask = pi->udma_mask;
5203 ap->flags |= pi->flags;
0c88758b 5204 ap->link.flags |= pi->link_flags;
f5cda257
TH
5205 ap->ops = pi->port_ops;
5206
5207 if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5208 host->ops = pi->port_ops;
f5cda257
TH
5209 }
5210
5211 return host;
5212}
5213
32ebbc0c
TH
5214static void ata_host_stop(struct device *gendev, void *res)
5215{
5216 struct ata_host *host = dev_get_drvdata(gendev);
5217 int i;
5218
5219 WARN_ON(!(host->flags & ATA_HOST_STARTED));
5220
5221 for (i = 0; i < host->n_ports; i++) {
5222 struct ata_port *ap = host->ports[i];
5223
5224 if (ap->ops->port_stop)
5225 ap->ops->port_stop(ap);
5226 }
5227
5228 if (host->ops->host_stop)
5229 host->ops->host_stop(host);
5230}
5231
029cfd6b
TH
5232/**
5233 * ata_finalize_port_ops - finalize ata_port_operations
5234 * @ops: ata_port_operations to finalize
5235 *
5236 * An ata_port_operations can inherit from another ops and that
5237 * ops can again inherit from another. This can go on as many
5238 * times as necessary as long as there is no loop in the
5239 * inheritance chain.
5240 *
5241 * Ops tables are finalized when the host is started. NULL or
5242 * unspecified entries are inherited from the closet ancestor
5243 * which has the method and the entry is populated with it.
5244 * After finalization, the ops table directly points to all the
5245 * methods and ->inherits is no longer necessary and cleared.
5246 *
5247 * Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5248 *
5249 * LOCKING:
5250 * None.
5251 */
5252static void ata_finalize_port_ops(struct ata_port_operations *ops)
5253{
5254 static spinlock_t lock = SPIN_LOCK_UNLOCKED;
5255 const struct ata_port_operations *cur;
5256 void **begin = (void **)ops;
5257 void **end = (void **)&ops->inherits;
5258 void **pp;
5259
5260 if (!ops || !ops->inherits)
5261 return;
5262
5263 spin_lock(&lock);
5264
5265 for (cur = ops->inherits; cur; cur = cur->inherits) {
5266 void **inherit = (void **)cur;
5267
5268 for (pp = begin; pp < end; pp++, inherit++)
5269 if (!*pp)
5270 *pp = *inherit;
5271 }
5272
5273 for (pp = begin; pp < end; pp++)
5274 if (IS_ERR(*pp))
5275 *pp = NULL;
5276
5277 ops->inherits = NULL;
5278
5279 spin_unlock(&lock);
5280}
5281
ecef7253
TH
5282/**
5283 * ata_host_start - start and freeze ports of an ATA host
5284 * @host: ATA host to start ports for
5285 *
5286 * Start and then freeze ports of @host. Started status is
5287 * recorded in host->flags, so this function can be called
5288 * multiple times. Ports are guaranteed to get started only
f3187195
TH
5289 * once. If host->ops isn't initialized yet, its set to the
5290 * first non-dummy port ops.
ecef7253
TH
5291 *
5292 * LOCKING:
5293 * Inherited from calling layer (may sleep).
5294 *
5295 * RETURNS:
5296 * 0 if all ports are started successfully, -errno otherwise.
5297 */
5298int ata_host_start(struct ata_host *host)
5299{
32ebbc0c
TH
5300 int have_stop = 0;
5301 void *start_dr = NULL;
ecef7253
TH
5302 int i, rc;
5303
5304 if (host->flags & ATA_HOST_STARTED)
5305 return 0;
5306
029cfd6b
TH
5307 ata_finalize_port_ops(host->ops);
5308
ecef7253
TH
5309 for (i = 0; i < host->n_ports; i++) {
5310 struct ata_port *ap = host->ports[i];
5311
029cfd6b
TH
5312 ata_finalize_port_ops(ap->ops);
5313
f3187195
TH
5314 if (!host->ops && !ata_port_is_dummy(ap))
5315 host->ops = ap->ops;
5316
32ebbc0c
TH
5317 if (ap->ops->port_stop)
5318 have_stop = 1;
5319 }
5320
5321 if (host->ops->host_stop)
5322 have_stop = 1;
5323
5324 if (have_stop) {
5325 start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
5326 if (!start_dr)
5327 return -ENOMEM;
5328 }
5329
5330 for (i = 0; i < host->n_ports; i++) {
5331 struct ata_port *ap = host->ports[i];
5332
ecef7253
TH
5333 if (ap->ops->port_start) {
5334 rc = ap->ops->port_start(ap);
5335 if (rc) {
0f9fe9b7 5336 if (rc != -ENODEV)
0f757743
AM
5337 dev_printk(KERN_ERR, host->dev,
5338 "failed to start port %d "
5339 "(errno=%d)\n", i, rc);
ecef7253
TH
5340 goto err_out;
5341 }
5342 }
ecef7253
TH
5343 ata_eh_freeze_port(ap);
5344 }
5345
32ebbc0c
TH
5346 if (start_dr)
5347 devres_add(host->dev, start_dr);
ecef7253
TH
5348 host->flags |= ATA_HOST_STARTED;
5349 return 0;
5350
5351 err_out:
5352 while (--i >= 0) {
5353 struct ata_port *ap = host->ports[i];
5354
5355 if (ap->ops->port_stop)
5356 ap->ops->port_stop(ap);
5357 }
32ebbc0c 5358 devres_free(start_dr);
ecef7253
TH
5359 return rc;
5360}
5361
b03732f0 5362/**
cca3974e
JG
5363 * ata_sas_host_init - Initialize a host struct
5364 * @host: host to initialize
5365 * @dev: device host is attached to
5366 * @flags: host flags
5367 * @ops: port_ops
b03732f0
BK
5368 *
5369 * LOCKING:
5370 * PCI/etc. bus probe sem.
5371 *
5372 */
f3187195 5373/* KILLME - the only user left is ipr */
cca3974e 5374void ata_host_init(struct ata_host *host, struct device *dev,
029cfd6b 5375 unsigned long flags, struct ata_port_operations *ops)
b03732f0 5376{
cca3974e
JG
5377 spin_lock_init(&host->lock);
5378 host->dev = dev;
5379 host->flags = flags;
5380 host->ops = ops;
b03732f0
BK
5381}
5382
f3187195
TH
5383/**
5384 * ata_host_register - register initialized ATA host
5385 * @host: ATA host to register
5386 * @sht: template for SCSI host
5387 *
5388 * Register initialized ATA host. @host is allocated using
5389 * ata_host_alloc() and fully initialized by LLD. This function
5390 * starts ports, registers @host with ATA and SCSI layers and
5391 * probe registered devices.
5392 *
5393 * LOCKING:
5394 * Inherited from calling layer (may sleep).
5395 *
5396 * RETURNS:
5397 * 0 on success, -errno otherwise.
5398 */
5399int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
5400{
5401 int i, rc;
5402
5403 /* host must have been started */
5404 if (!(host->flags & ATA_HOST_STARTED)) {
5405 dev_printk(KERN_ERR, host->dev,
5406 "BUG: trying to register unstarted host\n");
5407 WARN_ON(1);
5408 return -EINVAL;
5409 }
5410
5411 /* Blow away unused ports. This happens when LLD can't
5412 * determine the exact number of ports to allocate at
5413 * allocation time.
5414 */
5415 for (i = host->n_ports; host->ports[i]; i++)
5416 kfree(host->ports[i]);
5417
5418 /* give ports names and add SCSI hosts */
5419 for (i = 0; i < host->n_ports; i++)
5420 host->ports[i]->print_id = ata_print_id++;
5421
5422 rc = ata_scsi_add_hosts(host, sht);
5423 if (rc)
5424 return rc;
5425
fafbae87
TH
5426 /* associate with ACPI nodes */
5427 ata_acpi_associate(host);
5428
f3187195
TH
5429 /* set cable, sata_spd_limit and report */
5430 for (i = 0; i < host->n_ports; i++) {
5431 struct ata_port *ap = host->ports[i];
f3187195
TH
5432 unsigned long xfer_mask;
5433
5434 /* set SATA cable type if still unset */
5435 if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
5436 ap->cbl = ATA_CBL_SATA;
5437
5438 /* init sata_spd_limit to the current value */
4fb37a25 5439 sata_link_init_spd(&ap->link);
f3187195 5440
cbcdd875 5441 /* print per-port info to dmesg */
f3187195
TH
5442 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
5443 ap->udma_mask);
5444
abf6e8ed 5445 if (!ata_port_is_dummy(ap)) {
cbcdd875
TH
5446 ata_port_printk(ap, KERN_INFO,
5447 "%cATA max %s %s\n",
a16abc0b 5448 (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
f3187195 5449 ata_mode_string(xfer_mask),
cbcdd875 5450 ap->link.eh_info.desc);
abf6e8ed
TH
5451 ata_ehi_clear_desc(&ap->link.eh_info);
5452 } else
f3187195
TH
5453 ata_port_printk(ap, KERN_INFO, "DUMMY\n");
5454 }
5455
5456 /* perform each probe synchronously */
5457 DPRINTK("probe begin\n");
5458 for (i = 0; i < host->n_ports; i++) {
5459 struct ata_port *ap = host->ports[i];
f3187195
TH
5460
5461 /* probe */
5462 if (ap->ops->error_handler) {
9af5c9c9 5463 struct ata_eh_info *ehi = &ap->link.eh_info;
f3187195
TH
5464 unsigned long flags;
5465
5466 ata_port_probe(ap);
5467
5468 /* kick EH for boot probing */
5469 spin_lock_irqsave(ap->lock, flags);
5470
b558eddd 5471 ehi->probe_mask |= ATA_ALL_DEVICES;
cf480626 5472 ehi->action |= ATA_EH_RESET;
f3187195
TH
5473 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
5474
f4d6d004 5475 ap->pflags &= ~ATA_PFLAG_INITIALIZING;
f3187195
TH
5476 ap->pflags |= ATA_PFLAG_LOADING;
5477 ata_port_schedule_eh(ap);
5478
5479 spin_unlock_irqrestore(ap->lock, flags);
5480
5481 /* wait for EH to finish */
5482 ata_port_wait_eh(ap);
5483 } else {
5484 DPRINTK("ata%u: bus probe begin\n", ap->print_id);
5485 rc = ata_bus_probe(ap);
5486 DPRINTK("ata%u: bus probe end\n", ap->print_id);
5487
5488 if (rc) {
5489 /* FIXME: do something useful here?
5490 * Current libata behavior will
5491 * tear down everything when
5492 * the module is removed
5493 * or the h/w is unplugged.
5494 */
5495 }
5496 }
5497 }
5498
5499 /* probes are done, now scan each port's disk(s) */
5500 DPRINTK("host probe begin\n");
5501 for (i = 0; i < host->n_ports; i++) {
5502 struct ata_port *ap = host->ports[i];
5503
1ae46317 5504 ata_scsi_scan_host(ap, 1);
ca77329f 5505 ata_lpm_schedule(ap, ap->pm_policy);
f3187195
TH
5506 }
5507
5508 return 0;
5509}
5510
f5cda257
TH
5511/**
5512 * ata_host_activate - start host, request IRQ and register it
5513 * @host: target ATA host
5514 * @irq: IRQ to request
5515 * @irq_handler: irq_handler used when requesting IRQ
5516 * @irq_flags: irq_flags used when requesting IRQ
5517 * @sht: scsi_host_template to use when registering the host
5518 *
5519 * After allocating an ATA host and initializing it, most libata
5520 * LLDs perform three steps to activate the host - start host,
5521 * request IRQ and register it. This helper takes necessasry
5522 * arguments and performs the three steps in one go.
5523 *
3d46b2e2
PM
5524 * An invalid IRQ skips the IRQ registration and expects the host to
5525 * have set polling mode on the port. In this case, @irq_handler
5526 * should be NULL.
5527 *
f5cda257
TH
5528 * LOCKING:
5529 * Inherited from calling layer (may sleep).
5530 *
5531 * RETURNS:
5532 * 0 on success, -errno otherwise.
5533 */
5534int ata_host_activate(struct ata_host *host, int irq,
5535 irq_handler_t irq_handler, unsigned long irq_flags,
5536 struct scsi_host_template *sht)
5537{
cbcdd875 5538 int i, rc;
f5cda257
TH
5539
5540 rc = ata_host_start(host);
5541 if (rc)
5542 return rc;
5543
3d46b2e2
PM
5544 /* Special case for polling mode */
5545 if (!irq) {
5546 WARN_ON(irq_handler);
5547 return ata_host_register(host, sht);
5548 }
5549
f5cda257
TH
5550 rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
5551 dev_driver_string(host->dev), host);
5552 if (rc)
5553 return rc;
5554
cbcdd875
TH
5555 for (i = 0; i < host->n_ports; i++)
5556 ata_port_desc(host->ports[i], "irq %d", irq);
4031826b 5557
f5cda257
TH
5558 rc = ata_host_register(host, sht);
5559 /* if failed, just free the IRQ and leave ports alone */
5560 if (rc)
5561 devm_free_irq(host->dev, irq, host);
5562
5563 return rc;
5564}
5565
720ba126
TH
5566/**
5567 * ata_port_detach - Detach ATA port in prepration of device removal
5568 * @ap: ATA port to be detached
5569 *
5570 * Detach all ATA devices and the associated SCSI devices of @ap;
5571 * then, remove the associated SCSI host. @ap is guaranteed to
5572 * be quiescent on return from this function.
5573 *
5574 * LOCKING:
5575 * Kernel thread context (may sleep).
5576 */
741b7763 5577static void ata_port_detach(struct ata_port *ap)
720ba126
TH
5578{
5579 unsigned long flags;
41bda9c9 5580 struct ata_link *link;
f58229f8 5581 struct ata_device *dev;
720ba126
TH
5582
5583 if (!ap->ops->error_handler)
c3cf30a9 5584 goto skip_eh;
720ba126
TH
5585
5586 /* tell EH we're leaving & flush EH */
ba6a1308 5587 spin_lock_irqsave(ap->lock, flags);
b51e9e5d 5588 ap->pflags |= ATA_PFLAG_UNLOADING;
ba6a1308 5589 spin_unlock_irqrestore(ap->lock, flags);
720ba126
TH
5590
5591 ata_port_wait_eh(ap);
5592
7f9ad9b8
TH
5593 /* EH is now guaranteed to see UNLOADING - EH context belongs
5594 * to us. Disable all existing devices.
720ba126 5595 */
41bda9c9
TH
5596 ata_port_for_each_link(link, ap) {
5597 ata_link_for_each_dev(dev, link)
5598 ata_dev_disable(dev);
5599 }
720ba126 5600
720ba126
TH
5601 /* Final freeze & EH. All in-flight commands are aborted. EH
5602 * will be skipped and retrials will be terminated with bad
5603 * target.
5604 */
ba6a1308 5605 spin_lock_irqsave(ap->lock, flags);
720ba126 5606 ata_port_freeze(ap); /* won't be thawed */
ba6a1308 5607 spin_unlock_irqrestore(ap->lock, flags);
720ba126
TH
5608
5609 ata_port_wait_eh(ap);
45a66c1c 5610 cancel_rearming_delayed_work(&ap->hotplug_task);
720ba126 5611
c3cf30a9 5612 skip_eh:
720ba126 5613 /* remove the associated SCSI host */
cca3974e 5614 scsi_remove_host(ap->scsi_host);
720ba126
TH
5615}
5616
0529c159
TH
5617/**
5618 * ata_host_detach - Detach all ports of an ATA host
5619 * @host: Host to detach
5620 *
5621 * Detach all ports of @host.
5622 *
5623 * LOCKING:
5624 * Kernel thread context (may sleep).
5625 */
5626void ata_host_detach(struct ata_host *host)
5627{
5628 int i;
5629
5630 for (i = 0; i < host->n_ports; i++)
5631 ata_port_detach(host->ports[i]);
562f0c2d
TH
5632
5633 /* the host is dead now, dissociate ACPI */
5634 ata_acpi_dissociate(host);
0529c159
TH
5635}
5636
374b1873
JG
5637#ifdef CONFIG_PCI
5638
1da177e4
LT
5639/**
5640 * ata_pci_remove_one - PCI layer callback for device removal
5641 * @pdev: PCI device that was removed
5642 *
b878ca5d
TH
5643 * PCI layer indicates to libata via this hook that hot-unplug or
5644 * module unload event has occurred. Detach all ports. Resource
5645 * release is handled via devres.
1da177e4
LT
5646 *
5647 * LOCKING:
5648 * Inherited from PCI layer (may sleep).
5649 */
f0d36efd 5650void ata_pci_remove_one(struct pci_dev *pdev)
1da177e4 5651{
2855568b 5652 struct device *dev = &pdev->dev;
cca3974e 5653 struct ata_host *host = dev_get_drvdata(dev);
1da177e4 5654
b878ca5d 5655 ata_host_detach(host);
1da177e4
LT
5656}
5657
5658/* move to PCI subsystem */
057ace5e 5659int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
1da177e4
LT
5660{
5661 unsigned long tmp = 0;
5662
5663 switch (bits->width) {
5664 case 1: {
5665 u8 tmp8 = 0;
5666 pci_read_config_byte(pdev, bits->reg, &tmp8);
5667 tmp = tmp8;
5668 break;
5669 }
5670 case 2: {
5671 u16 tmp16 = 0;
5672 pci_read_config_word(pdev, bits->reg, &tmp16);
5673 tmp = tmp16;
5674 break;
5675 }
5676 case 4: {
5677 u32 tmp32 = 0;
5678 pci_read_config_dword(pdev, bits->reg, &tmp32);
5679 tmp = tmp32;
5680 break;
5681 }
5682
5683 default:
5684 return -EINVAL;
5685 }
5686
5687 tmp &= bits->mask;
5688
5689 return (tmp == bits->val) ? 1 : 0;
5690}
9b847548 5691
6ffa01d8 5692#ifdef CONFIG_PM
3c5100c1 5693void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
9b847548
JA
5694{
5695 pci_save_state(pdev);
4c90d971 5696 pci_disable_device(pdev);
500530f6 5697
3a2d5b70 5698 if (mesg.event & PM_EVENT_SLEEP)
500530f6 5699 pci_set_power_state(pdev, PCI_D3hot);
9b847548
JA
5700}
5701
553c4aa6 5702int ata_pci_device_do_resume(struct pci_dev *pdev)
9b847548 5703{
553c4aa6
TH
5704 int rc;
5705
9b847548
JA
5706 pci_set_power_state(pdev, PCI_D0);
5707 pci_restore_state(pdev);
553c4aa6 5708
b878ca5d 5709 rc = pcim_enable_device(pdev);
553c4aa6
TH
5710 if (rc) {
5711 dev_printk(KERN_ERR, &pdev->dev,
5712 "failed to enable device after resume (%d)\n", rc);
5713 return rc;
5714 }
5715
9b847548 5716 pci_set_master(pdev);
553c4aa6 5717 return 0;
500530f6
TH
5718}
5719
3c5100c1 5720int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
500530f6 5721{
cca3974e 5722 struct ata_host *host = dev_get_drvdata(&pdev->dev);
500530f6
TH
5723 int rc = 0;
5724
cca3974e 5725 rc = ata_host_suspend(host, mesg);
500530f6
TH
5726 if (rc)
5727 return rc;
5728
3c5100c1 5729 ata_pci_device_do_suspend(pdev, mesg);
500530f6
TH
5730
5731 return 0;
5732}
5733
5734int ata_pci_device_resume(struct pci_dev *pdev)
5735{
cca3974e 5736 struct ata_host *host = dev_get_drvdata(&pdev->dev);
553c4aa6 5737 int rc;
500530f6 5738
553c4aa6
TH
5739 rc = ata_pci_device_do_resume(pdev);
5740 if (rc == 0)
5741 ata_host_resume(host);
5742 return rc;
9b847548 5743}
6ffa01d8
TH
5744#endif /* CONFIG_PM */
5745
1da177e4
LT
5746#endif /* CONFIG_PCI */
5747
33267325
TH
5748static int __init ata_parse_force_one(char **cur,
5749 struct ata_force_ent *force_ent,
5750 const char **reason)
5751{
5752 /* FIXME: Currently, there's no way to tag init const data and
5753 * using __initdata causes build failure on some versions of
5754 * gcc. Once __initdataconst is implemented, add const to the
5755 * following structure.
5756 */
5757 static struct ata_force_param force_tbl[] __initdata = {
5758 { "40c", .cbl = ATA_CBL_PATA40 },
5759 { "80c", .cbl = ATA_CBL_PATA80 },
5760 { "short40c", .cbl = ATA_CBL_PATA40_SHORT },
5761 { "unk", .cbl = ATA_CBL_PATA_UNK },
5762 { "ign", .cbl = ATA_CBL_PATA_IGN },
5763 { "sata", .cbl = ATA_CBL_SATA },
5764 { "1.5Gbps", .spd_limit = 1 },
5765 { "3.0Gbps", .spd_limit = 2 },
5766 { "noncq", .horkage_on = ATA_HORKAGE_NONCQ },
5767 { "ncq", .horkage_off = ATA_HORKAGE_NONCQ },
5768 { "pio0", .xfer_mask = 1 << (ATA_SHIFT_PIO + 0) },
5769 { "pio1", .xfer_mask = 1 << (ATA_SHIFT_PIO + 1) },
5770 { "pio2", .xfer_mask = 1 << (ATA_SHIFT_PIO + 2) },
5771 { "pio3", .xfer_mask = 1 << (ATA_SHIFT_PIO + 3) },
5772 { "pio4", .xfer_mask = 1 << (ATA_SHIFT_PIO + 4) },
5773 { "pio5", .xfer_mask = 1 << (ATA_SHIFT_PIO + 5) },
5774 { "pio6", .xfer_mask = 1 << (ATA_SHIFT_PIO + 6) },
5775 { "mwdma0", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 0) },
5776 { "mwdma1", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 1) },
5777 { "mwdma2", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 2) },
5778 { "mwdma3", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 3) },
5779 { "mwdma4", .xfer_mask = 1 << (ATA_SHIFT_MWDMA + 4) },
5780 { "udma0", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
5781 { "udma16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
5782 { "udma/16", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 0) },
5783 { "udma1", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
5784 { "udma25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
5785 { "udma/25", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 1) },
5786 { "udma2", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
5787 { "udma33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
5788 { "udma/33", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 2) },
5789 { "udma3", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
5790 { "udma44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
5791 { "udma/44", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 3) },
5792 { "udma4", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
5793 { "udma66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
5794 { "udma/66", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 4) },
5795 { "udma5", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
5796 { "udma100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
5797 { "udma/100", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 5) },
5798 { "udma6", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
5799 { "udma133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
5800 { "udma/133", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 6) },
5801 { "udma7", .xfer_mask = 1 << (ATA_SHIFT_UDMA + 7) },
5802 };
5803 char *start = *cur, *p = *cur;
5804 char *id, *val, *endp;
5805 const struct ata_force_param *match_fp = NULL;
5806 int nr_matches = 0, i;
5807
5808 /* find where this param ends and update *cur */
5809 while (*p != '\0' && *p != ',')
5810 p++;
5811
5812 if (*p == '\0')
5813 *cur = p;
5814 else
5815 *cur = p + 1;
5816
5817 *p = '\0';
5818
5819 /* parse */
5820 p = strchr(start, ':');
5821 if (!p) {
5822 val = strstrip(start);
5823 goto parse_val;
5824 }
5825 *p = '\0';
5826
5827 id = strstrip(start);
5828 val = strstrip(p + 1);
5829
5830 /* parse id */
5831 p = strchr(id, '.');
5832 if (p) {
5833 *p++ = '\0';
5834 force_ent->device = simple_strtoul(p, &endp, 10);
5835 if (p == endp || *endp != '\0') {
5836 *reason = "invalid device";
5837 return -EINVAL;
5838 }
5839 }
5840
5841 force_ent->port = simple_strtoul(id, &endp, 10);
5842 if (p == endp || *endp != '\0') {
5843 *reason = "invalid port/link";
5844 return -EINVAL;
5845 }
5846
5847 parse_val:
5848 /* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
5849 for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
5850 const struct ata_force_param *fp = &force_tbl[i];
5851
5852 if (strncasecmp(val, fp->name, strlen(val)))
5853 continue;
5854
5855 nr_matches++;
5856 match_fp = fp;
5857
5858 if (strcasecmp(val, fp->name) == 0) {
5859 nr_matches = 1;
5860 break;
5861 }
5862 }
5863
5864 if (!nr_matches) {
5865 *reason = "unknown value";
5866 return -EINVAL;
5867 }
5868 if (nr_matches > 1) {
5869 *reason = "ambigious value";
5870 return -EINVAL;
5871 }
5872
5873 force_ent->param = *match_fp;
5874
5875 return 0;
5876}
5877
5878static void __init ata_parse_force_param(void)
5879{
5880 int idx = 0, size = 1;
5881 int last_port = -1, last_device = -1;
5882 char *p, *cur, *next;
5883
5884 /* calculate maximum number of params and allocate force_tbl */
5885 for (p = ata_force_param_buf; *p; p++)
5886 if (*p == ',')
5887 size++;
5888
5889 ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
5890 if (!ata_force_tbl) {
5891 printk(KERN_WARNING "ata: failed to extend force table, "
5892 "libata.force ignored\n");
5893 return;
5894 }
5895
5896 /* parse and populate the table */
5897 for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
5898 const char *reason = "";
5899 struct ata_force_ent te = { .port = -1, .device = -1 };
5900
5901 next = cur;
5902 if (ata_parse_force_one(&next, &te, &reason)) {
5903 printk(KERN_WARNING "ata: failed to parse force "
5904 "parameter \"%s\" (%s)\n",
5905 cur, reason);
5906 continue;
5907 }
5908
5909 if (te.port == -1) {
5910 te.port = last_port;
5911 te.device = last_device;
5912 }
5913
5914 ata_force_tbl[idx++] = te;
5915
5916 last_port = te.port;
5917 last_device = te.device;
5918 }
5919
5920 ata_force_tbl_size = idx;
5921}
1da177e4 5922
1da177e4
LT
5923static int __init ata_init(void)
5924{
a8601e5f 5925 ata_probe_timeout *= HZ;
33267325
TH
5926
5927 ata_parse_force_param();
5928
1da177e4
LT
5929 ata_wq = create_workqueue("ata");
5930 if (!ata_wq)
5931 return -ENOMEM;
5932
453b07ac
TH
5933 ata_aux_wq = create_singlethread_workqueue("ata_aux");
5934 if (!ata_aux_wq) {
5935 destroy_workqueue(ata_wq);
5936 return -ENOMEM;
5937 }
5938
1da177e4
LT
5939 printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
5940 return 0;
5941}
5942
5943static void __exit ata_exit(void)
5944{
33267325 5945 kfree(ata_force_tbl);
1da177e4 5946 destroy_workqueue(ata_wq);
453b07ac 5947 destroy_workqueue(ata_aux_wq);
1da177e4
LT
5948}
5949
a4625085 5950subsys_initcall(ata_init);
1da177e4
LT
5951module_exit(ata_exit);
5952
67846b30 5953static unsigned long ratelimit_time;
34af946a 5954static DEFINE_SPINLOCK(ata_ratelimit_lock);
67846b30
JG
5955
5956int ata_ratelimit(void)
5957{
5958 int rc;
5959 unsigned long flags;
5960
5961 spin_lock_irqsave(&ata_ratelimit_lock, flags);
5962
5963 if (time_after(jiffies, ratelimit_time)) {
5964 rc = 1;
5965 ratelimit_time = jiffies + (HZ/5);
5966 } else
5967 rc = 0;
5968
5969 spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
5970
5971 return rc;
5972}
5973
c22daff4
TH
5974/**
5975 * ata_wait_register - wait until register value changes
5976 * @reg: IO-mapped register
5977 * @mask: Mask to apply to read register value
5978 * @val: Wait condition
5979 * @interval_msec: polling interval in milliseconds
5980 * @timeout_msec: timeout in milliseconds
5981 *
5982 * Waiting for some bits of register to change is a common
5983 * operation for ATA controllers. This function reads 32bit LE
5984 * IO-mapped register @reg and tests for the following condition.
5985 *
5986 * (*@reg & mask) != val
5987 *
5988 * If the condition is met, it returns; otherwise, the process is
5989 * repeated after @interval_msec until timeout.
5990 *
5991 * LOCKING:
5992 * Kernel thread context (may sleep)
5993 *
5994 * RETURNS:
5995 * The final register value.
5996 */
5997u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
5998 unsigned long interval_msec,
5999 unsigned long timeout_msec)
6000{
6001 unsigned long timeout;
6002 u32 tmp;
6003
6004 tmp = ioread32(reg);
6005
6006 /* Calculate timeout _after_ the first read to make sure
6007 * preceding writes reach the controller before starting to
6008 * eat away the timeout.
6009 */
6010 timeout = jiffies + (timeout_msec * HZ) / 1000;
6011
6012 while ((tmp & mask) == val && time_before(jiffies, timeout)) {
6013 msleep(interval_msec);
6014 tmp = ioread32(reg);
6015 }
6016
6017 return tmp;
6018}
6019
dd5b06c4
TH
6020/*
6021 * Dummy port_ops
6022 */
6023static void ata_dummy_noret(struct ata_port *ap) { }
6024static int ata_dummy_ret0(struct ata_port *ap) { return 0; }
6025static void ata_dummy_qc_noret(struct ata_queued_cmd *qc) { }
6026
6027static u8 ata_dummy_check_status(struct ata_port *ap)
6028{
6029 return ATA_DRDY;
6030}
6031
6032static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6033{
6034 return AC_ERR_SYSTEM;
6035}
6036
029cfd6b 6037struct ata_port_operations ata_dummy_port_ops = {
dd5b06c4
TH
6038 .check_status = ata_dummy_check_status,
6039 .check_altstatus = ata_dummy_check_status,
6040 .dev_select = ata_noop_dev_select,
6041 .qc_prep = ata_noop_qc_prep,
6042 .qc_issue = ata_dummy_qc_issue,
6043 .freeze = ata_dummy_noret,
6044 .thaw = ata_dummy_noret,
6045 .error_handler = ata_dummy_noret,
6046 .post_internal_cmd = ata_dummy_qc_noret,
6047 .irq_clear = ata_dummy_noret,
6048 .port_start = ata_dummy_ret0,
6049 .port_stop = ata_dummy_noret,
6050};
6051
21b0ad4f
TH
6052const struct ata_port_info ata_dummy_port_info = {
6053 .port_ops = &ata_dummy_port_ops,
6054};
6055
1da177e4
LT
6056/*
6057 * libata is essentially a library of internal helper functions for
6058 * low-level ATA host controller drivers. As such, the API/ABI is
6059 * likely to change as new drivers are added and updated.
6060 * Do not depend on ABI/API stability.
6061 */
e9c83914
TH
6062EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
6063EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
6064EXPORT_SYMBOL_GPL(sata_deb_timing_long);
029cfd6b
TH
6065EXPORT_SYMBOL_GPL(ata_base_port_ops);
6066EXPORT_SYMBOL_GPL(sata_port_ops);
6067EXPORT_SYMBOL_GPL(sata_pmp_port_ops);
dd5b06c4 6068EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
21b0ad4f 6069EXPORT_SYMBOL_GPL(ata_dummy_port_info);
1da177e4 6070EXPORT_SYMBOL_GPL(ata_std_bios_param);
cca3974e 6071EXPORT_SYMBOL_GPL(ata_host_init);
f3187195 6072EXPORT_SYMBOL_GPL(ata_host_alloc);
f5cda257 6073EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
ecef7253 6074EXPORT_SYMBOL_GPL(ata_host_start);
f3187195 6075EXPORT_SYMBOL_GPL(ata_host_register);
f5cda257 6076EXPORT_SYMBOL_GPL(ata_host_activate);
0529c159 6077EXPORT_SYMBOL_GPL(ata_host_detach);
1da177e4 6078EXPORT_SYMBOL_GPL(ata_sg_init);
f686bcb8 6079EXPORT_SYMBOL_GPL(ata_qc_complete);
dedaf2b0 6080EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
1da177e4 6081EXPORT_SYMBOL_GPL(ata_noop_dev_select);
43727fbc 6082EXPORT_SYMBOL_GPL(sata_print_link_status);
436d34b3 6083EXPORT_SYMBOL_GPL(atapi_cmd_type);
1da177e4
LT
6084EXPORT_SYMBOL_GPL(ata_tf_to_fis);
6085EXPORT_SYMBOL_GPL(ata_tf_from_fis);
6357357c
TH
6086EXPORT_SYMBOL_GPL(ata_pack_xfermask);
6087EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
6088EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
6089EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
6090EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
6091EXPORT_SYMBOL_GPL(ata_mode_string);
6092EXPORT_SYMBOL_GPL(ata_id_xfermask);
1da177e4 6093EXPORT_SYMBOL_GPL(ata_port_start);
04351821 6094EXPORT_SYMBOL_GPL(ata_do_set_mode);
31cc23b3 6095EXPORT_SYMBOL_GPL(ata_std_qc_defer);
e46834cd 6096EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
358f9a77 6097EXPORT_SYMBOL_GPL(ata_noop_irq_clear);
1da177e4 6098EXPORT_SYMBOL_GPL(ata_port_probe);
10305f0f 6099EXPORT_SYMBOL_GPL(ata_dev_disable);
3c567b7d 6100EXPORT_SYMBOL_GPL(sata_set_spd);
936fd732
TH
6101EXPORT_SYMBOL_GPL(sata_link_debounce);
6102EXPORT_SYMBOL_GPL(sata_link_resume);
cc0680a5 6103EXPORT_SYMBOL_GPL(sata_link_hardreset);
2e9edbf8
JG
6104EXPORT_SYMBOL_GPL(ata_dev_classify);
6105EXPORT_SYMBOL_GPL(ata_dev_pair);
1da177e4 6106EXPORT_SYMBOL_GPL(ata_port_disable);
67846b30 6107EXPORT_SYMBOL_GPL(ata_ratelimit);
c22daff4 6108EXPORT_SYMBOL_GPL(ata_wait_register);
1da177e4
LT
6109EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
6110EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
1da177e4 6111EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
83c47bcb 6112EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
a6e6ce8e 6113EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
34bf2170
TH
6114EXPORT_SYMBOL_GPL(sata_scr_valid);
6115EXPORT_SYMBOL_GPL(sata_scr_read);
6116EXPORT_SYMBOL_GPL(sata_scr_write);
6117EXPORT_SYMBOL_GPL(sata_scr_write_flush);
936fd732
TH
6118EXPORT_SYMBOL_GPL(ata_link_online);
6119EXPORT_SYMBOL_GPL(ata_link_offline);
6ffa01d8 6120#ifdef CONFIG_PM
cca3974e
JG
6121EXPORT_SYMBOL_GPL(ata_host_suspend);
6122EXPORT_SYMBOL_GPL(ata_host_resume);
6ffa01d8 6123#endif /* CONFIG_PM */
6a62a04d
TH
6124EXPORT_SYMBOL_GPL(ata_id_string);
6125EXPORT_SYMBOL_GPL(ata_id_c_string);
1da177e4
LT
6126EXPORT_SYMBOL_GPL(ata_scsi_simulate);
6127
1bc4ccff 6128EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
6357357c 6129EXPORT_SYMBOL_GPL(ata_timing_find_mode);
452503f9
AC
6130EXPORT_SYMBOL_GPL(ata_timing_compute);
6131EXPORT_SYMBOL_GPL(ata_timing_merge);
a0f79b92 6132EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
452503f9 6133
1da177e4
LT
6134#ifdef CONFIG_PCI
6135EXPORT_SYMBOL_GPL(pci_test_config_bits);
1da177e4 6136EXPORT_SYMBOL_GPL(ata_pci_remove_one);
6ffa01d8 6137#ifdef CONFIG_PM
500530f6
TH
6138EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6139EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
9b847548
JA
6140EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6141EXPORT_SYMBOL_GPL(ata_pci_device_resume);
6ffa01d8 6142#endif /* CONFIG_PM */
1da177e4 6143#endif /* CONFIG_PCI */
9b847548 6144
31f88384 6145EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch);
3af9a77a
TH
6146EXPORT_SYMBOL_GPL(sata_pmp_std_prereset);
6147EXPORT_SYMBOL_GPL(sata_pmp_std_hardreset);
6148EXPORT_SYMBOL_GPL(sata_pmp_std_postreset);
a1efdaba 6149EXPORT_SYMBOL_GPL(sata_pmp_error_handler);
3af9a77a 6150
b64bbc39
TH
6151EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
6152EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
6153EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
cbcdd875
TH
6154EXPORT_SYMBOL_GPL(ata_port_desc);
6155#ifdef CONFIG_PCI
6156EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
6157#endif /* CONFIG_PCI */
7b70fc03 6158EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
dbd82616 6159EXPORT_SYMBOL_GPL(ata_link_abort);
7b70fc03 6160EXPORT_SYMBOL_GPL(ata_port_abort);
e3180499 6161EXPORT_SYMBOL_GPL(ata_port_freeze);
7d77b247 6162EXPORT_SYMBOL_GPL(sata_async_notification);
e3180499
TH
6163EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
6164EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
ece1d636
TH
6165EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
6166EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
022bdb07 6167EXPORT_SYMBOL_GPL(ata_do_eh);
a1efdaba 6168EXPORT_SYMBOL_GPL(ata_std_error_handler);
be0d18df
AC
6169
6170EXPORT_SYMBOL_GPL(ata_cable_40wire);
6171EXPORT_SYMBOL_GPL(ata_cable_80wire);
6172EXPORT_SYMBOL_GPL(ata_cable_unknown);
c88f90c3 6173EXPORT_SYMBOL_GPL(ata_cable_ignore);
be0d18df 6174EXPORT_SYMBOL_GPL(ata_cable_sata);