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