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/ata.h>
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>
17#include "libata.h"
18
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>
26
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
31
32#define REGS_PER_GTF		7
33struct taskfile_array {
34	u8	tfa[REGS_PER_GTF];	/* regs. 0x1f1 - 0x1f7 */
35};
36
37/*
38 *	Helper - belongs in the PCI layer somewhere eventually
39 */
40static int is_pci_dev(struct device *dev)
41{
42	return (dev->bus == &pci_bus_type);
43}
44
45/**
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
50 *
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.
54 *
55 * Returns 0 on success, <0 on error.
56 */
57static int sata_get_dev_handle(struct device *dev, acpi_handle *handle,
58					acpi_integer *pcidevfn)
59{
60	struct pci_dev	*pci_dev;
61	acpi_integer	addr;
62
63	if (!is_pci_dev(dev))
64		return -ENODEV;
65
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);
69	*pcidevfn = addr;
70	*handle = acpi_get_child(DEVICE_ACPI_HANDLE(dev->parent), addr);
71	if (!*handle)
72		return -ENODEV;
73	return 0;
74}
75
76/**
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
81 *
82 * The PATA and SATA versions of this function are different.
83 *
84 * Returns 0 on success, <0 on error.
85 */
86static int pata_get_dev_handle(struct device *dev, acpi_handle *handle,
87				acpi_integer *pcidevfn)
88{
89	unsigned int bus, devnum, func;
90	acpi_integer addr;
91	acpi_handle dev_handle, parent_handle;
92	struct acpi_buffer buffer = {.length = ACPI_ALLOCATE_BUFFER,
93					.pointer = NULL};
94	acpi_status status;
95	struct acpi_device_info	*dinfo = NULL;
96	int ret = -ENODEV;
97	struct pci_dev *pdev;
98
99	if (!is_pci_dev(dev))
100		return -ENODEV;
101
102	pdev = to_pci_dev(dev);
103
104	bus = pdev->bus->number;
105	devnum = PCI_SLOT(pdev->devfn);
106	func = PCI_FUNC(pdev->devfn);
107
108	dev_handle = DEVICE_ACPI_HANDLE(dev);
109	parent_handle = DEVICE_ACPI_HANDLE(dev->parent);
110
111	status = acpi_get_object_info(parent_handle, &buffer);
112	if (ACPI_FAILURE(status))
113		goto err;
114
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);
120		*pcidevfn = addr;
121		*handle = dev_handle;
122	} else {
123		goto err;
124	}
125
126	if (!*handle)
127		goto err;
128	ret = 0;
129err:
130	kfree(dinfo);
131	return ret;
132}
133
134struct walk_info {		/* can be trimmed some */
135	struct device	*dev;
136	struct acpi_device *adev;
137	acpi_handle	handle;
138	acpi_integer	pcidevfn;
139	unsigned int	drivenum;
140	acpi_handle	obj_handle;
141	struct ata_port *ataport;
142	struct ata_device *atadev;
143	u32		sata_adr;
144	int		status;
145	char		basepath[ACPI_PATHNAME_MAX];
146	int		basepath_len;
147};
148
149static acpi_status get_devices(acpi_handle handle,
150				u32 level, void *context, void **return_value)
151{
152	acpi_status		status;
153	struct walk_info	*winfo = context;
154	struct acpi_buffer	namebuf = {ACPI_ALLOCATE_BUFFER, NULL};
155	char			*pathname;
156	struct acpi_buffer	buffer;
157	struct acpi_device_info	*dinfo;
158
159	status = acpi_get_name(handle, ACPI_FULL_PATHNAME, &namebuf);
160	if (status)
161		goto ret;
162	pathname = namebuf.pointer;
163
164	buffer.length = ACPI_ALLOCATE_BUFFER;
165	buffer.pointer = NULL;
166	status = acpi_get_object_info(handle, &buffer);
167	if (ACPI_FAILURE(status))
168		goto out2;
169
170	dinfo = buffer.pointer;
171
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);
182		goto out;
183	}
184
185	/* if basepath is not yet known, ignore this object */
186	if (!winfo->basepath_len)
187		goto out;
188
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))
193			goto out;
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));
200		/* heuristics: */
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"
206					" multiplier\n");
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,
212					KERN_DEBUG,
213					"THIS ^^^^^ is the requested"
214					" SATA drive (handle = 0x%p)\n",
215					handle);
216			winfo->sata_adr = dinfo->address;
217			winfo->obj_handle = handle;
218		}
219	}
220out:
221	kfree(dinfo);
222out2:
223	kfree(pathname);
224
225ret:
226	return status;
227}
228
229/* Get the SATA drive _ADR object. */
230static int get_sata_adr(struct device *dev, acpi_handle handle,
231			acpi_integer pcidevfn, unsigned int drive,
232			struct ata_port *ap,
233			struct ata_device *atadev, u32 *dev_adr)
234{
235	acpi_status	status;
236	struct walk_info *winfo;
237	int		err = -ENOMEM;
238
239	winfo = kzalloc(sizeof(struct walk_info), GFP_KERNEL);
240	if (!winfo)
241		goto out;
242
243	winfo->dev = dev;
244	winfo->atadev = atadev;
245	winfo->ataport = ap;
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;
253
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",
259				__FUNCTION__);
260		err = -ENODEV;
261	} else {
262		*dev_adr = winfo->sata_adr;
263		atadev->obj_handle = winfo->obj_handle;
264		err = 0;
265	}
266	kfree(winfo);
267out:
268	return err;
269}
270
271/**
272 * do_drive_get_GTF - get the drive bootup default taskfile settings
273 * @dev: target ATA device
274 * @gtf_length: number of bytes of _GTF data returned at @gtf_address
275 * @gtf_address: buffer containing _GTF taskfile arrays
276 *
277 * This applies to both PATA and SATA drives.
278 *
279 * The _GTF method has no input parameters.
280 * It returns a variable number of register set values (registers
281 * hex 1F1..1F7, taskfiles).
282 * The <variable number> is not known in advance, so have ACPI-CA
283 * allocate the buffer as needed and return it, then free it later.
284 *
285 * The returned @gtf_length and @gtf_address are only valid if the
286 * function return value is 0.
287 */
288static int do_drive_get_GTF(struct ata_device *dev, unsigned int *gtf_length,
289			    unsigned long *gtf_address, unsigned long *obj_loc)
290{
291	struct ata_port *ap = dev->ap;
292	acpi_status status;
293	acpi_handle dev_handle = NULL;
294	acpi_handle chan_handle, drive_handle;
295	acpi_integer pcidevfn = 0;
296	u32 dev_adr;
297	struct acpi_buffer output;
298	union acpi_object *out_obj;
299	struct device *gdev = ap->host->dev;
300	int err = -ENODEV;
301
302	*gtf_length = 0;
303	*gtf_address = 0UL;
304	*obj_loc = 0UL;
305
306	if (libata_noacpi)
307		return 0;
308
309	if (ata_msg_probe(ap))
310		ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
311			       __FUNCTION__, ap->port_no);
312
313	if (!ata_dev_enabled(dev) || (ap->flags & ATA_FLAG_DISABLED)) {
314		if (ata_msg_probe(ap))
315			ata_dev_printk(dev, KERN_DEBUG, "%s: ERR: "
316				"ata_dev_present: %d, PORT_DISABLED: %lu\n",
317				__FUNCTION__, ata_dev_enabled(dev),
318				ap->flags & ATA_FLAG_DISABLED);
319		goto out;
320	}
321
322	/* Don't continue if device has no _ADR method.
323	 * _GTF is intended for known motherboard devices. */
324	if (!(ap->flags & ATA_FLAG_ACPI_SATA)) {
325		err = pata_get_dev_handle(gdev, &dev_handle, &pcidevfn);
326		if (err < 0) {
327			if (ata_msg_probe(ap))
328				ata_dev_printk(dev, KERN_DEBUG,
329					"%s: pata_get_dev_handle failed (%d)\n",
330					__FUNCTION__, err);
331			goto out;
332		}
333	} else {
334		err = sata_get_dev_handle(gdev, &dev_handle, &pcidevfn);
335		if (err < 0) {
336			if (ata_msg_probe(ap))
337				ata_dev_printk(dev, KERN_DEBUG,
338					"%s: sata_get_dev_handle failed (%d\n",
339					__FUNCTION__, err);
340			goto out;
341		}
342	}
343
344	/* Get this drive's _ADR info. if not already known. */
345	if (!dev->obj_handle) {
346		if (!(ap->flags & ATA_FLAG_ACPI_SATA)) {
347			/* get child objects of dev_handle == channel objects,
348	 		 * + _their_ children == drive objects */
349			/* channel is ap->port_no */
350			chan_handle = acpi_get_child(dev_handle,
351						ap->port_no);
352			if (ata_msg_probe(ap))
353				ata_dev_printk(dev, KERN_DEBUG,
354					"%s: chan adr=%d: chan_handle=0x%p\n",
355					__FUNCTION__, ap->port_no,
356					chan_handle);
357			if (!chan_handle) {
358				err = -ENODEV;
359				goto out;
360			}
361			/* TBD: could also check ACPI object VALID bits */
362			drive_handle = acpi_get_child(chan_handle, dev->devno);
363			if (!drive_handle) {
364				err = -ENODEV;
365				goto out;
366			}
367			dev_adr = dev->devno;
368			dev->obj_handle = drive_handle;
369		} else {	/* for SATA mode */
370			dev_adr = SATA_ADR_RSVD;
371			err = get_sata_adr(gdev, dev_handle, pcidevfn, 0,
372					ap, dev, &dev_adr);
373		}
374		if (err < 0 || dev_adr == SATA_ADR_RSVD ||
375		    !dev->obj_handle) {
376			if (ata_msg_probe(ap))
377				ata_dev_printk(dev, KERN_DEBUG,
378					"%s: get_sata/pata_adr failed: "
379					"err=%d, dev_adr=%u, obj_handle=0x%p\n",
380					__FUNCTION__, err, dev_adr,
381					dev->obj_handle);
382			goto out;
383		}
384	}
385
386	/* Setting up output buffer */
387	output.length = ACPI_ALLOCATE_BUFFER;
388	output.pointer = NULL;	/* ACPI-CA sets this; save/free it later */
389
390	/* _GTF has no input parameters */
391	err = -EIO;
392	status = acpi_evaluate_object(dev->obj_handle, "_GTF",
393					NULL, &output);
394	if (ACPI_FAILURE(status)) {
395		if (ata_msg_probe(ap))
396			ata_dev_printk(dev, KERN_DEBUG,
397				"%s: Run _GTF error: status = 0x%x\n",
398				__FUNCTION__, status);
399		goto out;
400	}
401
402	if (!output.length || !output.pointer) {
403		if (ata_msg_probe(ap))
404			ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: "
405				"length or ptr is NULL (0x%llx, 0x%p)\n",
406				__FUNCTION__,
407				(unsigned long long)output.length,
408				output.pointer);
409		kfree(output.pointer);
410		goto out;
411	}
412
413	out_obj = output.pointer;
414	if (out_obj->type != ACPI_TYPE_BUFFER) {
415		kfree(output.pointer);
416		if (ata_msg_probe(ap))
417			ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: "
418				"error: expected object type of "
419				" ACPI_TYPE_BUFFER, got 0x%x\n",
420				__FUNCTION__, out_obj->type);
421		err = -ENOENT;
422		goto out;
423	}
424
425	if (!out_obj->buffer.length || !out_obj->buffer.pointer ||
426	    out_obj->buffer.length % REGS_PER_GTF) {
427		if (ata_msg_drv(ap))
428			ata_dev_printk(dev, KERN_ERR,
429				"%s: unexpected GTF length (%d) or addr (0x%p)\n",
430				__FUNCTION__, out_obj->buffer.length,
431				out_obj->buffer.pointer);
432		err = -ENOENT;
433		goto out;
434	}
435
436	*gtf_length = out_obj->buffer.length;
437	*gtf_address = (unsigned long)out_obj->buffer.pointer;
438	*obj_loc = (unsigned long)out_obj;
439	if (ata_msg_probe(ap))
440		ata_dev_printk(dev, KERN_DEBUG, "%s: returning "
441			"gtf_length=%d, gtf_address=0x%lx, obj_loc=0x%lx\n",
442			__FUNCTION__, *gtf_length, *gtf_address, *obj_loc);
443	err = 0;
444out:
445	return err;
446}
447
448/**
449 * taskfile_load_raw - send taskfile registers to host controller
450 * @dev: target ATA device
451 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
452 *
453 * Outputs ATA taskfile to standard ATA host controller using MMIO
454 * or PIO as indicated by the ATA_FLAG_MMIO flag.
455 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
456 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
457 * hob_lbal, hob_lbam, and hob_lbah.
458 *
459 * This function waits for idle (!BUSY and !DRQ) after writing
460 * registers.  If the control register has a new value, this
461 * function also waits for idle after writing control and before
462 * writing the remaining registers.
463 *
464 * LOCKING: TBD:
465 * Inherited from caller.
466 */
467static void taskfile_load_raw(struct ata_device *dev,
468			      const struct taskfile_array *gtf)
469{
470	struct ata_port *ap = dev->ap;
471	struct ata_taskfile tf;
472	unsigned int err;
473
474	if (ata_msg_probe(ap))
475		ata_dev_printk(dev, KERN_DEBUG, "%s: (0x1f1-1f7): hex: "
476			"%02x %02x %02x %02x %02x %02x %02x\n",
477			__FUNCTION__,
478			gtf->tfa[0], gtf->tfa[1], gtf->tfa[2],
479			gtf->tfa[3], gtf->tfa[4], gtf->tfa[5], gtf->tfa[6]);
480
481	if ((gtf->tfa[0] == 0) && (gtf->tfa[1] == 0) && (gtf->tfa[2] == 0)
482	    && (gtf->tfa[3] == 0) && (gtf->tfa[4] == 0) && (gtf->tfa[5] == 0)
483	    && (gtf->tfa[6] == 0))
484		return;
485
486	ata_tf_init(dev, &tf);
487
488	/* convert gtf to tf */
489	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; /* TBD */
490	tf.protocol = ATA_PROT_NODATA;
491	tf.feature = gtf->tfa[0];	/* 0x1f1 */
492	tf.nsect   = gtf->tfa[1];	/* 0x1f2 */
493	tf.lbal    = gtf->tfa[2];	/* 0x1f3 */
494	tf.lbam    = gtf->tfa[3];	/* 0x1f4 */
495	tf.lbah    = gtf->tfa[4];	/* 0x1f5 */
496	tf.device  = gtf->tfa[5];	/* 0x1f6 */
497	tf.command = gtf->tfa[6];	/* 0x1f7 */
498
499	err = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0);
500	if (err && ata_msg_probe(ap))
501		ata_dev_printk(dev, KERN_ERR,
502			"%s: ata_exec_internal failed: %u\n",
503			__FUNCTION__, err);
504}
505
506/**
507 * do_drive_set_taskfiles - write the drive taskfile settings from _GTF
508 * @dev: target ATA device
509 * @gtf_length: total number of bytes of _GTF taskfiles
510 * @gtf_address: location of _GTF taskfile arrays
511 *
512 * This applies to both PATA and SATA drives.
513 *
514 * Write {gtf_address, length gtf_length} in groups of
515 * REGS_PER_GTF bytes.
516 */
517static int do_drive_set_taskfiles(struct ata_device *dev,
518				  unsigned int gtf_length,
519				  unsigned long gtf_address)
520{
521	struct ata_port *ap = dev->ap;
522	int err = -ENODEV;
523	int gtf_count = gtf_length / REGS_PER_GTF;
524	int ix;
525	struct taskfile_array	*gtf;
526
527	if (ata_msg_probe(ap))
528		ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
529			       __FUNCTION__, ap->port_no);
530
531	if (libata_noacpi || !(ap->flags & ATA_FLAG_ACPI_SATA))
532		return 0;
533
534	if (!ata_dev_enabled(dev) || (ap->flags & ATA_FLAG_DISABLED))
535		goto out;
536	if (!gtf_count)		/* shouldn't be here */
537		goto out;
538
539	if (gtf_length % REGS_PER_GTF) {
540		if (ata_msg_drv(ap))
541			ata_dev_printk(dev, KERN_ERR,
542				"%s: unexpected GTF length (%d)\n",
543				__FUNCTION__, gtf_length);
544		goto out;
545	}
546
547	for (ix = 0; ix < gtf_count; ix++) {
548		gtf = (struct taskfile_array *)
549			(gtf_address + ix * REGS_PER_GTF);
550
551		/* send all TaskFile registers (0x1f1-0x1f7) *in*that*order* */
552		taskfile_load_raw(dev, gtf);
553	}
554
555	err = 0;
556out:
557	return err;
558}
559
560/**
561 * ata_acpi_exec_tfs - get then write drive taskfile settings
562 * @ap: the ata_port for the drive
563 *
564 * This applies to both PATA and SATA drives.
565 */
566int ata_acpi_exec_tfs(struct ata_port *ap)
567{
568	int ix;
569	int ret = 0;
570	unsigned int gtf_length;
571	unsigned long gtf_address;
572	unsigned long obj_loc;
573
574	if (libata_noacpi)
575		return 0;
576	/*
577	 * TBD - implement PATA support.  For now,
578	 * we should not run GTF on PATA devices since some
579	 * PATA require execution of GTM/STM before GTF.
580	 */
581	if (!(ap->flags & ATA_FLAG_ACPI_SATA))
582		return 0;
583
584	for (ix = 0; ix < ATA_MAX_DEVICES; ix++) {
585		struct ata_device *dev = &ap->device[ix];
586
587		if (!ata_dev_enabled(dev))
588			continue;
589
590		ret = do_drive_get_GTF(dev, &gtf_length, &gtf_address,
591				       &obj_loc);
592		if (ret < 0) {
593			if (ata_msg_probe(ap))
594				ata_port_printk(ap, KERN_DEBUG,
595					"%s: get_GTF error (%d)\n",
596					__FUNCTION__, ret);
597			break;
598		}
599
600		ret = do_drive_set_taskfiles(dev, gtf_length, gtf_address);
601		kfree((void *)obj_loc);
602		if (ret < 0) {
603			if (ata_msg_probe(ap))
604				ata_port_printk(ap, KERN_DEBUG,
605					"%s: set_taskfiles error (%d)\n",
606					__FUNCTION__, ret);
607			break;
608		}
609	}
610
611	return ret;
612}
613
614/**
615 * ata_acpi_push_id - send Identify data to drive
616 * @dev: target ATA device
617 *
618 * _SDD ACPI object: for SATA mode only
619 * Must be after Identify (Packet) Device -- uses its data
620 * ATM this function never returns a failure.  It is an optional
621 * method and if it fails for whatever reason, we should still
622 * just keep going.
623 */
624int ata_acpi_push_id(struct ata_device *dev)
625{
626	struct ata_port *ap = dev->ap;
627	acpi_handle handle;
628	acpi_integer pcidevfn;
629	int err;
630	struct device *gdev = ap->host->dev;
631	u32 dev_adr;
632	acpi_status status;
633	struct acpi_object_list input;
634	union acpi_object in_params[1];
635
636	if (libata_noacpi)
637		return 0;
638
639	if (ata_msg_probe(ap))
640		ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
641			       __FUNCTION__, dev->devno, ap->port_no);
642
643	/* Don't continue if not a SATA device. */
644	if (!(ap->flags & ATA_FLAG_ACPI_SATA)) {
645		if (ata_msg_probe(ap))
646			ata_dev_printk(dev, KERN_DEBUG,
647				"%s: Not a SATA device\n", __FUNCTION__);
648		goto out;
649	}
650
651	/* Don't continue if device has no _ADR method.
652	 * _SDD is intended for known motherboard devices. */
653	err = sata_get_dev_handle(gdev, &handle, &pcidevfn);
654	if (err < 0) {
655		if (ata_msg_probe(ap))
656			ata_dev_printk(dev, KERN_DEBUG,
657				"%s: sata_get_dev_handle failed (%d\n",
658				__FUNCTION__, err);
659		goto out;
660	}
661
662	/* Get this drive's _ADR info, if not already known */
663	if (!dev->obj_handle) {
664		dev_adr = SATA_ADR_RSVD;
665		err = get_sata_adr(gdev, handle, pcidevfn, dev->devno, ap, dev,
666					&dev_adr);
667		if (err < 0 || dev_adr == SATA_ADR_RSVD ||
668			!dev->obj_handle) {
669			if (ata_msg_probe(ap))
670				ata_dev_printk(dev, KERN_DEBUG,
671					"%s: get_sata_adr failed: "
672					"err=%d, dev_adr=%u, obj_handle=0x%p\n",
673					__FUNCTION__, err, dev_adr,
674					dev->obj_handle);
675			goto out;
676		}
677	}
678
679	/* Give the drive Identify data to the drive via the _SDD method */
680	/* _SDD: set up input parameters */
681	input.count = 1;
682	input.pointer = in_params;
683	in_params[0].type = ACPI_TYPE_BUFFER;
684	in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
685	in_params[0].buffer.pointer = (u8 *)dev->id;
686	/* Output buffer: _SDD has no output */
687
688	/* It's OK for _SDD to be missing too. */
689	swap_buf_le16(dev->id, ATA_ID_WORDS);
690	status = acpi_evaluate_object(dev->obj_handle, "_SDD", &input, NULL);
691	swap_buf_le16(dev->id, ATA_ID_WORDS);
692
693	err = ACPI_FAILURE(status) ? -EIO : 0;
694	if (err < 0) {
695		if (ata_msg_probe(ap))
696			ata_dev_printk(dev, KERN_DEBUG,
697				       "%s _SDD error: status = 0x%x\n",
698				       __FUNCTION__, status);
699	}
700
701	/* always return success */
702out:
703	return 0;
704}
705