libata-acpi.c 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055
  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. struct ata_port *ap = dev->link->ap;
  349. acpi_status status;
  350. struct acpi_buffer output;
  351. union acpi_object *out_obj;
  352. int rc = 0;
  353. /* if _GTF is cached, use the cached value */
  354. if (dev->gtf_cache) {
  355. out_obj = dev->gtf_cache;
  356. goto done;
  357. }
  358. /* set up output buffer */
  359. output.length = ACPI_ALLOCATE_BUFFER;
  360. output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
  361. if (ata_msg_probe(ap))
  362. ata_dev_dbg(dev, "%s: ENTER: port#: %d\n",
  363. __func__, ap->port_no);
  364. /* _GTF has no input parameters */
  365. status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
  366. &output);
  367. out_obj = dev->gtf_cache = output.pointer;
  368. if (ACPI_FAILURE(status)) {
  369. if (status != AE_NOT_FOUND) {
  370. ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
  371. status);
  372. rc = -EINVAL;
  373. }
  374. goto out_free;
  375. }
  376. if (!output.length || !output.pointer) {
  377. if (ata_msg_probe(ap))
  378. ata_dev_dbg(dev, "%s: Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
  379. __func__,
  380. (unsigned long long)output.length,
  381. output.pointer);
  382. rc = -EINVAL;
  383. goto out_free;
  384. }
  385. if (out_obj->type != ACPI_TYPE_BUFFER) {
  386. ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
  387. out_obj->type);
  388. rc = -EINVAL;
  389. goto out_free;
  390. }
  391. if (out_obj->buffer.length % REGS_PER_GTF) {
  392. ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
  393. out_obj->buffer.length);
  394. rc = -EINVAL;
  395. goto out_free;
  396. }
  397. done:
  398. rc = out_obj->buffer.length / REGS_PER_GTF;
  399. if (gtf) {
  400. *gtf = (void *)out_obj->buffer.pointer;
  401. if (ata_msg_probe(ap))
  402. ata_dev_dbg(dev, "%s: returning gtf=%p, gtf_count=%d\n",
  403. __func__, *gtf, rc);
  404. }
  405. return rc;
  406. out_free:
  407. ata_acpi_clear_gtf(dev);
  408. return rc;
  409. }
  410. /**
  411. * ata_acpi_gtm_xfermode - determine xfermode from GTM parameter
  412. * @dev: target device
  413. * @gtm: GTM parameter to use
  414. *
  415. * Determine xfermask for @dev from @gtm.
  416. *
  417. * LOCKING:
  418. * None.
  419. *
  420. * RETURNS:
  421. * Determined xfermask.
  422. */
  423. unsigned long ata_acpi_gtm_xfermask(struct ata_device *dev,
  424. const struct ata_acpi_gtm *gtm)
  425. {
  426. unsigned long xfer_mask = 0;
  427. unsigned int type;
  428. int unit;
  429. u8 mode;
  430. /* we always use the 0 slot for crap hardware */
  431. unit = dev->devno;
  432. if (!(gtm->flags & 0x10))
  433. unit = 0;
  434. /* PIO */
  435. mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
  436. xfer_mask |= ata_xfer_mode2mask(mode);
  437. /* See if we have MWDMA or UDMA data. We don't bother with
  438. * MWDMA if UDMA is available as this means the BIOS set UDMA
  439. * and our error changedown if it works is UDMA to PIO anyway.
  440. */
  441. if (!(gtm->flags & (1 << (2 * unit))))
  442. type = ATA_SHIFT_MWDMA;
  443. else
  444. type = ATA_SHIFT_UDMA;
  445. mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
  446. xfer_mask |= ata_xfer_mode2mask(mode);
  447. return xfer_mask;
  448. }
  449. EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
  450. /**
  451. * ata_acpi_cbl_80wire - Check for 80 wire cable
  452. * @ap: Port to check
  453. * @gtm: GTM data to use
  454. *
  455. * Return 1 if the @gtm indicates the BIOS selected an 80wire mode.
  456. */
  457. int ata_acpi_cbl_80wire(struct ata_port *ap, const struct ata_acpi_gtm *gtm)
  458. {
  459. struct ata_device *dev;
  460. ata_for_each_dev(dev, &ap->link, ENABLED) {
  461. unsigned long xfer_mask, udma_mask;
  462. xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
  463. ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
  464. if (udma_mask & ~ATA_UDMA_MASK_40C)
  465. return 1;
  466. }
  467. return 0;
  468. }
  469. EXPORT_SYMBOL_GPL(ata_acpi_cbl_80wire);
  470. static void ata_acpi_gtf_to_tf(struct ata_device *dev,
  471. const struct ata_acpi_gtf *gtf,
  472. struct ata_taskfile *tf)
  473. {
  474. ata_tf_init(dev, tf);
  475. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  476. tf->protocol = ATA_PROT_NODATA;
  477. tf->feature = gtf->tf[0]; /* 0x1f1 */
  478. tf->nsect = gtf->tf[1]; /* 0x1f2 */
  479. tf->lbal = gtf->tf[2]; /* 0x1f3 */
  480. tf->lbam = gtf->tf[3]; /* 0x1f4 */
  481. tf->lbah = gtf->tf[4]; /* 0x1f5 */
  482. tf->device = gtf->tf[5]; /* 0x1f6 */
  483. tf->command = gtf->tf[6]; /* 0x1f7 */
  484. }
  485. static int ata_acpi_filter_tf(struct ata_device *dev,
  486. const struct ata_taskfile *tf,
  487. const struct ata_taskfile *ptf)
  488. {
  489. if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
  490. /* libata doesn't use ACPI to configure transfer mode.
  491. * It will only confuse device configuration. Skip.
  492. */
  493. if (tf->command == ATA_CMD_SET_FEATURES &&
  494. tf->feature == SETFEATURES_XFER)
  495. return 1;
  496. }
  497. if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
  498. /* BIOS writers, sorry but we don't wanna lock
  499. * features unless the user explicitly said so.
  500. */
  501. /* DEVICE CONFIGURATION FREEZE LOCK */
  502. if (tf->command == ATA_CMD_CONF_OVERLAY &&
  503. tf->feature == ATA_DCO_FREEZE_LOCK)
  504. return 1;
  505. /* SECURITY FREEZE LOCK */
  506. if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
  507. return 1;
  508. /* SET MAX LOCK and SET MAX FREEZE LOCK */
  509. if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
  510. tf->command == ATA_CMD_SET_MAX &&
  511. (tf->feature == ATA_SET_MAX_LOCK ||
  512. tf->feature == ATA_SET_MAX_FREEZE_LOCK))
  513. return 1;
  514. }
  515. if (tf->command == ATA_CMD_SET_FEATURES &&
  516. tf->feature == SETFEATURES_SATA_ENABLE) {
  517. /* inhibit enabling DIPM */
  518. if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
  519. tf->nsect == SATA_DIPM)
  520. return 1;
  521. /* inhibit FPDMA non-zero offset */
  522. if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
  523. (tf->nsect == SATA_FPDMA_OFFSET ||
  524. tf->nsect == SATA_FPDMA_IN_ORDER))
  525. return 1;
  526. /* inhibit FPDMA auto activation */
  527. if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
  528. tf->nsect == SATA_FPDMA_AA)
  529. return 1;
  530. }
  531. return 0;
  532. }
  533. /**
  534. * ata_acpi_run_tf - send taskfile registers to host controller
  535. * @dev: target ATA device
  536. * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
  537. *
  538. * Outputs ATA taskfile to standard ATA host controller.
  539. * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
  540. * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
  541. * hob_lbal, hob_lbam, and hob_lbah.
  542. *
  543. * This function waits for idle (!BUSY and !DRQ) after writing
  544. * registers. If the control register has a new value, this
  545. * function also waits for idle after writing control and before
  546. * writing the remaining registers.
  547. *
  548. * LOCKING:
  549. * EH context.
  550. *
  551. * RETURNS:
  552. * 1 if command is executed successfully. 0 if ignored, rejected or
  553. * filtered out, -errno on other errors.
  554. */
  555. static int ata_acpi_run_tf(struct ata_device *dev,
  556. const struct ata_acpi_gtf *gtf,
  557. const struct ata_acpi_gtf *prev_gtf)
  558. {
  559. struct ata_taskfile *pptf = NULL;
  560. struct ata_taskfile tf, ptf, rtf;
  561. unsigned int err_mask;
  562. const char *level;
  563. const char *descr;
  564. char msg[60];
  565. int rc;
  566. if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
  567. && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
  568. && (gtf->tf[6] == 0))
  569. return 0;
  570. ata_acpi_gtf_to_tf(dev, gtf, &tf);
  571. if (prev_gtf) {
  572. ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
  573. pptf = &ptf;
  574. }
  575. if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
  576. rtf = tf;
  577. err_mask = ata_exec_internal(dev, &rtf, NULL,
  578. DMA_NONE, NULL, 0, 0);
  579. switch (err_mask) {
  580. case 0:
  581. level = KERN_DEBUG;
  582. snprintf(msg, sizeof(msg), "succeeded");
  583. rc = 1;
  584. break;
  585. case AC_ERR_DEV:
  586. level = KERN_INFO;
  587. snprintf(msg, sizeof(msg),
  588. "rejected by device (Stat=0x%02x Err=0x%02x)",
  589. rtf.command, rtf.feature);
  590. rc = 0;
  591. break;
  592. default:
  593. level = KERN_ERR;
  594. snprintf(msg, sizeof(msg),
  595. "failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
  596. err_mask, rtf.command, rtf.feature);
  597. rc = -EIO;
  598. break;
  599. }
  600. } else {
  601. level = KERN_INFO;
  602. snprintf(msg, sizeof(msg), "filtered out");
  603. rc = 0;
  604. }
  605. descr = ata_get_cmd_descript(tf.command);
  606. ata_dev_printk(dev, level,
  607. "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x (%s) %s\n",
  608. tf.command, tf.feature, tf.nsect, tf.lbal,
  609. tf.lbam, tf.lbah, tf.device,
  610. (descr ? descr : "unknown"), msg);
  611. return rc;
  612. }
  613. /**
  614. * ata_acpi_exec_tfs - get then write drive taskfile settings
  615. * @dev: target ATA device
  616. * @nr_executed: out parameter for the number of executed commands
  617. *
  618. * Evaluate _GTF and execute returned taskfiles.
  619. *
  620. * LOCKING:
  621. * EH context.
  622. *
  623. * RETURNS:
  624. * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
  625. * -errno on other errors.
  626. */
  627. static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
  628. {
  629. struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
  630. int gtf_count, i, rc;
  631. /* get taskfiles */
  632. rc = ata_dev_get_GTF(dev, &gtf);
  633. if (rc < 0)
  634. return rc;
  635. gtf_count = rc;
  636. /* execute them */
  637. for (i = 0; i < gtf_count; i++, gtf++) {
  638. rc = ata_acpi_run_tf(dev, gtf, pgtf);
  639. if (rc < 0)
  640. break;
  641. if (rc) {
  642. (*nr_executed)++;
  643. pgtf = gtf;
  644. }
  645. }
  646. ata_acpi_clear_gtf(dev);
  647. if (rc < 0)
  648. return rc;
  649. return 0;
  650. }
  651. /**
  652. * ata_acpi_push_id - send Identify data to drive
  653. * @dev: target ATA device
  654. *
  655. * _SDD ACPI object: for SATA mode only
  656. * Must be after Identify (Packet) Device -- uses its data
  657. * ATM this function never returns a failure. It is an optional
  658. * method and if it fails for whatever reason, we should still
  659. * just keep going.
  660. *
  661. * LOCKING:
  662. * EH context.
  663. *
  664. * RETURNS:
  665. * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
  666. */
  667. static int ata_acpi_push_id(struct ata_device *dev)
  668. {
  669. struct ata_port *ap = dev->link->ap;
  670. acpi_status status;
  671. struct acpi_object_list input;
  672. union acpi_object in_params[1];
  673. if (ata_msg_probe(ap))
  674. ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
  675. __func__, dev->devno, ap->port_no);
  676. /* Give the drive Identify data to the drive via the _SDD method */
  677. /* _SDD: set up input parameters */
  678. input.count = 1;
  679. input.pointer = in_params;
  680. in_params[0].type = ACPI_TYPE_BUFFER;
  681. in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
  682. in_params[0].buffer.pointer = (u8 *)dev->id;
  683. /* Output buffer: _SDD has no output */
  684. /* It's OK for _SDD to be missing too. */
  685. swap_buf_le16(dev->id, ATA_ID_WORDS);
  686. status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
  687. NULL);
  688. swap_buf_le16(dev->id, ATA_ID_WORDS);
  689. if (status == AE_NOT_FOUND)
  690. return -ENOENT;
  691. if (ACPI_FAILURE(status)) {
  692. ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
  693. return -EIO;
  694. }
  695. return 0;
  696. }
  697. /**
  698. * ata_acpi_on_suspend - ATA ACPI hook called on suspend
  699. * @ap: target ATA port
  700. *
  701. * This function is called when @ap is about to be suspended. All
  702. * devices are already put to sleep but the port_suspend() callback
  703. * hasn't been executed yet. Error return from this function aborts
  704. * suspend.
  705. *
  706. * LOCKING:
  707. * EH context.
  708. *
  709. * RETURNS:
  710. * 0 on success, -errno on failure.
  711. */
  712. int ata_acpi_on_suspend(struct ata_port *ap)
  713. {
  714. /* nada */
  715. return 0;
  716. }
  717. /**
  718. * ata_acpi_on_resume - ATA ACPI hook called on resume
  719. * @ap: target ATA port
  720. *
  721. * This function is called when @ap is resumed - right after port
  722. * itself is resumed but before any EH action is taken.
  723. *
  724. * LOCKING:
  725. * EH context.
  726. */
  727. void ata_acpi_on_resume(struct ata_port *ap)
  728. {
  729. const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
  730. struct ata_device *dev;
  731. if (ACPI_HANDLE(&ap->tdev) && gtm) {
  732. /* _GTM valid */
  733. /* restore timing parameters */
  734. ata_acpi_stm(ap, gtm);
  735. /* _GTF should immediately follow _STM so that it can
  736. * use values set by _STM. Cache _GTF result and
  737. * schedule _GTF.
  738. */
  739. ata_for_each_dev(dev, &ap->link, ALL) {
  740. ata_acpi_clear_gtf(dev);
  741. if (ata_dev_enabled(dev) &&
  742. ata_dev_acpi_handle(dev) &&
  743. ata_dev_get_GTF(dev, NULL) >= 0)
  744. dev->flags |= ATA_DFLAG_ACPI_PENDING;
  745. }
  746. } else {
  747. /* SATA _GTF needs to be evaulated after _SDD and
  748. * there's no reason to evaluate IDE _GTF early
  749. * without _STM. Clear cache and schedule _GTF.
  750. */
  751. ata_for_each_dev(dev, &ap->link, ALL) {
  752. ata_acpi_clear_gtf(dev);
  753. if (ata_dev_enabled(dev))
  754. dev->flags |= ATA_DFLAG_ACPI_PENDING;
  755. }
  756. }
  757. }
  758. static int ata_acpi_choose_suspend_state(struct ata_device *dev, bool runtime)
  759. {
  760. int d_max_in = ACPI_STATE_D3_COLD;
  761. if (!runtime)
  762. goto out;
  763. /*
  764. * For ATAPI, runtime D3 cold is only allowed
  765. * for ZPODD in zero power ready state
  766. */
  767. if (dev->class == ATA_DEV_ATAPI &&
  768. !(zpodd_dev_enabled(dev) && zpodd_zpready(dev)))
  769. d_max_in = ACPI_STATE_D3_HOT;
  770. out:
  771. return acpi_pm_device_sleep_state(&dev->tdev, NULL, d_max_in);
  772. }
  773. static void sata_acpi_set_state(struct ata_port *ap, pm_message_t state)
  774. {
  775. bool runtime = PMSG_IS_AUTO(state);
  776. struct ata_device *dev;
  777. acpi_handle handle;
  778. int acpi_state;
  779. ata_for_each_dev(dev, &ap->link, ENABLED) {
  780. handle = ata_dev_acpi_handle(dev);
  781. if (!handle)
  782. continue;
  783. if (!(state.event & PM_EVENT_RESUME)) {
  784. acpi_state = ata_acpi_choose_suspend_state(dev, runtime);
  785. if (acpi_state == ACPI_STATE_D0)
  786. continue;
  787. if (runtime && zpodd_dev_enabled(dev) &&
  788. acpi_state == ACPI_STATE_D3_COLD)
  789. zpodd_enable_run_wake(dev);
  790. acpi_bus_set_power(handle, acpi_state);
  791. } else {
  792. if (runtime && zpodd_dev_enabled(dev))
  793. zpodd_disable_run_wake(dev);
  794. acpi_bus_set_power(handle, ACPI_STATE_D0);
  795. }
  796. }
  797. }
  798. /* ACPI spec requires _PS0 when IDE power on and _PS3 when power off */
  799. static void pata_acpi_set_state(struct ata_port *ap, pm_message_t state)
  800. {
  801. struct ata_device *dev;
  802. acpi_handle port_handle;
  803. port_handle = ACPI_HANDLE(&ap->tdev);
  804. if (!port_handle)
  805. return;
  806. /* channel first and then drives for power on and vica versa
  807. for power off */
  808. if (state.event & PM_EVENT_RESUME)
  809. acpi_bus_set_power(port_handle, ACPI_STATE_D0);
  810. ata_for_each_dev(dev, &ap->link, ENABLED) {
  811. acpi_handle dev_handle = ata_dev_acpi_handle(dev);
  812. if (!dev_handle)
  813. continue;
  814. acpi_bus_set_power(dev_handle, state.event & PM_EVENT_RESUME ?
  815. ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
  816. }
  817. if (!(state.event & PM_EVENT_RESUME))
  818. acpi_bus_set_power(port_handle, ACPI_STATE_D3_COLD);
  819. }
  820. /**
  821. * ata_acpi_set_state - set the port power state
  822. * @ap: target ATA port
  823. * @state: state, on/off
  824. *
  825. * This function sets a proper ACPI D state for the device on
  826. * system and runtime PM operations.
  827. */
  828. void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
  829. {
  830. if (ap->flags & ATA_FLAG_ACPI_SATA)
  831. sata_acpi_set_state(ap, state);
  832. else
  833. pata_acpi_set_state(ap, state);
  834. }
  835. /**
  836. * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
  837. * @dev: target ATA device
  838. *
  839. * This function is called when @dev is about to be configured.
  840. * IDENTIFY data might have been modified after this hook is run.
  841. *
  842. * LOCKING:
  843. * EH context.
  844. *
  845. * RETURNS:
  846. * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
  847. * -errno on failure.
  848. */
  849. int ata_acpi_on_devcfg(struct ata_device *dev)
  850. {
  851. struct ata_port *ap = dev->link->ap;
  852. struct ata_eh_context *ehc = &ap->link.eh_context;
  853. int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
  854. int nr_executed = 0;
  855. int rc;
  856. if (!ata_dev_acpi_handle(dev))
  857. return 0;
  858. /* do we need to do _GTF? */
  859. if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
  860. !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
  861. return 0;
  862. /* do _SDD if SATA */
  863. if (acpi_sata) {
  864. rc = ata_acpi_push_id(dev);
  865. if (rc && rc != -ENOENT)
  866. goto acpi_err;
  867. }
  868. /* do _GTF */
  869. rc = ata_acpi_exec_tfs(dev, &nr_executed);
  870. if (rc)
  871. goto acpi_err;
  872. dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
  873. /* refresh IDENTIFY page if any _GTF command has been executed */
  874. if (nr_executed) {
  875. rc = ata_dev_reread_id(dev, 0);
  876. if (rc < 0) {
  877. ata_dev_err(dev,
  878. "failed to IDENTIFY after ACPI commands\n");
  879. return rc;
  880. }
  881. }
  882. return 0;
  883. acpi_err:
  884. /* ignore evaluation failure if we can continue safely */
  885. if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
  886. return 0;
  887. /* fail and let EH retry once more for unknown IO errors */
  888. if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
  889. dev->flags |= ATA_DFLAG_ACPI_FAILED;
  890. return rc;
  891. }
  892. dev->flags |= ATA_DFLAG_ACPI_DISABLED;
  893. ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
  894. /* We can safely continue if no _GTF command has been executed
  895. * and port is not frozen.
  896. */
  897. if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
  898. return 0;
  899. return rc;
  900. }
  901. /**
  902. * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
  903. * @dev: target ATA device
  904. *
  905. * This function is called when @dev is about to be disabled.
  906. *
  907. * LOCKING:
  908. * EH context.
  909. */
  910. void ata_acpi_on_disable(struct ata_device *dev)
  911. {
  912. ata_acpi_clear_gtf(dev);
  913. }