libata-acpi.c 25 KB

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