Linux Kernel  3.7.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
libata-acpi.c
Go to the documentation of this file.
1 /*
2  * libata-acpi.c
3  * Provides ACPI support for PATA/SATA.
4  *
5  * Copyright (C) 2006 Intel Corp.
6  * Copyright (C) 2006 Randy Dunlap
7  */
8 
9 #include <linux/module.h>
10 #include <linux/ata.h>
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/errno.h>
14 #include <linux/kernel.h>
15 #include <linux/acpi.h>
16 #include <linux/libata.h>
17 #include <linux/pci.h>
18 #include <linux/slab.h>
19 #include <linux/pm_runtime.h>
20 #include <scsi/scsi_device.h>
21 #include "libata.h"
22 
23 #include <acpi/acpi_bus.h>
24 
26 module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644);
27 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)");
28 
29 #define NO_PORT_MULT 0xffff
30 #define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
31 
32 #define REGS_PER_GTF 7
33 struct ata_acpi_gtf {
34  u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
35 } __packed;
36 
37 /*
38  * Helper - belongs in the PCI layer somewhere eventually
39  */
40 static int is_pci_dev(struct device *dev)
41 {
42  return (dev->bus == &pci_bus_type);
43 }
44 
45 static void ata_acpi_clear_gtf(struct ata_device *dev)
46 {
47  kfree(dev->gtf_cache);
48  dev->gtf_cache = NULL;
49 }
50 
59 {
60  if (ap->flags & ATA_FLAG_ACPI_SATA)
61  return NULL;
62 
63  return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), ap->port_no);
64 }
66 
75 {
76  acpi_integer adr;
77  struct ata_port *ap = dev->link->ap;
78 
79  if (ap->flags & ATA_FLAG_ACPI_SATA) {
80  if (!sata_pmp_attached(ap))
81  adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
82  else
83  adr = SATA_ADR(ap->port_no, dev->link->pmp);
84  return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), adr);
85  } else
86  return acpi_get_child(ata_ap_acpi_handle(ap), dev->devno);
87 }
89 
90 /* @ap and @dev are the same as ata_acpi_handle_hotplug() */
91 static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
92 {
93  if (dev)
94  dev->flags |= ATA_DFLAG_DETACH;
95  else {
96  struct ata_link *tlink;
97  struct ata_device *tdev;
98 
99  ata_for_each_link(tlink, ap, EDGE)
100  ata_for_each_dev(tdev, tlink, ALL)
101  tdev->flags |= ATA_DFLAG_DETACH;
102  }
103 
105 }
106 
124 static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
125  u32 event)
126 {
127  struct ata_eh_info *ehi = &ap->link.eh_info;
128  int wait = 0;
129  unsigned long flags;
130 
131  spin_lock_irqsave(ap->lock, flags);
132  /*
133  * When dock driver calls into the routine, it will always use
134  * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
135  * ACPI_NOTIFY_EJECT_REQUEST for remove
136  */
137  switch (event) {
140  ata_ehi_push_desc(ehi, "ACPI event");
141 
142  ata_ehi_hotplugged(ehi);
143  ata_port_freeze(ap);
144  break;
146  ata_ehi_push_desc(ehi, "ACPI event");
147 
148  ata_acpi_detach_device(ap, dev);
149  wait = 1;
150  break;
151  }
152 
153  spin_unlock_irqrestore(ap->lock, flags);
154 
155  if (wait)
156  ata_port_wait_eh(ap);
157 }
158 
159 static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data)
160 {
161  struct ata_device *dev = data;
162 
163  ata_acpi_handle_hotplug(dev->link->ap, dev, event);
164 }
165 
166 static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data)
167 {
168  struct ata_port *ap = data;
169 
170  ata_acpi_handle_hotplug(ap, NULL, event);
171 }
172 
173 static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
174  u32 event)
175 {
176  struct kobject *kobj = NULL;
177  char event_string[20];
178  char *envp[] = { event_string, NULL };
179 
180  if (dev) {
181  if (dev->sdev)
182  kobj = &dev->sdev->sdev_gendev.kobj;
183  } else
184  kobj = &ap->dev->kobj;
185 
186  if (kobj) {
187  snprintf(event_string, 20, "BAY_EVENT=%d", event);
188  kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
189  }
190 }
191 
192 static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data)
193 {
194  ata_acpi_uevent(data, NULL, event);
195 }
196 
197 static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data)
198 {
199  struct ata_device *dev = data;
200  ata_acpi_uevent(dev->link->ap, dev, event);
201 }
202 
203 static const struct acpi_dock_ops ata_acpi_dev_dock_ops = {
204  .handler = ata_acpi_dev_notify_dock,
205  .uevent = ata_acpi_dev_uevent,
206 };
207 
208 static const struct acpi_dock_ops ata_acpi_ap_dock_ops = {
209  .handler = ata_acpi_ap_notify_dock,
210  .uevent = ata_acpi_ap_uevent,
211 };
212 
224 {
225  int i;
226 
227  /* Restore initial _GTM values so that driver which attaches
228  * afterward can use them too.
229  */
230  for (i = 0; i < host->n_ports; i++) {
231  struct ata_port *ap = host->ports[i];
232  const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
233 
234  if (ata_ap_acpi_handle(ap) && gtm)
235  ata_acpi_stm(ap, gtm);
236  }
237 }
238 
252 int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
253 {
254  struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
255  union acpi_object *out_obj;
257  int rc = 0;
258 
259  status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_GTM", NULL,
260  &output);
261 
262  rc = -ENOENT;
263  if (status == AE_NOT_FOUND)
264  goto out_free;
265 
266  rc = -EINVAL;
267  if (ACPI_FAILURE(status)) {
268  ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
269  status);
270  goto out_free;
271  }
272 
273  out_obj = output.pointer;
274  if (out_obj->type != ACPI_TYPE_BUFFER) {
275  ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
276  out_obj->type);
277 
278  goto out_free;
279  }
280 
281  if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
282  ata_port_err(ap, "_GTM returned invalid length %d\n",
283  out_obj->buffer.length);
284  goto out_free;
285  }
286 
287  memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
288  rc = 0;
289  out_free:
290  kfree(output.pointer);
291  return rc;
292 }
293 
295 
309 int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
310 {
312  struct ata_acpi_gtm stm_buf = *stm;
313  struct acpi_object_list input;
314  union acpi_object in_params[3];
315 
316  in_params[0].type = ACPI_TYPE_BUFFER;
317  in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
318  in_params[0].buffer.pointer = (u8 *)&stm_buf;
319  /* Buffers for id may need byteswapping ? */
320  in_params[1].type = ACPI_TYPE_BUFFER;
321  in_params[1].buffer.length = 512;
322  in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
323  in_params[2].type = ACPI_TYPE_BUFFER;
324  in_params[2].buffer.length = 512;
325  in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
326 
327  input.count = 3;
328  input.pointer = in_params;
329 
330  status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_STM", &input,
331  NULL);
332 
333  if (status == AE_NOT_FOUND)
334  return -ENOENT;
335  if (ACPI_FAILURE(status)) {
336  ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
337  status);
338  return -EINVAL;
339  }
340  return 0;
341 }
342 
344 
365 static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
366 {
367  struct ata_port *ap = dev->link->ap;
369  struct acpi_buffer output;
370  union acpi_object *out_obj;
371  int rc = 0;
372 
373  /* if _GTF is cached, use the cached value */
374  if (dev->gtf_cache) {
375  out_obj = dev->gtf_cache;
376  goto done;
377  }
378 
379  /* set up output buffer */
380  output.length = ACPI_ALLOCATE_BUFFER;
381  output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
382 
383  if (ata_msg_probe(ap))
384  ata_dev_dbg(dev, "%s: ENTER: port#: %d\n",
385  __func__, ap->port_no);
386 
387  /* _GTF has no input parameters */
388  status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
389  &output);
390  out_obj = dev->gtf_cache = output.pointer;
391 
392  if (ACPI_FAILURE(status)) {
393  if (status != AE_NOT_FOUND) {
394  ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
395  status);
396  rc = -EINVAL;
397  }
398  goto out_free;
399  }
400 
401  if (!output.length || !output.pointer) {
402  if (ata_msg_probe(ap))
403  ata_dev_dbg(dev, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
404  __func__,
405  (unsigned long long)output.length,
406  output.pointer);
407  rc = -EINVAL;
408  goto out_free;
409  }
410 
411  if (out_obj->type != ACPI_TYPE_BUFFER) {
412  ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
413  out_obj->type);
414  rc = -EINVAL;
415  goto out_free;
416  }
417 
418  if (out_obj->buffer.length % REGS_PER_GTF) {
419  ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
420  out_obj->buffer.length);
421  rc = -EINVAL;
422  goto out_free;
423  }
424 
425  done:
426  rc = out_obj->buffer.length / REGS_PER_GTF;
427  if (gtf) {
428  *gtf = (void *)out_obj->buffer.pointer;
429  if (ata_msg_probe(ap))
430  ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n",
431  __func__, *gtf, rc);
432  }
433  return rc;
434 
435  out_free:
436  ata_acpi_clear_gtf(dev);
437  return rc;
438 }
439 
453 unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
454  const struct ata_acpi_gtm *gtm)
455 {
456  unsigned long xfer_mask = 0;
457  unsigned int type;
458  int unit;
459  u8 mode;
460 
461  /* we always use the 0 slot for crap hardware */
462  unit = dev->devno;
463  if (!(gtm->flags & 0x10))
464  unit = 0;
465 
466  /* PIO */
467  mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
468  xfer_mask |= ata_xfer_mode2mask(mode);
469 
470  /* See if we have MWDMA or UDMA data. We don't bother with
471  * MWDMA if UDMA is available as this means the BIOS set UDMA
472  * and our error changedown if it works is UDMA to PIO anyway.
473  */
474  if (!(gtm->flags & (1 << (2 * unit))))
475  type = ATA_SHIFT_MWDMA;
476  else
477  type = ATA_SHIFT_UDMA;
478 
479  mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
480  xfer_mask |= ata_xfer_mode2mask(mode);
481 
482  return xfer_mask;
483 }
485 
493 int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
494 {
495  struct ata_device *dev;
496 
497  ata_for_each_dev(dev, &ap->link, ENABLED) {
498  unsigned long xfer_mask, udma_mask;
499 
500  xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
501  ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
502 
503  if (udma_mask & ~ATA_UDMA_MASK_40C)
504  return 1;
505  }
506 
507  return 0;
508 }
510 
511 static void ata_acpi_gtf_to_tf(struct ata_device *dev,
512  const struct ata_acpi_gtf *gtf,
513  struct ata_taskfile *tf)
514 {
515  ata_tf_init(dev, tf);
516 
519  tf->feature = gtf->tf[0]; /* 0x1f1 */
520  tf->nsect = gtf->tf[1]; /* 0x1f2 */
521  tf->lbal = gtf->tf[2]; /* 0x1f3 */
522  tf->lbam = gtf->tf[3]; /* 0x1f4 */
523  tf->lbah = gtf->tf[4]; /* 0x1f5 */
524  tf->device = gtf->tf[5]; /* 0x1f6 */
525  tf->command = gtf->tf[6]; /* 0x1f7 */
526 }
527 
528 static int ata_acpi_filter_tf(struct ata_device *dev,
529  const struct ata_taskfile *tf,
530  const struct ata_taskfile *ptf)
531 {
532  if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
533  /* libata doesn't use ACPI to configure transfer mode.
534  * It will only confuse device configuration. Skip.
535  */
536  if (tf->command == ATA_CMD_SET_FEATURES &&
537  tf->feature == SETFEATURES_XFER)
538  return 1;
539  }
540 
541  if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
542  /* BIOS writers, sorry but we don't wanna lock
543  * features unless the user explicitly said so.
544  */
545 
546  /* DEVICE CONFIGURATION FREEZE LOCK */
547  if (tf->command == ATA_CMD_CONF_OVERLAY &&
549  return 1;
550 
551  /* SECURITY FREEZE LOCK */
552  if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
553  return 1;
554 
555  /* SET MAX LOCK and SET MAX FREEZE LOCK */
556  if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
557  tf->command == ATA_CMD_SET_MAX &&
558  (tf->feature == ATA_SET_MAX_LOCK ||
560  return 1;
561  }
562 
563  if (tf->command == ATA_CMD_SET_FEATURES &&
565  /* inhibit enabling DIPM */
566  if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
567  tf->nsect == SATA_DIPM)
568  return 1;
569 
570  /* inhibit FPDMA non-zero offset */
571  if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
572  (tf->nsect == SATA_FPDMA_OFFSET ||
573  tf->nsect == SATA_FPDMA_IN_ORDER))
574  return 1;
575 
576  /* inhibit FPDMA auto activation */
577  if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
578  tf->nsect == SATA_FPDMA_AA)
579  return 1;
580  }
581 
582  return 0;
583 }
584 
607 static int ata_acpi_run_tf(struct ata_device *dev,
608  const struct ata_acpi_gtf *gtf,
609  const struct ata_acpi_gtf *prev_gtf)
610 {
611  struct ata_taskfile *pptf = NULL;
612  struct ata_taskfile tf, ptf, rtf;
613  unsigned int err_mask;
614  const char *level;
615  const char *descr;
616  char msg[60];
617  int rc;
618 
619  if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
620  && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
621  && (gtf->tf[6] == 0))
622  return 0;
623 
624  ata_acpi_gtf_to_tf(dev, gtf, &tf);
625  if (prev_gtf) {
626  ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
627  pptf = &ptf;
628  }
629 
630  if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
631  rtf = tf;
632  err_mask = ata_exec_internal(dev, &rtf, NULL,
633  DMA_NONE, NULL, 0, 0);
634 
635  switch (err_mask) {
636  case 0:
637  level = KERN_DEBUG;
638  snprintf(msg, sizeof(msg), "succeeded");
639  rc = 1;
640  break;
641 
642  case AC_ERR_DEV:
643  level = KERN_INFO;
644  snprintf(msg, sizeof(msg),
645  "rejected by device (Stat=0x%02x Err=0x%02x)",
646  rtf.command, rtf.feature);
647  rc = 0;
648  break;
649 
650  default:
651  level = KERN_ERR;
652  snprintf(msg, sizeof(msg),
653  "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
654  err_mask, rtf.command, rtf.feature);
655  rc = -EIO;
656  break;
657  }
658  } else {
659  level = KERN_INFO;
660  snprintf(msg, sizeof(msg), "filtered out");
661  rc = 0;
662  }
663  descr = ata_get_cmd_descript(tf.command);
664 
665  ata_dev_printk(dev, level,
666  "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
667  tf.command, tf.feature, tf.nsect, tf.lbal,
668  tf.lbam, tf.lbah, tf.device,
669  (descr ? descr : "unknown"), msg);
670 
671  return rc;
672 }
673 
688 static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
689 {
690  struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
691  int gtf_count, i, rc;
692 
693  /* get taskfiles */
694  rc = ata_dev_get_GTF(dev, &gtf);
695  if (rc < 0)
696  return rc;
697  gtf_count = rc;
698 
699  /* execute them */
700  for (i = 0; i < gtf_count; i++, gtf++) {
701  rc = ata_acpi_run_tf(dev, gtf, pgtf);
702  if (rc < 0)
703  break;
704  if (rc) {
705  (*nr_executed)++;
706  pgtf = gtf;
707  }
708  }
709 
710  ata_acpi_clear_gtf(dev);
711 
712  if (rc < 0)
713  return rc;
714  return 0;
715 }
716 
733 static int ata_acpi_push_id(struct ata_device *dev)
734 {
735  struct ata_port *ap = dev->link->ap;
737  struct acpi_object_list input;
738  union acpi_object in_params[1];
739 
740  if (ata_msg_probe(ap))
741  ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
742  __func__, dev->devno, ap->port_no);
743 
744  /* Give the drive Identify data to the drive via the _SDD method */
745  /* _SDD: set up input parameters */
746  input.count = 1;
747  input.pointer = in_params;
748  in_params[0].type = ACPI_TYPE_BUFFER;
749  in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
750  in_params[0].buffer.pointer = (u8 *)dev->id;
751  /* Output buffer: _SDD has no output */
752 
753  /* It's OK for _SDD to be missing too. */
755  status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
756  NULL);
758 
759  if (status == AE_NOT_FOUND)
760  return -ENOENT;
761 
762  if (ACPI_FAILURE(status)) {
763  ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
764  return -EIO;
765  }
766 
767  return 0;
768 }
769 
786 {
787  /* nada */
788  return 0;
789 }
790 
801 void ata_acpi_on_resume(struct ata_port *ap)
802 {
803  const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
804  struct ata_device *dev;
805 
806  if (ata_ap_acpi_handle(ap) && gtm) {
807  /* _GTM valid */
808 
809  /* restore timing parameters */
810  ata_acpi_stm(ap, gtm);
811 
812  /* _GTF should immediately follow _STM so that it can
813  * use values set by _STM. Cache _GTF result and
814  * schedule _GTF.
815  */
816  ata_for_each_dev(dev, &ap->link, ALL) {
817  ata_acpi_clear_gtf(dev);
818  if (ata_dev_enabled(dev) &&
819  ata_dev_get_GTF(dev, NULL) >= 0)
821  }
822  } else {
823  /* SATA _GTF needs to be evaulated after _SDD and
824  * there's no reason to evaluate IDE _GTF early
825  * without _STM. Clear cache and schedule _GTF.
826  */
827  ata_for_each_dev(dev, &ap->link, ALL) {
828  ata_acpi_clear_gtf(dev);
829  if (ata_dev_enabled(dev))
831  }
832  }
833 }
834 
844 {
845  struct ata_device *dev;
847  int acpi_state;
848 
849  /* channel first and then drives for power on and vica versa
850  for power off */
851  handle = ata_ap_acpi_handle(ap);
852  if (handle && state.event == PM_EVENT_ON)
854 
855  ata_for_each_dev(dev, &ap->link, ENABLED) {
856  handle = ata_dev_acpi_handle(dev);
857  if (!handle)
858  continue;
859 
860  if (state.event != PM_EVENT_ON) {
861  acpi_state = acpi_pm_device_sleep_state(
862  &dev->sdev->sdev_gendev, NULL, ACPI_STATE_D3);
863  if (acpi_state > 0)
864  acpi_bus_set_power(handle, acpi_state);
865  /* TBD: need to check if it's runtime pm request */
866  acpi_pm_device_run_wake(
867  &dev->sdev->sdev_gendev, true);
868  } else {
869  /* Ditto */
870  acpi_pm_device_run_wake(
871  &dev->sdev->sdev_gendev, false);
873  }
874  }
875 
876  handle = ata_ap_acpi_handle(ap);
877  if (handle && state.event != PM_EVENT_ON)
879 }
880 
896 {
897  struct ata_port *ap = dev->link->ap;
898  struct ata_eh_context *ehc = &ap->link.eh_context;
899  int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
900  int nr_executed = 0;
901  int rc;
902 
903  if (!ata_dev_acpi_handle(dev))
904  return 0;
905 
906  /* do we need to do _GTF? */
907  if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
908  !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
909  return 0;
910 
911  /* do _SDD if SATA */
912  if (acpi_sata) {
913  rc = ata_acpi_push_id(dev);
914  if (rc && rc != -ENOENT)
915  goto acpi_err;
916  }
917 
918  /* do _GTF */
919  rc = ata_acpi_exec_tfs(dev, &nr_executed);
920  if (rc)
921  goto acpi_err;
922 
923  dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
924 
925  /* refresh IDENTIFY page if any _GTF command has been executed */
926  if (nr_executed) {
927  rc = ata_dev_reread_id(dev, 0);
928  if (rc < 0) {
929  ata_dev_err(dev,
930  "failed to IDENTIFY after ACPI commands\n");
931  return rc;
932  }
933  }
934 
935  return 0;
936 
937  acpi_err:
938  /* ignore evaluation failure if we can continue safely */
939  if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
940  return 0;
941 
942  /* fail and let EH retry once more for unknown IO errors */
943  if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
945  return rc;
946  }
947 
948  ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
949 
950  /* We can safely continue if no _GTF command has been executed
951  * and port is not frozen.
952  */
953  if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
954  return 0;
955 
956  return rc;
957 }
958 
969 {
970  ata_acpi_clear_gtf(dev);
971 }
972 
973 static void ata_acpi_wake_dev(acpi_handle handle, u32 event, void *context)
974 {
975  struct ata_device *ata_dev = context;
976 
977  if (event == ACPI_NOTIFY_DEVICE_WAKE && ata_dev &&
978  pm_runtime_suspended(&ata_dev->sdev->sdev_gendev))
979  scsi_autopm_get_device(ata_dev->sdev);
980 }
981 
982 static void ata_acpi_add_pm_notifier(struct ata_device *dev)
983 {
984  struct acpi_device *acpi_dev;
987 
988  handle = ata_dev_acpi_handle(dev);
989  if (!handle)
990  return;
991 
992  status = acpi_bus_get_device(handle, &acpi_dev);
993  if (ACPI_FAILURE(status))
994  return;
995 
996  if (dev->sdev->can_power_off) {
998  ata_acpi_wake_dev, dev);
999  device_set_run_wake(&dev->sdev->sdev_gendev, true);
1000  }
1001 }
1002 
1003 static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
1004 {
1005  struct acpi_device *acpi_dev;
1008 
1009  handle = ata_dev_acpi_handle(dev);
1010  if (!handle)
1011  return;
1012 
1013  status = acpi_bus_get_device(handle, &acpi_dev);
1014  if (ACPI_FAILURE(status))
1015  return;
1016 
1017  if (dev->sdev->can_power_off) {
1018  device_set_run_wake(&dev->sdev->sdev_gendev, false);
1020  ata_acpi_wake_dev);
1021  }
1022 }
1023 
1024 static void ata_acpi_register_power_resource(struct ata_device *dev)
1025 {
1026  struct scsi_device *sdev = dev->sdev;
1028  struct device *device;
1029 
1030  handle = ata_dev_acpi_handle(dev);
1031  if (!handle)
1032  return;
1033 
1034  device = &sdev->sdev_gendev;
1035 
1036  acpi_power_resource_register_device(device, handle);
1037 }
1038 
1039 static void ata_acpi_unregister_power_resource(struct ata_device *dev)
1040 {
1041  struct scsi_device *sdev = dev->sdev;
1043  struct device *device;
1044 
1045  handle = ata_dev_acpi_handle(dev);
1046  if (!handle)
1047  return;
1048 
1049  device = &sdev->sdev_gendev;
1050 
1051  acpi_power_resource_unregister_device(device, handle);
1052 }
1053 
1054 void ata_acpi_bind(struct ata_device *dev)
1055 {
1056  ata_acpi_add_pm_notifier(dev);
1057  ata_acpi_register_power_resource(dev);
1058 }
1059 
1060 void ata_acpi_unbind(struct ata_device *dev)
1061 {
1062  ata_acpi_remove_pm_notifier(dev);
1063  ata_acpi_unregister_power_resource(dev);
1064 }
1065 
1066 static int compat_pci_ata(struct ata_port *ap)
1067 {
1068  struct device *dev = ap->tdev.parent;
1069  struct pci_dev *pdev;
1070 
1071  if (!is_pci_dev(dev))
1072  return 0;
1073 
1074  pdev = to_pci_dev(dev);
1075 
1076  if ((pdev->class >> 8) != PCI_CLASS_STORAGE_SATA &&
1077  (pdev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1078  return 0;
1079 
1080  return 1;
1081 }
1082 
1083 static int ata_acpi_bind_host(struct ata_port *ap, acpi_handle *handle)
1084 {
1085  if (ap->flags & ATA_FLAG_ACPI_SATA)
1086  return -ENODEV;
1087 
1088  *handle = acpi_get_child(DEVICE_ACPI_HANDLE(ap->tdev.parent),
1089  ap->port_no);
1090 
1091  if (!*handle)
1092  return -ENODEV;
1093 
1094  if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
1096 
1097  return 0;
1098 }
1099 
1100 static int ata_acpi_bind_device(struct ata_port *ap, struct scsi_device *sdev,
1101  acpi_handle *handle)
1102 {
1103  struct ata_device *ata_dev;
1105  struct acpi_device *acpi_dev;
1106  struct acpi_device_power_state *states;
1107 
1108  if (ap->flags & ATA_FLAG_ACPI_SATA) {
1109  if (!sata_pmp_attached(ap))
1110  ata_dev = &ap->link.device[sdev->id];
1111  else
1112  ata_dev = &ap->pmp_link[sdev->channel].device[sdev->id];
1113  }
1114  else {
1115  ata_dev = &ap->link.device[sdev->id];
1116  }
1117 
1118  *handle = ata_dev_acpi_handle(ata_dev);
1119 
1120  if (!*handle)
1121  return -ENODEV;
1122 
1123  status = acpi_bus_get_device(*handle, &acpi_dev);
1124  if (ACPI_FAILURE(status))
1125  return 0;
1126 
1127  /*
1128  * If firmware has _PS3 or _PR3 for this device,
1129  * and this ata ODD device support device attention,
1130  * it means this device can be powered off
1131  */
1132  states = acpi_dev->power.states;
1133  if ((states[ACPI_STATE_D3_HOT].flags.valid ||
1134  states[ACPI_STATE_D3_COLD].flags.explicit_set) &&
1135  ata_dev->flags & ATA_DFLAG_DA)
1136  sdev->can_power_off = 1;
1137 
1138  return 0;
1139 }
1140 
1141 static int is_ata_port(const struct device *dev)
1142 {
1143  return dev->type == &ata_port_type;
1144 }
1145 
1146 static struct ata_port *dev_to_ata_port(struct device *dev)
1147 {
1148  while (!is_ata_port(dev)) {
1149  if (!dev->parent)
1150  return NULL;
1151  dev = dev->parent;
1152  }
1153  return to_ata_port(dev);
1154 }
1155 
1156 static int ata_acpi_find_device(struct device *dev, acpi_handle *handle)
1157 {
1158  struct ata_port *ap = dev_to_ata_port(dev);
1159 
1160  if (!ap)
1161  return -ENODEV;
1162 
1163  if (!compat_pci_ata(ap))
1164  return -ENODEV;
1165 
1166  if (scsi_is_host_device(dev))
1167  return ata_acpi_bind_host(ap, handle);
1168  else if (scsi_is_sdev_device(dev)) {
1169  struct scsi_device *sdev = to_scsi_device(dev);
1170 
1171  return ata_acpi_bind_device(ap, sdev, handle);
1172  } else
1173  return -ENODEV;
1174 }
1175 
1176 static int ata_acpi_find_dummy(struct device *dev, acpi_handle *handle)
1177 {
1178  return -ENODEV;
1179 }
1180 
1181 static struct acpi_bus_type ata_acpi_bus = {
1182  .find_bridge = ata_acpi_find_dummy,
1183  .find_device = ata_acpi_find_device,
1184 };
1185 
1187 {
1188  return scsi_register_acpi_bus_type(&ata_acpi_bus);
1189 }
1190 
1192 {
1193  scsi_unregister_acpi_bus_type(&ata_acpi_bus);
1194 }