1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * SATA specific part of ATA helper library
5 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
6 * Copyright 2003-2004 Jeff Garzik
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/libata.h>
16 * sata_scr_valid - test whether SCRs are accessible
17 * @link: ATA link to test SCR accessibility for
19 * Test whether SCRs are accessible for @link.
25 * 1 if SCRs are accessible, 0 otherwise.
27 int sata_scr_valid(struct ata_link
*link
)
29 struct ata_port
*ap
= link
->ap
;
31 return (ap
->flags
& ATA_FLAG_SATA
) && ap
->ops
->scr_read
;
33 EXPORT_SYMBOL_GPL(sata_scr_valid
);
36 * sata_scr_read - read SCR register of the specified port
37 * @link: ATA link to read SCR for
39 * @val: Place to store read value
41 * Read SCR register @reg of @link into *@val. This function is
42 * guaranteed to succeed if @link is ap->link, the cable type of
43 * the port is SATA and the port implements ->scr_read.
46 * None if @link is ap->link. Kernel thread context otherwise.
49 * 0 on success, negative errno on failure.
51 int sata_scr_read(struct ata_link
*link
, int reg
, u32
*val
)
53 if (ata_is_host_link(link
)) {
54 if (sata_scr_valid(link
))
55 return link
->ap
->ops
->scr_read(link
, reg
, val
);
59 return sata_pmp_scr_read(link
, reg
, val
);
61 EXPORT_SYMBOL_GPL(sata_scr_read
);
64 * sata_scr_write - write SCR register of the specified port
65 * @link: ATA link to write SCR for
67 * @val: value to write
69 * Write @val to SCR register @reg of @link. This function is
70 * guaranteed to succeed if @link is ap->link, the cable type of
71 * the port is SATA and the port implements ->scr_read.
74 * None if @link is ap->link. Kernel thread context otherwise.
77 * 0 on success, negative errno on failure.
79 int sata_scr_write(struct ata_link
*link
, int reg
, u32 val
)
81 if (ata_is_host_link(link
)) {
82 if (sata_scr_valid(link
))
83 return link
->ap
->ops
->scr_write(link
, reg
, val
);
87 return sata_pmp_scr_write(link
, reg
, val
);
89 EXPORT_SYMBOL_GPL(sata_scr_write
);
92 * sata_scr_write_flush - write SCR register of the specified port and flush
93 * @link: ATA link to write SCR for
95 * @val: value to write
97 * This function is identical to sata_scr_write() except that this
98 * function performs flush after writing to the register.
101 * None if @link is ap->link. Kernel thread context otherwise.
104 * 0 on success, negative errno on failure.
106 int sata_scr_write_flush(struct ata_link
*link
, int reg
, u32 val
)
108 if (ata_is_host_link(link
)) {
111 if (sata_scr_valid(link
)) {
112 rc
= link
->ap
->ops
->scr_write(link
, reg
, val
);
114 rc
= link
->ap
->ops
->scr_read(link
, reg
, &val
);
120 return sata_pmp_scr_write(link
, reg
, val
);
122 EXPORT_SYMBOL_GPL(sata_scr_write_flush
);
125 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
126 * @tf: Taskfile to convert
127 * @pmp: Port multiplier port
128 * @is_cmd: This FIS is for command
129 * @fis: Buffer into which data will output
131 * Converts a standard ATA taskfile to a Serial ATA
132 * FIS structure (Register - Host to Device).
135 * Inherited from caller.
137 void ata_tf_to_fis(const struct ata_taskfile
*tf
, u8 pmp
, int is_cmd
, u8
*fis
)
139 fis
[0] = 0x27; /* Register - Host to Device FIS */
140 fis
[1] = pmp
& 0xf; /* Port multiplier number*/
142 fis
[1] |= (1 << 7); /* bit 7 indicates Command FIS */
144 fis
[2] = tf
->command
;
145 fis
[3] = tf
->feature
;
152 fis
[8] = tf
->hob_lbal
;
153 fis
[9] = tf
->hob_lbam
;
154 fis
[10] = tf
->hob_lbah
;
155 fis
[11] = tf
->hob_feature
;
158 fis
[13] = tf
->hob_nsect
;
162 fis
[16] = tf
->auxiliary
& 0xff;
163 fis
[17] = (tf
->auxiliary
>> 8) & 0xff;
164 fis
[18] = (tf
->auxiliary
>> 16) & 0xff;
165 fis
[19] = (tf
->auxiliary
>> 24) & 0xff;
167 EXPORT_SYMBOL_GPL(ata_tf_to_fis
);
170 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
171 * @fis: Buffer from which data will be input
172 * @tf: Taskfile to output
174 * Converts a serial ATA FIS structure to a standard ATA taskfile.
177 * Inherited from caller.
180 void ata_tf_from_fis(const u8
*fis
, struct ata_taskfile
*tf
)
182 tf
->command
= fis
[2]; /* status */
183 tf
->feature
= fis
[3]; /* error */
190 tf
->hob_lbal
= fis
[8];
191 tf
->hob_lbam
= fis
[9];
192 tf
->hob_lbah
= fis
[10];
195 tf
->hob_nsect
= fis
[13];
197 EXPORT_SYMBOL_GPL(ata_tf_from_fis
);
200 * sata_link_scr_lpm - manipulate SControl IPM and SPM fields
201 * @link: ATA link to manipulate SControl for
202 * @policy: LPM policy to configure
203 * @spm_wakeup: initiate LPM transition to active state
205 * Manipulate the IPM field of the SControl register of @link
206 * according to @policy. If @policy is ATA_LPM_MAX_POWER and
207 * @spm_wakeup is %true, the SPM field is manipulated to wake up
208 * the link. This function also clears PHYRDY_CHG before
215 * 0 on success, -errno otherwise.
217 int sata_link_scr_lpm(struct ata_link
*link
, enum ata_lpm_policy policy
,
220 struct ata_eh_context
*ehc
= &link
->eh_context
;
221 bool woken_up
= false;
225 rc
= sata_scr_read(link
, SCR_CONTROL
, &scontrol
);
230 case ATA_LPM_MAX_POWER
:
231 /* disable all LPM transitions */
232 scontrol
|= (0x7 << 8);
233 /* initiate transition to active state */
235 scontrol
|= (0x4 << 12);
239 case ATA_LPM_MED_POWER
:
240 /* allow LPM to PARTIAL */
241 scontrol
&= ~(0x1 << 8);
242 scontrol
|= (0x6 << 8);
244 case ATA_LPM_MED_POWER_WITH_DIPM
:
245 case ATA_LPM_MIN_POWER_WITH_PARTIAL
:
246 case ATA_LPM_MIN_POWER
:
247 if (ata_link_nr_enabled(link
) > 0)
248 /* no restrictions on LPM transitions */
249 scontrol
&= ~(0x7 << 8);
251 /* empty port, power off */
253 scontrol
|= (0x1 << 2);
260 rc
= sata_scr_write(link
, SCR_CONTROL
, scontrol
);
264 /* give the link time to transit out of LPM state */
268 /* clear PHYRDY_CHG from SError */
269 ehc
->i
.serror
&= ~SERR_PHYRDY_CHG
;
270 return sata_scr_write(link
, SCR_ERROR
, SERR_PHYRDY_CHG
);
272 EXPORT_SYMBOL_GPL(sata_link_scr_lpm
);
274 static int __sata_set_spd_needed(struct ata_link
*link
, u32
*scontrol
)
276 struct ata_link
*host_link
= &link
->ap
->link
;
277 u32 limit
, target
, spd
;
279 limit
= link
->sata_spd_limit
;
281 /* Don't configure downstream link faster than upstream link.
282 * It doesn't speed up anything and some PMPs choke on such
285 if (!ata_is_host_link(link
) && host_link
->sata_spd
)
286 limit
&= (1 << host_link
->sata_spd
) - 1;
288 if (limit
== UINT_MAX
)
293 spd
= (*scontrol
>> 4) & 0xf;
294 *scontrol
= (*scontrol
& ~0xf0) | ((target
& 0xf) << 4);
296 return spd
!= target
;
300 * sata_set_spd_needed - is SATA spd configuration needed
301 * @link: Link in question
303 * Test whether the spd limit in SControl matches
304 * @link->sata_spd_limit. This function is used to determine
305 * whether hardreset is necessary to apply SATA spd
309 * Inherited from caller.
312 * 1 if SATA spd configuration is needed, 0 otherwise.
314 int sata_set_spd_needed(struct ata_link
*link
)
318 if (sata_scr_read(link
, SCR_CONTROL
, &scontrol
))
321 return __sata_set_spd_needed(link
, &scontrol
);
325 * sata_set_spd - set SATA spd according to spd limit
326 * @link: Link to set SATA spd for
328 * Set SATA spd of @link according to sata_spd_limit.
331 * Inherited from caller.
334 * 0 if spd doesn't need to be changed, 1 if spd has been
335 * changed. Negative errno if SCR registers are inaccessible.
337 int sata_set_spd(struct ata_link
*link
)
342 if ((rc
= sata_scr_read(link
, SCR_CONTROL
, &scontrol
)))
345 if (!__sata_set_spd_needed(link
, &scontrol
))
348 if ((rc
= sata_scr_write(link
, SCR_CONTROL
, scontrol
)))
353 EXPORT_SYMBOL_GPL(sata_set_spd
);
356 * ata_slave_link_init - initialize slave link
357 * @ap: port to initialize slave link for
359 * Create and initialize slave link for @ap. This enables slave
360 * link handling on the port.
362 * In libata, a port contains links and a link contains devices.
363 * There is single host link but if a PMP is attached to it,
364 * there can be multiple fan-out links. On SATA, there's usually
365 * a single device connected to a link but PATA and SATA
366 * controllers emulating TF based interface can have two - master
369 * However, there are a few controllers which don't fit into this
370 * abstraction too well - SATA controllers which emulate TF
371 * interface with both master and slave devices but also have
372 * separate SCR register sets for each device. These controllers
373 * need separate links for physical link handling
374 * (e.g. onlineness, link speed) but should be treated like a
375 * traditional M/S controller for everything else (e.g. command
378 * slave_link is libata's way of handling this class of
379 * controllers without impacting core layer too much. For
380 * anything other than physical link handling, the default host
381 * link is used for both master and slave. For physical link
382 * handling, separate @ap->slave_link is used. All dirty details
383 * are implemented inside libata core layer. From LLD's POV, the
384 * only difference is that prereset, hardreset and postreset are
385 * called once more for the slave link, so the reset sequence
386 * looks like the following.
388 * prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
389 * softreset(M) -> postreset(M) -> postreset(S)
391 * Note that softreset is called only for the master. Softreset
392 * resets both M/S by definition, so SRST on master should handle
393 * both (the standard method will work just fine).
396 * Should be called before host is registered.
399 * 0 on success, -errno on failure.
401 int ata_slave_link_init(struct ata_port
*ap
)
403 struct ata_link
*link
;
405 WARN_ON(ap
->slave_link
);
406 WARN_ON(ap
->flags
& ATA_FLAG_PMP
);
408 link
= kzalloc(sizeof(*link
), GFP_KERNEL
);
412 ata_link_init(ap
, link
, 1);
413 ap
->slave_link
= link
;
416 EXPORT_SYMBOL_GPL(ata_slave_link_init
);
419 * sata_lpm_ignore_phy_events - test if PHY event should be ignored
420 * @link: Link receiving the event
422 * Test whether the received PHY event has to be ignored or not.
428 * True if the event has to be ignored.
430 bool sata_lpm_ignore_phy_events(struct ata_link
*link
)
432 unsigned long lpm_timeout
= link
->last_lpm_change
+
433 msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY
);
435 /* if LPM is enabled, PHYRDY doesn't mean anything */
436 if (link
->lpm_policy
> ATA_LPM_MAX_POWER
)
439 /* ignore the first PHY event after the LPM policy changed
440 * as it is might be spurious
442 if ((link
->flags
& ATA_LFLAG_CHANGED
) &&
443 time_before(jiffies
, lpm_timeout
))
448 EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events
);