1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * libata-scsi.c - helper library for ATA
4 *
5 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
6 * Copyright 2003-2004 Jeff Garzik
7 *
8 * libata documentation is available via 'make {ps|pdf}docs',
9 * as Documentation/driver-api/libata.rst
10 *
11 * Hardware documentation available from
12 * - http://www.t10.org/
13 * - http://www.t13.org/
14 */
15
16 #include <linux/compat.h>
17 #include <linux/slab.h>
18 #include <linux/kernel.h>
19 #include <linux/blkdev.h>
20 #include <linux/spinlock.h>
21 #include <linux/export.h>
22 #include <scsi/scsi.h>
23 #include <scsi/scsi_host.h>
24 #include <scsi/scsi_cmnd.h>
25 #include <scsi/scsi_eh.h>
26 #include <scsi/scsi_device.h>
27 #include <scsi/scsi_tcq.h>
28 #include <scsi/scsi_transport.h>
29 #include <linux/libata.h>
30 #include <linux/hdreg.h>
31 #include <linux/uaccess.h>
32 #include <linux/suspend.h>
33 #include <linux/unaligned.h>
34 #include <linux/ioprio.h>
35 #include <linux/of.h>
36
37 #include "libata.h"
38 #include "libata-transport.h"
39
40 static DEFINE_SPINLOCK(ata_scsi_rbuf_lock);
41 static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE];
42
43 typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
44
45 static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
46 const struct scsi_device *scsidev);
47
48 #define RW_RECOVERY_MPAGE 0x1
49 #define RW_RECOVERY_MPAGE_LEN 12
50 #define CACHE_MPAGE 0x8
51 #define CACHE_MPAGE_LEN 20
52 #define CONTROL_MPAGE 0xa
53 #define CONTROL_MPAGE_LEN 12
54 #define ALL_MPAGES 0x3f
55 #define ALL_SUB_MPAGES 0xff
56 #define CDL_T2A_SUB_MPAGE 0x07
57 #define CDL_T2B_SUB_MPAGE 0x08
58 #define CDL_T2_SUB_MPAGE_LEN 232
59 #define ATA_FEATURE_SUB_MPAGE 0xf2
60 #define ATA_FEATURE_SUB_MPAGE_LEN 16
61
62 static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
63 RW_RECOVERY_MPAGE,
64 RW_RECOVERY_MPAGE_LEN - 2,
65 (1 << 7), /* AWRE */
66 0, /* read retry count */
67 0, 0, 0, 0,
68 0, /* write retry count */
69 0, 0, 0
70 };
71
72 static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
73 CACHE_MPAGE,
74 CACHE_MPAGE_LEN - 2,
75 0, /* contains WCE, needs to be 0 for logic */
76 0, 0, 0, 0, 0, 0, 0, 0, 0,
77 0, /* contains DRA, needs to be 0 for logic */
78 0, 0, 0, 0, 0, 0, 0
79 };
80
81 static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
82 CONTROL_MPAGE,
83 CONTROL_MPAGE_LEN - 2,
84 2, /* DSENSE=0, GLTSD=1 */
85 0, /* [QAM+QERR may be 1, see 05-359r1] */
86 0, 0, 0, 0, 0xff, 0xff,
87 0, 30 /* extended self test time, see 05-359r1 */
88 };
89
ata_scsi_park_show(struct device * device,struct device_attribute * attr,char * buf)90 static ssize_t ata_scsi_park_show(struct device *device,
91 struct device_attribute *attr, char *buf)
92 {
93 struct scsi_device *sdev = to_scsi_device(device);
94 struct ata_port *ap;
95 struct ata_link *link;
96 struct ata_device *dev;
97 unsigned long now;
98 unsigned int msecs;
99 int rc = 0;
100
101 ap = ata_shost_to_port(sdev->host);
102
103 spin_lock_irq(ap->lock);
104 dev = ata_scsi_find_dev(ap, sdev);
105 if (!dev) {
106 rc = -ENODEV;
107 goto unlock;
108 }
109 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
110 rc = -EOPNOTSUPP;
111 goto unlock;
112 }
113
114 link = dev->link;
115 now = jiffies;
116 if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS &&
117 link->eh_context.unloaded_mask & (1 << dev->devno) &&
118 time_after(dev->unpark_deadline, now))
119 msecs = jiffies_to_msecs(dev->unpark_deadline - now);
120 else
121 msecs = 0;
122
123 unlock:
124 spin_unlock_irq(ap->lock);
125
126 return rc ? rc : sysfs_emit(buf, "%u\n", msecs);
127 }
128
ata_scsi_park_store(struct device * device,struct device_attribute * attr,const char * buf,size_t len)129 static ssize_t ata_scsi_park_store(struct device *device,
130 struct device_attribute *attr,
131 const char *buf, size_t len)
132 {
133 struct scsi_device *sdev = to_scsi_device(device);
134 struct ata_port *ap;
135 struct ata_device *dev;
136 int input;
137 unsigned long flags;
138 int rc;
139
140 rc = kstrtoint(buf, 10, &input);
141 if (rc)
142 return rc;
143 if (input < -2)
144 return -EINVAL;
145 if (input > ATA_TMOUT_MAX_PARK) {
146 rc = -EOVERFLOW;
147 input = ATA_TMOUT_MAX_PARK;
148 }
149
150 ap = ata_shost_to_port(sdev->host);
151
152 spin_lock_irqsave(ap->lock, flags);
153 dev = ata_scsi_find_dev(ap, sdev);
154 if (unlikely(!dev)) {
155 rc = -ENODEV;
156 goto unlock;
157 }
158 if (dev->class != ATA_DEV_ATA &&
159 dev->class != ATA_DEV_ZAC) {
160 rc = -EOPNOTSUPP;
161 goto unlock;
162 }
163
164 if (input >= 0) {
165 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
166 rc = -EOPNOTSUPP;
167 goto unlock;
168 }
169
170 dev->unpark_deadline = ata_deadline(jiffies, input);
171 dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK;
172 ata_port_schedule_eh(ap);
173 complete(&ap->park_req_pending);
174 } else {
175 switch (input) {
176 case -1:
177 dev->flags &= ~ATA_DFLAG_NO_UNLOAD;
178 break;
179 case -2:
180 dev->flags |= ATA_DFLAG_NO_UNLOAD;
181 break;
182 }
183 }
184 unlock:
185 spin_unlock_irqrestore(ap->lock, flags);
186
187 return rc ? rc : len;
188 }
189 DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR,
190 ata_scsi_park_show, ata_scsi_park_store);
191 EXPORT_SYMBOL_GPL(dev_attr_unload_heads);
192
ata_scsi_sense_is_valid(u8 sk,u8 asc,u8 ascq)193 bool ata_scsi_sense_is_valid(u8 sk, u8 asc, u8 ascq)
194 {
195 /*
196 * If sk == NO_SENSE, and asc + ascq == NO ADDITIONAL SENSE INFORMATION,
197 * then there is no sense data to add.
198 */
199 if (sk == 0 && asc == 0 && ascq == 0)
200 return false;
201
202 /* If sk > COMPLETED, sense data is bogus. */
203 if (sk > COMPLETED)
204 return false;
205
206 return true;
207 }
208
ata_scsi_set_sense(struct ata_device * dev,struct scsi_cmnd * cmd,u8 sk,u8 asc,u8 ascq)209 void ata_scsi_set_sense(struct ata_device *dev, struct scsi_cmnd *cmd,
210 u8 sk, u8 asc, u8 ascq)
211 {
212 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
213
214 scsi_build_sense(cmd, d_sense, sk, asc, ascq);
215 }
216
ata_scsi_set_sense_information(struct ata_queued_cmd * qc)217 static void ata_scsi_set_sense_information(struct ata_queued_cmd *qc)
218 {
219 u64 information;
220
221 if (!(qc->flags & ATA_QCFLAG_RTF_FILLED)) {
222 ata_dev_dbg(qc->dev,
223 "missing result TF: can't set INFORMATION sense field\n");
224 return;
225 }
226
227 information = ata_tf_read_block(&qc->result_tf, qc->dev);
228 if (information == U64_MAX)
229 return;
230
231 scsi_set_sense_information(qc->scsicmd->sense_buffer,
232 SCSI_SENSE_BUFFERSIZE, information);
233 }
234
235 /**
236 * ata_scsi_set_passthru_sense_fields - Set ATA fields in sense buffer
237 * @qc: ATA PASS-THROUGH command.
238 *
239 * Populates "ATA Status Return sense data descriptor" / "Fixed format
240 * sense data" with ATA taskfile fields.
241 *
242 * LOCKING:
243 * None.
244 */
ata_scsi_set_passthru_sense_fields(struct ata_queued_cmd * qc)245 static void ata_scsi_set_passthru_sense_fields(struct ata_queued_cmd *qc)
246 {
247 struct ata_device *dev = qc->dev;
248 struct scsi_cmnd *cmd = qc->scsicmd;
249 struct ata_taskfile *tf = &qc->result_tf;
250 unsigned char *sb = cmd->sense_buffer;
251
252 if (!(qc->flags & ATA_QCFLAG_RTF_FILLED)) {
253 ata_dev_dbg(dev,
254 "missing result TF: can't set ATA PT sense fields\n");
255 return;
256 }
257
258 if ((sb[0] & 0x7f) >= 0x72) {
259 unsigned char *desc;
260 u8 len;
261
262 /* descriptor format */
263 len = sb[7];
264 desc = (char *)scsi_sense_desc_find(sb, SCSI_SENSE_BUFFERSIZE, 9);
265 if (!desc) {
266 /*
267 * The descriptor is written at sb[8 + len] and is 14
268 * bytes long, so it needs len + 22 bytes of buffer.
269 */
270 if (len + 22 > SCSI_SENSE_BUFFERSIZE)
271 return;
272 sb[7] = len + 14;
273 desc = sb + 8 + len;
274 } else if (desc - sb > SCSI_SENSE_BUFFERSIZE - 14) {
275 return;
276 }
277 desc[0] = 9;
278 desc[1] = 12;
279 /*
280 * Copy registers into sense buffer.
281 */
282 desc[2] = 0x00;
283 desc[3] = tf->error;
284 desc[5] = tf->nsect;
285 desc[7] = tf->lbal;
286 desc[9] = tf->lbam;
287 desc[11] = tf->lbah;
288 desc[12] = tf->device;
289 desc[13] = tf->status;
290
291 /*
292 * Fill in Extend bit, and the high order bytes
293 * if applicable.
294 */
295 if (tf->flags & ATA_TFLAG_LBA48) {
296 desc[2] |= 0x01;
297 desc[4] = tf->hob_nsect;
298 desc[6] = tf->hob_lbal;
299 desc[8] = tf->hob_lbam;
300 desc[10] = tf->hob_lbah;
301 }
302 } else {
303 /* Fixed sense format */
304 sb[0] |= 0x80;
305 sb[3] = tf->error;
306 sb[4] = tf->status;
307 sb[5] = tf->device;
308 sb[6] = tf->nsect;
309 if (tf->flags & ATA_TFLAG_LBA48) {
310 sb[8] |= 0x80;
311 if (tf->hob_nsect)
312 sb[8] |= 0x40;
313 if (tf->hob_lbal || tf->hob_lbam || tf->hob_lbah)
314 sb[8] |= 0x20;
315 }
316 sb[9] = tf->lbal;
317 sb[10] = tf->lbam;
318 sb[11] = tf->lbah;
319 }
320 }
321
ata_scsi_set_invalid_field(struct ata_device * dev,struct scsi_cmnd * cmd,u16 field,u8 bit)322 static void ata_scsi_set_invalid_field(struct ata_device *dev,
323 struct scsi_cmnd *cmd, u16 field, u8 bit)
324 {
325 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x24, 0x0);
326 /* "Invalid field in CDB" */
327 scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
328 field, bit, 1);
329 }
330
ata_scsi_set_invalid_parameter(struct ata_device * dev,struct scsi_cmnd * cmd,u16 field)331 static void ata_scsi_set_invalid_parameter(struct ata_device *dev,
332 struct scsi_cmnd *cmd, u16 field)
333 {
334 /* "Invalid field in parameter list" */
335 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x26, 0x0);
336 scsi_set_sense_field_pointer(cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE,
337 field, 0xff, 0);
338 }
339
340 static struct attribute *ata_common_sdev_attrs[] = {
341 &dev_attr_unload_heads.attr,
342 NULL
343 };
344
345 static const struct attribute_group ata_common_sdev_attr_group = {
346 .attrs = ata_common_sdev_attrs
347 };
348
349 const struct attribute_group *ata_common_sdev_groups[] = {
350 &ata_common_sdev_attr_group,
351 NULL
352 };
353 EXPORT_SYMBOL_GPL(ata_common_sdev_groups);
354
355 /**
356 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
357 * @sdev: SCSI device for which BIOS geometry is to be determined
358 * @unused: gendisk associated with @sdev
359 * @capacity: capacity of SCSI device
360 * @geom: location to which geometry will be output
361 *
362 * Generic bios head/sector/cylinder calculator
363 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
364 * mapping. Some situations may arise where the disk is not
365 * bootable if this is not used.
366 *
367 * LOCKING:
368 * Defined by the SCSI layer. We don't really care.
369 *
370 * RETURNS:
371 * Zero.
372 */
ata_std_bios_param(struct scsi_device * sdev,struct gendisk * unused,sector_t capacity,int geom[])373 int ata_std_bios_param(struct scsi_device *sdev, struct gendisk *unused,
374 sector_t capacity, int geom[])
375 {
376 geom[0] = 255;
377 geom[1] = 63;
378 sector_div(capacity, 255*63);
379 geom[2] = capacity;
380
381 return 0;
382 }
383 EXPORT_SYMBOL_GPL(ata_std_bios_param);
384
385 /**
386 * ata_scsi_unlock_native_capacity - unlock native capacity
387 * @sdev: SCSI device to adjust device capacity for
388 *
389 * This function is called if a partition on @sdev extends beyond
390 * the end of the device. It requests EH to unlock HPA.
391 *
392 * LOCKING:
393 * Defined by the SCSI layer. Might sleep.
394 */
ata_scsi_unlock_native_capacity(struct scsi_device * sdev)395 void ata_scsi_unlock_native_capacity(struct scsi_device *sdev)
396 {
397 struct ata_port *ap = ata_shost_to_port(sdev->host);
398 struct ata_device *dev;
399 unsigned long flags;
400
401 spin_lock_irqsave(ap->lock, flags);
402
403 dev = ata_scsi_find_dev(ap, sdev);
404 if (dev && dev->n_sectors < dev->n_native_sectors) {
405 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
406 dev->link->eh_info.action |= ATA_EH_RESET;
407 ata_port_schedule_eh(ap);
408 }
409
410 spin_unlock_irqrestore(ap->lock, flags);
411 ata_port_wait_eh(ap);
412 }
413 EXPORT_SYMBOL_GPL(ata_scsi_unlock_native_capacity);
414
415 /**
416 * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
417 * @ap: target port
418 * @sdev: SCSI device to get identify data for
419 * @arg: User buffer area for identify data
420 *
421 * LOCKING:
422 * Defined by the SCSI layer. We don't really care.
423 *
424 * RETURNS:
425 * Zero on success, negative errno on error.
426 */
ata_get_identity(struct ata_port * ap,struct scsi_device * sdev,void __user * arg)427 static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
428 void __user *arg)
429 {
430 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
431 u16 __user *dst = arg;
432 char buf[40];
433
434 if (!dev)
435 return -ENOMSG;
436
437 if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
438 return -EFAULT;
439
440 ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
441 if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
442 return -EFAULT;
443
444 ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
445 if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
446 return -EFAULT;
447
448 ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
449 if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
450 return -EFAULT;
451
452 return 0;
453 }
454
455 /**
456 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
457 * @scsidev: Device to which we are issuing command
458 * @arg: User provided data for issuing command
459 *
460 * LOCKING:
461 * Defined by the SCSI layer. We don't really care.
462 *
463 * RETURNS:
464 * Zero on success, negative errno on error.
465 */
ata_cmd_ioctl(struct scsi_device * scsidev,void __user * arg)466 int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
467 {
468 int rc = 0;
469 u8 sensebuf[SCSI_SENSE_BUFFERSIZE];
470 u8 scsi_cmd[MAX_COMMAND_SIZE];
471 u8 args[4], *argbuf = NULL;
472 int argsize = 0;
473 struct scsi_sense_hdr sshdr;
474 const struct scsi_exec_args exec_args = {
475 .sshdr = &sshdr,
476 .sense = sensebuf,
477 .sense_len = sizeof(sensebuf),
478 };
479 int cmd_result;
480
481 if (arg == NULL)
482 return -EINVAL;
483
484 if (copy_from_user(args, arg, sizeof(args)))
485 return -EFAULT;
486
487 memset(sensebuf, 0, sizeof(sensebuf));
488 memset(scsi_cmd, 0, sizeof(scsi_cmd));
489
490 if (args[3]) {
491 argsize = ATA_SECT_SIZE * args[3];
492 argbuf = kmalloc(argsize, GFP_KERNEL);
493 if (argbuf == NULL) {
494 rc = -ENOMEM;
495 goto error;
496 }
497
498 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
499 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
500 block count in sector count field */
501 } else {
502 scsi_cmd[1] = (3 << 1); /* Non-data */
503 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
504 }
505
506 scsi_cmd[0] = ATA_16;
507
508 scsi_cmd[4] = args[2];
509 if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
510 scsi_cmd[6] = args[3];
511 scsi_cmd[8] = args[1];
512 scsi_cmd[10] = ATA_SMART_LBAM_PASS;
513 scsi_cmd[12] = ATA_SMART_LBAH_PASS;
514 } else {
515 scsi_cmd[6] = args[1];
516 }
517 scsi_cmd[14] = args[0];
518
519 /* Good values for timeout and retries? Values below
520 from scsi_ioctl_send_command() for default case... */
521 cmd_result = scsi_execute_cmd(scsidev, scsi_cmd, REQ_OP_DRV_IN, argbuf,
522 argsize, 10 * HZ, 5, &exec_args);
523 if (cmd_result < 0) {
524 rc = cmd_result;
525 goto error;
526 }
527 if (scsi_sense_valid(&sshdr)) {/* sense data available */
528 u8 *desc = sensebuf + 8;
529
530 /* If we set cc then ATA pass-through will cause a
531 * check condition even if no error. Filter that. */
532 if (scsi_status_is_check_condition(cmd_result)) {
533 if (sshdr.sense_key == RECOVERED_ERROR &&
534 sshdr.asc == 0 && sshdr.ascq == 0x1d)
535 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
536 }
537
538 /* Send userspace a few ATA registers (same as drivers/ide) */
539 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
540 desc[0] == 0x09) { /* code is "ATA Descriptor" */
541 args[0] = desc[13]; /* status */
542 args[1] = desc[3]; /* error */
543 args[2] = desc[5]; /* sector count (0:7) */
544 if (copy_to_user(arg, args, sizeof(args)))
545 rc = -EFAULT;
546 }
547 }
548
549
550 if (cmd_result) {
551 rc = -EIO;
552 goto error;
553 }
554
555 if ((argbuf)
556 && copy_to_user(arg + sizeof(args), argbuf, argsize))
557 rc = -EFAULT;
558 error:
559 kfree(argbuf);
560 return rc;
561 }
562
563 /**
564 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
565 * @scsidev: Device to which we are issuing command
566 * @arg: User provided data for issuing command
567 *
568 * LOCKING:
569 * Defined by the SCSI layer. We don't really care.
570 *
571 * RETURNS:
572 * Zero on success, negative errno on error.
573 */
ata_task_ioctl(struct scsi_device * scsidev,void __user * arg)574 int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
575 {
576 int rc = 0;
577 u8 sensebuf[SCSI_SENSE_BUFFERSIZE];
578 u8 scsi_cmd[MAX_COMMAND_SIZE];
579 u8 args[7];
580 struct scsi_sense_hdr sshdr;
581 int cmd_result;
582 const struct scsi_exec_args exec_args = {
583 .sshdr = &sshdr,
584 .sense = sensebuf,
585 .sense_len = sizeof(sensebuf),
586 };
587
588 if (arg == NULL)
589 return -EINVAL;
590
591 if (copy_from_user(args, arg, sizeof(args)))
592 return -EFAULT;
593
594 memset(sensebuf, 0, sizeof(sensebuf));
595 memset(scsi_cmd, 0, sizeof(scsi_cmd));
596 scsi_cmd[0] = ATA_16;
597 scsi_cmd[1] = (3 << 1); /* Non-data */
598 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
599 scsi_cmd[4] = args[1];
600 scsi_cmd[6] = args[2];
601 scsi_cmd[8] = args[3];
602 scsi_cmd[10] = args[4];
603 scsi_cmd[12] = args[5];
604 scsi_cmd[13] = args[6] & 0x4f;
605 scsi_cmd[14] = args[0];
606
607 /* Good values for timeout and retries? Values below
608 from scsi_ioctl_send_command() for default case... */
609 cmd_result = scsi_execute_cmd(scsidev, scsi_cmd, REQ_OP_DRV_IN, NULL,
610 0, 10 * HZ, 5, &exec_args);
611 if (cmd_result < 0) {
612 rc = cmd_result;
613 goto error;
614 }
615 if (scsi_sense_valid(&sshdr)) {/* sense data available */
616 u8 *desc = sensebuf + 8;
617
618 /* If we set cc then ATA pass-through will cause a
619 * check condition even if no error. Filter that. */
620 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
621 if (sshdr.sense_key == RECOVERED_ERROR &&
622 sshdr.asc == 0 && sshdr.ascq == 0x1d)
623 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
624 }
625
626 /* Send userspace ATA registers */
627 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
628 desc[0] == 0x09) {/* code is "ATA Descriptor" */
629 args[0] = desc[13]; /* status */
630 args[1] = desc[3]; /* error */
631 args[2] = desc[5]; /* sector count (0:7) */
632 args[3] = desc[7]; /* lbal */
633 args[4] = desc[9]; /* lbam */
634 args[5] = desc[11]; /* lbah */
635 args[6] = desc[12]; /* select */
636 if (copy_to_user(arg, args, sizeof(args)))
637 rc = -EFAULT;
638 }
639 }
640
641 if (cmd_result) {
642 rc = -EIO;
643 goto error;
644 }
645
646 error:
647 return rc;
648 }
649
ata_ioc32(struct ata_port * ap)650 static bool ata_ioc32(struct ata_port *ap)
651 {
652 if (ap->flags & ATA_FLAG_PIO_DMA)
653 return true;
654 if (ap->pflags & ATA_PFLAG_PIO32)
655 return true;
656 return false;
657 }
658
659 /*
660 * This handles both native and compat commands, so anything added
661 * here must have a compatible argument, or check in_compat_syscall()
662 */
ata_sas_scsi_ioctl(struct ata_port * ap,struct scsi_device * scsidev,unsigned int cmd,void __user * arg)663 int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
664 unsigned int cmd, void __user *arg)
665 {
666 unsigned long val;
667 int rc = -EINVAL;
668 unsigned long flags;
669
670 switch (cmd) {
671 case HDIO_GET_32BIT:
672 spin_lock_irqsave(ap->lock, flags);
673 val = ata_ioc32(ap);
674 spin_unlock_irqrestore(ap->lock, flags);
675 #ifdef CONFIG_COMPAT
676 if (in_compat_syscall())
677 return put_user(val, (compat_ulong_t __user *)arg);
678 #endif
679 return put_user(val, (unsigned long __user *)arg);
680
681 case HDIO_SET_32BIT:
682 val = (unsigned long) arg;
683 rc = 0;
684 spin_lock_irqsave(ap->lock, flags);
685 if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
686 if (val)
687 ap->pflags |= ATA_PFLAG_PIO32;
688 else
689 ap->pflags &= ~ATA_PFLAG_PIO32;
690 } else {
691 if (val != ata_ioc32(ap))
692 rc = -EINVAL;
693 }
694 spin_unlock_irqrestore(ap->lock, flags);
695 return rc;
696
697 case HDIO_GET_IDENTITY:
698 return ata_get_identity(ap, scsidev, arg);
699
700 case HDIO_DRIVE_CMD:
701 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
702 return -EACCES;
703 return ata_cmd_ioctl(scsidev, arg);
704
705 case HDIO_DRIVE_TASK:
706 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
707 return -EACCES;
708 return ata_task_ioctl(scsidev, arg);
709
710 default:
711 rc = -ENOTTY;
712 break;
713 }
714
715 return rc;
716 }
717 EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
718
ata_scsi_ioctl(struct scsi_device * scsidev,unsigned int cmd,void __user * arg)719 int ata_scsi_ioctl(struct scsi_device *scsidev, unsigned int cmd,
720 void __user *arg)
721 {
722 return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
723 scsidev, cmd, arg);
724 }
725 EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
726
727 /**
728 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
729 * @dev: ATA device to which the new command is attached
730 * @cmd: SCSI command that originated this ATA command
731 *
732 * Obtain a reference to an unused ata_queued_cmd structure,
733 * which is the basic libata structure representing a single
734 * ATA command sent to the hardware.
735 *
736 * If a command was available, fill in the SCSI-specific
737 * portions of the structure with information on the
738 * current command.
739 *
740 * LOCKING:
741 * spin_lock_irqsave(host lock)
742 *
743 * RETURNS:
744 * Command allocated, or %NULL if none available.
745 */
ata_scsi_qc_new(struct ata_device * dev,struct scsi_cmnd * cmd)746 static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
747 struct scsi_cmnd *cmd)
748 {
749 struct ata_port *ap = dev->link->ap;
750 struct ata_queued_cmd *qc;
751 int tag;
752
753 if (unlikely(ata_port_is_frozen(ap)))
754 goto fail;
755
756 if (ap->flags & ATA_FLAG_SAS_HOST) {
757 /*
758 * SAS hosts may queue > ATA_MAX_QUEUE commands so use
759 * unique per-device budget token as a tag.
760 */
761 if (WARN_ON_ONCE(cmd->budget_token >= ATA_MAX_QUEUE))
762 goto fail;
763 tag = cmd->budget_token;
764 } else {
765 tag = scsi_cmd_to_rq(cmd)->tag;
766 }
767
768 qc = __ata_qc_from_tag(ap, tag);
769 qc->tag = qc->hw_tag = tag;
770 qc->ap = ap;
771 qc->dev = dev;
772
773 ata_qc_reinit(qc);
774
775 qc->scsicmd = cmd;
776 qc->scsidone = scsi_done;
777
778 qc->sg = scsi_sglist(cmd);
779 qc->n_elem = scsi_sg_count(cmd);
780
781 if (scsi_cmd_to_rq(cmd)->rq_flags & RQF_QUIET)
782 qc->flags |= ATA_QCFLAG_QUIET;
783
784 return qc;
785
786 fail:
787 set_host_byte(cmd, DID_OK);
788 set_status_byte(cmd, SAM_STAT_TASK_SET_FULL);
789 scsi_done(cmd);
790 return NULL;
791 }
792
ata_qc_set_pc_nbytes(struct ata_queued_cmd * qc)793 static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
794 {
795 struct scsi_cmnd *scmd = qc->scsicmd;
796
797 qc->extrabytes = scmd->extra_len;
798 qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
799 }
800
801 /**
802 * ata_to_sense_error - convert ATA error to SCSI error
803 * @drv_stat: value contained in ATA status register
804 * @drv_err: value contained in ATA error register
805 * @sk: the sense key we'll fill out
806 * @asc: the additional sense code we'll fill out
807 * @ascq: the additional sense code qualifier we'll fill out
808 *
809 * Converts an ATA error into a SCSI error. Fill out pointers to
810 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
811 * format sense blocks.
812 *
813 * LOCKING:
814 * spin_lock_irqsave(host lock)
815 */
ata_to_sense_error(u8 drv_stat,u8 drv_err,u8 * sk,u8 * asc,u8 * ascq)816 static void ata_to_sense_error(u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
817 u8 *ascq)
818 {
819 int i;
820
821 /* Based on the 3ware driver translation table */
822 static const unsigned char sense_table[][4] = {
823 /* BBD|ECC|ID|MAR */
824 {0xd1, ABORTED_COMMAND, 0x00, 0x00},
825 // Device busy Aborted command
826 /* BBD|ECC|ID */
827 {0xd0, ABORTED_COMMAND, 0x00, 0x00},
828 // Device busy Aborted command
829 /* ECC|MC|MARK */
830 {0x61, HARDWARE_ERROR, 0x00, 0x00},
831 // Device fault Hardware error
832 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
833 {0x84, ABORTED_COMMAND, 0x47, 0x00},
834 // Data CRC error SCSI parity error
835 /* MC|ID|ABRT|TRK0|MARK */
836 {0x37, NOT_READY, 0x04, 0x00},
837 // Unit offline Not ready
838 /* MCR|MARK */
839 {0x09, NOT_READY, 0x04, 0x00},
840 // Unrecovered disk error Not ready
841 /* Bad address mark */
842 {0x01, MEDIUM_ERROR, 0x13, 0x00},
843 // Address mark not found for data field
844 /* TRK0 - Track 0 not found */
845 {0x02, HARDWARE_ERROR, 0x00, 0x00},
846 // Hardware error
847 /* Abort: 0x04 is not translated here, see below */
848 /* Media change request */
849 {0x08, NOT_READY, 0x04, 0x00},
850 // FIXME: faking offline
851 /* SRV/IDNF - ID not found */
852 {0x10, ILLEGAL_REQUEST, 0x21, 0x00},
853 // Logical address out of range
854 /* MC - Media Changed */
855 {0x20, UNIT_ATTENTION, 0x28, 0x00},
856 // Not ready to ready change, medium may have changed
857 /* ECC - Uncorrectable ECC error */
858 {0x40, MEDIUM_ERROR, 0x11, 0x04},
859 // Unrecovered read error
860 /* BBD - block marked bad */
861 {0x80, MEDIUM_ERROR, 0x11, 0x04},
862 // Block marked bad Medium error, unrecovered read error
863 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
864 };
865 static const unsigned char stat_table[][4] = {
866 /* Busy: must be first because BUSY means no other bits valid */
867 { ATA_BUSY, ABORTED_COMMAND, 0x00, 0x00 },
868 /* Device fault: INTERNAL TARGET FAILURE */
869 { ATA_DF, HARDWARE_ERROR, 0x44, 0x00 },
870 /* Corrected data error */
871 { ATA_CORR, RECOVERED_ERROR, 0x00, 0x00 },
872
873 { 0xFF, 0xFF, 0xFF, 0xFF }, /* END mark */
874 };
875
876 /*
877 * Is this an error we can process/parse
878 */
879 if (drv_stat & ATA_BUSY) {
880 drv_err = 0; /* Ignore the err bits, they're invalid */
881 }
882
883 if (drv_err) {
884 /* Look for drv_err */
885 for (i = 0; sense_table[i][0] != 0xFF; i++) {
886 /* Look for best matches first */
887 if ((sense_table[i][0] & drv_err) ==
888 sense_table[i][0]) {
889 *sk = sense_table[i][1];
890 *asc = sense_table[i][2];
891 *ascq = sense_table[i][3];
892 return;
893 }
894 }
895 }
896
897 /*
898 * Fall back to interpreting status bits. Note that if the drv_err
899 * has only the ABRT bit set, we decode drv_stat. ABRT by itself
900 * is not descriptive enough.
901 */
902 for (i = 0; stat_table[i][0] != 0xFF; i++) {
903 if (stat_table[i][0] & drv_stat) {
904 *sk = stat_table[i][1];
905 *asc = stat_table[i][2];
906 *ascq = stat_table[i][3];
907 return;
908 }
909 }
910
911 /*
912 * We need a sensible error return here, which is tricky, and one
913 * that won't cause people to do things like return a disk wrongly.
914 */
915 *sk = ABORTED_COMMAND;
916 *asc = 0x00;
917 *ascq = 0x00;
918 }
919
920 /*
921 * ata_gen_passthru_sense - Generate check condition sense block.
922 * @qc: Command that completed.
923 *
924 * This function is specific to the ATA pass through commands.
925 * Regardless of whether the command errored or not, return a sense
926 * block. If there was no error, we get the request from an ATA
927 * passthrough command, so we use the following sense data:
928 * sk = RECOVERED ERROR
929 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
930 *
931 *
932 * LOCKING:
933 * None.
934 */
ata_gen_passthru_sense(struct ata_queued_cmd * qc)935 static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
936 {
937 struct ata_device *dev = qc->dev;
938 struct scsi_cmnd *cmd = qc->scsicmd;
939 struct ata_taskfile *tf = &qc->result_tf;
940 u8 sense_key, asc, ascq;
941
942 if (!(qc->flags & ATA_QCFLAG_RTF_FILLED)) {
943 ata_dev_dbg(dev,
944 "missing result TF: can't generate ATA PT sense data\n");
945 if (qc->err_mask)
946 ata_scsi_set_sense(dev, cmd, ABORTED_COMMAND, 0, 0);
947 return;
948 }
949
950 /*
951 * Use ata_to_sense_error() to map status register bits
952 * onto sense key, asc & ascq.
953 */
954 if (qc->err_mask ||
955 tf->status & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
956 ata_to_sense_error(tf->status, tf->error,
957 &sense_key, &asc, &ascq);
958 ata_scsi_set_sense(qc->dev, cmd, sense_key, asc, ascq);
959 } else {
960 /*
961 * ATA PASS-THROUGH INFORMATION AVAILABLE
962 *
963 * Note: we are supposed to call ata_scsi_set_sense(), which
964 * respects the D_SENSE bit, instead of unconditionally
965 * generating the sense data in descriptor format. However,
966 * because hdparm, hddtemp, and udisks incorrectly assume sense
967 * data in descriptor format, without even looking at the
968 * RESPONSE CODE field in the returned sense data (to see which
969 * format the returned sense data is in), we are stuck with
970 * being bug compatible with older kernels.
971 */
972 scsi_build_sense(cmd, 1, RECOVERED_ERROR, 0, 0x1D);
973 }
974 }
975
976 /**
977 * ata_gen_ata_sense - generate a SCSI fixed sense block
978 * @qc: Command that we are erroring out
979 *
980 * Generate sense block for a failed ATA command @qc.
981 *
982 * LOCKING:
983 * None.
984 */
ata_gen_ata_sense(struct ata_queued_cmd * qc)985 static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
986 {
987 struct ata_device *dev = qc->dev;
988 struct scsi_cmnd *cmd = qc->scsicmd;
989 struct ata_taskfile *tf = &qc->result_tf;
990 u8 sense_key, asc, ascq;
991
992 if (ata_dev_disabled(dev)) {
993 /* Device disabled after error recovery */
994 /* LOGICAL UNIT NOT READY, HARD RESET REQUIRED */
995 ata_scsi_set_sense(dev, cmd, NOT_READY, 0x04, 0x21);
996 return;
997 }
998
999 if (ata_id_is_locked(dev->id)) {
1000 /* Security locked */
1001 /* LOGICAL UNIT ACCESS NOT AUTHORIZED */
1002 ata_scsi_set_sense(dev, cmd, DATA_PROTECT, 0x74, 0x71);
1003 return;
1004 }
1005
1006 if (!(qc->flags & ATA_QCFLAG_RTF_FILLED)) {
1007 ata_dev_dbg(dev,
1008 "Missing result TF: reporting aborted command\n");
1009 goto aborted;
1010 }
1011
1012 /* Use ata_to_sense_error() to map status register bits
1013 * onto sense key, asc & ascq.
1014 */
1015 if (qc->err_mask ||
1016 tf->status & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
1017 ata_to_sense_error(tf->status, tf->error,
1018 &sense_key, &asc, &ascq);
1019 ata_scsi_set_sense(dev, cmd, sense_key, asc, ascq);
1020 return;
1021 }
1022
1023 /* Could not decode error */
1024 ata_dev_warn(dev,
1025 "Could not decode error 0x%x, status 0x%x (err_mask=0x%x)\n",
1026 tf->error, tf->status, qc->err_mask);
1027 aborted:
1028 ata_scsi_set_sense(dev, cmd, ABORTED_COMMAND, 0, 0);
1029 }
1030
ata_scsi_sdev_config(struct scsi_device * sdev)1031 void ata_scsi_sdev_config(struct scsi_device *sdev)
1032 {
1033 sdev->use_10_for_rw = 1;
1034 sdev->use_10_for_ms = 1;
1035 sdev->no_write_same = 1;
1036
1037 /* Schedule policy is determined by ->qc_defer() callback and
1038 * it needs to see every deferred qc. Set dev_blocked to 1 to
1039 * prevent SCSI midlayer from automatically deferring
1040 * requests.
1041 */
1042 sdev->max_device_blocked = 1;
1043 }
1044
1045 /**
1046 * ata_scsi_dma_need_drain - Check whether data transfer may overflow
1047 * @rq: request to be checked
1048 *
1049 * ATAPI commands which transfer variable length data to host
1050 * might overflow due to application error or hardware bug. This
1051 * function checks whether overflow should be drained and ignored
1052 * for @request.
1053 *
1054 * LOCKING:
1055 * None.
1056 *
1057 * RETURNS:
1058 * 1 if ; otherwise, 0.
1059 */
ata_scsi_dma_need_drain(struct request * rq)1060 bool ata_scsi_dma_need_drain(struct request *rq)
1061 {
1062 struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(rq);
1063
1064 return atapi_cmd_type(scmd->cmnd[0]) == ATAPI_MISC;
1065 }
1066 EXPORT_SYMBOL_GPL(ata_scsi_dma_need_drain);
1067
ata_scsi_dev_config(struct scsi_device * sdev,struct queue_limits * lim,struct ata_device * dev)1068 int ata_scsi_dev_config(struct scsi_device *sdev, struct queue_limits *lim,
1069 struct ata_device *dev)
1070 {
1071 int depth = 1;
1072
1073 if (!ata_id_has_unload(dev->id))
1074 dev->flags |= ATA_DFLAG_NO_UNLOAD;
1075
1076 /* configure max sectors */
1077 dev->max_sectors = min(dev->max_sectors, sdev->host->max_sectors);
1078 lim->max_hw_sectors = dev->max_sectors;
1079
1080 if (dev->class == ATA_DEV_ATAPI) {
1081 sdev->sector_size = ATA_SECT_SIZE;
1082
1083 /* set DMA padding */
1084 lim->dma_pad_mask = ATA_DMA_PAD_SZ - 1;
1085
1086 /* make room for appending the drain */
1087 lim->max_segments--;
1088
1089 sdev->dma_drain_len = ATAPI_MAX_DRAIN;
1090 sdev->dma_drain_buf = kmalloc(sdev->dma_drain_len, GFP_NOIO);
1091 if (!sdev->dma_drain_buf) {
1092 ata_dev_err(dev, "drain buffer allocation failed\n");
1093 return -ENOMEM;
1094 }
1095 } else {
1096 sdev->sector_size = ata_id_logical_sector_size(dev->id);
1097
1098 /*
1099 * Ask the sd driver to issue START STOP UNIT on runtime suspend
1100 * and resume and shutdown only. For system level suspend/resume,
1101 * devices power state is handled directly by libata EH.
1102 * Given that disks are always spun up on system resume, also
1103 * make sure that the sd driver forces runtime suspended disks
1104 * to be resumed to correctly reflect the power state of the
1105 * device.
1106 */
1107 sdev->manage_runtime_start_stop = 1;
1108 sdev->manage_shutdown = 1;
1109 sdev->manage_restart = ata_acpi_dev_manage_restart(dev);
1110 sdev->force_runtime_start_on_system_start = 1;
1111 }
1112
1113 /*
1114 * ata_pio_sectors() expects buffer for each sector to not cross
1115 * page boundary. Enforce it by requiring buffers to be sector
1116 * aligned, which works iff sector_size is not larger than
1117 * PAGE_SIZE. ATAPI devices also need the alignment as
1118 * IDENTIFY_PACKET is executed as ATA_PROT_PIO.
1119 */
1120 if (sdev->sector_size > PAGE_SIZE)
1121 ata_dev_warn(dev,
1122 "sector_size=%u > PAGE_SIZE, PIO may malfunction\n",
1123 sdev->sector_size);
1124
1125 lim->dma_alignment = sdev->sector_size - 1;
1126
1127 if (dev->flags & ATA_DFLAG_AN)
1128 set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
1129
1130 if (ata_ncq_supported(dev))
1131 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
1132 depth = min(ATA_MAX_QUEUE, depth);
1133 scsi_change_queue_depth(sdev, depth);
1134
1135 if (dev->flags & ATA_DFLAG_TRUSTED)
1136 sdev->security_supported = 1;
1137
1138 dev->sdev = sdev;
1139 return 0;
1140 }
1141
1142 /**
1143 * ata_scsi_sdev_init - Early setup of SCSI device
1144 * @sdev: SCSI device to examine
1145 *
1146 * This is called from scsi_alloc_sdev() when the scsi device
1147 * associated with an ATA device is scanned on a port.
1148 *
1149 * LOCKING:
1150 * Defined by SCSI layer. We don't really care.
1151 */
1152
ata_scsi_sdev_init(struct scsi_device * sdev)1153 int ata_scsi_sdev_init(struct scsi_device *sdev)
1154 {
1155 struct ata_port *ap = ata_shost_to_port(sdev->host);
1156 struct device_link *link;
1157
1158 ata_scsi_sdev_config(sdev);
1159
1160 /*
1161 * Create a link from the ata_port device to the scsi device to ensure
1162 * that PM does suspend/resume in the correct order: the scsi device is
1163 * consumer (child) and the ata port the supplier (parent).
1164 */
1165 link = device_link_add(&sdev->sdev_gendev, &ap->tdev,
1166 DL_FLAG_STATELESS |
1167 DL_FLAG_PM_RUNTIME | DL_FLAG_RPM_ACTIVE);
1168 if (!link) {
1169 ata_port_err(ap, "Failed to create link to scsi device %s\n",
1170 dev_name(&sdev->sdev_gendev));
1171 return -ENODEV;
1172 }
1173
1174 return 0;
1175 }
1176 EXPORT_SYMBOL_GPL(ata_scsi_sdev_init);
1177
1178 /**
1179 * ata_scsi_sdev_configure - Set SCSI device attributes
1180 * @sdev: SCSI device to examine
1181 * @lim: queue limits
1182 *
1183 * This is called before we actually start reading
1184 * and writing to the device, to configure certain
1185 * SCSI mid-layer behaviors.
1186 *
1187 * LOCKING:
1188 * Defined by SCSI layer. We don't really care.
1189 */
1190
ata_scsi_sdev_configure(struct scsi_device * sdev,struct queue_limits * lim)1191 int ata_scsi_sdev_configure(struct scsi_device *sdev, struct queue_limits *lim)
1192 {
1193 struct ata_port *ap = ata_shost_to_port(sdev->host);
1194 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
1195
1196 if (dev)
1197 return ata_scsi_dev_config(sdev, lim, dev);
1198
1199 return 0;
1200 }
1201 EXPORT_SYMBOL_GPL(ata_scsi_sdev_configure);
1202
1203 /**
1204 * ata_scsi_sdev_destroy - SCSI device is about to be destroyed
1205 * @sdev: SCSI device to be destroyed
1206 *
1207 * @sdev is about to be destroyed for hot/warm unplugging. If
1208 * this unplugging was initiated by libata as indicated by NULL
1209 * dev->sdev, this function doesn't have to do anything.
1210 * Otherwise, SCSI layer initiated warm-unplug is in progress.
1211 * Clear dev->sdev, schedule the device for ATA detach and invoke
1212 * EH.
1213 *
1214 * LOCKING:
1215 * Defined by SCSI layer. We don't really care.
1216 */
ata_scsi_sdev_destroy(struct scsi_device * sdev)1217 void ata_scsi_sdev_destroy(struct scsi_device *sdev)
1218 {
1219 struct ata_port *ap = ata_shost_to_port(sdev->host);
1220 unsigned long flags;
1221 struct ata_device *dev;
1222
1223 device_link_remove(&sdev->sdev_gendev, &ap->tdev);
1224
1225 spin_lock_irqsave(ap->lock, flags);
1226 dev = __ata_scsi_find_dev(ap, sdev);
1227 if (dev && dev->sdev) {
1228 /* SCSI device already in CANCEL state, no need to offline it */
1229 dev->sdev = NULL;
1230 dev->flags |= ATA_DFLAG_DETACH;
1231 ata_port_schedule_eh(ap);
1232 }
1233 spin_unlock_irqrestore(ap->lock, flags);
1234
1235 kfree(sdev->dma_drain_buf);
1236 }
1237 EXPORT_SYMBOL_GPL(ata_scsi_sdev_destroy);
1238
1239 /**
1240 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
1241 * @qc: Storage for translated ATA taskfile
1242 *
1243 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
1244 * (to start). Perhaps these commands should be preceded by
1245 * CHECK POWER MODE to see what power mode the device is already in.
1246 * [See SAT revision 5 at www.t10.org]
1247 *
1248 * LOCKING:
1249 * spin_lock_irqsave(host lock)
1250 *
1251 * RETURNS:
1252 * Zero on success, non-zero on error.
1253 */
ata_scsi_start_stop_xlat(struct ata_queued_cmd * qc)1254 static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
1255 {
1256 struct scsi_cmnd *scmd = qc->scsicmd;
1257 const u8 *cdb = scmd->cmnd;
1258 u16 fp;
1259 u8 bp = 0xff;
1260
1261 if (scmd->cmd_len < 5) {
1262 fp = 4;
1263 goto invalid_fld;
1264 }
1265
1266 /* LOEJ bit set not supported */
1267 if (cdb[4] & 0x2) {
1268 fp = 4;
1269 bp = 1;
1270 goto invalid_fld;
1271 }
1272
1273 /* Power conditions not supported */
1274 if (((cdb[4] >> 4) & 0xf) != 0) {
1275 fp = 4;
1276 bp = 3;
1277 goto invalid_fld;
1278 }
1279
1280 /* Ignore IMMED bit (cdb[1] & 0x1), violates sat-r05 */
1281 if (!ata_dev_power_init_tf(qc->dev, &qc->tf, cdb[4] & 0x1)) {
1282 ata_scsi_set_sense(qc->dev, scmd, ABORTED_COMMAND, 0, 0);
1283 return 1;
1284 }
1285
1286 /*
1287 * Standby and Idle condition timers could be implemented but that
1288 * would require libata to implement the Power condition mode page
1289 * and allow the user to change it. Changing mode pages requires
1290 * MODE SELECT to be implemented.
1291 */
1292
1293 return 0;
1294
1295 invalid_fld:
1296 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
1297 return 1;
1298 }
1299
1300 /**
1301 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
1302 * @qc: Storage for translated ATA taskfile
1303 *
1304 * Sets up an ATA taskfile to issue FLUSH CACHE or
1305 * FLUSH CACHE EXT.
1306 *
1307 * LOCKING:
1308 * spin_lock_irqsave(host lock)
1309 *
1310 * RETURNS:
1311 * Zero on success, non-zero on error.
1312 */
ata_scsi_flush_xlat(struct ata_queued_cmd * qc)1313 static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
1314 {
1315 struct ata_taskfile *tf = &qc->tf;
1316
1317 tf->flags |= ATA_TFLAG_DEVICE;
1318 tf->protocol = ATA_PROT_NODATA;
1319
1320 if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
1321 tf->command = ATA_CMD_FLUSH_EXT;
1322 else
1323 tf->command = ATA_CMD_FLUSH;
1324
1325 /* flush is critical for IO integrity, consider it an IO command */
1326 qc->flags |= ATA_QCFLAG_IO;
1327
1328 return 0;
1329 }
1330
1331 /**
1332 * scsi_6_lba_len - Get LBA and transfer length
1333 * @cdb: SCSI command to translate
1334 *
1335 * Calculate LBA and transfer length for 6-byte commands.
1336 *
1337 * RETURNS:
1338 * @plba: the LBA
1339 * @plen: the transfer length
1340 */
scsi_6_lba_len(const u8 * cdb,u64 * plba,u32 * plen)1341 static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1342 {
1343 *plba = get_unaligned_be24(&cdb[1]) & 0x1fffff;
1344 *plen = cdb[4];
1345 }
1346
1347 /**
1348 * scsi_10_lba_len - Get LBA and transfer length
1349 * @cdb: SCSI command to translate
1350 *
1351 * Calculate LBA and transfer length for 10-byte commands.
1352 *
1353 * RETURNS:
1354 * @plba: the LBA
1355 * @plen: the transfer length
1356 */
scsi_10_lba_len(const u8 * cdb,u64 * plba,u32 * plen)1357 static inline void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1358 {
1359 *plba = get_unaligned_be32(&cdb[2]);
1360 *plen = get_unaligned_be16(&cdb[7]);
1361 }
1362
1363 /**
1364 * scsi_16_lba_len - Get LBA and transfer length
1365 * @cdb: SCSI command to translate
1366 *
1367 * Calculate LBA and transfer length for 16-byte commands.
1368 *
1369 * RETURNS:
1370 * @plba: the LBA
1371 * @plen: the transfer length
1372 */
scsi_16_lba_len(const u8 * cdb,u64 * plba,u32 * plen)1373 static inline void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1374 {
1375 *plba = get_unaligned_be64(&cdb[2]);
1376 *plen = get_unaligned_be32(&cdb[10]);
1377 }
1378
1379 /**
1380 * scsi_dld - Get duration limit descriptor index
1381 * @cdb: SCSI command to translate
1382 *
1383 * Returns the dld bits indicating the index of a command duration limit
1384 * descriptor.
1385 */
scsi_dld(const u8 * cdb)1386 static inline int scsi_dld(const u8 *cdb)
1387 {
1388 return ((cdb[1] & 0x01) << 2) | ((cdb[14] >> 6) & 0x03);
1389 }
1390
1391 /**
1392 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1393 * @qc: Storage for translated ATA taskfile
1394 *
1395 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1396 *
1397 * LOCKING:
1398 * spin_lock_irqsave(host lock)
1399 *
1400 * RETURNS:
1401 * Zero on success, non-zero on error.
1402 */
ata_scsi_verify_xlat(struct ata_queued_cmd * qc)1403 static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
1404 {
1405 struct scsi_cmnd *scmd = qc->scsicmd;
1406 struct ata_taskfile *tf = &qc->tf;
1407 struct ata_device *dev = qc->dev;
1408 u64 dev_sectors = qc->dev->n_sectors;
1409 const u8 *cdb = scmd->cmnd;
1410 u64 block;
1411 u32 n_block;
1412 u16 fp;
1413
1414 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1415 tf->protocol = ATA_PROT_NODATA;
1416
1417 switch (cdb[0]) {
1418 case VERIFY:
1419 if (scmd->cmd_len < 10) {
1420 fp = 9;
1421 goto invalid_fld;
1422 }
1423 scsi_10_lba_len(cdb, &block, &n_block);
1424 break;
1425 case VERIFY_16:
1426 if (scmd->cmd_len < 16) {
1427 fp = 15;
1428 goto invalid_fld;
1429 }
1430 scsi_16_lba_len(cdb, &block, &n_block);
1431 break;
1432 default:
1433 fp = 0;
1434 goto invalid_fld;
1435 }
1436
1437 if (!n_block)
1438 goto nothing_to_do;
1439 if (block >= dev_sectors)
1440 goto out_of_range;
1441 if ((block + n_block) > dev_sectors)
1442 goto out_of_range;
1443
1444 if (dev->flags & ATA_DFLAG_LBA) {
1445 tf->flags |= ATA_TFLAG_LBA;
1446
1447 if (lba_28_ok(block, n_block)) {
1448 /* use LBA28 */
1449 tf->command = ATA_CMD_VERIFY;
1450 tf->device |= (block >> 24) & 0xf;
1451 } else if (lba_48_ok(block, n_block)) {
1452 if (!(dev->flags & ATA_DFLAG_LBA48))
1453 goto out_of_range;
1454
1455 /* use LBA48 */
1456 tf->flags |= ATA_TFLAG_LBA48;
1457 tf->command = ATA_CMD_VERIFY_EXT;
1458
1459 tf->hob_nsect = (n_block >> 8) & 0xff;
1460
1461 tf->hob_lbah = (block >> 40) & 0xff;
1462 tf->hob_lbam = (block >> 32) & 0xff;
1463 tf->hob_lbal = (block >> 24) & 0xff;
1464 } else
1465 /* request too large even for LBA48 */
1466 goto out_of_range;
1467
1468 tf->nsect = n_block & 0xff;
1469
1470 tf->lbah = (block >> 16) & 0xff;
1471 tf->lbam = (block >> 8) & 0xff;
1472 tf->lbal = block & 0xff;
1473
1474 tf->device |= ATA_LBA;
1475 } else {
1476 /* CHS */
1477 u32 sect, head, cyl, track;
1478
1479 if (!lba_28_ok(block, n_block))
1480 goto out_of_range;
1481
1482 /* Convert LBA to CHS */
1483 track = (u32)block / dev->sectors;
1484 cyl = track / dev->heads;
1485 head = track % dev->heads;
1486 sect = (u32)block % dev->sectors + 1;
1487
1488 /* Check whether the converted CHS can fit.
1489 Cylinder: 0-65535
1490 Head: 0-15
1491 Sector: 1-255*/
1492 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
1493 goto out_of_range;
1494
1495 tf->command = ATA_CMD_VERIFY;
1496 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1497 tf->lbal = sect;
1498 tf->lbam = cyl;
1499 tf->lbah = cyl >> 8;
1500 tf->device |= head;
1501 }
1502
1503 return 0;
1504
1505 invalid_fld:
1506 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
1507 return 1;
1508
1509 out_of_range:
1510 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
1511 /* "Logical Block Address out of range" */
1512 return 1;
1513
1514 nothing_to_do:
1515 scmd->result = SAM_STAT_GOOD;
1516 return 1;
1517 }
1518
ata_check_nblocks(struct scsi_cmnd * scmd,u32 n_blocks)1519 static bool ata_check_nblocks(struct scsi_cmnd *scmd, u32 n_blocks)
1520 {
1521 struct request *rq = scsi_cmd_to_rq(scmd);
1522 u32 req_blocks;
1523
1524 if (!blk_rq_is_passthrough(rq))
1525 return true;
1526
1527 req_blocks = blk_rq_bytes(rq) / scmd->device->sector_size;
1528 if (n_blocks > req_blocks)
1529 return false;
1530
1531 return true;
1532 }
1533
1534 /**
1535 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1536 * @qc: Storage for translated ATA taskfile
1537 *
1538 * Converts any of six SCSI read/write commands into the
1539 * ATA counterpart, including starting sector (LBA),
1540 * sector count, and taking into account the device's LBA48
1541 * support.
1542 *
1543 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1544 * %WRITE_16 are currently supported.
1545 *
1546 * LOCKING:
1547 * spin_lock_irqsave(host lock)
1548 *
1549 * RETURNS:
1550 * Zero on success, non-zero on error.
1551 */
ata_scsi_rw_xlat(struct ata_queued_cmd * qc)1552 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
1553 {
1554 struct scsi_cmnd *scmd = qc->scsicmd;
1555 const u8 *cdb = scmd->cmnd;
1556 struct request *rq = scsi_cmd_to_rq(scmd);
1557 int class = IOPRIO_PRIO_CLASS(req_get_ioprio(rq));
1558 unsigned int tf_flags = 0;
1559 int dld = 0;
1560 u64 block;
1561 u32 n_block;
1562 int rc;
1563 u16 fp = 0;
1564
1565 switch (cdb[0]) {
1566 case WRITE_6:
1567 case WRITE_10:
1568 case WRITE_16:
1569 tf_flags |= ATA_TFLAG_WRITE;
1570 break;
1571 }
1572
1573 /* Calculate the SCSI LBA, transfer length and FUA. */
1574 switch (cdb[0]) {
1575 case READ_10:
1576 case WRITE_10:
1577 if (unlikely(scmd->cmd_len < 10)) {
1578 fp = 9;
1579 goto invalid_fld;
1580 }
1581 scsi_10_lba_len(cdb, &block, &n_block);
1582 if (cdb[1] & (1 << 3))
1583 tf_flags |= ATA_TFLAG_FUA;
1584 if (!ata_check_nblocks(scmd, n_block))
1585 goto invalid_fld;
1586 break;
1587 case READ_6:
1588 case WRITE_6:
1589 if (unlikely(scmd->cmd_len < 6)) {
1590 fp = 5;
1591 goto invalid_fld;
1592 }
1593 scsi_6_lba_len(cdb, &block, &n_block);
1594
1595 /* for 6-byte r/w commands, transfer length 0
1596 * means 256 blocks of data, not 0 block.
1597 */
1598 if (!n_block)
1599 n_block = 256;
1600 if (!ata_check_nblocks(scmd, n_block))
1601 goto invalid_fld;
1602 break;
1603 case READ_16:
1604 case WRITE_16:
1605 if (unlikely(scmd->cmd_len < 16)) {
1606 fp = 15;
1607 goto invalid_fld;
1608 }
1609 scsi_16_lba_len(cdb, &block, &n_block);
1610 dld = scsi_dld(cdb);
1611 if (cdb[1] & (1 << 3))
1612 tf_flags |= ATA_TFLAG_FUA;
1613 if (!ata_check_nblocks(scmd, n_block))
1614 goto invalid_fld;
1615 break;
1616 default:
1617 fp = 0;
1618 goto invalid_fld;
1619 }
1620
1621 /* Check and compose ATA command */
1622 if (!n_block)
1623 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1624 * length 0 means transfer 0 block of data.
1625 * However, for ATA R/W commands, sector count 0 means
1626 * 256 or 65536 sectors, not 0 sectors as in SCSI.
1627 *
1628 * WARNING: one or two older ATA drives treat 0 as 0...
1629 */
1630 goto nothing_to_do;
1631
1632 qc->flags |= ATA_QCFLAG_IO;
1633 qc->nbytes = n_block * scmd->device->sector_size;
1634
1635 rc = ata_build_rw_tf(qc, block, n_block, tf_flags, dld, class);
1636 if (likely(rc == 0))
1637 return 0;
1638
1639 if (rc == -ERANGE)
1640 goto out_of_range;
1641 /* treat all other errors as -EINVAL, fall through */
1642 invalid_fld:
1643 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
1644 return 1;
1645
1646 out_of_range:
1647 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x21, 0x0);
1648 /* "Logical Block Address out of range" */
1649 return 1;
1650
1651 nothing_to_do:
1652 scmd->result = SAM_STAT_GOOD;
1653 return 1;
1654 }
1655
ata_scsi_qc_done(struct ata_queued_cmd * qc,bool set_result,u32 scmd_result)1656 static void ata_scsi_qc_done(struct ata_queued_cmd *qc, bool set_result,
1657 u32 scmd_result)
1658 {
1659 struct scsi_cmnd *cmd = qc->scsicmd;
1660 void (*done)(struct scsi_cmnd *) = qc->scsidone;
1661
1662 ata_qc_free(qc);
1663
1664 if (set_result)
1665 cmd->result = scmd_result;
1666 done(cmd);
1667 }
1668
ata_scsi_deferred_qc_work(struct work_struct * work)1669 void ata_scsi_deferred_qc_work(struct work_struct *work)
1670 {
1671 struct ata_link *link =
1672 container_of(work, struct ata_link, deferred_qc_work);
1673 struct ata_port *ap = link->ap;
1674 struct ata_queued_cmd *qc;
1675 unsigned long flags;
1676
1677 spin_lock_irqsave(ap->lock, flags);
1678
1679 /*
1680 * If we still have a deferred qc and we are not in EH, issue it. In
1681 * such case, we should not need any more deferring the qc, so warn if
1682 * qc_defer() says otherwise.
1683 */
1684 qc = link->deferred_qc;
1685 if (qc && !ata_port_eh_scheduled(ap)) {
1686 WARN_ON_ONCE(ap->ops->qc_defer(qc));
1687 link->deferred_qc = NULL;
1688 ata_qc_issue(ap, qc);
1689 }
1690
1691 spin_unlock_irqrestore(ap->lock, flags);
1692 }
1693
ata_scsi_requeue_deferred_qc(struct ata_port * ap,struct scsi_cmnd * timedout_scmd)1694 enum scsi_timeout_action ata_scsi_requeue_deferred_qc(struct ata_port *ap,
1695 struct scsi_cmnd *timedout_scmd)
1696 {
1697 enum scsi_timeout_action action = SCSI_EH_NOT_HANDLED;
1698 struct ata_queued_cmd *qc;
1699 struct ata_link *link;
1700 u32 host_byte;
1701
1702 lockdep_assert_held(ap->lock);
1703
1704 /*
1705 * If we have deferred QCs when a reset, a timeout or an NCQ command
1706 * fails, do not try to be smart about what to do with the deferred
1707 * commands and simply terminate them and let the SCSI layer decide
1708 * what to do.
1709 */
1710 ata_for_each_link(link, ap, PMP_FIRST) {
1711 qc = link->deferred_qc;
1712 if (!qc)
1713 continue;
1714
1715 /*
1716 * Clear the deferred QC so that the deferred work does not try
1717 * to issue it.
1718 */
1719 link->deferred_qc = NULL;
1720 cancel_work(&link->deferred_qc_work);
1721
1722 /*
1723 * We are going to complete some scsi command, either with
1724 * DID_TIME_OUT if the command timed out while waiting for being
1725 * issued, or with DID_REQUEUE if another command timed out or
1726 * we had a failed command. However, the block layer may re-issue
1727 * these commands immediately, keeping the scsi host busy and
1728 * thus preventing the SCSI EH task from running.
1729 * So schedule EH on the port to prevent accepting new commands
1730 * until everything is sorted out with the error or timeout that
1731 * got us here in the first place. Note that we set EH pending
1732 * on the port before calling ata_port_schedule_eh() so that we
1733 * do not reenter this function from ata_eh_set_pending() with
1734 * timedout_scmd being NULL and erroneously retry deferred QCs
1735 * that have timed out on other links.
1736 */
1737 if (!ata_port_eh_scheduled(ap)) {
1738 ap->pflags |= ATA_PFLAG_EH_PENDING;
1739 ata_port_schedule_eh(ap);
1740 }
1741
1742 /*
1743 * If we are being called from scsi_timeout(), then we have a
1744 * non-NULL timedout_scmd. If the timed out command is for a
1745 * deferred QC, terminate that deferred QC with DID_TIME_OUT and
1746 * requeue all other deferred QCs. In this case we need to
1747 * return SCSI_EH_DONE, because the timed out command was
1748 * handled.
1749 * If the timed out command is not for a deferred QC, we need to
1750 * requeue all deferred QCs, and return SCSI_EH_NOT_HANDLED so
1751 * that the timed out command gets added to the EH work queue
1752 * with scsi_eh_scmd_add(), for later handling with libata EH
1753 * ata_scsi_cmd_error_handler().
1754 * If timedout_scmd is NULL, we simply need to requeue all
1755 * deferred QCs and the return value does not matter as we were
1756 * not called from scsi_timeout().
1757 */
1758 if (timedout_scmd && qc->scsicmd == timedout_scmd) {
1759 host_byte = DID_TIME_OUT;
1760 action = SCSI_EH_DONE;
1761 } else {
1762 host_byte = DID_REQUEUE;
1763 }
1764 ata_scsi_qc_done(qc, true, host_byte << 16);
1765 }
1766
1767 return action;
1768 }
1769
ata_scsi_schedule_deferred_qc(struct ata_link * link)1770 static void ata_scsi_schedule_deferred_qc(struct ata_link *link)
1771 {
1772 struct ata_queued_cmd *qc = link->deferred_qc;
1773 struct ata_port *ap = link->ap;
1774
1775 lockdep_assert_held(ap->lock);
1776
1777 /*
1778 * If we have a deferred qc, then qc_defer() is defined and we can use
1779 * this callback to determine if this qc is good to go, unless EH has
1780 * been scheduled.
1781 */
1782 if (!qc)
1783 return;
1784
1785 if (ata_port_eh_scheduled(ap)) {
1786 ata_scsi_requeue_deferred_qc(ap, NULL);
1787 return;
1788 }
1789 if (!ap->ops->qc_defer(qc))
1790 queue_work(system_highpri_wq, &link->deferred_qc_work);
1791 }
1792
ata_scsi_retry_deferred_qc(struct ata_port * ap,struct scsi_cmnd * scmd)1793 enum scsi_timeout_action ata_scsi_retry_deferred_qc(struct ata_port *ap,
1794 struct scsi_cmnd *scmd)
1795 {
1796 enum scsi_timeout_action action;
1797 unsigned long flags;
1798
1799 spin_lock_irqsave(ap->lock, flags);
1800 action = ata_scsi_requeue_deferred_qc(ap, scmd);
1801 spin_unlock_irqrestore(ap->lock, flags);
1802
1803 return action;
1804 }
1805 EXPORT_SYMBOL_GPL(ata_scsi_retry_deferred_qc);
1806
ata_scsi_eh_timed_out(struct scsi_cmnd * scmd)1807 enum scsi_timeout_action ata_scsi_eh_timed_out(struct scsi_cmnd *scmd)
1808 {
1809 struct ata_port *ap = ata_shost_to_port(scmd->device->host);
1810
1811 /*
1812 * ata_scsi_cmd_error_handler() takes care of commands that timed out
1813 * while executing. However, if we have deferred QCs while a timeout
1814 * triggers, we must requeue these commands for retry so that we do not
1815 * unnecessarily delay starting the SCSI EH task until these deferred
1816 * commands also time out.
1817 */
1818 return ata_scsi_retry_deferred_qc(ap, scmd);
1819 }
1820 EXPORT_SYMBOL_GPL(ata_scsi_eh_timed_out);
1821
ata_scsi_qc_complete(struct ata_queued_cmd * qc)1822 static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
1823 {
1824 struct ata_link *link = qc->dev->link;
1825 struct scsi_cmnd *cmd = qc->scsicmd;
1826 u8 *cdb = cmd->cmnd;
1827 bool have_sense = qc->flags & ATA_QCFLAG_SENSE_VALID;
1828 bool is_ata_passthru = cdb[0] == ATA_16 || cdb[0] == ATA_12;
1829 bool is_ck_cond_request = cdb[2] & 0x20;
1830 bool is_error = qc->err_mask != 0;
1831
1832 /* For ATA pass thru (SAT) commands, generate a sense block if
1833 * user mandated it or if there's an error. Note that if we
1834 * generate because the user forced us to [CK_COND=1], a check
1835 * condition is generated and the ATA register values are returned
1836 * whether the command completed successfully or not. If there
1837 * was no error, and CK_COND=1, we use the following sense data:
1838 * sk = RECOVERED ERROR
1839 * asc,ascq = ATA PASS-THROUGH INFORMATION AVAILABLE
1840 */
1841 if (is_ata_passthru && (is_ck_cond_request || is_error || have_sense)) {
1842 if (!have_sense)
1843 ata_gen_passthru_sense(qc);
1844 ata_scsi_set_passthru_sense_fields(qc);
1845 if (is_ck_cond_request)
1846 set_status_byte(qc->scsicmd, SAM_STAT_CHECK_CONDITION);
1847 } else if (is_error) {
1848 if (!have_sense)
1849 ata_gen_ata_sense(qc);
1850 ata_scsi_set_sense_information(qc);
1851 }
1852
1853 ata_scsi_qc_done(qc, false, 0);
1854
1855 ata_scsi_schedule_deferred_qc(link);
1856 }
1857
ata_scsi_qc_issue(struct ata_port * ap,struct ata_queued_cmd * qc)1858 static int ata_scsi_qc_issue(struct ata_port *ap, struct ata_queued_cmd *qc)
1859 __must_hold(ap->lock)
1860 {
1861 struct ata_link *link = qc->dev->link;
1862 int ret;
1863
1864 if (!ap->ops->qc_defer)
1865 goto issue_qc;
1866
1867 /*
1868 * If we already have a deferred qc, then rely on the SCSI layer to
1869 * requeue and defer all incoming commands until the deferred qc is
1870 * processed, once all on-going commands complete.
1871 */
1872 if (link->deferred_qc) {
1873 ata_qc_free(qc);
1874 return SCSI_MLQUEUE_DEVICE_BUSY;
1875 }
1876
1877 /* Check if the command needs to be deferred. */
1878 ret = ap->ops->qc_defer(qc);
1879 switch (ret) {
1880 case 0:
1881 break;
1882 case ATA_DEFER_LINK:
1883 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1884 goto defer_qc;
1885 case ATA_DEFER_LINK_EXCL:
1886 /*
1887 * Drivers making use of ap->excl_link cannot store the QC in
1888 * link->deferred_qc, because the ap->excl_link handling is
1889 * incompatible with the link->deferred_qc workqueue handling.
1890 */
1891 ret = SCSI_MLQUEUE_DEVICE_BUSY;
1892 goto free_qc;
1893 case ATA_DEFER_PORT:
1894 ret = SCSI_MLQUEUE_HOST_BUSY;
1895 goto free_qc;
1896 default:
1897 WARN_ON_ONCE(1);
1898 ret = SCSI_MLQUEUE_HOST_BUSY;
1899 goto free_qc;
1900 }
1901
1902 issue_qc:
1903 ata_qc_issue(ap, qc);
1904 return 0;
1905
1906 defer_qc:
1907 /*
1908 * We must defer this qc: if this is not an NCQ command, keep
1909 * this qc as a deferred one and report to the SCSI layer that
1910 * we issued it so that it is not requeued. The deferred qc will
1911 * be issued with the port deferred_qc_work once all on-going
1912 * commands complete.
1913 */
1914 if (!ata_is_ncq(qc->tf.protocol)) {
1915 link->deferred_qc = qc;
1916 return 0;
1917 }
1918
1919 free_qc:
1920 /* Force a requeue of the command to defer its execution. */
1921 ata_qc_free(qc);
1922
1923 return ret;
1924 }
1925
1926 /**
1927 * ata_scsi_translate - Translate then issue SCSI command to ATA device
1928 * @dev: ATA device to which the command is addressed
1929 * @cmd: SCSI command to execute
1930 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1931 * @ap: ATA port of interest
1932 *
1933 * Our ->queuecommand() function has decided that the SCSI
1934 * command issued can be directly translated into an ATA
1935 * command, rather than handled internally.
1936 *
1937 * This function sets up an ata_queued_cmd structure for the
1938 * SCSI command, and sends that ata_queued_cmd to the hardware.
1939 *
1940 * The xlat_func argument (actor) returns 0 if ready to execute
1941 * ATA command, else 1 to finish translation. If 1 is returned
1942 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1943 * to be set reflecting an error condition or clean (early)
1944 * termination.
1945 *
1946 * LOCKING:
1947 * spin_lock_irqsave(host lock)
1948 *
1949 * RETURNS:
1950 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY or SCSI_MLQUEUE_HOST_BUSY if the
1951 * command needs to be deferred.
1952 */
ata_scsi_translate(struct ata_device * dev,struct scsi_cmnd * cmd,ata_xlat_func_t xlat_func,struct ata_port * ap)1953 static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
1954 ata_xlat_func_t xlat_func, struct ata_port *ap)
1955 __must_hold(ap->lock)
1956 {
1957 struct ata_queued_cmd *qc;
1958
1959 lockdep_assert_held(ap->lock);
1960
1961 /*
1962 * ata_scsi_qc_new() calls scsi_done(cmd) in case of failure. So we
1963 * have nothing further to do when allocating a qc fails.
1964 */
1965 qc = ata_scsi_qc_new(dev, cmd);
1966 if (!qc)
1967 return 0;
1968
1969 /* data is present; dma-map it */
1970 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1971 cmd->sc_data_direction == DMA_TO_DEVICE) {
1972 if (unlikely(scsi_bufflen(cmd) < 1)) {
1973 ata_dev_warn(dev, "WARNING: zero len r/w req\n");
1974 cmd->result = (DID_ERROR << 16);
1975 goto done;
1976 }
1977
1978 ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
1979 qc->dma_dir = cmd->sc_data_direction;
1980 }
1981
1982 qc->complete_fn = ata_scsi_qc_complete;
1983
1984 if (xlat_func(qc))
1985 goto done;
1986
1987 return ata_scsi_qc_issue(ap, qc);
1988
1989 done:
1990 ata_qc_free(qc);
1991 scsi_done(cmd);
1992 return 0;
1993 }
1994
1995 /**
1996 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1997 * @dev: Target device.
1998 * @cmd: SCSI command of interest.
1999 * @actor: Callback hook for desired SCSI command simulator
2000 *
2001 * Takes care of the hard work of simulating a SCSI command...
2002 * Mapping the response buffer, calling the command's handler,
2003 * and handling the handler's return value. This return value
2004 * indicates whether the handler wishes the SCSI command to be
2005 * completed successfully (0), or not (in which case cmd->result
2006 * and sense buffer are assumed to be set).
2007 *
2008 * LOCKING:
2009 * spin_lock_irqsave(host lock)
2010 */
ata_scsi_rbuf_fill(struct ata_device * dev,struct scsi_cmnd * cmd,unsigned int (* actor)(struct ata_device * dev,struct scsi_cmnd * cmd,u8 * rbuf))2011 static void ata_scsi_rbuf_fill(struct ata_device *dev, struct scsi_cmnd *cmd,
2012 unsigned int (*actor)(struct ata_device *dev,
2013 struct scsi_cmnd *cmd, u8 *rbuf))
2014 {
2015 unsigned long flags;
2016 unsigned int len;
2017
2018 spin_lock_irqsave(&ata_scsi_rbuf_lock, flags);
2019
2020 memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
2021 len = actor(dev, cmd, ata_scsi_rbuf);
2022 if (len) {
2023 if (WARN_ON(len > ATA_SCSI_RBUF_SIZE)) {
2024 ata_scsi_set_sense(dev, cmd, ABORTED_COMMAND, 0, 0);
2025 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, flags);
2026 return;
2027 }
2028 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
2029 ata_scsi_rbuf, len);
2030 cmd->result = SAM_STAT_GOOD;
2031 if (scsi_bufflen(cmd) > len)
2032 scsi_set_resid(cmd, scsi_bufflen(cmd) - len);
2033 }
2034
2035 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, flags);
2036 }
2037
2038 /**
2039 * ata_scsiop_inq_std - Simulate standard INQUIRY command
2040 * @dev: Target device.
2041 * @cmd: SCSI command of interest.
2042 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2043 *
2044 * Returns standard device identification data associated
2045 * with non-VPD INQUIRY command output.
2046 *
2047 * LOCKING:
2048 * spin_lock_irqsave(host lock)
2049 */
ata_scsiop_inq_std(struct ata_device * dev,struct scsi_cmnd * cmd,u8 * rbuf)2050 static unsigned int ata_scsiop_inq_std(struct ata_device *dev,
2051 struct scsi_cmnd *cmd, u8 *rbuf)
2052 {
2053 static const u8 versions[] = {
2054 0x00,
2055 0x60, /* SAM-3 (no version claimed) */
2056
2057 0x03,
2058 0x20, /* SBC-2 (no version claimed) */
2059
2060 0x03,
2061 0x00 /* SPC-3 (no version claimed) */
2062 };
2063 static const u8 versions_zbc[] = {
2064 0x00,
2065 0xA0, /* SAM-5 (no version claimed) */
2066
2067 0x06,
2068 0x00, /* SBC-4 (no version claimed) */
2069
2070 0x05,
2071 0xC0, /* SPC-5 (no version claimed) */
2072
2073 0x60,
2074 0x24, /* ZBC r05 */
2075 };
2076
2077 u8 hdr[] = {
2078 TYPE_DISK,
2079 0,
2080 0x5, /* claim SPC-3 version compatibility */
2081 2,
2082 95 - 4,
2083 0,
2084 0,
2085 2
2086 };
2087
2088 /*
2089 * Set the SCSI Removable Media Bit (RMB) if the ATA removable media
2090 * device bit (obsolete since ATA-8 ACS) is set.
2091 */
2092 if (ata_id_removable(dev->id))
2093 hdr[1] |= (1 << 7);
2094
2095 if (dev->class == ATA_DEV_ZAC) {
2096 hdr[0] = TYPE_ZBC;
2097 hdr[2] = 0x7; /* claim SPC-5 version compatibility */
2098 }
2099
2100 if (dev->flags & ATA_DFLAG_CDL)
2101 hdr[2] = 0xd; /* claim SPC-6 version compatibility */
2102
2103 memcpy(rbuf, hdr, sizeof(hdr));
2104 memcpy(&rbuf[8], "ATA ", 8);
2105 ata_id_string(dev->id, &rbuf[16], ATA_ID_PROD, 16);
2106
2107 /* From SAT, use last 2 words from fw rev unless they are spaces */
2108 ata_id_string(dev->id, &rbuf[32], ATA_ID_FW_REV + 2, 4);
2109 if (strncmp(&rbuf[32], " ", 4) == 0)
2110 ata_id_string(dev->id, &rbuf[32], ATA_ID_FW_REV, 4);
2111
2112 if (rbuf[32] == 0 || rbuf[32] == ' ')
2113 memcpy(&rbuf[32], "n/a ", 4);
2114
2115 if (ata_dev_is_zoned(dev))
2116 memcpy(rbuf + 58, versions_zbc, sizeof(versions_zbc));
2117 else
2118 memcpy(rbuf + 58, versions, sizeof(versions));
2119
2120 /*
2121 * Include all 8 possible version descriptors, even if not all of
2122 * them are popoulated.
2123 */
2124 return 96;
2125 }
2126
2127 /**
2128 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
2129 * @dev: Target device.
2130 * @cmd: SCSI command of interest.
2131 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2132 *
2133 * Returns list of inquiry VPD pages available.
2134 *
2135 * LOCKING:
2136 * spin_lock_irqsave(host lock)
2137 */
ata_scsiop_inq_00(struct ata_device * dev,struct scsi_cmnd * cmd,u8 * rbuf)2138 static unsigned int ata_scsiop_inq_00(struct ata_device *dev,
2139 struct scsi_cmnd *cmd, u8 *rbuf)
2140 {
2141 int i, num_pages = 0;
2142 static const u8 pages[] = {
2143 0x00, /* page 0x00, this page */
2144 0x80, /* page 0x80, unit serial no page */
2145 0x83, /* page 0x83, device ident page */
2146 0x89, /* page 0x89, ata info page */
2147 0xb0, /* page 0xb0, block limits page */
2148 0xb1, /* page 0xb1, block device characteristics page */
2149 0xb2, /* page 0xb2, thin provisioning page */
2150 0xb6, /* page 0xb6, zoned block device characteristics */
2151 0xb9, /* page 0xb9, concurrent positioning ranges */
2152 };
2153
2154 for (i = 0; i < sizeof(pages); i++) {
2155 if (pages[i] == 0xb6 && !ata_dev_is_zoned(dev))
2156 continue;
2157 rbuf[num_pages + 4] = pages[i];
2158 num_pages++;
2159 }
2160 rbuf[3] = num_pages; /* number of supported VPD pages */
2161
2162 return get_unaligned_be16(&rbuf[2]) + 4;
2163 }
2164
2165 /**
2166 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
2167 * @dev: Target device.
2168 * @cmd: SCSI command of interest.
2169 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2170 *
2171 * Returns ATA device serial number.
2172 *
2173 * LOCKING:
2174 * spin_lock_irqsave(host lock)
2175 */
ata_scsiop_inq_80(struct ata_device * dev,struct scsi_cmnd * cmd,u8 * rbuf)2176 static unsigned int ata_scsiop_inq_80(struct ata_device *dev,
2177 struct scsi_cmnd *cmd, u8 *rbuf)
2178 {
2179 static const u8 hdr[] = {
2180 0,
2181 0x80, /* this page code */
2182 0,
2183 ATA_ID_SERNO_LEN, /* page len */
2184 };
2185
2186 memcpy(rbuf, hdr, sizeof(hdr));
2187 ata_id_string(dev->id, (unsigned char *) &rbuf[4],
2188 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
2189
2190 return get_unaligned_be16(&rbuf[2]) + 4;
2191 }
2192
2193 /**
2194 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
2195 * @dev: Target device.
2196 * @cmd: SCSI command of interest.
2197 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2198 *
2199 * Yields two logical unit device identification designators:
2200 * - vendor specific ASCII containing the ATA serial number
2201 * - SAT defined "t10 vendor id based" containing ASCII vendor
2202 * name ("ATA "), model and serial numbers.
2203 *
2204 * LOCKING:
2205 * spin_lock_irqsave(host lock)
2206 */
ata_scsiop_inq_83(struct ata_device * dev,struct scsi_cmnd * cmd,u8 * rbuf)2207 static unsigned int ata_scsiop_inq_83(struct ata_device *dev,
2208 struct scsi_cmnd *cmd, u8 *rbuf)
2209 {
2210 const int sat_model_serial_desc_len = 68;
2211 int num;
2212
2213 rbuf[1] = 0x83; /* this page code */
2214 num = 4;
2215
2216 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
2217 rbuf[num + 0] = 2;
2218 rbuf[num + 3] = ATA_ID_SERNO_LEN;
2219 num += 4;
2220 ata_id_string(dev->id, (unsigned char *) rbuf + num,
2221 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
2222 num += ATA_ID_SERNO_LEN;
2223
2224 /* SAT defined lu model and serial numbers descriptor */
2225 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
2226 rbuf[num + 0] = 2;
2227 rbuf[num + 1] = 1;
2228 rbuf[num + 3] = sat_model_serial_desc_len;
2229 num += 4;
2230 memcpy(rbuf + num, "ATA ", 8);
2231 num += 8;
2232 ata_id_string(dev->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
2233 ATA_ID_PROD_LEN);
2234 num += ATA_ID_PROD_LEN;
2235 ata_id_string(dev->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
2236 ATA_ID_SERNO_LEN);
2237 num += ATA_ID_SERNO_LEN;
2238
2239 if (ata_id_has_wwn(dev->id)) {
2240 /* SAT defined lu world wide name */
2241 /* piv=0, assoc=lu, code_set=binary, designator=NAA */
2242 rbuf[num + 0] = 1;
2243 rbuf[num + 1] = 3;
2244 rbuf[num + 3] = ATA_ID_WWN_LEN;
2245 num += 4;
2246 ata_id_string(dev->id, (unsigned char *) rbuf + num,
2247 ATA_ID_WWN, ATA_ID_WWN_LEN);
2248 num += ATA_ID_WWN_LEN;
2249 }
2250 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
2251
2252 return get_unaligned_be16(&rbuf[2]) + 4;
2253 }
2254
2255 /**
2256 * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
2257 * @dev: Target device.
2258 * @cmd: SCSI command of interest.
2259 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2260 *
2261 * Yields SAT-specified ATA VPD page.
2262 *
2263 * LOCKING:
2264 * spin_lock_irqsave(host lock)
2265 */
ata_scsiop_inq_89(struct ata_device * dev,struct scsi_cmnd * cmd,u8 * rbuf)2266 static unsigned int ata_scsiop_inq_89(struct ata_device *dev,
2267 struct scsi_cmnd *cmd, u8 *rbuf)
2268 {
2269 rbuf[1] = 0x89; /* our page code */
2270 rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
2271 rbuf[3] = (0x238 & 0xff);
2272
2273 memcpy(&rbuf[8], "linux ", 8);
2274 memcpy(&rbuf[16], "libata ", 16);
2275 memcpy(&rbuf[32], DRV_VERSION, 4);
2276
2277 rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
2278 rbuf[37] = (1 << 7); /* bit 7 indicates Command FIS */
2279 /* TODO: PMP? */
2280
2281 /* we don't store the ATA device signature, so we fake it */
2282 rbuf[38] = ATA_DRDY; /* really, this is Status reg */
2283 rbuf[40] = 0x1;
2284 rbuf[48] = 0x1;
2285
2286 rbuf[56] = ATA_CMD_ID_ATA;
2287
2288 memcpy(&rbuf[60], &dev->id[0], 512);
2289
2290 return get_unaligned_be16(&rbuf[2]) + 4;
2291 }
2292
2293 /**
2294 * ata_dsm_trim_pages - maximum DSM TRIM payload for a device, in 512-byte pages
2295 * @dev: ATA device the DATA SET MANAGEMENT TRIM command will be sent to
2296 *
2297 * A DATA SET MANAGEMENT TRIM payload is a list of 512-byte pages, each holding
2298 * up to ATA_MAX_TRIM_RNUM (64) LBA Range Entries; the format is page-based and
2299 * unrelated to the logical sector size.
2300 *
2301 * The logical sector size still bounds it, though: the descriptor is written
2302 * directly into the WRITE SAME data-out buffer, which sd sizes to a single
2303 * logical block, so it can hold at most sector_size / 512 pages.
2304 *
2305 * Return: the maximum number of 512-byte pages a single translated WRITE SAME
2306 * command may send to @dev, that is the smaller of MAX PAGES PER DSM COMMAND
2307 * (IDENTIFY DEVICE word 105, when the device reports a non-zero limit) and
2308 * the logical sector size expressed in 512-byte pages; never less than one.
2309 */
ata_dsm_trim_pages(struct ata_device * dev)2310 static unsigned int ata_dsm_trim_pages(struct ata_device *dev)
2311 {
2312 unsigned int sector_size = ata_id_logical_sector_size(dev->id);
2313 unsigned int max_pages = ata_id_dsm_max_pages(dev->id);
2314 unsigned int pages = sector_size / ATA_SECT_SIZE;
2315
2316 /* If the device does not specify a limit, assume only a single page. */
2317 if (!max_pages)
2318 max_pages = 1;
2319
2320 pages = min_not_zero(pages, max_pages);
2321
2322 return pages;
2323 }
2324
2325 /**
2326 * ata_scsiop_inq_b0 - Simulate INQUIRY VPD page B0, Block Limits
2327 * @dev: Target device.
2328 * @cmd: SCSI command of interest.
2329 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2330 *
2331 * Return data for the VPD page B0h (Block Limits).
2332 *
2333 * LOCKING:
2334 * spin_lock_irqsave(host lock)
2335 */
ata_scsiop_inq_b0(struct ata_device * dev,struct scsi_cmnd * cmd,u8 * rbuf)2336 static unsigned int ata_scsiop_inq_b0(struct ata_device *dev,
2337 struct scsi_cmnd *cmd, u8 *rbuf)
2338 {
2339 u16 min_io_sectors;
2340
2341 rbuf[1] = 0xb0;
2342 rbuf[3] = 0x3c; /* required VPD size with unmap support */
2343
2344 /*
2345 * Optimal transfer length granularity.
2346 *
2347 * This is always one physical block, but for disks with a smaller
2348 * logical than physical sector size we need to figure out what the
2349 * latter is.
2350 */
2351 min_io_sectors = 1 << ata_id_log2_per_physical_sector(dev->id);
2352 put_unaligned_be16(min_io_sectors, &rbuf[6]);
2353
2354 /*
2355 * Optimal unmap granularity.
2356 *
2357 * The ATA spec doesn't even know about a granularity or alignment
2358 * for the TRIM command. We can leave away most of the unmap related
2359 * VPD page entries, but we have specifify a granularity to signal
2360 * that we support some form of unmap - in thise case via WRITE SAME
2361 * with the unmap bit set.
2362 */
2363 if (ata_id_has_trim(dev->id)) {
2364 unsigned int max_pages = ata_dsm_trim_pages(dev);
2365 u64 max_blocks = max_pages * ATA_MAX_TRIM_RNUM * (u64)U16_MAX;
2366
2367 if (dev->quirks & ATA_QUIRK_MAX_TRIM_128M)
2368 max_blocks = 128 << (20 - SECTOR_SHIFT);
2369
2370 put_unaligned_be64(max_blocks, &rbuf[36]);
2371 put_unaligned_be32(1, &rbuf[28]);
2372 }
2373
2374 return get_unaligned_be16(&rbuf[2]) + 4;
2375 }
2376
2377 /**
2378 * ata_scsiop_inq_b1 - Simulate INQUIRY VPD page B1, Block Device
2379 * Characteristics
2380 * @dev: Target device.
2381 * @cmd: SCSI command of interest.
2382 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2383 *
2384 * Return data for the VPD page B1h (Block Device Characteristics).
2385 *
2386 * LOCKING:
2387 * spin_lock_irqsave(host lock)
2388 */
ata_scsiop_inq_b1(struct ata_device * dev,struct scsi_cmnd * cmd,u8 * rbuf)2389 static unsigned int ata_scsiop_inq_b1(struct ata_device *dev,
2390 struct scsi_cmnd *cmd, u8 *rbuf)
2391 {
2392 int form_factor = ata_id_form_factor(dev->id);
2393 int media_rotation_rate = ata_id_rotation_rate(dev->id);
2394 u8 zoned = ata_id_zoned_cap(dev->id);
2395
2396 rbuf[1] = 0xb1;
2397 rbuf[3] = 0x3c;
2398 rbuf[4] = media_rotation_rate >> 8;
2399 rbuf[5] = media_rotation_rate;
2400 rbuf[7] = form_factor;
2401 if (zoned)
2402 rbuf[8] = (zoned << 4);
2403
2404 return get_unaligned_be16(&rbuf[2]) + 4;
2405 }
2406
2407 /**
2408 * ata_scsiop_inq_b2 - Simulate INQUIRY VPD page B2, Logical Block
2409 * Provisioning
2410 * @dev: Target device.
2411 * @cmd: SCSI command of interest.
2412 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2413 *
2414 * Return data for the VPD page B2h (Logical Block Provisioning).
2415 *
2416 * LOCKING:
2417 * spin_lock_irqsave(host lock)
2418 */
ata_scsiop_inq_b2(struct ata_device * dev,struct scsi_cmnd * cmd,u8 * rbuf)2419 static unsigned int ata_scsiop_inq_b2(struct ata_device *dev,
2420 struct scsi_cmnd *cmd, u8 *rbuf)
2421 {
2422 /* SCSI Thin Provisioning VPD page: SBC-3 rev 22 or later */
2423 rbuf[1] = 0xb2;
2424 rbuf[3] = 0x4;
2425 rbuf[5] = 1 << 6; /* TPWS */
2426
2427 return get_unaligned_be16(&rbuf[2]) + 4;
2428 }
2429
2430 /**
2431 * ata_scsiop_inq_b6 - Simulate INQUIRY VPD page B6, Zoned Block Device
2432 * Characteristics
2433 * @dev: Target device.
2434 * @cmd: SCSI command of interest.
2435 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2436 *
2437 * Return data for the VPD page B2h (Zoned Block Device Characteristics).
2438 *
2439 * LOCKING:
2440 * spin_lock_irqsave(host lock)
2441 */
ata_scsiop_inq_b6(struct ata_device * dev,struct scsi_cmnd * cmd,u8 * rbuf)2442 static unsigned int ata_scsiop_inq_b6(struct ata_device *dev,
2443 struct scsi_cmnd *cmd, u8 *rbuf)
2444 {
2445 if (!ata_dev_is_zoned(dev)) {
2446 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff);
2447 return 0;
2448 }
2449
2450 /*
2451 * zbc-r05 SCSI Zoned Block device characteristics VPD page
2452 */
2453 rbuf[1] = 0xb6;
2454 rbuf[3] = 0x3C;
2455
2456 /*
2457 * URSWRZ bit is only meaningful for host-managed ZAC drives
2458 */
2459 if (dev->zac_zoned_cap & 1)
2460 rbuf[4] |= 1;
2461 put_unaligned_be32(dev->zac_zones_optimal_open, &rbuf[8]);
2462 put_unaligned_be32(dev->zac_zones_optimal_nonseq, &rbuf[12]);
2463 put_unaligned_be32(dev->zac_zones_max_open, &rbuf[16]);
2464
2465 return get_unaligned_be16(&rbuf[2]) + 4;
2466 }
2467
2468 /**
2469 * ata_scsiop_inq_b9 - Simulate INQUIRY VPD page B9, Concurrent Positioning
2470 * Ranges
2471 * @dev: Target device.
2472 * @cmd: SCSI command of interest.
2473 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2474 *
2475 * Return data for the VPD page B9h (Concurrent Positioning Ranges).
2476 *
2477 * LOCKING:
2478 * spin_lock_irqsave(host lock)
2479 */
ata_scsiop_inq_b9(struct ata_device * dev,struct scsi_cmnd * cmd,u8 * rbuf)2480 static unsigned int ata_scsiop_inq_b9(struct ata_device *dev,
2481 struct scsi_cmnd *cmd, u8 *rbuf)
2482 {
2483 struct ata_cpr_log *cpr_log = dev->cpr_log;
2484 u8 *desc = &rbuf[64];
2485 int i;
2486
2487 if (!cpr_log) {
2488 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff);
2489 return 0;
2490 }
2491
2492 /* SCSI Concurrent Positioning Ranges VPD page: SBC-5 rev 1 or later */
2493 rbuf[1] = 0xb9;
2494 put_unaligned_be16(64 + (int)cpr_log->nr_cpr * 32 - 4, &rbuf[2]);
2495
2496 for (i = 0; i < cpr_log->nr_cpr; i++, desc += 32) {
2497 desc[0] = cpr_log->cpr[i].num;
2498 desc[1] = cpr_log->cpr[i].num_storage_elements;
2499 put_unaligned_be64(cpr_log->cpr[i].start_lba, &desc[8]);
2500 put_unaligned_be64(cpr_log->cpr[i].num_lbas, &desc[16]);
2501 }
2502
2503 return get_unaligned_be16(&rbuf[2]) + 4;
2504 }
2505
2506 /**
2507 * ata_scsiop_inquiry - Simulate INQUIRY command
2508 * @dev: Target device.
2509 * @cmd: SCSI command of interest.
2510 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2511 *
2512 * Returns data associated with an INQUIRY command output.
2513 *
2514 * LOCKING:
2515 * spin_lock_irqsave(host lock)
2516 */
ata_scsiop_inquiry(struct ata_device * dev,struct scsi_cmnd * cmd,u8 * rbuf)2517 static unsigned int ata_scsiop_inquiry(struct ata_device *dev,
2518 struct scsi_cmnd *cmd, u8 *rbuf)
2519 {
2520 const u8 *scsicmd = cmd->cmnd;
2521
2522 /* is CmdDt set? */
2523 if (scsicmd[1] & 2) {
2524 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
2525 return 0;
2526 }
2527
2528 /* Is EVPD clear? */
2529 if ((scsicmd[1] & 1) == 0)
2530 return ata_scsiop_inq_std(dev, cmd, rbuf);
2531
2532 switch (scsicmd[2]) {
2533 case 0x00:
2534 return ata_scsiop_inq_00(dev, cmd, rbuf);
2535 case 0x80:
2536 return ata_scsiop_inq_80(dev, cmd, rbuf);
2537 case 0x83:
2538 return ata_scsiop_inq_83(dev, cmd, rbuf);
2539 case 0x89:
2540 return ata_scsiop_inq_89(dev, cmd, rbuf);
2541 case 0xb0:
2542 return ata_scsiop_inq_b0(dev, cmd, rbuf);
2543 case 0xb1:
2544 return ata_scsiop_inq_b1(dev, cmd, rbuf);
2545 case 0xb2:
2546 return ata_scsiop_inq_b2(dev, cmd, rbuf);
2547 case 0xb6:
2548 return ata_scsiop_inq_b6(dev, cmd, rbuf);
2549 case 0xb9:
2550 return ata_scsiop_inq_b9(dev, cmd, rbuf);
2551 default:
2552 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff);
2553 return 0;
2554 }
2555 }
2556
2557 /**
2558 * modecpy - Prepare response for MODE SENSE
2559 * @dest: output buffer
2560 * @src: data being copied
2561 * @n: length of mode page
2562 * @changeable: whether changeable parameters are requested
2563 *
2564 * Generate a generic MODE SENSE page for either current or changeable
2565 * parameters.
2566 *
2567 * LOCKING:
2568 * None.
2569 */
modecpy(u8 * dest,const u8 * src,int n,bool changeable)2570 static void modecpy(u8 *dest, const u8 *src, int n, bool changeable)
2571 {
2572 if (changeable) {
2573 memcpy(dest, src, 2);
2574 memset(dest + 2, 0, n - 2);
2575 } else {
2576 memcpy(dest, src, n);
2577 }
2578 }
2579
2580 /**
2581 * ata_msense_caching - Simulate MODE SENSE caching info page
2582 * @id: device IDENTIFY data
2583 * @buf: output buffer
2584 * @changeable: whether changeable parameters are requested
2585 *
2586 * Generate a caching info page, which conditionally indicates
2587 * write caching to the SCSI layer, depending on device
2588 * capabilities.
2589 *
2590 * LOCKING:
2591 * None.
2592 */
ata_msense_caching(u16 * id,u8 * buf,bool changeable)2593 static unsigned int ata_msense_caching(u16 *id, u8 *buf, bool changeable)
2594 {
2595 modecpy(buf, def_cache_mpage, sizeof(def_cache_mpage), changeable);
2596 if (changeable) {
2597 buf[2] |= (1 << 2); /* ata_mselect_caching() */
2598 } else {
2599 buf[2] |= (ata_id_wcache_enabled(id) << 2); /* write cache enable */
2600 buf[12] |= (!ata_id_rahead_enabled(id) << 5); /* disable read ahead */
2601 }
2602 return sizeof(def_cache_mpage);
2603 }
2604
2605 /*
2606 * Simulate MODE SENSE control mode page, sub-page 0.
2607 */
ata_msense_control_spg0(struct ata_device * dev,u8 * buf,bool changeable)2608 static unsigned int ata_msense_control_spg0(struct ata_device *dev, u8 *buf,
2609 bool changeable)
2610 {
2611 modecpy(buf, def_control_mpage,
2612 sizeof(def_control_mpage), changeable);
2613 if (changeable) {
2614 /* ata_mselect_control() */
2615 buf[2] |= (1 << 2);
2616 } else {
2617 bool d_sense = (dev->flags & ATA_DFLAG_D_SENSE);
2618
2619 /* descriptor format sense data */
2620 buf[2] |= (d_sense << 2);
2621 }
2622
2623 return sizeof(def_control_mpage);
2624 }
2625
2626 /*
2627 * Translate an ATA duration limit in microseconds to a SCSI duration limit
2628 * using the t2cdlunits 0xa (10ms). Since the SCSI duration limits are 2-bytes
2629 * only, take care of overflows.
2630 */
ata_xlat_cdl_limit(u8 * buf)2631 static inline u16 ata_xlat_cdl_limit(u8 *buf)
2632 {
2633 u32 limit = get_unaligned_le32(buf);
2634
2635 return min_t(u32, limit / 10000, 65535);
2636 }
2637
2638 /*
2639 * Simulate MODE SENSE control mode page, sub-pages 07h and 08h
2640 * (command duration limits T2A and T2B mode pages).
2641 */
ata_msense_control_spgt2(struct ata_device * dev,u8 * buf,u8 spg)2642 static unsigned int ata_msense_control_spgt2(struct ata_device *dev, u8 *buf,
2643 u8 spg)
2644 {
2645 u8 *b, *cdl, *desc;
2646 u32 policy;
2647 int i;
2648
2649 if (!(dev->flags & ATA_DFLAG_CDL) || !dev->cdl)
2650 return 0;
2651
2652 cdl = dev->cdl->desc_log_buf;
2653
2654 /*
2655 * Fill the subpage. The first four bytes of the T2A/T2B mode pages
2656 * are a header. The PAGE LENGTH field is the size of the page
2657 * excluding the header.
2658 */
2659 buf[0] = CONTROL_MPAGE;
2660 buf[1] = spg;
2661 put_unaligned_be16(CDL_T2_SUB_MPAGE_LEN - 4, &buf[2]);
2662 if (spg == CDL_T2A_SUB_MPAGE) {
2663 /*
2664 * Read descriptors map to the T2A page:
2665 * set perf_vs_duration_guidleine.
2666 */
2667 buf[7] = (cdl[0] & 0x03) << 4;
2668 desc = cdl + 64;
2669 } else {
2670 /* Write descriptors map to the T2B page */
2671 desc = cdl + 288;
2672 }
2673
2674 /* Fill the T2 page descriptors */
2675 b = &buf[8];
2676 policy = get_unaligned_le32(&cdl[0]);
2677 for (i = 0; i < 7; i++, b += 32, desc += 32) {
2678 /* t2cdlunits: fixed to 10ms */
2679 b[0] = 0x0a;
2680
2681 /* Max inactive time and its policy */
2682 put_unaligned_be16(ata_xlat_cdl_limit(&desc[8]), &b[2]);
2683 b[6] = ((policy >> 8) & 0x0f) << 4;
2684
2685 /* Max active time and its policy */
2686 put_unaligned_be16(ata_xlat_cdl_limit(&desc[4]), &b[4]);
2687 b[6] |= (policy >> 4) & 0x0f;
2688
2689 /* Command duration guideline and its policy */
2690 put_unaligned_be16(ata_xlat_cdl_limit(&desc[16]), &b[10]);
2691 b[14] = policy & 0x0f;
2692 }
2693
2694 return CDL_T2_SUB_MPAGE_LEN;
2695 }
2696
2697 /*
2698 * Simulate MODE SENSE control mode page, sub-page f2h
2699 * (ATA feature control mode page).
2700 */
ata_msense_control_ata_feature(struct ata_device * dev,u8 * buf)2701 static unsigned int ata_msense_control_ata_feature(struct ata_device *dev,
2702 u8 *buf)
2703 {
2704 /* PS=0, SPF=1 */
2705 buf[0] = CONTROL_MPAGE | (1 << 6);
2706 buf[1] = ATA_FEATURE_SUB_MPAGE;
2707
2708 /*
2709 * The first four bytes of ATA Feature Control mode page are a header.
2710 * The PAGE LENGTH field is the size of the page excluding the header.
2711 */
2712 put_unaligned_be16(ATA_FEATURE_SUB_MPAGE_LEN - 4, &buf[2]);
2713
2714 if (dev->flags & ATA_DFLAG_CDL_ENABLED)
2715 buf[4] = 0x02; /* T2A and T2B pages enabled */
2716 else
2717 buf[4] = 0;
2718
2719 return ATA_FEATURE_SUB_MPAGE_LEN;
2720 }
2721
2722 /**
2723 * ata_msense_control - Simulate MODE SENSE control mode page
2724 * @dev: ATA device of interest
2725 * @buf: output buffer
2726 * @spg: sub-page code
2727 * @changeable: whether changeable parameters are requested
2728 *
2729 * Generate a generic MODE SENSE control mode page.
2730 *
2731 * LOCKING:
2732 * None.
2733 */
ata_msense_control(struct ata_device * dev,u8 * buf,u8 spg,bool changeable)2734 static unsigned int ata_msense_control(struct ata_device *dev, u8 *buf,
2735 u8 spg, bool changeable)
2736 {
2737 unsigned int n;
2738
2739 switch (spg) {
2740 case 0:
2741 return ata_msense_control_spg0(dev, buf, changeable);
2742 case CDL_T2A_SUB_MPAGE:
2743 case CDL_T2B_SUB_MPAGE:
2744 return ata_msense_control_spgt2(dev, buf, spg);
2745 case ATA_FEATURE_SUB_MPAGE:
2746 return ata_msense_control_ata_feature(dev, buf);
2747 case ALL_SUB_MPAGES:
2748 n = ata_msense_control_spg0(dev, buf, changeable);
2749 n += ata_msense_control_spgt2(dev, buf + n, CDL_T2A_SUB_MPAGE);
2750 n += ata_msense_control_spgt2(dev, buf + n, CDL_T2B_SUB_MPAGE);
2751 n += ata_msense_control_ata_feature(dev, buf + n);
2752 return n;
2753 default:
2754 return 0;
2755 }
2756 }
2757
2758 /**
2759 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
2760 * @buf: output buffer
2761 * @changeable: whether changeable parameters are requested
2762 *
2763 * Generate a generic MODE SENSE r/w error recovery page.
2764 *
2765 * LOCKING:
2766 * None.
2767 */
ata_msense_rw_recovery(u8 * buf,bool changeable)2768 static unsigned int ata_msense_rw_recovery(u8 *buf, bool changeable)
2769 {
2770 modecpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage),
2771 changeable);
2772 return sizeof(def_rw_recovery_mpage);
2773 }
2774
2775 /**
2776 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
2777 * @dev: Target device.
2778 * @cmd: SCSI command of interest.
2779 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2780 *
2781 * Simulate MODE SENSE commands. Assume this is invoked for direct
2782 * access devices (e.g. disks) only. There should be no block
2783 * descriptor for other device types.
2784 *
2785 * LOCKING:
2786 * spin_lock_irqsave(host lock)
2787 */
ata_scsiop_mode_sense(struct ata_device * dev,struct scsi_cmnd * cmd,u8 * rbuf)2788 static unsigned int ata_scsiop_mode_sense(struct ata_device *dev,
2789 struct scsi_cmnd *cmd, u8 *rbuf)
2790 {
2791 u8 *scsicmd = cmd->cmnd, *p = rbuf;
2792 static const u8 sat_blk_desc[] = {
2793 0, 0, 0, 0, /* number of blocks: sat unspecified */
2794 0,
2795 0, 0x2, 0x0 /* block length: 512 bytes */
2796 };
2797 u8 pg, spg;
2798 unsigned int ebd, page_control, six_byte;
2799 u8 dpofua = 0, bp = 0xff;
2800 u16 fp;
2801
2802 six_byte = (scsicmd[0] == MODE_SENSE);
2803 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
2804 /*
2805 * LLBA bit in msense(10) ignored (compliant)
2806 */
2807
2808 page_control = scsicmd[2] >> 6;
2809 switch (page_control) {
2810 case 0: /* current */
2811 case 1: /* changeable */
2812 case 2: /* defaults */
2813 break; /* supported */
2814 case 3: /* saved */
2815 goto saving_not_supp;
2816 default:
2817 fp = 2;
2818 bp = 6;
2819 goto invalid_fld;
2820 }
2821
2822 if (six_byte)
2823 p += 4 + (ebd ? 8 : 0);
2824 else
2825 p += 8 + (ebd ? 8 : 0);
2826
2827 pg = scsicmd[2] & 0x3f;
2828 spg = scsicmd[3];
2829
2830 /*
2831 * Supported subpages: all subpages and sub-pages 07h, 08h and f2h of
2832 * the control page.
2833 */
2834 if (spg) {
2835 switch (spg) {
2836 case ALL_SUB_MPAGES:
2837 break;
2838 case CDL_T2A_SUB_MPAGE:
2839 case CDL_T2B_SUB_MPAGE:
2840 case ATA_FEATURE_SUB_MPAGE:
2841 if (dev->flags & ATA_DFLAG_CDL && pg == CONTROL_MPAGE)
2842 break;
2843 fallthrough;
2844 default:
2845 fp = 3;
2846 goto invalid_fld;
2847 }
2848 }
2849
2850 switch(pg) {
2851 case RW_RECOVERY_MPAGE:
2852 p += ata_msense_rw_recovery(p, page_control == 1);
2853 break;
2854
2855 case CACHE_MPAGE:
2856 p += ata_msense_caching(dev->id, p, page_control == 1);
2857 break;
2858
2859 case CONTROL_MPAGE:
2860 p += ata_msense_control(dev, p, spg, page_control == 1);
2861 break;
2862
2863 case ALL_MPAGES:
2864 p += ata_msense_rw_recovery(p, page_control == 1);
2865 p += ata_msense_caching(dev->id, p, page_control == 1);
2866 p += ata_msense_control(dev, p, spg, page_control == 1);
2867 break;
2868
2869 default: /* invalid page code */
2870 fp = 2;
2871 goto invalid_fld;
2872 }
2873
2874 if (dev->flags & ATA_DFLAG_FUA)
2875 dpofua = 1 << 4;
2876
2877 if (six_byte) {
2878 rbuf[0] = p - rbuf - 1;
2879 rbuf[2] |= dpofua;
2880 if (ebd) {
2881 rbuf[3] = sizeof(sat_blk_desc);
2882 memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
2883 }
2884
2885 return rbuf[0] + 1;
2886 }
2887
2888 put_unaligned_be16(p - rbuf - 2, &rbuf[0]);
2889 rbuf[3] |= dpofua;
2890 if (ebd) {
2891 rbuf[7] = sizeof(sat_blk_desc);
2892 memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
2893 }
2894
2895 return get_unaligned_be16(&rbuf[0]) + 2;
2896
2897 invalid_fld:
2898 ata_scsi_set_invalid_field(dev, cmd, fp, bp);
2899 return 0;
2900
2901 saving_not_supp:
2902 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x39, 0x0);
2903 /* "Saving parameters not supported" */
2904 return 0;
2905 }
2906
2907 /**
2908 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2909 * @dev: Target device.
2910 * @cmd: SCSI command of interest.
2911 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2912 *
2913 * Simulate READ CAPACITY commands.
2914 *
2915 * LOCKING:
2916 * None.
2917 */
ata_scsiop_read_cap(struct ata_device * dev,struct scsi_cmnd * cmd,u8 * rbuf)2918 static unsigned int ata_scsiop_read_cap(struct ata_device *dev,
2919 struct scsi_cmnd *cmd, u8 *rbuf)
2920 {
2921 u8 *scsicmd = cmd->cmnd;
2922 u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
2923 u32 sector_size; /* physical sector size in bytes */
2924 u8 log2_per_phys;
2925 u16 lowest_aligned;
2926
2927 sector_size = ata_id_logical_sector_size(dev->id);
2928 log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
2929 lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
2930
2931 if (scsicmd[0] == READ_CAPACITY) {
2932 if (last_lba >= 0xffffffffULL)
2933 last_lba = 0xffffffff;
2934
2935 /* sector count, 32-bit */
2936 rbuf[0] = last_lba >> (8 * 3);
2937 rbuf[1] = last_lba >> (8 * 2);
2938 rbuf[2] = last_lba >> (8 * 1);
2939 rbuf[3] = last_lba;
2940
2941 /* sector size */
2942 rbuf[4] = sector_size >> (8 * 3);
2943 rbuf[5] = sector_size >> (8 * 2);
2944 rbuf[6] = sector_size >> (8 * 1);
2945 rbuf[7] = sector_size;
2946
2947 return 8;
2948 }
2949
2950 /*
2951 * READ CAPACITY 16 command is defined as a service action
2952 * (SERVICE_ACTION_IN_16 command).
2953 */
2954 if (scsicmd[0] != SERVICE_ACTION_IN_16 ||
2955 (scsicmd[1] & 0x1f) != SAI_READ_CAPACITY_16) {
2956 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
2957 return 0;
2958 }
2959
2960 /* sector count, 64-bit */
2961 rbuf[0] = last_lba >> (8 * 7);
2962 rbuf[1] = last_lba >> (8 * 6);
2963 rbuf[2] = last_lba >> (8 * 5);
2964 rbuf[3] = last_lba >> (8 * 4);
2965 rbuf[4] = last_lba >> (8 * 3);
2966 rbuf[5] = last_lba >> (8 * 2);
2967 rbuf[6] = last_lba >> (8 * 1);
2968 rbuf[7] = last_lba;
2969
2970 /* sector size */
2971 rbuf[ 8] = sector_size >> (8 * 3);
2972 rbuf[ 9] = sector_size >> (8 * 2);
2973 rbuf[10] = sector_size >> (8 * 1);
2974 rbuf[11] = sector_size;
2975
2976 if (ata_dev_is_zoned(dev))
2977 rbuf[12] = (1 << 4); /* RC_BASIS */
2978 rbuf[13] = log2_per_phys;
2979 rbuf[14] = (lowest_aligned >> 8) & 0x3f;
2980 rbuf[15] = lowest_aligned;
2981
2982 if (ata_id_has_trim(dev->id) && !(dev->quirks & ATA_QUIRK_NOTRIM)) {
2983 rbuf[14] |= 0x80; /* LBPME */
2984
2985 if (ata_id_has_zero_after_trim(dev->id) &&
2986 dev->quirks & ATA_QUIRK_ZERO_AFTER_TRIM) {
2987 ata_dev_info(dev, "Enabling discard_zeroes_data\n");
2988 rbuf[14] |= 0x40; /* LBPRZ */
2989 }
2990 }
2991
2992 return 16;
2993 }
2994
2995 /**
2996 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2997 * @dev: Target device.
2998 * @cmd: SCSI command of interest.
2999 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
3000 *
3001 * Simulate REPORT LUNS command.
3002 *
3003 * LOCKING:
3004 * spin_lock_irqsave(host lock)
3005 */
ata_scsiop_report_luns(struct ata_device * dev,struct scsi_cmnd * cmd,u8 * rbuf)3006 static unsigned int ata_scsiop_report_luns(struct ata_device *dev,
3007 struct scsi_cmnd *cmd, u8 *rbuf)
3008 {
3009 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
3010
3011 return 16;
3012 }
3013
3014 /*
3015 * ATAPI devices typically report zero for their SCSI version, and sometimes
3016 * deviate from the spec WRT response data format. If SCSI version is
3017 * reported as zero like normal, then we make the following fixups:
3018 * 1) Fake MMC-5 version, to indicate to the Linux scsi midlayer this is a
3019 * modern device.
3020 * 2) Ensure response data format / ATAPI information are always correct.
3021 */
atapi_fixup_inquiry(struct scsi_cmnd * cmd)3022 static void atapi_fixup_inquiry(struct scsi_cmnd *cmd)
3023 {
3024 u8 buf[4];
3025
3026 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
3027 if (buf[2] == 0) {
3028 buf[2] = 0x5;
3029 buf[3] = 0x32;
3030 }
3031 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd), buf, 4);
3032 }
3033
atapi_qc_complete(struct ata_queued_cmd * qc)3034 static void atapi_qc_complete(struct ata_queued_cmd *qc)
3035 {
3036 struct ata_link *link = qc->dev->link;
3037 struct scsi_cmnd *cmd = qc->scsicmd;
3038 unsigned int err_mask = qc->err_mask;
3039
3040 /* handle completion from EH */
3041 if (unlikely(err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID)) {
3042
3043 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID))
3044 ata_gen_passthru_sense(qc);
3045
3046 /* SCSI EH automatically locks door if sdev->locked is
3047 * set. Sometimes door lock request continues to
3048 * fail, for example, when no media is present. This
3049 * creates a loop - SCSI EH issues door lock which
3050 * fails and gets invoked again to acquire sense data
3051 * for the failed command.
3052 *
3053 * If door lock fails, always clear sdev->locked to
3054 * avoid this infinite loop.
3055 *
3056 * This may happen before SCSI scan is complete. Make
3057 * sure qc->dev->sdev isn't NULL before dereferencing.
3058 */
3059 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL && qc->dev->sdev)
3060 qc->dev->sdev->locked = 0;
3061
3062 /*
3063 * Report CHECK CONDITION unless the command was terminated
3064 * with a host byte set. Only set the status byte, so that the
3065 * SCSI midlayer internal byte, which scsi_check_sense() may
3066 * have set from the sense data, is preserved.
3067 */
3068 if (get_host_byte(cmd) == DID_OK)
3069 set_status_byte(cmd, SAM_STAT_CHECK_CONDITION);
3070 ata_scsi_qc_done(qc, false, 0);
3071 goto schedule_deferred;
3072 }
3073
3074 if (cmd->result) {
3075 ata_scsi_qc_done(qc, false, 0);
3076 goto schedule_deferred;
3077 }
3078
3079 /* successful completion path */
3080 if (cmd->cmnd[0] == INQUIRY && (cmd->cmnd[1] & 0x03) == 0)
3081 atapi_fixup_inquiry(cmd);
3082
3083 ata_scsi_qc_done(qc, true, SAM_STAT_GOOD);
3084
3085 schedule_deferred:
3086 ata_scsi_schedule_deferred_qc(link);
3087 }
3088 /**
3089 * atapi_xlat - Initialize PACKET taskfile
3090 * @qc: command structure to be initialized
3091 *
3092 * LOCKING:
3093 * spin_lock_irqsave(host lock)
3094 *
3095 * RETURNS:
3096 * Zero on success, non-zero on failure.
3097 */
atapi_xlat(struct ata_queued_cmd * qc)3098 static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
3099 {
3100 struct scsi_cmnd *scmd = qc->scsicmd;
3101 struct ata_device *dev = qc->dev;
3102 int nodata = (scmd->sc_data_direction == DMA_NONE);
3103 int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
3104 unsigned int nbytes;
3105
3106 memset(qc->cdb, 0, dev->cdb_len);
3107 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
3108
3109 qc->complete_fn = atapi_qc_complete;
3110
3111 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3112 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
3113 qc->tf.flags |= ATA_TFLAG_WRITE;
3114 }
3115
3116 qc->tf.command = ATA_CMD_PACKET;
3117 ata_qc_set_pc_nbytes(qc);
3118
3119 /* check whether ATAPI DMA is safe */
3120 if (!nodata && !using_pio && atapi_check_dma(qc))
3121 using_pio = 1;
3122
3123 /* Some controller variants snoop this value for Packet
3124 * transfers to do state machine and FIFO management. Thus we
3125 * want to set it properly, and for DMA where it is
3126 * effectively meaningless.
3127 */
3128 nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
3129
3130 /* Most ATAPI devices which honor transfer chunk size don't
3131 * behave according to the spec when odd chunk size which
3132 * matches the transfer length is specified. If the number of
3133 * bytes to transfer is 2n+1. According to the spec, what
3134 * should happen is to indicate that 2n+1 is going to be
3135 * transferred and transfer 2n+2 bytes where the last byte is
3136 * padding.
3137 *
3138 * In practice, this doesn't happen. ATAPI devices first
3139 * indicate and transfer 2n bytes and then indicate and
3140 * transfer 2 bytes where the last byte is padding.
3141 *
3142 * This inconsistency confuses several controllers which
3143 * perform PIO using DMA such as Intel AHCIs and sil3124/32.
3144 * These controllers use actual number of transferred bytes to
3145 * update DMA pointer and transfer of 4n+2 bytes make those
3146 * controller push DMA pointer by 4n+4 bytes because SATA data
3147 * FISes are aligned to 4 bytes. This causes data corruption
3148 * and buffer overrun.
3149 *
3150 * Always setting nbytes to even number solves this problem
3151 * because then ATAPI devices don't have to split data at 2n
3152 * boundaries.
3153 */
3154 if (nbytes & 0x1)
3155 nbytes++;
3156
3157 qc->tf.lbam = (nbytes & 0xFF);
3158 qc->tf.lbah = (nbytes >> 8);
3159
3160 if (nodata)
3161 qc->tf.protocol = ATAPI_PROT_NODATA;
3162 else if (using_pio)
3163 qc->tf.protocol = ATAPI_PROT_PIO;
3164 else {
3165 /* DMA data xfer */
3166 qc->tf.protocol = ATAPI_PROT_DMA;
3167 qc->tf.feature |= ATAPI_PKT_DMA;
3168
3169 if ((dev->flags & ATA_DFLAG_DMADIR) &&
3170 (scmd->sc_data_direction != DMA_TO_DEVICE))
3171 /* some SATA bridges need us to indicate data xfer direction */
3172 qc->tf.feature |= ATAPI_DMADIR;
3173 }
3174
3175
3176 /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
3177 as ATAPI tape drives don't get this right otherwise */
3178 return 0;
3179 }
3180
ata_find_dev(struct ata_port * ap,unsigned int devno)3181 static struct ata_device *ata_find_dev(struct ata_port *ap, unsigned int devno)
3182 {
3183 /*
3184 * For the non-PMP case, ata_link_max_devices() returns 1 (SATA case),
3185 * or 2 (IDE master + slave case). However, the former case includes
3186 * libsas hosted devices which are numbered per scsi host, leading
3187 * to devno potentially being larger than 0 but with each struct
3188 * ata_device having its own struct ata_port and struct ata_link.
3189 * To accommodate these, ignore devno and always use device number 0.
3190 */
3191 if (likely(!sata_pmp_attached(ap))) {
3192 int link_max_devices = ata_link_max_devices(&ap->link);
3193
3194 if (link_max_devices == 1)
3195 return &ap->link.device[0];
3196
3197 if (devno < link_max_devices)
3198 return &ap->link.device[devno];
3199
3200 return NULL;
3201 }
3202
3203 /*
3204 * For PMP-attached devices, the device number corresponds to C
3205 * (channel) of SCSI [H:C:I:L], indicating the port pmp link
3206 * for the device.
3207 */
3208 if (devno < ap->nr_pmp_links)
3209 return &ap->pmp_link[devno].device[0];
3210
3211 return NULL;
3212 }
3213
__ata_scsi_find_dev(struct ata_port * ap,const struct scsi_device * scsidev)3214 static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
3215 const struct scsi_device *scsidev)
3216 {
3217 int devno;
3218
3219 /* skip commands not addressed to targets we simulate */
3220 if (!sata_pmp_attached(ap)) {
3221 if (unlikely(scsidev->channel || scsidev->lun))
3222 return NULL;
3223 devno = scsidev->id;
3224 } else {
3225 if (unlikely(scsidev->id || scsidev->lun))
3226 return NULL;
3227 devno = scsidev->channel;
3228 }
3229
3230 return ata_find_dev(ap, devno);
3231 }
3232
3233 /**
3234 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
3235 * @ap: ATA port to which the device is attached
3236 * @scsidev: SCSI device from which we derive the ATA device
3237 *
3238 * Given various information provided in struct scsi_cmnd,
3239 * map that onto an ATA bus, and using that mapping
3240 * determine which ata_device is associated with the
3241 * SCSI command to be sent.
3242 *
3243 * LOCKING:
3244 * spin_lock_irqsave(host lock)
3245 *
3246 * RETURNS:
3247 * Associated ATA device, or %NULL if not found.
3248 */
3249 struct ata_device *
ata_scsi_find_dev(struct ata_port * ap,const struct scsi_device * scsidev)3250 ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
3251 {
3252 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
3253
3254 if (!ata_adapter_is_online(ap))
3255 return NULL;
3256
3257 if (unlikely(!dev || !ata_dev_enabled(dev)))
3258 return NULL;
3259
3260 return dev;
3261 }
3262
3263 /*
3264 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
3265 * @byte1: Byte 1 from pass-thru CDB.
3266 *
3267 * RETURNS:
3268 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
3269 */
3270 static u8
ata_scsi_map_proto(u8 byte1)3271 ata_scsi_map_proto(u8 byte1)
3272 {
3273 switch((byte1 & 0x1e) >> 1) {
3274 case 3: /* Non-data */
3275 return ATA_PROT_NODATA;
3276
3277 case 6: /* DMA */
3278 case 10: /* UDMA Data-in */
3279 case 11: /* UDMA Data-Out */
3280 return ATA_PROT_DMA;
3281
3282 case 4: /* PIO Data-in */
3283 case 5: /* PIO Data-out */
3284 return ATA_PROT_PIO;
3285
3286 case 12: /* FPDMA */
3287 return ATA_PROT_NCQ;
3288
3289 case 0: /* Hard Reset */
3290 case 1: /* SRST */
3291 case 8: /* Device Diagnostic */
3292 case 9: /* Device Reset */
3293 case 7: /* DMA Queued */
3294 case 15: /* Return Response Info */
3295 default: /* Reserved */
3296 break;
3297 }
3298
3299 return ATA_PROT_UNKNOWN;
3300 }
3301
3302 /**
3303 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
3304 * @qc: command structure to be initialized
3305 *
3306 * Handles either 12, 16, or 32-byte versions of the CDB.
3307 *
3308 * RETURNS:
3309 * Zero on success, non-zero on failure.
3310 */
ata_scsi_pass_thru(struct ata_queued_cmd * qc)3311 static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
3312 {
3313 struct ata_taskfile *tf = &(qc->tf);
3314 struct scsi_cmnd *scmd = qc->scsicmd;
3315 struct ata_device *dev = qc->dev;
3316 const u8 *cdb = scmd->cmnd;
3317 u16 fp;
3318 u16 cdb_offset = 0;
3319
3320 /* 7Fh variable length cmd means a ata pass-thru(32) */
3321 if (cdb[0] == VARIABLE_LENGTH_CMD)
3322 cdb_offset = 9;
3323
3324 tf->protocol = ata_scsi_map_proto(cdb[1 + cdb_offset]);
3325 if (tf->protocol == ATA_PROT_UNKNOWN) {
3326 fp = 1;
3327 goto invalid_fld;
3328 }
3329
3330 if ((cdb[2 + cdb_offset] & 0x3) == 0) {
3331 /*
3332 * When T_LENGTH is zero (No data is transferred), dir should
3333 * be DMA_NONE.
3334 */
3335 if (scmd->sc_data_direction != DMA_NONE) {
3336 fp = 2 + cdb_offset;
3337 goto invalid_fld;
3338 }
3339
3340 if (ata_is_ncq(tf->protocol))
3341 tf->protocol = ATA_PROT_NCQ_NODATA;
3342 }
3343
3344 /* enable LBA */
3345 tf->flags |= ATA_TFLAG_LBA;
3346
3347 /*
3348 * 12 and 16 byte CDBs use different offsets to
3349 * provide the various register values.
3350 */
3351 switch (cdb[0]) {
3352 case ATA_16:
3353 /*
3354 * 16-byte CDB - may contain extended commands.
3355 *
3356 * If that is the case, copy the upper byte register values.
3357 */
3358 if (cdb[1] & 0x01) {
3359 tf->hob_feature = cdb[3];
3360 tf->hob_nsect = cdb[5];
3361 tf->hob_lbal = cdb[7];
3362 tf->hob_lbam = cdb[9];
3363 tf->hob_lbah = cdb[11];
3364 tf->flags |= ATA_TFLAG_LBA48;
3365 } else
3366 tf->flags &= ~ATA_TFLAG_LBA48;
3367
3368 /*
3369 * Always copy low byte, device and command registers.
3370 */
3371 tf->feature = cdb[4];
3372 tf->nsect = cdb[6];
3373 tf->lbal = cdb[8];
3374 tf->lbam = cdb[10];
3375 tf->lbah = cdb[12];
3376 tf->device = cdb[13];
3377 tf->command = cdb[14];
3378 break;
3379 case ATA_12:
3380 /*
3381 * 12-byte CDB - incapable of extended commands.
3382 */
3383 tf->flags &= ~ATA_TFLAG_LBA48;
3384
3385 tf->feature = cdb[3];
3386 tf->nsect = cdb[4];
3387 tf->lbal = cdb[5];
3388 tf->lbam = cdb[6];
3389 tf->lbah = cdb[7];
3390 tf->device = cdb[8];
3391 tf->command = cdb[9];
3392 break;
3393 default:
3394 /*
3395 * 32-byte CDB - may contain extended command fields.
3396 *
3397 * If that is the case, copy the upper byte register values.
3398 */
3399 if (cdb[10] & 0x01) {
3400 tf->hob_feature = cdb[20];
3401 tf->hob_nsect = cdb[22];
3402 tf->hob_lbal = cdb[16];
3403 tf->hob_lbam = cdb[15];
3404 tf->hob_lbah = cdb[14];
3405 tf->flags |= ATA_TFLAG_LBA48;
3406 } else
3407 tf->flags &= ~ATA_TFLAG_LBA48;
3408
3409 tf->feature = cdb[21];
3410 tf->nsect = cdb[23];
3411 tf->lbal = cdb[19];
3412 tf->lbam = cdb[18];
3413 tf->lbah = cdb[17];
3414 tf->device = cdb[24];
3415 tf->command = cdb[25];
3416 tf->auxiliary = get_unaligned_be32(&cdb[28]);
3417 break;
3418 }
3419
3420 /* For NCQ commands copy the tag value */
3421 if (ata_is_ncq(tf->protocol))
3422 tf->nsect = qc->hw_tag << 3;
3423
3424 /* enforce correct master/slave bit */
3425 tf->device = dev->devno ?
3426 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
3427
3428 switch (tf->command) {
3429 /* READ/WRITE LONG use a non-standard sect_size */
3430 case ATA_CMD_READ_LONG:
3431 case ATA_CMD_READ_LONG_ONCE:
3432 case ATA_CMD_WRITE_LONG:
3433 case ATA_CMD_WRITE_LONG_ONCE:
3434 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1) {
3435 fp = 1;
3436 goto invalid_fld;
3437 }
3438 qc->sect_size = scsi_bufflen(scmd);
3439 break;
3440
3441 /* commands using reported Logical Block size (e.g. 512 or 4K) */
3442 case ATA_CMD_CFA_WRITE_NE:
3443 case ATA_CMD_CFA_TRANS_SECT:
3444 case ATA_CMD_CFA_WRITE_MULT_NE:
3445 /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */
3446 case ATA_CMD_READ:
3447 case ATA_CMD_READ_EXT:
3448 case ATA_CMD_READ_QUEUED:
3449 /* XXX: case ATA_CMD_READ_QUEUED_EXT: */
3450 case ATA_CMD_FPDMA_READ:
3451 case ATA_CMD_READ_MULTI:
3452 case ATA_CMD_READ_MULTI_EXT:
3453 case ATA_CMD_PIO_READ:
3454 case ATA_CMD_PIO_READ_EXT:
3455 case ATA_CMD_READ_STREAM_DMA_EXT:
3456 case ATA_CMD_READ_STREAM_EXT:
3457 case ATA_CMD_VERIFY:
3458 case ATA_CMD_VERIFY_EXT:
3459 case ATA_CMD_WRITE:
3460 case ATA_CMD_WRITE_EXT:
3461 case ATA_CMD_WRITE_FUA_EXT:
3462 case ATA_CMD_WRITE_QUEUED:
3463 case ATA_CMD_WRITE_QUEUED_FUA_EXT:
3464 case ATA_CMD_FPDMA_WRITE:
3465 case ATA_CMD_WRITE_MULTI:
3466 case ATA_CMD_WRITE_MULTI_EXT:
3467 case ATA_CMD_WRITE_MULTI_FUA_EXT:
3468 case ATA_CMD_PIO_WRITE:
3469 case ATA_CMD_PIO_WRITE_EXT:
3470 case ATA_CMD_WRITE_STREAM_DMA_EXT:
3471 case ATA_CMD_WRITE_STREAM_EXT:
3472 qc->sect_size = scmd->device->sector_size;
3473 break;
3474
3475 /* Everything else uses 512 byte "sectors" */
3476 default:
3477 qc->sect_size = ATA_SECT_SIZE;
3478 }
3479
3480 /*
3481 * Set flags so that all registers will be written, pass on
3482 * write indication (used for PIO/DMA setup), result TF is
3483 * copied back and we don't whine too much about its failure.
3484 */
3485 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
3486 if (scmd->sc_data_direction == DMA_TO_DEVICE)
3487 tf->flags |= ATA_TFLAG_WRITE;
3488
3489 qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
3490
3491 /*
3492 * Set transfer length.
3493 *
3494 * TODO: find out if we need to do more here to
3495 * cover scatter/gather case.
3496 */
3497 ata_qc_set_pc_nbytes(qc);
3498
3499 /* We may not issue DMA commands if no DMA mode is set */
3500 if (tf->protocol == ATA_PROT_DMA && !ata_dma_enabled(dev)) {
3501 fp = 1;
3502 goto invalid_fld;
3503 }
3504
3505 /* We may not issue NCQ commands to devices not supporting NCQ */
3506 if (ata_is_ncq(tf->protocol) && !ata_ncq_enabled(dev)) {
3507 fp = 1;
3508 goto invalid_fld;
3509 }
3510
3511 /* sanity check for pio multi commands */
3512 if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf)) {
3513 fp = 1;
3514 goto invalid_fld;
3515 }
3516
3517 if (is_multi_taskfile(tf)) {
3518 unsigned int multi_count = 1 << (cdb[1] >> 5);
3519
3520 /* compare the passed through multi_count
3521 * with the cached multi_count of libata
3522 */
3523 if (multi_count != dev->multi_count)
3524 ata_dev_warn(dev, "invalid multi_count %u ignored\n",
3525 multi_count);
3526 }
3527
3528 /*
3529 * Filter SET_FEATURES - XFER MODE command -- otherwise,
3530 * SET_FEATURES - XFER MODE must be preceded/succeeded
3531 * by an update to hardware-specific registers for each
3532 * controller (i.e. the reason for ->set_piomode(),
3533 * ->set_dmamode(), and ->post_set_mode() hooks).
3534 */
3535 if (tf->command == ATA_CMD_SET_FEATURES &&
3536 tf->feature == SETFEATURES_XFER) {
3537 fp = (cdb[0] == ATA_16) ? 4 : 3;
3538 goto invalid_fld;
3539 }
3540
3541 /*
3542 * Filter TPM commands by default. These provide an
3543 * essentially uncontrolled encrypted "back door" between
3544 * applications and the disk. Set libata.allow_tpm=1 if you
3545 * have a real reason for wanting to use them. This ensures
3546 * that installed software cannot easily mess stuff up without
3547 * user intent. DVR type users will probably ship with this enabled
3548 * for movie content management.
3549 *
3550 * Note that for ATA8 we can issue a DCS change and DCS freeze lock
3551 * for this and should do in future but that it is not sufficient as
3552 * DCS is an optional feature set. Thus we also do the software filter
3553 * so that we comply with the TC consortium stated goal that the user
3554 * can turn off TC features of their system.
3555 */
3556 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm) {
3557 fp = (cdb[0] == ATA_16) ? 14 : 9;
3558 goto invalid_fld;
3559 }
3560
3561 return 0;
3562
3563 invalid_fld:
3564 ata_scsi_set_invalid_field(dev, scmd, fp, 0xff);
3565 return 1;
3566 }
3567
3568 /**
3569 * ata_format_dsm_trim_descr() - SATL Write Same to DSM Trim
3570 * @cmd: SCSI command being translated
3571 * @size: DSM TRIM payload size in bytes (a multiple of 512)
3572 * @sector: Starting sector
3573 * @count: Total Range of request in logical sectors
3574 *
3575 * Rewrite the WRITE SAME descriptor to be a DSM TRIM little-endian formatted
3576 * descriptor.
3577 *
3578 * The payload is a list of @size / 8 entries of the format:
3579 * 63:48 Range Length
3580 * 47:0 LBA
3581 *
3582 * Range Length of 0 is ignored.
3583 * LBA's should be sorted order and not overlap.
3584 *
3585 * NOTE: this is the same format as ADD LBA(S) TO NV CACHE PINNED SET
3586 *
3587 * The descriptor is written straight into the WRITE SAME data-out buffer;
3588 * ata_dsm_trim_pages() guarantees @size does not exceed that buffer (one
3589 * logical block). An atomic sg_miter mapping is used so this works from the
3590 * command submission path and, unlike page_address(), copes with a high
3591 * memory payload.
3592 *
3593 * Return: Number of bytes written into the data-out buffer.
3594 */
ata_format_dsm_trim_descr(struct scsi_cmnd * cmd,size_t size,u64 sector,u32 count)3595 static size_t ata_format_dsm_trim_descr(struct scsi_cmnd *cmd, size_t size,
3596 u64 sector, u32 count)
3597 {
3598 struct sg_mapping_iter miter;
3599 size_t offset = 0;
3600
3601 sg_miter_start(&miter, scsi_sglist(cmd), scsi_sg_count(cmd),
3602 SG_MITER_TO_SG | SG_MITER_ATOMIC);
3603 while (offset < size && sg_miter_next(&miter)) {
3604 __le64 *buf = miter.addr;
3605 size_t chunk = min_t(size_t, miter.length, size - offset);
3606 unsigned int n = chunk / sizeof(__le64);
3607 unsigned int i;
3608
3609 for (i = 0; i < n; i++) {
3610 u64 entry = 0;
3611
3612 if (count) {
3613 u32 rlen = min_t(u32, count, 0xffff);
3614
3615 entry = sector | ((u64)rlen << 48);
3616 sector += rlen;
3617 count -= rlen;
3618 }
3619 buf[i] = cpu_to_le64(entry);
3620 }
3621 offset += n * sizeof(__le64);
3622 }
3623 sg_miter_stop(&miter);
3624
3625 return offset;
3626 }
3627
3628 /**
3629 * ata_scsi_write_same_xlat() - SATL Write Same to ATA SCT Write Same
3630 * @qc: Command to be translated
3631 *
3632 * Translate a SCSI WRITE SAME command to be either a DSM TRIM command or
3633 * an SCT Write Same command.
3634 * Based on WRITE SAME has the UNMAP flag:
3635 *
3636 * - When set translate to DSM TRIM
3637 * - When clear translate to SCT Write Same
3638 */
ata_scsi_write_same_xlat(struct ata_queued_cmd * qc)3639 static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
3640 {
3641 struct ata_taskfile *tf = &qc->tf;
3642 struct scsi_cmnd *scmd = qc->scsicmd;
3643 struct ata_device *dev = qc->dev;
3644 const u8 *cdb = scmd->cmnd;
3645 unsigned int max_pages = ata_dsm_trim_pages(dev);
3646 unsigned int n_pages;
3647 size_t size;
3648 u64 block;
3649 u32 n_block;
3650 u16 fp;
3651 u8 bp = 0xff;
3652 u8 unmap = cdb[1] & 0x8;
3653
3654 /* we may not issue DMA commands if no DMA mode is set */
3655 if (unlikely(!ata_dma_enabled(dev)))
3656 goto invalid_opcode;
3657
3658 /*
3659 * We only allow sending this command through the block layer,
3660 * as it modifies the DATA OUT buffer, which would corrupt user
3661 * memory for SG_IO commands.
3662 */
3663 if (unlikely(blk_rq_is_passthrough(scsi_cmd_to_rq(scmd))))
3664 goto invalid_opcode;
3665
3666 if (unlikely(scmd->cmd_len < 16)) {
3667 fp = 15;
3668 goto invalid_fld;
3669 }
3670 scsi_16_lba_len(cdb, &block, &n_block);
3671
3672 if (!unmap || (dev->quirks & ATA_QUIRK_NOTRIM) ||
3673 !ata_id_has_trim(dev->id)) {
3674 fp = 1;
3675 bp = 3;
3676 goto invalid_fld;
3677 }
3678 /* If the request is too large the cmd is invalid */
3679 if (n_block > max_pages * ATA_MAX_TRIM_RNUM * (u64)U16_MAX) {
3680 fp = 2;
3681 goto invalid_fld;
3682 }
3683
3684 /*
3685 * WRITE SAME always has a sector sized buffer as payload, this
3686 * should never be a multiple entry S/G list.
3687 */
3688 if (!scsi_sg_count(scmd))
3689 goto invalid_param_len;
3690
3691 /*
3692 * The DATA SET MANAGEMENT TRIM payload is a whole number of 512-byte
3693 * pages (each holding up to ATA_MAX_TRIM_RNUM LBA Range Entries),
3694 * independent of the logical sector size. Only use as many pages as
3695 * are needed to describe the request, capped at max_pages.
3696 */
3697 n_pages = DIV_ROUND_UP(DIV_ROUND_UP(n_block, U16_MAX),
3698 ATA_MAX_TRIM_RNUM);
3699 n_pages = clamp(n_pages, 1U, max_pages);
3700 size = (size_t)n_pages * ATA_SECT_SIZE;
3701
3702 if (ata_format_dsm_trim_descr(scmd, size, block, n_block) != size)
3703 goto invalid_param_len;
3704
3705 /*
3706 * For DATA SET MANAGEMENT TRIM the COUNT field (aka nsect) is the
3707 * number of 512-byte pages to be transferred.
3708 */
3709 if (ata_ncq_enabled(dev) && ata_fpdma_dsm_supported(dev)) {
3710 /* Newer devices support queued TRIM commands */
3711 tf->protocol = ATA_PROT_NCQ;
3712 tf->command = ATA_CMD_FPDMA_SEND;
3713 tf->hob_nsect = ATA_SUBCMD_FPDMA_SEND_DSM & 0x1f;
3714 tf->nsect = qc->hw_tag << 3;
3715 tf->hob_feature = n_pages >> 8;
3716 tf->feature = n_pages;
3717
3718 tf->auxiliary = 1;
3719 } else {
3720 tf->protocol = ATA_PROT_DMA;
3721 tf->hob_feature = 0;
3722 tf->feature = ATA_DSM_TRIM;
3723 tf->hob_nsect = n_pages >> 8;
3724 tf->nsect = n_pages;
3725 tf->command = ATA_CMD_DSM;
3726 }
3727
3728 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
3729 ATA_TFLAG_WRITE;
3730
3731 ata_qc_set_pc_nbytes(qc);
3732 /*
3733 * The DSM TRIM payload (size) may be smaller than the WRITE SAME
3734 * data-out buffer (one logical block); only transfer the pages that
3735 * were actually built so the transfer length matches the COUNT field.
3736 */
3737 qc->nbytes = size;
3738
3739 return 0;
3740
3741 invalid_fld:
3742 ata_scsi_set_invalid_field(dev, scmd, fp, bp);
3743 return 1;
3744 invalid_param_len:
3745 /* "Parameter list length error" */
3746 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
3747 return 1;
3748 invalid_opcode:
3749 /* "Invalid command operation code" */
3750 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0);
3751 return 1;
3752 }
3753
3754 struct ata_scsi_cmd {
3755 u8 op;
3756 u8 cdb_len;
3757 bool sa_valid;
3758 u16 sa;
3759 };
3760
3761 /*
3762 * Array of commands supported with translation or emulation, sorted in
3763 * ascending opcode and service action order. All of these commands are
3764 * processed either in ata_xlat_func() or in ata_scsi_simulate();
3765 *
3766 * Note: commands that are not fully supported may be left out of this array
3767 * so that they are not reported as supported for passthrough but still
3768 * available through the block layer. For now, this includes the following
3769 * commands:
3770 * - WRITE_SAME_16: ata_scsi_write_same_xlat() forbids passthrough commands
3771 */
3772 static const struct ata_scsi_cmd ata_supported_cmds[] = {
3773 { .op = TEST_UNIT_READY, .cdb_len = 6 },
3774 { .op = REZERO_UNIT, .cdb_len = 6 },
3775 { .op = REQUEST_SENSE, .cdb_len = 6 },
3776 { .op = READ_6, .cdb_len = 6 },
3777 { .op = WRITE_6, .cdb_len = 6 },
3778 { .op = SEEK_6, .cdb_len = 6 },
3779 { .op = INQUIRY, .cdb_len = 6 },
3780 { .op = MODE_SELECT, .cdb_len = 6 },
3781 { .op = MODE_SENSE, .cdb_len = 6 },
3782 { .op = START_STOP, .cdb_len = 6 },
3783 { .op = SEND_DIAGNOSTIC, .cdb_len = 6 },
3784 { .op = READ_CAPACITY, .cdb_len = 10 },
3785 { .op = READ_10, .cdb_len = 10 },
3786 { .op = WRITE_10, .cdb_len = 10 },
3787 { .op = SEEK_10, .cdb_len = 10 },
3788 { .op = VERIFY, .cdb_len = 10 },
3789 { .op = SYNCHRONIZE_CACHE, .cdb_len = 10 },
3790 { .op = MODE_SELECT_10, .cdb_len = 10 },
3791 { .op = MODE_SENSE_10, .cdb_len = 10 },
3792 {
3793 .op = VARIABLE_LENGTH_CMD, .cdb_len = 32,
3794 .sa_valid = true,
3795 .sa = ATA_32
3796 },
3797 { .op = ATA_16, .cdb_len = 16 },
3798 { .op = READ_16, .cdb_len = 16 },
3799 { .op = WRITE_16, .cdb_len = 16 },
3800 { .op = VERIFY_16, .cdb_len = 16 },
3801 { .op = SYNCHRONIZE_CACHE_16, .cdb_len = 16 },
3802 {
3803 .op = ZBC_OUT, .cdb_len = 16,
3804 .sa_valid = true,
3805 .sa = ZO_CLOSE_ZONE
3806 },
3807 {
3808 .op = ZBC_OUT, .cdb_len = 16,
3809 .sa_valid = true,
3810 .sa = ZO_FINISH_ZONE
3811 },
3812 {
3813 .op = ZBC_OUT, .cdb_len = 16,
3814 .sa_valid = true,
3815 .sa = ZO_OPEN_ZONE
3816 },
3817 {
3818 .op = ZBC_OUT, .cdb_len = 16,
3819 .sa_valid = true,
3820 .sa = ZO_RESET_WRITE_POINTER
3821 },
3822 {
3823 .op = ZBC_IN, .cdb_len = 16,
3824 .sa_valid = true,
3825 .sa = ZI_REPORT_ZONES
3826 },
3827 {
3828 .op = SERVICE_ACTION_IN_16, .cdb_len = 16,
3829 .sa_valid = true,
3830 .sa = SAI_READ_CAPACITY_16
3831 },
3832 {
3833 .op = SERVICE_ACTION_IN_16, .cdb_len = 16,
3834 .sa_valid = true,
3835 .sa = SAI_GET_PHYSICAL_ELEMENT_STATUS
3836 },
3837 {
3838 .op = SERVICE_ACTION_IN_16, .cdb_len = 16,
3839 .sa_valid = true,
3840 .sa = SAI_REMOVE_ELEMENT_AND_TRUNCATE
3841 },
3842 {
3843 .op = SERVICE_ACTION_IN_16, .cdb_len = 16,
3844 .sa_valid = true,
3845 .sa = SAI_RESTORE_ELEMENTS_AND_REBUILD
3846 },
3847 {
3848 .op = SERVICE_ACTION_IN_16, .cdb_len = 16,
3849 .sa_valid = true,
3850 .sa = SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES
3851 },
3852 { .op = REPORT_LUNS, .cdb_len = 12 },
3853 { .op = ATA_12, .cdb_len = 12 },
3854 { .op = SECURITY_PROTOCOL_IN, .cdb_len = 12 },
3855 {
3856 .op = MAINTENANCE_IN, .cdb_len = 12,
3857 .sa_valid = true,
3858 .sa = MI_REPORT_SUPPORTED_OPERATION_CODES
3859 },
3860 { .op = SECURITY_PROTOCOL_OUT, .cdb_len = 12 },
3861 };
3862
ata_scsi_get_supported_cmd(u8 op,u16 sa)3863 static const struct ata_scsi_cmd *ata_scsi_get_supported_cmd(u8 op, u16 sa)
3864 {
3865 const struct ata_scsi_cmd *cmd;
3866 int i;
3867
3868 for (i = 0; i < ARRAY_SIZE(ata_supported_cmds); i++) {
3869 cmd = &ata_supported_cmds[i];
3870 if (cmd->op == op && cmd->sa == sa)
3871 return cmd;
3872 }
3873
3874 return NULL;
3875 }
3876
ata_scsi_supported_cmd_use_sa(u8 op)3877 static bool ata_scsi_supported_cmd_use_sa(u8 op)
3878 {
3879 const struct ata_scsi_cmd *cmd;
3880 int i;
3881
3882 for (i = 0; i < ARRAY_SIZE(ata_supported_cmds); i++) {
3883 cmd = &ata_supported_cmds[i];
3884 if (cmd->op == op)
3885 return cmd->sa_valid;
3886 }
3887
3888 return false;
3889 }
3890
3891 struct ata_scsi_cmd_support {
3892 u8 cdlp;
3893 u8 rwcdlp;
3894 };
3895
ata_scsi_cmd_is_supported(struct ata_device * dev,u8 op,u16 sa,struct ata_scsi_cmd_support * sup)3896 static bool ata_scsi_cmd_is_supported(struct ata_device *dev, u8 op, u16 sa,
3897 struct ata_scsi_cmd_support *sup)
3898 {
3899 const struct ata_scsi_cmd *cmd;
3900
3901 /* First, see if we support the command. */
3902 cmd = ata_scsi_get_supported_cmd(op, sa);
3903 if (!cmd)
3904 return false;
3905
3906 /* Now refine the support report depending on the device features. */
3907 memset(sup, 0, sizeof(*sup));
3908 switch (op) {
3909 case READ_16:
3910 if (dev->flags & ATA_DFLAG_CDL) {
3911 /*
3912 * CDL read descriptors map to the T2A page, that is,
3913 * rwcdlp = 0x01 and cdlp = 0x01
3914 */
3915 sup->rwcdlp = 0x01;
3916 sup->cdlp = 0x01;
3917 }
3918 break;
3919 case WRITE_16:
3920 if (dev->flags & ATA_DFLAG_CDL) {
3921 /*
3922 * CDL write descriptors map to the T2B page, that is,
3923 * rwcdlp = 0x01 and cdlp = 0x02
3924 */
3925 sup->rwcdlp = 0x01;
3926 sup->cdlp = 0x02;
3927 }
3928 break;
3929 case ZBC_IN:
3930 case ZBC_OUT:
3931 return ata_dev_is_zoned(dev);
3932 case SERVICE_ACTION_IN_16:
3933 switch (sa) {
3934 case SAI_GET_PHYSICAL_ELEMENT_STATUS:
3935 case SAI_REMOVE_ELEMENT_AND_TRUNCATE:
3936 return dev->flags & ATA_DFLAG_DEPOP;
3937 case SAI_RESTORE_ELEMENTS_AND_REBUILD:
3938 return dev->flags & ATA_DFLAG_DEPOP_RESTORE;
3939 case SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES:
3940 return dev->flags & ATA_DFLAG_DEPOP_MODIFY;
3941 default:
3942 return true;
3943 }
3944 break;
3945 case SECURITY_PROTOCOL_IN:
3946 case SECURITY_PROTOCOL_OUT:
3947 return dev->flags & ATA_DFLAG_TRUSTED;
3948 default:
3949 break;
3950 }
3951
3952 return true;
3953 }
3954
3955 static unsigned int
ata_scsi_report_all_supported_opcodes(struct ata_device * dev,u8 * rbuf)3956 ata_scsi_report_all_supported_opcodes(struct ata_device *dev, u8 *rbuf)
3957 {
3958 struct ata_scsi_cmd_support sup;
3959 const struct ata_scsi_cmd *cmd;
3960 unsigned int len = 4;
3961 u8 *buf = &rbuf[len];
3962 int i;
3963
3964 for (i = 0; i < ARRAY_SIZE(ata_supported_cmds); i++) {
3965 if (len > ATA_SCSI_RBUF_SIZE - 8)
3966 break;
3967
3968 cmd = &ata_supported_cmds[i];
3969
3970 /* All command format */
3971 if (!ata_scsi_cmd_is_supported(dev, cmd->op, cmd->sa, &sup))
3972 continue;
3973
3974 buf[0] = cmd->op;
3975 put_unaligned_be16(cmd->sa, &buf[2]);
3976 buf[5] = (sup.rwcdlp << 6) | (sup.cdlp << 2);
3977 if (cmd->sa_valid)
3978 buf[5] |= 0x01;
3979 put_unaligned_be16(cmd->cdb_len, &buf[6]);
3980
3981 /* CTDP == 0 */
3982 len += 8;
3983 buf += 8;
3984 }
3985
3986 put_unaligned_be32(len - 4, &rbuf[0]);
3987
3988 return len;
3989 }
3990
ata_scsi_report_supported_opcodes(struct ata_device * dev,struct scsi_cmnd * cmd,u8 * rbuf)3991 static unsigned int ata_scsi_report_supported_opcodes(struct ata_device *dev,
3992 struct scsi_cmnd *cmd,
3993 u8 *rbuf)
3994 {
3995 struct ata_scsi_cmd_support sup;
3996 u8 *cdb = cmd->cmnd;
3997 u16 sa = 0;
3998
3999 switch (cdb[2]) {
4000 case 0:
4001 /* All command format */
4002 return ata_scsi_report_all_supported_opcodes(dev, rbuf);
4003 case 1:
4004 /* One command format with command support data, ignore sa. */
4005 if (ata_scsi_supported_cmd_use_sa(cdb[3])) {
4006 ata_scsi_set_invalid_field(dev, cmd, 3, 0xff);
4007 return 0;
4008 }
4009 break;
4010 case 2:
4011 /* One command format, must have sa. */
4012 if (!ata_scsi_supported_cmd_use_sa(cdb[3])) {
4013 ata_scsi_set_invalid_field(dev, cmd, 3, 0xff);
4014 return 0;
4015 }
4016 fallthrough;
4017 case 3:
4018 /* One command format */
4019 sa = get_unaligned_be16(&cdb[4]);
4020 break;
4021 default:
4022 ata_dev_warn(dev, "invalid command format %d\n", cdb[2]);
4023 ata_scsi_set_invalid_field(dev, cmd, 2, 0xff);
4024 return 0;
4025 }
4026
4027 /* One command format */
4028 if (ata_scsi_cmd_is_supported(dev, cdb[3], sa, &sup)) {
4029 rbuf[0] = sup.rwcdlp;
4030 rbuf[1] = (sup.cdlp << 3) | 0x03;
4031 } else {
4032 rbuf[1] = 0x01;
4033 }
4034
4035 return 4;
4036 }
4037
4038 /**
4039 * ata_scsiop_maint_in - Simulate a subset of MAINTENANCE_IN
4040 * @dev: Target device.
4041 * @cmd: SCSI command of interest.
4042 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
4043 *
4044 * Yields a subset to satisfy scsi_report_opcode()
4045 *
4046 * LOCKING:
4047 * spin_lock_irqsave(host lock)
4048 */
ata_scsiop_maint_in(struct ata_device * dev,struct scsi_cmnd * cmd,u8 * rbuf)4049 static unsigned int ata_scsiop_maint_in(struct ata_device *dev,
4050 struct scsi_cmnd *cmd, u8 *rbuf)
4051 {
4052 u8 *cdb = cmd->cmnd;
4053 u8 service_action = cdb[1] & 0x1f;
4054
4055 switch (service_action) {
4056 case MI_REPORT_SUPPORTED_OPERATION_CODES:
4057 return ata_scsi_report_supported_opcodes(dev, cmd, rbuf);
4058 default:
4059 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
4060 return 0;
4061 }
4062 }
4063
4064 /**
4065 * ata_scsi_report_zones_complete - convert ATA output
4066 * @qc: command structure returning the data
4067 *
4068 * Convert T-13 little-endian field representation into
4069 * T-10 big-endian field representation.
4070 * What a mess.
4071 */
ata_scsi_report_zones_complete(struct ata_queued_cmd * qc)4072 static void ata_scsi_report_zones_complete(struct ata_queued_cmd *qc)
4073 {
4074 struct scsi_cmnd *scmd = qc->scsicmd;
4075 struct sg_mapping_iter miter;
4076 unsigned int bytes = 0;
4077
4078 lockdep_assert_held(qc->ap->lock);
4079
4080 sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd),
4081 SG_MITER_TO_SG | SG_MITER_ATOMIC);
4082
4083 while (sg_miter_next(&miter)) {
4084 unsigned int offset = 0;
4085
4086 if (bytes == 0) {
4087 char *hdr;
4088 u32 list_length;
4089 u64 max_lba, opt_lba;
4090 u16 same;
4091
4092 /* Swizzle header */
4093 hdr = miter.addr;
4094 list_length = get_unaligned_le32(&hdr[0]);
4095 same = get_unaligned_le16(&hdr[4]);
4096 max_lba = get_unaligned_le64(&hdr[8]);
4097 opt_lba = get_unaligned_le64(&hdr[16]);
4098 put_unaligned_be32(list_length, &hdr[0]);
4099 hdr[4] = same & 0xf;
4100 put_unaligned_be64(max_lba, &hdr[8]);
4101 put_unaligned_be64(opt_lba, &hdr[16]);
4102 offset += 64;
4103 bytes += 64;
4104 }
4105 while (offset < miter.length) {
4106 char *rec;
4107 u8 cond, type, non_seq, reset;
4108 u64 size, start, wp;
4109
4110 /* Swizzle zone descriptor */
4111 rec = miter.addr + offset;
4112 type = rec[0] & 0xf;
4113 cond = (rec[1] >> 4) & 0xf;
4114 non_seq = (rec[1] & 2);
4115 reset = (rec[1] & 1);
4116 size = get_unaligned_le64(&rec[8]);
4117 start = get_unaligned_le64(&rec[16]);
4118 wp = get_unaligned_le64(&rec[24]);
4119 rec[0] = type;
4120 rec[1] = (cond << 4) | non_seq | reset;
4121 put_unaligned_be64(size, &rec[8]);
4122 put_unaligned_be64(start, &rec[16]);
4123 put_unaligned_be64(wp, &rec[24]);
4124 WARN_ON(offset + 64 > miter.length);
4125 offset += 64;
4126 bytes += 64;
4127 }
4128 }
4129 sg_miter_stop(&miter);
4130
4131 ata_scsi_qc_complete(qc);
4132 }
4133
ata_scsi_zbc_in_xlat(struct ata_queued_cmd * qc)4134 static unsigned int ata_scsi_zbc_in_xlat(struct ata_queued_cmd *qc)
4135 {
4136 struct ata_taskfile *tf = &qc->tf;
4137 struct scsi_cmnd *scmd = qc->scsicmd;
4138 const u8 *cdb = scmd->cmnd;
4139 u16 sect, fp = (u16)-1;
4140 u8 sa, options, bp = 0xff;
4141 u64 block;
4142 u32 n_block;
4143
4144 if (unlikely(scmd->cmd_len < 16)) {
4145 ata_dev_warn(qc->dev, "invalid cdb length %d\n",
4146 scmd->cmd_len);
4147 fp = 15;
4148 goto invalid_fld;
4149 }
4150 scsi_16_lba_len(cdb, &block, &n_block);
4151 if (n_block != scsi_bufflen(scmd)) {
4152 ata_dev_warn(qc->dev, "non-matching transfer count (%d/%d)\n",
4153 n_block, scsi_bufflen(scmd));
4154 goto invalid_param_len;
4155 }
4156 sa = cdb[1] & 0x1f;
4157 if (sa != ZI_REPORT_ZONES) {
4158 ata_dev_warn(qc->dev, "invalid service action %d\n", sa);
4159 fp = 1;
4160 goto invalid_fld;
4161 }
4162 /*
4163 * ZAC allows only for transfers in 512 byte blocks,
4164 * and uses a 16 bit value for the transfer count.
4165 */
4166 if ((n_block / 512) > 0xffff || n_block < 512 || (n_block % 512)) {
4167 ata_dev_warn(qc->dev, "invalid transfer count %d\n", n_block);
4168 goto invalid_param_len;
4169 }
4170 sect = n_block / 512;
4171 options = cdb[14] & 0xbf;
4172
4173 if (ata_ncq_enabled(qc->dev) &&
4174 ata_fpdma_zac_mgmt_in_supported(qc->dev)) {
4175 tf->protocol = ATA_PROT_NCQ;
4176 tf->command = ATA_CMD_FPDMA_RECV;
4177 tf->hob_nsect = ATA_SUBCMD_FPDMA_RECV_ZAC_MGMT_IN & 0x1f;
4178 tf->nsect = qc->hw_tag << 3;
4179 tf->feature = sect & 0xff;
4180 tf->hob_feature = (sect >> 8) & 0xff;
4181 tf->auxiliary = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES | (options << 8);
4182 } else {
4183 tf->command = ATA_CMD_ZAC_MGMT_IN;
4184 tf->feature = ATA_SUBCMD_ZAC_MGMT_IN_REPORT_ZONES;
4185 tf->protocol = ATA_PROT_DMA;
4186 tf->hob_feature = options;
4187 tf->hob_nsect = (sect >> 8) & 0xff;
4188 tf->nsect = sect & 0xff;
4189 }
4190 tf->device = ATA_LBA;
4191 tf->lbah = (block >> 16) & 0xff;
4192 tf->lbam = (block >> 8) & 0xff;
4193 tf->lbal = block & 0xff;
4194 tf->hob_lbah = (block >> 40) & 0xff;
4195 tf->hob_lbam = (block >> 32) & 0xff;
4196 tf->hob_lbal = (block >> 24) & 0xff;
4197
4198 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
4199 qc->flags |= ATA_QCFLAG_RESULT_TF;
4200
4201 ata_qc_set_pc_nbytes(qc);
4202
4203 qc->complete_fn = ata_scsi_report_zones_complete;
4204
4205 return 0;
4206
4207 invalid_fld:
4208 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
4209 return 1;
4210
4211 invalid_param_len:
4212 /* "Parameter list length error" */
4213 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
4214 return 1;
4215 }
4216
ata_scsi_zbc_out_xlat(struct ata_queued_cmd * qc)4217 static unsigned int ata_scsi_zbc_out_xlat(struct ata_queued_cmd *qc)
4218 {
4219 struct ata_taskfile *tf = &qc->tf;
4220 struct scsi_cmnd *scmd = qc->scsicmd;
4221 struct ata_device *dev = qc->dev;
4222 const u8 *cdb = scmd->cmnd;
4223 u8 all, sa;
4224 u64 block;
4225 u32 n_block;
4226 u16 fp = (u16)-1;
4227
4228 if (unlikely(scmd->cmd_len < 16)) {
4229 fp = 15;
4230 goto invalid_fld;
4231 }
4232
4233 sa = cdb[1] & 0x1f;
4234 if ((sa != ZO_CLOSE_ZONE) && (sa != ZO_FINISH_ZONE) &&
4235 (sa != ZO_OPEN_ZONE) && (sa != ZO_RESET_WRITE_POINTER)) {
4236 fp = 1;
4237 goto invalid_fld;
4238 }
4239
4240 scsi_16_lba_len(cdb, &block, &n_block);
4241 if (n_block) {
4242 /*
4243 * ZAC MANAGEMENT OUT doesn't define any length
4244 */
4245 goto invalid_param_len;
4246 }
4247
4248 all = cdb[14] & 0x1;
4249 if (all) {
4250 /*
4251 * Ignore the block address (zone ID) as defined by ZBC.
4252 */
4253 block = 0;
4254 } else if (block >= dev->n_sectors) {
4255 /*
4256 * Block must be a valid zone ID (a zone start LBA).
4257 */
4258 fp = 2;
4259 goto invalid_fld;
4260 }
4261
4262 if (ata_ncq_enabled(qc->dev) &&
4263 ata_fpdma_zac_mgmt_out_supported(qc->dev)) {
4264 tf->protocol = ATA_PROT_NCQ_NODATA;
4265 tf->command = ATA_CMD_NCQ_NON_DATA;
4266 tf->feature = ATA_SUBCMD_NCQ_NON_DATA_ZAC_MGMT_OUT;
4267 tf->nsect = qc->hw_tag << 3;
4268 tf->auxiliary = sa | ((u16)all << 8);
4269 } else {
4270 tf->protocol = ATA_PROT_NODATA;
4271 tf->command = ATA_CMD_ZAC_MGMT_OUT;
4272 tf->feature = sa;
4273 tf->hob_feature = all;
4274 }
4275 tf->lbah = (block >> 16) & 0xff;
4276 tf->lbam = (block >> 8) & 0xff;
4277 tf->lbal = block & 0xff;
4278 tf->hob_lbah = (block >> 40) & 0xff;
4279 tf->hob_lbam = (block >> 32) & 0xff;
4280 tf->hob_lbal = (block >> 24) & 0xff;
4281 tf->device = ATA_LBA;
4282 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
4283
4284 return 0;
4285
4286 invalid_fld:
4287 ata_scsi_set_invalid_field(qc->dev, scmd, fp, 0xff);
4288 return 1;
4289 invalid_param_len:
4290 /* "Parameter list length error" */
4291 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
4292 return 1;
4293 }
4294
4295 /**
4296 * ata_mselect_caching - Simulate MODE SELECT for caching info page
4297 * @qc: Storage for translated ATA taskfile
4298 * @buf: input buffer
4299 * @len: number of valid bytes in the input buffer
4300 * @fp: out parameter for the failed field on error
4301 *
4302 * Prepare a taskfile to modify caching information for the device.
4303 *
4304 * LOCKING:
4305 * None.
4306 */
ata_mselect_caching(struct ata_queued_cmd * qc,const u8 * buf,int len,u16 * fp)4307 static int ata_mselect_caching(struct ata_queued_cmd *qc,
4308 const u8 *buf, int len, u16 *fp)
4309 {
4310 struct ata_taskfile *tf = &qc->tf;
4311 struct ata_device *dev = qc->dev;
4312 u8 mpage[CACHE_MPAGE_LEN];
4313 u8 wce;
4314 int i;
4315
4316 /*
4317 * The first two bytes of def_cache_mpage are a header, so offsets
4318 * in mpage are off by 2 compared to buf. Same for len.
4319 */
4320
4321 if (len != CACHE_MPAGE_LEN - 2) {
4322 *fp = min(len, CACHE_MPAGE_LEN - 2);
4323 return -EINVAL;
4324 }
4325
4326 wce = buf[0] & (1 << 2);
4327
4328 /*
4329 * Check that read-only bits are not modified.
4330 */
4331 ata_msense_caching(dev->id, mpage, false);
4332 for (i = 0; i < CACHE_MPAGE_LEN - 2; i++) {
4333 if (i == 0)
4334 continue;
4335 if (mpage[i + 2] != buf[i]) {
4336 *fp = i;
4337 return -EINVAL;
4338 }
4339 }
4340
4341 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
4342 tf->protocol = ATA_PROT_NODATA;
4343 tf->nsect = 0;
4344 tf->command = ATA_CMD_SET_FEATURES;
4345 tf->feature = wce ? SETFEATURES_WC_ON : SETFEATURES_WC_OFF;
4346 return 0;
4347 }
4348
4349 /*
4350 * Simulate MODE SELECT control mode page, sub-page 0.
4351 */
ata_mselect_control_spg0(struct ata_queued_cmd * qc,const u8 * buf,int len,u16 * fp)4352 static int ata_mselect_control_spg0(struct ata_queued_cmd *qc,
4353 const u8 *buf, int len, u16 *fp)
4354 {
4355 struct ata_device *dev = qc->dev;
4356 u8 mpage[CONTROL_MPAGE_LEN];
4357 u8 d_sense;
4358 int i;
4359
4360 /*
4361 * The first two bytes of def_control_mpage are a header, so offsets
4362 * in mpage are off by 2 compared to buf. Same for len.
4363 */
4364
4365 if (len != CONTROL_MPAGE_LEN - 2) {
4366 *fp = min(len, CONTROL_MPAGE_LEN - 2);
4367 return -EINVAL;
4368 }
4369
4370 d_sense = buf[0] & (1 << 2);
4371
4372 /*
4373 * Check that read-only bits are not modified.
4374 */
4375 ata_msense_control_spg0(dev, mpage, false);
4376 for (i = 0; i < CONTROL_MPAGE_LEN - 2; i++) {
4377 if (i == 0)
4378 continue;
4379 if (mpage[2 + i] != buf[i]) {
4380 *fp = i;
4381 return -EINVAL;
4382 }
4383 }
4384 if (d_sense & (1 << 2))
4385 dev->flags |= ATA_DFLAG_D_SENSE;
4386 else
4387 dev->flags &= ~ATA_DFLAG_D_SENSE;
4388 return 0;
4389 }
4390
4391 /*
4392 * Translate MODE SELECT control mode page, sub-page f2h (ATA feature mode
4393 * page) into a SET FEATURES command.
4394 */
ata_mselect_control_ata_feature(struct ata_queued_cmd * qc,const u8 * buf,int len,u16 * fp)4395 static int ata_mselect_control_ata_feature(struct ata_queued_cmd *qc,
4396 const u8 *buf, int len, u16 *fp)
4397 {
4398 struct ata_device *dev = qc->dev;
4399 struct ata_taskfile *tf = &qc->tf;
4400 u8 cdl_action;
4401
4402 /*
4403 * The first four bytes of ATA Feature Control mode page are a header,
4404 * so offsets in mpage are off by 4 compared to buf. Same for len.
4405 */
4406 if (len != ATA_FEATURE_SUB_MPAGE_LEN - 4) {
4407 *fp = min(len, ATA_FEATURE_SUB_MPAGE_LEN - 4);
4408 return -EINVAL;
4409 }
4410
4411 /* Check cdl_ctrl */
4412 switch (buf[0] & 0x03) {
4413 case 0:
4414 /* Disable CDL */
4415 ata_dev_dbg(dev, "Disabling CDL\n");
4416 cdl_action = 0;
4417 dev->flags &= ~ATA_DFLAG_CDL_ENABLED;
4418 break;
4419 case 0x02:
4420 /*
4421 * Enable CDL. Since CDL is mutually exclusive with NCQ
4422 * priority, allow this only if NCQ priority is disabled.
4423 */
4424 if (dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLED) {
4425 ata_dev_err(dev,
4426 "NCQ priority must be disabled to enable CDL\n");
4427 return -EINVAL;
4428 }
4429 ata_dev_dbg(dev, "Enabling CDL\n");
4430 cdl_action = 1;
4431 dev->flags |= ATA_DFLAG_CDL_ENABLED;
4432 break;
4433 default:
4434 *fp = 0;
4435 return -EINVAL;
4436 }
4437
4438 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
4439 tf->protocol = ATA_PROT_NODATA;
4440 tf->command = ATA_CMD_SET_FEATURES;
4441 tf->feature = SETFEATURES_CDL;
4442 tf->nsect = cdl_action;
4443
4444 return 1;
4445 }
4446
4447 /**
4448 * ata_mselect_control - Simulate MODE SELECT for control page
4449 * @qc: Storage for translated ATA taskfile
4450 * @spg: target sub-page of the control page
4451 * @buf: input buffer
4452 * @len: number of valid bytes in the input buffer
4453 * @fp: out parameter for the failed field on error
4454 *
4455 * Prepare a taskfile to modify caching information for the device.
4456 *
4457 * LOCKING:
4458 * None.
4459 */
ata_mselect_control(struct ata_queued_cmd * qc,u8 spg,const u8 * buf,int len,u16 * fp)4460 static int ata_mselect_control(struct ata_queued_cmd *qc, u8 spg,
4461 const u8 *buf, int len, u16 *fp)
4462 {
4463 switch (spg) {
4464 case 0:
4465 return ata_mselect_control_spg0(qc, buf, len, fp);
4466 case ATA_FEATURE_SUB_MPAGE:
4467 return ata_mselect_control_ata_feature(qc, buf, len, fp);
4468 default:
4469 return -EINVAL;
4470 }
4471 }
4472
4473 /**
4474 * ata_scsi_mode_select_xlat - Simulate MODE SELECT 6, 10 commands
4475 * @qc: Storage for translated ATA taskfile
4476 *
4477 * Converts a MODE SELECT command to an ATA SET FEATURES taskfile.
4478 * Assume this is invoked for direct access devices (e.g. disks) only.
4479 * There should be no block descriptor for other device types.
4480 *
4481 * LOCKING:
4482 * spin_lock_irqsave(host lock)
4483 */
ata_scsi_mode_select_xlat(struct ata_queued_cmd * qc)4484 static unsigned int ata_scsi_mode_select_xlat(struct ata_queued_cmd *qc)
4485 {
4486 struct scsi_cmnd *scmd = qc->scsicmd;
4487 const u8 *cdb = scmd->cmnd;
4488 u8 pg, spg;
4489 unsigned six_byte, pg_len, hdr_len, bd_len;
4490 int len, ret;
4491 u16 fp = (u16)-1;
4492 u8 bp = 0xff;
4493 u8 buffer[64];
4494 const u8 *p = buffer;
4495
4496 six_byte = (cdb[0] == MODE_SELECT);
4497 if (six_byte) {
4498 if (scmd->cmd_len < 5) {
4499 fp = 4;
4500 goto invalid_fld;
4501 }
4502
4503 len = cdb[4];
4504 hdr_len = 4;
4505 } else {
4506 if (scmd->cmd_len < 9) {
4507 fp = 8;
4508 goto invalid_fld;
4509 }
4510
4511 len = get_unaligned_be16(&cdb[7]);
4512 hdr_len = 8;
4513 }
4514
4515 /* We only support PF=1, SP=0. */
4516 if ((cdb[1] & 0x11) != 0x10) {
4517 fp = 1;
4518 bp = (cdb[1] & 0x01) ? 1 : 5;
4519 goto invalid_fld;
4520 }
4521
4522 /* Test early for possible overrun. */
4523 if (!scsi_sg_count(scmd) || scsi_sglist(scmd)->length < len)
4524 goto invalid_param_len;
4525
4526 /* Move past header and block descriptors. */
4527 if (len < hdr_len)
4528 goto invalid_param_len;
4529
4530 if (!sg_copy_to_buffer(scsi_sglist(scmd), scsi_sg_count(scmd),
4531 buffer, sizeof(buffer)))
4532 goto invalid_param_len;
4533
4534 if (six_byte)
4535 bd_len = p[3];
4536 else
4537 bd_len = get_unaligned_be16(&p[6]);
4538
4539 len -= hdr_len;
4540 p += hdr_len;
4541 if (len < bd_len)
4542 goto invalid_param_len;
4543 if (bd_len != 0 && bd_len != 8) {
4544 fp = (six_byte) ? 3 : 6;
4545 fp += bd_len + hdr_len;
4546 goto invalid_param;
4547 }
4548
4549 len -= bd_len;
4550 p += bd_len;
4551 if (len == 0)
4552 goto skip;
4553
4554 /* Parse both possible formats for the mode page headers. */
4555 pg = p[0] & 0x3f;
4556 if (p[0] & 0x40) {
4557 if (len < 4)
4558 goto invalid_param_len;
4559
4560 spg = p[1];
4561 pg_len = get_unaligned_be16(&p[2]);
4562 p += 4;
4563 len -= 4;
4564 } else {
4565 if (len < 2)
4566 goto invalid_param_len;
4567
4568 spg = 0;
4569 pg_len = p[1];
4570 p += 2;
4571 len -= 2;
4572 }
4573
4574 /*
4575 * Supported subpages: all subpages and ATA feature sub-page f2h of
4576 * the control page.
4577 */
4578 if (spg) {
4579 switch (spg) {
4580 case ALL_SUB_MPAGES:
4581 /* All subpages is not supported for the control page */
4582 if (pg == CONTROL_MPAGE) {
4583 fp = (p[0] & 0x40) ? 1 : 0;
4584 fp += hdr_len + bd_len;
4585 goto invalid_param;
4586 }
4587 break;
4588 case ATA_FEATURE_SUB_MPAGE:
4589 if (qc->dev->flags & ATA_DFLAG_CDL &&
4590 pg == CONTROL_MPAGE)
4591 break;
4592 fallthrough;
4593 default:
4594 fp = (p[0] & 0x40) ? 1 : 0;
4595 fp += hdr_len + bd_len;
4596 goto invalid_param;
4597 }
4598 }
4599 if (pg_len > len)
4600 goto invalid_param_len;
4601
4602 switch (pg) {
4603 case CACHE_MPAGE:
4604 if (ata_mselect_caching(qc, p, pg_len, &fp) < 0) {
4605 fp += hdr_len + bd_len;
4606 goto invalid_param;
4607 }
4608 break;
4609 case CONTROL_MPAGE:
4610 ret = ata_mselect_control(qc, spg, p, pg_len, &fp);
4611 if (ret < 0) {
4612 fp += hdr_len + bd_len;
4613 goto invalid_param;
4614 }
4615 if (!ret)
4616 goto skip; /* No ATA command to send */
4617 break;
4618 default:
4619 /* Invalid page code */
4620 fp = bd_len + hdr_len;
4621 goto invalid_param;
4622 }
4623
4624 /*
4625 * Only one page has changeable data, so we only support setting one
4626 * page at a time.
4627 */
4628 if (len > pg_len)
4629 goto invalid_param;
4630
4631 return 0;
4632
4633 invalid_fld:
4634 ata_scsi_set_invalid_field(qc->dev, scmd, fp, bp);
4635 return 1;
4636
4637 invalid_param:
4638 ata_scsi_set_invalid_parameter(qc->dev, scmd, fp);
4639 return 1;
4640
4641 invalid_param_len:
4642 /* "Parameter list length error" */
4643 ata_scsi_set_sense(qc->dev, scmd, ILLEGAL_REQUEST, 0x1a, 0x0);
4644 return 1;
4645
4646 skip:
4647 scmd->result = SAM_STAT_GOOD;
4648 return 1;
4649 }
4650
ata_scsi_trusted_op(u32 len,bool send,bool dma)4651 static u8 ata_scsi_trusted_op(u32 len, bool send, bool dma)
4652 {
4653 if (len == 0)
4654 return ATA_CMD_TRUSTED_NONDATA;
4655 else if (send)
4656 return dma ? ATA_CMD_TRUSTED_SND_DMA : ATA_CMD_TRUSTED_SND;
4657 else
4658 return dma ? ATA_CMD_TRUSTED_RCV_DMA : ATA_CMD_TRUSTED_RCV;
4659 }
4660
ata_scsi_security_inout_xlat(struct ata_queued_cmd * qc)4661 static unsigned int ata_scsi_security_inout_xlat(struct ata_queued_cmd *qc)
4662 {
4663 struct scsi_cmnd *scmd = qc->scsicmd;
4664 const u8 *cdb = scmd->cmnd;
4665 struct ata_taskfile *tf = &qc->tf;
4666 u8 secp = cdb[1];
4667 bool send = (cdb[0] == SECURITY_PROTOCOL_OUT);
4668 u16 spsp = get_unaligned_be16(&cdb[2]);
4669 u32 len = get_unaligned_be32(&cdb[6]);
4670 bool dma = !(qc->dev->flags & ATA_DFLAG_PIO);
4671
4672 /*
4673 * We don't support the ATA "security" protocol.
4674 */
4675 if (secp == 0xef) {
4676 ata_scsi_set_invalid_field(qc->dev, scmd, 1, 0);
4677 return 1;
4678 }
4679
4680 if (cdb[4] & 7) { /* INC_512 */
4681 if (len > 0xffff) {
4682 ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0);
4683 return 1;
4684 }
4685 } else {
4686 if (len > 0x01fffe00) {
4687 ata_scsi_set_invalid_field(qc->dev, scmd, 6, 0);
4688 return 1;
4689 }
4690
4691 /* convert to the sector-based ATA addressing */
4692 len = (len + 511) / 512;
4693 }
4694
4695 tf->protocol = dma ? ATA_PROT_DMA : ATA_PROT_PIO;
4696 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR | ATA_TFLAG_LBA;
4697 if (send)
4698 tf->flags |= ATA_TFLAG_WRITE;
4699 tf->command = ata_scsi_trusted_op(len, send, dma);
4700 tf->feature = secp;
4701 tf->lbam = spsp & 0xff;
4702 tf->lbah = spsp >> 8;
4703
4704 if (len) {
4705 tf->nsect = len & 0xff;
4706 tf->lbal = len >> 8;
4707 } else {
4708 if (!send)
4709 tf->lbah = (1 << 7);
4710 }
4711
4712 ata_qc_set_pc_nbytes(qc);
4713 return 0;
4714 }
4715
4716 /*
4717 * Convert T-13 little-endian field representation of GET PHYSICAL ELEMENT
4718 * STATUS DMA command reply into T-10 big-endian field representation.
4719 */
ata_scsi_get_phys_element_status_complete(struct ata_queued_cmd * qc)4720 static void ata_scsi_get_phys_element_status_complete(struct ata_queued_cmd *qc)
4721 {
4722 struct scsi_cmnd *scmd = qc->scsicmd;
4723 struct sg_mapping_iter miter;
4724 unsigned int bytes = 0;
4725
4726 lockdep_assert_held(qc->ap->lock);
4727
4728 sg_miter_start(&miter, scsi_sglist(scmd), scsi_sg_count(scmd),
4729 SG_MITER_TO_SG | SG_MITER_ATOMIC);
4730
4731 while (sg_miter_next(&miter)) {
4732 unsigned int offset = 0;
4733
4734 if (bytes == 0) {
4735 u32 num_desc, num_desc_returned, id;
4736 u16 max_depop, cur_depop;
4737 char *hdr;
4738
4739 /* Swizzle the header */
4740 hdr = miter.addr;
4741 num_desc = get_unaligned_le32(&hdr[0]);
4742 num_desc_returned = get_unaligned_le32(&hdr[4]);
4743 id = get_unaligned_le32(&hdr[8]);
4744 max_depop = get_unaligned_le16(&hdr[12]);
4745 cur_depop = get_unaligned_le16(&hdr[14]);
4746
4747 put_unaligned_be32(num_desc, &hdr[0]);
4748 put_unaligned_be32(num_desc_returned, &hdr[4]);
4749 put_unaligned_be32(id, &hdr[8]);
4750 put_unaligned_be16(max_depop, &hdr[12]);
4751 put_unaligned_be16(cur_depop, &hdr[14]);
4752
4753 offset += 32;
4754 bytes += 32;
4755 }
4756
4757 /* Swizzle the descriptors. */
4758 while (offset < miter.length) {
4759 char *desc;
4760 u32 id;
4761 u8 type;
4762
4763 desc = miter.addr + offset;
4764 id = get_unaligned_le32(&desc[4]);
4765 put_unaligned_be32(id, &desc[4]);
4766
4767 type = desc[14];
4768 if (type == SCSI_PHYS_ELEM_TYPE_ALL_ACCESS_STORAGE) {
4769 u64 capacity = get_unaligned_le64(&desc[16]);
4770
4771 put_unaligned_be64(capacity, &desc[16]);
4772 } else {
4773 u64 num_zones;
4774
4775 id = get_unaligned_le32(&desc[16]);
4776 num_zones = get_unaligned_le64(&desc[24]);
4777
4778 put_unaligned_be32(id, &desc[16]);
4779 put_unaligned_be64(num_zones, &desc[24]);
4780 }
4781
4782 offset += 32;
4783 bytes += 32;
4784 }
4785 }
4786 sg_miter_stop(&miter);
4787
4788 ata_scsi_qc_complete(qc);
4789 }
4790
4791 static unsigned int
ata_scsi_get_phys_element_status_xlat(struct ata_queued_cmd * qc)4792 ata_scsi_get_phys_element_status_xlat(struct ata_queued_cmd *qc)
4793 {
4794 struct scsi_cmnd *scmd = qc->scsicmd;
4795 const u8 *cdb = scmd->cmnd;
4796 struct ata_device *dev = qc->dev;
4797 struct ata_taskfile *tf = &qc->tf;
4798 u32 starting_element, len;
4799
4800 /* ATA_CMD_GET_PHYS_ELEMENT_STATUS is a DMA command. */
4801 if (!(dev->flags & ATA_DFLAG_DEPOP) || !ata_dma_enabled(dev)) {
4802 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0);
4803 return 1;
4804 }
4805
4806 len = get_unaligned_be32(&cdb[10]) / ATA_SECT_SIZE;
4807 if (!len || len > U16_MAX) {
4808 ata_scsi_set_invalid_field(dev, scmd, 10, 0);
4809 return 1;
4810 }
4811
4812 tf->protocol = ATA_PROT_DMA;
4813 tf->command = ATA_CMD_GET_PHYS_ELEMENT_STATUS;
4814 tf->hob_feature = cdb[14];
4815 tf->hob_nsect = (len >> 8) & 0xff;
4816 tf->nsect = len & 0xff;
4817
4818 starting_element = get_unaligned_be32(&cdb[6]);
4819 if (starting_element) {
4820 tf->hob_lbal = (starting_element >> 24) & 0xff;
4821 tf->lbah = (starting_element >> 16) & 0xff;
4822 tf->lbam = (starting_element >> 8) & 0xff;
4823 tf->lbal = starting_element & 0xff;
4824 }
4825 tf->device = ATA_LBA;
4826 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
4827
4828 ata_qc_set_pc_nbytes(qc);
4829
4830 qc->flags |= ATA_QCFLAG_RESULT_TF;
4831 qc->complete_fn = ata_scsi_get_phys_element_status_complete;
4832
4833 return 0;
4834 }
4835
4836 static unsigned int
ata_scsi_remove_element_and_truncate_xlat(struct ata_queued_cmd * qc)4837 ata_scsi_remove_element_and_truncate_xlat(struct ata_queued_cmd *qc)
4838 {
4839 struct scsi_cmnd *scmd = qc->scsicmd;
4840 const u8 *cdb = scmd->cmnd;
4841 struct ata_device *dev = qc->dev;
4842 struct ata_taskfile *tf = &qc->tf;
4843 u64 req_capacity;
4844 u32 id;
4845
4846 if (!(dev->flags & ATA_DFLAG_DEPOP)) {
4847 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0);
4848 return 1;
4849 }
4850
4851 req_capacity = get_unaligned_be64(&cdb[2]);
4852 if (req_capacity == 1) {
4853 ata_scsi_set_invalid_field(dev, scmd, 2, 0);
4854 return 1;
4855 }
4856
4857 id = get_unaligned_be32(&cdb[10]);
4858
4859 tf->protocol = ATA_PROT_NODATA;
4860 tf->command = ATA_CMD_REMOVE_ELEMENT_AND_TRUNCATE;
4861 tf->hob_feature = (id >> 24) & 0xff;
4862 tf->feature = (id >> 16) & 0xff;
4863 tf->hob_nsect = (id >> 8) & 0xff;
4864 tf->nsect = id & 0xff;
4865 tf->hob_lbah = (req_capacity >> 40) & 0xff;
4866 tf->hob_lbam = (req_capacity >> 32) & 0xff;
4867 tf->hob_lbal = (req_capacity >> 24) & 0xff;
4868 tf->lbah = (req_capacity >> 16) & 0xff;
4869 tf->lbam = (req_capacity >> 8) & 0xff;
4870 tf->lbal = req_capacity & 0xff;
4871 tf->device = ATA_LBA;
4872 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
4873
4874 qc->flags |= ATA_QCFLAG_RESULT_TF;
4875
4876 return 0;
4877 }
4878
4879 static unsigned int
ata_scsi_remove_element_and_modify_zones_xlat(struct ata_queued_cmd * qc)4880 ata_scsi_remove_element_and_modify_zones_xlat(struct ata_queued_cmd *qc)
4881 {
4882 struct scsi_cmnd *scmd = qc->scsicmd;
4883 const u8 *cdb = scmd->cmnd;
4884 struct ata_device *dev = qc->dev;
4885 struct ata_taskfile *tf = &qc->tf;
4886 u32 id;
4887
4888 if (!(dev->flags & ATA_DFLAG_DEPOP_MODIFY)) {
4889 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0);
4890 return 1;
4891 }
4892
4893 id = get_unaligned_be32(&cdb[10]);
4894
4895 tf->protocol = ATA_PROT_NODATA;
4896 tf->command = ATA_CMD_REMOVE_ELEMENT_AND_MODIFY_ZONES;
4897 tf->hob_feature = (id >> 24) & 0xff;
4898 tf->feature = (id >> 16) & 0xff;
4899 tf->hob_nsect = (id >> 8) & 0xff;
4900 tf->nsect = id & 0xff;
4901 tf->device = ATA_LBA;
4902 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
4903
4904 qc->flags |= ATA_QCFLAG_RESULT_TF;
4905
4906 return 0;
4907 }
4908
4909 static unsigned int
ata_scsi_restore_elements_and_rebuild_xlat(struct ata_queued_cmd * qc)4910 ata_scsi_restore_elements_and_rebuild_xlat(struct ata_queued_cmd *qc)
4911 {
4912 struct scsi_cmnd *scmd = qc->scsicmd;
4913 struct ata_device *dev = qc->dev;
4914 struct ata_taskfile *tf = &qc->tf;
4915
4916 if (!(dev->flags & ATA_DFLAG_DEPOP_RESTORE)) {
4917 ata_scsi_set_sense(dev, scmd, ILLEGAL_REQUEST, 0x20, 0x0);
4918 return 1;
4919 }
4920
4921 tf->protocol = ATA_PROT_NODATA;
4922 tf->command = ATA_CMD_RESTORE_ELEMENTS_AND_REBUILD;
4923 tf->device = ATA_LBA;
4924 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48;
4925
4926 qc->flags |= ATA_QCFLAG_RESULT_TF;
4927
4928 return 0;
4929 }
4930
4931 /**
4932 * ata_scsi_var_len_cdb_xlat - SATL variable length CDB to Handler
4933 * @qc: Command to be translated
4934 *
4935 * Translate a SCSI variable length CDB to specified commands.
4936 * It checks a service action value in CDB to call corresponding handler.
4937 *
4938 * RETURNS:
4939 * Zero on success, non-zero on failure
4940 *
4941 */
ata_scsi_var_len_cdb_xlat(struct ata_queued_cmd * qc)4942 static unsigned int ata_scsi_var_len_cdb_xlat(struct ata_queued_cmd *qc)
4943 {
4944 struct scsi_cmnd *scmd = qc->scsicmd;
4945 const u8 *cdb = scmd->cmnd;
4946 const u16 sa = get_unaligned_be16(&cdb[8]);
4947
4948 /*
4949 * if service action represents a ata pass-thru(32) command,
4950 * then pass it to ata_scsi_pass_thru handler.
4951 */
4952 if (sa == ATA_32)
4953 return ata_scsi_pass_thru(qc);
4954
4955 /* unsupported service action */
4956 return 1;
4957 }
4958
4959 /**
4960 * ata_get_xlat_func - check if SCSI to ATA translation is possible
4961 * @dev: ATA device
4962 * @cdb: CDB of the SCSI command to consider
4963 *
4964 * Look up the SCSI command given, and determine whether the
4965 * SCSI command is to be translated or simulated.
4966 *
4967 * RETURNS:
4968 * Pointer to translation function if possible, %NULL if not.
4969 */
4970
ata_get_xlat_func(struct ata_device * dev,u8 * cdb)4971 static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev,
4972 u8 *cdb)
4973 {
4974 u8 sa;
4975
4976 switch (cdb[0]) {
4977 case READ_6:
4978 case READ_10:
4979 case READ_16:
4980
4981 case WRITE_6:
4982 case WRITE_10:
4983 case WRITE_16:
4984 return ata_scsi_rw_xlat;
4985
4986 case WRITE_SAME_16:
4987 return ata_scsi_write_same_xlat;
4988
4989 case SYNCHRONIZE_CACHE:
4990 case SYNCHRONIZE_CACHE_16:
4991 if (ata_try_flush_cache(dev))
4992 return ata_scsi_flush_xlat;
4993 break;
4994
4995 case VERIFY:
4996 case VERIFY_16:
4997 return ata_scsi_verify_xlat;
4998
4999 case ATA_12:
5000 case ATA_16:
5001 return ata_scsi_pass_thru;
5002
5003 case VARIABLE_LENGTH_CMD:
5004 return ata_scsi_var_len_cdb_xlat;
5005
5006 case MODE_SELECT:
5007 case MODE_SELECT_10:
5008 return ata_scsi_mode_select_xlat;
5009
5010 case SERVICE_ACTION_IN_16:
5011 sa = cdb[1] & 0x1f;
5012 if (sa == SAI_GET_PHYSICAL_ELEMENT_STATUS)
5013 return ata_scsi_get_phys_element_status_xlat;
5014 if (sa == SAI_REMOVE_ELEMENT_AND_TRUNCATE)
5015 return ata_scsi_remove_element_and_truncate_xlat;
5016 if (sa == SAI_REMOVE_ELEMENT_AND_MODIFY_ZONES)
5017 return ata_scsi_remove_element_and_modify_zones_xlat;
5018 if (sa == SAI_RESTORE_ELEMENTS_AND_REBUILD)
5019 return ata_scsi_restore_elements_and_rebuild_xlat;
5020 break;
5021
5022 case ZBC_IN:
5023 return ata_scsi_zbc_in_xlat;
5024
5025 case ZBC_OUT:
5026 return ata_scsi_zbc_out_xlat;
5027
5028 case SECURITY_PROTOCOL_IN:
5029 case SECURITY_PROTOCOL_OUT:
5030 if (!(dev->flags & ATA_DFLAG_TRUSTED))
5031 break;
5032 return ata_scsi_security_inout_xlat;
5033
5034 case START_STOP:
5035 return ata_scsi_start_stop_xlat;
5036 }
5037
5038 return NULL;
5039 }
5040
5041 /**
5042 * ata_scsi_simulate - simulate SCSI command on ATA device
5043 * @dev: the target device
5044 * @cmd: SCSI command being sent to device.
5045 *
5046 * Interprets and directly executes a select list of SCSI commands
5047 * that can be handled internally.
5048 *
5049 * LOCKING:
5050 * spin_lock_irqsave(host lock)
5051 */
ata_scsi_simulate(struct ata_device * dev,struct scsi_cmnd * cmd)5052 static void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd)
5053 {
5054 const u8 *scsicmd = cmd->cmnd;
5055 u8 tmp8;
5056
5057 switch (scsicmd[0]) {
5058 case INQUIRY:
5059 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_inquiry);
5060 break;
5061
5062 case MODE_SENSE:
5063 case MODE_SENSE_10:
5064 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_mode_sense);
5065 break;
5066
5067 case READ_CAPACITY:
5068 case SERVICE_ACTION_IN_16:
5069 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_read_cap);
5070 break;
5071
5072 case REPORT_LUNS:
5073 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_report_luns);
5074 break;
5075
5076 case REQUEST_SENSE:
5077 ata_scsi_set_sense(dev, cmd, 0, 0, 0);
5078 break;
5079
5080 /* if we reach this, then writeback caching is disabled,
5081 * turning this into a no-op.
5082 */
5083 case SYNCHRONIZE_CACHE:
5084 case SYNCHRONIZE_CACHE_16:
5085 fallthrough;
5086
5087 /* no-op's, complete with success */
5088 case REZERO_UNIT:
5089 case SEEK_6:
5090 case SEEK_10:
5091 case TEST_UNIT_READY:
5092 break;
5093
5094 case SEND_DIAGNOSTIC:
5095 tmp8 = scsicmd[1] & ~(1 << 3);
5096 if (tmp8 != 0x4 || scsicmd[3] || scsicmd[4])
5097 ata_scsi_set_invalid_field(dev, cmd, 1, 0xff);
5098 break;
5099
5100 case MAINTENANCE_IN:
5101 ata_scsi_rbuf_fill(dev, cmd, ata_scsiop_maint_in);
5102 break;
5103
5104 /* all other commands */
5105 default:
5106 ata_scsi_set_sense(dev, cmd, ILLEGAL_REQUEST, 0x20, 0x0);
5107 /* "Invalid command operation code" */
5108 break;
5109 }
5110
5111 scsi_done(cmd);
5112 }
5113
__ata_scsi_queuecmd(struct scsi_cmnd * scmd,struct ata_device * dev,struct ata_port * ap)5114 enum scsi_qc_status __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
5115 struct ata_device *dev,
5116 struct ata_port *ap)
5117 __must_hold(ap->lock)
5118 {
5119 u8 *cdb = scmd->cmnd;
5120 u8 scsi_op = cdb[0];
5121 ata_xlat_func_t xlat_func;
5122
5123 /*
5124 * scsi_queue_rq() will defer commands if scsi_host_in_recovery().
5125 * However, this check is done without holding the ap->lock (a libata
5126 * specific lock), so we can have received an error irq since then,
5127 * therefore we must check if EH is pending or running, while holding
5128 * ap->lock.
5129 */
5130 if (ata_port_eh_scheduled(ap))
5131 return SCSI_MLQUEUE_DEVICE_BUSY;
5132
5133 if (unlikely(!scmd->cmd_len))
5134 goto bad_cdb_len;
5135
5136 if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
5137 if (unlikely(scmd->cmd_len > dev->cdb_len))
5138 goto bad_cdb_len;
5139
5140 xlat_func = ata_get_xlat_func(dev, cdb);
5141 } else if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
5142 /* relay SCSI command to ATAPI device */
5143 int len = COMMAND_SIZE(scsi_op);
5144
5145 if (unlikely(len > scmd->cmd_len ||
5146 len > dev->cdb_len ||
5147 scmd->cmd_len > ATAPI_CDB_LEN))
5148 goto bad_cdb_len;
5149
5150 xlat_func = atapi_xlat;
5151 } else {
5152 /* ATA_16 passthru, treat as an ATA command */
5153 if (unlikely(scmd->cmd_len > 16))
5154 goto bad_cdb_len;
5155
5156 xlat_func = ata_get_xlat_func(dev, cdb);
5157 }
5158
5159 if (xlat_func)
5160 return ata_scsi_translate(dev, scmd, xlat_func, ap);
5161
5162 ata_scsi_simulate(dev, scmd);
5163
5164 return 0;
5165
5166 bad_cdb_len:
5167 scmd->result = DID_ERROR << 16;
5168 scsi_done(scmd);
5169 return 0;
5170 }
5171
5172 /**
5173 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
5174 * @shost: SCSI host of command to be sent
5175 * @cmd: SCSI command to be sent
5176 *
5177 * In some cases, this function translates SCSI commands into
5178 * ATA taskfiles, and queues the taskfiles to be sent to
5179 * hardware. In other cases, this function simulates a
5180 * SCSI device by evaluating and responding to certain
5181 * SCSI commands. This creates the overall effect of
5182 * ATA and ATAPI devices appearing as SCSI devices.
5183 *
5184 * LOCKING:
5185 * ATA host lock
5186 *
5187 * RETURNS:
5188 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
5189 * 0 otherwise.
5190 */
ata_scsi_queuecmd(struct Scsi_Host * shost,struct scsi_cmnd * cmd)5191 enum scsi_qc_status ata_scsi_queuecmd(struct Scsi_Host *shost,
5192 struct scsi_cmnd *cmd)
5193 {
5194 struct ata_port *ap;
5195 struct ata_device *dev;
5196 struct scsi_device *scsidev = cmd->device;
5197 enum scsi_qc_status rc = 0;
5198 unsigned long irq_flags;
5199
5200 ap = ata_shost_to_port(shost);
5201
5202 spin_lock_irqsave(ap->lock, irq_flags);
5203
5204 dev = ata_scsi_find_dev(ap, scsidev);
5205 if (likely(dev))
5206 rc = __ata_scsi_queuecmd(cmd, dev, ap);
5207 else {
5208 cmd->result = (DID_BAD_TARGET << 16);
5209 scsi_done(cmd);
5210 }
5211
5212 spin_unlock_irqrestore(ap->lock, irq_flags);
5213
5214 return rc;
5215 }
5216 EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
5217
ata_scsi_add_hosts(struct ata_host * host,const struct scsi_host_template * sht)5218 int ata_scsi_add_hosts(struct ata_host *host, const struct scsi_host_template *sht)
5219 {
5220 int i, rc;
5221
5222 for (i = 0; i < host->n_ports; i++) {
5223 struct ata_port *ap = host->ports[i];
5224 struct Scsi_Host *shost;
5225
5226 rc = -ENOMEM;
5227 shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
5228 if (!shost)
5229 goto err_alloc;
5230
5231 shost->eh_noresume = 1;
5232 *(struct ata_port **)&shost->hostdata[0] = ap;
5233 ap->scsi_host = shost;
5234
5235 shost->transportt = &ata_scsi_transportt;
5236 shost->unique_id = ap->print_id;
5237 shost->max_id = 16;
5238 shost->max_lun = 1;
5239 shost->max_channel = 1;
5240 shost->max_cmd_len = 32;
5241
5242 /* Schedule policy is determined by ->qc_defer()
5243 * callback and it needs to see every deferred qc.
5244 * Set host_blocked to 1 to prevent SCSI midlayer from
5245 * automatically deferring requests.
5246 */
5247 shost->max_host_blocked = 1;
5248
5249 rc = scsi_add_host_with_dma(shost, &ap->tdev, ap->host->dev);
5250 if (rc)
5251 goto err_alloc;
5252 }
5253
5254 return 0;
5255
5256 err_alloc:
5257 while (--i >= 0) {
5258 struct Scsi_Host *shost = host->ports[i]->scsi_host;
5259
5260 /* scsi_host_put() is in ata_devres_release() */
5261 scsi_remove_host(shost);
5262 }
5263 return rc;
5264 }
5265
5266 #ifdef CONFIG_OF
ata_scsi_assign_ofnode(struct ata_device * dev,struct ata_port * ap)5267 static void ata_scsi_assign_ofnode(struct ata_device *dev, struct ata_port *ap)
5268 {
5269 struct scsi_device *sdev = dev->sdev;
5270 struct device *d = ap->host->dev;
5271 struct device_node *np = d->of_node;
5272 struct device_node *child;
5273
5274 for_each_available_child_of_node(np, child) {
5275 int ret;
5276 u32 val;
5277
5278 ret = of_property_read_u32(child, "reg", &val);
5279 if (ret)
5280 continue;
5281 if (val == dev->devno) {
5282 dev_dbg(d, "found matching device node\n");
5283 sdev->sdev_gendev.of_node = child;
5284 return;
5285 }
5286 }
5287 }
5288 #else
ata_scsi_assign_ofnode(struct ata_device * dev,struct ata_port * ap)5289 static void ata_scsi_assign_ofnode(struct ata_device *dev, struct ata_port *ap)
5290 {
5291 }
5292 #endif
5293
ata_scsi_scan_host(struct ata_port * ap,int sync)5294 void ata_scsi_scan_host(struct ata_port *ap, int sync)
5295 {
5296 int tries = 5;
5297 struct ata_device *last_failed_dev = NULL;
5298 struct ata_link *link;
5299 struct ata_device *dev;
5300
5301 repeat:
5302 ata_for_each_link(link, ap, EDGE) {
5303 ata_for_each_dev(dev, link, ENABLED) {
5304 struct scsi_device *sdev;
5305 int channel = 0, id = 0;
5306
5307 if (dev->sdev)
5308 continue;
5309
5310 if (ata_is_host_link(link))
5311 id = dev->devno;
5312 else
5313 channel = link->pmp;
5314
5315 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
5316 NULL);
5317 if (!IS_ERR(sdev)) {
5318 dev->sdev = sdev;
5319 ata_scsi_assign_ofnode(dev, ap);
5320 scsi_device_put(sdev);
5321 } else {
5322 dev->sdev = NULL;
5323 }
5324 }
5325 }
5326
5327 /* If we scanned while EH was in progress or allocation
5328 * failure occurred, scan would have failed silently. Check
5329 * whether all devices are attached.
5330 */
5331 ata_for_each_link(link, ap, EDGE) {
5332 ata_for_each_dev(dev, link, ENABLED) {
5333 if (!dev->sdev)
5334 goto exit_loop;
5335 }
5336 }
5337 exit_loop:
5338 if (!link)
5339 return;
5340
5341 /* we're missing some SCSI devices */
5342 if (sync) {
5343 /* If caller requested synchrnous scan && we've made
5344 * any progress, sleep briefly and repeat.
5345 */
5346 if (dev != last_failed_dev) {
5347 msleep(100);
5348 last_failed_dev = dev;
5349 goto repeat;
5350 }
5351
5352 /* We might be failing to detect boot device, give it
5353 * a few more chances.
5354 */
5355 if (--tries) {
5356 msleep(100);
5357 goto repeat;
5358 }
5359
5360 ata_port_err(ap,
5361 "WARNING: synchronous SCSI scan failed without making any progress, switching to async\n");
5362 }
5363
5364 queue_delayed_work(system_dfl_long_wq, &ap->hotplug_task,
5365 round_jiffies_relative(HZ));
5366 }
5367
5368 /**
5369 * ata_scsi_offline_dev - offline attached SCSI device
5370 * @dev: ATA device to offline attached SCSI device for
5371 *
5372 * This function is called from ata_eh_detach_dev() and is responsible for
5373 * taking the SCSI device attached to @dev offline. This function is
5374 * called with host lock which protects dev->sdev against clearing.
5375 *
5376 * LOCKING:
5377 * spin_lock_irqsave(host lock)
5378 *
5379 * RETURNS:
5380 * true if attached SCSI device exists, false otherwise.
5381 */
ata_scsi_offline_dev(struct ata_device * dev)5382 bool ata_scsi_offline_dev(struct ata_device *dev)
5383 {
5384 if (dev->sdev) {
5385 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
5386 return true;
5387 }
5388 return false;
5389 }
5390
5391 /**
5392 * ata_scsi_remove_dev - remove attached SCSI device
5393 * @dev: ATA device to remove attached SCSI device for
5394 *
5395 * This function is called from ata_eh_scsi_hotplug() and
5396 * responsible for removing the SCSI device attached to @dev.
5397 *
5398 * LOCKING:
5399 * Kernel thread context (may sleep).
5400 */
ata_scsi_remove_dev(struct ata_device * dev)5401 static void ata_scsi_remove_dev(struct ata_device *dev)
5402 {
5403 struct ata_port *ap = dev->link->ap;
5404 struct scsi_device *sdev;
5405 unsigned long flags;
5406
5407 /* Alas, we need to grab scan_mutex to ensure SCSI device
5408 * state doesn't change underneath us and thus
5409 * scsi_device_get() always succeeds. The mutex locking can
5410 * be removed if there is __scsi_device_get() interface which
5411 * increments reference counts regardless of device state.
5412 */
5413 mutex_lock(&ap->scsi_host->scan_mutex);
5414 spin_lock_irqsave(ap->lock, flags);
5415
5416 /* clearing dev->sdev is protected by host lock */
5417 sdev = dev->sdev;
5418 dev->sdev = NULL;
5419
5420 if (sdev) {
5421 /* If user initiated unplug races with us, sdev can go
5422 * away underneath us after the host lock and
5423 * scan_mutex are released. Hold onto it.
5424 */
5425 if (scsi_device_get(sdev) == 0) {
5426 /* The following ensures the attached sdev is
5427 * offline on return from ata_scsi_offline_dev()
5428 * regardless it wins or loses the race
5429 * against this function.
5430 */
5431 scsi_device_set_state(sdev, SDEV_OFFLINE);
5432 } else {
5433 WARN_ON(1);
5434 sdev = NULL;
5435 }
5436 }
5437
5438 spin_unlock_irqrestore(ap->lock, flags);
5439 mutex_unlock(&ap->scsi_host->scan_mutex);
5440
5441 if (sdev) {
5442 ata_dev_info(dev, "detaching (SCSI %s)\n",
5443 dev_name(&sdev->sdev_gendev));
5444
5445 scsi_remove_device(sdev);
5446 scsi_device_put(sdev);
5447 }
5448 }
5449
ata_scsi_handle_link_detach(struct ata_link * link)5450 static void ata_scsi_handle_link_detach(struct ata_link *link)
5451 {
5452 struct ata_port *ap = link->ap;
5453 struct ata_device *dev;
5454
5455 ata_for_each_dev(dev, link, ALL) {
5456 unsigned long flags;
5457
5458 spin_lock_irqsave(ap->lock, flags);
5459 if (!(dev->flags & ATA_DFLAG_DETACHED)) {
5460 spin_unlock_irqrestore(ap->lock, flags);
5461 continue;
5462 }
5463
5464 dev->flags &= ~ATA_DFLAG_DETACHED;
5465 spin_unlock_irqrestore(ap->lock, flags);
5466
5467 ata_scsi_remove_dev(dev);
5468 }
5469 }
5470
5471 /**
5472 * ata_scsi_media_change_notify - send media change event
5473 * @dev: Pointer to the disk device with media change event
5474 *
5475 * Tell the block layer to send a media change notification
5476 * event.
5477 *
5478 * LOCKING:
5479 * spin_lock_irqsave(host lock)
5480 */
ata_scsi_media_change_notify(struct ata_device * dev)5481 void ata_scsi_media_change_notify(struct ata_device *dev)
5482 {
5483 if (dev->sdev)
5484 sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
5485 GFP_ATOMIC);
5486 }
5487
5488 /**
5489 * ata_scsi_hotplug - SCSI part of hotplug
5490 * @work: Pointer to ATA port to perform SCSI hotplug on
5491 *
5492 * Perform SCSI part of hotplug. It's executed from a separate
5493 * workqueue after EH completes. This is necessary because SCSI
5494 * hot plugging requires working EH and hot unplugging is
5495 * synchronized with hot plugging with a mutex.
5496 *
5497 * LOCKING:
5498 * Kernel thread context (may sleep).
5499 */
ata_scsi_hotplug(struct work_struct * work)5500 void ata_scsi_hotplug(struct work_struct *work)
5501 {
5502 struct ata_port *ap =
5503 container_of(work, struct ata_port, hotplug_task.work);
5504 int i;
5505
5506 if (ap->pflags & ATA_PFLAG_UNLOADING)
5507 return;
5508
5509 mutex_lock(&ap->scsi_scan_mutex);
5510
5511 /* Unplug detached devices. We cannot use link iterator here
5512 * because PMP links have to be scanned even if PMP is
5513 * currently not attached. Iterate manually.
5514 */
5515 ata_scsi_handle_link_detach(&ap->link);
5516 if (ap->pmp_link)
5517 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
5518 ata_scsi_handle_link_detach(&ap->pmp_link[i]);
5519
5520 /* scan for new ones */
5521 ata_scsi_scan_host(ap, 0);
5522
5523 mutex_unlock(&ap->scsi_scan_mutex);
5524 }
5525
5526 /**
5527 * ata_scsi_user_scan - indication for user-initiated bus scan
5528 * @shost: SCSI host to scan
5529 * @channel: Channel to scan
5530 * @id: ID to scan
5531 * @lun: LUN to scan
5532 *
5533 * This function is called when user explicitly requests bus
5534 * scan. Set probe pending flag and invoke EH.
5535 *
5536 * LOCKING:
5537 * SCSI layer (we don't care)
5538 *
5539 * RETURNS:
5540 * Zero.
5541 */
ata_scsi_user_scan(struct Scsi_Host * shost,unsigned int channel,unsigned int id,u64 lun)5542 int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
5543 unsigned int id, u64 lun)
5544 {
5545 struct ata_port *ap = ata_shost_to_port(shost);
5546 unsigned long flags;
5547 int devno, rc = 0;
5548
5549 if (lun != SCAN_WILD_CARD && lun)
5550 return -EINVAL;
5551
5552 if (!sata_pmp_attached(ap)) {
5553 if (channel != SCAN_WILD_CARD && channel)
5554 return -EINVAL;
5555 devno = id;
5556 } else {
5557 if (id != SCAN_WILD_CARD && id)
5558 return -EINVAL;
5559 devno = channel;
5560 }
5561
5562 spin_lock_irqsave(ap->lock, flags);
5563
5564 if (devno == SCAN_WILD_CARD) {
5565 struct ata_link *link;
5566
5567 ata_for_each_link(link, ap, EDGE) {
5568 struct ata_eh_info *ehi = &link->eh_info;
5569 ehi->probe_mask |= ATA_ALL_DEVICES;
5570 ehi->action |= ATA_EH_RESET;
5571 }
5572 } else {
5573 struct ata_device *dev = ata_find_dev(ap, devno);
5574
5575 if (dev) {
5576 struct ata_eh_info *ehi = &dev->link->eh_info;
5577 ehi->probe_mask |= 1 << dev->devno;
5578 ehi->action |= ATA_EH_RESET;
5579 } else
5580 rc = -EINVAL;
5581 }
5582
5583 if (rc == 0) {
5584 ata_port_schedule_eh(ap);
5585 spin_unlock_irqrestore(ap->lock, flags);
5586 ata_port_wait_eh(ap);
5587 } else
5588 spin_unlock_irqrestore(ap->lock, flags);
5589
5590 return rc;
5591 }
5592
5593 /**
5594 * ata_scsi_dev_rescan - initiate scsi_rescan_device()
5595 * @work: Pointer to ATA port to perform scsi_rescan_device()
5596 *
5597 * After ATA pass thru (SAT) commands are executed successfully,
5598 * libata need to propagate the changes to SCSI layer.
5599 *
5600 * LOCKING:
5601 * Kernel thread context (may sleep).
5602 */
ata_scsi_dev_rescan(struct work_struct * work)5603 void ata_scsi_dev_rescan(struct work_struct *work)
5604 {
5605 struct ata_port *ap =
5606 container_of(work, struct ata_port, scsi_rescan_task.work);
5607 struct ata_link *link;
5608 struct ata_device *dev;
5609 unsigned long flags;
5610 bool do_resume;
5611 int ret = 0;
5612
5613 mutex_lock(&ap->scsi_scan_mutex);
5614 spin_lock_irqsave(ap->lock, flags);
5615
5616 ata_for_each_link(link, ap, EDGE) {
5617 ata_for_each_dev(dev, link, ENABLED) {
5618 struct scsi_device *sdev = dev->sdev;
5619
5620 /*
5621 * If the port was suspended before this was scheduled,
5622 * bail out.
5623 */
5624 if (ap->pflags & ATA_PFLAG_SUSPENDED)
5625 goto unlock_ap;
5626
5627 if (!sdev)
5628 continue;
5629 if (scsi_device_get(sdev))
5630 continue;
5631
5632 do_resume = dev->flags & ATA_DFLAG_RESUMING;
5633
5634 spin_unlock_irqrestore(ap->lock, flags);
5635 if (do_resume) {
5636 ret = scsi_resume_device(sdev);
5637 if (ret == -EWOULDBLOCK) {
5638 scsi_device_put(sdev);
5639 goto unlock_scan;
5640 }
5641 dev->flags &= ~ATA_DFLAG_RESUMING;
5642 }
5643 ret = scsi_rescan_device(sdev);
5644 scsi_device_put(sdev);
5645 spin_lock_irqsave(ap->lock, flags);
5646
5647 if (ret)
5648 goto unlock_ap;
5649 }
5650 }
5651
5652 unlock_ap:
5653 spin_unlock_irqrestore(ap->lock, flags);
5654 unlock_scan:
5655 mutex_unlock(&ap->scsi_scan_mutex);
5656
5657 /* Reschedule with a delay if scsi_rescan_device() returned an error */
5658 if (ret)
5659 schedule_delayed_work(&ap->scsi_rescan_task,
5660 msecs_to_jiffies(5));
5661 }
5662