libata-acpi.c 26 KB

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