libata-acpi.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037
  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. #include <linux/module.h>
  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 <scsi/scsi_device.h>
  18. #include "libata.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>
  26. enum {
  27. ATA_ACPI_FILTER_SETXFER = 1 << 0,
  28. ATA_ACPI_FILTER_LOCK = 1 << 1,
  29. ATA_ACPI_FILTER_DIPM = 1 << 2,
  30. ATA_ACPI_FILTER_DEFAULT = ATA_ACPI_FILTER_SETXFER |
  31. ATA_ACPI_FILTER_LOCK |
  32. ATA_ACPI_FILTER_DIPM,
  33. };
  34. static unsigned int ata_acpi_gtf_filter = ATA_ACPI_FILTER_DEFAULT;
  35. module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644);
  36. 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)");
  37. #define NO_PORT_MULT 0xffff
  38. #define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
  39. #define REGS_PER_GTF 7
  40. struct ata_acpi_gtf {
  41. u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
  42. } __packed;
  43. /*
  44. * Helper - belongs in the PCI layer somewhere eventually
  45. */
  46. static int is_pci_dev(struct device *dev)
  47. {
  48. return (dev->bus == &pci_bus_type);
  49. }
  50. static void ata_acpi_clear_gtf(struct ata_device *dev)
  51. {
  52. kfree(dev->gtf_cache);
  53. dev->gtf_cache = NULL;
  54. }
  55. /**
  56. * ata_acpi_associate_sata_port - associate SATA port with ACPI objects
  57. * @ap: target SATA port
  58. *
  59. * Look up ACPI objects associated with @ap and initialize acpi_handle
  60. * fields of @ap, the port and devices accordingly.
  61. *
  62. * LOCKING:
  63. * EH context.
  64. *
  65. * RETURNS:
  66. * 0 on success, -errno on failure.
  67. */
  68. void ata_acpi_associate_sata_port(struct ata_port *ap)
  69. {
  70. WARN_ON(!(ap->flags & ATA_FLAG_ACPI_SATA));
  71. if (!sata_pmp_attached(ap)) {
  72. acpi_integer adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
  73. ap->link.device->acpi_handle =
  74. acpi_get_child(ap->host->acpi_handle, adr);
  75. } else {
  76. struct ata_link *link;
  77. ap->link.device->acpi_handle = NULL;
  78. ata_port_for_each_link(link, ap) {
  79. acpi_integer adr = SATA_ADR(ap->port_no, link->pmp);
  80. link->device->acpi_handle =
  81. acpi_get_child(ap->host->acpi_handle, adr);
  82. }
  83. }
  84. }
  85. static void ata_acpi_associate_ide_port(struct ata_port *ap)
  86. {
  87. int max_devices, i;
  88. ap->acpi_handle = acpi_get_child(ap->host->acpi_handle, ap->port_no);
  89. if (!ap->acpi_handle)
  90. return;
  91. max_devices = 1;
  92. if (ap->flags & ATA_FLAG_SLAVE_POSS)
  93. max_devices++;
  94. for (i = 0; i < max_devices; i++) {
  95. struct ata_device *dev = &ap->link.device[i];
  96. dev->acpi_handle = acpi_get_child(ap->acpi_handle, i);
  97. }
  98. if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
  99. ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
  100. }
  101. /* @ap and @dev are the same as ata_acpi_handle_hotplug() */
  102. static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
  103. {
  104. if (dev)
  105. dev->flags |= ATA_DFLAG_DETACH;
  106. else {
  107. struct ata_link *tlink;
  108. struct ata_device *tdev;
  109. ata_port_for_each_link(tlink, ap)
  110. ata_link_for_each_dev(tdev, tlink)
  111. tdev->flags |= ATA_DFLAG_DETACH;
  112. }
  113. ata_port_schedule_eh(ap);
  114. }
  115. /**
  116. * ata_acpi_handle_hotplug - ACPI event handler backend
  117. * @ap: ATA port ACPI event occurred
  118. * @dev: ATA device ACPI event occurred (can be NULL)
  119. * @event: ACPI event which occurred
  120. *
  121. * All ACPI bay / device realted events end up in this function. If
  122. * the event is port-wide @dev is NULL. If the event is specific to a
  123. * device, @dev points to it.
  124. *
  125. * Hotplug (as opposed to unplug) notification is always handled as
  126. * port-wide while unplug only kills the target device on device-wide
  127. * event.
  128. *
  129. * LOCKING:
  130. * ACPI notify handler context. May sleep.
  131. */
  132. static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
  133. u32 event)
  134. {
  135. struct ata_eh_info *ehi = &ap->link.eh_info;
  136. int wait = 0;
  137. unsigned long flags;
  138. acpi_handle handle;
  139. if (dev)
  140. handle = dev->acpi_handle;
  141. else
  142. handle = ap->acpi_handle;
  143. spin_lock_irqsave(ap->lock, flags);
  144. /*
  145. * When dock driver calls into the routine, it will always use
  146. * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
  147. * ACPI_NOTIFY_EJECT_REQUEST for remove
  148. */
  149. switch (event) {
  150. case ACPI_NOTIFY_BUS_CHECK:
  151. case ACPI_NOTIFY_DEVICE_CHECK:
  152. ata_ehi_push_desc(ehi, "ACPI event");
  153. ata_ehi_hotplugged(ehi);
  154. ata_port_freeze(ap);
  155. break;
  156. case ACPI_NOTIFY_EJECT_REQUEST:
  157. ata_ehi_push_desc(ehi, "ACPI event");
  158. ata_acpi_detach_device(ap, dev);
  159. wait = 1;
  160. break;
  161. }
  162. spin_unlock_irqrestore(ap->lock, flags);
  163. if (wait)
  164. ata_port_wait_eh(ap);
  165. }
  166. static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data)
  167. {
  168. struct ata_device *dev = data;
  169. ata_acpi_handle_hotplug(dev->link->ap, dev, event);
  170. }
  171. static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data)
  172. {
  173. struct ata_port *ap = data;
  174. ata_acpi_handle_hotplug(ap, NULL, event);
  175. }
  176. static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
  177. u32 event)
  178. {
  179. struct kobject *kobj = NULL;
  180. char event_string[20];
  181. char *envp[] = { event_string, NULL };
  182. if (dev) {
  183. if (dev->sdev)
  184. kobj = &dev->sdev->sdev_gendev.kobj;
  185. } else
  186. kobj = &ap->dev->kobj;
  187. if (kobj) {
  188. snprintf(event_string, 20, "BAY_EVENT=%d", event);
  189. kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
  190. }
  191. }
  192. static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data)
  193. {
  194. ata_acpi_uevent(data, NULL, event);
  195. }
  196. static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data)
  197. {
  198. struct ata_device *dev = data;
  199. ata_acpi_uevent(dev->link->ap, dev, event);
  200. }
  201. static struct acpi_dock_ops ata_acpi_dev_dock_ops = {
  202. .handler = ata_acpi_dev_notify_dock,
  203. .uevent = ata_acpi_dev_uevent,
  204. };
  205. static struct acpi_dock_ops ata_acpi_ap_dock_ops = {
  206. .handler = ata_acpi_ap_notify_dock,
  207. .uevent = ata_acpi_ap_uevent,
  208. };
  209. /**
  210. * ata_acpi_associate - associate ATA host with ACPI objects
  211. * @host: target ATA host
  212. *
  213. * Look up ACPI objects associated with @host and initialize
  214. * acpi_handle fields of @host, its ports and devices accordingly.
  215. *
  216. * LOCKING:
  217. * EH context.
  218. *
  219. * RETURNS:
  220. * 0 on success, -errno on failure.
  221. */
  222. void ata_acpi_associate(struct ata_host *host)
  223. {
  224. int i, j;
  225. if (!is_pci_dev(host->dev) || libata_noacpi)
  226. return;
  227. host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev);
  228. if (!host->acpi_handle)
  229. return;
  230. for (i = 0; i < host->n_ports; i++) {
  231. struct ata_port *ap = host->ports[i];
  232. if (host->ports[0]->flags & ATA_FLAG_ACPI_SATA)
  233. ata_acpi_associate_sata_port(ap);
  234. else
  235. ata_acpi_associate_ide_port(ap);
  236. if (ap->acpi_handle) {
  237. /* we might be on a docking station */
  238. register_hotplug_dock_device(ap->acpi_handle,
  239. &ata_acpi_ap_dock_ops, ap);
  240. }
  241. for (j = 0; j < ata_link_max_devices(&ap->link); j++) {
  242. struct ata_device *dev = &ap->link.device[j];
  243. if (dev->acpi_handle) {
  244. /* we might be on a docking station */
  245. register_hotplug_dock_device(dev->acpi_handle,
  246. &ata_acpi_dev_dock_ops, dev);
  247. }
  248. }
  249. }
  250. }
  251. /**
  252. * ata_acpi_dissociate - dissociate ATA host from ACPI objects
  253. * @host: target ATA host
  254. *
  255. * This function is called during driver detach after the whole host
  256. * is shut down.
  257. *
  258. * LOCKING:
  259. * EH context.
  260. */
  261. void ata_acpi_dissociate(struct ata_host *host)
  262. {
  263. int i;
  264. /* Restore initial _GTM values so that driver which attaches
  265. * afterward can use them too.
  266. */
  267. for (i = 0; i < host->n_ports; i++) {
  268. struct ata_port *ap = host->ports[i];
  269. const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
  270. if (ap->acpi_handle && gtm)
  271. ata_acpi_stm(ap, gtm);
  272. }
  273. }
  274. /**
  275. * ata_acpi_gtm - execute _GTM
  276. * @ap: target ATA port
  277. * @gtm: out parameter for _GTM result
  278. *
  279. * Evaluate _GTM and store the result in @gtm.
  280. *
  281. * LOCKING:
  282. * EH context.
  283. *
  284. * RETURNS:
  285. * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
  286. */
  287. int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
  288. {
  289. struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
  290. union acpi_object *out_obj;
  291. acpi_status status;
  292. int rc = 0;
  293. status = acpi_evaluate_object(ap->acpi_handle, "_GTM", NULL, &output);
  294. rc = -ENOENT;
  295. if (status == AE_NOT_FOUND)
  296. goto out_free;
  297. rc = -EINVAL;
  298. if (ACPI_FAILURE(status)) {
  299. ata_port_printk(ap, KERN_ERR,
  300. "ACPI get timing mode failed (AE 0x%x)\n",
  301. status);
  302. goto out_free;
  303. }
  304. out_obj = output.pointer;
  305. if (out_obj->type != ACPI_TYPE_BUFFER) {
  306. ata_port_printk(ap, KERN_WARNING,
  307. "_GTM returned unexpected object type 0x%x\n",
  308. out_obj->type);
  309. goto out_free;
  310. }
  311. if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
  312. ata_port_printk(ap, KERN_ERR,
  313. "_GTM returned invalid length %d\n",
  314. out_obj->buffer.length);
  315. goto out_free;
  316. }
  317. memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
  318. rc = 0;
  319. out_free:
  320. kfree(output.pointer);
  321. return rc;
  322. }
  323. EXPORT_SYMBOL_GPL(ata_acpi_gtm);
  324. /**
  325. * ata_acpi_stm - execute _STM
  326. * @ap: target ATA port
  327. * @stm: timing parameter to _STM
  328. *
  329. * Evaluate _STM with timing parameter @stm.
  330. *
  331. * LOCKING:
  332. * EH context.
  333. *
  334. * RETURNS:
  335. * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
  336. */
  337. int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
  338. {
  339. acpi_status status;
  340. struct ata_acpi_gtm stm_buf = *stm;
  341. struct acpi_object_list input;
  342. union acpi_object in_params[3];
  343. in_params[0].type = ACPI_TYPE_BUFFER;
  344. in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
  345. in_params[0].buffer.pointer = (u8 *)&stm_buf;
  346. /* Buffers for id may need byteswapping ? */
  347. in_params[1].type = ACPI_TYPE_BUFFER;
  348. in_params[1].buffer.length = 512;
  349. in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
  350. in_params[2].type = ACPI_TYPE_BUFFER;
  351. in_params[2].buffer.length = 512;
  352. in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
  353. input.count = 3;
  354. input.pointer = in_params;
  355. status = acpi_evaluate_object(ap->acpi_handle, "_STM", &input, NULL);
  356. if (status == AE_NOT_FOUND)
  357. return -ENOENT;
  358. if (ACPI_FAILURE(status)) {
  359. ata_port_printk(ap, KERN_ERR,
  360. "ACPI set timing mode failed (status=0x%x)\n", status);
  361. return -EINVAL;
  362. }
  363. return 0;
  364. }
  365. EXPORT_SYMBOL_GPL(ata_acpi_stm);
  366. /**
  367. * ata_dev_get_GTF - get the drive bootup default taskfile settings
  368. * @dev: target ATA device
  369. * @gtf: output parameter for buffer containing _GTF taskfile arrays
  370. *
  371. * This applies to both PATA and SATA drives.
  372. *
  373. * The _GTF method has no input parameters.
  374. * It returns a variable number of register set values (registers
  375. * hex 1F1..1F7, taskfiles).
  376. * The <variable number> is not known in advance, so have ACPI-CA
  377. * allocate the buffer as needed and return it, then free it later.
  378. *
  379. * LOCKING:
  380. * EH context.
  381. *
  382. * RETURNS:
  383. * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
  384. * if _GTF is invalid.
  385. */
  386. static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
  387. {
  388. struct ata_port *ap = dev->link->ap;
  389. acpi_status status;
  390. struct acpi_buffer output;
  391. union acpi_object *out_obj;
  392. int rc = 0;
  393. /* if _GTF is cached, use the cached value */
  394. if (dev->gtf_cache) {
  395. out_obj = dev->gtf_cache;
  396. goto done;
  397. }
  398. /* set up output buffer */
  399. output.length = ACPI_ALLOCATE_BUFFER;
  400. output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
  401. if (ata_msg_probe(ap))
  402. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
  403. __func__, ap->port_no);
  404. /* _GTF has no input parameters */
  405. status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output);
  406. out_obj = dev->gtf_cache = output.pointer;
  407. if (ACPI_FAILURE(status)) {
  408. if (status != AE_NOT_FOUND) {
  409. ata_dev_printk(dev, KERN_WARNING,
  410. "_GTF evaluation failed (AE 0x%x)\n",
  411. status);
  412. rc = -EINVAL;
  413. }
  414. goto out_free;
  415. }
  416. if (!output.length || !output.pointer) {
  417. if (ata_msg_probe(ap))
  418. ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: "
  419. "length or ptr is NULL (0x%llx, 0x%p)\n",
  420. __func__,
  421. (unsigned long long)output.length,
  422. output.pointer);
  423. rc = -EINVAL;
  424. goto out_free;
  425. }
  426. if (out_obj->type != ACPI_TYPE_BUFFER) {
  427. ata_dev_printk(dev, KERN_WARNING,
  428. "_GTF unexpected object type 0x%x\n",
  429. out_obj->type);
  430. rc = -EINVAL;
  431. goto out_free;
  432. }
  433. if (out_obj->buffer.length % REGS_PER_GTF) {
  434. ata_dev_printk(dev, KERN_WARNING,
  435. "unexpected _GTF length (%d)\n",
  436. out_obj->buffer.length);
  437. rc = -EINVAL;
  438. goto out_free;
  439. }
  440. done:
  441. rc = out_obj->buffer.length / REGS_PER_GTF;
  442. if (gtf) {
  443. *gtf = (void *)out_obj->buffer.pointer;
  444. if (ata_msg_probe(ap))
  445. ata_dev_printk(dev, KERN_DEBUG,
  446. "%s: returning gtf=%p, gtf_count=%d\n",
  447. __func__, *gtf, rc);
  448. }
  449. return rc;
  450. out_free:
  451. ata_acpi_clear_gtf(dev);
  452. return rc;
  453. }
  454. /**
  455. * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
  456. * @dev: target device
  457. * @gtm: GTM parameter to use
  458. *
  459. * Determine xfermask for @dev from @gtm.
  460. *
  461. * LOCKING:
  462. * None.
  463. *
  464. * RETURNS:
  465. * Determined xfermask.
  466. */
  467. unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
  468. const struct ata_acpi_gtm *gtm)
  469. {
  470. unsigned long xfer_mask = 0;
  471. unsigned int type;
  472. int unit;
  473. u8 mode;
  474. /* we always use the 0 slot for crap hardware */
  475. unit = dev->devno;
  476. if (!(gtm->flags & 0x10))
  477. unit = 0;
  478. /* PIO */
  479. mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
  480. xfer_mask |= ata_xfer_mode2mask(mode);
  481. /* See if we have MWDMA or UDMA data. We don't bother with
  482. * MWDMA if UDMA is available as this means the BIOS set UDMA
  483. * and our error changedown if it works is UDMA to PIO anyway.
  484. */
  485. if (!(gtm->flags & (1 << (2 * unit))))
  486. type = ATA_SHIFT_MWDMA;
  487. else
  488. type = ATA_SHIFT_UDMA;
  489. mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
  490. xfer_mask |= ata_xfer_mode2mask(mode);
  491. return xfer_mask;
  492. }
  493. EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
  494. /**
  495. * ata_acpi_cbl_80wire - Check for 80 wire cable
  496. * @ap: Port to check
  497. * @gtm: GTM data to use
  498. *
  499. * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
  500. */
  501. int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
  502. {
  503. struct ata_device *dev;
  504. ata_link_for_each_dev(dev, &ap->link) {
  505. unsigned long xfer_mask, udma_mask;
  506. if (!ata_dev_enabled(dev))
  507. continue;
  508. xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
  509. ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
  510. if (udma_mask & ~ATA_UDMA_MASK_40C)
  511. return 1;
  512. }
  513. return 0;
  514. }
  515. EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
  516. static void ata_acpi_gtf_to_tf(struct ata_device *dev,
  517. const struct ata_acpi_gtf *gtf,
  518. struct ata_taskfile *tf)
  519. {
  520. ata_tf_init(dev, tf);
  521. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  522. tf->protocol = ATA_PROT_NODATA;
  523. tf->feature = gtf->tf[0]; /* 0x1f1 */
  524. tf->nsect = gtf->tf[1]; /* 0x1f2 */
  525. tf->lbal = gtf->tf[2]; /* 0x1f3 */
  526. tf->lbam = gtf->tf[3]; /* 0x1f4 */
  527. tf->lbah = gtf->tf[4]; /* 0x1f5 */
  528. tf->device = gtf->tf[5]; /* 0x1f6 */
  529. tf->command = gtf->tf[6]; /* 0x1f7 */
  530. }
  531. static int ata_acpi_filter_tf(const struct ata_taskfile *tf,
  532. const struct ata_taskfile *ptf)
  533. {
  534. if (ata_acpi_gtf_filter & ATA_ACPI_FILTER_SETXFER) {
  535. /* libata doesn't use ACPI to configure transfer mode.
  536. * It will only confuse device configuration. Skip.
  537. */
  538. if (tf->command == ATA_CMD_SET_FEATURES &&
  539. tf->feature == SETFEATURES_XFER)
  540. return 1;
  541. }
  542. if (ata_acpi_gtf_filter & ATA_ACPI_FILTER_LOCK) {
  543. /* BIOS writers, sorry but we don't wanna lock
  544. * features unless the user explicitly said so.
  545. */
  546. /* DEVICE CONFIGURATION FREEZE LOCK */
  547. if (tf->command == ATA_CMD_CONF_OVERLAY &&
  548. tf->feature == ATA_DCO_FREEZE_LOCK)
  549. return 1;
  550. /* SECURITY FREEZE LOCK */
  551. if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
  552. return 1;
  553. /* SET MAX LOCK and SET MAX FREEZE LOCK */
  554. if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
  555. tf->command == ATA_CMD_SET_MAX &&
  556. (tf->feature == ATA_SET_MAX_LOCK ||
  557. tf->feature == ATA_SET_MAX_FREEZE_LOCK))
  558. return 1;
  559. }
  560. if (ata_acpi_gtf_filter & ATA_ACPI_FILTER_DIPM) {
  561. /* inhibit enabling DIPM */
  562. if (tf->command == ATA_CMD_SET_FEATURES &&
  563. tf->feature == SETFEATURES_SATA_ENABLE &&
  564. tf->nsect == SATA_DIPM)
  565. return 1;
  566. }
  567. return 0;
  568. }
  569. /**
  570. * ata_acpi_run_tf - send taskfile registers to host controller
  571. * @dev: target ATA device
  572. * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
  573. *
  574. * Outputs ATA taskfile to standard ATA host controller using MMIO
  575. * or PIO as indicated by the ATA_FLAG_MMIO flag.
  576. * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
  577. * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
  578. * hob_lbal, hob_lbam, and hob_lbah.
  579. *
  580. * This function waits for idle (!BUSY and !DRQ) after writing
  581. * registers. If the control register has a new value, this
  582. * function also waits for idle after writing control and before
  583. * writing the remaining registers.
  584. *
  585. * LOCKING:
  586. * EH context.
  587. *
  588. * RETURNS:
  589. * 1 if command is executed successfully. 0 if ignored, rejected or
  590. * filtered out, -errno on other errors.
  591. */
  592. static int ata_acpi_run_tf(struct ata_device *dev,
  593. const struct ata_acpi_gtf *gtf,
  594. const struct ata_acpi_gtf *prev_gtf)
  595. {
  596. struct ata_taskfile *pptf = NULL;
  597. struct ata_taskfile tf, ptf, rtf;
  598. unsigned int err_mask;
  599. const char *level;
  600. char msg[60];
  601. int rc;
  602. if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
  603. && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
  604. && (gtf->tf[6] == 0))
  605. return 0;
  606. ata_acpi_gtf_to_tf(dev, gtf, &tf);
  607. if (prev_gtf) {
  608. ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
  609. pptf = &ptf;
  610. }
  611. if (!ata_acpi_filter_tf(&tf, pptf)) {
  612. rtf = tf;
  613. err_mask = ata_exec_internal(dev, &rtf, NULL,
  614. DMA_NONE, NULL, 0, 0);
  615. switch (err_mask) {
  616. case 0:
  617. level = KERN_DEBUG;
  618. snprintf(msg, sizeof(msg), "succeeded");
  619. rc = 1;
  620. break;
  621. case AC_ERR_DEV:
  622. level = KERN_INFO;
  623. snprintf(msg, sizeof(msg),
  624. "rejected by device (Stat=0x%02x Err=0x%02x)",
  625. rtf.command, rtf.feature);
  626. rc = 0;
  627. break;
  628. default:
  629. level = KERN_ERR;
  630. snprintf(msg, sizeof(msg),
  631. "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
  632. err_mask, rtf.command, rtf.feature);
  633. rc = -EIO;
  634. break;
  635. }
  636. } else {
  637. level = KERN_INFO;
  638. snprintf(msg, sizeof(msg), "filtered out");
  639. rc = 0;
  640. }
  641. ata_dev_printk(dev, level,
  642. "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x %s\n",
  643. tf.command, tf.feature, tf.nsect, tf.lbal,
  644. tf.lbam, tf.lbah, tf.device, msg);
  645. return rc;
  646. }
  647. /**
  648. * ata_acpi_exec_tfs - get then write drive taskfile settings
  649. * @dev: target ATA device
  650. * @nr_executed: out paramter for the number of executed commands
  651. *
  652. * Evaluate _GTF and excute returned taskfiles.
  653. *
  654. * LOCKING:
  655. * EH context.
  656. *
  657. * RETURNS:
  658. * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
  659. * -errno on other errors.
  660. */
  661. static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
  662. {
  663. struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
  664. int gtf_count, i, rc;
  665. /* get taskfiles */
  666. rc = ata_dev_get_GTF(dev, &gtf);
  667. if (rc < 0)
  668. return rc;
  669. gtf_count = rc;
  670. /* execute them */
  671. for (i = 0; i < gtf_count; i++, gtf++) {
  672. rc = ata_acpi_run_tf(dev, gtf, pgtf);
  673. if (rc < 0)
  674. break;
  675. if (rc) {
  676. (*nr_executed)++;
  677. pgtf = gtf;
  678. }
  679. }
  680. ata_acpi_clear_gtf(dev);
  681. if (rc < 0)
  682. return rc;
  683. return 0;
  684. }
  685. /**
  686. * ata_acpi_push_id - send Identify data to drive
  687. * @dev: target ATA device
  688. *
  689. * _SDD ACPI object: for SATA mode only
  690. * Must be after Identify (Packet) Device -- uses its data
  691. * ATM this function never returns a failure. It is an optional
  692. * method and if it fails for whatever reason, we should still
  693. * just keep going.
  694. *
  695. * LOCKING:
  696. * EH context.
  697. *
  698. * RETURNS:
  699. * 0 on success, -errno on failure.
  700. */
  701. static int ata_acpi_push_id(struct ata_device *dev)
  702. {
  703. struct ata_port *ap = dev->link->ap;
  704. int err;
  705. acpi_status status;
  706. struct acpi_object_list input;
  707. union acpi_object in_params[1];
  708. if (ata_msg_probe(ap))
  709. ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
  710. __func__, dev->devno, ap->port_no);
  711. /* Give the drive Identify data to the drive via the _SDD method */
  712. /* _SDD: set up input parameters */
  713. input.count = 1;
  714. input.pointer = in_params;
  715. in_params[0].type = ACPI_TYPE_BUFFER;
  716. in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
  717. in_params[0].buffer.pointer = (u8 *)dev->id;
  718. /* Output buffer: _SDD has no output */
  719. /* It's OK for _SDD to be missing too. */
  720. swap_buf_le16(dev->id, ATA_ID_WORDS);
  721. status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
  722. swap_buf_le16(dev->id, ATA_ID_WORDS);
  723. err = ACPI_FAILURE(status) ? -EIO : 0;
  724. if (err < 0)
  725. ata_dev_printk(dev, KERN_WARNING,
  726. "ACPI _SDD failed (AE 0x%x)\n", status);
  727. return err;
  728. }
  729. /**
  730. * ata_acpi_on_suspend - ATA ACPI hook called on suspend
  731. * @ap: target ATA port
  732. *
  733. * This function is called when @ap is about to be suspended. All
  734. * devices are already put to sleep but the port_suspend() callback
  735. * hasn't been executed yet. Error return from this function aborts
  736. * suspend.
  737. *
  738. * LOCKING:
  739. * EH context.
  740. *
  741. * RETURNS:
  742. * 0 on success, -errno on failure.
  743. */
  744. int ata_acpi_on_suspend(struct ata_port *ap)
  745. {
  746. /* nada */
  747. return 0;
  748. }
  749. /**
  750. * ata_acpi_on_resume - ATA ACPI hook called on resume
  751. * @ap: target ATA port
  752. *
  753. * This function is called when @ap is resumed - right after port
  754. * itself is resumed but before any EH action is taken.
  755. *
  756. * LOCKING:
  757. * EH context.
  758. */
  759. void ata_acpi_on_resume(struct ata_port *ap)
  760. {
  761. const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
  762. struct ata_device *dev;
  763. if (ap->acpi_handle && gtm) {
  764. /* _GTM valid */
  765. /* restore timing parameters */
  766. ata_acpi_stm(ap, gtm);
  767. /* _GTF should immediately follow _STM so that it can
  768. * use values set by _STM. Cache _GTF result and
  769. * schedule _GTF.
  770. */
  771. ata_link_for_each_dev(dev, &ap->link) {
  772. ata_acpi_clear_gtf(dev);
  773. if (ata_dev_enabled(dev) &&
  774. ata_dev_get_GTF(dev, NULL) >= 0)
  775. dev->flags |= ATA_DFLAG_ACPI_PENDING;
  776. }
  777. } else {
  778. /* SATA _GTF needs to be evaulated after _SDD and
  779. * there's no reason to evaluate IDE _GTF early
  780. * without _STM. Clear cache and schedule _GTF.
  781. */
  782. ata_link_for_each_dev(dev, &ap->link) {
  783. ata_acpi_clear_gtf(dev);
  784. if (ata_dev_enabled(dev))
  785. dev->flags |= ATA_DFLAG_ACPI_PENDING;
  786. }
  787. }
  788. }
  789. /**
  790. * ata_acpi_set_state - set the port power state
  791. * @ap: target ATA port
  792. * @state: state, on/off
  793. *
  794. * This function executes the _PS0/_PS3 ACPI method to set the power state.
  795. * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
  796. */
  797. void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
  798. {
  799. struct ata_device *dev;
  800. if (!ap->acpi_handle || (ap->flags & ATA_FLAG_ACPI_SATA))
  801. return;
  802. /* channel first and then drives for power on and vica versa
  803. for power off */
  804. if (state.event == PM_EVENT_ON)
  805. acpi_bus_set_power(ap->acpi_handle, ACPI_STATE_D0);
  806. ata_link_for_each_dev(dev, &ap->link) {
  807. if (dev->acpi_handle && ata_dev_enabled(dev))
  808. acpi_bus_set_power(dev->acpi_handle,
  809. state.event == PM_EVENT_ON ?
  810. ACPI_STATE_D0 : ACPI_STATE_D3);
  811. }
  812. if (state.event != PM_EVENT_ON)
  813. acpi_bus_set_power(ap->acpi_handle, ACPI_STATE_D3);
  814. }
  815. /**
  816. * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
  817. * @dev: target ATA device
  818. *
  819. * This function is called when @dev is about to be configured.
  820. * IDENTIFY data might have been modified after this hook is run.
  821. *
  822. * LOCKING:
  823. * EH context.
  824. *
  825. * RETURNS:
  826. * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
  827. * -errno on failure.
  828. */
  829. int ata_acpi_on_devcfg(struct ata_device *dev)
  830. {
  831. struct ata_port *ap = dev->link->ap;
  832. struct ata_eh_context *ehc = &ap->link.eh_context;
  833. int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
  834. int nr_executed = 0;
  835. int rc;
  836. if (!dev->acpi_handle)
  837. return 0;
  838. /* do we need to do _GTF? */
  839. if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
  840. !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
  841. return 0;
  842. /* do _SDD if SATA */
  843. if (acpi_sata) {
  844. rc = ata_acpi_push_id(dev);
  845. if (rc)
  846. goto acpi_err;
  847. }
  848. /* do _GTF */
  849. rc = ata_acpi_exec_tfs(dev, &nr_executed);
  850. if (rc)
  851. goto acpi_err;
  852. dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
  853. /* refresh IDENTIFY page if any _GTF command has been executed */
  854. if (nr_executed) {
  855. rc = ata_dev_reread_id(dev, 0);
  856. if (rc < 0) {
  857. ata_dev_printk(dev, KERN_ERR, "failed to IDENTIFY "
  858. "after ACPI commands\n");
  859. return rc;
  860. }
  861. }
  862. return 0;
  863. acpi_err:
  864. /* ignore evaluation failure if we can continue safely */
  865. if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
  866. return 0;
  867. /* fail and let EH retry once more for unknown IO errors */
  868. if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
  869. dev->flags |= ATA_DFLAG_ACPI_FAILED;
  870. return rc;
  871. }
  872. ata_dev_printk(dev, KERN_WARNING,
  873. "ACPI: failed the second time, disabled\n");
  874. dev->acpi_handle = NULL;
  875. /* We can safely continue if no _GTF command has been executed
  876. * and port is not frozen.
  877. */
  878. if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
  879. return 0;
  880. return rc;
  881. }
  882. /**
  883. * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
  884. * @dev: target ATA device
  885. *
  886. * This function is called when @dev is about to be disabled.
  887. *
  888. * LOCKING:
  889. * EH context.
  890. */
  891. void ata_acpi_on_disable(struct ata_device *dev)
  892. {
  893. ata_acpi_clear_gtf(dev);
  894. }