0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <scsi/scsi_cmnd.h>
0013 #include <scsi/scsi_device.h>
0014 #include <linux/libata.h>
0015
0016 #include "libata.h"
0017 #include "libata-transport.h"
0018
0019
0020 const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
0021 EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
0022 const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
0023 EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
0024 const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
0025 EXPORT_SYMBOL_GPL(sata_deb_timing_long);
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039 int sata_scr_valid(struct ata_link *link)
0040 {
0041 struct ata_port *ap = link->ap;
0042
0043 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
0044 }
0045 EXPORT_SYMBOL_GPL(sata_scr_valid);
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063 int sata_scr_read(struct ata_link *link, int reg, u32 *val)
0064 {
0065 if (ata_is_host_link(link)) {
0066 if (sata_scr_valid(link))
0067 return link->ap->ops->scr_read(link, reg, val);
0068 return -EOPNOTSUPP;
0069 }
0070
0071 return sata_pmp_scr_read(link, reg, val);
0072 }
0073 EXPORT_SYMBOL_GPL(sata_scr_read);
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091 int sata_scr_write(struct ata_link *link, int reg, u32 val)
0092 {
0093 if (ata_is_host_link(link)) {
0094 if (sata_scr_valid(link))
0095 return link->ap->ops->scr_write(link, reg, val);
0096 return -EOPNOTSUPP;
0097 }
0098
0099 return sata_pmp_scr_write(link, reg, val);
0100 }
0101 EXPORT_SYMBOL_GPL(sata_scr_write);
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118 int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
0119 {
0120 if (ata_is_host_link(link)) {
0121 int rc;
0122
0123 if (sata_scr_valid(link)) {
0124 rc = link->ap->ops->scr_write(link, reg, val);
0125 if (rc == 0)
0126 rc = link->ap->ops->scr_read(link, reg, &val);
0127 return rc;
0128 }
0129 return -EOPNOTSUPP;
0130 }
0131
0132 return sata_pmp_scr_write(link, reg, val);
0133 }
0134 EXPORT_SYMBOL_GPL(sata_scr_write_flush);
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149 void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
0150 {
0151 fis[0] = 0x27;
0152 fis[1] = pmp & 0xf;
0153 if (is_cmd)
0154 fis[1] |= (1 << 7);
0155
0156 fis[2] = tf->command;
0157 fis[3] = tf->feature;
0158
0159 fis[4] = tf->lbal;
0160 fis[5] = tf->lbam;
0161 fis[6] = tf->lbah;
0162 fis[7] = tf->device;
0163
0164 fis[8] = tf->hob_lbal;
0165 fis[9] = tf->hob_lbam;
0166 fis[10] = tf->hob_lbah;
0167 fis[11] = tf->hob_feature;
0168
0169 fis[12] = tf->nsect;
0170 fis[13] = tf->hob_nsect;
0171 fis[14] = 0;
0172 fis[15] = tf->ctl;
0173
0174 fis[16] = tf->auxiliary & 0xff;
0175 fis[17] = (tf->auxiliary >> 8) & 0xff;
0176 fis[18] = (tf->auxiliary >> 16) & 0xff;
0177 fis[19] = (tf->auxiliary >> 24) & 0xff;
0178 }
0179 EXPORT_SYMBOL_GPL(ata_tf_to_fis);
0180
0181
0182
0183
0184
0185
0186
0187
0188
0189
0190
0191
0192 void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
0193 {
0194 tf->status = fis[2];
0195 tf->error = fis[3];
0196
0197 tf->lbal = fis[4];
0198 tf->lbam = fis[5];
0199 tf->lbah = fis[6];
0200 tf->device = fis[7];
0201
0202 tf->hob_lbal = fis[8];
0203 tf->hob_lbam = fis[9];
0204 tf->hob_lbah = fis[10];
0205
0206 tf->nsect = fis[12];
0207 tf->hob_nsect = fis[13];
0208 }
0209 EXPORT_SYMBOL_GPL(ata_tf_from_fis);
0210
0211
0212
0213
0214
0215
0216
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233 int sata_link_debounce(struct ata_link *link, const unsigned long *params,
0234 unsigned long deadline)
0235 {
0236 unsigned long interval = params[0];
0237 unsigned long duration = params[1];
0238 unsigned long last_jiffies, t;
0239 u32 last, cur;
0240 int rc;
0241
0242 t = ata_deadline(jiffies, params[2]);
0243 if (time_before(t, deadline))
0244 deadline = t;
0245
0246 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
0247 return rc;
0248 cur &= 0xf;
0249
0250 last = cur;
0251 last_jiffies = jiffies;
0252
0253 while (1) {
0254 ata_msleep(link->ap, interval);
0255 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
0256 return rc;
0257 cur &= 0xf;
0258
0259
0260 if (cur == last) {
0261 if (cur == 1 && time_before(jiffies, deadline))
0262 continue;
0263 if (time_after(jiffies,
0264 ata_deadline(last_jiffies, duration)))
0265 return 0;
0266 continue;
0267 }
0268
0269
0270 last = cur;
0271 last_jiffies = jiffies;
0272
0273
0274
0275
0276 if (time_after(jiffies, deadline))
0277 return -EPIPE;
0278 }
0279 }
0280 EXPORT_SYMBOL_GPL(sata_link_debounce);
0281
0282
0283
0284
0285
0286
0287
0288
0289
0290
0291
0292
0293
0294
0295
0296 int sata_link_resume(struct ata_link *link, const unsigned long *params,
0297 unsigned long deadline)
0298 {
0299 int tries = ATA_LINK_RESUME_TRIES;
0300 u32 scontrol, serror;
0301 int rc;
0302
0303 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
0304 return rc;
0305
0306
0307
0308
0309
0310
0311 do {
0312 scontrol = (scontrol & 0x0f0) | 0x300;
0313 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
0314 return rc;
0315
0316
0317
0318
0319
0320 if (!(link->flags & ATA_LFLAG_NO_DEBOUNCE_DELAY))
0321 ata_msleep(link->ap, 200);
0322
0323
0324 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
0325 return rc;
0326 } while ((scontrol & 0xf0f) != 0x300 && --tries);
0327
0328 if ((scontrol & 0xf0f) != 0x300) {
0329 ata_link_warn(link, "failed to resume link (SControl %X)\n",
0330 scontrol);
0331 return 0;
0332 }
0333
0334 if (tries < ATA_LINK_RESUME_TRIES)
0335 ata_link_warn(link, "link resume succeeded after %d retries\n",
0336 ATA_LINK_RESUME_TRIES - tries);
0337
0338 if ((rc = sata_link_debounce(link, params, deadline)))
0339 return rc;
0340
0341
0342 if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
0343 rc = sata_scr_write(link, SCR_ERROR, serror);
0344
0345 return rc != -EINVAL ? rc : 0;
0346 }
0347 EXPORT_SYMBOL_GPL(sata_link_resume);
0348
0349
0350
0351
0352
0353
0354
0355
0356
0357
0358
0359
0360
0361
0362
0363
0364
0365
0366
0367 int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
0368 bool spm_wakeup)
0369 {
0370 struct ata_eh_context *ehc = &link->eh_context;
0371 bool woken_up = false;
0372 u32 scontrol;
0373 int rc;
0374
0375 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
0376 if (rc)
0377 return rc;
0378
0379 switch (policy) {
0380 case ATA_LPM_MAX_POWER:
0381
0382 scontrol |= (0x7 << 8);
0383
0384 if (spm_wakeup) {
0385 scontrol |= (0x4 << 12);
0386 woken_up = true;
0387 }
0388 break;
0389 case ATA_LPM_MED_POWER:
0390
0391 scontrol &= ~(0x1 << 8);
0392 scontrol |= (0x6 << 8);
0393 break;
0394 case ATA_LPM_MED_POWER_WITH_DIPM:
0395 case ATA_LPM_MIN_POWER_WITH_PARTIAL:
0396 case ATA_LPM_MIN_POWER:
0397 if (ata_link_nr_enabled(link) > 0)
0398
0399 scontrol &= ~(0x7 << 8);
0400 else {
0401
0402 scontrol &= ~0xf;
0403 scontrol |= (0x1 << 2);
0404 }
0405 break;
0406 default:
0407 WARN_ON(1);
0408 }
0409
0410 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
0411 if (rc)
0412 return rc;
0413
0414
0415 if (woken_up)
0416 msleep(10);
0417
0418
0419 ehc->i.serror &= ~SERR_PHYRDY_CHG;
0420 return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
0421 }
0422 EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
0423
0424 static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
0425 {
0426 struct ata_link *host_link = &link->ap->link;
0427 u32 limit, target, spd;
0428
0429 limit = link->sata_spd_limit;
0430
0431
0432
0433
0434
0435 if (!ata_is_host_link(link) && host_link->sata_spd)
0436 limit &= (1 << host_link->sata_spd) - 1;
0437
0438 if (limit == UINT_MAX)
0439 target = 0;
0440 else
0441 target = fls(limit);
0442
0443 spd = (*scontrol >> 4) & 0xf;
0444 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
0445
0446 return spd != target;
0447 }
0448
0449
0450
0451
0452
0453
0454
0455
0456
0457
0458
0459
0460
0461
0462
0463
0464 static int sata_set_spd_needed(struct ata_link *link)
0465 {
0466 u32 scontrol;
0467
0468 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
0469 return 1;
0470
0471 return __sata_set_spd_needed(link, &scontrol);
0472 }
0473
0474
0475
0476
0477
0478
0479
0480
0481
0482
0483
0484
0485
0486
0487 int sata_set_spd(struct ata_link *link)
0488 {
0489 u32 scontrol;
0490 int rc;
0491
0492 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
0493 return rc;
0494
0495 if (!__sata_set_spd_needed(link, &scontrol))
0496 return 0;
0497
0498 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
0499 return rc;
0500
0501 return 1;
0502 }
0503 EXPORT_SYMBOL_GPL(sata_set_spd);
0504
0505
0506
0507
0508
0509
0510
0511
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523
0524
0525
0526
0527
0528
0529 int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
0530 unsigned long deadline,
0531 bool *online, int (*check_ready)(struct ata_link *))
0532 {
0533 u32 scontrol;
0534 int rc;
0535
0536 if (online)
0537 *online = false;
0538
0539 if (sata_set_spd_needed(link)) {
0540
0541
0542
0543
0544
0545 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
0546 goto out;
0547
0548 scontrol = (scontrol & 0x0f0) | 0x304;
0549
0550 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
0551 goto out;
0552
0553 sata_set_spd(link);
0554 }
0555
0556
0557 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
0558 goto out;
0559
0560 scontrol = (scontrol & 0x0f0) | 0x301;
0561
0562 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
0563 goto out;
0564
0565
0566
0567
0568 ata_msleep(link->ap, 1);
0569
0570
0571 rc = sata_link_resume(link, timing, deadline);
0572 if (rc)
0573 goto out;
0574
0575 if (ata_phys_link_offline(link))
0576 goto out;
0577
0578
0579 if (online)
0580 *online = true;
0581
0582 if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
0583
0584
0585
0586
0587
0588 if (check_ready) {
0589 unsigned long pmp_deadline;
0590
0591 pmp_deadline = ata_deadline(jiffies,
0592 ATA_TMOUT_PMP_SRST_WAIT);
0593 if (time_after(pmp_deadline, deadline))
0594 pmp_deadline = deadline;
0595 ata_wait_ready(link, pmp_deadline, check_ready);
0596 }
0597 rc = -EAGAIN;
0598 goto out;
0599 }
0600
0601 rc = 0;
0602 if (check_ready)
0603 rc = ata_wait_ready(link, deadline, check_ready);
0604 out:
0605 if (rc && rc != -EAGAIN) {
0606
0607 if (online)
0608 *online = false;
0609 ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
0610 }
0611 return rc;
0612 }
0613 EXPORT_SYMBOL_GPL(sata_link_hardreset);
0614
0615
0616
0617
0618
0619
0620
0621
0622
0623
0624
0625
0626
0627
0628
0629
0630
0631
0632
0633
0634
0635 int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active)
0636 {
0637 u64 done_mask, ap_qc_active = ap->qc_active;
0638 int nr_done = 0;
0639
0640
0641
0642
0643
0644
0645 if (ap_qc_active & (1ULL << ATA_TAG_INTERNAL)) {
0646 qc_active |= (qc_active & 0x01) << ATA_TAG_INTERNAL;
0647 qc_active ^= qc_active & 0x01;
0648 }
0649
0650 done_mask = ap_qc_active ^ qc_active;
0651
0652 if (unlikely(done_mask & qc_active)) {
0653 ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n",
0654 ap->qc_active, qc_active);
0655 return -EINVAL;
0656 }
0657
0658 while (done_mask) {
0659 struct ata_queued_cmd *qc;
0660 unsigned int tag = __ffs64(done_mask);
0661
0662 qc = ata_qc_from_tag(ap, tag);
0663 if (qc) {
0664 ata_qc_complete(qc);
0665 nr_done++;
0666 }
0667 done_mask &= ~(1ULL << tag);
0668 }
0669
0670 return nr_done;
0671 }
0672 EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
0673
0674
0675
0676
0677
0678
0679
0680
0681
0682
0683
0684
0685
0686
0687
0688
0689
0690
0691
0692
0693
0694
0695
0696
0697
0698
0699
0700
0701
0702
0703
0704
0705
0706
0707
0708
0709
0710
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720 int ata_slave_link_init(struct ata_port *ap)
0721 {
0722 struct ata_link *link;
0723
0724 WARN_ON(ap->slave_link);
0725 WARN_ON(ap->flags & ATA_FLAG_PMP);
0726
0727 link = kzalloc(sizeof(*link), GFP_KERNEL);
0728 if (!link)
0729 return -ENOMEM;
0730
0731 ata_link_init(ap, link, 1);
0732 ap->slave_link = link;
0733 return 0;
0734 }
0735 EXPORT_SYMBOL_GPL(ata_slave_link_init);
0736
0737
0738
0739
0740
0741
0742
0743
0744
0745
0746
0747
0748
0749 bool sata_lpm_ignore_phy_events(struct ata_link *link)
0750 {
0751 unsigned long lpm_timeout = link->last_lpm_change +
0752 msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);
0753
0754
0755 if (link->lpm_policy > ATA_LPM_MAX_POWER)
0756 return true;
0757
0758
0759
0760
0761 if ((link->flags & ATA_LFLAG_CHANGED) &&
0762 time_before(jiffies, lpm_timeout))
0763 return true;
0764
0765 return false;
0766 }
0767 EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);
0768
0769 static const char *ata_lpm_policy_names[] = {
0770 [ATA_LPM_UNKNOWN] = "max_performance",
0771 [ATA_LPM_MAX_POWER] = "max_performance",
0772 [ATA_LPM_MED_POWER] = "medium_power",
0773 [ATA_LPM_MED_POWER_WITH_DIPM] = "med_power_with_dipm",
0774 [ATA_LPM_MIN_POWER_WITH_PARTIAL] = "min_power_with_partial",
0775 [ATA_LPM_MIN_POWER] = "min_power",
0776 };
0777
0778 static ssize_t ata_scsi_lpm_store(struct device *device,
0779 struct device_attribute *attr,
0780 const char *buf, size_t count)
0781 {
0782 struct Scsi_Host *shost = class_to_shost(device);
0783 struct ata_port *ap = ata_shost_to_port(shost);
0784 struct ata_link *link;
0785 struct ata_device *dev;
0786 enum ata_lpm_policy policy;
0787 unsigned long flags;
0788
0789
0790 for (policy = ATA_LPM_MAX_POWER;
0791 policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
0792 const char *name = ata_lpm_policy_names[policy];
0793
0794 if (strncmp(name, buf, strlen(name)) == 0)
0795 break;
0796 }
0797 if (policy == ARRAY_SIZE(ata_lpm_policy_names))
0798 return -EINVAL;
0799
0800 spin_lock_irqsave(ap->lock, flags);
0801
0802 ata_for_each_link(link, ap, EDGE) {
0803 ata_for_each_dev(dev, &ap->link, ENABLED) {
0804 if (dev->horkage & ATA_HORKAGE_NOLPM) {
0805 count = -EOPNOTSUPP;
0806 goto out_unlock;
0807 }
0808 }
0809 }
0810
0811 ap->target_lpm_policy = policy;
0812 ata_port_schedule_eh(ap);
0813 out_unlock:
0814 spin_unlock_irqrestore(ap->lock, flags);
0815 return count;
0816 }
0817
0818 static ssize_t ata_scsi_lpm_show(struct device *dev,
0819 struct device_attribute *attr, char *buf)
0820 {
0821 struct Scsi_Host *shost = class_to_shost(dev);
0822 struct ata_port *ap = ata_shost_to_port(shost);
0823
0824 if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
0825 return -EINVAL;
0826
0827 return sysfs_emit(buf, "%s\n",
0828 ata_lpm_policy_names[ap->target_lpm_policy]);
0829 }
0830 DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
0831 ata_scsi_lpm_show, ata_scsi_lpm_store);
0832 EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
0833
0834 static ssize_t ata_ncq_prio_supported_show(struct device *device,
0835 struct device_attribute *attr,
0836 char *buf)
0837 {
0838 struct scsi_device *sdev = to_scsi_device(device);
0839 struct ata_port *ap = ata_shost_to_port(sdev->host);
0840 struct ata_device *dev;
0841 bool ncq_prio_supported;
0842 int rc = 0;
0843
0844 spin_lock_irq(ap->lock);
0845 dev = ata_scsi_find_dev(ap, sdev);
0846 if (!dev)
0847 rc = -ENODEV;
0848 else
0849 ncq_prio_supported = dev->flags & ATA_DFLAG_NCQ_PRIO;
0850 spin_unlock_irq(ap->lock);
0851
0852 return rc ? rc : sysfs_emit(buf, "%u\n", ncq_prio_supported);
0853 }
0854
0855 DEVICE_ATTR(ncq_prio_supported, S_IRUGO, ata_ncq_prio_supported_show, NULL);
0856 EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_supported);
0857
0858 static ssize_t ata_ncq_prio_enable_show(struct device *device,
0859 struct device_attribute *attr,
0860 char *buf)
0861 {
0862 struct scsi_device *sdev = to_scsi_device(device);
0863 struct ata_port *ap = ata_shost_to_port(sdev->host);
0864 struct ata_device *dev;
0865 bool ncq_prio_enable;
0866 int rc = 0;
0867
0868 spin_lock_irq(ap->lock);
0869 dev = ata_scsi_find_dev(ap, sdev);
0870 if (!dev)
0871 rc = -ENODEV;
0872 else
0873 ncq_prio_enable = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE;
0874 spin_unlock_irq(ap->lock);
0875
0876 return rc ? rc : sysfs_emit(buf, "%u\n", ncq_prio_enable);
0877 }
0878
0879 static ssize_t ata_ncq_prio_enable_store(struct device *device,
0880 struct device_attribute *attr,
0881 const char *buf, size_t len)
0882 {
0883 struct scsi_device *sdev = to_scsi_device(device);
0884 struct ata_port *ap;
0885 struct ata_device *dev;
0886 long int input;
0887 int rc = 0;
0888
0889 rc = kstrtol(buf, 10, &input);
0890 if (rc)
0891 return rc;
0892 if ((input < 0) || (input > 1))
0893 return -EINVAL;
0894
0895 ap = ata_shost_to_port(sdev->host);
0896 dev = ata_scsi_find_dev(ap, sdev);
0897 if (unlikely(!dev))
0898 return -ENODEV;
0899
0900 spin_lock_irq(ap->lock);
0901
0902 if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) {
0903 rc = -EINVAL;
0904 goto unlock;
0905 }
0906
0907 if (input)
0908 dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLE;
0909 else
0910 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
0911
0912 unlock:
0913 spin_unlock_irq(ap->lock);
0914
0915 return rc ? rc : len;
0916 }
0917
0918 DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR,
0919 ata_ncq_prio_enable_show, ata_ncq_prio_enable_store);
0920 EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable);
0921
0922 static struct attribute *ata_ncq_sdev_attrs[] = {
0923 &dev_attr_unload_heads.attr,
0924 &dev_attr_ncq_prio_enable.attr,
0925 &dev_attr_ncq_prio_supported.attr,
0926 NULL
0927 };
0928
0929 static const struct attribute_group ata_ncq_sdev_attr_group = {
0930 .attrs = ata_ncq_sdev_attrs
0931 };
0932
0933 const struct attribute_group *ata_ncq_sdev_groups[] = {
0934 &ata_ncq_sdev_attr_group,
0935 NULL
0936 };
0937 EXPORT_SYMBOL_GPL(ata_ncq_sdev_groups);
0938
0939 static ssize_t
0940 ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
0941 const char *buf, size_t count)
0942 {
0943 struct Scsi_Host *shost = class_to_shost(dev);
0944 struct ata_port *ap = ata_shost_to_port(shost);
0945 if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
0946 return ap->ops->em_store(ap, buf, count);
0947 return -EINVAL;
0948 }
0949
0950 static ssize_t
0951 ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
0952 char *buf)
0953 {
0954 struct Scsi_Host *shost = class_to_shost(dev);
0955 struct ata_port *ap = ata_shost_to_port(shost);
0956
0957 if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
0958 return ap->ops->em_show(ap, buf);
0959 return -EINVAL;
0960 }
0961 DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
0962 ata_scsi_em_message_show, ata_scsi_em_message_store);
0963 EXPORT_SYMBOL_GPL(dev_attr_em_message);
0964
0965 static ssize_t
0966 ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
0967 char *buf)
0968 {
0969 struct Scsi_Host *shost = class_to_shost(dev);
0970 struct ata_port *ap = ata_shost_to_port(shost);
0971
0972 return sysfs_emit(buf, "%d\n", ap->em_message_type);
0973 }
0974 DEVICE_ATTR(em_message_type, S_IRUGO,
0975 ata_scsi_em_message_type_show, NULL);
0976 EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
0977
0978 static ssize_t
0979 ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
0980 char *buf)
0981 {
0982 struct scsi_device *sdev = to_scsi_device(dev);
0983 struct ata_port *ap = ata_shost_to_port(sdev->host);
0984 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
0985
0986 if (atadev && ap->ops->sw_activity_show &&
0987 (ap->flags & ATA_FLAG_SW_ACTIVITY))
0988 return ap->ops->sw_activity_show(atadev, buf);
0989 return -EINVAL;
0990 }
0991
0992 static ssize_t
0993 ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
0994 const char *buf, size_t count)
0995 {
0996 struct scsi_device *sdev = to_scsi_device(dev);
0997 struct ata_port *ap = ata_shost_to_port(sdev->host);
0998 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
0999 enum sw_activity val;
1000 int rc;
1001
1002 if (atadev && ap->ops->sw_activity_store &&
1003 (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
1004 val = simple_strtoul(buf, NULL, 0);
1005 switch (val) {
1006 case OFF: case BLINK_ON: case BLINK_OFF:
1007 rc = ap->ops->sw_activity_store(atadev, val);
1008 if (!rc)
1009 return count;
1010 else
1011 return rc;
1012 }
1013 }
1014 return -EINVAL;
1015 }
1016 DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
1017 ata_scsi_activity_store);
1018 EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031 int ata_change_queue_depth(struct ata_port *ap, struct ata_device *dev,
1032 struct scsi_device *sdev, int queue_depth)
1033 {
1034 unsigned long flags;
1035
1036 if (!dev || !ata_dev_enabled(dev))
1037 return sdev->queue_depth;
1038
1039 if (queue_depth < 1 || queue_depth == sdev->queue_depth)
1040 return sdev->queue_depth;
1041
1042
1043 spin_lock_irqsave(ap->lock, flags);
1044 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1045 if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
1046 dev->flags |= ATA_DFLAG_NCQ_OFF;
1047 queue_depth = 1;
1048 }
1049 spin_unlock_irqrestore(ap->lock, flags);
1050
1051
1052 queue_depth = min(queue_depth, sdev->host->can_queue);
1053 queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
1054 queue_depth = min(queue_depth, ATA_MAX_QUEUE);
1055
1056 if (sdev->queue_depth == queue_depth)
1057 return -EINVAL;
1058
1059 return scsi_change_queue_depth(sdev, queue_depth);
1060 }
1061 EXPORT_SYMBOL_GPL(ata_change_queue_depth);
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078 int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
1079 {
1080 struct ata_port *ap = ata_shost_to_port(sdev->host);
1081
1082 return ata_change_queue_depth(ap, ata_scsi_find_dev(ap, sdev),
1083 sdev, queue_depth);
1084 }
1085 EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100 struct ata_port *ata_sas_port_alloc(struct ata_host *host,
1101 struct ata_port_info *port_info,
1102 struct Scsi_Host *shost)
1103 {
1104 struct ata_port *ap;
1105
1106 ap = ata_port_alloc(host);
1107 if (!ap)
1108 return NULL;
1109
1110 ap->port_no = 0;
1111 ap->lock = &host->lock;
1112 ap->pio_mask = port_info->pio_mask;
1113 ap->mwdma_mask = port_info->mwdma_mask;
1114 ap->udma_mask = port_info->udma_mask;
1115 ap->flags |= port_info->flags;
1116 ap->ops = port_info->port_ops;
1117 ap->cbl = ATA_CBL_SATA;
1118
1119 return ap;
1120 }
1121 EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135 int ata_sas_port_start(struct ata_port *ap)
1136 {
1137
1138
1139
1140
1141 if (!ap->ops->error_handler)
1142 ap->pflags &= ~ATA_PFLAG_FROZEN;
1143 return 0;
1144 }
1145 EXPORT_SYMBOL_GPL(ata_sas_port_start);
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157 void ata_sas_port_stop(struct ata_port *ap)
1158 {
1159 }
1160 EXPORT_SYMBOL_GPL(ata_sas_port_stop);
1161
1162
1163
1164
1165
1166
1167
1168
1169 void ata_sas_async_probe(struct ata_port *ap)
1170 {
1171 __ata_port_probe(ap);
1172 }
1173 EXPORT_SYMBOL_GPL(ata_sas_async_probe);
1174
1175 int ata_sas_sync_probe(struct ata_port *ap)
1176 {
1177 return ata_port_probe(ap);
1178 }
1179 EXPORT_SYMBOL_GPL(ata_sas_sync_probe);
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193 int ata_sas_port_init(struct ata_port *ap)
1194 {
1195 int rc = ap->ops->port_start(ap);
1196
1197 if (rc)
1198 return rc;
1199 ap->print_id = atomic_inc_return(&ata_print_id);
1200 return 0;
1201 }
1202 EXPORT_SYMBOL_GPL(ata_sas_port_init);
1203
1204 int ata_sas_tport_add(struct device *parent, struct ata_port *ap)
1205 {
1206 return ata_tport_add(parent, ap);
1207 }
1208 EXPORT_SYMBOL_GPL(ata_sas_tport_add);
1209
1210 void ata_sas_tport_delete(struct ata_port *ap)
1211 {
1212 ata_tport_delete(ap);
1213 }
1214 EXPORT_SYMBOL_GPL(ata_sas_tport_delete);
1215
1216
1217
1218
1219
1220
1221
1222 void ata_sas_port_destroy(struct ata_port *ap)
1223 {
1224 if (ap->ops->port_stop)
1225 ap->ops->port_stop(ap);
1226 kfree(ap);
1227 }
1228 EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239 int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
1240 {
1241 ata_scsi_sdev_config(sdev);
1242 ata_scsi_dev_config(sdev, ap->link.device);
1243 return 0;
1244 }
1245 EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257 int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
1258 {
1259 int rc = 0;
1260
1261 if (likely(ata_dev_enabled(ap->link.device)))
1262 rc = __ata_scsi_queuecmd(cmd, ap->link.device);
1263 else {
1264 cmd->result = (DID_BAD_TARGET << 16);
1265 scsi_done(cmd);
1266 }
1267 return rc;
1268 }
1269 EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284 int sata_async_notification(struct ata_port *ap)
1285 {
1286 u32 sntf;
1287 int rc;
1288
1289 if (!(ap->flags & ATA_FLAG_AN))
1290 return 0;
1291
1292 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
1293 if (rc == 0)
1294 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
1295
1296 if (!sata_pmp_attached(ap) || rc) {
1297
1298 if (!sata_pmp_attached(ap)) {
1299
1300
1301
1302
1303 struct ata_device *dev = ap->link.device;
1304
1305 if ((dev->class == ATA_DEV_ATAPI) &&
1306 (dev->flags & ATA_DFLAG_AN))
1307 ata_scsi_media_change_notify(dev);
1308 return 0;
1309 } else {
1310
1311
1312
1313
1314
1315 ata_port_schedule_eh(ap);
1316 return 1;
1317 }
1318 } else {
1319
1320 struct ata_link *link;
1321
1322
1323 ata_for_each_link(link, ap, EDGE) {
1324 if (!(sntf & (1 << link->pmp)))
1325 continue;
1326
1327 if ((link->device->class == ATA_DEV_ATAPI) &&
1328 (link->device->flags & ATA_DFLAG_AN))
1329 ata_scsi_media_change_notify(link->device);
1330 }
1331
1332
1333
1334
1335 if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
1336 ata_port_schedule_eh(ap);
1337 return 1;
1338 }
1339
1340 return 0;
1341 }
1342 }
1343 EXPORT_SYMBOL_GPL(sata_async_notification);
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360 static int ata_eh_read_log_10h(struct ata_device *dev,
1361 int *tag, struct ata_taskfile *tf)
1362 {
1363 u8 *buf = dev->link->ap->sector_buf;
1364 unsigned int err_mask;
1365 u8 csum;
1366 int i;
1367
1368 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, 0, buf, 1);
1369 if (err_mask)
1370 return -EIO;
1371
1372 csum = 0;
1373 for (i = 0; i < ATA_SECT_SIZE; i++)
1374 csum += buf[i];
1375 if (csum)
1376 ata_dev_warn(dev, "invalid checksum 0x%x on log page 10h\n",
1377 csum);
1378
1379 if (buf[0] & 0x80)
1380 return -ENOENT;
1381
1382 *tag = buf[0] & 0x1f;
1383
1384 tf->status = buf[2];
1385 tf->error = buf[3];
1386 tf->lbal = buf[4];
1387 tf->lbam = buf[5];
1388 tf->lbah = buf[6];
1389 tf->device = buf[7];
1390 tf->hob_lbal = buf[8];
1391 tf->hob_lbam = buf[9];
1392 tf->hob_lbah = buf[10];
1393 tf->nsect = buf[12];
1394 tf->hob_nsect = buf[13];
1395 if (dev->class == ATA_DEV_ZAC && ata_id_has_ncq_autosense(dev->id))
1396 tf->auxiliary = buf[14] << 16 | buf[15] << 8 | buf[16];
1397
1398 return 0;
1399 }
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413 void ata_eh_analyze_ncq_error(struct ata_link *link)
1414 {
1415 struct ata_port *ap = link->ap;
1416 struct ata_eh_context *ehc = &link->eh_context;
1417 struct ata_device *dev = link->device;
1418 struct ata_queued_cmd *qc;
1419 struct ata_taskfile tf;
1420 int tag, rc;
1421
1422
1423 if (ap->pflags & ATA_PFLAG_FROZEN)
1424 return;
1425
1426
1427 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1428 return;
1429
1430
1431 ata_qc_for_each_raw(ap, qc, tag) {
1432 if (!(qc->flags & ATA_QCFLAG_FAILED))
1433 continue;
1434
1435 if (qc->err_mask)
1436 return;
1437 }
1438
1439
1440 memset(&tf, 0, sizeof(tf));
1441 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1442 if (rc) {
1443 ata_link_err(link, "failed to read log page 10h (errno=%d)\n",
1444 rc);
1445 return;
1446 }
1447
1448 if (!(link->sactive & (1 << tag))) {
1449 ata_link_err(link, "log page 10h reported inactive tag %d\n",
1450 tag);
1451 return;
1452 }
1453
1454
1455 qc = __ata_qc_from_tag(ap, tag);
1456 memcpy(&qc->result_tf, &tf, sizeof(tf));
1457 qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1458 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1459 if (dev->class == ATA_DEV_ZAC &&
1460 ((qc->result_tf.status & ATA_SENSE) || qc->result_tf.auxiliary)) {
1461 char sense_key, asc, ascq;
1462
1463 sense_key = (qc->result_tf.auxiliary >> 16) & 0xff;
1464 asc = (qc->result_tf.auxiliary >> 8) & 0xff;
1465 ascq = qc->result_tf.auxiliary & 0xff;
1466 ata_scsi_set_sense(dev, qc->scsicmd, sense_key, asc, ascq);
1467 ata_scsi_set_sense_information(dev, qc->scsicmd,
1468 &qc->result_tf);
1469 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1470 }
1471
1472 ehc->i.err_mask &= ~AC_ERR_DEV;
1473 }
1474 EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);