blob: d59b14eae34f333935ced55c270ed719532282b5 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * libata-scsi.c - helper library for ATA
4 *
5 * Maintained by: Tejun Heo <tj@kernel.org>
6 * Please ALWAYS copy linux-ide@vger.kernel.org
7 * on emails.
8 *
9 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
10 * Copyright 2003-2004 Jeff Garzik
11 *
12 * libata documentation is available via 'make {ps|pdf}docs',
13 * as Documentation/driver-api/libata.rst
14 *
15 * Hardware documentation available from
16 * - http://www.t10.org/
17 * - http://www.t13.org/
18 */
19
20#include <linux/slab.h>
21#include <linux/kernel.h>
22#include <linux/blkdev.h>
23#include <linux/spinlock.h>
24#include <linux/export.h>
25#include <scsi/scsi.h>
26#include <scsi/scsi_host.h>
27#include <scsi/scsi_cmnd.h>
28#include <scsi/scsi_eh.h>
29#include <scsi/scsi_device.h>
30#include <scsi/scsi_tcq.h>
31#include <scsi/scsi_transport.h>
32#include <linux/libata.h>
33#include <linux/hdreg.h>
34#include <linux/uaccess.h>
35#include <linux/suspend.h>
36#include <asm/unaligned.h>
37#include <linux/ioprio.h>
38#include <linux/of.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), /* AWRE */
69 0, /* read retry count */
70 0, 0, 0, 0,
71 0, /* write retry count */
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, /* contains WCE, needs to be 0 for logic */
79 0, 0, 0, 0, 0, 0, 0, 0, 0,
80 0, /* contains DRA, needs to be 0 for logic */
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, /* DSENSE=0, GLTSD=1 */
88 0, /* [QAM+QERR may be 1, see 05-359r1] */
89 0, 0, 0, 0, 0xff, 0xff,
90 0, 30 /* extended self test time, see 05-359r1 */
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 /* UNKNOWN is internal state, iterate from MAX_POWER */
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 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 /* "Invalid field in CDB" */
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 /* "Invalid field in parameter list" */
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 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
476 * @sdev: SCSI device for which BIOS geometry is to be determined
477 * @bdev: block device associated with @sdev
478 * @capacity: capacity of SCSI device
479 * @geom: location to which geometry will be output
480 *
481 * Generic bios head/sector/cylinder calculator
482 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
483 * mapping. Some situations may arise where the disk is not
484 * bootable if this is not used.
485 *
486 * LOCKING:
487 * Defined by the SCSI layer. We don't really care.
488 *
489 * RETURNS:
490 * Zero.
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 * ata_scsi_unlock_native_capacity - unlock native capacity
505 * @sdev: SCSI device to adjust device capacity for
506 *
507 * This function is called if a partition on @sdev extends beyond
508 * the end of the device. It requests EH to unlock HPA.
509 *
510 * LOCKING:
511 * Defined by the SCSI layer. Might sleep.
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 * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
534 * @ap: target port
535 * @sdev: SCSI device to get identify data for
536 * @arg: User buffer area for identify data
537 *
538 * LOCKING:
539 * Defined by the SCSI layer. We don't really care.
540 *
541 * RETURNS:
542 * Zero on success, negative errno on error.
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 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
574 * @scsidev: Device to which we are issuing command
575 * @arg: User provided data for issuing command
576 *
577 * LOCKING:
578 * Defined by the SCSI layer. We don't really care.
579 *
580 * RETURNS:
581 * Zero on success, negative errno on error.
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); /* PIO Data-in */
612 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
613 block count in sector count field */
614 data_dir = DMA_FROM_DEVICE;
615 } else {
616 scsi_cmd[1] = (3 << 1); /* Non-data */
617 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
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) { /* hack -- ide driver does this too */
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 /* Good values for timeout and retries? Values below
635 from scsi_ioctl_send_command() for default case... */
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) {/* sense data available */
640 u8 *desc = sensebuf + 8;
641 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
642
643 /* If we set cc then ATA pass-through will cause a
644 * check condition even if no error. Filter that. */
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 /* Send userspace a few ATA registers (same as drivers/ide) */
652 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
653 desc[0] == 0x09) { /* code is "ATA Descriptor" */
654 args[0] = desc[13]; /* status */
655 args[1] = desc[3]; /* error */
656 args[2] = desc[5]; /* sector count (0:7) */
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 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
678 * @scsidev: Device to which we are issuing command
679 * @arg: User provided data for issuing command
680 *
681 * LOCKING:
682 * Defined by the SCSI layer. We don't really care.
683 *
684 * RETURNS:
685 * Zero on success, negative errno on error.
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); /* Non-data */
706 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
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 /* Good values for timeout and retries? Values below
716 from scsi_ioctl_send_command() for default case... */
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) {/* sense data available */
721 u8 *desc = sensebuf + 8;
722 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
723
724 /* If we set cc then ATA pass-through will cause a
725 * check condition even if no error. Filter that. */
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 /* Send userspace ATA registers */
733 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
734 desc[0] == 0x09) {/* code is "ATA Descriptor" */
735 args[0] = desc[13]; /* status */
736 args[1] = desc[3]; /* error */
737 args[2] = desc[5]; /* sector count (0:7) */
738 args[3] = desc[7]; /* lbal */
739 args[4] = desc[9]; /* lbam */
740 args[5] = desc[11]; /* lbah */
741 args[6] = desc[12]; /* select */
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
765int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
766 unsigned int cmd, void __user *arg)
767{
768 unsigned long val;
769 int rc = -EINVAL;
770 unsigned long flags;
771
772 switch (cmd) {
773 case HDIO_GET_32BIT:
774 spin_lock_irqsave(ap->lock, flags);
775 val = ata_ioc32(ap);
776 spin_unlock_irqrestore(ap->lock, flags);
777 return put_user(val, (unsigned long __user *)arg);
778
779 case HDIO_SET_32BIT:
780 val = (unsigned long) arg;
781 rc = 0;
782 spin_lock_irqsave(ap->lock, flags);
783 if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
784 if (val)
785 ap->pflags |= ATA_PFLAG_PIO32;
786 else
787 ap->pflags &= ~ATA_PFLAG_PIO32;
788 } else {
789 if (val != ata_ioc32(ap))
790 rc = -EINVAL;
791 }
792 spin_unlock_irqrestore(ap->lock, flags);
793 return rc;
794
795 case HDIO_GET_IDENTITY:
796 return ata_get_identity(ap, scsidev, arg);
797
798 case HDIO_DRIVE_CMD:
799 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
800 return -EACCES;
801 return ata_cmd_ioctl(scsidev, arg);
802
803 case HDIO_DRIVE_TASK:
804 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
805 return -EACCES;
806 return ata_task_ioctl(scsidev, arg);
807
808 default:
809 rc = -ENOTTY;
810 break;
811 }
812
813 return rc;
814}
815EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
816
817int ata_scsi_ioctl(struct scsi_device *scsidev, unsigned int cmd,
818 void __user *arg)
819{
820 return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
821 scsidev, cmd, arg);
822}
823EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
824
825/**
826 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
827 * @dev: ATA device to which the new command is attached
828 * @cmd: SCSI command that originated this ATA command
829 *
830 * Obtain a reference to an unused ata_queued_cmd structure,
831 * which is the basic libata structure representing a single
832 * ATA command sent to the hardware.
833 *
834 * If a command was available, fill in the SCSI-specific
835 * portions of the structure with information on the
836 * current command.
837 *
838 * LOCKING:
839 * spin_lock_irqsave(host lock)
840 *
841 * RETURNS:
842 * Command allocated, or %NULL if none available.
843 */
844static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
845 struct scsi_cmnd *cmd)
846{
847 struct ata_queued_cmd *qc;
848
849 qc = ata_qc_new_init(dev, cmd->request->tag);
850 if (qc) {
851 qc->scsicmd = cmd;
852 qc->scsidone = cmd->scsi_done;
853
854 qc->sg = scsi_sglist(cmd);
855 qc->n_elem = scsi_sg_count(cmd);
856
857 if (cmd->request->rq_flags & RQF_QUIET)
858 qc->flags |= ATA_QCFLAG_QUIET;
859 } else {
860 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
861 cmd->scsi_done(cmd);
862 }
863
864 return qc;
865}
866
867static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
868{
869 struct scsi_cmnd *scmd = qc->scsicmd;
870
871 qc->extrabytes = scmd->request->extra_len;
872 qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
873}
874
875/**
876 * ata_dump_status - user friendly display of error info
877 * @id: id of the port in question
878 * @tf: ptr to filled out taskfile
879 *
880 * Decode and dump the ATA error/status registers for the user so
881 * that they have some idea what really happened at the non
882 * make-believe layer.
883 *
884 * LOCKING:
885 * inherited from caller
886 */
887static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
888{
889 u8 stat = tf->command, err = tf->feature;
890
891 pr_warn("ata%u: status=0x%02x { ", id, stat);
892 if (stat & ATA_BUSY) {
893 pr_cont("Busy }\n"); /* Data is not valid in this case */
894 } else {
895 if (stat & ATA_DRDY) pr_cont("DriveReady ");
896 if (stat & ATA_DF) pr_cont("DeviceFault ");
897 if (stat & ATA_DSC) pr_cont("SeekComplete ");
898 if (stat & ATA_DRQ) pr_cont("DataRequest ");
899 if (stat & ATA_CORR) pr_cont("CorrectedError ");
900 if (stat & ATA_SENSE) pr_cont("Sense ");
901 if (stat & ATA_ERR) pr_cont("Error ");
902 pr_cont("}\n");
903
904 if (err) {
905 pr_warn("ata%u: error=0x%02x { ", id, err);
906 if (err & ATA_ABORTED) pr_cont("DriveStatusError ");
907 if (err & ATA_ICRC) {
908 if (err & ATA_ABORTED)
909 pr_cont("BadCRC ");
910 else pr_cont("Sector ");
911 }
912 if (err & ATA_UNC) pr_cont("UncorrectableError ");
913 if (err & ATA_IDNF) pr_cont("SectorIdNotFound ");
914 if (err & ATA_TRK0NF) pr_cont("TrackZeroNotFound ");
915 if (err & ATA_AMNF) pr_cont("AddrMarkNotFound ");
916 pr_cont("}\n");
917 }
918 }
919}
920
921/**
922 * ata_to_sense_error - convert ATA error to SCSI error
923 * @id: ATA device number
924 * @drv_stat: value contained in ATA status register
925 * @drv_err: value contained in ATA error register
926 * @sk: the sense key we'll fill out
927 * @asc: the additional sense code we'll fill out
928 * @ascq: the additional sense code qualifier we'll fill out
929 * @verbose: be verbose
930 *
931 * Converts an ATA error into a SCSI error. Fill out pointers to
932 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
933 * format sense blocks.
934 *
935 * LOCKING:
936 * spin_lock_irqsave(host lock)
937 */
938static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
939 u8 *asc, u8 *ascq, int verbose)
940{
941 int i;
942
943 /* Based on the 3ware driver translation table */
944 static const unsigned char sense_table[][4] = {
945 /* BBD|ECC|ID|MAR */
946 {0xd1, ABORTED_COMMAND, 0x00, 0x00},
947 // Device busy Aborted command
948 /* BBD|ECC|ID */
949 {0xd0, ABORTED_COMMAND, 0x00, 0x00},
950 // Device busy Aborted command
951 /* ECC|MC|MARK */
952 {0x61, HARDWARE_ERROR, 0x00, 0x00},
953 // Device fault Hardware error
954 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
955 {0x84, ABORTED_COMMAND, 0x47, 0x00},
956 // Data CRC error SCSI parity error
957 /* MC|ID|ABRT|TRK0|MARK */
958 {0x37, NOT_READY, 0x04, 0x00},
959 // Unit offline Not ready
960 /* MCR|MARK */
961 {0x09, NOT_READY, 0x04, 0x00},
962 // Unrecovered disk error Not ready
963 /* Bad address mark */
964 {0x01, MEDIUM_ERROR, 0x13, 0x00},
965 // Address mark not found for data field
966 /* TRK0 - Track 0 not found */
967 {0x02, HARDWARE_ERROR, 0x00, 0x00},
968 // Hardware error
969 /* Abort: 0x04 is not translated here, see below */
970 /* Media change request */
971 {0x08, NOT_READY, 0x04, 0x00},
972 // FIXME: faking offline
973 /* SRV/IDNF - ID not found */
974 {0x10, ILLEGAL_REQUEST, 0x21, 0x00},
975 // Logical address out of range
976 /* MC - Media Changed */
977 {0x20, UNIT_ATTENTION, 0x28, 0x00},
978 // Not ready to ready change, medium may have changed
979 /* ECC - Uncorrectable ECC error */
980 {0x40, MEDIUM_ERROR, 0x11, 0x04},
981 // Unrecovered read error
982 /* BBD - block marked bad */
983 {0x80, MEDIUM_ERROR, 0x11, 0x04},
984 // Block marked bad Medium error, unrecovered read error
985 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
986 };
987 static const unsigned char stat_table[][4] = {
988 /* Must be first because BUSY means no other bits valid */
989 {0x80, ABORTED_COMMAND, 0x47, 0x00},
990 // Busy, fake parity for now
991 {0x40, ILLEGAL_REQUEST, 0x21, 0x04},
992 // Device ready, unaligned write command
993 {0x20, HARDWARE_ERROR, 0x44, 0x00},
994 // Device fault, internal target failure
995 {0x08, ABORTED_COMMAND, 0x47, 0x00},
996 // Timed out in xfer, fake parity for now
997 {0x04, RECOVERED_ERROR, 0x11, 0x00},
998 // Recovered ECC error Medium error, recovered
999 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
1000 };
1001
1002 /*
1003 * Is this an error we can process/parse
1004 */
1005 if (drv_stat & ATA_BUSY) {
1006 drv_err = 0; /* Ignore the err bits, they're invalid */
1007 }
1008
1009 if (drv_err) {
1010 /* Look for drv_err */
1011 for (i = 0; sense_table[i][0] != 0xFF; i++) {
1012 /* Look for best matches first */
1013 if ((sense_table[i][0] & drv_err) ==
1014 sense_table[i][0]) {
1015 *sk = sense_table[i][1];
1016 *asc = sense_table[i][2];
1017 *ascq = sense_table[i][3];
1018 goto translate_done;
1019 }
1020 }
1021 }
1022
1023 /*
1024 * Fall back to interpreting status bits. Note that if the drv_err
1025 * has only the ABRT bit set, we decode drv_stat. ABRT by itself
1026 * is not descriptive enough.
1027 */
1028 for (i = 0; stat_table[i][0] != 0xFF; i++) {
1029 if (stat_table[i][0] & drv_stat) {
1030 *sk = stat_table[i][1];
1031 *asc = stat_table[i][2];
1032 *ascq = stat_table[i][3];
1033 goto translate_done;
1034 }
1035 }
1036
1037 /*
1038 * We need a sensible error return here, which is tricky, and one
1039 * that won't cause people to do things like return a disk wrongly.
1040 */
1041 *sk = ABORTED_COMMAND;
1042 *asc = 0x00;
1043 *ascq = 0x00;
1044
1045 translate_done:
1046 if (verbose)
1047 pr_err("ata%u: translated ATA stat/err 0x%02x/%02x to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
1048 id, drv_stat, drv_err, *sk, *asc, *ascq);
1049 return;
1050}
1051
1052/*
1053 * ata_gen_passthru_sense - Generate check condition sense block.
1054 * @qc: Command that completed.
1055 *
1056 * This function is specific to the ATA descriptor format sense
1057 * block specified for the ATA pass through commands. Regardless
1058 * of whether the command errored or not, return a sense
1059 * block. Copy all controller registers into the sense
1060 * block. If there was no error, we get the request from an ATA
1061 * passthrough command, so we use the following sense data:
1062 * sk = RECOVERED ERROR
1063 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
1064 *
1065 *
1066 * LOCKING:
1067 * None.
1068 */
1069static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
1070{
1071 struct scsi_cmnd *cmd = qc->scsicmd;
1072 struct ata_taskfile *tf = &qc->result_tf;
1073 unsigned char *sb = cmd->sense_buffer;
1074 unsigned char *desc = sb + 8;
1075 int verbose = qc->ap->ops->error_handler == NULL;
1076 u8 sense_key, asc, ascq;
1077
1078 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1079
1080 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
1081
1082 /*
1083 * Use ata_to_sense_error() to map status register bits
1084 * onto sense key, asc & ascq.
1085 */
1086 if (qc->err_mask ||
1087 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
1088 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
1089 &sense_key, &asc, &ascq, verbose);
1090 ata_scsi_set_sense(qc->dev, cmd, sense_key, asc, ascq);
1091 } else {
1092 /*
1093 * ATA PASS-THROUGH INFORMATION AVAILABLE
1094 * Always in descriptor format sense.
1095 */
1096 scsi_build_sense_buffer(1, cmd->sense_buffer,
1097 RECOVERED_ERROR, 0, 0x1D);
1098 }
1099
1100 if ((cmd->sense_buffer[0] & 0x7f) >= 0x72) {
1101 u8 len;
1102
1103 /* descriptor format */
1104 len = sb[7];
1105 desc = (char *)scsi_sense_desc_find(sb, len + 8, 9);
1106 if (!desc) {
1107 if (SCSI_SENSE_BUFFERSIZE < len + 14)
1108 return;
1109 sb[7] = len + 14;
1110 desc = sb + 8 + len;
1111 }
1112 desc[0] = 9;
1113 desc[1] = 12;
1114 /*
1115 * Copy registers into sense buffer.
1116 */
1117 desc[2] = 0x00;
1118 desc[3] = tf->feature; /* == error reg */
1119 desc[5] = tf->nsect;
1120 desc[7] = tf->lbal;
1121 desc[9] = tf->lbam;
1122 desc[11] = tf->lbah;
1123 desc[12] = tf->device;
1124 desc[13] = tf->command; /* == status reg */
1125
1126 /*
1127 * Fill in Extend bit, and the high order bytes
1128 * if applicable.
1129 */
1130 if (tf->flags & ATA_TFLAG_LBA48) {
1131 desc[2] |= 0x01;
1132 desc[4] = tf->hob_nsect;
1133 desc[6] = tf->hob_lbal;
1134 desc[8] = tf->hob_lbam;
1135 desc[10] = tf->hob_lbah;
1136 }
1137 } else {
1138 /* Fixed sense format */
1139 desc[0] = tf->feature;
1140 desc[1] = tf->command; /* status */
1141 desc[2] = tf->device;
1142 desc[3] = tf->nsect;
1143 desc[7] = 0;
1144 if (tf->flags & ATA_TFLAG_LBA48) {
1145 desc[8] |= 0x80;
1146 if (tf->hob_nsect)
1147 desc[8] |= 0x40;
1148 if (tf->hob_lbal || tf->hob_lbam || tf->hob_lbah)
1149 desc[8] |= 0x20;
1150 }
1151 desc[9] = tf->lbal;
1152 desc[10] = tf->lbam;
1153 desc[11] = tf->lbah;
1154 }
1155}
1156
1157/**
1158 * ata_gen_ata_sense - generate a SCSI fixed sense block
1159 * @qc: Command that we are erroring out
1160 *
1161 * Generate sense block for a failed ATA command @qc. Descriptor
1162 * format is used to accommodate LBA48 block address.
1163 *
1164 * LOCKING:
1165 * None.
1166 */
1167static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
1168{
1169 struct ata_device *dev = qc->dev;
1170 struct scsi_cmnd *cmd = qc->scsicmd;
1171 struct ata_taskfile *tf = &qc->result_tf;
1172 unsigned char *sb = cmd->sense_buffer;
1173 int verbose = qc->ap->ops->error_handler == NULL;
1174 u64 block;
1175 u8 sense_key, asc, ascq;
1176
1177 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1178
1179 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
1180
1181 if (ata_dev_disabled(dev)) {
1182 /* Device disabled after error recovery */
1183 /* LOGICAL UNIT NOT READY, HARD RESET REQUIRED */
1184 ata_scsi_set_sense(dev, cmd, NOT_READY, 0x04, 0x21);
1185 return;
1186 }
1187 /* Use ata_to_sense_error() to map status register bits
1188 * onto sense key, asc & ascq.
1189 */
1190 if (qc->err_mask ||
1191 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
1192 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
1193 &sense_key, &asc, &ascq, verbose);
1194 ata_scsi_set_sense(dev, cmd, sense_key, asc, ascq);
1195 } else {
1196 /* Could not decode error */
1197 ata_dev_warn(dev, "could not decode error status 0x%x err_mask 0x%x\n",
1198 tf->command, qc->err_mask);
1199 ata_scsi_set_sense(dev, cmd, ABORTED_COMMAND, 0, 0);
1200 return;
1201 }
1202
1203 block = ata_tf_read_block(&qc->result_tf, dev);
1204 if (block == U64_MAX)
1205 return;
1206
1207 scsi_set_sense_information(sb, SCSI_SENSE_BUFFERSIZE, block);
1208}
1209
1210static void ata_scsi_sdev_config(struct scsi_device *sdev)
1211{
1212 sdev->use_10_for_rw = 1;
1213 sdev->use_10_for_ms = 1;
1214 sdev->no_write_same = 1;
1215
1216 /* Schedule policy is determined by ->qc_defer() callback and
1217 * it needs to see every deferred qc. Set dev_blocked to 1 to
1218 * prevent SCSI midlayer from automatically deferring
1219 * requests.
1220 */
1221 sdev->max_device_blocked = 1;
1222}
1223
1224/**
1225 * atapi_drain_needed - Check whether data transfer may overflow
1226 * @rq: request to be checked
1227 *
1228 * ATAPI commands which transfer variable length data to host
1229 * might overflow due to application error or hardware bug. This
1230 * function checks whether overflow should be drained and ignored
1231 * for @request.
1232 *
1233 * LOCKING:
1234 * None.
1235 *
1236 * RETURNS:
1237 * 1 if ; otherwise, 0.
1238 */
1239static int atapi_drain_needed(struct request *rq)
1240{
1241 if (likely(!blk_rq_is_passthrough(rq)))
1242 return 0;
1243
1244 if (!blk_rq_bytes(rq) || op_is_write(req_op(rq)))
1245 return 0;
1246
1247 return atapi_cmd_type(scsi_req(rq)->cmd[0]) == ATAPI_MISC;
1248}
1249
1250static int ata_scsi_dev_config(struct scsi_device *sdev,
1251 struct ata_device *dev)
1252{
1253 struct request_queue *q = sdev->request_queue;
1254
1255 if (!ata_id_has_unload(dev->id))
1256 dev->flags |= ATA_DFLAG_NO_UNLOAD;
1257
1258 /* configure max sectors */
1259 blk_queue_max_hw_sectors(q, dev->max_sectors);
1260
1261 if (dev->class == ATA_DEV_ATAPI) {
1262 void *buf;
1263
1264 sdev->sector_size = ATA_SECT_SIZE;
1265
1266 /* set DMA padding */
1267 blk_queue_update_dma_pad(q, ATA_DMA_PAD_SZ - 1);
1268
1269 /* configure draining */
1270 buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
1271 if (!buf) {
1272 ata_dev_err(dev, "drain buffer allocation failed\n");
1273 return -ENOMEM;
1274 }
1275
1276 blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
1277 } else {
1278 sdev->sector_size = ata_id_logical_sector_size(dev->id);
1279 sdev->manage_start_stop = 1;
1280 }
1281
1282 /*
1283 * ata_pio_sectors() expects buffer for each sector to not cross
1284 * page boundary. Enforce it by requiring buffers to be sector
1285 * aligned, which works iff sector_size is not larger than
1286 * PAGE_SIZE. ATAPI devices also need the alignment as
1287 * IDENTIFY_PACKET is executed as ATA_PROT_PIO.
1288 */
1289 if (sdev->sector_size > PAGE_SIZE)
1290 ata_dev_warn(dev,
1291 "sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
1292 sdev->sector_size);
1293
1294 blk_queue_update_dma_alignment(q, sdev->sector_size - 1);
1295
1296 if (dev->flags & ATA_DFLAG_AN)
1297 set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
1298
1299 if (dev->flags & ATA_DFLAG_NCQ) {
1300 int depth;
1301
1302 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
1303 depth = min(ATA_MAX_QUEUE, depth);
1304 scsi_change_queue_depth(sdev, depth);
1305 }
1306
1307 if (dev->flags & ATA_DFLAG_TRUSTED)
1308 sdev->security_supported = 1;
1309
1310 dev->sdev = sdev;
1311 return 0;
1312}
1313
1314/**
1315 * ata_scsi_slave_config - Set SCSI device attributes
1316 * @sdev: SCSI device to examine
1317 *
1318 * This is called before we actually start reading
1319 * and writing to the device, to configure certain
1320 * SCSI mid-layer behaviors.
1321 *
1322 * LOCKING:
1323 * Defined by SCSI layer. We don't really care.
1324 */
1325
1326int ata_scsi_slave_config(struct scsi_device *sdev)
1327{
1328 struct ata_port *ap = ata_shost_to_port(sdev->host);
1329 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
1330 int rc = 0;
1331
1332 ata_scsi_sdev_config(sdev);
1333
1334 if (dev)
1335 rc = ata_scsi_dev_config(sdev, dev);
1336
1337 return rc;
1338}
1339
1340/**
1341 * ata_scsi_slave_destroy - SCSI device is about to be destroyed
1342 * @sdev: SCSI device to be destroyed
1343 *
1344 * @sdev is about to be destroyed for hot/warm unplugging. If
1345 * this unplugging was initiated by libata as indicated by NULL
1346 * dev->sdev, this function doesn't have to do anything.
1347 * Otherwise, SCSI layer initiated warm-unplug is in progress.
1348 * Clear dev->sdev, schedule the device for ATA detach and invoke
1349 * EH.
1350 *
1351 * LOCKING:
1352 * Defined by SCSI layer. We don't really care.
1353 */
1354void ata_scsi_slave_destroy(struct scsi_device *sdev)
1355{
1356 struct ata_port *ap = ata_shost_to_port(sdev->host);
1357 struct request_queue *q = sdev->request_queue;
1358 unsigned long flags;
1359 struct ata_device *dev;
1360
1361 if (!ap->ops->error_handler)
1362 return;
1363
1364 spin_lock_irqsave(ap->lock, flags);
1365 dev = __ata_scsi_find_dev(ap, sdev);
1366 if (dev && dev->sdev) {
1367 /* SCSI device already in CANCEL state, no need to offline it */
1368 dev->sdev = NULL;
1369 dev->flags |= ATA_DFLAG_DETACH;
1370 ata_port_schedule_eh(ap);
1371 }
1372 spin_unlock_irqrestore(ap->lock, flags);
1373
1374 kfree(q->dma_drain_buffer);
1375 q->dma_drain_buffer = NULL;
1376 q->dma_drain_size = 0;
1377}
1378
1379/**
1380 * __ata_change_queue_depth - helper for ata_scsi_change_queue_depth
1381 * @ap: ATA port to which the device change the queue depth
1382 * @sdev: SCSI device to configure queue depth for
1383 * @queue_depth: new queue depth
1384 *
1385 * libsas and libata have different approaches for associating a sdev to
1386 * its ata_port.
1387 *
1388 */
1389int __ata_change_queue_depth(struct ata_port *ap, struct scsi_device *sdev,
1390 int queue_depth)
1391{
1392 struct ata_device *dev;
1393 unsigned long flags;
1394
1395 if (queue_depth < 1 || queue_depth == sdev->queue_depth)
1396 return sdev->queue_depth;
1397
1398 dev = ata_scsi_find_dev(ap, sdev);
1399 if (!dev || !ata_dev_enabled(dev))
1400 return sdev->queue_depth;
1401
1402 /* NCQ enabled? */
1403 spin_lock_irqsave(ap->lock, flags);
1404 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1405 if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
1406 dev->flags |= ATA_DFLAG_NCQ_OFF;
1407 queue_depth = 1;
1408 }
1409 spin_unlock_irqrestore(ap->lock, flags);
1410
1411 /* limit and apply queue depth */
1412 queue_depth = min(queue_depth, sdev->host->can_queue);
1413 queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
1414 queue_depth = min(queue_depth, ATA_MAX_QUEUE);
1415
1416 if (sdev->queue_depth == queue_depth)
1417 return -EINVAL;
1418
1419 return scsi_change_queue_depth(sdev, queue_depth);
1420}
1421
1422/**
1423 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
1424 * @sdev: SCSI device to configure queue depth for
1425 * @queue_depth: new queue depth
1426 *
1427 * This is libata standard hostt->change_queue_depth callback.
1428 * SCSI will call into this callback when user tries to set queue
1429 * depth via sysfs.
1430 *
1431 * LOCKING:
1432 * SCSI layer (we don't care)
1433 *
1434 * RETURNS:
1435 * Newly configured queue depth.
1436 */
1437int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
1438{
1439 struct ata_port *ap = ata_shost_to_port(sdev->host);
1440
1441 return __ata_change_queue_depth(ap, sdev, queue_depth);
1442}
1443
1444/**
1445 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
1446 * @qc: Storage for translated ATA taskfile
1447 *
1448 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
1449 * (to start). Perhaps these commands should be preceded by
1450 * CHECK POWER MODE to see what power mode the device is already in.
1451 * [See SAT revision 5 at www.t10.org]
1452 *
1453 * LOCKING:
1454 * spin_lock_irqsave(host lock)
1455 *
1456 * RETURNS:
1457 * Zero on success, non-zero on error.
1458 */
1459static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
1460{
1461 struct scsi_cmnd *scmd = qc->scsicmd;
1462 struct ata_taskfile *tf = &qc->tf;
1463 const u8 *cdb = scmd->cmnd;
1464 u16 fp;
1465 u8 bp = 0xff;
1466
1467 if (scmd->cmd_len < 5) {
1468 fp = 4;
1469 goto invalid_fld;
1470 }
1471
1472 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1473 tf->protocol = ATA_PROT_NODATA;
1474 if (cdb[1] & 0x1) {
1475 ; /* ignore IMMED bit, violates sat-r05 */
1476 }
1477 if (cdb[4] & 0x2) {
1478 fp = 4;
1479 bp = 1;
1480 goto invalid_fld; /* LOEJ bit set not supported */
1481 }
1482 if (((cdb[4] >> 4) & 0xf) != 0) {
1483 fp = 4;
1484 bp = 3;
1485 goto invalid_fld; /* power conditions not supported */
1486 }
1487
1488 if (cdb[4] & 0x1) {
1489 tf->nsect = 1; /* 1 sector, lba=0 */
1490
1491 if (qc->dev->flags & ATA_DFLAG_LBA) {
1492 tf->flags |= ATA_TFLAG_LBA;
1493
1494 tf->lbah = 0x0;
1495 tf->lbam = 0x0;
1496 tf->lbal = 0x0;
1497 tf->device |= ATA_LBA;
1498 } else {
1499 /* CHS */
1500 tf->lbal = 0x1; /* sect */
1501 tf->lbam = 0x0; /* cyl low */
1502 tf->lbah = 0x0; /* cyl high */
1503 }
1504
1505 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
1506 } else {
1507 /* Some odd clown BIOSen issue spindown on power off (ACPI S4
1508 * or S5) causing some drives to spin up and down again.
1509 */
1510 if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
1511 system_state == SYSTEM_POWER_OFF)
1512 goto skip;
1513
1514 if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
1515 system_entering_hibernation())
1516 goto skip;
1517
1518 /* Issue ATA STANDBY IMMEDIATE command */
1519 tf->command = ATA_CMD_STANDBYNOW1;
1520 }
1521
1522 /*
1523 * Standby and Idle condition timers could be implemented but that
1524 * would require libata to implement the Power condition mode page
1525 * and allow the user to change it. Changing mode pages requires
1526 * MODE SELECT to be implemented.
1527 */
1528
1529 return 0;
1530
1531 invalid_fld:
1532 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
1533 return 1;
1534 skip:
1535 scmd->result = SAM_STAT_GOOD;
1536 return 1;
1537}
1538
1539
1540/**
1541 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
1542 * @qc: Storage for translated ATA taskfile
1543 *
1544 * Sets up an ATA taskfile to issue FLUSH CACHE or
1545 * FLUSH CACHE EXT.
1546 *
1547 * LOCKING:
1548 * spin_lock_irqsave(host lock)
1549 *
1550 * RETURNS:
1551 * Zero on success, non-zero on error.
1552 */
1553static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
1554{
1555 struct ata_taskfile *tf = &qc->tf;
1556
1557 tf->flags |= ATA_TFLAG_DEVICE;
1558 tf->protocol = ATA_PROT_NODATA;
1559
1560 if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
1561 tf->command = ATA_CMD_FLUSH_EXT;
1562 else
1563 tf->command = ATA_CMD_FLUSH;
1564
1565 /* flush is critical for IO integrity, consider it an IO command */
1566 qc->flags |= ATA_QCFLAG_IO;
1567
1568 return 0;
1569}
1570
1571/**
1572 * scsi_6_lba_len - Get LBA and transfer length
1573 * @cdb: SCSI command to translate
1574 *
1575 * Calculate LBA and transfer length for 6-byte commands.
1576 *
1577 * RETURNS:
1578 * @plba: the LBA
1579 * @plen: the transfer length
1580 */
1581static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1582{
1583 u64 lba = 0;
1584 u32 len;
1585
1586 VPRINTK("six-byte command\n");
1587
1588 lba |= ((u64)(cdb[1] & 0x1f)) << 16;
1589 lba |= ((u64)cdb[2]) << 8;
1590 lba |= ((u64)cdb[3]);
1591
1592 len = cdb[4];
1593
1594 *plba = lba;
1595 *plen = len;
1596}
1597
1598/**
1599 * scsi_10_lba_len - Get LBA and transfer length
1600 * @cdb: SCSI command to translate
1601 *
1602 * Calculate LBA and transfer length for 10-byte commands.
1603 *
1604 * RETURNS:
1605 * @plba: the LBA
1606 * @plen: the transfer length
1607 */
1608static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1609{
1610 u64 lba = 0;
1611 u32 len = 0;
1612
1613 VPRINTK("ten-byte command\n");
1614
1615 lba |= ((u64)cdb[2]) << 24;
1616 lba |= ((u64)cdb[3]) << 16;
1617 lba |= ((u64)cdb[4]) << 8;
1618 lba |= ((u64)cdb[5]);
1619
1620 len |= ((u32)cdb[7]) << 8;
1621 len |= ((u32)cdb[8]);
1622
1623 *plba = lba;
1624 *plen = len;
1625}
1626
1627/**
1628 * scsi_16_lba_len - Get LBA and transfer length
1629 * @cdb: SCSI command to translate
1630 *
1631 * Calculate LBA and transfer length for 16-byte commands.
1632 *
1633 * RETURNS:
1634 * @plba: the LBA
1635 * @plen: the transfer length
1636 */
1637static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1638{
1639 u64 lba = 0;
1640 u32 len = 0;
1641
1642 VPRINTK("sixteen-byte command\n");
1643
1644 lba |= ((u64)cdb[2]) << 56;
1645 lba |= ((u64)cdb[3]) << 48;
1646 lba |= ((u64)cdb[4]) << 40;
1647 lba |= ((u64)cdb[5]) << 32;
1648 lba |= ((u64)cdb[6]) << 24;
1649 lba |= ((u64)cdb[7]) << 16;
1650 lba |= ((u64)cdb[8]) << 8;
1651 lba |= ((u64)cdb[9]);
1652
1653 len |= ((u32)cdb[10]) << 24;
1654 len |= ((u32)cdb[11]) << 16;
1655 len |= ((u32)cdb[12]) << 8;
1656 len |= ((u32)cdb[13]);
1657
1658 *plba = lba;
1659 *plen = len;
1660}
1661
1662/**
1663 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1664 * @qc: Storage for translated ATA taskfile
1665 *
1666 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1667 *
1668 * LOCKING:
1669 * spin_lock_irqsave(host lock)
1670 *
1671 * RETURNS:
1672 * Zero on success, non-zero on error.
1673 */
1674static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
1675{
1676 struct scsi_cmnd *scmd = qc->scsicmd;
1677 struct ata_taskfile *tf = &qc->tf;
1678 struct ata_device *dev = qc->dev;
1679 u64 dev_sectors = qc->dev->n_sectors;
1680 const u8 *cdb = scmd->cmnd;
1681 u64 block;
1682 u32 n_block;
1683 u16 fp;
1684
1685 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1686 tf->protocol = ATA_PROT_NODATA;
1687
1688 if (cdb[0] == VERIFY) {
1689 if (scmd->cmd_len < 10) {
1690 fp = 9;
1691 goto invalid_fld;
1692 }
1693 scsi_10_lba_len(cdb, &block, &n_block);
1694 } else if (cdb[0] == VERIFY_16) {
1695 if (scmd->cmd_len < 16) {
1696 fp = 15;
1697 goto invalid_fld;
1698 }
1699 scsi_16_lba_len(cdb, &block, &n_block);
1700 } else {
1701 fp = 0;
1702 goto invalid_fld;
1703 }
1704
1705 if (!n_block)
1706 goto nothing_to_do;
1707 if (block >= dev_sectors)
1708 goto out_of_range;
1709 if ((block + n_block) > dev_sectors)
1710 goto out_of_range;
1711
1712 if (dev->flags & ATA_DFLAG_LBA) {
1713 tf->flags |= ATA_TFLAG_LBA;
1714
1715 if (lba_28_ok(block, n_block)) {
1716 /* use LBA28 */
1717 tf->command = ATA_CMD_VERIFY;
1718 tf->device |= (block >> 24) & 0xf;
1719 } else if (lba_48_ok(block, n_block)) {
1720 if (!(dev->flags & ATA_DFLAG_LBA48))
1721 goto out_of_range;
1722
1723 /* use LBA48 */
1724 tf->flags |= ATA_TFLAG_LBA48;
1725 tf->command = ATA_CMD_VERIFY_EXT;
1726
1727 tf->hob_nsect = (n_block >> 8) & 0xff;
1728
1729 tf->hob_lbah = (block >> 40) & 0xff;
1730 tf->hob_lbam = (block >> 32) & 0xff;
1731 tf->hob_lbal = (block >> 24) & 0xff;
1732 } else
1733 /* request too large even for LBA48 */
1734 goto out_of_range;
1735
1736 tf->nsect = n_block & 0xff;
1737
1738 tf->lbah = (block >> 16) & 0xff;
1739 tf->lbam = (block >> 8) & 0xff;
1740 tf->lbal = block & 0xff;
1741
1742 tf->device |= ATA_LBA;
1743 } else {
1744 /* CHS */
1745 u32 sect, head, cyl, track;
1746
1747 if (!lba_28_ok(block, n_block))
1748 goto out_of_range;
1749
1750 /* Convert LBA to CHS */
1751 track = (u32)block / dev->sectors;
1752 cyl = track / dev->heads;
1753 head = track % dev->heads;
1754 sect = (u32)block % dev->sectors + 1;
1755
1756 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1757 (u32)block, track, cyl, head, sect);
1758
1759 /* Check whether the converted CHS can fit.
1760 Cylinder: 0-65535
1761 Head: 0-15
1762 Sector: 1-255*/
1763 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
1764 goto out_of_range;
1765
1766 tf->command = ATA_CMD_VERIFY;
1767 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1768 tf->lbal = sect;
1769 tf->lbam = cyl;
1770 tf->lbah = cyl >> 8;
1771 tf->device |= head;
1772 }
1773
1774 return 0;
1775
1776invalid_fld:
1777 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
1778 return 1;
1779
1780out_of_range:
1781 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
1782 /* "Logical Block Address out of range" */
1783 return 1;
1784
1785nothing_to_do:
1786 scmd->result = SAM_STAT_GOOD;
1787 return 1;
1788}
1789
1790static bool ata_check_nblocks(struct scsi_cmnd *scmd, u32 n_blocks)
1791{
1792 struct request *rq = scmd->request;
1793 u32 req_blocks;
1794
1795 if (!blk_rq_is_passthrough(rq))
1796 return true;
1797
1798 req_blocks = blk_rq_bytes(rq) / scmd->device->sector_size;
1799 if (n_blocks > req_blocks)
1800 return false;
1801
1802 return true;
1803}
1804
1805/**
1806 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1807 * @qc: Storage for translated ATA taskfile
1808 *
1809 * Converts any of six SCSI read/write commands into the
1810 * ATA counterpart, including starting sector (LBA),
1811 * sector count, and taking into account the device's LBA48
1812 * support.
1813 *
1814 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1815 * %WRITE_16 are currently supported.
1816 *
1817 * LOCKING:
1818 * spin_lock_irqsave(host lock)
1819 *
1820 * RETURNS:
1821 * Zero on success, non-zero on error.
1822 */
1823static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
1824{
1825 struct scsi_cmnd *scmd = qc->scsicmd;
1826 const u8 *cdb = scmd->cmnd;
1827 struct request *rq = scmd->request;
1828 int class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
1829 unsigned int tf_flags = 0;
1830 u64 block;
1831 u32 n_block;
1832 int rc;
1833 u16 fp = 0;
1834
1835 if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
1836 tf_flags |= ATA_TFLAG_WRITE;
1837
1838 /* Calculate the SCSI LBA, transfer length and FUA. */
1839 switch (cdb[0]) {
1840 case READ_10:
1841 case WRITE_10:
1842 if (unlikely(scmd->cmd_len < 10)) {
1843 fp = 9;
1844 goto invalid_fld;
1845 }
1846 scsi_10_lba_len(cdb, &block, &n_block);
1847 if (cdb[1] & (1 << 3))
1848 tf_flags |= ATA_TFLAG_FUA;
1849 if (!ata_check_nblocks(scmd, n_block))
1850 goto invalid_fld;
1851 break;
1852 case READ_6:
1853 case WRITE_6:
1854 if (unlikely(scmd->cmd_len < 6)) {
1855 fp = 5;
1856 goto invalid_fld;
1857 }
1858 scsi_6_lba_len(cdb, &block, &n_block);
1859
1860 /* for 6-byte r/w commands, transfer length 0
1861 * means 256 blocks of data, not 0 block.
1862 */
1863 if (!n_block)
1864 n_block = 256;
1865 if (!ata_check_nblocks(scmd, n_block))
1866 goto invalid_fld;
1867 break;
1868 case READ_16:
1869 case WRITE_16:
1870 if (unlikely(scmd->cmd_len < 16)) {
1871 fp = 15;
1872 goto invalid_fld;
1873 }
1874 scsi_16_lba_len(cdb, &block, &n_block);
1875 if (cdb[1] & (1 << 3))
1876 tf_flags |= ATA_TFLAG_FUA;
1877 if (!ata_check_nblocks(scmd, n_block))
1878 goto invalid_fld;
1879 break;
1880 default:
1881 DPRINTK("no-byte command\n");
1882 fp = 0;
1883 goto invalid_fld;
1884 }
1885
1886 /* Check and compose ATA command */
1887 if (!n_block)
1888 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1889 * length 0 means transfer 0 block of data.
1890 * However, for ATA R/W commands, sector count 0 means
1891 * 256 or 65536 sectors, not 0 sectors as in SCSI.
1892 *
1893 * WARNING: one or two older ATA drives treat 0 as 0...
1894 */
1895 goto nothing_to_do;
1896
1897 qc->flags |= ATA_QCFLAG_IO;
1898 qc->nbytes = n_block * scmd->device->sector_size;
1899
1900 rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
1901 qc->hw_tag, class);
1902
1903 if (likely(rc == 0))
1904 return 0;
1905
1906 if (rc == -ERANGE)
1907 goto out_of_range;
1908 /* treat all other errors as -EINVAL, fall through */
1909invalid_fld:
1910 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
1911 return 1;
1912
1913out_of_range:
1914 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
1915 /* "Logical Block Address out of range" */
1916 return 1;
1917
1918nothing_to_do:
1919 scmd->result = SAM_STAT_GOOD;
1920 return 1;
1921}
1922
1923static void ata_qc_done(struct ata_queued_cmd *qc)
1924{
1925 struct scsi_cmnd *cmd = qc->scsicmd;
1926 void (*done)(struct scsi_cmnd *) = qc->scsidone;
1927
1928 ata_qc_free(qc);
1929 done(cmd);
1930}
1931
1932static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
1933{
1934 struct ata_port *ap = qc->ap;
1935 struct scsi_cmnd *cmd = qc->scsicmd;
1936 u8 *cdb = cmd->cmnd;
1937 int need_sense = (qc->err_mask != 0);
1938
1939 /* For ATA pass thru (SAT) commands, generate a sense block if
1940 * user mandated it or if there's an error. Note that if we
1941 * generate because the user forced us to [CK_COND =1], a check
1942 * condition is generated and the ATA register values are returned
1943 * whether the command completed successfully or not. If there
1944 * was no error, we use the following sense data:
1945 * sk = RECOVERED ERROR
1946 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
1947 */
1948 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
1949 ((cdb[2] & 0x20) || need_sense))
1950 ata_gen_passthru_sense(qc);
1951 else if (qc->flags & ATA_QCFLAG_SENSE_VALID)
1952 cmd->result = SAM_STAT_CHECK_CONDITION;
1953 else if (need_sense)
1954 ata_gen_ata_sense(qc);
1955 else
1956 cmd->result = SAM_STAT_GOOD;
1957
1958 if (need_sense && !ap->ops->error_handler)
1959 ata_dump_status(ap->print_id, &qc->result_tf);
1960
1961 ata_qc_done(qc);
1962}
1963
1964/**
1965 * ata_scsi_translate - Translate then issue SCSI command to ATA device
1966 * @dev: ATA device to which the command is addressed
1967 * @cmd: SCSI command to execute
1968 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1969 *
1970 * Our ->queuecommand() function has decided that the SCSI
1971 * command issued can be directly translated into an ATA
1972 * command, rather than handled internally.
1973 *
1974 * This function sets up an ata_queued_cmd structure for the
1975 * SCSI command, and sends that ata_queued_cmd to the hardware.
1976 *
1977 * The xlat_func argument (actor) returns 0 if ready to execute
1978 * ATA command, else 1 to finish translation. If 1 is returned
1979 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1980 * to be set reflecting an error condition or clean (early)
1981 * termination.
1982 *
1983 * LOCKING:
1984 * spin_lock_irqsave(host lock)
1985 *
1986 * RETURNS:
1987 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
1988 * needs to be deferred.
1989 */
1990static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
1991 ata_xlat_func_t xlat_func)
1992{
1993 struct ata_port *ap = dev->link->ap;
1994 struct ata_queued_cmd *qc;
1995 int rc;
1996
1997 VPRINTK("ENTER\n");
1998
1999 qc = ata_scsi_qc_new(dev, cmd);
2000 if (!qc)
2001 goto err_mem;
2002
2003 /* data is present; dma-map it */
2004 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
2005 cmd->sc_data_direction == DMA_TO_DEVICE) {
2006 if (unlikely(scsi_bufflen(cmd) < 1)) {
2007 ata_dev_warn(dev, "WARNING: zero len r/w req\n");
2008 goto err_did;
2009 }
2010
2011 ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
2012
2013 qc->dma_dir = cmd->sc_data_direction;
2014 }
2015
2016 qc->complete_fn = ata_scsi_qc_complete;
2017
2018 if (xlat_func(qc))
2019 goto early_finish;
2020
2021 if (ap->ops->qc_defer) {
2022 if ((rc = ap->ops->qc_defer(qc)))
2023 goto defer;
2024 }
2025
2026 /* select device, send command to hardware */
2027 ata_qc_issue(qc);
2028
2029 VPRINTK("EXIT\n");
2030 return 0;
2031
2032early_finish:
2033 ata_qc_free(qc);
2034 cmd->scsi_done(cmd);
2035 DPRINTK("EXIT - early finish (good or error)\n");
2036 return 0;
2037
2038err_did:
2039 ata_qc_free(qc);
2040 cmd->result = (DID_ERROR << 16);
2041 cmd->scsi_done(cmd);
2042err_mem:
2043 DPRINTK("EXIT - internal\n");
2044 return 0;
2045
2046defer:
2047 ata_qc_free(qc);
2048 DPRINTK("EXIT - defer\n");
2049 if (rc == ATA_DEFER_LINK)
2050 return SCSI_MLQUEUE_DEVICE_BUSY;
2051 else
2052 return SCSI_MLQUEUE_HOST_BUSY;
2053}
2054
2055struct ata_scsi_args {
2056 struct ata_device *dev;
2057 u16 *id;
2058 struct scsi_cmnd *cmd;
2059};
2060
2061/**
2062 * ata_scsi_rbuf_get - Map response buffer.
2063 * @cmd: SCSI command containing buffer to be mapped.
2064 * @flags: unsigned long variable to store irq enable status
2065 * @copy_in: copy in from user buffer
2066 *
2067 * Prepare buffer for simulated SCSI commands.
2068 *
2069 * LOCKING:
2070 * spin_lock_irqsave(ata_scsi_rbuf_lock) on success
2071 *
2072 * RETURNS:
2073 * Pointer to response buffer.
2074 */
2075static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in,
2076 unsigned long *flags)
2077{
2078 spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags);
2079
2080 memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
2081 if (copy_in)
2082 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
2083 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
2084 return ata_scsi_rbuf;
2085}
2086
2087/**
2088 * ata_scsi_rbuf_put - Unmap response buffer.
2089 * @cmd: SCSI command containing buffer to be unmapped.
2090 * @copy_out: copy out result
2091 * @flags: @flags passed to ata_scsi_rbuf_get()
2092 *
2093 * Returns rbuf buffer. The result is copied to @cmd's buffer if
2094 * @copy_back is true.
2095 *
2096 * LOCKING:
2097 * Unlocks ata_scsi_rbuf_lock.
2098 */
2099static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out,
2100 unsigned long *flags)
2101{
2102 if (copy_out)
2103 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
2104 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
2105 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags);
2106}
2107
2108/**
2109 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
2110 * @args: device IDENTIFY data / SCSI command of interest.
2111 * @actor: Callback hook for desired SCSI command simulator
2112 *
2113 * Takes care of the hard work of simulating a SCSI command...
2114 * Mapping the response buffer, calling the command's handler,
2115 * and handling the handler's return value. This return value
2116 * indicates whether the handler wishes the SCSI command to be
2117 * completed successfully (0), or not (in which case cmd->result
2118 * and sense buffer are assumed to be set).
2119 *
2120 * LOCKING:
2121 * spin_lock_irqsave(host lock)
2122 */
2123static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
2124 unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
2125{
2126 u8 *rbuf;
2127 unsigned int rc;
2128 struct scsi_cmnd *cmd = args->cmd;
2129 unsigned long flags;
2130
2131 rbuf = ata_scsi_rbuf_get(cmd, false, &flags);
2132 rc = actor(args, rbuf);
2133 ata_scsi_rbuf_put(cmd, rc == 0, &flags);
2134
2135 if (rc == 0)
2136 cmd->result = SAM_STAT_GOOD;
2137}
2138
2139/**
2140 * ata_scsiop_inq_std - Simulate INQUIRY command
2141 * @args: device IDENTIFY data / SCSI command of interest.
2142 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2143 *
2144 * Returns standard device identification data associated
2145 * with non-VPD INQUIRY command output.
2146 *
2147 * LOCKING:
2148 * spin_lock_irqsave(host lock)
2149 */
2150static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
2151{
2152 static const u8 versions[] = {
2153 0x00,
2154 0x60, /* SAM-3 (no version claimed) */
2155
2156 0x03,
2157 0x20, /* SBC-2 (no version claimed) */
2158
2159 0x03,
2160 0x00 /* SPC-3 (no version claimed) */
2161 };
2162 static const u8 versions_zbc[] = {
2163 0x00,
2164 0xA0, /* SAM-5 (no version claimed) */
2165
2166 0x06,
2167 0x00, /* SBC-4 (no version claimed) */
2168
2169 0x05,
2170 0xC0, /* SPC-5 (no version claimed) */
2171
2172 0x60,
2173 0x24, /* ZBC r05 */
2174 };
2175
2176 u8 hdr[] = {
2177 TYPE_DISK,
2178 0,
2179 0x5, /* claim SPC-3 version compatibility */
2180 2,
2181 95 - 4,
2182 0,
2183 0,
2184 2
2185 };
2186
2187 VPRINTK("ENTER\n");
2188
2189 /* set scsi removable (RMB) bit per ata bit, or if the
2190 * AHCI port says it's external (Hotplug-capable, eSATA).
2191 */
2192 if (ata_id_removable(args->id) ||
2193 (args->dev->link->ap->pflags & ATA_PFLAG_EXTERNAL))
2194 hdr[1] |= (1 << 7);
2195
2196 if (args->dev->class == ATA_DEV_ZAC) {
2197 hdr[0] = TYPE_ZBC;
2198 hdr[2] = 0x7; /* claim SPC-5 version compatibility */
2199 }
2200
2201 memcpy(rbuf, hdr, sizeof(hdr));
2202 memcpy(&rbuf[8], "ATA ", 8);
2203 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
2204
2205 /* From SAT, use last 2 words from fw rev unless they are spaces */
2206 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV + 2, 4);
2207 if (strncmp(&rbuf[32], " ", 4) == 0)
2208 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
2209
2210 if (rbuf[32] == 0 || rbuf[32] == ' ')
2211 memcpy(&rbuf[32], "n/a ", 4);
2212
2213 if (ata_id_zoned_cap(args->id) || args->dev->class == ATA_DEV_ZAC)
2214 memcpy(rbuf + 58, versions_zbc, sizeof(versions_zbc));
2215 else
2216 memcpy(rbuf + 58, versions, sizeof(versions));
2217
2218 return 0;
2219}
2220
2221/**
2222 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
2223 * @args: device IDENTIFY data / SCSI command of interest.
2224 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2225 *
2226 * Returns list of inquiry VPD pages available.
2227 *
2228 * LOCKING:
2229 * spin_lock_irqsave(host lock)
2230 */
2231static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
2232{
2233 int num_pages;
2234 static const u8 pages[] = {
2235 0x00, /* page 0x00, this page */
2236 0x80, /* page 0x80, unit serial no page */
2237 0x83, /* page 0x83, device ident page */
2238 0x89, /* page 0x89, ata info page */
2239 0xb0, /* page 0xb0, block limits page */
2240 0xb1, /* page 0xb1, block device characteristics page */
2241 0xb2, /* page 0xb2, thin provisioning page */
2242 0xb6, /* page 0xb6, zoned block device characteristics */
2243 };
2244
2245 num_pages = sizeof(pages);
2246 if (!(args->dev->flags & ATA_DFLAG_ZAC))
2247 num_pages--;
2248 rbuf[3] = num_pages; /* number of supported VPD pages */
2249 memcpy(rbuf + 4, pages, num_pages);
2250 return 0;
2251}
2252
2253/**
2254 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
2255 * @args: device IDENTIFY data / SCSI command of interest.
2256 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2257 *
2258 * Returns ATA device serial number.
2259 *
2260 * LOCKING:
2261 * spin_lock_irqsave(host lock)
2262 */
2263static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf)
2264{
2265 static const u8 hdr[] = {
2266 0,
2267 0x80, /* this page code */
2268 0,
2269 ATA_ID_SERNO_LEN, /* page len */
2270 };
2271
2272 memcpy(rbuf, hdr, sizeof(hdr));
2273 ata_id_string(args->id, (unsigned char *) &rbuf[4],
2274 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
2275 return 0;
2276}
2277
2278/**
2279 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
2280 * @args: device IDENTIFY data / SCSI command of interest.
2281 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2282 *
2283 * Yields two logical unit device identification designators:
2284 * - vendor specific ASCII containing the ATA serial number
2285 * - SAT defined "t10 vendor id based" containing ASCII vendor
2286 * name ("ATA "), model and serial numbers.
2287 *
2288 * LOCKING:
2289 * spin_lock_irqsave(host lock)
2290 */
2291static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
2292{
2293 const int sat_model_serial_desc_len = 68;
2294 int num;
2295
2296 rbuf[1] = 0x83; /* this page code */
2297 num = 4;
2298
2299 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
2300 rbuf[num + 0] = 2;
2301 rbuf[num + 3] = ATA_ID_SERNO_LEN;
2302 num += 4;
2303 ata_id_string(args->id, (unsigned char *) rbuf + num,
2304 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
2305 num += ATA_ID_SERNO_LEN;
2306
2307 /* SAT defined lu model and serial numbers descriptor */
2308 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
2309 rbuf[num + 0] = 2;
2310 rbuf[num + 1] = 1;
2311 rbuf[num + 3] = sat_model_serial_desc_len;
2312 num += 4;
2313 memcpy(rbuf + num, "ATA ", 8);
2314 num += 8;
2315 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
2316 ATA_ID_PROD_LEN);
2317 num += ATA_ID_PROD_LEN;
2318 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
2319 ATA_ID_SERNO_LEN);
2320 num += ATA_ID_SERNO_LEN;
2321
2322 if (ata_id_has_wwn(args->id)) {
2323 /* SAT defined lu world wide name */
2324 /* piv=0, assoc=lu, code_set=binary, designator=NAA */
2325 rbuf[num + 0] = 1;
2326 rbuf[num + 1] = 3;
2327 rbuf[num + 3] = ATA_ID_WWN_LEN;
2328 num += 4;
2329 ata_id_string(args->id, (unsigned char *) rbuf + num,
2330 ATA_ID_WWN, ATA_ID_WWN_LEN);
2331 num += ATA_ID_WWN_LEN;
2332 }
2333 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
2334 return 0;
2335}
2336
2337/**
2338 * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
2339 * @args: device IDENTIFY data / SCSI command of interest.
2340 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2341 *
2342 * Yields SAT-specified ATA VPD page.
2343 *
2344 * LOCKING:
2345 * spin_lock_irqsave(host lock)
2346 */
2347static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
2348{
2349 struct ata_taskfile tf;
2350
2351 memset(&tf, 0, sizeof(tf));
2352
2353 rbuf[1] = 0x89; /* our page code */
2354 rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
2355 rbuf[3] = (0x238 & 0xff);
2356
2357 memcpy(&rbuf[8], "linux ", 8);
2358 memcpy(&rbuf[16], "libata ", 16);
2359 memcpy(&rbuf[32], DRV_VERSION, 4);
2360
2361 /* we don't store the ATA device signature, so we fake it */
2362
2363 tf.command = ATA_DRDY; /* really, this is Status reg */
2364 tf.lbal = 0x1;
2365 tf.nsect = 0x1;
2366
2367 ata_tf_to_fis(&tf, 0, 1, &rbuf[36]); /* TODO: PMP? */
2368 rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
2369
2370 rbuf[56] = ATA_CMD_ID_ATA;
2371
2372 memcpy(&rbuf[60], &args->id[0], 512);
2373 return 0;
2374}
2375
2376static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
2377{
2378 struct ata_device *dev = args->dev;
2379 u16 min_io_sectors;
2380
2381 rbuf[1] = 0xb0;
2382 rbuf[3] = 0x3c; /* required VPD size with unmap support */
2383
2384 /*
2385 * Optimal transfer length granularity.
2386 *
2387 * This is always one physical block, but for disks with a smaller
2388 * logical than physical sector size we need to figure out what the
2389 * latter is.
2390 */
2391 min_io_sectors = 1 << ata_id_log2_per_physical_sector(args->id);
2392 put_unaligned_be16(min_io_sectors, &rbuf[6]);
2393
2394 /*
2395 * Optimal unmap granularity.
2396 *
2397 * The ATA spec doesn't even know about a granularity or alignment
2398 * for the TRIM command. We can leave away most of the unmap related
2399 * VPD page entries, but we have specifify a granularity to signal
2400 * that we support some form of unmap - in thise case via WRITE SAME
2401 * with the unmap bit set.
2402 */
2403 if (ata_id_has_trim(args->id)) {
2404 u64 max_blocks = 65535 * ATA_MAX_TRIM_RNUM;
2405
2406 if (dev->horkage & ATA_HORKAGE_MAX_TRIM_128M)
2407 max_blocks = 128 << (20 - SECTOR_SHIFT);
2408
2409 put_unaligned_be64(max_blocks, &rbuf[36]);
2410 put_unaligned_be32(1, &rbuf[28]);
2411 }
2412
2413 return 0;
2414}
2415
2416static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
2417{
2418 int form_factor = ata_id_form_factor(args->id);
2419 int media_rotation_rate = ata_id_rotation_rate(args->id);
2420 u8 zoned = ata_id_zoned_cap(args->id);
2421
2422 rbuf[1] = 0xb1;
2423 rbuf[3] = 0x3c;
2424 rbuf[4] = media_rotation_rate >> 8;
2425 rbuf[5] = media_rotation_rate;
2426 rbuf[7] = form_factor;
2427 if (zoned)
2428 rbuf[8] = (zoned << 4);
2429
2430 return 0;
2431}
2432
2433static unsigned int ata_scsiop_inq_b2(struct ata_scsi_args *args, u8 *rbuf)
2434{
2435 /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */
2436 rbuf[1] = 0xb2;
2437 rbuf[3] = 0x4;
2438 rbuf[5] = 1 << 6; /* TPWS */
2439
2440 return 0;
2441}
2442
2443static unsigned int ata_scsiop_inq_b6(struct ata_scsi_args *args, u8 *rbuf)
2444{
2445 /*
2446 * zbc-r05 SCSI Zoned Block device characteristics VPD page
2447 */
2448 rbuf[1] = 0xb6;
2449 rbuf[3] = 0x3C;
2450
2451 /*
2452 * URSWRZ bit is only meaningful for host-managed ZAC drives
2453 */
2454 if (args->dev->zac_zoned_cap & 1)
2455 rbuf[4] |= 1;
2456 put_unaligned_be32(args->dev->zac_zones_optimal_open, &rbuf[8]);
2457 put_unaligned_be32(args->dev->zac_zones_optimal_nonseq, &rbuf[12]);
2458 put_unaligned_be32(args->dev->zac_zones_max_open, &rbuf[16]);
2459
2460 return 0;
2461}
2462
2463/**
2464 * modecpy - Prepare response for MODE SENSE
2465 * @dest: output buffer
2466 * @src: data being copied
2467 * @n: length of mode page
2468 * @changeable: whether changeable parameters are requested
2469 *
2470 * Generate a generic MODE SENSE page for either current or changeable
2471 * parameters.
2472 *
2473 * LOCKING:
2474 * None.
2475 */
2476static void modecpy(u8 *dest, const u8 *src, int n, bool changeable)
2477{
2478 if (changeable) {
2479 memcpy(dest, src, 2);
2480 memset(dest + 2, 0, n - 2);
2481 } else {
2482 memcpy(dest, src, n);
2483 }
2484}
2485
2486/**
2487 * ata_msense_caching - Simulate MODE SENSE caching info page
2488 * @id: device IDENTIFY data
2489 * @buf: output buffer
2490 * @changeable: whether changeable parameters are requested
2491 *
2492 * Generate a caching info page, which conditionally indicates
2493 * write caching to the SCSI layer, depending on device
2494 * capabilities.
2495 *
2496 * LOCKING:
2497 * None.
2498 */
2499static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable)
2500{
2501 modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable);
2502 if (changeable) {
2503 buf[2] |= (1 << 2); /* ata_mselect_caching() */
2504 } else {
2505 buf[2] |= (ata_id_wcache_enabled(id) << 2); /* write cache enable */
2506 buf[12] |= (!ata_id_rahead_enabled(id) << 5); /* disable read ahead */
2507 }
2508 return sizeof(def_cache_mpage);
2509}
2510
2511/**
2512 * ata_msense_control - Simulate MODE SENSE control mode page
2513 * @dev: ATA device of interest
2514 * @buf: output buffer
2515 * @changeable: whether changeable parameters are requested
2516 *
2517 * Generate a generic MODE SENSE control mode page.
2518 *
2519 * LOCKING:
2520 * None.
2521 */
2522static unsigned int ata_msense_control(struct ata_device *dev, u8 *buf,
2523 bool changeable)
2524{
2525 modecpy(buf, def_control_mpage, sizeof(def_control_mpage), changeable);
2526 if (changeable) {
2527 buf[2] |= (1 << 2); /* ata_mselect_control() */
2528 } else {
2529 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
2530
2531 buf[2] |= (d_sense << 2); /* descriptor format sense data */
2532 }
2533 return sizeof(def_control_mpage);
2534}
2535
2536/**
2537 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
2538 * @buf: output buffer
2539 * @changeable: whether changeable parameters are requested
2540 *
2541 * Generate a generic MODE SENSE r/w error recovery page.
2542 *
2543 * LOCKING:
2544 * None.
2545 */
2546static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable)
2547{
2548 modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage),
2549 changeable);
2550 return sizeof(def_rw_recovery_mpage);
2551}
2552
2553/*
2554 * We can turn this into a real blacklist if it's needed, for now just
2555 * blacklist any Maxtor BANC1G10 revision firmware
2556 */
2557static int ata_dev_supports_fua(u16 *id)
2558{
2559 unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
2560
2561 if (!libata_fua)
2562 return 0;
2563 if (!ata_id_has_fua(id))
2564 return 0;
2565
2566 ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
2567 ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
2568
2569 if (strcmp(model, "Maxtor"))
2570 return 1;
2571 if (strcmp(fw, "BANC1G10"))
2572 return 1;
2573
2574 return 0; /* blacklisted */
2575}
2576
2577/**
2578 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
2579 * @args: device IDENTIFY data / SCSI command of interest.
2580 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2581 *
2582 * Simulate MODE SENSE commands. Assume this is invoked for direct
2583 * access devices (e.g. disks) only. There should be no block
2584 * descriptor for other device types.
2585 *
2586 * LOCKING:
2587 * spin_lock_irqsave(host lock)
2588 */
2589static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
2590{
2591 struct ata_device *dev = args->dev;
2592 u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
2593 static const u8 sat_blk_desc[] = {
2594 0, 0, 0, 0, /* number of blocks: sat unspecified */
2595 0,
2596 0, 0x2, 0x0 /* block length: 512 bytes */
2597 };
2598 u8 pg, spg;
2599 unsigned int ebd, page_control, six_byte;
2600 u8 dpofua, bp = 0xff;
2601 u16 fp;
2602
2603 VPRINTK("ENTER\n");
2604
2605 six_byte = (scsicmd[0] == MODE_SENSE);
2606 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
2607 /*
2608 * LLBA bit in msense(10) ignored (compliant)
2609 */
2610
2611 page_control = scsicmd[2] >> 6;
2612 switch (page_control) {
2613 case 0: /* current */
2614 case 1: /* changeable */
2615 case 2: /* defaults */
2616 break; /* supported */
2617 case 3: /* saved */
2618 goto saving_not_supp;
2619 default:
2620 fp = 2;
2621 bp = 6;
2622 goto invalid_fld;
2623 }
2624
2625 if (six_byte)
2626 p += 4 + (ebd ? 8 : 0);
2627 else
2628 p += 8 + (ebd ? 8 : 0);
2629
2630 pg = scsicmd[2] & 0x3f;
2631 spg = scsicmd[3];
2632 /*
2633 * No mode subpages supported (yet) but asking for _all_
2634 * subpages may be valid
2635 */
2636 if (spg && (spg != ALL_SUB_MPAGES)) {
2637 fp = 3;
2638 goto invalid_fld;
2639 }
2640
2641 switch(pg) {
2642 case RW_RECOVERY_MPAGE:
2643 p += ata_msense_rw_recovery(p, page_control == 1);
2644 break;
2645
2646 case CACHE_MPAGE:
2647 p += ata_msense_caching(args->id, p, page_control == 1);
2648 break;
2649
2650 case CONTROL_MPAGE:
2651 p += ata_msense_control(args->dev, p, page_control == 1);
2652 break;
2653
2654 case ALL_MPAGES:
2655 p += ata_msense_rw_recovery(p, page_control == 1);
2656 p += ata_msense_caching(args->id, p, page_control == 1);
2657 p += ata_msense_control(args->dev, p, page_control == 1);
2658 break;
2659
2660 default: /* invalid page code */
2661 fp = 2;
2662 goto invalid_fld;
2663 }
2664
2665 dpofua = 0;
2666 if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
2667 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
2668 dpofua = 1 << 4;
2669
2670 if (six_byte) {
2671 rbuf[0] = p - rbuf - 1;
2672 rbuf[2] |= dpofua;
2673 if (ebd) {
2674 rbuf[3] = sizeof(sat_blk_desc);
2675 memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
2676 }
2677 } else {
2678 unsigned int output_len = p - rbuf - 2;
2679
2680 rbuf[0] = output_len >> 8;
2681 rbuf[1] = output_len;
2682 rbuf[3] |= dpofua;
2683 if (ebd) {
2684 rbuf[7] = sizeof(sat_blk_desc);
2685 memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
2686 }
2687 }
2688 return 0;
2689
2690invalid_fld:
2691 ata_scsi_set_invalid_field(dev, args->cmd, fp, bp);
2692 return 1;
2693
2694saving_not_supp:
2695 ata_scsi_set_sense(dev, args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
2696 /* "Saving parameters not supported" */
2697 return 1;
2698}
2699
2700/**
2701 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2702 * @args: device IDENTIFY data / SCSI command of interest.
2703 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2704 *
2705 * Simulate READ CAPACITY commands.
2706 *
2707 * LOCKING:
2708 * None.
2709 */
2710static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
2711{
2712 struct ata_device *dev = args->dev;
2713 u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
2714 u32 sector_size; /* physical sector size in bytes */
2715 u8 log2_per_phys;
2716 u16 lowest_aligned;
2717
2718 sector_size = ata_id_logical_sector_size(dev->id);
2719 log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
2720 lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
2721
2722 VPRINTK("ENTER\n");
2723
2724 if (args->cmd->cmnd[0] == READ_CAPACITY) {
2725 if (last_lba >= 0xffffffffULL)
2726 last_lba = 0xffffffff;
2727
2728 /* sector count, 32-bit */
2729 rbuf[0] = last_lba >> (8 * 3);
2730 rbuf[1] = last_lba >> (8 * 2);
2731 rbuf[2] = last_lba >> (8 * 1);
2732 rbuf[3] = last_lba;
2733
2734 /* sector size */
2735 rbuf[4] = sector_size >> (8 * 3);
2736 rbuf[5] = sector_size >> (8 * 2);
2737 rbuf[6] = sector_size >> (8 * 1);
2738 rbuf[7] = sector_size;
2739 } else {
2740 /* sector count, 64-bit */
2741 rbuf[0] = last_lba >> (8 * 7);
2742 rbuf[1] = last_lba >> (8 * 6);
2743 rbuf[2] = last_lba >> (8 * 5);
2744 rbuf[3] = last_lba >> (8 * 4);
2745 rbuf[4] = last_lba >> (8 * 3);
2746 rbuf[5] = last_lba >> (8 * 2);
2747 rbuf[6] = last_lba >> (8 * 1);
2748 rbuf[7] = last_lba;
2749
2750 /* sector size */
2751 rbuf[ 8] = sector_size >> (8 * 3);
2752 rbuf[ 9] = sector_size >> (8 * 2);
2753 rbuf[10] = sector_size >> (8 * 1);
2754 rbuf[11] = sector_size;
2755
2756 rbuf[12] = 0;
2757 rbuf[13] = log2_per_phys;
2758 rbuf[14] = (lowest_aligned >> 8) & 0x3f;
2759 rbuf[15] = lowest_aligned;
2760
2761 if (ata_id_has_trim(args->id) &&
2762 !(dev->horkage & ATA_HORKAGE_NOTRIM)) {
2763 rbuf[14] |= 0x80; /* LBPME */
2764
2765 if (ata_id_has_zero_after_trim(args->id) &&
2766 dev->horkage & ATA_HORKAGE_ZERO_AFTER_TRIM) {
2767 ata_dev_info(dev, "Enabling discard_zeroes_data\n");
2768 rbuf[14] |= 0x40; /* LBPRZ */
2769 }
2770 }
2771 if (ata_id_zoned_cap(args->id) ||
2772 args->dev->class == ATA_DEV_ZAC)
2773 rbuf[12] = (1 << 4); /* RC_BASIS */
2774 }
2775 return 0;
2776}
2777
2778/**
2779 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2780 * @args: device IDENTIFY data / SCSI command of interest.
2781 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2782 *
2783 * Simulate REPORT LUNS command.
2784 *
2785 * LOCKING:
2786 * spin_lock_irqsave(host lock)
2787 */
2788static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
2789{
2790 VPRINTK("ENTER\n");
2791 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2792
2793 return 0;
2794}
2795
2796static void atapi_sense_complete(struct ata_queued_cmd *qc)
2797{
2798 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
2799 /* FIXME: not quite right; we don't want the
2800 * translation of taskfile registers into
2801 * a sense descriptors, since that's only
2802 * correct for ATA, not ATAPI
2803 */
2804 ata_gen_passthru_sense(qc);
2805 }
2806
2807 ata_qc_done(qc);
2808}
2809
2810/* is it pointless to prefer PIO for "safety reasons"? */
2811static inline int ata_pio_use_silly(struct ata_port *ap)
2812{
2813 return (ap->flags & ATA_FLAG_PIO_DMA);
2814}
2815
2816static void atapi_request_sense(struct ata_queued_cmd *qc)
2817{
2818 struct ata_port *ap = qc->ap;
2819 struct scsi_cmnd *cmd = qc->scsicmd;
2820
2821 DPRINTK("ATAPI request sense\n");
2822
2823 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
2824
2825#ifdef CONFIG_ATA_SFF
2826 if (ap->ops->sff_tf_read)
2827 ap->ops->sff_tf_read(ap, &qc->tf);
2828#endif
2829
2830 /* fill these in, for the case where they are -not- overwritten */
2831 cmd->sense_buffer[0] = 0x70;
2832 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2833
2834 ata_qc_reinit(qc);
2835
2836 /* setup sg table and init transfer direction */
2837 sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
2838 ata_sg_init(qc, &qc->sgent, 1);
2839 qc->dma_dir = DMA_FROM_DEVICE;
2840
2841 memset(&qc->cdb, 0, qc->dev->cdb_len);
2842 qc->cdb[0] = REQUEST_SENSE;
2843 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2844
2845 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2846 qc->tf.command = ATA_CMD_PACKET;
2847
2848 if (ata_pio_use_silly(ap)) {
2849 qc->tf.protocol = ATAPI_PROT_DMA;
2850 qc->tf.feature |= ATAPI_PKT_DMA;
2851 } else {
2852 qc->tf.protocol = ATAPI_PROT_PIO;
2853 qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
2854 qc->tf.lbah = 0;
2855 }
2856 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2857
2858 qc->complete_fn = atapi_sense_complete;
2859
2860 ata_qc_issue(qc);
2861
2862 DPRINTK("EXIT\n");
2863}
2864
2865/*
2866 * ATAPI devices typically report zero for their SCSI version, and sometimes
2867 * deviate from the spec WRT response data format. If SCSI version is
2868 * reported as zero like normal, then we make the following fixups:
2869 * 1) Fake MMC-5 version, to indicate to the Linux scsi midlayer this is a
2870 * modern device.
2871 * 2) Ensure response data format / ATAPI information are always correct.
2872 */
2873static void atapi_fixup_inquiry(struct scsi_cmnd *cmd)
2874{
2875 u8 buf[4];
2876
2877 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
2878 if (buf[2] == 0) {
2879 buf[2] = 0x5;
2880 buf[3] = 0x32;
2881 }
2882 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
2883}
2884
2885static void atapi_qc_complete(struct ata_queued_cmd *qc)
2886{
2887 struct scsi_cmnd *cmd = qc->scsicmd;
2888 unsigned int err_mask = qc->err_mask;
2889
2890 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
2891
2892 /* handle completion from new EH */
2893 if (unlikely(qc->ap->ops->error_handler &&
2894 (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
2895
2896 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
2897 /* FIXME: not quite right; we don't want the
2898 * translation of taskfile registers into a
2899 * sense descriptors, since that's only
2900 * correct for ATA, not ATAPI
2901 */
2902 ata_gen_passthru_sense(qc);
2903 }
2904
2905 /* SCSI EH automatically locks door if sdev->locked is
2906 * set. Sometimes door lock request continues to
2907 * fail, for example, when no media is present. This
2908 * creates a loop - SCSI EH issues door lock which
2909 * fails and gets invoked again to acquire sense data
2910 * for the failed command.
2911 *
2912 * If door lock fails, always clear sdev->locked to
2913 * avoid this infinite loop.
2914 *
2915 * This may happen before SCSI scan is complete. Make
2916 * sure qc->dev->sdev isn't NULL before dereferencing.
2917 */
2918 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
2919 qc->dev->sdev->locked = 0;
2920
2921 qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
2922 ata_qc_done(qc);
2923 return;
2924 }
2925
2926 /* successful completion or old EH failure path */
2927 if (unlikely(err_mask & AC_ERR_DEV)) {
2928 cmd->result = SAM_STAT_CHECK_CONDITION;
2929 atapi_request_sense(qc);
2930 return;
2931 } else if (unlikely(err_mask)) {
2932 /* FIXME: not quite right; we don't want the
2933 * translation of taskfile registers into
2934 * a sense descriptors, since that's only
2935 * correct for ATA, not ATAPI
2936 */
2937 ata_gen_passthru_sense(qc);
2938 } else {
2939 if (cmd->cmnd[0] == INQUIRY && (cmd->cmnd[1] & 0x03) == 0)
2940 atapi_fixup_inquiry(cmd);
2941 cmd->result = SAM_STAT_GOOD;
2942 }
2943
2944 ata_qc_done(qc);
2945}
2946/**
2947 * atapi_xlat - Initialize PACKET taskfile
2948 * @qc: command structure to be initialized
2949 *
2950 * LOCKING:
2951 * spin_lock_irqsave(host lock)
2952 *
2953 * RETURNS:
2954 * Zero on success, non-zero on failure.
2955 */
2956static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
2957{
2958 struct scsi_cmnd *scmd = qc->scsicmd;
2959 struct ata_device *dev = qc->dev;
2960 int nodata = (scmd->sc_data_direction == DMA_NONE);
2961 int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
2962 unsigned int nbytes;
2963
2964 memset(qc->cdb, 0, dev->cdb_len);
2965 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
2966
2967 qc->complete_fn = atapi_qc_complete;
2968
2969 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2970 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
2971 qc->tf.flags |= ATA_TFLAG_WRITE;
2972 DPRINTK("direction: write\n");
2973 }
2974
2975 qc->tf.command = ATA_CMD_PACKET;
2976 ata_qc_set_pc_nbytes(qc);
2977
2978 /* check whether ATAPI DMA is safe */
2979 if (!nodata && !using_pio && atapi_check_dma(qc))
2980 using_pio = 1;
2981
2982 /* Some controller variants snoop this value for Packet
2983 * transfers to do state machine and FIFO management. Thus we
2984 * want to set it properly, and for DMA where it is
2985 * effectively meaningless.
2986 */
2987 nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
2988
2989 /* Most ATAPI devices which honor transfer chunk size don't
2990 * behave according to the spec when odd chunk size which
2991 * matches the transfer length is specified. If the number of
2992 * bytes to transfer is 2n+1. According to the spec, what
2993 * should happen is to indicate that 2n+1 is going to be
2994 * transferred and transfer 2n+2 bytes where the last byte is
2995 * padding.
2996 *
2997 * In practice, this doesn't happen. ATAPI devices first
2998 * indicate and transfer 2n bytes and then indicate and
2999 * transfer 2 bytes where the last byte is padding.
3000 *
3001 * This inconsistency confuses several controllers which
3002 * perform PIO using DMA such as Intel AHCIs and sil3124/32.
3003 * These controllers use actual number of transferred bytes to
3004 * update DMA pointer and transfer of 4n+2 bytes make those
3005 * controller push DMA pointer by 4n+4 bytes because SATA data
3006 * FISes are aligned to 4 bytes. This causes data corruption
3007 * and buffer overrun.
3008 *
3009 * Always setting nbytes to even number solves this problem
3010 * because then ATAPI devices don't have to split data at 2n
3011 * boundaries.
3012 */
3013 if (nbytes & 0x1)
3014 nbytes++;
3015
3016 qc->tf.lbam = (nbytes & 0xFF);
3017 qc->tf.lbah = (nbytes >> 8);
3018
3019 if (nodata)
3020 qc->tf.protocol = ATAPI_PROT_NODATA;
3021 else if (using_pio)
3022 qc->tf.protocol = ATAPI_PROT_PIO;
3023 else {
3024 /* DMA data xfer */
3025 qc->tf.protocol = ATAPI_PROT_DMA;
3026 qc->tf.feature |= ATAPI_PKT_DMA;
3027
3028 if ((dev->flags & ATA_DFLAG_DMADIR) &&
3029 (scmd->sc_data_direction != DMA_TO_DEVICE))
3030 /* some SATA bridges need us to indicate data xfer direction */
3031 qc->tf.feature |= ATAPI_DMADIR;
3032 }
3033
3034
3035 /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
3036 as ATAPI tape drives don't get this right otherwise */
3037 return 0;
3038}
3039
3040static struct ata_device *ata_find_dev(struct ata_port *ap, unsigned int devno)
3041{
3042 /*
3043 * For the non-PMP case, ata_link_max_devices() returns 1 (SATA case),
3044 * or 2 (IDE master + slave case). However, the former case includes
3045 * libsas hosted devices which are numbered per scsi host, leading
3046 * to devno potentially being larger than 0 but with each struct
3047 * ata_device having its own struct ata_port and struct ata_link.
3048 * To accommodate these, ignore devno and always use device number 0.
3049 */
3050 if (likely(!sata_pmp_attached(ap))) {
3051 int link_max_devices = ata_link_max_devices(&ap->link);
3052
3053 if (link_max_devices == 1)
3054 return &ap->link.device[0];
3055
3056 if (devno < link_max_devices)
3057 return &ap->link.device[devno];
3058
3059 return NULL;
3060 }
3061
3062 /*
3063 * For PMP-attached devices, the device number corresponds to C
3064 * (channel) of SCSI [H:C:I:L], indicating the port pmp link
3065 * for the device.
3066 */
3067 if (devno < ap->nr_pmp_links)
3068 return &ap->pmp_link[devno].device[0];
3069
3070 return NULL;
3071}
3072
3073static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
3074 const struct scsi_device *scsidev)
3075{
3076 int devno;
3077
3078 /* skip commands not addressed to targets we simulate */
3079 if (!sata_pmp_attached(ap)) {
3080 if (unlikely(scsidev->channel || scsidev->lun))
3081 return NULL;
3082 devno = scsidev->id;
3083 } else {
3084 if (unlikely(scsidev->id || scsidev->lun))
3085 return NULL;
3086 devno = scsidev->channel;
3087 }
3088
3089 return ata_find_dev(ap, devno);
3090}
3091
3092/**
3093 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
3094 * @ap: ATA port to which the device is attached
3095 * @scsidev: SCSI device from which we derive the ATA device
3096 *
3097 * Given various information provided in struct scsi_cmnd,
3098 * map that onto an ATA bus, and using that mapping
3099 * determine which ata_device is associated with the
3100 * SCSI command to be sent.
3101 *
3102 * LOCKING:
3103 * spin_lock_irqsave(host lock)
3104 *
3105 * RETURNS:
3106 * Associated ATA device, or %NULL if not found.
3107 */
3108static struct ata_device *
3109ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
3110{
3111 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
3112
3113 if (unlikely(!dev || !ata_dev_enabled(dev)))
3114 return NULL;
3115
3116 return dev;
3117}
3118
3119/*
3120 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
3121 * @byte1: Byte 1 from pass-thru CDB.
3122 *
3123 * RETURNS:
3124 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
3125 */
3126static u8
3127ata_scsi_map_proto(u8 byte1)
3128{
3129 switch((byte1 & 0x1e) >> 1) {
3130 case 3: /* Non-data */
3131 return ATA_PROT_NODATA;
3132
3133 case 6: /* DMA */
3134 case 10: /* UDMA Data-in */
3135 case 11: /* UDMA Data-Out */
3136 return ATA_PROT_DMA;
3137
3138 case 4: /* PIO Data-in */
3139 case 5: /* PIO Data-out */
3140 return ATA_PROT_PIO;
3141
3142 case 12: /* FPDMA */
3143 return ATA_PROT_NCQ;
3144
3145 case 0: /* Hard Reset */
3146 case 1: /* SRST */
3147 case 8: /* Device Diagnostic */
3148 case 9: /* Device Reset */
3149 case 7: /* DMA Queued */
3150 case 15: /* Return Response Info */
3151 default: /* Reserved */
3152 break;
3153 }
3154
3155 return ATA_PROT_UNKNOWN;
3156}
3157
3158/**
3159 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
3160 * @qc: command structure to be initialized
3161 *
3162 * Handles either 12, 16, or 32-byte versions of the CDB.
3163 *
3164 * RETURNS:
3165 * Zero on success, non-zero on failure.
3166 */
3167static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
3168{
3169 struct ata_taskfile *tf = &(qc->tf);
3170 struct scsi_cmnd *scmd = qc->scsicmd;
3171 struct ata_device *dev = qc->dev;
3172 const u8 *cdb = scmd->cmnd;
3173 u16 fp;
3174 u16 cdb_offset = 0;
3175
3176 /* 7Fh variable length cmd means a ata pass-thru(32) */
3177 if (cdb[0] == VARIABLE_LENGTH_CMD)
3178 cdb_offset = 9;
3179
3180 tf->protocol = ata_scsi_map_proto(cdb[1 + cdb_offset]);
3181 if (tf->protocol == ATA_PROT_UNKNOWN) {
3182 fp = 1;
3183 goto invalid_fld;
3184 }
3185
3186 if ((cdb[2 + cdb_offset] & 0x3) == 0) {
3187 /*
3188 * When T_LENGTH is zero (No data is transferred), dir should
3189 * be DMA_NONE.
3190 */
3191 if (scmd->sc_data_direction != DMA_NONE) {
3192 fp = 2 + cdb_offset;
3193 goto invalid_fld;
3194 }
3195
3196 if (ata_is_ncq(tf->protocol))
3197 tf->protocol = ATA_PROT_NCQ_NODATA;
3198 }
3199
3200 /* enable LBA */
3201 tf->flags |= ATA_TFLAG_LBA;
3202
3203 /*
3204 * 12 and 16 byte CDBs use different offsets to
3205 * provide the various register values.
3206 */
3207 if (cdb[0] == ATA_16) {
3208 /*
3209 * 16-byte CDB - may contain extended commands.
3210 *
3211 * If that is the case, copy the upper byte register values.
3212 */
3213 if (cdb[1] & 0x01) {
3214 tf->hob_feature = cdb[3];
3215 tf->hob_nsect = cdb[5];
3216 tf->hob_lbal = cdb[7];
3217 tf->hob_lbam = cdb[9];
3218 tf->hob_lbah = cdb[11];
3219 tf->flags |= ATA_TFLAG_LBA48;
3220 } else
3221 tf->flags &= ~ATA_TFLAG_LBA48;
3222
3223 /*
3224 * Always copy low byte, device and command registers.
3225 */
3226 tf->feature = cdb[4];
3227 tf->nsect = cdb[6];
3228 tf->lbal = cdb[8];
3229 tf->lbam = cdb[10];
3230 tf->lbah = cdb[12];
3231 tf->device = cdb[13];
3232 tf->command = cdb[14];
3233 } else if (cdb[0] == ATA_12) {
3234 /*
3235 * 12-byte CDB - incapable of extended commands.
3236 */
3237 tf->flags &= ~ATA_TFLAG_LBA48;
3238
3239 tf->feature = cdb[3];
3240 tf->nsect = cdb[4];
3241 tf->lbal = cdb[5];
3242 tf->lbam = cdb[6];
3243 tf->lbah = cdb[7];
3244 tf->device = cdb[8];
3245 tf->command = cdb[9];
3246 } else {
3247 /*
3248 * 32-byte CDB - may contain extended command fields.
3249 *
3250 * If that is the case, copy the upper byte register values.
3251 */
3252 if (cdb[10] & 0x01) {
3253 tf->hob_feature = cdb[20];
3254 tf->hob_nsect = cdb[22];
3255 tf->hob_lbal = cdb[16];
3256 tf->hob_lbam = cdb[15];
3257 tf->hob_lbah = cdb[14];
3258 tf->flags |= ATA_TFLAG_LBA48;
3259 } else
3260 tf->flags &= ~ATA_TFLAG_LBA48;
3261
3262 tf->feature = cdb[21];
3263 tf->nsect = cdb[23];
3264 tf->lbal = cdb[19];
3265 tf->lbam = cdb[18];
3266 tf->lbah = cdb[17];
3267 tf->device = cdb[24];
3268 tf->command = cdb[25];
3269 tf->auxiliary = get_unaligned_be32(&cdb[28]);
3270 }
3271
3272 /* For NCQ commands copy the tag value */
3273 if (ata_is_ncq(tf->protocol))
3274 tf->nsect = qc->hw_tag << 3;
3275
3276 /* enforce correct master/slave bit */
3277 tf->device = dev->devno ?
3278 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
3279
3280 switch (tf->command) {
3281 /* READ/WRITE LONG use a non-standard sect_size */
3282 case ATA_CMD_READ_LONG:
3283 case ATA_CMD_READ_LONG_ONCE:
3284 case ATA_CMD_WRITE_LONG:
3285 case ATA_CMD_WRITE_LONG_ONCE:
3286 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1) {
3287 fp = 1;
3288 goto invalid_fld;
3289 }
3290 qc->sect_size = scsi_bufflen(scmd);
3291 break;
3292
3293 /* commands using reported Logical Block size (e.g. 512 or 4K) */
3294 case ATA_CMD_CFA_WRITE_NE:
3295 case ATA_CMD_CFA_TRANS_SECT:
3296 case ATA_CMD_CFA_WRITE_MULT_NE:
3297 /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */
3298 case ATA_CMD_READ:
3299 case ATA_CMD_READ_EXT:
3300 case ATA_CMD_READ_QUEUED:
3301 /* XXX: case ATA_CMD_READ_QUEUED_EXT: */
3302 case ATA_CMD_FPDMA_READ:
3303 case ATA_CMD_READ_MULTI:
3304 case ATA_CMD_READ_MULTI_EXT:
3305 case ATA_CMD_PIO_READ:
3306 case ATA_CMD_PIO_READ_EXT:
3307 case ATA_CMD_READ_STREAM_DMA_EXT:
3308 case ATA_CMD_READ_STREAM_EXT:
3309 case ATA_CMD_VERIFY:
3310 case ATA_CMD_VERIFY_EXT:
3311 case ATA_CMD_WRITE:
3312 case ATA_CMD_WRITE_EXT:
3313 case ATA_CMD_WRITE_FUA_EXT:
3314 case ATA_CMD_WRITE_QUEUED:
3315 case ATA_CMD_WRITE_QUEUED_FUA_EXT:
3316 case ATA_CMD_FPDMA_WRITE:
3317 case ATA_CMD_WRITE_MULTI:
3318 case ATA_CMD_WRITE_MULTI_EXT:
3319 case ATA_CMD_WRITE_MULTI_FUA_EXT:
3320 case ATA_CMD_PIO_WRITE:
3321 case ATA_CMD_PIO_WRITE_EXT:
3322 case ATA_CMD_WRITE_STREAM_DMA_EXT:
3323 case ATA_CMD_WRITE_STREAM_EXT:
3324 qc->sect_size = scmd->device->sector_size;
3325 break;
3326
3327 /* Everything else uses 512 byte "sectors" */
3328 default:
3329 qc->sect_size = ATA_SECT_SIZE;
3330 }
3331
3332 /*
3333 * Set flags so that all registers will be written, pass on
3334 * write indication (used for PIO/DMA setup), result TF is
3335 * copied back and we don't whine too much about its failure.
3336 */
3337 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3338 if (scmd->sc_data_direction == DMA_TO_DEVICE)
3339 tf->flags |= ATA_TFLAG_WRITE;
3340
3341 qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
3342
3343 /*
3344 * Set transfer length.
3345 *
3346 * TODO: find out if we need to do more here to
3347 * cover scatter/gather case.
3348 */
3349 ata_qc_set_pc_nbytes(qc);
3350
3351 /* We may not issue DMA commands if no DMA mode is set */
3352 if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0) {
3353 fp = 1;
3354 goto invalid_fld;
3355 }
3356
3357 /* We may not issue NCQ commands to devices not supporting NCQ */
3358 if (ata_is_ncq(tf->protocol) && !ata_ncq_enabled(dev)) {
3359 fp = 1;
3360 goto invalid_fld;
3361 }
3362
3363 /* sanity check for pio multi commands */
3364 if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf)) {
3365 fp = 1;
3366 goto invalid_fld;
3367 }
3368
3369 if (is_multi_taskfile(tf)) {
3370 unsigned int multi_count = 1 << (cdb[1] >> 5);
3371
3372 /* compare the passed through multi_count
3373 * with the cached multi_count of libata
3374 */
3375 if (multi_count != dev->multi_count)
3376 ata_dev_warn(dev, "invalid multi_count %u ignored\n",
3377 multi_count);
3378 }
3379
3380 /*
3381 * Filter SET_FEATURES - XFER MODE command -- otherwise,
3382 * SET_FEATURES - XFER MODE must be preceded/succeeded
3383 * by an update to hardware-specific registers for each
3384 * controller (i.e. the reason for ->set_piomode(),
3385 * ->set_dmamode(), and ->post_set_mode() hooks).
3386 */
3387 if (tf->command == ATA_CMD_SET_FEATURES &&
3388 tf->feature == SETFEATURES_XFER) {
3389 fp = (cdb[0] == ATA_16) ? 4 : 3;
3390 goto invalid_fld;
3391 }
3392
3393 /*
3394 * Filter TPM commands by default. These provide an
3395 * essentially uncontrolled encrypted "back door" between
3396 * applications and the disk. Set libata.allow_tpm=1 if you
3397 * have a real reason for wanting to use them. This ensures
3398 * that installed software cannot easily mess stuff up without
3399 * user intent. DVR type users will probably ship with this enabled
3400 * for movie content management.
3401 *
3402 * Note that for ATA8 we can issue a DCS change and DCS freeze lock
3403 * for this and should do in future but that it is not sufficient as
3404 * DCS is an optional feature set. Thus we also do the software filter
3405 * so that we comply with the TC consortium stated goal that the user
3406 * can turn off TC features of their system.
3407 */
3408 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm) {
3409 fp = (cdb[0] == ATA_16) ? 14 : 9;
3410 goto invalid_fld;
3411 }
3412
3413 return 0;
3414
3415 invalid_fld:
3416 ata_scsi_set_invalid_field(dev, scmd, fp, 0xff);
3417 return 1;
3418}
3419
3420/**
3421 * ata_format_dsm_trim_descr() - SATL Write Same to DSM Trim
3422 * @cmd: SCSI command being translated
3423 * @trmax: Maximum number of entries that will fit in sector_size bytes.
3424 * @sector: Starting sector
3425 * @count: Total Range of request in logical sectors
3426 *
3427 * Rewrite the WRITE SAME descriptor to be a DSM TRIM little-endian formatted
3428 * descriptor.
3429 *
3430 * Upto 64 entries of the format:
3431 * 63:48 Range Length
3432 * 47:0 LBA
3433 *
3434 * Range Length of 0 is ignored.
3435 * LBA's should be sorted order and not overlap.
3436 *
3437 * NOTE: this is the same format as ADD LBA(S) TO NV CACHE PINNED SET
3438 *
3439 * Return: Number of bytes copied into sglist.
3440 */
3441static size_t ata_format_dsm_trim_descr(struct scsi_cmnd *cmd, u32 trmax,
3442 u64 sector, u32 count)
3443{
3444 struct scsi_device *sdp = cmd->device;
3445 size_t len = sdp->sector_size;
3446 size_t r;
3447 __le64 *buf;
3448 u32 i = 0;
3449 unsigned long flags;
3450
3451 WARN_ON(len > ATA_SCSI_RBUF_SIZE);
3452
3453 if (len > ATA_SCSI_RBUF_SIZE)
3454 len = ATA_SCSI_RBUF_SIZE;
3455
3456 spin_lock_irqsave(&ata_scsi_rbuf_lock, flags);
3457 buf = ((void *)ata_scsi_rbuf);
3458 memset(buf, 0, len);
3459 while (i < trmax) {
3460 u64 entry = sector |
3461 ((u64)(count > 0xffff ? 0xffff : count) << 48);
3462 buf[i++] = __cpu_to_le64(entry);
3463 if (count <= 0xffff)
3464 break;
3465 count -= 0xffff;
3466 sector += 0xffff;
3467 }
3468 r = sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, len);
3469 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, flags);
3470
3471 return r;
3472}
3473
3474/**
3475 * ata_scsi_write_same_xlat() - SATL Write Same to ATA SCT Write Same
3476 * @qc: Command to be translated
3477 *
3478 * Translate a SCSI WRITE SAME command to be either a DSM TRIM command or
3479 * an SCT Write Same command.
3480 * Based on WRITE SAME has the UNMAP flag:
3481 *
3482 * - When set translate to DSM TRIM
3483 * - When clear translate to SCT Write Same
3484 */
3485static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
3486{
3487 struct ata_taskfile *tf = &qc->tf;
3488 struct scsi_cmnd *scmd = qc->scsicmd;
3489 struct scsi_device *sdp = scmd->device;
3490 size_t len = sdp->sector_size;
3491 struct ata_device *dev = qc->dev;
3492 const u8 *cdb = scmd->cmnd;
3493 u64 block;
3494 u32 n_block;
3495 const u32 trmax = len >> 3;
3496 u32 size;
3497 u16 fp;
3498 u8 bp = 0xff;
3499 u8 unmap = cdb[1] & 0x8;
3500
3501 /* we may not issue DMA commands if no DMA mode is set */
3502 if (unlikely(!dev->dma_mode))
3503 goto invalid_opcode;
3504
3505 /*
3506 * We only allow sending this command through the block layer,
3507 * as it modifies the DATA OUT buffer, which would corrupt user
3508 * memory for SG_IO commands.
3509 */
3510 if (unlikely(blk_rq_is_passthrough(scmd->request)))
3511 goto invalid_opcode;
3512
3513 if (unlikely(scmd->cmd_len < 16)) {
3514 fp = 15;
3515 goto invalid_fld;
3516 }
3517 scsi_16_lba_len(cdb, &block, &n_block);
3518
3519 if (!unmap ||
3520 (dev->horkage & ATA_HORKAGE_NOTRIM) ||
3521 !ata_id_has_trim(dev->id)) {
3522 fp = 1;
3523 bp = 3;
3524 goto invalid_fld;
3525 }
3526 /* If the request is too large the cmd is invalid */
3527 if (n_block > 0xffff * trmax) {
3528 fp = 2;
3529 goto invalid_fld;
3530 }
3531
3532 /*
3533 * WRITE SAME always has a sector sized buffer as payload, this
3534 * should never be a multiple entry S/G list.
3535 */
3536 if (!scsi_sg_count(scmd))
3537 goto invalid_param_len;
3538
3539 /*
3540 * size must match sector size in bytes
3541 * For DATA SET MANAGEMENT TRIM in ACS-2 nsect (aka count)
3542 * is defined as number of 512 byte blocks to be transferred.
3543 */
3544
3545 size = ata_format_dsm_trim_descr(scmd, trmax, block, n_block);
3546 if (size != len)
3547 goto invalid_param_len;
3548
3549 if (ata_ncq_enabled(dev) && ata_fpdma_dsm_supported(dev)) {
3550 /* Newer devices support queued TRIM commands */
3551 tf->protocol = ATA_PROT_NCQ;
3552 tf->command = ATA_CMD_FPDMA_SEND;
3553 tf->hob_nsect = ATA_SUBCMD_FPDMA_SEND_DSM & 0x1f;
3554 tf->nsect = qc->hw_tag << 3;
3555 tf->hob_feature = (size / 512) >> 8;
3556 tf->feature = size / 512;
3557
3558 tf->auxiliary = 1;
3559 } else {
3560 tf->protocol = ATA_PROT_DMA;
3561 tf->hob_feature = 0;
3562 tf->feature = ATA_DSM_TRIM;
3563 tf->hob_nsect = (size / 512) >> 8;
3564 tf->nsect = size / 512;
3565 tf->command = ATA_CMD_DSM;
3566 }
3567
3568 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
3569 ATA_TFLAG_WRITE;
3570
3571 ata_qc_set_pc_nbytes(qc);
3572
3573 return 0;
3574
3575invalid_fld:
3576 ata_scsi_set_invalid_field(dev, scmd, fp, bp);
3577 return 1;
3578invalid_param_len:
3579 /* "Parameter list length error" */
3580 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3581 return 1;
3582invalid_opcode:
3583 /* "Invalid command operation code" */
3584 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0);
3585 return 1;
3586}
3587
3588/**
3589 * ata_scsiop_maint_in - Simulate a subset of MAINTENANCE_IN
3590 * @args: device MAINTENANCE_IN data / SCSI command of interest.
3591 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
3592 *
3593 * Yields a subset to satisfy scsi_report_opcode()
3594 *
3595 * LOCKING:
3596 * spin_lock_irqsave(host lock)
3597 */
3598static unsigned int ata_scsiop_maint_in(struct ata_scsi_args *args, u8 *rbuf)
3599{
3600 struct ata_device *dev = args->dev;
3601 u8 *cdb = args->cmd->cmnd;
3602 u8 supported = 0;
3603 unsigned int err = 0;
3604
3605 if (cdb[2] != 1) {
3606 ata_dev_warn(dev, "invalid command format %d\n", cdb[2]);
3607 err = 2;
3608 goto out;
3609 }
3610 switch (cdb[3]) {
3611 case INQUIRY:
3612 case MODE_SENSE:
3613 case MODE_SENSE_10:
3614 case READ_CAPACITY:
3615 case SERVICE_ACTION_IN_16:
3616 case REPORT_LUNS:
3617 case REQUEST_SENSE:
3618 case SYNCHRONIZE_CACHE:
3619 case REZERO_UNIT:
3620 case SEEK_6:
3621 case SEEK_10:
3622 case TEST_UNIT_READY:
3623 case SEND_DIAGNOSTIC:
3624 case MAINTENANCE_IN:
3625 case READ_6:
3626 case READ_10:
3627 case READ_16:
3628 case WRITE_6:
3629 case WRITE_10:
3630 case WRITE_16:
3631 case ATA_12:
3632 case ATA_16:
3633 case VERIFY:
3634 case VERIFY_16:
3635 case MODE_SELECT:
3636 case MODE_SELECT_10:
3637 case START_STOP:
3638 supported = 3;
3639 break;
3640 case ZBC_IN:
3641 case ZBC_OUT:
3642 if (ata_id_zoned_cap(dev->id) ||
3643 dev->class == ATA_DEV_ZAC)
3644 supported = 3;
3645 break;
3646 case SECURITY_PROTOCOL_IN:
3647 case SECURITY_PROTOCOL_OUT:
3648 if (dev->flags & ATA_DFLAG_TRUSTED)
3649 supported = 3;
3650 break;
3651 default:
3652 break;
3653 }
3654out:
3655 rbuf[1] = supported; /* supported */
3656 return err;
3657}
3658
3659/**
3660 * ata_scsi_report_zones_complete - convert ATA output
3661 * @qc: command structure returning the data
3662 *
3663 * Convert T-13 little-endian field representation into
3664 * T-10 big-endian field representation.
3665 * What a mess.
3666 */
3667static void ata_scsi_report_zones_complete(struct ata_queued_cmd *qc)
3668{
3669 struct scsi_cmnd *scmd = qc->scsicmd;
3670 struct sg_mapping_iter miter;
3671 unsigned long flags;
3672 unsigned int bytes = 0;
3673
3674 sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd),
3675 SG_MITER_TO_SG | SG_MITER_ATOMIC);
3676
3677 local_irq_save(flags);
3678 while (sg_miter_next(&miter)) {
3679 unsigned int offset = 0;
3680
3681 if (bytes == 0) {
3682 char *hdr;
3683 u32 list_length;
3684 u64 max_lba, opt_lba;
3685 u16 same;
3686
3687 /* Swizzle header */
3688 hdr = miter.addr;
3689 list_length = get_unaligned_le32(&hdr[0]);
3690 same = get_unaligned_le16(&hdr[4]);
3691 max_lba = get_unaligned_le64(&hdr[8]);
3692 opt_lba = get_unaligned_le64(&hdr[16]);
3693 put_unaligned_be32(list_length, &hdr[0]);
3694 hdr[4] = same & 0xf;
3695 put_unaligned_be64(max_lba, &hdr[8]);
3696 put_unaligned_be64(opt_lba, &hdr[16]);
3697 offset += 64;
3698 bytes += 64;
3699 }
3700 while (offset < miter.length) {
3701 char *rec;
3702 u8 cond, type, non_seq, reset;
3703 u64 size, start, wp;
3704
3705 /* Swizzle zone descriptor */
3706 rec = miter.addr + offset;
3707 type = rec[0] & 0xf;
3708 cond = (rec[1] >> 4) & 0xf;
3709 non_seq = (rec[1] & 2);
3710 reset = (rec[1] & 1);
3711 size = get_unaligned_le64(&rec[8]);
3712 start = get_unaligned_le64(&rec[16]);
3713 wp = get_unaligned_le64(&rec[24]);
3714 rec[0] = type;
3715 rec[1] = (cond << 4) | non_seq | reset;
3716 put_unaligned_be64(size, &rec[8]);
3717 put_unaligned_be64(start, &rec[16]);
3718 put_unaligned_be64(wp, &rec[24]);
3719 WARN_ON(offset + 64 > miter.length);
3720 offset += 64;
3721 bytes += 64;
3722 }
3723 }
3724 sg_miter_stop(&miter);
3725 local_irq_restore(flags);
3726
3727 ata_scsi_qc_complete(qc);
3728}
3729
3730static unsigned int ata_scsi_zbc_in_xlat(struct ata_queued_cmd *qc)
3731{
3732 struct ata_taskfile *tf = &qc->tf;
3733 struct scsi_cmnd *scmd = qc->scsicmd;
3734 const u8 *cdb = scmd->cmnd;
3735 u16 sect, fp = (u16)-1;
3736 u8 sa, options, bp = 0xff;
3737 u64 block;
3738 u32 n_block;
3739
3740 if (unlikely(scmd->cmd_len < 16)) {
3741 ata_dev_warn(qc->dev, "invalid cdb length %d\n",
3742 scmd->cmd_len);
3743 fp = 15;
3744 goto invalid_fld;
3745 }
3746 scsi_16_lba_len(cdb, &block, &n_block);
3747 if (n_block != scsi_bufflen(scmd)) {
3748 ata_dev_warn(qc->dev, "non-matching transfer count (%d/%d)\n",
3749 n_block, scsi_bufflen(scmd));
3750 goto invalid_param_len;
3751 }
3752 sa = cdb[1] & 0x1f;
3753 if (sa != ZI_REPORT_ZONES) {
3754 ata_dev_warn(qc->dev, "invalid service action %d\n", sa);
3755 fp = 1;
3756 goto invalid_fld;
3757 }
3758 /*
3759 * ZAC allows only for transfers in 512 byte blocks,
3760 * and uses a 16 bit value for the transfer count.
3761 */
3762 if ((n_block / 512) > 0xffff || n_block < 512 || (n_block % 512)) {
3763 ata_dev_warn(qc->dev, "invalid transfer count %d\n", n_block);
3764 goto invalid_param_len;
3765 }
3766 sect = n_block / 512;
3767 options = cdb[14] & 0xbf;
3768
3769 if (ata_ncq_enabled(qc->dev) &&
3770 ata_fpdma_zac_mgmt_in_supported(qc->dev)) {
3771 tf->protocol = ATA_PROT_NCQ;
3772 tf->command = ATA_CMD_FPDMA_RECV;
3773 tf->hob_nsect = ATA_SUBCMD_FPDMA_RECV_ZAC_MGMT_IN & 0x1f;
3774 tf->nsect = qc->hw_tag << 3;
3775 tf->feature = sect & 0xff;
3776 tf->hob_feature = (sect >> 8) & 0xff;
3777 tf->auxiliary = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES | (options << 8);
3778 } else {
3779 tf->command = ATA_CMD_ZAC_MGMT_IN;
3780 tf->feature = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES;
3781 tf->protocol = ATA_PROT_DMA;
3782 tf->hob_feature = options;
3783 tf->hob_nsect = (sect >> 8) & 0xff;
3784 tf->nsect = sect & 0xff;
3785 }
3786 tf->device = ATA_LBA;
3787 tf->lbah = (block >> 16) & 0xff;
3788 tf->lbam = (block >> 8) & 0xff;
3789 tf->lbal = block & 0xff;
3790 tf->hob_lbah = (block >> 40) & 0xff;
3791 tf->hob_lbam = (block >> 32) & 0xff;
3792 tf->hob_lbal = (block >> 24) & 0xff;
3793
3794 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
3795 qc->flags |= ATA_QCFLAG_RESULT_TF;
3796
3797 ata_qc_set_pc_nbytes(qc);
3798
3799 qc->complete_fn = ata_scsi_report_zones_complete;
3800
3801 return 0;
3802
3803invalid_fld:
3804 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
3805 return 1;
3806
3807invalid_param_len:
3808 /* "Parameter list length error" */
3809 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3810 return 1;
3811}
3812
3813static unsigned int ata_scsi_zbc_out_xlat(struct ata_queued_cmd *qc)
3814{
3815 struct ata_taskfile *tf = &qc->tf;
3816 struct scsi_cmnd *scmd = qc->scsicmd;
3817 struct ata_device *dev = qc->dev;
3818 const u8 *cdb = scmd->cmnd;
3819 u8 all, sa;
3820 u64 block;
3821 u32 n_block;
3822 u16 fp = (u16)-1;
3823
3824 if (unlikely(scmd->cmd_len < 16)) {
3825 fp = 15;
3826 goto invalid_fld;
3827 }
3828
3829 sa = cdb[1] & 0x1f;
3830 if ((sa != ZO_CLOSE_ZONE) && (sa != ZO_FINISH_ZONE) &&
3831 (sa != ZO_OPEN_ZONE) && (sa != ZO_RESET_WRITE_POINTER)) {
3832 fp = 1;
3833 goto invalid_fld;
3834 }
3835
3836 scsi_16_lba_len(cdb, &block, &n_block);
3837 if (n_block) {
3838 /*
3839 * ZAC MANAGEMENT OUT doesn't define any length
3840 */
3841 goto invalid_param_len;
3842 }
3843
3844 all = cdb[14] & 0x1;
3845 if (all) {
3846 /*
3847 * Ignore the block address (zone ID) as defined by ZBC.
3848 */
3849 block = 0;
3850 } else if (block >= dev->n_sectors) {
3851 /*
3852 * Block must be a valid zone ID (a zone start LBA).
3853 */
3854 fp = 2;
3855 goto invalid_fld;
3856 }
3857
3858 if (ata_ncq_enabled(qc->dev) &&
3859 ata_fpdma_zac_mgmt_out_supported(qc->dev)) {
3860 tf->protocol = ATA_PROT_NCQ_NODATA;
3861 tf->command = ATA_CMD_NCQ_NON_DATA;
3862 tf->feature = ATA_SUBCMD_NCQ_NON_DATA_ZAC_MGMT_OUT;
3863 tf->nsect = qc->hw_tag << 3;
3864 tf->auxiliary = sa | ((u16)all << 8);
3865 } else {
3866 tf->protocol = ATA_PROT_NODATA;
3867 tf->command = ATA_CMD_ZAC_MGMT_OUT;
3868 tf->feature = sa;
3869 tf->hob_feature = all;
3870 }
3871 tf->lbah = (block >> 16) & 0xff;
3872 tf->lbam = (block >> 8) & 0xff;
3873 tf->lbal = block & 0xff;
3874 tf->hob_lbah = (block >> 40) & 0xff;
3875 tf->hob_lbam = (block >> 32) & 0xff;
3876 tf->hob_lbal = (block >> 24) & 0xff;
3877 tf->device = ATA_LBA;
3878 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
3879
3880 return 0;
3881
3882 invalid_fld:
3883 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
3884 return 1;
3885invalid_param_len:
3886 /* "Parameter list length error" */
3887 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3888 return 1;
3889}
3890
3891/**
3892 * ata_mselect_caching - Simulate MODE SELECT for caching info page
3893 * @qc: Storage for translated ATA taskfile
3894 * @buf: input buffer
3895 * @len: number of valid bytes in the input buffer
3896 * @fp: out parameter for the failed field on error
3897 *
3898 * Prepare a taskfile to modify caching information for the device.
3899 *
3900 * LOCKING:
3901 * None.
3902 */
3903static int ata_mselect_caching(struct ata_queued_cmd *qc,
3904 const u8 *buf, int len, u16 *fp)
3905{
3906 struct ata_taskfile *tf = &qc->tf;
3907 struct ata_device *dev = qc->dev;
3908 u8 mpage[CACHE_MPAGE_LEN];
3909 u8 wce;
3910 int i;
3911
3912 /*
3913 * The first two bytes of def_cache_mpage are a header, so offsets
3914 * in mpage are off by 2 compared to buf. Same for len.
3915 */
3916
3917 if (len != CACHE_MPAGE_LEN - 2) {
3918 if (len < CACHE_MPAGE_LEN - 2)
3919 *fp = len;
3920 else
3921 *fp = CACHE_MPAGE_LEN - 2;
3922 return -EINVAL;
3923 }
3924
3925 wce = buf[0] & (1 << 2);
3926
3927 /*
3928 * Check that read-only bits are not modified.
3929 */
3930 ata_msense_caching(dev->id, mpage, false);
3931 for (i = 0; i < CACHE_MPAGE_LEN - 2; i++) {
3932 if (i == 0)
3933 continue;
3934 if (mpage[i + 2] != buf[i]) {
3935 *fp = i;
3936 return -EINVAL;
3937 }
3938 }
3939
3940 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
3941 tf->protocol = ATA_PROT_NODATA;
3942 tf->nsect = 0;
3943 tf->command = ATA_CMD_SET_FEATURES;
3944 tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF;
3945 return 0;
3946}
3947
3948/**
3949 * ata_mselect_control - Simulate MODE SELECT for control page
3950 * @qc: Storage for translated ATA taskfile
3951 * @buf: input buffer
3952 * @len: number of valid bytes in the input buffer
3953 * @fp: out parameter for the failed field on error
3954 *
3955 * Prepare a taskfile to modify caching information for the device.
3956 *
3957 * LOCKING:
3958 * None.
3959 */
3960static int ata_mselect_control(struct ata_queued_cmd *qc,
3961 const u8 *buf, int len, u16 *fp)
3962{
3963 struct ata_device *dev = qc->dev;
3964 u8 mpage[CONTROL_MPAGE_LEN];
3965 u8 d_sense;
3966 int i;
3967
3968 /*
3969 * The first two bytes of def_control_mpage are a header, so offsets
3970 * in mpage are off by 2 compared to buf. Same for len.
3971 */
3972
3973 if (len != CONTROL_MPAGE_LEN - 2) {
3974 if (len < CONTROL_MPAGE_LEN - 2)
3975 *fp = len;
3976 else
3977 *fp = CONTROL_MPAGE_LEN - 2;
3978 return -EINVAL;
3979 }
3980
3981 d_sense = buf[0] & (1 << 2);
3982
3983 /*
3984 * Check that read-only bits are not modified.
3985 */
3986 ata_msense_control(dev, mpage, false);
3987 for (i = 0; i < CONTROL_MPAGE_LEN - 2; i++) {
3988 if (i == 0)
3989 continue;
3990 if (mpage[2 + i] != buf[i]) {
3991 *fp = i;
3992 return -EINVAL;
3993 }
3994 }
3995 if (d_sense & (1 << 2))
3996 dev->flags |= ATA_DFLAG_D_SENSE;
3997 else
3998 dev->flags &= ~ATA_DFLAG_D_SENSE;
3999 return 0;
4000}
4001
4002/**
4003 * ata_scsi_mode_select_xlat - Simulate MODE SELECT 6, 10 commands
4004 * @qc: Storage for translated ATA taskfile
4005 *
4006 * Converts a MODE SELECT command to an ATA SET FEATURES taskfile.
4007 * Assume this is invoked for direct access devices (e.g. disks) only.
4008 * There should be no block descriptor for other device types.
4009 *
4010 * LOCKING:
4011 * spin_lock_irqsave(host lock)
4012 */
4013static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc)
4014{
4015 struct scsi_cmnd *scmd = qc->scsicmd;
4016 const u8 *cdb = scmd->cmnd;
4017 u8 pg, spg;
4018 unsigned six_byte, pg_len, hdr_len, bd_len;
4019 int len;
4020 u16 fp = (u16)-1;
4021 u8 bp = 0xff;
4022 u8 buffer[64];
4023 const u8 *p = buffer;
4024
4025 VPRINTK("ENTER\n");
4026
4027 six_byte = (cdb[0] == MODE_SELECT);
4028 if (six_byte) {
4029 if (scmd->cmd_len < 5) {
4030 fp = 4;
4031 goto invalid_fld;
4032 }
4033
4034 len = cdb[4];
4035 hdr_len = 4;
4036 } else {
4037 if (scmd->cmd_len < 9) {
4038 fp = 8;
4039 goto invalid_fld;
4040 }
4041
4042 len = (cdb[7] << 8) + cdb[8];
4043 hdr_len = 8;
4044 }
4045
4046 /* We only support PF=1, SP=0. */
4047 if ((cdb[1] & 0x11) != 0x10) {
4048 fp = 1;
4049 bp = (cdb[1] & 0x01) ? 1 : 5;
4050 goto invalid_fld;
4051 }
4052
4053 /* Test early for possible overrun. */
4054 if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len)
4055 goto invalid_param_len;
4056
4057 /* Move past header and block descriptors. */
4058 if (len < hdr_len)
4059 goto invalid_param_len;
4060
4061 if (!sg_copy_to_buffer(scsi_sglist(scmd), scsi_sg_count(scmd),
4062 buffer, sizeof(buffer)))
4063 goto invalid_param_len;
4064
4065 if (six_byte)
4066 bd_len = p[3];
4067 else
4068 bd_len = (p[6] << 8) + p[7];
4069
4070 len -= hdr_len;
4071 p += hdr_len;
4072 if (len < bd_len)
4073 goto invalid_param_len;
4074 if (bd_len != 0 && bd_len != 8) {
4075 fp = (six_byte) ? 3 : 6;
4076 fp += bd_len + hdr_len;
4077 goto invalid_param;
4078 }
4079
4080 len -= bd_len;
4081 p += bd_len;
4082 if (len == 0)
4083 goto skip;
4084
4085 /* Parse both possible formats for the mode page headers. */
4086 pg = p[0] & 0x3f;
4087 if (p[0] & 0x40) {
4088 if (len < 4)
4089 goto invalid_param_len;
4090
4091 spg = p[1];
4092 pg_len = (p[2] << 8) | p[3];
4093 p += 4;
4094 len -= 4;
4095 } else {
4096 if (len < 2)
4097 goto invalid_param_len;
4098
4099 spg = 0;
4100 pg_len = p[1];
4101 p += 2;
4102 len -= 2;
4103 }
4104
4105 /*
4106 * No mode subpages supported (yet) but asking for _all_
4107 * subpages may be valid
4108 */
4109 if (spg && (spg != ALL_SUB_MPAGES)) {
4110 fp = (p[0] & 0x40) ? 1 : 0;
4111 fp += hdr_len + bd_len;
4112 goto invalid_param;
4113 }
4114 if (pg_len > len)
4115 goto invalid_param_len;
4116
4117 switch (pg) {
4118 case CACHE_MPAGE:
4119 if (ata_mselect_caching(qc, p, pg_len, &fp) < 0) {
4120 fp += hdr_len + bd_len;
4121 goto invalid_param;
4122 }
4123 break;
4124 case CONTROL_MPAGE:
4125 if (ata_mselect_control(qc, p, pg_len, &fp) < 0) {
4126 fp += hdr_len + bd_len;
4127 goto invalid_param;
4128 } else {
4129 goto skip; /* No ATA command to send */
4130 }
4131 break;
4132 default: /* invalid page code */
4133 fp = bd_len + hdr_len;
4134 goto invalid_param;
4135 }
4136
4137 /*
4138 * Only one page has changeable data, so we only support setting one
4139 * page at a time.
4140 */
4141 if (len > pg_len)
4142 goto invalid_param;
4143
4144 return 0;
4145
4146 invalid_fld:
4147 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
4148 return 1;
4149
4150 invalid_param:
4151 ata_scsi_set_invalid_parameter(qc->dev, scmd, fp);
4152 return 1;
4153
4154 invalid_param_len:
4155 /* "Parameter list length error" */
4156 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
4157 return 1;
4158
4159 skip:
4160 scmd->result = SAM_STAT_GOOD;
4161 return 1;
4162}
4163
4164static u8 ata_scsi_trusted_op(u32 len, bool send, bool dma)
4165{
4166 if (len == 0)
4167 return ATA_CMD_TRUSTED_NONDATA;
4168 else if (send)
4169 return dma ? ATA_CMD_TRUSTED_SND_DMA : ATA_CMD_TRUSTED_SND;
4170 else
4171 return dma ? ATA_CMD_TRUSTED_RCV_DMA : ATA_CMD_TRUSTED_RCV;
4172}
4173
4174static unsigned int ata_scsi_security_inout_xlat(struct ata_queued_cmd *qc)
4175{
4176 struct scsi_cmnd *scmd = qc->scsicmd;
4177 const u8 *cdb = scmd->cmnd;
4178 struct ata_taskfile *tf = &qc->tf;
4179 u8 secp = cdb[1];
4180 bool send = (cdb[0] == SECURITY_PROTOCOL_OUT);
4181 u16 spsp = get_unaligned_be16(&cdb[2]);
4182 u32 len = get_unaligned_be32(&cdb[6]);
4183 bool dma = !(qc->dev->flags & ATA_DFLAG_PIO);
4184
4185 /*
4186 * We don't support the ATA "security" protocol.
4187 */
4188 if (secp == 0xef) {
4189 ata_scsi_set_invalid_field(qc->dev, scmd, 1, 0);
4190 return 1;
4191 }
4192
4193 if (cdb[4] & 7) { /* INC_512 */
4194 if (len > 0xffff) {
4195 ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0);
4196 return 1;
4197 }
4198 } else {
4199 if (len > 0x01fffe00) {
4200 ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0);
4201 return 1;
4202 }
4203
4204 /* convert to the sector-based ATA addressing */
4205 len = (len + 511) / 512;
4206 }
4207
4208 tf->protocol = dma ? ATA_PROT_DMA : ATA_PROT_PIO;
4209 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR | ATA_TFLAG_LBA;
4210 if (send)
4211 tf->flags |= ATA_TFLAG_WRITE;
4212 tf->command = ata_scsi_trusted_op(len, send, dma);
4213 tf->feature = secp;
4214 tf->lbam = spsp & 0xff;
4215 tf->lbah = spsp >> 8;
4216
4217 if (len) {
4218 tf->nsect = len & 0xff;
4219 tf->lbal = len >> 8;
4220 } else {
4221 if (!send)
4222 tf->lbah = (1 << 7);
4223 }
4224
4225 ata_qc_set_pc_nbytes(qc);
4226 return 0;
4227}
4228
4229/**
4230 * ata_scsi_var_len_cdb_xlat - SATL variable length CDB to Handler
4231 * @qc: Command to be translated
4232 *
4233 * Translate a SCSI variable length CDB to specified commands.
4234 * It checks a service action value in CDB to call corresponding handler.
4235 *
4236 * RETURNS:
4237 * Zero on success, non-zero on failure
4238 *
4239 */
4240static unsigned int ata_scsi_var_len_cdb_xlat(struct ata_queued_cmd *qc)
4241{
4242 struct scsi_cmnd *scmd = qc->scsicmd;
4243 const u8 *cdb = scmd->cmnd;
4244 const u16 sa = get_unaligned_be16(&cdb[8]);
4245
4246 /*
4247 * if service action represents a ata pass-thru(32) command,
4248 * then pass it to ata_scsi_pass_thru handler.
4249 */
4250 if (sa == ATA_32)
4251 return ata_scsi_pass_thru(qc);
4252
4253 /* unsupported service action */
4254 return 1;
4255}
4256
4257/**
4258 * ata_get_xlat_func - check if SCSI to ATA translation is possible
4259 * @dev: ATA device
4260 * @cmd: SCSI command opcode to consider
4261 *
4262 * Look up the SCSI command given, and determine whether the
4263 * SCSI command is to be translated or simulated.
4264 *
4265 * RETURNS:
4266 * Pointer to translation function if possible, %NULL if not.
4267 */
4268
4269static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
4270{
4271 switch (cmd) {
4272 case READ_6:
4273 case READ_10:
4274 case READ_16:
4275
4276 case WRITE_6:
4277 case WRITE_10:
4278 case WRITE_16:
4279 return ata_scsi_rw_xlat;
4280
4281 case WRITE_SAME_16:
4282 return ata_scsi_write_same_xlat;
4283
4284 case SYNCHRONIZE_CACHE:
4285 if (ata_try_flush_cache(dev))
4286 return ata_scsi_flush_xlat;
4287 break;
4288
4289 case VERIFY:
4290 case VERIFY_16:
4291 return ata_scsi_verify_xlat;
4292
4293 case ATA_12:
4294 case ATA_16:
4295 return ata_scsi_pass_thru;
4296
4297 case VARIABLE_LENGTH_CMD:
4298 return ata_scsi_var_len_cdb_xlat;
4299
4300 case MODE_SELECT:
4301 case MODE_SELECT_10:
4302 return ata_scsi_mode_select_xlat;
4303 break;
4304
4305 case ZBC_IN:
4306 return ata_scsi_zbc_in_xlat;
4307
4308 case ZBC_OUT:
4309 return ata_scsi_zbc_out_xlat;
4310
4311 case SECURITY_PROTOCOL_IN:
4312 case SECURITY_PROTOCOL_OUT:
4313 if (!(dev->flags & ATA_DFLAG_TRUSTED))
4314 break;
4315 return ata_scsi_security_inout_xlat;
4316
4317 case START_STOP:
4318 return ata_scsi_start_stop_xlat;
4319 }
4320
4321 return NULL;
4322}
4323
4324/**
4325 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
4326 * @ap: ATA port to which the command was being sent
4327 * @cmd: SCSI command to dump
4328 *
4329 * Prints the contents of a SCSI command via printk().
4330 */
4331
4332static inline void ata_scsi_dump_cdb(struct ata_port *ap,
4333 struct scsi_cmnd *cmd)
4334{
4335#ifdef ATA_VERBOSE_DEBUG
4336 struct scsi_device *scsidev = cmd->device;
4337
4338 VPRINTK("CDB (%u:%d,%d,%lld) %9ph\n",
4339 ap->print_id,
4340 scsidev->channel, scsidev->id, scsidev->lun,
4341 cmd->cmnd);
4342#endif
4343}
4344
4345static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
4346 struct ata_device *dev)
4347{
4348 u8 scsi_op = scmd->cmnd[0];
4349 ata_xlat_func_t xlat_func;
4350 int rc = 0;
4351
4352 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
4353 if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
4354 goto bad_cdb_len;
4355
4356 xlat_func = ata_get_xlat_func(dev, scsi_op);
4357 } else {
4358 if (unlikely(!scmd->cmd_len))
4359 goto bad_cdb_len;
4360
4361 xlat_func = NULL;
4362 if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
4363 /* relay SCSI command to ATAPI device */
4364 int len = COMMAND_SIZE(scsi_op);
4365 if (unlikely(len > scmd->cmd_len ||
4366 len > dev->cdb_len ||
4367 scmd->cmd_len > ATAPI_CDB_LEN))
4368 goto bad_cdb_len;
4369
4370 xlat_func = atapi_xlat;
4371 } else {
4372 /* ATA_16 passthru, treat as an ATA command */
4373 if (unlikely(scmd->cmd_len > 16))
4374 goto bad_cdb_len;
4375
4376 xlat_func = ata_get_xlat_func(dev, scsi_op);
4377 }
4378 }
4379
4380 if (xlat_func)
4381 rc = ata_scsi_translate(dev, scmd, xlat_func);
4382 else
4383 ata_scsi_simulate(dev, scmd);
4384
4385 return rc;
4386
4387 bad_cdb_len:
4388 DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
4389 scmd->cmd_len, scsi_op, dev->cdb_len);
4390 scmd->result = DID_ERROR << 16;
4391 scmd->scsi_done(scmd);
4392 return 0;
4393}
4394
4395/**
4396 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
4397 * @shost: SCSI host of command to be sent
4398 * @cmd: SCSI command to be sent
4399 *
4400 * In some cases, this function translates SCSI commands into
4401 * ATA taskfiles, and queues the taskfiles to be sent to
4402 * hardware. In other cases, this function simulates a
4403 * SCSI device by evaluating and responding to certain
4404 * SCSI commands. This creates the overall effect of
4405 * ATA and ATAPI devices appearing as SCSI devices.
4406 *
4407 * LOCKING:
4408 * ATA host lock
4409 *
4410 * RETURNS:
4411 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
4412 * 0 otherwise.
4413 */
4414int ata_scsi_queuecmd(struct Scsi_Host *shost, struct scsi_cmnd *cmd)
4415{
4416 struct ata_port *ap;
4417 struct ata_device *dev;
4418 struct scsi_device *scsidev = cmd->device;
4419 int rc = 0;
4420 unsigned long irq_flags;
4421
4422 ap = ata_shost_to_port(shost);
4423
4424 spin_lock_irqsave(ap->lock, irq_flags);
4425
4426 ata_scsi_dump_cdb(ap, cmd);
4427
4428 dev = ata_scsi_find_dev(ap, scsidev);
4429 if (likely(dev))
4430 rc = __ata_scsi_queuecmd(cmd, dev);
4431 else {
4432 cmd->result = (DID_BAD_TARGET << 16);
4433 cmd->scsi_done(cmd);
4434 }
4435
4436 spin_unlock_irqrestore(ap->lock, irq_flags);
4437
4438 return rc;
4439}
4440
4441/**
4442 * ata_scsi_simulate - simulate SCSI command on ATA device
4443 * @dev: the target device
4444 * @cmd: SCSI command being sent to device.
4445 *
4446 * Interprets and directly executes a select list of SCSI commands
4447 * that can be handled internally.
4448 *
4449 * LOCKING:
4450 * spin_lock_irqsave(host lock)
4451 */
4452
4453void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd)
4454{
4455 struct ata_scsi_args args;
4456 const u8 *scsicmd = cmd->cmnd;
4457 u8 tmp8;
4458
4459 args.dev = dev;
4460 args.id = dev->id;
4461 args.cmd = cmd;
4462
4463 switch(scsicmd[0]) {
4464 case INQUIRY:
4465 if (scsicmd[1] & 2) /* is CmdDt set? */
4466 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
4467 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
4468 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
4469 else switch (scsicmd[2]) {
4470 case 0x00:
4471 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
4472 break;
4473 case 0x80:
4474 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
4475 break;
4476 case 0x83:
4477 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
4478 break;
4479 case 0x89:
4480 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
4481 break;
4482 case 0xb0:
4483 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
4484 break;
4485 case 0xb1:
4486 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
4487 break;
4488 case 0xb2:
4489 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b2);
4490 break;
4491 case 0xb6:
4492 if (dev->flags & ATA_DFLAG_ZAC) {
4493 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b6);
4494 break;
4495 }
4496 /* Fallthrough */
4497 default:
4498 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff);
4499 break;
4500 }
4501 break;
4502
4503 case MODE_SENSE:
4504 case MODE_SENSE_10:
4505 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
4506 break;
4507
4508 case READ_CAPACITY:
4509 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
4510 break;
4511
4512 case SERVICE_ACTION_IN_16:
4513 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
4514 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
4515 else
4516 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
4517 break;
4518
4519 case REPORT_LUNS:
4520 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
4521 break;
4522
4523 case REQUEST_SENSE:
4524 ata_scsi_set_sense(dev, cmd, 0, 0, 0);
4525 cmd->result = (DRIVER_SENSE << 24);
4526 break;
4527
4528 /* if we reach this, then writeback caching is disabled,
4529 * turning this into a no-op.
4530 */
4531 case SYNCHRONIZE_CACHE:
4532 /* fall through */
4533
4534 /* no-op's, complete with success */
4535 case REZERO_UNIT:
4536 case SEEK_6:
4537 case SEEK_10:
4538 case TEST_UNIT_READY:
4539 break;
4540
4541 case SEND_DIAGNOSTIC:
4542 tmp8 = scsicmd[1] & ~(1 << 3);
4543 if (tmp8 != 0x4 || scsicmd[3] || scsicmd[4])
4544 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
4545 break;
4546
4547 case MAINTENANCE_IN:
4548 if ((scsicmd[1] & 0x1f) == MI_REPORT_SUPPORTED_OPERATION_CODES)
4549 ata_scsi_rbuf_fill(&args, ata_scsiop_maint_in);
4550 else
4551 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
4552 break;
4553
4554 /* all other commands */
4555 default:
4556 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x20, 0x0);
4557 /* "Invalid command operation code" */
4558 break;
4559 }
4560
4561 cmd->scsi_done(cmd);
4562}
4563
4564int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
4565{
4566 int i, rc;
4567
4568 for (i = 0; i < host->n_ports; i++) {
4569 struct ata_port *ap = host->ports[i];
4570 struct Scsi_Host *shost;
4571
4572 rc = -ENOMEM;
4573 shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
4574 if (!shost)
4575 goto err_alloc;
4576
4577 shost->eh_noresume = 1;
4578 *(struct ata_port **)&shost->hostdata[0] = ap;
4579 ap->scsi_host = shost;
4580
4581 shost->transportt = ata_scsi_transport_template;
4582 shost->unique_id = ap->print_id;
4583 shost->max_id = 16;
4584 shost->max_lun = 1;
4585 shost->max_channel = 1;
4586 shost->max_cmd_len = 32;
4587
4588 /* Schedule policy is determined by ->qc_defer()
4589 * callback and it needs to see every deferred qc.
4590 * Set host_blocked to 1 to prevent SCSI midlayer from
4591 * automatically deferring requests.
4592 */
4593 shost->max_host_blocked = 1;
4594
4595 rc = scsi_add_host_with_dma(shost, &ap->tdev, ap->host->dev);
4596 if (rc)
4597 goto err_alloc;
4598 }
4599
4600 return 0;
4601
4602 err_alloc:
4603 while (--i >= 0) {
4604 struct Scsi_Host *shost = host->ports[i]->scsi_host;
4605
4606 /* scsi_host_put() is in ata_devres_release() */
4607 scsi_remove_host(shost);
4608 }
4609 return rc;
4610}
4611
4612#ifdef CONFIG_OF
4613static void ata_scsi_assign_ofnode(struct ata_device *dev, struct ata_port *ap)
4614{
4615 struct scsi_device *sdev = dev->sdev;
4616 struct device *d = ap->host->dev;
4617 struct device_node *np = d->of_node;
4618 struct device_node *child;
4619
4620 for_each_available_child_of_node(np, child) {
4621 int ret;
4622 u32 val;
4623
4624 ret = of_property_read_u32(child, "reg", &val);
4625 if (ret)
4626 continue;
4627 if (val == dev->devno) {
4628 dev_dbg(d, "found matching device node\n");
4629 sdev->sdev_gendev.of_node = child;
4630 return;
4631 }
4632 }
4633}
4634#else
4635static void ata_scsi_assign_ofnode(struct ata_device *dev, struct ata_port *ap)
4636{
4637}
4638#endif
4639
4640void ata_scsi_scan_host(struct ata_port *ap, int sync)
4641{
4642 int tries = 5;
4643 struct ata_device *last_failed_dev = NULL;
4644 struct ata_link *link;
4645 struct ata_device *dev;
4646
4647 repeat:
4648 ata_for_each_link(link, ap, EDGE) {
4649 ata_for_each_dev(dev, link, ENABLED) {
4650 struct scsi_device *sdev;
4651 int channel = 0, id = 0;
4652
4653 if (dev->sdev)
4654 continue;
4655
4656 if (ata_is_host_link(link))
4657 id = dev->devno;
4658 else
4659 channel = link->pmp;
4660
4661 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
4662 NULL);
4663 if (!IS_ERR(sdev)) {
4664 dev->sdev = sdev;
4665 ata_scsi_assign_ofnode(dev, ap);
4666 scsi_device_put(sdev);
4667 } else {
4668 dev->sdev = NULL;
4669 }
4670 }
4671 }
4672
4673 /* If we scanned while EH was in progress or allocation
4674 * failure occurred, scan would have failed silently. Check
4675 * whether all devices are attached.
4676 */
4677 ata_for_each_link(link, ap, EDGE) {
4678 ata_for_each_dev(dev, link, ENABLED) {
4679 if (!dev->sdev)
4680 goto exit_loop;
4681 }
4682 }
4683 exit_loop:
4684 if (!link)
4685 return;
4686
4687 /* we're missing some SCSI devices */
4688 if (sync) {
4689 /* If caller requested synchrnous scan && we've made
4690 * any progress, sleep briefly and repeat.
4691 */
4692 if (dev != last_failed_dev) {
4693 msleep(100);
4694 last_failed_dev = dev;
4695 goto repeat;
4696 }
4697
4698 /* We might be failing to detect boot device, give it
4699 * a few more chances.
4700 */
4701 if (--tries) {
4702 msleep(100);
4703 goto repeat;
4704 }
4705
4706 ata_port_err(ap,
4707 "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n");
4708 }
4709
4710 queue_delayed_work(system_long_wq, &ap->hotplug_task,
4711 round_jiffies_relative(HZ));
4712}
4713
4714/**
4715 * ata_scsi_offline_dev - offline attached SCSI device
4716 * @dev: ATA device to offline attached SCSI device for
4717 *
4718 * This function is called from ata_eh_hotplug() and responsible
4719 * for taking the SCSI device attached to @dev offline. This
4720 * function is called with host lock which protects dev->sdev
4721 * against clearing.
4722 *
4723 * LOCKING:
4724 * spin_lock_irqsave(host lock)
4725 *
4726 * RETURNS:
4727 * 1 if attached SCSI device exists, 0 otherwise.
4728 */
4729int ata_scsi_offline_dev(struct ata_device *dev)
4730{
4731 if (dev->sdev) {
4732 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
4733 return 1;
4734 }
4735 return 0;
4736}
4737
4738/**
4739 * ata_scsi_remove_dev - remove attached SCSI device
4740 * @dev: ATA device to remove attached SCSI device for
4741 *
4742 * This function is called from ata_eh_scsi_hotplug() and
4743 * responsible for removing the SCSI device attached to @dev.
4744 *
4745 * LOCKING:
4746 * Kernel thread context (may sleep).
4747 */
4748static void ata_scsi_remove_dev(struct ata_device *dev)
4749{
4750 struct ata_port *ap = dev->link->ap;
4751 struct scsi_device *sdev;
4752 unsigned long flags;
4753
4754 /* Alas, we need to grab scan_mutex to ensure SCSI device
4755 * state doesn't change underneath us and thus
4756 * scsi_device_get() always succeeds. The mutex locking can
4757 * be removed if there is __scsi_device_get() interface which
4758 * increments reference counts regardless of device state.
4759 */
4760 mutex_lock(&ap->scsi_host->scan_mutex);
4761 spin_lock_irqsave(ap->lock, flags);
4762
4763 /* clearing dev->sdev is protected by host lock */
4764 sdev = dev->sdev;
4765 dev->sdev = NULL;
4766
4767 if (sdev) {
4768 /* If user initiated unplug races with us, sdev can go
4769 * away underneath us after the host lock and
4770 * scan_mutex are released. Hold onto it.
4771 */
4772 if (scsi_device_get(sdev) == 0) {
4773 /* The following ensures the attached sdev is
4774 * offline on return from ata_scsi_offline_dev()
4775 * regardless it wins or loses the race
4776 * against this function.
4777 */
4778 scsi_device_set_state(sdev, SDEV_OFFLINE);
4779 } else {
4780 WARN_ON(1);
4781 sdev = NULL;
4782 }
4783 }
4784
4785 spin_unlock_irqrestore(ap->lock, flags);
4786 mutex_unlock(&ap->scsi_host->scan_mutex);
4787
4788 if (sdev) {
4789 ata_dev_info(dev, "detaching (SCSI %s)\n",
4790 dev_name(&sdev->sdev_gendev));
4791
4792 scsi_remove_device(sdev);
4793 scsi_device_put(sdev);
4794 }
4795}
4796
4797static void ata_scsi_handle_link_detach(struct ata_link *link)
4798{
4799 struct ata_port *ap = link->ap;
4800 struct ata_device *dev;
4801
4802 ata_for_each_dev(dev, link, ALL) {
4803 unsigned long flags;
4804
4805 if (!(dev->flags & ATA_DFLAG_DETACHED))
4806 continue;
4807
4808 spin_lock_irqsave(ap->lock, flags);
4809 dev->flags &= ~ATA_DFLAG_DETACHED;
4810 spin_unlock_irqrestore(ap->lock, flags);
4811
4812 if (zpodd_dev_enabled(dev))
4813 zpodd_exit(dev);
4814
4815 ata_scsi_remove_dev(dev);
4816 }
4817}
4818
4819/**
4820 * ata_scsi_media_change_notify - send media change event
4821 * @dev: Pointer to the disk device with media change event
4822 *
4823 * Tell the block layer to send a media change notification
4824 * event.
4825 *
4826 * LOCKING:
4827 * spin_lock_irqsave(host lock)
4828 */
4829void ata_scsi_media_change_notify(struct ata_device *dev)
4830{
4831 if (dev->sdev)
4832 sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
4833 GFP_ATOMIC);
4834}
4835
4836/**
4837 * ata_scsi_hotplug - SCSI part of hotplug
4838 * @work: Pointer to ATA port to perform SCSI hotplug on
4839 *
4840 * Perform SCSI part of hotplug. It's executed from a separate
4841 * workqueue after EH completes. This is necessary because SCSI
4842 * hot plugging requires working EH and hot unplugging is
4843 * synchronized with hot plugging with a mutex.
4844 *
4845 * LOCKING:
4846 * Kernel thread context (may sleep).
4847 */
4848void ata_scsi_hotplug(struct work_struct *work)
4849{
4850 struct ata_port *ap =
4851 container_of(work, struct ata_port, hotplug_task.work);
4852 int i;
4853
4854 if (ap->pflags & ATA_PFLAG_UNLOADING) {
4855 DPRINTK("ENTER/EXIT - unloading\n");
4856 return;
4857 }
4858
4859 DPRINTK("ENTER\n");
4860 mutex_lock(&ap->scsi_scan_mutex);
4861
4862 /* Unplug detached devices. We cannot use link iterator here
4863 * because PMP links have to be scanned even if PMP is
4864 * currently not attached. Iterate manually.
4865 */
4866 ata_scsi_handle_link_detach(&ap->link);
4867 if (ap->pmp_link)
4868 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
4869 ata_scsi_handle_link_detach(&ap->pmp_link[i]);
4870
4871 /* scan for new ones */
4872 ata_scsi_scan_host(ap, 0);
4873
4874 mutex_unlock(&ap->scsi_scan_mutex);
4875 DPRINTK("EXIT\n");
4876}
4877
4878/**
4879 * ata_scsi_user_scan - indication for user-initiated bus scan
4880 * @shost: SCSI host to scan
4881 * @channel: Channel to scan
4882 * @id: ID to scan
4883 * @lun: LUN to scan
4884 *
4885 * This function is called when user explicitly requests bus
4886 * scan. Set probe pending flag and invoke EH.
4887 *
4888 * LOCKING:
4889 * SCSI layer (we don't care)
4890 *
4891 * RETURNS:
4892 * Zero.
4893 */
4894int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
4895 unsigned int id, u64 lun)
4896{
4897 struct ata_port *ap = ata_shost_to_port(shost);
4898 unsigned long flags;
4899 int devno, rc = 0;
4900
4901 if (!ap->ops->error_handler)
4902 return -EOPNOTSUPP;
4903
4904 if (lun != SCAN_WILD_CARD && lun)
4905 return -EINVAL;
4906
4907 if (!sata_pmp_attached(ap)) {
4908 if (channel != SCAN_WILD_CARD && channel)
4909 return -EINVAL;
4910 devno = id;
4911 } else {
4912 if (id != SCAN_WILD_CARD && id)
4913 return -EINVAL;
4914 devno = channel;
4915 }
4916
4917 spin_lock_irqsave(ap->lock, flags);
4918
4919 if (devno == SCAN_WILD_CARD) {
4920 struct ata_link *link;
4921
4922 ata_for_each_link(link, ap, EDGE) {
4923 struct ata_eh_info *ehi = &link->eh_info;
4924 ehi->probe_mask |= ATA_ALL_DEVICES;
4925 ehi->action |= ATA_EH_RESET;
4926 }
4927 } else {
4928 struct ata_device *dev = ata_find_dev(ap, devno);
4929
4930 if (dev) {
4931 struct ata_eh_info *ehi = &dev->link->eh_info;
4932 ehi->probe_mask |= 1 << dev->devno;
4933 ehi->action |= ATA_EH_RESET;
4934 } else
4935 rc = -EINVAL;
4936 }
4937
4938 if (rc == 0) {
4939 ata_port_schedule_eh(ap);
4940 spin_unlock_irqrestore(ap->lock, flags);
4941 ata_port_wait_eh(ap);
4942 } else
4943 spin_unlock_irqrestore(ap->lock, flags);
4944
4945 return rc;
4946}
4947
4948/**
4949 * ata_scsi_dev_rescan - initiate scsi_rescan_device()
4950 * @work: Pointer to ATA port to perform scsi_rescan_device()
4951 *
4952 * After ATA pass thru (SAT) commands are executed successfully,
4953 * libata need to propagate the changes to SCSI layer.
4954 *
4955 * LOCKING:
4956 * Kernel thread context (may sleep).
4957 */
4958void ata_scsi_dev_rescan(struct work_struct *work)
4959{
4960 struct ata_port *ap =
4961 container_of(work, struct ata_port, scsi_rescan_task);
4962 struct ata_link *link;
4963 struct ata_device *dev;
4964 unsigned long flags;
4965
4966 mutex_lock(&ap->scsi_scan_mutex);
4967 spin_lock_irqsave(ap->lock, flags);
4968
4969 ata_for_each_link(link, ap, EDGE) {
4970 ata_for_each_dev(dev, link, ENABLED) {
4971 struct scsi_device *sdev = dev->sdev;
4972
4973 if (!sdev)
4974 continue;
4975 if (scsi_device_get(sdev))
4976 continue;
4977
4978 spin_unlock_irqrestore(ap->lock, flags);
4979 scsi_rescan_device(&(sdev->sdev_gendev));
4980 scsi_device_put(sdev);
4981 spin_lock_irqsave(ap->lock, flags);
4982 }
4983 }
4984
4985 spin_unlock_irqrestore(ap->lock, flags);
4986 mutex_unlock(&ap->scsi_scan_mutex);
4987}
4988
4989/**
4990 * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
4991 * @host: ATA host container for all SAS ports
4992 * @port_info: Information from low-level host driver
4993 * @shost: SCSI host that the scsi device is attached to
4994 *
4995 * LOCKING:
4996 * PCI/etc. bus probe sem.
4997 *
4998 * RETURNS:
4999 * ata_port pointer on success / NULL on failure.
5000 */
5001
5002struct ata_port *ata_sas_port_alloc(struct ata_host *host,
5003 struct ata_port_info *port_info,
5004 struct Scsi_Host *shost)
5005{
5006 struct ata_port *ap;
5007
5008 ap = ata_port_alloc(host);
5009 if (!ap)
5010 return NULL;
5011
5012 ap->port_no = 0;
5013 ap->lock = &host->lock;
5014 ap->pio_mask = port_info->pio_mask;
5015 ap->mwdma_mask = port_info->mwdma_mask;
5016 ap->udma_mask = port_info->udma_mask;
5017 ap->flags |= port_info->flags;
5018 ap->ops = port_info->port_ops;
5019 ap->cbl = ATA_CBL_SATA;
5020
5021 return ap;
5022}
5023EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
5024
5025/**
5026 * ata_sas_port_start - Set port up for dma.
5027 * @ap: Port to initialize
5028 *
5029 * Called just after data structures for each port are
5030 * initialized.
5031 *
5032 * May be used as the port_start() entry in ata_port_operations.
5033 *
5034 * LOCKING:
5035 * Inherited from caller.
5036 */
5037int ata_sas_port_start(struct ata_port *ap)
5038{
5039 /*
5040 * the port is marked as frozen at allocation time, but if we don't
5041 * have new eh, we won't thaw it
5042 */
5043 if (!ap->ops->error_handler)
5044 ap->pflags &= ~ATA_PFLAG_FROZEN;
5045 return 0;
5046}
5047EXPORT_SYMBOL_GPL(ata_sas_port_start);
5048
5049/**
5050 * ata_port_stop - Undo ata_sas_port_start()
5051 * @ap: Port to shut down
5052 *
5053 * May be used as the port_stop() entry in ata_port_operations.
5054 *
5055 * LOCKING:
5056 * Inherited from caller.
5057 */
5058
5059void ata_sas_port_stop(struct ata_port *ap)
5060{
5061}
5062EXPORT_SYMBOL_GPL(ata_sas_port_stop);
5063
5064/**
5065 * ata_sas_async_probe - simply schedule probing and return
5066 * @ap: Port to probe
5067 *
5068 * For batch scheduling of probe for sas attached ata devices, assumes
5069 * the port has already been through ata_sas_port_init()
5070 */
5071void ata_sas_async_probe(struct ata_port *ap)
5072{
5073 __ata_port_probe(ap);
5074}
5075EXPORT_SYMBOL_GPL(ata_sas_async_probe);
5076
5077int ata_sas_sync_probe(struct ata_port *ap)
5078{
5079 return ata_port_probe(ap);
5080}
5081EXPORT_SYMBOL_GPL(ata_sas_sync_probe);
5082
5083
5084/**
5085 * ata_sas_port_init - Initialize a SATA device
5086 * @ap: SATA port to initialize
5087 *
5088 * LOCKING:
5089 * PCI/etc. bus probe sem.
5090 *
5091 * RETURNS:
5092 * Zero on success, non-zero on error.
5093 */
5094
5095int ata_sas_port_init(struct ata_port *ap)
5096{
5097 int rc = ap->ops->port_start(ap);
5098
5099 if (rc)
5100 return rc;
5101 ap->print_id = atomic_inc_return(&ata_print_id);
5102 return 0;
5103}
5104EXPORT_SYMBOL_GPL(ata_sas_port_init);
5105
5106int ata_sas_tport_add(struct device *parent, struct ata_port *ap)
5107{
5108 return ata_tport_add(parent, ap);
5109}
5110EXPORT_SYMBOL_GPL(ata_sas_tport_add);
5111
5112void ata_sas_tport_delete(struct ata_port *ap)
5113{
5114 ata_tport_delete(ap);
5115}
5116EXPORT_SYMBOL_GPL(ata_sas_tport_delete);
5117
5118/**
5119 * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
5120 * @ap: SATA port to destroy
5121 *
5122 */
5123
5124void ata_sas_port_destroy(struct ata_port *ap)
5125{
5126 if (ap->ops->port_stop)
5127 ap->ops->port_stop(ap);
5128 kfree(ap);
5129}
5130EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
5131
5132/**
5133 * ata_sas_slave_configure - Default slave_config routine for libata devices
5134 * @sdev: SCSI device to configure
5135 * @ap: ATA port to which SCSI device is attached
5136 *
5137 * RETURNS:
5138 * Zero.
5139 */
5140
5141int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
5142{
5143 ata_scsi_sdev_config(sdev);
5144 ata_scsi_dev_config(sdev, ap->link.device);
5145 return 0;
5146}
5147EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
5148
5149/**
5150 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
5151 * @cmd: SCSI command to be sent
5152 * @ap: ATA port to which the command is being sent
5153 *
5154 * RETURNS:
5155 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
5156 * 0 otherwise.
5157 */
5158
5159int ata_sas_queuecmd(struct scsi_cmnd *cmd, struct ata_port *ap)
5160{
5161 int rc = 0;
5162
5163 ata_scsi_dump_cdb(ap, cmd);
5164
5165 if (likely(ata_dev_enabled(ap->link.device)))
5166 rc = __ata_scsi_queuecmd(cmd, ap->link.device);
5167 else {
5168 cmd->result = (DID_BAD_TARGET << 16);
5169 cmd->scsi_done(cmd);
5170 }
5171 return rc;
5172}
5173EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
5174
5175int ata_sas_allocate_tag(struct ata_port *ap)
5176{
5177 unsigned int max_queue = ap->host->n_tags;
5178 unsigned int i, tag;
5179
5180 for (i = 0, tag = ap->sas_last_tag + 1; i < max_queue; i++, tag++) {
5181 tag = tag < max_queue ? tag : 0;
5182
5183 /* the last tag is reserved for internal command. */
5184 if (ata_tag_internal(tag))
5185 continue;
5186
5187 if (!test_and_set_bit(tag, &ap->sas_tag_allocated)) {
5188 ap->sas_last_tag = tag;
5189 return tag;
5190 }
5191 }
5192 return -1;
5193}
5194
5195void ata_sas_free_tag(unsigned int tag, struct ata_port *ap)
5196{
5197 clear_bit(tag, &ap->sas_tag_allocated);
5198}