libata-acpi.c 26 KB

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