Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / ata / libata-scsi.c
blob1f3b43c15db8e435cd8bdfd612f988b990f99fd9
1 /*
2 * libata-scsi.c - helper library for ATA
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
30 * Hardware documentation available from
31 * - http://www.t10.org/
32 * - http://www.t13.org/
36 #include <linux/kernel.h>
37 #include <linux/blkdev.h>
38 #include <linux/spinlock.h>
39 #include <scsi/scsi.h>
40 #include <scsi/scsi_host.h>
41 #include <scsi/scsi_cmnd.h>
42 #include <scsi/scsi_eh.h>
43 #include <scsi/scsi_device.h>
44 #include <scsi/scsi_tcq.h>
45 #include <scsi/scsi_transport.h>
46 #include <linux/libata.h>
47 #include <linux/hdreg.h>
48 #include <linux/uaccess.h>
50 #include "libata.h"
52 #define SECTOR_SIZE 512
54 typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
56 static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
57 const struct scsi_device *scsidev);
58 static struct ata_device *ata_scsi_find_dev(struct ata_port *ap,
59 const struct scsi_device *scsidev);
60 static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
61 unsigned int id, unsigned int lun);
64 #define RW_RECOVERY_MPAGE 0x1
65 #define RW_RECOVERY_MPAGE_LEN 12
66 #define CACHE_MPAGE 0x8
67 #define CACHE_MPAGE_LEN 20
68 #define CONTROL_MPAGE 0xa
69 #define CONTROL_MPAGE_LEN 12
70 #define ALL_MPAGES 0x3f
71 #define ALL_SUB_MPAGES 0xff
74 static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
75 RW_RECOVERY_MPAGE,
76 RW_RECOVERY_MPAGE_LEN - 2,
77 (1 << 7), /* AWRE */
78 0, /* read retry count */
79 0, 0, 0, 0,
80 0, /* write retry count */
81 0, 0, 0
84 static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
85 CACHE_MPAGE,
86 CACHE_MPAGE_LEN - 2,
87 0, /* contains WCE, needs to be 0 for logic */
88 0, 0, 0, 0, 0, 0, 0, 0, 0,
89 0, /* contains DRA, needs to be 0 for logic */
90 0, 0, 0, 0, 0, 0, 0
93 static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
94 CONTROL_MPAGE,
95 CONTROL_MPAGE_LEN - 2,
96 2, /* DSENSE=0, GLTSD=1 */
97 0, /* [QAM+QERR may be 1, see 05-359r1] */
98 0, 0, 0, 0, 0xff, 0xff,
99 0, 30 /* extended self test time, see 05-359r1 */
103 * libata transport template. libata doesn't do real transport stuff.
104 * It just needs the eh_timed_out hook.
106 static struct scsi_transport_template ata_scsi_transport_template = {
107 .eh_strategy_handler = ata_scsi_error,
108 .eh_timed_out = ata_scsi_timed_out,
109 .user_scan = ata_scsi_user_scan,
113 static const struct {
114 enum link_pm value;
115 const char *name;
116 } link_pm_policy[] = {
117 { NOT_AVAILABLE, "max_performance" },
118 { MIN_POWER, "min_power" },
119 { MAX_PERFORMANCE, "max_performance" },
120 { MEDIUM_POWER, "medium_power" },
123 static const char *ata_scsi_lpm_get(enum link_pm policy)
125 int i;
127 for (i = 0; i < ARRAY_SIZE(link_pm_policy); i++)
128 if (link_pm_policy[i].value == policy)
129 return link_pm_policy[i].name;
131 return NULL;
134 static ssize_t ata_scsi_lpm_put(struct class_device *class_dev,
135 const char *buf, size_t count)
137 struct Scsi_Host *shost = class_to_shost(class_dev);
138 struct ata_port *ap = ata_shost_to_port(shost);
139 enum link_pm policy = 0;
140 int i;
143 * we are skipping array location 0 on purpose - this
144 * is because a value of NOT_AVAILABLE is displayed
145 * to the user as max_performance, but when the user
146 * writes "max_performance", they actually want the
147 * value to match MAX_PERFORMANCE.
149 for (i = 1; i < ARRAY_SIZE(link_pm_policy); i++) {
150 const int len = strlen(link_pm_policy[i].name);
151 if (strncmp(link_pm_policy[i].name, buf, len) == 0 &&
152 buf[len] == '\n') {
153 policy = link_pm_policy[i].value;
154 break;
157 if (!policy)
158 return -EINVAL;
160 ata_lpm_schedule(ap, policy);
161 return count;
164 static ssize_t
165 ata_scsi_lpm_show(struct class_device *class_dev, char *buf)
167 struct Scsi_Host *shost = class_to_shost(class_dev);
168 struct ata_port *ap = ata_shost_to_port(shost);
169 const char *policy =
170 ata_scsi_lpm_get(ap->pm_policy);
172 if (!policy)
173 return -EINVAL;
175 return snprintf(buf, 23, "%s\n", policy);
177 CLASS_DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
178 ata_scsi_lpm_show, ata_scsi_lpm_put);
179 EXPORT_SYMBOL_GPL(class_device_attr_link_power_management_policy);
181 static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
182 void (*done)(struct scsi_cmnd *))
184 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
185 /* "Invalid field in cbd" */
186 done(cmd);
190 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
191 * @sdev: SCSI device for which BIOS geometry is to be determined
192 * @bdev: block device associated with @sdev
193 * @capacity: capacity of SCSI device
194 * @geom: location to which geometry will be output
196 * Generic bios head/sector/cylinder calculator
197 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
198 * mapping. Some situations may arise where the disk is not
199 * bootable if this is not used.
201 * LOCKING:
202 * Defined by the SCSI layer. We don't really care.
204 * RETURNS:
205 * Zero.
207 int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
208 sector_t capacity, int geom[])
210 geom[0] = 255;
211 geom[1] = 63;
212 sector_div(capacity, 255*63);
213 geom[2] = capacity;
215 return 0;
219 * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
220 * @sdev: SCSI device to get identify data for
221 * @arg: User buffer area for identify data
223 * LOCKING:
224 * Defined by the SCSI layer. We don't really care.
226 * RETURNS:
227 * Zero on success, negative errno on error.
229 static int ata_get_identity(struct scsi_device *sdev, void __user *arg)
231 struct ata_port *ap = ata_shost_to_port(sdev->host);
232 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
233 u16 __user *dst = arg;
234 char buf[40];
236 if (!dev)
237 return -ENOMSG;
239 if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
240 return -EFAULT;
242 ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
243 if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
244 return -EFAULT;
246 ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
247 if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
248 return -EFAULT;
250 ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
251 if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
252 return -EFAULT;
254 return 0;
258 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
259 * @scsidev: Device to which we are issuing command
260 * @arg: User provided data for issuing command
262 * LOCKING:
263 * Defined by the SCSI layer. We don't really care.
265 * RETURNS:
266 * Zero on success, negative errno on error.
268 int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
270 int rc = 0;
271 u8 scsi_cmd[MAX_COMMAND_SIZE];
272 u8 args[4], *argbuf = NULL, *sensebuf = NULL;
273 int argsize = 0;
274 enum dma_data_direction data_dir;
275 int cmd_result;
277 if (arg == NULL)
278 return -EINVAL;
280 if (copy_from_user(args, arg, sizeof(args)))
281 return -EFAULT;
283 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
284 if (!sensebuf)
285 return -ENOMEM;
287 memset(scsi_cmd, 0, sizeof(scsi_cmd));
289 if (args[3]) {
290 argsize = SECTOR_SIZE * args[3];
291 argbuf = kmalloc(argsize, GFP_KERNEL);
292 if (argbuf == NULL) {
293 rc = -ENOMEM;
294 goto error;
297 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
298 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
299 block count in sector count field */
300 data_dir = DMA_FROM_DEVICE;
301 } else {
302 scsi_cmd[1] = (3 << 1); /* Non-data */
303 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
304 data_dir = DMA_NONE;
307 scsi_cmd[0] = ATA_16;
309 scsi_cmd[4] = args[2];
310 if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
311 scsi_cmd[6] = args[3];
312 scsi_cmd[8] = args[1];
313 scsi_cmd[10] = 0x4f;
314 scsi_cmd[12] = 0xc2;
315 } else {
316 scsi_cmd[6] = args[1];
318 scsi_cmd[14] = args[0];
320 /* Good values for timeout and retries? Values below
321 from scsi_ioctl_send_command() for default case... */
322 cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
323 sensebuf, (10*HZ), 5, 0);
325 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
326 u8 *desc = sensebuf + 8;
327 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
329 /* If we set cc then ATA pass-through will cause a
330 * check condition even if no error. Filter that. */
331 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
332 struct scsi_sense_hdr sshdr;
333 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
334 &sshdr);
335 if (sshdr.sense_key == 0 &&
336 sshdr.asc == 0 && sshdr.ascq == 0)
337 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
340 /* Send userspace a few ATA registers (same as drivers/ide) */
341 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
342 desc[0] == 0x09) { /* code is "ATA Descriptor" */
343 args[0] = desc[13]; /* status */
344 args[1] = desc[3]; /* error */
345 args[2] = desc[5]; /* sector count (0:7) */
346 if (copy_to_user(arg, args, sizeof(args)))
347 rc = -EFAULT;
352 if (cmd_result) {
353 rc = -EIO;
354 goto error;
357 if ((argbuf)
358 && copy_to_user(arg + sizeof(args), argbuf, argsize))
359 rc = -EFAULT;
360 error:
361 kfree(sensebuf);
362 kfree(argbuf);
363 return rc;
367 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
368 * @scsidev: Device to which we are issuing command
369 * @arg: User provided data for issuing command
371 * LOCKING:
372 * Defined by the SCSI layer. We don't really care.
374 * RETURNS:
375 * Zero on success, negative errno on error.
377 int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
379 int rc = 0;
380 u8 scsi_cmd[MAX_COMMAND_SIZE];
381 u8 args[7], *sensebuf = NULL;
382 int cmd_result;
384 if (arg == NULL)
385 return -EINVAL;
387 if (copy_from_user(args, arg, sizeof(args)))
388 return -EFAULT;
390 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
391 if (!sensebuf)
392 return -ENOMEM;
394 memset(scsi_cmd, 0, sizeof(scsi_cmd));
395 scsi_cmd[0] = ATA_16;
396 scsi_cmd[1] = (3 << 1); /* Non-data */
397 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
398 scsi_cmd[4] = args[1];
399 scsi_cmd[6] = args[2];
400 scsi_cmd[8] = args[3];
401 scsi_cmd[10] = args[4];
402 scsi_cmd[12] = args[5];
403 scsi_cmd[13] = args[6] & 0x4f;
404 scsi_cmd[14] = args[0];
406 /* Good values for timeout and retries? Values below
407 from scsi_ioctl_send_command() for default case... */
408 cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
409 sensebuf, (10*HZ), 5, 0);
411 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
412 u8 *desc = sensebuf + 8;
413 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
415 /* If we set cc then ATA pass-through will cause a
416 * check condition even if no error. Filter that. */
417 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
418 struct scsi_sense_hdr sshdr;
419 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
420 &sshdr);
421 if (sshdr.sense_key == 0 &&
422 sshdr.asc == 0 && sshdr.ascq == 0)
423 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
426 /* Send userspace ATA registers */
427 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
428 desc[0] == 0x09) {/* code is "ATA Descriptor" */
429 args[0] = desc[13]; /* status */
430 args[1] = desc[3]; /* error */
431 args[2] = desc[5]; /* sector count (0:7) */
432 args[3] = desc[7]; /* lbal */
433 args[4] = desc[9]; /* lbam */
434 args[5] = desc[11]; /* lbah */
435 args[6] = desc[12]; /* select */
436 if (copy_to_user(arg, args, sizeof(args)))
437 rc = -EFAULT;
441 if (cmd_result) {
442 rc = -EIO;
443 goto error;
446 error:
447 kfree(sensebuf);
448 return rc;
451 int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
453 int val = -EINVAL, rc = -EINVAL;
455 switch (cmd) {
456 case ATA_IOC_GET_IO32:
457 val = 0;
458 if (copy_to_user(arg, &val, 1))
459 return -EFAULT;
460 return 0;
462 case ATA_IOC_SET_IO32:
463 val = (unsigned long) arg;
464 if (val != 0)
465 return -EINVAL;
466 return 0;
468 case HDIO_GET_IDENTITY:
469 return ata_get_identity(scsidev, arg);
471 case HDIO_DRIVE_CMD:
472 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
473 return -EACCES;
474 return ata_cmd_ioctl(scsidev, arg);
476 case HDIO_DRIVE_TASK:
477 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
478 return -EACCES;
479 return ata_task_ioctl(scsidev, arg);
481 default:
482 rc = -ENOTTY;
483 break;
486 return rc;
490 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
491 * @dev: ATA device to which the new command is attached
492 * @cmd: SCSI command that originated this ATA command
493 * @done: SCSI command completion function
495 * Obtain a reference to an unused ata_queued_cmd structure,
496 * which is the basic libata structure representing a single
497 * ATA command sent to the hardware.
499 * If a command was available, fill in the SCSI-specific
500 * portions of the structure with information on the
501 * current command.
503 * LOCKING:
504 * spin_lock_irqsave(host lock)
506 * RETURNS:
507 * Command allocated, or %NULL if none available.
509 static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
510 struct scsi_cmnd *cmd,
511 void (*done)(struct scsi_cmnd *))
513 struct ata_queued_cmd *qc;
515 qc = ata_qc_new_init(dev);
516 if (qc) {
517 qc->scsicmd = cmd;
518 qc->scsidone = done;
520 qc->sg = scsi_sglist(cmd);
521 qc->n_elem = scsi_sg_count(cmd);
522 } else {
523 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
524 done(cmd);
527 return qc;
531 * ata_dump_status - user friendly display of error info
532 * @id: id of the port in question
533 * @tf: ptr to filled out taskfile
535 * Decode and dump the ATA error/status registers for the user so
536 * that they have some idea what really happened at the non
537 * make-believe layer.
539 * LOCKING:
540 * inherited from caller
542 static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
544 u8 stat = tf->command, err = tf->feature;
546 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
547 if (stat & ATA_BUSY) {
548 printk("Busy }\n"); /* Data is not valid in this case */
549 } else {
550 if (stat & 0x40) printk("DriveReady ");
551 if (stat & 0x20) printk("DeviceFault ");
552 if (stat & 0x10) printk("SeekComplete ");
553 if (stat & 0x08) printk("DataRequest ");
554 if (stat & 0x04) printk("CorrectedError ");
555 if (stat & 0x02) printk("Index ");
556 if (stat & 0x01) printk("Error ");
557 printk("}\n");
559 if (err) {
560 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
561 if (err & 0x04) printk("DriveStatusError ");
562 if (err & 0x80) {
563 if (err & 0x04) printk("BadCRC ");
564 else printk("Sector ");
566 if (err & 0x40) printk("UncorrectableError ");
567 if (err & 0x10) printk("SectorIdNotFound ");
568 if (err & 0x02) printk("TrackZeroNotFound ");
569 if (err & 0x01) printk("AddrMarkNotFound ");
570 printk("}\n");
576 * ata_to_sense_error - convert ATA error to SCSI error
577 * @id: ATA device number
578 * @drv_stat: value contained in ATA status register
579 * @drv_err: value contained in ATA error register
580 * @sk: the sense key we'll fill out
581 * @asc: the additional sense code we'll fill out
582 * @ascq: the additional sense code qualifier we'll fill out
583 * @verbose: be verbose
585 * Converts an ATA error into a SCSI error. Fill out pointers to
586 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
587 * format sense blocks.
589 * LOCKING:
590 * spin_lock_irqsave(host lock)
592 static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
593 u8 *asc, u8 *ascq, int verbose)
595 int i;
597 /* Based on the 3ware driver translation table */
598 static const unsigned char sense_table[][4] = {
599 /* BBD|ECC|ID|MAR */
600 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
601 /* BBD|ECC|ID */
602 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
603 /* ECC|MC|MARK */
604 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
605 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
606 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
607 /* MC|ID|ABRT|TRK0|MARK */
608 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
609 /* MCR|MARK */
610 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
611 /* Bad address mark */
612 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
613 /* TRK0 */
614 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
615 /* Abort & !ICRC */
616 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
617 /* Media change request */
618 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
619 /* SRV */
620 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
621 /* Media change */
622 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
623 /* ECC */
624 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
625 /* BBD - block marked bad */
626 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
627 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
629 static const unsigned char stat_table[][4] = {
630 /* Must be first because BUSY means no other bits valid */
631 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
632 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
633 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
634 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
635 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
639 * Is this an error we can process/parse
641 if (drv_stat & ATA_BUSY) {
642 drv_err = 0; /* Ignore the err bits, they're invalid */
645 if (drv_err) {
646 /* Look for drv_err */
647 for (i = 0; sense_table[i][0] != 0xFF; i++) {
648 /* Look for best matches first */
649 if ((sense_table[i][0] & drv_err) ==
650 sense_table[i][0]) {
651 *sk = sense_table[i][1];
652 *asc = sense_table[i][2];
653 *ascq = sense_table[i][3];
654 goto translate_done;
657 /* No immediate match */
658 if (verbose)
659 printk(KERN_WARNING "ata%u: no sense translation for "
660 "error 0x%02x\n", id, drv_err);
663 /* Fall back to interpreting status bits */
664 for (i = 0; stat_table[i][0] != 0xFF; i++) {
665 if (stat_table[i][0] & drv_stat) {
666 *sk = stat_table[i][1];
667 *asc = stat_table[i][2];
668 *ascq = stat_table[i][3];
669 goto translate_done;
672 /* No error? Undecoded? */
673 if (verbose)
674 printk(KERN_WARNING "ata%u: no sense translation for "
675 "status: 0x%02x\n", id, drv_stat);
677 /* We need a sensible error return here, which is tricky, and one
678 that won't cause people to do things like return a disk wrongly */
679 *sk = ABORTED_COMMAND;
680 *asc = 0x00;
681 *ascq = 0x00;
683 translate_done:
684 if (verbose)
685 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x "
686 "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
687 id, drv_stat, drv_err, *sk, *asc, *ascq);
688 return;
692 * ata_gen_passthru_sense - Generate check condition sense block.
693 * @qc: Command that completed.
695 * This function is specific to the ATA descriptor format sense
696 * block specified for the ATA pass through commands. Regardless
697 * of whether the command errored or not, return a sense
698 * block. Copy all controller registers into the sense
699 * block. Clear sense key, ASC & ASCQ if there is no error.
701 * LOCKING:
702 * None.
704 static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
706 struct scsi_cmnd *cmd = qc->scsicmd;
707 struct ata_taskfile *tf = &qc->result_tf;
708 unsigned char *sb = cmd->sense_buffer;
709 unsigned char *desc = sb + 8;
710 int verbose = qc->ap->ops->error_handler == NULL;
712 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
714 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
717 * Use ata_to_sense_error() to map status register bits
718 * onto sense key, asc & ascq.
720 if (qc->err_mask ||
721 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
722 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
723 &sb[1], &sb[2], &sb[3], verbose);
724 sb[1] &= 0x0f;
728 * Sense data is current and format is descriptor.
730 sb[0] = 0x72;
732 desc[0] = 0x09;
734 /* set length of additional sense data */
735 sb[7] = 14;
736 desc[1] = 12;
739 * Copy registers into sense buffer.
741 desc[2] = 0x00;
742 desc[3] = tf->feature; /* == error reg */
743 desc[5] = tf->nsect;
744 desc[7] = tf->lbal;
745 desc[9] = tf->lbam;
746 desc[11] = tf->lbah;
747 desc[12] = tf->device;
748 desc[13] = tf->command; /* == status reg */
751 * Fill in Extend bit, and the high order bytes
752 * if applicable.
754 if (tf->flags & ATA_TFLAG_LBA48) {
755 desc[2] |= 0x01;
756 desc[4] = tf->hob_nsect;
757 desc[6] = tf->hob_lbal;
758 desc[8] = tf->hob_lbam;
759 desc[10] = tf->hob_lbah;
764 * ata_gen_ata_sense - generate a SCSI fixed sense block
765 * @qc: Command that we are erroring out
767 * Generate sense block for a failed ATA command @qc. Descriptor
768 * format is used to accomodate LBA48 block address.
770 * LOCKING:
771 * None.
773 static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
775 struct ata_device *dev = qc->dev;
776 struct scsi_cmnd *cmd = qc->scsicmd;
777 struct ata_taskfile *tf = &qc->result_tf;
778 unsigned char *sb = cmd->sense_buffer;
779 unsigned char *desc = sb + 8;
780 int verbose = qc->ap->ops->error_handler == NULL;
781 u64 block;
783 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
785 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
787 /* sense data is current and format is descriptor */
788 sb[0] = 0x72;
790 /* Use ata_to_sense_error() to map status register bits
791 * onto sense key, asc & ascq.
793 if (qc->err_mask ||
794 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
795 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
796 &sb[1], &sb[2], &sb[3], verbose);
797 sb[1] &= 0x0f;
800 block = ata_tf_read_block(&qc->result_tf, dev);
802 /* information sense data descriptor */
803 sb[7] = 12;
804 desc[0] = 0x00;
805 desc[1] = 10;
807 desc[2] |= 0x80; /* valid */
808 desc[6] = block >> 40;
809 desc[7] = block >> 32;
810 desc[8] = block >> 24;
811 desc[9] = block >> 16;
812 desc[10] = block >> 8;
813 desc[11] = block;
816 static void ata_scsi_sdev_config(struct scsi_device *sdev)
818 sdev->use_10_for_rw = 1;
819 sdev->use_10_for_ms = 1;
821 /* Schedule policy is determined by ->qc_defer() callback and
822 * it needs to see every deferred qc. Set dev_blocked to 1 to
823 * prevent SCSI midlayer from automatically deferring
824 * requests.
826 sdev->max_device_blocked = 1;
829 <<<<<<< HEAD:drivers/ata/libata-scsi.c
830 static void ata_scsi_dev_config(struct scsi_device *sdev,
831 struct ata_device *dev)
832 =======
834 * atapi_drain_needed - Check whether data transfer may overflow
835 * @rq: request to be checked
837 * ATAPI commands which transfer variable length data to host
838 * might overflow due to application error or hardare bug. This
839 * function checks whether overflow should be drained and ignored
840 * for @request.
842 * LOCKING:
843 * None.
845 * RETURNS:
846 * 1 if ; otherwise, 0.
848 static int atapi_drain_needed(struct request *rq)
850 if (likely(!blk_pc_request(rq)))
851 return 0;
853 if (!rq->data_len || (rq->cmd_flags & REQ_RW))
854 return 0;
856 return atapi_cmd_type(rq->cmd[0]) == ATAPI_MISC;
859 static int ata_scsi_dev_config(struct scsi_device *sdev,
860 struct ata_device *dev)
861 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
863 /* configure max sectors */
864 blk_queue_max_sectors(sdev->request_queue, dev->max_sectors);
866 <<<<<<< HEAD:drivers/ata/libata-scsi.c
867 /* SATA DMA transfers must be multiples of 4 byte, so
868 * we need to pad ATAPI transfers using an extra sg.
869 * Decrement max hw segments accordingly.
871 =======
872 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
873 if (dev->class == ATA_DEV_ATAPI) {
874 struct request_queue *q = sdev->request_queue;
875 <<<<<<< HEAD:drivers/ata/libata-scsi.c
876 blk_queue_max_hw_segments(q, q->max_hw_segments - 1);
877 =======
878 void *buf;
879 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
881 <<<<<<< HEAD:drivers/ata/libata-scsi.c
882 /* set the min alignment */
883 =======
884 /* set the min alignment and padding */
885 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
886 blk_queue_update_dma_alignment(sdev->request_queue,
887 ATA_DMA_PAD_SZ - 1);
888 <<<<<<< HEAD:drivers/ata/libata-scsi.c
889 } else
890 =======
891 blk_queue_dma_pad(sdev->request_queue, ATA_DMA_PAD_SZ - 1);
893 /* configure draining */
894 buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
895 if (!buf) {
896 ata_dev_printk(dev, KERN_ERR,
897 "drain buffer allocation failed\n");
898 return -ENOMEM;
901 blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
902 } else {
903 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
904 /* ATA devices must be sector aligned */
905 blk_queue_update_dma_alignment(sdev->request_queue,
906 ATA_SECT_SIZE - 1);
907 <<<<<<< HEAD:drivers/ata/libata-scsi.c
909 if (dev->class == ATA_DEV_ATA)
910 =======
911 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
912 sdev->manage_start_stop = 1;
913 <<<<<<< HEAD:drivers/ata/libata-scsi.c
914 =======
916 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
918 if (dev->flags & ATA_DFLAG_AN)
919 set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
921 if (dev->flags & ATA_DFLAG_NCQ) {
922 int depth;
924 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
925 depth = min(ATA_MAX_QUEUE - 1, depth);
926 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
928 <<<<<<< HEAD:drivers/ata/libata-scsi.c
929 =======
931 return 0;
932 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
936 * ata_scsi_slave_config - Set SCSI device attributes
937 * @sdev: SCSI device to examine
939 * This is called before we actually start reading
940 * and writing to the device, to configure certain
941 * SCSI mid-layer behaviors.
943 * LOCKING:
944 * Defined by SCSI layer. We don't really care.
947 int ata_scsi_slave_config(struct scsi_device *sdev)
949 struct ata_port *ap = ata_shost_to_port(sdev->host);
950 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
951 <<<<<<< HEAD:drivers/ata/libata-scsi.c
952 =======
953 int rc = 0;
954 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
956 ata_scsi_sdev_config(sdev);
958 if (dev)
959 <<<<<<< HEAD:drivers/ata/libata-scsi.c
960 ata_scsi_dev_config(sdev, dev);
961 =======
962 rc = ata_scsi_dev_config(sdev, dev);
963 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
965 <<<<<<< HEAD:drivers/ata/libata-scsi.c
966 return 0;
967 =======
968 return rc;
969 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
973 * ata_scsi_slave_destroy - SCSI device is about to be destroyed
974 * @sdev: SCSI device to be destroyed
976 * @sdev is about to be destroyed for hot/warm unplugging. If
977 * this unplugging was initiated by libata as indicated by NULL
978 * dev->sdev, this function doesn't have to do anything.
979 * Otherwise, SCSI layer initiated warm-unplug is in progress.
980 * Clear dev->sdev, schedule the device for ATA detach and invoke
981 * EH.
983 * LOCKING:
984 * Defined by SCSI layer. We don't really care.
986 void ata_scsi_slave_destroy(struct scsi_device *sdev)
988 struct ata_port *ap = ata_shost_to_port(sdev->host);
989 <<<<<<< HEAD:drivers/ata/libata-scsi.c
990 =======
991 struct request_queue *q = sdev->request_queue;
992 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
993 unsigned long flags;
994 struct ata_device *dev;
996 if (!ap->ops->error_handler)
997 return;
999 spin_lock_irqsave(ap->lock, flags);
1000 dev = __ata_scsi_find_dev(ap, sdev);
1001 if (dev && dev->sdev) {
1002 /* SCSI device already in CANCEL state, no need to offline it */
1003 dev->sdev = NULL;
1004 dev->flags |= ATA_DFLAG_DETACH;
1005 ata_port_schedule_eh(ap);
1007 spin_unlock_irqrestore(ap->lock, flags);
1008 <<<<<<< HEAD:drivers/ata/libata-scsi.c
1009 =======
1011 kfree(q->dma_drain_buffer);
1012 q->dma_drain_buffer = NULL;
1013 q->dma_drain_size = 0;
1014 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
1018 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
1019 * @sdev: SCSI device to configure queue depth for
1020 * @queue_depth: new queue depth
1022 * This is libata standard hostt->change_queue_depth callback.
1023 * SCSI will call into this callback when user tries to set queue
1024 * depth via sysfs.
1026 * LOCKING:
1027 * SCSI layer (we don't care)
1029 * RETURNS:
1030 * Newly configured queue depth.
1032 int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
1034 struct ata_port *ap = ata_shost_to_port(sdev->host);
1035 struct ata_device *dev;
1036 unsigned long flags;
1038 if (queue_depth < 1 || queue_depth == sdev->queue_depth)
1039 return sdev->queue_depth;
1041 dev = ata_scsi_find_dev(ap, sdev);
1042 if (!dev || !ata_dev_enabled(dev))
1043 return sdev->queue_depth;
1045 /* NCQ enabled? */
1046 spin_lock_irqsave(ap->lock, flags);
1047 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1048 if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
1049 dev->flags |= ATA_DFLAG_NCQ_OFF;
1050 queue_depth = 1;
1052 spin_unlock_irqrestore(ap->lock, flags);
1054 /* limit and apply queue depth */
1055 queue_depth = min(queue_depth, sdev->host->can_queue);
1056 queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
1057 queue_depth = min(queue_depth, ATA_MAX_QUEUE - 1);
1059 if (sdev->queue_depth == queue_depth)
1060 return -EINVAL;
1062 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, queue_depth);
1063 return queue_depth;
1066 /* XXX: for spindown warning */
1067 static void ata_delayed_done_timerfn(unsigned long arg)
1069 struct scsi_cmnd *scmd = (void *)arg;
1071 scmd->scsi_done(scmd);
1074 /* XXX: for spindown warning */
1075 static void ata_delayed_done(struct scsi_cmnd *scmd)
1077 static struct timer_list timer;
1079 setup_timer(&timer, ata_delayed_done_timerfn, (unsigned long)scmd);
1080 mod_timer(&timer, jiffies + 5 * HZ);
1084 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
1085 * @qc: Storage for translated ATA taskfile
1087 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
1088 * (to start). Perhaps these commands should be preceded by
1089 * CHECK POWER MODE to see what power mode the device is already in.
1090 * [See SAT revision 5 at www.t10.org]
1092 * LOCKING:
1093 * spin_lock_irqsave(host lock)
1095 * RETURNS:
1096 * Zero on success, non-zero on error.
1098 static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
1100 struct scsi_cmnd *scmd = qc->scsicmd;
1101 struct ata_taskfile *tf = &qc->tf;
1102 const u8 *cdb = scmd->cmnd;
1104 if (scmd->cmd_len < 5)
1105 goto invalid_fld;
1107 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1108 tf->protocol = ATA_PROT_NODATA;
1109 if (cdb[1] & 0x1) {
1110 ; /* ignore IMMED bit, violates sat-r05 */
1112 if (cdb[4] & 0x2)
1113 goto invalid_fld; /* LOEJ bit set not supported */
1114 if (((cdb[4] >> 4) & 0xf) != 0)
1115 goto invalid_fld; /* power conditions not supported */
1117 if (qc->dev->horkage & ATA_HORKAGE_SKIP_PM) {
1118 /* the device lacks PM support, finish without doing anything */
1119 scmd->result = SAM_STAT_GOOD;
1120 return 1;
1123 if (cdb[4] & 0x1) {
1124 tf->nsect = 1; /* 1 sector, lba=0 */
1126 if (qc->dev->flags & ATA_DFLAG_LBA) {
1127 tf->flags |= ATA_TFLAG_LBA;
1129 tf->lbah = 0x0;
1130 tf->lbam = 0x0;
1131 tf->lbal = 0x0;
1132 tf->device |= ATA_LBA;
1133 } else {
1134 /* CHS */
1135 tf->lbal = 0x1; /* sect */
1136 tf->lbam = 0x0; /* cyl low */
1137 tf->lbah = 0x0; /* cyl high */
1140 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
1141 } else {
1142 /* XXX: This is for backward compatibility, will be
1143 * removed. Read Documentation/feature-removal-schedule.txt
1144 * for more info.
1146 if ((qc->dev->flags & ATA_DFLAG_SPUNDOWN) &&
1147 (system_state == SYSTEM_HALT ||
1148 system_state == SYSTEM_POWER_OFF)) {
1149 static unsigned long warned;
1151 if (!test_and_set_bit(0, &warned)) {
1152 ata_dev_printk(qc->dev, KERN_WARNING,
1153 "DISK MIGHT NOT BE SPUN DOWN PROPERLY. "
1154 "UPDATE SHUTDOWN UTILITY\n");
1155 ata_dev_printk(qc->dev, KERN_WARNING,
1156 "For more info, visit "
1157 "http://linux-ata.org/shutdown.html\n");
1159 /* ->scsi_done is not used, use it for
1160 * delayed completion.
1162 scmd->scsi_done = qc->scsidone;
1163 qc->scsidone = ata_delayed_done;
1165 scmd->result = SAM_STAT_GOOD;
1166 return 1;
1169 /* Issue ATA STANDBY IMMEDIATE command */
1170 tf->command = ATA_CMD_STANDBYNOW1;
1174 * Standby and Idle condition timers could be implemented but that
1175 * would require libata to implement the Power condition mode page
1176 * and allow the user to change it. Changing mode pages requires
1177 * MODE SELECT to be implemented.
1180 return 0;
1182 invalid_fld:
1183 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
1184 /* "Invalid field in cbd" */
1185 return 1;
1190 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
1191 * @qc: Storage for translated ATA taskfile
1193 * Sets up an ATA taskfile to issue FLUSH CACHE or
1194 * FLUSH CACHE EXT.
1196 * LOCKING:
1197 * spin_lock_irqsave(host lock)
1199 * RETURNS:
1200 * Zero on success, non-zero on error.
1202 static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
1204 struct ata_taskfile *tf = &qc->tf;
1206 tf->flags |= ATA_TFLAG_DEVICE;
1207 tf->protocol = ATA_PROT_NODATA;
1209 if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
1210 tf->command = ATA_CMD_FLUSH_EXT;
1211 else
1212 tf->command = ATA_CMD_FLUSH;
1214 /* flush is critical for IO integrity, consider it an IO command */
1215 qc->flags |= ATA_QCFLAG_IO;
1217 return 0;
1221 * scsi_6_lba_len - Get LBA and transfer length
1222 * @cdb: SCSI command to translate
1224 * Calculate LBA and transfer length for 6-byte commands.
1226 * RETURNS:
1227 * @plba: the LBA
1228 * @plen: the transfer length
1230 static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1232 u64 lba = 0;
1233 u32 len;
1235 VPRINTK("six-byte command\n");
1237 lba |= ((u64)(cdb[1] & 0x1f)) << 16;
1238 lba |= ((u64)cdb[2]) << 8;
1239 lba |= ((u64)cdb[3]);
1241 len = cdb[4];
1243 *plba = lba;
1244 *plen = len;
1248 * scsi_10_lba_len - Get LBA and transfer length
1249 * @cdb: SCSI command to translate
1251 * Calculate LBA and transfer length for 10-byte commands.
1253 * RETURNS:
1254 * @plba: the LBA
1255 * @plen: the transfer length
1257 static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1259 u64 lba = 0;
1260 u32 len = 0;
1262 VPRINTK("ten-byte command\n");
1264 lba |= ((u64)cdb[2]) << 24;
1265 lba |= ((u64)cdb[3]) << 16;
1266 lba |= ((u64)cdb[4]) << 8;
1267 lba |= ((u64)cdb[5]);
1269 len |= ((u32)cdb[7]) << 8;
1270 len |= ((u32)cdb[8]);
1272 *plba = lba;
1273 *plen = len;
1277 * scsi_16_lba_len - Get LBA and transfer length
1278 * @cdb: SCSI command to translate
1280 * Calculate LBA and transfer length for 16-byte commands.
1282 * RETURNS:
1283 * @plba: the LBA
1284 * @plen: the transfer length
1286 static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
1288 u64 lba = 0;
1289 u32 len = 0;
1291 VPRINTK("sixteen-byte command\n");
1293 lba |= ((u64)cdb[2]) << 56;
1294 lba |= ((u64)cdb[3]) << 48;
1295 lba |= ((u64)cdb[4]) << 40;
1296 lba |= ((u64)cdb[5]) << 32;
1297 lba |= ((u64)cdb[6]) << 24;
1298 lba |= ((u64)cdb[7]) << 16;
1299 lba |= ((u64)cdb[8]) << 8;
1300 lba |= ((u64)cdb[9]);
1302 len |= ((u32)cdb[10]) << 24;
1303 len |= ((u32)cdb[11]) << 16;
1304 len |= ((u32)cdb[12]) << 8;
1305 len |= ((u32)cdb[13]);
1307 *plba = lba;
1308 *plen = len;
1312 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1313 * @qc: Storage for translated ATA taskfile
1315 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1317 * LOCKING:
1318 * spin_lock_irqsave(host lock)
1320 * RETURNS:
1321 * Zero on success, non-zero on error.
1323 static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
1325 struct scsi_cmnd *scmd = qc->scsicmd;
1326 struct ata_taskfile *tf = &qc->tf;
1327 struct ata_device *dev = qc->dev;
1328 u64 dev_sectors = qc->dev->n_sectors;
1329 const u8 *cdb = scmd->cmnd;
1330 u64 block;
1331 u32 n_block;
1333 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1334 tf->protocol = ATA_PROT_NODATA;
1336 if (cdb[0] == VERIFY) {
1337 if (scmd->cmd_len < 10)
1338 goto invalid_fld;
1339 scsi_10_lba_len(cdb, &block, &n_block);
1340 } else if (cdb[0] == VERIFY_16) {
1341 if (scmd->cmd_len < 16)
1342 goto invalid_fld;
1343 scsi_16_lba_len(cdb, &block, &n_block);
1344 } else
1345 goto invalid_fld;
1347 if (!n_block)
1348 goto nothing_to_do;
1349 if (block >= dev_sectors)
1350 goto out_of_range;
1351 if ((block + n_block) > dev_sectors)
1352 goto out_of_range;
1354 if (dev->flags & ATA_DFLAG_LBA) {
1355 tf->flags |= ATA_TFLAG_LBA;
1357 if (lba_28_ok(block, n_block)) {
1358 /* use LBA28 */
1359 tf->command = ATA_CMD_VERIFY;
1360 tf->device |= (block >> 24) & 0xf;
1361 } else if (lba_48_ok(block, n_block)) {
1362 if (!(dev->flags & ATA_DFLAG_LBA48))
1363 goto out_of_range;
1365 /* use LBA48 */
1366 tf->flags |= ATA_TFLAG_LBA48;
1367 tf->command = ATA_CMD_VERIFY_EXT;
1369 tf->hob_nsect = (n_block >> 8) & 0xff;
1371 tf->hob_lbah = (block >> 40) & 0xff;
1372 tf->hob_lbam = (block >> 32) & 0xff;
1373 tf->hob_lbal = (block >> 24) & 0xff;
1374 } else
1375 /* request too large even for LBA48 */
1376 goto out_of_range;
1378 tf->nsect = n_block & 0xff;
1380 tf->lbah = (block >> 16) & 0xff;
1381 tf->lbam = (block >> 8) & 0xff;
1382 tf->lbal = block & 0xff;
1384 tf->device |= ATA_LBA;
1385 } else {
1386 /* CHS */
1387 u32 sect, head, cyl, track;
1389 if (!lba_28_ok(block, n_block))
1390 goto out_of_range;
1392 /* Convert LBA to CHS */
1393 track = (u32)block / dev->sectors;
1394 cyl = track / dev->heads;
1395 head = track % dev->heads;
1396 sect = (u32)block % dev->sectors + 1;
1398 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1399 (u32)block, track, cyl, head, sect);
1401 /* Check whether the converted CHS can fit.
1402 Cylinder: 0-65535
1403 Head: 0-15
1404 Sector: 1-255*/
1405 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
1406 goto out_of_range;
1408 tf->command = ATA_CMD_VERIFY;
1409 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1410 tf->lbal = sect;
1411 tf->lbam = cyl;
1412 tf->lbah = cyl >> 8;
1413 tf->device |= head;
1416 return 0;
1418 invalid_fld:
1419 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
1420 /* "Invalid field in cbd" */
1421 return 1;
1423 out_of_range:
1424 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
1425 /* "Logical Block Address out of range" */
1426 return 1;
1428 nothing_to_do:
1429 scmd->result = SAM_STAT_GOOD;
1430 return 1;
1434 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1435 * @qc: Storage for translated ATA taskfile
1437 * Converts any of six SCSI read/write commands into the
1438 * ATA counterpart, including starting sector (LBA),
1439 * sector count, and taking into account the device's LBA48
1440 * support.
1442 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1443 * %WRITE_16 are currently supported.
1445 * LOCKING:
1446 * spin_lock_irqsave(host lock)
1448 * RETURNS:
1449 * Zero on success, non-zero on error.
1451 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
1453 struct scsi_cmnd *scmd = qc->scsicmd;
1454 const u8 *cdb = scmd->cmnd;
1455 unsigned int tf_flags = 0;
1456 u64 block;
1457 u32 n_block;
1458 int rc;
1460 if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
1461 tf_flags |= ATA_TFLAG_WRITE;
1463 /* Calculate the SCSI LBA, transfer length and FUA. */
1464 switch (cdb[0]) {
1465 case READ_10:
1466 case WRITE_10:
1467 if (unlikely(scmd->cmd_len < 10))
1468 goto invalid_fld;
1469 scsi_10_lba_len(cdb, &block, &n_block);
1470 if (unlikely(cdb[1] & (1 << 3)))
1471 tf_flags |= ATA_TFLAG_FUA;
1472 break;
1473 case READ_6:
1474 case WRITE_6:
1475 if (unlikely(scmd->cmd_len < 6))
1476 goto invalid_fld;
1477 scsi_6_lba_len(cdb, &block, &n_block);
1479 /* for 6-byte r/w commands, transfer length 0
1480 * means 256 blocks of data, not 0 block.
1482 if (!n_block)
1483 n_block = 256;
1484 break;
1485 case READ_16:
1486 case WRITE_16:
1487 if (unlikely(scmd->cmd_len < 16))
1488 goto invalid_fld;
1489 scsi_16_lba_len(cdb, &block, &n_block);
1490 if (unlikely(cdb[1] & (1 << 3)))
1491 tf_flags |= ATA_TFLAG_FUA;
1492 break;
1493 default:
1494 DPRINTK("no-byte command\n");
1495 goto invalid_fld;
1498 /* Check and compose ATA command */
1499 if (!n_block)
1500 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1501 * length 0 means transfer 0 block of data.
1502 * However, for ATA R/W commands, sector count 0 means
1503 * 256 or 65536 sectors, not 0 sectors as in SCSI.
1505 * WARNING: one or two older ATA drives treat 0 as 0...
1507 goto nothing_to_do;
1509 qc->flags |= ATA_QCFLAG_IO;
1510 qc->nbytes = n_block * ATA_SECT_SIZE;
1512 rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
1513 qc->tag);
1514 if (likely(rc == 0))
1515 return 0;
1517 if (rc == -ERANGE)
1518 goto out_of_range;
1519 /* treat all other errors as -EINVAL, fall through */
1520 invalid_fld:
1521 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
1522 /* "Invalid field in cbd" */
1523 return 1;
1525 out_of_range:
1526 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
1527 /* "Logical Block Address out of range" */
1528 return 1;
1530 nothing_to_do:
1531 scmd->result = SAM_STAT_GOOD;
1532 return 1;
1535 static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
1537 struct ata_port *ap = qc->ap;
1538 struct scsi_cmnd *cmd = qc->scsicmd;
1539 u8 *cdb = cmd->cmnd;
1540 int need_sense = (qc->err_mask != 0);
1542 /* For ATA pass thru (SAT) commands, generate a sense block if
1543 * user mandated it or if there's an error. Note that if we
1544 * generate because the user forced us to, a check condition
1545 * is generated and the ATA register values are returned
1546 * whether the command completed successfully or not. If there
1547 * was no error, SK, ASC and ASCQ will all be zero.
1549 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
1550 ((cdb[2] & 0x20) || need_sense)) {
1551 ata_gen_passthru_sense(qc);
1552 } else {
1553 if (!need_sense) {
1554 cmd->result = SAM_STAT_GOOD;
1555 } else {
1556 /* TODO: decide which descriptor format to use
1557 * for 48b LBA devices and call that here
1558 * instead of the fixed desc, which is only
1559 * good for smaller LBA (and maybe CHS?)
1560 * devices.
1562 ata_gen_ata_sense(qc);
1566 /* XXX: track spindown state for spindown skipping and warning */
1567 if (unlikely(qc->tf.command == ATA_CMD_STANDBY ||
1568 qc->tf.command == ATA_CMD_STANDBYNOW1))
1569 qc->dev->flags |= ATA_DFLAG_SPUNDOWN;
1570 else if (likely(system_state != SYSTEM_HALT &&
1571 system_state != SYSTEM_POWER_OFF))
1572 qc->dev->flags &= ~ATA_DFLAG_SPUNDOWN;
1574 if (need_sense && !ap->ops->error_handler)
1575 ata_dump_status(ap->print_id, &qc->result_tf);
1577 qc->scsidone(cmd);
1579 ata_qc_free(qc);
1583 * ata_scsi_translate - Translate then issue SCSI command to ATA device
1584 * @dev: ATA device to which the command is addressed
1585 * @cmd: SCSI command to execute
1586 * @done: SCSI command completion function
1587 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1589 * Our ->queuecommand() function has decided that the SCSI
1590 * command issued can be directly translated into an ATA
1591 * command, rather than handled internally.
1593 * This function sets up an ata_queued_cmd structure for the
1594 * SCSI command, and sends that ata_queued_cmd to the hardware.
1596 * The xlat_func argument (actor) returns 0 if ready to execute
1597 * ATA command, else 1 to finish translation. If 1 is returned
1598 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1599 * to be set reflecting an error condition or clean (early)
1600 * termination.
1602 * LOCKING:
1603 * spin_lock_irqsave(host lock)
1605 * RETURNS:
1606 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
1607 * needs to be deferred.
1609 static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
1610 void (*done)(struct scsi_cmnd *),
1611 ata_xlat_func_t xlat_func)
1613 struct ata_port *ap = dev->link->ap;
1614 struct ata_queued_cmd *qc;
1615 int rc;
1617 VPRINTK("ENTER\n");
1619 qc = ata_scsi_qc_new(dev, cmd, done);
1620 if (!qc)
1621 goto err_mem;
1623 /* data is present; dma-map it */
1624 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1625 cmd->sc_data_direction == DMA_TO_DEVICE) {
1626 if (unlikely(scsi_bufflen(cmd) < 1)) {
1627 ata_dev_printk(dev, KERN_WARNING,
1628 "WARNING: zero len r/w req\n");
1629 goto err_did;
1632 ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
1634 qc->dma_dir = cmd->sc_data_direction;
1637 qc->complete_fn = ata_scsi_qc_complete;
1639 if (xlat_func(qc))
1640 goto early_finish;
1642 if (ap->ops->qc_defer) {
1643 if ((rc = ap->ops->qc_defer(qc)))
1644 goto defer;
1647 /* select device, send command to hardware */
1648 ata_qc_issue(qc);
1650 VPRINTK("EXIT\n");
1651 return 0;
1653 early_finish:
1654 ata_qc_free(qc);
1655 qc->scsidone(cmd);
1656 DPRINTK("EXIT - early finish (good or error)\n");
1657 return 0;
1659 err_did:
1660 ata_qc_free(qc);
1661 cmd->result = (DID_ERROR << 16);
1662 qc->scsidone(cmd);
1663 err_mem:
1664 DPRINTK("EXIT - internal\n");
1665 return 0;
1667 defer:
1668 ata_qc_free(qc);
1669 DPRINTK("EXIT - defer\n");
1670 if (rc == ATA_DEFER_LINK)
1671 return SCSI_MLQUEUE_DEVICE_BUSY;
1672 else
1673 return SCSI_MLQUEUE_HOST_BUSY;
1677 * ata_scsi_rbuf_get - Map response buffer.
1678 * @cmd: SCSI command containing buffer to be mapped.
1679 * @buf_out: Pointer to mapped area.
1681 * Maps buffer contained within SCSI command @cmd.
1683 * LOCKING:
1684 * spin_lock_irqsave(host lock)
1686 * RETURNS:
1687 * Length of response buffer.
1690 static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
1692 u8 *buf;
1693 unsigned int buflen;
1695 struct scatterlist *sg = scsi_sglist(cmd);
1697 if (sg) {
1698 buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
1699 buflen = sg->length;
1700 } else {
1701 buf = NULL;
1702 buflen = 0;
1705 *buf_out = buf;
1706 return buflen;
1710 * ata_scsi_rbuf_put - Unmap response buffer.
1711 * @cmd: SCSI command containing buffer to be unmapped.
1712 * @buf: buffer to unmap
1714 * Unmaps response buffer contained within @cmd.
1716 * LOCKING:
1717 * spin_lock_irqsave(host lock)
1720 static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
1722 struct scatterlist *sg = scsi_sglist(cmd);
1723 if (sg)
1724 kunmap_atomic(buf - sg->offset, KM_IRQ0);
1728 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1729 * @args: device IDENTIFY data / SCSI command of interest.
1730 * @actor: Callback hook for desired SCSI command simulator
1732 * Takes care of the hard work of simulating a SCSI command...
1733 * Mapping the response buffer, calling the command's handler,
1734 * and handling the handler's return value. This return value
1735 * indicates whether the handler wishes the SCSI command to be
1736 * completed successfully (0), or not (in which case cmd->result
1737 * and sense buffer are assumed to be set).
1739 * LOCKING:
1740 * spin_lock_irqsave(host lock)
1743 void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
1744 unsigned int (*actor) (struct ata_scsi_args *args,
1745 u8 *rbuf, unsigned int buflen))
1747 u8 *rbuf;
1748 unsigned int buflen, rc;
1749 struct scsi_cmnd *cmd = args->cmd;
1750 <<<<<<< HEAD:drivers/ata/libata-scsi.c
1751 =======
1752 unsigned long flags;
1754 local_irq_save(flags);
1755 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
1757 buflen = ata_scsi_rbuf_get(cmd, &rbuf);
1758 memset(rbuf, 0, buflen);
1759 rc = actor(args, rbuf, buflen);
1760 ata_scsi_rbuf_put(cmd, rbuf);
1762 <<<<<<< HEAD:drivers/ata/libata-scsi.c
1763 =======
1764 local_irq_restore(flags);
1766 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
1767 if (rc == 0)
1768 cmd->result = SAM_STAT_GOOD;
1769 args->done(cmd);
1773 * ATA_SCSI_RBUF_SET - helper to set values in SCSI response buffer
1774 * @idx: byte index into SCSI response buffer
1775 * @val: value to set
1777 * To be used by SCSI command simulator functions. This macros
1778 * expects two local variables, u8 *rbuf and unsigned int buflen,
1779 * are in scope.
1781 * LOCKING:
1782 * None.
1784 #define ATA_SCSI_RBUF_SET(idx, val) do { \
1785 if ((idx) < buflen) rbuf[(idx)] = (u8)(val); \
1786 } while (0)
1789 * ata_scsiop_inq_std - Simulate INQUIRY command
1790 * @args: device IDENTIFY data / SCSI command of interest.
1791 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1792 * @buflen: Response buffer length.
1794 * Returns standard device identification data associated
1795 * with non-VPD INQUIRY command output.
1797 * LOCKING:
1798 * spin_lock_irqsave(host lock)
1801 unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
1802 unsigned int buflen)
1804 u8 hdr[] = {
1805 TYPE_DISK,
1807 0x5, /* claim SPC-3 version compatibility */
1809 95 - 4
1812 /* set scsi removeable (RMB) bit per ata bit */
1813 if (ata_id_removeable(args->id))
1814 hdr[1] |= (1 << 7);
1816 VPRINTK("ENTER\n");
1818 memcpy(rbuf, hdr, sizeof(hdr));
1820 if (buflen > 35) {
1821 memcpy(&rbuf[8], "ATA ", 8);
1822 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
1823 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
1824 if (rbuf[32] == 0 || rbuf[32] == ' ')
1825 memcpy(&rbuf[32], "n/a ", 4);
1828 if (buflen > 63) {
1829 const u8 versions[] = {
1830 0x60, /* SAM-3 (no version claimed) */
1832 0x03,
1833 0x20, /* SBC-2 (no version claimed) */
1835 0x02,
1836 0x60 /* SPC-3 (no version claimed) */
1839 memcpy(rbuf + 59, versions, sizeof(versions));
1842 return 0;
1846 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
1847 * @args: device IDENTIFY data / SCSI command of interest.
1848 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1849 * @buflen: Response buffer length.
1851 * Returns list of inquiry VPD pages available.
1853 * LOCKING:
1854 * spin_lock_irqsave(host lock)
1857 unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
1858 unsigned int buflen)
1860 const u8 pages[] = {
1861 0x00, /* page 0x00, this page */
1862 0x80, /* page 0x80, unit serial no page */
1863 0x83 /* page 0x83, device ident page */
1865 rbuf[3] = sizeof(pages); /* number of supported VPD pages */
1867 if (buflen > 6)
1868 memcpy(rbuf + 4, pages, sizeof(pages));
1870 return 0;
1874 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
1875 * @args: device IDENTIFY data / SCSI command of interest.
1876 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1877 * @buflen: Response buffer length.
1879 * Returns ATA device serial number.
1881 * LOCKING:
1882 * spin_lock_irqsave(host lock)
1885 unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
1886 unsigned int buflen)
1888 const u8 hdr[] = {
1890 0x80, /* this page code */
1892 ATA_ID_SERNO_LEN, /* page len */
1894 memcpy(rbuf, hdr, sizeof(hdr));
1896 if (buflen > (ATA_ID_SERNO_LEN + 4 - 1))
1897 ata_id_string(args->id, (unsigned char *) &rbuf[4],
1898 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
1900 return 0;
1904 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
1905 * @args: device IDENTIFY data / SCSI command of interest.
1906 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1907 * @buflen: Response buffer length.
1909 * Yields two logical unit device identification designators:
1910 * - vendor specific ASCII containing the ATA serial number
1911 * - SAT defined "t10 vendor id based" containing ASCII vendor
1912 * name ("ATA "), model and serial numbers.
1914 * LOCKING:
1915 * spin_lock_irqsave(host lock)
1918 unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
1919 unsigned int buflen)
1921 int num;
1922 const int sat_model_serial_desc_len = 68;
1924 rbuf[1] = 0x83; /* this page code */
1925 num = 4;
1927 if (buflen > (ATA_ID_SERNO_LEN + num + 3)) {
1928 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
1929 rbuf[num + 0] = 2;
1930 rbuf[num + 3] = ATA_ID_SERNO_LEN;
1931 num += 4;
1932 ata_id_string(args->id, (unsigned char *) rbuf + num,
1933 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
1934 num += ATA_ID_SERNO_LEN;
1936 if (buflen > (sat_model_serial_desc_len + num + 3)) {
1937 /* SAT defined lu model and serial numbers descriptor */
1938 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
1939 rbuf[num + 0] = 2;
1940 rbuf[num + 1] = 1;
1941 rbuf[num + 3] = sat_model_serial_desc_len;
1942 num += 4;
1943 memcpy(rbuf + num, "ATA ", 8);
1944 num += 8;
1945 ata_id_string(args->id, (unsigned char *) rbuf + num,
1946 ATA_ID_PROD, ATA_ID_PROD_LEN);
1947 num += ATA_ID_PROD_LEN;
1948 ata_id_string(args->id, (unsigned char *) rbuf + num,
1949 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
1950 num += ATA_ID_SERNO_LEN;
1952 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
1953 return 0;
1957 * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
1958 * @args: device IDENTIFY data / SCSI command of interest.
1959 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1960 * @buflen: Response buffer length.
1962 * Yields SAT-specified ATA VPD page.
1964 * LOCKING:
1965 * spin_lock_irqsave(host lock)
1968 static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf,
1969 unsigned int buflen)
1971 u8 pbuf[60];
1972 struct ata_taskfile tf;
1973 unsigned int i;
1975 if (!buflen)
1976 return 0;
1978 memset(&pbuf, 0, sizeof(pbuf));
1979 memset(&tf, 0, sizeof(tf));
1981 pbuf[1] = 0x89; /* our page code */
1982 pbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
1983 pbuf[3] = (0x238 & 0xff);
1985 memcpy(&pbuf[8], "linux ", 8);
1986 memcpy(&pbuf[16], "libata ", 16);
1987 memcpy(&pbuf[32], DRV_VERSION, 4);
1988 ata_id_string(args->id, &pbuf[32], ATA_ID_FW_REV, 4);
1990 /* we don't store the ATA device signature, so we fake it */
1992 tf.command = ATA_DRDY; /* really, this is Status reg */
1993 tf.lbal = 0x1;
1994 tf.nsect = 0x1;
1996 ata_tf_to_fis(&tf, 0, 1, &pbuf[36]); /* TODO: PMP? */
1997 pbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
1999 pbuf[56] = ATA_CMD_ID_ATA;
2001 i = min(buflen, 60U);
2002 memcpy(rbuf, &pbuf[0], i);
2003 buflen -= i;
2005 if (!buflen)
2006 return 0;
2008 memcpy(&rbuf[60], &args->id[0], min(buflen, 512U));
2009 return 0;
2013 * ata_scsiop_noop - Command handler that simply returns success.
2014 * @args: device IDENTIFY data / SCSI command of interest.
2015 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2016 * @buflen: Response buffer length.
2018 * No operation. Simply returns success to caller, to indicate
2019 * that the caller should successfully complete this SCSI command.
2021 * LOCKING:
2022 * spin_lock_irqsave(host lock)
2025 unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
2026 unsigned int buflen)
2028 VPRINTK("ENTER\n");
2029 return 0;
2033 * ata_msense_push - Push data onto MODE SENSE data output buffer
2034 * @ptr_io: (input/output) Location to store more output data
2035 * @last: End of output data buffer
2036 * @buf: Pointer to BLOB being added to output buffer
2037 * @buflen: Length of BLOB
2039 * Store MODE SENSE data on an output buffer.
2041 * LOCKING:
2042 * None.
2045 static void ata_msense_push(u8 **ptr_io, const u8 *last,
2046 const u8 *buf, unsigned int buflen)
2048 u8 *ptr = *ptr_io;
2050 if ((ptr + buflen - 1) > last)
2051 return;
2053 memcpy(ptr, buf, buflen);
2055 ptr += buflen;
2057 *ptr_io = ptr;
2061 * ata_msense_caching - Simulate MODE SENSE caching info page
2062 * @id: device IDENTIFY data
2063 * @ptr_io: (input/output) Location to store more output data
2064 * @last: End of output data buffer
2066 * Generate a caching info page, which conditionally indicates
2067 * write caching to the SCSI layer, depending on device
2068 * capabilities.
2070 * LOCKING:
2071 * None.
2074 static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
2075 const u8 *last)
2077 u8 page[CACHE_MPAGE_LEN];
2079 memcpy(page, def_cache_mpage, sizeof(page));
2080 if (ata_id_wcache_enabled(id))
2081 page[2] |= (1 << 2); /* write cache enable */
2082 if (!ata_id_rahead_enabled(id))
2083 page[12] |= (1 << 5); /* disable read ahead */
2085 ata_msense_push(ptr_io, last, page, sizeof(page));
2086 return sizeof(page);
2090 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
2091 * @dev: Device associated with this MODE SENSE command
2092 * @ptr_io: (input/output) Location to store more output data
2093 * @last: End of output data buffer
2095 * Generate a generic MODE SENSE control mode page.
2097 * LOCKING:
2098 * None.
2101 static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
2103 ata_msense_push(ptr_io, last, def_control_mpage,
2104 sizeof(def_control_mpage));
2105 return sizeof(def_control_mpage);
2109 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
2110 * @dev: Device associated with this MODE SENSE command
2111 * @ptr_io: (input/output) Location to store more output data
2112 * @last: End of output data buffer
2114 * Generate a generic MODE SENSE r/w error recovery page.
2116 * LOCKING:
2117 * None.
2120 static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
2123 ata_msense_push(ptr_io, last, def_rw_recovery_mpage,
2124 sizeof(def_rw_recovery_mpage));
2125 return sizeof(def_rw_recovery_mpage);
2129 * We can turn this into a real blacklist if it's needed, for now just
2130 * blacklist any Maxtor BANC1G10 revision firmware
2132 static int ata_dev_supports_fua(u16 *id)
2134 unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
2136 if (!libata_fua)
2137 return 0;
2138 if (!ata_id_has_fua(id))
2139 return 0;
2141 ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
2142 ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
2144 if (strcmp(model, "Maxtor"))
2145 return 1;
2146 if (strcmp(fw, "BANC1G10"))
2147 return 1;
2149 return 0; /* blacklisted */
2153 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
2154 * @args: device IDENTIFY data / SCSI command of interest.
2155 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2156 * @buflen: Response buffer length.
2158 * Simulate MODE SENSE commands. Assume this is invoked for direct
2159 * access devices (e.g. disks) only. There should be no block
2160 * descriptor for other device types.
2162 * LOCKING:
2163 * spin_lock_irqsave(host lock)
2166 unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
2167 unsigned int buflen)
2169 struct ata_device *dev = args->dev;
2170 u8 *scsicmd = args->cmd->cmnd, *p, *last;
2171 const u8 sat_blk_desc[] = {
2172 0, 0, 0, 0, /* number of blocks: sat unspecified */
2174 0, 0x2, 0x0 /* block length: 512 bytes */
2176 u8 pg, spg;
2177 unsigned int ebd, page_control, six_byte, output_len, alloc_len, minlen;
2178 u8 dpofua;
2180 VPRINTK("ENTER\n");
2182 six_byte = (scsicmd[0] == MODE_SENSE);
2183 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
2185 * LLBA bit in msense(10) ignored (compliant)
2188 page_control = scsicmd[2] >> 6;
2189 switch (page_control) {
2190 case 0: /* current */
2191 break; /* supported */
2192 case 3: /* saved */
2193 goto saving_not_supp;
2194 case 1: /* changeable */
2195 case 2: /* defaults */
2196 default:
2197 goto invalid_fld;
2200 if (six_byte) {
2201 output_len = 4 + (ebd ? 8 : 0);
2202 alloc_len = scsicmd[4];
2203 } else {
2204 output_len = 8 + (ebd ? 8 : 0);
2205 alloc_len = (scsicmd[7] << 8) + scsicmd[8];
2207 minlen = (alloc_len < buflen) ? alloc_len : buflen;
2209 p = rbuf + output_len;
2210 last = rbuf + minlen - 1;
2212 pg = scsicmd[2] & 0x3f;
2213 spg = scsicmd[3];
2215 * No mode subpages supported (yet) but asking for _all_
2216 * subpages may be valid
2218 if (spg && (spg != ALL_SUB_MPAGES))
2219 goto invalid_fld;
2221 switch(pg) {
2222 case RW_RECOVERY_MPAGE:
2223 output_len += ata_msense_rw_recovery(&p, last);
2224 break;
2226 case CACHE_MPAGE:
2227 output_len += ata_msense_caching(args->id, &p, last);
2228 break;
2230 case CONTROL_MPAGE: {
2231 output_len += ata_msense_ctl_mode(&p, last);
2232 break;
2235 case ALL_MPAGES:
2236 output_len += ata_msense_rw_recovery(&p, last);
2237 output_len += ata_msense_caching(args->id, &p, last);
2238 output_len += ata_msense_ctl_mode(&p, last);
2239 break;
2241 default: /* invalid page code */
2242 goto invalid_fld;
2245 if (minlen < 1)
2246 return 0;
2248 dpofua = 0;
2249 if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
2250 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
2251 dpofua = 1 << 4;
2253 if (six_byte) {
2254 output_len--;
2255 rbuf[0] = output_len;
2256 if (minlen > 2)
2257 rbuf[2] |= dpofua;
2258 if (ebd) {
2259 if (minlen > 3)
2260 rbuf[3] = sizeof(sat_blk_desc);
2261 if (minlen > 11)
2262 memcpy(rbuf + 4, sat_blk_desc,
2263 sizeof(sat_blk_desc));
2265 } else {
2266 output_len -= 2;
2267 rbuf[0] = output_len >> 8;
2268 if (minlen > 1)
2269 rbuf[1] = output_len;
2270 if (minlen > 3)
2271 rbuf[3] |= dpofua;
2272 if (ebd) {
2273 if (minlen > 7)
2274 rbuf[7] = sizeof(sat_blk_desc);
2275 if (minlen > 15)
2276 memcpy(rbuf + 8, sat_blk_desc,
2277 sizeof(sat_blk_desc));
2280 return 0;
2282 invalid_fld:
2283 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
2284 /* "Invalid field in cbd" */
2285 return 1;
2287 saving_not_supp:
2288 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
2289 /* "Saving parameters not supported" */
2290 return 1;
2294 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2295 * @args: device IDENTIFY data / SCSI command of interest.
2296 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2297 * @buflen: Response buffer length.
2299 * Simulate READ CAPACITY commands.
2301 * LOCKING:
2302 * None.
2304 unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
2305 unsigned int buflen)
2307 u64 last_lba = args->dev->n_sectors - 1; /* LBA of the last block */
2309 VPRINTK("ENTER\n");
2311 if (args->cmd->cmnd[0] == READ_CAPACITY) {
2312 if (last_lba >= 0xffffffffULL)
2313 last_lba = 0xffffffff;
2315 /* sector count, 32-bit */
2316 ATA_SCSI_RBUF_SET(0, last_lba >> (8 * 3));
2317 ATA_SCSI_RBUF_SET(1, last_lba >> (8 * 2));
2318 ATA_SCSI_RBUF_SET(2, last_lba >> (8 * 1));
2319 ATA_SCSI_RBUF_SET(3, last_lba);
2321 /* sector size */
2322 ATA_SCSI_RBUF_SET(6, ATA_SECT_SIZE >> 8);
2323 ATA_SCSI_RBUF_SET(7, ATA_SECT_SIZE & 0xff);
2324 } else {
2325 /* sector count, 64-bit */
2326 ATA_SCSI_RBUF_SET(0, last_lba >> (8 * 7));
2327 ATA_SCSI_RBUF_SET(1, last_lba >> (8 * 6));
2328 ATA_SCSI_RBUF_SET(2, last_lba >> (8 * 5));
2329 ATA_SCSI_RBUF_SET(3, last_lba >> (8 * 4));
2330 ATA_SCSI_RBUF_SET(4, last_lba >> (8 * 3));
2331 ATA_SCSI_RBUF_SET(5, last_lba >> (8 * 2));
2332 ATA_SCSI_RBUF_SET(6, last_lba >> (8 * 1));
2333 ATA_SCSI_RBUF_SET(7, last_lba);
2335 /* sector size */
2336 ATA_SCSI_RBUF_SET(10, ATA_SECT_SIZE >> 8);
2337 ATA_SCSI_RBUF_SET(11, ATA_SECT_SIZE & 0xff);
2340 return 0;
2344 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2345 * @args: device IDENTIFY data / SCSI command of interest.
2346 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2347 * @buflen: Response buffer length.
2349 * Simulate REPORT LUNS command.
2351 * LOCKING:
2352 * spin_lock_irqsave(host lock)
2355 unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
2356 unsigned int buflen)
2358 VPRINTK("ENTER\n");
2359 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2361 return 0;
2365 * ata_scsi_set_sense - Set SCSI sense data and status
2366 * @cmd: SCSI request to be handled
2367 * @sk: SCSI-defined sense key
2368 * @asc: SCSI-defined additional sense code
2369 * @ascq: SCSI-defined additional sense code qualifier
2371 * Helper function that builds a valid fixed format, current
2372 * response code and the given sense key (sk), additional sense
2373 * code (asc) and additional sense code qualifier (ascq) with
2374 * a SCSI command status of %SAM_STAT_CHECK_CONDITION and
2375 * DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
2377 * LOCKING:
2378 * Not required
2381 void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
2383 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
2385 cmd->sense_buffer[0] = 0x70; /* fixed format, current */
2386 cmd->sense_buffer[2] = sk;
2387 cmd->sense_buffer[7] = 18 - 8; /* additional sense length */
2388 cmd->sense_buffer[12] = asc;
2389 cmd->sense_buffer[13] = ascq;
2393 * ata_scsi_badcmd - End a SCSI request with an error
2394 * @cmd: SCSI request to be handled
2395 * @done: SCSI command completion function
2396 * @asc: SCSI-defined additional sense code
2397 * @ascq: SCSI-defined additional sense code qualifier
2399 * Helper function that completes a SCSI command with
2400 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
2401 * and the specified additional sense codes.
2403 * LOCKING:
2404 * spin_lock_irqsave(host lock)
2407 void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
2409 DPRINTK("ENTER\n");
2410 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
2412 done(cmd);
2415 static void atapi_sense_complete(struct ata_queued_cmd *qc)
2417 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
2418 /* FIXME: not quite right; we don't want the
2419 * translation of taskfile registers into
2420 * a sense descriptors, since that's only
2421 * correct for ATA, not ATAPI
2423 ata_gen_passthru_sense(qc);
2426 qc->scsidone(qc->scsicmd);
2427 ata_qc_free(qc);
2430 /* is it pointless to prefer PIO for "safety reasons"? */
2431 static inline int ata_pio_use_silly(struct ata_port *ap)
2433 return (ap->flags & ATA_FLAG_PIO_DMA);
2436 static void atapi_request_sense(struct ata_queued_cmd *qc)
2438 struct ata_port *ap = qc->ap;
2439 struct scsi_cmnd *cmd = qc->scsicmd;
2441 DPRINTK("ATAPI request sense\n");
2443 /* FIXME: is this needed? */
2444 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
2446 ap->ops->tf_read(ap, &qc->tf);
2448 /* fill these in, for the case where they are -not- overwritten */
2449 cmd->sense_buffer[0] = 0x70;
2450 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2452 ata_qc_reinit(qc);
2454 /* setup sg table and init transfer direction */
2455 sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
2456 ata_sg_init(qc, &qc->sgent, 1);
2457 qc->dma_dir = DMA_FROM_DEVICE;
2459 memset(&qc->cdb, 0, qc->dev->cdb_len);
2460 qc->cdb[0] = REQUEST_SENSE;
2461 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2463 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2464 qc->tf.command = ATA_CMD_PACKET;
2466 if (ata_pio_use_silly(ap)) {
2467 qc->tf.protocol = ATAPI_PROT_DMA;
2468 qc->tf.feature |= ATAPI_PKT_DMA;
2469 } else {
2470 qc->tf.protocol = ATAPI_PROT_PIO;
2471 qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
2472 qc->tf.lbah = 0;
2474 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2476 qc->complete_fn = atapi_sense_complete;
2478 ata_qc_issue(qc);
2480 DPRINTK("EXIT\n");
2483 static void atapi_qc_complete(struct ata_queued_cmd *qc)
2485 struct scsi_cmnd *cmd = qc->scsicmd;
2486 unsigned int err_mask = qc->err_mask;
2488 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
2490 /* handle completion from new EH */
2491 if (unlikely(qc->ap->ops->error_handler &&
2492 (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
2494 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
2495 /* FIXME: not quite right; we don't want the
2496 * translation of taskfile registers into a
2497 * sense descriptors, since that's only
2498 * correct for ATA, not ATAPI
2500 ata_gen_passthru_sense(qc);
2503 /* SCSI EH automatically locks door if sdev->locked is
2504 * set. Sometimes door lock request continues to
2505 * fail, for example, when no media is present. This
2506 * creates a loop - SCSI EH issues door lock which
2507 * fails and gets invoked again to acquire sense data
2508 * for the failed command.
2510 * If door lock fails, always clear sdev->locked to
2511 * avoid this infinite loop.
2513 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL)
2514 qc->dev->sdev->locked = 0;
2516 qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
2517 qc->scsidone(cmd);
2518 ata_qc_free(qc);
2519 return;
2522 /* successful completion or old EH failure path */
2523 if (unlikely(err_mask & AC_ERR_DEV)) {
2524 cmd->result = SAM_STAT_CHECK_CONDITION;
2525 atapi_request_sense(qc);
2526 return;
2527 } else if (unlikely(err_mask)) {
2528 /* FIXME: not quite right; we don't want the
2529 * translation of taskfile registers into
2530 * a sense descriptors, since that's only
2531 * correct for ATA, not ATAPI
2533 ata_gen_passthru_sense(qc);
2534 } else {
2535 u8 *scsicmd = cmd->cmnd;
2537 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
2538 u8 *buf = NULL;
2539 unsigned int buflen;
2540 <<<<<<< HEAD:drivers/ata/libata-scsi.c
2541 =======
2542 unsigned long flags;
2544 local_irq_save(flags);
2545 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
2547 buflen = ata_scsi_rbuf_get(cmd, &buf);
2549 /* ATAPI devices typically report zero for their SCSI version,
2550 * and sometimes deviate from the spec WRT response data
2551 * format. If SCSI version is reported as zero like normal,
2552 * then we make the following fixups: 1) Fake MMC-5 version,
2553 * to indicate to the Linux scsi midlayer this is a modern
2554 * device. 2) Ensure response data format / ATAPI information
2555 * are always correct.
2557 if (buf[2] == 0) {
2558 buf[2] = 0x5;
2559 buf[3] = 0x32;
2562 ata_scsi_rbuf_put(cmd, buf);
2563 <<<<<<< HEAD:drivers/ata/libata-scsi.c
2564 =======
2566 local_irq_restore(flags);
2567 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
2570 cmd->result = SAM_STAT_GOOD;
2573 qc->scsidone(cmd);
2574 ata_qc_free(qc);
2577 * atapi_xlat - Initialize PACKET taskfile
2578 * @qc: command structure to be initialized
2580 * LOCKING:
2581 * spin_lock_irqsave(host lock)
2583 * RETURNS:
2584 * Zero on success, non-zero on failure.
2586 static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
2588 struct scsi_cmnd *scmd = qc->scsicmd;
2589 struct ata_device *dev = qc->dev;
2590 int using_pio = (dev->flags & ATA_DFLAG_PIO);
2591 int nodata = (scmd->sc_data_direction == DMA_NONE);
2592 unsigned int nbytes;
2594 memset(qc->cdb, 0, dev->cdb_len);
2595 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
2597 qc->complete_fn = atapi_qc_complete;
2599 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2600 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
2601 qc->tf.flags |= ATA_TFLAG_WRITE;
2602 DPRINTK("direction: write\n");
2605 qc->tf.command = ATA_CMD_PACKET;
2606 <<<<<<< HEAD:drivers/ata/libata-scsi.c
2607 qc->nbytes = scsi_bufflen(scmd);
2608 =======
2609 qc->nbytes = scsi_bufflen(scmd) + scmd->request->extra_len;
2610 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
2612 /* check whether ATAPI DMA is safe */
2613 if (!using_pio && ata_check_atapi_dma(qc))
2614 using_pio = 1;
2616 /* Some controller variants snoop this value for Packet
2617 * transfers to do state machine and FIFO management. Thus we
2618 * want to set it properly, and for DMA where it is
2619 * effectively meaningless.
2621 <<<<<<< HEAD:drivers/ata/libata-scsi.c
2622 nbytes = min(qc->nbytes, (unsigned int)63 * 1024);
2623 =======
2624 nbytes = min(scmd->request->data_len, (unsigned int)63 * 1024);
2625 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
2627 /* Most ATAPI devices which honor transfer chunk size don't
2628 * behave according to the spec when odd chunk size which
2629 * matches the transfer length is specified. If the number of
2630 * bytes to transfer is 2n+1. According to the spec, what
2631 * should happen is to indicate that 2n+1 is going to be
2632 * transferred and transfer 2n+2 bytes where the last byte is
2633 * padding.
2635 * In practice, this doesn't happen. ATAPI devices first
2636 * indicate and transfer 2n bytes and then indicate and
2637 * transfer 2 bytes where the last byte is padding.
2639 * This inconsistency confuses several controllers which
2640 * perform PIO using DMA such as Intel AHCIs and sil3124/32.
2641 * These controllers use actual number of transferred bytes to
2642 * update DMA poitner and transfer of 4n+2 bytes make those
2643 * controller push DMA pointer by 4n+4 bytes because SATA data
2644 * FISes are aligned to 4 bytes. This causes data corruption
2645 * and buffer overrun.
2647 * Always setting nbytes to even number solves this problem
2648 * because then ATAPI devices don't have to split data at 2n
2649 * boundaries.
2651 if (nbytes & 0x1)
2652 nbytes++;
2654 qc->tf.lbam = (nbytes & 0xFF);
2655 qc->tf.lbah = (nbytes >> 8);
2657 if (using_pio || nodata) {
2658 /* no data, or PIO data xfer */
2659 if (nodata)
2660 qc->tf.protocol = ATAPI_PROT_NODATA;
2661 else
2662 qc->tf.protocol = ATAPI_PROT_PIO;
2663 } else {
2664 /* DMA data xfer */
2665 qc->tf.protocol = ATAPI_PROT_DMA;
2666 qc->tf.feature |= ATAPI_PKT_DMA;
2668 <<<<<<< HEAD:drivers/ata/libata-scsi.c
2669 if (atapi_dmadir && (scmd->sc_data_direction != DMA_TO_DEVICE))
2670 =======
2671 if ((dev->flags & ATA_DFLAG_DMADIR) &&
2672 (scmd->sc_data_direction != DMA_TO_DEVICE))
2673 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
2674 /* some SATA bridges need us to indicate data xfer direction */
2675 qc->tf.feature |= ATAPI_DMADIR;
2679 /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
2680 as ATAPI tape drives don't get this right otherwise */
2681 return 0;
2684 static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
2686 if (ap->nr_pmp_links == 0) {
2687 if (likely(devno < ata_link_max_devices(&ap->link)))
2688 return &ap->link.device[devno];
2689 } else {
2690 if (likely(devno < ap->nr_pmp_links))
2691 return &ap->pmp_link[devno].device[0];
2694 return NULL;
2697 static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
2698 const struct scsi_device *scsidev)
2700 int devno;
2702 /* skip commands not addressed to targets we simulate */
2703 if (ap->nr_pmp_links == 0) {
2704 if (unlikely(scsidev->channel || scsidev->lun))
2705 return NULL;
2706 devno = scsidev->id;
2707 } else {
2708 if (unlikely(scsidev->id || scsidev->lun))
2709 return NULL;
2710 devno = scsidev->channel;
2713 return ata_find_dev(ap, devno);
2717 * ata_scsi_dev_enabled - determine if device is enabled
2718 * @dev: ATA device
2720 * Determine if commands should be sent to the specified device.
2722 * LOCKING:
2723 * spin_lock_irqsave(host lock)
2725 * RETURNS:
2726 * 0 if commands are not allowed / 1 if commands are allowed
2729 static int ata_scsi_dev_enabled(struct ata_device *dev)
2731 if (unlikely(!ata_dev_enabled(dev)))
2732 return 0;
2734 if (!atapi_enabled || (dev->link->ap->flags & ATA_FLAG_NO_ATAPI)) {
2735 if (unlikely(dev->class == ATA_DEV_ATAPI)) {
2736 ata_dev_printk(dev, KERN_WARNING,
2737 "WARNING: ATAPI is %s, device ignored.\n",
2738 atapi_enabled ? "not supported with this driver" : "disabled");
2739 return 0;
2743 return 1;
2747 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2748 * @ap: ATA port to which the device is attached
2749 * @scsidev: SCSI device from which we derive the ATA device
2751 * Given various information provided in struct scsi_cmnd,
2752 * map that onto an ATA bus, and using that mapping
2753 * determine which ata_device is associated with the
2754 * SCSI command to be sent.
2756 * LOCKING:
2757 * spin_lock_irqsave(host lock)
2759 * RETURNS:
2760 * Associated ATA device, or %NULL if not found.
2762 static struct ata_device *
2763 ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
2765 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
2767 if (unlikely(!dev || !ata_scsi_dev_enabled(dev)))
2768 return NULL;
2770 return dev;
2774 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2775 * @byte1: Byte 1 from pass-thru CDB.
2777 * RETURNS:
2778 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2780 static u8
2781 ata_scsi_map_proto(u8 byte1)
2783 switch((byte1 & 0x1e) >> 1) {
2784 case 3: /* Non-data */
2785 return ATA_PROT_NODATA;
2787 case 6: /* DMA */
2788 case 10: /* UDMA Data-in */
2789 case 11: /* UDMA Data-Out */
2790 return ATA_PROT_DMA;
2792 case 4: /* PIO Data-in */
2793 case 5: /* PIO Data-out */
2794 return ATA_PROT_PIO;
2796 case 0: /* Hard Reset */
2797 case 1: /* SRST */
2798 case 8: /* Device Diagnostic */
2799 case 9: /* Device Reset */
2800 case 7: /* DMA Queued */
2801 case 12: /* FPDMA */
2802 case 15: /* Return Response Info */
2803 default: /* Reserved */
2804 break;
2807 return ATA_PROT_UNKNOWN;
2811 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
2812 * @qc: command structure to be initialized
2814 * Handles either 12 or 16-byte versions of the CDB.
2816 * RETURNS:
2817 * Zero on success, non-zero on failure.
2819 static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
2821 struct ata_taskfile *tf = &(qc->tf);
2822 struct scsi_cmnd *scmd = qc->scsicmd;
2823 struct ata_device *dev = qc->dev;
2824 const u8 *cdb = scmd->cmnd;
2826 if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN)
2827 goto invalid_fld;
2830 * Filter TPM commands by default. These provide an
2831 * essentially uncontrolled encrypted "back door" between
2832 * applications and the disk. Set libata.allow_tpm=1 if you
2833 * have a real reason for wanting to use them. This ensures
2834 * that installed software cannot easily mess stuff up without
2835 * user intent. DVR type users will probably ship with this enabled
2836 * for movie content management.
2838 * Note that for ATA8 we can issue a DCS change and DCS freeze lock
2839 * for this and should do in future but that it is not sufficient as
2840 * DCS is an optional feature set. Thus we also do the software filter
2841 * so that we comply with the TC consortium stated goal that the user
2842 * can turn off TC features of their system.
2844 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm)
2845 goto invalid_fld;
2847 /* We may not issue DMA commands if no DMA mode is set */
2848 if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0)
2849 goto invalid_fld;
2852 * 12 and 16 byte CDBs use different offsets to
2853 * provide the various register values.
2855 if (cdb[0] == ATA_16) {
2857 * 16-byte CDB - may contain extended commands.
2859 * If that is the case, copy the upper byte register values.
2861 if (cdb[1] & 0x01) {
2862 tf->hob_feature = cdb[3];
2863 tf->hob_nsect = cdb[5];
2864 tf->hob_lbal = cdb[7];
2865 tf->hob_lbam = cdb[9];
2866 tf->hob_lbah = cdb[11];
2867 tf->flags |= ATA_TFLAG_LBA48;
2868 } else
2869 tf->flags &= ~ATA_TFLAG_LBA48;
2872 * Always copy low byte, device and command registers.
2874 tf->feature = cdb[4];
2875 tf->nsect = cdb[6];
2876 tf->lbal = cdb[8];
2877 tf->lbam = cdb[10];
2878 tf->lbah = cdb[12];
2879 tf->device = cdb[13];
2880 tf->command = cdb[14];
2881 } else {
2883 * 12-byte CDB - incapable of extended commands.
2885 tf->flags &= ~ATA_TFLAG_LBA48;
2887 tf->feature = cdb[3];
2888 tf->nsect = cdb[4];
2889 tf->lbal = cdb[5];
2890 tf->lbam = cdb[6];
2891 tf->lbah = cdb[7];
2892 tf->device = cdb[8];
2893 tf->command = cdb[9];
2896 /* enforce correct master/slave bit */
2897 tf->device = dev->devno ?
2898 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
2900 /* sanity check for pio multi commands */
2901 if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf))
2902 goto invalid_fld;
2904 if (is_multi_taskfile(tf)) {
2905 unsigned int multi_count = 1 << (cdb[1] >> 5);
2907 /* compare the passed through multi_count
2908 * with the cached multi_count of libata
2910 if (multi_count != dev->multi_count)
2911 ata_dev_printk(dev, KERN_WARNING,
2912 "invalid multi_count %u ignored\n",
2913 multi_count);
2916 /* READ/WRITE LONG use a non-standard sect_size */
2917 qc->sect_size = ATA_SECT_SIZE;
2918 switch (tf->command) {
2919 case ATA_CMD_READ_LONG:
2920 case ATA_CMD_READ_LONG_ONCE:
2921 case ATA_CMD_WRITE_LONG:
2922 case ATA_CMD_WRITE_LONG_ONCE:
2923 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1)
2924 goto invalid_fld;
2925 qc->sect_size = scsi_bufflen(scmd);
2929 * Filter SET_FEATURES - XFER MODE command -- otherwise,
2930 * SET_FEATURES - XFER MODE must be preceded/succeeded
2931 * by an update to hardware-specific registers for each
2932 * controller (i.e. the reason for ->set_piomode(),
2933 * ->set_dmamode(), and ->post_set_mode() hooks).
2935 if ((tf->command == ATA_CMD_SET_FEATURES)
2936 && (tf->feature == SETFEATURES_XFER))
2937 goto invalid_fld;
2940 * Set flags so that all registers will be written,
2941 * and pass on write indication (used for PIO/DMA
2942 * setup.)
2944 tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
2946 if (scmd->sc_data_direction == DMA_TO_DEVICE)
2947 tf->flags |= ATA_TFLAG_WRITE;
2950 * Set transfer length.
2952 * TODO: find out if we need to do more here to
2953 * cover scatter/gather case.
2955 <<<<<<< HEAD:drivers/ata/libata-scsi.c
2956 qc->nbytes = scsi_bufflen(scmd);
2957 =======
2958 qc->nbytes = scsi_bufflen(scmd) + scmd->request->extra_len;
2959 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
2961 /* request result TF and be quiet about device error */
2962 qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
2964 return 0;
2966 invalid_fld:
2967 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
2968 /* "Invalid field in cdb" */
2969 return 1;
2973 * ata_get_xlat_func - check if SCSI to ATA translation is possible
2974 * @dev: ATA device
2975 * @cmd: SCSI command opcode to consider
2977 * Look up the SCSI command given, and determine whether the
2978 * SCSI command is to be translated or simulated.
2980 * RETURNS:
2981 * Pointer to translation function if possible, %NULL if not.
2984 static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
2986 switch (cmd) {
2987 case READ_6:
2988 case READ_10:
2989 case READ_16:
2991 case WRITE_6:
2992 case WRITE_10:
2993 case WRITE_16:
2994 return ata_scsi_rw_xlat;
2996 case SYNCHRONIZE_CACHE:
2997 if (ata_try_flush_cache(dev))
2998 return ata_scsi_flush_xlat;
2999 break;
3001 case VERIFY:
3002 case VERIFY_16:
3003 return ata_scsi_verify_xlat;
3005 case ATA_12:
3006 case ATA_16:
3007 return ata_scsi_pass_thru;
3009 case START_STOP:
3010 return ata_scsi_start_stop_xlat;
3013 return NULL;
3017 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
3018 * @ap: ATA port to which the command was being sent
3019 * @cmd: SCSI command to dump
3021 * Prints the contents of a SCSI command via printk().
3024 static inline void ata_scsi_dump_cdb(struct ata_port *ap,
3025 struct scsi_cmnd *cmd)
3027 #ifdef ATA_DEBUG
3028 struct scsi_device *scsidev = cmd->device;
3029 u8 *scsicmd = cmd->cmnd;
3031 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
3032 ap->print_id,
3033 scsidev->channel, scsidev->id, scsidev->lun,
3034 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
3035 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
3036 scsicmd[8]);
3037 #endif
3040 static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
3041 void (*done)(struct scsi_cmnd *),
3042 struct ata_device *dev)
3044 u8 scsi_op = scmd->cmnd[0];
3045 ata_xlat_func_t xlat_func;
3046 int rc = 0;
3048 if (dev->class == ATA_DEV_ATA) {
3049 if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
3050 goto bad_cdb_len;
3052 xlat_func = ata_get_xlat_func(dev, scsi_op);
3053 } else {
3054 if (unlikely(!scmd->cmd_len))
3055 goto bad_cdb_len;
3057 xlat_func = NULL;
3058 if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
3059 /* relay SCSI command to ATAPI device */
3060 int len = COMMAND_SIZE(scsi_op);
3061 if (unlikely(len > scmd->cmd_len || len > dev->cdb_len))
3062 goto bad_cdb_len;
3064 xlat_func = atapi_xlat;
3065 } else {
3066 /* ATA_16 passthru, treat as an ATA command */
3067 if (unlikely(scmd->cmd_len > 16))
3068 goto bad_cdb_len;
3070 xlat_func = ata_get_xlat_func(dev, scsi_op);
3074 if (xlat_func)
3075 rc = ata_scsi_translate(dev, scmd, done, xlat_func);
3076 else
3077 ata_scsi_simulate(dev, scmd, done);
3079 return rc;
3081 bad_cdb_len:
3082 DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
3083 scmd->cmd_len, scsi_op, dev->cdb_len);
3084 scmd->result = DID_ERROR << 16;
3085 done(scmd);
3086 return 0;
3090 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
3091 * @cmd: SCSI command to be sent
3092 * @done: Completion function, called when command is complete
3094 * In some cases, this function translates SCSI commands into
3095 * ATA taskfiles, and queues the taskfiles to be sent to
3096 * hardware. In other cases, this function simulates a
3097 * SCSI device by evaluating and responding to certain
3098 * SCSI commands. This creates the overall effect of
3099 * ATA and ATAPI devices appearing as SCSI devices.
3101 * LOCKING:
3102 * Releases scsi-layer-held lock, and obtains host lock.
3104 * RETURNS:
3105 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
3106 * 0 otherwise.
3108 int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
3110 struct ata_port *ap;
3111 struct ata_device *dev;
3112 struct scsi_device *scsidev = cmd->device;
3113 struct Scsi_Host *shost = scsidev->host;
3114 int rc = 0;
3116 ap = ata_shost_to_port(shost);
3118 spin_unlock(shost->host_lock);
3119 spin_lock(ap->lock);
3121 ata_scsi_dump_cdb(ap, cmd);
3123 dev = ata_scsi_find_dev(ap, scsidev);
3124 if (likely(dev))
3125 rc = __ata_scsi_queuecmd(cmd, done, dev);
3126 else {
3127 cmd->result = (DID_BAD_TARGET << 16);
3128 done(cmd);
3131 spin_unlock(ap->lock);
3132 spin_lock(shost->host_lock);
3133 return rc;
3137 * ata_scsi_simulate - simulate SCSI command on ATA device
3138 * @dev: the target device
3139 * @cmd: SCSI command being sent to device.
3140 * @done: SCSI command completion function.
3142 * Interprets and directly executes a select list of SCSI commands
3143 * that can be handled internally.
3145 * LOCKING:
3146 * spin_lock_irqsave(host lock)
3149 void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
3150 void (*done)(struct scsi_cmnd *))
3152 struct ata_scsi_args args;
3153 const u8 *scsicmd = cmd->cmnd;
3154 u8 tmp8;
3156 args.dev = dev;
3157 args.id = dev->id;
3158 args.cmd = cmd;
3159 args.done = done;
3161 switch(scsicmd[0]) {
3162 /* TODO: worth improving? */
3163 case FORMAT_UNIT:
3164 ata_scsi_invalid_field(cmd, done);
3165 break;
3167 case INQUIRY:
3168 if (scsicmd[1] & 2) /* is CmdDt set? */
3169 ata_scsi_invalid_field(cmd, done);
3170 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
3171 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
3172 else switch (scsicmd[2]) {
3173 case 0x00:
3174 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
3175 break;
3176 case 0x80:
3177 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
3178 break;
3179 case 0x83:
3180 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
3181 break;
3182 case 0x89:
3183 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
3184 break;
3185 default:
3186 ata_scsi_invalid_field(cmd, done);
3187 break;
3189 break;
3191 case MODE_SENSE:
3192 case MODE_SENSE_10:
3193 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
3194 break;
3196 case MODE_SELECT: /* unconditionally return */
3197 case MODE_SELECT_10: /* bad-field-in-cdb */
3198 ata_scsi_invalid_field(cmd, done);
3199 break;
3201 case READ_CAPACITY:
3202 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
3203 break;
3205 case SERVICE_ACTION_IN:
3206 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
3207 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
3208 else
3209 ata_scsi_invalid_field(cmd, done);
3210 break;
3212 case REPORT_LUNS:
3213 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
3214 break;
3216 case REQUEST_SENSE:
3217 ata_scsi_set_sense(cmd, 0, 0, 0);
3218 cmd->result = (DRIVER_SENSE << 24);
3219 done(cmd);
3220 break;
3222 /* if we reach this, then writeback caching is disabled,
3223 * turning this into a no-op.
3225 case SYNCHRONIZE_CACHE:
3226 /* fall through */
3228 /* no-op's, complete with success */
3229 case REZERO_UNIT:
3230 case SEEK_6:
3231 case SEEK_10:
3232 case TEST_UNIT_READY:
3233 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
3234 break;
3236 case SEND_DIAGNOSTIC:
3237 tmp8 = scsicmd[1] & ~(1 << 3);
3238 if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4]))
3239 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
3240 else
3241 ata_scsi_invalid_field(cmd, done);
3242 break;
3244 /* all other commands */
3245 default:
3246 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
3247 /* "Invalid command operation code" */
3248 done(cmd);
3249 break;
3253 int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
3255 int i, rc;
3257 for (i = 0; i < host->n_ports; i++) {
3258 struct ata_port *ap = host->ports[i];
3259 struct Scsi_Host *shost;
3261 rc = -ENOMEM;
3262 shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
3263 if (!shost)
3264 goto err_alloc;
3266 *(struct ata_port **)&shost->hostdata[0] = ap;
3267 ap->scsi_host = shost;
3269 shost->transportt = &ata_scsi_transport_template;
3270 shost->unique_id = ap->print_id;
3271 shost->max_id = 16;
3272 shost->max_lun = 1;
3273 shost->max_channel = 1;
3274 shost->max_cmd_len = 16;
3276 /* Schedule policy is determined by ->qc_defer()
3277 * callback and it needs to see every deferred qc.
3278 * Set host_blocked to 1 to prevent SCSI midlayer from
3279 * automatically deferring requests.
3281 shost->max_host_blocked = 1;
3283 rc = scsi_add_host(ap->scsi_host, ap->host->dev);
3284 if (rc)
3285 goto err_add;
3288 return 0;
3290 err_add:
3291 scsi_host_put(host->ports[i]->scsi_host);
3292 err_alloc:
3293 while (--i >= 0) {
3294 struct Scsi_Host *shost = host->ports[i]->scsi_host;
3296 scsi_remove_host(shost);
3297 scsi_host_put(shost);
3299 return rc;
3302 void ata_scsi_scan_host(struct ata_port *ap, int sync)
3304 int tries = 5;
3305 struct ata_device *last_failed_dev = NULL;
3306 struct ata_link *link;
3307 struct ata_device *dev;
3309 if (ap->flags & ATA_FLAG_DISABLED)
3310 return;
3312 repeat:
3313 ata_port_for_each_link(link, ap) {
3314 ata_link_for_each_dev(dev, link) {
3315 struct scsi_device *sdev;
3316 int channel = 0, id = 0;
3318 if (!ata_dev_enabled(dev) || dev->sdev)
3319 continue;
3321 if (ata_is_host_link(link))
3322 id = dev->devno;
3323 else
3324 channel = link->pmp;
3326 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
3327 NULL);
3328 if (!IS_ERR(sdev)) {
3329 dev->sdev = sdev;
3330 scsi_device_put(sdev);
3335 /* If we scanned while EH was in progress or allocation
3336 * failure occurred, scan would have failed silently. Check
3337 * whether all devices are attached.
3339 ata_port_for_each_link(link, ap) {
3340 ata_link_for_each_dev(dev, link) {
3341 if (ata_dev_enabled(dev) && !dev->sdev)
3342 goto exit_loop;
3345 exit_loop:
3346 if (!link)
3347 return;
3349 /* we're missing some SCSI devices */
3350 if (sync) {
3351 /* If caller requested synchrnous scan && we've made
3352 * any progress, sleep briefly and repeat.
3354 if (dev != last_failed_dev) {
3355 msleep(100);
3356 last_failed_dev = dev;
3357 goto repeat;
3360 /* We might be failing to detect boot device, give it
3361 * a few more chances.
3363 if (--tries) {
3364 msleep(100);
3365 goto repeat;
3368 ata_port_printk(ap, KERN_ERR, "WARNING: synchronous SCSI scan "
3369 "failed without making any progress,\n"
3370 " switching to async\n");
3373 queue_delayed_work(ata_aux_wq, &ap->hotplug_task,
3374 round_jiffies_relative(HZ));
3378 * ata_scsi_offline_dev - offline attached SCSI device
3379 * @dev: ATA device to offline attached SCSI device for
3381 * This function is called from ata_eh_hotplug() and responsible
3382 * for taking the SCSI device attached to @dev offline. This
3383 * function is called with host lock which protects dev->sdev
3384 * against clearing.
3386 * LOCKING:
3387 * spin_lock_irqsave(host lock)
3389 * RETURNS:
3390 * 1 if attached SCSI device exists, 0 otherwise.
3392 int ata_scsi_offline_dev(struct ata_device *dev)
3394 if (dev->sdev) {
3395 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
3396 return 1;
3398 return 0;
3402 * ata_scsi_remove_dev - remove attached SCSI device
3403 * @dev: ATA device to remove attached SCSI device for
3405 * This function is called from ata_eh_scsi_hotplug() and
3406 * responsible for removing the SCSI device attached to @dev.
3408 * LOCKING:
3409 * Kernel thread context (may sleep).
3411 static void ata_scsi_remove_dev(struct ata_device *dev)
3413 struct ata_port *ap = dev->link->ap;
3414 struct scsi_device *sdev;
3415 unsigned long flags;
3417 /* Alas, we need to grab scan_mutex to ensure SCSI device
3418 * state doesn't change underneath us and thus
3419 * scsi_device_get() always succeeds. The mutex locking can
3420 * be removed if there is __scsi_device_get() interface which
3421 * increments reference counts regardless of device state.
3423 mutex_lock(&ap->scsi_host->scan_mutex);
3424 spin_lock_irqsave(ap->lock, flags);
3426 /* clearing dev->sdev is protected by host lock */
3427 sdev = dev->sdev;
3428 dev->sdev = NULL;
3430 if (sdev) {
3431 /* If user initiated unplug races with us, sdev can go
3432 * away underneath us after the host lock and
3433 * scan_mutex are released. Hold onto it.
3435 if (scsi_device_get(sdev) == 0) {
3436 /* The following ensures the attached sdev is
3437 * offline on return from ata_scsi_offline_dev()
3438 * regardless it wins or loses the race
3439 * against this function.
3441 scsi_device_set_state(sdev, SDEV_OFFLINE);
3442 } else {
3443 WARN_ON(1);
3444 sdev = NULL;
3448 spin_unlock_irqrestore(ap->lock, flags);
3449 mutex_unlock(&ap->scsi_host->scan_mutex);
3451 if (sdev) {
3452 ata_dev_printk(dev, KERN_INFO, "detaching (SCSI %s)\n",
3453 sdev->sdev_gendev.bus_id);
3455 scsi_remove_device(sdev);
3456 scsi_device_put(sdev);
3460 static void ata_scsi_handle_link_detach(struct ata_link *link)
3462 struct ata_port *ap = link->ap;
3463 struct ata_device *dev;
3465 ata_link_for_each_dev(dev, link) {
3466 unsigned long flags;
3468 if (!(dev->flags & ATA_DFLAG_DETACHED))
3469 continue;
3471 spin_lock_irqsave(ap->lock, flags);
3472 dev->flags &= ~ATA_DFLAG_DETACHED;
3473 spin_unlock_irqrestore(ap->lock, flags);
3475 ata_scsi_remove_dev(dev);
3480 * ata_scsi_media_change_notify - send media change event
3481 * @dev: Pointer to the disk device with media change event
3483 * Tell the block layer to send a media change notification
3484 * event.
3486 * LOCKING:
3487 * spin_lock_irqsave(host lock)
3489 void ata_scsi_media_change_notify(struct ata_device *dev)
3491 if (dev->sdev)
3492 sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
3493 GFP_ATOMIC);
3497 * ata_scsi_hotplug - SCSI part of hotplug
3498 * @work: Pointer to ATA port to perform SCSI hotplug on
3500 * Perform SCSI part of hotplug. It's executed from a separate
3501 * workqueue after EH completes. This is necessary because SCSI
3502 * hot plugging requires working EH and hot unplugging is
3503 * synchronized with hot plugging with a mutex.
3505 * LOCKING:
3506 * Kernel thread context (may sleep).
3508 void ata_scsi_hotplug(struct work_struct *work)
3510 struct ata_port *ap =
3511 container_of(work, struct ata_port, hotplug_task.work);
3512 int i;
3514 if (ap->pflags & ATA_PFLAG_UNLOADING) {
3515 DPRINTK("ENTER/EXIT - unloading\n");
3516 return;
3519 DPRINTK("ENTER\n");
3521 /* Unplug detached devices. We cannot use link iterator here
3522 * because PMP links have to be scanned even if PMP is
3523 * currently not attached. Iterate manually.
3525 ata_scsi_handle_link_detach(&ap->link);
3526 if (ap->pmp_link)
3527 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
3528 ata_scsi_handle_link_detach(&ap->pmp_link[i]);
3530 /* scan for new ones */
3531 ata_scsi_scan_host(ap, 0);
3533 DPRINTK("EXIT\n");
3537 * ata_scsi_user_scan - indication for user-initiated bus scan
3538 * @shost: SCSI host to scan
3539 * @channel: Channel to scan
3540 * @id: ID to scan
3541 * @lun: LUN to scan
3543 * This function is called when user explicitly requests bus
3544 * scan. Set probe pending flag and invoke EH.
3546 * LOCKING:
3547 * SCSI layer (we don't care)
3549 * RETURNS:
3550 * Zero.
3552 static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
3553 unsigned int id, unsigned int lun)
3555 struct ata_port *ap = ata_shost_to_port(shost);
3556 unsigned long flags;
3557 int devno, rc = 0;
3559 if (!ap->ops->error_handler)
3560 return -EOPNOTSUPP;
3562 if (lun != SCAN_WILD_CARD && lun)
3563 return -EINVAL;
3565 if (ap->nr_pmp_links == 0) {
3566 if (channel != SCAN_WILD_CARD && channel)
3567 return -EINVAL;
3568 devno = id;
3569 } else {
3570 if (id != SCAN_WILD_CARD && id)
3571 return -EINVAL;
3572 devno = channel;
3575 spin_lock_irqsave(ap->lock, flags);
3577 if (devno == SCAN_WILD_CARD) {
3578 struct ata_link *link;
3580 ata_port_for_each_link(link, ap) {
3581 struct ata_eh_info *ehi = &link->eh_info;
3582 ehi->probe_mask |= (1 << ata_link_max_devices(link)) - 1;
3583 ehi->action |= ATA_EH_SOFTRESET;
3585 } else {
3586 struct ata_device *dev = ata_find_dev(ap, devno);
3588 if (dev) {
3589 struct ata_eh_info *ehi = &dev->link->eh_info;
3590 ehi->probe_mask |= 1 << dev->devno;
3591 ehi->action |= ATA_EH_SOFTRESET;
3592 ehi->flags |= ATA_EHI_RESUME_LINK;
3593 } else
3594 rc = -EINVAL;
3597 if (rc == 0) {
3598 ata_port_schedule_eh(ap);
3599 spin_unlock_irqrestore(ap->lock, flags);
3600 ata_port_wait_eh(ap);
3601 } else
3602 spin_unlock_irqrestore(ap->lock, flags);
3604 return rc;
3608 * ata_scsi_dev_rescan - initiate scsi_rescan_device()
3609 * @work: Pointer to ATA port to perform scsi_rescan_device()
3611 * After ATA pass thru (SAT) commands are executed successfully,
3612 * libata need to propagate the changes to SCSI layer. This
3613 * function must be executed from ata_aux_wq such that sdev
3614 * attach/detach don't race with rescan.
3616 * LOCKING:
3617 * Kernel thread context (may sleep).
3619 void ata_scsi_dev_rescan(struct work_struct *work)
3621 struct ata_port *ap =
3622 container_of(work, struct ata_port, scsi_rescan_task);
3623 struct ata_link *link;
3624 struct ata_device *dev;
3625 unsigned long flags;
3627 spin_lock_irqsave(ap->lock, flags);
3629 ata_port_for_each_link(link, ap) {
3630 ata_link_for_each_dev(dev, link) {
3631 struct scsi_device *sdev = dev->sdev;
3633 if (!ata_dev_enabled(dev) || !sdev)
3634 continue;
3635 if (scsi_device_get(sdev))
3636 continue;
3638 spin_unlock_irqrestore(ap->lock, flags);
3639 scsi_rescan_device(&(sdev->sdev_gendev));
3640 scsi_device_put(sdev);
3641 spin_lock_irqsave(ap->lock, flags);
3645 spin_unlock_irqrestore(ap->lock, flags);
3649 * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
3650 * @host: ATA host container for all SAS ports
3651 * @port_info: Information from low-level host driver
3652 * @shost: SCSI host that the scsi device is attached to
3654 * LOCKING:
3655 * PCI/etc. bus probe sem.
3657 * RETURNS:
3658 * ata_port pointer on success / NULL on failure.
3661 struct ata_port *ata_sas_port_alloc(struct ata_host *host,
3662 struct ata_port_info *port_info,
3663 struct Scsi_Host *shost)
3665 struct ata_port *ap;
3667 ap = ata_port_alloc(host);
3668 if (!ap)
3669 return NULL;
3671 ap->port_no = 0;
3672 ap->lock = shost->host_lock;
3673 ap->pio_mask = port_info->pio_mask;
3674 ap->mwdma_mask = port_info->mwdma_mask;
3675 ap->udma_mask = port_info->udma_mask;
3676 ap->flags |= port_info->flags;
3677 ap->ops = port_info->port_ops;
3678 ap->cbl = ATA_CBL_SATA;
3680 return ap;
3682 EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
3685 * ata_sas_port_start - Set port up for dma.
3686 * @ap: Port to initialize
3688 * Called just after data structures for each port are
3689 <<<<<<< HEAD:drivers/ata/libata-scsi.c
3690 * initialized. Allocates DMA pad.
3691 =======
3692 * initialized.
3693 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
3695 * May be used as the port_start() entry in ata_port_operations.
3697 * LOCKING:
3698 * Inherited from caller.
3700 int ata_sas_port_start(struct ata_port *ap)
3702 <<<<<<< HEAD:drivers/ata/libata-scsi.c
3703 return ata_pad_alloc(ap, ap->dev);
3704 =======
3705 return 0;
3706 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
3708 EXPORT_SYMBOL_GPL(ata_sas_port_start);
3711 * ata_port_stop - Undo ata_sas_port_start()
3712 * @ap: Port to shut down
3714 <<<<<<< HEAD:drivers/ata/libata-scsi.c
3715 * Frees the DMA pad.
3717 =======
3718 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
3719 * May be used as the port_stop() entry in ata_port_operations.
3721 * LOCKING:
3722 * Inherited from caller.
3725 void ata_sas_port_stop(struct ata_port *ap)
3727 <<<<<<< HEAD:drivers/ata/libata-scsi.c
3728 ata_pad_free(ap, ap->dev);
3729 =======
3730 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/ata/libata-scsi.c
3732 EXPORT_SYMBOL_GPL(ata_sas_port_stop);
3735 * ata_sas_port_init - Initialize a SATA device
3736 * @ap: SATA port to initialize
3738 * LOCKING:
3739 * PCI/etc. bus probe sem.
3741 * RETURNS:
3742 * Zero on success, non-zero on error.
3745 int ata_sas_port_init(struct ata_port *ap)
3747 int rc = ap->ops->port_start(ap);
3749 if (!rc) {
3750 ap->print_id = ata_print_id++;
3751 rc = ata_bus_probe(ap);
3754 return rc;
3756 EXPORT_SYMBOL_GPL(ata_sas_port_init);
3759 * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
3760 * @ap: SATA port to destroy
3764 void ata_sas_port_destroy(struct ata_port *ap)
3766 if (ap->ops->port_stop)
3767 ap->ops->port_stop(ap);
3768 kfree(ap);
3770 EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
3773 * ata_sas_slave_configure - Default slave_config routine for libata devices
3774 * @sdev: SCSI device to configure
3775 * @ap: ATA port to which SCSI device is attached
3777 * RETURNS:
3778 * Zero.
3781 int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
3783 ata_scsi_sdev_config(sdev);
3784 ata_scsi_dev_config(sdev, ap->link.device);
3785 return 0;
3787 EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
3790 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
3791 * @cmd: SCSI command to be sent
3792 * @done: Completion function, called when command is complete
3793 * @ap: ATA port to which the command is being sent
3795 * RETURNS:
3796 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
3797 * 0 otherwise.
3800 int ata_sas_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *),
3801 struct ata_port *ap)
3803 int rc = 0;
3805 ata_scsi_dump_cdb(ap, cmd);
3807 if (likely(ata_scsi_dev_enabled(ap->link.device)))
3808 rc = __ata_scsi_queuecmd(cmd, done, ap->link.device);
3809 else {
3810 cmd->result = (DID_BAD_TARGET << 16);
3811 done(cmd);
3813 return rc;
3815 EXPORT_SYMBOL_GPL(ata_sas_queuecmd);