1 diff -urNp -x '*.orig' linux-4.9/Documentation/scsi/link_power_management_policy.txt linux-4.9/Documentation/scsi/link_power_management_policy.txt
2 --- linux-4.9/Documentation/scsi/link_power_management_policy.txt 2016-12-11 20:17:54.000000000 +0100
3 +++ linux-4.9/Documentation/scsi/link_power_management_policy.txt 2021-02-24 16:30:09.260516752 +0100
5 This parameter allows the user to set the link (interface) power management.
6 -There are 3 possible options:
7 +There are 4 possible options:
10 ----------------------------------------------------------------------------
11 +firmware_defaults Inherit configuration from the state programmed by
12 + the firmware during system init.
14 min_power Tell the controller to try to make the link use the
15 least possible power when possible. This may
16 sacrifice some performance due to increased latency
17 diff -urNp -x '*.orig' linux-4.9/drivers/ata/acard-ahci.c linux-4.9/drivers/ata/acard-ahci.c
18 --- linux-4.9/drivers/ata/acard-ahci.c 2021-02-24 16:29:54.606714867 +0100
19 +++ linux-4.9/drivers/ata/acard-ahci.c 2021-02-24 16:30:09.257183312 +0100
20 @@ -480,6 +480,9 @@ static int acard_ahci_init_one(struct pc
21 ata_port_pbar_desc(ap, AHCI_PCI_BAR,
22 0x100 + ap->port_no * 0x80, "port");
24 + rc = ahci_setup_port_privdata(ap);
27 /* set initial link pm policy */
29 ap->pm_policy = NOT_AVAILABLE;
30 diff -urNp -x '*.orig' linux-4.9/drivers/ata/ahci.c linux-4.9/drivers/ata/ahci.c
31 --- linux-4.9/drivers/ata/ahci.c 2021-02-24 16:29:54.606714867 +0100
32 +++ linux-4.9/drivers/ata/ahci.c 2021-02-24 16:30:09.257183312 +0100
33 @@ -1743,6 +1743,9 @@ static int ahci_init_one(struct pci_dev
34 if (ap->flags & ATA_FLAG_EM)
35 ap->em_message_type = hpriv->em_msg_type;
37 + rc = ahci_setup_port_privdata(ap);
41 /* disabled/not-implemented port */
42 if (!(hpriv->port_map & (1 << i)))
43 diff -urNp -x '*.orig' linux-4.9/drivers/ata/ahci.h linux-4.9/drivers/ata/ahci.h
44 --- linux-4.9/drivers/ata/ahci.h 2016-12-11 20:17:54.000000000 +0100
45 +++ linux-4.9/drivers/ata/ahci.h 2021-02-24 16:30:09.257183312 +0100
46 @@ -322,6 +322,12 @@ struct ahci_port_priv {
47 /* enclosure management info per PM slot */
48 struct ahci_em_priv em_priv[EM_MAX_SLOTS];
49 char *irq_desc; /* desc in /proc/interrupts */
50 + bool init_alpe; /* alpe enabled by default */
51 + bool init_asp; /* asp enabled by default */
52 + bool init_devslp; /* devslp enabled by default */
53 + u32 init_dito; /* initial dito configuration */
54 + u32 init_deto; /* initial deto configuration */
55 + u32 init_mdat; /* initial mdat configuration */
58 struct ahci_host_priv {
59 @@ -387,6 +393,7 @@ extern struct ata_port_operations ahci_p
60 extern struct ata_port_operations ahci_pmp_retry_srst_ops;
62 unsigned int ahci_dev_classify(struct ata_port *ap);
63 +int ahci_setup_port_privdata(struct ata_port *ap);
64 void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
66 void ahci_save_initial_config(struct device *dev,
67 diff -urNp -x '*.orig' linux-4.9/drivers/ata/libahci.c linux-4.9/drivers/ata/libahci.c
68 --- linux-4.9/drivers/ata/libahci.c 2021-02-24 16:29:54.606714867 +0100
69 +++ linux-4.9/drivers/ata/libahci.c 2021-02-24 16:30:09.263850192 +0100
70 @@ -743,6 +743,7 @@ static int ahci_set_lpm(struct ata_link
72 struct ata_port *ap = link->ap;
73 struct ahci_host_priv *hpriv = ap->host->private_data;
74 + struct ahci_port_priv *ppriv = ap->private_data;
75 struct ahci_port_priv *pp = ap->private_data;
76 void __iomem *port_mmio = ahci_port_base(ap);
78 @@ -763,10 +764,10 @@ static int ahci_set_lpm(struct ata_link
80 if (hpriv->cap & HOST_CAP_ALPM) {
81 u32 cmd = readl(port_mmio + PORT_CMD);
82 + if (!(hints & ATA_LPM_WAKE_ONLY))
83 + cmd &= ~(PORT_CMD_ASP | PORT_CMD_ALPE);
85 if (policy == ATA_LPM_MAX_POWER || !(hints & ATA_LPM_HIPM)) {
86 - if (!(hints & ATA_LPM_WAKE_ONLY))
87 - cmd &= ~(PORT_CMD_ASP | PORT_CMD_ALPE);
88 cmd |= PORT_CMD_ICC_ACTIVE;
90 writel(cmd, port_mmio + PORT_CMD);
91 @@ -777,6 +778,13 @@ static int ahci_set_lpm(struct ata_link
93 if (hints & ATA_LPM_WAKE_ONLY)
95 + } else if (policy == ATA_LPM_FIRMWARE_DEFAULTS) {
96 + if (ppriv->init_alpe)
97 + cmd |= PORT_CMD_ALPE;
98 + if (ppriv->init_asp)
99 + cmd |= PORT_CMD_ASP;
101 + writel(cmd, port_mmio + PORT_CMD);
103 cmd |= PORT_CMD_ALPE;
104 if (policy == ATA_LPM_MIN_POWER)
105 @@ -791,10 +799,18 @@ static int ahci_set_lpm(struct ata_link
106 if ((hpriv->cap2 & HOST_CAP2_SDS) &&
107 (hpriv->cap2 & HOST_CAP2_SADM) &&
108 (link->device->flags & ATA_DFLAG_DEVSLP)) {
109 - if (policy == ATA_LPM_MIN_POWER)
111 + case ATA_LPM_MIN_POWER:
112 ahci_set_aggressive_devslp(ap, true);
115 + case ATA_LPM_FIRMWARE_DEFAULTS:
116 + case ATA_LPM_MED_POWER:
117 + ahci_set_aggressive_devslp(ap, ppriv->init_devslp);
120 ahci_set_aggressive_devslp(ap, false);
125 if (policy == ATA_LPM_MAX_POWER) {
126 @@ -2076,6 +2092,7 @@ static void ahci_post_internal_cmd(struc
127 static void ahci_set_aggressive_devslp(struct ata_port *ap, bool sleep)
129 struct ahci_host_priv *hpriv = ap->host->private_data;
130 + struct ahci_port_priv *ppriv = ap->private_data;
131 void __iomem *port_mmio = ahci_port_base(ap);
132 struct ata_device *dev = ap->link.device;
133 u32 devslp, dm, dito, mdat, deto;
134 @@ -2111,26 +2128,32 @@ static void ahci_set_aggressive_devslp(s
138 - dm = (devslp & PORT_DEVSLP_DM_MASK) >> PORT_DEVSLP_DM_OFFSET;
139 - dito = devslp_idle_timeout / (dm + 1);
143 - /* Use the nominal value 10 ms if the read MDAT is zero,
144 - * the nominal value of DETO is 20 ms.
146 - if (dev->devslp_timing[ATA_LOG_DEVSLP_VALID] &
147 - ATA_LOG_DEVSLP_VALID_MASK) {
148 - mdat = dev->devslp_timing[ATA_LOG_DEVSLP_MDAT] &
149 - ATA_LOG_DEVSLP_MDAT_MASK;
151 + if (ppriv->init_devslp) {
152 + dito = ppriv->init_dito;
153 + deto = ppriv->init_deto;
154 + mdat = ppriv->init_mdat;
156 + dm = (devslp & PORT_DEVSLP_DM_MASK) >> PORT_DEVSLP_DM_OFFSET;
157 + dito = devslp_idle_timeout / (dm + 1);
161 + /* Use the nominal value 10 ms if the read MDAT is zero,
162 + * the nominal value of DETO is 20 ms.
164 + if (dev->devslp_timing[ATA_LOG_DEVSLP_VALID] &
165 + ATA_LOG_DEVSLP_VALID_MASK) {
166 + mdat = dev->devslp_timing[ATA_LOG_DEVSLP_MDAT] &
167 + ATA_LOG_DEVSLP_MDAT_MASK;
170 + deto = dev->devslp_timing[ATA_LOG_DEVSLP_DETO];
175 - deto = dev->devslp_timing[ATA_LOG_DEVSLP_DETO];
184 /* Make dito, mdat, deto bits to 0s */
185 @@ -2298,19 +2321,53 @@ static int ahci_port_suspend(struct ata_
190 + * Allocate port privdata and read back initial power management configuration
192 +int ahci_setup_port_privdata(struct ata_port *ap)
194 + struct ahci_port_priv *pp;
196 + void __iomem *port_mmio = ahci_port_base(ap);
198 + pp = kzalloc(sizeof(*pp), GFP_KERNEL);
202 + ap->private_data = pp;
204 + cmd = readl(port_mmio + PORT_CMD);
206 + if (cmd & PORT_CMD_ALPE)
207 + pp->init_alpe = true;
209 + if (cmd & PORT_CMD_ASP)
210 + pp->init_asp = true;
212 + devslp = readl(port_mmio + PORT_DEVSLP);
214 + /* devslp unsupported or disabled */
215 + if (!(devslp & PORT_DEVSLP_DSP) || !(devslp & PORT_DEVSLP_ADSE))
218 + pp->init_devslp = true;
219 + pp->init_dito = (devslp >> PORT_DEVSLP_DITO_OFFSET) & 0x3ff;
220 + pp->init_deto = (devslp >> PORT_DEVSLP_DETO_OFFSET) & 0xff;
221 + pp->init_mdat = (devslp >> PORT_DEVSLP_MDAT_OFFSET) & 0x1f;
225 +EXPORT_SYMBOL_GPL(ahci_setup_port_privdata);
227 static int ahci_port_start(struct ata_port *ap)
229 struct ahci_host_priv *hpriv = ap->host->private_data;
230 + struct ahci_port_priv *pp = ap->private_data;
231 struct device *dev = ap->host->dev;
232 - struct ahci_port_priv *pp;
235 size_t dma_sz, rx_fis_sz;
237 - pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
241 if (ap->host->n_ports > 1) {
242 pp->irq_desc = devm_kzalloc(dev, 8, GFP_KERNEL);
244 @@ -2389,8 +2446,6 @@ static int ahci_port_start(struct ata_po
245 ap->lock = &pp->lock;
248 - ap->private_data = pp;
250 /* engage engines, captain */
251 return ahci_port_resume(ap);
253 diff -urNp -x '*.orig' linux-4.9/drivers/ata/libahci_platform.c linux-4.9/drivers/ata/libahci_platform.c
254 --- linux-4.9/drivers/ata/libahci_platform.c 2021-02-24 16:29:54.606714867 +0100
255 +++ linux-4.9/drivers/ata/libahci_platform.c 2021-02-24 16:30:09.257183312 +0100
256 @@ -571,6 +571,10 @@ int ahci_platform_init_host(struct platf
257 if (ap->flags & ATA_FLAG_EM)
258 ap->em_message_type = hpriv->em_msg_type;
260 + rc = ahci_setup_port_privdata(ap);
264 /* disabled/not-implemented port */
265 if (!(hpriv->port_map & (1 << i)))
266 ap->ops = &ata_dummy_port_ops;
267 diff -urNp -x '*.orig' linux-4.9/drivers/ata/libata-core.c linux-4.9/drivers/ata/libata-core.c
268 --- linux-4.9/drivers/ata/libata-core.c 2021-02-24 16:29:54.610048307 +0100
269 +++ linux-4.9/drivers/ata/libata-core.c 2021-02-24 16:30:09.263850192 +0100
270 @@ -2028,6 +2028,9 @@ retry:
274 + if (id[79] & (1 << SATA_DIPM))
275 + dev->init_dipm = true;
280 @@ -3835,6 +3838,11 @@ int sata_link_scr_lpm(struct ata_link *l
284 + case ATA_LPM_FIRMWARE_DEFAULTS:
285 + /* use the values we read at probe */
286 + scontrol &= ~(0x7 << 8);
287 + scontrol |= (link->init_lpm << 8);
289 case ATA_LPM_MAX_POWER:
290 /* disable all LPM transitions */
291 scontrol |= (0x7 << 8);
292 @@ -3845,10 +3853,6 @@ int sata_link_scr_lpm(struct ata_link *l
295 case ATA_LPM_MED_POWER:
296 - /* allow LPM to PARTIAL */
297 - scontrol &= ~(0x1 << 8);
298 - scontrol |= (0x6 << 8);
300 case ATA_LPM_MIN_POWER:
301 if (ata_link_nr_enabled(link) > 0)
302 /* no restrictions on LPM transitions */
303 @@ -5814,11 +5818,11 @@ void ata_link_init(struct ata_port *ap,
307 - * sata_link_init_spd - Initialize link->sata_spd_limit
308 - * @link: Link to configure sata_spd_limit for
309 + * sata_link_init_config - Initialize link->sata_spd_limit and init_lpm
310 + * @link: Link to configure sata_spd_limit and init_lpm for
312 - * Initialize @link->[hw_]sata_spd_limit to the currently
313 - * configured value.
314 + * Initialize @link->[hw_]sata_spd_limit and @link->init_lpm to the
315 + * currently configured value.
318 * Kernel thread context (may sleep).
319 @@ -5826,7 +5830,7 @@ void ata_link_init(struct ata_port *ap,
321 * 0 on success, -errno on failure.
323 -int sata_link_init_spd(struct ata_link *link)
324 +int sata_link_init_config(struct ata_link *link)
328 @@ -5843,6 +5847,8 @@ int sata_link_init_spd(struct ata_link *
330 link->sata_spd_limit = link->hw_sata_spd_limit;
332 + link->init_lpm = (link->saved_scontrol >> 8) & 0x7;
337 @@ -6392,9 +6398,9 @@ int ata_host_register(struct ata_host *h
338 ap->cbl = ATA_CBL_SATA;
340 /* init sata_spd_limit to the current value */
341 - sata_link_init_spd(&ap->link);
342 + sata_link_init_config(&ap->link);
344 - sata_link_init_spd(ap->slave_link);
345 + sata_link_init_config(ap->slave_link);
347 /* print per-port info to dmesg */
348 xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
349 diff -urNp -x '*.orig' linux-4.9/drivers/ata/libata-eh.c linux-4.9/drivers/ata/libata-eh.c
350 --- linux-4.9/drivers/ata/libata-eh.c 2021-02-24 16:29:54.610048307 +0100
351 +++ linux-4.9/drivers/ata/libata-eh.c 2021-02-24 16:30:09.267183632 +0100
352 @@ -3534,9 +3534,7 @@ static int ata_eh_set_lpm(struct ata_lin
356 - * DIPM is enabled only for MIN_POWER as some devices
357 - * misbehave when the host NACKs transition to SLUMBER. Order
358 - * device and link configurations such that the host always
359 + * Order device and link configurations such that the host always
360 * allows DIPM requests.
362 ata_for_each_dev(dev, link, ENABLED) {
363 @@ -3555,7 +3553,7 @@ static int ata_eh_set_lpm(struct ata_lin
364 hints &= ~ATA_LPM_HIPM;
366 /* disable DIPM before changing link config */
367 - if (policy != ATA_LPM_MIN_POWER && dipm) {
368 + if (policy < ATA_LPM_MED_POWER && dipm) {
369 err_mask = ata_dev_set_feature(dev,
370 SETFEATURES_SATA_DISABLE, SATA_DIPM);
371 if (err_mask && err_mask != AC_ERR_DEV) {
372 @@ -3596,10 +3594,13 @@ static int ata_eh_set_lpm(struct ata_lin
373 if (ap && ap->slave_link)
374 ap->slave_link->lpm_policy = policy;
376 - /* host config updated, enable DIPM if transitioning to MIN_POWER */
377 + /* host config updated, enable DIPM if transitioning to MED_POWER,
378 + * MIN_POWER or FIRMWARE_DEFAULT when enabled by firmware
380 ata_for_each_dev(dev, link, ENABLED) {
381 - if (policy == ATA_LPM_MIN_POWER && !no_dipm &&
382 - ata_id_has_dipm(dev->id)) {
383 + if ((policy >= ATA_LPM_MED_POWER && !no_dipm &&
384 + ata_id_has_dipm(dev->id)) ||
385 + (policy == ATA_LPM_FIRMWARE_DEFAULTS && dev->init_dipm)) {
386 err_mask = ata_dev_set_feature(dev,
387 SETFEATURES_SATA_ENABLE, SATA_DIPM);
388 if (err_mask && err_mask != AC_ERR_DEV) {
389 diff -urNp -x '*.orig' linux-4.9/drivers/ata/libata-pmp.c linux-4.9/drivers/ata/libata-pmp.c
390 --- linux-4.9/drivers/ata/libata-pmp.c 2021-02-24 16:29:54.610048307 +0100
391 +++ linux-4.9/drivers/ata/libata-pmp.c 2021-02-24 16:30:09.260516752 +0100
392 @@ -538,7 +538,7 @@ int sata_pmp_attach(struct ata_device *d
393 ap->ops->pmp_attach(ap);
395 ata_for_each_link(tlink, ap, EDGE)
396 - sata_link_init_spd(tlink);
397 + sata_link_init_config(tlink);
401 diff -urNp -x '*.orig' linux-4.9/drivers/ata/libata-scsi.c linux-4.9/drivers/ata/libata-scsi.c
402 --- linux-4.9/drivers/ata/libata-scsi.c 2021-02-24 16:29:54.610048307 +0100
403 +++ linux-4.9/drivers/ata/libata-scsi.c 2021-02-24 16:30:09.263850192 +0100
404 @@ -107,6 +107,7 @@ static const u8 def_control_mpage[CONTRO
405 static const char *ata_lpm_policy_names[] = {
406 [ATA_LPM_UNKNOWN] = "max_performance",
407 [ATA_LPM_MAX_POWER] = "max_performance",
408 + [ATA_LPM_FIRMWARE_DEFAULTS] = "firmware_defaults",
409 [ATA_LPM_MED_POWER] = "medium_power",
410 [ATA_LPM_MIN_POWER] = "min_power",
412 diff -urNp -x '*.orig' linux-4.9/drivers/ata/libata.h linux-4.9/drivers/ata/libata.h
413 --- linux-4.9/drivers/ata/libata.h 2016-12-11 20:17:54.000000000 +0100
414 +++ linux-4.9/drivers/ata/libata.h 2021-02-24 16:30:09.260516752 +0100
415 @@ -99,7 +99,7 @@ extern bool ata_phys_link_online(struct
416 extern bool ata_phys_link_offline(struct ata_link *link);
417 extern void ata_dev_init(struct ata_device *dev);
418 extern void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp);
419 -extern int sata_link_init_spd(struct ata_link *link);
420 +extern int sata_link_init_config(struct ata_link *link);
421 extern int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg);
422 extern int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg);
423 extern struct ata_port *ata_port_alloc(struct ata_host *host);
424 diff -urNp -x '*.orig' linux-4.9/drivers/ata/sata_highbank.c linux-4.9/drivers/ata/sata_highbank.c
425 --- linux-4.9/drivers/ata/sata_highbank.c 2016-12-11 20:17:54.000000000 +0100
426 +++ linux-4.9/drivers/ata/sata_highbank.c 2021-02-24 16:30:09.260516752 +0100
427 @@ -557,6 +557,10 @@ static int ahci_highbank_probe(struct pl
428 if (ap->flags & ATA_FLAG_EM)
429 ap->em_message_type = hpriv->em_msg_type;
431 + rc = ahci_setup_port_privdata(ap);
435 /* disabled/not-implemented port */
436 if (!(hpriv->port_map & (1 << i)))
437 ap->ops = &ata_dummy_port_ops;
438 diff -urNp -x '*.orig' linux-4.9/include/linux/libata.h linux-4.9/include/linux/libata.h
439 --- linux-4.9/include/linux/libata.h 2021-02-24 16:29:56.030093715 +0100
440 +++ linux-4.9/include/linux/libata.h 2021-02-24 16:30:09.263850192 +0100
441 @@ -520,6 +520,7 @@ enum ata_completion_errors {
442 enum ata_lpm_policy {
445 + ATA_LPM_FIRMWARE_DEFAULTS,
449 @@ -740,6 +741,8 @@ struct ata_device {
451 /* ering is CLEAR_END, read comment above CLEAR_END */
452 struct ata_ering ering;
453 + /* Initial DIPM configuration */
457 /* Fields between ATA_DEVICE_CLEAR_BEGIN and ATA_DEVICE_CLEAR_END are
458 @@ -811,6 +814,7 @@ struct ata_link {
459 struct ata_device device[ATA_MAX_DEVICES];
461 unsigned long last_lpm_change; /* when last LPM change happened */
462 + u8 init_lpm; /* initial lpm configuration */
464 #define ATA_LINK_CLEAR_BEGIN offsetof(struct ata_link, active_tag)
465 #define ATA_LINK_CLEAR_END offsetof(struct ata_link, device[0])