Commit 834009170986f295c5eca37c76c59f1b28670d69

Authored by Aaron Lu
Committed by Jeff Garzik
1 parent 389cd78496

[libata] acpi: call ata_acpi_gtm during ata port init time

Commit 30dcf76acc695cbd2fa919e294670fe9552e16e7 mistakenly dropped
the code to get an initial gtm for the IDE channel. This caused the
following problem for Sergei:
http://marc.info/?l=linux-kernel&m=134484963618457&w=2

Fix this by adding the call back in ata_acpi_bind_host, and due to
this, the ata_ap_acpi_handle is modified accordingly.

Tested-by: Sergei Trofimovich <slyich@gmail.com>
Signed-off-by: Aaron Lu <aaron.lu@intel.com>
Signed-off-by: Jeff Garzik <jgarzik@redhat.com>

Showing 1 changed file with 4 additions and 11 deletions Inline Diff

drivers/ata/libata-acpi.c
1 /* 1 /*
2 * libata-acpi.c 2 * libata-acpi.c
3 * Provides ACPI support for PATA/SATA. 3 * Provides ACPI support for PATA/SATA.
4 * 4 *
5 * Copyright (C) 2006 Intel Corp. 5 * Copyright (C) 2006 Intel Corp.
6 * Copyright (C) 2006 Randy Dunlap 6 * Copyright (C) 2006 Randy Dunlap
7 */ 7 */
8 8
9 #include <linux/module.h> 9 #include <linux/module.h>
10 #include <linux/ata.h> 10 #include <linux/ata.h>
11 #include <linux/delay.h> 11 #include <linux/delay.h>
12 #include <linux/device.h> 12 #include <linux/device.h>
13 #include <linux/errno.h> 13 #include <linux/errno.h>
14 #include <linux/kernel.h> 14 #include <linux/kernel.h>
15 #include <linux/acpi.h> 15 #include <linux/acpi.h>
16 #include <linux/libata.h> 16 #include <linux/libata.h>
17 #include <linux/pci.h> 17 #include <linux/pci.h>
18 #include <linux/slab.h> 18 #include <linux/slab.h>
19 #include <linux/pm_runtime.h> 19 #include <linux/pm_runtime.h>
20 #include <scsi/scsi_device.h> 20 #include <scsi/scsi_device.h>
21 #include "libata.h" 21 #include "libata.h"
22 22
23 #include <acpi/acpi_bus.h> 23 #include <acpi/acpi_bus.h>
24 24
25 unsigned int ata_acpi_gtf_filter = ATA_ACPI_FILTER_DEFAULT; 25 unsigned int ata_acpi_gtf_filter = ATA_ACPI_FILTER_DEFAULT;
26 module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644); 26 module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644);
27 MODULE_PARM_DESC(acpi_gtf_filter, "filter mask for ACPI _GTF commands, set to filter out (0x1=set xfermode, 0x2=lock/freeze lock, 0x4=DIPM, 0x8=FPDMA non-zero offset, 0x10=FPDMA DMA Setup FIS auto-activate)"); 27 MODULE_PARM_DESC(acpi_gtf_filter, "filter mask for ACPI _GTF commands, set to filter out (0x1=set xfermode, 0x2=lock/freeze lock, 0x4=DIPM, 0x8=FPDMA non-zero offset, 0x10=FPDMA DMA Setup FIS auto-activate)");
28 28
29 #define NO_PORT_MULT 0xffff 29 #define NO_PORT_MULT 0xffff
30 #define SATA_ADR(root, pmp) (((root) << 16) | (pmp)) 30 #define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
31 31
32 #define REGS_PER_GTF 7 32 #define REGS_PER_GTF 7
33 struct ata_acpi_gtf { 33 struct ata_acpi_gtf {
34 u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */ 34 u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
35 } __packed; 35 } __packed;
36 36
37 /* 37 /*
38 * Helper - belongs in the PCI layer somewhere eventually 38 * Helper - belongs in the PCI layer somewhere eventually
39 */ 39 */
40 static int is_pci_dev(struct device *dev) 40 static int is_pci_dev(struct device *dev)
41 { 41 {
42 return (dev->bus == &pci_bus_type); 42 return (dev->bus == &pci_bus_type);
43 } 43 }
44 44
45 static void ata_acpi_clear_gtf(struct ata_device *dev) 45 static void ata_acpi_clear_gtf(struct ata_device *dev)
46 { 46 {
47 kfree(dev->gtf_cache); 47 kfree(dev->gtf_cache);
48 dev->gtf_cache = NULL; 48 dev->gtf_cache = NULL;
49 } 49 }
50 50
51 /** 51 /**
52 * ata_ap_acpi_handle - provide the acpi_handle for an ata_port 52 * ata_ap_acpi_handle - provide the acpi_handle for an ata_port
53 * @ap: the acpi_handle returned will correspond to this port 53 * @ap: the acpi_handle returned will correspond to this port
54 * 54 *
55 * Returns the acpi_handle for the ACPI namespace object corresponding to 55 * Returns the acpi_handle for the ACPI namespace object corresponding to
56 * the ata_port passed into the function, or NULL if no such object exists 56 * the ata_port passed into the function, or NULL if no such object exists
57 */ 57 */
58 acpi_handle ata_ap_acpi_handle(struct ata_port *ap) 58 acpi_handle ata_ap_acpi_handle(struct ata_port *ap)
59 { 59 {
60 if (ap->flags & ATA_FLAG_ACPI_SATA) 60 if (ap->flags & ATA_FLAG_ACPI_SATA)
61 return NULL; 61 return NULL;
62 62
63 /* 63 return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), ap->port_no);
64 * If acpi bind operation has already happened, we can get the handle
65 * for the port by checking the corresponding scsi_host device's
66 * firmware node, otherwise we will need to find out the handle from
67 * its parent's acpi node.
68 */
69 if (ap->scsi_host)
70 return DEVICE_ACPI_HANDLE(&ap->scsi_host->shost_gendev);
71 else
72 return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev),
73 ap->port_no);
74 } 64 }
75 EXPORT_SYMBOL(ata_ap_acpi_handle); 65 EXPORT_SYMBOL(ata_ap_acpi_handle);
76 66
77 /** 67 /**
78 * ata_dev_acpi_handle - provide the acpi_handle for an ata_device 68 * ata_dev_acpi_handle - provide the acpi_handle for an ata_device
79 * @dev: the acpi_device returned will correspond to this port 69 * @dev: the acpi_device returned will correspond to this port
80 * 70 *
81 * Returns the acpi_handle for the ACPI namespace object corresponding to 71 * Returns the acpi_handle for the ACPI namespace object corresponding to
82 * the ata_device passed into the function, or NULL if no such object exists 72 * the ata_device passed into the function, or NULL if no such object exists
83 */ 73 */
84 acpi_handle ata_dev_acpi_handle(struct ata_device *dev) 74 acpi_handle ata_dev_acpi_handle(struct ata_device *dev)
85 { 75 {
86 acpi_integer adr; 76 acpi_integer adr;
87 struct ata_port *ap = dev->link->ap; 77 struct ata_port *ap = dev->link->ap;
88 78
89 if (ap->flags & ATA_FLAG_ACPI_SATA) { 79 if (ap->flags & ATA_FLAG_ACPI_SATA) {
90 if (!sata_pmp_attached(ap)) 80 if (!sata_pmp_attached(ap))
91 adr = SATA_ADR(ap->port_no, NO_PORT_MULT); 81 adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
92 else 82 else
93 adr = SATA_ADR(ap->port_no, dev->link->pmp); 83 adr = SATA_ADR(ap->port_no, dev->link->pmp);
94 return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), adr); 84 return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), adr);
95 } else 85 } else
96 return acpi_get_child(ata_ap_acpi_handle(ap), dev->devno); 86 return acpi_get_child(ata_ap_acpi_handle(ap), dev->devno);
97 } 87 }
98 EXPORT_SYMBOL(ata_dev_acpi_handle); 88 EXPORT_SYMBOL(ata_dev_acpi_handle);
99 89
100 /* @ap and @dev are the same as ata_acpi_handle_hotplug() */ 90 /* @ap and @dev are the same as ata_acpi_handle_hotplug() */
101 static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev) 91 static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
102 { 92 {
103 if (dev) 93 if (dev)
104 dev->flags |= ATA_DFLAG_DETACH; 94 dev->flags |= ATA_DFLAG_DETACH;
105 else { 95 else {
106 struct ata_link *tlink; 96 struct ata_link *tlink;
107 struct ata_device *tdev; 97 struct ata_device *tdev;
108 98
109 ata_for_each_link(tlink, ap, EDGE) 99 ata_for_each_link(tlink, ap, EDGE)
110 ata_for_each_dev(tdev, tlink, ALL) 100 ata_for_each_dev(tdev, tlink, ALL)
111 tdev->flags |= ATA_DFLAG_DETACH; 101 tdev->flags |= ATA_DFLAG_DETACH;
112 } 102 }
113 103
114 ata_port_schedule_eh(ap); 104 ata_port_schedule_eh(ap);
115 } 105 }
116 106
117 /** 107 /**
118 * ata_acpi_handle_hotplug - ACPI event handler backend 108 * ata_acpi_handle_hotplug - ACPI event handler backend
119 * @ap: ATA port ACPI event occurred 109 * @ap: ATA port ACPI event occurred
120 * @dev: ATA device ACPI event occurred (can be NULL) 110 * @dev: ATA device ACPI event occurred (can be NULL)
121 * @event: ACPI event which occurred 111 * @event: ACPI event which occurred
122 * 112 *
123 * All ACPI bay / device realted events end up in this function. If 113 * All ACPI bay / device realted events end up in this function. If
124 * the event is port-wide @dev is NULL. If the event is specific to a 114 * the event is port-wide @dev is NULL. If the event is specific to a
125 * device, @dev points to it. 115 * device, @dev points to it.
126 * 116 *
127 * Hotplug (as opposed to unplug) notification is always handled as 117 * Hotplug (as opposed to unplug) notification is always handled as
128 * port-wide while unplug only kills the target device on device-wide 118 * port-wide while unplug only kills the target device on device-wide
129 * event. 119 * event.
130 * 120 *
131 * LOCKING: 121 * LOCKING:
132 * ACPI notify handler context. May sleep. 122 * ACPI notify handler context. May sleep.
133 */ 123 */
134 static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev, 124 static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
135 u32 event) 125 u32 event)
136 { 126 {
137 struct ata_eh_info *ehi = &ap->link.eh_info; 127 struct ata_eh_info *ehi = &ap->link.eh_info;
138 int wait = 0; 128 int wait = 0;
139 unsigned long flags; 129 unsigned long flags;
140 130
141 spin_lock_irqsave(ap->lock, flags); 131 spin_lock_irqsave(ap->lock, flags);
142 /* 132 /*
143 * When dock driver calls into the routine, it will always use 133 * When dock driver calls into the routine, it will always use
144 * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and 134 * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
145 * ACPI_NOTIFY_EJECT_REQUEST for remove 135 * ACPI_NOTIFY_EJECT_REQUEST for remove
146 */ 136 */
147 switch (event) { 137 switch (event) {
148 case ACPI_NOTIFY_BUS_CHECK: 138 case ACPI_NOTIFY_BUS_CHECK:
149 case ACPI_NOTIFY_DEVICE_CHECK: 139 case ACPI_NOTIFY_DEVICE_CHECK:
150 ata_ehi_push_desc(ehi, "ACPI event"); 140 ata_ehi_push_desc(ehi, "ACPI event");
151 141
152 ata_ehi_hotplugged(ehi); 142 ata_ehi_hotplugged(ehi);
153 ata_port_freeze(ap); 143 ata_port_freeze(ap);
154 break; 144 break;
155 case ACPI_NOTIFY_EJECT_REQUEST: 145 case ACPI_NOTIFY_EJECT_REQUEST:
156 ata_ehi_push_desc(ehi, "ACPI event"); 146 ata_ehi_push_desc(ehi, "ACPI event");
157 147
158 ata_acpi_detach_device(ap, dev); 148 ata_acpi_detach_device(ap, dev);
159 wait = 1; 149 wait = 1;
160 break; 150 break;
161 } 151 }
162 152
163 spin_unlock_irqrestore(ap->lock, flags); 153 spin_unlock_irqrestore(ap->lock, flags);
164 154
165 if (wait) 155 if (wait)
166 ata_port_wait_eh(ap); 156 ata_port_wait_eh(ap);
167 } 157 }
168 158
169 static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data) 159 static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data)
170 { 160 {
171 struct ata_device *dev = data; 161 struct ata_device *dev = data;
172 162
173 ata_acpi_handle_hotplug(dev->link->ap, dev, event); 163 ata_acpi_handle_hotplug(dev->link->ap, dev, event);
174 } 164 }
175 165
176 static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data) 166 static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data)
177 { 167 {
178 struct ata_port *ap = data; 168 struct ata_port *ap = data;
179 169
180 ata_acpi_handle_hotplug(ap, NULL, event); 170 ata_acpi_handle_hotplug(ap, NULL, event);
181 } 171 }
182 172
183 static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev, 173 static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
184 u32 event) 174 u32 event)
185 { 175 {
186 struct kobject *kobj = NULL; 176 struct kobject *kobj = NULL;
187 char event_string[20]; 177 char event_string[20];
188 char *envp[] = { event_string, NULL }; 178 char *envp[] = { event_string, NULL };
189 179
190 if (dev) { 180 if (dev) {
191 if (dev->sdev) 181 if (dev->sdev)
192 kobj = &dev->sdev->sdev_gendev.kobj; 182 kobj = &dev->sdev->sdev_gendev.kobj;
193 } else 183 } else
194 kobj = &ap->dev->kobj; 184 kobj = &ap->dev->kobj;
195 185
196 if (kobj) { 186 if (kobj) {
197 snprintf(event_string, 20, "BAY_EVENT=%d", event); 187 snprintf(event_string, 20, "BAY_EVENT=%d", event);
198 kobject_uevent_env(kobj, KOBJ_CHANGE, envp); 188 kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
199 } 189 }
200 } 190 }
201 191
202 static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data) 192 static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data)
203 { 193 {
204 ata_acpi_uevent(data, NULL, event); 194 ata_acpi_uevent(data, NULL, event);
205 } 195 }
206 196
207 static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data) 197 static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data)
208 { 198 {
209 struct ata_device *dev = data; 199 struct ata_device *dev = data;
210 ata_acpi_uevent(dev->link->ap, dev, event); 200 ata_acpi_uevent(dev->link->ap, dev, event);
211 } 201 }
212 202
213 static const struct acpi_dock_ops ata_acpi_dev_dock_ops = { 203 static const struct acpi_dock_ops ata_acpi_dev_dock_ops = {
214 .handler = ata_acpi_dev_notify_dock, 204 .handler = ata_acpi_dev_notify_dock,
215 .uevent = ata_acpi_dev_uevent, 205 .uevent = ata_acpi_dev_uevent,
216 }; 206 };
217 207
218 static const struct acpi_dock_ops ata_acpi_ap_dock_ops = { 208 static const struct acpi_dock_ops ata_acpi_ap_dock_ops = {
219 .handler = ata_acpi_ap_notify_dock, 209 .handler = ata_acpi_ap_notify_dock,
220 .uevent = ata_acpi_ap_uevent, 210 .uevent = ata_acpi_ap_uevent,
221 }; 211 };
222 212
223 /** 213 /**
224 * ata_acpi_dissociate - dissociate ATA host from ACPI objects 214 * ata_acpi_dissociate - dissociate ATA host from ACPI objects
225 * @host: target ATA host 215 * @host: target ATA host
226 * 216 *
227 * This function is called during driver detach after the whole host 217 * This function is called during driver detach after the whole host
228 * is shut down. 218 * is shut down.
229 * 219 *
230 * LOCKING: 220 * LOCKING:
231 * EH context. 221 * EH context.
232 */ 222 */
233 void ata_acpi_dissociate(struct ata_host *host) 223 void ata_acpi_dissociate(struct ata_host *host)
234 { 224 {
235 int i; 225 int i;
236 226
237 /* Restore initial _GTM values so that driver which attaches 227 /* Restore initial _GTM values so that driver which attaches
238 * afterward can use them too. 228 * afterward can use them too.
239 */ 229 */
240 for (i = 0; i < host->n_ports; i++) { 230 for (i = 0; i < host->n_ports; i++) {
241 struct ata_port *ap = host->ports[i]; 231 struct ata_port *ap = host->ports[i];
242 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap); 232 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
243 233
244 if (ata_ap_acpi_handle(ap) && gtm) 234 if (ata_ap_acpi_handle(ap) && gtm)
245 ata_acpi_stm(ap, gtm); 235 ata_acpi_stm(ap, gtm);
246 } 236 }
247 } 237 }
248 238
249 /** 239 /**
250 * ata_acpi_gtm - execute _GTM 240 * ata_acpi_gtm - execute _GTM
251 * @ap: target ATA port 241 * @ap: target ATA port
252 * @gtm: out parameter for _GTM result 242 * @gtm: out parameter for _GTM result
253 * 243 *
254 * Evaluate _GTM and store the result in @gtm. 244 * Evaluate _GTM and store the result in @gtm.
255 * 245 *
256 * LOCKING: 246 * LOCKING:
257 * EH context. 247 * EH context.
258 * 248 *
259 * RETURNS: 249 * RETURNS:
260 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure. 250 * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
261 */ 251 */
262 int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm) 252 int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
263 { 253 {
264 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER }; 254 struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
265 union acpi_object *out_obj; 255 union acpi_object *out_obj;
266 acpi_status status; 256 acpi_status status;
267 int rc = 0; 257 int rc = 0;
268 258
269 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_GTM", NULL, 259 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_GTM", NULL,
270 &output); 260 &output);
271 261
272 rc = -ENOENT; 262 rc = -ENOENT;
273 if (status == AE_NOT_FOUND) 263 if (status == AE_NOT_FOUND)
274 goto out_free; 264 goto out_free;
275 265
276 rc = -EINVAL; 266 rc = -EINVAL;
277 if (ACPI_FAILURE(status)) { 267 if (ACPI_FAILURE(status)) {
278 ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n", 268 ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
279 status); 269 status);
280 goto out_free; 270 goto out_free;
281 } 271 }
282 272
283 out_obj = output.pointer; 273 out_obj = output.pointer;
284 if (out_obj->type != ACPI_TYPE_BUFFER) { 274 if (out_obj->type != ACPI_TYPE_BUFFER) {
285 ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n", 275 ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
286 out_obj->type); 276 out_obj->type);
287 277
288 goto out_free; 278 goto out_free;
289 } 279 }
290 280
291 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) { 281 if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
292 ata_port_err(ap, "_GTM returned invalid length %d\n", 282 ata_port_err(ap, "_GTM returned invalid length %d\n",
293 out_obj->buffer.length); 283 out_obj->buffer.length);
294 goto out_free; 284 goto out_free;
295 } 285 }
296 286
297 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm)); 287 memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
298 rc = 0; 288 rc = 0;
299 out_free: 289 out_free:
300 kfree(output.pointer); 290 kfree(output.pointer);
301 return rc; 291 return rc;
302 } 292 }
303 293
304 EXPORT_SYMBOL_GPL(ata_acpi_gtm); 294 EXPORT_SYMBOL_GPL(ata_acpi_gtm);
305 295
306 /** 296 /**
307 * ata_acpi_stm - execute _STM 297 * ata_acpi_stm - execute _STM
308 * @ap: target ATA port 298 * @ap: target ATA port
309 * @stm: timing parameter to _STM 299 * @stm: timing parameter to _STM
310 * 300 *
311 * Evaluate _STM with timing parameter @stm. 301 * Evaluate _STM with timing parameter @stm.
312 * 302 *
313 * LOCKING: 303 * LOCKING:
314 * EH context. 304 * EH context.
315 * 305 *
316 * RETURNS: 306 * RETURNS:
317 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure. 307 * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
318 */ 308 */
319 int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm) 309 int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
320 { 310 {
321 acpi_status status; 311 acpi_status status;
322 struct ata_acpi_gtm stm_buf = *stm; 312 struct ata_acpi_gtm stm_buf = *stm;
323 struct acpi_object_list input; 313 struct acpi_object_list input;
324 union acpi_object in_params[3]; 314 union acpi_object in_params[3];
325 315
326 in_params[0].type = ACPI_TYPE_BUFFER; 316 in_params[0].type = ACPI_TYPE_BUFFER;
327 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm); 317 in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
328 in_params[0].buffer.pointer = (u8 *)&stm_buf; 318 in_params[0].buffer.pointer = (u8 *)&stm_buf;
329 /* Buffers for id may need byteswapping ? */ 319 /* Buffers for id may need byteswapping ? */
330 in_params[1].type = ACPI_TYPE_BUFFER; 320 in_params[1].type = ACPI_TYPE_BUFFER;
331 in_params[1].buffer.length = 512; 321 in_params[1].buffer.length = 512;
332 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id; 322 in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
333 in_params[2].type = ACPI_TYPE_BUFFER; 323 in_params[2].type = ACPI_TYPE_BUFFER;
334 in_params[2].buffer.length = 512; 324 in_params[2].buffer.length = 512;
335 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id; 325 in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
336 326
337 input.count = 3; 327 input.count = 3;
338 input.pointer = in_params; 328 input.pointer = in_params;
339 329
340 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_STM", &input, 330 status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_STM", &input,
341 NULL); 331 NULL);
342 332
343 if (status == AE_NOT_FOUND) 333 if (status == AE_NOT_FOUND)
344 return -ENOENT; 334 return -ENOENT;
345 if (ACPI_FAILURE(status)) { 335 if (ACPI_FAILURE(status)) {
346 ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n", 336 ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
347 status); 337 status);
348 return -EINVAL; 338 return -EINVAL;
349 } 339 }
350 return 0; 340 return 0;
351 } 341 }
352 342
353 EXPORT_SYMBOL_GPL(ata_acpi_stm); 343 EXPORT_SYMBOL_GPL(ata_acpi_stm);
354 344
355 /** 345 /**
356 * ata_dev_get_GTF - get the drive bootup default taskfile settings 346 * ata_dev_get_GTF - get the drive bootup default taskfile settings
357 * @dev: target ATA device 347 * @dev: target ATA device
358 * @gtf: output parameter for buffer containing _GTF taskfile arrays 348 * @gtf: output parameter for buffer containing _GTF taskfile arrays
359 * 349 *
360 * This applies to both PATA and SATA drives. 350 * This applies to both PATA and SATA drives.
361 * 351 *
362 * The _GTF method has no input parameters. 352 * The _GTF method has no input parameters.
363 * It returns a variable number of register set values (registers 353 * It returns a variable number of register set values (registers
364 * hex 1F1..1F7, taskfiles). 354 * hex 1F1..1F7, taskfiles).
365 * The <variable number> is not known in advance, so have ACPI-CA 355 * The <variable number> is not known in advance, so have ACPI-CA
366 * allocate the buffer as needed and return it, then free it later. 356 * allocate the buffer as needed and return it, then free it later.
367 * 357 *
368 * LOCKING: 358 * LOCKING:
369 * EH context. 359 * EH context.
370 * 360 *
371 * RETURNS: 361 * RETURNS:
372 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL 362 * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
373 * if _GTF is invalid. 363 * if _GTF is invalid.
374 */ 364 */
375 static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf) 365 static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
376 { 366 {
377 struct ata_port *ap = dev->link->ap; 367 struct ata_port *ap = dev->link->ap;
378 acpi_status status; 368 acpi_status status;
379 struct acpi_buffer output; 369 struct acpi_buffer output;
380 union acpi_object *out_obj; 370 union acpi_object *out_obj;
381 int rc = 0; 371 int rc = 0;
382 372
383 /* if _GTF is cached, use the cached value */ 373 /* if _GTF is cached, use the cached value */
384 if (dev->gtf_cache) { 374 if (dev->gtf_cache) {
385 out_obj = dev->gtf_cache; 375 out_obj = dev->gtf_cache;
386 goto done; 376 goto done;
387 } 377 }
388 378
389 /* set up output buffer */ 379 /* set up output buffer */
390 output.length = ACPI_ALLOCATE_BUFFER; 380 output.length = ACPI_ALLOCATE_BUFFER;
391 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */ 381 output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
392 382
393 if (ata_msg_probe(ap)) 383 if (ata_msg_probe(ap))
394 ata_dev_dbg(dev, "%s: ENTER: port#: %d\n", 384 ata_dev_dbg(dev, "%s: ENTER: port#: %d\n",
395 __func__, ap->port_no); 385 __func__, ap->port_no);
396 386
397 /* _GTF has no input parameters */ 387 /* _GTF has no input parameters */
398 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL, 388 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
399 &output); 389 &output);
400 out_obj = dev->gtf_cache = output.pointer; 390 out_obj = dev->gtf_cache = output.pointer;
401 391
402 if (ACPI_FAILURE(status)) { 392 if (ACPI_FAILURE(status)) {
403 if (status != AE_NOT_FOUND) { 393 if (status != AE_NOT_FOUND) {
404 ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n", 394 ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
405 status); 395 status);
406 rc = -EINVAL; 396 rc = -EINVAL;
407 } 397 }
408 goto out_free; 398 goto out_free;
409 } 399 }
410 400
411 if (!output.length || !output.pointer) { 401 if (!output.length || !output.pointer) {
412 if (ata_msg_probe(ap)) 402 if (ata_msg_probe(ap))
413 ata_dev_dbg(dev, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n", 403 ata_dev_dbg(dev, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
414 __func__, 404 __func__,
415 (unsigned long long)output.length, 405 (unsigned long long)output.length,
416 output.pointer); 406 output.pointer);
417 rc = -EINVAL; 407 rc = -EINVAL;
418 goto out_free; 408 goto out_free;
419 } 409 }
420 410
421 if (out_obj->type != ACPI_TYPE_BUFFER) { 411 if (out_obj->type != ACPI_TYPE_BUFFER) {
422 ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n", 412 ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
423 out_obj->type); 413 out_obj->type);
424 rc = -EINVAL; 414 rc = -EINVAL;
425 goto out_free; 415 goto out_free;
426 } 416 }
427 417
428 if (out_obj->buffer.length % REGS_PER_GTF) { 418 if (out_obj->buffer.length % REGS_PER_GTF) {
429 ata_dev_warn(dev, "unexpected _GTF length (%d)\n", 419 ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
430 out_obj->buffer.length); 420 out_obj->buffer.length);
431 rc = -EINVAL; 421 rc = -EINVAL;
432 goto out_free; 422 goto out_free;
433 } 423 }
434 424
435 done: 425 done:
436 rc = out_obj->buffer.length / REGS_PER_GTF; 426 rc = out_obj->buffer.length / REGS_PER_GTF;
437 if (gtf) { 427 if (gtf) {
438 *gtf = (void *)out_obj->buffer.pointer; 428 *gtf = (void *)out_obj->buffer.pointer;
439 if (ata_msg_probe(ap)) 429 if (ata_msg_probe(ap))
440 ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n", 430 ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n",
441 __func__, *gtf, rc); 431 __func__, *gtf, rc);
442 } 432 }
443 return rc; 433 return rc;
444 434
445 out_free: 435 out_free:
446 ata_acpi_clear_gtf(dev); 436 ata_acpi_clear_gtf(dev);
447 return rc; 437 return rc;
448 } 438 }
449 439
450 /** 440 /**
451 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter 441 * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
452 * @dev: target device 442 * @dev: target device
453 * @gtm: GTM parameter to use 443 * @gtm: GTM parameter to use
454 * 444 *
455 * Determine xfermask for @dev from @gtm. 445 * Determine xfermask for @dev from @gtm.
456 * 446 *
457 * LOCKING: 447 * LOCKING:
458 * None. 448 * None.
459 * 449 *
460 * RETURNS: 450 * RETURNS:
461 * Determined xfermask. 451 * Determined xfermask.
462 */ 452 */
463 unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev, 453 unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
464 const struct ata_acpi_gtm *gtm) 454 const struct ata_acpi_gtm *gtm)
465 { 455 {
466 unsigned long xfer_mask = 0; 456 unsigned long xfer_mask = 0;
467 unsigned int type; 457 unsigned int type;
468 int unit; 458 int unit;
469 u8 mode; 459 u8 mode;
470 460
471 /* we always use the 0 slot for crap hardware */ 461 /* we always use the 0 slot for crap hardware */
472 unit = dev->devno; 462 unit = dev->devno;
473 if (!(gtm->flags & 0x10)) 463 if (!(gtm->flags & 0x10))
474 unit = 0; 464 unit = 0;
475 465
476 /* PIO */ 466 /* PIO */
477 mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio); 467 mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
478 xfer_mask |= ata_xfer_mode2mask(mode); 468 xfer_mask |= ata_xfer_mode2mask(mode);
479 469
480 /* See if we have MWDMA or UDMA data. We don't bother with 470 /* See if we have MWDMA or UDMA data. We don't bother with
481 * MWDMA if UDMA is available as this means the BIOS set UDMA 471 * MWDMA if UDMA is available as this means the BIOS set UDMA
482 * and our error changedown if it works is UDMA to PIO anyway. 472 * and our error changedown if it works is UDMA to PIO anyway.
483 */ 473 */
484 if (!(gtm->flags & (1 << (2 * unit)))) 474 if (!(gtm->flags & (1 << (2 * unit))))
485 type = ATA_SHIFT_MWDMA; 475 type = ATA_SHIFT_MWDMA;
486 else 476 else
487 type = ATA_SHIFT_UDMA; 477 type = ATA_SHIFT_UDMA;
488 478
489 mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma); 479 mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
490 xfer_mask |= ata_xfer_mode2mask(mode); 480 xfer_mask |= ata_xfer_mode2mask(mode);
491 481
492 return xfer_mask; 482 return xfer_mask;
493 } 483 }
494 EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask); 484 EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
495 485
496 /** 486 /**
497 * ata_acpi_cbl_80wire - Check for 80 wire cable 487 * ata_acpi_cbl_80wire - Check for 80 wire cable
498 * @ap: Port to check 488 * @ap: Port to check
499 * @gtm: GTM data to use 489 * @gtm: GTM data to use
500 * 490 *
501 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode. 491 * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
502 */ 492 */
503 int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm) 493 int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
504 { 494 {
505 struct ata_device *dev; 495 struct ata_device *dev;
506 496
507 ata_for_each_dev(dev, &ap->link, ENABLED) { 497 ata_for_each_dev(dev, &ap->link, ENABLED) {
508 unsigned long xfer_mask, udma_mask; 498 unsigned long xfer_mask, udma_mask;
509 499
510 xfer_mask = ata_acpi_gtm_xfermask(dev, gtm); 500 xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
511 ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask); 501 ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
512 502
513 if (udma_mask & ~ATA_UDMA_MASK_40C) 503 if (udma_mask & ~ATA_UDMA_MASK_40C)
514 return 1; 504 return 1;
515 } 505 }
516 506
517 return 0; 507 return 0;
518 } 508 }
519 EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire); 509 EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
520 510
521 static void ata_acpi_gtf_to_tf(struct ata_device *dev, 511 static void ata_acpi_gtf_to_tf(struct ata_device *dev,
522 const struct ata_acpi_gtf *gtf, 512 const struct ata_acpi_gtf *gtf,
523 struct ata_taskfile *tf) 513 struct ata_taskfile *tf)
524 { 514 {
525 ata_tf_init(dev, tf); 515 ata_tf_init(dev, tf);
526 516
527 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 517 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
528 tf->protocol = ATA_PROT_NODATA; 518 tf->protocol = ATA_PROT_NODATA;
529 tf->feature = gtf->tf[0]; /* 0x1f1 */ 519 tf->feature = gtf->tf[0]; /* 0x1f1 */
530 tf->nsect = gtf->tf[1]; /* 0x1f2 */ 520 tf->nsect = gtf->tf[1]; /* 0x1f2 */
531 tf->lbal = gtf->tf[2]; /* 0x1f3 */ 521 tf->lbal = gtf->tf[2]; /* 0x1f3 */
532 tf->lbam = gtf->tf[3]; /* 0x1f4 */ 522 tf->lbam = gtf->tf[3]; /* 0x1f4 */
533 tf->lbah = gtf->tf[4]; /* 0x1f5 */ 523 tf->lbah = gtf->tf[4]; /* 0x1f5 */
534 tf->device = gtf->tf[5]; /* 0x1f6 */ 524 tf->device = gtf->tf[5]; /* 0x1f6 */
535 tf->command = gtf->tf[6]; /* 0x1f7 */ 525 tf->command = gtf->tf[6]; /* 0x1f7 */
536 } 526 }
537 527
538 static int ata_acpi_filter_tf(struct ata_device *dev, 528 static int ata_acpi_filter_tf(struct ata_device *dev,
539 const struct ata_taskfile *tf, 529 const struct ata_taskfile *tf,
540 const struct ata_taskfile *ptf) 530 const struct ata_taskfile *ptf)
541 { 531 {
542 if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) { 532 if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
543 /* libata doesn't use ACPI to configure transfer mode. 533 /* libata doesn't use ACPI to configure transfer mode.
544 * It will only confuse device configuration. Skip. 534 * It will only confuse device configuration. Skip.
545 */ 535 */
546 if (tf->command == ATA_CMD_SET_FEATURES && 536 if (tf->command == ATA_CMD_SET_FEATURES &&
547 tf->feature == SETFEATURES_XFER) 537 tf->feature == SETFEATURES_XFER)
548 return 1; 538 return 1;
549 } 539 }
550 540
551 if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) { 541 if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
552 /* BIOS writers, sorry but we don't wanna lock 542 /* BIOS writers, sorry but we don't wanna lock
553 * features unless the user explicitly said so. 543 * features unless the user explicitly said so.
554 */ 544 */
555 545
556 /* DEVICE CONFIGURATION FREEZE LOCK */ 546 /* DEVICE CONFIGURATION FREEZE LOCK */
557 if (tf->command == ATA_CMD_CONF_OVERLAY && 547 if (tf->command == ATA_CMD_CONF_OVERLAY &&
558 tf->feature == ATA_DCO_FREEZE_LOCK) 548 tf->feature == ATA_DCO_FREEZE_LOCK)
559 return 1; 549 return 1;
560 550
561 /* SECURITY FREEZE LOCK */ 551 /* SECURITY FREEZE LOCK */
562 if (tf->command == ATA_CMD_SEC_FREEZE_LOCK) 552 if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
563 return 1; 553 return 1;
564 554
565 /* SET MAX LOCK and SET MAX FREEZE LOCK */ 555 /* SET MAX LOCK and SET MAX FREEZE LOCK */
566 if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) && 556 if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
567 tf->command == ATA_CMD_SET_MAX && 557 tf->command == ATA_CMD_SET_MAX &&
568 (tf->feature == ATA_SET_MAX_LOCK || 558 (tf->feature == ATA_SET_MAX_LOCK ||
569 tf->feature == ATA_SET_MAX_FREEZE_LOCK)) 559 tf->feature == ATA_SET_MAX_FREEZE_LOCK))
570 return 1; 560 return 1;
571 } 561 }
572 562
573 if (tf->command == ATA_CMD_SET_FEATURES && 563 if (tf->command == ATA_CMD_SET_FEATURES &&
574 tf->feature == SETFEATURES_SATA_ENABLE) { 564 tf->feature == SETFEATURES_SATA_ENABLE) {
575 /* inhibit enabling DIPM */ 565 /* inhibit enabling DIPM */
576 if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM && 566 if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
577 tf->nsect == SATA_DIPM) 567 tf->nsect == SATA_DIPM)
578 return 1; 568 return 1;
579 569
580 /* inhibit FPDMA non-zero offset */ 570 /* inhibit FPDMA non-zero offset */
581 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET && 571 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
582 (tf->nsect == SATA_FPDMA_OFFSET || 572 (tf->nsect == SATA_FPDMA_OFFSET ||
583 tf->nsect == SATA_FPDMA_IN_ORDER)) 573 tf->nsect == SATA_FPDMA_IN_ORDER))
584 return 1; 574 return 1;
585 575
586 /* inhibit FPDMA auto activation */ 576 /* inhibit FPDMA auto activation */
587 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA && 577 if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
588 tf->nsect == SATA_FPDMA_AA) 578 tf->nsect == SATA_FPDMA_AA)
589 return 1; 579 return 1;
590 } 580 }
591 581
592 return 0; 582 return 0;
593 } 583 }
594 584
595 /** 585 /**
596 * ata_acpi_run_tf - send taskfile registers to host controller 586 * ata_acpi_run_tf - send taskfile registers to host controller
597 * @dev: target ATA device 587 * @dev: target ATA device
598 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7) 588 * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
599 * 589 *
600 * Outputs ATA taskfile to standard ATA host controller. 590 * Outputs ATA taskfile to standard ATA host controller.
601 * Writes the control, feature, nsect, lbal, lbam, and lbah registers. 591 * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
602 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect, 592 * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
603 * hob_lbal, hob_lbam, and hob_lbah. 593 * hob_lbal, hob_lbam, and hob_lbah.
604 * 594 *
605 * This function waits for idle (!BUSY and !DRQ) after writing 595 * This function waits for idle (!BUSY and !DRQ) after writing
606 * registers. If the control register has a new value, this 596 * registers. If the control register has a new value, this
607 * function also waits for idle after writing control and before 597 * function also waits for idle after writing control and before
608 * writing the remaining registers. 598 * writing the remaining registers.
609 * 599 *
610 * LOCKING: 600 * LOCKING:
611 * EH context. 601 * EH context.
612 * 602 *
613 * RETURNS: 603 * RETURNS:
614 * 1 if command is executed successfully. 0 if ignored, rejected or 604 * 1 if command is executed successfully. 0 if ignored, rejected or
615 * filtered out, -errno on other errors. 605 * filtered out, -errno on other errors.
616 */ 606 */
617 static int ata_acpi_run_tf(struct ata_device *dev, 607 static int ata_acpi_run_tf(struct ata_device *dev,
618 const struct ata_acpi_gtf *gtf, 608 const struct ata_acpi_gtf *gtf,
619 const struct ata_acpi_gtf *prev_gtf) 609 const struct ata_acpi_gtf *prev_gtf)
620 { 610 {
621 struct ata_taskfile *pptf = NULL; 611 struct ata_taskfile *pptf = NULL;
622 struct ata_taskfile tf, ptf, rtf; 612 struct ata_taskfile tf, ptf, rtf;
623 unsigned int err_mask; 613 unsigned int err_mask;
624 const char *level; 614 const char *level;
625 const char *descr; 615 const char *descr;
626 char msg[60]; 616 char msg[60];
627 int rc; 617 int rc;
628 618
629 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0) 619 if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
630 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0) 620 && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
631 && (gtf->tf[6] == 0)) 621 && (gtf->tf[6] == 0))
632 return 0; 622 return 0;
633 623
634 ata_acpi_gtf_to_tf(dev, gtf, &tf); 624 ata_acpi_gtf_to_tf(dev, gtf, &tf);
635 if (prev_gtf) { 625 if (prev_gtf) {
636 ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf); 626 ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
637 pptf = &ptf; 627 pptf = &ptf;
638 } 628 }
639 629
640 if (!ata_acpi_filter_tf(dev, &tf, pptf)) { 630 if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
641 rtf = tf; 631 rtf = tf;
642 err_mask = ata_exec_internal(dev, &rtf, NULL, 632 err_mask = ata_exec_internal(dev, &rtf, NULL,
643 DMA_NONE, NULL, 0, 0); 633 DMA_NONE, NULL, 0, 0);
644 634
645 switch (err_mask) { 635 switch (err_mask) {
646 case 0: 636 case 0:
647 level = KERN_DEBUG; 637 level = KERN_DEBUG;
648 snprintf(msg, sizeof(msg), "succeeded"); 638 snprintf(msg, sizeof(msg), "succeeded");
649 rc = 1; 639 rc = 1;
650 break; 640 break;
651 641
652 case AC_ERR_DEV: 642 case AC_ERR_DEV:
653 level = KERN_INFO; 643 level = KERN_INFO;
654 snprintf(msg, sizeof(msg), 644 snprintf(msg, sizeof(msg),
655 "rejected by device (Stat=0x%02x Err=0x%02x)", 645 "rejected by device (Stat=0x%02x Err=0x%02x)",
656 rtf.command, rtf.feature); 646 rtf.command, rtf.feature);
657 rc = 0; 647 rc = 0;
658 break; 648 break;
659 649
660 default: 650 default:
661 level = KERN_ERR; 651 level = KERN_ERR;
662 snprintf(msg, sizeof(msg), 652 snprintf(msg, sizeof(msg),
663 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)", 653 "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
664 err_mask, rtf.command, rtf.feature); 654 err_mask, rtf.command, rtf.feature);
665 rc = -EIO; 655 rc = -EIO;
666 break; 656 break;
667 } 657 }
668 } else { 658 } else {
669 level = KERN_INFO; 659 level = KERN_INFO;
670 snprintf(msg, sizeof(msg), "filtered out"); 660 snprintf(msg, sizeof(msg), "filtered out");
671 rc = 0; 661 rc = 0;
672 } 662 }
673 descr = ata_get_cmd_descript(tf.command); 663 descr = ata_get_cmd_descript(tf.command);
674 664
675 ata_dev_printk(dev, level, 665 ata_dev_printk(dev, level,
676 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n", 666 "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
677 tf.command, tf.feature, tf.nsect, tf.lbal, 667 tf.command, tf.feature, tf.nsect, tf.lbal,
678 tf.lbam, tf.lbah, tf.device, 668 tf.lbam, tf.lbah, tf.device,
679 (descr ? descr : "unknown"), msg); 669 (descr ? descr : "unknown"), msg);
680 670
681 return rc; 671 return rc;
682 } 672 }
683 673
684 /** 674 /**
685 * ata_acpi_exec_tfs - get then write drive taskfile settings 675 * ata_acpi_exec_tfs - get then write drive taskfile settings
686 * @dev: target ATA device 676 * @dev: target ATA device
687 * @nr_executed: out parameter for the number of executed commands 677 * @nr_executed: out parameter for the number of executed commands
688 * 678 *
689 * Evaluate _GTF and execute returned taskfiles. 679 * Evaluate _GTF and execute returned taskfiles.
690 * 680 *
691 * LOCKING: 681 * LOCKING:
692 * EH context. 682 * EH context.
693 * 683 *
694 * RETURNS: 684 * RETURNS:
695 * Number of executed taskfiles on success, 0 if _GTF doesn't exist. 685 * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
696 * -errno on other errors. 686 * -errno on other errors.
697 */ 687 */
698 static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed) 688 static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
699 { 689 {
700 struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL; 690 struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
701 int gtf_count, i, rc; 691 int gtf_count, i, rc;
702 692
703 /* get taskfiles */ 693 /* get taskfiles */
704 rc = ata_dev_get_GTF(dev, &gtf); 694 rc = ata_dev_get_GTF(dev, &gtf);
705 if (rc < 0) 695 if (rc < 0)
706 return rc; 696 return rc;
707 gtf_count = rc; 697 gtf_count = rc;
708 698
709 /* execute them */ 699 /* execute them */
710 for (i = 0; i < gtf_count; i++, gtf++) { 700 for (i = 0; i < gtf_count; i++, gtf++) {
711 rc = ata_acpi_run_tf(dev, gtf, pgtf); 701 rc = ata_acpi_run_tf(dev, gtf, pgtf);
712 if (rc < 0) 702 if (rc < 0)
713 break; 703 break;
714 if (rc) { 704 if (rc) {
715 (*nr_executed)++; 705 (*nr_executed)++;
716 pgtf = gtf; 706 pgtf = gtf;
717 } 707 }
718 } 708 }
719 709
720 ata_acpi_clear_gtf(dev); 710 ata_acpi_clear_gtf(dev);
721 711
722 if (rc < 0) 712 if (rc < 0)
723 return rc; 713 return rc;
724 return 0; 714 return 0;
725 } 715 }
726 716
727 /** 717 /**
728 * ata_acpi_push_id - send Identify data to drive 718 * ata_acpi_push_id - send Identify data to drive
729 * @dev: target ATA device 719 * @dev: target ATA device
730 * 720 *
731 * _SDD ACPI object: for SATA mode only 721 * _SDD ACPI object: for SATA mode only
732 * Must be after Identify (Packet) Device -- uses its data 722 * Must be after Identify (Packet) Device -- uses its data
733 * ATM this function never returns a failure. It is an optional 723 * ATM this function never returns a failure. It is an optional
734 * method and if it fails for whatever reason, we should still 724 * method and if it fails for whatever reason, we should still
735 * just keep going. 725 * just keep going.
736 * 726 *
737 * LOCKING: 727 * LOCKING:
738 * EH context. 728 * EH context.
739 * 729 *
740 * RETURNS: 730 * RETURNS:
741 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure. 731 * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
742 */ 732 */
743 static int ata_acpi_push_id(struct ata_device *dev) 733 static int ata_acpi_push_id(struct ata_device *dev)
744 { 734 {
745 struct ata_port *ap = dev->link->ap; 735 struct ata_port *ap = dev->link->ap;
746 acpi_status status; 736 acpi_status status;
747 struct acpi_object_list input; 737 struct acpi_object_list input;
748 union acpi_object in_params[1]; 738 union acpi_object in_params[1];
749 739
750 if (ata_msg_probe(ap)) 740 if (ata_msg_probe(ap))
751 ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n", 741 ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
752 __func__, dev->devno, ap->port_no); 742 __func__, dev->devno, ap->port_no);
753 743
754 /* Give the drive Identify data to the drive via the _SDD method */ 744 /* Give the drive Identify data to the drive via the _SDD method */
755 /* _SDD: set up input parameters */ 745 /* _SDD: set up input parameters */
756 input.count = 1; 746 input.count = 1;
757 input.pointer = in_params; 747 input.pointer = in_params;
758 in_params[0].type = ACPI_TYPE_BUFFER; 748 in_params[0].type = ACPI_TYPE_BUFFER;
759 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS; 749 in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
760 in_params[0].buffer.pointer = (u8 *)dev->id; 750 in_params[0].buffer.pointer = (u8 *)dev->id;
761 /* Output buffer: _SDD has no output */ 751 /* Output buffer: _SDD has no output */
762 752
763 /* It's OK for _SDD to be missing too. */ 753 /* It's OK for _SDD to be missing too. */
764 swap_buf_le16(dev->id, ATA_ID_WORDS); 754 swap_buf_le16(dev->id, ATA_ID_WORDS);
765 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input, 755 status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
766 NULL); 756 NULL);
767 swap_buf_le16(dev->id, ATA_ID_WORDS); 757 swap_buf_le16(dev->id, ATA_ID_WORDS);
768 758
769 if (status == AE_NOT_FOUND) 759 if (status == AE_NOT_FOUND)
770 return -ENOENT; 760 return -ENOENT;
771 761
772 if (ACPI_FAILURE(status)) { 762 if (ACPI_FAILURE(status)) {
773 ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status); 763 ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
774 return -EIO; 764 return -EIO;
775 } 765 }
776 766
777 return 0; 767 return 0;
778 } 768 }
779 769
780 /** 770 /**
781 * ata_acpi_on_suspend - ATA ACPI hook called on suspend 771 * ata_acpi_on_suspend - ATA ACPI hook called on suspend
782 * @ap: target ATA port 772 * @ap: target ATA port
783 * 773 *
784 * This function is called when @ap is about to be suspended. All 774 * This function is called when @ap is about to be suspended. All
785 * devices are already put to sleep but the port_suspend() callback 775 * devices are already put to sleep but the port_suspend() callback
786 * hasn't been executed yet. Error return from this function aborts 776 * hasn't been executed yet. Error return from this function aborts
787 * suspend. 777 * suspend.
788 * 778 *
789 * LOCKING: 779 * LOCKING:
790 * EH context. 780 * EH context.
791 * 781 *
792 * RETURNS: 782 * RETURNS:
793 * 0 on success, -errno on failure. 783 * 0 on success, -errno on failure.
794 */ 784 */
795 int ata_acpi_on_suspend(struct ata_port *ap) 785 int ata_acpi_on_suspend(struct ata_port *ap)
796 { 786 {
797 /* nada */ 787 /* nada */
798 return 0; 788 return 0;
799 } 789 }
800 790
801 /** 791 /**
802 * ata_acpi_on_resume - ATA ACPI hook called on resume 792 * ata_acpi_on_resume - ATA ACPI hook called on resume
803 * @ap: target ATA port 793 * @ap: target ATA port
804 * 794 *
805 * This function is called when @ap is resumed - right after port 795 * This function is called when @ap is resumed - right after port
806 * itself is resumed but before any EH action is taken. 796 * itself is resumed but before any EH action is taken.
807 * 797 *
808 * LOCKING: 798 * LOCKING:
809 * EH context. 799 * EH context.
810 */ 800 */
811 void ata_acpi_on_resume(struct ata_port *ap) 801 void ata_acpi_on_resume(struct ata_port *ap)
812 { 802 {
813 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap); 803 const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
814 struct ata_device *dev; 804 struct ata_device *dev;
815 805
816 if (ata_ap_acpi_handle(ap) && gtm) { 806 if (ata_ap_acpi_handle(ap) && gtm) {
817 /* _GTM valid */ 807 /* _GTM valid */
818 808
819 /* restore timing parameters */ 809 /* restore timing parameters */
820 ata_acpi_stm(ap, gtm); 810 ata_acpi_stm(ap, gtm);
821 811
822 /* _GTF should immediately follow _STM so that it can 812 /* _GTF should immediately follow _STM so that it can
823 * use values set by _STM. Cache _GTF result and 813 * use values set by _STM. Cache _GTF result and
824 * schedule _GTF. 814 * schedule _GTF.
825 */ 815 */
826 ata_for_each_dev(dev, &ap->link, ALL) { 816 ata_for_each_dev(dev, &ap->link, ALL) {
827 ata_acpi_clear_gtf(dev); 817 ata_acpi_clear_gtf(dev);
828 if (ata_dev_enabled(dev) && 818 if (ata_dev_enabled(dev) &&
829 ata_dev_get_GTF(dev, NULL) >= 0) 819 ata_dev_get_GTF(dev, NULL) >= 0)
830 dev->flags |= ATA_DFLAG_ACPI_PENDING; 820 dev->flags |= ATA_DFLAG_ACPI_PENDING;
831 } 821 }
832 } else { 822 } else {
833 /* SATA _GTF needs to be evaulated after _SDD and 823 /* SATA _GTF needs to be evaulated after _SDD and
834 * there's no reason to evaluate IDE _GTF early 824 * there's no reason to evaluate IDE _GTF early
835 * without _STM. Clear cache and schedule _GTF. 825 * without _STM. Clear cache and schedule _GTF.
836 */ 826 */
837 ata_for_each_dev(dev, &ap->link, ALL) { 827 ata_for_each_dev(dev, &ap->link, ALL) {
838 ata_acpi_clear_gtf(dev); 828 ata_acpi_clear_gtf(dev);
839 if (ata_dev_enabled(dev)) 829 if (ata_dev_enabled(dev))
840 dev->flags |= ATA_DFLAG_ACPI_PENDING; 830 dev->flags |= ATA_DFLAG_ACPI_PENDING;
841 } 831 }
842 } 832 }
843 } 833 }
844 834
845 /** 835 /**
846 * ata_acpi_set_state - set the port power state 836 * ata_acpi_set_state - set the port power state
847 * @ap: target ATA port 837 * @ap: target ATA port
848 * @state: state, on/off 838 * @state: state, on/off
849 * 839 *
850 * This function executes the _PS0/_PS3 ACPI method to set the power state. 840 * This function executes the _PS0/_PS3 ACPI method to set the power state.
851 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off 841 * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
852 */ 842 */
853 void ata_acpi_set_state(struct ata_port *ap, pm_message_t state) 843 void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
854 { 844 {
855 struct ata_device *dev; 845 struct ata_device *dev;
856 acpi_handle handle; 846 acpi_handle handle;
857 int acpi_state; 847 int acpi_state;
858 848
859 /* channel first and then drives for power on and vica versa 849 /* channel first and then drives for power on and vica versa
860 for power off */ 850 for power off */
861 handle = ata_ap_acpi_handle(ap); 851 handle = ata_ap_acpi_handle(ap);
862 if (handle && state.event == PM_EVENT_ON) 852 if (handle && state.event == PM_EVENT_ON)
863 acpi_bus_set_power(handle, ACPI_STATE_D0); 853 acpi_bus_set_power(handle, ACPI_STATE_D0);
864 854
865 ata_for_each_dev(dev, &ap->link, ENABLED) { 855 ata_for_each_dev(dev, &ap->link, ENABLED) {
866 handle = ata_dev_acpi_handle(dev); 856 handle = ata_dev_acpi_handle(dev);
867 if (!handle) 857 if (!handle)
868 continue; 858 continue;
869 859
870 if (state.event != PM_EVENT_ON) { 860 if (state.event != PM_EVENT_ON) {
871 acpi_state = acpi_pm_device_sleep_state( 861 acpi_state = acpi_pm_device_sleep_state(
872 &dev->sdev->sdev_gendev, NULL, ACPI_STATE_D3); 862 &dev->sdev->sdev_gendev, NULL, ACPI_STATE_D3);
873 if (acpi_state > 0) 863 if (acpi_state > 0)
874 acpi_bus_set_power(handle, acpi_state); 864 acpi_bus_set_power(handle, acpi_state);
875 /* TBD: need to check if it's runtime pm request */ 865 /* TBD: need to check if it's runtime pm request */
876 acpi_pm_device_run_wake( 866 acpi_pm_device_run_wake(
877 &dev->sdev->sdev_gendev, true); 867 &dev->sdev->sdev_gendev, true);
878 } else { 868 } else {
879 /* Ditto */ 869 /* Ditto */
880 acpi_pm_device_run_wake( 870 acpi_pm_device_run_wake(
881 &dev->sdev->sdev_gendev, false); 871 &dev->sdev->sdev_gendev, false);
882 acpi_bus_set_power(handle, ACPI_STATE_D0); 872 acpi_bus_set_power(handle, ACPI_STATE_D0);
883 } 873 }
884 } 874 }
885 875
886 handle = ata_ap_acpi_handle(ap); 876 handle = ata_ap_acpi_handle(ap);
887 if (handle && state.event != PM_EVENT_ON) 877 if (handle && state.event != PM_EVENT_ON)
888 acpi_bus_set_power(handle, ACPI_STATE_D3); 878 acpi_bus_set_power(handle, ACPI_STATE_D3);
889 } 879 }
890 880
891 /** 881 /**
892 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration 882 * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
893 * @dev: target ATA device 883 * @dev: target ATA device
894 * 884 *
895 * This function is called when @dev is about to be configured. 885 * This function is called when @dev is about to be configured.
896 * IDENTIFY data might have been modified after this hook is run. 886 * IDENTIFY data might have been modified after this hook is run.
897 * 887 *
898 * LOCKING: 888 * LOCKING:
899 * EH context. 889 * EH context.
900 * 890 *
901 * RETURNS: 891 * RETURNS:
902 * Positive number if IDENTIFY data needs to be refreshed, 0 if not, 892 * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
903 * -errno on failure. 893 * -errno on failure.
904 */ 894 */
905 int ata_acpi_on_devcfg(struct ata_device *dev) 895 int ata_acpi_on_devcfg(struct ata_device *dev)
906 { 896 {
907 struct ata_port *ap = dev->link->ap; 897 struct ata_port *ap = dev->link->ap;
908 struct ata_eh_context *ehc = &ap->link.eh_context; 898 struct ata_eh_context *ehc = &ap->link.eh_context;
909 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA; 899 int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
910 int nr_executed = 0; 900 int nr_executed = 0;
911 int rc; 901 int rc;
912 902
913 if (!ata_dev_acpi_handle(dev)) 903 if (!ata_dev_acpi_handle(dev))
914 return 0; 904 return 0;
915 905
916 /* do we need to do _GTF? */ 906 /* do we need to do _GTF? */
917 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) && 907 if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
918 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET))) 908 !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
919 return 0; 909 return 0;
920 910
921 /* do _SDD if SATA */ 911 /* do _SDD if SATA */
922 if (acpi_sata) { 912 if (acpi_sata) {
923 rc = ata_acpi_push_id(dev); 913 rc = ata_acpi_push_id(dev);
924 if (rc && rc != -ENOENT) 914 if (rc && rc != -ENOENT)
925 goto acpi_err; 915 goto acpi_err;
926 } 916 }
927 917
928 /* do _GTF */ 918 /* do _GTF */
929 rc = ata_acpi_exec_tfs(dev, &nr_executed); 919 rc = ata_acpi_exec_tfs(dev, &nr_executed);
930 if (rc) 920 if (rc)
931 goto acpi_err; 921 goto acpi_err;
932 922
933 dev->flags &= ~ATA_DFLAG_ACPI_PENDING; 923 dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
934 924
935 /* refresh IDENTIFY page if any _GTF command has been executed */ 925 /* refresh IDENTIFY page if any _GTF command has been executed */
936 if (nr_executed) { 926 if (nr_executed) {
937 rc = ata_dev_reread_id(dev, 0); 927 rc = ata_dev_reread_id(dev, 0);
938 if (rc < 0) { 928 if (rc < 0) {
939 ata_dev_err(dev, 929 ata_dev_err(dev,
940 "failed to IDENTIFY after ACPI commands\n"); 930 "failed to IDENTIFY after ACPI commands\n");
941 return rc; 931 return rc;
942 } 932 }
943 } 933 }
944 934
945 return 0; 935 return 0;
946 936
947 acpi_err: 937 acpi_err:
948 /* ignore evaluation failure if we can continue safely */ 938 /* ignore evaluation failure if we can continue safely */
949 if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN)) 939 if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
950 return 0; 940 return 0;
951 941
952 /* fail and let EH retry once more for unknown IO errors */ 942 /* fail and let EH retry once more for unknown IO errors */
953 if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) { 943 if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
954 dev->flags |= ATA_DFLAG_ACPI_FAILED; 944 dev->flags |= ATA_DFLAG_ACPI_FAILED;
955 return rc; 945 return rc;
956 } 946 }
957 947
958 ata_dev_warn(dev, "ACPI: failed the second time, disabled\n"); 948 ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
959 949
960 /* We can safely continue if no _GTF command has been executed 950 /* We can safely continue if no _GTF command has been executed
961 * and port is not frozen. 951 * and port is not frozen.
962 */ 952 */
963 if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN)) 953 if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
964 return 0; 954 return 0;
965 955
966 return rc; 956 return rc;
967 } 957 }
968 958
969 /** 959 /**
970 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled 960 * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
971 * @dev: target ATA device 961 * @dev: target ATA device
972 * 962 *
973 * This function is called when @dev is about to be disabled. 963 * This function is called when @dev is about to be disabled.
974 * 964 *
975 * LOCKING: 965 * LOCKING:
976 * EH context. 966 * EH context.
977 */ 967 */
978 void ata_acpi_on_disable(struct ata_device *dev) 968 void ata_acpi_on_disable(struct ata_device *dev)
979 { 969 {
980 ata_acpi_clear_gtf(dev); 970 ata_acpi_clear_gtf(dev);
981 } 971 }
982 972
983 static void ata_acpi_wake_dev(acpi_handle handle, u32 event, void *context) 973 static void ata_acpi_wake_dev(acpi_handle handle, u32 event, void *context)
984 { 974 {
985 struct ata_device *ata_dev = context; 975 struct ata_device *ata_dev = context;
986 976
987 if (event == ACPI_NOTIFY_DEVICE_WAKE && ata_dev && 977 if (event == ACPI_NOTIFY_DEVICE_WAKE && ata_dev &&
988 pm_runtime_suspended(&ata_dev->sdev->sdev_gendev)) 978 pm_runtime_suspended(&ata_dev->sdev->sdev_gendev))
989 scsi_autopm_get_device(ata_dev->sdev); 979 scsi_autopm_get_device(ata_dev->sdev);
990 } 980 }
991 981
992 static void ata_acpi_add_pm_notifier(struct ata_device *dev) 982 static void ata_acpi_add_pm_notifier(struct ata_device *dev)
993 { 983 {
994 struct acpi_device *acpi_dev; 984 struct acpi_device *acpi_dev;
995 acpi_handle handle; 985 acpi_handle handle;
996 acpi_status status; 986 acpi_status status;
997 987
998 handle = ata_dev_acpi_handle(dev); 988 handle = ata_dev_acpi_handle(dev);
999 if (!handle) 989 if (!handle)
1000 return; 990 return;
1001 991
1002 status = acpi_bus_get_device(handle, &acpi_dev); 992 status = acpi_bus_get_device(handle, &acpi_dev);
1003 if (ACPI_FAILURE(status)) 993 if (ACPI_FAILURE(status))
1004 return; 994 return;
1005 995
1006 if (dev->sdev->can_power_off) { 996 if (dev->sdev->can_power_off) {
1007 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY, 997 acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1008 ata_acpi_wake_dev, dev); 998 ata_acpi_wake_dev, dev);
1009 device_set_run_wake(&dev->sdev->sdev_gendev, true); 999 device_set_run_wake(&dev->sdev->sdev_gendev, true);
1010 } 1000 }
1011 } 1001 }
1012 1002
1013 static void ata_acpi_remove_pm_notifier(struct ata_device *dev) 1003 static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
1014 { 1004 {
1015 struct acpi_device *acpi_dev; 1005 struct acpi_device *acpi_dev;
1016 acpi_handle handle; 1006 acpi_handle handle;
1017 acpi_status status; 1007 acpi_status status;
1018 1008
1019 handle = ata_dev_acpi_handle(dev); 1009 handle = ata_dev_acpi_handle(dev);
1020 if (!handle) 1010 if (!handle)
1021 return; 1011 return;
1022 1012
1023 status = acpi_bus_get_device(handle, &acpi_dev); 1013 status = acpi_bus_get_device(handle, &acpi_dev);
1024 if (ACPI_FAILURE(status)) 1014 if (ACPI_FAILURE(status))
1025 return; 1015 return;
1026 1016
1027 if (dev->sdev->can_power_off) { 1017 if (dev->sdev->can_power_off) {
1028 device_set_run_wake(&dev->sdev->sdev_gendev, false); 1018 device_set_run_wake(&dev->sdev->sdev_gendev, false);
1029 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY, 1019 acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
1030 ata_acpi_wake_dev); 1020 ata_acpi_wake_dev);
1031 } 1021 }
1032 } 1022 }
1033 1023
1034 static void ata_acpi_register_power_resource(struct ata_device *dev) 1024 static void ata_acpi_register_power_resource(struct ata_device *dev)
1035 { 1025 {
1036 struct scsi_device *sdev = dev->sdev; 1026 struct scsi_device *sdev = dev->sdev;
1037 acpi_handle handle; 1027 acpi_handle handle;
1038 struct device *device; 1028 struct device *device;
1039 1029
1040 handle = ata_dev_acpi_handle(dev); 1030 handle = ata_dev_acpi_handle(dev);
1041 if (!handle) 1031 if (!handle)
1042 return; 1032 return;
1043 1033
1044 device = &sdev->sdev_gendev; 1034 device = &sdev->sdev_gendev;
1045 1035
1046 acpi_power_resource_register_device(device, handle); 1036 acpi_power_resource_register_device(device, handle);
1047 } 1037 }
1048 1038
1049 static void ata_acpi_unregister_power_resource(struct ata_device *dev) 1039 static void ata_acpi_unregister_power_resource(struct ata_device *dev)
1050 { 1040 {
1051 struct scsi_device *sdev = dev->sdev; 1041 struct scsi_device *sdev = dev->sdev;
1052 acpi_handle handle; 1042 acpi_handle handle;
1053 struct device *device; 1043 struct device *device;
1054 1044
1055 handle = ata_dev_acpi_handle(dev); 1045 handle = ata_dev_acpi_handle(dev);
1056 if (!handle) 1046 if (!handle)
1057 return; 1047 return;
1058 1048
1059 device = &sdev->sdev_gendev; 1049 device = &sdev->sdev_gendev;
1060 1050
1061 acpi_power_resource_unregister_device(device, handle); 1051 acpi_power_resource_unregister_device(device, handle);
1062 } 1052 }
1063 1053
1064 void ata_acpi_bind(struct ata_device *dev) 1054 void ata_acpi_bind(struct ata_device *dev)
1065 { 1055 {
1066 ata_acpi_add_pm_notifier(dev); 1056 ata_acpi_add_pm_notifier(dev);
1067 ata_acpi_register_power_resource(dev); 1057 ata_acpi_register_power_resource(dev);
1068 } 1058 }
1069 1059
1070 void ata_acpi_unbind(struct ata_device *dev) 1060 void ata_acpi_unbind(struct ata_device *dev)
1071 { 1061 {
1072 ata_acpi_remove_pm_notifier(dev); 1062 ata_acpi_remove_pm_notifier(dev);
1073 ata_acpi_unregister_power_resource(dev); 1063 ata_acpi_unregister_power_resource(dev);
1074 } 1064 }
1075 1065
1076 static int compat_pci_ata(struct ata_port *ap) 1066 static int compat_pci_ata(struct ata_port *ap)
1077 { 1067 {
1078 struct device *dev = ap->tdev.parent; 1068 struct device *dev = ap->tdev.parent;
1079 struct pci_dev *pdev; 1069 struct pci_dev *pdev;
1080 1070
1081 if (!is_pci_dev(dev)) 1071 if (!is_pci_dev(dev))
1082 return 0; 1072 return 0;
1083 1073
1084 pdev = to_pci_dev(dev); 1074 pdev = to_pci_dev(dev);
1085 1075
1086 if ((pdev->class >> 8) != PCI_CLASS_STORAGE_SATA && 1076 if ((pdev->class >> 8) != PCI_CLASS_STORAGE_SATA &&
1087 (pdev->class >> 8) != PCI_CLASS_STORAGE_IDE) 1077 (pdev->class >> 8) != PCI_CLASS_STORAGE_IDE)
1088 return 0; 1078 return 0;
1089 1079
1090 return 1; 1080 return 1;
1091 } 1081 }
1092 1082
1093 static int ata_acpi_bind_host(struct ata_port *ap, acpi_handle *handle) 1083 static int ata_acpi_bind_host(struct ata_port *ap, acpi_handle *handle)
1094 { 1084 {
1095 if (ap->flags & ATA_FLAG_ACPI_SATA) 1085 if (ap->flags & ATA_FLAG_ACPI_SATA)
1096 return -ENODEV; 1086 return -ENODEV;
1097 1087
1098 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(ap->tdev.parent), 1088 *handle = acpi_get_child(DEVICE_ACPI_HANDLE(ap->tdev.parent),
1099 ap->port_no); 1089 ap->port_no);
1100 1090
1101 if (!*handle) 1091 if (!*handle)
1102 return -ENODEV; 1092 return -ENODEV;
1093
1094 if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
1095 ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
1103 1096
1104 return 0; 1097 return 0;
1105 } 1098 }
1106 1099
1107 static int ata_acpi_bind_device(struct ata_port *ap, struct scsi_device *sdev, 1100 static int ata_acpi_bind_device(struct ata_port *ap, struct scsi_device *sdev,
1108 acpi_handle *handle) 1101 acpi_handle *handle)
1109 { 1102 {
1110 struct ata_device *ata_dev; 1103 struct ata_device *ata_dev;
1111 acpi_status status; 1104 acpi_status status;
1112 struct acpi_device *acpi_dev; 1105 struct acpi_device *acpi_dev;
1113 struct acpi_device_power_state *states; 1106 struct acpi_device_power_state *states;
1114 1107
1115 if (ap->flags & ATA_FLAG_ACPI_SATA) 1108 if (ap->flags & ATA_FLAG_ACPI_SATA)
1116 ata_dev = &ap->link.device[sdev->channel]; 1109 ata_dev = &ap->link.device[sdev->channel];
1117 else 1110 else
1118 ata_dev = &ap->link.device[sdev->id]; 1111 ata_dev = &ap->link.device[sdev->id];
1119 1112
1120 *handle = ata_dev_acpi_handle(ata_dev); 1113 *handle = ata_dev_acpi_handle(ata_dev);
1121 1114
1122 if (!*handle) 1115 if (!*handle)
1123 return -ENODEV; 1116 return -ENODEV;
1124 1117
1125 status = acpi_bus_get_device(*handle, &acpi_dev); 1118 status = acpi_bus_get_device(*handle, &acpi_dev);
1126 if (ACPI_FAILURE(status)) 1119 if (ACPI_FAILURE(status))
1127 return 0; 1120 return 0;
1128 1121
1129 /* 1122 /*
1130 * If firmware has _PS3 or _PR3 for this device, 1123 * If firmware has _PS3 or _PR3 for this device,
1131 * and this ata ODD device support device attention, 1124 * and this ata ODD device support device attention,
1132 * it means this device can be powered off 1125 * it means this device can be powered off
1133 */ 1126 */
1134 states = acpi_dev->power.states; 1127 states = acpi_dev->power.states;
1135 if ((states[ACPI_STATE_D3_HOT].flags.valid || 1128 if ((states[ACPI_STATE_D3_HOT].flags.valid ||
1136 states[ACPI_STATE_D3_COLD].flags.explicit_set) && 1129 states[ACPI_STATE_D3_COLD].flags.explicit_set) &&
1137 ata_dev->flags & ATA_DFLAG_DA) 1130 ata_dev->flags & ATA_DFLAG_DA)
1138 sdev->can_power_off = 1; 1131 sdev->can_power_off = 1;
1139 1132
1140 return 0; 1133 return 0;
1141 } 1134 }
1142 1135
1143 static int is_ata_port(const struct device *dev) 1136 static int is_ata_port(const struct device *dev)
1144 { 1137 {
1145 return dev->type == &ata_port_type; 1138 return dev->type == &ata_port_type;
1146 } 1139 }
1147 1140
1148 static struct ata_port *dev_to_ata_port(struct device *dev) 1141 static struct ata_port *dev_to_ata_port(struct device *dev)
1149 { 1142 {
1150 while (!is_ata_port(dev)) { 1143 while (!is_ata_port(dev)) {
1151 if (!dev->parent) 1144 if (!dev->parent)
1152 return NULL; 1145 return NULL;
1153 dev = dev->parent; 1146 dev = dev->parent;
1154 } 1147 }
1155 return to_ata_port(dev); 1148 return to_ata_port(dev);
1156 } 1149 }
1157 1150
1158 static int ata_acpi_find_device(struct device *dev, acpi_handle *handle) 1151 static int ata_acpi_find_device(struct device *dev, acpi_handle *handle)
1159 { 1152 {
1160 struct ata_port *ap = dev_to_ata_port(dev); 1153 struct ata_port *ap = dev_to_ata_port(dev);
1161 1154
1162 if (!ap) 1155 if (!ap)
1163 return -ENODEV; 1156 return -ENODEV;
1164 1157
1165 if (!compat_pci_ata(ap)) 1158 if (!compat_pci_ata(ap))
1166 return -ENODEV; 1159 return -ENODEV;
1167 1160
1168 if (scsi_is_host_device(dev)) 1161 if (scsi_is_host_device(dev))
1169 return ata_acpi_bind_host(ap, handle); 1162 return ata_acpi_bind_host(ap, handle);
1170 else if (scsi_is_sdev_device(dev)) { 1163 else if (scsi_is_sdev_device(dev)) {
1171 struct scsi_device *sdev = to_scsi_device(dev); 1164 struct scsi_device *sdev = to_scsi_device(dev);
1172 1165
1173 return ata_acpi_bind_device(ap, sdev, handle); 1166 return ata_acpi_bind_device(ap, sdev, handle);
1174 } else 1167 } else
1175 return -ENODEV; 1168 return -ENODEV;
1176 } 1169 }
1177 1170
1178 static int ata_acpi_find_dummy(struct device *dev, acpi_handle *handle) 1171 static int ata_acpi_find_dummy(struct device *dev, acpi_handle *handle)
1179 { 1172 {
1180 return -ENODEV; 1173 return -ENODEV;
1181 } 1174 }
1182 1175
1183 static struct acpi_bus_type ata_acpi_bus = { 1176 static struct acpi_bus_type ata_acpi_bus = {
1184 .find_bridge = ata_acpi_find_dummy, 1177 .find_bridge = ata_acpi_find_dummy,
1185 .find_device = ata_acpi_find_device, 1178 .find_device = ata_acpi_find_device,
1186 }; 1179 };
1187 1180
1188 int ata_acpi_register(void) 1181 int ata_acpi_register(void)
1189 { 1182 {
1190 return scsi_register_acpi_bus_type(&ata_acpi_bus); 1183 return scsi_register_acpi_bus_type(&ata_acpi_bus);
1191 } 1184 }
1192 1185
1193 void ata_acpi_unregister(void) 1186 void ata_acpi_unregister(void)
1194 { 1187 {