libata-acpi.c 26 KB

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