Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * libata-pmp.c - libata port multiplier support
0004  *
0005  * Copyright (c) 2007  SUSE Linux Products GmbH
0006  * Copyright (c) 2007  Tejun Heo <teheo@suse.de>
0007  */
0008 
0009 #include <linux/kernel.h>
0010 #include <linux/export.h>
0011 #include <linux/libata.h>
0012 #include <linux/slab.h>
0013 #include "libata.h"
0014 #include "libata-transport.h"
0015 
0016 const struct ata_port_operations sata_pmp_port_ops = {
0017     .inherits       = &sata_port_ops,
0018     .pmp_prereset       = ata_std_prereset,
0019     .pmp_hardreset      = sata_std_hardreset,
0020     .pmp_postreset      = ata_std_postreset,
0021     .error_handler      = sata_pmp_error_handler,
0022 };
0023 
0024 /**
0025  *  sata_pmp_read - read PMP register
0026  *  @link: link to read PMP register for
0027  *  @reg: register to read
0028  *  @r_val: resulting value
0029  *
0030  *  Read PMP register.
0031  *
0032  *  LOCKING:
0033  *  Kernel thread context (may sleep).
0034  *
0035  *  RETURNS:
0036  *  0 on success, AC_ERR_* mask on failure.
0037  */
0038 static unsigned int sata_pmp_read(struct ata_link *link, int reg, u32 *r_val)
0039 {
0040     struct ata_port *ap = link->ap;
0041     struct ata_device *pmp_dev = ap->link.device;
0042     struct ata_taskfile tf;
0043     unsigned int err_mask;
0044 
0045     ata_tf_init(pmp_dev, &tf);
0046     tf.command = ATA_CMD_PMP_READ;
0047     tf.protocol = ATA_PROT_NODATA;
0048     tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
0049     tf.feature = reg;
0050     tf.device = link->pmp;
0051 
0052     err_mask = ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
0053                      SATA_PMP_RW_TIMEOUT);
0054     if (err_mask)
0055         return err_mask;
0056 
0057     *r_val = tf.nsect | tf.lbal << 8 | tf.lbam << 16 | tf.lbah << 24;
0058     return 0;
0059 }
0060 
0061 /**
0062  *  sata_pmp_write - write PMP register
0063  *  @link: link to write PMP register for
0064  *  @reg: register to write
0065  *  @val: value to write
0066  *
0067  *  Write PMP register.
0068  *
0069  *  LOCKING:
0070  *  Kernel thread context (may sleep).
0071  *
0072  *  RETURNS:
0073  *  0 on success, AC_ERR_* mask on failure.
0074  */
0075 static unsigned int sata_pmp_write(struct ata_link *link, int reg, u32 val)
0076 {
0077     struct ata_port *ap = link->ap;
0078     struct ata_device *pmp_dev = ap->link.device;
0079     struct ata_taskfile tf;
0080 
0081     ata_tf_init(pmp_dev, &tf);
0082     tf.command = ATA_CMD_PMP_WRITE;
0083     tf.protocol = ATA_PROT_NODATA;
0084     tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
0085     tf.feature = reg;
0086     tf.device = link->pmp;
0087     tf.nsect = val & 0xff;
0088     tf.lbal = (val >> 8) & 0xff;
0089     tf.lbam = (val >> 16) & 0xff;
0090     tf.lbah = (val >> 24) & 0xff;
0091 
0092     return ata_exec_internal(pmp_dev, &tf, NULL, DMA_NONE, NULL, 0,
0093                  SATA_PMP_RW_TIMEOUT);
0094 }
0095 
0096 /**
0097  *  sata_pmp_qc_defer_cmd_switch - qc_defer for command switching PMP
0098  *  @qc: ATA command in question
0099  *
0100  *  A host which has command switching PMP support cannot issue
0101  *  commands to multiple links simultaneously.
0102  *
0103  *  LOCKING:
0104  *  spin_lock_irqsave(host lock)
0105  *
0106  *  RETURNS:
0107  *  ATA_DEFER_* if deferring is needed, 0 otherwise.
0108  */
0109 int sata_pmp_qc_defer_cmd_switch(struct ata_queued_cmd *qc)
0110 {
0111     struct ata_link *link = qc->dev->link;
0112     struct ata_port *ap = link->ap;
0113 
0114     if (ap->excl_link == NULL || ap->excl_link == link) {
0115         if (ap->nr_active_links == 0 || ata_link_active(link)) {
0116             qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
0117             return ata_std_qc_defer(qc);
0118         }
0119 
0120         ap->excl_link = link;
0121     }
0122 
0123     return ATA_DEFER_PORT;
0124 }
0125 
0126 /**
0127  *  sata_pmp_scr_read - read PSCR
0128  *  @link: ATA link to read PSCR for
0129  *  @reg: PSCR to read
0130  *  @r_val: resulting value
0131  *
0132  *  Read PSCR @reg into @r_val for @link, to be called from
0133  *  ata_scr_read().
0134  *
0135  *  LOCKING:
0136  *  Kernel thread context (may sleep).
0137  *
0138  *  RETURNS:
0139  *  0 on success, -errno on failure.
0140  */
0141 int sata_pmp_scr_read(struct ata_link *link, int reg, u32 *r_val)
0142 {
0143     unsigned int err_mask;
0144 
0145     if (reg > SATA_PMP_PSCR_CONTROL)
0146         return -EINVAL;
0147 
0148     err_mask = sata_pmp_read(link, reg, r_val);
0149     if (err_mask) {
0150         ata_link_warn(link, "failed to read SCR %d (Emask=0x%x)\n",
0151                   reg, err_mask);
0152         return -EIO;
0153     }
0154     return 0;
0155 }
0156 
0157 /**
0158  *  sata_pmp_scr_write - write PSCR
0159  *  @link: ATA link to write PSCR for
0160  *  @reg: PSCR to write
0161  *  @val: value to be written
0162  *
0163  *  Write @val to PSCR @reg for @link, to be called from
0164  *  ata_scr_write() and ata_scr_write_flush().
0165  *
0166  *  LOCKING:
0167  *  Kernel thread context (may sleep).
0168  *
0169  *  RETURNS:
0170  *  0 on success, -errno on failure.
0171  */
0172 int sata_pmp_scr_write(struct ata_link *link, int reg, u32 val)
0173 {
0174     unsigned int err_mask;
0175 
0176     if (reg > SATA_PMP_PSCR_CONTROL)
0177         return -EINVAL;
0178 
0179     err_mask = sata_pmp_write(link, reg, val);
0180     if (err_mask) {
0181         ata_link_warn(link, "failed to write SCR %d (Emask=0x%x)\n",
0182                   reg, err_mask);
0183         return -EIO;
0184     }
0185     return 0;
0186 }
0187 
0188 /**
0189  *  sata_pmp_set_lpm - configure LPM for a PMP link
0190  *  @link: PMP link to configure LPM for
0191  *  @policy: target LPM policy
0192  *  @hints: LPM hints
0193  *
0194  *  Configure LPM for @link.  This function will contain any PMP
0195  *  specific workarounds if necessary.
0196  *
0197  *  LOCKING:
0198  *  EH context.
0199  *
0200  *  RETURNS:
0201  *  0 on success, -errno on failure.
0202  */
0203 int sata_pmp_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
0204              unsigned hints)
0205 {
0206     return sata_link_scr_lpm(link, policy, true);
0207 }
0208 
0209 /**
0210  *  sata_pmp_read_gscr - read GSCR block of SATA PMP
0211  *  @dev: PMP device
0212  *  @gscr: buffer to read GSCR block into
0213  *
0214  *  Read selected PMP GSCRs from the PMP at @dev.  This will serve
0215  *  as configuration and identification info for the PMP.
0216  *
0217  *  LOCKING:
0218  *  Kernel thread context (may sleep).
0219  *
0220  *  RETURNS:
0221  *  0 on success, -errno on failure.
0222  */
0223 static int sata_pmp_read_gscr(struct ata_device *dev, u32 *gscr)
0224 {
0225     static const int gscr_to_read[] = { 0, 1, 2, 32, 33, 64, 96 };
0226     int i;
0227 
0228     for (i = 0; i < ARRAY_SIZE(gscr_to_read); i++) {
0229         int reg = gscr_to_read[i];
0230         unsigned int err_mask;
0231 
0232         err_mask = sata_pmp_read(dev->link, reg, &gscr[reg]);
0233         if (err_mask) {
0234             ata_dev_err(dev, "failed to read PMP GSCR[%d] (Emask=0x%x)\n",
0235                     reg, err_mask);
0236             return -EIO;
0237         }
0238     }
0239 
0240     return 0;
0241 }
0242 
0243 static const char *sata_pmp_spec_rev_str(const u32 *gscr)
0244 {
0245     u32 rev = gscr[SATA_PMP_GSCR_REV];
0246 
0247     if (rev & (1 << 3))
0248         return "1.2";
0249     if (rev & (1 << 2))
0250         return "1.1";
0251     if (rev & (1 << 1))
0252         return "1.0";
0253     return "<unknown>";
0254 }
0255 
0256 #define PMP_GSCR_SII_POL 129
0257 
0258 static int sata_pmp_configure(struct ata_device *dev, int print_info)
0259 {
0260     struct ata_port *ap = dev->link->ap;
0261     u32 *gscr = dev->gscr;
0262     u16 vendor = sata_pmp_gscr_vendor(gscr);
0263     u16 devid = sata_pmp_gscr_devid(gscr);
0264     unsigned int err_mask = 0;
0265     const char *reason;
0266     int nr_ports, rc;
0267 
0268     nr_ports = sata_pmp_gscr_ports(gscr);
0269 
0270     if (nr_ports <= 0 || nr_ports > SATA_PMP_MAX_PORTS) {
0271         rc = -EINVAL;
0272         reason = "invalid nr_ports";
0273         goto fail;
0274     }
0275 
0276     if ((ap->flags & ATA_FLAG_AN) &&
0277         (gscr[SATA_PMP_GSCR_FEAT] & SATA_PMP_FEAT_NOTIFY))
0278         dev->flags |= ATA_DFLAG_AN;
0279 
0280     /* monitor SERR_PHYRDY_CHG on fan-out ports */
0281     err_mask = sata_pmp_write(dev->link, SATA_PMP_GSCR_ERROR_EN,
0282                   SERR_PHYRDY_CHG);
0283     if (err_mask) {
0284         rc = -EIO;
0285         reason = "failed to write GSCR_ERROR_EN";
0286         goto fail;
0287     }
0288 
0289     /* Disable sending Early R_OK.
0290      * With "cached read" HDD testing and multiple ports busy on a SATA
0291      * host controller, 3x26 PMP will very rarely drop a deferred
0292      * R_OK that was intended for the host. Symptom will be all
0293      * 5 drives under test will timeout, get reset, and recover.
0294      */
0295     if (vendor == 0x1095 && (devid == 0x3726 || devid == 0x3826)) {
0296         u32 reg;
0297 
0298         err_mask = sata_pmp_read(&ap->link, PMP_GSCR_SII_POL, &reg);
0299         if (err_mask) {
0300             rc = -EIO;
0301             reason = "failed to read Sil3x26 Private Register";
0302             goto fail;
0303         }
0304         reg &= ~0x1;
0305         err_mask = sata_pmp_write(&ap->link, PMP_GSCR_SII_POL, reg);
0306         if (err_mask) {
0307             rc = -EIO;
0308             reason = "failed to write Sil3x26 Private Register";
0309             goto fail;
0310         }
0311     }
0312 
0313     if (print_info) {
0314         ata_dev_info(dev, "Port Multiplier %s, "
0315                  "0x%04x:0x%04x r%d, %d ports, feat 0x%x/0x%x\n",
0316                  sata_pmp_spec_rev_str(gscr), vendor, devid,
0317                  sata_pmp_gscr_rev(gscr),
0318                  nr_ports, gscr[SATA_PMP_GSCR_FEAT_EN],
0319                  gscr[SATA_PMP_GSCR_FEAT]);
0320 
0321         if (!(dev->flags & ATA_DFLAG_AN))
0322             ata_dev_info(dev,
0323                 "Asynchronous notification not supported, "
0324                 "hotplug won't work on fan-out ports. Use warm-plug instead.\n");
0325     }
0326 
0327     return 0;
0328 
0329  fail:
0330     ata_dev_err(dev,
0331             "failed to configure Port Multiplier (%s, Emask=0x%x)\n",
0332             reason, err_mask);
0333     return rc;
0334 }
0335 
0336 static int sata_pmp_init_links (struct ata_port *ap, int nr_ports)
0337 {
0338     struct ata_link *pmp_link = ap->pmp_link;
0339     int i, err;
0340 
0341     if (!pmp_link) {
0342         pmp_link = kcalloc(SATA_PMP_MAX_PORTS, sizeof(pmp_link[0]),
0343                    GFP_NOIO);
0344         if (!pmp_link)
0345             return -ENOMEM;
0346 
0347         for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
0348             ata_link_init(ap, &pmp_link[i], i);
0349 
0350         ap->pmp_link = pmp_link;
0351 
0352         for (i = 0; i < SATA_PMP_MAX_PORTS; i++) {
0353             err = ata_tlink_add(&pmp_link[i]);
0354             if (err) {
0355                 goto err_tlink;
0356             }
0357         }
0358     }
0359 
0360     for (i = 0; i < nr_ports; i++) {
0361         struct ata_link *link = &pmp_link[i];
0362         struct ata_eh_context *ehc = &link->eh_context;
0363 
0364         link->flags = 0;
0365         ehc->i.probe_mask |= ATA_ALL_DEVICES;
0366         ehc->i.action |= ATA_EH_RESET;
0367     }
0368 
0369     return 0;
0370   err_tlink:
0371     while (--i >= 0)
0372         ata_tlink_delete(&pmp_link[i]);
0373     kfree(pmp_link);
0374     ap->pmp_link = NULL;
0375     return err;
0376 }
0377 
0378 static void sata_pmp_quirks(struct ata_port *ap)
0379 {
0380     u32 *gscr = ap->link.device->gscr;
0381     u16 vendor = sata_pmp_gscr_vendor(gscr);
0382     u16 devid = sata_pmp_gscr_devid(gscr);
0383     struct ata_link *link;
0384 
0385     if (vendor == 0x1095 && (devid == 0x3726 || devid == 0x3826)) {
0386         /* sil3x26 quirks */
0387         ata_for_each_link(link, ap, EDGE) {
0388             /* link reports offline after LPM */
0389             link->flags |= ATA_LFLAG_NO_LPM;
0390 
0391             /*
0392              * Class code report is unreliable and SRST times
0393              * out under certain configurations.
0394              */
0395             if (link->pmp < 5)
0396                 link->flags |= ATA_LFLAG_NO_SRST |
0397                            ATA_LFLAG_ASSUME_ATA;
0398 
0399             /* port 5 is for SEMB device and it doesn't like SRST */
0400             if (link->pmp == 5)
0401                 link->flags |= ATA_LFLAG_NO_SRST |
0402                            ATA_LFLAG_ASSUME_SEMB;
0403         }
0404     } else if (vendor == 0x1095 && devid == 0x4723) {
0405         /*
0406          * sil4723 quirks
0407          *
0408          * Link reports offline after LPM.  Class code report is
0409          * unreliable.  SIMG PMPs never got SRST reliable and the
0410          * config device at port 2 locks up on SRST.
0411          */
0412         ata_for_each_link(link, ap, EDGE)
0413             link->flags |= ATA_LFLAG_NO_LPM |
0414                        ATA_LFLAG_NO_SRST |
0415                        ATA_LFLAG_ASSUME_ATA;
0416     } else if (vendor == 0x1095 && devid == 0x4726) {
0417         /* sil4726 quirks */
0418         ata_for_each_link(link, ap, EDGE) {
0419             /* link reports offline after LPM */
0420             link->flags |= ATA_LFLAG_NO_LPM;
0421 
0422             /* Class code report is unreliable and SRST
0423              * times out under certain configurations.
0424              * Config device can be at port 0 or 5 and
0425              * locks up on SRST.
0426              */
0427             if (link->pmp <= 5)
0428                 link->flags |= ATA_LFLAG_NO_SRST |
0429                            ATA_LFLAG_ASSUME_ATA;
0430 
0431             /* Port 6 is for SEMB device which doesn't
0432              * like SRST either.
0433              */
0434             if (link->pmp == 6)
0435                 link->flags |= ATA_LFLAG_NO_SRST |
0436                            ATA_LFLAG_ASSUME_SEMB;
0437         }
0438     } else if (vendor == 0x1095 && (devid == 0x5723 || devid == 0x5733 ||
0439                     devid == 0x5734 || devid == 0x5744)) {
0440         /* sil5723/5744 quirks */
0441 
0442         /* sil5723/5744 has either two or three downstream
0443          * ports depending on operation mode.  The last port
0444          * is empty if any actual IO device is available or
0445          * occupied by a pseudo configuration device
0446          * otherwise.  Don't try hard to recover it.
0447          */
0448         ap->pmp_link[ap->nr_pmp_links - 1].flags |= ATA_LFLAG_NO_RETRY;
0449     } else if (vendor == 0x197b && (devid == 0x2352 || devid == 0x0325)) {
0450         /*
0451          * 0x2352: found in Thermaltake BlackX Duet, jmicron JMB350?
0452          * 0x0325: jmicron JMB394.
0453          */
0454         ata_for_each_link(link, ap, EDGE) {
0455             /* SRST breaks detection and disks get misclassified
0456              * LPM disabled to avoid potential problems
0457              */
0458             link->flags |= ATA_LFLAG_NO_LPM |
0459                        ATA_LFLAG_NO_SRST |
0460                        ATA_LFLAG_ASSUME_ATA;
0461         }
0462     } else if (vendor == 0x11ab && devid == 0x4140) {
0463         /* Marvell 4140 quirks */
0464         ata_for_each_link(link, ap, EDGE) {
0465             /* port 4 is for SEMB device and it doesn't like SRST */
0466             if (link->pmp == 4)
0467                 link->flags |= ATA_LFLAG_DISABLED;
0468         }
0469     }
0470 }
0471 
0472 /**
0473  *  sata_pmp_attach - attach a SATA PMP device
0474  *  @dev: SATA PMP device to attach
0475  *
0476  *  Configure and attach SATA PMP device @dev.  This function is
0477  *  also responsible for allocating and initializing PMP links.
0478  *
0479  *  LOCKING:
0480  *  Kernel thread context (may sleep).
0481  *
0482  *  RETURNS:
0483  *  0 on success, -errno on failure.
0484  */
0485 int sata_pmp_attach(struct ata_device *dev)
0486 {
0487     struct ata_link *link = dev->link;
0488     struct ata_port *ap = link->ap;
0489     unsigned long flags;
0490     struct ata_link *tlink;
0491     int rc;
0492 
0493     /* is it hanging off the right place? */
0494     if (!sata_pmp_supported(ap)) {
0495         ata_dev_err(dev, "host does not support Port Multiplier\n");
0496         return -EINVAL;
0497     }
0498 
0499     if (!ata_is_host_link(link)) {
0500         ata_dev_err(dev, "Port Multipliers cannot be nested\n");
0501         return -EINVAL;
0502     }
0503 
0504     if (dev->devno) {
0505         ata_dev_err(dev, "Port Multiplier must be the first device\n");
0506         return -EINVAL;
0507     }
0508 
0509     WARN_ON(link->pmp != 0);
0510     link->pmp = SATA_PMP_CTRL_PORT;
0511 
0512     /* read GSCR block */
0513     rc = sata_pmp_read_gscr(dev, dev->gscr);
0514     if (rc)
0515         goto fail;
0516 
0517     /* config PMP */
0518     rc = sata_pmp_configure(dev, 1);
0519     if (rc)
0520         goto fail;
0521 
0522     rc = sata_pmp_init_links(ap, sata_pmp_gscr_ports(dev->gscr));
0523     if (rc) {
0524         ata_dev_info(dev, "failed to initialize PMP links\n");
0525         goto fail;
0526     }
0527 
0528     /* attach it */
0529     spin_lock_irqsave(ap->lock, flags);
0530     WARN_ON(ap->nr_pmp_links);
0531     ap->nr_pmp_links = sata_pmp_gscr_ports(dev->gscr);
0532     spin_unlock_irqrestore(ap->lock, flags);
0533 
0534     sata_pmp_quirks(ap);
0535 
0536     if (ap->ops->pmp_attach)
0537         ap->ops->pmp_attach(ap);
0538 
0539     ata_for_each_link(tlink, ap, EDGE)
0540         sata_link_init_spd(tlink);
0541 
0542     return 0;
0543 
0544  fail:
0545     link->pmp = 0;
0546     return rc;
0547 }
0548 
0549 /**
0550  *  sata_pmp_detach - detach a SATA PMP device
0551  *  @dev: SATA PMP device to detach
0552  *
0553  *  Detach SATA PMP device @dev.  This function is also
0554  *  responsible for deconfiguring PMP links.
0555  *
0556  *  LOCKING:
0557  *  Kernel thread context (may sleep).
0558  */
0559 static void sata_pmp_detach(struct ata_device *dev)
0560 {
0561     struct ata_link *link = dev->link;
0562     struct ata_port *ap = link->ap;
0563     struct ata_link *tlink;
0564     unsigned long flags;
0565 
0566     ata_dev_info(dev, "Port Multiplier detaching\n");
0567 
0568     WARN_ON(!ata_is_host_link(link) || dev->devno ||
0569         link->pmp != SATA_PMP_CTRL_PORT);
0570 
0571     if (ap->ops->pmp_detach)
0572         ap->ops->pmp_detach(ap);
0573 
0574     ata_for_each_link(tlink, ap, EDGE)
0575         ata_eh_detach_dev(tlink->device);
0576 
0577     spin_lock_irqsave(ap->lock, flags);
0578     ap->nr_pmp_links = 0;
0579     link->pmp = 0;
0580     spin_unlock_irqrestore(ap->lock, flags);
0581 }
0582 
0583 /**
0584  *  sata_pmp_same_pmp - does new GSCR matches the configured PMP?
0585  *  @dev: PMP device to compare against
0586  *  @new_gscr: GSCR block of the new device
0587  *
0588  *  Compare @new_gscr against @dev and determine whether @dev is
0589  *  the PMP described by @new_gscr.
0590  *
0591  *  LOCKING:
0592  *  None.
0593  *
0594  *  RETURNS:
0595  *  1 if @dev matches @new_gscr, 0 otherwise.
0596  */
0597 static int sata_pmp_same_pmp(struct ata_device *dev, const u32 *new_gscr)
0598 {
0599     const u32 *old_gscr = dev->gscr;
0600     u16 old_vendor, new_vendor, old_devid, new_devid;
0601     int old_nr_ports, new_nr_ports;
0602 
0603     old_vendor = sata_pmp_gscr_vendor(old_gscr);
0604     new_vendor = sata_pmp_gscr_vendor(new_gscr);
0605     old_devid = sata_pmp_gscr_devid(old_gscr);
0606     new_devid = sata_pmp_gscr_devid(new_gscr);
0607     old_nr_ports = sata_pmp_gscr_ports(old_gscr);
0608     new_nr_ports = sata_pmp_gscr_ports(new_gscr);
0609 
0610     if (old_vendor != new_vendor) {
0611         ata_dev_info(dev,
0612                  "Port Multiplier vendor mismatch '0x%x' != '0x%x'\n",
0613                  old_vendor, new_vendor);
0614         return 0;
0615     }
0616 
0617     if (old_devid != new_devid) {
0618         ata_dev_info(dev,
0619                  "Port Multiplier device ID mismatch '0x%x' != '0x%x'\n",
0620                  old_devid, new_devid);
0621         return 0;
0622     }
0623 
0624     if (old_nr_ports != new_nr_ports) {
0625         ata_dev_info(dev,
0626                  "Port Multiplier nr_ports mismatch '0x%x' != '0x%x'\n",
0627                  old_nr_ports, new_nr_ports);
0628         return 0;
0629     }
0630 
0631     return 1;
0632 }
0633 
0634 /**
0635  *  sata_pmp_revalidate - revalidate SATA PMP
0636  *  @dev: PMP device to revalidate
0637  *  @new_class: new class code
0638  *
0639  *  Re-read GSCR block and make sure @dev is still attached to the
0640  *  port and properly configured.
0641  *
0642  *  LOCKING:
0643  *  Kernel thread context (may sleep).
0644  *
0645  *  RETURNS:
0646  *  0 on success, -errno otherwise.
0647  */
0648 static int sata_pmp_revalidate(struct ata_device *dev, unsigned int new_class)
0649 {
0650     struct ata_link *link = dev->link;
0651     struct ata_port *ap = link->ap;
0652     u32 *gscr = (void *)ap->sector_buf;
0653     int rc;
0654 
0655     ata_eh_about_to_do(link, NULL, ATA_EH_REVALIDATE);
0656 
0657     if (!ata_dev_enabled(dev)) {
0658         rc = -ENODEV;
0659         goto fail;
0660     }
0661 
0662     /* wrong class? */
0663     if (ata_class_enabled(new_class) && new_class != ATA_DEV_PMP) {
0664         rc = -ENODEV;
0665         goto fail;
0666     }
0667 
0668     /* read GSCR */
0669     rc = sata_pmp_read_gscr(dev, gscr);
0670     if (rc)
0671         goto fail;
0672 
0673     /* is the pmp still there? */
0674     if (!sata_pmp_same_pmp(dev, gscr)) {
0675         rc = -ENODEV;
0676         goto fail;
0677     }
0678 
0679     memcpy(dev->gscr, gscr, sizeof(gscr[0]) * SATA_PMP_GSCR_DWORDS);
0680 
0681     rc = sata_pmp_configure(dev, 0);
0682     if (rc)
0683         goto fail;
0684 
0685     ata_eh_done(link, NULL, ATA_EH_REVALIDATE);
0686 
0687     return 0;
0688 
0689  fail:
0690     ata_dev_err(dev, "PMP revalidation failed (errno=%d)\n", rc);
0691     return rc;
0692 }
0693 
0694 /**
0695  *  sata_pmp_revalidate_quick - revalidate SATA PMP quickly
0696  *  @dev: PMP device to revalidate
0697  *
0698  *  Make sure the attached PMP is accessible.
0699  *
0700  *  LOCKING:
0701  *  Kernel thread context (may sleep).
0702  *
0703  *  RETURNS:
0704  *  0 on success, -errno otherwise.
0705  */
0706 static int sata_pmp_revalidate_quick(struct ata_device *dev)
0707 {
0708     unsigned int err_mask;
0709     u32 prod_id;
0710 
0711     err_mask = sata_pmp_read(dev->link, SATA_PMP_GSCR_PROD_ID, &prod_id);
0712     if (err_mask) {
0713         ata_dev_err(dev,
0714                 "failed to read PMP product ID (Emask=0x%x)\n",
0715                 err_mask);
0716         return -EIO;
0717     }
0718 
0719     if (prod_id != dev->gscr[SATA_PMP_GSCR_PROD_ID]) {
0720         ata_dev_err(dev, "PMP product ID mismatch\n");
0721         /* something weird is going on, request full PMP recovery */
0722         return -EIO;
0723     }
0724 
0725     return 0;
0726 }
0727 
0728 /**
0729  *  sata_pmp_eh_recover_pmp - recover PMP
0730  *  @ap: ATA port PMP is attached to
0731  *  @prereset: prereset method (can be NULL)
0732  *  @softreset: softreset method
0733  *  @hardreset: hardreset method
0734  *  @postreset: postreset method (can be NULL)
0735  *
0736  *  Recover PMP attached to @ap.  Recovery procedure is somewhat
0737  *  similar to that of ata_eh_recover() except that reset should
0738  *  always be performed in hard->soft sequence and recovery
0739  *  failure results in PMP detachment.
0740  *
0741  *  LOCKING:
0742  *  Kernel thread context (may sleep).
0743  *
0744  *  RETURNS:
0745  *  0 on success, -errno on failure.
0746  */
0747 static int sata_pmp_eh_recover_pmp(struct ata_port *ap,
0748         ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
0749         ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
0750 {
0751     struct ata_link *link = &ap->link;
0752     struct ata_eh_context *ehc = &link->eh_context;
0753     struct ata_device *dev = link->device;
0754     int tries = ATA_EH_PMP_TRIES;
0755     int detach = 0, rc = 0;
0756     int reval_failed = 0;
0757 
0758     if (dev->flags & ATA_DFLAG_DETACH) {
0759         detach = 1;
0760         rc = -ENODEV;
0761         goto fail;
0762     }
0763 
0764  retry:
0765     ehc->classes[0] = ATA_DEV_UNKNOWN;
0766 
0767     if (ehc->i.action & ATA_EH_RESET) {
0768         struct ata_link *tlink;
0769 
0770         /* reset */
0771         rc = ata_eh_reset(link, 0, prereset, softreset, hardreset,
0772                   postreset);
0773         if (rc) {
0774             ata_link_err(link, "failed to reset PMP, giving up\n");
0775             goto fail;
0776         }
0777 
0778         /* PMP is reset, SErrors cannot be trusted, scan all */
0779         ata_for_each_link(tlink, ap, EDGE) {
0780             struct ata_eh_context *ehc = &tlink->eh_context;
0781 
0782             ehc->i.probe_mask |= ATA_ALL_DEVICES;
0783             ehc->i.action |= ATA_EH_RESET;
0784         }
0785     }
0786 
0787     /* If revalidation is requested, revalidate and reconfigure;
0788      * otherwise, do quick revalidation.
0789      */
0790     if (ehc->i.action & ATA_EH_REVALIDATE)
0791         rc = sata_pmp_revalidate(dev, ehc->classes[0]);
0792     else
0793         rc = sata_pmp_revalidate_quick(dev);
0794 
0795     if (rc) {
0796         tries--;
0797 
0798         if (rc == -ENODEV) {
0799             ehc->i.probe_mask |= ATA_ALL_DEVICES;
0800             detach = 1;
0801             /* give it just two more chances */
0802             tries = min(tries, 2);
0803         }
0804 
0805         if (tries) {
0806             /* consecutive revalidation failures? speed down */
0807             if (reval_failed)
0808                 sata_down_spd_limit(link, 0);
0809             else
0810                 reval_failed = 1;
0811 
0812             ehc->i.action |= ATA_EH_RESET;
0813             goto retry;
0814         } else {
0815             ata_dev_err(dev,
0816                     "failed to recover PMP after %d tries, giving up\n",
0817                     ATA_EH_PMP_TRIES);
0818             goto fail;
0819         }
0820     }
0821 
0822     /* okay, PMP resurrected */
0823     ehc->i.flags = 0;
0824 
0825     return 0;
0826 
0827  fail:
0828     sata_pmp_detach(dev);
0829     if (detach)
0830         ata_eh_detach_dev(dev);
0831     else
0832         ata_dev_disable(dev);
0833 
0834     return rc;
0835 }
0836 
0837 static int sata_pmp_eh_handle_disabled_links(struct ata_port *ap)
0838 {
0839     struct ata_link *link;
0840     unsigned long flags;
0841     int rc;
0842 
0843     spin_lock_irqsave(ap->lock, flags);
0844 
0845     ata_for_each_link(link, ap, EDGE) {
0846         if (!(link->flags & ATA_LFLAG_DISABLED))
0847             continue;
0848 
0849         spin_unlock_irqrestore(ap->lock, flags);
0850 
0851         /* Some PMPs require hardreset sequence to get
0852          * SError.N working.
0853          */
0854         sata_link_hardreset(link, sata_deb_timing_normal,
0855                 ata_deadline(jiffies, ATA_TMOUT_INTERNAL_QUICK),
0856                 NULL, NULL);
0857 
0858         /* unconditionally clear SError.N */
0859         rc = sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
0860         if (rc) {
0861             ata_link_err(link,
0862                      "failed to clear SError.N (errno=%d)\n",
0863                      rc);
0864             return rc;
0865         }
0866 
0867         spin_lock_irqsave(ap->lock, flags);
0868     }
0869 
0870     spin_unlock_irqrestore(ap->lock, flags);
0871 
0872     return 0;
0873 }
0874 
0875 static int sata_pmp_handle_link_fail(struct ata_link *link, int *link_tries)
0876 {
0877     struct ata_port *ap = link->ap;
0878     unsigned long flags;
0879 
0880     if (link_tries[link->pmp] && --link_tries[link->pmp])
0881         return 1;
0882 
0883     /* disable this link */
0884     if (!(link->flags & ATA_LFLAG_DISABLED)) {
0885         ata_link_warn(link,
0886             "failed to recover link after %d tries, disabling\n",
0887             ATA_EH_PMP_LINK_TRIES);
0888 
0889         spin_lock_irqsave(ap->lock, flags);
0890         link->flags |= ATA_LFLAG_DISABLED;
0891         spin_unlock_irqrestore(ap->lock, flags);
0892     }
0893 
0894     ata_dev_disable(link->device);
0895     link->eh_context.i.action = 0;
0896 
0897     return 0;
0898 }
0899 
0900 /**
0901  *  sata_pmp_eh_recover - recover PMP-enabled port
0902  *  @ap: ATA port to recover
0903  *
0904  *  Drive EH recovery operation for PMP enabled port @ap.  This
0905  *  function recovers host and PMP ports with proper retrials and
0906  *  fallbacks.  Actual recovery operations are performed using
0907  *  ata_eh_recover() and sata_pmp_eh_recover_pmp().
0908  *
0909  *  LOCKING:
0910  *  Kernel thread context (may sleep).
0911  *
0912  *  RETURNS:
0913  *  0 on success, -errno on failure.
0914  */
0915 static int sata_pmp_eh_recover(struct ata_port *ap)
0916 {
0917     struct ata_port_operations *ops = ap->ops;
0918     int pmp_tries, link_tries[SATA_PMP_MAX_PORTS];
0919     struct ata_link *pmp_link = &ap->link;
0920     struct ata_device *pmp_dev = pmp_link->device;
0921     struct ata_eh_context *pmp_ehc = &pmp_link->eh_context;
0922     u32 *gscr = pmp_dev->gscr;
0923     struct ata_link *link;
0924     struct ata_device *dev;
0925     unsigned int err_mask;
0926     u32 gscr_error, sntf;
0927     int cnt, rc;
0928 
0929     pmp_tries = ATA_EH_PMP_TRIES;
0930     ata_for_each_link(link, ap, EDGE)
0931         link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
0932 
0933  retry:
0934     /* PMP attached? */
0935     if (!sata_pmp_attached(ap)) {
0936         rc = ata_eh_recover(ap, ops->prereset, ops->softreset,
0937                     ops->hardreset, ops->postreset, NULL);
0938         if (rc) {
0939             ata_for_each_dev(dev, &ap->link, ALL)
0940                 ata_dev_disable(dev);
0941             return rc;
0942         }
0943 
0944         if (pmp_dev->class != ATA_DEV_PMP)
0945             return 0;
0946 
0947         /* new PMP online */
0948         ata_for_each_link(link, ap, EDGE)
0949             link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
0950 
0951         /* fall through */
0952     }
0953 
0954     /* recover pmp */
0955     rc = sata_pmp_eh_recover_pmp(ap, ops->prereset, ops->softreset,
0956                      ops->hardreset, ops->postreset);
0957     if (rc)
0958         goto pmp_fail;
0959 
0960     /* PHY event notification can disturb reset and other recovery
0961      * operations.  Turn it off.
0962      */
0963     if (gscr[SATA_PMP_GSCR_FEAT_EN] & SATA_PMP_FEAT_NOTIFY) {
0964         gscr[SATA_PMP_GSCR_FEAT_EN] &= ~SATA_PMP_FEAT_NOTIFY;
0965 
0966         err_mask = sata_pmp_write(pmp_link, SATA_PMP_GSCR_FEAT_EN,
0967                       gscr[SATA_PMP_GSCR_FEAT_EN]);
0968         if (err_mask) {
0969             ata_link_warn(pmp_link,
0970                 "failed to disable NOTIFY (err_mask=0x%x)\n",
0971                 err_mask);
0972             goto pmp_fail;
0973         }
0974     }
0975 
0976     /* handle disabled links */
0977     rc = sata_pmp_eh_handle_disabled_links(ap);
0978     if (rc)
0979         goto pmp_fail;
0980 
0981     /* recover links */
0982     rc = ata_eh_recover(ap, ops->pmp_prereset, ops->pmp_softreset,
0983                 ops->pmp_hardreset, ops->pmp_postreset, &link);
0984     if (rc)
0985         goto link_fail;
0986 
0987     /* clear SNotification */
0988     rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
0989     if (rc == 0)
0990         sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
0991 
0992     /*
0993      * If LPM is active on any fan-out port, hotplug wouldn't
0994      * work.  Return w/ PHY event notification disabled.
0995      */
0996     ata_for_each_link(link, ap, EDGE)
0997         if (link->lpm_policy > ATA_LPM_MAX_POWER)
0998             return 0;
0999 
1000     /*
1001      * Connection status might have changed while resetting other
1002      * links, enable notification and check SATA_PMP_GSCR_ERROR
1003      * before returning.
1004      */
1005 
1006     /* enable notification */
1007     if (pmp_dev->flags & ATA_DFLAG_AN) {
1008         gscr[SATA_PMP_GSCR_FEAT_EN] |= SATA_PMP_FEAT_NOTIFY;
1009 
1010         err_mask = sata_pmp_write(pmp_link, SATA_PMP_GSCR_FEAT_EN,
1011                       gscr[SATA_PMP_GSCR_FEAT_EN]);
1012         if (err_mask) {
1013             ata_dev_err(pmp_dev,
1014                     "failed to write PMP_FEAT_EN (Emask=0x%x)\n",
1015                     err_mask);
1016             rc = -EIO;
1017             goto pmp_fail;
1018         }
1019     }
1020 
1021     /* check GSCR_ERROR */
1022     err_mask = sata_pmp_read(pmp_link, SATA_PMP_GSCR_ERROR, &gscr_error);
1023     if (err_mask) {
1024         ata_dev_err(pmp_dev,
1025                 "failed to read PMP_GSCR_ERROR (Emask=0x%x)\n",
1026                 err_mask);
1027         rc = -EIO;
1028         goto pmp_fail;
1029     }
1030 
1031     cnt = 0;
1032     ata_for_each_link(link, ap, EDGE) {
1033         if (!(gscr_error & (1 << link->pmp)))
1034             continue;
1035 
1036         if (sata_pmp_handle_link_fail(link, link_tries)) {
1037             ata_ehi_hotplugged(&link->eh_context.i);
1038             cnt++;
1039         } else {
1040             ata_link_warn(link,
1041                 "PHY status changed but maxed out on retries, giving up\n");
1042             ata_link_warn(link,
1043                 "Manually issue scan to resume this link\n");
1044         }
1045     }
1046 
1047     if (cnt) {
1048         ata_port_info(ap,
1049             "PMP SError.N set for some ports, repeating recovery\n");
1050         goto retry;
1051     }
1052 
1053     return 0;
1054 
1055  link_fail:
1056     if (sata_pmp_handle_link_fail(link, link_tries)) {
1057         pmp_ehc->i.action |= ATA_EH_RESET;
1058         goto retry;
1059     }
1060 
1061     /* fall through */
1062  pmp_fail:
1063     /* Control always ends up here after detaching PMP.  Shut up
1064      * and return if we're unloading.
1065      */
1066     if (ap->pflags & ATA_PFLAG_UNLOADING)
1067         return rc;
1068 
1069     if (!sata_pmp_attached(ap))
1070         goto retry;
1071 
1072     if (--pmp_tries) {
1073         pmp_ehc->i.action |= ATA_EH_RESET;
1074         goto retry;
1075     }
1076 
1077     ata_port_err(ap, "failed to recover PMP after %d tries, giving up\n",
1078              ATA_EH_PMP_TRIES);
1079     sata_pmp_detach(pmp_dev);
1080     ata_dev_disable(pmp_dev);
1081 
1082     return rc;
1083 }
1084 
1085 /**
1086  *  sata_pmp_error_handler - do standard error handling for PMP-enabled host
1087  *  @ap: host port to handle error for
1088  *
1089  *  Perform standard error handling sequence for PMP-enabled host
1090  *  @ap.
1091  *
1092  *  LOCKING:
1093  *  Kernel thread context (may sleep).
1094  */
1095 void sata_pmp_error_handler(struct ata_port *ap)
1096 {
1097     ata_eh_autopsy(ap);
1098     ata_eh_report(ap);
1099     sata_pmp_eh_recover(ap);
1100     ata_eh_finish(ap);
1101 }
1102 
1103 EXPORT_SYMBOL_GPL(sata_pmp_port_ops);
1104 EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch);
1105 EXPORT_SYMBOL_GPL(sata_pmp_error_handler);