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