1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36#include <linux/slab.h>
37#include <linux/kernel.h>
38#include <linux/blkdev.h>
39#include <linux/spinlock.h>
40#include <linux/export.h>
41#include <scsi/scsi.h>
42#include <scsi/scsi_host.h>
43#include <scsi/scsi_cmnd.h>
44#include <scsi/scsi_eh.h>
45#include <scsi/scsi_device.h>
46#include <scsi/scsi_tcq.h>
47#include <scsi/scsi_transport.h>
48#include <linux/libata.h>
49#include <linux/hdreg.h>
50#include <linux/uaccess.h>
51#include <linux/suspend.h>
52#include <asm/unaligned.h>
53#include <linux/ioprio.h>
54
55#include "libata.h"
56#include "libata-transport.h"
57
58#define ATA_SCSI_RBUF_SIZE 4096
59
60static DEFINE_SPINLOCK(ata_scsi_rbuf_lock);
61static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE];
62
63typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
64
65static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
66 const struct scsi_device *scsidev);
67static struct ata_device *ata_scsi_find_dev(struct ata_port *ap,
68 const struct scsi_device *scsidev);
69
70#define RW_RECOVERY_MPAGE 0x1
71#define RW_RECOVERY_MPAGE_LEN 12
72#define CACHE_MPAGE 0x8
73#define CACHE_MPAGE_LEN 20
74#define CONTROL_MPAGE 0xa
75#define CONTROL_MPAGE_LEN 12
76#define ALL_MPAGES 0x3f
77#define ALL_SUB_MPAGES 0xff
78
79
80static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
81 RW_RECOVERY_MPAGE,
82 RW_RECOVERY_MPAGE_LEN - 2,
83 (1 << 7),
84 0,
85 0, 0, 0, 0,
86 0,
87 0, 0, 0
88};
89
90static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
91 CACHE_MPAGE,
92 CACHE_MPAGE_LEN - 2,
93 0,
94 0, 0, 0, 0, 0, 0, 0, 0, 0,
95 0,
96 0, 0, 0, 0, 0, 0, 0
97};
98
99static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
100 CONTROL_MPAGE,
101 CONTROL_MPAGE_LEN - 2,
102 2,
103 0,
104 0, 0, 0, 0, 0xff, 0xff,
105 0, 30
106};
107
108static const char *ata_lpm_policy_names[] = {
109 [ATA_LPM_UNKNOWN] = "max_performance",
110 [ATA_LPM_MAX_POWER] = "max_performance",
111 [ATA_LPM_MED_POWER] = "medium_power",
112 [ATA_LPM_MED_POWER_WITH_DIPM] = "med_power_with_dipm",
113 [ATA_LPM_MIN_POWER] = "min_power",
114};
115
116static ssize_t ata_scsi_lpm_store(struct device *device,
117 struct device_attribute *attr,
118 const char *buf, size_t count)
119{
120 struct Scsi_Host *shost = class_to_shost(device);
121 struct ata_port *ap = ata_shost_to_port(shost);
122 struct ata_link *link;
123 struct ata_device *dev;
124 enum ata_lpm_policy policy;
125 unsigned long flags;
126
127
128 for (policy = ATA_LPM_MAX_POWER;
129 policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
130 const char *name = ata_lpm_policy_names[policy];
131
132 if (strncmp(name, buf, strlen(name)) == 0)
133 break;
134 }
135 if (policy == ARRAY_SIZE(ata_lpm_policy_names))
136 return -EINVAL;
137
138 spin_lock_irqsave(ap->lock, flags);
139
140 ata_for_each_link(link, ap, EDGE) {
141 ata_for_each_dev(dev, &ap->link, ENABLED) {
142 if (dev->horkage & ATA_HORKAGE_NOLPM) {
143 count = -EOPNOTSUPP;
144 goto out_unlock;
145 }
146 }
147 }
148
149 ap->target_lpm_policy = policy;
150 ata_port_schedule_eh(ap);
151out_unlock:
152 spin_unlock_irqrestore(ap->lock, flags);
153 return count;
154}
155
156static ssize_t ata_scsi_lpm_show(struct device *dev,
157 struct device_attribute *attr, char *buf)
158{
159 struct Scsi_Host *shost = class_to_shost(dev);
160 struct ata_port *ap = ata_shost_to_port(shost);
161
162 if (ap->target_lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
163 return -EINVAL;
164
165 return snprintf(buf, PAGE_SIZE, "%s\n",
166 ata_lpm_policy_names[ap->target_lpm_policy]);
167}
168DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
169 ata_scsi_lpm_show, ata_scsi_lpm_store);
170EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
171
172static ssize_t ata_scsi_park_show(struct device *device,
173 struct device_attribute *attr, char *buf)
174{
175 struct scsi_device *sdev = to_scsi_device(device);
176 struct ata_port *ap;
177 struct ata_link *link;
178 struct ata_device *dev;
179 unsigned long now;
180 unsigned int uninitialized_var(msecs);
181 int rc = 0;
182
183 ap = ata_shost_to_port(sdev->host);
184
185 spin_lock_irq(ap->lock);
186 dev = ata_scsi_find_dev(ap, sdev);
187 if (!dev) {
188 rc = -ENODEV;
189 goto unlock;
190 }
191 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
192 rc = -EOPNOTSUPP;
193 goto unlock;
194 }
195
196 link = dev->link;
197 now = jiffies;
198 if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS &&
199 link->eh_context.unloaded_mask & (1 << dev->devno) &&
200 time_after(dev->unpark_deadline, now))
201 msecs = jiffies_to_msecs(dev->unpark_deadline - now);
202 else
203 msecs = 0;
204
205unlock:
206 spin_unlock_irq(ap->lock);
207
208 return rc ? rc : snprintf(buf, 20, "%u\n", msecs);
209}
210
211static ssize_t ata_scsi_park_store(struct device *device,
212 struct device_attribute *attr,
213 const char *buf, size_t len)
214{
215 struct scsi_device *sdev = to_scsi_device(device);
216 struct ata_port *ap;
217 struct ata_device *dev;
218 long int input;
219 unsigned long flags;
220 int rc;
221
222 rc = kstrtol(buf, 10, &input);
223 if (rc)
224 return rc;
225 if (input < -2)
226 return -EINVAL;
227 if (input > ATA_TMOUT_MAX_PARK) {
228 rc = -EOVERFLOW;
229 input = ATA_TMOUT_MAX_PARK;
230 }
231
232 ap = ata_shost_to_port(sdev->host);
233
234 spin_lock_irqsave(ap->lock, flags);
235 dev = ata_scsi_find_dev(ap, sdev);
236 if (unlikely(!dev)) {
237 rc = -ENODEV;
238 goto unlock;
239 }
240 if (dev->class != ATA_DEV_ATA &&
241 dev->class != ATA_DEV_ZAC) {
242 rc = -EOPNOTSUPP;
243 goto unlock;
244 }
245
246 if (input >= 0) {
247 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
248 rc = -EOPNOTSUPP;
249 goto unlock;
250 }
251
252 dev->unpark_deadline = ata_deadline(jiffies, input);
253 dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK;
254 ata_port_schedule_eh(ap);
255 complete(&ap->park_req_pending);
256 } else {
257 switch (input) {
258 case -1:
259 dev->flags &= ~ATA_DFLAG_NO_UNLOAD;
260 break;
261 case -2:
262 dev->flags |= ATA_DFLAG_NO_UNLOAD;
263 break;
264 }
265 }
266unlock:
267 spin_unlock_irqrestore(ap->lock, flags);
268
269 return rc ? rc : len;
270}
271DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR,
272 ata_scsi_park_show, ata_scsi_park_store);
273EXPORT_SYMBOL_GPL(dev_attr_unload_heads);
274
275static ssize_t ata_ncq_prio_enable_show(struct device *device,
276 struct device_attribute *attr,
277 char *buf)
278{
279 struct scsi_device *sdev = to_scsi_device(device);
280 struct ata_port *ap;
281 struct ata_device *dev;
282 bool ncq_prio_enable;
283 int rc = 0;
284
285 ap = ata_shost_to_port(sdev->host);
286
287 spin_lock_irq(ap->lock);
288 dev = ata_scsi_find_dev(ap, sdev);
289 if (!dev) {
290 rc = -ENODEV;
291 goto unlock;
292 }
293
294 ncq_prio_enable = dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE;
295
296unlock:
297 spin_unlock_irq(ap->lock);
298
299 return rc ? rc : snprintf(buf, 20, "%u\n", ncq_prio_enable);
300}
301
302static ssize_t ata_ncq_prio_enable_store(struct device *device,
303 struct device_attribute *attr,
304 const char *buf, size_t len)
305{
306 struct scsi_device *sdev = to_scsi_device(device);
307 struct ata_port *ap;
308 struct ata_device *dev;
309 long int input;
310 int rc;
311
312 rc = kstrtol(buf, 10, &input);
313 if (rc)
314 return rc;
315 if ((input < 0) || (input > 1))
316 return -EINVAL;
317
318 ap = ata_shost_to_port(sdev->host);
319 dev = ata_scsi_find_dev(ap, sdev);
320 if (unlikely(!dev))
321 return -ENODEV;
322
323 spin_lock_irq(ap->lock);
324 if (input)
325 dev->flags |= ATA_DFLAG_NCQ_PRIO_ENABLE;
326 else
327 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
328
329 dev->link->eh_info.action |= ATA_EH_REVALIDATE;
330 dev->link->eh_info.flags |= ATA_EHI_QUIET;
331 ata_port_schedule_eh(ap);
332 spin_unlock_irq(ap->lock);
333
334 ata_port_wait_eh(ap);
335
336 if (input) {
337 spin_lock_irq(ap->lock);
338 if (!(dev->flags & ATA_DFLAG_NCQ_PRIO)) {
339 dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLE;
340 rc = -EIO;
341 }
342 spin_unlock_irq(ap->lock);
343 }
344
345 return rc ? rc : len;
346}
347
348DEVICE_ATTR(ncq_prio_enable, S_IRUGO | S_IWUSR,
349 ata_ncq_prio_enable_show, ata_ncq_prio_enable_store);
350EXPORT_SYMBOL_GPL(dev_attr_ncq_prio_enable);
351
352void ata_scsi_set_sense(struct ata_device *dev, struct scsi_cmnd *cmd,
353 u8 sk, u8 asc, u8 ascq)
354{
355 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
356
357 if (!cmd)
358 return;
359
360 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
361
362 scsi_build_sense_buffer(d_sense, cmd->sense_buffer, sk, asc, ascq);
363}
364
365void ata_scsi_set_sense_information(struct ata_device *dev,
366 struct scsi_cmnd *cmd,
367 const struct ata_taskfile *tf)
368{
369 u64 information;
370
371 if (!cmd)
372 return;
373
374 information = ata_tf_read_block(tf, dev);
375 if (information == U64_MAX)
376 return;
377
378 scsi_set_sense_information(cmd->sense_buffer,
379 SCSI_SENSE_BUFFERSIZE, information);
380}
381
382static void ata_scsi_set_invalid_field(struct ata_device *dev,
383 struct scsi_cmnd *cmd, u16 field, u8 bit)
384{
385 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x24, 0x0);
386
387 scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
388 field, bit, 1);
389}
390
391static void ata_scsi_set_invalid_parameter(struct ata_device *dev,
392 struct scsi_cmnd *cmd, u16 field)
393{
394
395 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x26, 0x0);
396 scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
397 field, 0xff, 0);
398}
399
400static ssize_t
401ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
402 const char *buf, size_t count)
403{
404 struct Scsi_Host *shost = class_to_shost(dev);
405 struct ata_port *ap = ata_shost_to_port(shost);
406 if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
407 return ap->ops->em_store(ap, buf, count);
408 return -EINVAL;
409}
410
411static ssize_t
412ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
413 char *buf)
414{
415 struct Scsi_Host *shost = class_to_shost(dev);
416 struct ata_port *ap = ata_shost_to_port(shost);
417
418 if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
419 return ap->ops->em_show(ap, buf);
420 return -EINVAL;
421}
422DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
423 ata_scsi_em_message_show, ata_scsi_em_message_store);
424EXPORT_SYMBOL_GPL(dev_attr_em_message);
425
426static ssize_t
427ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
428 char *buf)
429{
430 struct Scsi_Host *shost = class_to_shost(dev);
431 struct ata_port *ap = ata_shost_to_port(shost);
432
433 return snprintf(buf, 23, "%d\n", ap->em_message_type);
434}
435DEVICE_ATTR(em_message_type, S_IRUGO,
436 ata_scsi_em_message_type_show, NULL);
437EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
438
439static ssize_t
440ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
441 char *buf)
442{
443 struct scsi_device *sdev = to_scsi_device(dev);
444 struct ata_port *ap = ata_shost_to_port(sdev->host);
445 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
446
447 if (atadev && ap->ops->sw_activity_show &&
448 (ap->flags & ATA_FLAG_SW_ACTIVITY))
449 return ap->ops->sw_activity_show(atadev, buf);
450 return -EINVAL;
451}
452
453static ssize_t
454ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
455 const char *buf, size_t count)
456{
457 struct scsi_device *sdev = to_scsi_device(dev);
458 struct ata_port *ap = ata_shost_to_port(sdev->host);
459 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
460 enum sw_activity val;
461 int rc;
462
463 if (atadev && ap->ops->sw_activity_store &&
464 (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
465 val = simple_strtoul(buf, NULL, 0);
466 switch (val) {
467 case OFF: case BLINK_ON: case BLINK_OFF:
468 rc = ap->ops->sw_activity_store(atadev, val);
469 if (!rc)
470 return count;
471 else
472 return rc;
473 }
474 }
475 return -EINVAL;
476}
477DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
478 ata_scsi_activity_store);
479EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
480
481struct device_attribute *ata_common_sdev_attrs[] = {
482 &dev_attr_unload_heads,
483 &dev_attr_ncq_prio_enable,
484 NULL
485};
486EXPORT_SYMBOL_GPL(ata_common_sdev_attrs);
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
507 sector_t capacity, int geom[])
508{
509 geom[0] = 255;
510 geom[1] = 63;
511 sector_div(capacity, 255*63);
512 geom[2] = capacity;
513
514 return 0;
515}
516
517
518
519
520
521
522
523
524
525
526
527void ata_scsi_unlock_native_capacity(struct scsi_device *sdev)
528{
529 struct ata_port *ap = ata_shost_to_port(sdev->host);
530 struct ata_device *dev;
531 unsigned long flags;
532
533 spin_lock_irqsave(ap->lock, flags);
534
535 dev = ata_scsi_find_dev(ap, sdev);
536 if (dev && dev->n_sectors < dev->n_native_sectors) {
537 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
538 dev->link->eh_info.action |= ATA_EH_RESET;
539 ata_port_schedule_eh(ap);
540 }
541
542 spin_unlock_irqrestore(ap->lock, flags);
543 ata_port_wait_eh(ap);
544}
545
546
547
548
549
550
551
552
553
554
555
556
557
558static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
559 void __user *arg)
560{
561 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
562 u16 __user *dst = arg;
563 char buf[40];
564
565 if (!dev)
566 return -ENOMSG;
567
568 if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
569 return -EFAULT;
570
571 ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
572 if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
573 return -EFAULT;
574
575 ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
576 if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
577 return -EFAULT;
578
579 ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
580 if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
581 return -EFAULT;
582
583 return 0;
584}
585
586
587
588
589
590
591
592
593
594
595
596
597int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
598{
599 int rc = 0;
600 u8 sensebuf[SCSI_SENSE_BUFFERSIZE];
601 u8 scsi_cmd[MAX_COMMAND_SIZE];
602 u8 args[4], *argbuf = NULL;
603 int argsize = 0;
604 enum dma_data_direction data_dir;
605 struct scsi_sense_hdr sshdr;
606 int cmd_result;
607
608 if (arg == NULL)
609 return -EINVAL;
610
611 if (copy_from_user(args, arg, sizeof(args)))
612 return -EFAULT;
613
614 memset(sensebuf, 0, sizeof(sensebuf));
615 memset(scsi_cmd, 0, sizeof(scsi_cmd));
616
617 if (args[3]) {
618 argsize = ATA_SECT_SIZE * args[3];
619 argbuf = kmalloc(argsize, GFP_KERNEL);
620 if (argbuf == NULL) {
621 rc = -ENOMEM;
622 goto error;
623 }
624
625 scsi_cmd[1] = (4 << 1);
626 scsi_cmd[2] = 0x0e;
627
628 data_dir = DMA_FROM_DEVICE;
629 } else {
630 scsi_cmd[1] = (3 << 1);
631 scsi_cmd[2] = 0x20;
632 data_dir = DMA_NONE;
633 }
634
635 scsi_cmd[0] = ATA_16;
636
637 scsi_cmd[4] = args[2];
638 if (args[0] == ATA_CMD_SMART) {
639 scsi_cmd[6] = args[3];
640 scsi_cmd[8] = args[1];
641 scsi_cmd[10] = 0x4f;
642 scsi_cmd[12] = 0xc2;
643 } else {
644 scsi_cmd[6] = args[1];
645 }
646 scsi_cmd[14] = args[0];
647
648
649
650 cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
651 sensebuf, &sshdr, (10*HZ), 5, 0, 0, NULL);
652
653 if (driver_byte(cmd_result) == DRIVER_SENSE) {
654 u8 *desc = sensebuf + 8;
655 cmd_result &= ~(0xFF<<24);
656
657
658
659 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
660 if (sshdr.sense_key == RECOVERED_ERROR &&
661 sshdr.asc == 0 && sshdr.ascq == 0x1d)
662 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
663 }
664
665
666 if (sensebuf[0] == 0x72 &&
667 desc[0] == 0x09) {
668 args[0] = desc[13];
669 args[1] = desc[3];
670 args[2] = desc[5];
671 if (copy_to_user(arg, args, sizeof(args)))
672 rc = -EFAULT;
673 }
674 }
675
676
677 if (cmd_result) {
678 rc = -EIO;
679 goto error;
680 }
681
682 if ((argbuf)
683 && copy_to_user(arg + sizeof(args), argbuf, argsize))
684 rc = -EFAULT;
685error:
686 kfree(argbuf);
687 return rc;
688}
689
690
691
692
693
694
695
696
697
698
699
700
701int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
702{
703 int rc = 0;
704 u8 sensebuf[SCSI_SENSE_BUFFERSIZE];
705 u8 scsi_cmd[MAX_COMMAND_SIZE];
706 u8 args[7];
707 struct scsi_sense_hdr sshdr;
708 int cmd_result;
709
710 if (arg == NULL)
711 return -EINVAL;
712
713 if (copy_from_user(args, arg, sizeof(args)))
714 return -EFAULT;
715
716 memset(sensebuf, 0, sizeof(sensebuf));
717 memset(scsi_cmd, 0, sizeof(scsi_cmd));
718 scsi_cmd[0] = ATA_16;
719 scsi_cmd[1] = (3 << 1);
720 scsi_cmd[2] = 0x20;
721 scsi_cmd[4] = args[1];
722 scsi_cmd[6] = args[2];
723 scsi_cmd[8] = args[3];
724 scsi_cmd[10] = args[4];
725 scsi_cmd[12] = args[5];
726 scsi_cmd[13] = args[6] & 0x4f;
727 scsi_cmd[14] = args[0];
728
729
730
731 cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
732 sensebuf, &sshdr, (10*HZ), 5, 0, 0, NULL);
733
734 if (driver_byte(cmd_result) == DRIVER_SENSE) {
735 u8 *desc = sensebuf + 8;
736 cmd_result &= ~(0xFF<<24);
737
738
739
740 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
741 if (sshdr.sense_key == RECOVERED_ERROR &&
742 sshdr.asc == 0 && sshdr.ascq == 0x1d)
743 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
744 }
745
746
747 if (sensebuf[0] == 0x72 &&
748 desc[0] == 0x09) {
749 args[0] = desc[13];
750 args[1] = desc[3];
751 args[2] = desc[5];
752 args[3] = desc[7];
753 args[4] = desc[9];
754 args[5] = desc[11];
755 args[6] = desc[12];
756 if (copy_to_user(arg, args, sizeof(args)))
757 rc = -EFAULT;
758 }
759 }
760
761 if (cmd_result) {
762 rc = -EIO;
763 goto error;
764 }
765
766 error:
767 return rc;
768}
769
770static int ata_ioc32(struct ata_port *ap)
771{
772 if (ap->flags & ATA_FLAG_PIO_DMA)
773 return 1;
774 if (ap->pflags & ATA_PFLAG_PIO32)
775 return 1;
776 return 0;
777}
778
779int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
780 int cmd, void __user *arg)
781{
782 unsigned long val;
783 int rc = -EINVAL;
784 unsigned long flags;
785
786 switch (cmd) {
787 case HDIO_GET_32BIT:
788 spin_lock_irqsave(ap->lock, flags);
789 val = ata_ioc32(ap);
790 spin_unlock_irqrestore(ap->lock, flags);
791 return put_user(val, (unsigned long __user *)arg);
792
793 case HDIO_SET_32BIT:
794 val = (unsigned long) arg;
795 rc = 0;
796 spin_lock_irqsave(ap->lock, flags);
797 if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
798 if (val)
799 ap->pflags |= ATA_PFLAG_PIO32;
800 else
801 ap->pflags &= ~ATA_PFLAG_PIO32;
802 } else {
803 if (val != ata_ioc32(ap))
804 rc = -EINVAL;
805 }
806 spin_unlock_irqrestore(ap->lock, flags);
807 return rc;
808
809 case HDIO_GET_IDENTITY:
810 return ata_get_identity(ap, scsidev, arg);
811
812 case HDIO_DRIVE_CMD:
813 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
814 return -EACCES;
815 return ata_cmd_ioctl(scsidev, arg);
816
817 case HDIO_DRIVE_TASK:
818 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
819 return -EACCES;
820 return ata_task_ioctl(scsidev, arg);
821
822 default:
823 rc = -ENOTTY;
824 break;
825 }
826
827 return rc;
828}
829EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
830
831int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
832{
833 return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
834 scsidev, cmd, arg);
835}
836EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
858 struct scsi_cmnd *cmd)
859{
860 struct ata_queued_cmd *qc;
861
862 qc = ata_qc_new_init(dev, cmd->request->tag);
863 if (qc) {
864 qc->scsicmd = cmd;
865 qc->scsidone = cmd->scsi_done;
866
867 qc->sg = scsi_sglist(cmd);
868 qc->n_elem = scsi_sg_count(cmd);
869
870 if (cmd->request->rq_flags & RQF_QUIET)
871 qc->flags |= ATA_QCFLAG_QUIET;
872 } else {
873 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
874 cmd->scsi_done(cmd);
875 }
876
877 return qc;
878}
879
880static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
881{
882 struct scsi_cmnd *scmd = qc->scsicmd;
883
884 qc->extrabytes = scmd->request->extra_len;
885 qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
886}
887
888
889
890
891
892
893
894
895
896
897
898
899
900static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
901{
902 u8 stat = tf->command, err = tf->feature;
903
904 pr_warn("ata%u: status=0x%02x { ", id, stat);
905 if (stat & ATA_BUSY) {
906 pr_cont("Busy }\n");
907 } else {
908 if (stat & ATA_DRDY) pr_cont("DriveReady ");
909 if (stat & ATA_DF) pr_cont("DeviceFault ");
910 if (stat & ATA_DSC) pr_cont("SeekComplete ");
911 if (stat & ATA_DRQ) pr_cont("DataRequest ");
912 if (stat & ATA_CORR) pr_cont("CorrectedError ");
913 if (stat & ATA_SENSE) pr_cont("Sense ");
914 if (stat & ATA_ERR) pr_cont("Error ");
915 pr_cont("}\n");
916
917 if (err) {
918 pr_warn("ata%u: error=0x%02x { ", id, err);
919 if (err & ATA_ABORTED) pr_cont("DriveStatusError ");
920 if (err & ATA_ICRC) {
921 if (err & ATA_ABORTED)
922 pr_cont("BadCRC ");
923 else pr_cont("Sector ");
924 }
925 if (err & ATA_UNC) pr_cont("UncorrectableError ");
926 if (err & ATA_IDNF) pr_cont("SectorIdNotFound ");
927 if (err & ATA_TRK0NF) pr_cont("TrackZeroNotFound ");
928 if (err & ATA_AMNF) pr_cont("AddrMarkNotFound ");
929 pr_cont("}\n");
930 }
931 }
932}
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
952 u8 *asc, u8 *ascq, int verbose)
953{
954 int i;
955
956
957 static const unsigned char sense_table[][4] = {
958
959 {0xd1, ABORTED_COMMAND, 0x00, 0x00},
960
961
962 {0xd0, ABORTED_COMMAND, 0x00, 0x00},
963
964
965 {0x61, HARDWARE_ERROR, 0x00, 0x00},
966
967
968 {0x84, ABORTED_COMMAND, 0x47, 0x00},
969
970
971 {0x37, NOT_READY, 0x04, 0x00},
972
973
974 {0x09, NOT_READY, 0x04, 0x00},
975
976
977 {0x01, MEDIUM_ERROR, 0x13, 0x00},
978
979
980 {0x02, HARDWARE_ERROR, 0x00, 0x00},
981
982
983
984 {0x08, NOT_READY, 0x04, 0x00},
985
986
987 {0x10, ILLEGAL_REQUEST, 0x21, 0x00},
988
989
990 {0x20, UNIT_ATTENTION, 0x28, 0x00},
991
992
993 {0x40, MEDIUM_ERROR, 0x11, 0x04},
994
995
996 {0x80, MEDIUM_ERROR, 0x11, 0x04},
997
998 {0xFF, 0xFF, 0xFF, 0xFF},
999 };
1000 static const unsigned char stat_table[][4] = {
1001
1002 {0x80, ABORTED_COMMAND, 0x47, 0x00},
1003
1004 {0x40, ILLEGAL_REQUEST, 0x21, 0x04},
1005
1006 {0x20, HARDWARE_ERROR, 0x44, 0x00},
1007
1008 {0x08, ABORTED_COMMAND, 0x47, 0x00},
1009
1010 {0x04, RECOVERED_ERROR, 0x11, 0x00},
1011
1012 {0xFF, 0xFF, 0xFF, 0xFF},
1013 };
1014
1015
1016
1017
1018 if (drv_stat & ATA_BUSY) {
1019 drv_err = 0;
1020 }
1021
1022 if (drv_err) {
1023
1024 for (i = 0; sense_table[i][0] != 0xFF; i++) {
1025
1026 if ((sense_table[i][0] & drv_err) ==
1027 sense_table[i][0]) {
1028 *sk = sense_table[i][1];
1029 *asc = sense_table[i][2];
1030 *ascq = sense_table[i][3];
1031 goto translate_done;
1032 }
1033 }
1034 }
1035
1036
1037
1038
1039
1040
1041 for (i = 0; stat_table[i][0] != 0xFF; i++) {
1042 if (stat_table[i][0] & drv_stat) {
1043 *sk = stat_table[i][1];
1044 *asc = stat_table[i][2];
1045 *ascq = stat_table[i][3];
1046 goto translate_done;
1047 }
1048 }
1049
1050
1051
1052
1053
1054 *sk = ABORTED_COMMAND;
1055 *asc = 0x00;
1056 *ascq = 0x00;
1057
1058 translate_done:
1059 if (verbose)
1060 pr_err("ata%u: translated ATA stat/err 0x%02x/%02x to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
1061 id, drv_stat, drv_err, *sk, *asc, *ascq);
1062 return;
1063}
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
1083{
1084 struct scsi_cmnd *cmd = qc->scsicmd;
1085 struct ata_taskfile *tf = &qc->result_tf;
1086 unsigned char *sb = cmd->sense_buffer;
1087 unsigned char *desc = sb + 8;
1088 int verbose = qc->ap->ops->error_handler == NULL;
1089 u8 sense_key, asc, ascq;
1090
1091 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1092
1093 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
1094
1095
1096
1097
1098
1099 if (qc->err_mask ||
1100 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
1101 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
1102 &sense_key, &asc, &ascq, verbose);
1103 ata_scsi_set_sense(qc->dev, cmd, sense_key, asc, ascq);
1104 } else {
1105
1106
1107
1108
1109 scsi_build_sense_buffer(1, cmd->sense_buffer,
1110 RECOVERED_ERROR, 0, 0x1D);
1111 }
1112
1113 if ((cmd->sense_buffer[0] & 0x7f) >= 0x72) {
1114 u8 len;
1115
1116
1117 len = sb[7];
1118 desc = (char *)scsi_sense_desc_find(sb, len + 8, 9);
1119 if (!desc) {
1120 if (SCSI_SENSE_BUFFERSIZE < len + 14)
1121 return;
1122 sb[7] = len + 14;
1123 desc = sb + 8 + len;
1124 }
1125 desc[0] = 9;
1126 desc[1] = 12;
1127
1128
1129
1130 desc[2] = 0x00;
1131 desc[3] = tf->feature;
1132 desc[5] = tf->nsect;
1133 desc[7] = tf->lbal;
1134 desc[9] = tf->lbam;
1135 desc[11] = tf->lbah;
1136 desc[12] = tf->device;
1137 desc[13] = tf->command;
1138
1139
1140
1141
1142
1143 if (tf->flags & ATA_TFLAG_LBA48) {
1144 desc[2] |= 0x01;
1145 desc[4] = tf->hob_nsect;
1146 desc[6] = tf->hob_lbal;
1147 desc[8] = tf->hob_lbam;
1148 desc[10] = tf->hob_lbah;
1149 }
1150 } else {
1151
1152 desc[0] = tf->feature;
1153 desc[1] = tf->command;
1154 desc[2] = tf->device;
1155 desc[3] = tf->nsect;
1156 desc[7] = 0;
1157 if (tf->flags & ATA_TFLAG_LBA48) {
1158 desc[8] |= 0x80;
1159 if (tf->hob_nsect)
1160 desc[8] |= 0x40;
1161 if (tf->hob_lbal || tf->hob_lbam || tf->hob_lbah)
1162 desc[8] |= 0x20;
1163 }
1164 desc[9] = tf->lbal;
1165 desc[10] = tf->lbam;
1166 desc[11] = tf->lbah;
1167 }
1168}
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
1181{
1182 struct ata_device *dev = qc->dev;
1183 struct scsi_cmnd *cmd = qc->scsicmd;
1184 struct ata_taskfile *tf = &qc->result_tf;
1185 unsigned char *sb = cmd->sense_buffer;
1186 int verbose = qc->ap->ops->error_handler == NULL;
1187 u64 block;
1188 u8 sense_key, asc, ascq;
1189
1190 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1191
1192 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
1193
1194 if (ata_dev_disabled(dev)) {
1195
1196
1197 ata_scsi_set_sense(dev, cmd, NOT_READY, 0x04, 0x21);
1198 return;
1199 }
1200
1201
1202
1203 if (qc->err_mask ||
1204 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
1205 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
1206 &sense_key, &asc, &ascq, verbose);
1207 ata_scsi_set_sense(dev, cmd, sense_key, asc, ascq);
1208 } else {
1209
1210 ata_dev_warn(dev, "could not decode error status 0x%x err_mask 0x%x\n",
1211 tf->command, qc->err_mask);
1212 ata_scsi_set_sense(dev, cmd, ABORTED_COMMAND, 0, 0);
1213 return;
1214 }
1215
1216 block = ata_tf_read_block(&qc->result_tf, dev);
1217 if (block == U64_MAX)
1218 return;
1219
1220 scsi_set_sense_information(sb, SCSI_SENSE_BUFFERSIZE, block);
1221}
1222
1223static void ata_scsi_sdev_config(struct scsi_device *sdev)
1224{
1225 sdev->use_10_for_rw = 1;
1226 sdev->use_10_for_ms = 1;
1227 sdev->no_write_same = 1;
1228
1229
1230
1231
1232
1233
1234 sdev->max_device_blocked = 1;
1235}
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252static int atapi_drain_needed(struct request *rq)
1253{
1254 if (likely(!blk_rq_is_passthrough(rq)))
1255 return 0;
1256
1257 if (!blk_rq_bytes(rq) || op_is_write(req_op(rq)))
1258 return 0;
1259
1260 return atapi_cmd_type(scsi_req(rq)->cmd[0]) == ATAPI_MISC;
1261}
1262
1263static int ata_scsi_dev_config(struct scsi_device *sdev,
1264 struct ata_device *dev)
1265{
1266 struct request_queue *q = sdev->request_queue;
1267
1268 if (!ata_id_has_unload(dev->id))
1269 dev->flags |= ATA_DFLAG_NO_UNLOAD;
1270
1271
1272 blk_queue_max_hw_sectors(q, dev->max_sectors);
1273
1274 if (dev->class == ATA_DEV_ATAPI) {
1275 void *buf;
1276
1277 sdev->sector_size = ATA_SECT_SIZE;
1278
1279
1280 blk_queue_update_dma_pad(q, ATA_DMA_PAD_SZ - 1);
1281
1282
1283 buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
1284 if (!buf) {
1285 ata_dev_err(dev, "drain buffer allocation failed\n");
1286 return -ENOMEM;
1287 }
1288
1289 blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
1290 } else {
1291 sdev->sector_size = ata_id_logical_sector_size(dev->id);
1292 sdev->manage_start_stop = 1;
1293 }
1294
1295
1296
1297
1298
1299
1300
1301
1302 if (sdev->sector_size > PAGE_SIZE)
1303 ata_dev_warn(dev,
1304 "sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
1305 sdev->sector_size);
1306
1307 blk_queue_update_dma_alignment(q, sdev->sector_size - 1);
1308
1309 if (dev->flags & ATA_DFLAG_AN)
1310 set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
1311
1312 if (dev->flags & ATA_DFLAG_NCQ) {
1313 int depth;
1314
1315 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
1316 depth = min(ATA_MAX_QUEUE, depth);
1317 scsi_change_queue_depth(sdev, depth);
1318 }
1319
1320 blk_queue_flush_queueable(q, false);
1321
1322 if (dev->flags & ATA_DFLAG_TRUSTED)
1323 sdev->security_supported = 1;
1324
1325 dev->sdev = sdev;
1326 return 0;
1327}
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341int ata_scsi_slave_config(struct scsi_device *sdev)
1342{
1343 struct ata_port *ap = ata_shost_to_port(sdev->host);
1344 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
1345 int rc = 0;
1346
1347 ata_scsi_sdev_config(sdev);
1348
1349 if (dev)
1350 rc = ata_scsi_dev_config(sdev, dev);
1351
1352 return rc;
1353}
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369void ata_scsi_slave_destroy(struct scsi_device *sdev)
1370{
1371 struct ata_port *ap = ata_shost_to_port(sdev->host);
1372 struct request_queue *q = sdev->request_queue;
1373 unsigned long flags;
1374 struct ata_device *dev;
1375
1376 if (!ap->ops->error_handler)
1377 return;
1378
1379 spin_lock_irqsave(ap->lock, flags);
1380 dev = __ata_scsi_find_dev(ap, sdev);
1381 if (dev && dev->sdev) {
1382
1383 dev->sdev = NULL;
1384 dev->flags |= ATA_DFLAG_DETACH;
1385 ata_port_schedule_eh(ap);
1386 }
1387 spin_unlock_irqrestore(ap->lock, flags);
1388
1389 kfree(q->dma_drain_buffer);
1390 q->dma_drain_buffer = NULL;
1391 q->dma_drain_size = 0;
1392}
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404int __ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
1405 int queue_depth)
1406{
1407 struct ata_device *dev;
1408 unsigned long flags;
1409
1410 if (queue_depth < 1 || queue_depth == sdev->queue_depth)
1411 return sdev->queue_depth;
1412
1413 dev = ata_scsi_find_dev(ap, sdev);
1414 if (!dev || !ata_dev_enabled(dev))
1415 return sdev->queue_depth;
1416
1417
1418 spin_lock_irqsave(ap->lock, flags);
1419 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1420 if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
1421 dev->flags |= ATA_DFLAG_NCQ_OFF;
1422 queue_depth = 1;
1423 }
1424 spin_unlock_irqrestore(ap->lock, flags);
1425
1426
1427 queue_depth = min(queue_depth, sdev->host->can_queue);
1428 queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
1429 queue_depth = min(queue_depth, ATA_MAX_QUEUE);
1430
1431 if (sdev->queue_depth == queue_depth)
1432 return -EINVAL;
1433
1434 return scsi_change_queue_depth(sdev, queue_depth);
1435}
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
1453{
1454 struct ata_port *ap = ata_shost_to_port(sdev->host);
1455
1456 return __ata_change_queue_depth(ap, sdev, queue_depth);
1457}
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
1475{
1476 struct scsi_cmnd *scmd = qc->scsicmd;
1477 struct ata_taskfile *tf = &qc->tf;
1478 const u8 *cdb = scmd->cmnd;
1479 u16 fp;
1480 u8 bp = 0xff;
1481
1482 if (scmd->cmd_len < 5) {
1483 fp = 4;
1484 goto invalid_fld;
1485 }
1486
1487 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1488 tf->protocol = ATA_PROT_NODATA;
1489 if (cdb[1] & 0x1) {
1490 ;
1491 }
1492 if (cdb[4] & 0x2) {
1493 fp = 4;
1494 bp = 1;
1495 goto invalid_fld;
1496 }
1497 if (((cdb[4] >> 4) & 0xf) != 0) {
1498 fp = 4;
1499 bp = 3;
1500 goto invalid_fld;
1501 }
1502
1503 if (cdb[4] & 0x1) {
1504 tf->nsect = 1;
1505
1506 if (qc->dev->flags & ATA_DFLAG_LBA) {
1507 tf->flags |= ATA_TFLAG_LBA;
1508
1509 tf->lbah = 0x0;
1510 tf->lbam = 0x0;
1511 tf->lbal = 0x0;
1512 tf->device |= ATA_LBA;
1513 } else {
1514
1515 tf->lbal = 0x1;
1516 tf->lbam = 0x0;
1517 tf->lbah = 0x0;
1518 }
1519
1520 tf->command = ATA_CMD_VERIFY;
1521 } else {
1522
1523
1524
1525 if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
1526 system_state == SYSTEM_POWER_OFF)
1527 goto skip;
1528
1529 if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
1530 system_entering_hibernation())
1531 goto skip;
1532
1533
1534 tf->command = ATA_CMD_STANDBYNOW1;
1535 }
1536
1537
1538
1539
1540
1541
1542
1543
1544 return 0;
1545
1546 invalid_fld:
1547 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
1548 return 1;
1549 skip:
1550 scmd->result = SAM_STAT_GOOD;
1551 return 1;
1552}
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
1569{
1570 struct ata_taskfile *tf = &qc->tf;
1571
1572 tf->flags |= ATA_TFLAG_DEVICE;
1573 tf->protocol = ATA_PROT_NODATA;
1574
1575 if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
1576 tf->command = ATA_CMD_FLUSH_EXT;
1577 else
1578 tf->command = ATA_CMD_FLUSH;
1579
1580
1581 qc->flags |= ATA_QCFLAG_IO;
1582
1583 return 0;
1584}
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1597{
1598 u64 lba = 0;
1599 u32 len;
1600
1601 VPRINTK("six-byte command\n");
1602
1603 lba |= ((u64)(cdb[1] & 0x1f)) << 16;
1604 lba |= ((u64)cdb[2]) << 8;
1605 lba |= ((u64)cdb[3]);
1606
1607 len = cdb[4];
1608
1609 *plba = lba;
1610 *plen = len;
1611}
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1624{
1625 u64 lba = 0;
1626 u32 len = 0;
1627
1628 VPRINTK("ten-byte command\n");
1629
1630 lba |= ((u64)cdb[2]) << 24;
1631 lba |= ((u64)cdb[3]) << 16;
1632 lba |= ((u64)cdb[4]) << 8;
1633 lba |= ((u64)cdb[5]);
1634
1635 len |= ((u32)cdb[7]) << 8;
1636 len |= ((u32)cdb[8]);
1637
1638 *plba = lba;
1639 *plen = len;
1640}
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1653{
1654 u64 lba = 0;
1655 u32 len = 0;
1656
1657 VPRINTK("sixteen-byte command\n");
1658
1659 lba |= ((u64)cdb[2]) << 56;
1660 lba |= ((u64)cdb[3]) << 48;
1661 lba |= ((u64)cdb[4]) << 40;
1662 lba |= ((u64)cdb[5]) << 32;
1663 lba |= ((u64)cdb[6]) << 24;
1664 lba |= ((u64)cdb[7]) << 16;
1665 lba |= ((u64)cdb[8]) << 8;
1666 lba |= ((u64)cdb[9]);
1667
1668 len |= ((u32)cdb[10]) << 24;
1669 len |= ((u32)cdb[11]) << 16;
1670 len |= ((u32)cdb[12]) << 8;
1671 len |= ((u32)cdb[13]);
1672
1673 *plba = lba;
1674 *plen = len;
1675}
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
1690{
1691 struct scsi_cmnd *scmd = qc->scsicmd;
1692 struct ata_taskfile *tf = &qc->tf;
1693 struct ata_device *dev = qc->dev;
1694 u64 dev_sectors = qc->dev->n_sectors;
1695 const u8 *cdb = scmd->cmnd;
1696 u64 block;
1697 u32 n_block;
1698 u16 fp;
1699
1700 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1701 tf->protocol = ATA_PROT_NODATA;
1702
1703 if (cdb[0] == VERIFY) {
1704 if (scmd->cmd_len < 10) {
1705 fp = 9;
1706 goto invalid_fld;
1707 }
1708 scsi_10_lba_len(cdb, &block, &n_block);
1709 } else if (cdb[0] == VERIFY_16) {
1710 if (scmd->cmd_len < 16) {
1711 fp = 15;
1712 goto invalid_fld;
1713 }
1714 scsi_16_lba_len(cdb, &block, &n_block);
1715 } else {
1716 fp = 0;
1717 goto invalid_fld;
1718 }
1719
1720 if (!n_block)
1721 goto nothing_to_do;
1722 if (block >= dev_sectors)
1723 goto out_of_range;
1724 if ((block + n_block) > dev_sectors)
1725 goto out_of_range;
1726
1727 if (dev->flags & ATA_DFLAG_LBA) {
1728 tf->flags |= ATA_TFLAG_LBA;
1729
1730 if (lba_28_ok(block, n_block)) {
1731
1732 tf->command = ATA_CMD_VERIFY;
1733 tf->device |= (block >> 24) & 0xf;
1734 } else if (lba_48_ok(block, n_block)) {
1735 if (!(dev->flags & ATA_DFLAG_LBA48))
1736 goto out_of_range;
1737
1738
1739 tf->flags |= ATA_TFLAG_LBA48;
1740 tf->command = ATA_CMD_VERIFY_EXT;
1741
1742 tf->hob_nsect = (n_block >> 8) & 0xff;
1743
1744 tf->hob_lbah = (block >> 40) & 0xff;
1745 tf->hob_lbam = (block >> 32) & 0xff;
1746 tf->hob_lbal = (block >> 24) & 0xff;
1747 } else
1748
1749 goto out_of_range;
1750
1751 tf->nsect = n_block & 0xff;
1752
1753 tf->lbah = (block >> 16) & 0xff;
1754 tf->lbam = (block >> 8) & 0xff;
1755 tf->lbal = block & 0xff;
1756
1757 tf->device |= ATA_LBA;
1758 } else {
1759
1760 u32 sect, head, cyl, track;
1761
1762 if (!lba_28_ok(block, n_block))
1763 goto out_of_range;
1764
1765
1766 track = (u32)block / dev->sectors;
1767 cyl = track / dev->heads;
1768 head = track % dev->heads;
1769 sect = (u32)block % dev->sectors + 1;
1770
1771 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1772 (u32)block, track, cyl, head, sect);
1773
1774
1775
1776
1777
1778 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
1779 goto out_of_range;
1780
1781 tf->command = ATA_CMD_VERIFY;
1782 tf->nsect = n_block & 0xff;
1783 tf->lbal = sect;
1784 tf->lbam = cyl;
1785 tf->lbah = cyl >> 8;
1786 tf->device |= head;
1787 }
1788
1789 return 0;
1790
1791invalid_fld:
1792 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
1793 return 1;
1794
1795out_of_range:
1796 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
1797
1798 return 1;
1799
1800nothing_to_do:
1801 scmd->result = SAM_STAT_GOOD;
1802 return 1;
1803}
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
1824{
1825 struct scsi_cmnd *scmd = qc->scsicmd;
1826 const u8 *cdb = scmd->cmnd;
1827 struct request *rq = scmd->request;
1828 int class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
1829 unsigned int tf_flags = 0;
1830 u64 block;
1831 u32 n_block;
1832 int rc;
1833 u16 fp = 0;
1834
1835 if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
1836 tf_flags |= ATA_TFLAG_WRITE;
1837
1838
1839 switch (cdb[0]) {
1840 case READ_10:
1841 case WRITE_10:
1842 if (unlikely(scmd->cmd_len < 10)) {
1843 fp = 9;
1844 goto invalid_fld;
1845 }
1846 scsi_10_lba_len(cdb, &block, &n_block);
1847 if (cdb[1] & (1 << 3))
1848 tf_flags |= ATA_TFLAG_FUA;
1849 break;
1850 case READ_6:
1851 case WRITE_6:
1852 if (unlikely(scmd->cmd_len < 6)) {
1853 fp = 5;
1854 goto invalid_fld;
1855 }
1856 scsi_6_lba_len(cdb, &block, &n_block);
1857
1858
1859
1860
1861 if (!n_block)
1862 n_block = 256;
1863 break;
1864 case READ_16:
1865 case WRITE_16:
1866 if (unlikely(scmd->cmd_len < 16)) {
1867 fp = 15;
1868 goto invalid_fld;
1869 }
1870 scsi_16_lba_len(cdb, &block, &n_block);
1871 if (cdb[1] & (1 << 3))
1872 tf_flags |= ATA_TFLAG_FUA;
1873 break;
1874 default:
1875 DPRINTK("no-byte command\n");
1876 fp = 0;
1877 goto invalid_fld;
1878 }
1879
1880
1881 if (!n_block)
1882
1883
1884
1885
1886
1887
1888
1889 goto nothing_to_do;
1890
1891 qc->flags |= ATA_QCFLAG_IO;
1892 qc->nbytes = n_block * scmd->device->sector_size;
1893
1894 rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
1895 qc->hw_tag, class);
1896
1897 if (likely(rc == 0))
1898 return 0;
1899
1900 if (rc == -ERANGE)
1901 goto out_of_range;
1902
1903invalid_fld:
1904 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
1905 return 1;
1906
1907out_of_range:
1908 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
1909
1910 return 1;
1911
1912nothing_to_do:
1913 scmd->result = SAM_STAT_GOOD;
1914 return 1;
1915}
1916
1917static void ata_qc_done(struct ata_queued_cmd *qc)
1918{
1919 struct scsi_cmnd *cmd = qc->scsicmd;
1920 void (*done)(struct scsi_cmnd *) = qc->scsidone;
1921
1922 ata_qc_free(qc);
1923 done(cmd);
1924}
1925
1926static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
1927{
1928 struct ata_port *ap = qc->ap;
1929 struct scsi_cmnd *cmd = qc->scsicmd;
1930 u8 *cdb = cmd->cmnd;
1931 int need_sense = (qc->err_mask != 0);
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
1943 ((cdb[2] & 0x20) || need_sense))
1944 ata_gen_passthru_sense(qc);
1945 else if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1946 cmd->result = SAM_STAT_CHECK_CONDITION;
1947 else if (need_sense)
1948 ata_gen_ata_sense(qc);
1949 else
1950 cmd->result = SAM_STAT_GOOD;
1951
1952 if (need_sense && !ap->ops->error_handler)
1953 ata_dump_status(ap->print_id, &qc->result_tf);
1954
1955 ata_qc_done(qc);
1956}
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
1985 ata_xlat_func_t xlat_func)
1986{
1987 struct ata_port *ap = dev->link->ap;
1988 struct ata_queued_cmd *qc;
1989 int rc;
1990
1991 VPRINTK("ENTER\n");
1992
1993 qc = ata_scsi_qc_new(dev, cmd);
1994 if (!qc)
1995 goto err_mem;
1996
1997
1998 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1999 cmd->sc_data_direction == DMA_TO_DEVICE) {
2000 if (unlikely(scsi_bufflen(cmd) < 1)) {
2001 ata_dev_warn(dev, "WARNING: zero len r/w req\n");
2002 goto err_did;
2003 }
2004
2005 ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
2006
2007 qc->dma_dir = cmd->sc_data_direction;
2008 }
2009
2010 qc->complete_fn = ata_scsi_qc_complete;
2011
2012 if (xlat_func(qc))
2013 goto early_finish;
2014
2015 if (ap->ops->qc_defer) {
2016 if ((rc = ap->ops->qc_defer(qc)))
2017 goto defer;
2018 }
2019
2020
2021 ata_qc_issue(qc);
2022
2023 VPRINTK("EXIT\n");
2024 return 0;
2025
2026early_finish:
2027 ata_qc_free(qc);
2028 cmd->scsi_done(cmd);
2029 DPRINTK("EXIT - early finish (good or error)\n");
2030 return 0;
2031
2032err_did:
2033 ata_qc_free(qc);
2034 cmd->result = (DID_ERROR << 16);
2035 cmd->scsi_done(cmd);
2036err_mem:
2037 DPRINTK("EXIT - internal\n");
2038 return 0;
2039
2040defer:
2041 ata_qc_free(qc);
2042 DPRINTK("EXIT - defer\n");
2043 if (rc == ATA_DEFER_LINK)
2044 return SCSI_MLQUEUE_DEVICE_BUSY;
2045 else
2046 return SCSI_MLQUEUE_HOST_BUSY;
2047}
2048
2049struct ata_scsi_args {
2050 struct ata_device *dev;
2051 u16 *id;
2052 struct scsi_cmnd *cmd;
2053};
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in,
2070 unsigned long *flags)
2071{
2072 spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags);
2073
2074 memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
2075 if (copy_in)
2076 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
2077 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
2078 return ata_scsi_rbuf;
2079}
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out,
2094 unsigned long *flags)
2095{
2096 if (copy_out)
2097 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
2098 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
2099 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags);
2100}
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
2118 unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
2119{
2120 u8 *rbuf;
2121 unsigned int rc;
2122 struct scsi_cmnd *cmd = args->cmd;
2123 unsigned long flags;
2124
2125 rbuf = ata_scsi_rbuf_get(cmd, false, &flags);
2126 rc = actor(args, rbuf);
2127 ata_scsi_rbuf_put(cmd, rc == 0, &flags);
2128
2129 if (rc == 0)
2130 cmd->result = SAM_STAT_GOOD;
2131}
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
2145{
2146 static const u8 versions[] = {
2147 0x00,
2148 0x60,
2149
2150 0x03,
2151 0x20,
2152
2153 0x03,
2154 0x00
2155 };
2156 static const u8 versions_zbc[] = {
2157 0x00,
2158 0xA0,
2159
2160 0x06,
2161 0x00,
2162
2163 0x05,
2164 0xC0,
2165
2166 0x60,
2167 0x24,
2168 };
2169
2170 u8 hdr[] = {
2171 TYPE_DISK,
2172 0,
2173 0x5,
2174 2,
2175 95 - 4,
2176 0,
2177 0,
2178 2
2179 };
2180
2181 VPRINTK("ENTER\n");
2182
2183
2184
2185
2186 if (ata_id_removable(args->id) ||
2187 (args->dev->link->ap->pflags & ATA_PFLAG_EXTERNAL))
2188 hdr[1] |= (1 << 7);
2189
2190 if (args->dev->class == ATA_DEV_ZAC) {
2191 hdr[0] = TYPE_ZBC;
2192 hdr[2] = 0x7;
2193 }
2194
2195 memcpy(rbuf, hdr, sizeof(hdr));
2196 memcpy(&rbuf[8], "ATA ", 8);
2197 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
2198
2199
2200 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV + 2, 4);
2201 if (strncmp(&rbuf[32], " ", 4) == 0)
2202 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
2203
2204 if (rbuf[32] == 0 || rbuf[32] == ' ')
2205 memcpy(&rbuf[32], "n/a ", 4);
2206
2207 if (ata_id_zoned_cap(args->id) || args->dev->class == ATA_DEV_ZAC)
2208 memcpy(rbuf + 58, versions_zbc, sizeof(versions_zbc));
2209 else
2210 memcpy(rbuf + 58, versions, sizeof(versions));
2211
2212 return 0;
2213}
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
2226{
2227 int num_pages;
2228 static const u8 pages[] = {
2229 0x00,
2230 0x80,
2231 0x83,
2232 0x89,
2233 0xb0,
2234 0xb1,
2235 0xb2,
2236 0xb6,
2237 };
2238
2239 num_pages = sizeof(pages);
2240 if (!(args->dev->flags & ATA_DFLAG_ZAC))
2241 num_pages--;
2242 rbuf[3] = num_pages;
2243 memcpy(rbuf + 4, pages, num_pages);
2244 return 0;
2245}
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf)
2258{
2259 static const u8 hdr[] = {
2260 0,
2261 0x80,
2262 0,
2263 ATA_ID_SERNO_LEN,
2264 };
2265
2266 memcpy(rbuf, hdr, sizeof(hdr));
2267 ata_id_string(args->id, (unsigned char *) &rbuf[4],
2268 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
2269 return 0;
2270}
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
2286{
2287 const int sat_model_serial_desc_len = 68;
2288 int num;
2289
2290 rbuf[1] = 0x83;
2291 num = 4;
2292
2293
2294 rbuf[num + 0] = 2;
2295 rbuf[num + 3] = ATA_ID_SERNO_LEN;
2296 num += 4;
2297 ata_id_string(args->id, (unsigned char *) rbuf + num,
2298 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
2299 num += ATA_ID_SERNO_LEN;
2300
2301
2302
2303 rbuf[num + 0] = 2;
2304 rbuf[num + 1] = 1;
2305 rbuf[num + 3] = sat_model_serial_desc_len;
2306 num += 4;
2307 memcpy(rbuf + num, "ATA ", 8);
2308 num += 8;
2309 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
2310 ATA_ID_PROD_LEN);
2311 num += ATA_ID_PROD_LEN;
2312 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
2313 ATA_ID_SERNO_LEN);
2314 num += ATA_ID_SERNO_LEN;
2315
2316 if (ata_id_has_wwn(args->id)) {
2317
2318
2319 rbuf[num + 0] = 1;
2320 rbuf[num + 1] = 3;
2321 rbuf[num + 3] = ATA_ID_WWN_LEN;
2322 num += 4;
2323 ata_id_string(args->id, (unsigned char *) rbuf + num,
2324 ATA_ID_WWN, ATA_ID_WWN_LEN);
2325 num += ATA_ID_WWN_LEN;
2326 }
2327 rbuf[3] = num - 4;
2328 return 0;
2329}
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
2342{
2343 struct ata_taskfile tf;
2344
2345 memset(&tf, 0, sizeof(tf));
2346
2347 rbuf[1] = 0x89;
2348 rbuf[2] = (0x238 >> 8);
2349 rbuf[3] = (0x238 & 0xff);
2350
2351 memcpy(&rbuf[8], "linux ", 8);
2352 memcpy(&rbuf[16], "libata ", 16);
2353 memcpy(&rbuf[32], DRV_VERSION, 4);
2354
2355
2356
2357 tf.command = ATA_DRDY;
2358 tf.lbal = 0x1;
2359 tf.nsect = 0x1;
2360
2361 ata_tf_to_fis(&tf, 0, 1, &rbuf[36]);
2362 rbuf[36] = 0x34;
2363
2364 rbuf[56] = ATA_CMD_ID_ATA;
2365
2366 memcpy(&rbuf[60], &args->id[0], 512);
2367 return 0;
2368}
2369
2370static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
2371{
2372 u16 min_io_sectors;
2373
2374 rbuf[1] = 0xb0;
2375 rbuf[3] = 0x3c;
2376
2377
2378
2379
2380
2381
2382
2383
2384 min_io_sectors = 1 << ata_id_log2_per_physical_sector(args->id);
2385 put_unaligned_be16(min_io_sectors, &rbuf[6]);
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396 if (ata_id_has_trim(args->id)) {
2397 put_unaligned_be64(65535 * ATA_MAX_TRIM_RNUM, &rbuf[36]);
2398 put_unaligned_be32(1, &rbuf[28]);
2399 }
2400
2401 return 0;
2402}
2403
2404static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
2405{
2406 int form_factor = ata_id_form_factor(args->id);
2407 int media_rotation_rate = ata_id_rotation_rate(args->id);
2408 u8 zoned = ata_id_zoned_cap(args->id);
2409
2410 rbuf[1] = 0xb1;
2411 rbuf[3] = 0x3c;
2412 rbuf[4] = media_rotation_rate >> 8;
2413 rbuf[5] = media_rotation_rate;
2414 rbuf[7] = form_factor;
2415 if (zoned)
2416 rbuf[8] = (zoned << 4);
2417
2418 return 0;
2419}
2420
2421static unsigned int ata_scsiop_inq_b2(struct ata_scsi_args *args, u8 *rbuf)
2422{
2423
2424 rbuf[1] = 0xb2;
2425 rbuf[3] = 0x4;
2426 rbuf[5] = 1 << 6;
2427
2428 return 0;
2429}
2430
2431static unsigned int ata_scsiop_inq_b6(struct ata_scsi_args *args, u8 *rbuf)
2432{
2433
2434
2435
2436 rbuf[1] = 0xb6;
2437 rbuf[3] = 0x3C;
2438
2439
2440
2441
2442 if (args->dev->zac_zoned_cap & 1)
2443 rbuf[4] |= 1;
2444 put_unaligned_be32(args->dev->zac_zones_optimal_open, &rbuf[8]);
2445 put_unaligned_be32(args->dev->zac_zones_optimal_nonseq, &rbuf[12]);
2446 put_unaligned_be32(args->dev->zac_zones_max_open, &rbuf[16]);
2447
2448 return 0;
2449}
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464static void modecpy(u8 *dest, const u8 *src, int n, bool changeable)
2465{
2466 if (changeable) {
2467 memcpy(dest, src, 2);
2468 memset(dest + 2, 0, n - 2);
2469 } else {
2470 memcpy(dest, src, n);
2471 }
2472}
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable)
2488{
2489 modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable);
2490 if (changeable) {
2491 buf[2] |= (1 << 2);
2492 } else {
2493 buf[2] |= (ata_id_wcache_enabled(id) << 2);
2494 buf[12] |= (!ata_id_rahead_enabled(id) << 5);
2495 }
2496 return sizeof(def_cache_mpage);
2497}
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510static unsigned int ata_msense_control(struct ata_device *dev, u8 *buf,
2511 bool changeable)
2512{
2513 modecpy(buf, def_control_mpage, sizeof(def_control_mpage), changeable);
2514 if (changeable) {
2515 buf[2] |= (1 << 2);
2516 } else {
2517 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
2518
2519 buf[2] |= (d_sense << 2);
2520 }
2521 return sizeof(def_control_mpage);
2522}
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable)
2535{
2536 modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage),
2537 changeable);
2538 return sizeof(def_rw_recovery_mpage);
2539}
2540
2541
2542
2543
2544
2545static int ata_dev_supports_fua(u16 *id)
2546{
2547 unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
2548
2549 if (!libata_fua)
2550 return 0;
2551 if (!ata_id_has_fua(id))
2552 return 0;
2553
2554 ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
2555 ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
2556
2557 if (strcmp(model, "Maxtor"))
2558 return 1;
2559 if (strcmp(fw, "BANC1G10"))
2560 return 1;
2561
2562 return 0;
2563}
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
2578{
2579 struct ata_device *dev = args->dev;
2580 u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
2581 static const u8 sat_blk_desc[] = {
2582 0, 0, 0, 0,
2583 0,
2584 0, 0x2, 0x0
2585 };
2586 u8 pg, spg;
2587 unsigned int ebd, page_control, six_byte;
2588 u8 dpofua, bp = 0xff;
2589 u16 fp;
2590
2591 VPRINTK("ENTER\n");
2592
2593 six_byte = (scsicmd[0] == MODE_SENSE);
2594 ebd = !(scsicmd[1] & 0x8);
2595
2596
2597
2598
2599 page_control = scsicmd[2] >> 6;
2600 switch (page_control) {
2601 case 0:
2602 case 1:
2603 case 2:
2604 break;
2605 case 3:
2606 goto saving_not_supp;
2607 default:
2608 fp = 2;
2609 bp = 6;
2610 goto invalid_fld;
2611 }
2612
2613 if (six_byte)
2614 p += 4 + (ebd ? 8 : 0);
2615 else
2616 p += 8 + (ebd ? 8 : 0);
2617
2618 pg = scsicmd[2] & 0x3f;
2619 spg = scsicmd[3];
2620
2621
2622
2623
2624 if (spg && (spg != ALL_SUB_MPAGES)) {
2625 fp = 3;
2626 goto invalid_fld;
2627 }
2628
2629 switch(pg) {
2630 case RW_RECOVERY_MPAGE:
2631 p += ata_msense_rw_recovery(p, page_control == 1);
2632 break;
2633
2634 case CACHE_MPAGE:
2635 p += ata_msense_caching(args->id, p, page_control == 1);
2636 break;
2637
2638 case CONTROL_MPAGE:
2639 p += ata_msense_control(args->dev, p, page_control == 1);
2640 break;
2641
2642 case ALL_MPAGES:
2643 p += ata_msense_rw_recovery(p, page_control == 1);
2644 p += ata_msense_caching(args->id, p, page_control == 1);
2645 p += ata_msense_control(args->dev, p, page_control == 1);
2646 break;
2647
2648 default:
2649 fp = 2;
2650 goto invalid_fld;
2651 }
2652
2653 dpofua = 0;
2654 if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
2655 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
2656 dpofua = 1 << 4;
2657
2658 if (six_byte) {
2659 rbuf[0] = p - rbuf - 1;
2660 rbuf[2] |= dpofua;
2661 if (ebd) {
2662 rbuf[3] = sizeof(sat_blk_desc);
2663 memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
2664 }
2665 } else {
2666 unsigned int output_len = p - rbuf - 2;
2667
2668 rbuf[0] = output_len >> 8;
2669 rbuf[1] = output_len;
2670 rbuf[3] |= dpofua;
2671 if (ebd) {
2672 rbuf[7] = sizeof(sat_blk_desc);
2673 memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
2674 }
2675 }
2676 return 0;
2677
2678invalid_fld:
2679 ata_scsi_set_invalid_field(dev, args->cmd, fp, bp);
2680 return 1;
2681
2682saving_not_supp:
2683 ata_scsi_set_sense(dev, args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
2684
2685 return 1;
2686}
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
2699{
2700 struct ata_device *dev = args->dev;
2701 u64 last_lba = dev->n_sectors - 1;
2702 u32 sector_size;
2703 u8 log2_per_phys;
2704 u16 lowest_aligned;
2705
2706 sector_size = ata_id_logical_sector_size(dev->id);
2707 log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
2708 lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
2709
2710 VPRINTK("ENTER\n");
2711
2712 if (args->cmd->cmnd[0] == READ_CAPACITY) {
2713 if (last_lba >= 0xffffffffULL)
2714 last_lba = 0xffffffff;
2715
2716
2717 rbuf[0] = last_lba >> (8 * 3);
2718 rbuf[1] = last_lba >> (8 * 2);
2719 rbuf[2] = last_lba >> (8 * 1);
2720 rbuf[3] = last_lba;
2721
2722
2723 rbuf[4] = sector_size >> (8 * 3);
2724 rbuf[5] = sector_size >> (8 * 2);
2725 rbuf[6] = sector_size >> (8 * 1);
2726 rbuf[7] = sector_size;
2727 } else {
2728
2729 rbuf[0] = last_lba >> (8 * 7);
2730 rbuf[1] = last_lba >> (8 * 6);
2731 rbuf[2] = last_lba >> (8 * 5);
2732 rbuf[3] = last_lba >> (8 * 4);
2733 rbuf[4] = last_lba >> (8 * 3);
2734 rbuf[5] = last_lba >> (8 * 2);
2735 rbuf[6] = last_lba >> (8 * 1);
2736 rbuf[7] = last_lba;
2737
2738
2739 rbuf[ 8] = sector_size >> (8 * 3);
2740 rbuf[ 9] = sector_size >> (8 * 2);
2741 rbuf[10] = sector_size >> (8 * 1);
2742 rbuf[11] = sector_size;
2743
2744 rbuf[12] = 0;
2745 rbuf[13] = log2_per_phys;
2746 rbuf[14] = (lowest_aligned >> 8) & 0x3f;
2747 rbuf[15] = lowest_aligned;
2748
2749 if (ata_id_has_trim(args->id) &&
2750 !(dev->horkage & ATA_HORKAGE_NOTRIM)) {
2751 rbuf[14] |= 0x80;
2752
2753 if (ata_id_has_zero_after_trim(args->id) &&
2754 dev->horkage & ATA_HORKAGE_ZERO_AFTER_TRIM) {
2755 ata_dev_info(dev, "Enabling discard_zeroes_data\n");
2756 rbuf[14] |= 0x40;
2757 }
2758 }
2759 if (ata_id_zoned_cap(args->id) ||
2760 args->dev->class == ATA_DEV_ZAC)
2761 rbuf[12] = (1 << 4);
2762 }
2763 return 0;
2764}
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
2777{
2778 VPRINTK("ENTER\n");
2779 rbuf[3] = 8;
2780
2781 return 0;
2782}
2783
2784static void atapi_sense_complete(struct ata_queued_cmd *qc)
2785{
2786 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
2787
2788
2789
2790
2791
2792 ata_gen_passthru_sense(qc);
2793 }
2794
2795 ata_qc_done(qc);
2796}
2797
2798
2799static inline int ata_pio_use_silly(struct ata_port *ap)
2800{
2801 return (ap->flags & ATA_FLAG_PIO_DMA);
2802}
2803
2804static void atapi_request_sense(struct ata_queued_cmd *qc)
2805{
2806 struct ata_port *ap = qc->ap;
2807 struct scsi_cmnd *cmd = qc->scsicmd;
2808
2809 DPRINTK("ATAPI request sense\n");
2810
2811 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
2812
2813#ifdef CONFIG_ATA_SFF
2814 if (ap->ops->sff_tf_read)
2815 ap->ops->sff_tf_read(ap, &qc->tf);
2816#endif
2817
2818
2819 cmd->sense_buffer[0] = 0x70;
2820 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2821
2822 ata_qc_reinit(qc);
2823
2824
2825 sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
2826 ata_sg_init(qc, &qc->sgent, 1);
2827 qc->dma_dir = DMA_FROM_DEVICE;
2828
2829 memset(&qc->cdb, 0, qc->dev->cdb_len);
2830 qc->cdb[0] = REQUEST_SENSE;
2831 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2832
2833 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2834 qc->tf.command = ATA_CMD_PACKET;
2835
2836 if (ata_pio_use_silly(ap)) {
2837 qc->tf.protocol = ATAPI_PROT_DMA;
2838 qc->tf.feature |= ATAPI_PKT_DMA;
2839 } else {
2840 qc->tf.protocol = ATAPI_PROT_PIO;
2841 qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
2842 qc->tf.lbah = 0;
2843 }
2844 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2845
2846 qc->complete_fn = atapi_sense_complete;
2847
2848 ata_qc_issue(qc);
2849
2850 DPRINTK("EXIT\n");
2851}
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861static void atapi_fixup_inquiry(struct scsi_cmnd *cmd)
2862{
2863 u8 buf[4];
2864
2865 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
2866 if (buf[2] == 0) {
2867 buf[2] = 0x5;
2868 buf[3] = 0x32;
2869 }
2870 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
2871}
2872
2873static void atapi_qc_complete(struct ata_queued_cmd *qc)
2874{
2875 struct scsi_cmnd *cmd = qc->scsicmd;
2876 unsigned int err_mask = qc->err_mask;
2877
2878 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
2879
2880
2881 if (unlikely(qc->ap->ops->error_handler &&
2882 (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
2883
2884 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
2885
2886
2887
2888
2889
2890 ata_gen_passthru_sense(qc);
2891 }
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
2907 qc->dev->sdev->locked = 0;
2908
2909 qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
2910 ata_qc_done(qc);
2911 return;
2912 }
2913
2914
2915 if (unlikely(err_mask & AC_ERR_DEV)) {
2916 cmd->result = SAM_STAT_CHECK_CONDITION;
2917 atapi_request_sense(qc);
2918 return;
2919 } else if (unlikely(err_mask)) {
2920
2921
2922
2923
2924
2925 ata_gen_passthru_sense(qc);
2926 } else {
2927 if (cmd->cmnd[0] == INQUIRY && (cmd->cmnd[1] & 0x03) == 0)
2928 atapi_fixup_inquiry(cmd);
2929 cmd->result = SAM_STAT_GOOD;
2930 }
2931
2932 ata_qc_done(qc);
2933}
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
2945{
2946 struct scsi_cmnd *scmd = qc->scsicmd;
2947 struct ata_device *dev = qc->dev;
2948 int nodata = (scmd->sc_data_direction == DMA_NONE);
2949 int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
2950 unsigned int nbytes;
2951
2952 memset(qc->cdb, 0, dev->cdb_len);
2953 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
2954
2955 qc->complete_fn = atapi_qc_complete;
2956
2957 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2958 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
2959 qc->tf.flags |= ATA_TFLAG_WRITE;
2960 DPRINTK("direction: write\n");
2961 }
2962
2963 qc->tf.command = ATA_CMD_PACKET;
2964 ata_qc_set_pc_nbytes(qc);
2965
2966
2967 if (!nodata && !using_pio && atapi_check_dma(qc))
2968 using_pio = 1;
2969
2970
2971
2972
2973
2974
2975 nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001 if (nbytes & 0x1)
3002 nbytes++;
3003
3004 qc->tf.lbam = (nbytes & 0xFF);
3005 qc->tf.lbah = (nbytes >> 8);
3006
3007 if (nodata)
3008 qc->tf.protocol = ATAPI_PROT_NODATA;
3009 else if (using_pio)
3010 qc->tf.protocol = ATAPI_PROT_PIO;
3011 else {
3012
3013 qc->tf.protocol = ATAPI_PROT_DMA;
3014 qc->tf.feature |= ATAPI_PKT_DMA;
3015
3016 if ((dev->flags & ATA_DFLAG_DMADIR) &&
3017 (scmd->sc_data_direction != DMA_TO_DEVICE))
3018
3019 qc->tf.feature |= ATAPI_DMADIR;
3020 }
3021
3022
3023
3024
3025 return 0;
3026}
3027
3028static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
3029{
3030 if (!sata_pmp_attached(ap)) {
3031 if (likely(devno >= 0 &&
3032 devno < ata_link_max_devices(&ap->link)))
3033 return &ap->link.device[devno];
3034 } else {
3035 if (likely(devno >= 0 &&
3036 devno < ap->nr_pmp_links))
3037 return &ap->pmp_link[devno].device[0];
3038 }
3039
3040 return NULL;
3041}
3042
3043static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
3044 const struct scsi_device *scsidev)
3045{
3046 int devno;
3047
3048
3049 if (!sata_pmp_attached(ap)) {
3050 if (unlikely(scsidev->channel || scsidev->lun))
3051 return NULL;
3052 devno = scsidev->id;
3053 } else {
3054 if (unlikely(scsidev->id || scsidev->lun))
3055 return NULL;
3056 devno = scsidev->channel;
3057 }
3058
3059 return ata_find_dev(ap, devno);
3060}
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078static struct ata_device *
3079ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
3080{
3081 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
3082
3083 if (unlikely(!dev || !ata_dev_enabled(dev)))
3084 return NULL;
3085
3086 return dev;
3087}
3088
3089
3090
3091
3092
3093
3094
3095
3096static u8
3097ata_scsi_map_proto(u8 byte1)
3098{
3099 switch((byte1 & 0x1e) >> 1) {
3100 case 3:
3101 return ATA_PROT_NODATA;
3102
3103 case 6:
3104 case 10:
3105 case 11:
3106 return ATA_PROT_DMA;
3107
3108 case 4:
3109 case 5:
3110 return ATA_PROT_PIO;
3111
3112 case 12:
3113 return ATA_PROT_NCQ;
3114
3115 case 0:
3116 case 1:
3117 case 8:
3118 case 9:
3119 case 7:
3120 case 15:
3121 default:
3122 break;
3123 }
3124
3125 return ATA_PROT_UNKNOWN;
3126}
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
3138{
3139 struct ata_taskfile *tf = &(qc->tf);
3140 struct scsi_cmnd *scmd = qc->scsicmd;
3141 struct ata_device *dev = qc->dev;
3142 const u8 *cdb = scmd->cmnd;
3143 u16 fp;
3144 u16 cdb_offset = 0;
3145
3146
3147 if (cdb[0] == VARIABLE_LENGTH_CMD)
3148 cdb_offset = 9;
3149
3150 tf->protocol = ata_scsi_map_proto(cdb[1 + cdb_offset]);
3151 if (tf->protocol == ATA_PROT_UNKNOWN) {
3152 fp = 1;
3153 goto invalid_fld;
3154 }
3155
3156 if (ata_is_ncq(tf->protocol) && (cdb[2 + cdb_offset] & 0x3) == 0)
3157 tf->protocol = ATA_PROT_NCQ_NODATA;
3158
3159
3160 tf->flags |= ATA_TFLAG_LBA;
3161
3162
3163
3164
3165
3166 if (cdb[0] == ATA_16) {
3167
3168
3169
3170
3171
3172 if (cdb[1] & 0x01) {
3173 tf->hob_feature = cdb[3];
3174 tf->hob_nsect = cdb[5];
3175 tf->hob_lbal = cdb[7];
3176 tf->hob_lbam = cdb[9];
3177 tf->hob_lbah = cdb[11];
3178 tf->flags |= ATA_TFLAG_LBA48;
3179 } else
3180 tf->flags &= ~ATA_TFLAG_LBA48;
3181
3182
3183
3184
3185 tf->feature = cdb[4];
3186 tf->nsect = cdb[6];
3187 tf->lbal = cdb[8];
3188 tf->lbam = cdb[10];
3189 tf->lbah = cdb[12];
3190 tf->device = cdb[13];
3191 tf->command = cdb[14];
3192 } else if (cdb[0] == ATA_12) {
3193
3194
3195
3196 tf->flags &= ~ATA_TFLAG_LBA48;
3197
3198 tf->feature = cdb[3];
3199 tf->nsect = cdb[4];
3200 tf->lbal = cdb[5];
3201 tf->lbam = cdb[6];
3202 tf->lbah = cdb[7];
3203 tf->device = cdb[8];
3204 tf->command = cdb[9];
3205 } else {
3206
3207
3208
3209
3210
3211 if (cdb[10] & 0x01) {
3212 tf->hob_feature = cdb[20];
3213 tf->hob_nsect = cdb[22];
3214 tf->hob_lbal = cdb[16];
3215 tf->hob_lbam = cdb[15];
3216 tf->hob_lbah = cdb[14];
3217 tf->flags |= ATA_TFLAG_LBA48;
3218 } else
3219 tf->flags &= ~ATA_TFLAG_LBA48;
3220
3221 tf->feature = cdb[21];
3222 tf->nsect = cdb[23];
3223 tf->lbal = cdb[19];
3224 tf->lbam = cdb[18];
3225 tf->lbah = cdb[17];
3226 tf->device = cdb[24];
3227 tf->command = cdb[25];
3228 tf->auxiliary = get_unaligned_be32(&cdb[28]);
3229 }
3230
3231
3232 if (ata_is_ncq(tf->protocol))
3233 tf->nsect = qc->hw_tag << 3;
3234
3235
3236 tf->device = dev->devno ?
3237 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
3238
3239 switch (tf->command) {
3240
3241 case ATA_CMD_READ_LONG:
3242 case ATA_CMD_READ_LONG_ONCE:
3243 case ATA_CMD_WRITE_LONG:
3244 case ATA_CMD_WRITE_LONG_ONCE:
3245 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1) {
3246 fp = 1;
3247 goto invalid_fld;
3248 }
3249 qc->sect_size = scsi_bufflen(scmd);
3250 break;
3251
3252
3253 case ATA_CMD_CFA_WRITE_NE:
3254 case ATA_CMD_CFA_TRANS_SECT:
3255 case ATA_CMD_CFA_WRITE_MULT_NE:
3256
3257 case ATA_CMD_READ:
3258 case ATA_CMD_READ_EXT:
3259 case ATA_CMD_READ_QUEUED:
3260
3261 case ATA_CMD_FPDMA_READ:
3262 case ATA_CMD_READ_MULTI:
3263 case ATA_CMD_READ_MULTI_EXT:
3264 case ATA_CMD_PIO_READ:
3265 case ATA_CMD_PIO_READ_EXT:
3266 case ATA_CMD_READ_STREAM_DMA_EXT:
3267 case ATA_CMD_READ_STREAM_EXT:
3268 case ATA_CMD_VERIFY:
3269 case ATA_CMD_VERIFY_EXT:
3270 case ATA_CMD_WRITE:
3271 case ATA_CMD_WRITE_EXT:
3272 case ATA_CMD_WRITE_FUA_EXT:
3273 case ATA_CMD_WRITE_QUEUED:
3274 case ATA_CMD_WRITE_QUEUED_FUA_EXT:
3275 case ATA_CMD_FPDMA_WRITE:
3276 case ATA_CMD_WRITE_MULTI:
3277 case ATA_CMD_WRITE_MULTI_EXT:
3278 case ATA_CMD_WRITE_MULTI_FUA_EXT:
3279 case ATA_CMD_PIO_WRITE:
3280 case ATA_CMD_PIO_WRITE_EXT:
3281 case ATA_CMD_WRITE_STREAM_DMA_EXT:
3282 case ATA_CMD_WRITE_STREAM_EXT:
3283 qc->sect_size = scmd->device->sector_size;
3284 break;
3285
3286
3287 default:
3288 qc->sect_size = ATA_SECT_SIZE;
3289 }
3290
3291
3292
3293
3294
3295
3296 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3297 if (scmd->sc_data_direction == DMA_TO_DEVICE)
3298 tf->flags |= ATA_TFLAG_WRITE;
3299
3300 qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
3301
3302
3303
3304
3305
3306
3307
3308 ata_qc_set_pc_nbytes(qc);
3309
3310
3311 if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0) {
3312 fp = 1;
3313 goto invalid_fld;
3314 }
3315
3316
3317 if (ata_is_ncq(tf->protocol) && !ata_ncq_enabled(dev)) {
3318 fp = 1;
3319 goto invalid_fld;
3320 }
3321
3322
3323 if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf)) {
3324 fp = 1;
3325 goto invalid_fld;
3326 }
3327
3328 if (is_multi_taskfile(tf)) {
3329 unsigned int multi_count = 1 << (cdb[1] >> 5);
3330
3331
3332
3333
3334 if (multi_count != dev->multi_count)
3335 ata_dev_warn(dev, "invalid multi_count %u ignored\n",
3336 multi_count);
3337 }
3338
3339
3340
3341
3342
3343
3344
3345
3346 if (tf->command == ATA_CMD_SET_FEATURES &&
3347 tf->feature == SETFEATURES_XFER) {
3348 fp = (cdb[0] == ATA_16) ? 4 : 3;
3349 goto invalid_fld;
3350 }
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm) {
3368 fp = (cdb[0] == ATA_16) ? 14 : 9;
3369 goto invalid_fld;
3370 }
3371
3372 return 0;
3373
3374 invalid_fld:
3375 ata_scsi_set_invalid_field(dev, scmd, fp, 0xff);
3376 return 1;
3377}
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400static size_t ata_format_dsm_trim_descr(struct scsi_cmnd *cmd, u32 trmax,
3401 u64 sector, u32 count)
3402{
3403 struct scsi_device *sdp = cmd->device;
3404 size_t len = sdp->sector_size;
3405 size_t r;
3406 __le64 *buf;
3407 u32 i = 0;
3408 unsigned long flags;
3409
3410 WARN_ON(len > ATA_SCSI_RBUF_SIZE);
3411
3412 if (len > ATA_SCSI_RBUF_SIZE)
3413 len = ATA_SCSI_RBUF_SIZE;
3414
3415 spin_lock_irqsave(&ata_scsi_rbuf_lock, flags);
3416 buf = ((void *)ata_scsi_rbuf);
3417 memset(buf, 0, len);
3418 while (i < trmax) {
3419 u64 entry = sector |
3420 ((u64)(count > 0xffff ? 0xffff : count) << 48);
3421 buf[i++] = __cpu_to_le64(entry);
3422 if (count <= 0xffff)
3423 break;
3424 count -= 0xffff;
3425 sector += 0xffff;
3426 }
3427 r = sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, len);
3428 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, flags);
3429
3430 return r;
3431}
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
3445{
3446 struct ata_taskfile *tf = &qc->tf;
3447 struct scsi_cmnd *scmd = qc->scsicmd;
3448 struct scsi_device *sdp = scmd->device;
3449 size_t len = sdp->sector_size;
3450 struct ata_device *dev = qc->dev;
3451 const u8 *cdb = scmd->cmnd;
3452 u64 block;
3453 u32 n_block;
3454 const u32 trmax = len >> 3;
3455 u32 size;
3456 u16 fp;
3457 u8 bp = 0xff;
3458 u8 unmap = cdb[1] & 0x8;
3459
3460
3461 if (unlikely(!dev->dma_mode))
3462 goto invalid_opcode;
3463
3464
3465
3466
3467
3468
3469 if (unlikely(blk_rq_is_passthrough(scmd->request)))
3470 goto invalid_opcode;
3471
3472 if (unlikely(scmd->cmd_len < 16)) {
3473 fp = 15;
3474 goto invalid_fld;
3475 }
3476 scsi_16_lba_len(cdb, &block, &n_block);
3477
3478 if (!unmap ||
3479 (dev->horkage & ATA_HORKAGE_NOTRIM) ||
3480 !ata_id_has_trim(dev->id)) {
3481 fp = 1;
3482 bp = 3;
3483 goto invalid_fld;
3484 }
3485
3486 if (n_block > 0xffff * trmax) {
3487 fp = 2;
3488 goto invalid_fld;
3489 }
3490
3491
3492
3493
3494
3495 if (!scsi_sg_count(scmd))
3496 goto invalid_param_len;
3497
3498
3499
3500
3501
3502
3503
3504 size = ata_format_dsm_trim_descr(scmd, trmax, block, n_block);
3505 if (size != len)
3506 goto invalid_param_len;
3507
3508 if (ata_ncq_enabled(dev) && ata_fpdma_dsm_supported(dev)) {
3509
3510 tf->protocol = ATA_PROT_NCQ;
3511 tf->command = ATA_CMD_FPDMA_SEND;
3512 tf->hob_nsect = ATA_SUBCMD_FPDMA_SEND_DSM & 0x1f;
3513 tf->nsect = qc->hw_tag << 3;
3514 tf->hob_feature = (size / 512) >> 8;
3515 tf->feature = size / 512;
3516
3517 tf->auxiliary = 1;
3518 } else {
3519 tf->protocol = ATA_PROT_DMA;
3520 tf->hob_feature = 0;
3521 tf->feature = ATA_DSM_TRIM;
3522 tf->hob_nsect = (size / 512) >> 8;
3523 tf->nsect = size / 512;
3524 tf->command = ATA_CMD_DSM;
3525 }
3526
3527 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
3528 ATA_TFLAG_WRITE;
3529
3530 ata_qc_set_pc_nbytes(qc);
3531
3532 return 0;
3533
3534invalid_fld:
3535 ata_scsi_set_invalid_field(dev, scmd, fp, bp);
3536 return 1;
3537invalid_param_len:
3538
3539 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3540 return 1;
3541invalid_opcode:
3542
3543 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0);
3544 return 1;
3545}
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557static unsigned int ata_scsiop_maint_in(struct ata_scsi_args *args, u8 *rbuf)
3558{
3559 struct ata_device *dev = args->dev;
3560 u8 *cdb = args->cmd->cmnd;
3561 u8 supported = 0;
3562 unsigned int err = 0;
3563
3564 if (cdb[2] != 1) {
3565 ata_dev_warn(dev, "invalid command format %d\n", cdb[2]);
3566 err = 2;
3567 goto out;
3568 }
3569 switch (cdb[3]) {
3570 case INQUIRY:
3571 case MODE_SENSE:
3572 case MODE_SENSE_10:
3573 case READ_CAPACITY:
3574 case SERVICE_ACTION_IN_16:
3575 case REPORT_LUNS:
3576 case REQUEST_SENSE:
3577 case SYNCHRONIZE_CACHE:
3578 case REZERO_UNIT:
3579 case SEEK_6:
3580 case SEEK_10:
3581 case TEST_UNIT_READY:
3582 case SEND_DIAGNOSTIC:
3583 case MAINTENANCE_IN:
3584 case READ_6:
3585 case READ_10:
3586 case READ_16:
3587 case WRITE_6:
3588 case WRITE_10:
3589 case WRITE_16:
3590 case ATA_12:
3591 case ATA_16:
3592 case VERIFY:
3593 case VERIFY_16:
3594 case MODE_SELECT:
3595 case MODE_SELECT_10:
3596 case START_STOP:
3597 supported = 3;
3598 break;
3599 case ZBC_IN:
3600 case ZBC_OUT:
3601 if (ata_id_zoned_cap(dev->id) ||
3602 dev->class == ATA_DEV_ZAC)
3603 supported = 3;
3604 break;
3605 case SECURITY_PROTOCOL_IN:
3606 case SECURITY_PROTOCOL_OUT:
3607 if (dev->flags & ATA_DFLAG_TRUSTED)
3608 supported = 3;
3609 break;
3610 default:
3611 break;
3612 }
3613out:
3614 rbuf[1] = supported;
3615 return err;
3616}
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626static void ata_scsi_report_zones_complete(struct ata_queued_cmd *qc)
3627{
3628 struct scsi_cmnd *scmd = qc->scsicmd;
3629 struct sg_mapping_iter miter;
3630 unsigned long flags;
3631 unsigned int bytes = 0;
3632
3633 sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd),
3634 SG_MITER_TO_SG | SG_MITER_ATOMIC);
3635
3636 local_irq_save(flags);
3637 while (sg_miter_next(&miter)) {
3638 unsigned int offset = 0;
3639
3640 if (bytes == 0) {
3641 char *hdr;
3642 u32 list_length;
3643 u64 max_lba, opt_lba;
3644 u16 same;
3645
3646
3647 hdr = miter.addr;
3648 list_length = get_unaligned_le32(&hdr[0]);
3649 same = get_unaligned_le16(&hdr[4]);
3650 max_lba = get_unaligned_le64(&hdr[8]);
3651 opt_lba = get_unaligned_le64(&hdr[16]);
3652 put_unaligned_be32(list_length, &hdr[0]);
3653 hdr[4] = same & 0xf;
3654 put_unaligned_be64(max_lba, &hdr[8]);
3655 put_unaligned_be64(opt_lba, &hdr[16]);
3656 offset += 64;
3657 bytes += 64;
3658 }
3659 while (offset < miter.length) {
3660 char *rec;
3661 u8 cond, type, non_seq, reset;
3662 u64 size, start, wp;
3663
3664
3665 rec = miter.addr + offset;
3666 type = rec[0] & 0xf;
3667 cond = (rec[1] >> 4) & 0xf;
3668 non_seq = (rec[1] & 2);
3669 reset = (rec[1] & 1);
3670 size = get_unaligned_le64(&rec[8]);
3671 start = get_unaligned_le64(&rec[16]);
3672 wp = get_unaligned_le64(&rec[24]);
3673 rec[0] = type;
3674 rec[1] = (cond << 4) | non_seq | reset;
3675 put_unaligned_be64(size, &rec[8]);
3676 put_unaligned_be64(start, &rec[16]);
3677 put_unaligned_be64(wp, &rec[24]);
3678 WARN_ON(offset + 64 > miter.length);
3679 offset += 64;
3680 bytes += 64;
3681 }
3682 }
3683 sg_miter_stop(&miter);
3684 local_irq_restore(flags);
3685
3686 ata_scsi_qc_complete(qc);
3687}
3688
3689static unsigned int ata_scsi_zbc_in_xlat(struct ata_queued_cmd *qc)
3690{
3691 struct ata_taskfile *tf = &qc->tf;
3692 struct scsi_cmnd *scmd = qc->scsicmd;
3693 const u8 *cdb = scmd->cmnd;
3694 u16 sect, fp = (u16)-1;
3695 u8 sa, options, bp = 0xff;
3696 u64 block;
3697 u32 n_block;
3698
3699 if (unlikely(scmd->cmd_len < 16)) {
3700 ata_dev_warn(qc->dev, "invalid cdb length %d\n",
3701 scmd->cmd_len);
3702 fp = 15;
3703 goto invalid_fld;
3704 }
3705 scsi_16_lba_len(cdb, &block, &n_block);
3706 if (n_block != scsi_bufflen(scmd)) {
3707 ata_dev_warn(qc->dev, "non-matching transfer count (%d/%d)\n",
3708 n_block, scsi_bufflen(scmd));
3709 goto invalid_param_len;
3710 }
3711 sa = cdb[1] & 0x1f;
3712 if (sa != ZI_REPORT_ZONES) {
3713 ata_dev_warn(qc->dev, "invalid service action %d\n", sa);
3714 fp = 1;
3715 goto invalid_fld;
3716 }
3717
3718
3719
3720
3721 if ((n_block / 512) > 0xffff || n_block < 512 || (n_block % 512)) {
3722 ata_dev_warn(qc->dev, "invalid transfer count %d\n", n_block);
3723 goto invalid_param_len;
3724 }
3725 sect = n_block / 512;
3726 options = cdb[14] & 0xbf;
3727
3728 if (ata_ncq_enabled(qc->dev) &&
3729 ata_fpdma_zac_mgmt_in_supported(qc->dev)) {
3730 tf->protocol = ATA_PROT_NCQ;
3731 tf->command = ATA_CMD_FPDMA_RECV;
3732 tf->hob_nsect = ATA_SUBCMD_FPDMA_RECV_ZAC_MGMT_IN & 0x1f;
3733 tf->nsect = qc->hw_tag << 3;
3734 tf->feature = sect & 0xff;
3735 tf->hob_feature = (sect >> 8) & 0xff;
3736 tf->auxiliary = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES | (options << 8);
3737 } else {
3738 tf->command = ATA_CMD_ZAC_MGMT_IN;
3739 tf->feature = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES;
3740 tf->protocol = ATA_PROT_DMA;
3741 tf->hob_feature = options;
3742 tf->hob_nsect = (sect >> 8) & 0xff;
3743 tf->nsect = sect & 0xff;
3744 }
3745 tf->device = ATA_LBA;
3746 tf->lbah = (block >> 16) & 0xff;
3747 tf->lbam = (block >> 8) & 0xff;
3748 tf->lbal = block & 0xff;
3749 tf->hob_lbah = (block >> 40) & 0xff;
3750 tf->hob_lbam = (block >> 32) & 0xff;
3751 tf->hob_lbal = (block >> 24) & 0xff;
3752
3753 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
3754 qc->flags |= ATA_QCFLAG_RESULT_TF;
3755
3756 ata_qc_set_pc_nbytes(qc);
3757
3758 qc->complete_fn = ata_scsi_report_zones_complete;
3759
3760 return 0;
3761
3762invalid_fld:
3763 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
3764 return 1;
3765
3766invalid_param_len:
3767
3768 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3769 return 1;
3770}
3771
3772static unsigned int ata_scsi_zbc_out_xlat(struct ata_queued_cmd *qc)
3773{
3774 struct ata_taskfile *tf = &qc->tf;
3775 struct scsi_cmnd *scmd = qc->scsicmd;
3776 struct ata_device *dev = qc->dev;
3777 const u8 *cdb = scmd->cmnd;
3778 u8 all, sa;
3779 u64 block;
3780 u32 n_block;
3781 u16 fp = (u16)-1;
3782
3783 if (unlikely(scmd->cmd_len < 16)) {
3784 fp = 15;
3785 goto invalid_fld;
3786 }
3787
3788 sa = cdb[1] & 0x1f;
3789 if ((sa != ZO_CLOSE_ZONE) && (sa != ZO_FINISH_ZONE) &&
3790 (sa != ZO_OPEN_ZONE) && (sa != ZO_RESET_WRITE_POINTER)) {
3791 fp = 1;
3792 goto invalid_fld;
3793 }
3794
3795 scsi_16_lba_len(cdb, &block, &n_block);
3796 if (n_block) {
3797
3798
3799
3800 goto invalid_param_len;
3801 }
3802
3803 all = cdb[14] & 0x1;
3804 if (all) {
3805
3806
3807
3808 block = 0;
3809 } else if (block >= dev->n_sectors) {
3810
3811
3812
3813 fp = 2;
3814 goto invalid_fld;
3815 }
3816
3817 if (ata_ncq_enabled(qc->dev) &&
3818 ata_fpdma_zac_mgmt_out_supported(qc->dev)) {
3819 tf->protocol = ATA_PROT_NCQ_NODATA;
3820 tf->command = ATA_CMD_NCQ_NON_DATA;
3821 tf->feature = ATA_SUBCMD_NCQ_NON_DATA_ZAC_MGMT_OUT;
3822 tf->nsect = qc->hw_tag << 3;
3823 tf->auxiliary = sa | ((u16)all << 8);
3824 } else {
3825 tf->protocol = ATA_PROT_NODATA;
3826 tf->command = ATA_CMD_ZAC_MGMT_OUT;
3827 tf->feature = sa;
3828 tf->hob_feature = all;
3829 }
3830 tf->lbah = (block >> 16) & 0xff;
3831 tf->lbam = (block >> 8) & 0xff;
3832 tf->lbal = block & 0xff;
3833 tf->hob_lbah = (block >> 40) & 0xff;
3834 tf->hob_lbam = (block >> 32) & 0xff;
3835 tf->hob_lbal = (block >> 24) & 0xff;
3836 tf->device = ATA_LBA;
3837 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
3838
3839 return 0;
3840
3841 invalid_fld:
3842 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
3843 return 1;
3844invalid_param_len:
3845
3846 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3847 return 1;
3848}
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862static int ata_mselect_caching(struct ata_queued_cmd *qc,
3863 const u8 *buf, int len, u16 *fp)
3864{
3865 struct ata_taskfile *tf = &qc->tf;
3866 struct ata_device *dev = qc->dev;
3867 u8 mpage[CACHE_MPAGE_LEN];
3868 u8 wce;
3869 int i;
3870
3871
3872
3873
3874
3875
3876 if (len != CACHE_MPAGE_LEN - 2) {
3877 if (len < CACHE_MPAGE_LEN - 2)
3878 *fp = len;
3879 else
3880 *fp = CACHE_MPAGE_LEN - 2;
3881 return -EINVAL;
3882 }
3883
3884 wce = buf[0] & (1 << 2);
3885
3886
3887
3888
3889 ata_msense_caching(dev->id, mpage, false);
3890 for (i = 0; i < CACHE_MPAGE_LEN - 2; i++) {
3891 if (i == 0)
3892 continue;
3893 if (mpage[i + 2] != buf[i]) {
3894 *fp = i;
3895 return -EINVAL;
3896 }
3897 }
3898
3899 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
3900 tf->protocol = ATA_PROT_NODATA;
3901 tf->nsect = 0;
3902 tf->command = ATA_CMD_SET_FEATURES;
3903 tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF;
3904 return 0;
3905}
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919static int ata_mselect_control(struct ata_queued_cmd *qc,
3920 const u8 *buf, int len, u16 *fp)
3921{
3922 struct ata_device *dev = qc->dev;
3923 u8 mpage[CONTROL_MPAGE_LEN];
3924 u8 d_sense;
3925 int i;
3926
3927
3928
3929
3930
3931
3932 if (len != CONTROL_MPAGE_LEN - 2) {
3933 if (len < CONTROL_MPAGE_LEN - 2)
3934 *fp = len;
3935 else
3936 *fp = CONTROL_MPAGE_LEN - 2;
3937 return -EINVAL;
3938 }
3939
3940 d_sense = buf[0] & (1 << 2);
3941
3942
3943
3944
3945 ata_msense_control(dev, mpage, false);
3946 for (i = 0; i < CONTROL_MPAGE_LEN - 2; i++) {
3947 if (i == 0)
3948 continue;
3949 if (mpage[2 + i] != buf[i]) {
3950 *fp = i;
3951 return -EINVAL;
3952 }
3953 }
3954 if (d_sense & (1 << 2))
3955 dev->flags |= ATA_DFLAG_D_SENSE;
3956 else
3957 dev->flags &= ~ATA_DFLAG_D_SENSE;
3958 return 0;
3959}
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc)
3973{
3974 struct scsi_cmnd *scmd = qc->scsicmd;
3975 const u8 *cdb = scmd->cmnd;
3976 const u8 *p;
3977 u8 pg, spg;
3978 unsigned six_byte, pg_len, hdr_len, bd_len;
3979 int len;
3980 u16 fp = (u16)-1;
3981 u8 bp = 0xff;
3982
3983 VPRINTK("ENTER\n");
3984
3985 six_byte = (cdb[0] == MODE_SELECT);
3986 if (six_byte) {
3987 if (scmd->cmd_len < 5) {
3988 fp = 4;
3989 goto invalid_fld;
3990 }
3991
3992 len = cdb[4];
3993 hdr_len = 4;
3994 } else {
3995 if (scmd->cmd_len < 9) {
3996 fp = 8;
3997 goto invalid_fld;
3998 }
3999
4000 len = (cdb[7] << 8) + cdb[8];
4001 hdr_len = 8;
4002 }
4003
4004
4005 if ((cdb[1] & 0x11) != 0x10) {
4006 fp = 1;
4007 bp = (cdb[1] & 0x01) ? 1 : 5;
4008 goto invalid_fld;
4009 }
4010
4011
4012 if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len)
4013 goto invalid_param_len;
4014
4015 p = page_address(sg_page(scsi_sglist(scmd)));
4016
4017
4018 if (len < hdr_len)
4019 goto invalid_param_len;
4020
4021 if (six_byte)
4022 bd_len = p[3];
4023 else
4024 bd_len = (p[6] << 8) + p[7];
4025
4026 len -= hdr_len;
4027 p += hdr_len;
4028 if (len < bd_len)
4029 goto invalid_param_len;
4030 if (bd_len != 0 && bd_len != 8) {
4031 fp = (six_byte) ? 3 : 6;
4032 fp += bd_len + hdr_len;
4033 goto invalid_param;
4034 }
4035
4036 len -= bd_len;
4037 p += bd_len;
4038 if (len == 0)
4039 goto skip;
4040
4041
4042 pg = p[0] & 0x3f;
4043 if (p[0] & 0x40) {
4044 if (len < 4)
4045 goto invalid_param_len;
4046
4047 spg = p[1];
4048 pg_len = (p[2] << 8) | p[3];
4049 p += 4;
4050 len -= 4;
4051 } else {
4052 if (len < 2)
4053 goto invalid_param_len;
4054
4055 spg = 0;
4056 pg_len = p[1];
4057 p += 2;
4058 len -= 2;
4059 }
4060
4061
4062
4063
4064
4065 if (spg && (spg != ALL_SUB_MPAGES)) {
4066 fp = (p[0] & 0x40) ? 1 : 0;
4067 fp += hdr_len + bd_len;
4068 goto invalid_param;
4069 }
4070 if (pg_len > len)
4071 goto invalid_param_len;
4072
4073 switch (pg) {
4074 case CACHE_MPAGE:
4075 if (ata_mselect_caching(qc, p, pg_len, &fp) < 0) {
4076 fp += hdr_len + bd_len;
4077 goto invalid_param;
4078 }
4079 break;
4080 case CONTROL_MPAGE:
4081 if (ata_mselect_control(qc, p, pg_len, &fp) < 0) {
4082 fp += hdr_len + bd_len;
4083 goto invalid_param;
4084 } else {
4085 goto skip;
4086 }
4087 break;
4088 default:
4089 fp = bd_len + hdr_len;
4090 goto invalid_param;
4091 }
4092
4093
4094
4095
4096
4097 if (len > pg_len)
4098 goto invalid_param;
4099
4100 return 0;
4101
4102 invalid_fld:
4103 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
4104 return 1;
4105
4106 invalid_param:
4107 ata_scsi_set_invalid_parameter(qc->dev, scmd, fp);
4108 return 1;
4109
4110 invalid_param_len:
4111
4112 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
4113 return 1;
4114
4115 skip:
4116 scmd->result = SAM_STAT_GOOD;
4117 return 1;
4118}
4119
4120static u8 ata_scsi_trusted_op(u32 len, bool send, bool dma)
4121{
4122 if (len == 0)
4123 return ATA_CMD_TRUSTED_NONDATA;
4124 else if (send)
4125 return dma ? ATA_CMD_TRUSTED_SND_DMA : ATA_CMD_TRUSTED_SND;
4126 else
4127 return dma ? ATA_CMD_TRUSTED_RCV_DMA : ATA_CMD_TRUSTED_RCV;
4128}
4129
4130static unsigned int ata_scsi_security_inout_xlat(struct ata_queued_cmd *qc)
4131{
4132 struct scsi_cmnd *scmd = qc->scsicmd;
4133 const u8 *cdb = scmd->cmnd;
4134 struct ata_taskfile *tf = &qc->tf;
4135 u8 secp = cdb[1];
4136 bool send = (cdb[0] == SECURITY_PROTOCOL_OUT);
4137 u16 spsp = get_unaligned_be16(&cdb[2]);
4138 u32 len = get_unaligned_be32(&cdb[6]);
4139 bool dma = !(qc->dev->flags & ATA_DFLAG_PIO);
4140
4141
4142
4143
4144 if (secp == 0xef) {
4145 ata_scsi_set_invalid_field(qc->dev, scmd, 1, 0);
4146 return 1;
4147 }
4148
4149 if (cdb[4] & 7) {
4150 if (len > 0xffff) {
4151 ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0);
4152 return 1;
4153 }
4154 } else {
4155 if (len > 0x01fffe00) {
4156 ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0);
4157 return 1;
4158 }
4159
4160
4161 len = (len + 511) / 512;
4162 }
4163
4164 tf->protocol = dma ? ATA_PROT_DMA : ATA_PROT_PIO;
4165 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR | ATA_TFLAG_LBA;
4166 if (send)
4167 tf->flags |= ATA_TFLAG_WRITE;
4168 tf->command = ata_scsi_trusted_op(len, send, dma);
4169 tf->feature = secp;
4170 tf->lbam = spsp & 0xff;
4171 tf->lbah = spsp >> 8;
4172
4173 if (len) {
4174 tf->nsect = len & 0xff;
4175 tf->lbal = len >> 8;
4176 } else {
4177 if (!send)
4178 tf->lbah = (1 << 7);
4179 }
4180
4181 ata_qc_set_pc_nbytes(qc);
4182 return 0;
4183}
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196static unsigned int ata_scsi_var_len_cdb_xlat(struct ata_queued_cmd *qc)
4197{
4198 struct scsi_cmnd *scmd = qc->scsicmd;
4199 const u8 *cdb = scmd->cmnd;
4200 const u16 sa = get_unaligned_be16(&cdb[8]);
4201
4202
4203
4204
4205
4206 if (sa == ATA_32)
4207 return ata_scsi_pass_thru(qc);
4208
4209
4210 return 1;
4211}
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
4226{
4227 switch (cmd) {
4228 case READ_6:
4229 case READ_10:
4230 case READ_16:
4231
4232 case WRITE_6:
4233 case WRITE_10:
4234 case WRITE_16:
4235 return ata_scsi_rw_xlat;
4236
4237 case WRITE_SAME_16:
4238 return ata_scsi_write_same_xlat;
4239
4240 case SYNCHRONIZE_CACHE:
4241 if (ata_try_flush_cache(dev))
4242 return ata_scsi_flush_xlat;
4243 break;
4244
4245 case VERIFY:
4246 case VERIFY_16:
4247 return ata_scsi_verify_xlat;
4248
4249 case ATA_12:
4250 case ATA_16:
4251 return ata_scsi_pass_thru;
4252
4253 case VARIABLE_LENGTH_CMD:
4254 return ata_scsi_var_len_cdb_xlat;
4255
4256 case MODE_SELECT:
4257 case MODE_SELECT_10:
4258 return ata_scsi_mode_select_xlat;
4259 break;
4260
4261 case ZBC_IN:
4262 return ata_scsi_zbc_in_xlat;
4263
4264 case ZBC_OUT:
4265 return ata_scsi_zbc_out_xlat;
4266
4267 case SECURITY_PROTOCOL_IN:
4268 case SECURITY_PROTOCOL_OUT:
4269 if (!(dev->flags & ATA_DFLAG_TRUSTED))
4270 break;
4271 return ata_scsi_security_inout_xlat;
4272
4273 case START_STOP:
4274 return ata_scsi_start_stop_xlat;
4275 }
4276
4277 return NULL;
4278}
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288static inline void ata_scsi_dump_cdb(struct ata_port *ap,
4289 struct scsi_cmnd *cmd)
4290{
4291#ifdef ATA_DEBUG
4292 struct scsi_device *scsidev = cmd->device;
4293
4294 DPRINTK("CDB (%u:%d,%d,%lld) %9ph\n",
4295 ap->print_id,
4296 scsidev->channel, scsidev->id, scsidev->lun,
4297 cmd->cmnd);
4298#endif
4299}
4300
4301static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
4302 struct ata_device *dev)
4303{
4304 u8 scsi_op = scmd->cmnd[0];
4305 ata_xlat_func_t xlat_func;
4306 int rc = 0;
4307
4308 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
4309 if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
4310 goto bad_cdb_len;
4311
4312 xlat_func = ata_get_xlat_func(dev, scsi_op);
4313 } else {
4314 if (unlikely(!scmd->cmd_len))
4315 goto bad_cdb_len;
4316
4317 xlat_func = NULL;
4318 if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
4319
4320 int len = COMMAND_SIZE(scsi_op);
4321 if (unlikely(len > scmd->cmd_len ||
4322 len > dev->cdb_len ||
4323 scmd->cmd_len > ATAPI_CDB_LEN))
4324 goto bad_cdb_len;
4325
4326 xlat_func = atapi_xlat;
4327 } else {
4328
4329 if (unlikely(scmd->cmd_len > 16))
4330 goto bad_cdb_len;
4331
4332 xlat_func = ata_get_xlat_func(dev, scsi_op);
4333 }
4334 }
4335
4336 if (xlat_func)
4337 rc = ata_scsi_translate(dev, scmd, xlat_func);
4338 else
4339 ata_scsi_simulate(dev, scmd);
4340
4341 return rc;
4342
4343 bad_cdb_len:
4344 DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
4345 scmd->cmd_len, scsi_op, dev->cdb_len);
4346 scmd->result = DID_ERROR << 16;
4347 scmd->scsi_done(scmd);
4348 return 0;
4349}
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370int ata_scsi_queuecmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
4371{
4372 struct ata_port *ap;
4373 struct ata_device *dev;
4374 struct scsi_device *scsidev = cmd->device;
4375 int rc = 0;
4376 unsigned long irq_flags;
4377
4378 ap = ata_shost_to_port(shost);
4379
4380 spin_lock_irqsave(ap->lock, irq_flags);
4381
4382 ata_scsi_dump_cdb(ap, cmd);
4383
4384 dev = ata_scsi_find_dev(ap, scsidev);
4385 if (likely(dev))
4386 rc = __ata_scsi_queuecmd(cmd, dev);
4387 else {
4388 cmd->result = (DID_BAD_TARGET << 16);
4389 cmd->scsi_done(cmd);
4390 }
4391
4392 spin_unlock_irqrestore(ap->lock, irq_flags);
4393
4394 return rc;
4395}
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd)
4410{
4411 struct ata_scsi_args args;
4412 const u8 *scsicmd = cmd->cmnd;
4413 u8 tmp8;
4414
4415 args.dev = dev;
4416 args.id = dev->id;
4417 args.cmd = cmd;
4418
4419 switch(scsicmd[0]) {
4420 case INQUIRY:
4421 if (scsicmd[1] & 2)
4422 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
4423 else if ((scsicmd[1] & 1) == 0)
4424 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
4425 else switch (scsicmd[2]) {
4426 case 0x00:
4427 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
4428 break;
4429 case 0x80:
4430 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
4431 break;
4432 case 0x83:
4433 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
4434 break;
4435 case 0x89:
4436 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
4437 break;
4438 case 0xb0:
4439 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
4440 break;
4441 case 0xb1:
4442 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
4443 break;
4444 case 0xb2:
4445 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b2);
4446 break;
4447 case 0xb6:
4448 if (dev->flags & ATA_DFLAG_ZAC) {
4449 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b6);
4450 break;
4451 }
4452
4453 default:
4454 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff);
4455 break;
4456 }
4457 break;
4458
4459 case MODE_SENSE:
4460 case MODE_SENSE_10:
4461 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
4462 break;
4463
4464 case READ_CAPACITY:
4465 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
4466 break;
4467
4468 case SERVICE_ACTION_IN_16:
4469 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
4470 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
4471 else
4472 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
4473 break;
4474
4475 case REPORT_LUNS:
4476 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
4477 break;
4478
4479 case REQUEST_SENSE:
4480 ata_scsi_set_sense(dev, cmd, 0, 0, 0);
4481 cmd->result = (DRIVER_SENSE << 24);
4482 break;
4483
4484
4485
4486
4487 case SYNCHRONIZE_CACHE:
4488
4489
4490
4491 case REZERO_UNIT:
4492 case SEEK_6:
4493 case SEEK_10:
4494 case TEST_UNIT_READY:
4495 break;
4496
4497 case SEND_DIAGNOSTIC:
4498 tmp8 = scsicmd[1] & ~(1 << 3);
4499 if (tmp8 != 0x4 || scsicmd[3] || scsicmd[4])
4500 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
4501 break;
4502
4503 case MAINTENANCE_IN:
4504 if (scsicmd[1] == MI_REPORT_SUPPORTED_OPERATION_CODES)
4505 ata_scsi_rbuf_fill(&args, ata_scsiop_maint_in);
4506 else
4507 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
4508 break;
4509
4510
4511 default:
4512 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x20, 0x0);
4513
4514 break;
4515 }
4516
4517 cmd->scsi_done(cmd);
4518}
4519
4520int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
4521{
4522 int i, rc;
4523
4524 for (i = 0; i < host->n_ports; i++) {
4525 struct ata_port *ap = host->ports[i];
4526 struct Scsi_Host *shost;
4527
4528 rc = -ENOMEM;
4529 shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
4530 if (!shost)
4531 goto err_alloc;
4532
4533 shost->eh_noresume = 1;
4534 *(struct ata_port **)&shost->hostdata[0] = ap;
4535 ap->scsi_host = shost;
4536
4537 shost->transportt = ata_scsi_transport_template;
4538 shost->unique_id = ap->print_id;
4539 shost->max_id = 16;
4540 shost->max_lun = 1;
4541 shost->max_channel = 1;
4542 shost->max_cmd_len = 32;
4543
4544
4545
4546
4547
4548
4549 shost->max_host_blocked = 1;
4550
4551 rc = scsi_add_host_with_dma(ap->scsi_host,
4552 &ap->tdev, ap->host->dev);
4553 if (rc)
4554 goto err_add;
4555 }
4556
4557 return 0;
4558
4559 err_add:
4560 scsi_host_put(host->ports[i]->scsi_host);
4561 err_alloc:
4562 while (--i >= 0) {
4563 struct Scsi_Host *shost = host->ports[i]->scsi_host;
4564
4565 scsi_remove_host(shost);
4566 scsi_host_put(shost);
4567 }
4568 return rc;
4569}
4570
4571void ata_scsi_scan_host(struct ata_port *ap, int sync)
4572{
4573 int tries = 5;
4574 struct ata_device *last_failed_dev = NULL;
4575 struct ata_link *link;
4576 struct ata_device *dev;
4577
4578 repeat:
4579 ata_for_each_link(link, ap, EDGE) {
4580 ata_for_each_dev(dev, link, ENABLED) {
4581 struct scsi_device *sdev;
4582 int channel = 0, id = 0;
4583
4584 if (dev->sdev)
4585 continue;
4586
4587 if (ata_is_host_link(link))
4588 id = dev->devno;
4589 else
4590 channel = link->pmp;
4591
4592 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
4593 NULL);
4594 if (!IS_ERR(sdev)) {
4595 dev->sdev = sdev;
4596 scsi_device_put(sdev);
4597 } else {
4598 dev->sdev = NULL;
4599 }
4600 }
4601 }
4602
4603
4604
4605
4606
4607 ata_for_each_link(link, ap, EDGE) {
4608 ata_for_each_dev(dev, link, ENABLED) {
4609 if (!dev->sdev)
4610 goto exit_loop;
4611 }
4612 }
4613 exit_loop:
4614 if (!link)
4615 return;
4616
4617
4618 if (sync) {
4619
4620
4621
4622 if (dev != last_failed_dev) {
4623 msleep(100);
4624 last_failed_dev = dev;
4625 goto repeat;
4626 }
4627
4628
4629
4630
4631 if (--tries) {
4632 msleep(100);
4633 goto repeat;
4634 }
4635
4636 ata_port_err(ap,
4637 "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n");
4638 }
4639
4640 queue_delayed_work(system_long_wq, &ap->hotplug_task,
4641 round_jiffies_relative(HZ));
4642}
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659int ata_scsi_offline_dev(struct ata_device *dev)
4660{
4661 if (dev->sdev) {
4662 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
4663 return 1;
4664 }
4665 return 0;
4666}
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678static void ata_scsi_remove_dev(struct ata_device *dev)
4679{
4680 struct ata_port *ap = dev->link->ap;
4681 struct scsi_device *sdev;
4682 unsigned long flags;
4683
4684
4685
4686
4687
4688
4689
4690 mutex_lock(&ap->scsi_host->scan_mutex);
4691 spin_lock_irqsave(ap->lock, flags);
4692
4693
4694 sdev = dev->sdev;
4695 dev->sdev = NULL;
4696
4697 if (sdev) {
4698
4699
4700
4701
4702 if (scsi_device_get(sdev) == 0) {
4703
4704
4705
4706
4707
4708 scsi_device_set_state(sdev, SDEV_OFFLINE);
4709 } else {
4710 WARN_ON(1);
4711 sdev = NULL;
4712 }
4713 }
4714
4715 spin_unlock_irqrestore(ap->lock, flags);
4716 mutex_unlock(&ap->scsi_host->scan_mutex);
4717
4718 if (sdev) {
4719 ata_dev_info(dev, "detaching (SCSI %s)\n",
4720 dev_name(&sdev->sdev_gendev));
4721
4722 scsi_remove_device(sdev);
4723 scsi_device_put(sdev);
4724 }
4725}
4726
4727static void ata_scsi_handle_link_detach(struct ata_link *link)
4728{
4729 struct ata_port *ap = link->ap;
4730 struct ata_device *dev;
4731
4732 ata_for_each_dev(dev, link, ALL) {
4733 unsigned long flags;
4734
4735 if (!(dev->flags & ATA_DFLAG_DETACHED))
4736 continue;
4737
4738 spin_lock_irqsave(ap->lock, flags);
4739 dev->flags &= ~ATA_DFLAG_DETACHED;
4740 spin_unlock_irqrestore(ap->lock, flags);
4741
4742 if (zpodd_dev_enabled(dev))
4743 zpodd_exit(dev);
4744
4745 ata_scsi_remove_dev(dev);
4746 }
4747}
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759void ata_scsi_media_change_notify(struct ata_device *dev)
4760{
4761 if (dev->sdev)
4762 sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
4763 GFP_ATOMIC);
4764}
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778void ata_scsi_hotplug(struct work_struct *work)
4779{
4780 struct ata_port *ap =
4781 container_of(work, struct ata_port, hotplug_task.work);
4782 int i;
4783
4784 if (ap->pflags & ATA_PFLAG_UNLOADING) {
4785 DPRINTK("ENTER/EXIT - unloading\n");
4786 return;
4787 }
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805#ifdef CONFIG_FREEZER
4806 while (pm_freezing)
4807 msleep(10);
4808#endif
4809
4810 DPRINTK("ENTER\n");
4811 mutex_lock(&ap->scsi_scan_mutex);
4812
4813
4814
4815
4816
4817 ata_scsi_handle_link_detach(&ap->link);
4818 if (ap->pmp_link)
4819 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
4820 ata_scsi_handle_link_detach(&ap->pmp_link[i]);
4821
4822
4823 ata_scsi_scan_host(ap, 0);
4824
4825 mutex_unlock(&ap->scsi_scan_mutex);
4826 DPRINTK("EXIT\n");
4827}
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
4846 unsigned int id, u64 lun)
4847{
4848 struct ata_port *ap = ata_shost_to_port(shost);
4849 unsigned long flags;
4850 int devno, rc = 0;
4851
4852 if (!ap->ops->error_handler)
4853 return -EOPNOTSUPP;
4854
4855 if (lun != SCAN_WILD_CARD && lun)
4856 return -EINVAL;
4857
4858 if (!sata_pmp_attached(ap)) {
4859 if (channel != SCAN_WILD_CARD && channel)
4860 return -EINVAL;
4861 devno = id;
4862 } else {
4863 if (id != SCAN_WILD_CARD && id)
4864 return -EINVAL;
4865 devno = channel;
4866 }
4867
4868 spin_lock_irqsave(ap->lock, flags);
4869
4870 if (devno == SCAN_WILD_CARD) {
4871 struct ata_link *link;
4872
4873 ata_for_each_link(link, ap, EDGE) {
4874 struct ata_eh_info *ehi = &link->eh_info;
4875 ehi->probe_mask |= ATA_ALL_DEVICES;
4876 ehi->action |= ATA_EH_RESET;
4877 }
4878 } else {
4879 struct ata_device *dev = ata_find_dev(ap, devno);
4880
4881 if (dev) {
4882 struct ata_eh_info *ehi = &dev->link->eh_info;
4883 ehi->probe_mask |= 1 << dev->devno;
4884 ehi->action |= ATA_EH_RESET;
4885 } else
4886 rc = -EINVAL;
4887 }
4888
4889 if (rc == 0) {
4890 ata_port_schedule_eh(ap);
4891 spin_unlock_irqrestore(ap->lock, flags);
4892 ata_port_wait_eh(ap);
4893 } else
4894 spin_unlock_irqrestore(ap->lock, flags);
4895
4896 return rc;
4897}
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909void ata_scsi_dev_rescan(struct work_struct *work)
4910{
4911 struct ata_port *ap =
4912 container_of(work, struct ata_port, scsi_rescan_task);
4913 struct ata_link *link;
4914 struct ata_device *dev;
4915 unsigned long flags;
4916
4917 mutex_lock(&ap->scsi_scan_mutex);
4918 spin_lock_irqsave(ap->lock, flags);
4919
4920 ata_for_each_link(link, ap, EDGE) {
4921 ata_for_each_dev(dev, link, ENABLED) {
4922 struct scsi_device *sdev = dev->sdev;
4923
4924 if (!sdev)
4925 continue;
4926 if (scsi_device_get(sdev))
4927 continue;
4928
4929 spin_unlock_irqrestore(ap->lock, flags);
4930 scsi_rescan_device(&(sdev->sdev_gendev));
4931 scsi_device_put(sdev);
4932 spin_lock_irqsave(ap->lock, flags);
4933 }
4934 }
4935
4936 spin_unlock_irqrestore(ap->lock, flags);
4937 mutex_unlock(&ap->scsi_scan_mutex);
4938}
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953struct ata_port *ata_sas_port_alloc(struct ata_host *host,
4954 struct ata_port_info *port_info,
4955 struct Scsi_Host *shost)
4956{
4957 struct ata_port *ap;
4958
4959 ap = ata_port_alloc(host);
4960 if (!ap)
4961 return NULL;
4962
4963 ap->port_no = 0;
4964 ap->lock = &host->lock;
4965 ap->pio_mask = port_info->pio_mask;
4966 ap->mwdma_mask = port_info->mwdma_mask;
4967 ap->udma_mask = port_info->udma_mask;
4968 ap->flags |= port_info->flags;
4969 ap->ops = port_info->port_ops;
4970 ap->cbl = ATA_CBL_SATA;
4971
4972 return ap;
4973}
4974EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988int ata_sas_port_start(struct ata_port *ap)
4989{
4990
4991
4992
4993
4994 if (!ap->ops->error_handler)
4995 ap->pflags &= ~ATA_PFLAG_FROZEN;
4996 return 0;
4997}
4998EXPORT_SYMBOL_GPL(ata_sas_port_start);
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010void ata_sas_port_stop(struct ata_port *ap)
5011{
5012}
5013EXPORT_SYMBOL_GPL(ata_sas_port_stop);
5014
5015
5016
5017
5018
5019
5020
5021
5022void ata_sas_async_probe(struct ata_port *ap)
5023{
5024 __ata_port_probe(ap);
5025}
5026EXPORT_SYMBOL_GPL(ata_sas_async_probe);
5027
5028int ata_sas_sync_probe(struct ata_port *ap)
5029{
5030 return ata_port_probe(ap);
5031}
5032EXPORT_SYMBOL_GPL(ata_sas_sync_probe);
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046int ata_sas_port_init(struct ata_port *ap)
5047{
5048 int rc = ap->ops->port_start(ap);
5049
5050 if (rc)
5051 return rc;
5052 ap->print_id = atomic_inc_return(&ata_print_id);
5053 return 0;
5054}
5055EXPORT_SYMBOL_GPL(ata_sas_port_init);
5056
5057int ata_sas_tport_add(struct device *parent, struct ata_port *ap)
5058{
5059 return ata_tport_add(parent, ap);
5060}
5061EXPORT_SYMBOL_GPL(ata_sas_tport_add);
5062
5063void ata_sas_tport_delete(struct ata_port *ap)
5064{
5065 ata_tport_delete(ap);
5066}
5067EXPORT_SYMBOL_GPL(ata_sas_tport_delete);
5068
5069
5070
5071
5072
5073
5074
5075void ata_sas_port_destroy(struct ata_port *ap)
5076{
5077 if (ap->ops->port_stop)
5078 ap->ops->port_stop(ap);
5079 kfree(ap);
5080}
5081EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
5093{
5094 ata_scsi_sdev_config(sdev);
5095 ata_scsi_dev_config(sdev, ap->link.device);
5096 return 0;
5097}
5098EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
5111{
5112 int rc = 0;
5113
5114 ata_scsi_dump_cdb(ap, cmd);
5115
5116 if (likely(ata_dev_enabled(ap->link.device)))
5117 rc = __ata_scsi_queuecmd(cmd, ap->link.device);
5118 else {
5119 cmd->result = (DID_BAD_TARGET << 16);
5120 cmd->scsi_done(cmd);
5121 }
5122 return rc;
5123}
5124EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
5125
5126int ata_sas_allocate_tag(struct ata_port *ap)
5127{
5128 unsigned int max_queue = ap->host->n_tags;
5129 unsigned int i, tag;
5130
5131 for (i = 0, tag = ap->sas_last_tag + 1; i < max_queue; i++, tag++) {
5132 tag = tag < max_queue ? tag : 0;
5133
5134
5135 if (ata_tag_internal(tag))
5136 continue;
5137
5138 if (!test_and_set_bit(tag, &ap->sas_tag_allocated)) {
5139 ap->sas_last_tag = tag;
5140 return tag;
5141 }
5142 }
5143 return -1;
5144}
5145
5146void ata_sas_free_tag(unsigned int tag, struct ata_port *ap)
5147{
5148 clear_bit(tag, &ap->sas_tag_allocated);
5149}
5150