libata-acpi.c 27 KB

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