projects/linux·blob master · refs

1118 lines · 26.3 KiB

1// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * libata-pmp.c - libata port multiplier support
4 *
5 * Copyright (c) 2007 SUSE Linux Products GmbH
6 * Copyright (c) 2007 Tejun Heo <[email protected]>
7 */
8
9#include <linux/kernel.h>
10#include <linux/export.h>
11#include <linux/libata.h>
12#include <linux/slab.h>
13#include "libata.h"
14#include "libata-transport.h"
15
16const struct ata_port_operations sata_pmp_port_ops = {
17 .inherits = &sata_port_ops,
18 .pmp_reset.prereset = ata_std_prereset,
19 .pmp_reset.hardreset = sata_std_hardreset,
20 .pmp_reset.postreset = ata_std_postreset,
21 .error_handler = sata_pmp_error_handler,
22};
23
24/**
25 * sata_pmp_read - read PMP register
26 * @link: link to read PMP register for
27 * @reg: register to read
28 * @r_val: resulting value
29 *
30 * Read PMP register.
31 *
32 * LOCKING:
33 * Kernel thread context (may sleep).
34 *
35 * RETURNS:
36 * 0 on success, AC_ERR_* mask on failure.
37 */
38static unsigned int sata_pmp_read(struct ata_link *link, int reg, u32 *r_val)
39{
40 struct ata_port *ap = link->ap;
41 struct ata_device *pmp_dev = ap->link.device;
42 struct ata_taskfile tf;
43 unsigned int err_mask;
44
45 ata_tf_init(pmp_dev, &tf);
46 tf.command = ATA_CMD_PMP_READ;
47 tf.protocol = ATA_PROT_NODATA;
48 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
49 tf.feature = reg;
50 tf.device = link->pmp;
51
52 err_mask = ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
53 SATA_PMP_RW_TIMEOUT);
54 if (err_mask)
55 return err_mask;
56
57 *r_val = tf.nsect | tf.lbal << 8 | tf.lbam << 16 | tf.lbah << 24;
58 return 0;
59}
60
61/**
62 * sata_pmp_write - write PMP register
63 * @link: link to write PMP register for
64 * @reg: register to write
65 * @val: value to write
66 *
67 * Write PMP register.
68 *
69 * LOCKING:
70 * Kernel thread context (may sleep).
71 *
72 * RETURNS:
73 * 0 on success, AC_ERR_* mask on failure.
74 */
75static unsigned int sata_pmp_write(struct ata_link *link, int reg, u32 val)
76{
77 struct ata_port *ap = link->ap;
78 struct ata_device *pmp_dev = ap->link.device;
79 struct ata_taskfile tf;
80
81 ata_tf_init(pmp_dev, &tf);
82 tf.command = ATA_CMD_PMP_WRITE;
83 tf.protocol = ATA_PROT_NODATA;
84 tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
85 tf.feature = reg;
86 tf.device = link->pmp;
87 tf.nsect = val & 0xff;
88 tf.lbal = (val >> 8) & 0xff;
89 tf.lbam = (val >> 16) & 0xff;
90 tf.lbah = (val >> 24) & 0xff;
91
92 return ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
93 SATA_PMP_RW_TIMEOUT);
94}
95
96/**
97 * sata_pmp_qc_defer_cmd_switch - qc_defer for command switching PMP
98 * @qc: ATA command in question
99 *
100 * A host which has command switching PMP support cannot issue
101 * commands to multiple links simultaneously.
102 *
103 * LOCKING:
104 * spin_lock_irqsave(host lock)
105 *
106 * RETURNS:
107 * ATA_DEFER_* if deferring is needed, 0 otherwise.
108 */
109int sata_pmp_qc_defer_cmd_switch(struct ata_queued_cmd *qc)
110{
111 struct ata_link *link = qc->dev->link;
112 struct ata_port *ap = link->ap;
113 int ret;
114
115 if (ap->excl_link == NULL || ap->excl_link == link) {
116 if (ap->nr_active_links == 0 || ata_link_active(link)) {
117 qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
118 ret = ata_std_qc_defer(qc);
119 if (ret == ATA_DEFER_LINK)
120 return ATA_DEFER_LINK_EXCL;
121 return ret;
122 }
123
124 /*
125 * Note: ap->excl_link contains the link that is next in line,
126 * i.e. implicit round robin. If there is only one link
127 * dispatching, ap->excl_link will be left unclaimed, allowing
128 * other links to set ap->excl_link, ensuring that the currently
129 * active link cannot queue any more.
130 */
131 ap->excl_link = link;
132 }
133
134 return ATA_DEFER_PORT;
135}
136
137/**
138 * sata_pmp_scr_read - read PSCR
139 * @link: ATA link to read PSCR for
140 * @reg: PSCR to read
141 * @r_val: resulting value
142 *
143 * Read PSCR @reg into @r_val for @link, to be called from
144 * ata_scr_read().
145 *
146 * LOCKING:
147 * Kernel thread context (may sleep).
148 *
149 * RETURNS:
150 * 0 on success, -errno on failure.
151 */
152int sata_pmp_scr_read(struct ata_link *link, int reg, u32 *r_val)
153{
154 unsigned int err_mask;
155
156 if (reg > SATA_PMP_PSCR_CONTROL)
157 return -EINVAL;
158
159 err_mask = sata_pmp_read(link, reg, r_val);
160 if (err_mask) {
161 ata_link_warn(link, "failed to read SCR %d (Emask=0x%x)\n",
162 reg, err_mask);
163 return -EIO;
164 }
165 return 0;
166}
167
168/**
169 * sata_pmp_scr_write - write PSCR
170 * @link: ATA link to write PSCR for
171 * @reg: PSCR to write
172 * @val: value to be written
173 *
174 * Write @val to PSCR @reg for @link, to be called from
175 * ata_scr_write() and ata_scr_write_flush().
176 *
177 * LOCKING:
178 * Kernel thread context (may sleep).
179 *
180 * RETURNS:
181 * 0 on success, -errno on failure.
182 */
183int sata_pmp_scr_write(struct ata_link *link, int reg, u32 val)
184{
185 unsigned int err_mask;
186
187 if (reg > SATA_PMP_PSCR_CONTROL)
188 return -EINVAL;
189
190 err_mask = sata_pmp_write(link, reg, val);
191 if (err_mask) {
192 ata_link_warn(link, "failed to write SCR %d (Emask=0x%x)\n",
193 reg, err_mask);
194 return -EIO;
195 }
196 return 0;
197}
198
199/**
200 * sata_pmp_set_lpm - configure LPM for a PMP link
201 * @link: PMP link to configure LPM for
202 * @policy: target LPM policy
203 * @hints: LPM hints
204 *
205 * Configure LPM for @link. This function will contain any PMP
206 * specific workarounds if necessary.
207 *
208 * LOCKING:
209 * EH context.
210 *
211 * RETURNS:
212 * 0 on success, -errno on failure.
213 */
214int sata_pmp_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
215 unsigned hints)
216{
217 return sata_link_scr_lpm(link, policy, true);
218}
219
220/**
221 * sata_pmp_read_gscr - read GSCR block of SATA PMP
222 * @dev: PMP device
223 * @gscr: buffer to read GSCR block into
224 *
225 * Read selected PMP GSCRs from the PMP at @dev. This will serve
226 * as configuration and identification info for the PMP.
227 *
228 * LOCKING:
229 * Kernel thread context (may sleep).
230 *
231 * RETURNS:
232 * 0 on success, -errno on failure.
233 */
234static int sata_pmp_read_gscr(struct ata_device *dev, u32 *gscr)
235{
236 static const int gscr_to_read[] = { 0, 1, 2, 32, 33, 64, 96 };
237 int i;
238
239 for (i = 0; i < ARRAY_SIZE(gscr_to_read); i++) {
240 int reg = gscr_to_read[i];
241 unsigned int err_mask;
242
243 err_mask = sata_pmp_read(dev->link, reg, &gscr[reg]);
244 if (err_mask) {
245 ata_dev_err(dev, "failed to read PMP GSCR[%d] (Emask=0x%x)\n",
246 reg, err_mask);
247 return -EIO;
248 }
249 }
250
251 return 0;
252}
253
254static const char *sata_pmp_spec_rev_str(const u32 *gscr)
255{
256 u32 rev = gscr[SATA_PMP_GSCR_REV];
257
258 if (rev & (1 << 3))
259 return "1.2";
260 if (rev & (1 << 2))
261 return "1.1";
262 if (rev & (1 << 1))
263 return "1.0";
264 return "<unknown>";
265}
266
267#define PMP_GSCR_SII_POL 129
268
269static int sata_pmp_configure(struct ata_device *dev, int print_info)
270{
271 struct ata_port *ap = dev->link->ap;
272 u32 *gscr = dev->gscr;
273 u16 vendor = sata_pmp_gscr_vendor(gscr);
274 u16 devid = sata_pmp_gscr_devid(gscr);
275 unsigned int err_mask = 0;
276 const char *reason;
277 int nr_ports, rc;
278
279 nr_ports = sata_pmp_gscr_ports(gscr);
280
281 if (nr_ports <= 0 || nr_ports > SATA_PMP_MAX_PORTS) {
282 rc = -EINVAL;
283 reason = "invalid nr_ports";
284 goto fail;
285 }
286
287 if ((ap->flags & ATA_FLAG_AN) &&
288 (gscr[SATA_PMP_GSCR_FEAT] & SATA_PMP_FEAT_NOTIFY))
289 dev->flags |= ATA_DFLAG_AN;
290
291 /* monitor SERR_PHYRDY_CHG on fan-out ports */
292 err_mask = sata_pmp_write(dev->link, SATA_PMP_GSCR_ERROR_EN,
293 SERR_PHYRDY_CHG);
294 if (err_mask) {
295 rc = -EIO;
296 reason = "failed to write GSCR_ERROR_EN";
297 goto fail;
298 }
299
300 /* Disable sending Early R_OK.
301 * With "cached read" HDD testing and multiple ports busy on a SATA
302 * host controller, 3x26 PMP will very rarely drop a deferred
303 * R_OK that was intended for the host. Symptom will be all
304 * 5 drives under test will timeout, get reset, and recover.
305 */
306 if (vendor == 0x1095 && (devid == 0x3726 || devid == 0x3826)) {
307 u32 reg;
308
309 err_mask = sata_pmp_read(&ap->link, PMP_GSCR_SII_POL, &reg);
310 if (err_mask) {
311 rc = -EIO;
312 reason = "failed to read Sil3x26 Private Register";
313 goto fail;
314 }
315 reg &= ~0x1;
316 err_mask = sata_pmp_write(&ap->link, PMP_GSCR_SII_POL, reg);
317 if (err_mask) {
318 rc = -EIO;
319 reason = "failed to write Sil3x26 Private Register";
320 goto fail;
321 }
322 }
323
324 if (print_info) {
325 ata_dev_info(dev, "Port Multiplier %s, "
326 "0x%04x:0x%04x r%d, %d ports, feat 0x%x/0x%x\n",
327 sata_pmp_spec_rev_str(gscr), vendor, devid,
328 sata_pmp_gscr_rev(gscr),
329 nr_ports, gscr[SATA_PMP_GSCR_FEAT_EN],
330 gscr[SATA_PMP_GSCR_FEAT]);
331
332 if (!(dev->flags & ATA_DFLAG_AN))
333 ata_dev_info(dev,
334 "Asynchronous notification not supported, "
335 "hotplug won't work on fan-out ports. Use warm-plug instead.\n");
336 }
337
338 return 0;
339
340 fail:
341 ata_dev_err(dev,
342 "failed to configure Port Multiplier (%s, Emask=0x%x)\n",
343 reason, err_mask);
344 return rc;
345}
346
347static int sata_pmp_init_links (struct ata_port *ap, int nr_ports)
348{
349 struct ata_link *pmp_link = ap->pmp_link;
350 int i, err;
351
352 if (!pmp_link) {
353 pmp_link = kzalloc_objs(pmp_link[0], SATA_PMP_MAX_PORTS,
354 GFP_NOIO);
355 if (!pmp_link)
356 return -ENOMEM;
357
358 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
359 ata_link_init(ap, &pmp_link[i], i);
360
361 ap->pmp_link = pmp_link;
362
363 for (i = 0; i < SATA_PMP_MAX_PORTS; i++) {
364 err = ata_tlink_add(&pmp_link[i]);
365 if (err) {
366 goto err_tlink;
367 }
368 }
369 }
370
371 for (i = 0; i < nr_ports; i++) {
372 struct ata_link *link = &pmp_link[i];
373 struct ata_eh_context *ehc = &link->eh_context;
374
375 link->flags = 0;
376 ehc->i.probe_mask |= ATA_ALL_DEVICES;
377 ehc->i.action |= ATA_EH_RESET;
378 }
379
380 return 0;
381 err_tlink:
382 while (--i >= 0)
383 ata_tlink_delete(&pmp_link[i]);
384 kfree(pmp_link);
385 ap->pmp_link = NULL;
386 return err;
387}
388
389static void sata_pmp_quirks(struct ata_port *ap)
390{
391 u32 *gscr = ap->link.device->gscr;
392 u16 vendor = sata_pmp_gscr_vendor(gscr);
393 u16 devid = sata_pmp_gscr_devid(gscr);
394 struct ata_link *link;
395
396 if (vendor == 0x1095 && (devid == 0x3726 || devid == 0x3826)) {
397 /* sil3x26 quirks */
398 ata_for_each_link(link, ap, EDGE) {
399 /* link reports offline after LPM */
400 link->flags |= ATA_LFLAG_NO_LPM;
401
402 /*
403 * Class code report is unreliable and SRST times
404 * out under certain configurations.
405 */
406 if (link->pmp < 5)
407 link->flags |= ATA_LFLAG_NO_SRST |
408 ATA_LFLAG_ASSUME_ATA;
409
410 /* port 5 is for SEMB device and it doesn't like SRST */
411 if (link->pmp == 5)
412 link->flags |= ATA_LFLAG_NO_SRST |
413 ATA_LFLAG_ASSUME_SEMB;
414 }
415 } else if (vendor == 0x1095 && devid == 0x4723) {
416 /*
417 * sil4723 quirks
418 *
419 * Link reports offline after LPM. Class code report is
420 * unreliable. SIMG PMPs never got SRST reliable and the
421 * config device at port 2 locks up on SRST.
422 */
423 ata_for_each_link(link, ap, EDGE)
424 link->flags |= ATA_LFLAG_NO_LPM |
425 ATA_LFLAG_NO_SRST |
426 ATA_LFLAG_ASSUME_ATA;
427 } else if (vendor == 0x1095 && devid == 0x4726) {
428 /* sil4726 quirks */
429 ata_for_each_link(link, ap, EDGE) {
430 /* link reports offline after LPM */
431 link->flags |= ATA_LFLAG_NO_LPM;
432
433 /* Class code report is unreliable and SRST
434 * times out under certain configurations.
435 * Config device can be at port 0 or 5 and
436 * locks up on SRST.
437 */
438 if (link->pmp <= 5)
439 link->flags |= ATA_LFLAG_NO_SRST |
440 ATA_LFLAG_ASSUME_ATA;
441
442 /* Port 6 is for SEMB device which doesn't
443 * like SRST either.
444 */
445 if (link->pmp == 6)
446 link->flags |= ATA_LFLAG_NO_SRST |
447 ATA_LFLAG_ASSUME_SEMB;
448 }
449 } else if (vendor == 0x1095 && (devid == 0x5723 || devid == 0x5733 ||
450 devid == 0x5734 || devid == 0x5744)) {
451 /* sil5723/5744 quirks */
452
453 /* sil5723/5744 has either two or three downstream
454 * ports depending on operation mode. The last port
455 * is empty if any actual IO device is available or
456 * occupied by a pseudo configuration device
457 * otherwise. Don't try hard to recover it.
458 */
459 ap->pmp_link[ap->nr_pmp_links - 1].flags |= ATA_LFLAG_NO_RETRY;
460 } else if (vendor == 0x197b &&
461 (devid == 0x0562 || devid == 0x2352 || devid == 0x0325)) {
462 /*
463 * 0x0562: JMicron JMS562, as used in QNAP QDA-A2AR RAID1
464 * adapters. The exported device may stay not ready
465 * while the array is rebuilding, and SRST/classify can
466 * time out before the RAID volume is exported.
467 * 0x2352: found in Thermaltake BlackX Duet, jmicron JMB350?
468 * 0x0325: jmicron JMB394.
469 */
470 ata_for_each_link(link, ap, EDGE) {
471 /* SRST breaks detection and disks get misclassified
472 * LPM disabled to avoid potential problems
473 */
474 link->flags |= ATA_LFLAG_NO_LPM |
475 ATA_LFLAG_NO_SRST |
476 ATA_LFLAG_ASSUME_ATA;
477 }
478 } else if (vendor == 0x11ab && devid == 0x4140) {
479 /* Marvell 4140 quirks */
480 ata_for_each_link(link, ap, EDGE) {
481 /* port 4 is for SEMB device and it doesn't like SRST */
482 if (link->pmp == 4)
483 link->flags |= ATA_LFLAG_DISABLED;
484 }
485 }
486}
487
488/**
489 * sata_pmp_attach - attach a SATA PMP device
490 * @dev: SATA PMP device to attach
491 *
492 * Configure and attach SATA PMP device @dev. This function is
493 * also responsible for allocating and initializing PMP links.
494 *
495 * LOCKING:
496 * Kernel thread context (may sleep).
497 *
498 * RETURNS:
499 * 0 on success, -errno on failure.
500 */
501int sata_pmp_attach(struct ata_device *dev)
502{
503 struct ata_link *link = dev->link;
504 struct ata_port *ap = link->ap;
505 unsigned long flags;
506 struct ata_link *tlink;
507 int rc;
508
509 /* is it hanging off the right place? */
510 if (!sata_pmp_supported(ap)) {
511 ata_dev_err(dev, "host does not support Port Multiplier\n");
512 return -EINVAL;
513 }
514
515 if (!ata_is_host_link(link)) {
516 ata_dev_err(dev, "Port Multipliers cannot be nested\n");
517 return -EINVAL;
518 }
519
520 if (dev->devno) {
521 ata_dev_err(dev, "Port Multiplier must be the first device\n");
522 return -EINVAL;
523 }
524
525 WARN_ON(link->pmp != 0);
526 link->pmp = SATA_PMP_CTRL_PORT;
527
528 /* read GSCR block */
529 rc = sata_pmp_read_gscr(dev, dev->gscr);
530 if (rc)
531 goto fail;
532
533 /* config PMP */
534 rc = sata_pmp_configure(dev, 1);
535 if (rc)
536 goto fail;
537
538 rc = sata_pmp_init_links(ap, sata_pmp_gscr_ports(dev->gscr));
539 if (rc) {
540 ata_dev_info(dev, "failed to initialize PMP links\n");
541 goto fail;
542 }
543
544 /* attach it */
545 spin_lock_irqsave(ap->lock, flags);
546 WARN_ON(ap->nr_pmp_links);
547 ap->nr_pmp_links = sata_pmp_gscr_ports(dev->gscr);
548 spin_unlock_irqrestore(ap->lock, flags);
549
550 sata_pmp_quirks(ap);
551
552 if (ap->ops->pmp_attach)
553 ap->ops->pmp_attach(ap);
554
555 ata_for_each_link(tlink, ap, EDGE)
556 sata_link_init_spd(tlink);
557
558 return 0;
559
560 fail:
561 link->pmp = 0;
562 return rc;
563}
564
565/**
566 * sata_pmp_detach - detach a SATA PMP device
567 * @dev: SATA PMP device to detach
568 *
569 * Detach SATA PMP device @dev. This function is also
570 * responsible for deconfiguring PMP links.
571 *
572 * LOCKING:
573 * Kernel thread context (may sleep).
574 */
575static void sata_pmp_detach(struct ata_device *dev)
576{
577 struct ata_link *link = dev->link;
578 struct ata_port *ap = link->ap;
579 struct ata_link *tlink;
580 unsigned long flags;
581
582 ata_dev_info(dev, "Port Multiplier detaching\n");
583
584 WARN_ON(!ata_is_host_link(link) || dev->devno ||
585 link->pmp != SATA_PMP_CTRL_PORT);
586
587 if (ap->ops->pmp_detach)
588 ap->ops->pmp_detach(ap);
589
590 ata_for_each_link(tlink, ap, EDGE) {
591 WARN_ON(tlink->deferred_qc);
592 cancel_work_sync(&tlink->deferred_qc_work);
593 ata_eh_detach_dev(tlink->device);
594 }
595
596 spin_lock_irqsave(ap->lock, flags);
597 ap->nr_pmp_links = 0;
598 link->pmp = 0;
599 spin_unlock_irqrestore(ap->lock, flags);
600}
601
602/**
603 * sata_pmp_same_pmp - does new GSCR matches the configured PMP?
604 * @dev: PMP device to compare against
605 * @new_gscr: GSCR block of the new device
606 *
607 * Compare @new_gscr against @dev and determine whether @dev is
608 * the PMP described by @new_gscr.
609 *
610 * LOCKING:
611 * None.
612 *
613 * RETURNS:
614 * 1 if @dev matches @new_gscr, 0 otherwise.
615 */
616static int sata_pmp_same_pmp(struct ata_device *dev, const u32 *new_gscr)
617{
618 const u32 *old_gscr = dev->gscr;
619 u16 old_vendor, new_vendor, old_devid, new_devid;
620 int old_nr_ports, new_nr_ports;
621
622 old_vendor = sata_pmp_gscr_vendor(old_gscr);
623 new_vendor = sata_pmp_gscr_vendor(new_gscr);
624 old_devid = sata_pmp_gscr_devid(old_gscr);
625 new_devid = sata_pmp_gscr_devid(new_gscr);
626 old_nr_ports = sata_pmp_gscr_ports(old_gscr);
627 new_nr_ports = sata_pmp_gscr_ports(new_gscr);
628
629 if (old_vendor != new_vendor) {
630 ata_dev_info(dev,
631 "Port Multiplier vendor mismatch '0x%x' != '0x%x'\n",
632 old_vendor, new_vendor);
633 return 0;
634 }
635
636 if (old_devid != new_devid) {
637 ata_dev_info(dev,
638 "Port Multiplier device ID mismatch '0x%x' != '0x%x'\n",
639 old_devid, new_devid);
640 return 0;
641 }
642
643 if (old_nr_ports != new_nr_ports) {
644 ata_dev_info(dev,
645 "Port Multiplier nr_ports mismatch '0x%x' != '0x%x'\n",
646 old_nr_ports, new_nr_ports);
647 return 0;
648 }
649
650 return 1;
651}
652
653/**
654 * sata_pmp_revalidate - revalidate SATA PMP
655 * @dev: PMP device to revalidate
656 * @new_class: new class code
657 *
658 * Re-read GSCR block and make sure @dev is still attached to the
659 * port and properly configured.
660 *
661 * LOCKING:
662 * Kernel thread context (may sleep).
663 *
664 * RETURNS:
665 * 0 on success, -errno otherwise.
666 */
667static int sata_pmp_revalidate(struct ata_device *dev, unsigned int new_class)
668{
669 struct ata_link *link = dev->link;
670 u32 *gscr = (void *)dev->sector_buf;
671 int rc;
672
673 ata_eh_about_to_do(link, NULL, ATA_EH_REVALIDATE);
674
675 if (!ata_dev_enabled(dev)) {
676 rc = -ENODEV;
677 goto fail;
678 }
679
680 /* wrong class? */
681 if (ata_class_enabled(new_class) && new_class != ATA_DEV_PMP) {
682 rc = -ENODEV;
683 goto fail;
684 }
685
686 /* read GSCR */
687 rc = sata_pmp_read_gscr(dev, gscr);
688 if (rc)
689 goto fail;
690
691 /* is the pmp still there? */
692 if (!sata_pmp_same_pmp(dev, gscr)) {
693 rc = -ENODEV;
694 goto fail;
695 }
696
697 memcpy(dev->gscr, gscr, sizeof(gscr[0]) * SATA_PMP_GSCR_DWORDS);
698
699 rc = sata_pmp_configure(dev, 0);
700 if (rc)
701 goto fail;
702
703 ata_eh_done(link, NULL, ATA_EH_REVALIDATE);
704
705 return 0;
706
707 fail:
708 ata_dev_err(dev, "PMP revalidation failed (errno=%d)\n", rc);
709 return rc;
710}
711
712/**
713 * sata_pmp_revalidate_quick - revalidate SATA PMP quickly
714 * @dev: PMP device to revalidate
715 *
716 * Make sure the attached PMP is accessible.
717 *
718 * LOCKING:
719 * Kernel thread context (may sleep).
720 *
721 * RETURNS:
722 * 0 on success, -errno otherwise.
723 */
724static int sata_pmp_revalidate_quick(struct ata_device *dev)
725{
726 unsigned int err_mask;
727 u32 prod_id;
728
729 err_mask = sata_pmp_read(dev->link, SATA_PMP_GSCR_PROD_ID, &prod_id);
730 if (err_mask) {
731 ata_dev_err(dev,
732 "failed to read PMP product ID (Emask=0x%x)\n",
733 err_mask);
734 return -EIO;
735 }
736
737 if (prod_id != dev->gscr[SATA_PMP_GSCR_PROD_ID]) {
738 ata_dev_err(dev, "PMP product ID mismatch\n");
739 /* something weird is going on, request full PMP recovery */
740 return -EIO;
741 }
742
743 return 0;
744}
745
746/**
747 * sata_pmp_eh_recover_pmp - recover PMP
748 * @ap: ATA port PMP is attached to
749 * @reset_ops: The set of reset operations to use
750 *
751 * Recover PMP attached to @ap. Recovery procedure is somewhat
752 * similar to that of ata_eh_recover() except that reset should
753 * always be performed in hard->soft sequence and recovery
754 * failure results in PMP detachment.
755 *
756 * LOCKING:
757 * Kernel thread context (may sleep).
758 *
759 * RETURNS:
760 * 0 on success, -errno on failure.
761 */
762static int sata_pmp_eh_recover_pmp(struct ata_port *ap,
763 struct ata_reset_operations *reset_ops)
764 __must_hold(&ap->host->eh_mutex)
765{
766 struct ata_link *link = &ap->link;
767 struct ata_eh_context *ehc = &link->eh_context;
768 struct ata_device *dev = link->device;
769 int tries = ATA_EH_PMP_TRIES;
770 int detach = 0, rc = 0;
771 int reval_failed = 0;
772
773 if (dev->flags & ATA_DFLAG_DETACH) {
774 detach = 1;
775 rc = -ENODEV;
776 goto fail;
777 }
778
779 retry:
780 ehc->classes[0] = ATA_DEV_UNKNOWN;
781
782 if (ehc->i.action & ATA_EH_RESET) {
783 struct ata_link *tlink;
784
785 /* reset */
786 rc = ata_eh_reset(ap, link, 0, reset_ops);
787 if (rc) {
788 ata_link_err(link, "failed to reset PMP, giving up\n");
789 goto fail;
790 }
791
792 /* PMP is reset, SErrors cannot be trusted, scan all */
793 ata_for_each_link(tlink, ap, EDGE) {
794 struct ata_eh_context *ehc = &tlink->eh_context;
795
796 ehc->i.probe_mask |= ATA_ALL_DEVICES;
797 ehc->i.action |= ATA_EH_RESET;
798 }
799 }
800
801 /* If revalidation is requested, revalidate and reconfigure;
802 * otherwise, do quick revalidation.
803 */
804 if (ehc->i.action & ATA_EH_REVALIDATE)
805 rc = sata_pmp_revalidate(dev, ehc->classes[0]);
806 else
807 rc = sata_pmp_revalidate_quick(dev);
808
809 if (rc) {
810 tries--;
811
812 if (rc == -ENODEV) {
813 ehc->i.probe_mask |= ATA_ALL_DEVICES;
814 detach = 1;
815 /* give it just two more chances */
816 tries = min(tries, 2);
817 }
818
819 if (tries) {
820 /* consecutive revalidation failures? speed down */
821 if (reval_failed)
822 sata_down_spd_limit(link, 0);
823 else
824 reval_failed = 1;
825
826 ehc->i.action |= ATA_EH_RESET;
827 goto retry;
828 } else {
829 ata_dev_err(dev,
830 "failed to recover PMP after %d tries, giving up\n",
831 ATA_EH_PMP_TRIES);
832 goto fail;
833 }
834 }
835
836 /* okay, PMP resurrected */
837 ehc->i.flags = 0;
838
839 return 0;
840
841 fail:
842 sata_pmp_detach(dev);
843 if (detach)
844 ata_eh_detach_dev(dev);
845 else
846 ata_dev_disable(dev);
847
848 return rc;
849}
850
851static int sata_pmp_eh_handle_disabled_links(struct ata_port *ap)
852{
853 struct ata_link *link;
854 unsigned long flags;
855 int rc;
856
857 spin_lock_irqsave(ap->lock, flags);
858
859 ata_for_each_link(link, ap, EDGE) {
860 if (!(link->flags & ATA_LFLAG_DISABLED))
861 continue;
862
863 spin_unlock_irqrestore(ap->lock, flags);
864
865 /* Some PMPs require hardreset sequence to get
866 * SError.N working.
867 */
868 sata_link_hardreset(link, sata_deb_timing_normal,
869 ata_deadline(jiffies, ATA_TMOUT_INTERNAL_QUICK),
870 NULL, NULL);
871
872 /* unconditionally clear SError.N */
873 rc = sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
874 if (rc) {
875 ata_link_err(link,
876 "failed to clear SError.N (errno=%d)\n",
877 rc);
878 return rc;
879 }
880
881 spin_lock_irqsave(ap->lock, flags);
882 }
883
884 spin_unlock_irqrestore(ap->lock, flags);
885
886 return 0;
887}
888
889static int sata_pmp_handle_link_fail(struct ata_link *link, int *link_tries)
890{
891 struct ata_port *ap = link->ap;
892 unsigned long flags;
893
894 if (link_tries[link->pmp] && --link_tries[link->pmp])
895 return 1;
896
897 /* disable this link */
898 if (!(link->flags & ATA_LFLAG_DISABLED)) {
899 ata_link_warn(link,
900 "failed to recover link after %d tries, disabling\n",
901 ATA_EH_PMP_LINK_TRIES);
902
903 spin_lock_irqsave(ap->lock, flags);
904 link->flags |= ATA_LFLAG_DISABLED;
905 spin_unlock_irqrestore(ap->lock, flags);
906 }
907
908 ata_dev_disable(link->device);
909 link->eh_context.i.action = 0;
910
911 return 0;
912}
913
914/**
915 * sata_pmp_eh_recover - recover PMP-enabled port
916 * @ap: ATA port to recover
917 *
918 * Drive EH recovery operation for PMP enabled port @ap. This
919 * function recovers host and PMP ports with proper retrials and
920 * fallbacks. Actual recovery operations are performed using
921 * ata_eh_recover() and sata_pmp_eh_recover_pmp().
922 *
923 * LOCKING:
924 * Kernel thread context (may sleep).
925 *
926 * RETURNS:
927 * 0 on success, -errno on failure.
928 */
929static int sata_pmp_eh_recover(struct ata_port *ap)
930 __must_hold(&ap->host->eh_mutex)
931{
932 struct ata_port_operations *ops = ap->ops;
933 int pmp_tries, link_tries[SATA_PMP_MAX_PORTS];
934 struct ata_link *pmp_link = &ap->link;
935 struct ata_device *pmp_dev = pmp_link->device;
936 struct ata_eh_context *pmp_ehc = &pmp_link->eh_context;
937 u32 *gscr = pmp_dev->gscr;
938 struct ata_link *link;
939 struct ata_device *dev;
940 unsigned int err_mask;
941 u32 gscr_error, sntf;
942 int cnt, rc;
943
944 pmp_tries = ATA_EH_PMP_TRIES;
945 ata_for_each_link(link, ap, EDGE)
946 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
947
948 retry:
949 /* PMP attached? */
950 if (!sata_pmp_attached(ap)) {
951 rc = ata_eh_recover(ap, &ops->reset, NULL);
952 if (rc) {
953 ata_for_each_dev(dev, &ap->link, ALL)
954 ata_dev_disable(dev);
955 return rc;
956 }
957
958 if (pmp_dev->class != ATA_DEV_PMP)
959 return 0;
960
961 /* new PMP online */
962 ata_for_each_link(link, ap, EDGE)
963 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
964
965 /* fall through */
966 }
967
968 /* recover pmp */
969 rc = sata_pmp_eh_recover_pmp(ap, &ops->reset);
970 if (rc)
971 goto pmp_fail;
972
973 /* PHY event notification can disturb reset and other recovery
974 * operations. Turn it off.
975 */
976 if (gscr[SATA_PMP_GSCR_FEAT_EN] & SATA_PMP_FEAT_NOTIFY) {
977 gscr[SATA_PMP_GSCR_FEAT_EN] &= ~SATA_PMP_FEAT_NOTIFY;
978
979 err_mask = sata_pmp_write(pmp_link, SATA_PMP_GSCR_FEAT_EN,
980 gscr[SATA_PMP_GSCR_FEAT_EN]);
981 if (err_mask) {
982 ata_link_warn(pmp_link,
983 "failed to disable NOTIFY (err_mask=0x%x)\n",
984 err_mask);
985 goto pmp_fail;
986 }
987 }
988
989 /* handle disabled links */
990 rc = sata_pmp_eh_handle_disabled_links(ap);
991 if (rc)
992 goto pmp_fail;
993
994 /* recover links */
995 rc = ata_eh_recover(ap, &ops->pmp_reset, &link);
996 if (rc)
997 goto link_fail;
998
999 /* clear SNotification */
1000 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
1001 if (rc == 0)
1002 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
1003
1004 /*
1005 * If LPM is active on any fan-out port, hotplug wouldn't
1006 * work. Return w/ PHY event notification disabled.
1007 */
1008 ata_for_each_link(link, ap, EDGE)
1009 if (link->lpm_policy > ATA_LPM_MAX_POWER)
1010 return 0;
1011
1012 /*
1013 * Connection status might have changed while resetting other
1014 * links, enable notification and check SATA_PMP_GSCR_ERROR
1015 * before returning.
1016 */
1017
1018 /* enable notification */
1019 if (pmp_dev->flags & ATA_DFLAG_AN) {
1020 gscr[SATA_PMP_GSCR_FEAT_EN] |= SATA_PMP_FEAT_NOTIFY;
1021
1022 err_mask = sata_pmp_write(pmp_link, SATA_PMP_GSCR_FEAT_EN,
1023 gscr[SATA_PMP_GSCR_FEAT_EN]);
1024 if (err_mask) {
1025 ata_dev_err(pmp_dev,
1026 "failed to write PMP_FEAT_EN (Emask=0x%x)\n",
1027 err_mask);
1028 rc = -EIO;
1029 goto pmp_fail;
1030 }
1031 }
1032
1033 /* check GSCR_ERROR */
1034 err_mask = sata_pmp_read(pmp_link, SATA_PMP_GSCR_ERROR, &gscr_error);
1035 if (err_mask) {
1036 ata_dev_err(pmp_dev,
1037 "failed to read PMP_GSCR_ERROR (Emask=0x%x)\n",
1038 err_mask);
1039 rc = -EIO;
1040 goto pmp_fail;
1041 }
1042
1043 cnt = 0;
1044 ata_for_each_link(link, ap, EDGE) {
1045 if (!(gscr_error & (1 << link->pmp)))
1046 continue;
1047
1048 if (sata_pmp_handle_link_fail(link, link_tries)) {
1049 ata_ehi_hotplugged(&link->eh_context.i);
1050 cnt++;
1051 } else {
1052 ata_link_warn(link,
1053 "PHY status changed but maxed out on retries, giving up\n");
1054 ata_link_warn(link,
1055 "Manually issue scan to resume this link\n");
1056 }
1057 }
1058
1059 if (cnt) {
1060 ata_port_info(ap,
1061 "PMP SError.N set for some ports, repeating recovery\n");
1062 goto retry;
1063 }
1064
1065 return 0;
1066
1067 link_fail:
1068 if (sata_pmp_handle_link_fail(link, link_tries)) {
1069 pmp_ehc->i.action |= ATA_EH_RESET;
1070 goto retry;
1071 }
1072
1073 /* fall through */
1074 pmp_fail:
1075 /* Control always ends up here after detaching PMP. Shut up
1076 * and return if we're unloading.
1077 */
1078 if (ap->pflags & ATA_PFLAG_UNLOADING)
1079 return rc;
1080
1081 if (!sata_pmp_attached(ap))
1082 goto retry;
1083
1084 if (--pmp_tries) {
1085 pmp_ehc->i.action |= ATA_EH_RESET;
1086 goto retry;
1087 }
1088
1089 ata_port_err(ap, "failed to recover PMP after %d tries, giving up\n",
1090 ATA_EH_PMP_TRIES);
1091 sata_pmp_detach(pmp_dev);
1092 ata_dev_disable(pmp_dev);
1093
1094 return rc;
1095}
1096
1097/**
1098 * sata_pmp_error_handler - do standard error handling for PMP-enabled host
1099 * @ap: host port to handle error for
1100 *
1101 * Perform standard error handling sequence for PMP-enabled host
1102 * @ap.
1103 *
1104 * LOCKING:
1105 * Kernel thread context (may sleep).
1106 */
1107void sata_pmp_error_handler(struct ata_port *ap)
1108 __must_hold(&ap->host->eh_mutex)
1109{
1110 ata_eh_autopsy(ap);
1111 ata_eh_report(ap);
1112 sata_pmp_eh_recover(ap);
1113 ata_eh_finish(ap);
1114}
1115
1116EXPORT_SYMBOL_GPL(sata_pmp_port_ops);
1117EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch);
1118EXPORT_SYMBOL_GPL(sata_pmp_error_handler);