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.
  565. * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
  566. * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
  567. * hob_lbal, hob_lbam, and hob_lbah.
  568. *
  569. * This function waits for idle (!BUSY and !DRQ) after writing
  570. * registers. If the control register has a new value, this
  571. * function also waits for idle after writing control and before
  572. * writing the remaining registers.
  573. *
  574. * LOCKING:
  575. * EH context.
  576. *
  577. * RETURNS:
  578. * 1 if command is executed successfully. 0 if ignored, rejected or
  579. * filtered out, -errno on other errors.
  580. */
  581. static int ata_acpi_run_tf(struct ata_device *dev,
  582. const struct ata_acpi_gtf *gtf,
  583. const struct ata_acpi_gtf *prev_gtf)
  584. {
  585. struct ata_taskfile *pptf = NULL;
  586. struct ata_taskfile tf, ptf, rtf;
  587. unsigned int err_mask;
  588. const char *level;
  589. const char *descr;
  590. char msg[60];
  591. int rc;
  592. if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
  593. && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
  594. && (gtf->tf[6] == 0))
  595. return 0;
  596. ata_acpi_gtf_to_tf(dev, gtf, &tf);
  597. if (prev_gtf) {
  598. ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
  599. pptf = &ptf;
  600. }
  601. if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
  602. rtf = tf;
  603. err_mask = ata_exec_internal(dev, &rtf, NULL,
  604. DMA_NONE, NULL, 0, 0);
  605. switch (err_mask) {
  606. case 0:
  607. level = KERN_DEBUG;
  608. snprintf(msg, sizeof(msg), "succeeded");
  609. rc = 1;
  610. break;
  611. case AC_ERR_DEV:
  612. level = KERN_INFO;
  613. snprintf(msg, sizeof(msg),
  614. "rejected by device (Stat=0x%02x Err=0x%02x)",
  615. rtf.command, rtf.feature);
  616. rc = 0;
  617. break;
  618. default:
  619. level = KERN_ERR;
  620. snprintf(msg, sizeof(msg),
  621. "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
  622. err_mask, rtf.command, rtf.feature);
  623. rc = -EIO;
  624. break;
  625. }
  626. } else {
  627. level = KERN_INFO;
  628. snprintf(msg, sizeof(msg), "filtered out");
  629. rc = 0;
  630. }
  631. descr = ata_get_cmd_descript(tf.command);
  632. ata_dev_printk(dev, level,
  633. "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
  634. tf.command, tf.feature, tf.nsect, tf.lbal,
  635. tf.lbam, tf.lbah, tf.device,
  636. (descr ? descr : "unknown"), 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, -ENOENT if _SDD doesn't exist, -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. acpi_status status;
  697. struct acpi_object_list input;
  698. union acpi_object in_params[1];
  699. if (ata_msg_probe(ap))
  700. ata_dev_printk(dev, KERN_DEBUG, "%s: ix = %d, port#: %d\n",
  701. __func__, dev->devno, ap->port_no);
  702. /* Give the drive Identify data to the drive via the _SDD method */
  703. /* _SDD: set up input parameters */
  704. input.count = 1;
  705. input.pointer = in_params;
  706. in_params[0].type = ACPI_TYPE_BUFFER;
  707. in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
  708. in_params[0].buffer.pointer = (u8 *)dev->id;
  709. /* Output buffer: _SDD has no output */
  710. /* It's OK for _SDD to be missing too. */
  711. swap_buf_le16(dev->id, ATA_ID_WORDS);
  712. status = acpi_evaluate_object(dev->acpi_handle, "_SDD", &input, NULL);
  713. swap_buf_le16(dev->id, ATA_ID_WORDS);
  714. if (status == AE_NOT_FOUND)
  715. return -ENOENT;
  716. if (ACPI_FAILURE(status)) {
  717. ata_dev_printk(dev, KERN_WARNING,
  718. "ACPI _SDD failed (AE 0x%x)\n", status);
  719. return -EIO;
  720. }
  721. return 0;
  722. }
  723. /**
  724. * ata_acpi_on_suspend - ATA ACPI hook called on suspend
  725. * @ap: target ATA port
  726. *
  727. * This function is called when @ap is about to be suspended. All
  728. * devices are already put to sleep but the port_suspend() callback
  729. * hasn't been executed yet. Error return from this function aborts
  730. * suspend.
  731. *
  732. * LOCKING:
  733. * EH context.
  734. *
  735. * RETURNS:
  736. * 0 on success, -errno on failure.
  737. */
  738. int ata_acpi_on_suspend(struct ata_port *ap)
  739. {
  740. /* nada */
  741. return 0;
  742. }
  743. /**
  744. * ata_acpi_on_resume - ATA ACPI hook called on resume
  745. * @ap: target ATA port
  746. *
  747. * This function is called when @ap is resumed - right after port
  748. * itself is resumed but before any EH action is taken.
  749. *
  750. * LOCKING:
  751. * EH context.
  752. */
  753. void ata_acpi_on_resume(struct ata_port *ap)
  754. {
  755. const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
  756. struct ata_device *dev;
  757. if (ap->acpi_handle && gtm) {
  758. /* _GTM valid */
  759. /* restore timing parameters */
  760. ata_acpi_stm(ap, gtm);
  761. /* _GTF should immediately follow _STM so that it can
  762. * use values set by _STM. Cache _GTF result and
  763. * schedule _GTF.
  764. */
  765. ata_for_each_dev(dev, &ap->link, ALL) {
  766. ata_acpi_clear_gtf(dev);
  767. if (ata_dev_enabled(dev) &&
  768. ata_dev_get_GTF(dev, NULL) >= 0)
  769. dev->flags |= ATA_DFLAG_ACPI_PENDING;
  770. }
  771. } else {
  772. /* SATA _GTF needs to be evaulated after _SDD and
  773. * there's no reason to evaluate IDE _GTF early
  774. * without _STM. Clear cache and schedule _GTF.
  775. */
  776. ata_for_each_dev(dev, &ap->link, ALL) {
  777. ata_acpi_clear_gtf(dev);
  778. if (ata_dev_enabled(dev))
  779. dev->flags |= ATA_DFLAG_ACPI_PENDING;
  780. }
  781. }
  782. }
  783. /**
  784. * ata_acpi_set_state - set the port power state
  785. * @ap: target ATA port
  786. * @state: state, on/off
  787. *
  788. * This function executes the _PS0/_PS3 ACPI method to set the power state.
  789. * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
  790. */
  791. void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
  792. {
  793. struct ata_device *dev;
  794. if (!ap->acpi_handle || (ap->flags & ATA_FLAG_ACPI_SATA))
  795. return;
  796. /* channel first and then drives for power on and vica versa
  797. for power off */
  798. if (state.event == PM_EVENT_ON)
  799. acpi_bus_set_power(ap->acpi_handle, ACPI_STATE_D0);
  800. ata_for_each_dev(dev, &ap->link, ENABLED) {
  801. if (dev->acpi_handle)
  802. acpi_bus_set_power(dev->acpi_handle,
  803. state.event == PM_EVENT_ON ?
  804. ACPI_STATE_D0 : ACPI_STATE_D3);
  805. }
  806. if (state.event != PM_EVENT_ON)
  807. acpi_bus_set_power(ap->acpi_handle, ACPI_STATE_D3);
  808. }
  809. /**
  810. * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
  811. * @dev: target ATA device
  812. *
  813. * This function is called when @dev is about to be configured.
  814. * IDENTIFY data might have been modified after this hook is run.
  815. *
  816. * LOCKING:
  817. * EH context.
  818. *
  819. * RETURNS:
  820. * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
  821. * -errno on failure.
  822. */
  823. int ata_acpi_on_devcfg(struct ata_device *dev)
  824. {
  825. struct ata_port *ap = dev->link->ap;
  826. struct ata_eh_context *ehc = &ap->link.eh_context;
  827. int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
  828. int nr_executed = 0;
  829. int rc;
  830. if (!dev->acpi_handle)
  831. return 0;
  832. /* do we need to do _GTF? */
  833. if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
  834. !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
  835. return 0;
  836. /* do _SDD if SATA */
  837. if (acpi_sata) {
  838. rc = ata_acpi_push_id(dev);
  839. if (rc && rc != -ENOENT)
  840. goto acpi_err;
  841. }
  842. /* do _GTF */
  843. rc = ata_acpi_exec_tfs(dev, &nr_executed);
  844. if (rc)
  845. goto acpi_err;
  846. dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
  847. /* refresh IDENTIFY page if any _GTF command has been executed */
  848. if (nr_executed) {
  849. rc = ata_dev_reread_id(dev, 0);
  850. if (rc < 0) {
  851. ata_dev_printk(dev, KERN_ERR, "failed to IDENTIFY "
  852. "after ACPI commands\n");
  853. return rc;
  854. }
  855. }
  856. return 0;
  857. acpi_err:
  858. /* ignore evaluation failure if we can continue safely */
  859. if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
  860. return 0;
  861. /* fail and let EH retry once more for unknown IO errors */
  862. if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
  863. dev->flags |= ATA_DFLAG_ACPI_FAILED;
  864. return rc;
  865. }
  866. ata_dev_printk(dev, KERN_WARNING,
  867. "ACPI: failed the second time, disabled\n");
  868. dev->acpi_handle = NULL;
  869. /* We can safely continue if no _GTF command has been executed
  870. * and port is not frozen.
  871. */
  872. if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
  873. return 0;
  874. return rc;
  875. }
  876. /**
  877. * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
  878. * @dev: target ATA device
  879. *
  880. * This function is called when @dev is about to be disabled.
  881. *
  882. * LOCKING:
  883. * EH context.
  884. */
  885. void ata_acpi_on_disable(struct ata_device *dev)
  886. {
  887. ata_acpi_clear_gtf(dev);
  888. }