Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Jeff Garzik | af36d7f | 2005-08-28 20:18:39 -0400 | [diff] [blame] | 2 | * libata-scsi.c - helper library for ATA |
| 3 | * |
| 4 | * Maintained by: Jeff Garzik <jgarzik@pobox.com> |
| 5 | * Please ALWAYS copy linux-ide@vger.kernel.org |
| 6 | * on emails. |
| 7 | * |
| 8 | * Copyright 2003-2004 Red Hat, Inc. All rights reserved. |
| 9 | * Copyright 2003-2004 Jeff Garzik |
| 10 | * |
| 11 | * |
| 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. |
| 16 | * |
| 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. |
| 21 | * |
| 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. |
| 25 | * |
| 26 | * |
| 27 | * libata documentation is available via 'make {ps|pdf}docs', |
| 28 | * as Documentation/DocBook/libata.* |
| 29 | * |
| 30 | * Hardware documentation available from |
| 31 | * - http://www.t10.org/ |
| 32 | * - http://www.t13.org/ |
| 33 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | */ |
| 35 | |
| 36 | #include <linux/kernel.h> |
| 37 | #include <linux/blkdev.h> |
| 38 | #include <linux/spinlock.h> |
| 39 | #include <scsi/scsi.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | #include <scsi/scsi_host.h> |
Mike Christie | 85837eb | 2005-11-11 16:38:53 -0600 | [diff] [blame] | 41 | #include <scsi/scsi_eh.h> |
Jeff Garzik | 005a5a0 | 2005-10-30 23:31:48 -0500 | [diff] [blame] | 42 | #include <scsi/scsi_device.h> |
Jeff Garzik | 193515d | 2005-11-07 00:59:37 -0500 | [diff] [blame] | 43 | #include <scsi/scsi_request.h> |
Tejun Heo | 30afc84 | 2006-03-18 18:40:14 +0900 | [diff] [blame] | 44 | #include <scsi/scsi_transport.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <linux/libata.h> |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 46 | #include <linux/hdreg.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | #include <asm/uaccess.h> |
| 48 | |
| 49 | #include "libata.h" |
| 50 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 51 | #define SECTOR_SIZE 512 |
| 52 | |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 53 | typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, const u8 *scsicmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | static struct ata_device * |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 55 | ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 57 | #define RW_RECOVERY_MPAGE 0x1 |
| 58 | #define RW_RECOVERY_MPAGE_LEN 12 |
| 59 | #define CACHE_MPAGE 0x8 |
| 60 | #define CACHE_MPAGE_LEN 20 |
| 61 | #define CONTROL_MPAGE 0xa |
| 62 | #define CONTROL_MPAGE_LEN 12 |
| 63 | #define ALL_MPAGES 0x3f |
| 64 | #define ALL_SUB_MPAGES 0xff |
| 65 | |
| 66 | |
| 67 | static const u8 def_rw_recovery_mpage[] = { |
| 68 | RW_RECOVERY_MPAGE, |
| 69 | RW_RECOVERY_MPAGE_LEN - 2, |
| 70 | (1 << 7) | /* AWRE, sat-r06 say it shall be 0 */ |
| 71 | (1 << 6), /* ARRE (auto read reallocation) */ |
| 72 | 0, /* read retry count */ |
| 73 | 0, 0, 0, 0, |
| 74 | 0, /* write retry count */ |
| 75 | 0, 0, 0 |
| 76 | }; |
| 77 | |
| 78 | static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = { |
| 79 | CACHE_MPAGE, |
| 80 | CACHE_MPAGE_LEN - 2, |
| 81 | 0, /* contains WCE, needs to be 0 for logic */ |
| 82 | 0, 0, 0, 0, 0, 0, 0, 0, 0, |
| 83 | 0, /* contains DRA, needs to be 0 for logic */ |
| 84 | 0, 0, 0, 0, 0, 0, 0 |
| 85 | }; |
| 86 | |
| 87 | static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = { |
| 88 | CONTROL_MPAGE, |
| 89 | CONTROL_MPAGE_LEN - 2, |
| 90 | 2, /* DSENSE=0, GLTSD=1 */ |
| 91 | 0, /* [QAM+QERR may be 1, see 05-359r1] */ |
| 92 | 0, 0, 0, 0, 0xff, 0xff, |
| 93 | 0, 30 /* extended self test time, see 05-359r1 */ |
| 94 | }; |
| 95 | |
Tejun Heo | 30afc84 | 2006-03-18 18:40:14 +0900 | [diff] [blame] | 96 | /* |
| 97 | * libata transport template. libata doesn't do real transport stuff. |
| 98 | * It just needs the eh_timed_out hook. |
| 99 | */ |
| 100 | struct scsi_transport_template ata_scsi_transport_template = { |
Christoph Hellwig | 9227c33 | 2006-04-01 19:21:04 +0200 | [diff] [blame] | 101 | .eh_strategy_handler = ata_scsi_error, |
Tejun Heo | 30afc84 | 2006-03-18 18:40:14 +0900 | [diff] [blame] | 102 | .eh_timed_out = ata_scsi_timed_out, |
| 103 | }; |
| 104 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 106 | static void ata_scsi_invalid_field(struct scsi_cmnd *cmd, |
| 107 | void (*done)(struct scsi_cmnd *)) |
| 108 | { |
| 109 | ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0); |
| 110 | /* "Invalid field in cbd" */ |
| 111 | done(cmd); |
| 112 | } |
| 113 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | /** |
| 115 | * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd. |
| 116 | * @sdev: SCSI device for which BIOS geometry is to be determined |
| 117 | * @bdev: block device associated with @sdev |
| 118 | * @capacity: capacity of SCSI device |
| 119 | * @geom: location to which geometry will be output |
| 120 | * |
| 121 | * Generic bios head/sector/cylinder calculator |
| 122 | * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS) |
| 123 | * mapping. Some situations may arise where the disk is not |
| 124 | * bootable if this is not used. |
| 125 | * |
| 126 | * LOCKING: |
| 127 | * Defined by the SCSI layer. We don't really care. |
| 128 | * |
| 129 | * RETURNS: |
| 130 | * Zero. |
| 131 | */ |
| 132 | int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev, |
| 133 | sector_t capacity, int geom[]) |
| 134 | { |
| 135 | geom[0] = 255; |
| 136 | geom[1] = 63; |
| 137 | sector_div(capacity, 255*63); |
| 138 | geom[2] = capacity; |
| 139 | |
| 140 | return 0; |
| 141 | } |
| 142 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 143 | /** |
| 144 | * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl |
Randy Dunlap | 8e8b77d | 2005-11-01 21:29:27 -0800 | [diff] [blame] | 145 | * @scsidev: Device to which we are issuing command |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 146 | * @arg: User provided data for issuing command |
| 147 | * |
| 148 | * LOCKING: |
| 149 | * Defined by the SCSI layer. We don't really care. |
| 150 | * |
| 151 | * RETURNS: |
| 152 | * Zero on success, negative errno on error. |
| 153 | */ |
| 154 | |
| 155 | int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg) |
| 156 | { |
| 157 | int rc = 0; |
| 158 | u8 scsi_cmd[MAX_COMMAND_SIZE]; |
| 159 | u8 args[4], *argbuf = NULL; |
| 160 | int argsize = 0; |
Mike Christie | 85837eb | 2005-11-11 16:38:53 -0600 | [diff] [blame] | 161 | struct scsi_sense_hdr sshdr; |
| 162 | enum dma_data_direction data_dir; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 163 | |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 164 | if (arg == NULL) |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 165 | return -EINVAL; |
| 166 | |
| 167 | if (copy_from_user(args, arg, sizeof(args))) |
| 168 | return -EFAULT; |
| 169 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 170 | memset(scsi_cmd, 0, sizeof(scsi_cmd)); |
| 171 | |
| 172 | if (args[3]) { |
| 173 | argsize = SECTOR_SIZE * args[3]; |
| 174 | argbuf = kmalloc(argsize, GFP_KERNEL); |
Jeff Raubitschek | 54dac83 | 2005-10-04 10:21:19 -0400 | [diff] [blame] | 175 | if (argbuf == NULL) { |
| 176 | rc = -ENOMEM; |
| 177 | goto error; |
| 178 | } |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 179 | |
| 180 | scsi_cmd[1] = (4 << 1); /* PIO Data-in */ |
| 181 | scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev, |
| 182 | block count in sector count field */ |
Mike Christie | 85837eb | 2005-11-11 16:38:53 -0600 | [diff] [blame] | 183 | data_dir = DMA_FROM_DEVICE; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 184 | } else { |
| 185 | scsi_cmd[1] = (3 << 1); /* Non-data */ |
| 186 | /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */ |
Mike Christie | 85837eb | 2005-11-11 16:38:53 -0600 | [diff] [blame] | 187 | data_dir = DMA_NONE; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | scsi_cmd[0] = ATA_16; |
| 191 | |
| 192 | scsi_cmd[4] = args[2]; |
| 193 | if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */ |
| 194 | scsi_cmd[6] = args[3]; |
| 195 | scsi_cmd[8] = args[1]; |
| 196 | scsi_cmd[10] = 0x4f; |
| 197 | scsi_cmd[12] = 0xc2; |
| 198 | } else { |
| 199 | scsi_cmd[6] = args[1]; |
| 200 | } |
| 201 | scsi_cmd[14] = args[0]; |
| 202 | |
| 203 | /* Good values for timeout and retries? Values below |
| 204 | from scsi_ioctl_send_command() for default case... */ |
Mike Christie | 85837eb | 2005-11-11 16:38:53 -0600 | [diff] [blame] | 205 | if (scsi_execute_req(scsidev, scsi_cmd, data_dir, argbuf, argsize, |
| 206 | &sshdr, (10*HZ), 5)) { |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 207 | rc = -EIO; |
| 208 | goto error; |
| 209 | } |
| 210 | |
| 211 | /* Need code to retrieve data from check condition? */ |
| 212 | |
| 213 | if ((argbuf) |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 214 | && copy_to_user(arg + sizeof(args), argbuf, argsize)) |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 215 | rc = -EFAULT; |
| 216 | error: |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 217 | if (argbuf) |
| 218 | kfree(argbuf); |
| 219 | |
| 220 | return rc; |
| 221 | } |
| 222 | |
| 223 | /** |
| 224 | * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl |
Randy Dunlap | 8e8b77d | 2005-11-01 21:29:27 -0800 | [diff] [blame] | 225 | * @scsidev: Device to which we are issuing command |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 226 | * @arg: User provided data for issuing command |
| 227 | * |
| 228 | * LOCKING: |
| 229 | * Defined by the SCSI layer. We don't really care. |
| 230 | * |
| 231 | * RETURNS: |
| 232 | * Zero on success, negative errno on error. |
| 233 | */ |
| 234 | int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg) |
| 235 | { |
| 236 | int rc = 0; |
| 237 | u8 scsi_cmd[MAX_COMMAND_SIZE]; |
| 238 | u8 args[7]; |
Mike Christie | 85837eb | 2005-11-11 16:38:53 -0600 | [diff] [blame] | 239 | struct scsi_sense_hdr sshdr; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 240 | |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 241 | if (arg == NULL) |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 242 | return -EINVAL; |
| 243 | |
| 244 | if (copy_from_user(args, arg, sizeof(args))) |
| 245 | return -EFAULT; |
| 246 | |
| 247 | memset(scsi_cmd, 0, sizeof(scsi_cmd)); |
| 248 | scsi_cmd[0] = ATA_16; |
| 249 | scsi_cmd[1] = (3 << 1); /* Non-data */ |
| 250 | /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */ |
| 251 | scsi_cmd[4] = args[1]; |
| 252 | scsi_cmd[6] = args[2]; |
| 253 | scsi_cmd[8] = args[3]; |
| 254 | scsi_cmd[10] = args[4]; |
| 255 | scsi_cmd[12] = args[5]; |
| 256 | scsi_cmd[14] = args[0]; |
| 257 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 258 | /* Good values for timeout and retries? Values below |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 259 | from scsi_ioctl_send_command() for default case... */ |
Mike Christie | 85837eb | 2005-11-11 16:38:53 -0600 | [diff] [blame] | 260 | if (scsi_execute_req(scsidev, scsi_cmd, DMA_NONE, NULL, 0, &sshdr, |
| 261 | (10*HZ), 5)) |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 262 | rc = -EIO; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 263 | |
| 264 | /* Need code to retrieve data from check condition? */ |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 265 | return rc; |
| 266 | } |
| 267 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg) |
| 269 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 270 | int val = -EINVAL, rc = -EINVAL; |
| 271 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | switch (cmd) { |
| 273 | case ATA_IOC_GET_IO32: |
| 274 | val = 0; |
| 275 | if (copy_to_user(arg, &val, 1)) |
| 276 | return -EFAULT; |
| 277 | return 0; |
| 278 | |
| 279 | case ATA_IOC_SET_IO32: |
| 280 | val = (unsigned long) arg; |
| 281 | if (val != 0) |
| 282 | return -EINVAL; |
| 283 | return 0; |
| 284 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 285 | case HDIO_DRIVE_CMD: |
| 286 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) |
| 287 | return -EACCES; |
| 288 | return ata_cmd_ioctl(scsidev, arg); |
| 289 | |
| 290 | case HDIO_DRIVE_TASK: |
| 291 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) |
| 292 | return -EACCES; |
| 293 | return ata_task_ioctl(scsidev, arg); |
| 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | default: |
| 296 | rc = -ENOTTY; |
| 297 | break; |
| 298 | } |
| 299 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | return rc; |
| 301 | } |
| 302 | |
| 303 | /** |
| 304 | * ata_scsi_qc_new - acquire new ata_queued_cmd reference |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | * @dev: ATA device to which the new command is attached |
| 306 | * @cmd: SCSI command that originated this ATA command |
| 307 | * @done: SCSI command completion function |
| 308 | * |
| 309 | * Obtain a reference to an unused ata_queued_cmd structure, |
| 310 | * which is the basic libata structure representing a single |
| 311 | * ATA command sent to the hardware. |
| 312 | * |
| 313 | * If a command was available, fill in the SCSI-specific |
| 314 | * portions of the structure with information on the |
| 315 | * current command. |
| 316 | * |
| 317 | * LOCKING: |
| 318 | * spin_lock_irqsave(host_set lock) |
| 319 | * |
| 320 | * RETURNS: |
| 321 | * Command allocated, or %NULL if none available. |
| 322 | */ |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 323 | struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | struct scsi_cmnd *cmd, |
| 325 | void (*done)(struct scsi_cmnd *)) |
| 326 | { |
| 327 | struct ata_queued_cmd *qc; |
| 328 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 329 | qc = ata_qc_new_init(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | if (qc) { |
| 331 | qc->scsicmd = cmd; |
| 332 | qc->scsidone = done; |
| 333 | |
| 334 | if (cmd->use_sg) { |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 335 | qc->__sg = (struct scatterlist *) cmd->request_buffer; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | qc->n_elem = cmd->use_sg; |
| 337 | } else { |
Jeff Garzik | cedc9a4 | 2005-10-05 07:13:30 -0400 | [diff] [blame] | 338 | qc->__sg = &qc->sgent; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 339 | qc->n_elem = 1; |
| 340 | } |
| 341 | } else { |
| 342 | cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1); |
| 343 | done(cmd); |
| 344 | } |
| 345 | |
| 346 | return qc; |
| 347 | } |
| 348 | |
| 349 | /** |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 350 | * ata_dump_status - user friendly display of error info |
| 351 | * @id: id of the port in question |
| 352 | * @tf: ptr to filled out taskfile |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | * |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 354 | * Decode and dump the ATA error/status registers for the user so |
| 355 | * that they have some idea what really happened at the non |
| 356 | * make-believe layer. |
| 357 | * |
| 358 | * LOCKING: |
| 359 | * inherited from caller |
| 360 | */ |
| 361 | void ata_dump_status(unsigned id, struct ata_taskfile *tf) |
| 362 | { |
| 363 | u8 stat = tf->command, err = tf->feature; |
| 364 | |
| 365 | printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat); |
| 366 | if (stat & ATA_BUSY) { |
| 367 | printk("Busy }\n"); /* Data is not valid in this case */ |
| 368 | } else { |
| 369 | if (stat & 0x40) printk("DriveReady "); |
| 370 | if (stat & 0x20) printk("DeviceFault "); |
| 371 | if (stat & 0x10) printk("SeekComplete "); |
| 372 | if (stat & 0x08) printk("DataRequest "); |
| 373 | if (stat & 0x04) printk("CorrectedError "); |
| 374 | if (stat & 0x02) printk("Index "); |
| 375 | if (stat & 0x01) printk("Error "); |
| 376 | printk("}\n"); |
| 377 | |
| 378 | if (err) { |
| 379 | printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err); |
| 380 | if (err & 0x04) printk("DriveStatusError "); |
| 381 | if (err & 0x80) { |
| 382 | if (err & 0x04) printk("BadCRC "); |
| 383 | else printk("Sector "); |
| 384 | } |
| 385 | if (err & 0x40) printk("UncorrectableError "); |
| 386 | if (err & 0x10) printk("SectorIdNotFound "); |
| 387 | if (err & 0x02) printk("TrackZeroNotFound "); |
| 388 | if (err & 0x01) printk("AddrMarkNotFound "); |
| 389 | printk("}\n"); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 394 | int ata_scsi_device_resume(struct scsi_device *sdev) |
| 395 | { |
Jeff Garzik | 35bb94b | 2006-04-11 13:12:34 -0400 | [diff] [blame] | 396 | struct ata_port *ap = ata_shost_to_port(sdev->host); |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 397 | struct ata_device *dev = &ap->device[sdev->id]; |
| 398 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 399 | return ata_device_resume(dev); |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 400 | } |
| 401 | |
Nigel Cunningham | 082776e | 2006-03-23 23:22:16 +1000 | [diff] [blame] | 402 | int ata_scsi_device_suspend(struct scsi_device *sdev, pm_message_t state) |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 403 | { |
Jeff Garzik | 35bb94b | 2006-04-11 13:12:34 -0400 | [diff] [blame] | 404 | struct ata_port *ap = ata_shost_to_port(sdev->host); |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 405 | struct ata_device *dev = &ap->device[sdev->id]; |
| 406 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 407 | return ata_device_suspend(dev, state); |
Jens Axboe | 9b84754 | 2006-01-06 09:28:07 +0100 | [diff] [blame] | 408 | } |
| 409 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 410 | /** |
| 411 | * ata_to_sense_error - convert ATA error to SCSI error |
Randy Dunlap | 8e8b77d | 2005-11-01 21:29:27 -0800 | [diff] [blame] | 412 | * @id: ATA device number |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 413 | * @drv_stat: value contained in ATA status register |
| 414 | * @drv_err: value contained in ATA error register |
| 415 | * @sk: the sense key we'll fill out |
| 416 | * @asc: the additional sense code we'll fill out |
| 417 | * @ascq: the additional sense code qualifier we'll fill out |
| 418 | * |
| 419 | * Converts an ATA error into a SCSI error. Fill out pointers to |
| 420 | * SK, ASC, and ASCQ bytes for later use in fixed or descriptor |
| 421 | * format sense blocks. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | * |
| 423 | * LOCKING: |
| 424 | * spin_lock_irqsave(host_set lock) |
| 425 | */ |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 426 | void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc, |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 427 | u8 *ascq) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | { |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 429 | int i; |
Jeff Garzik | ffe75ef | 2005-10-09 10:40:44 -0400 | [diff] [blame] | 430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | /* Based on the 3ware driver translation table */ |
Arjan van de Ven | 98ac62d | 2005-11-28 10:06:23 +0100 | [diff] [blame] | 432 | static const unsigned char sense_table[][4] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | /* BBD|ECC|ID|MAR */ |
| 434 | {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command |
| 435 | /* BBD|ECC|ID */ |
| 436 | {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command |
| 437 | /* ECC|MC|MARK */ |
| 438 | {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error |
| 439 | /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */ |
| 440 | {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error |
| 441 | /* MC|ID|ABRT|TRK0|MARK */ |
| 442 | {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready |
| 443 | /* MCR|MARK */ |
| 444 | {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready |
| 445 | /* Bad address mark */ |
| 446 | {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field |
| 447 | /* TRK0 */ |
| 448 | {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error |
| 449 | /* Abort & !ICRC */ |
| 450 | {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command |
| 451 | /* Media change request */ |
| 452 | {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline |
| 453 | /* SRV */ |
| 454 | {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found |
| 455 | /* Media change */ |
| 456 | {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline |
| 457 | /* ECC */ |
| 458 | {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error |
| 459 | /* BBD - block marked bad */ |
| 460 | {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error |
| 461 | {0xFF, 0xFF, 0xFF, 0xFF}, // END mark |
| 462 | }; |
Arjan van de Ven | 98ac62d | 2005-11-28 10:06:23 +0100 | [diff] [blame] | 463 | static const unsigned char stat_table[][4] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | /* Must be first because BUSY means no other bits valid */ |
| 465 | {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now |
| 466 | {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault |
| 467 | {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now |
| 468 | {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered |
| 469 | {0xFF, 0xFF, 0xFF, 0xFF}, // END mark |
| 470 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | /* |
| 473 | * Is this an error we can process/parse |
| 474 | */ |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 475 | if (drv_stat & ATA_BUSY) { |
| 476 | drv_err = 0; /* Ignore the err bits, they're invalid */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 479 | if (drv_err) { |
| 480 | /* Look for drv_err */ |
| 481 | for (i = 0; sense_table[i][0] != 0xFF; i++) { |
| 482 | /* Look for best matches first */ |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 483 | if ((sense_table[i][0] & drv_err) == |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 484 | sense_table[i][0]) { |
| 485 | *sk = sense_table[i][1]; |
| 486 | *asc = sense_table[i][2]; |
| 487 | *ascq = sense_table[i][3]; |
| 488 | goto translate_done; |
| 489 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | } |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 491 | /* No immediate match */ |
| 492 | printk(KERN_WARNING "ata%u: no sense translation for " |
| 493 | "error 0x%02x\n", id, drv_err); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | } |
| 495 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | /* Fall back to interpreting status bits */ |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 497 | for (i = 0; stat_table[i][0] != 0xFF; i++) { |
| 498 | if (stat_table[i][0] & drv_stat) { |
| 499 | *sk = stat_table[i][1]; |
| 500 | *asc = stat_table[i][2]; |
| 501 | *ascq = stat_table[i][3]; |
| 502 | goto translate_done; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | } |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 505 | /* No error? Undecoded? */ |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 506 | printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n", |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 507 | id, drv_stat); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
Alan Cox | 2d20202 | 2006-03-21 15:53:46 +0000 | [diff] [blame] | 509 | /* We need a sensible error return here, which is tricky, and one |
| 510 | that won't cause people to do things like return a disk wrongly */ |
| 511 | *sk = ABORTED_COMMAND; |
| 512 | *asc = 0x00; |
| 513 | *ascq = 0x00; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 514 | |
| 515 | translate_done: |
| 516 | printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to " |
| 517 | "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err, |
| 518 | *sk, *asc, *ascq); |
| 519 | return; |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * ata_gen_ata_desc_sense - Generate check condition sense block. |
| 524 | * @qc: Command that completed. |
| 525 | * |
| 526 | * This function is specific to the ATA descriptor format sense |
| 527 | * block specified for the ATA pass through commands. Regardless |
| 528 | * of whether the command errored or not, return a sense |
| 529 | * block. Copy all controller registers into the sense |
| 530 | * block. Clear sense key, ASC & ASCQ if there is no error. |
| 531 | * |
| 532 | * LOCKING: |
| 533 | * spin_lock_irqsave(host_set lock) |
| 534 | */ |
| 535 | void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc) |
| 536 | { |
| 537 | struct scsi_cmnd *cmd = qc->scsicmd; |
Tejun Heo | e61e067 | 2006-05-15 20:57:40 +0900 | [diff] [blame] | 538 | struct ata_taskfile *tf = &qc->result_tf; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 539 | unsigned char *sb = cmd->sense_buffer; |
| 540 | unsigned char *desc = sb + 8; |
| 541 | |
| 542 | memset(sb, 0, SCSI_SENSE_BUFFERSIZE); |
| 543 | |
Jeff Garzik | 0e5dec4 | 2005-10-06 09:40:20 -0400 | [diff] [blame] | 544 | cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 545 | |
| 546 | /* |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 547 | * Use ata_to_sense_error() to map status register bits |
| 548 | * onto sense key, asc & ascq. |
| 549 | */ |
Tejun Heo | 058e55e | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 550 | if (qc->err_mask || |
| 551 | tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) { |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 552 | ata_to_sense_error(qc->ap->id, tf->command, tf->feature, |
| 553 | &sb[1], &sb[2], &sb[3]); |
| 554 | sb[1] &= 0x0f; |
| 555 | } |
| 556 | |
| 557 | /* |
| 558 | * Sense data is current and format is descriptor. |
| 559 | */ |
| 560 | sb[0] = 0x72; |
| 561 | |
| 562 | desc[0] = 0x09; |
| 563 | |
| 564 | /* |
| 565 | * Set length of additional sense data. |
| 566 | * Since we only populate descriptor 0, the total |
| 567 | * length is the same (fixed) length as descriptor 0. |
| 568 | */ |
| 569 | desc[1] = sb[7] = 14; |
| 570 | |
| 571 | /* |
| 572 | * Copy registers into sense buffer. |
| 573 | */ |
| 574 | desc[2] = 0x00; |
| 575 | desc[3] = tf->feature; /* == error reg */ |
| 576 | desc[5] = tf->nsect; |
| 577 | desc[7] = tf->lbal; |
| 578 | desc[9] = tf->lbam; |
| 579 | desc[11] = tf->lbah; |
| 580 | desc[12] = tf->device; |
| 581 | desc[13] = tf->command; /* == status reg */ |
| 582 | |
| 583 | /* |
| 584 | * Fill in Extend bit, and the high order bytes |
| 585 | * if applicable. |
| 586 | */ |
| 587 | if (tf->flags & ATA_TFLAG_LBA48) { |
| 588 | desc[2] |= 0x01; |
| 589 | desc[4] = tf->hob_nsect; |
| 590 | desc[6] = tf->hob_lbal; |
| 591 | desc[8] = tf->hob_lbam; |
| 592 | desc[10] = tf->hob_lbah; |
| 593 | } |
| 594 | } |
| 595 | |
| 596 | /** |
| 597 | * ata_gen_fixed_sense - generate a SCSI fixed sense block |
| 598 | * @qc: Command that we are erroring out |
| 599 | * |
| 600 | * Leverage ata_to_sense_error() to give us the codes. Fit our |
| 601 | * LBA in here if there's room. |
| 602 | * |
| 603 | * LOCKING: |
| 604 | * inherited from caller |
| 605 | */ |
| 606 | void ata_gen_fixed_sense(struct ata_queued_cmd *qc) |
| 607 | { |
| 608 | struct scsi_cmnd *cmd = qc->scsicmd; |
Tejun Heo | e61e067 | 2006-05-15 20:57:40 +0900 | [diff] [blame] | 609 | struct ata_taskfile *tf = &qc->result_tf; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 610 | unsigned char *sb = cmd->sense_buffer; |
| 611 | |
| 612 | memset(sb, 0, SCSI_SENSE_BUFFERSIZE); |
| 613 | |
Jeff Garzik | 0e5dec4 | 2005-10-06 09:40:20 -0400 | [diff] [blame] | 614 | cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 615 | |
| 616 | /* |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 617 | * Use ata_to_sense_error() to map status register bits |
| 618 | * onto sense key, asc & ascq. |
| 619 | */ |
Tejun Heo | 058e55e | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 620 | if (qc->err_mask || |
| 621 | tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) { |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 622 | ata_to_sense_error(qc->ap->id, tf->command, tf->feature, |
| 623 | &sb[2], &sb[12], &sb[13]); |
| 624 | sb[2] &= 0x0f; |
| 625 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | |
| 627 | sb[0] = 0x70; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 628 | sb[7] = 0x0a; |
Jeff Garzik | 0274aa2 | 2005-06-22 13:50:56 -0400 | [diff] [blame] | 629 | |
Jeff Garzik | a7dac44 | 2005-10-30 04:44:42 -0500 | [diff] [blame] | 630 | if (tf->flags & ATA_TFLAG_LBA48) { |
| 631 | /* TODO: find solution for LBA48 descriptors */ |
| 632 | } |
| 633 | |
| 634 | else if (tf->flags & ATA_TFLAG_LBA) { |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 635 | /* A small (28b) LBA will fit in the 32b info field */ |
| 636 | sb[0] |= 0x80; /* set valid bit */ |
| 637 | sb[3] = tf->device & 0x0f; |
| 638 | sb[4] = tf->lbah; |
| 639 | sb[5] = tf->lbam; |
| 640 | sb[6] = tf->lbal; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 641 | } |
Jeff Garzik | a7dac44 | 2005-10-30 04:44:42 -0500 | [diff] [blame] | 642 | |
| 643 | else { |
| 644 | /* TODO: C/H/S */ |
| 645 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | } |
| 647 | |
Brian King | a6cce2a | 2006-03-17 17:04:15 -0600 | [diff] [blame] | 648 | static void ata_scsi_sdev_config(struct scsi_device *sdev) |
| 649 | { |
| 650 | sdev->use_10_for_rw = 1; |
| 651 | sdev->use_10_for_ms = 1; |
| 652 | } |
| 653 | |
| 654 | static void ata_scsi_dev_config(struct scsi_device *sdev, |
| 655 | struct ata_device *dev) |
| 656 | { |
| 657 | unsigned int max_sectors; |
| 658 | |
| 659 | /* TODO: 2048 is an arbitrary number, not the |
| 660 | * hardware maximum. This should be increased to |
| 661 | * 65534 when Jens Axboe's patch for dynamically |
| 662 | * determining max_sectors is merged. |
| 663 | */ |
| 664 | max_sectors = ATA_MAX_SECTORS; |
| 665 | if (dev->flags & ATA_DFLAG_LBA48) |
| 666 | max_sectors = 2048; |
| 667 | if (dev->max_sectors) |
| 668 | max_sectors = dev->max_sectors; |
| 669 | |
| 670 | blk_queue_max_sectors(sdev->request_queue, max_sectors); |
| 671 | |
| 672 | /* |
| 673 | * SATA DMA transfers must be multiples of 4 byte, so |
| 674 | * we need to pad ATAPI transfers using an extra sg. |
| 675 | * Decrement max hw segments accordingly. |
| 676 | */ |
| 677 | if (dev->class == ATA_DEV_ATAPI) { |
| 678 | request_queue_t *q = sdev->request_queue; |
| 679 | blk_queue_max_hw_segments(q, q->max_hw_segments - 1); |
| 680 | } |
| 681 | } |
| 682 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 683 | /** |
| 684 | * ata_scsi_slave_config - Set SCSI device attributes |
| 685 | * @sdev: SCSI device to examine |
| 686 | * |
| 687 | * This is called before we actually start reading |
| 688 | * and writing to the device, to configure certain |
| 689 | * SCSI mid-layer behaviors. |
| 690 | * |
| 691 | * LOCKING: |
| 692 | * Defined by SCSI layer. We don't really care. |
| 693 | */ |
| 694 | |
| 695 | int ata_scsi_slave_config(struct scsi_device *sdev) |
| 696 | { |
Brian King | a6cce2a | 2006-03-17 17:04:15 -0600 | [diff] [blame] | 697 | ata_scsi_sdev_config(sdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | |
| 699 | blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD); |
| 700 | |
| 701 | if (sdev->id < ATA_MAX_DEVICES) { |
| 702 | struct ata_port *ap; |
| 703 | struct ata_device *dev; |
| 704 | |
Jeff Garzik | 35bb94b | 2006-04-11 13:12:34 -0400 | [diff] [blame] | 705 | ap = ata_shost_to_port(sdev->host); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 706 | dev = &ap->device[sdev->id]; |
| 707 | |
Brian King | a6cce2a | 2006-03-17 17:04:15 -0600 | [diff] [blame] | 708 | ata_scsi_dev_config(sdev, dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | return 0; /* scsi layer doesn't check return value, sigh */ |
| 712 | } |
| 713 | |
| 714 | /** |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 715 | * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command |
| 716 | * @qc: Storage for translated ATA taskfile |
| 717 | * @scsicmd: SCSI command to translate |
| 718 | * |
| 719 | * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY |
| 720 | * (to start). Perhaps these commands should be preceded by |
| 721 | * CHECK POWER MODE to see what power mode the device is already in. |
| 722 | * [See SAT revision 5 at www.t10.org] |
| 723 | * |
| 724 | * LOCKING: |
| 725 | * spin_lock_irqsave(host_set lock) |
| 726 | * |
| 727 | * RETURNS: |
| 728 | * Zero on success, non-zero on error. |
| 729 | */ |
| 730 | |
| 731 | static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc, |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 732 | const u8 *scsicmd) |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 733 | { |
| 734 | struct ata_taskfile *tf = &qc->tf; |
| 735 | |
| 736 | tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; |
| 737 | tf->protocol = ATA_PROT_NODATA; |
| 738 | if (scsicmd[1] & 0x1) { |
| 739 | ; /* ignore IMMED bit, violates sat-r05 */ |
| 740 | } |
| 741 | if (scsicmd[4] & 0x2) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 742 | goto invalid_fld; /* LOEJ bit set not supported */ |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 743 | if (((scsicmd[4] >> 4) & 0xf) != 0) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 744 | goto invalid_fld; /* power conditions not supported */ |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 745 | if (scsicmd[4] & 0x1) { |
| 746 | tf->nsect = 1; /* 1 sector, lba=0 */ |
Albert Lee | 9d5b130 | 2005-10-04 08:48:17 -0400 | [diff] [blame] | 747 | |
| 748 | if (qc->dev->flags & ATA_DFLAG_LBA) { |
Tejun Heo | c44078c | 2006-05-15 20:57:21 +0900 | [diff] [blame] | 749 | tf->flags |= ATA_TFLAG_LBA; |
Albert Lee | 9d5b130 | 2005-10-04 08:48:17 -0400 | [diff] [blame] | 750 | |
| 751 | tf->lbah = 0x0; |
| 752 | tf->lbam = 0x0; |
| 753 | tf->lbal = 0x0; |
| 754 | tf->device |= ATA_LBA; |
| 755 | } else { |
| 756 | /* CHS */ |
| 757 | tf->lbal = 0x1; /* sect */ |
| 758 | tf->lbam = 0x0; /* cyl low */ |
| 759 | tf->lbah = 0x0; /* cyl high */ |
| 760 | } |
| 761 | |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 762 | tf->command = ATA_CMD_VERIFY; /* READ VERIFY */ |
| 763 | } else { |
| 764 | tf->nsect = 0; /* time period value (0 implies now) */ |
| 765 | tf->command = ATA_CMD_STANDBY; |
| 766 | /* Consider: ATA STANDBY IMMEDIATE command */ |
| 767 | } |
| 768 | /* |
| 769 | * Standby and Idle condition timers could be implemented but that |
| 770 | * would require libata to implement the Power condition mode page |
| 771 | * and allow the user to change it. Changing mode pages requires |
| 772 | * MODE SELECT to be implemented. |
| 773 | */ |
| 774 | |
| 775 | return 0; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 776 | |
| 777 | invalid_fld: |
| 778 | ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0); |
| 779 | /* "Invalid field in cbd" */ |
| 780 | return 1; |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | |
| 784 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command |
| 786 | * @qc: Storage for translated ATA taskfile |
| 787 | * @scsicmd: SCSI command to translate (ignored) |
| 788 | * |
| 789 | * Sets up an ATA taskfile to issue FLUSH CACHE or |
| 790 | * FLUSH CACHE EXT. |
| 791 | * |
| 792 | * LOCKING: |
| 793 | * spin_lock_irqsave(host_set lock) |
| 794 | * |
| 795 | * RETURNS: |
| 796 | * Zero on success, non-zero on error. |
| 797 | */ |
| 798 | |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 799 | static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | { |
| 801 | struct ata_taskfile *tf = &qc->tf; |
| 802 | |
| 803 | tf->flags |= ATA_TFLAG_DEVICE; |
| 804 | tf->protocol = ATA_PROT_NODATA; |
| 805 | |
Albert Lee | 0750669 | 2005-10-12 15:04:18 +0800 | [diff] [blame] | 806 | if ((qc->dev->flags & ATA_DFLAG_LBA48) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 807 | (ata_id_has_flush_ext(qc->dev->id))) |
| 808 | tf->command = ATA_CMD_FLUSH_EXT; |
| 809 | else |
| 810 | tf->command = ATA_CMD_FLUSH; |
| 811 | |
| 812 | return 0; |
| 813 | } |
| 814 | |
| 815 | /** |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 816 | * scsi_6_lba_len - Get LBA and transfer length |
| 817 | * @scsicmd: SCSI command to translate |
| 818 | * |
| 819 | * Calculate LBA and transfer length for 6-byte commands. |
| 820 | * |
| 821 | * RETURNS: |
| 822 | * @plba: the LBA |
| 823 | * @plen: the transfer length |
| 824 | */ |
| 825 | |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 826 | static void scsi_6_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen) |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 827 | { |
| 828 | u64 lba = 0; |
| 829 | u32 len = 0; |
| 830 | |
| 831 | VPRINTK("six-byte command\n"); |
| 832 | |
| 833 | lba |= ((u64)scsicmd[2]) << 8; |
| 834 | lba |= ((u64)scsicmd[3]); |
| 835 | |
| 836 | len |= ((u32)scsicmd[4]); |
| 837 | |
| 838 | *plba = lba; |
| 839 | *plen = len; |
| 840 | } |
| 841 | |
| 842 | /** |
| 843 | * scsi_10_lba_len - Get LBA and transfer length |
| 844 | * @scsicmd: SCSI command to translate |
| 845 | * |
| 846 | * Calculate LBA and transfer length for 10-byte commands. |
| 847 | * |
| 848 | * RETURNS: |
| 849 | * @plba: the LBA |
| 850 | * @plen: the transfer length |
| 851 | */ |
| 852 | |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 853 | static void scsi_10_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen) |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 854 | { |
| 855 | u64 lba = 0; |
| 856 | u32 len = 0; |
| 857 | |
| 858 | VPRINTK("ten-byte command\n"); |
| 859 | |
| 860 | lba |= ((u64)scsicmd[2]) << 24; |
| 861 | lba |= ((u64)scsicmd[3]) << 16; |
| 862 | lba |= ((u64)scsicmd[4]) << 8; |
| 863 | lba |= ((u64)scsicmd[5]); |
| 864 | |
| 865 | len |= ((u32)scsicmd[7]) << 8; |
| 866 | len |= ((u32)scsicmd[8]); |
| 867 | |
| 868 | *plba = lba; |
| 869 | *plen = len; |
| 870 | } |
| 871 | |
| 872 | /** |
| 873 | * scsi_16_lba_len - Get LBA and transfer length |
| 874 | * @scsicmd: SCSI command to translate |
| 875 | * |
| 876 | * Calculate LBA and transfer length for 16-byte commands. |
| 877 | * |
| 878 | * RETURNS: |
| 879 | * @plba: the LBA |
| 880 | * @plen: the transfer length |
| 881 | */ |
| 882 | |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 883 | static void scsi_16_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen) |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 884 | { |
| 885 | u64 lba = 0; |
| 886 | u32 len = 0; |
| 887 | |
| 888 | VPRINTK("sixteen-byte command\n"); |
| 889 | |
| 890 | lba |= ((u64)scsicmd[2]) << 56; |
| 891 | lba |= ((u64)scsicmd[3]) << 48; |
| 892 | lba |= ((u64)scsicmd[4]) << 40; |
| 893 | lba |= ((u64)scsicmd[5]) << 32; |
| 894 | lba |= ((u64)scsicmd[6]) << 24; |
| 895 | lba |= ((u64)scsicmd[7]) << 16; |
| 896 | lba |= ((u64)scsicmd[8]) << 8; |
| 897 | lba |= ((u64)scsicmd[9]); |
| 898 | |
| 899 | len |= ((u32)scsicmd[10]) << 24; |
| 900 | len |= ((u32)scsicmd[11]) << 16; |
| 901 | len |= ((u32)scsicmd[12]) << 8; |
| 902 | len |= ((u32)scsicmd[13]); |
| 903 | |
| 904 | *plba = lba; |
| 905 | *plen = len; |
| 906 | } |
| 907 | |
| 908 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one |
| 910 | * @qc: Storage for translated ATA taskfile |
| 911 | * @scsicmd: SCSI command to translate |
| 912 | * |
| 913 | * Converts SCSI VERIFY command to an ATA READ VERIFY command. |
| 914 | * |
| 915 | * LOCKING: |
| 916 | * spin_lock_irqsave(host_set lock) |
| 917 | * |
| 918 | * RETURNS: |
| 919 | * Zero on success, non-zero on error. |
| 920 | */ |
| 921 | |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 922 | static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 923 | { |
| 924 | struct ata_taskfile *tf = &qc->tf; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 925 | struct ata_device *dev = qc->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 926 | u64 dev_sectors = qc->dev->n_sectors; |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 927 | u64 block; |
| 928 | u32 n_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 929 | |
| 930 | tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 931 | tf->protocol = ATA_PROT_NODATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 933 | if (scsicmd[0] == VERIFY) |
| 934 | scsi_10_lba_len(scsicmd, &block, &n_block); |
| 935 | else if (scsicmd[0] == VERIFY_16) |
| 936 | scsi_16_lba_len(scsicmd, &block, &n_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 937 | else |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 938 | goto invalid_fld; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 940 | if (!n_block) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 941 | goto nothing_to_do; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 942 | if (block >= dev_sectors) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 943 | goto out_of_range; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 944 | if ((block + n_block) > dev_sectors) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 945 | goto out_of_range; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 946 | |
Albert Lee | 0750669 | 2005-10-12 15:04:18 +0800 | [diff] [blame] | 947 | if (dev->flags & ATA_DFLAG_LBA) { |
| 948 | tf->flags |= ATA_TFLAG_LBA; |
| 949 | |
Albert Lee | c6a33e2 | 2005-10-12 15:12:26 +0800 | [diff] [blame] | 950 | if (lba_28_ok(block, n_block)) { |
| 951 | /* use LBA28 */ |
| 952 | tf->command = ATA_CMD_VERIFY; |
| 953 | tf->device |= (block >> 24) & 0xf; |
| 954 | } else if (lba_48_ok(block, n_block)) { |
| 955 | if (!(dev->flags & ATA_DFLAG_LBA48)) |
| 956 | goto out_of_range; |
Albert Lee | 0750669 | 2005-10-12 15:04:18 +0800 | [diff] [blame] | 957 | |
| 958 | /* use LBA48 */ |
| 959 | tf->flags |= ATA_TFLAG_LBA48; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 960 | tf->command = ATA_CMD_VERIFY_EXT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 961 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 962 | tf->hob_nsect = (n_block >> 8) & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 963 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 964 | tf->hob_lbah = (block >> 40) & 0xff; |
| 965 | tf->hob_lbam = (block >> 32) & 0xff; |
| 966 | tf->hob_lbal = (block >> 24) & 0xff; |
Albert Lee | c6a33e2 | 2005-10-12 15:12:26 +0800 | [diff] [blame] | 967 | } else |
| 968 | /* request too large even for LBA48 */ |
| 969 | goto out_of_range; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 970 | |
| 971 | tf->nsect = n_block & 0xff; |
| 972 | |
| 973 | tf->lbah = (block >> 16) & 0xff; |
| 974 | tf->lbam = (block >> 8) & 0xff; |
| 975 | tf->lbal = block & 0xff; |
| 976 | |
| 977 | tf->device |= ATA_LBA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | } else { |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 979 | /* CHS */ |
| 980 | u32 sect, head, cyl, track; |
| 981 | |
Albert Lee | c6a33e2 | 2005-10-12 15:12:26 +0800 | [diff] [blame] | 982 | if (!lba_28_ok(block, n_block)) |
| 983 | goto out_of_range; |
Albert Lee | 0750669 | 2005-10-12 15:04:18 +0800 | [diff] [blame] | 984 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 985 | /* Convert LBA to CHS */ |
| 986 | track = (u32)block / dev->sectors; |
| 987 | cyl = track / dev->heads; |
| 988 | head = track % dev->heads; |
| 989 | sect = (u32)block % dev->sectors + 1; |
| 990 | |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 991 | DPRINTK("block %u track %u cyl %u head %u sect %u\n", |
| 992 | (u32)block, track, cyl, head, sect); |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 993 | |
| 994 | /* Check whether the converted CHS can fit. |
| 995 | Cylinder: 0-65535 |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 996 | Head: 0-15 |
| 997 | Sector: 1-255*/ |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 998 | if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect)) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 999 | goto out_of_range; |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 1000 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1001 | tf->command = ATA_CMD_VERIFY; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1002 | tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */ |
| 1003 | tf->lbal = sect; |
| 1004 | tf->lbam = cyl; |
| 1005 | tf->lbah = cyl >> 8; |
| 1006 | tf->device |= head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1007 | } |
| 1008 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1009 | return 0; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1010 | |
| 1011 | invalid_fld: |
| 1012 | ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0); |
| 1013 | /* "Invalid field in cbd" */ |
| 1014 | return 1; |
| 1015 | |
| 1016 | out_of_range: |
| 1017 | ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0); |
| 1018 | /* "Logical Block Address out of range" */ |
| 1019 | return 1; |
| 1020 | |
| 1021 | nothing_to_do: |
| 1022 | qc->scsicmd->result = SAM_STAT_GOOD; |
| 1023 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | } |
| 1025 | |
| 1026 | /** |
| 1027 | * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one |
| 1028 | * @qc: Storage for translated ATA taskfile |
| 1029 | * @scsicmd: SCSI command to translate |
| 1030 | * |
| 1031 | * Converts any of six SCSI read/write commands into the |
| 1032 | * ATA counterpart, including starting sector (LBA), |
| 1033 | * sector count, and taking into account the device's LBA48 |
| 1034 | * support. |
| 1035 | * |
| 1036 | * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and |
| 1037 | * %WRITE_16 are currently supported. |
| 1038 | * |
| 1039 | * LOCKING: |
| 1040 | * spin_lock_irqsave(host_set lock) |
| 1041 | * |
| 1042 | * RETURNS: |
| 1043 | * Zero on success, non-zero on error. |
| 1044 | */ |
| 1045 | |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 1046 | static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | { |
| 1048 | struct ata_taskfile *tf = &qc->tf; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1049 | struct ata_device *dev = qc->dev; |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1050 | u64 block; |
| 1051 | u32 n_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1052 | |
Tejun Heo | 2719736 | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 1053 | qc->flags |= ATA_QCFLAG_IO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 1056 | if (scsicmd[0] == WRITE_10 || scsicmd[0] == WRITE_6 || |
| 1057 | scsicmd[0] == WRITE_16) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1058 | tf->flags |= ATA_TFLAG_WRITE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1059 | |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 1060 | /* Calculate the SCSI LBA, transfer length and FUA. */ |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1061 | switch (scsicmd[0]) { |
| 1062 | case READ_10: |
| 1063 | case WRITE_10: |
| 1064 | scsi_10_lba_len(scsicmd, &block, &n_block); |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 1065 | if (unlikely(scsicmd[1] & (1 << 3))) |
| 1066 | tf->flags |= ATA_TFLAG_FUA; |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1067 | break; |
| 1068 | case READ_6: |
| 1069 | case WRITE_6: |
| 1070 | scsi_6_lba_len(scsicmd, &block, &n_block); |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 1071 | |
| 1072 | /* for 6-byte r/w commands, transfer length 0 |
| 1073 | * means 256 blocks of data, not 0 block. |
| 1074 | */ |
Jeff Garzik | 76b2bf9 | 2005-08-29 19:24:43 -0400 | [diff] [blame] | 1075 | if (!n_block) |
| 1076 | n_block = 256; |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1077 | break; |
| 1078 | case READ_16: |
| 1079 | case WRITE_16: |
| 1080 | scsi_16_lba_len(scsicmd, &block, &n_block); |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 1081 | if (unlikely(scsicmd[1] & (1 << 3))) |
| 1082 | tf->flags |= ATA_TFLAG_FUA; |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 1083 | break; |
| 1084 | default: |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1085 | DPRINTK("no-byte command\n"); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1086 | goto invalid_fld; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1089 | /* Check and compose ATA command */ |
| 1090 | if (!n_block) |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 1091 | /* For 10-byte and 16-byte SCSI R/W commands, transfer |
| 1092 | * length 0 means transfer 0 block of data. |
| 1093 | * However, for ATA R/W commands, sector count 0 means |
| 1094 | * 256 or 65536 sectors, not 0 sectors as in SCSI. |
Alan Cox | f51750d | 2005-11-07 17:06:33 +0000 | [diff] [blame] | 1095 | * |
| 1096 | * WARNING: one or two older ATA drives treat 0 as 0... |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 1097 | */ |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1098 | goto nothing_to_do; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1099 | |
Albert Lee | 0750669 | 2005-10-12 15:04:18 +0800 | [diff] [blame] | 1100 | if (dev->flags & ATA_DFLAG_LBA) { |
| 1101 | tf->flags |= ATA_TFLAG_LBA; |
| 1102 | |
Albert Lee | c6a33e2 | 2005-10-12 15:12:26 +0800 | [diff] [blame] | 1103 | if (lba_28_ok(block, n_block)) { |
| 1104 | /* use LBA28 */ |
| 1105 | tf->device |= (block >> 24) & 0xf; |
| 1106 | } else if (lba_48_ok(block, n_block)) { |
| 1107 | if (!(dev->flags & ATA_DFLAG_LBA48)) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1108 | goto out_of_range; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1109 | |
Albert Lee | 0750669 | 2005-10-12 15:04:18 +0800 | [diff] [blame] | 1110 | /* use LBA48 */ |
| 1111 | tf->flags |= ATA_TFLAG_LBA48; |
| 1112 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1113 | tf->hob_nsect = (n_block >> 8) & 0xff; |
| 1114 | |
| 1115 | tf->hob_lbah = (block >> 40) & 0xff; |
| 1116 | tf->hob_lbam = (block >> 32) & 0xff; |
| 1117 | tf->hob_lbal = (block >> 24) & 0xff; |
Albert Lee | c6a33e2 | 2005-10-12 15:12:26 +0800 | [diff] [blame] | 1118 | } else |
| 1119 | /* request too large even for LBA48 */ |
| 1120 | goto out_of_range; |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 1121 | |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 1122 | if (unlikely(ata_rwcmd_protocol(qc) < 0)) |
| 1123 | goto invalid_fld; |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 1124 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1125 | qc->nsect = n_block; |
| 1126 | tf->nsect = n_block & 0xff; |
| 1127 | |
| 1128 | tf->lbah = (block >> 16) & 0xff; |
| 1129 | tf->lbam = (block >> 8) & 0xff; |
| 1130 | tf->lbal = block & 0xff; |
| 1131 | |
| 1132 | tf->device |= ATA_LBA; |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 1133 | } else { |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1134 | /* CHS */ |
| 1135 | u32 sect, head, cyl, track; |
| 1136 | |
| 1137 | /* The request -may- be too large for CHS addressing. */ |
Albert Lee | c6a33e2 | 2005-10-12 15:12:26 +0800 | [diff] [blame] | 1138 | if (!lba_28_ok(block, n_block)) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1139 | goto out_of_range; |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 1140 | |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 1141 | if (unlikely(ata_rwcmd_protocol(qc) < 0)) |
| 1142 | goto invalid_fld; |
Albert Lee | 8cbd6df | 2005-10-12 15:06:27 +0800 | [diff] [blame] | 1143 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1144 | /* Convert LBA to CHS */ |
| 1145 | track = (u32)block / dev->sectors; |
| 1146 | cyl = track / dev->heads; |
| 1147 | head = track % dev->heads; |
| 1148 | sect = (u32)block % dev->sectors + 1; |
| 1149 | |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 1150 | DPRINTK("block %u track %u cyl %u head %u sect %u\n", |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1151 | (u32)block, track, cyl, head, sect); |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 1152 | |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 1153 | /* Check whether the converted CHS can fit. |
| 1154 | Cylinder: 0-65535 |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1155 | Head: 0-15 |
| 1156 | Sector: 1-255*/ |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 1157 | if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect)) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1158 | goto out_of_range; |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 1159 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1160 | qc->nsect = n_block; |
| 1161 | tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */ |
| 1162 | tf->lbal = sect; |
| 1163 | tf->lbam = cyl; |
| 1164 | tf->lbah = cyl >> 8; |
| 1165 | tf->device |= head; |
| 1166 | } |
| 1167 | |
| 1168 | return 0; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1169 | |
| 1170 | invalid_fld: |
| 1171 | ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0); |
| 1172 | /* "Invalid field in cbd" */ |
| 1173 | return 1; |
| 1174 | |
| 1175 | out_of_range: |
| 1176 | ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0); |
| 1177 | /* "Logical Block Address out of range" */ |
| 1178 | return 1; |
| 1179 | |
| 1180 | nothing_to_do: |
| 1181 | qc->scsicmd->result = SAM_STAT_GOOD; |
| 1182 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1183 | } |
| 1184 | |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1185 | static void ata_scsi_qc_complete(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | { |
| 1187 | struct scsi_cmnd *cmd = qc->scsicmd; |
Jeff Garzik | a7dac44 | 2005-10-30 04:44:42 -0500 | [diff] [blame] | 1188 | u8 *cdb = cmd->cmnd; |
Albert Lee | a22e2eb | 2005-12-05 15:38:02 +0800 | [diff] [blame] | 1189 | int need_sense = (qc->err_mask != 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1190 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1191 | /* For ATA pass thru (SAT) commands, generate a sense block if |
| 1192 | * user mandated it or if there's an error. Note that if we |
| 1193 | * generate because the user forced us to, a check condition |
| 1194 | * is generated and the ATA register values are returned |
| 1195 | * whether the command completed successfully or not. If there |
| 1196 | * was no error, SK, ASC and ASCQ will all be zero. |
| 1197 | */ |
Jeff Garzik | a7dac44 | 2005-10-30 04:44:42 -0500 | [diff] [blame] | 1198 | if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) && |
| 1199 | ((cdb[2] & 0x20) || need_sense)) { |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 1200 | ata_gen_ata_desc_sense(qc); |
| 1201 | } else { |
| 1202 | if (!need_sense) { |
| 1203 | cmd->result = SAM_STAT_GOOD; |
| 1204 | } else { |
| 1205 | /* TODO: decide which descriptor format to use |
| 1206 | * for 48b LBA devices and call that here |
| 1207 | * instead of the fixed desc, which is only |
| 1208 | * good for smaller LBA (and maybe CHS?) |
| 1209 | * devices. |
| 1210 | */ |
| 1211 | ata_gen_fixed_sense(qc); |
| 1212 | } |
| 1213 | } |
| 1214 | |
Tejun Heo | e61e067 | 2006-05-15 20:57:40 +0900 | [diff] [blame] | 1215 | if (need_sense) |
| 1216 | ata_dump_status(qc->ap->id, &qc->result_tf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1217 | |
| 1218 | qc->scsidone(cmd); |
| 1219 | |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1220 | ata_qc_free(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1221 | } |
| 1222 | |
| 1223 | /** |
| 1224 | * ata_scsi_translate - Translate then issue SCSI command to ATA device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1225 | * @dev: ATA device to which the command is addressed |
| 1226 | * @cmd: SCSI command to execute |
| 1227 | * @done: SCSI command completion function |
| 1228 | * @xlat_func: Actor which translates @cmd to an ATA taskfile |
| 1229 | * |
| 1230 | * Our ->queuecommand() function has decided that the SCSI |
| 1231 | * command issued can be directly translated into an ATA |
| 1232 | * command, rather than handled internally. |
| 1233 | * |
| 1234 | * This function sets up an ata_queued_cmd structure for the |
| 1235 | * SCSI command, and sends that ata_queued_cmd to the hardware. |
| 1236 | * |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1237 | * The xlat_func argument (actor) returns 0 if ready to execute |
| 1238 | * ATA command, else 1 to finish translation. If 1 is returned |
| 1239 | * then cmd->result (and possibly cmd->sense_buffer) are assumed |
| 1240 | * to be set reflecting an error condition or clean (early) |
| 1241 | * termination. |
| 1242 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1243 | * LOCKING: |
| 1244 | * spin_lock_irqsave(host_set lock) |
| 1245 | */ |
| 1246 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 1247 | static void ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd, |
| 1248 | void (*done)(struct scsi_cmnd *), |
| 1249 | ata_xlat_func_t xlat_func) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1250 | { |
| 1251 | struct ata_queued_cmd *qc; |
| 1252 | u8 *scsicmd = cmd->cmnd; |
| 1253 | |
| 1254 | VPRINTK("ENTER\n"); |
| 1255 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 1256 | qc = ata_scsi_qc_new(dev, cmd, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | if (!qc) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1258 | goto err_mem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1259 | |
| 1260 | /* data is present; dma-map it */ |
| be7db05 | 2005-04-17 15:26:13 -0500 | [diff] [blame] | 1261 | if (cmd->sc_data_direction == DMA_FROM_DEVICE || |
| 1262 | cmd->sc_data_direction == DMA_TO_DEVICE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1263 | if (unlikely(cmd->request_bufflen < 1)) { |
Tejun Heo | f15a1da | 2006-05-15 20:57:56 +0900 | [diff] [blame^] | 1264 | ata_dev_printk(dev, KERN_WARNING, |
| 1265 | "WARNING: zero len r/w req\n"); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1266 | goto err_did; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | } |
| 1268 | |
| 1269 | if (cmd->use_sg) |
| 1270 | ata_sg_init(qc, cmd->request_buffer, cmd->use_sg); |
| 1271 | else |
| 1272 | ata_sg_init_one(qc, cmd->request_buffer, |
| 1273 | cmd->request_bufflen); |
| 1274 | |
| 1275 | qc->dma_dir = cmd->sc_data_direction; |
| 1276 | } |
| 1277 | |
| 1278 | qc->complete_fn = ata_scsi_qc_complete; |
| 1279 | |
| 1280 | if (xlat_func(qc, scsicmd)) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1281 | goto early_finish; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1282 | |
| 1283 | /* select device, send command to hardware */ |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 1284 | ata_qc_issue(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | |
| 1286 | VPRINTK("EXIT\n"); |
| 1287 | return; |
| 1288 | |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1289 | early_finish: |
| 1290 | ata_qc_free(qc); |
| 1291 | done(cmd); |
| 1292 | DPRINTK("EXIT - early finish (good or error)\n"); |
| 1293 | return; |
| 1294 | |
| 1295 | err_did: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1296 | ata_qc_free(qc); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1297 | err_mem: |
| 1298 | cmd->result = (DID_ERROR << 16); |
| 1299 | done(cmd); |
| 1300 | DPRINTK("EXIT - internal\n"); |
| 1301 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | /** |
| 1305 | * ata_scsi_rbuf_get - Map response buffer. |
| 1306 | * @cmd: SCSI command containing buffer to be mapped. |
| 1307 | * @buf_out: Pointer to mapped area. |
| 1308 | * |
| 1309 | * Maps buffer contained within SCSI command @cmd. |
| 1310 | * |
| 1311 | * LOCKING: |
| 1312 | * spin_lock_irqsave(host_set lock) |
| 1313 | * |
| 1314 | * RETURNS: |
| 1315 | * Length of response buffer. |
| 1316 | */ |
| 1317 | |
| 1318 | static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out) |
| 1319 | { |
| 1320 | u8 *buf; |
| 1321 | unsigned int buflen; |
| 1322 | |
| 1323 | if (cmd->use_sg) { |
| 1324 | struct scatterlist *sg; |
| 1325 | |
| 1326 | sg = (struct scatterlist *) cmd->request_buffer; |
| 1327 | buf = kmap_atomic(sg->page, KM_USER0) + sg->offset; |
| 1328 | buflen = sg->length; |
| 1329 | } else { |
| 1330 | buf = cmd->request_buffer; |
| 1331 | buflen = cmd->request_bufflen; |
| 1332 | } |
| 1333 | |
| 1334 | *buf_out = buf; |
| 1335 | return buflen; |
| 1336 | } |
| 1337 | |
| 1338 | /** |
| 1339 | * ata_scsi_rbuf_put - Unmap response buffer. |
| 1340 | * @cmd: SCSI command containing buffer to be unmapped. |
| 1341 | * @buf: buffer to unmap |
| 1342 | * |
| 1343 | * Unmaps response buffer contained within @cmd. |
| 1344 | * |
| 1345 | * LOCKING: |
| 1346 | * spin_lock_irqsave(host_set lock) |
| 1347 | */ |
| 1348 | |
| 1349 | static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf) |
| 1350 | { |
| 1351 | if (cmd->use_sg) { |
| 1352 | struct scatterlist *sg; |
| 1353 | |
| 1354 | sg = (struct scatterlist *) cmd->request_buffer; |
| 1355 | kunmap_atomic(buf - sg->offset, KM_USER0); |
| 1356 | } |
| 1357 | } |
| 1358 | |
| 1359 | /** |
| 1360 | * ata_scsi_rbuf_fill - wrapper for SCSI command simulators |
| 1361 | * @args: device IDENTIFY data / SCSI command of interest. |
| 1362 | * @actor: Callback hook for desired SCSI command simulator |
| 1363 | * |
| 1364 | * Takes care of the hard work of simulating a SCSI command... |
| 1365 | * Mapping the response buffer, calling the command's handler, |
| 1366 | * and handling the handler's return value. This return value |
| 1367 | * indicates whether the handler wishes the SCSI command to be |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1368 | * completed successfully (0), or not (in which case cmd->result |
| 1369 | * and sense buffer are assumed to be set). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1370 | * |
| 1371 | * LOCKING: |
| 1372 | * spin_lock_irqsave(host_set lock) |
| 1373 | */ |
| 1374 | |
| 1375 | void ata_scsi_rbuf_fill(struct ata_scsi_args *args, |
| 1376 | unsigned int (*actor) (struct ata_scsi_args *args, |
| 1377 | u8 *rbuf, unsigned int buflen)) |
| 1378 | { |
| 1379 | u8 *rbuf; |
| 1380 | unsigned int buflen, rc; |
| 1381 | struct scsi_cmnd *cmd = args->cmd; |
| 1382 | |
| 1383 | buflen = ata_scsi_rbuf_get(cmd, &rbuf); |
| 1384 | memset(rbuf, 0, buflen); |
| 1385 | rc = actor(args, rbuf, buflen); |
| 1386 | ata_scsi_rbuf_put(cmd, rbuf); |
| 1387 | |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1388 | if (rc == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1389 | cmd->result = SAM_STAT_GOOD; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1390 | args->done(cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1391 | } |
| 1392 | |
| 1393 | /** |
| 1394 | * ata_scsiop_inq_std - Simulate INQUIRY command |
| 1395 | * @args: device IDENTIFY data / SCSI command of interest. |
| 1396 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
| 1397 | * @buflen: Response buffer length. |
| 1398 | * |
| 1399 | * Returns standard device identification data associated |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 1400 | * with non-VPD INQUIRY command output. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | * |
| 1402 | * LOCKING: |
| 1403 | * spin_lock_irqsave(host_set lock) |
| 1404 | */ |
| 1405 | |
| 1406 | unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf, |
| 1407 | unsigned int buflen) |
| 1408 | { |
| 1409 | u8 hdr[] = { |
| 1410 | TYPE_DISK, |
| 1411 | 0, |
| 1412 | 0x5, /* claim SPC-3 version compatibility */ |
| 1413 | 2, |
| 1414 | 95 - 4 |
| 1415 | }; |
| 1416 | |
| 1417 | /* set scsi removeable (RMB) bit per ata bit */ |
| 1418 | if (ata_id_removeable(args->id)) |
| 1419 | hdr[1] |= (1 << 7); |
| 1420 | |
| 1421 | VPRINTK("ENTER\n"); |
| 1422 | |
| 1423 | memcpy(rbuf, hdr, sizeof(hdr)); |
| 1424 | |
| 1425 | if (buflen > 35) { |
| 1426 | memcpy(&rbuf[8], "ATA ", 8); |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1427 | ata_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16); |
| 1428 | ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | if (rbuf[32] == 0 || rbuf[32] == ' ') |
| 1430 | memcpy(&rbuf[32], "n/a ", 4); |
| 1431 | } |
| 1432 | |
| 1433 | if (buflen > 63) { |
| 1434 | const u8 versions[] = { |
| 1435 | 0x60, /* SAM-3 (no version claimed) */ |
| 1436 | |
| 1437 | 0x03, |
| 1438 | 0x20, /* SBC-2 (no version claimed) */ |
| 1439 | |
| 1440 | 0x02, |
| 1441 | 0x60 /* SPC-3 (no version claimed) */ |
| 1442 | }; |
| 1443 | |
| 1444 | memcpy(rbuf + 59, versions, sizeof(versions)); |
| 1445 | } |
| 1446 | |
| 1447 | return 0; |
| 1448 | } |
| 1449 | |
| 1450 | /** |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 1451 | * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | * @args: device IDENTIFY data / SCSI command of interest. |
| 1453 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
| 1454 | * @buflen: Response buffer length. |
| 1455 | * |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 1456 | * Returns list of inquiry VPD pages available. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | * |
| 1458 | * LOCKING: |
| 1459 | * spin_lock_irqsave(host_set lock) |
| 1460 | */ |
| 1461 | |
| 1462 | unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf, |
| 1463 | unsigned int buflen) |
| 1464 | { |
| 1465 | const u8 pages[] = { |
| 1466 | 0x00, /* page 0x00, this page */ |
| 1467 | 0x80, /* page 0x80, unit serial no page */ |
| 1468 | 0x83 /* page 0x83, device ident page */ |
| 1469 | }; |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 1470 | rbuf[3] = sizeof(pages); /* number of supported VPD pages */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | |
| 1472 | if (buflen > 6) |
| 1473 | memcpy(rbuf + 4, pages, sizeof(pages)); |
| 1474 | |
| 1475 | return 0; |
| 1476 | } |
| 1477 | |
| 1478 | /** |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 1479 | * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1480 | * @args: device IDENTIFY data / SCSI command of interest. |
| 1481 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
| 1482 | * @buflen: Response buffer length. |
| 1483 | * |
| 1484 | * Returns ATA device serial number. |
| 1485 | * |
| 1486 | * LOCKING: |
| 1487 | * spin_lock_irqsave(host_set lock) |
| 1488 | */ |
| 1489 | |
| 1490 | unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf, |
| 1491 | unsigned int buflen) |
| 1492 | { |
| 1493 | const u8 hdr[] = { |
| 1494 | 0, |
| 1495 | 0x80, /* this page code */ |
| 1496 | 0, |
| 1497 | ATA_SERNO_LEN, /* page len */ |
| 1498 | }; |
| 1499 | memcpy(rbuf, hdr, sizeof(hdr)); |
| 1500 | |
| 1501 | if (buflen > (ATA_SERNO_LEN + 4 - 1)) |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1502 | ata_id_string(args->id, (unsigned char *) &rbuf[4], |
| 1503 | ATA_ID_SERNO_OFS, ATA_SERNO_LEN); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1504 | |
| 1505 | return 0; |
| 1506 | } |
| 1507 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1508 | /** |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 1509 | * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1510 | * @args: device IDENTIFY data / SCSI command of interest. |
| 1511 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
| 1512 | * @buflen: Response buffer length. |
| 1513 | * |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 1514 | * Yields two logical unit device identification designators: |
| 1515 | * - vendor specific ASCII containing the ATA serial number |
| 1516 | * - SAT defined "t10 vendor id based" containing ASCII vendor |
| 1517 | * name ("ATA "), model and serial numbers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | * |
| 1519 | * LOCKING: |
| 1520 | * spin_lock_irqsave(host_set lock) |
| 1521 | */ |
| 1522 | |
| 1523 | unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf, |
| 1524 | unsigned int buflen) |
| 1525 | { |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 1526 | int num; |
| 1527 | const int sat_model_serial_desc_len = 68; |
| 1528 | const int ata_model_byte_len = 40; |
| 1529 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1530 | rbuf[1] = 0x83; /* this page code */ |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 1531 | num = 4; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 1533 | if (buflen > (ATA_SERNO_LEN + num + 3)) { |
| 1534 | /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */ |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 1535 | rbuf[num + 0] = 2; |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 1536 | rbuf[num + 3] = ATA_SERNO_LEN; |
| 1537 | num += 4; |
| 1538 | ata_id_string(args->id, (unsigned char *) rbuf + num, |
| 1539 | ATA_ID_SERNO_OFS, ATA_SERNO_LEN); |
| 1540 | num += ATA_SERNO_LEN; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1541 | } |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 1542 | if (buflen > (sat_model_serial_desc_len + num + 3)) { |
| 1543 | /* SAT defined lu model and serial numbers descriptor */ |
| 1544 | /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */ |
Jeff Garzik | 2e9edbf | 2006-03-24 09:56:57 -0500 | [diff] [blame] | 1545 | rbuf[num + 0] = 2; |
| 1546 | rbuf[num + 1] = 1; |
Jeff Garzik | b142eb6 | 2006-03-21 20:37:47 -0500 | [diff] [blame] | 1547 | rbuf[num + 3] = sat_model_serial_desc_len; |
| 1548 | num += 4; |
| 1549 | memcpy(rbuf + num, "ATA ", 8); |
| 1550 | num += 8; |
| 1551 | ata_id_string(args->id, (unsigned char *) rbuf + num, |
| 1552 | ATA_ID_PROD_OFS, ata_model_byte_len); |
| 1553 | num += ata_model_byte_len; |
| 1554 | ata_id_string(args->id, (unsigned char *) rbuf + num, |
| 1555 | ATA_ID_SERNO_OFS, ATA_SERNO_LEN); |
| 1556 | num += ATA_SERNO_LEN; |
| 1557 | } |
| 1558 | rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | return 0; |
| 1560 | } |
| 1561 | |
| 1562 | /** |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 1563 | * ata_scsiop_noop - Command handler that simply returns success. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1564 | * @args: device IDENTIFY data / SCSI command of interest. |
| 1565 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
| 1566 | * @buflen: Response buffer length. |
| 1567 | * |
| 1568 | * No operation. Simply returns success to caller, to indicate |
| 1569 | * that the caller should successfully complete this SCSI command. |
| 1570 | * |
| 1571 | * LOCKING: |
| 1572 | * spin_lock_irqsave(host_set lock) |
| 1573 | */ |
| 1574 | |
| 1575 | unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf, |
| 1576 | unsigned int buflen) |
| 1577 | { |
| 1578 | VPRINTK("ENTER\n"); |
| 1579 | return 0; |
| 1580 | } |
| 1581 | |
| 1582 | /** |
| 1583 | * ata_msense_push - Push data onto MODE SENSE data output buffer |
| 1584 | * @ptr_io: (input/output) Location to store more output data |
| 1585 | * @last: End of output data buffer |
| 1586 | * @buf: Pointer to BLOB being added to output buffer |
| 1587 | * @buflen: Length of BLOB |
| 1588 | * |
| 1589 | * Store MODE SENSE data on an output buffer. |
| 1590 | * |
| 1591 | * LOCKING: |
| 1592 | * None. |
| 1593 | */ |
| 1594 | |
| 1595 | static void ata_msense_push(u8 **ptr_io, const u8 *last, |
| 1596 | const u8 *buf, unsigned int buflen) |
| 1597 | { |
| 1598 | u8 *ptr = *ptr_io; |
| 1599 | |
| 1600 | if ((ptr + buflen - 1) > last) |
| 1601 | return; |
| 1602 | |
| 1603 | memcpy(ptr, buf, buflen); |
| 1604 | |
| 1605 | ptr += buflen; |
| 1606 | |
| 1607 | *ptr_io = ptr; |
| 1608 | } |
| 1609 | |
| 1610 | /** |
| 1611 | * ata_msense_caching - Simulate MODE SENSE caching info page |
| 1612 | * @id: device IDENTIFY data |
| 1613 | * @ptr_io: (input/output) Location to store more output data |
| 1614 | * @last: End of output data buffer |
| 1615 | * |
| 1616 | * Generate a caching info page, which conditionally indicates |
| 1617 | * write caching to the SCSI layer, depending on device |
| 1618 | * capabilities. |
| 1619 | * |
| 1620 | * LOCKING: |
| 1621 | * None. |
| 1622 | */ |
| 1623 | |
| 1624 | static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io, |
| 1625 | const u8 *last) |
| 1626 | { |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 1627 | u8 page[CACHE_MPAGE_LEN]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1628 | |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 1629 | memcpy(page, def_cache_mpage, sizeof(page)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | if (ata_id_wcache_enabled(id)) |
| 1631 | page[2] |= (1 << 2); /* write cache enable */ |
| 1632 | if (!ata_id_rahead_enabled(id)) |
| 1633 | page[12] |= (1 << 5); /* disable read ahead */ |
| 1634 | |
| 1635 | ata_msense_push(ptr_io, last, page, sizeof(page)); |
| 1636 | return sizeof(page); |
| 1637 | } |
| 1638 | |
| 1639 | /** |
| 1640 | * ata_msense_ctl_mode - Simulate MODE SENSE control mode page |
| 1641 | * @dev: Device associated with this MODE SENSE command |
| 1642 | * @ptr_io: (input/output) Location to store more output data |
| 1643 | * @last: End of output data buffer |
| 1644 | * |
| 1645 | * Generate a generic MODE SENSE control mode page. |
| 1646 | * |
| 1647 | * LOCKING: |
| 1648 | * None. |
| 1649 | */ |
| 1650 | |
| 1651 | static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last) |
| 1652 | { |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 1653 | ata_msense_push(ptr_io, last, def_control_mpage, |
| 1654 | sizeof(def_control_mpage)); |
| 1655 | return sizeof(def_control_mpage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | } |
| 1657 | |
| 1658 | /** |
| 1659 | * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page |
| 1660 | * @dev: Device associated with this MODE SENSE command |
| 1661 | * @ptr_io: (input/output) Location to store more output data |
| 1662 | * @last: End of output data buffer |
| 1663 | * |
| 1664 | * Generate a generic MODE SENSE r/w error recovery page. |
| 1665 | * |
| 1666 | * LOCKING: |
| 1667 | * None. |
| 1668 | */ |
| 1669 | |
| 1670 | static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last) |
| 1671 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 1673 | ata_msense_push(ptr_io, last, def_rw_recovery_mpage, |
| 1674 | sizeof(def_rw_recovery_mpage)); |
| 1675 | return sizeof(def_rw_recovery_mpage); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1676 | } |
| 1677 | |
Jens Axboe | 48bdc8e | 2006-01-30 16:09:35 +0100 | [diff] [blame] | 1678 | /* |
| 1679 | * We can turn this into a real blacklist if it's needed, for now just |
| 1680 | * blacklist any Maxtor BANC1G10 revision firmware |
| 1681 | */ |
| 1682 | static int ata_dev_supports_fua(u16 *id) |
| 1683 | { |
| 1684 | unsigned char model[41], fw[9]; |
| 1685 | |
Jeff Garzik | c3c013a | 2006-02-27 22:31:19 -0500 | [diff] [blame] | 1686 | if (!libata_fua) |
| 1687 | return 0; |
Jens Axboe | 48bdc8e | 2006-01-30 16:09:35 +0100 | [diff] [blame] | 1688 | if (!ata_id_has_fua(id)) |
| 1689 | return 0; |
| 1690 | |
Tejun Heo | 6a62a04 | 2006-02-13 10:02:46 +0900 | [diff] [blame] | 1691 | ata_id_c_string(id, model, ATA_ID_PROD_OFS, sizeof(model)); |
| 1692 | ata_id_c_string(id, fw, ATA_ID_FW_REV_OFS, sizeof(fw)); |
Jens Axboe | 48bdc8e | 2006-01-30 16:09:35 +0100 | [diff] [blame] | 1693 | |
Tejun Heo | 2e02671 | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 1694 | if (strcmp(model, "Maxtor")) |
Jens Axboe | 48bdc8e | 2006-01-30 16:09:35 +0100 | [diff] [blame] | 1695 | return 1; |
Tejun Heo | 2e02671 | 2006-02-12 22:47:04 +0900 | [diff] [blame] | 1696 | if (strcmp(fw, "BANC1G10")) |
Jens Axboe | 48bdc8e | 2006-01-30 16:09:35 +0100 | [diff] [blame] | 1697 | return 1; |
| 1698 | |
| 1699 | return 0; /* blacklisted */ |
| 1700 | } |
| 1701 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | /** |
| 1703 | * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands |
| 1704 | * @args: device IDENTIFY data / SCSI command of interest. |
| 1705 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
| 1706 | * @buflen: Response buffer length. |
| 1707 | * |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 1708 | * Simulate MODE SENSE commands. Assume this is invoked for direct |
| 1709 | * access devices (e.g. disks) only. There should be no block |
| 1710 | * descriptor for other device types. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1711 | * |
| 1712 | * LOCKING: |
| 1713 | * spin_lock_irqsave(host_set lock) |
| 1714 | */ |
| 1715 | |
| 1716 | unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf, |
| 1717 | unsigned int buflen) |
| 1718 | { |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 1719 | struct ata_device *dev = args->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | u8 *scsicmd = args->cmd->cmnd, *p, *last; |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 1721 | const u8 sat_blk_desc[] = { |
| 1722 | 0, 0, 0, 0, /* number of blocks: sat unspecified */ |
| 1723 | 0, |
| 1724 | 0, 0x2, 0x0 /* block length: 512 bytes */ |
| 1725 | }; |
| 1726 | u8 pg, spg; |
| 1727 | unsigned int ebd, page_control, six_byte, output_len, alloc_len, minlen; |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 1728 | u8 dpofua; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1729 | |
| 1730 | VPRINTK("ENTER\n"); |
| 1731 | |
| 1732 | six_byte = (scsicmd[0] == MODE_SENSE); |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 1733 | ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */ |
| 1734 | /* |
| 1735 | * LLBA bit in msense(10) ignored (compliant) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | */ |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 1737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | page_control = scsicmd[2] >> 6; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1739 | switch (page_control) { |
| 1740 | case 0: /* current */ |
| 1741 | break; /* supported */ |
| 1742 | case 3: /* saved */ |
| 1743 | goto saving_not_supp; |
| 1744 | case 1: /* changeable */ |
| 1745 | case 2: /* defaults */ |
| 1746 | default: |
| 1747 | goto invalid_fld; |
| 1748 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1749 | |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 1750 | if (six_byte) { |
| 1751 | output_len = 4 + (ebd ? 8 : 0); |
| 1752 | alloc_len = scsicmd[4]; |
| 1753 | } else { |
| 1754 | output_len = 8 + (ebd ? 8 : 0); |
| 1755 | alloc_len = (scsicmd[7] << 8) + scsicmd[8]; |
| 1756 | } |
| 1757 | minlen = (alloc_len < buflen) ? alloc_len : buflen; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1758 | |
| 1759 | p = rbuf + output_len; |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 1760 | last = rbuf + minlen - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 1762 | pg = scsicmd[2] & 0x3f; |
| 1763 | spg = scsicmd[3]; |
| 1764 | /* |
| 1765 | * No mode subpages supported (yet) but asking for _all_ |
| 1766 | * subpages may be valid |
| 1767 | */ |
| 1768 | if (spg && (spg != ALL_SUB_MPAGES)) |
| 1769 | goto invalid_fld; |
| 1770 | |
| 1771 | switch(pg) { |
| 1772 | case RW_RECOVERY_MPAGE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1773 | output_len += ata_msense_rw_recovery(&p, last); |
| 1774 | break; |
| 1775 | |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 1776 | case CACHE_MPAGE: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | output_len += ata_msense_caching(args->id, &p, last); |
| 1778 | break; |
| 1779 | |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 1780 | case CONTROL_MPAGE: { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1781 | output_len += ata_msense_ctl_mode(&p, last); |
| 1782 | break; |
| 1783 | } |
| 1784 | |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 1785 | case ALL_MPAGES: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | output_len += ata_msense_rw_recovery(&p, last); |
| 1787 | output_len += ata_msense_caching(args->id, &p, last); |
| 1788 | output_len += ata_msense_ctl_mode(&p, last); |
| 1789 | break; |
| 1790 | |
| 1791 | default: /* invalid page code */ |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1792 | goto invalid_fld; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | } |
| 1794 | |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 1795 | if (minlen < 1) |
| 1796 | return 0; |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 1797 | |
| 1798 | dpofua = 0; |
Jens Axboe | 48bdc8e | 2006-01-30 16:09:35 +0100 | [diff] [blame] | 1799 | if (ata_dev_supports_fua(args->id) && dev->flags & ATA_DFLAG_LBA48 && |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 1800 | (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count)) |
| 1801 | dpofua = 1 << 4; |
| 1802 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1803 | if (six_byte) { |
| 1804 | output_len--; |
| 1805 | rbuf[0] = output_len; |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 1806 | if (minlen > 2) |
| 1807 | rbuf[2] |= dpofua; |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 1808 | if (ebd) { |
| 1809 | if (minlen > 3) |
| 1810 | rbuf[3] = sizeof(sat_blk_desc); |
| 1811 | if (minlen > 11) |
| 1812 | memcpy(rbuf + 4, sat_blk_desc, |
| 1813 | sizeof(sat_blk_desc)); |
| 1814 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1815 | } else { |
| 1816 | output_len -= 2; |
| 1817 | rbuf[0] = output_len >> 8; |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 1818 | if (minlen > 1) |
| 1819 | rbuf[1] = output_len; |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 1820 | if (minlen > 3) |
| 1821 | rbuf[3] |= dpofua; |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 1822 | if (ebd) { |
| 1823 | if (minlen > 7) |
| 1824 | rbuf[7] = sizeof(sat_blk_desc); |
| 1825 | if (minlen > 15) |
| 1826 | memcpy(rbuf + 8, sat_blk_desc, |
| 1827 | sizeof(sat_blk_desc)); |
| 1828 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1829 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | return 0; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1831 | |
| 1832 | invalid_fld: |
| 1833 | ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0); |
| 1834 | /* "Invalid field in cbd" */ |
| 1835 | return 1; |
| 1836 | |
| 1837 | saving_not_supp: |
| 1838 | ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0); |
| 1839 | /* "Saving parameters not supported" */ |
| 1840 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | } |
| 1842 | |
| 1843 | /** |
| 1844 | * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands |
| 1845 | * @args: device IDENTIFY data / SCSI command of interest. |
| 1846 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
| 1847 | * @buflen: Response buffer length. |
| 1848 | * |
| 1849 | * Simulate READ CAPACITY commands. |
| 1850 | * |
| 1851 | * LOCKING: |
| 1852 | * spin_lock_irqsave(host_set lock) |
| 1853 | */ |
| 1854 | |
| 1855 | unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf, |
| 1856 | unsigned int buflen) |
| 1857 | { |
| 1858 | u64 n_sectors; |
| 1859 | u32 tmp; |
| 1860 | |
| 1861 | VPRINTK("ENTER\n"); |
| 1862 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1863 | if (ata_id_has_lba(args->id)) { |
| 1864 | if (ata_id_has_lba48(args->id)) |
| 1865 | n_sectors = ata_id_u64(args->id, 100); |
| 1866 | else |
| 1867 | n_sectors = ata_id_u32(args->id, 60); |
| 1868 | } else { |
| 1869 | /* CHS default translation */ |
| 1870 | n_sectors = args->id[1] * args->id[3] * args->id[6]; |
| 1871 | |
| 1872 | if (ata_id_current_chs_valid(args->id)) |
| 1873 | /* CHS current translation */ |
| 1874 | n_sectors = ata_id_u32(args->id, 57); |
| 1875 | } |
| 1876 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1877 | n_sectors--; /* ATA TotalUserSectors - 1 */ |
| 1878 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1879 | if (args->cmd->cmnd[0] == READ_CAPACITY) { |
Philip Pokorny | 0c144d0 | 2005-05-28 01:24:47 -0700 | [diff] [blame] | 1880 | if( n_sectors >= 0xffffffffULL ) |
| 1881 | tmp = 0xffffffff ; /* Return max count on overflow */ |
| 1882 | else |
| 1883 | tmp = n_sectors ; |
| 1884 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1885 | /* sector count, 32-bit */ |
| 1886 | rbuf[0] = tmp >> (8 * 3); |
| 1887 | rbuf[1] = tmp >> (8 * 2); |
| 1888 | rbuf[2] = tmp >> (8 * 1); |
| 1889 | rbuf[3] = tmp; |
| 1890 | |
| 1891 | /* sector size */ |
| 1892 | tmp = ATA_SECT_SIZE; |
| 1893 | rbuf[6] = tmp >> 8; |
| 1894 | rbuf[7] = tmp; |
| 1895 | |
| 1896 | } else { |
| 1897 | /* sector count, 64-bit */ |
Philip Pokorny | 0c144d0 | 2005-05-28 01:24:47 -0700 | [diff] [blame] | 1898 | tmp = n_sectors >> (8 * 4); |
| 1899 | rbuf[2] = tmp >> (8 * 3); |
| 1900 | rbuf[3] = tmp >> (8 * 2); |
| 1901 | rbuf[4] = tmp >> (8 * 1); |
| 1902 | rbuf[5] = tmp; |
| 1903 | tmp = n_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1904 | rbuf[6] = tmp >> (8 * 3); |
| 1905 | rbuf[7] = tmp >> (8 * 2); |
| 1906 | rbuf[8] = tmp >> (8 * 1); |
| 1907 | rbuf[9] = tmp; |
| 1908 | |
| 1909 | /* sector size */ |
| 1910 | tmp = ATA_SECT_SIZE; |
| 1911 | rbuf[12] = tmp >> 8; |
| 1912 | rbuf[13] = tmp; |
| 1913 | } |
| 1914 | |
| 1915 | return 0; |
| 1916 | } |
| 1917 | |
| 1918 | /** |
| 1919 | * ata_scsiop_report_luns - Simulate REPORT LUNS command |
| 1920 | * @args: device IDENTIFY data / SCSI command of interest. |
| 1921 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
| 1922 | * @buflen: Response buffer length. |
| 1923 | * |
| 1924 | * Simulate REPORT LUNS command. |
| 1925 | * |
| 1926 | * LOCKING: |
| 1927 | * spin_lock_irqsave(host_set lock) |
| 1928 | */ |
| 1929 | |
| 1930 | unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf, |
| 1931 | unsigned int buflen) |
| 1932 | { |
| 1933 | VPRINTK("ENTER\n"); |
| 1934 | rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */ |
| 1935 | |
| 1936 | return 0; |
| 1937 | } |
| 1938 | |
| 1939 | /** |
Douglas Gilbert | 845c583 | 2005-10-09 08:55:41 -0400 | [diff] [blame] | 1940 | * ata_scsi_set_sense - Set SCSI sense data and status |
| 1941 | * @cmd: SCSI request to be handled |
| 1942 | * @sk: SCSI-defined sense key |
| 1943 | * @asc: SCSI-defined additional sense code |
| 1944 | * @ascq: SCSI-defined additional sense code qualifier |
| 1945 | * |
| 1946 | * Helper function that builds a valid fixed format, current |
| 1947 | * response code and the given sense key (sk), additional sense |
| 1948 | * code (asc) and additional sense code qualifier (ascq) with |
| 1949 | * a SCSI command status of %SAM_STAT_CHECK_CONDITION and |
| 1950 | * DRIVER_SENSE set in the upper bits of scsi_cmnd::result . |
| 1951 | * |
| 1952 | * LOCKING: |
| 1953 | * Not required |
| 1954 | */ |
| 1955 | |
| 1956 | void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq) |
| 1957 | { |
| 1958 | cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; |
| 1959 | |
| 1960 | cmd->sense_buffer[0] = 0x70; /* fixed format, current */ |
| 1961 | cmd->sense_buffer[2] = sk; |
| 1962 | cmd->sense_buffer[7] = 18 - 8; /* additional sense length */ |
| 1963 | cmd->sense_buffer[12] = asc; |
| 1964 | cmd->sense_buffer[13] = ascq; |
| 1965 | } |
| 1966 | |
| 1967 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1968 | * ata_scsi_badcmd - End a SCSI request with an error |
| 1969 | * @cmd: SCSI request to be handled |
| 1970 | * @done: SCSI command completion function |
| 1971 | * @asc: SCSI-defined additional sense code |
| 1972 | * @ascq: SCSI-defined additional sense code qualifier |
| 1973 | * |
| 1974 | * Helper function that completes a SCSI command with |
| 1975 | * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST |
| 1976 | * and the specified additional sense codes. |
| 1977 | * |
| 1978 | * LOCKING: |
| 1979 | * spin_lock_irqsave(host_set lock) |
| 1980 | */ |
| 1981 | |
| 1982 | void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq) |
| 1983 | { |
| 1984 | DPRINTK("ENTER\n"); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1985 | ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1986 | |
| 1987 | done(cmd); |
| 1988 | } |
| 1989 | |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 1990 | static void atapi_sense_complete(struct ata_queued_cmd *qc) |
Jeff Garzik | a939c96 | 2005-10-05 17:09:16 -0400 | [diff] [blame] | 1991 | { |
Tejun Heo | 74e6c8c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 1992 | if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) { |
Jeff Garzik | c6e6e666 | 2005-11-14 14:50:05 -0500 | [diff] [blame] | 1993 | /* FIXME: not quite right; we don't want the |
| 1994 | * translation of taskfile registers into |
| 1995 | * a sense descriptors, since that's only |
| 1996 | * correct for ATA, not ATAPI |
| 1997 | */ |
| 1998 | ata_gen_ata_desc_sense(qc); |
Tejun Heo | 74e6c8c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 1999 | } |
Jeff Garzik | c6e6e666 | 2005-11-14 14:50:05 -0500 | [diff] [blame] | 2000 | |
| 2001 | qc->scsidone(qc->scsicmd); |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 2002 | ata_qc_free(qc); |
Jeff Garzik | c6e6e666 | 2005-11-14 14:50:05 -0500 | [diff] [blame] | 2003 | } |
| 2004 | |
| 2005 | /* is it pointless to prefer PIO for "safety reasons"? */ |
| 2006 | static inline int ata_pio_use_silly(struct ata_port *ap) |
| 2007 | { |
| 2008 | return (ap->flags & ATA_FLAG_PIO_DMA); |
| 2009 | } |
| 2010 | |
| 2011 | static void atapi_request_sense(struct ata_queued_cmd *qc) |
| 2012 | { |
| 2013 | struct ata_port *ap = qc->ap; |
| 2014 | struct scsi_cmnd *cmd = qc->scsicmd; |
Jeff Garzik | a939c96 | 2005-10-05 17:09:16 -0400 | [diff] [blame] | 2015 | |
| 2016 | DPRINTK("ATAPI request sense\n"); |
| 2017 | |
Jeff Garzik | a939c96 | 2005-10-05 17:09:16 -0400 | [diff] [blame] | 2018 | /* FIXME: is this needed? */ |
| 2019 | memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer)); |
| 2020 | |
Jeff Garzik | c6e6e666 | 2005-11-14 14:50:05 -0500 | [diff] [blame] | 2021 | ap->ops->tf_read(ap, &qc->tf); |
| 2022 | |
| 2023 | /* fill these in, for the case where they are -not- overwritten */ |
| 2024 | cmd->sense_buffer[0] = 0x70; |
| 2025 | cmd->sense_buffer[2] = qc->tf.feature >> 4; |
| 2026 | |
| 2027 | ata_qc_reinit(qc); |
| 2028 | |
Jeff Garzik | a939c96 | 2005-10-05 17:09:16 -0400 | [diff] [blame] | 2029 | ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer)); |
| 2030 | qc->dma_dir = DMA_FROM_DEVICE; |
| 2031 | |
Tejun Heo | 6e7846e | 2006-02-12 23:32:58 +0900 | [diff] [blame] | 2032 | memset(&qc->cdb, 0, qc->dev->cdb_len); |
Jeff Garzik | a939c96 | 2005-10-05 17:09:16 -0400 | [diff] [blame] | 2033 | qc->cdb[0] = REQUEST_SENSE; |
| 2034 | qc->cdb[4] = SCSI_SENSE_BUFFERSIZE; |
| 2035 | |
| 2036 | qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 2037 | qc->tf.command = ATA_CMD_PACKET; |
| 2038 | |
Jeff Garzik | c6e6e666 | 2005-11-14 14:50:05 -0500 | [diff] [blame] | 2039 | if (ata_pio_use_silly(ap)) { |
| 2040 | qc->tf.protocol = ATA_PROT_ATAPI_DMA; |
| 2041 | qc->tf.feature |= ATAPI_PKT_DMA; |
| 2042 | } else { |
| 2043 | qc->tf.protocol = ATA_PROT_ATAPI; |
| 2044 | qc->tf.lbam = (8 * 1024) & 0xff; |
| 2045 | qc->tf.lbah = (8 * 1024) >> 8; |
| 2046 | } |
Jeff Garzik | a939c96 | 2005-10-05 17:09:16 -0400 | [diff] [blame] | 2047 | qc->nbytes = SCSI_SENSE_BUFFERSIZE; |
| 2048 | |
Jeff Garzik | c6e6e666 | 2005-11-14 14:50:05 -0500 | [diff] [blame] | 2049 | qc->complete_fn = atapi_sense_complete; |
Jeff Garzik | a939c96 | 2005-10-05 17:09:16 -0400 | [diff] [blame] | 2050 | |
Tejun Heo | 8e0e694 | 2006-03-31 20:41:11 +0900 | [diff] [blame] | 2051 | ata_qc_issue(qc); |
Jeff Garzik | a939c96 | 2005-10-05 17:09:16 -0400 | [diff] [blame] | 2052 | |
| 2053 | DPRINTK("EXIT\n"); |
| 2054 | } |
| 2055 | |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 2056 | static void atapi_qc_complete(struct ata_queued_cmd *qc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2057 | { |
| 2058 | struct scsi_cmnd *cmd = qc->scsicmd; |
Albert Lee | a22e2eb | 2005-12-05 15:38:02 +0800 | [diff] [blame] | 2059 | unsigned int err_mask = qc->err_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2060 | |
Jeff Garzik | a7dac44 | 2005-10-30 04:44:42 -0500 | [diff] [blame] | 2061 | VPRINTK("ENTER, err_mask 0x%X\n", err_mask); |
Jeff Garzik | e12669e | 2005-10-05 18:39:23 -0400 | [diff] [blame] | 2062 | |
Jeff Garzik | a7dac44 | 2005-10-30 04:44:42 -0500 | [diff] [blame] | 2063 | if (unlikely(err_mask & AC_ERR_DEV)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2064 | cmd->result = SAM_STAT_CHECK_CONDITION; |
Jeff Garzik | c6e6e666 | 2005-11-14 14:50:05 -0500 | [diff] [blame] | 2065 | atapi_request_sense(qc); |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 2066 | return; |
Tejun Heo | 74e6c8c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 2067 | } else if (unlikely(err_mask)) { |
Jeff Garzik | a7dac44 | 2005-10-30 04:44:42 -0500 | [diff] [blame] | 2068 | /* FIXME: not quite right; we don't want the |
| 2069 | * translation of taskfile registers into |
| 2070 | * a sense descriptors, since that's only |
| 2071 | * correct for ATA, not ATAPI |
| 2072 | */ |
| 2073 | ata_gen_ata_desc_sense(qc); |
Tejun Heo | 74e6c8c | 2006-04-02 18:51:53 +0900 | [diff] [blame] | 2074 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2075 | u8 *scsicmd = cmd->cmnd; |
| 2076 | |
Tony Battersby | fd71da4 | 2005-12-21 16:35:44 -0500 | [diff] [blame] | 2077 | if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2078 | u8 *buf = NULL; |
| 2079 | unsigned int buflen; |
| 2080 | |
| 2081 | buflen = ata_scsi_rbuf_get(cmd, &buf); |
Jeff Garzik | a15dbeb | 2005-10-05 15:02:14 -0400 | [diff] [blame] | 2082 | |
| 2083 | /* ATAPI devices typically report zero for their SCSI version, |
| 2084 | * and sometimes deviate from the spec WRT response data |
| 2085 | * format. If SCSI version is reported as zero like normal, |
| 2086 | * then we make the following fixups: 1) Fake MMC-5 version, |
| 2087 | * to indicate to the Linux scsi midlayer this is a modern |
| 2088 | * device. 2) Ensure response data format / ATAPI information |
| 2089 | * are always correct. |
| 2090 | */ |
Jeff Garzik | a15dbeb | 2005-10-05 15:02:14 -0400 | [diff] [blame] | 2091 | if (buf[2] == 0) { |
| 2092 | buf[2] = 0x5; |
| 2093 | buf[3] = 0x32; |
| 2094 | } |
| 2095 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | ata_scsi_rbuf_put(cmd, buf); |
| 2097 | } |
Jeff Garzik | a15dbeb | 2005-10-05 15:02:14 -0400 | [diff] [blame] | 2098 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2099 | cmd->result = SAM_STAT_GOOD; |
| 2100 | } |
| 2101 | |
| 2102 | qc->scsidone(cmd); |
Tejun Heo | 77853bf | 2006-01-23 13:09:36 +0900 | [diff] [blame] | 2103 | ata_qc_free(qc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | } |
| 2105 | /** |
| 2106 | * atapi_xlat - Initialize PACKET taskfile |
| 2107 | * @qc: command structure to be initialized |
| 2108 | * @scsicmd: SCSI CDB associated with this PACKET command |
| 2109 | * |
| 2110 | * LOCKING: |
| 2111 | * spin_lock_irqsave(host_set lock) |
| 2112 | * |
| 2113 | * RETURNS: |
| 2114 | * Zero on success, non-zero on failure. |
| 2115 | */ |
| 2116 | |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 2117 | static unsigned int atapi_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | { |
| 2119 | struct scsi_cmnd *cmd = qc->scsicmd; |
| 2120 | struct ata_device *dev = qc->dev; |
| 2121 | int using_pio = (dev->flags & ATA_DFLAG_PIO); |
| be7db05 | 2005-04-17 15:26:13 -0500 | [diff] [blame] | 2122 | int nodata = (cmd->sc_data_direction == DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2123 | |
| 2124 | if (!using_pio) |
| 2125 | /* Check whether ATAPI DMA is safe */ |
| 2126 | if (ata_check_atapi_dma(qc)) |
| 2127 | using_pio = 1; |
| 2128 | |
Tejun Heo | 6e7846e | 2006-02-12 23:32:58 +0900 | [diff] [blame] | 2129 | memcpy(&qc->cdb, scsicmd, dev->cdb_len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2130 | |
| 2131 | qc->complete_fn = atapi_qc_complete; |
| 2132 | |
| 2133 | qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| be7db05 | 2005-04-17 15:26:13 -0500 | [diff] [blame] | 2134 | if (cmd->sc_data_direction == DMA_TO_DEVICE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2135 | qc->tf.flags |= ATA_TFLAG_WRITE; |
| 2136 | DPRINTK("direction: write\n"); |
| 2137 | } |
| 2138 | |
| 2139 | qc->tf.command = ATA_CMD_PACKET; |
| 2140 | |
| 2141 | /* no data, or PIO data xfer */ |
| 2142 | if (using_pio || nodata) { |
| 2143 | if (nodata) |
| 2144 | qc->tf.protocol = ATA_PROT_ATAPI_NODATA; |
| 2145 | else |
| 2146 | qc->tf.protocol = ATA_PROT_ATAPI; |
| 2147 | qc->tf.lbam = (8 * 1024) & 0xff; |
| 2148 | qc->tf.lbah = (8 * 1024) >> 8; |
| 2149 | } |
| 2150 | |
| 2151 | /* DMA data xfer */ |
| 2152 | else { |
| 2153 | qc->tf.protocol = ATA_PROT_ATAPI_DMA; |
| 2154 | qc->tf.feature |= ATAPI_PKT_DMA; |
| 2155 | |
Albert Lee | 95de719 | 2006-04-04 10:57:18 +0800 | [diff] [blame] | 2156 | if (atapi_dmadir && (cmd->sc_data_direction != DMA_TO_DEVICE)) |
| 2157 | /* some SATA bridges need us to indicate data xfer direction */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2158 | qc->tf.feature |= ATAPI_DMADIR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | qc->nbytes = cmd->bufflen; |
| 2162 | |
| 2163 | return 0; |
| 2164 | } |
| 2165 | |
| 2166 | /** |
| 2167 | * ata_scsi_find_dev - lookup ata_device from scsi_cmnd |
| 2168 | * @ap: ATA port to which the device is attached |
| 2169 | * @scsidev: SCSI device from which we derive the ATA device |
| 2170 | * |
| 2171 | * Given various information provided in struct scsi_cmnd, |
| 2172 | * map that onto an ATA bus, and using that mapping |
| 2173 | * determine which ata_device is associated with the |
| 2174 | * SCSI command to be sent. |
| 2175 | * |
| 2176 | * LOCKING: |
| 2177 | * spin_lock_irqsave(host_set lock) |
| 2178 | * |
| 2179 | * RETURNS: |
| 2180 | * Associated ATA device, or %NULL if not found. |
| 2181 | */ |
| 2182 | |
| 2183 | static struct ata_device * |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 2184 | ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | { |
| 2186 | struct ata_device *dev; |
| 2187 | |
| 2188 | /* skip commands not addressed to targets we simulate */ |
| 2189 | if (likely(scsidev->id < ATA_MAX_DEVICES)) |
| 2190 | dev = &ap->device[scsidev->id]; |
| 2191 | else |
| 2192 | return NULL; |
| 2193 | |
| 2194 | if (unlikely((scsidev->channel != 0) || |
| 2195 | (scsidev->lun != 0))) |
| 2196 | return NULL; |
| 2197 | |
Tejun Heo | e1211e3 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2198 | if (unlikely(!ata_dev_enabled(dev))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2199 | return NULL; |
| 2200 | |
Jeff Garzik | 5063019 | 2005-12-13 02:29:45 -0500 | [diff] [blame] | 2201 | if (!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) { |
| 2202 | if (unlikely(dev->class == ATA_DEV_ATAPI)) { |
Tejun Heo | f15a1da | 2006-05-15 20:57:56 +0900 | [diff] [blame^] | 2203 | ata_dev_printk(dev, KERN_WARNING, |
| 2204 | "WARNING: ATAPI is %s, device ignored.\n", |
| 2205 | atapi_enabled ? "not supported with this driver" : "disabled"); |
Jeff Garzik | 1623c81 | 2005-08-30 03:37:42 -0400 | [diff] [blame] | 2206 | return NULL; |
Jeff Garzik | 5063019 | 2005-12-13 02:29:45 -0500 | [diff] [blame] | 2207 | } |
Jeff Garzik | 1623c81 | 2005-08-30 03:37:42 -0400 | [diff] [blame] | 2208 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | |
| 2210 | return dev; |
| 2211 | } |
| 2212 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2213 | /* |
| 2214 | * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value. |
| 2215 | * @byte1: Byte 1 from pass-thru CDB. |
| 2216 | * |
| 2217 | * RETURNS: |
| 2218 | * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise. |
| 2219 | */ |
| 2220 | static u8 |
| 2221 | ata_scsi_map_proto(u8 byte1) |
| 2222 | { |
| 2223 | switch((byte1 & 0x1e) >> 1) { |
| 2224 | case 3: /* Non-data */ |
| 2225 | return ATA_PROT_NODATA; |
| 2226 | |
| 2227 | case 6: /* DMA */ |
| 2228 | return ATA_PROT_DMA; |
| 2229 | |
| 2230 | case 4: /* PIO Data-in */ |
| 2231 | case 5: /* PIO Data-out */ |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2232 | return ATA_PROT_PIO; |
| 2233 | |
| 2234 | case 10: /* Device Reset */ |
| 2235 | case 0: /* Hard Reset */ |
| 2236 | case 1: /* SRST */ |
| 2237 | case 2: /* Bus Idle */ |
| 2238 | case 7: /* Packet */ |
| 2239 | case 8: /* DMA Queued */ |
| 2240 | case 9: /* Device Diagnostic */ |
| 2241 | case 11: /* UDMA Data-in */ |
| 2242 | case 12: /* UDMA Data-Out */ |
| 2243 | case 13: /* FPDMA */ |
| 2244 | default: /* Reserved */ |
| 2245 | break; |
| 2246 | } |
| 2247 | |
| 2248 | return ATA_PROT_UNKNOWN; |
| 2249 | } |
| 2250 | |
| 2251 | /** |
| 2252 | * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile |
| 2253 | * @qc: command structure to be initialized |
Randy Dunlap | 8e8b77d | 2005-11-01 21:29:27 -0800 | [diff] [blame] | 2254 | * @scsicmd: SCSI command to convert |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2255 | * |
| 2256 | * Handles either 12 or 16-byte versions of the CDB. |
| 2257 | * |
| 2258 | * RETURNS: |
| 2259 | * Zero on success, non-zero on failure. |
| 2260 | */ |
| 2261 | static unsigned int |
Douglas Gilbert | 00ac37f | 2005-10-28 15:58:28 -0400 | [diff] [blame] | 2262 | ata_scsi_pass_thru(struct ata_queued_cmd *qc, const u8 *scsicmd) |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2263 | { |
| 2264 | struct ata_taskfile *tf = &(qc->tf); |
| 2265 | struct scsi_cmnd *cmd = qc->scsicmd; |
| 2266 | |
| 2267 | if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN) |
Tejun Heo | 9a40525 | 2005-12-02 11:49:11 +0900 | [diff] [blame] | 2268 | goto invalid_fld; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2269 | |
Albert Lee | f59b0cf | 2006-03-16 17:59:22 +0800 | [diff] [blame] | 2270 | if (scsicmd[1] & 0xe0) |
| 2271 | /* PIO multi not supported yet */ |
| 2272 | goto invalid_fld; |
| 2273 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2274 | /* |
| 2275 | * 12 and 16 byte CDBs use different offsets to |
| 2276 | * provide the various register values. |
| 2277 | */ |
| 2278 | if (scsicmd[0] == ATA_16) { |
| 2279 | /* |
| 2280 | * 16-byte CDB - may contain extended commands. |
| 2281 | * |
| 2282 | * If that is the case, copy the upper byte register values. |
| 2283 | */ |
| 2284 | if (scsicmd[1] & 0x01) { |
| 2285 | tf->hob_feature = scsicmd[3]; |
| 2286 | tf->hob_nsect = scsicmd[5]; |
| 2287 | tf->hob_lbal = scsicmd[7]; |
| 2288 | tf->hob_lbam = scsicmd[9]; |
| 2289 | tf->hob_lbah = scsicmd[11]; |
| 2290 | tf->flags |= ATA_TFLAG_LBA48; |
| 2291 | } else |
| 2292 | tf->flags &= ~ATA_TFLAG_LBA48; |
| 2293 | |
| 2294 | /* |
| 2295 | * Always copy low byte, device and command registers. |
| 2296 | */ |
| 2297 | tf->feature = scsicmd[4]; |
| 2298 | tf->nsect = scsicmd[6]; |
| 2299 | tf->lbal = scsicmd[8]; |
| 2300 | tf->lbam = scsicmd[10]; |
| 2301 | tf->lbah = scsicmd[12]; |
| 2302 | tf->device = scsicmd[13]; |
| 2303 | tf->command = scsicmd[14]; |
| 2304 | } else { |
| 2305 | /* |
| 2306 | * 12-byte CDB - incapable of extended commands. |
| 2307 | */ |
| 2308 | tf->flags &= ~ATA_TFLAG_LBA48; |
| 2309 | |
| 2310 | tf->feature = scsicmd[3]; |
| 2311 | tf->nsect = scsicmd[4]; |
| 2312 | tf->lbal = scsicmd[5]; |
| 2313 | tf->lbam = scsicmd[6]; |
| 2314 | tf->lbah = scsicmd[7]; |
| 2315 | tf->device = scsicmd[8]; |
| 2316 | tf->command = scsicmd[9]; |
| 2317 | } |
Mark Lord | dcc2d1e | 2005-11-13 16:22:06 -0500 | [diff] [blame] | 2318 | /* |
| 2319 | * If slave is possible, enforce correct master/slave bit |
| 2320 | */ |
| 2321 | if (qc->ap->flags & ATA_FLAG_SLAVE_POSS) |
| 2322 | tf->device = qc->dev->devno ? |
| 2323 | tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2324 | |
| 2325 | /* |
| 2326 | * Filter SET_FEATURES - XFER MODE command -- otherwise, |
| 2327 | * SET_FEATURES - XFER MODE must be preceded/succeeded |
| 2328 | * by an update to hardware-specific registers for each |
| 2329 | * controller (i.e. the reason for ->set_piomode(), |
| 2330 | * ->set_dmamode(), and ->post_set_mode() hooks). |
| 2331 | */ |
| 2332 | if ((tf->command == ATA_CMD_SET_FEATURES) |
| 2333 | && (tf->feature == SETFEATURES_XFER)) |
Tejun Heo | 9a40525 | 2005-12-02 11:49:11 +0900 | [diff] [blame] | 2334 | goto invalid_fld; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2335 | |
| 2336 | /* |
| 2337 | * Set flags so that all registers will be written, |
| 2338 | * and pass on write indication (used for PIO/DMA |
| 2339 | * setup.) |
| 2340 | */ |
| 2341 | tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE); |
| 2342 | |
Jeff Garzik | 0274aa2 | 2005-06-22 13:50:56 -0400 | [diff] [blame] | 2343 | if (cmd->sc_data_direction == DMA_TO_DEVICE) |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2344 | tf->flags |= ATA_TFLAG_WRITE; |
| 2345 | |
| 2346 | /* |
| 2347 | * Set transfer length. |
| 2348 | * |
| 2349 | * TODO: find out if we need to do more here to |
| 2350 | * cover scatter/gather case. |
| 2351 | */ |
| 2352 | qc->nsect = cmd->bufflen / ATA_SECT_SIZE; |
| 2353 | |
Tejun Heo | e61e067 | 2006-05-15 20:57:40 +0900 | [diff] [blame] | 2354 | /* request result TF */ |
| 2355 | qc->flags |= ATA_QCFLAG_RESULT_TF; |
| 2356 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2357 | return 0; |
Tejun Heo | 9a40525 | 2005-12-02 11:49:11 +0900 | [diff] [blame] | 2358 | |
| 2359 | invalid_fld: |
| 2360 | ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x00); |
| 2361 | /* "Invalid field in cdb" */ |
| 2362 | return 1; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2363 | } |
| 2364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | /** |
| 2366 | * ata_get_xlat_func - check if SCSI to ATA translation is possible |
| 2367 | * @dev: ATA device |
| 2368 | * @cmd: SCSI command opcode to consider |
| 2369 | * |
| 2370 | * Look up the SCSI command given, and determine whether the |
| 2371 | * SCSI command is to be translated or simulated. |
| 2372 | * |
| 2373 | * RETURNS: |
| 2374 | * Pointer to translation function if possible, %NULL if not. |
| 2375 | */ |
| 2376 | |
| 2377 | static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd) |
| 2378 | { |
| 2379 | switch (cmd) { |
| 2380 | case READ_6: |
| 2381 | case READ_10: |
| 2382 | case READ_16: |
| 2383 | |
| 2384 | case WRITE_6: |
| 2385 | case WRITE_10: |
| 2386 | case WRITE_16: |
| 2387 | return ata_scsi_rw_xlat; |
| 2388 | |
| 2389 | case SYNCHRONIZE_CACHE: |
| 2390 | if (ata_try_flush_cache(dev)) |
| 2391 | return ata_scsi_flush_xlat; |
| 2392 | break; |
| 2393 | |
| 2394 | case VERIFY: |
| 2395 | case VERIFY_16: |
| 2396 | return ata_scsi_verify_xlat; |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2397 | |
| 2398 | case ATA_12: |
| 2399 | case ATA_16: |
| 2400 | return ata_scsi_pass_thru; |
Jeff Garzik | da61396 | 2005-08-29 19:01:43 -0400 | [diff] [blame] | 2401 | |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 2402 | case START_STOP: |
| 2403 | return ata_scsi_start_stop_xlat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | } |
| 2405 | |
| 2406 | return NULL; |
| 2407 | } |
| 2408 | |
| 2409 | /** |
| 2410 | * ata_scsi_dump_cdb - dump SCSI command contents to dmesg |
| 2411 | * @ap: ATA port to which the command was being sent |
| 2412 | * @cmd: SCSI command to dump |
| 2413 | * |
| 2414 | * Prints the contents of a SCSI command via printk(). |
| 2415 | */ |
| 2416 | |
| 2417 | static inline void ata_scsi_dump_cdb(struct ata_port *ap, |
| 2418 | struct scsi_cmnd *cmd) |
| 2419 | { |
| 2420 | #ifdef ATA_DEBUG |
| 2421 | struct scsi_device *scsidev = cmd->device; |
| 2422 | u8 *scsicmd = cmd->cmnd; |
| 2423 | |
| 2424 | DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", |
| 2425 | ap->id, |
| 2426 | scsidev->channel, scsidev->id, scsidev->lun, |
| 2427 | scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3], |
| 2428 | scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7], |
| 2429 | scsicmd[8]); |
| 2430 | #endif |
| 2431 | } |
| 2432 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 2433 | static inline void __ata_scsi_queuecmd(struct scsi_cmnd *cmd, |
| 2434 | void (*done)(struct scsi_cmnd *), |
| 2435 | struct ata_device *dev) |
Brian King | eb3f0f9 | 2006-03-23 17:30:02 -0600 | [diff] [blame] | 2436 | { |
| 2437 | if (dev->class == ATA_DEV_ATA) { |
| 2438 | ata_xlat_func_t xlat_func = ata_get_xlat_func(dev, |
| 2439 | cmd->cmnd[0]); |
| 2440 | |
| 2441 | if (xlat_func) |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 2442 | ata_scsi_translate(dev, cmd, done, xlat_func); |
Brian King | eb3f0f9 | 2006-03-23 17:30:02 -0600 | [diff] [blame] | 2443 | else |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 2444 | ata_scsi_simulate(dev, cmd, done); |
Brian King | eb3f0f9 | 2006-03-23 17:30:02 -0600 | [diff] [blame] | 2445 | } else |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 2446 | ata_scsi_translate(dev, cmd, done, atapi_xlat); |
Brian King | eb3f0f9 | 2006-03-23 17:30:02 -0600 | [diff] [blame] | 2447 | } |
| 2448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2449 | /** |
| 2450 | * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device |
| 2451 | * @cmd: SCSI command to be sent |
| 2452 | * @done: Completion function, called when command is complete |
| 2453 | * |
| 2454 | * In some cases, this function translates SCSI commands into |
| 2455 | * ATA taskfiles, and queues the taskfiles to be sent to |
| 2456 | * hardware. In other cases, this function simulates a |
| 2457 | * SCSI device by evaluating and responding to certain |
| 2458 | * SCSI commands. This creates the overall effect of |
| 2459 | * ATA and ATAPI devices appearing as SCSI devices. |
| 2460 | * |
| 2461 | * LOCKING: |
| 2462 | * Releases scsi-layer-held lock, and obtains host_set lock. |
| 2463 | * |
| 2464 | * RETURNS: |
| 2465 | * Zero. |
| 2466 | */ |
| 2467 | |
| 2468 | int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *)) |
| 2469 | { |
| 2470 | struct ata_port *ap; |
| 2471 | struct ata_device *dev; |
| 2472 | struct scsi_device *scsidev = cmd->device; |
Jeff Garzik | 005a5a0 | 2005-10-30 23:31:48 -0500 | [diff] [blame] | 2473 | struct Scsi_Host *shost = scsidev->host; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2474 | |
Jeff Garzik | 35bb94b | 2006-04-11 13:12:34 -0400 | [diff] [blame] | 2475 | ap = ata_shost_to_port(shost); |
Jeff Garzik | 005a5a0 | 2005-10-30 23:31:48 -0500 | [diff] [blame] | 2476 | |
| 2477 | spin_unlock(shost->host_lock); |
| 2478 | spin_lock(&ap->host_set->lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2479 | |
| 2480 | ata_scsi_dump_cdb(ap, cmd); |
| 2481 | |
| 2482 | dev = ata_scsi_find_dev(ap, scsidev); |
Brian King | eb3f0f9 | 2006-03-23 17:30:02 -0600 | [diff] [blame] | 2483 | if (likely(dev)) |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 2484 | __ata_scsi_queuecmd(cmd, done, dev); |
Brian King | eb3f0f9 | 2006-03-23 17:30:02 -0600 | [diff] [blame] | 2485 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2486 | cmd->result = (DID_BAD_TARGET << 16); |
| 2487 | done(cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2488 | } |
| 2489 | |
Jeff Garzik | 005a5a0 | 2005-10-30 23:31:48 -0500 | [diff] [blame] | 2490 | spin_unlock(&ap->host_set->lock); |
| 2491 | spin_lock(shost->host_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | return 0; |
| 2493 | } |
| 2494 | |
| 2495 | /** |
| 2496 | * ata_scsi_simulate - simulate SCSI command on ATA device |
Randy Dunlap | c893a3a | 2006-01-28 13:15:32 -0500 | [diff] [blame] | 2497 | * @dev: the target device |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | * @cmd: SCSI command being sent to device. |
| 2499 | * @done: SCSI command completion function. |
| 2500 | * |
| 2501 | * Interprets and directly executes a select list of SCSI commands |
| 2502 | * that can be handled internally. |
| 2503 | * |
| 2504 | * LOCKING: |
| 2505 | * spin_lock_irqsave(host_set lock) |
| 2506 | */ |
| 2507 | |
Tejun Heo | 3373efd | 2006-05-15 20:57:53 +0900 | [diff] [blame] | 2508 | void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2509 | void (*done)(struct scsi_cmnd *)) |
| 2510 | { |
| 2511 | struct ata_scsi_args args; |
Jeff Garzik | 057ace5 | 2005-10-22 14:27:05 -0400 | [diff] [blame] | 2512 | const u8 *scsicmd = cmd->cmnd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2513 | |
Tejun Heo | 9a3dccc | 2006-01-06 09:56:18 +0100 | [diff] [blame] | 2514 | args.dev = dev; |
| 2515 | args.id = dev->id; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | args.cmd = cmd; |
| 2517 | args.done = done; |
| 2518 | |
| 2519 | switch(scsicmd[0]) { |
| 2520 | /* no-op's, complete with success */ |
| 2521 | case SYNCHRONIZE_CACHE: |
| 2522 | case REZERO_UNIT: |
| 2523 | case SEEK_6: |
| 2524 | case SEEK_10: |
| 2525 | case TEST_UNIT_READY: |
| 2526 | case FORMAT_UNIT: /* FIXME: correct? */ |
| 2527 | case SEND_DIAGNOSTIC: /* FIXME: correct? */ |
| 2528 | ata_scsi_rbuf_fill(&args, ata_scsiop_noop); |
| 2529 | break; |
| 2530 | |
| 2531 | case INQUIRY: |
| 2532 | if (scsicmd[1] & 2) /* is CmdDt set? */ |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 2533 | ata_scsi_invalid_field(cmd, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2534 | else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */ |
| 2535 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std); |
| 2536 | else if (scsicmd[2] == 0x00) |
| 2537 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00); |
| 2538 | else if (scsicmd[2] == 0x80) |
| 2539 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80); |
| 2540 | else if (scsicmd[2] == 0x83) |
| 2541 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83); |
| 2542 | else |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 2543 | ata_scsi_invalid_field(cmd, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2544 | break; |
| 2545 | |
| 2546 | case MODE_SENSE: |
| 2547 | case MODE_SENSE_10: |
| 2548 | ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense); |
| 2549 | break; |
| 2550 | |
| 2551 | case MODE_SELECT: /* unconditionally return */ |
| 2552 | case MODE_SELECT_10: /* bad-field-in-cdb */ |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 2553 | ata_scsi_invalid_field(cmd, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2554 | break; |
| 2555 | |
| 2556 | case READ_CAPACITY: |
| 2557 | ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap); |
| 2558 | break; |
| 2559 | |
| 2560 | case SERVICE_ACTION_IN: |
| 2561 | if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16) |
| 2562 | ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap); |
| 2563 | else |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 2564 | ata_scsi_invalid_field(cmd, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2565 | break; |
| 2566 | |
| 2567 | case REPORT_LUNS: |
| 2568 | ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns); |
| 2569 | break; |
| 2570 | |
Jeff Garzik | b095518 | 2005-05-12 15:45:22 -0400 | [diff] [blame] | 2571 | /* mandatory commands we haven't implemented yet */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2572 | case REQUEST_SENSE: |
| 2573 | |
| 2574 | /* all other commands */ |
| 2575 | default: |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 2576 | ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0); |
| 2577 | /* "Invalid command operation code" */ |
| 2578 | done(cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | break; |
| 2580 | } |
| 2581 | } |
| 2582 | |
Jeff Garzik | 644dd0c | 2005-10-03 15:55:19 -0400 | [diff] [blame] | 2583 | void ata_scsi_scan_host(struct ata_port *ap) |
| 2584 | { |
Jeff Garzik | 3f19ee8 | 2005-10-03 21:36:41 -0400 | [diff] [blame] | 2585 | struct ata_device *dev; |
Jeff Garzik | 644dd0c | 2005-10-03 15:55:19 -0400 | [diff] [blame] | 2586 | unsigned int i; |
| 2587 | |
Tejun Heo | 198e0fe | 2006-04-02 18:51:52 +0900 | [diff] [blame] | 2588 | if (ap->flags & ATA_FLAG_DISABLED) |
Jeff Garzik | 644dd0c | 2005-10-03 15:55:19 -0400 | [diff] [blame] | 2589 | return; |
| 2590 | |
Jeff Garzik | 3f19ee8 | 2005-10-03 21:36:41 -0400 | [diff] [blame] | 2591 | for (i = 0; i < ATA_MAX_DEVICES; i++) { |
| 2592 | dev = &ap->device[i]; |
| 2593 | |
Tejun Heo | e1211e3 | 2006-04-01 01:38:18 +0900 | [diff] [blame] | 2594 | if (ata_dev_enabled(dev)) |
Jeff Garzik | 3f19ee8 | 2005-10-03 21:36:41 -0400 | [diff] [blame] | 2595 | scsi_scan_target(&ap->host->shost_gendev, 0, i, 0, 0); |
| 2596 | } |
Jeff Garzik | 644dd0c | 2005-10-03 15:55:19 -0400 | [diff] [blame] | 2597 | } |
| 2598 | |