libata-acpi.c 26 KB

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