Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * libata-acpi.c
0004  * Provides ACPI support for PATA/SATA.
0005  *
0006  * Copyright (C) 2006 Intel Corp.
0007  * Copyright (C) 2006 Randy Dunlap
0008  */
0009 
0010 #include <linux/module.h>
0011 #include <linux/ata.h>
0012 #include <linux/delay.h>
0013 #include <linux/device.h>
0014 #include <linux/errno.h>
0015 #include <linux/kernel.h>
0016 #include <linux/acpi.h>
0017 #include <linux/libata.h>
0018 #include <linux/pci.h>
0019 #include <linux/slab.h>
0020 #include <linux/pm_runtime.h>
0021 #include <scsi/scsi_device.h>
0022 #include "libata.h"
0023 
0024 unsigned int ata_acpi_gtf_filter = ATA_ACPI_FILTER_DEFAULT;
0025 module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644);
0026 MODULE_PARM_DESC(acpi_gtf_filter, "filter mask for ACPI _GTF commands, set to filter out (0x1=set xfermode, 0x2=lock/freeze lock, 0x4=DIPM, 0x8=FPDMA non-zero offset, 0x10=FPDMA DMA Setup FIS auto-activate)");
0027 
0028 #define NO_PORT_MULT        0xffff
0029 #define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
0030 
0031 #define REGS_PER_GTF        7
0032 struct ata_acpi_gtf {
0033     u8  tf[REGS_PER_GTF];   /* regs. 0x1f1 - 0x1f7 */
0034 } __packed;
0035 
0036 static void ata_acpi_clear_gtf(struct ata_device *dev)
0037 {
0038     kfree(dev->gtf_cache);
0039     dev->gtf_cache = NULL;
0040 }
0041 
0042 struct ata_acpi_hotplug_context {
0043     struct acpi_hotplug_context hp;
0044     union {
0045         struct ata_port *ap;
0046         struct ata_device *dev;
0047     } data;
0048 };
0049 
0050 #define ata_hotplug_data(context) (container_of((context), struct ata_acpi_hotplug_context, hp)->data)
0051 
0052 /**
0053  * ata_dev_acpi_handle - provide the acpi_handle for an ata_device
0054  * @dev: the acpi_handle returned will correspond to this device
0055  *
0056  * Returns the acpi_handle for the ACPI namespace object corresponding to
0057  * the ata_device passed into the function, or NULL if no such object exists
0058  * or ACPI is disabled for this device due to consecutive errors.
0059  */
0060 acpi_handle ata_dev_acpi_handle(struct ata_device *dev)
0061 {
0062     return dev->flags & ATA_DFLAG_ACPI_DISABLED ?
0063             NULL : ACPI_HANDLE(&dev->tdev);
0064 }
0065 
0066 /* @ap and @dev are the same as ata_acpi_handle_hotplug() */
0067 static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
0068 {
0069     if (dev)
0070         dev->flags |= ATA_DFLAG_DETACH;
0071     else {
0072         struct ata_link *tlink;
0073         struct ata_device *tdev;
0074 
0075         ata_for_each_link(tlink, ap, EDGE)
0076             ata_for_each_dev(tdev, tlink, ALL)
0077                 tdev->flags |= ATA_DFLAG_DETACH;
0078     }
0079 
0080     ata_port_schedule_eh(ap);
0081 }
0082 
0083 /**
0084  * ata_acpi_handle_hotplug - ACPI event handler backend
0085  * @ap: ATA port ACPI event occurred
0086  * @dev: ATA device ACPI event occurred (can be NULL)
0087  * @event: ACPI event which occurred
0088  *
0089  * All ACPI bay / device realted events end up in this function.  If
0090  * the event is port-wide @dev is NULL.  If the event is specific to a
0091  * device, @dev points to it.
0092  *
0093  * Hotplug (as opposed to unplug) notification is always handled as
0094  * port-wide while unplug only kills the target device on device-wide
0095  * event.
0096  *
0097  * LOCKING:
0098  * ACPI notify handler context.  May sleep.
0099  */
0100 static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
0101                     u32 event)
0102 {
0103     struct ata_eh_info *ehi = &ap->link.eh_info;
0104     int wait = 0;
0105     unsigned long flags;
0106 
0107     spin_lock_irqsave(ap->lock, flags);
0108     /*
0109      * When dock driver calls into the routine, it will always use
0110      * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
0111      * ACPI_NOTIFY_EJECT_REQUEST for remove
0112      */
0113     switch (event) {
0114     case ACPI_NOTIFY_BUS_CHECK:
0115     case ACPI_NOTIFY_DEVICE_CHECK:
0116         ata_ehi_push_desc(ehi, "ACPI event");
0117 
0118         ata_ehi_hotplugged(ehi);
0119         ata_port_freeze(ap);
0120         break;
0121     case ACPI_NOTIFY_EJECT_REQUEST:
0122         ata_ehi_push_desc(ehi, "ACPI event");
0123 
0124         ata_acpi_detach_device(ap, dev);
0125         wait = 1;
0126         break;
0127     }
0128 
0129     spin_unlock_irqrestore(ap->lock, flags);
0130 
0131     if (wait)
0132         ata_port_wait_eh(ap);
0133 }
0134 
0135 static int ata_acpi_dev_notify_dock(struct acpi_device *adev, u32 event)
0136 {
0137     struct ata_device *dev = ata_hotplug_data(adev->hp).dev;
0138     ata_acpi_handle_hotplug(dev->link->ap, dev, event);
0139     return 0;
0140 }
0141 
0142 static int ata_acpi_ap_notify_dock(struct acpi_device *adev, u32 event)
0143 {
0144     ata_acpi_handle_hotplug(ata_hotplug_data(adev->hp).ap, NULL, event);
0145     return 0;
0146 }
0147 
0148 static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
0149     u32 event)
0150 {
0151     struct kobject *kobj = NULL;
0152     char event_string[20];
0153     char *envp[] = { event_string, NULL };
0154 
0155     if (dev) {
0156         if (dev->sdev)
0157             kobj = &dev->sdev->sdev_gendev.kobj;
0158     } else
0159         kobj = &ap->dev->kobj;
0160 
0161     if (kobj) {
0162         snprintf(event_string, 20, "BAY_EVENT=%d", event);
0163         kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
0164     }
0165 }
0166 
0167 static void ata_acpi_ap_uevent(struct acpi_device *adev, u32 event)
0168 {
0169     ata_acpi_uevent(ata_hotplug_data(adev->hp).ap, NULL, event);
0170 }
0171 
0172 static void ata_acpi_dev_uevent(struct acpi_device *adev, u32 event)
0173 {
0174     struct ata_device *dev = ata_hotplug_data(adev->hp).dev;
0175     ata_acpi_uevent(dev->link->ap, dev, event);
0176 }
0177 
0178 /* bind acpi handle to pata port */
0179 void ata_acpi_bind_port(struct ata_port *ap)
0180 {
0181     struct acpi_device *host_companion = ACPI_COMPANION(ap->host->dev);
0182     struct acpi_device *adev;
0183     struct ata_acpi_hotplug_context *context;
0184 
0185     if (libata_noacpi || ap->flags & ATA_FLAG_ACPI_SATA || !host_companion)
0186         return;
0187 
0188     acpi_preset_companion(&ap->tdev, host_companion, ap->port_no);
0189 
0190     if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
0191         ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
0192 
0193     adev = ACPI_COMPANION(&ap->tdev);
0194     if (!adev || adev->hp)
0195         return;
0196 
0197     context = kzalloc(sizeof(*context), GFP_KERNEL);
0198     if (!context)
0199         return;
0200 
0201     context->data.ap = ap;
0202     acpi_initialize_hp_context(adev, &context->hp, ata_acpi_ap_notify_dock,
0203                    ata_acpi_ap_uevent);
0204 }
0205 
0206 void ata_acpi_bind_dev(struct ata_device *dev)
0207 {
0208     struct ata_port *ap = dev->link->ap;
0209     struct acpi_device *port_companion = ACPI_COMPANION(&ap->tdev);
0210     struct acpi_device *host_companion = ACPI_COMPANION(ap->host->dev);
0211     struct acpi_device *parent, *adev;
0212     struct ata_acpi_hotplug_context *context;
0213     u64 adr;
0214 
0215     /*
0216      * For both sata/pata devices, host companion device is required.
0217      * For pata device, port companion device is also required.
0218      */
0219     if (libata_noacpi || !host_companion ||
0220             (!(ap->flags & ATA_FLAG_ACPI_SATA) && !port_companion))
0221         return;
0222 
0223     if (ap->flags & ATA_FLAG_ACPI_SATA) {
0224         if (!sata_pmp_attached(ap))
0225             adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
0226         else
0227             adr = SATA_ADR(ap->port_no, dev->link->pmp);
0228         parent = host_companion;
0229     } else {
0230         adr = dev->devno;
0231         parent = port_companion;
0232     }
0233 
0234     acpi_preset_companion(&dev->tdev, parent, adr);
0235     adev = ACPI_COMPANION(&dev->tdev);
0236     if (!adev || adev->hp)
0237         return;
0238 
0239     context = kzalloc(sizeof(*context), GFP_KERNEL);
0240     if (!context)
0241         return;
0242 
0243     context->data.dev = dev;
0244     acpi_initialize_hp_context(adev, &context->hp, ata_acpi_dev_notify_dock,
0245                    ata_acpi_dev_uevent);
0246 }
0247 
0248 /**
0249  * ata_acpi_dissociate - dissociate ATA host from ACPI objects
0250  * @host: target ATA host
0251  *
0252  * This function is called during driver detach after the whole host
0253  * is shut down.
0254  *
0255  * LOCKING:
0256  * EH context.
0257  */
0258 void ata_acpi_dissociate(struct ata_host *host)
0259 {
0260     int i;
0261 
0262     /* Restore initial _GTM values so that driver which attaches
0263      * afterward can use them too.
0264      */
0265     for (i = 0; i < host->n_ports; i++) {
0266         struct ata_port *ap = host->ports[i];
0267         const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
0268 
0269         if (ACPI_HANDLE(&ap->tdev) && gtm)
0270             ata_acpi_stm(ap, gtm);
0271     }
0272 }
0273 
0274 /**
0275  * ata_acpi_gtm - execute _GTM
0276  * @ap: target ATA port
0277  * @gtm: out parameter for _GTM result
0278  *
0279  * Evaluate _GTM and store the result in @gtm.
0280  *
0281  * LOCKING:
0282  * EH context.
0283  *
0284  * RETURNS:
0285  * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
0286  */
0287 int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
0288 {
0289     struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
0290     union acpi_object *out_obj;
0291     acpi_status status;
0292     int rc = 0;
0293     acpi_handle handle = ACPI_HANDLE(&ap->tdev);
0294 
0295     if (!handle)
0296         return -EINVAL;
0297 
0298     status = acpi_evaluate_object(handle, "_GTM", NULL, &output);
0299 
0300     rc = -ENOENT;
0301     if (status == AE_NOT_FOUND)
0302         goto out_free;
0303 
0304     rc = -EINVAL;
0305     if (ACPI_FAILURE(status)) {
0306         ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
0307                  status);
0308         goto out_free;
0309     }
0310 
0311     out_obj = output.pointer;
0312     if (out_obj->type != ACPI_TYPE_BUFFER) {
0313         ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
0314                   out_obj->type);
0315 
0316         goto out_free;
0317     }
0318 
0319     if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
0320         ata_port_err(ap, "_GTM returned invalid length %d\n",
0321                  out_obj->buffer.length);
0322         goto out_free;
0323     }
0324 
0325     memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
0326     rc = 0;
0327  out_free:
0328     kfree(output.pointer);
0329     return rc;
0330 }
0331 
0332 EXPORT_SYMBOL_GPL(ata_acpi_gtm);
0333 
0334 /**
0335  * ata_acpi_stm - execute _STM
0336  * @ap: target ATA port
0337  * @stm: timing parameter to _STM
0338  *
0339  * Evaluate _STM with timing parameter @stm.
0340  *
0341  * LOCKING:
0342  * EH context.
0343  *
0344  * RETURNS:
0345  * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
0346  */
0347 int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
0348 {
0349     acpi_status status;
0350     struct ata_acpi_gtm     stm_buf = *stm;
0351     struct acpi_object_list         input;
0352     union acpi_object               in_params[3];
0353 
0354     in_params[0].type = ACPI_TYPE_BUFFER;
0355     in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
0356     in_params[0].buffer.pointer = (u8 *)&stm_buf;
0357     /* Buffers for id may need byteswapping ? */
0358     in_params[1].type = ACPI_TYPE_BUFFER;
0359     in_params[1].buffer.length = 512;
0360     in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
0361     in_params[2].type = ACPI_TYPE_BUFFER;
0362     in_params[2].buffer.length = 512;
0363     in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
0364 
0365     input.count = 3;
0366     input.pointer = in_params;
0367 
0368     status = acpi_evaluate_object(ACPI_HANDLE(&ap->tdev), "_STM",
0369                       &input, NULL);
0370 
0371     if (status == AE_NOT_FOUND)
0372         return -ENOENT;
0373     if (ACPI_FAILURE(status)) {
0374         ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
0375                  status);
0376         return -EINVAL;
0377     }
0378     return 0;
0379 }
0380 
0381 EXPORT_SYMBOL_GPL(ata_acpi_stm);
0382 
0383 /**
0384  * ata_dev_get_GTF - get the drive bootup default taskfile settings
0385  * @dev: target ATA device
0386  * @gtf: output parameter for buffer containing _GTF taskfile arrays
0387  *
0388  * This applies to both PATA and SATA drives.
0389  *
0390  * The _GTF method has no input parameters.
0391  * It returns a variable number of register set values (registers
0392  * hex 1F1..1F7, taskfiles).
0393  * The <variable number> is not known in advance, so have ACPI-CA
0394  * allocate the buffer as needed and return it, then free it later.
0395  *
0396  * LOCKING:
0397  * EH context.
0398  *
0399  * RETURNS:
0400  * Number of taskfiles on success, 0 if _GTF doesn't exist.  -EINVAL
0401  * if _GTF is invalid.
0402  */
0403 static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
0404 {
0405     acpi_status status;
0406     struct acpi_buffer output;
0407     union acpi_object *out_obj;
0408     int rc = 0;
0409 
0410     /* if _GTF is cached, use the cached value */
0411     if (dev->gtf_cache) {
0412         out_obj = dev->gtf_cache;
0413         goto done;
0414     }
0415 
0416     /* set up output buffer */
0417     output.length = ACPI_ALLOCATE_BUFFER;
0418     output.pointer = NULL;  /* ACPI-CA sets this; save/free it later */
0419 
0420     /* _GTF has no input parameters */
0421     status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
0422                       &output);
0423     out_obj = dev->gtf_cache = output.pointer;
0424 
0425     if (ACPI_FAILURE(status)) {
0426         if (status != AE_NOT_FOUND) {
0427             ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
0428                      status);
0429             rc = -EINVAL;
0430         }
0431         goto out_free;
0432     }
0433 
0434     if (!output.length || !output.pointer) {
0435         ata_dev_dbg(dev, "Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
0436                 (unsigned long long)output.length,
0437                 output.pointer);
0438         rc = -EINVAL;
0439         goto out_free;
0440     }
0441 
0442     if (out_obj->type != ACPI_TYPE_BUFFER) {
0443         ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
0444                  out_obj->type);
0445         rc = -EINVAL;
0446         goto out_free;
0447     }
0448 
0449     if (out_obj->buffer.length % REGS_PER_GTF) {
0450         ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
0451                  out_obj->buffer.length);
0452         rc = -EINVAL;
0453         goto out_free;
0454     }
0455 
0456  done:
0457     rc = out_obj->buffer.length / REGS_PER_GTF;
0458     if (gtf) {
0459         *gtf = (void *)out_obj->buffer.pointer;
0460         ata_dev_dbg(dev, "returning gtf=%p, gtf_count=%d\n",
0461                 *gtf, rc);
0462     }
0463     return rc;
0464 
0465  out_free:
0466     ata_acpi_clear_gtf(dev);
0467     return rc;
0468 }
0469 
0470 /**
0471  * ata_acpi_gtm_xfermask - determine xfermode from GTM parameter
0472  * @dev: target device
0473  * @gtm: GTM parameter to use
0474  *
0475  * Determine xfermask for @dev from @gtm.
0476  *
0477  * LOCKING:
0478  * None.
0479  *
0480  * RETURNS:
0481  * Determined xfermask.
0482  */
0483 unsigned int ata_acpi_gtm_xfermask(struct ata_device *dev,
0484                    const struct ata_acpi_gtm *gtm)
0485 {
0486     unsigned int xfer_mask = 0;
0487     unsigned int type;
0488     int unit;
0489     u8 mode;
0490 
0491     /* we always use the 0 slot for crap hardware */
0492     unit = dev->devno;
0493     if (!(gtm->flags & 0x10))
0494         unit = 0;
0495 
0496     /* PIO */
0497     mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
0498     xfer_mask |= ata_xfer_mode2mask(mode);
0499 
0500     /* See if we have MWDMA or UDMA data. We don't bother with
0501      * MWDMA if UDMA is available as this means the BIOS set UDMA
0502      * and our error changedown if it works is UDMA to PIO anyway.
0503      */
0504     if (!(gtm->flags & (1 << (2 * unit))))
0505         type = ATA_SHIFT_MWDMA;
0506     else
0507         type = ATA_SHIFT_UDMA;
0508 
0509     mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
0510     xfer_mask |= ata_xfer_mode2mask(mode);
0511 
0512     return xfer_mask;
0513 }
0514 EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
0515 
0516 /**
0517  * ata_acpi_cbl_80wire      -   Check for 80 wire cable
0518  * @ap: Port to check
0519  * @gtm: GTM data to use
0520  *
0521  * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
0522  */
0523 int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
0524 {
0525     struct ata_device *dev;
0526 
0527     ata_for_each_dev(dev, &ap->link, ENABLED) {
0528         unsigned int xfer_mask, udma_mask;
0529 
0530         xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
0531         ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
0532 
0533         if (udma_mask & ~ATA_UDMA_MASK_40C)
0534             return 1;
0535     }
0536 
0537     return 0;
0538 }
0539 EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
0540 
0541 static void ata_acpi_gtf_to_tf(struct ata_device *dev,
0542                    const struct ata_acpi_gtf *gtf,
0543                    struct ata_taskfile *tf)
0544 {
0545     ata_tf_init(dev, tf);
0546 
0547     tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
0548     tf->protocol = ATA_PROT_NODATA;
0549     tf->error   = gtf->tf[0];   /* 0x1f1 */
0550     tf->nsect   = gtf->tf[1];   /* 0x1f2 */
0551     tf->lbal    = gtf->tf[2];   /* 0x1f3 */
0552     tf->lbam    = gtf->tf[3];   /* 0x1f4 */
0553     tf->lbah    = gtf->tf[4];   /* 0x1f5 */
0554     tf->device  = gtf->tf[5];   /* 0x1f6 */
0555     tf->status  = gtf->tf[6];   /* 0x1f7 */
0556 }
0557 
0558 static int ata_acpi_filter_tf(struct ata_device *dev,
0559                   const struct ata_taskfile *tf,
0560                   const struct ata_taskfile *ptf)
0561 {
0562     if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
0563         /* libata doesn't use ACPI to configure transfer mode.
0564          * It will only confuse device configuration.  Skip.
0565          */
0566         if (tf->command == ATA_CMD_SET_FEATURES &&
0567             tf->feature == SETFEATURES_XFER)
0568             return 1;
0569     }
0570 
0571     if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
0572         /* BIOS writers, sorry but we don't wanna lock
0573          * features unless the user explicitly said so.
0574          */
0575 
0576         /* DEVICE CONFIGURATION FREEZE LOCK */
0577         if (tf->command == ATA_CMD_CONF_OVERLAY &&
0578             tf->feature == ATA_DCO_FREEZE_LOCK)
0579             return 1;
0580 
0581         /* SECURITY FREEZE LOCK */
0582         if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
0583             return 1;
0584 
0585         /* SET MAX LOCK and SET MAX FREEZE LOCK */
0586         if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
0587             tf->command == ATA_CMD_SET_MAX &&
0588             (tf->feature == ATA_SET_MAX_LOCK ||
0589              tf->feature == ATA_SET_MAX_FREEZE_LOCK))
0590             return 1;
0591     }
0592 
0593     if (tf->command == ATA_CMD_SET_FEATURES &&
0594         tf->feature == SETFEATURES_SATA_ENABLE) {
0595         /* inhibit enabling DIPM */
0596         if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
0597             tf->nsect == SATA_DIPM)
0598             return 1;
0599 
0600         /* inhibit FPDMA non-zero offset */
0601         if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
0602             (tf->nsect == SATA_FPDMA_OFFSET ||
0603              tf->nsect == SATA_FPDMA_IN_ORDER))
0604             return 1;
0605 
0606         /* inhibit FPDMA auto activation */
0607         if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
0608             tf->nsect == SATA_FPDMA_AA)
0609             return 1;
0610     }
0611 
0612     return 0;
0613 }
0614 
0615 /**
0616  * ata_acpi_run_tf - send taskfile registers to host controller
0617  * @dev: target ATA device
0618  * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
0619  * @prev_gtf: previous command
0620  *
0621  * Outputs ATA taskfile to standard ATA host controller.
0622  * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
0623  * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
0624  * hob_lbal, hob_lbam, and hob_lbah.
0625  *
0626  * This function waits for idle (!BUSY and !DRQ) after writing
0627  * registers.  If the control register has a new value, this
0628  * function also waits for idle after writing control and before
0629  * writing the remaining registers.
0630  *
0631  * LOCKING:
0632  * EH context.
0633  *
0634  * RETURNS:
0635  * 1 if command is executed successfully.  0 if ignored, rejected or
0636  * filtered out, -errno on other errors.
0637  */
0638 static int ata_acpi_run_tf(struct ata_device *dev,
0639                const struct ata_acpi_gtf *gtf,
0640                const struct ata_acpi_gtf *prev_gtf)
0641 {
0642     struct ata_taskfile *pptf = NULL;
0643     struct ata_taskfile tf, ptf, rtf;
0644     unsigned int err_mask;
0645     const char *descr;
0646     int rc;
0647 
0648     if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
0649         && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
0650         && (gtf->tf[6] == 0))
0651         return 0;
0652 
0653     ata_acpi_gtf_to_tf(dev, gtf, &tf);
0654     if (prev_gtf) {
0655         ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
0656         pptf = &ptf;
0657     }
0658 
0659     descr = ata_get_cmd_name(tf.command);
0660 
0661     if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
0662         rtf = tf;
0663         err_mask = ata_exec_internal(dev, &rtf, NULL,
0664                          DMA_NONE, NULL, 0, 0);
0665 
0666         switch (err_mask) {
0667         case 0:
0668             ata_dev_dbg(dev,
0669                 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x"
0670                 "(%s) succeeded\n",
0671                 tf.command, tf.feature, tf.nsect, tf.lbal,
0672                 tf.lbam, tf.lbah, tf.device, descr);
0673             rc = 1;
0674             break;
0675 
0676         case AC_ERR_DEV:
0677             ata_dev_info(dev,
0678                 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x"
0679                 "(%s) rejected by device (Stat=0x%02x Err=0x%02x)",
0680                 tf.command, tf.feature, tf.nsect, tf.lbal,
0681                 tf.lbam, tf.lbah, tf.device, descr,
0682                 rtf.status, rtf.error);
0683             rc = 0;
0684             break;
0685 
0686         default:
0687             ata_dev_err(dev,
0688                 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x"
0689                 "(%s) failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
0690                 tf.command, tf.feature, tf.nsect, tf.lbal,
0691                 tf.lbam, tf.lbah, tf.device, descr,
0692                 err_mask, rtf.status, rtf.error);
0693             rc = -EIO;
0694             break;
0695         }
0696     } else {
0697         ata_dev_info(dev,
0698             "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x"
0699             "(%s) filtered out\n",
0700             tf.command, tf.feature, tf.nsect, tf.lbal,
0701             tf.lbam, tf.lbah, tf.device, descr);
0702         rc = 0;
0703     }
0704     return rc;
0705 }
0706 
0707 /**
0708  * ata_acpi_exec_tfs - get then write drive taskfile settings
0709  * @dev: target ATA device
0710  * @nr_executed: out parameter for the number of executed commands
0711  *
0712  * Evaluate _GTF and execute returned taskfiles.
0713  *
0714  * LOCKING:
0715  * EH context.
0716  *
0717  * RETURNS:
0718  * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
0719  * -errno on other errors.
0720  */
0721 static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
0722 {
0723     struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
0724     int gtf_count, i, rc;
0725 
0726     /* get taskfiles */
0727     rc = ata_dev_get_GTF(dev, &gtf);
0728     if (rc < 0)
0729         return rc;
0730     gtf_count = rc;
0731 
0732     /* execute them */
0733     for (i = 0; i < gtf_count; i++, gtf++) {
0734         rc = ata_acpi_run_tf(dev, gtf, pgtf);
0735         if (rc < 0)
0736             break;
0737         if (rc) {
0738             (*nr_executed)++;
0739             pgtf = gtf;
0740         }
0741     }
0742 
0743     ata_acpi_clear_gtf(dev);
0744 
0745     if (rc < 0)
0746         return rc;
0747     return 0;
0748 }
0749 
0750 /**
0751  * ata_acpi_push_id - send Identify data to drive
0752  * @dev: target ATA device
0753  *
0754  * _SDD ACPI object: for SATA mode only
0755  * Must be after Identify (Packet) Device -- uses its data
0756  * ATM this function never returns a failure.  It is an optional
0757  * method and if it fails for whatever reason, we should still
0758  * just keep going.
0759  *
0760  * LOCKING:
0761  * EH context.
0762  *
0763  * RETURNS:
0764  * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
0765  */
0766 static int ata_acpi_push_id(struct ata_device *dev)
0767 {
0768     struct ata_port *ap = dev->link->ap;
0769     acpi_status status;
0770     struct acpi_object_list input;
0771     union acpi_object in_params[1];
0772 
0773     ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
0774             __func__, dev->devno, ap->port_no);
0775 
0776     /* Give the drive Identify data to the drive via the _SDD method */
0777     /* _SDD: set up input parameters */
0778     input.count = 1;
0779     input.pointer = in_params;
0780     in_params[0].type = ACPI_TYPE_BUFFER;
0781     in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
0782     in_params[0].buffer.pointer = (u8 *)dev->id;
0783     /* Output buffer: _SDD has no output */
0784 
0785     /* It's OK for _SDD to be missing too. */
0786     swap_buf_le16(dev->id, ATA_ID_WORDS);
0787     status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
0788                       NULL);
0789     swap_buf_le16(dev->id, ATA_ID_WORDS);
0790 
0791     if (status == AE_NOT_FOUND)
0792         return -ENOENT;
0793 
0794     if (ACPI_FAILURE(status)) {
0795         ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
0796         return -EIO;
0797     }
0798 
0799     return 0;
0800 }
0801 
0802 /**
0803  * ata_acpi_on_resume - ATA ACPI hook called on resume
0804  * @ap: target ATA port
0805  *
0806  * This function is called when @ap is resumed - right after port
0807  * itself is resumed but before any EH action is taken.
0808  *
0809  * LOCKING:
0810  * EH context.
0811  */
0812 void ata_acpi_on_resume(struct ata_port *ap)
0813 {
0814     const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
0815     struct ata_device *dev;
0816 
0817     if (ACPI_HANDLE(&ap->tdev) && gtm) {
0818         /* _GTM valid */
0819 
0820         /* restore timing parameters */
0821         ata_acpi_stm(ap, gtm);
0822 
0823         /* _GTF should immediately follow _STM so that it can
0824          * use values set by _STM.  Cache _GTF result and
0825          * schedule _GTF.
0826          */
0827         ata_for_each_dev(dev, &ap->link, ALL) {
0828             ata_acpi_clear_gtf(dev);
0829             if (ata_dev_enabled(dev) &&
0830                 ata_dev_acpi_handle(dev) &&
0831                 ata_dev_get_GTF(dev, NULL) >= 0)
0832                 dev->flags |= ATA_DFLAG_ACPI_PENDING;
0833         }
0834     } else {
0835         /* SATA _GTF needs to be evaulated after _SDD and
0836          * there's no reason to evaluate IDE _GTF early
0837          * without _STM.  Clear cache and schedule _GTF.
0838          */
0839         ata_for_each_dev(dev, &ap->link, ALL) {
0840             ata_acpi_clear_gtf(dev);
0841             if (ata_dev_enabled(dev))
0842                 dev->flags |= ATA_DFLAG_ACPI_PENDING;
0843         }
0844     }
0845 }
0846 
0847 static int ata_acpi_choose_suspend_state(struct ata_device *dev, bool runtime)
0848 {
0849     int d_max_in = ACPI_STATE_D3_COLD;
0850     if (!runtime)
0851         goto out;
0852 
0853     /*
0854      * For ATAPI, runtime D3 cold is only allowed
0855      * for ZPODD in zero power ready state
0856      */
0857     if (dev->class == ATA_DEV_ATAPI &&
0858         !(zpodd_dev_enabled(dev) && zpodd_zpready(dev)))
0859         d_max_in = ACPI_STATE_D3_HOT;
0860 
0861 out:
0862     return acpi_pm_device_sleep_state(&dev->tdev, NULL, d_max_in);
0863 }
0864 
0865 static void sata_acpi_set_state(struct ata_port *ap, pm_message_t state)
0866 {
0867     bool runtime = PMSG_IS_AUTO(state);
0868     struct ata_device *dev;
0869     acpi_handle handle;
0870     int acpi_state;
0871 
0872     ata_for_each_dev(dev, &ap->link, ENABLED) {
0873         handle = ata_dev_acpi_handle(dev);
0874         if (!handle)
0875             continue;
0876 
0877         if (!(state.event & PM_EVENT_RESUME)) {
0878             acpi_state = ata_acpi_choose_suspend_state(dev, runtime);
0879             if (acpi_state == ACPI_STATE_D0)
0880                 continue;
0881             if (runtime && zpodd_dev_enabled(dev) &&
0882                 acpi_state == ACPI_STATE_D3_COLD)
0883                 zpodd_enable_run_wake(dev);
0884             acpi_bus_set_power(handle, acpi_state);
0885         } else {
0886             if (runtime && zpodd_dev_enabled(dev))
0887                 zpodd_disable_run_wake(dev);
0888             acpi_bus_set_power(handle, ACPI_STATE_D0);
0889         }
0890     }
0891 }
0892 
0893 /* ACPI spec requires _PS0 when IDE power on and _PS3 when power off */
0894 static void pata_acpi_set_state(struct ata_port *ap, pm_message_t state)
0895 {
0896     struct ata_device *dev;
0897     acpi_handle port_handle;
0898 
0899     port_handle = ACPI_HANDLE(&ap->tdev);
0900     if (!port_handle)
0901         return;
0902 
0903     /* channel first and then drives for power on and vica versa
0904        for power off */
0905     if (state.event & PM_EVENT_RESUME)
0906         acpi_bus_set_power(port_handle, ACPI_STATE_D0);
0907 
0908     ata_for_each_dev(dev, &ap->link, ENABLED) {
0909         acpi_handle dev_handle = ata_dev_acpi_handle(dev);
0910         if (!dev_handle)
0911             continue;
0912 
0913         acpi_bus_set_power(dev_handle, state.event & PM_EVENT_RESUME ?
0914                     ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
0915     }
0916 
0917     if (!(state.event & PM_EVENT_RESUME))
0918         acpi_bus_set_power(port_handle, ACPI_STATE_D3_COLD);
0919 }
0920 
0921 /**
0922  * ata_acpi_set_state - set the port power state
0923  * @ap: target ATA port
0924  * @state: state, on/off
0925  *
0926  * This function sets a proper ACPI D state for the device on
0927  * system and runtime PM operations.
0928  */
0929 void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
0930 {
0931     if (ap->flags & ATA_FLAG_ACPI_SATA)
0932         sata_acpi_set_state(ap, state);
0933     else
0934         pata_acpi_set_state(ap, state);
0935 }
0936 
0937 /**
0938  * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
0939  * @dev: target ATA device
0940  *
0941  * This function is called when @dev is about to be configured.
0942  * IDENTIFY data might have been modified after this hook is run.
0943  *
0944  * LOCKING:
0945  * EH context.
0946  *
0947  * RETURNS:
0948  * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
0949  * -errno on failure.
0950  */
0951 int ata_acpi_on_devcfg(struct ata_device *dev)
0952 {
0953     struct ata_port *ap = dev->link->ap;
0954     struct ata_eh_context *ehc = &ap->link.eh_context;
0955     int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
0956     int nr_executed = 0;
0957     int rc;
0958 
0959     if (!ata_dev_acpi_handle(dev))
0960         return 0;
0961 
0962     /* do we need to do _GTF? */
0963     if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
0964         !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
0965         return 0;
0966 
0967     /* do _SDD if SATA */
0968     if (acpi_sata) {
0969         rc = ata_acpi_push_id(dev);
0970         if (rc && rc != -ENOENT)
0971             goto acpi_err;
0972     }
0973 
0974     /* do _GTF */
0975     rc = ata_acpi_exec_tfs(dev, &nr_executed);
0976     if (rc)
0977         goto acpi_err;
0978 
0979     dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
0980 
0981     /* refresh IDENTIFY page if any _GTF command has been executed */
0982     if (nr_executed) {
0983         rc = ata_dev_reread_id(dev, 0);
0984         if (rc < 0) {
0985             ata_dev_err(dev,
0986                     "failed to IDENTIFY after ACPI commands\n");
0987             return rc;
0988         }
0989     }
0990 
0991     return 0;
0992 
0993  acpi_err:
0994     /* ignore evaluation failure if we can continue safely */
0995     if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
0996         return 0;
0997 
0998     /* fail and let EH retry once more for unknown IO errors */
0999     if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
1000         dev->flags |= ATA_DFLAG_ACPI_FAILED;
1001         return rc;
1002     }
1003 
1004     dev->flags |= ATA_DFLAG_ACPI_DISABLED;
1005     ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
1006 
1007     /* We can safely continue if no _GTF command has been executed
1008      * and port is not frozen.
1009      */
1010     if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
1011         return 0;
1012 
1013     return rc;
1014 }
1015 
1016 /**
1017  * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
1018  * @dev: target ATA device
1019  *
1020  * This function is called when @dev is about to be disabled.
1021  *
1022  * LOCKING:
1023  * EH context.
1024  */
1025 void ata_acpi_on_disable(struct ata_device *dev)
1026 {
1027     ata_acpi_clear_gtf(dev);
1028 }