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