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 <teheo@suse.de> 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 16 const 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 */ 38 static 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 */ 75 static 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 */ 109 int 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 */ 152 int 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 */ 183 int 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 */ 214 int 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 */ 234 static 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 254 static 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 269 static 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, ®); 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 347 static 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 389 static 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 } else if (vendor == 0x1b4b && devid == 0x9705) { 486 /* 487 * Marvell 88SM9705 quirks 488 * 489 * 1-to-5 PMP with an extra SEMB port. It reports 6 ports and 490 * port 5 is the SEMB, which never answers SRST: EH spends about 491 * a minute per round retrying it, and disk discovery waits for 492 * that. Seen behind a Marvell 88SE9235 in the Synology DX1215 493 * and DX1222. 494 */ 495 ata_for_each_link(link, ap, EDGE) { 496 if (link->pmp == 5) 497 link->flags |= ATA_LFLAG_DISABLED; 498 } 499 } 500 } 501 502 /** 503 * sata_pmp_attach - attach a SATA PMP device 504 * @dev: SATA PMP device to attach 505 * 506 * Configure and attach SATA PMP device @dev. This function is 507 * also responsible for allocating and initializing PMP links. 508 * 509 * LOCKING: 510 * Kernel thread context (may sleep). 511 * 512 * RETURNS: 513 * 0 on success, -errno on failure. 514 */ 515 int sata_pmp_attach(struct ata_device *dev) 516 { 517 struct ata_link *link = dev->link; 518 struct ata_port *ap = link->ap; 519 unsigned long flags; 520 struct ata_link *tlink; 521 int rc; 522 523 /* is it hanging off the right place? */ 524 if (!sata_pmp_supported(ap)) { 525 ata_dev_err(dev, "host does not support Port Multiplier\n"); 526 return -EINVAL; 527 } 528 529 if (!ata_is_host_link(link)) { 530 ata_dev_err(dev, "Port Multipliers cannot be nested\n"); 531 return -EINVAL; 532 } 533 534 if (dev->devno) { 535 ata_dev_err(dev, "Port Multiplier must be the first device\n"); 536 return -EINVAL; 537 } 538 539 WARN_ON(link->pmp != 0); 540 link->pmp = SATA_PMP_CTRL_PORT; 541 542 /* read GSCR block */ 543 rc = sata_pmp_read_gscr(dev, dev->gscr); 544 if (rc) 545 goto fail; 546 547 /* config PMP */ 548 rc = sata_pmp_configure(dev, 1); 549 if (rc) 550 goto fail; 551 552 rc = sata_pmp_init_links(ap, sata_pmp_gscr_ports(dev->gscr)); 553 if (rc) { 554 ata_dev_info(dev, "failed to initialize PMP links\n"); 555 goto fail; 556 } 557 558 /* attach it */ 559 spin_lock_irqsave(ap->lock, flags); 560 WARN_ON(ap->nr_pmp_links); 561 ap->nr_pmp_links = sata_pmp_gscr_ports(dev->gscr); 562 spin_unlock_irqrestore(ap->lock, flags); 563 564 sata_pmp_quirks(ap); 565 566 if (ap->ops->pmp_attach) 567 ap->ops->pmp_attach(ap); 568 569 ata_for_each_link(tlink, ap, EDGE) 570 sata_link_init_spd(tlink); 571 572 return 0; 573 574 fail: 575 link->pmp = 0; 576 return rc; 577 } 578 579 /** 580 * sata_pmp_detach - detach a SATA PMP device 581 * @dev: SATA PMP device to detach 582 * 583 * Detach SATA PMP device @dev. This function is also 584 * responsible for deconfiguring PMP links. 585 * 586 * LOCKING: 587 * Kernel thread context (may sleep). 588 */ 589 static void sata_pmp_detach(struct ata_device *dev) 590 { 591 struct ata_link *link = dev->link; 592 struct ata_port *ap = link->ap; 593 struct ata_link *tlink; 594 unsigned long flags; 595 596 ata_dev_info(dev, "Port Multiplier detaching\n"); 597 598 WARN_ON(!ata_is_host_link(link) || dev->devno || 599 link->pmp != SATA_PMP_CTRL_PORT); 600 601 if (ap->ops->pmp_detach) 602 ap->ops->pmp_detach(ap); 603 604 ata_for_each_link(tlink, ap, EDGE) { 605 WARN_ON(tlink->deferred_qc); 606 cancel_work_sync(&tlink->deferred_qc_work); 607 ata_eh_detach_dev(tlink->device); 608 } 609 610 spin_lock_irqsave(ap->lock, flags); 611 ap->nr_pmp_links = 0; 612 link->pmp = 0; 613 spin_unlock_irqrestore(ap->lock, flags); 614 } 615 616 /** 617 * sata_pmp_same_pmp - does new GSCR matches the configured PMP? 618 * @dev: PMP device to compare against 619 * @new_gscr: GSCR block of the new device 620 * 621 * Compare @new_gscr against @dev and determine whether @dev is 622 * the PMP described by @new_gscr. 623 * 624 * LOCKING: 625 * None. 626 * 627 * RETURNS: 628 * 1 if @dev matches @new_gscr, 0 otherwise. 629 */ 630 static int sata_pmp_same_pmp(struct ata_device *dev, const u32 *new_gscr) 631 { 632 const u32 *old_gscr = dev->gscr; 633 u16 old_vendor, new_vendor, old_devid, new_devid; 634 int old_nr_ports, new_nr_ports; 635 636 old_vendor = sata_pmp_gscr_vendor(old_gscr); 637 new_vendor = sata_pmp_gscr_vendor(new_gscr); 638 old_devid = sata_pmp_gscr_devid(old_gscr); 639 new_devid = sata_pmp_gscr_devid(new_gscr); 640 old_nr_ports = sata_pmp_gscr_ports(old_gscr); 641 new_nr_ports = sata_pmp_gscr_ports(new_gscr); 642 643 if (old_vendor != new_vendor) { 644 ata_dev_info(dev, 645 "Port Multiplier vendor mismatch '0x%x' != '0x%x'\n", 646 old_vendor, new_vendor); 647 return 0; 648 } 649 650 if (old_devid != new_devid) { 651 ata_dev_info(dev, 652 "Port Multiplier device ID mismatch '0x%x' != '0x%x'\n", 653 old_devid, new_devid); 654 return 0; 655 } 656 657 if (old_nr_ports != new_nr_ports) { 658 ata_dev_info(dev, 659 "Port Multiplier nr_ports mismatch '0x%x' != '0x%x'\n", 660 old_nr_ports, new_nr_ports); 661 return 0; 662 } 663 664 return 1; 665 } 666 667 /** 668 * sata_pmp_revalidate - revalidate SATA PMP 669 * @dev: PMP device to revalidate 670 * @new_class: new class code 671 * 672 * Re-read GSCR block and make sure @dev is still attached to the 673 * port and properly configured. 674 * 675 * LOCKING: 676 * Kernel thread context (may sleep). 677 * 678 * RETURNS: 679 * 0 on success, -errno otherwise. 680 */ 681 static int sata_pmp_revalidate(struct ata_device *dev, unsigned int new_class) 682 { 683 struct ata_link *link = dev->link; 684 u32 *gscr = (void *)dev->sector_buf; 685 int rc; 686 687 ata_eh_about_to_do(link, NULL, ATA_EH_REVALIDATE); 688 689 if (!ata_dev_enabled(dev)) { 690 rc = -ENODEV; 691 goto fail; 692 } 693 694 /* wrong class? */ 695 if (ata_class_enabled(new_class) && new_class != ATA_DEV_PMP) { 696 rc = -ENODEV; 697 goto fail; 698 } 699 700 /* read GSCR */ 701 rc = sata_pmp_read_gscr(dev, gscr); 702 if (rc) 703 goto fail; 704 705 /* is the pmp still there? */ 706 if (!sata_pmp_same_pmp(dev, gscr)) { 707 rc = -ENODEV; 708 goto fail; 709 } 710 711 memcpy(dev->gscr, gscr, sizeof(gscr[0]) * SATA_PMP_GSCR_DWORDS); 712 713 rc = sata_pmp_configure(dev, 0); 714 if (rc) 715 goto fail; 716 717 ata_eh_done(link, NULL, ATA_EH_REVALIDATE); 718 719 return 0; 720 721 fail: 722 ata_dev_err(dev, "PMP revalidation failed (errno=%d)\n", rc); 723 return rc; 724 } 725 726 /** 727 * sata_pmp_revalidate_quick - revalidate SATA PMP quickly 728 * @dev: PMP device to revalidate 729 * 730 * Make sure the attached PMP is accessible. 731 * 732 * LOCKING: 733 * Kernel thread context (may sleep). 734 * 735 * RETURNS: 736 * 0 on success, -errno otherwise. 737 */ 738 static int sata_pmp_revalidate_quick(struct ata_device *dev) 739 { 740 unsigned int err_mask; 741 u32 prod_id; 742 743 err_mask = sata_pmp_read(dev->link, SATA_PMP_GSCR_PROD_ID, &prod_id); 744 if (err_mask) { 745 ata_dev_err(dev, 746 "failed to read PMP product ID (Emask=0x%x)\n", 747 err_mask); 748 return -EIO; 749 } 750 751 if (prod_id != dev->gscr[SATA_PMP_GSCR_PROD_ID]) { 752 ata_dev_err(dev, "PMP product ID mismatch\n"); 753 /* something weird is going on, request full PMP recovery */ 754 return -EIO; 755 } 756 757 return 0; 758 } 759 760 /** 761 * sata_pmp_eh_recover_pmp - recover PMP 762 * @ap: ATA port PMP is attached to 763 * @reset_ops: The set of reset operations to use 764 * 765 * Recover PMP attached to @ap. Recovery procedure is somewhat 766 * similar to that of ata_eh_recover() except that reset should 767 * always be performed in hard->soft sequence and recovery 768 * failure results in PMP detachment. 769 * 770 * LOCKING: 771 * Kernel thread context (may sleep). 772 * 773 * RETURNS: 774 * 0 on success, -errno on failure. 775 */ 776 static int sata_pmp_eh_recover_pmp(struct ata_port *ap, 777 struct ata_reset_operations *reset_ops) 778 __must_hold(&ap->host->eh_mutex) 779 { 780 struct ata_link *link = &ap->link; 781 struct ata_eh_context *ehc = &link->eh_context; 782 struct ata_device *dev = link->device; 783 int tries = ATA_EH_PMP_TRIES; 784 int detach = 0, rc = 0; 785 int reval_failed = 0; 786 787 if (dev->flags & ATA_DFLAG_DETACH) { 788 detach = 1; 789 rc = -ENODEV; 790 goto fail; 791 } 792 793 retry: 794 ehc->classes[0] = ATA_DEV_UNKNOWN; 795 796 if (ehc->i.action & ATA_EH_RESET) { 797 struct ata_link *tlink; 798 799 /* reset */ 800 rc = ata_eh_reset(ap, link, 0, reset_ops); 801 if (rc) { 802 ata_link_err(link, "failed to reset PMP, giving up\n"); 803 goto fail; 804 } 805 806 /* PMP is reset, SErrors cannot be trusted, scan all */ 807 ata_for_each_link(tlink, ap, EDGE) { 808 struct ata_eh_context *ehc = &tlink->eh_context; 809 810 ehc->i.probe_mask |= ATA_ALL_DEVICES; 811 ehc->i.action |= ATA_EH_RESET; 812 } 813 } 814 815 /* If revalidation is requested, revalidate and reconfigure; 816 * otherwise, do quick revalidation. 817 */ 818 if (ehc->i.action & ATA_EH_REVALIDATE) 819 rc = sata_pmp_revalidate(dev, ehc->classes[0]); 820 else 821 rc = sata_pmp_revalidate_quick(dev); 822 823 if (rc) { 824 tries--; 825 826 if (rc == -ENODEV) { 827 ehc->i.probe_mask |= ATA_ALL_DEVICES; 828 detach = 1; 829 /* give it just two more chances */ 830 tries = min(tries, 2); 831 } 832 833 if (tries) { 834 /* consecutive revalidation failures? speed down */ 835 if (reval_failed) 836 sata_down_spd_limit(link, 0); 837 else 838 reval_failed = 1; 839 840 ehc->i.action |= ATA_EH_RESET; 841 goto retry; 842 } else { 843 ata_dev_err(dev, 844 "failed to recover PMP after %d tries, giving up\n", 845 ATA_EH_PMP_TRIES); 846 goto fail; 847 } 848 } 849 850 /* okay, PMP resurrected */ 851 ehc->i.flags = 0; 852 853 return 0; 854 855 fail: 856 sata_pmp_detach(dev); 857 if (detach) 858 ata_eh_detach_dev(dev); 859 else 860 ata_dev_disable(dev); 861 862 return rc; 863 } 864 865 static int sata_pmp_eh_handle_disabled_links(struct ata_port *ap) 866 { 867 struct ata_link *link; 868 unsigned long flags; 869 int rc; 870 871 spin_lock_irqsave(ap->lock, flags); 872 873 ata_for_each_link(link, ap, EDGE) { 874 if (!(link->flags & ATA_LFLAG_DISABLED)) 875 continue; 876 877 spin_unlock_irqrestore(ap->lock, flags); 878 879 /* Some PMPs require hardreset sequence to get 880 * SError.N working. 881 */ 882 sata_link_hardreset(link, sata_deb_timing_normal, 883 ata_deadline(jiffies, ATA_TMOUT_INTERNAL_QUICK), 884 NULL, NULL); 885 886 /* unconditionally clear SError.N */ 887 rc = sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG); 888 if (rc) { 889 ata_link_err(link, 890 "failed to clear SError.N (errno=%d)\n", 891 rc); 892 return rc; 893 } 894 895 spin_lock_irqsave(ap->lock, flags); 896 } 897 898 spin_unlock_irqrestore(ap->lock, flags); 899 900 return 0; 901 } 902 903 static int sata_pmp_handle_link_fail(struct ata_link *link, int *link_tries) 904 { 905 struct ata_port *ap = link->ap; 906 unsigned long flags; 907 908 if (link_tries[link->pmp] && --link_tries[link->pmp]) 909 return 1; 910 911 /* disable this link */ 912 if (!(link->flags & ATA_LFLAG_DISABLED)) { 913 ata_link_warn(link, 914 "failed to recover link after %d tries, disabling\n", 915 ATA_EH_PMP_LINK_TRIES); 916 917 spin_lock_irqsave(ap->lock, flags); 918 link->flags |= ATA_LFLAG_DISABLED; 919 spin_unlock_irqrestore(ap->lock, flags); 920 } 921 922 ata_dev_disable(link->device); 923 link->eh_context.i.action = 0; 924 925 return 0; 926 } 927 928 /** 929 * sata_pmp_eh_recover - recover PMP-enabled port 930 * @ap: ATA port to recover 931 * 932 * Drive EH recovery operation for PMP enabled port @ap. This 933 * function recovers host and PMP ports with proper retrials and 934 * fallbacks. Actual recovery operations are performed using 935 * ata_eh_recover() and sata_pmp_eh_recover_pmp(). 936 * 937 * LOCKING: 938 * Kernel thread context (may sleep). 939 * 940 * RETURNS: 941 * 0 on success, -errno on failure. 942 */ 943 static int sata_pmp_eh_recover(struct ata_port *ap) 944 __must_hold(&ap->host->eh_mutex) 945 { 946 struct ata_port_operations *ops = ap->ops; 947 int pmp_tries, link_tries[SATA_PMP_MAX_PORTS]; 948 struct ata_link *pmp_link = &ap->link; 949 struct ata_device *pmp_dev = pmp_link->device; 950 struct ata_eh_context *pmp_ehc = &pmp_link->eh_context; 951 u32 *gscr = pmp_dev->gscr; 952 struct ata_link *link; 953 struct ata_device *dev; 954 unsigned int err_mask; 955 u32 gscr_error, sntf; 956 int cnt, rc; 957 958 pmp_tries = ATA_EH_PMP_TRIES; 959 ata_for_each_link(link, ap, EDGE) 960 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES; 961 962 retry: 963 /* PMP attached? */ 964 if (!sata_pmp_attached(ap)) { 965 rc = ata_eh_recover(ap, &ops->reset, NULL); 966 if (rc) { 967 ata_for_each_dev(dev, &ap->link, ALL) 968 ata_dev_disable(dev); 969 return rc; 970 } 971 972 if (pmp_dev->class != ATA_DEV_PMP) 973 return 0; 974 975 /* new PMP online */ 976 ata_for_each_link(link, ap, EDGE) 977 link_tries[link->pmp] = ATA_EH_PMP_LINK_TRIES; 978 979 /* fall through */ 980 } 981 982 /* recover pmp */ 983 rc = sata_pmp_eh_recover_pmp(ap, &ops->reset); 984 if (rc) 985 goto pmp_fail; 986 987 /* PHY event notification can disturb reset and other recovery 988 * operations. Turn it off. 989 */ 990 if (gscr[SATA_PMP_GSCR_FEAT_EN] & SATA_PMP_FEAT_NOTIFY) { 991 gscr[SATA_PMP_GSCR_FEAT_EN] &= ~SATA_PMP_FEAT_NOTIFY; 992 993 err_mask = sata_pmp_write(pmp_link, SATA_PMP_GSCR_FEAT_EN, 994 gscr[SATA_PMP_GSCR_FEAT_EN]); 995 if (err_mask) { 996 ata_link_warn(pmp_link, 997 "failed to disable NOTIFY (err_mask=0x%x)\n", 998 err_mask); 999 goto pmp_fail; 1000 } 1001 } 1002 1003 /* handle disabled links */ 1004 rc = sata_pmp_eh_handle_disabled_links(ap); 1005 if (rc) 1006 goto pmp_fail; 1007 1008 /* recover links */ 1009 rc = ata_eh_recover(ap, &ops->pmp_reset, &link); 1010 if (rc) 1011 goto link_fail; 1012 1013 /* clear SNotification */ 1014 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf); 1015 if (rc == 0) 1016 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf); 1017 1018 /* 1019 * If LPM is active on any fan-out port, hotplug wouldn't 1020 * work. Return w/ PHY event notification disabled. 1021 */ 1022 ata_for_each_link(link, ap, EDGE) 1023 if (link->lpm_policy > ATA_LPM_MAX_POWER) 1024 return 0; 1025 1026 /* 1027 * Connection status might have changed while resetting other 1028 * links, enable notification and check SATA_PMP_GSCR_ERROR 1029 * before returning. 1030 */ 1031 1032 /* enable notification */ 1033 if (pmp_dev->flags & ATA_DFLAG_AN) { 1034 gscr[SATA_PMP_GSCR_FEAT_EN] |= SATA_PMP_FEAT_NOTIFY; 1035 1036 err_mask = sata_pmp_write(pmp_link, SATA_PMP_GSCR_FEAT_EN, 1037 gscr[SATA_PMP_GSCR_FEAT_EN]); 1038 if (err_mask) { 1039 ata_dev_err(pmp_dev, 1040 "failed to write PMP_FEAT_EN (Emask=0x%x)\n", 1041 err_mask); 1042 rc = -EIO; 1043 goto pmp_fail; 1044 } 1045 } 1046 1047 /* check GSCR_ERROR */ 1048 err_mask = sata_pmp_read(pmp_link, SATA_PMP_GSCR_ERROR, &gscr_error); 1049 if (err_mask) { 1050 ata_dev_err(pmp_dev, 1051 "failed to read PMP_GSCR_ERROR (Emask=0x%x)\n", 1052 err_mask); 1053 rc = -EIO; 1054 goto pmp_fail; 1055 } 1056 1057 cnt = 0; 1058 ata_for_each_link(link, ap, EDGE) { 1059 if (!(gscr_error & (1 << link->pmp))) 1060 continue; 1061 1062 if (sata_pmp_handle_link_fail(link, link_tries)) { 1063 ata_ehi_hotplugged(&link->eh_context.i); 1064 cnt++; 1065 } else { 1066 ata_link_warn(link, 1067 "PHY status changed but maxed out on retries, giving up\n"); 1068 ata_link_warn(link, 1069 "Manually issue scan to resume this link\n"); 1070 } 1071 } 1072 1073 if (cnt) { 1074 ata_port_info(ap, 1075 "PMP SError.N set for some ports, repeating recovery\n"); 1076 goto retry; 1077 } 1078 1079 return 0; 1080 1081 link_fail: 1082 if (sata_pmp_handle_link_fail(link, link_tries)) { 1083 pmp_ehc->i.action |= ATA_EH_RESET; 1084 goto retry; 1085 } 1086 1087 /* fall through */ 1088 pmp_fail: 1089 /* Control always ends up here after detaching PMP. Shut up 1090 * and return if we're unloading. 1091 */ 1092 if (ap->pflags & ATA_PFLAG_UNLOADING) 1093 return rc; 1094 1095 if (!sata_pmp_attached(ap)) 1096 goto retry; 1097 1098 if (--pmp_tries) { 1099 pmp_ehc->i.action |= ATA_EH_RESET; 1100 goto retry; 1101 } 1102 1103 ata_port_err(ap, "failed to recover PMP after %d tries, giving up\n", 1104 ATA_EH_PMP_TRIES); 1105 sata_pmp_detach(pmp_dev); 1106 ata_dev_disable(pmp_dev); 1107 1108 return rc; 1109 } 1110 1111 /** 1112 * sata_pmp_error_handler - do standard error handling for PMP-enabled host 1113 * @ap: host port to handle error for 1114 * 1115 * Perform standard error handling sequence for PMP-enabled host 1116 * @ap. 1117 * 1118 * LOCKING: 1119 * Kernel thread context (may sleep). 1120 */ 1121 void sata_pmp_error_handler(struct ata_port *ap) 1122 __must_hold(&ap->host->eh_mutex) 1123 { 1124 ata_eh_autopsy(ap); 1125 ata_eh_report(ap); 1126 sata_pmp_eh_recover(ap); 1127 ata_eh_finish(ap); 1128 } 1129 1130 EXPORT_SYMBOL_GPL(sata_pmp_port_ops); 1131 EXPORT_SYMBOL_GPL(sata_pmp_qc_defer_cmd_switch); 1132 EXPORT_SYMBOL_GPL(sata_pmp_error_handler); 1133