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