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