libata-acpi.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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. /*
  55. * If acpi bind operation has already happened, we can get the handle
  56. * for the port by checking the corresponding scsi_host device's
  57. * firmware node, otherwise we will need to find out the handle from
  58. * its parent's acpi node.
  59. */
  60. if (ap->scsi_host)
  61. return DEVICE_ACPI_HANDLE(&ap->scsi_host->shost_gendev);
  62. else
  63. return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev),
  64. ap->port_no);
  65. }
  66. EXPORT_SYMBOL(ata_ap_acpi_handle);
  67. /**
  68. * ata_dev_acpi_handle - provide the acpi_handle for an ata_device
  69. * @dev: the acpi_device returned will correspond to this port
  70. *
  71. * Returns the acpi_handle for the ACPI namespace object corresponding to
  72. * the ata_device passed into the function, or NULL if no such object exists
  73. */
  74. acpi_handle ata_dev_acpi_handle(struct ata_device *dev)
  75. {
  76. acpi_integer adr;
  77. struct ata_port *ap = dev->link->ap;
  78. if (ap->flags & ATA_FLAG_ACPI_SATA) {
  79. if (!sata_pmp_attached(ap))
  80. adr = SATA_ADR(ap->port_no, NO_PORT_MULT);
  81. else
  82. adr = SATA_ADR(ap->port_no, dev->link->pmp);
  83. return acpi_get_child(DEVICE_ACPI_HANDLE(ap->host->dev), adr);
  84. } else
  85. return acpi_get_child(ata_ap_acpi_handle(ap), dev->devno);
  86. }
  87. EXPORT_SYMBOL(ata_dev_acpi_handle);
  88. /* @ap and @dev are the same as ata_acpi_handle_hotplug() */
  89. static void ata_acpi_detach_device(struct ata_port *ap, struct ata_device *dev)
  90. {
  91. if (dev)
  92. dev->flags |= ATA_DFLAG_DETACH;
  93. else {
  94. struct ata_link *tlink;
  95. struct ata_device *tdev;
  96. ata_for_each_link(tlink, ap, EDGE)
  97. ata_for_each_dev(tdev, tlink, ALL)
  98. tdev->flags |= ATA_DFLAG_DETACH;
  99. }
  100. ata_port_schedule_eh(ap);
  101. }
  102. /**
  103. * ata_acpi_handle_hotplug - ACPI event handler backend
  104. * @ap: ATA port ACPI event occurred
  105. * @dev: ATA device ACPI event occurred (can be NULL)
  106. * @event: ACPI event which occurred
  107. *
  108. * All ACPI bay / device realted events end up in this function. If
  109. * the event is port-wide @dev is NULL. If the event is specific to a
  110. * device, @dev points to it.
  111. *
  112. * Hotplug (as opposed to unplug) notification is always handled as
  113. * port-wide while unplug only kills the target device on device-wide
  114. * event.
  115. *
  116. * LOCKING:
  117. * ACPI notify handler context. May sleep.
  118. */
  119. static void ata_acpi_handle_hotplug(struct ata_port *ap, struct ata_device *dev,
  120. u32 event)
  121. {
  122. struct ata_eh_info *ehi = &ap->link.eh_info;
  123. int wait = 0;
  124. unsigned long flags;
  125. spin_lock_irqsave(ap->lock, flags);
  126. /*
  127. * When dock driver calls into the routine, it will always use
  128. * ACPI_NOTIFY_BUS_CHECK/ACPI_NOTIFY_DEVICE_CHECK for add and
  129. * ACPI_NOTIFY_EJECT_REQUEST for remove
  130. */
  131. switch (event) {
  132. case ACPI_NOTIFY_BUS_CHECK:
  133. case ACPI_NOTIFY_DEVICE_CHECK:
  134. ata_ehi_push_desc(ehi, "ACPI event");
  135. ata_ehi_hotplugged(ehi);
  136. ata_port_freeze(ap);
  137. break;
  138. case ACPI_NOTIFY_EJECT_REQUEST:
  139. ata_ehi_push_desc(ehi, "ACPI event");
  140. ata_acpi_detach_device(ap, dev);
  141. wait = 1;
  142. break;
  143. }
  144. spin_unlock_irqrestore(ap->lock, flags);
  145. if (wait)
  146. ata_port_wait_eh(ap);
  147. }
  148. static void ata_acpi_dev_notify_dock(acpi_handle handle, u32 event, void *data)
  149. {
  150. struct ata_device *dev = data;
  151. ata_acpi_handle_hotplug(dev->link->ap, dev, event);
  152. }
  153. static void ata_acpi_ap_notify_dock(acpi_handle handle, u32 event, void *data)
  154. {
  155. struct ata_port *ap = data;
  156. ata_acpi_handle_hotplug(ap, NULL, event);
  157. }
  158. static void ata_acpi_uevent(struct ata_port *ap, struct ata_device *dev,
  159. u32 event)
  160. {
  161. struct kobject *kobj = NULL;
  162. char event_string[20];
  163. char *envp[] = { event_string, NULL };
  164. if (dev) {
  165. if (dev->sdev)
  166. kobj = &dev->sdev->sdev_gendev.kobj;
  167. } else
  168. kobj = &ap->dev->kobj;
  169. if (kobj) {
  170. snprintf(event_string, 20, "BAY_EVENT=%d", event);
  171. kobject_uevent_env(kobj, KOBJ_CHANGE, envp);
  172. }
  173. }
  174. static void ata_acpi_ap_uevent(acpi_handle handle, u32 event, void *data)
  175. {
  176. ata_acpi_uevent(data, NULL, event);
  177. }
  178. static void ata_acpi_dev_uevent(acpi_handle handle, u32 event, void *data)
  179. {
  180. struct ata_device *dev = data;
  181. ata_acpi_uevent(dev->link->ap, dev, event);
  182. }
  183. static const struct acpi_dock_ops ata_acpi_dev_dock_ops = {
  184. .handler = ata_acpi_dev_notify_dock,
  185. .uevent = ata_acpi_dev_uevent,
  186. };
  187. static const struct acpi_dock_ops ata_acpi_ap_dock_ops = {
  188. .handler = ata_acpi_ap_notify_dock,
  189. .uevent = ata_acpi_ap_uevent,
  190. };
  191. /**
  192. * ata_acpi_dissociate - dissociate ATA host from ACPI objects
  193. * @host: target ATA host
  194. *
  195. * This function is called during driver detach after the whole host
  196. * is shut down.
  197. *
  198. * LOCKING:
  199. * EH context.
  200. */
  201. void ata_acpi_dissociate(struct ata_host *host)
  202. {
  203. int i;
  204. /* Restore initial _GTM values so that driver which attaches
  205. * afterward can use them too.
  206. */
  207. for (i = 0; i < host->n_ports; i++) {
  208. struct ata_port *ap = host->ports[i];
  209. const struct ata_acpi_gtm *gtm = ata_acpi_init_gtm(ap);
  210. if (ata_ap_acpi_handle(ap) && gtm)
  211. ata_acpi_stm(ap, gtm);
  212. }
  213. }
  214. /**
  215. * ata_acpi_gtm - execute _GTM
  216. * @ap: target ATA port
  217. * @gtm: out parameter for _GTM result
  218. *
  219. * Evaluate _GTM and store the result in @gtm.
  220. *
  221. * LOCKING:
  222. * EH context.
  223. *
  224. * RETURNS:
  225. * 0 on success, -ENOENT if _GTM doesn't exist, -errno on failure.
  226. */
  227. int ata_acpi_gtm(struct ata_port *ap, struct ata_acpi_gtm *gtm)
  228. {
  229. struct acpi_buffer output = { .length = ACPI_ALLOCATE_BUFFER };
  230. union acpi_object *out_obj;
  231. acpi_status status;
  232. int rc = 0;
  233. status = acpi_evaluate_object(ata_ap_acpi_handle(ap), "_GTM", NULL,
  234. &output);
  235. rc = -ENOENT;
  236. if (status == AE_NOT_FOUND)
  237. goto out_free;
  238. rc = -EINVAL;
  239. if (ACPI_FAILURE(status)) {
  240. ata_port_err(ap, "ACPI get timing mode failed (AE 0x%x)\n",
  241. status);
  242. goto out_free;
  243. }
  244. out_obj = output.pointer;
  245. if (out_obj->type != ACPI_TYPE_BUFFER) {
  246. ata_port_warn(ap, "_GTM returned unexpected object type 0x%x\n",
  247. out_obj->type);
  248. goto out_free;
  249. }
  250. if (out_obj->buffer.length != sizeof(struct ata_acpi_gtm)) {
  251. ata_port_err(ap, "_GTM returned invalid length %d\n",
  252. out_obj->buffer.length);
  253. goto out_free;
  254. }
  255. memcpy(gtm, out_obj->buffer.pointer, sizeof(struct ata_acpi_gtm));
  256. rc = 0;
  257. out_free:
  258. kfree(output.pointer);
  259. return rc;
  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. /**
  737. * ata_acpi_set_state - set the port power state
  738. * @ap: target ATA port
  739. * @state: state, on/off
  740. *
  741. * This function executes the _PS0/_PS3 ACPI method to set the power state.
  742. * ACPI spec requires _PS0 when IDE power on and _PS3 when power off
  743. */
  744. void ata_acpi_set_state(struct ata_port *ap, pm_message_t state)
  745. {
  746. struct ata_device *dev;
  747. acpi_handle handle;
  748. int acpi_state;
  749. /* channel first and then drives for power on and vica versa
  750. for power off */
  751. handle = ata_ap_acpi_handle(ap);
  752. if (handle && state.event == PM_EVENT_ON)
  753. acpi_bus_set_power(handle, ACPI_STATE_D0);
  754. ata_for_each_dev(dev, &ap->link, ENABLED) {
  755. handle = ata_dev_acpi_handle(dev);
  756. if (!handle)
  757. continue;
  758. if (state.event != PM_EVENT_ON) {
  759. acpi_state = acpi_pm_device_sleep_state(
  760. &dev->sdev->sdev_gendev, NULL, ACPI_STATE_D3);
  761. if (acpi_state > 0)
  762. acpi_bus_set_power(handle, acpi_state);
  763. /* TBD: need to check if it's runtime pm request */
  764. acpi_pm_device_run_wake(
  765. &dev->sdev->sdev_gendev, true);
  766. } else {
  767. /* Ditto */
  768. acpi_pm_device_run_wake(
  769. &dev->sdev->sdev_gendev, false);
  770. acpi_bus_set_power(handle, ACPI_STATE_D0);
  771. }
  772. }
  773. handle = ata_ap_acpi_handle(ap);
  774. if (handle && state.event != PM_EVENT_ON)
  775. acpi_bus_set_power(handle, ACPI_STATE_D3);
  776. }
  777. /**
  778. * ata_acpi_on_devcfg - ATA ACPI hook called on device donfiguration
  779. * @dev: target ATA device
  780. *
  781. * This function is called when @dev is about to be configured.
  782. * IDENTIFY data might have been modified after this hook is run.
  783. *
  784. * LOCKING:
  785. * EH context.
  786. *
  787. * RETURNS:
  788. * Positive number if IDENTIFY data needs to be refreshed, 0 if not,
  789. * -errno on failure.
  790. */
  791. int ata_acpi_on_devcfg(struct ata_device *dev)
  792. {
  793. struct ata_port *ap = dev->link->ap;
  794. struct ata_eh_context *ehc = &ap->link.eh_context;
  795. int acpi_sata = ap->flags & ATA_FLAG_ACPI_SATA;
  796. int nr_executed = 0;
  797. int rc;
  798. if (!ata_dev_acpi_handle(dev))
  799. return 0;
  800. /* do we need to do _GTF? */
  801. if (!(dev->flags & ATA_DFLAG_ACPI_PENDING) &&
  802. !(acpi_sata && (ehc->i.flags & ATA_EHI_DID_HARDRESET)))
  803. return 0;
  804. /* do _SDD if SATA */
  805. if (acpi_sata) {
  806. rc = ata_acpi_push_id(dev);
  807. if (rc && rc != -ENOENT)
  808. goto acpi_err;
  809. }
  810. /* do _GTF */
  811. rc = ata_acpi_exec_tfs(dev, &nr_executed);
  812. if (rc)
  813. goto acpi_err;
  814. dev->flags &= ~ATA_DFLAG_ACPI_PENDING;
  815. /* refresh IDENTIFY page if any _GTF command has been executed */
  816. if (nr_executed) {
  817. rc = ata_dev_reread_id(dev, 0);
  818. if (rc < 0) {
  819. ata_dev_err(dev,
  820. "failed to IDENTIFY after ACPI commands\n");
  821. return rc;
  822. }
  823. }
  824. return 0;
  825. acpi_err:
  826. /* ignore evaluation failure if we can continue safely */
  827. if (rc == -EINVAL && !nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
  828. return 0;
  829. /* fail and let EH retry once more for unknown IO errors */
  830. if (!(dev->flags & ATA_DFLAG_ACPI_FAILED)) {
  831. dev->flags |= ATA_DFLAG_ACPI_FAILED;
  832. return rc;
  833. }
  834. ata_dev_warn(dev, "ACPI: failed the second time, disabled\n");
  835. /* We can safely continue if no _GTF command has been executed
  836. * and port is not frozen.
  837. */
  838. if (!nr_executed && !(ap->pflags & ATA_PFLAG_FROZEN))
  839. return 0;
  840. return rc;
  841. }
  842. /**
  843. * ata_acpi_on_disable - ATA ACPI hook called when a device is disabled
  844. * @dev: target ATA device
  845. *
  846. * This function is called when @dev is about to be disabled.
  847. *
  848. * LOCKING:
  849. * EH context.
  850. */
  851. void ata_acpi_on_disable(struct ata_device *dev)
  852. {
  853. ata_acpi_clear_gtf(dev);
  854. }
  855. static void ata_acpi_wake_dev(acpi_handle handle, u32 event, void *context)
  856. {
  857. struct ata_device *ata_dev = context;
  858. if (event == ACPI_NOTIFY_DEVICE_WAKE && ata_dev &&
  859. pm_runtime_suspended(&ata_dev->sdev->sdev_gendev))
  860. scsi_autopm_get_device(ata_dev->sdev);
  861. }
  862. static void ata_acpi_add_pm_notifier(struct ata_device *dev)
  863. {
  864. struct acpi_device *acpi_dev;
  865. acpi_handle handle;
  866. acpi_status status;
  867. handle = ata_dev_acpi_handle(dev);
  868. if (!handle)
  869. return;
  870. status = acpi_bus_get_device(handle, &acpi_dev);
  871. if (ACPI_FAILURE(status))
  872. return;
  873. if (dev->sdev->can_power_off) {
  874. acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  875. ata_acpi_wake_dev, dev);
  876. device_set_run_wake(&dev->sdev->sdev_gendev, true);
  877. }
  878. }
  879. static void ata_acpi_remove_pm_notifier(struct ata_device *dev)
  880. {
  881. struct acpi_device *acpi_dev;
  882. acpi_handle handle;
  883. acpi_status status;
  884. handle = ata_dev_acpi_handle(dev);
  885. if (!handle)
  886. return;
  887. status = acpi_bus_get_device(handle, &acpi_dev);
  888. if (ACPI_FAILURE(status))
  889. return;
  890. if (dev->sdev->can_power_off) {
  891. device_set_run_wake(&dev->sdev->sdev_gendev, false);
  892. acpi_remove_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  893. ata_acpi_wake_dev);
  894. }
  895. }
  896. static void ata_acpi_register_power_resource(struct ata_device *dev)
  897. {
  898. struct scsi_device *sdev = dev->sdev;
  899. acpi_handle handle;
  900. struct device *device;
  901. handle = ata_dev_acpi_handle(dev);
  902. if (!handle)
  903. return;
  904. device = &sdev->sdev_gendev;
  905. acpi_power_resource_register_device(device, handle);
  906. }
  907. static void ata_acpi_unregister_power_resource(struct ata_device *dev)
  908. {
  909. struct scsi_device *sdev = dev->sdev;
  910. acpi_handle handle;
  911. struct device *device;
  912. handle = ata_dev_acpi_handle(dev);
  913. if (!handle)
  914. return;
  915. device = &sdev->sdev_gendev;
  916. acpi_power_resource_unregister_device(device, handle);
  917. }
  918. void ata_acpi_bind(struct ata_device *dev)
  919. {
  920. ata_acpi_add_pm_notifier(dev);
  921. ata_acpi_register_power_resource(dev);
  922. }
  923. void ata_acpi_unbind(struct ata_device *dev)
  924. {
  925. ata_acpi_remove_pm_notifier(dev);
  926. ata_acpi_unregister_power_resource(dev);
  927. }
  928. static int compat_pci_ata(struct ata_port *ap)
  929. {
  930. struct device *dev = ap->tdev.parent;
  931. struct pci_dev *pdev;
  932. if (!is_pci_dev(dev))
  933. return 0;
  934. pdev = to_pci_dev(dev);
  935. if ((pdev->class >> 8) != PCI_CLASS_STORAGE_SATA &&
  936. (pdev->class >> 8) != PCI_CLASS_STORAGE_IDE)
  937. return 0;
  938. return 1;
  939. }
  940. static int ata_acpi_bind_host(struct ata_port *ap, acpi_handle *handle)
  941. {
  942. if (ap->flags & ATA_FLAG_ACPI_SATA)
  943. return -ENODEV;
  944. *handle = acpi_get_child(DEVICE_ACPI_HANDLE(ap->tdev.parent),
  945. ap->port_no);
  946. if (!*handle)
  947. return -ENODEV;
  948. return 0;
  949. }
  950. static int ata_acpi_bind_device(struct ata_port *ap, struct scsi_device *sdev,
  951. acpi_handle *handle)
  952. {
  953. struct ata_device *ata_dev;
  954. acpi_status status;
  955. struct acpi_device *acpi_dev;
  956. struct acpi_device_power_state *states;
  957. if (ap->flags & ATA_FLAG_ACPI_SATA)
  958. ata_dev = &ap->link.device[sdev->channel];
  959. else
  960. ata_dev = &ap->link.device[sdev->id];
  961. *handle = ata_dev_acpi_handle(ata_dev);
  962. if (!*handle)
  963. return -ENODEV;
  964. status = acpi_bus_get_device(*handle, &acpi_dev);
  965. if (ACPI_FAILURE(status))
  966. return 0;
  967. /*
  968. * If firmware has _PS3 or _PR3 for this device,
  969. * and this ata ODD device support device attention,
  970. * it means this device can be powered off
  971. */
  972. states = acpi_dev->power.states;
  973. if ((states[ACPI_STATE_D3_HOT].flags.valid ||
  974. states[ACPI_STATE_D3_COLD].flags.explicit_set) &&
  975. ata_dev->flags & ATA_DFLAG_DA)
  976. sdev->can_power_off = 1;
  977. return 0;
  978. }
  979. static int is_ata_port(const struct device *dev)
  980. {
  981. return dev->type == &ata_port_type;
  982. }
  983. static struct ata_port *dev_to_ata_port(struct device *dev)
  984. {
  985. while (!is_ata_port(dev)) {
  986. if (!dev->parent)
  987. return NULL;
  988. dev = dev->parent;
  989. }
  990. return to_ata_port(dev);
  991. }
  992. static int ata_acpi_find_device(struct device *dev, acpi_handle *handle)
  993. {
  994. struct ata_port *ap = dev_to_ata_port(dev);
  995. if (!ap)
  996. return -ENODEV;
  997. if (!compat_pci_ata(ap))
  998. return -ENODEV;
  999. if (scsi_is_host_device(dev))
  1000. return ata_acpi_bind_host(ap, handle);
  1001. else if (scsi_is_sdev_device(dev)) {
  1002. struct scsi_device *sdev = to_scsi_device(dev);
  1003. return ata_acpi_bind_device(ap, sdev, handle);
  1004. } else
  1005. return -ENODEV;
  1006. }
  1007. static int ata_acpi_find_dummy(struct device *dev, acpi_handle *handle)
  1008. {
  1009. return -ENODEV;
  1010. }
  1011. static struct acpi_bus_type ata_acpi_bus = {
  1012. .find_bridge = ata_acpi_find_dummy,
  1013. .find_device = ata_acpi_find_device,
  1014. };
  1015. int ata_acpi_register(void)
  1016. {
  1017. return scsi_register_acpi_bus_type(&ata_acpi_bus);
  1018. }
  1019. void ata_acpi_unregister(void)
  1020. {
  1021. scsi_unregister_acpi_bus_type(&ata_acpi_bus);
  1022. }