libata-acpi.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103
  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 related 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_obj(*context);
  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_obj(*context);
  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_dev_manage_restart - if the disk should be stopped (spun down) on
  212. * system restart.
  213. * @dev: target ATA device
  214. *
  215. * RETURNS:
  216. * true if the disk should be stopped, otherwise false.
  217. */
  218. bool ata_acpi_dev_manage_restart(struct ata_device *dev)
  219. {
  220. struct device *tdev;
  221. /*
  222. * If ATA_FLAG_ACPI_SATA is set, the acpi fwnode is attached to the
  223. * ata_device instead of the ata_port.
  224. */
  225. if (dev->link->ap->flags & ATA_FLAG_ACPI_SATA)
  226. tdev = &dev->tdev;
  227. else
  228. tdev = &dev->link->ap->tdev;
  229. if (!is_acpi_device_node(tdev->fwnode))
  230. return false;
  231. return acpi_bus_power_manageable(ACPI_HANDLE(tdev));
  232. }
  233. /**
  234. * ata_acpi_port_power_on - set the power state of the ata port to D0
  235. * @ap: target ATA port
  236. *
  237. * This function is called at the beginning of ata_port_probe().
  238. */
  239. void ata_acpi_port_power_on(struct ata_port *ap)
  240. {
  241. acpi_handle handle;
  242. int i;
  243. /*
  244. * If ATA_FLAG_ACPI_SATA is set, the acpi fwnode is attached to the
  245. * ata_device instead of the ata_port.
  246. */
  247. if (ap->flags & ATA_FLAG_ACPI_SATA) {
  248. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  249. struct ata_device *dev = &ap->link.device[i];
  250. if (!is_acpi_device_node(dev->tdev.fwnode))
  251. continue;
  252. handle = ACPI_HANDLE(&dev->tdev);
  253. if (!acpi_bus_power_manageable(handle))
  254. continue;
  255. if (acpi_bus_set_power(handle, ACPI_STATE_D0))
  256. ata_dev_err(dev,
  257. "acpi: failed to set power state to D0\n");
  258. }
  259. return;
  260. }
  261. if (!is_acpi_device_node(ap->tdev.fwnode))
  262. return;
  263. handle = ACPI_HANDLE(&ap->tdev);
  264. if (!acpi_bus_power_manageable(handle))
  265. return;
  266. if (acpi_bus_set_power(handle, ACPI_STATE_D0))
  267. ata_port_err(ap, "acpi: failed to set power state to D0\n");
  268. }
  269. /**
  270. * ata_acpi_dissociate - dissociate ATA host from ACPI objects
  271. * @host: target ATA host
  272. *
  273. * This function is called during driver detach after the whole host
  274. * is shut down.
  275. *
  276. * LOCKING:
  277. * EH context.
  278. */
  279. void ata_acpi_dissociate(struct ata_host *host)
  280. {
  281. int i;
  282. /* Restore initial _GTM values so that driver which attaches
  283. * afterward can use them too.
  284. */
  285. for (i = 0; i < host->n_ports; i++) {
  286. struct ata_port *ap = host->ports[i];
  287. const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
  288. if (ACPI_HANDLE(&ap->tdev) && gtm)
  289. ata_acpi_stm(ap, gtm);
  290. }
  291. }
  292. /**
  293. * ata_acpi_gtm - execute _GTM
  294. * @ap: target ATA port
  295. * @gtm: out parameter for _GTM result
  296. *
  297. * Evaluate _GTM and store the result in @gtm.
  298. *
  299. * LOCKING:
  300. * EH context.
  301. *
  302. * RETURNS:
  303. * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
  304. */
  305. int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
  306. {
  307. struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
  308. union acpi_object *out_obj;
  309. acpi_status status;
  310. int rc = 0;
  311. acpi_handle handle = ACPI_HANDLE(&ap->tdev);
  312. if (!handle)
  313. return -EINVAL;
  314. status = acpi_evaluate_object(handle, "_GTM", NULL, &output);
  315. rc = -ENOENT;
  316. if (status == AE_NOT_FOUND)
  317. goto out_free;
  318. rc = -EINVAL;
  319. if (ACPI_FAILURE(status)) {
  320. ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
  321. status);
  322. goto out_free;
  323. }
  324. out_obj = output.pointer;
  325. if (out_obj->type != ACPI_TYPE_BUFFER) {
  326. ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
  327. out_obj->type);
  328. goto out_free;
  329. }
  330. if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
  331. ata_port_err(ap, "_GTM returned invalid length %d\n",
  332. out_obj->buffer.length);
  333. goto out_free;
  334. }
  335. memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
  336. rc = 0;
  337. out_free:
  338. kfree(output.pointer);
  339. return rc;
  340. }
  341. EXPORT_SYMBOL_GPL(ata_acpi_gtm);
  342. /**
  343. * ata_acpi_stm - execute _STM
  344. * @ap: target ATA port
  345. * @stm: timing parameter to _STM
  346. *
  347. * Evaluate _STM with timing parameter @stm.
  348. *
  349. * LOCKING:
  350. * EH context.
  351. *
  352. * RETURNS:
  353. * 0 on success, -ENOENT if _STM doesn't exist, -errno on failure.
  354. */
  355. int ata_acpi_stm(struct ata_port *ap, const struct ata_acpi_gtm *stm)
  356. {
  357. acpi_status status;
  358. struct ata_acpi_gtm stm_buf = *stm;
  359. struct acpi_object_list input;
  360. union acpi_object in_params[3];
  361. in_params[0].type = ACPI_TYPE_BUFFER;
  362. in_params[0].buffer.length = sizeof(struct ata_acpi_gtm);
  363. in_params[0].buffer.pointer = (u8 *)&stm_buf;
  364. /* Buffers for id may need byteswapping ? */
  365. in_params[1].type = ACPI_TYPE_BUFFER;
  366. in_params[1].buffer.length = 512;
  367. in_params[1].buffer.pointer = (u8 *)ap->link.device[0].id;
  368. in_params[2].type = ACPI_TYPE_BUFFER;
  369. in_params[2].buffer.length = 512;
  370. in_params[2].buffer.pointer = (u8 *)ap->link.device[1].id;
  371. input.count = 3;
  372. input.pointer = in_params;
  373. status = acpi_evaluate_object(ACPI_HANDLE(&ap->tdev), "_STM",
  374. &input, NULL);
  375. if (status == AE_NOT_FOUND)
  376. return -ENOENT;
  377. if (ACPI_FAILURE(status)) {
  378. ata_port_err(ap, "ACPI set timing mode failed (status=0x%x)\n",
  379. status);
  380. return -EINVAL;
  381. }
  382. return 0;
  383. }
  384. EXPORT_SYMBOL_GPL(ata_acpi_stm);
  385. /**
  386. * ata_dev_get_GTF - get the drive bootup default taskfile settings
  387. * @dev: target ATA device
  388. * @gtf: output parameter for buffer containing _GTF taskfile arrays
  389. *
  390. * This applies to both PATA and SATA drives.
  391. *
  392. * The _GTF method has no input parameters.
  393. * It returns a variable number of register set values (registers
  394. * hex 1F1..1F7, taskfiles).
  395. * The <variable number> is not known in advance, so have ACPI-CA
  396. * allocate the buffer as needed and return it, then free it later.
  397. *
  398. * LOCKING:
  399. * EH context.
  400. *
  401. * RETURNS:
  402. * Number of taskfiles on success, 0 if _GTF doesn't exist. -EINVAL
  403. * if _GTF is invalid.
  404. */
  405. static int ata_dev_get_GTF(struct ata_device *dev, struct ata_acpi_gtf **gtf)
  406. {
  407. acpi_status status;
  408. struct acpi_buffer output;
  409. union acpi_object *out_obj;
  410. int rc = 0;
  411. /* if _GTF is cached, use the cached value */
  412. if (dev->gtf_cache) {
  413. out_obj = dev->gtf_cache;
  414. goto done;
  415. }
  416. /* set up output buffer */
  417. output.length = ACPI_ALLOCATE_BUFFER;
  418. output.pointer = NULL; /* ACPI-CA sets this; save/free it later */
  419. /* _GTF has no input parameters */
  420. status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_GTF", NULL,
  421. &output);
  422. out_obj = dev->gtf_cache = output.pointer;
  423. if (ACPI_FAILURE(status)) {
  424. if (status != AE_NOT_FOUND) {
  425. ata_dev_warn(dev, "_GTF evaluation failed (AE 0x%x)\n",
  426. status);
  427. rc = -EINVAL;
  428. }
  429. goto out_free;
  430. }
  431. if (!output.length || !output.pointer) {
  432. ata_dev_dbg(dev, "Run _GTF: length or ptr is NULL (0x%llx, 0x%p)\n",
  433. (unsigned long long)output.length,
  434. output.pointer);
  435. rc = -EINVAL;
  436. goto out_free;
  437. }
  438. if (out_obj->type != ACPI_TYPE_BUFFER) {
  439. ata_dev_warn(dev, "_GTF unexpected object type 0x%x\n",
  440. out_obj->type);
  441. rc = -EINVAL;
  442. goto out_free;
  443. }
  444. if (out_obj->buffer.length % REGS_PER_GTF) {
  445. ata_dev_warn(dev, "unexpected _GTF length (%d)\n",
  446. out_obj->buffer.length);
  447. rc = -EINVAL;
  448. goto out_free;
  449. }
  450. done:
  451. rc = out_obj->buffer.length / REGS_PER_GTF;
  452. if (gtf) {
  453. *gtf = (void *)out_obj->buffer.pointer;
  454. ata_dev_dbg(dev, "returning gtf=%p, gtf_count=%d\n",
  455. *gtf, rc);
  456. }
  457. return rc;
  458. out_free:
  459. ata_acpi_clear_gtf(dev);
  460. return rc;
  461. }
  462. /**
  463. * ata_acpi_gtm_xfermask - determine xfermode from GTM parameter
  464. * @dev: target device
  465. * @gtm: GTM parameter to use
  466. *
  467. * Determine xfermask for @dev from @gtm.
  468. *
  469. * LOCKING:
  470. * None.
  471. *
  472. * RETURNS:
  473. * Determined xfermask.
  474. */
  475. unsigned int ata_acpi_gtm_xfermask(struct ata_device *dev,
  476. const struct ata_acpi_gtm *gtm)
  477. {
  478. unsigned int xfer_mask = 0;
  479. unsigned int type;
  480. int unit;
  481. u8 mode;
  482. /* we always use the 0 slot for crap hardware */
  483. unit = dev->devno;
  484. if (!(gtm->flags & 0x10))
  485. unit = 0;
  486. /* PIO */
  487. mode = ata_timing_cycle2mode(ATA_SHIFT_PIO, gtm->drive[unit].pio);
  488. xfer_mask |= ata_xfer_mode2mask(mode);
  489. /* See if we have MWDMA or UDMA data. We don't bother with
  490. * MWDMA if UDMA is available as this means the BIOS set UDMA
  491. * and our error changedown if it works is UDMA to PIO anyway.
  492. */
  493. if (!(gtm->flags & (1 << (2 * unit))))
  494. type = ATA_SHIFT_MWDMA;
  495. else
  496. type = ATA_SHIFT_UDMA;
  497. mode = ata_timing_cycle2mode(type, gtm->drive[unit].dma);
  498. xfer_mask |= ata_xfer_mode2mask(mode);
  499. return xfer_mask;
  500. }
  501. EXPORT_SYMBOL_GPL(ata_acpi_gtm_xfermask);
  502. /**
  503. * ata_acpi_cbl_pata_type - Return PATA cable type
  504. * @ap: Port to check
  505. *
  506. * Return ATA_CBL_PATA* according to the transfer mode selected by BIOS
  507. */
  508. int ata_acpi_cbl_pata_type(struct ata_port *ap)
  509. {
  510. struct ata_device *dev;
  511. int ret = ATA_CBL_PATA_UNK;
  512. const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
  513. if (!gtm)
  514. return ATA_CBL_PATA40;
  515. ata_for_each_dev(dev, &ap->link, ENABLED) {
  516. unsigned int xfer_mask, udma_mask;
  517. xfer_mask = ata_acpi_gtm_xfermask(dev, gtm);
  518. ata_unpack_xfermask(xfer_mask, NULL, NULL, &udma_mask);
  519. ret = ATA_CBL_PATA40;
  520. if (udma_mask & ~ATA_UDMA_MASK_40C) {
  521. ret = ATA_CBL_PATA80;
  522. break;
  523. }
  524. }
  525. return ret;
  526. }
  527. EXPORT_SYMBOL_GPL(ata_acpi_cbl_pata_type);
  528. static void ata_acpi_gtf_to_tf(struct ata_device *dev,
  529. const struct ata_acpi_gtf *gtf,
  530. struct ata_taskfile *tf)
  531. {
  532. ata_tf_init(dev, tf);
  533. tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  534. tf->protocol = ATA_PROT_NODATA;
  535. tf->error = gtf->tf[0]; /* 0x1f1 */
  536. tf->nsect = gtf->tf[1]; /* 0x1f2 */
  537. tf->lbal = gtf->tf[2]; /* 0x1f3 */
  538. tf->lbam = gtf->tf[3]; /* 0x1f4 */
  539. tf->lbah = gtf->tf[4]; /* 0x1f5 */
  540. tf->device = gtf->tf[5]; /* 0x1f6 */
  541. tf->status = gtf->tf[6]; /* 0x1f7 */
  542. }
  543. static int ata_acpi_filter_tf(struct ata_device *dev,
  544. const struct ata_taskfile *tf,
  545. const struct ata_taskfile *ptf)
  546. {
  547. if (dev->gtf_filter & ATA_ACPI_FILTER_SETXFER) {
  548. /* libata doesn't use ACPI to configure transfer mode.
  549. * It will only confuse device configuration. Skip.
  550. */
  551. if (tf->command == ATA_CMD_SET_FEATURES &&
  552. tf->feature == SETFEATURES_XFER)
  553. return 1;
  554. }
  555. if (dev->gtf_filter & ATA_ACPI_FILTER_LOCK) {
  556. /* BIOS writers, sorry but we don't wanna lock
  557. * features unless the user explicitly said so.
  558. */
  559. /* DEVICE CONFIGURATION FREEZE LOCK */
  560. if (tf->command == ATA_CMD_CONF_OVERLAY &&
  561. tf->feature == ATA_DCO_FREEZE_LOCK)
  562. return 1;
  563. /* SECURITY FREEZE LOCK */
  564. if (tf->command == ATA_CMD_SEC_FREEZE_LOCK)
  565. return 1;
  566. /* SET MAX LOCK and SET MAX FREEZE LOCK */
  567. if ((!ptf || ptf->command != ATA_CMD_READ_NATIVE_MAX) &&
  568. tf->command == ATA_CMD_SET_MAX &&
  569. (tf->feature == ATA_SET_MAX_LOCK ||
  570. tf->feature == ATA_SET_MAX_FREEZE_LOCK))
  571. return 1;
  572. }
  573. if (tf->command == ATA_CMD_SET_FEATURES &&
  574. tf->feature == SETFEATURES_SATA_ENABLE) {
  575. /* inhibit enabling DIPM */
  576. if (dev->gtf_filter & ATA_ACPI_FILTER_DIPM &&
  577. tf->nsect == SATA_DIPM)
  578. return 1;
  579. /* inhibit FPDMA non-zero offset */
  580. if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_OFFSET &&
  581. (tf->nsect == SATA_FPDMA_OFFSET ||
  582. tf->nsect == SATA_FPDMA_IN_ORDER))
  583. return 1;
  584. /* inhibit FPDMA auto activation */
  585. if (dev->gtf_filter & ATA_ACPI_FILTER_FPDMA_AA &&
  586. tf->nsect == SATA_FPDMA_AA)
  587. return 1;
  588. }
  589. return 0;
  590. }
  591. /**
  592. * ata_acpi_run_tf - send taskfile registers to host controller
  593. * @dev: target ATA device
  594. * @gtf: raw ATA taskfile register set (0x1f1 - 0x1f7)
  595. * @prev_gtf: previous command
  596. *
  597. * Outputs ATA taskfile to standard ATA host controller.
  598. * Writes the control, feature, nsect, lbal, lbam, and lbah registers.
  599. * Optionally (ATA_TFLAG_LBA48) writes hob_feature, hob_nsect,
  600. * hob_lbal, hob_lbam, and hob_lbah.
  601. *
  602. * This function waits for idle (!BUSY and !DRQ) after writing
  603. * registers. If the control register has a new value, this
  604. * function also waits for idle after writing control and before
  605. * writing the remaining registers.
  606. *
  607. * LOCKING:
  608. * EH context.
  609. *
  610. * RETURNS:
  611. * 1 if command is executed successfully. 0 if ignored, rejected or
  612. * filtered out, -errno on other errors.
  613. */
  614. static int ata_acpi_run_tf(struct ata_device *dev,
  615. const struct ata_acpi_gtf *gtf,
  616. const struct ata_acpi_gtf *prev_gtf)
  617. {
  618. struct ata_taskfile *pptf = NULL;
  619. struct ata_taskfile tf, ptf, rtf;
  620. unsigned int err_mask;
  621. const char *descr;
  622. int rc;
  623. if ((gtf->tf[0] == 0) && (gtf->tf[1] == 0) && (gtf->tf[2] == 0)
  624. && (gtf->tf[3] == 0) && (gtf->tf[4] == 0) && (gtf->tf[5] == 0)
  625. && (gtf->tf[6] == 0))
  626. return 0;
  627. ata_acpi_gtf_to_tf(dev, gtf, &tf);
  628. if (prev_gtf) {
  629. ata_acpi_gtf_to_tf(dev, prev_gtf, &ptf);
  630. pptf = &ptf;
  631. }
  632. descr = ata_get_cmd_name(tf.command);
  633. if (!ata_acpi_filter_tf(dev, &tf, pptf)) {
  634. rtf = tf;
  635. err_mask = ata_exec_internal(dev, &rtf, NULL,
  636. DMA_NONE, NULL, 0, 0);
  637. switch (err_mask) {
  638. case 0:
  639. ata_dev_dbg(dev,
  640. "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x"
  641. "(%s) succeeded\n",
  642. tf.command, tf.feature, tf.nsect, tf.lbal,
  643. tf.lbam, tf.lbah, tf.device, descr);
  644. rc = 1;
  645. break;
  646. case AC_ERR_DEV:
  647. ata_dev_info(dev,
  648. "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x"
  649. "(%s) rejected by device (Stat=0x%02x Err=0x%02x)",
  650. tf.command, tf.feature, tf.nsect, tf.lbal,
  651. tf.lbam, tf.lbah, tf.device, descr,
  652. rtf.status, rtf.error);
  653. rc = 0;
  654. break;
  655. default:
  656. ata_dev_err(dev,
  657. "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x"
  658. "(%s) failed (Emask=0x%x Stat=0x%02x Err=0x%02x)",
  659. tf.command, tf.feature, tf.nsect, tf.lbal,
  660. tf.lbam, tf.lbah, tf.device, descr,
  661. err_mask, rtf.status, rtf.error);
  662. rc = -EIO;
  663. break;
  664. }
  665. } else {
  666. ata_dev_info(dev,
  667. "ACPI cmd %02x/%02x:%02x:%02x:%02x:%02x:%02x"
  668. "(%s) filtered out\n",
  669. tf.command, tf.feature, tf.nsect, tf.lbal,
  670. tf.lbam, tf.lbah, tf.device, descr);
  671. rc = 0;
  672. }
  673. return rc;
  674. }
  675. /**
  676. * ata_acpi_exec_tfs - get then write drive taskfile settings
  677. * @dev: target ATA device
  678. * @nr_executed: out parameter for the number of executed commands
  679. *
  680. * Evaluate _GTF and execute returned taskfiles.
  681. *
  682. * LOCKING:
  683. * EH context.
  684. *
  685. * RETURNS:
  686. * Number of executed taskfiles on success, 0 if _GTF doesn't exist.
  687. * -errno on other errors.
  688. */
  689. static int ata_acpi_exec_tfs(struct ata_device *dev, int *nr_executed)
  690. {
  691. struct ata_acpi_gtf *gtf = NULL, *pgtf = NULL;
  692. int gtf_count, i, rc;
  693. /* get taskfiles */
  694. rc = ata_dev_get_GTF(dev, &gtf);
  695. if (rc < 0)
  696. return rc;
  697. gtf_count = rc;
  698. /* execute them */
  699. for (i = 0; i < gtf_count; i++, gtf++) {
  700. rc = ata_acpi_run_tf(dev, gtf, pgtf);
  701. if (rc < 0)
  702. break;
  703. if (rc) {
  704. (*nr_executed)++;
  705. pgtf = gtf;
  706. }
  707. }
  708. ata_acpi_clear_gtf(dev);
  709. if (rc < 0)
  710. return rc;
  711. return 0;
  712. }
  713. /**
  714. * ata_acpi_push_id - send Identify data to drive
  715. * @dev: target ATA device
  716. *
  717. * _SDD ACPI object: for SATA mode only
  718. * Must be after Identify (Packet) Device -- uses its data
  719. * ATM this function never returns a failure. It is an optional
  720. * method and if it fails for whatever reason, we should still
  721. * just keep going.
  722. *
  723. * LOCKING:
  724. * EH context.
  725. *
  726. * RETURNS:
  727. * 0 on success, -ENOENT if _SDD doesn't exist, -errno on failure.
  728. */
  729. static int ata_acpi_push_id(struct ata_device *dev)
  730. {
  731. struct ata_port *ap = dev->link->ap;
  732. acpi_status status;
  733. struct acpi_object_list input;
  734. union acpi_object in_params[1];
  735. ata_dev_dbg(dev, "%s: ix = %d, port#: %d\n",
  736. __func__, dev->devno, ap->port_no);
  737. /* Give the drive Identify data to the drive via the _SDD method */
  738. /* _SDD: set up input parameters */
  739. input.count = 1;
  740. input.pointer = in_params;
  741. in_params[0].type = ACPI_TYPE_BUFFER;
  742. in_params[0].buffer.length = sizeof(dev->id[0]) * ATA_ID_WORDS;
  743. in_params[0].buffer.pointer = (u8 *)dev->id;
  744. /* Output buffer: _SDD has no output */
  745. /* It's OK for _SDD to be missing too. */
  746. swap_buf_le16(dev->id, ATA_ID_WORDS);
  747. status = acpi_evaluate_object(ata_dev_acpi_handle(dev), "_SDD", &input,
  748. NULL);
  749. swap_buf_le16(dev->id, ATA_ID_WORDS);
  750. if (status == AE_NOT_FOUND)
  751. return -ENOENT;
  752. if (ACPI_FAILURE(status)) {
  753. ata_dev_warn(dev, "ACPI _SDD failed (AE 0x%x)\n", status);
  754. return -EIO;
  755. }
  756. return 0;
  757. }
  758. /**
  759. * ata_acpi_on_resume - ATA ACPI hook called on resume
  760. * @ap: target ATA port
  761. *
  762. * This function is called when @ap is resumed - right after port
  763. * itself is resumed but before any EH action is taken.
  764. *
  765. * LOCKING:
  766. * EH context.
  767. */
  768. void ata_acpi_on_resume(struct ata_port *ap)
  769. {
  770. const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
  771. struct ata_device *dev;
  772. if (ACPI_HANDLE(&ap->tdev) && gtm) {
  773. /* _GTM valid */
  774. /* restore timing parameters */
  775. ata_acpi_stm(ap, gtm);
  776. /* _GTF should immediately follow _STM so that it can
  777. * use values set by _STM. Cache _GTF result and
  778. * schedule _GTF.
  779. */
  780. ata_for_each_dev(dev, &ap->link, ALL) {
  781. ata_acpi_clear_gtf(dev);
  782. if (ata_dev_enabled(dev) &&
  783. ata_dev_acpi_handle(dev) &&
  784. ata_dev_get_GTF(dev, NULL) >= 0)
  785. dev->flags |= ATA_DFLAG_ACPI_PENDING;
  786. }
  787. } else {
  788. /* SATA _GTF needs to be evaluated after _SDD and
  789. * there's no reason to evaluate IDE _GTF early
  790. * without _STM. Clear cache and schedule _GTF.
  791. */
  792. ata_for_each_dev(dev, &ap->link, ALL) {
  793. ata_acpi_clear_gtf(dev);
  794. if (ata_dev_enabled(dev))
  795. dev->flags |= ATA_DFLAG_ACPI_PENDING;
  796. }
  797. }
  798. }
  799. static int ata_acpi_choose_suspend_state(struct ata_device *dev, bool runtime)
  800. {
  801. int d_max_in = ACPI_STATE_D3_COLD;
  802. if (!runtime)
  803. goto out;
  804. /*
  805. * For ATAPI, runtime D3 cold is only allowed
  806. * for ZPODD in zero power ready state
  807. */
  808. if (dev->class == ATA_DEV_ATAPI &&
  809. !(zpodd_dev_enabled(dev) && zpodd_zpready(dev)))
  810. d_max_in = ACPI_STATE_D3_HOT;
  811. out:
  812. return acpi_pm_device_sleep_state(&dev->tdev, NULL, d_max_in);
  813. }
  814. static void sata_acpi_set_state(struct ata_port *ap, pm_message_t state)
  815. {
  816. bool runtime = PMSG_IS_AUTO(state);
  817. struct ata_device *dev;
  818. acpi_handle handle;
  819. int acpi_state;
  820. ata_for_each_dev(dev, &ap->link, ENABLED) {
  821. handle = ata_dev_acpi_handle(dev);
  822. if (!handle)
  823. continue;
  824. if (!(state.event & PM_EVENT_RESUME)) {
  825. acpi_state = ata_acpi_choose_suspend_state(dev, runtime);
  826. if (acpi_state == ACPI_STATE_D0)
  827. continue;
  828. if (runtime && zpodd_dev_enabled(dev) &&
  829. acpi_state == ACPI_STATE_D3_COLD)
  830. zpodd_enable_run_wake(dev);
  831. acpi_bus_set_power(handle, acpi_state);
  832. } else {
  833. if (runtime && zpodd_dev_enabled(dev))
  834. zpodd_disable_run_wake(dev);
  835. acpi_bus_set_power(handle, ACPI_STATE_D0);
  836. }
  837. }
  838. }
  839. /* ACPI spec requires _PS0 when IDE power on and _PS3 when power off */
  840. static void pata_acpi_set_state(struct ata_port *ap, pm_message_t state)
  841. {
  842. struct ata_device *dev;
  843. acpi_handle port_handle;
  844. port_handle = ACPI_HANDLE(&ap->tdev);
  845. if (!port_handle)
  846. return;
  847. /* channel first and then drives for power on and vica versa
  848. for power off */
  849. if (state.event & PM_EVENT_RESUME)
  850. acpi_bus_set_power(port_handle, ACPI_STATE_D0);
  851. ata_for_each_dev(dev, &ap->link, ENABLED) {
  852. acpi_handle dev_handle = ata_dev_acpi_handle(dev);
  853. if (!dev_handle)
  854. continue;
  855. acpi_bus_set_power(dev_handle, state.event & PM_EVENT_RESUME ?
  856. ACPI_STATE_D0 : ACPI_STATE_D3_COLD);
  857. }
  858. if (!(state.event & PM_EVENT_RESUME))
  859. acpi_bus_set_power(port_handle, ACPI_STATE_D3_COLD);
  860. }
  861. /**
  862. * ata_acpi_set_state - set the port power state
  863. * @ap: target ATA port
  864. * @state: state, on/off
  865. *
  866. * This function sets a proper ACPI D state for the device on
  867. * system and runtime PM operations.
  868. */
  869. void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
  870. {
  871. if (ap->flags & ATA_FLAG_ACPI_SATA)
  872. sata_acpi_set_state(ap, state);
  873. else
  874. pata_acpi_set_state(ap, state);
  875. }
  876. /**
  877. * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
  878. * @dev: target ATA device
  879. *
  880. * This function is called when @dev is about to be configured.
  881. * IDENTIFY data might have been modified after this hook is run.
  882. *
  883. * LOCKING:
  884. * EH context.
  885. *
  886. * RETURNS:
  887. * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
  888. * -errno on failure.
  889. */
  890. int ata_acpi_on_devcfg(struct ata_device *dev)
  891. {
  892. struct ata_port *ap = dev->link->ap;
  893. struct ata_eh_context *ehc = &ap->link.eh_context;
  894. int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
  895. int nr_executed = 0;
  896. int rc;
  897. if (!ata_dev_acpi_handle(dev))
  898. return 0;
  899. /* do we need to do _GTF? */
  900. if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
  901. !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
  902. return 0;
  903. /* do _SDD if SATA */
  904. if (acpi_sata) {
  905. rc = ata_acpi_push_id(dev);
  906. if (rc && rc != -ENOENT)
  907. goto acpi_err;
  908. }
  909. /* do _GTF */
  910. rc = ata_acpi_exec_tfs(dev, &nr_executed);
  911. if (rc)
  912. goto acpi_err;
  913. dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
  914. /* refresh IDENTIFY page if any _GTF command has been executed */
  915. if (nr_executed) {
  916. rc = ata_dev_reread_id(dev, 0);
  917. if (rc < 0) {
  918. ata_dev_err(dev,
  919. "failed to IDENTIFY after ACPI commands\n");
  920. return rc;
  921. }
  922. }
  923. return 0;
  924. acpi_err:
  925. /* ignore evaluation failure if we can continue safely */
  926. if (rc == -EINVAL && !nr_executed && !ata_port_is_frozen(ap))
  927. return 0;
  928. /* fail and let EH retry once more for unknown IO errors */
  929. if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
  930. dev->flags |= ATA_DFLAG_ACPI_FAILED;
  931. return rc;
  932. }
  933. dev->flags |= ATA_DFLAG_ACPI_DISABLED;
  934. ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
  935. /* We can safely continue if no _GTF command has been executed
  936. * and port is not frozen.
  937. */
  938. if (!nr_executed && !ata_port_is_frozen(ap))
  939. return 0;
  940. return rc;
  941. }
  942. /**
  943. * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
  944. * @dev: target ATA device
  945. *
  946. * This function is called when @dev is about to be disabled.
  947. *
  948. * LOCKING:
  949. * EH context.
  950. */
  951. void ata_acpi_on_disable(struct ata_device *dev)
  952. {
  953. ata_acpi_clear_gtf(dev);
  954. }