libata-acpi.c 25 KB

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