libata-acpi.c 27 KB

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