libata-acpi.c 26 KB

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