1
2
3
4
5
6
7
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#include <acpi/acnames.h>
22#include <acpi/acnamesp.h>
23#include <acpi/acparser.h>
24#include <acpi/acexcep.h>
25#include <acpi/acmacros.h>
26#include <acpi/actypes.h>
27
28#define REGS_PER_GTF 7
29struct taskfile_array {
30 u8 tfa[REGS_PER_GTF];
31};
32
33struct GTM_buffer {
34 u32 PIO_speed0;
35 u32 DMA_speed0;
36 u32 PIO_speed1;
37 u32 DMA_speed1;
38 u32 GTM_flags;
39};
40
41struct ide_acpi_drive_link {
42 acpi_handle obj_handle;
43 u8 idbuff[512];
44};
45
46struct ide_acpi_hwif_link {
47 ide_hwif_t *hwif;
48 acpi_handle obj_handle;
49 struct GTM_buffer gtm;
50 struct ide_acpi_drive_link master;
51 struct ide_acpi_drive_link slave;
52};
53
54#undef DEBUGGING
55
56#ifdef DEBUGGING
57#define DEBPRINT(fmt, args...) \
58 printk(KERN_DEBUG "%s: " fmt, __FUNCTION__, ## args)
59#else
60#define DEBPRINT(fmt, args...) do {} while (0)
61#endif
62
63extern int ide_noacpi;
64extern int ide_noacpitfs;
65extern int ide_noacpionboot;
66
67static bool ide_noacpi_psx;
68static int no_acpi_psx(const struct dmi_system_id *id)
69{
70 ide_noacpi_psx = true;
71 printk(KERN_NOTICE"%s detected - disable ACPI _PSx.\n", id->ident);
72 return 0;
73}
74
75static const struct dmi_system_id ide_acpi_dmi_table[] = {
76
77
78 {
79 .callback = no_acpi_psx,
80 .ident = "HP nx9005",
81 .matches = {
82 DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies Ltd."),
83 DMI_MATCH(DMI_BIOS_VERSION, "KAM1.60")
84 },
85 },
86
87 { }
88};
89
90static int ide_acpi_blacklist(void)
91{
92 static int done;
93 if (done)
94 return 0;
95 done = 1;
96 dmi_check_system(ide_acpi_dmi_table);
97 return 0;
98}
99
100
101
102
103
104
105
106
107
108
109
110static 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
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;
158err:
159 kfree(dinfo);
160 return ret;
161}
162
163
164
165
166
167
168
169
170
171static 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
193
194
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
204
205
206
207
208
209
210
211static acpi_handle ide_acpi_drive_get_handle(ide_drive_t *drive)
212{
213 ide_hwif_t *hwif = HWIF(drive);
214 int port;
215 acpi_handle drive_handle;
216
217 if (!hwif->acpidata)
218 return NULL;
219
220 if (!hwif->acpidata->obj_handle)
221 return NULL;
222
223 port = hwif->channel ? drive->dn - 2: drive->dn;
224
225 DEBPRINT("ENTER: %s at channel#: %d port#: %d\n",
226 drive->name, hwif->channel, port);
227
228
229
230 drive_handle = acpi_get_child(hwif->acpidata->obj_handle, port);
231 DEBPRINT("drive %s handle 0x%p\n", drive->name, drive_handle);
232
233 return drive_handle;
234}
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251static int do_drive_get_GTF(ide_drive_t *drive,
252 unsigned int *gtf_length, unsigned long *gtf_address,
253 unsigned long *obj_loc)
254{
255 acpi_status status;
256 struct acpi_buffer output;
257 union acpi_object *out_obj;
258 ide_hwif_t *hwif = HWIF(drive);
259 struct device *dev = hwif->gendev.parent;
260 int err = -ENODEV;
261 int port;
262
263 *gtf_length = 0;
264 *gtf_address = 0UL;
265 *obj_loc = 0UL;
266
267 if (ide_noacpi)
268 return 0;
269
270 if (!dev) {
271 DEBPRINT("no PCI device for %s\n", hwif->name);
272 goto out;
273 }
274
275 if (!hwif->acpidata) {
276 DEBPRINT("no ACPI data for %s\n", hwif->name);
277 goto out;
278 }
279
280 port = hwif->channel ? drive->dn - 2: drive->dn;
281
282 DEBPRINT("ENTER: %s at %s, port#: %d, hard_port#: %d\n",
283 hwif->name, dev->bus_id, port, hwif->channel);
284
285 if (!drive->present) {
286 DEBPRINT("%s drive %d:%d not present\n",
287 hwif->name, hwif->channel, port);
288 goto out;
289 }
290
291
292 if (!drive->acpidata->obj_handle) {
293 drive->acpidata->obj_handle = ide_acpi_drive_get_handle(drive);
294 if (!drive->acpidata->obj_handle) {
295 DEBPRINT("No ACPI object found for %s\n",
296 drive->name);
297 goto out;
298 }
299 }
300
301
302 output.length = ACPI_ALLOCATE_BUFFER;
303 output.pointer = NULL;
304
305
306 err = -EIO;
307 status = acpi_evaluate_object(drive->acpidata->obj_handle, "_GTF",
308 NULL, &output);
309 if (ACPI_FAILURE(status)) {
310 printk(KERN_DEBUG
311 "%s: Run _GTF error: status = 0x%x\n",
312 __FUNCTION__, status);
313 goto out;
314 }
315
316 if (!output.length || !output.pointer) {
317 DEBPRINT("Run _GTF: "
318 "length or ptr is NULL (0x%llx, 0x%p)\n",
319 (unsigned long long)output.length,
320 output.pointer);
321 goto out;
322 }
323
324 out_obj = output.pointer;
325 if (out_obj->type != ACPI_TYPE_BUFFER) {
326 DEBPRINT("Run _GTF: error: "
327 "expected object type of ACPI_TYPE_BUFFER, "
328 "got 0x%x\n", out_obj->type);
329 err = -ENOENT;
330 kfree(output.pointer);
331 goto out;
332 }
333
334 if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
335 out_obj->buffer.length % REGS_PER_GTF) {
336 printk(KERN_ERR
337 "%s: unexpected GTF length (%d) or addr (0x%p)\n",
338 __FUNCTION__, out_obj->buffer.length,
339 out_obj->buffer.pointer);
340 err = -ENOENT;
341 kfree(output.pointer);
342 goto out;
343 }
344
345 *gtf_length = out_obj->buffer.length;
346 *gtf_address = (unsigned long)out_obj->buffer.pointer;
347 *obj_loc = (unsigned long)out_obj;
348 DEBPRINT("returning gtf_length=%d, gtf_address=0x%lx, obj_loc=0x%lx\n",
349 *gtf_length, *gtf_address, *obj_loc);
350 err = 0;
351out:
352 return err;
353}
354
355
356
357
358
359
360
361
362static int taskfile_load_raw(ide_drive_t *drive,
363 const struct taskfile_array *gtf)
364{
365 ide_task_t args;
366 int err = 0;
367
368 DEBPRINT("(0x1f1-1f7): hex: "
369 "%02x %02x %02x %02x %02x %02x %02x\n",
370 gtf->tfa[0], gtf->tfa[1], gtf->tfa[2],
371 gtf->tfa[3], gtf->tfa[4], gtf->tfa[5], gtf->tfa[6]);
372
373 memset(&args, 0, sizeof(ide_task_t));
374
375
376 memcpy(&args.tf_array[7], >f->tfa, 7);
377 args.tf_flags = IDE_TFLAG_TF | IDE_TFLAG_DEVICE;
378
379 if (ide_noacpitfs) {
380 DEBPRINT("_GTF execution disabled\n");
381 return err;
382 }
383
384 err = ide_no_data_taskfile(drive, &args);
385 if (err)
386 printk(KERN_ERR "%s: ide_no_data_taskfile failed: %u\n",
387 __FUNCTION__, err);
388
389 return err;
390}
391
392
393
394
395
396
397
398
399
400
401static int do_drive_set_taskfiles(ide_drive_t *drive,
402 unsigned int gtf_length,
403 unsigned long gtf_address)
404{
405 int rc = -ENODEV, err;
406 int gtf_count = gtf_length / REGS_PER_GTF;
407 int ix;
408 struct taskfile_array *gtf;
409
410 if (ide_noacpi)
411 return 0;
412
413 DEBPRINT("ENTER: %s, hard_port#: %d\n", drive->name, drive->dn);
414
415 if (!drive->present)
416 goto out;
417 if (!gtf_count)
418 goto out;
419
420 DEBPRINT("total GTF bytes=%u (0x%x), gtf_count=%d, addr=0x%lx\n",
421 gtf_length, gtf_length, gtf_count, gtf_address);
422
423 if (gtf_length % REGS_PER_GTF) {
424 printk(KERN_ERR "%s: unexpected GTF length (%d)\n",
425 __FUNCTION__, gtf_length);
426 goto out;
427 }
428
429 rc = 0;
430 for (ix = 0; ix < gtf_count; ix++) {
431 gtf = (struct taskfile_array *)
432 (gtf_address + ix * REGS_PER_GTF);
433
434
435 err = taskfile_load_raw(drive, gtf);
436 if (err)
437 rc = err;
438 }
439
440out:
441 return rc;
442}
443
444
445
446
447
448
449
450
451
452
453
454
455
456int ide_acpi_exec_tfs(ide_drive_t *drive)
457{
458 int ret;
459 unsigned int gtf_length;
460 unsigned long gtf_address;
461 unsigned long obj_loc;
462
463 if (ide_noacpi)
464 return 0;
465
466 DEBPRINT("call get_GTF, drive=%s port=%d\n", drive->name, drive->dn);
467
468 ret = do_drive_get_GTF(drive, >f_length, >f_address, &obj_loc);
469 if (ret < 0) {
470 DEBPRINT("get_GTF error (%d)\n", ret);
471 return ret;
472 }
473
474 DEBPRINT("call set_taskfiles, drive=%s\n", drive->name);
475
476 ret = do_drive_set_taskfiles(drive, gtf_length, gtf_address);
477 kfree((void *)obj_loc);
478 if (ret < 0) {
479 DEBPRINT("set_taskfiles error (%d)\n", ret);
480 }
481
482 DEBPRINT("ret=%d\n", ret);
483
484 return ret;
485}
486
487
488
489
490
491
492
493
494void ide_acpi_get_timing(ide_hwif_t *hwif)
495{
496 acpi_status status;
497 struct acpi_buffer output;
498 union acpi_object *out_obj;
499
500 if (ide_noacpi)
501 return;
502
503 DEBPRINT("ENTER:\n");
504
505 if (!hwif->acpidata) {
506 DEBPRINT("no ACPI data for %s\n", hwif->name);
507 return;
508 }
509
510
511 output.length = ACPI_ALLOCATE_BUFFER;
512 output.pointer = NULL;
513
514
515 status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_GTM",
516 NULL, &output);
517
518 DEBPRINT("_GTM status: %d, outptr: 0x%p, outlen: 0x%llx\n",
519 status, output.pointer,
520 (unsigned long long)output.length);
521
522 if (ACPI_FAILURE(status)) {
523 DEBPRINT("Run _GTM error: status = 0x%x\n", status);
524 return;
525 }
526
527 if (!output.length || !output.pointer) {
528 DEBPRINT("Run _GTM: length or ptr is NULL (0x%llx, 0x%p)\n",
529 (unsigned long long)output.length,
530 output.pointer);
531 kfree(output.pointer);
532 return;
533 }
534
535 out_obj = output.pointer;
536 if (out_obj->type != ACPI_TYPE_BUFFER) {
537 kfree(output.pointer);
538 DEBPRINT("Run _GTM: error: "
539 "expected object type of ACPI_TYPE_BUFFER, "
540 "got 0x%x\n", out_obj->type);
541 return;
542 }
543
544 if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
545 out_obj->buffer.length != sizeof(struct GTM_buffer)) {
546 kfree(output.pointer);
547 printk(KERN_ERR
548 "%s: unexpected _GTM length (0x%x)[should be 0x%zx] or "
549 "addr (0x%p)\n",
550 __FUNCTION__, out_obj->buffer.length,
551 sizeof(struct GTM_buffer), out_obj->buffer.pointer);
552 return;
553 }
554
555 memcpy(&hwif->acpidata->gtm, out_obj->buffer.pointer,
556 sizeof(struct GTM_buffer));
557
558 DEBPRINT("_GTM info: ptr: 0x%p, len: 0x%x, exp.len: 0x%Zx\n",
559 out_obj->buffer.pointer, out_obj->buffer.length,
560 sizeof(struct GTM_buffer));
561
562 DEBPRINT("_GTM fields: 0x%x, 0x%x, 0x%x, 0x%x, 0x%x\n",
563 hwif->acpidata->gtm.PIO_speed0,
564 hwif->acpidata->gtm.DMA_speed0,
565 hwif->acpidata->gtm.PIO_speed1,
566 hwif->acpidata->gtm.DMA_speed1,
567 hwif->acpidata->gtm.GTM_flags);
568
569 kfree(output.pointer);
570}
571
572
573
574
575
576
577
578
579
580
581
582void ide_acpi_push_timing(ide_hwif_t *hwif)
583{
584 acpi_status status;
585 struct acpi_object_list input;
586 union acpi_object in_params[3];
587 struct ide_acpi_drive_link *master = &hwif->acpidata->master;
588 struct ide_acpi_drive_link *slave = &hwif->acpidata->slave;
589
590 if (ide_noacpi)
591 return;
592
593 DEBPRINT("ENTER:\n");
594
595 if (!hwif->acpidata) {
596 DEBPRINT("no ACPI data for %s\n", hwif->name);
597 return;
598 }
599
600
601
602
603 input.count = 3;
604 input.pointer = in_params;
605 in_params[0].type = ACPI_TYPE_BUFFER;
606 in_params[0].buffer.length = sizeof(struct GTM_buffer);
607 in_params[0].buffer.pointer = (u8 *)&hwif->acpidata->gtm;
608 in_params[1].type = ACPI_TYPE_BUFFER;
609 in_params[1].buffer.length = sizeof(struct hd_driveid);
610 in_params[1].buffer.pointer = (u8 *)&master->idbuff;
611 in_params[2].type = ACPI_TYPE_BUFFER;
612 in_params[2].buffer.length = sizeof(struct hd_driveid);
613 in_params[2].buffer.pointer = (u8 *)&slave->idbuff;
614
615
616 status = acpi_evaluate_object(hwif->acpidata->obj_handle, "_STM",
617 &input, NULL);
618
619 if (ACPI_FAILURE(status)) {
620 DEBPRINT("Run _STM error: status = 0x%x\n", status);
621 }
622 DEBPRINT("_STM status: %d\n", status);
623}
624
625
626
627
628
629
630
631
632
633void ide_acpi_set_state(ide_hwif_t *hwif, int on)
634{
635 int unit;
636
637 if (ide_noacpi || ide_noacpi_psx)
638 return;
639
640 DEBPRINT("ENTER:\n");
641
642 if (!hwif->acpidata) {
643 DEBPRINT("no ACPI data for %s\n", hwif->name);
644 return;
645 }
646
647 if (on)
648 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D0);
649 for (unit = 0; unit < MAX_DRIVES; ++unit) {
650 ide_drive_t *drive = &hwif->drives[unit];
651
652 if (!drive->acpidata->obj_handle)
653 drive->acpidata->obj_handle = ide_acpi_drive_get_handle(drive);
654
655 if (drive->acpidata->obj_handle && drive->present) {
656 acpi_bus_set_power(drive->acpidata->obj_handle,
657 on? ACPI_STATE_D0: ACPI_STATE_D3);
658 }
659 }
660 if (!on)
661 acpi_bus_set_power(hwif->acpidata->obj_handle, ACPI_STATE_D3);
662}
663
664
665
666
667
668
669
670
671
672
673
674
675void ide_acpi_init(ide_hwif_t *hwif)
676{
677 ide_acpi_blacklist();
678
679 hwif->acpidata = kzalloc(sizeof(struct ide_acpi_hwif_link), GFP_KERNEL);
680 if (!hwif->acpidata)
681 return;
682
683 hwif->acpidata->obj_handle = ide_acpi_hwif_get_handle(hwif);
684 if (!hwif->acpidata->obj_handle) {
685 DEBPRINT("no ACPI object for %s found\n", hwif->name);
686 kfree(hwif->acpidata);
687 hwif->acpidata = NULL;
688 }
689}
690
691void ide_acpi_port_init_devices(ide_hwif_t *hwif)
692{
693 ide_drive_t *drive;
694 int i, err;
695
696 if (hwif->acpidata == NULL)
697 return;
698
699
700
701
702
703
704 hwif->drives[0].acpidata = &hwif->acpidata->master;
705 hwif->drives[1].acpidata = &hwif->acpidata->slave;
706
707
708
709
710 for (i = 0; i < MAX_DRIVES; i++) {
711 drive = &hwif->drives[i];
712
713 if (!drive->present)
714 continue;
715
716 err = taskfile_lib_get_identify(drive, drive->acpidata->idbuff);
717 if (err)
718 DEBPRINT("identify device %s failed (%d)\n",
719 drive->name, err);
720 }
721
722 if (ide_noacpionboot) {
723 DEBPRINT("ACPI methods disabled on boot\n");
724 return;
725 }
726
727
728 ide_acpi_set_state(hwif, 1);
729
730
731
732 ide_acpi_get_timing(hwif);
733 ide_acpi_push_timing(hwif);
734
735 for (i = 0; i < MAX_DRIVES; i++) {
736 drive = &hwif->drives[i];
737
738 if (drive->present)
739
740 ide_acpi_exec_tfs(drive);
741 }
742}
743