libata-acpi.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034
  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_for_each_link(link, ap, EDGE) {
  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_for_each_link(tlink, ap, EDGE)
  110. ata_for_each_dev(tdev, tlink, ALL)
  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_for_each_dev(dev, &ap->link, ENABLED) {
  505. unsigned long xfer_mask, udma_mask;
  506. xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
  507. ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
  508. if (udma_mask & ~ATA_UDMA_MASK_40C)
  509. return 1;
  510. }
  511. return 0;
  512. }
  513. EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
  514. static void ata_acpi_gtf_to_tf(struct ata_device *dev,
  515. const struct ata_acpi_gtf *gtf,
  516. struct ata_taskfile *tf)
  517. {
  518. ata_tf_init(dev, tf);
  519. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  520. tf->protocol = ATA_PROT_NODATA;
  521. tf->feature = gtf->tf[0]; /* 0x1f1 */
  522. tf->nsect = gtf->tf[1]; /* 0x1f2 */
  523. tf->lbal = gtf->tf[2]; /* 0x1f3 */
  524. tf->lbam = gtf->tf[3]; /* 0x1f4 */
  525. tf->lbah = gtf->tf[4]; /* 0x1f5 */
  526. tf->device = gtf->tf[5]; /* 0x1f6 */
  527. tf->command = gtf->tf[6]; /* 0x1f7 */
  528. }
  529. static int ata_acpi_filter_tf(const struct ata_taskfile *tf,
  530. const struct ata_taskfile *ptf)
  531. {
  532. if (ata_acpi_gtf_filter & ATA_ACPI_FILTER_SETXFER) {
  533. /* libata doesn't use ACPI to configure transfer mode.
  534. * It will only confuse device configuration. Skip.
  535. */
  536. if (tf->command == ATA_CMD_SET_FEATURES &&
  537. tf->feature == SETFEATURES_XFER)
  538. return 1;
  539. }
  540. if (ata_acpi_gtf_filter & ATA_ACPI_FILTER_LOCK) {
  541. /* BIOS writers, sorry but we don't wanna lock
  542. * features unless the user explicitly said so.
  543. */
  544. /* DEVICE CONFIGURATION FREEZE LOCK */
  545. if (tf->command == ATA_CMD_CONF_OVERLAY &&
  546. tf->feature == ATA_DCO_FREEZE_LOCK)
  547. return 1;
  548. /* SECURITY FREEZE LOCK */
  549. if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
  550. return 1;
  551. /* SET MAX LOCK and SET MAX FREEZE LOCK */
  552. if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
  553. tf->command == ATA_CMD_SET_MAX &&
  554. (tf->feature == ATA_SET_MAX_LOCK ||
  555. tf->feature == ATA_SET_MAX_FREEZE_LOCK))
  556. return 1;
  557. }
  558. if (ata_acpi_gtf_filter & ATA_ACPI_FILTER_DIPM) {
  559. /* inhibit enabling DIPM */
  560. if (tf->command == ATA_CMD_SET_FEATURES &&
  561. tf->feature == SETFEATURES_SATA_ENABLE &&
  562. tf->nsect == SATA_DIPM)
  563. return 1;
  564. }
  565. return 0;
  566. }
  567. /**
  568. * ata_acpi_run_tf - send taskfile registers to host controller
  569. * @dev: target ATA device
  570. * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
  571. *
  572. * Outputs ATA taskfile to standard ATA host controller using MMIO
  573. * or PIO as indicated by the ATA_FLAG_MMIO flag.
  574. * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
  575. * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
  576. * hob_lbal, hob_lbam, and hob_lbah.
  577. *
  578. * This function waits for idle (!BUSY and !DRQ) after writing
  579. * registers. If the control register has a new value, this
  580. * function also waits for idle after writing control and before
  581. * writing the remaining registers.
  582. *
  583. * LOCKING:
  584. * EH context.
  585. *
  586. * RETURNS:
  587. * 1 if command is executed successfully. 0 if ignored, rejected or
  588. * filtered out, -errno on other errors.
  589. */
  590. static int ata_acpi_run_tf(struct ata_device *dev,
  591. const struct ata_acpi_gtf *gtf,
  592. const struct ata_acpi_gtf *prev_gtf)
  593. {
  594. struct ata_taskfile *pptf = NULL;
  595. struct ata_taskfile tf, ptf, rtf;
  596. unsigned int err_mask;
  597. const char *level;
  598. char msg[60];
  599. int rc;
  600. if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
  601. && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
  602. && (gtf->tf[6] == 0))
  603. return 0;
  604. ata_acpi_gtf_to_tf(dev, gtf, &tf);
  605. if (prev_gtf) {
  606. ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
  607. pptf = &ptf;
  608. }
  609. if (!ata_acpi_filter_tf(&tf, pptf)) {
  610. rtf = tf;
  611. err_mask = ata_exec_internal(dev, &rtf, NULL,
  612. DMA_NONE, NULL, 0, 0);
  613. switch (err_mask) {
  614. case 0:
  615. level = KERN_DEBUG;
  616. snprintf(msg, sizeof(msg), "succeeded");
  617. rc = 1;
  618. break;
  619. case AC_ERR_DEV:
  620. level = KERN_INFO;
  621. snprintf(msg, sizeof(msg),
  622. "rejected by device (Stat=0x%02x Err=0x%02x)",
  623. rtf.command, rtf.feature);
  624. rc = 0;
  625. break;
  626. default:
  627. level = KERN_ERR;
  628. snprintf(msg, sizeof(msg),
  629. "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
  630. err_mask, rtf.command, rtf.feature);
  631. rc = -EIO;
  632. break;
  633. }
  634. } else {
  635. level = KERN_INFO;
  636. snprintf(msg, sizeof(msg), "filtered out");
  637. rc = 0;
  638. }
  639. ata_dev_printk(dev, level,
  640. "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x %s\n",
  641. tf.command, tf.feature, tf.nsect, tf.lbal,
  642. tf.lbam, tf.lbah, tf.device, msg);
  643. return rc;
  644. }
  645. /**
  646. * ata_acpi_exec_tfs - get then write drive taskfile settings
  647. * @dev: target ATA device
  648. * @nr_executed: out paramter for the number of executed commands
  649. *
  650. * Evaluate _GTF and excute returned taskfiles.
  651. *
  652. * LOCKING:
  653. * EH context.
  654. *
  655. * RETURNS:
  656. * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
  657. * -errno on other errors.
  658. */
  659. static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
  660. {
  661. struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
  662. int gtf_count, i, rc;
  663. /* get taskfiles */
  664. rc = ata_dev_get_GTF(dev, &gtf);
  665. if (rc < 0)
  666. return rc;
  667. gtf_count = rc;
  668. /* execute them */
  669. for (i = 0; i < gtf_count; i++, gtf++) {
  670. rc = ata_acpi_run_tf(dev, gtf, pgtf);
  671. if (rc < 0)
  672. break;
  673. if (rc) {
  674. (*nr_executed)++;
  675. pgtf = gtf;
  676. }
  677. }
  678. ata_acpi_clear_gtf(dev);
  679. if (rc < 0)
  680. return rc;
  681. return 0;
  682. }
  683. /**
  684. * ata_acpi_push_id - send Identify data to drive
  685. * @dev: target ATA device
  686. *
  687. * _SDD ACPI object: for SATA mode only
  688. * Must be after Identify (Packet) Device -- uses its data
  689. * ATM this function never returns a failure. It is an optional
  690. * method and if it fails for whatever reason, we should still
  691. * just keep going.
  692. *
  693. * LOCKING:
  694. * EH context.
  695. *
  696. * RETURNS:
  697. * 0 on success, -errno on failure.
  698. */
  699. static int ata_acpi_push_id(struct ata_device *dev)
  700. {
  701. struct ata_port *ap = dev->link->ap;
  702. int err;
  703. acpi_status status;
  704. struct acpi_object_list input;
  705. union acpi_object in_params[1];
  706. if (ata_msg_probe(ap))
  707. ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
  708. __func__, dev->devno, ap->port_no);
  709. /* Give the drive Identify data to the drive via the _SDD method */
  710. /* _SDD: set up input parameters */
  711. input.count = 1;
  712. input.pointer = in_params;
  713. in_params[0].type = ACPI_TYPE_BUFFER;
  714. in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
  715. in_params[0].buffer.pointer = (u8 *)dev->id;
  716. /* Output buffer: _SDD has no output */
  717. /* It's OK for _SDD to be missing too. */
  718. swap_buf_le16(dev->id, ATA_ID_WORDS);
  719. status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
  720. swap_buf_le16(dev->id, ATA_ID_WORDS);
  721. err = ACPI_FAILURE(status) ? -EIO : 0;
  722. if (err < 0)
  723. ata_dev_printk(dev, KERN_WARNING,
  724. "ACPI _SDD failed (AE 0x%x)\n", status);
  725. return err;
  726. }
  727. /**
  728. * ata_acpi_on_suspend - ATA ACPI hook called on suspend
  729. * @ap: target ATA port
  730. *
  731. * This function is called when @ap is about to be suspended. All
  732. * devices are already put to sleep but the port_suspend() callback
  733. * hasn't been executed yet. Error return from this function aborts
  734. * suspend.
  735. *
  736. * LOCKING:
  737. * EH context.
  738. *
  739. * RETURNS:
  740. * 0 on success, -errno on failure.
  741. */
  742. int ata_acpi_on_suspend(struct ata_port *ap)
  743. {
  744. /* nada */
  745. return 0;
  746. }
  747. /**
  748. * ata_acpi_on_resume - ATA ACPI hook called on resume
  749. * @ap: target ATA port
  750. *
  751. * This function is called when @ap is resumed - right after port
  752. * itself is resumed but before any EH action is taken.
  753. *
  754. * LOCKING:
  755. * EH context.
  756. */
  757. void ata_acpi_on_resume(struct ata_port *ap)
  758. {
  759. const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
  760. struct ata_device *dev;
  761. if (ap->acpi_handle && gtm) {
  762. /* _GTM valid */
  763. /* restore timing parameters */
  764. ata_acpi_stm(ap, gtm);
  765. /* _GTF should immediately follow _STM so that it can
  766. * use values set by _STM. Cache _GTF result and
  767. * schedule _GTF.
  768. */
  769. ata_for_each_dev(dev, &ap->link, ALL) {
  770. ata_acpi_clear_gtf(dev);
  771. if (ata_dev_enabled(dev) &&
  772. ata_dev_get_GTF(dev, NULL) >= 0)
  773. dev->flags |= ATA_DFLAG_ACPI_PENDING;
  774. }
  775. } else {
  776. /* SATA _GTF needs to be evaulated after _SDD and
  777. * there's no reason to evaluate IDE _GTF early
  778. * without _STM. Clear cache and schedule _GTF.
  779. */
  780. ata_for_each_dev(dev, &ap->link, ALL) {
  781. ata_acpi_clear_gtf(dev);
  782. if (ata_dev_enabled(dev))
  783. dev->flags |= ATA_DFLAG_ACPI_PENDING;
  784. }
  785. }
  786. }
  787. /**
  788. * ata_acpi_set_state - set the port power state
  789. * @ap: target ATA port
  790. * @state: state, on/off
  791. *
  792. * This function executes the _PS0/_PS3 ACPI method to set the power state.
  793. * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
  794. */
  795. void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
  796. {
  797. struct ata_device *dev;
  798. if (!ap->acpi_handle || (ap->flags & ATA_FLAG_ACPI_SATA))
  799. return;
  800. /* channel first and then drives for power on and vica versa
  801. for power off */
  802. if (state.event == PM_EVENT_ON)
  803. acpi_bus_set_power(ap->acpi_handle, ACPI_STATE_D0);
  804. ata_for_each_dev(dev, &ap->link, ENABLED) {
  805. if (dev->acpi_handle)
  806. acpi_bus_set_power(dev->acpi_handle,
  807. state.event == PM_EVENT_ON ?
  808. ACPI_STATE_D0 : ACPI_STATE_D3);
  809. }
  810. if (state.event != PM_EVENT_ON)
  811. acpi_bus_set_power(ap->acpi_handle, ACPI_STATE_D3);
  812. }
  813. /**
  814. * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
  815. * @dev: target ATA device
  816. *
  817. * This function is called when @dev is about to be configured.
  818. * IDENTIFY data might have been modified after this hook is run.
  819. *
  820. * LOCKING:
  821. * EH context.
  822. *
  823. * RETURNS:
  824. * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
  825. * -errno on failure.
  826. */
  827. int ata_acpi_on_devcfg(struct ata_device *dev)
  828. {
  829. struct ata_port *ap = dev->link->ap;
  830. struct ata_eh_context *ehc = &ap->link.eh_context;
  831. int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
  832. int nr_executed = 0;
  833. int rc;
  834. if (!dev->acpi_handle)
  835. return 0;
  836. /* do we need to do _GTF? */
  837. if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
  838. !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
  839. return 0;
  840. /* do _SDD if SATA */
  841. if (acpi_sata) {
  842. rc = ata_acpi_push_id(dev);
  843. if (rc)
  844. goto acpi_err;
  845. }
  846. /* do _GTF */
  847. rc = ata_acpi_exec_tfs(dev, &nr_executed);
  848. if (rc)
  849. goto acpi_err;
  850. dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
  851. /* refresh IDENTIFY page if any _GTF command has been executed */
  852. if (nr_executed) {
  853. rc = ata_dev_reread_id(dev, 0);
  854. if (rc < 0) {
  855. ata_dev_printk(dev, KERN_ERR, "failed to IDENTIFY "
  856. "after ACPI commands\n");
  857. return rc;
  858. }
  859. }
  860. return 0;
  861. acpi_err:
  862. /* ignore evaluation failure if we can continue safely */
  863. if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
  864. return 0;
  865. /* fail and let EH retry once more for unknown IO errors */
  866. if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
  867. dev->flags |= ATA_DFLAG_ACPI_FAILED;
  868. return rc;
  869. }
  870. ata_dev_printk(dev, KERN_WARNING,
  871. "ACPI: failed the second time, disabled\n");
  872. dev->acpi_handle = NULL;
  873. /* We can safely continue if no _GTF command has been executed
  874. * and port is not frozen.
  875. */
  876. if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
  877. return 0;
  878. return rc;
  879. }
  880. /**
  881. * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
  882. * @dev: target ATA device
  883. *
  884. * This function is called when @dev is about to be disabled.
  885. *
  886. * LOCKING:
  887. * EH context.
  888. */
  889. void ata_acpi_on_disable(struct ata_device *dev)
  890. {
  891. ata_acpi_clear_gtf(dev);
  892. }