1
2
3
4
5
6
7
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 <linux/libata.h>
15
16#include "libata.h"
17#include "libata-transport.h"
18
19
20const unsigned long sata_deb_timing_normal[] = { 5, 100, 2000 };
21EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
22const unsigned long sata_deb_timing_hotplug[] = { 25, 500, 2000 };
23EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
24const unsigned long sata_deb_timing_long[] = { 100, 2000, 5000 };
25EXPORT_SYMBOL_GPL(sata_deb_timing_long);
26
27
28
29
30
31
32
33
34
35
36
37
38
39int sata_scr_valid(struct ata_link *link)
40{
41 struct ata_port *ap = link->ap;
42
43 return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
44}
45EXPORT_SYMBOL_GPL(sata_scr_valid);
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63int sata_scr_read(struct ata_link *link, int reg, u32 *val)
64{
65 if (ata_is_host_link(link)) {
66 if (sata_scr_valid(link))
67 return link->ap->ops->scr_read(link, reg, val);
68 return -EOPNOTSUPP;
69 }
70
71 return sata_pmp_scr_read(link, reg, val);
72}
73EXPORT_SYMBOL_GPL(sata_scr_read);
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91int sata_scr_write(struct ata_link *link, int reg, u32 val)
92{
93 if (ata_is_host_link(link)) {
94 if (sata_scr_valid(link))
95 return link->ap->ops->scr_write(link, reg, val);
96 return -EOPNOTSUPP;
97 }
98
99 return sata_pmp_scr_write(link, reg, val);
100}
101EXPORT_SYMBOL_GPL(sata_scr_write);
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
119{
120 if (ata_is_host_link(link)) {
121 int rc;
122
123 if (sata_scr_valid(link)) {
124 rc = link->ap->ops->scr_write(link, reg, val);
125 if (rc == 0)
126 rc = link->ap->ops->scr_read(link, reg, &val);
127 return rc;
128 }
129 return -EOPNOTSUPP;
130 }
131
132 return sata_pmp_scr_write(link, reg, val);
133}
134EXPORT_SYMBOL_GPL(sata_scr_write_flush);
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
150{
151 fis[0] = 0x27;
152 fis[1] = pmp & 0xf;
153 if (is_cmd)
154 fis[1] |= (1 << 7);
155
156 fis[2] = tf->command;
157 fis[3] = tf->feature;
158
159 fis[4] = tf->lbal;
160 fis[5] = tf->lbam;
161 fis[6] = tf->lbah;
162 fis[7] = tf->device;
163
164 fis[8] = tf->hob_lbal;
165 fis[9] = tf->hob_lbam;
166 fis[10] = tf->hob_lbah;
167 fis[11] = tf->hob_feature;
168
169 fis[12] = tf->nsect;
170 fis[13] = tf->hob_nsect;
171 fis[14] = 0;
172 fis[15] = tf->ctl;
173
174 fis[16] = tf->auxiliary & 0xff;
175 fis[17] = (tf->auxiliary >> 8) & 0xff;
176 fis[18] = (tf->auxiliary >> 16) & 0xff;
177 fis[19] = (tf->auxiliary >> 24) & 0xff;
178}
179EXPORT_SYMBOL_GPL(ata_tf_to_fis);
180
181
182
183
184
185
186
187
188
189
190
191
192void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
193{
194 tf->command = fis[2];
195 tf->feature = fis[3];
196
197 tf->lbal = fis[4];
198 tf->lbam = fis[5];
199 tf->lbah = fis[6];
200 tf->device = fis[7];
201
202 tf->hob_lbal = fis[8];
203 tf->hob_lbam = fis[9];
204 tf->hob_lbah = fis[10];
205
206 tf->nsect = fis[12];
207 tf->hob_nsect = fis[13];
208}
209EXPORT_SYMBOL_GPL(ata_tf_from_fis);
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233int sata_link_debounce(struct ata_link *link, const unsigned long *params,
234 unsigned long deadline)
235{
236 unsigned long interval = params[0];
237 unsigned long duration = params[1];
238 unsigned long last_jiffies, t;
239 u32 last, cur;
240 int rc;
241
242 t = ata_deadline(jiffies, params[2]);
243 if (time_before(t, deadline))
244 deadline = t;
245
246 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
247 return rc;
248 cur &= 0xf;
249
250 last = cur;
251 last_jiffies = jiffies;
252
253 while (1) {
254 ata_msleep(link->ap, interval);
255 if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
256 return rc;
257 cur &= 0xf;
258
259
260 if (cur == last) {
261 if (cur == 1 && time_before(jiffies, deadline))
262 continue;
263 if (time_after(jiffies,
264 ata_deadline(last_jiffies, duration)))
265 return 0;
266 continue;
267 }
268
269
270 last = cur;
271 last_jiffies = jiffies;
272
273
274
275
276 if (time_after(jiffies, deadline))
277 return -EPIPE;
278 }
279}
280EXPORT_SYMBOL_GPL(sata_link_debounce);
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296int sata_link_resume(struct ata_link *link, const unsigned long *params,
297 unsigned long deadline)
298{
299 int tries = ATA_LINK_RESUME_TRIES;
300 u32 scontrol, serror;
301 int rc;
302
303 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
304 return rc;
305
306
307
308
309
310
311 do {
312 scontrol = (scontrol & 0x0f0) | 0x300;
313 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
314 return rc;
315
316
317
318
319
320 if (!(link->flags & ATA_LFLAG_NO_DB_DELAY))
321 ata_msleep(link->ap, 200);
322
323
324 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
325 return rc;
326 } while ((scontrol & 0xf0f) != 0x300 && --tries);
327
328 if ((scontrol & 0xf0f) != 0x300) {
329 ata_link_warn(link, "failed to resume link (SControl %X)\n",
330 scontrol);
331 return 0;
332 }
333
334 if (tries < ATA_LINK_RESUME_TRIES)
335 ata_link_warn(link, "link resume succeeded after %d retries\n",
336 ATA_LINK_RESUME_TRIES - tries);
337
338 if ((rc = sata_link_debounce(link, params, deadline)))
339 return rc;
340
341
342 if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
343 rc = sata_scr_write(link, SCR_ERROR, serror);
344
345 return rc != -EINVAL ? rc : 0;
346}
347EXPORT_SYMBOL_GPL(sata_link_resume);
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
368 bool spm_wakeup)
369{
370 struct ata_eh_context *ehc = &link->eh_context;
371 bool woken_up = false;
372 u32 scontrol;
373 int rc;
374
375 rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
376 if (rc)
377 return rc;
378
379 switch (policy) {
380 case ATA_LPM_MAX_POWER:
381
382 scontrol |= (0x7 << 8);
383
384 if (spm_wakeup) {
385 scontrol |= (0x4 << 12);
386 woken_up = true;
387 }
388 break;
389 case ATA_LPM_MED_POWER:
390
391 scontrol &= ~(0x1 << 8);
392 scontrol |= (0x6 << 8);
393 break;
394 case ATA_LPM_MED_POWER_WITH_DIPM:
395 case ATA_LPM_MIN_POWER_WITH_PARTIAL:
396 case ATA_LPM_MIN_POWER:
397 if (ata_link_nr_enabled(link) > 0)
398
399 scontrol &= ~(0x7 << 8);
400 else {
401
402 scontrol &= ~0xf;
403 scontrol |= (0x1 << 2);
404 }
405 break;
406 default:
407 WARN_ON(1);
408 }
409
410 rc = sata_scr_write(link, SCR_CONTROL, scontrol);
411 if (rc)
412 return rc;
413
414
415 if (woken_up)
416 msleep(10);
417
418
419 ehc->i.serror &= ~SERR_PHYRDY_CHG;
420 return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
421}
422EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
423
424static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
425{
426 struct ata_link *host_link = &link->ap->link;
427 u32 limit, target, spd;
428
429 limit = link->sata_spd_limit;
430
431
432
433
434
435 if (!ata_is_host_link(link) && host_link->sata_spd)
436 limit &= (1 << host_link->sata_spd) - 1;
437
438 if (limit == UINT_MAX)
439 target = 0;
440 else
441 target = fls(limit);
442
443 spd = (*scontrol >> 4) & 0xf;
444 *scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
445
446 return spd != target;
447}
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464static int sata_set_spd_needed(struct ata_link *link)
465{
466 u32 scontrol;
467
468 if (sata_scr_read(link, SCR_CONTROL, &scontrol))
469 return 1;
470
471 return __sata_set_spd_needed(link, &scontrol);
472}
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487int sata_set_spd(struct ata_link *link)
488{
489 u32 scontrol;
490 int rc;
491
492 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
493 return rc;
494
495 if (!__sata_set_spd_needed(link, &scontrol))
496 return 0;
497
498 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
499 return rc;
500
501 return 1;
502}
503EXPORT_SYMBOL_GPL(sata_set_spd);
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
530 unsigned long deadline,
531 bool *online, int (*check_ready)(struct ata_link *))
532{
533 u32 scontrol;
534 int rc;
535
536 DPRINTK("ENTER\n");
537
538 if (online)
539 *online = false;
540
541 if (sata_set_spd_needed(link)) {
542
543
544
545
546
547 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
548 goto out;
549
550 scontrol = (scontrol & 0x0f0) | 0x304;
551
552 if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
553 goto out;
554
555 sata_set_spd(link);
556 }
557
558
559 if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
560 goto out;
561
562 scontrol = (scontrol & 0x0f0) | 0x301;
563
564 if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
565 goto out;
566
567
568
569
570 ata_msleep(link->ap, 1);
571
572
573 rc = sata_link_resume(link, timing, deadline);
574 if (rc)
575 goto out;
576
577 if (ata_phys_link_offline(link))
578 goto out;
579
580
581 if (online)
582 *online = true;
583
584 if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
585
586
587
588
589
590 if (check_ready) {
591 unsigned long pmp_deadline;
592
593 pmp_deadline = ata_deadline(jiffies,
594 ATA_TMOUT_PMP_SRST_WAIT);
595 if (time_after(pmp_deadline, deadline))
596 pmp_deadline = deadline;
597 ata_wait_ready(link, pmp_deadline, check_ready);
598 }
599 rc = -EAGAIN;
600 goto out;
601 }
602
603 rc = 0;
604 if (check_ready)
605 rc = ata_wait_ready(link, deadline, check_ready);
606 out:
607 if (rc && rc != -EAGAIN) {
608
609 if (online)
610 *online = false;
611 ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
612 }
613 DPRINTK("EXIT, rc=%d\n", rc);
614 return rc;
615}
616EXPORT_SYMBOL_GPL(sata_link_hardreset);
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active)
639{
640 u64 done_mask, ap_qc_active = ap->qc_active;
641 int nr_done = 0;
642
643
644
645
646
647
648 if (ap_qc_active & (1ULL << ATA_TAG_INTERNAL)) {
649 qc_active |= (qc_active & 0x01) << ATA_TAG_INTERNAL;
650 qc_active ^= qc_active & 0x01;
651 }
652
653 done_mask = ap_qc_active ^ qc_active;
654
655 if (unlikely(done_mask & qc_active)) {
656 ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n",
657 ap->qc_active, qc_active);
658 return -EINVAL;
659 }
660
661 while (done_mask) {
662 struct ata_queued_cmd *qc;
663 unsigned int tag = __ffs64(done_mask);
664
665 qc = ata_qc_from_tag(ap, tag);
666 if (qc) {
667 ata_qc_complete(qc);
668 nr_done++;
669 }
670 done_mask &= ~(1ULL << tag);
671 }
672
673 return nr_done;
674}
675EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723int ata_slave_link_init(struct ata_port *ap)
724{
725 struct ata_link *link;
726
727 WARN_ON(ap->slave_link);
728 WARN_ON(ap->flags & ATA_FLAG_PMP);
729
730 link = kzalloc(sizeof(*link), GFP_KERNEL);
731 if (!link)
732 return -ENOMEM;
733
734 ata_link_init(ap, link, 1);
735 ap->slave_link = link;
736 return 0;
737}
738EXPORT_SYMBOL_GPL(ata_slave_link_init);
739
740
741
742
743
744
745
746
747
748
749
750
751
752bool sata_lpm_ignore_phy_events(struct ata_link *link)
753{
754 unsigned long lpm_timeout = link->last_lpm_change +
755 msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);
756
757
758 if (link->lpm_policy > ATA_LPM_MAX_POWER)
759 return true;
760
761
762
763
764 if ((link->flags & ATA_LFLAG_CHANGED) &&
765 time_before(jiffies, lpm_timeout))
766 return true;
767
768 return false;
769}
770EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);
771
772static const char *ata_lpm_policy_names[] = {
773 [ATA_LPM_UNKNOWN] = "max_performance",
774 [ATA_LPM_MAX_POWER] = "max_performance",
775 [ATA_LPM_MED_POWER] = "medium_power",
776 [ATA_LPM_MED_POWER_WITH_DIPM] = "med_power_with_dipm",
777 [ATA_LPM_MIN_POWER_WITH_PARTIAL] = "min_power_with_partial",
778 [ATA_LPM_MIN_POWER] = "min_power",
779};
780
781static ssize_t ata_scsi_lpm_store(struct device *device,
782 struct device_attribute *attr,
783 const char *buf, size_t count)
784{
785 struct Scsi_Host *shost = class_to_shost(device);
786 struct ata_port *ap = ata_shost_to_port(shost);
787 struct ata_link *link;
788 struct ata_device *dev;
789 enum ata_lpm_policy policy;
790 unsigned long flags;
791
792
793 for (policy = ATA_LPM_MAX_POWER;
794 policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
795 const char *name = ata_lpm_policy_names[policy];
796
797 if (strncmp(name, buf, strlen(name)) == 0)
798 break;
799 }
800 if (policy == ARRAY_SIZE(ata_lpm_policy_names))
801 return -EINVAL;
802
803 spin_lock_irqsave(ap->lock, flags);
804
805 ata_for_each_link(link, ap, EDGE) {
806 ata_for_each_dev(dev, &ap->link, ENABLED) {
807 if (dev->horkage & ATA_HORKAGE_NOLPM) {
808 count = -EOPNOTSUPP;
809 goto out_unlock;
810 }
811 }
812 }
813
814 ap->target_lpm_policy = policy;
815 ata_port_schedule_eh(ap);
816out_unlock:
817 spin_unlock_irqrestore(ap->lock, flags);
818 return count;
819}
820
821static ssize_t ata_scsi_lpm_show(struct device *dev,
822 struct device_attribute *attr, char *buf)
823{
824 struct Scsi_Host *shost = class_to_shost(dev);
825 struct ata_port *ap = ata_shost_to_port(shost);
826
827 if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
828 return -EINVAL;
829
830 return snprintf(buf, PAGE_SIZE, "%s\n",
831 ata_lpm_policy_names[ap->target_lpm_policy]);
832}
833DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
834 ata_scsi_lpm_show, ata_scsi_lpm_store);
835EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
836
837static ssize_t ata_ncq_prio_enable_show(struct device *device,
838 struct device_attribute *attr,
839 char *buf)
840{
841 struct scsi_device *sdev = to_scsi_device(device);
842 struct ata_port *ap;
843 struct ata_device *dev;
844 bool ncq_prio_enable;
845 int rc = 0;
846
847 ap = ata_shost_to_port(sdev->host);
848
849 spin_lock_irq(ap->lock);
850 dev = ata_scsi_find_dev(ap, sdev);
851 if (!dev) {
852 rc = -ENODEV;
853 goto unlock;
854 }
855
856 ncq_prio_enable = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE;
857
858unlock:
859 spin_unlock_irq(ap->lock);
860
861 return rc ? rc : snprintf(buf, 20, "%u\n", ncq_prio_enable);
862}
863
864static ssize_t ata_ncq_prio_enable_store(struct device *device,
865 struct device_attribute *attr,
866 const char *buf, size_t len)
867{
868 struct scsi_device *sdev = to_scsi_device(device);
869 struct ata_port *ap;
870 struct ata_device *dev;
871 long int input;
872 int rc;
873
874 rc = kstrtol(buf, 10, &input);
875 if (rc)
876 return rc;
877 if ((input < 0) || (input > 1))
878 return -EINVAL;
879
880 ap = ata_shost_to_port(sdev->host);
881 dev = ata_scsi_find_dev(ap, sdev);
882 if (unlikely(!dev))
883 return -ENODEV;
884
885 spin_lock_irq(ap->lock);
886 if (input)
887 dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLE;
888 else
889 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
890
891 dev->link->eh_info.action |= ATA_EH_REVALIDATE;
892 dev->link->eh_info.flags |= ATA_EHI_QUIET;
893 ata_port_schedule_eh(ap);
894 spin_unlock_irq(ap->lock);
895
896 ata_port_wait_eh(ap);
897
898 if (input) {
899 spin_lock_irq(ap->lock);
900 if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) {
901 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
902 rc = -EIO;
903 }
904 spin_unlock_irq(ap->lock);
905 }
906
907 return rc ? rc : len;
908}
909
910DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR,
911 ata_ncq_prio_enable_show, ata_ncq_prio_enable_store);
912EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable);
913
914struct device_attribute *ata_ncq_sdev_attrs[] = {
915 &dev_attr_unload_heads,
916 &dev_attr_ncq_prio_enable,
917 NULL
918};
919EXPORT_SYMBOL_GPL(ata_ncq_sdev_attrs);
920
921static ssize_t
922ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
923 const char *buf, size_t count)
924{
925 struct Scsi_Host *shost = class_to_shost(dev);
926 struct ata_port *ap = ata_shost_to_port(shost);
927 if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
928 return ap->ops->em_store(ap, buf, count);
929 return -EINVAL;
930}
931
932static ssize_t
933ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
934 char *buf)
935{
936 struct Scsi_Host *shost = class_to_shost(dev);
937 struct ata_port *ap = ata_shost_to_port(shost);
938
939 if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
940 return ap->ops->em_show(ap, buf);
941 return -EINVAL;
942}
943DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
944 ata_scsi_em_message_show, ata_scsi_em_message_store);
945EXPORT_SYMBOL_GPL(dev_attr_em_message);
946
947static ssize_t
948ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
949 char *buf)
950{
951 struct Scsi_Host *shost = class_to_shost(dev);
952 struct ata_port *ap = ata_shost_to_port(shost);
953
954 return snprintf(buf, 23, "%d\n", ap->em_message_type);
955}
956DEVICE_ATTR(em_message_type, S_IRUGO,
957 ata_scsi_em_message_type_show, NULL);
958EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
959
960static ssize_t
961ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
962 char *buf)
963{
964 struct scsi_device *sdev = to_scsi_device(dev);
965 struct ata_port *ap = ata_shost_to_port(sdev->host);
966 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
967
968 if (atadev && ap->ops->sw_activity_show &&
969 (ap->flags & ATA_FLAG_SW_ACTIVITY))
970 return ap->ops->sw_activity_show(atadev, buf);
971 return -EINVAL;
972}
973
974static ssize_t
975ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
976 const char *buf, size_t count)
977{
978 struct scsi_device *sdev = to_scsi_device(dev);
979 struct ata_port *ap = ata_shost_to_port(sdev->host);
980 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
981 enum sw_activity val;
982 int rc;
983
984 if (atadev && ap->ops->sw_activity_store &&
985 (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
986 val = simple_strtoul(buf, NULL, 0);
987 switch (val) {
988 case OFF: case BLINK_ON: case BLINK_OFF:
989 rc = ap->ops->sw_activity_store(atadev, val);
990 if (!rc)
991 return count;
992 else
993 return rc;
994 }
995 }
996 return -EINVAL;
997}
998DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
999 ata_scsi_activity_store);
1000EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012int __ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
1013 int queue_depth)
1014{
1015 struct ata_device *dev;
1016 unsigned long flags;
1017
1018 if (queue_depth < 1 || queue_depth == sdev->queue_depth)
1019 return sdev->queue_depth;
1020
1021 dev = ata_scsi_find_dev(ap, sdev);
1022 if (!dev || !ata_dev_enabled(dev))
1023 return sdev->queue_depth;
1024
1025
1026 spin_lock_irqsave(ap->lock, flags);
1027 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1028 if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
1029 dev->flags |= ATA_DFLAG_NCQ_OFF;
1030 queue_depth = 1;
1031 }
1032 spin_unlock_irqrestore(ap->lock, flags);
1033
1034
1035 queue_depth = min(queue_depth, sdev->host->can_queue);
1036 queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
1037 queue_depth = min(queue_depth, ATA_MAX_QUEUE);
1038
1039 if (sdev->queue_depth == queue_depth)
1040 return -EINVAL;
1041
1042 return scsi_change_queue_depth(sdev, queue_depth);
1043}
1044EXPORT_SYMBOL_GPL(__ata_change_queue_depth);
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
1062{
1063 struct ata_port *ap = ata_shost_to_port(sdev->host);
1064
1065 return __ata_change_queue_depth(ap, sdev, queue_depth);
1066}
1067EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082struct ata_port *ata_sas_port_alloc(struct ata_host *host,
1083 struct ata_port_info *port_info,
1084 struct Scsi_Host *shost)
1085{
1086 struct ata_port *ap;
1087
1088 ap = ata_port_alloc(host);
1089 if (!ap)
1090 return NULL;
1091
1092 ap->port_no = 0;
1093 ap->lock = &host->lock;
1094 ap->pio_mask = port_info->pio_mask;
1095 ap->mwdma_mask = port_info->mwdma_mask;
1096 ap->udma_mask = port_info->udma_mask;
1097 ap->flags |= port_info->flags;
1098 ap->ops = port_info->port_ops;
1099 ap->cbl = ATA_CBL_SATA;
1100
1101 return ap;
1102}
1103EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117int ata_sas_port_start(struct ata_port *ap)
1118{
1119
1120
1121
1122
1123 if (!ap->ops->error_handler)
1124 ap->pflags &= ~ATA_PFLAG_FROZEN;
1125 return 0;
1126}
1127EXPORT_SYMBOL_GPL(ata_sas_port_start);
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139void ata_sas_port_stop(struct ata_port *ap)
1140{
1141}
1142EXPORT_SYMBOL_GPL(ata_sas_port_stop);
1143
1144
1145
1146
1147
1148
1149
1150
1151void ata_sas_async_probe(struct ata_port *ap)
1152{
1153 __ata_port_probe(ap);
1154}
1155EXPORT_SYMBOL_GPL(ata_sas_async_probe);
1156
1157int ata_sas_sync_probe(struct ata_port *ap)
1158{
1159 return ata_port_probe(ap);
1160}
1161EXPORT_SYMBOL_GPL(ata_sas_sync_probe);
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175int ata_sas_port_init(struct ata_port *ap)
1176{
1177 int rc = ap->ops->port_start(ap);
1178
1179 if (rc)
1180 return rc;
1181 ap->print_id = atomic_inc_return(&ata_print_id);
1182 return 0;
1183}
1184EXPORT_SYMBOL_GPL(ata_sas_port_init);
1185
1186int ata_sas_tport_add(struct device *parent, struct ata_port *ap)
1187{
1188 return ata_tport_add(parent, ap);
1189}
1190EXPORT_SYMBOL_GPL(ata_sas_tport_add);
1191
1192void ata_sas_tport_delete(struct ata_port *ap)
1193{
1194 ata_tport_delete(ap);
1195}
1196EXPORT_SYMBOL_GPL(ata_sas_tport_delete);
1197
1198
1199
1200
1201
1202
1203
1204void ata_sas_port_destroy(struct ata_port *ap)
1205{
1206 if (ap->ops->port_stop)
1207 ap->ops->port_stop(ap);
1208 kfree(ap);
1209}
1210EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
1222{
1223 ata_scsi_sdev_config(sdev);
1224 ata_scsi_dev_config(sdev, ap->link.device);
1225 return 0;
1226}
1227EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
1240{
1241 int rc = 0;
1242
1243 ata_scsi_dump_cdb(ap, cmd);
1244
1245 if (likely(ata_dev_enabled(ap->link.device)))
1246 rc = __ata_scsi_queuecmd(cmd, ap->link.device);
1247 else {
1248 cmd->result = (DID_BAD_TARGET << 16);
1249 cmd->scsi_done(cmd);
1250 }
1251 return rc;
1252}
1253EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
1254
1255int ata_sas_allocate_tag(struct ata_port *ap)
1256{
1257 unsigned int max_queue = ap->host->n_tags;
1258 unsigned int i, tag;
1259
1260 for (i = 0, tag = ap->sas_last_tag + 1; i < max_queue; i++, tag++) {
1261 tag = tag < max_queue ? tag : 0;
1262
1263
1264 if (ata_tag_internal(tag))
1265 continue;
1266
1267 if (!test_and_set_bit(tag, &ap->sas_tag_allocated)) {
1268 ap->sas_last_tag = tag;
1269 return tag;
1270 }
1271 }
1272 return -1;
1273}
1274
1275void ata_sas_free_tag(unsigned int tag, struct ata_port *ap)
1276{
1277 clear_bit(tag, &ap->sas_tag_allocated);
1278}
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293int sata_async_notification(struct ata_port *ap)
1294{
1295 u32 sntf;
1296 int rc;
1297
1298 if (!(ap->flags & ATA_FLAG_AN))
1299 return 0;
1300
1301 rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
1302 if (rc == 0)
1303 sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
1304
1305 if (!sata_pmp_attached(ap) || rc) {
1306
1307 if (!sata_pmp_attached(ap)) {
1308
1309
1310
1311
1312 struct ata_device *dev = ap->link.device;
1313
1314 if ((dev->class == ATA_DEV_ATAPI) &&
1315 (dev->flags & ATA_DFLAG_AN))
1316 ata_scsi_media_change_notify(dev);
1317 return 0;
1318 } else {
1319
1320
1321
1322
1323
1324 ata_port_schedule_eh(ap);
1325 return 1;
1326 }
1327 } else {
1328
1329 struct ata_link *link;
1330
1331
1332 ata_for_each_link(link, ap, EDGE) {
1333 if (!(sntf & (1 << link->pmp)))
1334 continue;
1335
1336 if ((link->device->class == ATA_DEV_ATAPI) &&
1337 (link->device->flags & ATA_DFLAG_AN))
1338 ata_scsi_media_change_notify(link->device);
1339 }
1340
1341
1342
1343
1344 if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
1345 ata_port_schedule_eh(ap);
1346 return 1;
1347 }
1348
1349 return 0;
1350 }
1351}
1352EXPORT_SYMBOL_GPL(sata_async_notification);
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369static int ata_eh_read_log_10h(struct ata_device *dev,
1370 int *tag, struct ata_taskfile *tf)
1371{
1372 u8 *buf = dev->link->ap->sector_buf;
1373 unsigned int err_mask;
1374 u8 csum;
1375 int i;
1376
1377 err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, 0, buf, 1);
1378 if (err_mask)
1379 return -EIO;
1380
1381 csum = 0;
1382 for (i = 0; i < ATA_SECT_SIZE; i++)
1383 csum += buf[i];
1384 if (csum)
1385 ata_dev_warn(dev, "invalid checksum 0x%x on log page 10h\n",
1386 csum);
1387
1388 if (buf[0] & 0x80)
1389 return -ENOENT;
1390
1391 *tag = buf[0] & 0x1f;
1392
1393 tf->command = buf[2];
1394 tf->feature = buf[3];
1395 tf->lbal = buf[4];
1396 tf->lbam = buf[5];
1397 tf->lbah = buf[6];
1398 tf->device = buf[7];
1399 tf->hob_lbal = buf[8];
1400 tf->hob_lbam = buf[9];
1401 tf->hob_lbah = buf[10];
1402 tf->nsect = buf[12];
1403 tf->hob_nsect = buf[13];
1404 if (dev->class == ATA_DEV_ZAC && ata_id_has_ncq_autosense(dev->id))
1405 tf->auxiliary = buf[14] << 16 | buf[15] << 8 | buf[16];
1406
1407 return 0;
1408}
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422void ata_eh_analyze_ncq_error(struct ata_link *link)
1423{
1424 struct ata_port *ap = link->ap;
1425 struct ata_eh_context *ehc = &link->eh_context;
1426 struct ata_device *dev = link->device;
1427 struct ata_queued_cmd *qc;
1428 struct ata_taskfile tf;
1429 int tag, rc;
1430
1431
1432 if (ap->pflags & ATA_PFLAG_FROZEN)
1433 return;
1434
1435
1436 if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
1437 return;
1438
1439
1440 ata_qc_for_each_raw(ap, qc, tag) {
1441 if (!(qc->flags & ATA_QCFLAG_FAILED))
1442 continue;
1443
1444 if (qc->err_mask)
1445 return;
1446 }
1447
1448
1449 memset(&tf, 0, sizeof(tf));
1450 rc = ata_eh_read_log_10h(dev, &tag, &tf);
1451 if (rc) {
1452 ata_link_err(link, "failed to read log page 10h (errno=%d)\n",
1453 rc);
1454 return;
1455 }
1456
1457 if (!(link->sactive & (1 << tag))) {
1458 ata_link_err(link, "log page 10h reported inactive tag %d\n",
1459 tag);
1460 return;
1461 }
1462
1463
1464 qc = __ata_qc_from_tag(ap, tag);
1465 memcpy(&qc->result_tf, &tf, sizeof(tf));
1466 qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
1467 qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
1468 if (dev->class == ATA_DEV_ZAC &&
1469 ((qc->result_tf.command & ATA_SENSE) || qc->result_tf.auxiliary)) {
1470 char sense_key, asc, ascq;
1471
1472 sense_key = (qc->result_tf.auxiliary >> 16) & 0xff;
1473 asc = (qc->result_tf.auxiliary >> 8) & 0xff;
1474 ascq = qc->result_tf.auxiliary & 0xff;
1475 ata_scsi_set_sense(dev, qc->scsicmd, sense_key, asc, ascq);
1476 ata_scsi_set_sense_information(dev, qc->scsicmd,
1477 &qc->result_tf);
1478 qc->flags |= ATA_QCFLAG_SENSE_VALID;
1479 }
1480
1481 ehc->i.err_mask &= ~AC_ERR_DEV;
1482}
1483EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
1484