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