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 */
39 * sata_get_dev_handle - finds acpi_handle and PCI device.function
40 * @dev: device to locate
41 * @handle: returned acpi_handle for @dev
42 * @pcidevfn: return PCI device.func for @dev
44 * This function is somewhat SATA-specific. Or at least the
45 * PATA & SATA versions of this function are different,
46 * so it's not entirely generic code.
48 * Returns 0 on success, <0 on error.
50 static int sata_get_dev_handle(struct device *dev, acpi_handle *handle,
51 acpi_integer *pcidevfn)
53 struct pci_dev *pci_dev;
56 pci_dev = to_pci_dev(dev); /* NOTE: PCI-specific */
57 /* Please refer to the ACPI spec for the syntax of _ADR. */
58 addr = (PCI_SLOT(pci_dev->devfn) << 16) | PCI_FUNC(pci_dev->devfn);
60 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(dev->parent), addr);
67 * pata_get_dev_handle - finds acpi_handle and PCI device.function
68 * @dev: device to locate
69 * @handle: returned acpi_handle for @dev
70 * @pcidevfn: return PCI device.func for @dev
72 * The PATA and SATA versions of this function are different.
74 * Returns 0 on success, <0 on error.
76 static int pata_get_dev_handle(struct device *dev, acpi_handle *handle,
77 acpi_integer *pcidevfn)
79 unsigned int bus, devnum, func;
81 acpi_handle dev_handle, parent_handle;
82 struct acpi_buffer buffer = {.length = ACPI_ALLOCATE_BUFFER,
85 struct acpi_device_info *dinfo = NULL;
87 struct pci_dev *pdev = to_pci_dev(dev);
89 bus = pdev->bus->number;
90 devnum = PCI_SLOT(pdev->devfn);
91 func = PCI_FUNC(pdev->devfn);
93 dev_handle = DEVICE_ACPI_HANDLE(dev);
94 parent_handle = DEVICE_ACPI_HANDLE(dev->parent);
96 status = acpi_get_object_info(parent_handle, &buffer);
97 if (ACPI_FAILURE(status))
100 dinfo = buffer.pointer;
101 if (dinfo && (dinfo->valid & ACPI_VALID_ADR) &&
102 dinfo->address == bus) {
103 /* ACPI spec for _ADR for PCI bus: */
104 addr = (acpi_integer)(devnum << 16 | func);
106 *handle = dev_handle;
119 struct walk_info { /* can be trimmed some */
121 struct acpi_device *adev;
123 acpi_integer pcidevfn;
124 unsigned int drivenum;
125 acpi_handle obj_handle;
126 struct ata_port *ataport;
127 struct ata_device *atadev;
130 char basepath[ACPI_PATHNAME_MAX];
134 static acpi_status get_devices(acpi_handle handle,
135 u32 level, void *context, void **return_value)
138 struct walk_info *winfo = context;
139 struct acpi_buffer namebuf = {ACPI_ALLOCATE_BUFFER, NULL};
141 struct acpi_buffer buffer;
142 struct acpi_device_info *dinfo;
144 status = acpi_get_name(handle, ACPI_FULL_PATHNAME, &namebuf);
147 pathname = namebuf.pointer;
149 buffer.length = ACPI_ALLOCATE_BUFFER;
150 buffer.pointer = NULL;
151 status = acpi_get_object_info(handle, &buffer);
152 if (ACPI_FAILURE(status))
155 dinfo = buffer.pointer;
157 /* find full device path name for pcidevfn */
158 if (dinfo && (dinfo->valid & ACPI_VALID_ADR) &&
159 dinfo->address == winfo->pcidevfn) {
160 if (ata_msg_probe(winfo->ataport))
161 ata_dev_printk(winfo->atadev, KERN_DEBUG,
162 ":%s: matches pcidevfn (0x%llx)\n",
163 pathname, winfo->pcidevfn);
164 strlcpy(winfo->basepath, pathname,
165 sizeof(winfo->basepath));
166 winfo->basepath_len = strlen(pathname);
170 /* if basepath is not yet known, ignore this object */
171 if (!winfo->basepath_len)
174 /* if this object is in scope of basepath, maybe use it */
175 if (strncmp(pathname, winfo->basepath,
176 winfo->basepath_len) == 0) {
177 if (!(dinfo->valid & ACPI_VALID_ADR))
179 if (ata_msg_probe(winfo->ataport))
180 ata_dev_printk(winfo->atadev, KERN_DEBUG,
181 "GOT ONE: (%s) root_port = 0x%llx,"
182 " port_num = 0x%llx\n", pathname,
183 SATA_ROOT_PORT(dinfo->address),
184 SATA_PORT_NUMBER(dinfo->address));
186 if (SATA_PORT_NUMBER(dinfo->address) != NO_PORT_MULT)
187 if (ata_msg_probe(winfo->ataport))
188 ata_dev_printk(winfo->atadev,
189 KERN_DEBUG, "warning: don't"
190 " know how to handle SATA port"
192 if (SATA_ROOT_PORT(dinfo->address) ==
193 winfo->ataport->port_no &&
194 SATA_PORT_NUMBER(dinfo->address) == NO_PORT_MULT) {
195 if (ata_msg_probe(winfo->ataport))
196 ata_dev_printk(winfo->atadev,
198 "THIS ^^^^^ is the requested"
199 " SATA drive (handle = 0x%p)\n",
201 winfo->sata_adr = dinfo->address;
202 winfo->obj_handle = handle;
214 /* Get the SATA drive _ADR object. */
215 static int get_sata_adr(struct device *dev, acpi_handle handle,
216 acpi_integer pcidevfn, unsigned int drive,
218 struct ata_device *atadev, u32 *dev_adr)
221 struct walk_info *winfo;
224 winfo = kzalloc(sizeof(struct walk_info), GFP_KERNEL);
229 winfo->atadev = atadev;
231 if (acpi_bus_get_device(handle, &winfo->adev) < 0)
232 if (ata_msg_probe(ap))
233 ata_dev_printk(winfo->atadev, KERN_DEBUG,
234 "acpi_bus_get_device failed\n");
235 winfo->handle = handle;
236 winfo->pcidevfn = pcidevfn;
237 winfo->drivenum = drive;
239 status = acpi_get_devices(NULL, get_devices, winfo, NULL);
240 if (ACPI_FAILURE(status)) {
241 if (ata_msg_probe(ap))
242 ata_dev_printk(winfo->atadev, KERN_DEBUG,
243 "%s: acpi_get_devices failed\n",
247 *dev_adr = winfo->sata_adr;
248 atadev->obj_handle = winfo->obj_handle;
257 * do_drive_get_GTF - get the drive bootup default taskfile settings
258 * @ap: the ata_port for the drive
259 * @ix: target ata_device (drive) index
260 * @gtf_length: number of bytes of _GTF data returned at @gtf_address
261 * @gtf_address: buffer containing _GTF taskfile arrays
263 * This applies to both PATA and SATA drives.
265 * The _GTF method has no input parameters.
266 * It returns a variable number of register set values (registers
267 * hex 1F1..1F7, taskfiles).
268 * The <variable number> is not known in advance, so have ACPI-CA
269 * allocate the buffer as needed and return it, then free it later.
271 * The returned @gtf_length and @gtf_address are only valid if the
272 * function return value is 0.
274 static int do_drive_get_GTF(struct ata_port *ap, int ix,
275 unsigned int *gtf_length, unsigned long *gtf_address,
276 unsigned long *obj_loc)
279 acpi_handle dev_handle = NULL;
280 acpi_handle chan_handle, drive_handle;
281 acpi_integer pcidevfn = 0;
283 struct acpi_buffer output;
284 union acpi_object *out_obj;
285 struct device *dev = ap->host->dev;
286 struct ata_device *atadev = &ap->device[ix];
296 if (ata_msg_probe(ap))
297 ata_dev_printk(atadev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
298 __FUNCTION__, ap->port_no);
300 if (!ata_dev_enabled(atadev) || (ap->flags & ATA_FLAG_DISABLED)) {
301 if (ata_msg_probe(ap))
302 ata_dev_printk(atadev, KERN_DEBUG, "%s: ERR: "
303 "ata_dev_present: %d, PORT_DISABLED: %lu\n",
304 __FUNCTION__, ata_dev_enabled(atadev),
305 ap->flags & ATA_FLAG_DISABLED);
309 /* Don't continue if device has no _ADR method.
310 * _GTF is intended for known motherboard devices. */
311 if (!(ap->cbl == ATA_CBL_SATA)) {
312 err = pata_get_dev_handle(dev, &dev_handle, &pcidevfn);
314 if (ata_msg_probe(ap))
315 ata_dev_printk(atadev, KERN_DEBUG,
316 "%s: pata_get_dev_handle failed (%d)\n",
321 err = sata_get_dev_handle(dev, &dev_handle, &pcidevfn);
323 if (ata_msg_probe(ap))
324 ata_dev_printk(atadev, KERN_DEBUG,
325 "%s: sata_get_dev_handle failed (%d\n",
331 /* Get this drive's _ADR info. if not already known. */
332 if (!atadev->obj_handle) {
333 if (!(ap->cbl == ATA_CBL_SATA)) {
334 /* get child objects of dev_handle == channel objects,
335 * + _their_ children == drive objects */
336 /* channel is ap->port_no */
337 chan_handle = acpi_get_child(dev_handle,
339 if (ata_msg_probe(ap))
340 ata_dev_printk(atadev, KERN_DEBUG,
341 "%s: chan adr=%d: chan_handle=0x%p\n",
342 __FUNCTION__, ap->port_no,
348 /* TBD: could also check ACPI object VALID bits */
349 drive_handle = acpi_get_child(chan_handle, ix);
355 atadev->obj_handle = drive_handle;
356 } else { /* for SATA mode */
357 dev_adr = SATA_ADR_RSVD;
358 err = get_sata_adr(dev, dev_handle, pcidevfn, 0,
359 ap, atadev, &dev_adr);
361 if (err < 0 || dev_adr == SATA_ADR_RSVD ||
362 !atadev->obj_handle) {
363 if (ata_msg_probe(ap))
364 ata_dev_printk(atadev, KERN_DEBUG,
365 "%s: get_sata/pata_adr failed: "
366 "err=%d, dev_adr=%u, obj_handle=0x%p\n",
367 __FUNCTION__, err, dev_adr,
373 /* Setting up output buffer */
374 output.length = ACPI_ALLOCATE_BUFFER;
375 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
377 /* _GTF has no input parameters */
379 status = acpi_evaluate_object(atadev->obj_handle, "_GTF",
381 if (ACPI_FAILURE(status)) {
382 if (ata_msg_probe(ap))
383 ata_dev_printk(atadev, KERN_DEBUG,
384 "%s: Run _GTF error: status = 0x%x\n",
385 __FUNCTION__, status);
389 if (!output.length || !output.pointer) {
390 if (ata_msg_probe(ap))
391 ata_dev_printk(atadev, KERN_DEBUG, "%s: Run _GTF: "
392 "length or ptr is NULL (0x%llx, 0x%p)\n",
394 (unsigned long long)output.length,
396 kfree(output.pointer);
400 out_obj = output.pointer;
401 if (out_obj->type != ACPI_TYPE_BUFFER) {
402 kfree(output.pointer);
403 if (ata_msg_probe(ap))
404 ata_dev_printk(atadev, KERN_DEBUG, "%s: Run _GTF: "
405 "error: expected object type of "
406 " ACPI_TYPE_BUFFER, got 0x%x\n",
407 __FUNCTION__, out_obj->type);
412 if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
413 out_obj->buffer.length % REGS_PER_GTF) {
415 ata_dev_printk(atadev, KERN_ERR,
416 "%s: unexpected GTF length (%d) or addr (0x%p)\n",
417 __FUNCTION__, out_obj->buffer.length,
418 out_obj->buffer.pointer);
423 *gtf_length = out_obj->buffer.length;
424 *gtf_address = (unsigned long)out_obj->buffer.pointer;
425 *obj_loc = (unsigned long)out_obj;
426 if (ata_msg_probe(ap))
427 ata_dev_printk(atadev, KERN_DEBUG, "%s: returning "
428 "gtf_length=%d, gtf_address=0x%lx, obj_loc=0x%lx\n",
429 __FUNCTION__, *gtf_length, *gtf_address, *obj_loc);
436 * taskfile_load_raw - send taskfile registers to host controller
437 * @ap: Port to which output is sent
438 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
440 * Outputs ATA taskfile to standard ATA host controller using MMIO
441 * or PIO as indicated by the ATA_FLAG_MMIO flag.
442 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
443 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
444 * hob_lbal, hob_lbam, and hob_lbah.
446 * This function waits for idle (!BUSY and !DRQ) after writing
447 * registers. If the control register has a new value, this
448 * function also waits for idle after writing control and before
449 * writing the remaining registers.
452 * Inherited from caller.
454 static void taskfile_load_raw(struct ata_port *ap,
455 struct ata_device *atadev,
456 const struct taskfile_array *gtf)
458 struct ata_taskfile tf;
461 if (ata_msg_probe(ap))
462 ata_dev_printk(atadev, KERN_DEBUG, "%s: (0x1f1-1f7): hex: "
463 "%02x %02x %02x %02x %02x %02x %02x\n",
465 gtf->tfa[0], gtf->tfa[1], gtf->tfa[2],
466 gtf->tfa[3], gtf->tfa[4], gtf->tfa[5], gtf->tfa[6]);
468 if ((gtf->tfa[0] == 0) && (gtf->tfa[1] == 0) && (gtf->tfa[2] == 0)
469 && (gtf->tfa[3] == 0) && (gtf->tfa[4] == 0) && (gtf->tfa[5] == 0)
470 && (gtf->tfa[6] == 0))
473 ata_tf_init(atadev, &tf);
475 /* convert gtf to tf */
476 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */
477 tf.protocol = atadev->class == ATA_DEV_ATAPI ?
478 ATA_PROT_ATAPI_NODATA : ATA_PROT_NODATA;
479 tf.feature = gtf->tfa[0]; /* 0x1f1 */
480 tf.nsect = gtf->tfa[1]; /* 0x1f2 */
481 tf.lbal = gtf->tfa[2]; /* 0x1f3 */
482 tf.lbam = gtf->tfa[3]; /* 0x1f4 */
483 tf.lbah = gtf->tfa[4]; /* 0x1f5 */
484 tf.device = gtf->tfa[5]; /* 0x1f6 */
485 tf.command = gtf->tfa[6]; /* 0x1f7 */
487 err = ata_exec_internal(atadev, &tf, NULL, DMA_NONE, NULL, 0);
488 if (err && ata_msg_probe(ap))
489 ata_dev_printk(atadev, KERN_ERR,
490 "%s: ata_exec_internal failed: %u\n",
495 * do_drive_set_taskfiles - write the drive taskfile settings from _GTF
496 * @ap: the ata_port for the drive
497 * @atadev: target ata_device
498 * @gtf_length: total number of bytes of _GTF taskfiles
499 * @gtf_address: location of _GTF taskfile arrays
501 * This applies to both PATA and SATA drives.
503 * Write {gtf_address, length gtf_length} in groups of
504 * REGS_PER_GTF bytes.
506 static int do_drive_set_taskfiles(struct ata_port *ap,
507 struct ata_device *atadev, unsigned int gtf_length,
508 unsigned long gtf_address)
511 int gtf_count = gtf_length / REGS_PER_GTF;
513 struct taskfile_array *gtf;
515 if (ata_msg_probe(ap))
516 ata_dev_printk(atadev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
517 __FUNCTION__, ap->port_no);
519 if (noacpi || !(ap->cbl == ATA_CBL_SATA))
522 if (!ata_dev_enabled(atadev) || (ap->flags & ATA_FLAG_DISABLED))
524 if (!gtf_count) /* shouldn't be here */
527 if (gtf_length % REGS_PER_GTF) {
529 ata_dev_printk(atadev, KERN_ERR,
530 "%s: unexpected GTF length (%d)\n",
531 __FUNCTION__, gtf_length);
535 for (ix = 0; ix < gtf_count; ix++) {
536 gtf = (struct taskfile_array *)
537 (gtf_address + ix * REGS_PER_GTF);
539 /* send all TaskFile registers (0x1f1-0x1f7) *in*that*order* */
540 taskfile_load_raw(ap, atadev, gtf);
549 * ata_acpi_exec_tfs - get then write drive taskfile settings
550 * @ap: the ata_port for the drive
552 * This applies to both PATA and SATA drives.
554 int ata_acpi_exec_tfs(struct ata_port *ap)
558 unsigned int gtf_length;
559 unsigned long gtf_address;
560 unsigned long obj_loc;
565 for (ix = 0; ix < ATA_MAX_DEVICES; ix++) {
566 if (!ata_dev_enabled(&ap->device[ix]))
569 ret = do_drive_get_GTF(ap, ix,
570 >f_length, >f_address, &obj_loc);
572 if (ata_msg_probe(ap))
573 ata_port_printk(ap, KERN_DEBUG,
574 "%s: get_GTF error (%d)\n",
579 ret = do_drive_set_taskfiles(ap, &ap->device[ix],
580 gtf_length, gtf_address);
581 kfree((void *)obj_loc);
583 if (ata_msg_probe(ap))
584 ata_port_printk(ap, KERN_DEBUG,
585 "%s: set_taskfiles error (%d)\n",
595 * ata_acpi_push_id - send Identify data to drive
596 * @ap: the ata_port for the drive
599 * _SDD ACPI object: for SATA mode only
600 * Must be after Identify (Packet) Device -- uses its data
601 * ATM this function never returns a failure. It is an optional
602 * method and if it fails for whatever reason, we should still
605 int ata_acpi_push_id(struct ata_port *ap, unsigned int ix)
608 acpi_integer pcidevfn;
610 struct device *dev = ap->host->dev;
611 struct ata_device *atadev = &ap->device[ix];
614 struct acpi_object_list input;
615 union acpi_object in_params[1];
620 if (ata_msg_probe(ap))
621 ata_dev_printk(atadev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
622 __FUNCTION__, ix, ap->port_no);
624 /* Don't continue if not a SATA device. */
625 if (!(ap->cbl == ATA_CBL_SATA)) {
626 if (ata_msg_probe(ap))
627 ata_dev_printk(atadev, KERN_DEBUG,
628 "%s: Not a SATA device\n", __FUNCTION__);
632 /* Don't continue if device has no _ADR method.
633 * _SDD is intended for known motherboard devices. */
634 err = sata_get_dev_handle(dev, &handle, &pcidevfn);
636 if (ata_msg_probe(ap))
637 ata_dev_printk(atadev, KERN_DEBUG,
638 "%s: sata_get_dev_handle failed (%d\n",
643 /* Get this drive's _ADR info, if not already known */
644 if (!atadev->obj_handle) {
645 dev_adr = SATA_ADR_RSVD;
646 err = get_sata_adr(dev, handle, pcidevfn, ix, ap, atadev,
648 if (err < 0 || dev_adr == SATA_ADR_RSVD ||
649 !atadev->obj_handle) {
650 if (ata_msg_probe(ap))
651 ata_dev_printk(atadev, KERN_DEBUG,
652 "%s: get_sata_adr failed: "
653 "err=%d, dev_adr=%u, obj_handle=0x%p\n",
654 __FUNCTION__, err, dev_adr,
660 /* Give the drive Identify data to the drive via the _SDD method */
661 /* _SDD: set up input parameters */
663 input.pointer = in_params;
664 in_params[0].type = ACPI_TYPE_BUFFER;
665 in_params[0].buffer.length = sizeof(atadev->id[0]) * ATA_ID_WORDS;
666 in_params[0].buffer.pointer = (u8 *)atadev->id;
667 /* Output buffer: _SDD has no output */
669 /* It's OK for _SDD to be missing too. */
670 swap_buf_le16(atadev->id, ATA_ID_WORDS);
671 status = acpi_evaluate_object(atadev->obj_handle, "_SDD", &input, NULL);
672 swap_buf_le16(atadev->id, ATA_ID_WORDS);
674 err = ACPI_FAILURE(status) ? -EIO : 0;
676 if (ata_msg_probe(ap))
677 ata_dev_printk(atadev, KERN_DEBUG,
678 "%s _SDD error: status = 0x%x\n",
679 __FUNCTION__, status);
682 /* always return success */