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 <linux/slab.h>
  18. #include <scsi/scsi_device.h>
  19. #include "libata.h"
  20. #include <acpi/acpi_bus.h>
  21. unsigned int ata_acpi_gtf_filter = ATA_ACPI_FILTER_DEFAULT;
  22. module_param_named(acpi_gtf_filter, ata_acpi_gtf_filter, int, 0644);
  23. MODULE_PARM_DESC(acpi_gtf_filter, "filter mask for ACPI _GTF commands, set to filter out (0x1=set xfermode, 0x2=lock/freeze lock, 0x4=DIPM, 0x8=FPDMA non-zero offset, 0x10=FPDMA DMA Setup FIS auto-activate)");
  24. #define NO_PORT_MULT 0xffff
  25. #define SATA_ADR(root, pmp) (((root) << 16) | (pmp))
  26. #define REGS_PER_GTF 7
  27. struct ata_acpi_gtf {
  28. u8 tf[REGS_PER_GTF]; /* regs. 0x1f1 - 0x1f7 */
  29. } __packed;
  30. /*
  31. * Helper - belongs in the PCI layer somewhere eventually
  32. */
  33. static int is_pci_dev(struct device *dev)
  34. {
  35. return (dev->bus == &pci_bus_type);
  36. }
  37. static void ata_acpi_clear_gtf(struct ata_device *dev)
  38. {
  39. kfree(dev->gtf_cache);
  40. dev->gtf_cache = NULL;
  41. }
  42. /**
  43. * ata_acpi_associate_sata_port - associate SATA port with ACPI objects
  44. * @ap: target SATA port
  45. *
  46. * Look up ACPI objects associated with @ap and initialize acpi_handle
  47. * fields of @ap, the port and devices accordingly.
  48. *
  49. * LOCKING:
  50. * EH context.
  51. *
  52. * RETURNS:
  53. * 0 on success, -errno on failure.
  54. */
  55. void ata_acpi_associate_sata_port(struct ata_port *ap)
  56. {
  57. WARN_ON(!(ap->flags & ATA_FLAG_ACPI_SATA));
  58. if (!sata_pmp_attached(ap)) {
  59. u64 adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
  60. ap->link.device->acpi_handle =
  61. acpi_get_child(ap->host->acpi_handle, adr);
  62. } else {
  63. struct ata_link *link;
  64. ap->link.device->acpi_handle = NULL;
  65. ata_for_each_link(link, ap, EDGE) {
  66. u64 adr = SATA_ADR(ap->port_no, link->pmp);
  67. link->device->acpi_handle =
  68. acpi_get_child(ap->host->acpi_handle, adr);
  69. }
  70. }
  71. }
  72. static void ata_acpi_associate_ide_port(struct ata_port *ap)
  73. {
  74. int max_devices, i;
  75. ap->acpi_handle = acpi_get_child(ap->host->acpi_handle, ap->port_no);
  76. if (!ap->acpi_handle)
  77. return;
  78. max_devices = 1;
  79. if (ap->flags & ATA_FLAG_SLAVE_POSS)
  80. max_devices++;
  81. for (i = 0; i < max_devices; i++) {
  82. struct ata_device *dev = &ap->link.device[i];
  83. dev->acpi_handle = acpi_get_child(ap->acpi_handle, i);
  84. }
  85. if (ata_acpi_gtm(ap, &ap->__acpi_init_gtm) == 0)
  86. ap->pflags |= ATA_PFLAG_INIT_GTM_VALID;
  87. }
  88. /* @ap and @dev are the same as ata_acpi_handle_hotplug() */
  89. static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
  90. {
  91. if (dev)
  92. dev->flags |= ATA_DFLAG_DETACH;
  93. else {
  94. struct ata_link *tlink;
  95. struct ata_device *tdev;
  96. ata_for_each_link(tlink, ap, EDGE)
  97. ata_for_each_dev(tdev, tlink, ALL)
  98. tdev->flags |= ATA_DFLAG_DETACH;
  99. }
  100. ata_port_schedule_eh(ap);
  101. }
  102. /**
  103. * ata_acpi_handle_hotplug - ACPI event handler backend
  104. * @ap: ATA port ACPI event occurred
  105. * @dev: ATA device ACPI event occurred (can be NULL)
  106. * @event: ACPI event which occurred
  107. *
  108. * All ACPI bay / device realted events end up in this function. If
  109. * the event is port-wide @dev is NULL. If the event is specific to a
  110. * device, @dev points to it.
  111. *
  112. * Hotplug (as opposed to unplug) notification is always handled as
  113. * port-wide while unplug only kills the target device on device-wide
  114. * event.
  115. *
  116. * LOCKING:
  117. * ACPI notify handler context. May sleep.
  118. */
  119. static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
  120. u32 event)
  121. {
  122. struct ata_eh_info *ehi = &ap->link.eh_info;
  123. int wait = 0;
  124. unsigned long flags;
  125. acpi_handle handle;
  126. if (dev)
  127. handle = dev->acpi_handle;
  128. else
  129. handle = ap->acpi_handle;
  130. spin_lock_irqsave(ap->lock, flags);
  131. /*
  132. * When dock driver calls into the routine, it will always use
  133. * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
  134. * ACPI_NOTIFY_EJECT_REQUEST for remove
  135. */
  136. switch (event) {
  137. case ACPI_NOTIFY_BUS_CHECK:
  138. case ACPI_NOTIFY_DEVICE_CHECK:
  139. ata_ehi_push_desc(ehi, "ACPI event");
  140. ata_ehi_hotplugged(ehi);
  141. ata_port_freeze(ap);
  142. break;
  143. case ACPI_NOTIFY_EJECT_REQUEST:
  144. ata_ehi_push_desc(ehi, "ACPI event");
  145. ata_acpi_detach_device(ap, dev);
  146. wait = 1;
  147. break;
  148. }
  149. spin_unlock_irqrestore(ap->lock, flags);
  150. if (wait)
  151. ata_port_wait_eh(ap);
  152. }
  153. static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data)
  154. {
  155. struct ata_device *dev = data;
  156. ata_acpi_handle_hotplug(dev->link->ap, dev, event);
  157. }
  158. static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data)
  159. {
  160. struct ata_port *ap = data;
  161. ata_acpi_handle_hotplug(ap, NULL, event);
  162. }
  163. static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
  164. u32 event)
  165. {
  166. struct kobject *kobj = NULL;
  167. char event_string[20];
  168. char *envp[] = { event_string, NULL };
  169. if (dev) {
  170. if (dev->sdev)
  171. kobj = &dev->sdev->sdev_gendev.kobj;
  172. } else
  173. kobj = &ap->dev->kobj;
  174. if (kobj) {
  175. snprintf(event_string, 20, "BAY_EVENT=%d", event);
  176. kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
  177. }
  178. }
  179. static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data)
  180. {
  181. ata_acpi_uevent(data, NULL, event);
  182. }
  183. static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data)
  184. {
  185. struct ata_device *dev = data;
  186. ata_acpi_uevent(dev->link->ap, dev, event);
  187. }
  188. static struct acpi_dock_ops ata_acpi_dev_dock_ops = {
  189. .handler = ata_acpi_dev_notify_dock,
  190. .uevent = ata_acpi_dev_uevent,
  191. };
  192. static struct acpi_dock_ops ata_acpi_ap_dock_ops = {
  193. .handler = ata_acpi_ap_notify_dock,
  194. .uevent = ata_acpi_ap_uevent,
  195. };
  196. /**
  197. * ata_acpi_associate - associate ATA host with ACPI objects
  198. * @host: target ATA host
  199. *
  200. * Look up ACPI objects associated with @host and initialize
  201. * acpi_handle fields of @host, its ports and devices accordingly.
  202. *
  203. * LOCKING:
  204. * EH context.
  205. *
  206. * RETURNS:
  207. * 0 on success, -errno on failure.
  208. */
  209. void ata_acpi_associate(struct ata_host *host)
  210. {
  211. int i, j;
  212. if (!is_pci_dev(host->dev) || libata_noacpi)
  213. return;
  214. host->acpi_handle = DEVICE_ACPI_HANDLE(host->dev);
  215. if (!host->acpi_handle)
  216. return;
  217. for (i = 0; i < host->n_ports; i++) {
  218. struct ata_port *ap = host->ports[i];
  219. if (host->ports[0]->flags & ATA_FLAG_ACPI_SATA)
  220. ata_acpi_associate_sata_port(ap);
  221. else
  222. ata_acpi_associate_ide_port(ap);
  223. if (ap->acpi_handle) {
  224. /* we might be on a docking station */
  225. register_hotplug_dock_device(ap->acpi_handle,
  226. &ata_acpi_ap_dock_ops, ap);
  227. }
  228. for (j = 0; j < ata_link_max_devices(&ap->link); j++) {
  229. struct ata_device *dev = &ap->link.device[j];
  230. if (dev->acpi_handle) {
  231. /* we might be on a docking station */
  232. register_hotplug_dock_device(dev->acpi_handle,
  233. &ata_acpi_dev_dock_ops, dev);
  234. }
  235. }
  236. }
  237. }
  238. /**
  239. * ata_acpi_dissociate - dissociate ATA host from ACPI objects
  240. * @host: target ATA host
  241. *
  242. * This function is called during driver detach after the whole host
  243. * is shut down.
  244. *
  245. * LOCKING:
  246. * EH context.
  247. */
  248. void ata_acpi_dissociate(struct ata_host *host)
  249. {
  250. int i;
  251. /* Restore initial _GTM values so that driver which attaches
  252. * afterward can use them too.
  253. */
  254. for (i = 0; i < host->n_ports; i++) {
  255. struct ata_port *ap = host->ports[i];
  256. const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
  257. if (ap->acpi_handle && gtm)
  258. ata_acpi_stm(ap, gtm);
  259. }
  260. }
  261. /**
  262. * ata_acpi_gtm - execute _GTM
  263. * @ap: target ATA port
  264. * @gtm: out parameter for _GTM result
  265. *
  266. * Evaluate _GTM and store the result in @gtm.
  267. *
  268. * LOCKING:
  269. * EH context.
  270. *
  271. * RETURNS:
  272. * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
  273. */
  274. int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
  275. {
  276. struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
  277. union acpi_object *out_obj;
  278. acpi_status status;
  279. int rc = 0;
  280. status = acpi_evaluate_object(ap->acpi_handle, "_GTM", NULL, &output);
  281. rc = -ENOENT;
  282. if (status == AE_NOT_FOUND)
  283. goto out_free;
  284. rc = -EINVAL;
  285. if (ACPI_FAILURE(status)) {
  286. ata_port_printk(ap, KERN_ERR,
  287. "ACPI get timing mode failed (AE 0x%x)\n",
  288. status);
  289. goto out_free;
  290. }
  291. out_obj = output.pointer;
  292. if (out_obj->type != ACPI_TYPE_BUFFER) {
  293. ata_port_printk(ap, KERN_WARNING,
  294. "_GTM returned unexpected object type 0x%x\n",
  295. out_obj->type);
  296. goto out_free;
  297. }
  298. if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
  299. ata_port_printk(ap, KERN_ERR,
  300. "_GTM returned invalid length %d\n",
  301. out_obj->buffer.length);
  302. goto out_free;
  303. }
  304. memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
  305. rc = 0;
  306. out_free:
  307. kfree(output.pointer);
  308. return rc;
  309. }
  310. EXPORT_SYMBOL_GPL(ata_acpi_gtm);
  311. /**
  312. * ata_acpi_stm - execute _STM
  313. * @ap: target ATA port
  314. * @stm: timing parameter to _STM
  315. *
  316. * Evaluate _STM with timing parameter @stm.
  317. *
  318. * LOCKING:
  319. * EH context.
  320. *
  321. * RETURNS:
  322. * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
  323. */
  324. int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
  325. {
  326. acpi_status status;
  327. struct ata_acpi_gtm stm_buf = *stm;
  328. struct acpi_object_list input;
  329. union acpi_object in_params[3];
  330. in_params[0].type = ACPI_TYPE_BUFFER;
  331. in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
  332. in_params[0].buffer.pointer = (u8 *)&stm_buf;
  333. /* Buffers for id may need byteswapping ? */
  334. in_params[1].type = ACPI_TYPE_BUFFER;
  335. in_params[1].buffer.length = 512;
  336. in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
  337. in_params[2].type = ACPI_TYPE_BUFFER;
  338. in_params[2].buffer.length = 512;
  339. in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
  340. input.count = 3;
  341. input.pointer = in_params;
  342. status = acpi_evaluate_object(ap->acpi_handle, "_STM", &input, NULL);
  343. if (status == AE_NOT_FOUND)
  344. return -ENOENT;
  345. if (ACPI_FAILURE(status)) {
  346. ata_port_printk(ap, KERN_ERR,
  347. "ACPI set timing mode failed (status=0x%x)\n", status);
  348. return -EINVAL;
  349. }
  350. return 0;
  351. }
  352. EXPORT_SYMBOL_GPL(ata_acpi_stm);
  353. /**
  354. * ata_dev_get_GTF - get the drive bootup default taskfile settings
  355. * @dev: target ATA device
  356. * @gtf: output parameter for buffer containing _GTF taskfile arrays
  357. *
  358. * This applies to both PATA and SATA drives.
  359. *
  360. * The _GTF method has no input parameters.
  361. * It returns a variable number of register set values (registers
  362. * hex 1F1..1F7, taskfiles).
  363. * The <variable number> is not known in advance, so have ACPI-CA
  364. * allocate the buffer as needed and return it, then free it later.
  365. *
  366. * LOCKING:
  367. * EH context.
  368. *
  369. * RETURNS:
  370. * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
  371. * if _GTF is invalid.
  372. */
  373. static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
  374. {
  375. struct ata_port *ap = dev->link->ap;
  376. acpi_status status;
  377. struct acpi_buffer output;
  378. union acpi_object *out_obj;
  379. int rc = 0;
  380. /* if _GTF is cached, use the cached value */
  381. if (dev->gtf_cache) {
  382. out_obj = dev->gtf_cache;
  383. goto done;
  384. }
  385. /* set up output buffer */
  386. output.length = ACPI_ALLOCATE_BUFFER;
  387. output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
  388. if (ata_msg_probe(ap))
  389. ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER: port#: %d\n",
  390. __func__, ap->port_no);
  391. /* _GTF has no input parameters */
  392. status = acpi_evaluate_object(dev->acpi_handle, "_GTF", NULL, &output);
  393. out_obj = dev->gtf_cache = output.pointer;
  394. if (ACPI_FAILURE(status)) {
  395. if (status != AE_NOT_FOUND) {
  396. ata_dev_printk(dev, KERN_WARNING,
  397. "_GTF evaluation failed (AE 0x%x)\n",
  398. status);
  399. rc = -EINVAL;
  400. }
  401. goto out_free;
  402. }
  403. if (!output.length || !output.pointer) {
  404. if (ata_msg_probe(ap))
  405. ata_dev_printk(dev, KERN_DEBUG, "%s: Run _GTF: "
  406. "length or ptr is NULL (0x%llx, 0x%p)\n",
  407. __func__,
  408. (unsigned long long)output.length,
  409. output.pointer);
  410. rc = -EINVAL;
  411. goto out_free;
  412. }
  413. if (out_obj->type != ACPI_TYPE_BUFFER) {
  414. ata_dev_printk(dev, KERN_WARNING,
  415. "_GTF unexpected object type 0x%x\n",
  416. out_obj->type);
  417. rc = -EINVAL;
  418. goto out_free;
  419. }
  420. if (out_obj->buffer.length % REGS_PER_GTF) {
  421. ata_dev_printk(dev, KERN_WARNING,
  422. "unexpected _GTF length (%d)\n",
  423. out_obj->buffer.length);
  424. rc = -EINVAL;
  425. goto out_free;
  426. }
  427. done:
  428. rc = out_obj->buffer.length / REGS_PER_GTF;
  429. if (gtf) {
  430. *gtf = (void *)out_obj->buffer.pointer;
  431. if (ata_msg_probe(ap))
  432. ata_dev_printk(dev, KERN_DEBUG,
  433. "%s: returning gtf=%p, gtf_count=%d\n",
  434. __func__, *gtf, rc);
  435. }
  436. return rc;
  437. out_free:
  438. ata_acpi_clear_gtf(dev);
  439. return rc;
  440. }
  441. /**
  442. * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
  443. * @dev: target device
  444. * @gtm: GTM parameter to use
  445. *
  446. * Determine xfermask for @dev from @gtm.
  447. *
  448. * LOCKING:
  449. * None.
  450. *
  451. * RETURNS:
  452. * Determined xfermask.
  453. */
  454. unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
  455. const struct ata_acpi_gtm *gtm)
  456. {
  457. unsigned long xfer_mask = 0;
  458. unsigned int type;
  459. int unit;
  460. u8 mode;
  461. /* we always use the 0 slot for crap hardware */
  462. unit = dev->devno;
  463. if (!(gtm->flags & 0x10))
  464. unit = 0;
  465. /* PIO */
  466. mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
  467. xfer_mask |= ata_xfer_mode2mask(mode);
  468. /* See if we have MWDMA or UDMA data. We don't bother with
  469. * MWDMA if UDMA is available as this means the BIOS set UDMA
  470. * and our error changedown if it works is UDMA to PIO anyway.
  471. */
  472. if (!(gtm->flags & (1 << (2 * unit))))
  473. type = ATA_SHIFT_MWDMA;
  474. else
  475. type = ATA_SHIFT_UDMA;
  476. mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
  477. xfer_mask |= ata_xfer_mode2mask(mode);
  478. return xfer_mask;
  479. }
  480. EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
  481. /**
  482. * ata_acpi_cbl_80wire - Check for 80 wire cable
  483. * @ap: Port to check
  484. * @gtm: GTM data to use
  485. *
  486. * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
  487. */
  488. int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
  489. {
  490. struct ata_device *dev;
  491. ata_for_each_dev(dev, &ap->link, ENABLED) {
  492. unsigned long xfer_mask, udma_mask;
  493. xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
  494. ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
  495. if (udma_mask & ~ATA_UDMA_MASK_40C)
  496. return 1;
  497. }
  498. return 0;
  499. }
  500. EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
  501. static void ata_acpi_gtf_to_tf(struct ata_device *dev,
  502. const struct ata_acpi_gtf *gtf,
  503. struct ata_taskfile *tf)
  504. {
  505. ata_tf_init(dev, tf);
  506. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  507. tf->protocol = ATA_PROT_NODATA;
  508. tf->feature = gtf->tf[0]; /* 0x1f1 */
  509. tf->nsect = gtf->tf[1]; /* 0x1f2 */
  510. tf->lbal = gtf->tf[2]; /* 0x1f3 */
  511. tf->lbam = gtf->tf[3]; /* 0x1f4 */
  512. tf->lbah = gtf->tf[4]; /* 0x1f5 */
  513. tf->device = gtf->tf[5]; /* 0x1f6 */
  514. tf->command = gtf->tf[6]; /* 0x1f7 */
  515. }
  516. static int ata_acpi_filter_tf(struct ata_device *dev,
  517. const struct ata_taskfile *tf,
  518. const struct ata_taskfile *ptf)
  519. {
  520. if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
  521. /* libata doesn't use ACPI to configure transfer mode.
  522. * It will only confuse device configuration. Skip.
  523. */
  524. if (tf->command == ATA_CMD_SET_FEATURES &&
  525. tf->feature == SETFEATURES_XFER)
  526. return 1;
  527. }
  528. if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
  529. /* BIOS writers, sorry but we don't wanna lock
  530. * features unless the user explicitly said so.
  531. */
  532. /* DEVICE CONFIGURATION FREEZE LOCK */
  533. if (tf->command == ATA_CMD_CONF_OVERLAY &&
  534. tf->feature == ATA_DCO_FREEZE_LOCK)
  535. return 1;
  536. /* SECURITY FREEZE LOCK */
  537. if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
  538. return 1;
  539. /* SET MAX LOCK and SET MAX FREEZE LOCK */
  540. if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
  541. tf->command == ATA_CMD_SET_MAX &&
  542. (tf->feature == ATA_SET_MAX_LOCK ||
  543. tf->feature == ATA_SET_MAX_FREEZE_LOCK))
  544. return 1;
  545. }
  546. if (tf->command == ATA_CMD_SET_FEATURES &&
  547. tf->feature == SETFEATURES_SATA_ENABLE) {
  548. /* inhibit enabling DIPM */
  549. if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
  550. tf->nsect == SATA_DIPM)
  551. return 1;
  552. /* inhibit FPDMA non-zero offset */
  553. if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
  554. (tf->nsect == SATA_FPDMA_OFFSET ||
  555. tf->nsect == SATA_FPDMA_IN_ORDER))
  556. return 1;
  557. /* inhibit FPDMA auto activation */
  558. if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
  559. tf->nsect == SATA_FPDMA_AA)
  560. return 1;
  561. }
  562. return 0;
  563. }
  564. /**
  565. * ata_acpi_run_tf - send taskfile registers to host controller
  566. * @dev: target ATA device
  567. * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
  568. *
  569. * Outputs ATA taskfile to standard ATA host controller using MMIO
  570. * or PIO as indicated by the ATA_FLAG_MMIO flag.
  571. * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
  572. * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
  573. * hob_lbal, hob_lbam, and hob_lbah.
  574. *
  575. * This function waits for idle (!BUSY and !DRQ) after writing
  576. * registers. If the control register has a new value, this
  577. * function also waits for idle after writing control and before
  578. * writing the remaining registers.
  579. *
  580. * LOCKING:
  581. * EH context.
  582. *
  583. * RETURNS:
  584. * 1 if command is executed successfully. 0 if ignored, rejected or
  585. * filtered out, -errno on other errors.
  586. */
  587. static int ata_acpi_run_tf(struct ata_device *dev,
  588. const struct ata_acpi_gtf *gtf,
  589. const struct ata_acpi_gtf *prev_gtf)
  590. {
  591. struct ata_taskfile *pptf = NULL;
  592. struct ata_taskfile tf, ptf, rtf;
  593. unsigned int err_mask;
  594. const char *level;
  595. const char *descr;
  596. char msg[60];
  597. int rc;
  598. if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
  599. && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
  600. && (gtf->tf[6] == 0))
  601. return 0;
  602. ata_acpi_gtf_to_tf(dev, gtf, &tf);
  603. if (prev_gtf) {
  604. ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
  605. pptf = &ptf;
  606. }
  607. if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
  608. rtf = tf;
  609. err_mask = ata_exec_internal(dev, &rtf, NULL,
  610. DMA_NONE, NULL, 0, 0);
  611. switch (err_mask) {
  612. case 0:
  613. level = KERN_DEBUG;
  614. snprintf(msg, sizeof(msg), "succeeded");
  615. rc = 1;
  616. break;
  617. case AC_ERR_DEV:
  618. level = KERN_INFO;
  619. snprintf(msg, sizeof(msg),
  620. "rejected by device (Stat=0x%02x Err=0x%02x)",
  621. rtf.command, rtf.feature);
  622. rc = 0;
  623. break;
  624. default:
  625. level = KERN_ERR;
  626. snprintf(msg, sizeof(msg),
  627. "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
  628. err_mask, rtf.command, rtf.feature);
  629. rc = -EIO;
  630. break;
  631. }
  632. } else {
  633. level = KERN_INFO;
  634. snprintf(msg, sizeof(msg), "filtered out");
  635. rc = 0;
  636. }
  637. descr = ata_get_cmd_descript(tf.command);
  638. ata_dev_printk(dev, level,
  639. "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
  640. tf.command, tf.feature, tf.nsect, tf.lbal,
  641. tf.lbam, tf.lbah, tf.device,
  642. (descr ? descr : "unknown"), 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 parameter for the number of executed commands
  649. *
  650. * Evaluate _GTF and execute 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, -ENOENT if _SDD doesn't exist, -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. acpi_status status;
  703. struct acpi_object_list input;
  704. union acpi_object in_params[1];
  705. if (ata_msg_probe(ap))
  706. ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
  707. __func__, dev->devno, ap->port_no);
  708. /* Give the drive Identify data to the drive via the _SDD method */
  709. /* _SDD: set up input parameters */
  710. input.count = 1;
  711. input.pointer = in_params;
  712. in_params[0].type = ACPI_TYPE_BUFFER;
  713. in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
  714. in_params[0].buffer.pointer = (u8 *)dev->id;
  715. /* Output buffer: _SDD has no output */
  716. /* It's OK for _SDD to be missing too. */
  717. swap_buf_le16(dev->id, ATA_ID_WORDS);
  718. status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
  719. swap_buf_le16(dev->id, ATA_ID_WORDS);
  720. if (status == AE_NOT_FOUND)
  721. return -ENOENT;
  722. if (ACPI_FAILURE(status)) {
  723. ata_dev_printk(dev, KERN_WARNING,
  724. "ACPI _SDD failed (AE 0x%x)\n", status);
  725. return -EIO;
  726. }
  727. return 0;
  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_for_each_dev(dev, &ap->link, ALL) {
  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_for_each_dev(dev, &ap->link, ALL) {
  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_for_each_dev(dev, &ap->link, ENABLED) {
  807. if (dev->acpi_handle)
  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 && rc != -ENOENT)
  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. }