1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * SATA specific part of ATA helper library 4 * 5 * Copyright 2003-2004 Red Hat, Inc. All rights reserved. 6 * Copyright 2003-2004 Jeff Garzik 7 * Copyright 2006 Tejun Heo <htejun@gmail.com> 8 */ 9 10 #include <linux/kernel.h> 11 #include <linux/module.h> 12 #include <scsi/scsi_cmnd.h> 13 #include <scsi/scsi_device.h> 14 #include <scsi/scsi_eh.h> 15 #include <linux/libata.h> 16 #include <linux/unaligned.h> 17 18 #include "libata.h" 19 #include "libata-transport.h" 20 21 /* debounce timing parameters in msecs { interval, duration, timeout } */ 22 const unsigned int sata_deb_timing_normal[] = { 5, 100, 2000 }; 23 EXPORT_SYMBOL_GPL(sata_deb_timing_normal); 24 const unsigned int sata_deb_timing_hotplug[] = { 25, 500, 2000 }; 25 EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug); 26 const unsigned int sata_deb_timing_long[] = { 100, 2000, 5000 }; 27 EXPORT_SYMBOL_GPL(sata_deb_timing_long); 28 29 /** 30 * sata_scr_valid - test whether SCRs are accessible 31 * @link: ATA link to test SCR accessibility for 32 * 33 * Test whether SCRs are accessible for @link. 34 * 35 * LOCKING: 36 * None. 37 * 38 * RETURNS: 39 * 1 if SCRs are accessible, 0 otherwise. 40 */ 41 int sata_scr_valid(struct ata_link *link) 42 { 43 struct ata_port *ap = link->ap; 44 45 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read; 46 } 47 EXPORT_SYMBOL_GPL(sata_scr_valid); 48 49 /** 50 * sata_scr_read - read SCR register of the specified port 51 * @link: ATA link to read SCR for 52 * @reg: SCR to read 53 * @val: Place to store read value 54 * 55 * Read SCR register @reg of @link into *@val. This function is 56 * guaranteed to succeed if @link is ap->link, the cable type of 57 * the port is SATA and the port implements ->scr_read. 58 * 59 * LOCKING: 60 * None if @link is ap->link. Kernel thread context otherwise. 61 * 62 * RETURNS: 63 * 0 on success, negative errno on failure. 64 */ 65 int sata_scr_read(struct ata_link *link, int reg, u32 *val) 66 { 67 if (ata_is_host_link(link)) { 68 if (sata_scr_valid(link)) 69 return link->ap->ops->scr_read(link, reg, val); 70 return -EOPNOTSUPP; 71 } 72 73 return sata_pmp_scr_read(link, reg, val); 74 } 75 EXPORT_SYMBOL_GPL(sata_scr_read); 76 77 /** 78 * sata_scr_write - write SCR register of the specified port 79 * @link: ATA link to write SCR for 80 * @reg: SCR to write 81 * @val: value to write 82 * 83 * Write @val to SCR register @reg of @link. This function is 84 * guaranteed to succeed if @link is ap->link, the cable type of 85 * the port is SATA and the port implements ->scr_read. 86 * 87 * LOCKING: 88 * None if @link is ap->link. Kernel thread context otherwise. 89 * 90 * RETURNS: 91 * 0 on success, negative errno on failure. 92 */ 93 int sata_scr_write(struct ata_link *link, int reg, u32 val) 94 { 95 if (ata_is_host_link(link)) { 96 if (sata_scr_valid(link)) 97 return link->ap->ops->scr_write(link, reg, val); 98 return -EOPNOTSUPP; 99 } 100 101 return sata_pmp_scr_write(link, reg, val); 102 } 103 EXPORT_SYMBOL_GPL(sata_scr_write); 104 105 /** 106 * sata_scr_write_flush - write SCR register of the specified port and flush 107 * @link: ATA link to write SCR for 108 * @reg: SCR to write 109 * @val: value to write 110 * 111 * This function is identical to sata_scr_write() except that this 112 * function performs flush after writing to the register. 113 * 114 * LOCKING: 115 * None if @link is ap->link. Kernel thread context otherwise. 116 * 117 * RETURNS: 118 * 0 on success, negative errno on failure. 119 */ 120 int sata_scr_write_flush(struct ata_link *link, int reg, u32 val) 121 { 122 if (ata_is_host_link(link)) { 123 int rc; 124 125 if (sata_scr_valid(link)) { 126 rc = link->ap->ops->scr_write(link, reg, val); 127 if (rc == 0) 128 rc = link->ap->ops->scr_read(link, reg, &val); 129 return rc; 130 } 131 return -EOPNOTSUPP; 132 } 133 134 return sata_pmp_scr_write(link, reg, val); 135 } 136 EXPORT_SYMBOL_GPL(sata_scr_write_flush); 137 138 /** 139 * ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure 140 * @tf: Taskfile to convert 141 * @pmp: Port multiplier port 142 * @is_cmd: This FIS is for command 143 * @fis: Buffer into which data will output 144 * 145 * Converts a standard ATA taskfile to a Serial ATA 146 * FIS structure (Register - Host to Device). 147 * 148 * LOCKING: 149 * Inherited from caller. 150 */ 151 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis) 152 { 153 fis[0] = 0x27; /* Register - Host to Device FIS */ 154 fis[1] = pmp & 0xf; /* Port multiplier number*/ 155 if (is_cmd) 156 fis[1] |= (1 << 7); /* bit 7 indicates Command FIS */ 157 158 fis[2] = tf->command; 159 fis[3] = tf->feature; 160 161 fis[4] = tf->lbal; 162 fis[5] = tf->lbam; 163 fis[6] = tf->lbah; 164 fis[7] = tf->device; 165 166 fis[8] = tf->hob_lbal; 167 fis[9] = tf->hob_lbam; 168 fis[10] = tf->hob_lbah; 169 fis[11] = tf->hob_feature; 170 171 fis[12] = tf->nsect; 172 fis[13] = tf->hob_nsect; 173 fis[14] = 0; 174 fis[15] = tf->ctl; 175 176 fis[16] = tf->auxiliary & 0xff; 177 fis[17] = (tf->auxiliary >> 8) & 0xff; 178 fis[18] = (tf->auxiliary >> 16) & 0xff; 179 fis[19] = (tf->auxiliary >> 24) & 0xff; 180 } 181 EXPORT_SYMBOL_GPL(ata_tf_to_fis); 182 183 /** 184 * ata_tf_from_fis - Convert SATA FIS to ATA taskfile 185 * @fis: Buffer from which data will be input 186 * @tf: Taskfile to output 187 * 188 * Converts a serial ATA FIS structure to a standard ATA taskfile. 189 * 190 * LOCKING: 191 * Inherited from caller. 192 */ 193 194 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf) 195 { 196 tf->status = fis[2]; 197 tf->error = fis[3]; 198 199 tf->lbal = fis[4]; 200 tf->lbam = fis[5]; 201 tf->lbah = fis[6]; 202 tf->device = fis[7]; 203 204 tf->hob_lbal = fis[8]; 205 tf->hob_lbam = fis[9]; 206 tf->hob_lbah = fis[10]; 207 208 tf->nsect = fis[12]; 209 tf->hob_nsect = fis[13]; 210 } 211 EXPORT_SYMBOL_GPL(ata_tf_from_fis); 212 213 /** 214 * sata_link_debounce - debounce SATA phy status 215 * @link: ATA link to debounce SATA phy status for 216 * @params: timing parameters { interval, duration, timeout } in msec 217 * @deadline: deadline jiffies for the operation 218 * 219 * Make sure SStatus of @link reaches stable state, determined by 220 * holding the same value where DET is not 1 for @duration polled 221 * every @interval, before @timeout. Timeout constraints the 222 * beginning of the stable state. Because DET gets stuck at 1 on 223 * some controllers after hot unplugging, this functions waits 224 * until timeout then returns 0 if DET is stable at 1. 225 * 226 * @timeout is further limited by @deadline. The sooner of the 227 * two is used. 228 * 229 * LOCKING: 230 * Kernel thread context (may sleep) 231 * 232 * RETURNS: 233 * 0 on success, -errno on failure. 234 */ 235 int sata_link_debounce(struct ata_link *link, const unsigned int *params, 236 unsigned long deadline) 237 { 238 unsigned int interval = params[0]; 239 unsigned int duration = params[1]; 240 unsigned long last_jiffies, t; 241 u32 last, cur; 242 int rc; 243 244 t = ata_deadline(jiffies, params[2]); 245 if (time_before(t, deadline)) 246 deadline = t; 247 248 if ((rc = sata_scr_read(link, SCR_STATUS, &cur))) 249 return rc; 250 cur &= 0xf; 251 252 last = cur; 253 last_jiffies = jiffies; 254 255 while (1) { 256 ata_msleep(link->ap, interval); 257 if ((rc = sata_scr_read(link, SCR_STATUS, &cur))) 258 return rc; 259 cur &= 0xf; 260 261 /* DET stable? */ 262 if (cur == last) { 263 if (cur == 1 && time_before(jiffies, deadline)) 264 continue; 265 if (time_after(jiffies, 266 ata_deadline(last_jiffies, duration))) 267 return 0; 268 continue; 269 } 270 271 /* unstable, start over */ 272 last = cur; 273 last_jiffies = jiffies; 274 275 /* Check deadline. If debouncing failed, return 276 * -EPIPE to tell upper layer to lower link speed. 277 */ 278 if (time_after(jiffies, deadline)) 279 return -EPIPE; 280 } 281 } 282 EXPORT_SYMBOL_GPL(sata_link_debounce); 283 284 /** 285 * sata_link_resume - resume SATA link 286 * @link: ATA link to resume SATA 287 * @params: timing parameters { interval, duration, timeout } in msec 288 * @deadline: deadline jiffies for the operation 289 * 290 * Resume SATA phy @link and debounce it. 291 * 292 * LOCKING: 293 * Kernel thread context (may sleep) 294 * 295 * RETURNS: 296 * 0 on success, -errno on failure. 297 */ 298 int sata_link_resume(struct ata_link *link, const unsigned int *params, 299 unsigned long deadline) 300 { 301 int tries = ATA_LINK_RESUME_TRIES; 302 u32 scontrol, serror; 303 int rc; 304 305 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol))) 306 return rc; 307 308 /* 309 * Writes to SControl sometimes get ignored under certain 310 * controllers (ata_piix SIDPR). Make sure DET actually is 311 * cleared. 312 */ 313 do { 314 scontrol = (scontrol & 0x0f0) | 0x300; 315 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol))) 316 return rc; 317 /* 318 * Some PHYs react badly if SStatus is pounded 319 * immediately after resuming. Delay 200ms before 320 * debouncing. 321 */ 322 if (!(link->flags & ATA_LFLAG_NO_DEBOUNCE_DELAY)) 323 ata_msleep(link->ap, 200); 324 325 /* is SControl restored correctly? */ 326 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol))) 327 return rc; 328 } while ((scontrol & 0xf0f) != 0x300 && --tries); 329 330 if ((scontrol & 0xf0f) != 0x300) { 331 ata_link_warn(link, "failed to resume link (SControl %X)\n", 332 scontrol); 333 return 0; 334 } 335 336 if (tries < ATA_LINK_RESUME_TRIES) 337 ata_link_warn(link, "link resume succeeded after %d retries\n", 338 ATA_LINK_RESUME_TRIES - tries); 339 340 if ((rc = sata_link_debounce(link, params, deadline))) 341 return rc; 342 343 /* clear SError, some PHYs require this even for SRST to work */ 344 if (!(rc = sata_scr_read(link, SCR_ERROR, &serror))) 345 rc = sata_scr_write(link, SCR_ERROR, serror); 346 347 return rc != -EINVAL ? rc : 0; 348 } 349 EXPORT_SYMBOL_GPL(sata_link_resume); 350 351 /** 352 * sata_link_scr_lpm - manipulate SControl IPM and SPM fields 353 * @link: ATA link to manipulate SControl for 354 * @policy: LPM policy to configure 355 * @spm_wakeup: initiate LPM transition to active state 356 * 357 * Manipulate the IPM field of the SControl register of @link 358 * according to @policy. If @policy is ATA_LPM_MAX_POWER and 359 * @spm_wakeup is %true, the SPM field is manipulated to wake up 360 * the link. This function also clears PHYRDY_CHG before 361 * returning. 362 * 363 * LOCKING: 364 * EH context. 365 * 366 * RETURNS: 367 * 0 on success, -errno otherwise. 368 */ 369 int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy, 370 bool spm_wakeup) 371 { 372 struct ata_eh_context *ehc = &link->eh_context; 373 bool woken_up = false; 374 u32 scontrol; 375 int rc; 376 377 rc = sata_scr_read(link, SCR_CONTROL, &scontrol); 378 if (rc) 379 return rc; 380 381 switch (policy) { 382 case ATA_LPM_MAX_POWER: 383 /* disable all LPM transitions */ 384 scontrol |= (0x7 << 8); 385 /* initiate transition to active state */ 386 if (spm_wakeup) { 387 scontrol |= (0x4 << 12); 388 woken_up = true; 389 } 390 break; 391 case ATA_LPM_MED_POWER: 392 /* allow LPM to PARTIAL */ 393 scontrol &= ~(0x1 << 8); 394 scontrol |= (0x6 << 8); 395 break; 396 case ATA_LPM_MED_POWER_WITH_DIPM: 397 case ATA_LPM_MIN_POWER_WITH_PARTIAL: 398 case ATA_LPM_MIN_POWER: 399 if (ata_link_nr_enabled(link) > 0) { 400 /* assume no restrictions on LPM transitions */ 401 scontrol &= ~(0x7 << 8); 402 403 /* 404 * If the controller does not support partial, slumber, 405 * or devsleep, then disallow these transitions. 406 */ 407 if (link->ap->host->flags & ATA_HOST_NO_PART) 408 scontrol |= (0x1 << 8); 409 410 if (link->ap->host->flags & ATA_HOST_NO_SSC) 411 scontrol |= (0x2 << 8); 412 413 if (link->ap->host->flags & ATA_HOST_NO_DEVSLP) 414 scontrol |= (0x4 << 8); 415 } else { 416 /* empty port, power off */ 417 scontrol &= ~0xf; 418 scontrol |= (0x1 << 2); 419 } 420 break; 421 default: 422 WARN_ON(1); 423 } 424 425 rc = sata_scr_write(link, SCR_CONTROL, scontrol); 426 if (rc) 427 return rc; 428 429 /* give the link time to transit out of LPM state */ 430 if (woken_up) 431 msleep(10); 432 433 /* clear PHYRDY_CHG from SError */ 434 ehc->i.serror &= ~SERR_PHYRDY_CHG; 435 return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG); 436 } 437 EXPORT_SYMBOL_GPL(sata_link_scr_lpm); 438 439 static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol) 440 { 441 struct ata_link *host_link = &link->ap->link; 442 u32 limit, target, spd; 443 444 limit = link->sata_spd_limit; 445 446 /* Don't configure downstream link faster than upstream link. 447 * It doesn't speed up anything and some PMPs choke on such 448 * configuration. 449 */ 450 if (!ata_is_host_link(link) && host_link->sata_spd) 451 limit &= (1 << host_link->sata_spd) - 1; 452 453 if (limit == UINT_MAX) 454 target = 0; 455 else 456 target = fls(limit); 457 458 spd = (*scontrol >> 4) & 0xf; 459 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4); 460 461 return spd != target; 462 } 463 464 /** 465 * sata_set_spd_needed - is SATA spd configuration needed 466 * @link: Link in question 467 * 468 * Test whether the spd limit in SControl matches 469 * @link->sata_spd_limit. This function is used to determine 470 * whether hardreset is necessary to apply SATA spd 471 * configuration. 472 * 473 * LOCKING: 474 * Inherited from caller. 475 * 476 * RETURNS: 477 * 1 if SATA spd configuration is needed, 0 otherwise. 478 */ 479 static int sata_set_spd_needed(struct ata_link *link) 480 { 481 u32 scontrol; 482 483 if (sata_scr_read(link, SCR_CONTROL, &scontrol)) 484 return 1; 485 486 return __sata_set_spd_needed(link, &scontrol); 487 } 488 489 /** 490 * sata_set_spd - set SATA spd according to spd limit 491 * @link: Link to set SATA spd for 492 * 493 * Set SATA spd of @link according to sata_spd_limit. 494 * 495 * LOCKING: 496 * Inherited from caller. 497 * 498 * RETURNS: 499 * 0 if spd doesn't need to be changed, 1 if spd has been 500 * changed. Negative errno if SCR registers are inaccessible. 501 */ 502 int sata_set_spd(struct ata_link *link) 503 { 504 u32 scontrol; 505 int rc; 506 507 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol))) 508 return rc; 509 510 if (!__sata_set_spd_needed(link, &scontrol)) 511 return 0; 512 513 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol))) 514 return rc; 515 516 return 1; 517 } 518 EXPORT_SYMBOL_GPL(sata_set_spd); 519 520 /** 521 * sata_down_spd_limit - adjust SATA spd limit downward 522 * @link: Link to adjust SATA spd limit for 523 * @spd_limit: Additional limit 524 * 525 * Adjust SATA spd limit of @link downward. Note that this 526 * function only adjusts the limit. The change must be applied 527 * using sata_set_spd(). 528 * 529 * If @spd_limit is non-zero, the speed is limited to equal to or 530 * lower than @spd_limit if such speed is supported. If 531 * @spd_limit is slower than any supported speed, only the lowest 532 * supported speed is allowed. 533 * 534 * LOCKING: 535 * Inherited from caller. 536 * 537 * RETURNS: 538 * 0 on success, negative errno on failure 539 */ 540 int sata_down_spd_limit(struct ata_link *link, u32 spd_limit) 541 { 542 u32 sstatus, spd, mask; 543 int rc, bit; 544 545 if (!sata_scr_valid(link)) 546 return -EOPNOTSUPP; 547 548 /* If SCR can be read, use it to determine the current SPD. 549 * If not, use cached value in link->sata_spd. 550 */ 551 rc = sata_scr_read(link, SCR_STATUS, &sstatus); 552 if (rc == 0 && ata_sstatus_online(sstatus)) 553 spd = (sstatus >> 4) & 0xf; 554 else 555 spd = link->sata_spd; 556 557 mask = link->sata_spd_limit; 558 if (mask <= 1) 559 return -EINVAL; 560 561 /* unconditionally mask off the highest bit */ 562 bit = fls(mask) - 1; 563 mask &= ~(1 << bit); 564 565 /* 566 * Mask off all speeds higher than or equal to the current one. At 567 * this point, if current SPD is not available and we previously 568 * recorded the link speed from SStatus, the driver has already 569 * masked off the highest bit so mask should already be 1 or 0. 570 * Otherwise, we should not force 1.5Gbps on a link where we have 571 * not previously recorded speed from SStatus. Just return in this 572 * case. 573 */ 574 if (spd > 1) 575 mask &= (1 << (spd - 1)) - 1; 576 else if (link->sata_spd) 577 return -EINVAL; 578 579 /* were we already at the bottom? */ 580 if (!mask) 581 return -EINVAL; 582 583 if (spd_limit) { 584 if (mask & ((1 << spd_limit) - 1)) 585 mask &= (1 << spd_limit) - 1; 586 else { 587 bit = ffs(mask) - 1; 588 mask = 1 << bit; 589 } 590 } 591 592 link->sata_spd_limit = mask; 593 594 ata_link_warn(link, "limiting SATA link speed to %s\n", 595 sata_spd_string(fls(mask))); 596 597 return 0; 598 } 599 600 /** 601 * sata_link_hardreset - reset link via SATA phy reset 602 * @link: link to reset 603 * @timing: timing parameters { interval, duration, timeout } in msec 604 * @deadline: deadline jiffies for the operation 605 * @online: optional out parameter indicating link onlineness 606 * @check_ready: optional callback to check link readiness 607 * 608 * SATA phy-reset @link using DET bits of SControl register. 609 * After hardreset, link readiness is waited upon using 610 * ata_wait_ready() if @check_ready is specified. LLDs are 611 * allowed to not specify @check_ready and wait itself after this 612 * function returns. Device classification is LLD's 613 * responsibility. 614 * 615 * *@online is set to one iff reset succeeded and @link is online 616 * after reset. 617 * 618 * LOCKING: 619 * Kernel thread context (may sleep) 620 * 621 * RETURNS: 622 * 0 on success, -errno otherwise. 623 */ 624 int sata_link_hardreset(struct ata_link *link, const unsigned int *timing, 625 unsigned long deadline, 626 bool *online, int (*check_ready)(struct ata_link *)) 627 { 628 u32 scontrol; 629 int rc; 630 631 if (online) 632 *online = false; 633 634 if (sata_set_spd_needed(link)) { 635 /* SATA spec says nothing about how to reconfigure 636 * spd. To be on the safe side, turn off phy during 637 * reconfiguration. This works for at least ICH7 AHCI 638 * and Sil3124. 639 */ 640 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol))) 641 goto out; 642 643 scontrol = (scontrol & 0x0f0) | 0x304; 644 645 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol))) 646 goto out; 647 648 sata_set_spd(link); 649 } 650 651 /* issue phy wake/reset */ 652 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol))) 653 goto out; 654 655 scontrol = (scontrol & 0x0f0) | 0x301; 656 657 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol))) 658 goto out; 659 660 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1 661 * 10.4.2 says at least 1 ms. 662 */ 663 ata_msleep(link->ap, 1); 664 665 /* bring link back */ 666 rc = sata_link_resume(link, timing, deadline); 667 if (rc) 668 goto out; 669 /* if link is offline nothing more to do */ 670 if (ata_phys_link_offline(link)) 671 goto out; 672 673 /* Link is online. From this point, -ENODEV too is an error. */ 674 if (online) 675 *online = true; 676 677 if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) { 678 /* If PMP is supported, we have to do follow-up SRST. 679 * Some PMPs don't send D2H Reg FIS after hardreset if 680 * the first port is empty. Wait only for 681 * ATA_TMOUT_PMP_SRST_WAIT. 682 */ 683 if (check_ready) { 684 unsigned long pmp_deadline; 685 686 pmp_deadline = ata_deadline(jiffies, 687 ATA_TMOUT_PMP_SRST_WAIT); 688 if (time_after(pmp_deadline, deadline)) 689 pmp_deadline = deadline; 690 ata_wait_ready(link, pmp_deadline, check_ready); 691 } 692 rc = -EAGAIN; 693 goto out; 694 } 695 696 rc = 0; 697 if (check_ready) 698 rc = ata_wait_ready(link, deadline, check_ready); 699 out: 700 if (rc && rc != -EAGAIN) { 701 /* online is set iff link is online && reset succeeded */ 702 if (online) 703 *online = false; 704 } 705 return rc; 706 } 707 EXPORT_SYMBOL_GPL(sata_link_hardreset); 708 709 /** 710 * sata_std_hardreset - COMRESET w/o waiting or classification 711 * @link: link to reset 712 * @class: resulting class of attached device 713 * @deadline: deadline jiffies for the operation 714 * 715 * Standard SATA COMRESET w/o waiting or classification. 716 * 717 * LOCKING: 718 * Kernel thread context (may sleep) 719 * 720 * RETURNS: 721 * 0 if link offline, -EAGAIN if link online, -errno on errors. 722 */ 723 int sata_std_hardreset(struct ata_link *link, unsigned int *class, 724 unsigned long deadline) 725 { 726 const unsigned int *timing = sata_ehc_deb_timing(&link->eh_context); 727 bool online; 728 int rc; 729 730 rc = sata_link_hardreset(link, timing, deadline, &online, NULL); 731 if (online) 732 return -EAGAIN; 733 return rc; 734 } 735 EXPORT_SYMBOL_GPL(sata_std_hardreset); 736 737 /** 738 * ata_qc_complete_multiple - Complete multiple qcs successfully 739 * @ap: port in question 740 * @qc_active: new qc_active mask 741 * 742 * Complete in-flight commands. This functions is meant to be 743 * called from low-level driver's interrupt routine to complete 744 * requests normally. ap->qc_active and @qc_active is compared 745 * and commands are completed accordingly. 746 * 747 * Always use this function when completing multiple NCQ commands 748 * from IRQ handlers instead of calling ata_qc_complete() 749 * multiple times to keep IRQ expect status properly in sync. 750 * 751 * LOCKING: 752 * spin_lock_irqsave(host lock) 753 * 754 * RETURNS: 755 * Number of completed commands on success, -errno otherwise. 756 */ 757 int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active) 758 { 759 u64 done_mask, ap_qc_active = ap->qc_active; 760 int nr_done = 0; 761 762 /* 763 * If the internal tag is set on ap->qc_active, then we care about 764 * bit0 on the passed in qc_active mask. Move that bit up to match 765 * the internal tag. 766 */ 767 if (ap_qc_active & (1ULL << ATA_TAG_INTERNAL)) { 768 qc_active |= (qc_active & 0x01) << ATA_TAG_INTERNAL; 769 qc_active ^= qc_active & 0x01; 770 } 771 772 done_mask = ap_qc_active ^ qc_active; 773 774 if (unlikely(done_mask & qc_active)) { 775 ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n", 776 ap->qc_active, qc_active); 777 return -EINVAL; 778 } 779 780 if (ap->ops->qc_ncq_fill_rtf) 781 ap->ops->qc_ncq_fill_rtf(ap, done_mask); 782 783 while (done_mask) { 784 struct ata_queued_cmd *qc; 785 unsigned int tag = __ffs64(done_mask); 786 787 qc = ata_qc_from_tag(ap, tag); 788 if (qc) { 789 ata_qc_complete(qc); 790 nr_done++; 791 } 792 done_mask &= ~(1ULL << tag); 793 } 794 795 return nr_done; 796 } 797 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple); 798 799 /** 800 * ata_slave_link_init - initialize slave link 801 * @ap: port to initialize slave link for 802 * 803 * Create and initialize slave link for @ap. This enables slave 804 * link handling on the port. 805 * 806 * In libata, a port contains links and a link contains devices. 807 * There is single host link but if a PMP is attached to it, 808 * there can be multiple fan-out links. On SATA, there's usually 809 * a single device connected to a link but PATA and SATA 810 * controllers emulating TF based interface can have two - master 811 * and slave. 812 * 813 * However, there are a few controllers which don't fit into this 814 * abstraction too well - SATA controllers which emulate TF 815 * interface with both master and slave devices but also have 816 * separate SCR register sets for each device. These controllers 817 * need separate links for physical link handling 818 * (e.g. onlineness, link speed) but should be treated like a 819 * traditional M/S controller for everything else (e.g. command 820 * issue, softreset). 821 * 822 * slave_link is libata's way of handling this class of 823 * controllers without impacting core layer too much. For 824 * anything other than physical link handling, the default host 825 * link is used for both master and slave. For physical link 826 * handling, separate @ap->slave_link is used. All dirty details 827 * are implemented inside libata core layer. From LLD's POV, the 828 * only difference is that prereset, hardreset and postreset are 829 * called once more for the slave link, so the reset sequence 830 * looks like the following. 831 * 832 * prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) -> 833 * softreset(M) -> postreset(M) -> postreset(S) 834 * 835 * Note that softreset is called only for the master. Softreset 836 * resets both M/S by definition, so SRST on master should handle 837 * both (the standard method will work just fine). 838 * 839 * LOCKING: 840 * Should be called before host is registered. 841 * 842 * RETURNS: 843 * 0 on success, -errno on failure. 844 */ 845 int ata_slave_link_init(struct ata_port *ap) 846 { 847 struct ata_link *link; 848 849 WARN_ON(ap->slave_link); 850 WARN_ON(ap->flags & ATA_FLAG_PMP); 851 852 link = kzalloc(sizeof(*link), GFP_KERNEL); 853 if (!link) 854 return -ENOMEM; 855 856 ata_link_init(ap, link, 1); 857 ap->slave_link = link; 858 return 0; 859 } 860 EXPORT_SYMBOL_GPL(ata_slave_link_init); 861 862 /** 863 * sata_lpm_ignore_phy_events - test if PHY event should be ignored 864 * @link: Link receiving the event 865 * 866 * Test whether the received PHY event has to be ignored or not. 867 * 868 * LOCKING: 869 * None: 870 * 871 * RETURNS: 872 * True if the event has to be ignored. 873 */ 874 bool sata_lpm_ignore_phy_events(struct ata_link *link) 875 { 876 unsigned long lpm_timeout = link->last_lpm_change + 877 msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY); 878 879 /* if LPM is enabled, PHYRDY doesn't mean anything */ 880 if (link->lpm_policy > ATA_LPM_MAX_POWER) 881 return true; 882 883 /* ignore the first PHY event after the LPM policy changed 884 * as it is might be spurious 885 */ 886 if ((link->flags & ATA_LFLAG_CHANGED) && 887 time_before(jiffies, lpm_timeout)) 888 return true; 889 890 return false; 891 } 892 EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events); 893 894 static const char *ata_lpm_policy_names[] = { 895 [ATA_LPM_UNKNOWN] = "keep_firmware_settings", 896 [ATA_LPM_MAX_POWER] = "max_performance", 897 [ATA_LPM_MED_POWER] = "medium_power", 898 [ATA_LPM_MED_POWER_WITH_DIPM] = "med_power_with_dipm", 899 [ATA_LPM_MIN_POWER_WITH_PARTIAL] = "min_power_with_partial", 900 [ATA_LPM_MIN_POWER] = "min_power", 901 }; 902 903 static ssize_t ata_scsi_lpm_store(struct device *device, 904 struct device_attribute *attr, 905 const char *buf, size_t count) 906 { 907 struct Scsi_Host *shost = class_to_shost(device); 908 struct ata_port *ap = ata_shost_to_port(shost); 909 struct ata_link *link; 910 struct ata_device *dev; 911 enum ata_lpm_policy policy; 912 unsigned long flags; 913 914 /* UNKNOWN is internal state, iterate from MAX_POWER */ 915 for (policy = ATA_LPM_MAX_POWER; 916 policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) { 917 const char *name = ata_lpm_policy_names[policy]; 918 919 if (strncmp(name, buf, strlen(name)) == 0) 920 break; 921 } 922 if (policy == ARRAY_SIZE(ata_lpm_policy_names)) 923 return -EINVAL; 924 925 spin_lock_irqsave(ap->lock, flags); 926 927 if (ap->flags & ATA_FLAG_NO_LPM) { 928 count = -EOPNOTSUPP; 929 goto out_unlock; 930 } 931 932 ata_for_each_link(link, ap, EDGE) { 933 ata_for_each_dev(dev, &ap->link, ENABLED) { 934 if (dev->quirks & ATA_QUIRK_NOLPM) { 935 count = -EOPNOTSUPP; 936 goto out_unlock; 937 } 938 } 939 } 940 941 ap->target_lpm_policy = policy; 942 ata_port_schedule_eh(ap); 943 out_unlock: 944 spin_unlock_irqrestore(ap->lock, flags); 945 return count; 946 } 947 948 static ssize_t ata_scsi_lpm_show(struct device *dev, 949 struct device_attribute *attr, char *buf) 950 { 951 struct Scsi_Host *shost = class_to_shost(dev); 952 struct ata_port *ap = ata_shost_to_port(shost); 953 954 if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names)) 955 return -EINVAL; 956 957 return sysfs_emit(buf, "%s\n", 958 ata_lpm_policy_names[ap->target_lpm_policy]); 959 } 960 DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR, 961 ata_scsi_lpm_show, ata_scsi_lpm_store); 962 EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy); 963 964 /** 965 * ata_ncq_prio_supported - Check if device supports NCQ Priority 966 * @ap: ATA port of the target device 967 * @sdev: SCSI device 968 * @supported: Address of a boolean to store the result 969 * 970 * Helper to check if device supports NCQ Priority feature. 971 * 972 * Context: Any context. Takes and releases @ap->lock. 973 * 974 * Return: 975 * * %0 - OK. Status is stored into @supported 976 * * %-ENODEV - Failed to find the ATA device 977 */ 978 int ata_ncq_prio_supported(struct ata_port *ap, struct scsi_device *sdev, 979 bool *supported) 980 { 981 struct ata_device *dev; 982 unsigned long flags; 983 int rc = 0; 984 985 spin_lock_irqsave(ap->lock, flags); 986 dev = ata_scsi_find_dev(ap, sdev); 987 if (!dev) 988 rc = -ENODEV; 989 else 990 *supported = dev->flags & ATA_DFLAG_NCQ_PRIO; 991 spin_unlock_irqrestore(ap->lock, flags); 992 993 return rc; 994 } 995 EXPORT_SYMBOL_GPL(ata_ncq_prio_supported); 996 997 static ssize_t ata_ncq_prio_supported_show(struct device *device, 998 struct device_attribute *attr, 999 char *buf) 1000 { 1001 struct scsi_device *sdev = to_scsi_device(device); 1002 struct ata_port *ap = ata_shost_to_port(sdev->host); 1003 bool supported; 1004 int rc; 1005 1006 rc = ata_ncq_prio_supported(ap, sdev, &supported); 1007 if (rc) 1008 return rc; 1009 1010 return sysfs_emit(buf, "%d\n", supported); 1011 } 1012 1013 DEVICE_ATTR(ncq_prio_supported, S_IRUGO, ata_ncq_prio_supported_show, NULL); 1014 EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_supported); 1015 1016 /** 1017 * ata_ncq_prio_enabled - Check if NCQ Priority is enabled 1018 * @ap: ATA port of the target device 1019 * @sdev: SCSI device 1020 * @enabled: Address of a boolean to store the result 1021 * 1022 * Helper to check if NCQ Priority feature is enabled. 1023 * 1024 * Context: Any context. Takes and releases @ap->lock. 1025 * 1026 * Return: 1027 * * %0 - OK. Status is stored into @enabled 1028 * * %-ENODEV - Failed to find the ATA device 1029 */ 1030 int ata_ncq_prio_enabled(struct ata_port *ap, struct scsi_device *sdev, 1031 bool *enabled) 1032 { 1033 struct ata_device *dev; 1034 unsigned long flags; 1035 int rc = 0; 1036 1037 spin_lock_irqsave(ap->lock, flags); 1038 dev = ata_scsi_find_dev(ap, sdev); 1039 if (!dev) 1040 rc = -ENODEV; 1041 else 1042 *enabled = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLED; 1043 spin_unlock_irqrestore(ap->lock, flags); 1044 1045 return rc; 1046 } 1047 EXPORT_SYMBOL_GPL(ata_ncq_prio_enabled); 1048 1049 static ssize_t ata_ncq_prio_enable_show(struct device *device, 1050 struct device_attribute *attr, 1051 char *buf) 1052 { 1053 struct scsi_device *sdev = to_scsi_device(device); 1054 struct ata_port *ap = ata_shost_to_port(sdev->host); 1055 bool enabled; 1056 int rc; 1057 1058 rc = ata_ncq_prio_enabled(ap, sdev, &enabled); 1059 if (rc) 1060 return rc; 1061 1062 return sysfs_emit(buf, "%d\n", enabled); 1063 } 1064 1065 /** 1066 * ata_ncq_prio_enable - Enable/disable NCQ Priority 1067 * @ap: ATA port of the target device 1068 * @sdev: SCSI device 1069 * @enable: true - enable NCQ Priority, false - disable NCQ Priority 1070 * 1071 * Helper to enable/disable NCQ Priority feature. 1072 * 1073 * Context: Any context. Takes and releases @ap->lock. 1074 * 1075 * Return: 1076 * * %0 - OK. Status is stored into @enabled 1077 * * %-ENODEV - Failed to find the ATA device 1078 * * %-EINVAL - NCQ Priority is not supported or CDL is enabled 1079 */ 1080 int ata_ncq_prio_enable(struct ata_port *ap, struct scsi_device *sdev, 1081 bool enable) 1082 { 1083 struct ata_device *dev; 1084 unsigned long flags; 1085 int rc = 0; 1086 1087 spin_lock_irqsave(ap->lock, flags); 1088 1089 dev = ata_scsi_find_dev(ap, sdev); 1090 if (!dev) { 1091 rc = -ENODEV; 1092 goto unlock; 1093 } 1094 1095 if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) { 1096 rc = -EINVAL; 1097 goto unlock; 1098 } 1099 1100 if (enable) { 1101 if (dev->flags & ATA_DFLAG_CDL_ENABLED) { 1102 ata_dev_err(dev, 1103 "CDL must be disabled to enable NCQ priority\n"); 1104 rc = -EINVAL; 1105 goto unlock; 1106 } 1107 dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLED; 1108 } else { 1109 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLED; 1110 } 1111 1112 unlock: 1113 spin_unlock_irqrestore(ap->lock, flags); 1114 1115 return rc; 1116 } 1117 EXPORT_SYMBOL_GPL(ata_ncq_prio_enable); 1118 1119 static ssize_t ata_ncq_prio_enable_store(struct device *device, 1120 struct device_attribute *attr, 1121 const char *buf, size_t len) 1122 { 1123 struct scsi_device *sdev = to_scsi_device(device); 1124 struct ata_port *ap = ata_shost_to_port(sdev->host); 1125 bool enable; 1126 int rc; 1127 1128 rc = kstrtobool(buf, &enable); 1129 if (rc) 1130 return rc; 1131 1132 rc = ata_ncq_prio_enable(ap, sdev, enable); 1133 if (rc) 1134 return rc; 1135 1136 return len; 1137 } 1138 1139 DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR, 1140 ata_ncq_prio_enable_show, ata_ncq_prio_enable_store); 1141 EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable); 1142 1143 static struct attribute *ata_ncq_sdev_attrs[] = { 1144 &dev_attr_unload_heads.attr, 1145 &dev_attr_ncq_prio_enable.attr, 1146 &dev_attr_ncq_prio_supported.attr, 1147 NULL 1148 }; 1149 1150 static const struct attribute_group ata_ncq_sdev_attr_group = { 1151 .attrs = ata_ncq_sdev_attrs 1152 }; 1153 1154 const struct attribute_group *ata_ncq_sdev_groups[] = { 1155 &ata_ncq_sdev_attr_group, 1156 NULL 1157 }; 1158 EXPORT_SYMBOL_GPL(ata_ncq_sdev_groups); 1159 1160 static ssize_t 1161 ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr, 1162 const char *buf, size_t count) 1163 { 1164 struct Scsi_Host *shost = class_to_shost(dev); 1165 struct ata_port *ap = ata_shost_to_port(shost); 1166 if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM)) 1167 return ap->ops->em_store(ap, buf, count); 1168 return -EINVAL; 1169 } 1170 1171 static ssize_t 1172 ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr, 1173 char *buf) 1174 { 1175 struct Scsi_Host *shost = class_to_shost(dev); 1176 struct ata_port *ap = ata_shost_to_port(shost); 1177 1178 if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM)) 1179 return ap->ops->em_show(ap, buf); 1180 return -EINVAL; 1181 } 1182 DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR, 1183 ata_scsi_em_message_show, ata_scsi_em_message_store); 1184 EXPORT_SYMBOL_GPL(dev_attr_em_message); 1185 1186 static ssize_t 1187 ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr, 1188 char *buf) 1189 { 1190 struct Scsi_Host *shost = class_to_shost(dev); 1191 struct ata_port *ap = ata_shost_to_port(shost); 1192 1193 return sysfs_emit(buf, "%d\n", ap->em_message_type); 1194 } 1195 DEVICE_ATTR(em_message_type, S_IRUGO, 1196 ata_scsi_em_message_type_show, NULL); 1197 EXPORT_SYMBOL_GPL(dev_attr_em_message_type); 1198 1199 static ssize_t 1200 ata_scsi_activity_show(struct device *dev, struct device_attribute *attr, 1201 char *buf) 1202 { 1203 struct scsi_device *sdev = to_scsi_device(dev); 1204 struct ata_port *ap = ata_shost_to_port(sdev->host); 1205 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev); 1206 1207 if (atadev && ap->ops->sw_activity_show && 1208 (ap->flags & ATA_FLAG_SW_ACTIVITY)) 1209 return ap->ops->sw_activity_show(atadev, buf); 1210 return -EINVAL; 1211 } 1212 1213 static ssize_t 1214 ata_scsi_activity_store(struct device *dev, struct device_attribute *attr, 1215 const char *buf, size_t count) 1216 { 1217 struct scsi_device *sdev = to_scsi_device(dev); 1218 struct ata_port *ap = ata_shost_to_port(sdev->host); 1219 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev); 1220 enum sw_activity val; 1221 int rc; 1222 1223 if (atadev && ap->ops->sw_activity_store && 1224 (ap->flags & ATA_FLAG_SW_ACTIVITY)) { 1225 val = simple_strtoul(buf, NULL, 0); 1226 switch (val) { 1227 case OFF: case BLINK_ON: case BLINK_OFF: 1228 rc = ap->ops->sw_activity_store(atadev, val); 1229 if (!rc) 1230 return count; 1231 else 1232 return rc; 1233 } 1234 } 1235 return -EINVAL; 1236 } 1237 DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show, 1238 ata_scsi_activity_store); 1239 EXPORT_SYMBOL_GPL(dev_attr_sw_activity); 1240 1241 /** 1242 * ata_change_queue_depth - Set a device maximum queue depth 1243 * @ap: ATA port of the target device 1244 * @sdev: SCSI device to configure queue depth for 1245 * @queue_depth: new queue depth 1246 * 1247 * Helper to set a device maximum queue depth, usable with both libsas 1248 * and libata. 1249 * 1250 */ 1251 int ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev, 1252 int queue_depth) 1253 { 1254 struct ata_device *dev; 1255 unsigned long flags; 1256 int max_queue_depth; 1257 1258 spin_lock_irqsave(ap->lock, flags); 1259 1260 dev = ata_scsi_find_dev(ap, sdev); 1261 if (!dev || queue_depth < 1 || queue_depth == sdev->queue_depth) { 1262 spin_unlock_irqrestore(ap->lock, flags); 1263 return sdev->queue_depth; 1264 } 1265 1266 /* 1267 * Make sure that the queue depth requested does not exceed the device 1268 * capabilities. 1269 */ 1270 max_queue_depth = min(ATA_MAX_QUEUE, sdev->host->can_queue); 1271 max_queue_depth = min(max_queue_depth, ata_id_queue_depth(dev->id)); 1272 if (queue_depth > max_queue_depth) { 1273 spin_unlock_irqrestore(ap->lock, flags); 1274 return -EINVAL; 1275 } 1276 1277 /* 1278 * If NCQ is not supported by the device or if the target queue depth 1279 * is 1 (to disable drive side command queueing), turn off NCQ. 1280 */ 1281 if (queue_depth == 1 || !ata_ncq_supported(dev)) { 1282 dev->flags |= ATA_DFLAG_NCQ_OFF; 1283 queue_depth = 1; 1284 } else { 1285 dev->flags &= ~ATA_DFLAG_NCQ_OFF; 1286 } 1287 1288 spin_unlock_irqrestore(ap->lock, flags); 1289 1290 if (queue_depth == sdev->queue_depth) 1291 return sdev->queue_depth; 1292 1293 return scsi_change_queue_depth(sdev, queue_depth); 1294 } 1295 EXPORT_SYMBOL_GPL(ata_change_queue_depth); 1296 1297 /** 1298 * ata_scsi_change_queue_depth - SCSI callback for queue depth config 1299 * @sdev: SCSI device to configure queue depth for 1300 * @queue_depth: new queue depth 1301 * 1302 * This is libata standard hostt->change_queue_depth callback. 1303 * SCSI will call into this callback when user tries to set queue 1304 * depth via sysfs. 1305 * 1306 * LOCKING: 1307 * SCSI layer (we don't care) 1308 * 1309 * RETURNS: 1310 * Newly configured queue depth. 1311 */ 1312 int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth) 1313 { 1314 struct ata_port *ap = ata_shost_to_port(sdev->host); 1315 1316 return ata_change_queue_depth(ap, sdev, queue_depth); 1317 } 1318 EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth); 1319 1320 /** 1321 * ata_sas_sdev_configure - Default sdev_configure routine for libata 1322 * devices 1323 * @sdev: SCSI device to configure 1324 * @lim: queue limits 1325 * @ap: ATA port to which SCSI device is attached 1326 * 1327 * RETURNS: 1328 * Zero. 1329 */ 1330 1331 int ata_sas_sdev_configure(struct scsi_device *sdev, struct queue_limits *lim, 1332 struct ata_port *ap) 1333 { 1334 ata_scsi_sdev_config(sdev); 1335 1336 return ata_scsi_dev_config(sdev, lim, ap->link.device); 1337 } 1338 EXPORT_SYMBOL_GPL(ata_sas_sdev_configure); 1339 1340 /** 1341 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device 1342 * @cmd: SCSI command to be sent 1343 * @ap: ATA port to which the command is being sent 1344 * 1345 * RETURNS: 1346 * Return value from __ata_scsi_queuecmd() if @cmd can be queued, 1347 * 0 otherwise. 1348 */ 1349 1350 int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap) 1351 { 1352 int rc = 0; 1353 1354 if (likely(ata_dev_enabled(ap->link.device))) 1355 rc = __ata_scsi_queuecmd(cmd, ap->link.device); 1356 else { 1357 cmd->result = (DID_BAD_TARGET << 16); 1358 scsi_done(cmd); 1359 } 1360 return rc; 1361 } 1362 EXPORT_SYMBOL_GPL(ata_sas_queuecmd); 1363 1364 /** 1365 * sata_async_notification - SATA async notification handler 1366 * @ap: ATA port where async notification is received 1367 * 1368 * Handler to be called when async notification via SDB FIS is 1369 * received. This function schedules EH if necessary. 1370 * 1371 * LOCKING: 1372 * spin_lock_irqsave(host lock) 1373 * 1374 * RETURNS: 1375 * 1 if EH is scheduled, 0 otherwise. 1376 */ 1377 int sata_async_notification(struct ata_port *ap) 1378 { 1379 u32 sntf; 1380 int rc; 1381 1382 if (!(ap->flags & ATA_FLAG_AN)) 1383 return 0; 1384 1385 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf); 1386 if (rc == 0) 1387 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf); 1388 1389 if (!sata_pmp_attached(ap) || rc) { 1390 /* PMP is not attached or SNTF is not available */ 1391 if (!sata_pmp_attached(ap)) { 1392 /* PMP is not attached. Check whether ATAPI 1393 * AN is configured. If so, notify media 1394 * change. 1395 */ 1396 struct ata_device *dev = ap->link.device; 1397 1398 if ((dev->class == ATA_DEV_ATAPI) && 1399 (dev->flags & ATA_DFLAG_AN)) 1400 ata_scsi_media_change_notify(dev); 1401 return 0; 1402 } else { 1403 /* PMP is attached but SNTF is not available. 1404 * ATAPI async media change notification is 1405 * not used. The PMP must be reporting PHY 1406 * status change, schedule EH. 1407 */ 1408 ata_port_schedule_eh(ap); 1409 return 1; 1410 } 1411 } else { 1412 /* PMP is attached and SNTF is available */ 1413 struct ata_link *link; 1414 1415 /* check and notify ATAPI AN */ 1416 ata_for_each_link(link, ap, EDGE) { 1417 if (!(sntf & (1 << link->pmp))) 1418 continue; 1419 1420 if ((link->device->class == ATA_DEV_ATAPI) && 1421 (link->device->flags & ATA_DFLAG_AN)) 1422 ata_scsi_media_change_notify(link->device); 1423 } 1424 1425 /* If PMP is reporting that PHY status of some 1426 * downstream ports has changed, schedule EH. 1427 */ 1428 if (sntf & (1 << SATA_PMP_CTRL_PORT)) { 1429 ata_port_schedule_eh(ap); 1430 return 1; 1431 } 1432 1433 return 0; 1434 } 1435 } 1436 EXPORT_SYMBOL_GPL(sata_async_notification); 1437 1438 /** 1439 * ata_eh_read_log_10h - Read log page 10h for NCQ error details 1440 * @dev: Device to read log page 10h from 1441 * @tag: Resulting tag of the failed command 1442 * @tf: Resulting taskfile registers of the failed command 1443 * 1444 * Read log page 10h to obtain NCQ error details and clear error 1445 * condition. 1446 * 1447 * LOCKING: 1448 * Kernel thread context (may sleep). 1449 * 1450 * RETURNS: 1451 * 0 on success, -errno otherwise. 1452 */ 1453 static int ata_eh_read_log_10h(struct ata_device *dev, 1454 int *tag, struct ata_taskfile *tf) 1455 { 1456 u8 *buf = dev->sector_buf; 1457 unsigned int err_mask; 1458 u8 csum; 1459 int i; 1460 1461 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, 0, buf, 1); 1462 if (err_mask) 1463 return -EIO; 1464 1465 csum = 0; 1466 for (i = 0; i < ATA_SECT_SIZE; i++) 1467 csum += buf[i]; 1468 if (csum) 1469 ata_dev_warn(dev, "invalid checksum 0x%x on log page 10h\n", 1470 csum); 1471 1472 if (buf[0] & 0x80) 1473 return -ENOENT; 1474 1475 *tag = buf[0] & 0x1f; 1476 1477 tf->status = buf[2]; 1478 tf->error = buf[3]; 1479 tf->lbal = buf[4]; 1480 tf->lbam = buf[5]; 1481 tf->lbah = buf[6]; 1482 tf->device = buf[7]; 1483 tf->hob_lbal = buf[8]; 1484 tf->hob_lbam = buf[9]; 1485 tf->hob_lbah = buf[10]; 1486 tf->nsect = buf[12]; 1487 tf->hob_nsect = buf[13]; 1488 if (ata_id_has_ncq_autosense(dev->id) && (tf->status & ATA_SENSE)) 1489 tf->auxiliary = buf[14] << 16 | buf[15] << 8 | buf[16]; 1490 1491 return 0; 1492 } 1493 1494 /** 1495 * ata_eh_get_ncq_success_sense - Read and process the sense data for 1496 * successful NCQ commands log page 1497 * @link: ATA link to get sense data for 1498 * 1499 * Read the sense data for successful NCQ commands log page to obtain 1500 * sense data for all NCQ commands that completed successfully with 1501 * the sense data available bit set. 1502 * 1503 * LOCKING: 1504 * Kernel thread context (may sleep). 1505 * 1506 * RETURNS: 1507 * 0 on success, -errno otherwise. 1508 */ 1509 int ata_eh_get_ncq_success_sense(struct ata_link *link) 1510 { 1511 struct ata_device *dev = link->device; 1512 struct ata_port *ap = dev->link->ap; 1513 u8 *buf = dev->cdl->ncq_sense_log_buf; 1514 struct ata_queued_cmd *qc; 1515 unsigned int err_mask, tag; 1516 u8 *sense, sk = 0, asc = 0, ascq = 0; 1517 u16 extended_sense; 1518 bool aux_icc_valid; 1519 u32 sense_valid; 1520 u64 val; 1521 int ret = 0; 1522 1523 err_mask = ata_read_log_page(dev, ATA_LOG_SENSE_NCQ, 0, buf, 2); 1524 if (err_mask) { 1525 ata_dev_err(dev, 1526 "Failed to read Sense Data for Successful NCQ Commands log\n"); 1527 return -EIO; 1528 } 1529 1530 /* Check the log header */ 1531 val = get_unaligned_le64(&buf[0]); 1532 if ((val & 0xffff) != 1 || ((val >> 16) & 0xff) != 0x0f) { 1533 ata_dev_err(dev, 1534 "Invalid Sense Data for Successful NCQ Commands log\n"); 1535 return -EIO; 1536 } 1537 1538 sense_valid = get_unaligned_le32(&buf[8]); 1539 extended_sense = get_unaligned_le16(&buf[14]); 1540 aux_icc_valid = extended_sense & BIT(15); 1541 1542 ata_qc_for_each_raw(ap, qc, tag) { 1543 if (!(qc->flags & ATA_QCFLAG_EH) || 1544 !(qc->flags & ATA_QCFLAG_EH_SUCCESS_CMD) || 1545 qc->err_mask || 1546 ata_dev_phys_link(qc->dev) != link) 1547 continue; 1548 1549 /* 1550 * If the command does not have any sense data, clear ATA_SENSE. 1551 * Keep ATA_QCFLAG_EH_SUCCESS_CMD so that command is finished. 1552 */ 1553 if (!(sense_valid & BIT(tag))) { 1554 qc->result_tf.status &= ~ATA_SENSE; 1555 continue; 1556 } 1557 1558 sense = &buf[32 + 24 * tag]; 1559 sk = sense[0]; 1560 asc = sense[1]; 1561 ascq = sense[2]; 1562 1563 if (!ata_scsi_sense_is_valid(sk, asc, ascq)) { 1564 ret = -EIO; 1565 continue; 1566 } 1567 1568 qc->result_tf.nsect = sense[6]; 1569 qc->result_tf.hob_nsect = sense[7]; 1570 qc->result_tf.lbal = sense[8]; 1571 qc->result_tf.lbam = sense[9]; 1572 qc->result_tf.lbah = sense[10]; 1573 qc->result_tf.hob_lbal = sense[11]; 1574 qc->result_tf.hob_lbam = sense[12]; 1575 qc->result_tf.hob_lbah = sense[13]; 1576 if (aux_icc_valid) 1577 qc->result_tf.auxiliary = get_unaligned_le32(&sense[16]); 1578 1579 /* Set sense without also setting scsicmd->result */ 1580 scsi_build_sense_buffer(dev->flags & ATA_DFLAG_D_SENSE, 1581 qc->scsicmd->sense_buffer, sk, 1582 asc, ascq); 1583 qc->flags |= ATA_QCFLAG_SENSE_VALID; 1584 1585 /* 1586 * No point in checking the return value, since the command has 1587 * already completed successfully. 1588 */ 1589 ata_eh_decide_disposition(qc); 1590 } 1591 1592 return ret; 1593 } 1594 1595 /** 1596 * ata_eh_analyze_ncq_error - analyze NCQ error 1597 * @link: ATA link to analyze NCQ error for 1598 * 1599 * Read log page 10h, determine the offending qc and acquire 1600 * error status TF. For NCQ device errors, all LLDDs have to do 1601 * is setting AC_ERR_DEV in ehi->err_mask. This function takes 1602 * care of the rest. 1603 * 1604 * LOCKING: 1605 * Kernel thread context (may sleep). 1606 */ 1607 void ata_eh_analyze_ncq_error(struct ata_link *link) 1608 { 1609 struct ata_port *ap = link->ap; 1610 struct ata_eh_context *ehc = &link->eh_context; 1611 struct ata_device *dev = link->device; 1612 struct ata_queued_cmd *qc; 1613 struct ata_taskfile tf; 1614 int tag, rc; 1615 1616 /* if frozen, we can't do much */ 1617 if (ata_port_is_frozen(ap)) 1618 return; 1619 1620 /* is it NCQ device error? */ 1621 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV)) 1622 return; 1623 1624 /* has LLDD analyzed already? */ 1625 ata_qc_for_each_raw(ap, qc, tag) { 1626 if (!(qc->flags & ATA_QCFLAG_EH)) 1627 continue; 1628 1629 if (qc->err_mask) 1630 return; 1631 } 1632 1633 /* okay, this error is ours */ 1634 memset(&tf, 0, sizeof(tf)); 1635 rc = ata_eh_read_log_10h(dev, &tag, &tf); 1636 if (rc) { 1637 ata_link_err(link, "failed to read log page 10h (errno=%d)\n", 1638 rc); 1639 return; 1640 } 1641 1642 if (!(link->sactive & BIT(tag))) { 1643 ata_link_err(link, "log page 10h reported inactive tag %d\n", 1644 tag); 1645 return; 1646 } 1647 1648 /* we've got the perpetrator, condemn it */ 1649 qc = __ata_qc_from_tag(ap, tag); 1650 memcpy(&qc->result_tf, &tf, sizeof(tf)); 1651 qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48; 1652 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ; 1653 1654 /* 1655 * If the device supports NCQ autosense, ata_eh_read_log_10h() will have 1656 * stored the sense data in qc->result_tf.auxiliary. 1657 */ 1658 if (qc->result_tf.auxiliary) { 1659 char sense_key, asc, ascq; 1660 1661 sense_key = (qc->result_tf.auxiliary >> 16) & 0xff; 1662 asc = (qc->result_tf.auxiliary >> 8) & 0xff; 1663 ascq = qc->result_tf.auxiliary & 0xff; 1664 if (ata_scsi_sense_is_valid(sense_key, asc, ascq)) { 1665 ata_scsi_set_sense(dev, qc->scsicmd, sense_key, asc, 1666 ascq); 1667 qc->flags |= ATA_QCFLAG_SENSE_VALID; 1668 } 1669 } 1670 1671 ata_qc_for_each_raw(ap, qc, tag) { 1672 if (!(qc->flags & ATA_QCFLAG_EH) || 1673 qc->flags & ATA_QCFLAG_EH_SUCCESS_CMD || 1674 ata_dev_phys_link(qc->dev) != link) 1675 continue; 1676 1677 /* Skip the single QC which caused the NCQ error. */ 1678 if (qc->err_mask) 1679 continue; 1680 1681 /* 1682 * For SATA, the STATUS and ERROR fields are shared for all NCQ 1683 * commands that were completed with the same SDB FIS. 1684 * Therefore, we have to clear the ATA_ERR bit for all QCs 1685 * except the one that caused the NCQ error. 1686 */ 1687 qc->result_tf.status &= ~ATA_ERR; 1688 qc->result_tf.error = 0; 1689 1690 /* 1691 * If we get a NCQ error, that means that a single command was 1692 * aborted. All other failed commands for our link should be 1693 * retried and has no business of going though further scrutiny 1694 * by ata_eh_link_autopsy(). 1695 */ 1696 qc->flags |= ATA_QCFLAG_RETRY; 1697 } 1698 1699 ehc->i.err_mask &= ~AC_ERR_DEV; 1700 } 1701 EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error); 1702 1703 const struct ata_port_operations sata_port_ops = { 1704 .inherits = &ata_base_port_ops, 1705 1706 .qc_defer = ata_std_qc_defer, 1707 .reset.hardreset = sata_std_hardreset, 1708 }; 1709 EXPORT_SYMBOL_GPL(sata_port_ops); 1710