In some cases if sata drive can't work with default controller
bandwidth, driver fails to initialize the device.

libata-core: sata_link_hardreset - if after reset the device is
offline, then driver should try again, with lower SPD.

sata_down_spd_limit - function should handle corner case values,
if device is not initialized yet.

Bugzilla: https://bugs.launchpad.net/ubuntu/+source/linux/+bug/1783906

Signed-off-by: Michał Wadowski <wad...@gmail.com>
---
 drivers/ata/libata-core.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/drivers/ata/libata-core.c b/drivers/ata/libata-core.c
index adf28788cab5..a37249c4ebeb 100644
--- a/drivers/ata/libata-core.c
+++ b/drivers/ata/libata-core.c
@@ -3061,6 +3061,7 @@ int sata_down_spd_limit(struct ata_link *link, u32 
spd_limit)
 
        /* If SCR can be read, use it to determine the current SPD.
         * If not, use cached value in link->sata_spd.
+        * Value of link->sata_spd may be 0.
         */
        rc = sata_scr_read(link, SCR_STATUS, &sstatus);
        if (rc == 0 && ata_sstatus_online(sstatus))
@@ -3072,23 +3073,23 @@ int sata_down_spd_limit(struct ata_link *link, u32 
spd_limit)
        if (mask <= 1)
                return -EINVAL;
 
+       /* sata_spd_limit may by initially INT_MAX, that is not correct
+        * value to working with. Cut down mask to highest correct value.
+        */
+       mask &= 0x7;
+
        /* unconditionally mask off the highest bit */
        bit = fls(mask) - 1;
        mask &= ~(1 << bit);
 
        /*
-        * Mask off all speeds higher than or equal to the current one.  At
-        * this point, if current SPD is not available and we previously
-        * recorded the link speed from SStatus, the driver has already
-        * masked off the highest bit so mask should already be 1 or 0.
-        * Otherwise, we should not force 1.5Gbps on a link where we have
-        * not previously recorded speed from SStatus.  Just return in this
-        * case.
+        * Mask off all speeds higher than or equal to the current one.
+        * If device is not initialized yet, value of SPD is 0, so
+        * we should ignore this. If spd is 1, then we can't speed down
+        * any more.
         */
        if (spd > 1)
                mask &= (1 << (spd - 1)) - 1;
-       else
-               return -EINVAL;
 
        /* were we already at the bottom? */
        if (!mask)
@@ -4116,7 +4117,10 @@ int sata_link_hardreset(struct ata_link *link, const 
unsigned long *timing,
                goto out;
        /* if link is offline nothing more to do */
        if (ata_phys_link_offline(link))
+       {
+               rc = -EPIPE;
                goto out;
+       }
 
        /* Link is online.  From this point, -ENODEV too is an error. */
        if (online)
-- 
2.7.4

Reply via email to