0001
0002
0003
0004
0005
0006
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
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
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
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
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
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
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
0128
0129
0130
0131
0132
0133
0134
0135
0136
0137
0138
0139
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
0159
0160
0161
0162
0163
0164
0165
0166
0167
0168
0169
0170
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
0190
0191
0192
0193
0194
0195
0196
0197
0198
0199
0200
0201
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
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
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
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
0290
0291
0292
0293
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, ®);
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
0387 ata_for_each_link(link, ap, EDGE) {
0388
0389 link->flags |= ATA_LFLAG_NO_LPM;
0390
0391
0392
0393
0394
0395 if (link->pmp < 5)
0396 link->flags |= ATA_LFLAG_NO_SRST |
0397 ATA_LFLAG_ASSUME_ATA;
0398
0399
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
0407
0408
0409
0410
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
0418 ata_for_each_link(link, ap, EDGE) {
0419
0420 link->flags |= ATA_LFLAG_NO_LPM;
0421
0422
0423
0424
0425
0426
0427 if (link->pmp <= 5)
0428 link->flags |= ATA_LFLAG_NO_SRST |
0429 ATA_LFLAG_ASSUME_ATA;
0430
0431
0432
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
0441
0442
0443
0444
0445
0446
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
0452
0453
0454 ata_for_each_link(link, ap, EDGE) {
0455
0456
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
0464 ata_for_each_link(link, ap, EDGE) {
0465
0466 if (link->pmp == 4)
0467 link->flags |= ATA_LFLAG_DISABLED;
0468 }
0469 }
0470 }
0471
0472
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
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
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
0513 rc = sata_pmp_read_gscr(dev, dev->gscr);
0514 if (rc)
0515 goto fail;
0516
0517
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
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
0551
0552
0553
0554
0555
0556
0557
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
0585
0586
0587
0588
0589
0590
0591
0592
0593
0594
0595
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
0636
0637
0638
0639
0640
0641
0642
0643
0644
0645
0646
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
0663 if (ata_class_enabled(new_class) && new_class != ATA_DEV_PMP) {
0664 rc = -ENODEV;
0665 goto fail;
0666 }
0667
0668
0669 rc = sata_pmp_read_gscr(dev, gscr);
0670 if (rc)
0671 goto fail;
0672
0673
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
0696
0697
0698
0699
0700
0701
0702
0703
0704
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
0722 return -EIO;
0723 }
0724
0725 return 0;
0726 }
0727
0728
0729
0730
0731
0732
0733
0734
0735
0736
0737
0738
0739
0740
0741
0742
0743
0744
0745
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
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
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
0788
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
0802 tries = min(tries, 2);
0803 }
0804
0805 if (tries) {
0806
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
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
0852
0853
0854 sata_link_hardreset(link, sata_deb_timing_normal,
0855 ata_deadline(jiffies, ATA_TMOUT_INTERNAL_QUICK),
0856 NULL, NULL);
0857
0858
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
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
0902
0903
0904
0905
0906
0907
0908
0909
0910
0911
0912
0913
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
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
0948 ata_for_each_link(link, ap, EDGE)
0949 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES;
0950
0951
0952 }
0953
0954
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
0961
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
0977 rc = sata_pmp_eh_handle_disabled_links(ap);
0978 if (rc)
0979 goto pmp_fail;
0980
0981
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
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
0994
0995
0996 ata_for_each_link(link, ap, EDGE)
0997 if (link->lpm_policy > ATA_LPM_MAX_POWER)
0998 return 0;
0999
1000
1001
1002
1003
1004
1005
1006
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
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
1062 pmp_fail:
1063
1064
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
1087
1088
1089
1090
1091
1092
1093
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);