blob: 341699f5af200288a142a877954e9c2bbe918314 [file] [log] [blame]
Bartlomiej Zolnierkiewicz7fe183c2020-03-26 16:58:11 +01001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * SATA specific part of ATA helper library
4 *
5 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
6 * Copyright 2003-2004 Jeff Garzik
7 */
8
9#include <linux/kernel.h>
10#include <linux/module.h>
Bartlomiej Zolnierkiewiczec811a92020-03-26 16:58:18 +010011#include <scsi/scsi_device.h>
Bartlomiej Zolnierkiewicz7fe183c2020-03-26 16:58:11 +010012#include <linux/libata.h>
13
14#include "libata.h"
15
Bartlomiej Zolnierkiewicz2b384ed2020-03-26 16:58:17 +010016/* debounce timing parameters in msecs { interval, duration, timeout } */
17const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
18EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
19const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
20EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
21const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
22EXPORT_SYMBOL_GPL(sata_deb_timing_long);
23
Bartlomiej Zolnierkiewicz7fe183c2020-03-26 16:58:11 +010024/**
Bartlomiej Zolnierkiewicz6eab1bc2020-03-26 16:58:12 +010025 * sata_scr_valid - test whether SCRs are accessible
26 * @link: ATA link to test SCR accessibility for
27 *
28 * Test whether SCRs are accessible for @link.
29 *
30 * LOCKING:
31 * None.
32 *
33 * RETURNS:
34 * 1 if SCRs are accessible, 0 otherwise.
35 */
36int sata_scr_valid(struct ata_link *link)
37{
38 struct ata_port *ap = link->ap;
39
40 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
41}
42EXPORT_SYMBOL_GPL(sata_scr_valid);
43
44/**
45 * sata_scr_read - read SCR register of the specified port
46 * @link: ATA link to read SCR for
47 * @reg: SCR to read
48 * @val: Place to store read value
49 *
50 * Read SCR register @reg of @link into *@val. This function is
51 * guaranteed to succeed if @link is ap->link, the cable type of
52 * the port is SATA and the port implements ->scr_read.
53 *
54 * LOCKING:
55 * None if @link is ap->link. Kernel thread context otherwise.
56 *
57 * RETURNS:
58 * 0 on success, negative errno on failure.
59 */
60int sata_scr_read(struct ata_link *link, int reg, u32 *val)
61{
62 if (ata_is_host_link(link)) {
63 if (sata_scr_valid(link))
64 return link->ap->ops->scr_read(link, reg, val);
65 return -EOPNOTSUPP;
66 }
67
68 return sata_pmp_scr_read(link, reg, val);
69}
70EXPORT_SYMBOL_GPL(sata_scr_read);
71
72/**
73 * sata_scr_write - write SCR register of the specified port
74 * @link: ATA link to write SCR for
75 * @reg: SCR to write
76 * @val: value to write
77 *
78 * Write @val to SCR register @reg of @link. This function is
79 * guaranteed to succeed if @link is ap->link, the cable type of
80 * the port is SATA and the port implements ->scr_read.
81 *
82 * LOCKING:
83 * None if @link is ap->link. Kernel thread context otherwise.
84 *
85 * RETURNS:
86 * 0 on success, negative errno on failure.
87 */
88int sata_scr_write(struct ata_link *link, int reg, u32 val)
89{
90 if (ata_is_host_link(link)) {
91 if (sata_scr_valid(link))
92 return link->ap->ops->scr_write(link, reg, val);
93 return -EOPNOTSUPP;
94 }
95
96 return sata_pmp_scr_write(link, reg, val);
97}
98EXPORT_SYMBOL_GPL(sata_scr_write);
99
100/**
101 * sata_scr_write_flush - write SCR register of the specified port and flush
102 * @link: ATA link to write SCR for
103 * @reg: SCR to write
104 * @val: value to write
105 *
106 * This function is identical to sata_scr_write() except that this
107 * function performs flush after writing to the register.
108 *
109 * LOCKING:
110 * None if @link is ap->link. Kernel thread context otherwise.
111 *
112 * RETURNS:
113 * 0 on success, negative errno on failure.
114 */
115int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
116{
117 if (ata_is_host_link(link)) {
118 int rc;
119
120 if (sata_scr_valid(link)) {
121 rc = link->ap->ops->scr_write(link, reg, val);
122 if (rc == 0)
123 rc = link->ap->ops->scr_read(link, reg, &val);
124 return rc;
125 }
126 return -EOPNOTSUPP;
127 }
128
129 return sata_pmp_scr_write(link, reg, val);
130}
131EXPORT_SYMBOL_GPL(sata_scr_write_flush);
132
133/**
Bartlomiej Zolnierkiewicz7fe183c2020-03-26 16:58:11 +0100134 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
135 * @tf: Taskfile to convert
136 * @pmp: Port multiplier port
137 * @is_cmd: This FIS is for command
138 * @fis: Buffer into which data will output
139 *
140 * Converts a standard ATA taskfile to a Serial ATA
141 * FIS structure (Register - Host to Device).
142 *
143 * LOCKING:
144 * Inherited from caller.
145 */
146void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
147{
148 fis[0] = 0x27; /* Register - Host to Device FIS */
149 fis[1] = pmp & 0xf; /* Port multiplier number*/
150 if (is_cmd)
151 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */
152
153 fis[2] = tf->command;
154 fis[3] = tf->feature;
155
156 fis[4] = tf->lbal;
157 fis[5] = tf->lbam;
158 fis[6] = tf->lbah;
159 fis[7] = tf->device;
160
161 fis[8] = tf->hob_lbal;
162 fis[9] = tf->hob_lbam;
163 fis[10] = tf->hob_lbah;
164 fis[11] = tf->hob_feature;
165
166 fis[12] = tf->nsect;
167 fis[13] = tf->hob_nsect;
168 fis[14] = 0;
169 fis[15] = tf->ctl;
170
171 fis[16] = tf->auxiliary & 0xff;
172 fis[17] = (tf->auxiliary >> 8) & 0xff;
173 fis[18] = (tf->auxiliary >> 16) & 0xff;
174 fis[19] = (tf->auxiliary >> 24) & 0xff;
175}
176EXPORT_SYMBOL_GPL(ata_tf_to_fis);
177
178/**
179 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile
180 * @fis: Buffer from which data will be input
181 * @tf: Taskfile to output
182 *
183 * Converts a serial ATA FIS structure to a standard ATA taskfile.
184 *
185 * LOCKING:
186 * Inherited from caller.
187 */
188
189void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
190{
191 tf->command = fis[2]; /* status */
192 tf->feature = fis[3]; /* error */
193
194 tf->lbal = fis[4];
195 tf->lbam = fis[5];
196 tf->lbah = fis[6];
197 tf->device = fis[7];
198
199 tf->hob_lbal = fis[8];
200 tf->hob_lbam = fis[9];
201 tf->hob_lbah = fis[10];
202
203 tf->nsect = fis[12];
204 tf->hob_nsect = fis[13];
205}
206EXPORT_SYMBOL_GPL(ata_tf_from_fis);
207
208/**
Bartlomiej Zolnierkiewicz9d3158f2020-03-26 16:58:14 +0100209 * sata_link_debounce - debounce SATA phy status
210 * @link: ATA link to debounce SATA phy status for
211 * @params: timing parameters { interval, duration, timeout } in msec
212 * @deadline: deadline jiffies for the operation
213 *
214 * Make sure SStatus of @link reaches stable state, determined by
215 * holding the same value where DET is not 1 for @duration polled
216 * every @interval, before @timeout. Timeout constraints the
217 * beginning of the stable state. Because DET gets stuck at 1 on
218 * some controllers after hot unplugging, this functions waits
219 * until timeout then returns 0 if DET is stable at 1.
220 *
221 * @timeout is further limited by @deadline. The sooner of the
222 * two is used.
223 *
224 * LOCKING:
225 * Kernel thread context (may sleep)
226 *
227 * RETURNS:
228 * 0 on success, -errno on failure.
229 */
230int sata_link_debounce(struct ata_link *link, const unsigned long *params,
231 unsigned long deadline)
232{
233 unsigned long interval = params[0];
234 unsigned long duration = params[1];
235 unsigned long last_jiffies, t;
236 u32 last, cur;
237 int rc;
238
239 t = ata_deadline(jiffies, params[2]);
240 if (time_before(t, deadline))
241 deadline = t;
242
243 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
244 return rc;
245 cur &= 0xf;
246
247 last = cur;
248 last_jiffies = jiffies;
249
250 while (1) {
251 ata_msleep(link->ap, interval);
252 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
253 return rc;
254 cur &= 0xf;
255
256 /* DET stable? */
257 if (cur == last) {
258 if (cur == 1 && time_before(jiffies, deadline))
259 continue;
260 if (time_after(jiffies,
261 ata_deadline(last_jiffies, duration)))
262 return 0;
263 continue;
264 }
265
266 /* unstable, start over */
267 last = cur;
268 last_jiffies = jiffies;
269
270 /* Check deadline. If debouncing failed, return
271 * -EPIPE to tell upper layer to lower link speed.
272 */
273 if (time_after(jiffies, deadline))
274 return -EPIPE;
275 }
276}
277EXPORT_SYMBOL_GPL(sata_link_debounce);
278
279/**
280 * sata_link_resume - resume SATA link
281 * @link: ATA link to resume SATA
282 * @params: timing parameters { interval, duration, timeout } in msec
283 * @deadline: deadline jiffies for the operation
284 *
285 * Resume SATA phy @link and debounce it.
286 *
287 * LOCKING:
288 * Kernel thread context (may sleep)
289 *
290 * RETURNS:
291 * 0 on success, -errno on failure.
292 */
293int sata_link_resume(struct ata_link *link, const unsigned long *params,
294 unsigned long deadline)
295{
296 int tries = ATA_LINK_RESUME_TRIES;
297 u32 scontrol, serror;
298 int rc;
299
300 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
301 return rc;
302
303 /*
304 * Writes to SControl sometimes get ignored under certain
305 * controllers (ata_piix SIDPR). Make sure DET actually is
306 * cleared.
307 */
308 do {
309 scontrol = (scontrol & 0x0f0) | 0x300;
310 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
311 return rc;
312 /*
313 * Some PHYs react badly if SStatus is pounded
314 * immediately after resuming. Delay 200ms before
315 * debouncing.
316 */
317 if (!(link->flags & ATA_LFLAG_NO_DB_DELAY))
318 ata_msleep(link->ap, 200);
319
320 /* is SControl restored correctly? */
321 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
322 return rc;
323 } while ((scontrol & 0xf0f) != 0x300 && --tries);
324
325 if ((scontrol & 0xf0f) != 0x300) {
326 ata_link_warn(link, "failed to resume link (SControl %X)\n",
327 scontrol);
328 return 0;
329 }
330
331 if (tries < ATA_LINK_RESUME_TRIES)
332 ata_link_warn(link, "link resume succeeded after %d retries\n",
333 ATA_LINK_RESUME_TRIES - tries);
334
335 if ((rc = sata_link_debounce(link, params, deadline)))
336 return rc;
337
338 /* clear SError, some PHYs require this even for SRST to work */
339 if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
340 rc = sata_scr_write(link, SCR_ERROR, serror);
341
342 return rc != -EINVAL ? rc : 0;
343}
344EXPORT_SYMBOL_GPL(sata_link_resume);
345
346/**
Bartlomiej Zolnierkiewicz7fe183c2020-03-26 16:58:11 +0100347 * sata_link_scr_lpm - manipulate SControl IPM and SPM fields
348 * @link: ATA link to manipulate SControl for
349 * @policy: LPM policy to configure
350 * @spm_wakeup: initiate LPM transition to active state
351 *
352 * Manipulate the IPM field of the SControl register of @link
353 * according to @policy. If @policy is ATA_LPM_MAX_POWER and
354 * @spm_wakeup is %true, the SPM field is manipulated to wake up
355 * the link. This function also clears PHYRDY_CHG before
356 * returning.
357 *
358 * LOCKING:
359 * EH context.
360 *
361 * RETURNS:
362 * 0 on success, -errno otherwise.
363 */
364int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
365 bool spm_wakeup)
366{
367 struct ata_eh_context *ehc = &link->eh_context;
368 bool woken_up = false;
369 u32 scontrol;
370 int rc;
371
372 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
373 if (rc)
374 return rc;
375
376 switch (policy) {
377 case ATA_LPM_MAX_POWER:
378 /* disable all LPM transitions */
379 scontrol |= (0x7 << 8);
380 /* initiate transition to active state */
381 if (spm_wakeup) {
382 scontrol |= (0x4 << 12);
383 woken_up = true;
384 }
385 break;
386 case ATA_LPM_MED_POWER:
387 /* allow LPM to PARTIAL */
388 scontrol &= ~(0x1 << 8);
389 scontrol |= (0x6 << 8);
390 break;
391 case ATA_LPM_MED_POWER_WITH_DIPM:
392 case ATA_LPM_MIN_POWER_WITH_PARTIAL:
393 case ATA_LPM_MIN_POWER:
394 if (ata_link_nr_enabled(link) > 0)
395 /* no restrictions on LPM transitions */
396 scontrol &= ~(0x7 << 8);
397 else {
398 /* empty port, power off */
399 scontrol &= ~0xf;
400 scontrol |= (0x1 << 2);
401 }
402 break;
403 default:
404 WARN_ON(1);
405 }
406
407 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
408 if (rc)
409 return rc;
410
411 /* give the link time to transit out of LPM state */
412 if (woken_up)
413 msleep(10);
414
415 /* clear PHYRDY_CHG from SError */
416 ehc->i.serror &= ~SERR_PHYRDY_CHG;
417 return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
418}
419EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
420
Bartlomiej Zolnierkiewiczab4117c2020-03-26 16:58:13 +0100421static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
422{
423 struct ata_link *host_link = &link->ap->link;
424 u32 limit, target, spd;
425
426 limit = link->sata_spd_limit;
427
428 /* Don't configure downstream link faster than upstream link.
429 * It doesn't speed up anything and some PMPs choke on such
430 * configuration.
431 */
432 if (!ata_is_host_link(link) && host_link->sata_spd)
433 limit &= (1 << host_link->sata_spd) - 1;
434
435 if (limit == UINT_MAX)
436 target = 0;
437 else
438 target = fls(limit);
439
440 spd = (*scontrol >> 4) & 0xf;
441 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
442
443 return spd != target;
444}
445
446/**
447 * sata_set_spd_needed - is SATA spd configuration needed
448 * @link: Link in question
449 *
450 * Test whether the spd limit in SControl matches
451 * @link->sata_spd_limit. This function is used to determine
452 * whether hardreset is necessary to apply SATA spd
453 * configuration.
454 *
455 * LOCKING:
456 * Inherited from caller.
457 *
458 * RETURNS:
459 * 1 if SATA spd configuration is needed, 0 otherwise.
460 */
Bartlomiej Zolnierkiewicz78c97c82020-03-26 16:58:15 +0100461static int sata_set_spd_needed(struct ata_link *link)
Bartlomiej Zolnierkiewiczab4117c2020-03-26 16:58:13 +0100462{
463 u32 scontrol;
464
465 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
466 return 1;
467
468 return __sata_set_spd_needed(link, &scontrol);
469}
470
471/**
472 * sata_set_spd - set SATA spd according to spd limit
473 * @link: Link to set SATA spd for
474 *
475 * Set SATA spd of @link according to sata_spd_limit.
476 *
477 * LOCKING:
478 * Inherited from caller.
479 *
480 * RETURNS:
481 * 0 if spd doesn't need to be changed, 1 if spd has been
482 * changed. Negative errno if SCR registers are inaccessible.
483 */
484int sata_set_spd(struct ata_link *link)
485{
486 u32 scontrol;
487 int rc;
488
489 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
490 return rc;
491
492 if (!__sata_set_spd_needed(link, &scontrol))
493 return 0;
494
495 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
496 return rc;
497
498 return 1;
499}
500EXPORT_SYMBOL_GPL(sata_set_spd);
501
Bartlomiej Zolnierkiewicz7fe183c2020-03-26 16:58:11 +0100502/**
Bartlomiej Zolnierkiewicz78c97c82020-03-26 16:58:15 +0100503 * sata_link_hardreset - reset link via SATA phy reset
504 * @link: link to reset
505 * @timing: timing parameters { interval, duration, timeout } in msec
506 * @deadline: deadline jiffies for the operation
507 * @online: optional out parameter indicating link onlineness
508 * @check_ready: optional callback to check link readiness
509 *
510 * SATA phy-reset @link using DET bits of SControl register.
511 * After hardreset, link readiness is waited upon using
512 * ata_wait_ready() if @check_ready is specified. LLDs are
513 * allowed to not specify @check_ready and wait itself after this
514 * function returns. Device classification is LLD's
515 * responsibility.
516 *
517 * *@online is set to one iff reset succeeded and @link is online
518 * after reset.
519 *
520 * LOCKING:
521 * Kernel thread context (may sleep)
522 *
523 * RETURNS:
524 * 0 on success, -errno otherwise.
525 */
526int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
527 unsigned long deadline,
528 bool *online, int (*check_ready)(struct ata_link *))
529{
530 u32 scontrol;
531 int rc;
532
533 DPRINTK("ENTER\n");
534
535 if (online)
536 *online = false;
537
538 if (sata_set_spd_needed(link)) {
539 /* SATA spec says nothing about how to reconfigure
540 * spd. To be on the safe side, turn off phy during
541 * reconfiguration. This works for at least ICH7 AHCI
542 * and Sil3124.
543 */
544 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
545 goto out;
546
547 scontrol = (scontrol & 0x0f0) | 0x304;
548
549 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
550 goto out;
551
552 sata_set_spd(link);
553 }
554
555 /* issue phy wake/reset */
556 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
557 goto out;
558
559 scontrol = (scontrol & 0x0f0) | 0x301;
560
561 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
562 goto out;
563
564 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1
565 * 10.4.2 says at least 1 ms.
566 */
567 ata_msleep(link->ap, 1);
568
569 /* bring link back */
570 rc = sata_link_resume(link, timing, deadline);
571 if (rc)
572 goto out;
573 /* if link is offline nothing more to do */
574 if (ata_phys_link_offline(link))
575 goto out;
576
577 /* Link is online. From this point, -ENODEV too is an error. */
578 if (online)
579 *online = true;
580
581 if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
582 /* If PMP is supported, we have to do follow-up SRST.
583 * Some PMPs don't send D2H Reg FIS after hardreset if
584 * the first port is empty. Wait only for
585 * ATA_TMOUT_PMP_SRST_WAIT.
586 */
587 if (check_ready) {
588 unsigned long pmp_deadline;
589
590 pmp_deadline = ata_deadline(jiffies,
591 ATA_TMOUT_PMP_SRST_WAIT);
592 if (time_after(pmp_deadline, deadline))
593 pmp_deadline = deadline;
594 ata_wait_ready(link, pmp_deadline, check_ready);
595 }
596 rc = -EAGAIN;
597 goto out;
598 }
599
600 rc = 0;
601 if (check_ready)
602 rc = ata_wait_ready(link, deadline, check_ready);
603 out:
604 if (rc && rc != -EAGAIN) {
605 /* online is set iff link is online && reset succeeded */
606 if (online)
607 *online = false;
608 ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
609 }
610 DPRINTK("EXIT, rc=%d\n", rc);
611 return rc;
612}
613EXPORT_SYMBOL_GPL(sata_link_hardreset);
614
615/**
Bartlomiej Zolnierkiewicz61a11982020-03-26 16:58:16 +0100616 * ata_qc_complete_multiple - Complete multiple qcs successfully
617 * @ap: port in question
618 * @qc_active: new qc_active mask
619 *
620 * Complete in-flight commands. This functions is meant to be
621 * called from low-level driver's interrupt routine to complete
622 * requests normally. ap->qc_active and @qc_active is compared
623 * and commands are completed accordingly.
624 *
625 * Always use this function when completing multiple NCQ commands
626 * from IRQ handlers instead of calling ata_qc_complete()
627 * multiple times to keep IRQ expect status properly in sync.
628 *
629 * LOCKING:
630 * spin_lock_irqsave(host lock)
631 *
632 * RETURNS:
633 * Number of completed commands on success, -errno otherwise.
634 */
635int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active)
636{
637 u64 done_mask, ap_qc_active = ap->qc_active;
638 int nr_done = 0;
639
640 /*
641 * If the internal tag is set on ap->qc_active, then we care about
642 * bit0 on the passed in qc_active mask. Move that bit up to match
643 * the internal tag.
644 */
645 if (ap_qc_active & (1ULL << ATA_TAG_INTERNAL)) {
646 qc_active |= (qc_active & 0x01) << ATA_TAG_INTERNAL;
647 qc_active ^= qc_active & 0x01;
648 }
649
650 done_mask = ap_qc_active ^ qc_active;
651
652 if (unlikely(done_mask & qc_active)) {
653 ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n",
654 ap->qc_active, qc_active);
655 return -EINVAL;
656 }
657
658 while (done_mask) {
659 struct ata_queued_cmd *qc;
660 unsigned int tag = __ffs64(done_mask);
661
662 qc = ata_qc_from_tag(ap, tag);
663 if (qc) {
664 ata_qc_complete(qc);
665 nr_done++;
666 }
667 done_mask &= ~(1ULL << tag);
668 }
669
670 return nr_done;
671}
672EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
673
674/**
Bartlomiej Zolnierkiewicz7fe183c2020-03-26 16:58:11 +0100675 * ata_slave_link_init - initialize slave link
676 * @ap: port to initialize slave link for
677 *
678 * Create and initialize slave link for @ap. This enables slave
679 * link handling on the port.
680 *
681 * In libata, a port contains links and a link contains devices.
682 * There is single host link but if a PMP is attached to it,
683 * there can be multiple fan-out links. On SATA, there's usually
684 * a single device connected to a link but PATA and SATA
685 * controllers emulating TF based interface can have two - master
686 * and slave.
687 *
688 * However, there are a few controllers which don't fit into this
689 * abstraction too well - SATA controllers which emulate TF
690 * interface with both master and slave devices but also have
691 * separate SCR register sets for each device. These controllers
692 * need separate links for physical link handling
693 * (e.g. onlineness, link speed) but should be treated like a
694 * traditional M/S controller for everything else (e.g. command
695 * issue, softreset).
696 *
697 * slave_link is libata's way of handling this class of
698 * controllers without impacting core layer too much. For
699 * anything other than physical link handling, the default host
700 * link is used for both master and slave. For physical link
701 * handling, separate @ap->slave_link is used. All dirty details
702 * are implemented inside libata core layer. From LLD's POV, the
703 * only difference is that prereset, hardreset and postreset are
704 * called once more for the slave link, so the reset sequence
705 * looks like the following.
706 *
707 * prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
708 * softreset(M) -> postreset(M) -> postreset(S)
709 *
710 * Note that softreset is called only for the master. Softreset
711 * resets both M/S by definition, so SRST on master should handle
712 * both (the standard method will work just fine).
713 *
714 * LOCKING:
715 * Should be called before host is registered.
716 *
717 * RETURNS:
718 * 0 on success, -errno on failure.
719 */
720int ata_slave_link_init(struct ata_port *ap)
721{
722 struct ata_link *link;
723
724 WARN_ON(ap->slave_link);
725 WARN_ON(ap->flags & ATA_FLAG_PMP);
726
727 link = kzalloc(sizeof(*link), GFP_KERNEL);
728 if (!link)
729 return -ENOMEM;
730
731 ata_link_init(ap, link, 1);
732 ap->slave_link = link;
733 return 0;
734}
735EXPORT_SYMBOL_GPL(ata_slave_link_init);
736
737/**
738 * sata_lpm_ignore_phy_events - test if PHY event should be ignored
739 * @link: Link receiving the event
740 *
741 * Test whether the received PHY event has to be ignored or not.
742 *
743 * LOCKING:
744 * None:
745 *
746 * RETURNS:
747 * True if the event has to be ignored.
748 */
749bool sata_lpm_ignore_phy_events(struct ata_link *link)
750{
751 unsigned long lpm_timeout = link->last_lpm_change +
752 msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);
753
754 /* if LPM is enabled, PHYRDY doesn't mean anything */
755 if (link->lpm_policy > ATA_LPM_MAX_POWER)
756 return true;
757
758 /* ignore the first PHY event after the LPM policy changed
759 * as it is might be spurious
760 */
761 if ((link->flags & ATA_LFLAG_CHANGED) &&
762 time_before(jiffies, lpm_timeout))
763 return true;
764
765 return false;
766}
767EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);
Bartlomiej Zolnierkiewiczec811a92020-03-26 16:58:18 +0100768
769static const char *ata_lpm_policy_names[] = {
770 [ATA_LPM_UNKNOWN] = "max_performance",
771 [ATA_LPM_MAX_POWER] = "max_performance",
772 [ATA_LPM_MED_POWER] = "medium_power",
773 [ATA_LPM_MED_POWER_WITH_DIPM] = "med_power_with_dipm",
774 [ATA_LPM_MIN_POWER_WITH_PARTIAL] = "min_power_with_partial",
775 [ATA_LPM_MIN_POWER] = "min_power",
776};
777
778static ssize_t ata_scsi_lpm_store(struct device *device,
779 struct device_attribute *attr,
780 const char *buf, size_t count)
781{
782 struct Scsi_Host *shost = class_to_shost(device);
783 struct ata_port *ap = ata_shost_to_port(shost);
784 struct ata_link *link;
785 struct ata_device *dev;
786 enum ata_lpm_policy policy;
787 unsigned long flags;
788
789 /* UNKNOWN is internal state, iterate from MAX_POWER */
790 for (policy = ATA_LPM_MAX_POWER;
791 policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
792 const char *name = ata_lpm_policy_names[policy];
793
794 if (strncmp(name, buf, strlen(name)) == 0)
795 break;
796 }
797 if (policy == ARRAY_SIZE(ata_lpm_policy_names))
798 return -EINVAL;
799
800 spin_lock_irqsave(ap->lock, flags);
801
802 ata_for_each_link(link, ap, EDGE) {
803 ata_for_each_dev(dev, &ap->link, ENABLED) {
804 if (dev->horkage & ATA_HORKAGE_NOLPM) {
805 count = -EOPNOTSUPP;
806 goto out_unlock;
807 }
808 }
809 }
810
811 ap->target_lpm_policy = policy;
812 ata_port_schedule_eh(ap);
813out_unlock:
814 spin_unlock_irqrestore(ap->lock, flags);
815 return count;
816}
817
818static ssize_t ata_scsi_lpm_show(struct device *dev,
819 struct device_attribute *attr, char *buf)
820{
821 struct Scsi_Host *shost = class_to_shost(dev);
822 struct ata_port *ap = ata_shost_to_port(shost);
823
824 if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
825 return -EINVAL;
826
827 return snprintf(buf, PAGE_SIZE, "%s\n",
828 ata_lpm_policy_names[ap->target_lpm_policy]);
829}
830DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
831 ata_scsi_lpm_show, ata_scsi_lpm_store);
832EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
833
834static ssize_t ata_ncq_prio_enable_show(struct device *device,
835 struct device_attribute *attr,
836 char *buf)
837{
838 struct scsi_device *sdev = to_scsi_device(device);
839 struct ata_port *ap;
840 struct ata_device *dev;
841 bool ncq_prio_enable;
842 int rc = 0;
843
844 ap = ata_shost_to_port(sdev->host);
845
846 spin_lock_irq(ap->lock);
847 dev = ata_scsi_find_dev(ap, sdev);
848 if (!dev) {
849 rc = -ENODEV;
850 goto unlock;
851 }
852
853 ncq_prio_enable = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE;
854
855unlock:
856 spin_unlock_irq(ap->lock);
857
858 return rc ? rc : snprintf(buf, 20, "%u\n", ncq_prio_enable);
859}
860
861static ssize_t ata_ncq_prio_enable_store(struct device *device,
862 struct device_attribute *attr,
863 const char *buf, size_t len)
864{
865 struct scsi_device *sdev = to_scsi_device(device);
866 struct ata_port *ap;
867 struct ata_device *dev;
868 long int input;
869 int rc;
870
871 rc = kstrtol(buf, 10, &input);
872 if (rc)
873 return rc;
874 if ((input < 0) || (input > 1))
875 return -EINVAL;
876
877 ap = ata_shost_to_port(sdev->host);
878 dev = ata_scsi_find_dev(ap, sdev);
879 if (unlikely(!dev))
880 return -ENODEV;
881
882 spin_lock_irq(ap->lock);
883 if (input)
884 dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLE;
885 else
886 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
887
888 dev->link->eh_info.action |= ATA_EH_REVALIDATE;
889 dev->link->eh_info.flags |= ATA_EHI_QUIET;
890 ata_port_schedule_eh(ap);
891 spin_unlock_irq(ap->lock);
892
893 ata_port_wait_eh(ap);
894
895 if (input) {
896 spin_lock_irq(ap->lock);
897 if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) {
898 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
899 rc = -EIO;
900 }
901 spin_unlock_irq(ap->lock);
902 }
903
904 return rc ? rc : len;
905}
906
907DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR,
908 ata_ncq_prio_enable_show, ata_ncq_prio_enable_store);
909EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable);
910
911struct device_attribute *ata_ncq_sdev_attrs[] = {
912 &dev_attr_unload_heads,
913 &dev_attr_ncq_prio_enable,
914 NULL
915};
916EXPORT_SYMBOL_GPL(ata_ncq_sdev_attrs);
917
918static ssize_t
919ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
920 const char *buf, size_t count)
921{
922 struct Scsi_Host *shost = class_to_shost(dev);
923 struct ata_port *ap = ata_shost_to_port(shost);
924 if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
925 return ap->ops->em_store(ap, buf, count);
926 return -EINVAL;
927}
928
929static ssize_t
930ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
931 char *buf)
932{
933 struct Scsi_Host *shost = class_to_shost(dev);
934 struct ata_port *ap = ata_shost_to_port(shost);
935
936 if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
937 return ap->ops->em_show(ap, buf);
938 return -EINVAL;
939}
940DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
941 ata_scsi_em_message_show, ata_scsi_em_message_store);
942EXPORT_SYMBOL_GPL(dev_attr_em_message);
943
944static ssize_t
945ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
946 char *buf)
947{
948 struct Scsi_Host *shost = class_to_shost(dev);
949 struct ata_port *ap = ata_shost_to_port(shost);
950
951 return snprintf(buf, 23, "%d\n", ap->em_message_type);
952}
953DEVICE_ATTR(em_message_type, S_IRUGO,
954 ata_scsi_em_message_type_show, NULL);
955EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
956
957static ssize_t
958ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
959 char *buf)
960{
961 struct scsi_device *sdev = to_scsi_device(dev);
962 struct ata_port *ap = ata_shost_to_port(sdev->host);
963 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
964
965 if (atadev && ap->ops->sw_activity_show &&
966 (ap->flags & ATA_FLAG_SW_ACTIVITY))
967 return ap->ops->sw_activity_show(atadev, buf);
968 return -EINVAL;
969}
970
971static ssize_t
972ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
973 const char *buf, size_t count)
974{
975 struct scsi_device *sdev = to_scsi_device(dev);
976 struct ata_port *ap = ata_shost_to_port(sdev->host);
977 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
978 enum sw_activity val;
979 int rc;
980
981 if (atadev && ap->ops->sw_activity_store &&
982 (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
983 val = simple_strtoul(buf, NULL, 0);
984 switch (val) {
985 case OFF: case BLINK_ON: case BLINK_OFF:
986 rc = ap->ops->sw_activity_store(atadev, val);
987 if (!rc)
988 return count;
989 else
990 return rc;
991 }
992 }
993 return -EINVAL;
994}
995DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
996 ata_scsi_activity_store);
997EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
998
999/**
1000 * __ata_change_queue_depth - helper for ata_scsi_change_queue_depth
1001 * @ap: ATA port to which the device change the queue depth
1002 * @sdev: SCSI device to configure queue depth for
1003 * @queue_depth: new queue depth
1004 *
1005 * libsas and libata have different approaches for associating a sdev to
1006 * its ata_port.
1007 *
1008 */
1009int __ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
1010 int queue_depth)
1011{
1012 struct ata_device *dev;
1013 unsigned long flags;
1014
1015 if (queue_depth < 1 || queue_depth == sdev->queue_depth)
1016 return sdev->queue_depth;
1017
1018 dev = ata_scsi_find_dev(ap, sdev);
1019 if (!dev || !ata_dev_enabled(dev))
1020 return sdev->queue_depth;
1021
1022 /* NCQ enabled? */
1023 spin_lock_irqsave(ap->lock, flags);
1024 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1025 if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
1026 dev->flags |= ATA_DFLAG_NCQ_OFF;
1027 queue_depth = 1;
1028 }
1029 spin_unlock_irqrestore(ap->lock, flags);
1030
1031 /* limit and apply queue depth */
1032 queue_depth = min(queue_depth, sdev->host->can_queue);
1033 queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
1034 queue_depth = min(queue_depth, ATA_MAX_QUEUE);
1035
1036 if (sdev->queue_depth == queue_depth)
1037 return -EINVAL;
1038
1039 return scsi_change_queue_depth(sdev, queue_depth);
1040}
1041EXPORT_SYMBOL_GPL(__ata_change_queue_depth);
1042
1043/**
1044 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
1045 * @sdev: SCSI device to configure queue depth for
1046 * @queue_depth: new queue depth
1047 *
1048 * This is libata standard hostt->change_queue_depth callback.
1049 * SCSI will call into this callback when user tries to set queue
1050 * depth via sysfs.
1051 *
1052 * LOCKING:
1053 * SCSI layer (we don't care)
1054 *
1055 * RETURNS:
1056 * Newly configured queue depth.
1057 */
1058int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
1059{
1060 struct ata_port *ap = ata_shost_to_port(sdev->host);
1061
1062 return __ata_change_queue_depth(ap, sdev, queue_depth);
1063}
1064EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);