Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi...
[linux-2.6] / drivers / ide / ide-acpi.c
1 /*
2  * Provides ACPI support for IDE drives.
3  *
4  * Copyright (C) 2005 Intel Corp.
5  * Copyright (C) 2005 Randy Dunlap
6  * Copyright (C) 2006 SUSE Linux Products GmbH
7  * Copyright (C) 2006 Hannes Reinecke
8  */
9
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 <acpi/acpi.h>
16 #include <linux/ide.h>
17 #include <linux/pci.h>
18 #include <linux/dmi.h>
19
20 #include <acpi/acpi_bus.h>
21
22 #define REGS_PER_GTF            7
23
24 struct GTM_buffer {
25         u32     PIO_speed0;
26         u32     DMA_speed0;
27         u32     PIO_speed1;
28         u32     DMA_speed1;
29         u32     GTM_flags;
30 };
31
32 struct ide_acpi_drive_link {
33         acpi_handle      obj_handle;
34         u8               idbuff[512];
35 };
36
37 struct ide_acpi_hwif_link {
38         ide_hwif_t                      *hwif;
39         acpi_handle                      obj_handle;
40         struct GTM_buffer                gtm;
41         struct ide_acpi_drive_link       master;
42         struct ide_acpi_drive_link       slave;
43 };
44
45 #undef DEBUGGING
46 /* note: adds function name and KERN_DEBUG */
47 #ifdef DEBUGGING
48 #define DEBPRINT(fmt, args...)  \
49                 printk(KERN_DEBUG "%s: " fmt, __func__, ## args)
50 #else
51 #define DEBPRINT(fmt, args...)  do {} while (0)
52 #endif  /* DEBUGGING */
53
54 static int ide_noacpi;
55 module_param_named(noacpi, ide_noacpi, bool, 0);
56 MODULE_PARM_DESC(noacpi, "disable IDE ACPI support");
57
58 static int ide_acpigtf;
59 module_param_named(acpigtf, ide_acpigtf, bool, 0);
60 MODULE_PARM_DESC(acpigtf, "enable IDE ACPI _GTF support");
61
62 static int ide_acpionboot;
63 module_param_named(acpionboot, ide_acpionboot, bool, 0);
64 MODULE_PARM_DESC(acpionboot, "call IDE ACPI methods on boot");
65
66 static bool ide_noacpi_psx;
67 static int no_acpi_psx(const struct dmi_system_id *id)
68 {
69         ide_noacpi_psx = true;
70         printk(KERN_NOTICE"%s detected - disable ACPI _PSx.\n", id->ident);
71         return 0;
72 }
73
74 static const struct dmi_system_id ide_acpi_dmi_table[] = {
75         /* Bug 9673. */
76         /* We should check if this is because ACPI NVS isn't save/restored. */
77         {
78                 .callback = no_acpi_psx,
79                 .ident    = "HP nx9005",
80                 .matches  = {
81                         DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies Ltd."),
82                         DMI_MATCH(DMI_BIOS_VERSION, "KAM1.60")
83                 },
84         },
85
86         { }     /* terminate list */
87 };
88
89 int ide_acpi_init(void)
90 {
91         dmi_check_system(ide_acpi_dmi_table);
92         return 0;
93 }
94
95 bool ide_port_acpi(ide_hwif_t *hwif)
96 {
97         return ide_noacpi == 0 && hwif->acpidata;
98 }
99
100 /**
101  * ide_get_dev_handle - finds acpi_handle and PCI device.function
102  * @dev: device to locate
103  * @handle: returned acpi_handle for @dev
104  * @pcidevfn: return PCI device.func for @dev
105  *
106  * Returns the ACPI object handle to the corresponding PCI device.
107  *
108  * Returns 0 on success, <0 on error.
109  */
110 static int ide_get_dev_handle(struct device *dev, acpi_handle *handle,
111                                acpi_integer *pcidevfn)
112 {
113         struct pci_dev *pdev = to_pci_dev(dev);
114         unsigned int bus, devnum, func;
115         acpi_integer addr;
116         acpi_handle dev_handle;
117         struct acpi_buffer buffer = {.length = ACPI_ALLOCATE_BUFFER,
118                                         .pointer = NULL};
119         acpi_status status;
120         struct acpi_device_info *dinfo = NULL;
121         int ret = -ENODEV;
122
123         bus = pdev->bus->number;
124         devnum = PCI_SLOT(pdev->devfn);
125         func = PCI_FUNC(pdev->devfn);
126         /* ACPI _ADR encoding for PCI bus: */
127         addr = (acpi_integer)(devnum << 16 | func);
128
129         DEBPRINT("ENTER: pci %02x:%02x.%01x\n", bus, devnum, func);
130
131         dev_handle = DEVICE_ACPI_HANDLE(dev);
132         if (!dev_handle) {
133                 DEBPRINT("no acpi handle for device\n");
134                 goto err;
135         }
136
137         status = acpi_get_object_info(dev_handle, &buffer);
138         if (ACPI_FAILURE(status)) {
139                 DEBPRINT("get_object_info for device failed\n");
140                 goto err;
141         }
142         dinfo = buffer.pointer;
143         if (dinfo && (dinfo->valid & ACPI_VALID_ADR) &&
144             dinfo->address == addr) {
145                 *pcidevfn = addr;
146                 *handle = dev_handle;
147         } else {
148                 DEBPRINT("get_object_info for device has wrong "
149                         " address: %llu, should be %u\n",
150                         dinfo ? (unsigned long long)dinfo->address : -1ULL,
151                         (unsigned int)addr);
152                 goto err;
153         }
154
155         DEBPRINT("for dev=0x%x.%x, addr=0x%llx, *handle=0x%p\n",
156                  devnum, func, (unsigned long long)addr, *handle);
157         ret = 0;
158 err:
159         kfree(dinfo);
160         return ret;
161 }
162
163 /**
164  * ide_acpi_hwif_get_handle - Get ACPI object handle for a given hwif
165  * @hwif: device to locate
166  *
167  * Retrieves the object handle for a given hwif.
168  *
169  * Returns handle on success, 0 on error.
170  */
171 static acpi_handle ide_acpi_hwif_get_handle(ide_hwif_t *hwif)
172 {
173         struct device           *dev = hwif->gendev.parent;
174         acpi_handle             uninitialized_var(dev_handle);
175         acpi_integer            pcidevfn;
176         acpi_handle             chan_handle;
177         int                     err;
178
179         DEBPRINT("ENTER: device %s\n", hwif->name);
180
181         if (!dev) {
182                 DEBPRINT("no PCI device for %s\n", hwif->name);
183                 return NULL;
184         }
185
186         err = ide_get_dev_handle(dev, &dev_handle, &pcidevfn);
187         if (err < 0) {
188                 DEBPRINT("ide_get_dev_handle failed (%d)\n", err);
189                 return NULL;
190         }
191
192         /* get child objects of dev_handle == channel objects,
193          * + _their_ children == drive objects */
194         /* channel is hwif->channel */
195         chan_handle = acpi_get_child(dev_handle, hwif->channel);
196         DEBPRINT("chan adr=%d: handle=0x%p\n",
197                  hwif->channel, chan_handle);
198
199         return chan_handle;
200 }
201
202 /**
203  * do_drive_get_GTF - get the drive bootup default taskfile settings
204  * @drive: the drive for which the taskfile settings should be retrieved
205  * @gtf_length: number of bytes of _GTF data returned at @gtf_address
206  * @gtf_address: buffer containing _GTF taskfile arrays
207  *
208  * The _GTF method has no input parameters.
209  * It returns a variable number of register set values (registers
210  * hex 1F1..1F7, taskfiles).
211  * The <variable number> is not known in advance, so have ACPI-CA
212  * allocate the buffer as needed and return it, then free it later.
213  *
214  * The returned @gtf_length and @gtf_address are only valid if the
215  * function return value is 0.
216  */
217 static int do_drive_get_GTF(ide_drive_t *drive,
218                      unsigned int *gtf_length, unsigned long *gtf_address,
219                      unsigned long *obj_loc)
220 {
221         acpi_status                     status;
222         struct acpi_buffer              output;
223         union acpi_object               *out_obj;
224         int                             err = -ENODEV;
225
226         *gtf_length = 0;
227         *gtf_address = 0UL;
228         *obj_loc = 0UL;
229
230         if (!drive->acpidata->obj_handle) {
231                 DEBPRINT("No ACPI object found for %s\n", drive->name);
232                 goto out;
233         }
234
235         /* Setting up output buffer */
236         output.length = ACPI_ALLOCATE_BUFFER;
237         output.pointer = NULL;  /* ACPI-CA sets this; save/free it later */
238
239         /* _GTF has no input parameters */
240         err = -EIO;
241         status = acpi_evaluate_object(drive->acpidata->obj_handle, "_GTF",
242                                       NULL, &output);
243         if (ACPI_FAILURE(status)) {
244                 printk(KERN_DEBUG
245                        "%s: Run _GTF error: status = 0x%x\n",
246                        __func__, status);
247                 goto out;
248         }
249
250         if (!output.length || !output.pointer) {
251                 DEBPRINT("Run _GTF: "
252                        "length or ptr is NULL (0x%llx, 0x%p)\n",
253                        (unsigned long long)output.length,
254                        output.pointer);
255                 goto out;
256         }
257
258         out_obj = output.pointer;
259         if (out_obj->type != ACPI_TYPE_BUFFER) {
260                 DEBPRINT("Run _GTF: error: "
261                        "expected object type of ACPI_TYPE_BUFFER, "
262                        "got 0x%x\n", out_obj->type);
263                 err = -ENOENT;
264                 kfree(output.pointer);
265                 goto out;
266         }
267
268         if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
269             out_obj->buffer.length % REGS_PER_GTF) {
270                 printk(KERN_ERR
271                        "%s: unexpected GTF length (%d) or addr (0x%p)\n",
272                        __func__, out_obj->buffer.length,
273                        out_obj->buffer.pointer);
274                 err = -ENOENT;
275                 kfree(output.pointer);
276                 goto out;
277         }
278
279         *gtf_length = out_obj->buffer.length;
280         *gtf_address = (unsigned long)out_obj->buffer.pointer;
281         *obj_loc = (unsigned long)out_obj;
282         DEBPRINT("returning gtf_length=%d, gtf_address=0x%lx, obj_loc=0x%lx\n",
283                  *gtf_length, *gtf_address, *obj_loc);
284         err = 0;
285 out:
286         return err;
287 }
288
289 /**
290  * do_drive_set_taskfiles - write the drive taskfile settings from _GTF
291  * @drive: the drive to which the taskfile command should be sent
292  * @gtf_length: total number of bytes of _GTF taskfiles
293  * @gtf_address: location of _GTF taskfile arrays
294  *
295  * Write {gtf_address, length gtf_length} in groups of
296  * REGS_PER_GTF bytes.
297  */
298 static int do_drive_set_taskfiles(ide_drive_t *drive,
299                                   unsigned int gtf_length,
300                                   unsigned long gtf_address)
301 {
302         int                     rc = 0, err;
303         int                     gtf_count = gtf_length / REGS_PER_GTF;
304         int                     ix;
305
306         DEBPRINT("total GTF bytes=%u (0x%x), gtf_count=%d, addr=0x%lx\n",
307                  gtf_length, gtf_length, gtf_count, gtf_address);
308
309         /* send all taskfile registers (0x1f1-0x1f7) *in*that*order* */
310         for (ix = 0; ix < gtf_count; ix++) {
311                 u8 *gtf = (u8 *)(gtf_address + ix * REGS_PER_GTF);
312                 struct ide_cmd cmd;
313
314                 DEBPRINT("(0x1f1-1f7): "
315                          "hex: %02x %02x %02x %02x %02x %02x %02x\n",
316                          gtf[0], gtf[1], gtf[2],
317                          gtf[3], gtf[4], gtf[5], gtf[6]);
318
319                 if (!ide_acpigtf) {
320                         DEBPRINT("_GTF execution disabled\n");
321                         continue;
322                 }
323
324                 /* convert GTF to taskfile */
325                 memset(&cmd, 0, sizeof(cmd));
326                 memcpy(&cmd.tf.feature, gtf, REGS_PER_GTF);
327                 cmd.valid.out.tf = IDE_VALID_OUT_TF | IDE_VALID_DEVICE;
328                 cmd.valid.in.tf  = IDE_VALID_IN_TF  | IDE_VALID_DEVICE;
329
330                 err = ide_no_data_taskfile(drive, &cmd);
331                 if (err) {
332                         printk(KERN_ERR "%s: ide_no_data_taskfile failed: %u\n",
333                                         __func__, err);
334                         rc = err;
335                 }
336         }
337
338         return rc;
339 }
340
341 /**
342  * ide_acpi_exec_tfs - get then write drive taskfile settings
343  * @drive: the drive for which the taskfile settings should be
344  *         written.
345  *
346  * According to the ACPI spec this should be called after _STM
347  * has been evaluated for the interface. Some ACPI vendors interpret
348  * that as a hard requirement and modify the taskfile according
349  * to the Identify Drive information passed down with _STM.
350  * So one should really make sure to call this only after _STM has
351  * been executed.
352  */
353 int ide_acpi_exec_tfs(ide_drive_t *drive)
354 {
355         int             ret;
356         unsigned int    gtf_length;
357         unsigned long   gtf_address;
358         unsigned long   obj_loc;
359
360         DEBPRINT("call get_GTF, drive=%s port=%d\n", drive->name, drive->dn);
361
362         ret = do_drive_get_GTF(drive, &gtf_length, &gtf_address, &obj_loc);
363         if (ret < 0) {
364                 DEBPRINT("get_GTF error (%d)\n", ret);
365                 return ret;
366         }
367
368         DEBPRINT("call set_taskfiles, drive=%s\n", drive->name);
369
370         ret = do_drive_set_taskfiles(drive, gtf_length, gtf_address);
371         kfree((void *)obj_loc);
372         if (ret < 0) {
373                 DEBPRINT("set_taskfiles error (%d)\n", ret);
374         }
375
376         DEBPRINT("ret=%d\n", ret);
377
378         return ret;
379 }
380
381 /**
382  * ide_acpi_get_timing - get the channel (controller) timings
383  * @hwif: target IDE interface (channel)
384  *
385  * This function executes the _GTM ACPI method for the target channel.
386  *
387  */
388 void ide_acpi_get_timing(ide_hwif_t *hwif)
389 {
390         acpi_status             status;
391         struct acpi_buffer      output;
392         union acpi_object       *out_obj;
393
394         /* Setting up output buffer for _GTM */
395         output.length = ACPI_ALLOCATE_BUFFER;
396         output.pointer = NULL;  /* ACPI-CA sets this; save/free it later */
397
398         /* _GTM has no input parameters */
399         status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_GTM",
400                                       NULL, &output);
401
402         DEBPRINT("_GTM status: %d, outptr: 0x%p, outlen: 0x%llx\n",
403                  status, output.pointer,
404                  (unsigned long long)output.length);
405
406         if (ACPI_FAILURE(status)) {
407                 DEBPRINT("Run _GTM error: status = 0x%x\n", status);
408                 return;
409         }
410
411         if (!output.length || !output.pointer) {
412                 DEBPRINT("Run _GTM: length or ptr is NULL (0x%llx, 0x%p)\n",
413                        (unsigned long long)output.length,
414                        output.pointer);
415                 kfree(output.pointer);
416                 return;
417         }
418
419         out_obj = output.pointer;
420         if (out_obj->type != ACPI_TYPE_BUFFER) {
421                 kfree(output.pointer);
422                 DEBPRINT("Run _GTM: error: "
423                        "expected object type of ACPI_TYPE_BUFFER, "
424                        "got 0x%x\n", out_obj->type);
425                 return;
426         }
427
428         if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
429             out_obj->buffer.length != sizeof(struct GTM_buffer)) {
430                 kfree(output.pointer);
431                 printk(KERN_ERR
432                         "%s: unexpected _GTM length (0x%x)[should be 0x%zx] or "
433                         "addr (0x%p)\n",
434                         __func__, out_obj->buffer.length,
435                         sizeof(struct GTM_buffer), out_obj->buffer.pointer);
436                 return;
437         }
438
439         memcpy(&hwif->acpidata->gtm, out_obj->buffer.pointer,
440                sizeof(struct GTM_buffer));
441
442         DEBPRINT("_GTM info: ptr: 0x%p, len: 0x%x, exp.len: 0x%Zx\n",
443                  out_obj->buffer.pointer, out_obj->buffer.length,
444                  sizeof(struct GTM_buffer));
445
446         DEBPRINT("_GTM fields: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
447                  hwif->acpidata->gtm.PIO_speed0,
448                  hwif->acpidata->gtm.DMA_speed0,
449                  hwif->acpidata->gtm.PIO_speed1,
450                  hwif->acpidata->gtm.DMA_speed1,
451                  hwif->acpidata->gtm.GTM_flags);
452
453         kfree(output.pointer);
454 }
455
456 /**
457  * ide_acpi_push_timing - set the channel (controller) timings
458  * @hwif: target IDE interface (channel)
459  *
460  * This function executes the _STM ACPI method for the target channel.
461  *
462  * _STM requires Identify Drive data, which has to passed as an argument.
463  * Unfortunately drive->id is a mangled version which we can't readily
464  * use; hence we'll get the information afresh.
465  */
466 void ide_acpi_push_timing(ide_hwif_t *hwif)
467 {
468         acpi_status             status;
469         struct acpi_object_list input;
470         union acpi_object       in_params[3];
471         struct ide_acpi_drive_link      *master = &hwif->acpidata->master;
472         struct ide_acpi_drive_link      *slave = &hwif->acpidata->slave;
473
474         /* Give the GTM buffer + drive Identify data to the channel via the
475          * _STM method: */
476         /* setup input parameters buffer for _STM */
477         input.count = 3;
478         input.pointer = in_params;
479         in_params[0].type = ACPI_TYPE_BUFFER;
480         in_params[0].buffer.length = sizeof(struct GTM_buffer);
481         in_params[0].buffer.pointer = (u8 *)&hwif->acpidata->gtm;
482         in_params[1].type = ACPI_TYPE_BUFFER;
483         in_params[1].buffer.length = ATA_ID_WORDS * 2;
484         in_params[1].buffer.pointer = (u8 *)&master->idbuff;
485         in_params[2].type = ACPI_TYPE_BUFFER;
486         in_params[2].buffer.length = ATA_ID_WORDS * 2;
487         in_params[2].buffer.pointer = (u8 *)&slave->idbuff;
488         /* Output buffer: _STM has no output */
489
490         status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_STM",
491                                       &input, NULL);
492
493         if (ACPI_FAILURE(status)) {
494                 DEBPRINT("Run _STM error: status = 0x%x\n", status);
495         }
496         DEBPRINT("_STM status: %d\n", status);
497 }
498
499 /**
500  * ide_acpi_set_state - set the channel power state
501  * @hwif: target IDE interface
502  * @on: state, on/off
503  *
504  * This function executes the _PS0/_PS3 ACPI method to set the power state.
505  * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
506  */
507 void ide_acpi_set_state(ide_hwif_t *hwif, int on)
508 {
509         ide_drive_t *drive;
510         int i;
511
512         if (ide_noacpi_psx)
513                 return;
514
515         DEBPRINT("ENTER:\n");
516
517         /* channel first and then drives for power on and verse versa for power off */
518         if (on)
519                 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D0);
520
521         ide_port_for_each_present_dev(i, drive, hwif) {
522                 if (drive->acpidata->obj_handle)
523                         acpi_bus_set_power(drive->acpidata->obj_handle,
524                                            on ? ACPI_STATE_D0 : ACPI_STATE_D3);
525         }
526
527         if (!on)
528                 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D3);
529 }
530
531 /**
532  * ide_acpi_init_port - initialize the ACPI link for an IDE interface
533  * @hwif: target IDE interface (channel)
534  *
535  * The ACPI spec is not quite clear when the drive identify buffer
536  * should be obtained. Calling IDENTIFY DEVICE during shutdown
537  * is not the best of ideas as the drive might already being put to
538  * sleep. And obviously we can't call it during resume.
539  * So we get the information during startup; but this means that
540  * any changes during run-time will be lost after resume.
541  */
542 void ide_acpi_init_port(ide_hwif_t *hwif)
543 {
544         hwif->acpidata = kzalloc(sizeof(struct ide_acpi_hwif_link), GFP_KERNEL);
545         if (!hwif->acpidata)
546                 return;
547
548         hwif->acpidata->obj_handle = ide_acpi_hwif_get_handle(hwif);
549         if (!hwif->acpidata->obj_handle) {
550                 DEBPRINT("no ACPI object for %s found\n", hwif->name);
551                 kfree(hwif->acpidata);
552                 hwif->acpidata = NULL;
553         }
554 }
555
556 void ide_acpi_port_init_devices(ide_hwif_t *hwif)
557 {
558         ide_drive_t *drive;
559         int i, err;
560
561         if (hwif->acpidata == NULL)
562                 return;
563
564         /*
565          * The ACPI spec mandates that we send information
566          * for both drives, regardless whether they are connected
567          * or not.
568          */
569         hwif->devices[0]->acpidata = &hwif->acpidata->master;
570         hwif->devices[1]->acpidata = &hwif->acpidata->slave;
571
572         /* get _ADR info for each device */
573         ide_port_for_each_present_dev(i, drive, hwif) {
574                 acpi_handle dev_handle;
575
576                 DEBPRINT("ENTER: %s at channel#: %d port#: %d\n",
577                          drive->name, hwif->channel, drive->dn & 1);
578
579                 /* TBD: could also check ACPI object VALID bits */
580                 dev_handle = acpi_get_child(hwif->acpidata->obj_handle,
581                                             drive->dn & 1);
582
583                 DEBPRINT("drive %s handle 0x%p\n", drive->name, dev_handle);
584
585                 drive->acpidata->obj_handle = dev_handle;
586         }
587
588         /* send IDENTIFY for each device */
589         ide_port_for_each_present_dev(i, drive, hwif) {
590                 err = taskfile_lib_get_identify(drive, drive->acpidata->idbuff);
591                 if (err)
592                         DEBPRINT("identify device %s failed (%d)\n",
593                                  drive->name, err);
594         }
595
596         if (ide_noacpi || ide_acpionboot == 0) {
597                 DEBPRINT("ACPI methods disabled on boot\n");
598                 return;
599         }
600
601         /* ACPI _PS0 before _STM */
602         ide_acpi_set_state(hwif, 1);
603         /*
604          * ACPI requires us to call _STM on startup
605          */
606         ide_acpi_get_timing(hwif);
607         ide_acpi_push_timing(hwif);
608
609         ide_port_for_each_present_dev(i, drive, hwif) {
610                 ide_acpi_exec_tfs(drive);
611         }
612 }