libata-acpi.c 28 KB

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