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 <asm/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_link_hardreset - reset link via SATA phy reset 522 * @link: link to reset 523 * @timing: timing parameters { interval, duration, timeout } in msec 524 * @deadline: deadline jiffies for the operation 525 * @online: optional out parameter indicating link onlineness 526 * @check_ready: optional callback to check link readiness 527 * 528 * SATA phy-reset @link using DET bits of SControl register. 529 * After hardreset, link readiness is waited upon using 530 * ata_wait_ready() if @check_ready is specified. LLDs are 531 * allowed to not specify @check_ready and wait itself after this 532 * function returns. Device classification is LLD's 533 * responsibility. 534 * 535 * *@online is set to one iff reset succeeded and @link is online 536 * after reset. 537 * 538 * LOCKING: 539 * Kernel thread context (may sleep) 540 * 541 * RETURNS: 542 * 0 on success, -errno otherwise. 543 */ 544 int sata_link_hardreset(struct ata_link *link, const unsigned int *timing, 545 unsigned long deadline, 546 bool *online, int (*check_ready)(struct ata_link *)) 547 { 548 u32 scontrol; 549 int rc; 550 551 if (online) 552 *online = false; 553 554 if (sata_set_spd_needed(link)) { 555 /* SATA spec says nothing about how to reconfigure 556 * spd. To be on the safe side, turn off phy during 557 * reconfiguration. This works for at least ICH7 AHCI 558 * and Sil3124. 559 */ 560 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol))) 561 goto out; 562 563 scontrol = (scontrol & 0x0f0) | 0x304; 564 565 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol))) 566 goto out; 567 568 sata_set_spd(link); 569 } 570 571 /* issue phy wake/reset */ 572 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol))) 573 goto out; 574 575 scontrol = (scontrol & 0x0f0) | 0x301; 576 577 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol))) 578 goto out; 579 580 /* Couldn't find anything in SATA I/II specs, but AHCI-1.1 581 * 10.4.2 says at least 1 ms. 582 */ 583 ata_msleep(link->ap, 1); 584 585 /* bring link back */ 586 rc = sata_link_resume(link, timing, deadline); 587 if (rc) 588 goto out; 589 /* if link is offline nothing more to do */ 590 if (ata_phys_link_offline(link)) 591 goto out; 592 593 /* Link is online. From this point, -ENODEV too is an error. */ 594 if (online) 595 *online = true; 596 597 if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) { 598 /* If PMP is supported, we have to do follow-up SRST. 599 * Some PMPs don't send D2H Reg FIS after hardreset if 600 * the first port is empty. Wait only for 601 * ATA_TMOUT_PMP_SRST_WAIT. 602 */ 603 if (check_ready) { 604 unsigned long pmp_deadline; 605 606 pmp_deadline = ata_deadline(jiffies, 607 ATA_TMOUT_PMP_SRST_WAIT); 608 if (time_after(pmp_deadline, deadline)) 609 pmp_deadline = deadline; 610 ata_wait_ready(link, pmp_deadline, check_ready); 611 } 612 rc = -EAGAIN; 613 goto out; 614 } 615 616 rc = 0; 617 if (check_ready) 618 rc = ata_wait_ready(link, deadline, check_ready); 619 out: 620 if (rc && rc != -EAGAIN) { 621 /* online is set iff link is online && reset succeeded */ 622 if (online) 623 *online = false; 624 } 625 return rc; 626 } 627 EXPORT_SYMBOL_GPL(sata_link_hardreset); 628 629 /** 630 * ata_qc_complete_multiple - Complete multiple qcs successfully 631 * @ap: port in question 632 * @qc_active: new qc_active mask 633 * 634 * Complete in-flight commands. This functions is meant to be 635 * called from low-level driver's interrupt routine to complete 636 * requests normally. ap->qc_active and @qc_active is compared 637 * and commands are completed accordingly. 638 * 639 * Always use this function when completing multiple NCQ commands 640 * from IRQ handlers instead of calling ata_qc_complete() 641 * multiple times to keep IRQ expect status properly in sync. 642 * 643 * LOCKING: 644 * spin_lock_irqsave(host lock) 645 * 646 * RETURNS: 647 * Number of completed commands on success, -errno otherwise. 648 */ 649 int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active) 650 { 651 u64 done_mask, ap_qc_active = ap->qc_active; 652 int nr_done = 0; 653 654 /* 655 * If the internal tag is set on ap->qc_active, then we care about 656 * bit0 on the passed in qc_active mask. Move that bit up to match 657 * the internal tag. 658 */ 659 if (ap_qc_active & (1ULL << ATA_TAG_INTERNAL)) { 660 qc_active |= (qc_active & 0x01) << ATA_TAG_INTERNAL; 661 qc_active ^= qc_active & 0x01; 662 } 663 664 done_mask = ap_qc_active ^ qc_active; 665 666 if (unlikely(done_mask & qc_active)) { 667 ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n", 668 ap->qc_active, qc_active); 669 return -EINVAL; 670 } 671 672 if (ap->ops->qc_ncq_fill_rtf) 673 ap->ops->qc_ncq_fill_rtf(ap, done_mask); 674 675 while (done_mask) { 676 struct ata_queued_cmd *qc; 677 unsigned int tag = __ffs64(done_mask); 678 679 qc = ata_qc_from_tag(ap, tag); 680 if (qc) { 681 ata_qc_complete(qc); 682 nr_done++; 683 } 684 done_mask &= ~(1ULL << tag); 685 } 686 687 return nr_done; 688 } 689 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple); 690 691 /** 692 * ata_slave_link_init - initialize slave link 693 * @ap: port to initialize slave link for 694 * 695 * Create and initialize slave link for @ap. This enables slave 696 * link handling on the port. 697 * 698 * In libata, a port contains links and a link contains devices. 699 * There is single host link but if a PMP is attached to it, 700 * there can be multiple fan-out links. On SATA, there's usually 701 * a single device connected to a link but PATA and SATA 702 * controllers emulating TF based interface can have two - master 703 * and slave. 704 * 705 * However, there are a few controllers which don't fit into this 706 * abstraction too well - SATA controllers which emulate TF 707 * interface with both master and slave devices but also have 708 * separate SCR register sets for each device. These controllers 709 * need separate links for physical link handling 710 * (e.g. onlineness, link speed) but should be treated like a 711 * traditional M/S controller for everything else (e.g. command 712 * issue, softreset). 713 * 714 * slave_link is libata's way of handling this class of 715 * controllers without impacting core layer too much. For 716 * anything other than physical link handling, the default host 717 * link is used for both master and slave. For physical link 718 * handling, separate @ap->slave_link is used. All dirty details 719 * are implemented inside libata core layer. From LLD's POV, the 720 * only difference is that prereset, hardreset and postreset are 721 * called once more for the slave link, so the reset sequence 722 * looks like the following. 723 * 724 * prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) -> 725 * softreset(M) -> postreset(M) -> postreset(S) 726 * 727 * Note that softreset is called only for the master. Softreset 728 * resets both M/S by definition, so SRST on master should handle 729 * both (the standard method will work just fine). 730 * 731 * LOCKING: 732 * Should be called before host is registered. 733 * 734 * RETURNS: 735 * 0 on success, -errno on failure. 736 */ 737 int ata_slave_link_init(struct ata_port *ap) 738 { 739 struct ata_link *link; 740 741 WARN_ON(ap->slave_link); 742 WARN_ON(ap->flags & ATA_FLAG_PMP); 743 744 link = kzalloc(sizeof(*link), GFP_KERNEL); 745 if (!link) 746 return -ENOMEM; 747 748 ata_link_init(ap, link, 1); 749 ap->slave_link = link; 750 return 0; 751 } 752 EXPORT_SYMBOL_GPL(ata_slave_link_init); 753 754 /** 755 * sata_lpm_ignore_phy_events - test if PHY event should be ignored 756 * @link: Link receiving the event 757 * 758 * Test whether the received PHY event has to be ignored or not. 759 * 760 * LOCKING: 761 * None: 762 * 763 * RETURNS: 764 * True if the event has to be ignored. 765 */ 766 bool sata_lpm_ignore_phy_events(struct ata_link *link) 767 { 768 unsigned long lpm_timeout = link->last_lpm_change + 769 msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY); 770 771 /* if LPM is enabled, PHYRDY doesn't mean anything */ 772 if (link->lpm_policy > ATA_LPM_MAX_POWER) 773 return true; 774 775 /* ignore the first PHY event after the LPM policy changed 776 * as it is might be spurious 777 */ 778 if ((link->flags & ATA_LFLAG_CHANGED) && 779 time_before(jiffies, lpm_timeout)) 780 return true; 781 782 return false; 783 } 784 EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events); 785 786 static const char *ata_lpm_policy_names[] = { 787 [ATA_LPM_UNKNOWN] = "keep_firmware_settings", 788 [ATA_LPM_MAX_POWER] = "max_performance", 789 [ATA_LPM_MED_POWER] = "medium_power", 790 [ATA_LPM_MED_POWER_WITH_DIPM] = "med_power_with_dipm", 791 [ATA_LPM_MIN_POWER_WITH_PARTIAL] = "min_power_with_partial", 792 [ATA_LPM_MIN_POWER] = "min_power", 793 }; 794 795 static ssize_t ata_scsi_lpm_store(struct device *device, 796 struct device_attribute *attr, 797 const char *buf, size_t count) 798 { 799 struct Scsi_Host *shost = class_to_shost(device); 800 struct ata_port *ap = ata_shost_to_port(shost); 801 struct ata_link *link; 802 struct ata_device *dev; 803 enum ata_lpm_policy policy; 804 unsigned long flags; 805 806 /* UNKNOWN is internal state, iterate from MAX_POWER */ 807 for (policy = ATA_LPM_MAX_POWER; 808 policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) { 809 const char *name = ata_lpm_policy_names[policy]; 810 811 if (strncmp(name, buf, strlen(name)) == 0) 812 break; 813 } 814 if (policy == ARRAY_SIZE(ata_lpm_policy_names)) 815 return -EINVAL; 816 817 spin_lock_irqsave(ap->lock, flags); 818 819 ata_for_each_link(link, ap, EDGE) { 820 ata_for_each_dev(dev, &ap->link, ENABLED) { 821 if (dev->horkage & ATA_HORKAGE_NOLPM) { 822 count = -EOPNOTSUPP; 823 goto out_unlock; 824 } 825 } 826 } 827 828 ap->target_lpm_policy = policy; 829 ata_port_schedule_eh(ap); 830 out_unlock: 831 spin_unlock_irqrestore(ap->lock, flags); 832 return count; 833 } 834 835 static ssize_t ata_scsi_lpm_show(struct device *dev, 836 struct device_attribute *attr, char *buf) 837 { 838 struct Scsi_Host *shost = class_to_shost(dev); 839 struct ata_port *ap = ata_shost_to_port(shost); 840 841 if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names)) 842 return -EINVAL; 843 844 return sysfs_emit(buf, "%s\n", 845 ata_lpm_policy_names[ap->target_lpm_policy]); 846 } 847 DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR, 848 ata_scsi_lpm_show, ata_scsi_lpm_store); 849 EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy); 850 851 /** 852 * ata_ncq_prio_supported - Check if device supports NCQ Priority 853 * @ap: ATA port of the target device 854 * @sdev: SCSI device 855 * @supported: Address of a boolean to store the result 856 * 857 * Helper to check if device supports NCQ Priority feature. 858 * 859 * Context: Any context. Takes and releases @ap->lock. 860 * 861 * Return: 862 * * %0 - OK. Status is stored into @supported 863 * * %-ENODEV - Failed to find the ATA device 864 */ 865 int ata_ncq_prio_supported(struct ata_port *ap, struct scsi_device *sdev, 866 bool *supported) 867 { 868 struct ata_device *dev; 869 unsigned long flags; 870 int rc = 0; 871 872 spin_lock_irqsave(ap->lock, flags); 873 dev = ata_scsi_find_dev(ap, sdev); 874 if (!dev) 875 rc = -ENODEV; 876 else 877 *supported = dev->flags & ATA_DFLAG_NCQ_PRIO; 878 spin_unlock_irqrestore(ap->lock, flags); 879 880 return rc; 881 } 882 EXPORT_SYMBOL_GPL(ata_ncq_prio_supported); 883 884 static ssize_t ata_ncq_prio_supported_show(struct device *device, 885 struct device_attribute *attr, 886 char *buf) 887 { 888 struct scsi_device *sdev = to_scsi_device(device); 889 struct ata_port *ap = ata_shost_to_port(sdev->host); 890 bool supported; 891 int rc; 892 893 rc = ata_ncq_prio_supported(ap, sdev, &supported); 894 if (rc) 895 return rc; 896 897 return sysfs_emit(buf, "%d\n", supported); 898 } 899 900 DEVICE_ATTR(ncq_prio_supported, S_IRUGO, ata_ncq_prio_supported_show, NULL); 901 EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_supported); 902 903 /** 904 * ata_ncq_prio_enabled - Check if NCQ Priority is enabled 905 * @ap: ATA port of the target device 906 * @sdev: SCSI device 907 * @enabled: Address of a boolean to store the result 908 * 909 * Helper to check if NCQ Priority feature is enabled. 910 * 911 * Context: Any context. Takes and releases @ap->lock. 912 * 913 * Return: 914 * * %0 - OK. Status is stored into @enabled 915 * * %-ENODEV - Failed to find the ATA device 916 */ 917 int ata_ncq_prio_enabled(struct ata_port *ap, struct scsi_device *sdev, 918 bool *enabled) 919 { 920 struct ata_device *dev; 921 unsigned long flags; 922 int rc = 0; 923 924 spin_lock_irqsave(ap->lock, flags); 925 dev = ata_scsi_find_dev(ap, sdev); 926 if (!dev) 927 rc = -ENODEV; 928 else 929 *enabled = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLED; 930 spin_unlock_irqrestore(ap->lock, flags); 931 932 return rc; 933 } 934 EXPORT_SYMBOL_GPL(ata_ncq_prio_enabled); 935 936 static ssize_t ata_ncq_prio_enable_show(struct device *device, 937 struct device_attribute *attr, 938 char *buf) 939 { 940 struct scsi_device *sdev = to_scsi_device(device); 941 struct ata_port *ap = ata_shost_to_port(sdev->host); 942 bool enabled; 943 int rc; 944 945 rc = ata_ncq_prio_enabled(ap, sdev, &enabled); 946 if (rc) 947 return rc; 948 949 return sysfs_emit(buf, "%d\n", enabled); 950 } 951 952 /** 953 * ata_ncq_prio_enable - Enable/disable NCQ Priority 954 * @ap: ATA port of the target device 955 * @sdev: SCSI device 956 * @enable: true - enable NCQ Priority, false - disable NCQ Priority 957 * 958 * Helper to enable/disable NCQ Priority feature. 959 * 960 * Context: Any context. Takes and releases @ap->lock. 961 * 962 * Return: 963 * * %0 - OK. Status is stored into @enabled 964 * * %-ENODEV - Failed to find the ATA device 965 * * %-EINVAL - NCQ Priority is not supported or CDL is enabled 966 */ 967 int ata_ncq_prio_enable(struct ata_port *ap, struct scsi_device *sdev, 968 bool enable) 969 { 970 struct ata_device *dev; 971 unsigned long flags; 972 int rc = 0; 973 974 spin_lock_irqsave(ap->lock, flags); 975 976 dev = ata_scsi_find_dev(ap, sdev); 977 if (!dev) { 978 rc = -ENODEV; 979 goto unlock; 980 } 981 982 if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) { 983 rc = -EINVAL; 984 goto unlock; 985 } 986 987 if (enable) { 988 if (dev->flags & ATA_DFLAG_CDL_ENABLED) { 989 ata_dev_err(dev, 990 "CDL must be disabled to enable NCQ priority\n"); 991 rc = -EINVAL; 992 goto unlock; 993 } 994 dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLED; 995 } else { 996 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLED; 997 } 998 999 unlock: 1000 spin_unlock_irqrestore(ap->lock, flags); 1001 1002 return rc; 1003 } 1004 EXPORT_SYMBOL_GPL(ata_ncq_prio_enable); 1005 1006 static ssize_t ata_ncq_prio_enable_store(struct device *device, 1007 struct device_attribute *attr, 1008 const char *buf, size_t len) 1009 { 1010 struct scsi_device *sdev = to_scsi_device(device); 1011 struct ata_port *ap = ata_shost_to_port(sdev->host); 1012 bool enable; 1013 int rc; 1014 1015 rc = kstrtobool(buf, &enable); 1016 if (rc) 1017 return rc; 1018 1019 rc = ata_ncq_prio_enable(ap, sdev, enable); 1020 if (rc) 1021 return rc; 1022 1023 return len; 1024 } 1025 1026 DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR, 1027 ata_ncq_prio_enable_show, ata_ncq_prio_enable_store); 1028 EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable); 1029 1030 static struct attribute *ata_ncq_sdev_attrs[] = { 1031 &dev_attr_unload_heads.attr, 1032 &dev_attr_ncq_prio_enable.attr, 1033 &dev_attr_ncq_prio_supported.attr, 1034 NULL 1035 }; 1036 1037 static const struct attribute_group ata_ncq_sdev_attr_group = { 1038 .attrs = ata_ncq_sdev_attrs 1039 }; 1040 1041 const struct attribute_group *ata_ncq_sdev_groups[] = { 1042 &ata_ncq_sdev_attr_group, 1043 NULL 1044 }; 1045 EXPORT_SYMBOL_GPL(ata_ncq_sdev_groups); 1046 1047 static ssize_t 1048 ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr, 1049 const char *buf, size_t count) 1050 { 1051 struct Scsi_Host *shost = class_to_shost(dev); 1052 struct ata_port *ap = ata_shost_to_port(shost); 1053 if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM)) 1054 return ap->ops->em_store(ap, buf, count); 1055 return -EINVAL; 1056 } 1057 1058 static ssize_t 1059 ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr, 1060 char *buf) 1061 { 1062 struct Scsi_Host *shost = class_to_shost(dev); 1063 struct ata_port *ap = ata_shost_to_port(shost); 1064 1065 if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM)) 1066 return ap->ops->em_show(ap, buf); 1067 return -EINVAL; 1068 } 1069 DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR, 1070 ata_scsi_em_message_show, ata_scsi_em_message_store); 1071 EXPORT_SYMBOL_GPL(dev_attr_em_message); 1072 1073 static ssize_t 1074 ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr, 1075 char *buf) 1076 { 1077 struct Scsi_Host *shost = class_to_shost(dev); 1078 struct ata_port *ap = ata_shost_to_port(shost); 1079 1080 return sysfs_emit(buf, "%d\n", ap->em_message_type); 1081 } 1082 DEVICE_ATTR(em_message_type, S_IRUGO, 1083 ata_scsi_em_message_type_show, NULL); 1084 EXPORT_SYMBOL_GPL(dev_attr_em_message_type); 1085 1086 static ssize_t 1087 ata_scsi_activity_show(struct device *dev, struct device_attribute *attr, 1088 char *buf) 1089 { 1090 struct scsi_device *sdev = to_scsi_device(dev); 1091 struct ata_port *ap = ata_shost_to_port(sdev->host); 1092 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev); 1093 1094 if (atadev && ap->ops->sw_activity_show && 1095 (ap->flags & ATA_FLAG_SW_ACTIVITY)) 1096 return ap->ops->sw_activity_show(atadev, buf); 1097 return -EINVAL; 1098 } 1099 1100 static ssize_t 1101 ata_scsi_activity_store(struct device *dev, struct device_attribute *attr, 1102 const char *buf, size_t count) 1103 { 1104 struct scsi_device *sdev = to_scsi_device(dev); 1105 struct ata_port *ap = ata_shost_to_port(sdev->host); 1106 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev); 1107 enum sw_activity val; 1108 int rc; 1109 1110 if (atadev && ap->ops->sw_activity_store && 1111 (ap->flags & ATA_FLAG_SW_ACTIVITY)) { 1112 val = simple_strtoul(buf, NULL, 0); 1113 switch (val) { 1114 case OFF: case BLINK_ON: case BLINK_OFF: 1115 rc = ap->ops->sw_activity_store(atadev, val); 1116 if (!rc) 1117 return count; 1118 else 1119 return rc; 1120 } 1121 } 1122 return -EINVAL; 1123 } 1124 DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show, 1125 ata_scsi_activity_store); 1126 EXPORT_SYMBOL_GPL(dev_attr_sw_activity); 1127 1128 /** 1129 * ata_change_queue_depth - Set a device maximum queue depth 1130 * @ap: ATA port of the target device 1131 * @sdev: SCSI device to configure queue depth for 1132 * @queue_depth: new queue depth 1133 * 1134 * Helper to set a device maximum queue depth, usable with both libsas 1135 * and libata. 1136 * 1137 */ 1138 int ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev, 1139 int queue_depth) 1140 { 1141 struct ata_device *dev; 1142 unsigned long flags; 1143 int max_queue_depth; 1144 1145 spin_lock_irqsave(ap->lock, flags); 1146 1147 dev = ata_scsi_find_dev(ap, sdev); 1148 if (!dev || queue_depth < 1 || queue_depth == sdev->queue_depth) { 1149 spin_unlock_irqrestore(ap->lock, flags); 1150 return sdev->queue_depth; 1151 } 1152 1153 /* 1154 * Make sure that the queue depth requested does not exceed the device 1155 * capabilities. 1156 */ 1157 max_queue_depth = min(ATA_MAX_QUEUE, sdev->host->can_queue); 1158 max_queue_depth = min(max_queue_depth, ata_id_queue_depth(dev->id)); 1159 if (queue_depth > max_queue_depth) { 1160 spin_unlock_irqrestore(ap->lock, flags); 1161 return -EINVAL; 1162 } 1163 1164 /* 1165 * If NCQ is not supported by the device or if the target queue depth 1166 * is 1 (to disable drive side command queueing), turn off NCQ. 1167 */ 1168 if (queue_depth == 1 || !ata_ncq_supported(dev)) { 1169 dev->flags |= ATA_DFLAG_NCQ_OFF; 1170 queue_depth = 1; 1171 } else { 1172 dev->flags &= ~ATA_DFLAG_NCQ_OFF; 1173 } 1174 1175 spin_unlock_irqrestore(ap->lock, flags); 1176 1177 if (queue_depth == sdev->queue_depth) 1178 return sdev->queue_depth; 1179 1180 return scsi_change_queue_depth(sdev, queue_depth); 1181 } 1182 EXPORT_SYMBOL_GPL(ata_change_queue_depth); 1183 1184 /** 1185 * ata_scsi_change_queue_depth - SCSI callback for queue depth config 1186 * @sdev: SCSI device to configure queue depth for 1187 * @queue_depth: new queue depth 1188 * 1189 * This is libata standard hostt->change_queue_depth callback. 1190 * SCSI will call into this callback when user tries to set queue 1191 * depth via sysfs. 1192 * 1193 * LOCKING: 1194 * SCSI layer (we don't care) 1195 * 1196 * RETURNS: 1197 * Newly configured queue depth. 1198 */ 1199 int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth) 1200 { 1201 struct ata_port *ap = ata_shost_to_port(sdev->host); 1202 1203 return ata_change_queue_depth(ap, sdev, queue_depth); 1204 } 1205 EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth); 1206 1207 /** 1208 * ata_sas_device_configure - Default device_configure routine for libata 1209 * devices 1210 * @sdev: SCSI device to configure 1211 * @lim: queue limits 1212 * @ap: ATA port to which SCSI device is attached 1213 * 1214 * RETURNS: 1215 * Zero. 1216 */ 1217 1218 int ata_sas_device_configure(struct scsi_device *sdev, struct queue_limits *lim, 1219 struct ata_port *ap) 1220 { 1221 ata_scsi_sdev_config(sdev); 1222 1223 return ata_scsi_dev_config(sdev, lim, ap->link.device); 1224 } 1225 EXPORT_SYMBOL_GPL(ata_sas_device_configure); 1226 1227 /** 1228 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device 1229 * @cmd: SCSI command to be sent 1230 * @ap: ATA port to which the command is being sent 1231 * 1232 * RETURNS: 1233 * Return value from __ata_scsi_queuecmd() if @cmd can be queued, 1234 * 0 otherwise. 1235 */ 1236 1237 int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap) 1238 { 1239 int rc = 0; 1240 1241 if (likely(ata_dev_enabled(ap->link.device))) 1242 rc = __ata_scsi_queuecmd(cmd, ap->link.device); 1243 else { 1244 cmd->result = (DID_BAD_TARGET << 16); 1245 scsi_done(cmd); 1246 } 1247 return rc; 1248 } 1249 EXPORT_SYMBOL_GPL(ata_sas_queuecmd); 1250 1251 /** 1252 * sata_async_notification - SATA async notification handler 1253 * @ap: ATA port where async notification is received 1254 * 1255 * Handler to be called when async notification via SDB FIS is 1256 * received. This function schedules EH if necessary. 1257 * 1258 * LOCKING: 1259 * spin_lock_irqsave(host lock) 1260 * 1261 * RETURNS: 1262 * 1 if EH is scheduled, 0 otherwise. 1263 */ 1264 int sata_async_notification(struct ata_port *ap) 1265 { 1266 u32 sntf; 1267 int rc; 1268 1269 if (!(ap->flags & ATA_FLAG_AN)) 1270 return 0; 1271 1272 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf); 1273 if (rc == 0) 1274 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf); 1275 1276 if (!sata_pmp_attached(ap) || rc) { 1277 /* PMP is not attached or SNTF is not available */ 1278 if (!sata_pmp_attached(ap)) { 1279 /* PMP is not attached. Check whether ATAPI 1280 * AN is configured. If so, notify media 1281 * change. 1282 */ 1283 struct ata_device *dev = ap->link.device; 1284 1285 if ((dev->class == ATA_DEV_ATAPI) && 1286 (dev->flags & ATA_DFLAG_AN)) 1287 ata_scsi_media_change_notify(dev); 1288 return 0; 1289 } else { 1290 /* PMP is attached but SNTF is not available. 1291 * ATAPI async media change notification is 1292 * not used. The PMP must be reporting PHY 1293 * status change, schedule EH. 1294 */ 1295 ata_port_schedule_eh(ap); 1296 return 1; 1297 } 1298 } else { 1299 /* PMP is attached and SNTF is available */ 1300 struct ata_link *link; 1301 1302 /* check and notify ATAPI AN */ 1303 ata_for_each_link(link, ap, EDGE) { 1304 if (!(sntf & (1 << link->pmp))) 1305 continue; 1306 1307 if ((link->device->class == ATA_DEV_ATAPI) && 1308 (link->device->flags & ATA_DFLAG_AN)) 1309 ata_scsi_media_change_notify(link->device); 1310 } 1311 1312 /* If PMP is reporting that PHY status of some 1313 * downstream ports has changed, schedule EH. 1314 */ 1315 if (sntf & (1 << SATA_PMP_CTRL_PORT)) { 1316 ata_port_schedule_eh(ap); 1317 return 1; 1318 } 1319 1320 return 0; 1321 } 1322 } 1323 EXPORT_SYMBOL_GPL(sata_async_notification); 1324 1325 /** 1326 * ata_eh_read_log_10h - Read log page 10h for NCQ error details 1327 * @dev: Device to read log page 10h from 1328 * @tag: Resulting tag of the failed command 1329 * @tf: Resulting taskfile registers of the failed command 1330 * 1331 * Read log page 10h to obtain NCQ error details and clear error 1332 * condition. 1333 * 1334 * LOCKING: 1335 * Kernel thread context (may sleep). 1336 * 1337 * RETURNS: 1338 * 0 on success, -errno otherwise. 1339 */ 1340 static int ata_eh_read_log_10h(struct ata_device *dev, 1341 int *tag, struct ata_taskfile *tf) 1342 { 1343 u8 *buf = dev->link->ap->sector_buf; 1344 unsigned int err_mask; 1345 u8 csum; 1346 int i; 1347 1348 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, 0, buf, 1); 1349 if (err_mask) 1350 return -EIO; 1351 1352 csum = 0; 1353 for (i = 0; i < ATA_SECT_SIZE; i++) 1354 csum += buf[i]; 1355 if (csum) 1356 ata_dev_warn(dev, "invalid checksum 0x%x on log page 10h\n", 1357 csum); 1358 1359 if (buf[0] & 0x80) 1360 return -ENOENT; 1361 1362 *tag = buf[0] & 0x1f; 1363 1364 tf->status = buf[2]; 1365 tf->error = buf[3]; 1366 tf->lbal = buf[4]; 1367 tf->lbam = buf[5]; 1368 tf->lbah = buf[6]; 1369 tf->device = buf[7]; 1370 tf->hob_lbal = buf[8]; 1371 tf->hob_lbam = buf[9]; 1372 tf->hob_lbah = buf[10]; 1373 tf->nsect = buf[12]; 1374 tf->hob_nsect = buf[13]; 1375 if (ata_id_has_ncq_autosense(dev->id) && (tf->status & ATA_SENSE)) 1376 tf->auxiliary = buf[14] << 16 | buf[15] << 8 | buf[16]; 1377 1378 return 0; 1379 } 1380 1381 /** 1382 * ata_eh_read_sense_success_ncq_log - Read the sense data for successful 1383 * NCQ commands log 1384 * @link: ATA link to get sense data for 1385 * 1386 * Read the sense data for successful NCQ commands log page to obtain 1387 * sense data for all NCQ commands that completed successfully with 1388 * the sense data available bit set. 1389 * 1390 * LOCKING: 1391 * Kernel thread context (may sleep). 1392 * 1393 * RETURNS: 1394 * 0 on success, -errno otherwise. 1395 */ 1396 int ata_eh_read_sense_success_ncq_log(struct ata_link *link) 1397 { 1398 struct ata_device *dev = link->device; 1399 struct ata_port *ap = dev->link->ap; 1400 u8 *buf = ap->ncq_sense_buf; 1401 struct ata_queued_cmd *qc; 1402 unsigned int err_mask, tag; 1403 u8 *sense, sk = 0, asc = 0, ascq = 0; 1404 u64 sense_valid, val; 1405 int ret = 0; 1406 1407 err_mask = ata_read_log_page(dev, ATA_LOG_SENSE_NCQ, 0, buf, 2); 1408 if (err_mask) { 1409 ata_dev_err(dev, 1410 "Failed to read Sense Data for Successful NCQ Commands log\n"); 1411 return -EIO; 1412 } 1413 1414 /* Check the log header */ 1415 val = get_unaligned_le64(&buf[0]); 1416 if ((val & 0xffff) != 1 || ((val >> 16) & 0xff) != 0x0f) { 1417 ata_dev_err(dev, 1418 "Invalid Sense Data for Successful NCQ Commands log\n"); 1419 return -EIO; 1420 } 1421 1422 sense_valid = (u64)buf[8] | ((u64)buf[9] << 8) | 1423 ((u64)buf[10] << 16) | ((u64)buf[11] << 24); 1424 1425 ata_qc_for_each_raw(ap, qc, tag) { 1426 if (!(qc->flags & ATA_QCFLAG_EH) || 1427 !(qc->flags & ATA_QCFLAG_EH_SUCCESS_CMD) || 1428 qc->err_mask || 1429 ata_dev_phys_link(qc->dev) != link) 1430 continue; 1431 1432 /* 1433 * If the command does not have any sense data, clear ATA_SENSE. 1434 * Keep ATA_QCFLAG_EH_SUCCESS_CMD so that command is finished. 1435 */ 1436 if (!(sense_valid & (1ULL << tag))) { 1437 qc->result_tf.status &= ~ATA_SENSE; 1438 continue; 1439 } 1440 1441 sense = &buf[32 + 24 * tag]; 1442 sk = sense[0]; 1443 asc = sense[1]; 1444 ascq = sense[2]; 1445 1446 if (!ata_scsi_sense_is_valid(sk, asc, ascq)) { 1447 ret = -EIO; 1448 continue; 1449 } 1450 1451 /* Set sense without also setting scsicmd->result */ 1452 scsi_build_sense_buffer(dev->flags & ATA_DFLAG_D_SENSE, 1453 qc->scsicmd->sense_buffer, sk, 1454 asc, ascq); 1455 qc->flags |= ATA_QCFLAG_SENSE_VALID; 1456 1457 /* 1458 * If we have sense data, call scsi_check_sense() in order to 1459 * set the correct SCSI ML byte (if any). No point in checking 1460 * the return value, since the command has already completed 1461 * successfully. 1462 */ 1463 scsi_check_sense(qc->scsicmd); 1464 } 1465 1466 return ret; 1467 } 1468 EXPORT_SYMBOL_GPL(ata_eh_read_sense_success_ncq_log); 1469 1470 /** 1471 * ata_eh_analyze_ncq_error - analyze NCQ error 1472 * @link: ATA link to analyze NCQ error for 1473 * 1474 * Read log page 10h, determine the offending qc and acquire 1475 * error status TF. For NCQ device errors, all LLDDs have to do 1476 * is setting AC_ERR_DEV in ehi->err_mask. This function takes 1477 * care of the rest. 1478 * 1479 * LOCKING: 1480 * Kernel thread context (may sleep). 1481 */ 1482 void ata_eh_analyze_ncq_error(struct ata_link *link) 1483 { 1484 struct ata_port *ap = link->ap; 1485 struct ata_eh_context *ehc = &link->eh_context; 1486 struct ata_device *dev = link->device; 1487 struct ata_queued_cmd *qc; 1488 struct ata_taskfile tf; 1489 int tag, rc; 1490 1491 /* if frozen, we can't do much */ 1492 if (ata_port_is_frozen(ap)) 1493 return; 1494 1495 /* is it NCQ device error? */ 1496 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV)) 1497 return; 1498 1499 /* has LLDD analyzed already? */ 1500 ata_qc_for_each_raw(ap, qc, tag) { 1501 if (!(qc->flags & ATA_QCFLAG_EH)) 1502 continue; 1503 1504 if (qc->err_mask) 1505 return; 1506 } 1507 1508 /* okay, this error is ours */ 1509 memset(&tf, 0, sizeof(tf)); 1510 rc = ata_eh_read_log_10h(dev, &tag, &tf); 1511 if (rc) { 1512 ata_link_err(link, "failed to read log page 10h (errno=%d)\n", 1513 rc); 1514 return; 1515 } 1516 1517 if (!(link->sactive & (1 << tag))) { 1518 ata_link_err(link, "log page 10h reported inactive tag %d\n", 1519 tag); 1520 return; 1521 } 1522 1523 /* we've got the perpetrator, condemn it */ 1524 qc = __ata_qc_from_tag(ap, tag); 1525 memcpy(&qc->result_tf, &tf, sizeof(tf)); 1526 qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48; 1527 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ; 1528 1529 /* 1530 * If the device supports NCQ autosense, ata_eh_read_log_10h() will have 1531 * stored the sense data in qc->result_tf.auxiliary. 1532 */ 1533 if (qc->result_tf.auxiliary) { 1534 char sense_key, asc, ascq; 1535 1536 sense_key = (qc->result_tf.auxiliary >> 16) & 0xff; 1537 asc = (qc->result_tf.auxiliary >> 8) & 0xff; 1538 ascq = qc->result_tf.auxiliary & 0xff; 1539 if (ata_scsi_sense_is_valid(sense_key, asc, ascq)) { 1540 ata_scsi_set_sense(dev, qc->scsicmd, sense_key, asc, 1541 ascq); 1542 ata_scsi_set_sense_information(dev, qc->scsicmd, 1543 &qc->result_tf); 1544 qc->flags |= ATA_QCFLAG_SENSE_VALID; 1545 } 1546 } 1547 1548 ata_qc_for_each_raw(ap, qc, tag) { 1549 if (!(qc->flags & ATA_QCFLAG_EH) || 1550 qc->flags & ATA_QCFLAG_EH_SUCCESS_CMD || 1551 ata_dev_phys_link(qc->dev) != link) 1552 continue; 1553 1554 /* Skip the single QC which caused the NCQ error. */ 1555 if (qc->err_mask) 1556 continue; 1557 1558 /* 1559 * For SATA, the STATUS and ERROR fields are shared for all NCQ 1560 * commands that were completed with the same SDB FIS. 1561 * Therefore, we have to clear the ATA_ERR bit for all QCs 1562 * except the one that caused the NCQ error. 1563 */ 1564 qc->result_tf.status &= ~ATA_ERR; 1565 qc->result_tf.error = 0; 1566 1567 /* 1568 * If we get a NCQ error, that means that a single command was 1569 * aborted. All other failed commands for our link should be 1570 * retried and has no business of going though further scrutiny 1571 * by ata_eh_link_autopsy(). 1572 */ 1573 qc->flags |= ATA_QCFLAG_RETRY; 1574 } 1575 1576 ehc->i.err_mask &= ~AC_ERR_DEV; 1577 } 1578 EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error); 1579