libata-acpi.c 26 KB

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