libata-acpi.c 29 KB

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