3 * Provides ACPI support for PATA/SATA.
5 * Copyright (C) 2006 Intel Corp.
6 * Copyright (C) 2006 Randy Dunlap
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/errno.h>
13 #include <linux/kernel.h>
14 #include <linux/acpi.h>
15 #include <linux/libata.h>
16 #include <linux/pci.h>
19 #include <acpi/acpi_bus.h>
20 #include <acpi/acnames.h>
21 #include <acpi/acnamesp.h>
22 #include <acpi/acparser.h>
23 #include <acpi/acexcep.h>
24 #include <acpi/acmacros.h>
25 #include <acpi/actypes.h>
27 #define SATA_ROOT_PORT(x) (((x) >> 16) & 0xffff)
28 #define SATA_PORT_NUMBER(x) ((x) & 0xffff) /* or NO_PORT_MULT */
29 #define NO_PORT_MULT 0xffff
30 #define SATA_ADR_RSVD 0xffffffff
32 #define REGS_PER_GTF 7
33 struct taskfile_array {
34 u8 tfa[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
38 * Helper - belongs in the PCI layer somewhere eventually
40 static int is_pci_dev(struct device *dev)
42 return (dev->bus == &pci_bus_type);
46 * sata_get_dev_handle - finds acpi_handle and PCI device.function
47 * @dev: device to locate
48 * @handle: returned acpi_handle for @dev
49 * @pcidevfn: return PCI device.func for @dev
51 * This function is somewhat SATA-specific. Or at least the
52 * PATA & SATA versions of this function are different,
53 * so it's not entirely generic code.
55 * Returns 0 on success, <0 on error.
57 static int sata_get_dev_handle(struct device *dev, acpi_handle *handle,
58 acpi_integer *pcidevfn)
60 struct pci_dev *pci_dev;
66 pci_dev = to_pci_dev(dev); /* NOTE: PCI-specific */
67 /* Please refer to the ACPI spec for the syntax of _ADR. */
68 addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
70 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(dev->parent), addr);
77 * pata_get_dev_handle - finds acpi_handle and PCI device.function
78 * @dev: device to locate
79 * @handle: returned acpi_handle for @dev
80 * @pcidevfn: return PCI device.func for @dev
82 * The PATA and SATA versions of this function are different.
84 * Returns 0 on success, <0 on error.
86 static int pata_get_dev_handle(struct device *dev, acpi_handle *handle,
87 acpi_integer *pcidevfn)
89 unsigned int bus, devnum, func;
91 acpi_handle dev_handle, parent_handle;
92 struct acpi_buffer buffer = {.length = ACPI_ALLOCATE_BUFFER,
95 struct acpi_device_info *dinfo = NULL;
102 pdev = to_pci_dev(dev);
104 bus = pdev->bus->number;
105 devnum = PCI_SLOT(pdev->devfn);
106 func = PCI_FUNC(pdev->devfn);
108 dev_handle = DEVICE_ACPI_HANDLE(dev);
109 parent_handle = DEVICE_ACPI_HANDLE(dev->parent);
111 status = acpi_get_object_info(parent_handle, &buffer);
112 if (ACPI_FAILURE(status))
115 dinfo = buffer.pointer;
116 if (dinfo && (dinfo->valid & ACPI_VALID_ADR) &&
117 dinfo->address == bus) {
118 /* ACPI spec for _ADR for PCI bus: */
119 addr = (acpi_integer)(devnum << 16 | func);
121 *handle = dev_handle;
134 struct walk_info { /* can be trimmed some */
136 struct acpi_device *adev;
138 acpi_integer pcidevfn;
139 unsigned int drivenum;
140 acpi_handle obj_handle;
141 struct ata_port *ataport;
142 struct ata_device *atadev;
145 char basepath[ACPI_PATHNAME_MAX];
149 static acpi_status get_devices(acpi_handle handle,
150 u32 level, void *context, void **return_value)
153 struct walk_info *winfo = context;
154 struct acpi_buffer namebuf = {ACPI_ALLOCATE_BUFFER, NULL};
156 struct acpi_buffer buffer;
157 struct acpi_device_info *dinfo;
159 status = acpi_get_name(handle, ACPI_FULL_PATHNAME, &namebuf);
162 pathname = namebuf.pointer;
164 buffer.length = ACPI_ALLOCATE_BUFFER;
165 buffer.pointer = NULL;
166 status = acpi_get_object_info(handle, &buffer);
167 if (ACPI_FAILURE(status))
170 dinfo = buffer.pointer;
172 /* find full device path name for pcidevfn */
173 if (dinfo && (dinfo->valid & ACPI_VALID_ADR) &&
174 dinfo->address == winfo->pcidevfn) {
175 if (ata_msg_probe(winfo->ataport))
176 ata_dev_printk(winfo->atadev, KERN_DEBUG,
177 ":%s: matches pcidevfn (0x%llx)\n",
178 pathname, winfo->pcidevfn);
179 strlcpy(winfo->basepath, pathname,
180 sizeof(winfo->basepath));
181 winfo->basepath_len = strlen(pathname);
185 /* if basepath is not yet known, ignore this object */
186 if (!winfo->basepath_len)
189 /* if this object is in scope of basepath, maybe use it */
190 if (strncmp(pathname, winfo->basepath,
191 winfo->basepath_len) == 0) {
192 if (!(dinfo->valid & ACPI_VALID_ADR))
194 if (ata_msg_probe(winfo->ataport))
195 ata_dev_printk(winfo->atadev, KERN_DEBUG,
196 "GOT ONE: (%s) root_port = 0x%llx,"
197 " port_num = 0x%llx\n", pathname,
198 SATA_ROOT_PORT(dinfo->address),
199 SATA_PORT_NUMBER(dinfo->address));
201 if (SATA_PORT_NUMBER(dinfo->address) != NO_PORT_MULT)
202 if (ata_msg_probe(winfo->ataport))
203 ata_dev_printk(winfo->atadev,
204 KERN_DEBUG, "warning: don't"
205 " know how to handle SATA port"
207 if (SATA_ROOT_PORT(dinfo->address) ==
208 winfo->ataport->port_no &&
209 SATA_PORT_NUMBER(dinfo->address) == NO_PORT_MULT) {
210 if (ata_msg_probe(winfo->ataport))
211 ata_dev_printk(winfo->atadev,
213 "THIS ^^^^^ is the requested"
214 " SATA drive (handle = 0x%p)\n",
216 winfo->sata_adr = dinfo->address;
217 winfo->obj_handle = handle;
229 /* Get the SATA drive _ADR object. */
230 static int get_sata_adr(struct device *dev, acpi_handle handle,
231 acpi_integer pcidevfn, unsigned int drive,
233 struct ata_device *atadev, u32 *dev_adr)
236 struct walk_info *winfo;
239 winfo = kzalloc(sizeof(struct walk_info), GFP_KERNEL);
244 winfo->atadev = atadev;
246 if (acpi_bus_get_device(handle, &winfo->adev) < 0)
247 if (ata_msg_probe(ap))
248 ata_dev_printk(winfo->atadev, KERN_DEBUG,
249 "acpi_bus_get_device failed\n");
250 winfo->handle = handle;
251 winfo->pcidevfn = pcidevfn;
252 winfo->drivenum = drive;
254 status = acpi_get_devices(NULL, get_devices, winfo, NULL);
255 if (ACPI_FAILURE(status)) {
256 if (ata_msg_probe(ap))
257 ata_dev_printk(winfo->atadev, KERN_DEBUG,
258 "%s: acpi_get_devices failed\n",
262 *dev_adr = winfo->sata_adr;
263 atadev->obj_handle = winfo->obj_handle;
272 * do_drive_get_GTF - get the drive bootup default taskfile settings
273 * @ap: the ata_port for the drive
274 * @ix: target ata_device (drive) index
275 * @gtf_length: number of bytes of _GTF data returned at @gtf_address
276 * @gtf_address: buffer containing _GTF taskfile arrays
278 * This applies to both PATA and SATA drives.
280 * The _GTF method has no input parameters.
281 * It returns a variable number of register set values (registers
282 * hex 1F1..1F7, taskfiles).
283 * The <variable number> is not known in advance, so have ACPI-CA
284 * allocate the buffer as needed and return it, then free it later.
286 * The returned @gtf_length and @gtf_address are only valid if the
287 * function return value is 0.
289 static int do_drive_get_GTF(struct ata_port *ap, int ix,
290 unsigned int *gtf_length, unsigned long *gtf_address,
291 unsigned long *obj_loc)
294 acpi_handle dev_handle = NULL;
295 acpi_handle chan_handle, drive_handle;
296 acpi_integer pcidevfn = 0;
298 struct acpi_buffer output;
299 union acpi_object *out_obj;
300 struct device *dev = ap->host->dev;
301 struct ata_device *atadev = &ap->device[ix];
311 if (ata_msg_probe(ap))
312 ata_dev_printk(atadev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
313 __FUNCTION__, ap->port_no);
315 if (!ata_dev_enabled(atadev) || (ap->flags & ATA_FLAG_DISABLED)) {
316 if (ata_msg_probe(ap))
317 ata_dev_printk(atadev, KERN_DEBUG, "%s: ERR: "
318 "ata_dev_present: %d, PORT_DISABLED: %lu\n",
319 __FUNCTION__, ata_dev_enabled(atadev),
320 ap->flags & ATA_FLAG_DISABLED);
324 /* Don't continue if device has no _ADR method.
325 * _GTF is intended for known motherboard devices. */
326 if (!(ap->cbl == ATA_CBL_SATA)) {
327 err = pata_get_dev_handle(dev, &dev_handle, &pcidevfn);
329 if (ata_msg_probe(ap))
330 ata_dev_printk(atadev, KERN_DEBUG,
331 "%s: pata_get_dev_handle failed (%d)\n",
336 err = sata_get_dev_handle(dev, &dev_handle, &pcidevfn);
338 if (ata_msg_probe(ap))
339 ata_dev_printk(atadev, KERN_DEBUG,
340 "%s: sata_get_dev_handle failed (%d\n",
346 /* Get this drive's _ADR info. if not already known. */
347 if (!atadev->obj_handle) {
348 if (!(ap->cbl == ATA_CBL_SATA)) {
349 /* get child objects of dev_handle == channel objects,
350 * + _their_ children == drive objects */
351 /* channel is ap->port_no */
352 chan_handle = acpi_get_child(dev_handle,
354 if (ata_msg_probe(ap))
355 ata_dev_printk(atadev, KERN_DEBUG,
356 "%s: chan adr=%d: chan_handle=0x%p\n",
357 __FUNCTION__, ap->port_no,
363 /* TBD: could also check ACPI object VALID bits */
364 drive_handle = acpi_get_child(chan_handle, ix);
370 atadev->obj_handle = drive_handle;
371 } else { /* for SATA mode */
372 dev_adr = SATA_ADR_RSVD;
373 err = get_sata_adr(dev, dev_handle, pcidevfn, 0,
374 ap, atadev, &dev_adr);
376 if (err < 0 || dev_adr == SATA_ADR_RSVD ||
377 !atadev->obj_handle) {
378 if (ata_msg_probe(ap))
379 ata_dev_printk(atadev, KERN_DEBUG,
380 "%s: get_sata/pata_adr failed: "
381 "err=%d, dev_adr=%u, obj_handle=0x%p\n",
382 __FUNCTION__, err, dev_adr,
388 /* Setting up output buffer */
389 output.length = ACPI_ALLOCATE_BUFFER;
390 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
392 /* _GTF has no input parameters */
394 status = acpi_evaluate_object(atadev->obj_handle, "_GTF",
396 if (ACPI_FAILURE(status)) {
397 if (ata_msg_probe(ap))
398 ata_dev_printk(atadev, KERN_DEBUG,
399 "%s: Run _GTF error: status = 0x%x\n",
400 __FUNCTION__, status);
404 if (!output.length || !output.pointer) {
405 if (ata_msg_probe(ap))
406 ata_dev_printk(atadev, KERN_DEBUG, "%s: Run _GTF: "
407 "length or ptr is NULL (0x%llx, 0x%p)\n",
409 (unsigned long long)output.length,
411 kfree(output.pointer);
415 out_obj = output.pointer;
416 if (out_obj->type != ACPI_TYPE_BUFFER) {
417 kfree(output.pointer);
418 if (ata_msg_probe(ap))
419 ata_dev_printk(atadev, KERN_DEBUG, "%s: Run _GTF: "
420 "error: expected object type of "
421 " ACPI_TYPE_BUFFER, got 0x%x\n",
422 __FUNCTION__, out_obj->type);
427 if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
428 out_obj->buffer.length % REGS_PER_GTF) {
430 ata_dev_printk(atadev, KERN_ERR,
431 "%s: unexpected GTF length (%d) or addr (0x%p)\n",
432 __FUNCTION__, out_obj->buffer.length,
433 out_obj->buffer.pointer);
438 *gtf_length = out_obj->buffer.length;
439 *gtf_address = (unsigned long)out_obj->buffer.pointer;
440 *obj_loc = (unsigned long)out_obj;
441 if (ata_msg_probe(ap))
442 ata_dev_printk(atadev, KERN_DEBUG, "%s: returning "
443 "gtf_length=%d, gtf_address=0x%lx, obj_loc=0x%lx\n",
444 __FUNCTION__, *gtf_length, *gtf_address, *obj_loc);
451 * taskfile_load_raw - send taskfile registers to host controller
452 * @ap: Port to which output is sent
453 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
455 * Outputs ATA taskfile to standard ATA host controller using MMIO
456 * or PIO as indicated by the ATA_FLAG_MMIO flag.
457 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
458 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
459 * hob_lbal, hob_lbam, and hob_lbah.
461 * This function waits for idle (!BUSY and !DRQ) after writing
462 * registers. If the control register has a new value, this
463 * function also waits for idle after writing control and before
464 * writing the remaining registers.
467 * Inherited from caller.
469 static void taskfile_load_raw(struct ata_port *ap,
470 struct ata_device *atadev,
471 const struct taskfile_array *gtf)
473 struct ata_taskfile tf;
476 if (ata_msg_probe(ap))
477 ata_dev_printk(atadev, KERN_DEBUG, "%s: (0x1f1-1f7): hex: "
478 "%02x %02x %02x %02x %02x %02x %02x\n",
480 gtf->tfa[0], gtf->tfa[1], gtf->tfa[2],
481 gtf->tfa[3], gtf->tfa[4], gtf->tfa[5], gtf->tfa[6]);
483 if ((gtf->tfa[0] == 0) && (gtf->tfa[1] == 0) && (gtf->tfa[2] == 0)
484 && (gtf->tfa[3] == 0) && (gtf->tfa[4] == 0) && (gtf->tfa[5] == 0)
485 && (gtf->tfa[6] == 0))
488 ata_tf_init(atadev, &tf);
490 /* convert gtf to tf */
491 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */
492 tf.protocol = atadev->class == ATA_DEV_ATAPI ?
493 ATA_PROT_ATAPI_NODATA : ATA_PROT_NODATA;
494 tf.feature = gtf->tfa[0]; /* 0x1f1 */
495 tf.nsect = gtf->tfa[1]; /* 0x1f2 */
496 tf.lbal = gtf->tfa[2]; /* 0x1f3 */
497 tf.lbam = gtf->tfa[3]; /* 0x1f4 */
498 tf.lbah = gtf->tfa[4]; /* 0x1f5 */
499 tf.device = gtf->tfa[5]; /* 0x1f6 */
500 tf.command = gtf->tfa[6]; /* 0x1f7 */
502 err = ata_exec_internal(atadev, &tf, NULL, DMA_NONE, NULL, 0);
503 if (err && ata_msg_probe(ap))
504 ata_dev_printk(atadev, KERN_ERR,
505 "%s: ata_exec_internal failed: %u\n",
510 * do_drive_set_taskfiles - write the drive taskfile settings from _GTF
511 * @ap: the ata_port for the drive
512 * @atadev: target ata_device
513 * @gtf_length: total number of bytes of _GTF taskfiles
514 * @gtf_address: location of _GTF taskfile arrays
516 * This applies to both PATA and SATA drives.
518 * Write {gtf_address, length gtf_length} in groups of
519 * REGS_PER_GTF bytes.
521 static int do_drive_set_taskfiles(struct ata_port *ap,
522 struct ata_device *atadev, unsigned int gtf_length,
523 unsigned long gtf_address)
526 int gtf_count = gtf_length / REGS_PER_GTF;
528 struct taskfile_array *gtf;
530 if (ata_msg_probe(ap))
531 ata_dev_printk(atadev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
532 __FUNCTION__, ap->port_no);
534 if (noacpi || !(ap->cbl == ATA_CBL_SATA))
537 if (!ata_dev_enabled(atadev) || (ap->flags & ATA_FLAG_DISABLED))
539 if (!gtf_count) /* shouldn't be here */
542 if (gtf_length % REGS_PER_GTF) {
544 ata_dev_printk(atadev, KERN_ERR,
545 "%s: unexpected GTF length (%d)\n",
546 __FUNCTION__, gtf_length);
550 for (ix = 0; ix < gtf_count; ix++) {
551 gtf = (struct taskfile_array *)
552 (gtf_address + ix * REGS_PER_GTF);
554 /* send all TaskFile registers (0x1f1-0x1f7) *in*that*order* */
555 taskfile_load_raw(ap, atadev, gtf);
564 * ata_acpi_exec_tfs - get then write drive taskfile settings
565 * @ap: the ata_port for the drive
567 * This applies to both PATA and SATA drives.
569 int ata_acpi_exec_tfs(struct ata_port *ap)
573 unsigned int gtf_length;
574 unsigned long gtf_address;
575 unsigned long obj_loc;
580 * TBD - implement PATA support. For now,
581 * we should not run GTF on PATA devices since some
582 * PATA require execution of GTM/STM before GTF.
584 if (!(ap->cbl == ATA_CBL_SATA))
587 for (ix = 0; ix < ATA_MAX_DEVICES; ix++) {
588 if (!ata_dev_enabled(&ap->device[ix]))
591 ret = do_drive_get_GTF(ap, ix,
592 >f_length, >f_address, &obj_loc);
594 if (ata_msg_probe(ap))
595 ata_port_printk(ap, KERN_DEBUG,
596 "%s: get_GTF error (%d)\n",
601 ret = do_drive_set_taskfiles(ap, &ap->device[ix],
602 gtf_length, gtf_address);
603 kfree((void *)obj_loc);
605 if (ata_msg_probe(ap))
606 ata_port_printk(ap, KERN_DEBUG,
607 "%s: set_taskfiles error (%d)\n",
617 * ata_acpi_push_id - send Identify data to drive
618 * @ap: the ata_port for the drive
621 * _SDD ACPI object: for SATA mode only
622 * Must be after Identify (Packet) Device -- uses its data
623 * ATM this function never returns a failure. It is an optional
624 * method and if it fails for whatever reason, we should still
627 int ata_acpi_push_id(struct ata_port *ap, unsigned int ix)
630 acpi_integer pcidevfn;
632 struct device *dev = ap->host->dev;
633 struct ata_device *atadev = &ap->device[ix];
636 struct acpi_object_list input;
637 union acpi_object in_params[1];
642 if (ata_msg_probe(ap))
643 ata_dev_printk(atadev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
644 __FUNCTION__, ix, ap->port_no);
646 /* Don't continue if not a SATA device. */
647 if (!(ap->cbl == ATA_CBL_SATA)) {
648 if (ata_msg_probe(ap))
649 ata_dev_printk(atadev, KERN_DEBUG,
650 "%s: Not a SATA device\n", __FUNCTION__);
654 /* Don't continue if device has no _ADR method.
655 * _SDD is intended for known motherboard devices. */
656 err = sata_get_dev_handle(dev, &handle, &pcidevfn);
658 if (ata_msg_probe(ap))
659 ata_dev_printk(atadev, KERN_DEBUG,
660 "%s: sata_get_dev_handle failed (%d\n",
665 /* Get this drive's _ADR info, if not already known */
666 if (!atadev->obj_handle) {
667 dev_adr = SATA_ADR_RSVD;
668 err = get_sata_adr(dev, handle, pcidevfn, ix, ap, atadev,
670 if (err < 0 || dev_adr == SATA_ADR_RSVD ||
671 !atadev->obj_handle) {
672 if (ata_msg_probe(ap))
673 ata_dev_printk(atadev, KERN_DEBUG,
674 "%s: get_sata_adr failed: "
675 "err=%d, dev_adr=%u, obj_handle=0x%p\n",
676 __FUNCTION__, err, dev_adr,
682 /* Give the drive Identify data to the drive via the _SDD method */
683 /* _SDD: set up input parameters */
685 input.pointer = in_params;
686 in_params[0].type = ACPI_TYPE_BUFFER;
687 in_params[0].buffer.length = sizeof(atadev->id[0]) * ATA_ID_WORDS;
688 in_params[0].buffer.pointer = (u8 *)atadev->id;
689 /* Output buffer: _SDD has no output */
691 /* It's OK for _SDD to be missing too. */
692 swap_buf_le16(atadev->id, ATA_ID_WORDS);
693 status = acpi_evaluate_object(atadev->obj_handle, "_SDD", &input, NULL);
694 swap_buf_le16(atadev->id, ATA_ID_WORDS);
696 err = ACPI_FAILURE(status) ? -EIO : 0;
698 if (ata_msg_probe(ap))
699 ata_dev_printk(atadev, KERN_DEBUG,
700 "%s _SDD error: status = 0x%x\n",
701 __FUNCTION__, status);
704 /* always return success */