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