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> |
| 40 | #include "scsi.h" |
| 41 | #include <scsi/scsi_host.h> |
| 42 | #include <linux/libata.h> |
| 43 | #include <asm/uaccess.h> |
| 44 | |
| 45 | #include "libata.h" |
| 46 | |
| 47 | typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, u8 *scsicmd); |
| 48 | static struct ata_device * |
| 49 | ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev); |
| 50 | |
| 51 | |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 52 | static void ata_scsi_invalid_field(struct scsi_cmnd *cmd, |
| 53 | void (*done)(struct scsi_cmnd *)) |
| 54 | { |
| 55 | ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0); |
| 56 | /* "Invalid field in cbd" */ |
| 57 | done(cmd); |
| 58 | } |
| 59 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 60 | /** |
| 61 | * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd. |
| 62 | * @sdev: SCSI device for which BIOS geometry is to be determined |
| 63 | * @bdev: block device associated with @sdev |
| 64 | * @capacity: capacity of SCSI device |
| 65 | * @geom: location to which geometry will be output |
| 66 | * |
| 67 | * Generic bios head/sector/cylinder calculator |
| 68 | * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS) |
| 69 | * mapping. Some situations may arise where the disk is not |
| 70 | * bootable if this is not used. |
| 71 | * |
| 72 | * LOCKING: |
| 73 | * Defined by the SCSI layer. We don't really care. |
| 74 | * |
| 75 | * RETURNS: |
| 76 | * Zero. |
| 77 | */ |
| 78 | int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev, |
| 79 | sector_t capacity, int geom[]) |
| 80 | { |
| 81 | geom[0] = 255; |
| 82 | geom[1] = 63; |
| 83 | sector_div(capacity, 255*63); |
| 84 | geom[2] = capacity; |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg) |
| 90 | { |
| 91 | struct ata_port *ap; |
| 92 | struct ata_device *dev; |
| 93 | int val = -EINVAL, rc = -EINVAL; |
| 94 | |
| 95 | ap = (struct ata_port *) &scsidev->host->hostdata[0]; |
| 96 | if (!ap) |
| 97 | goto out; |
| 98 | |
| 99 | dev = ata_scsi_find_dev(ap, scsidev); |
| 100 | if (!dev) { |
| 101 | rc = -ENODEV; |
| 102 | goto out; |
| 103 | } |
| 104 | |
| 105 | switch (cmd) { |
| 106 | case ATA_IOC_GET_IO32: |
| 107 | val = 0; |
| 108 | if (copy_to_user(arg, &val, 1)) |
| 109 | return -EFAULT; |
| 110 | return 0; |
| 111 | |
| 112 | case ATA_IOC_SET_IO32: |
| 113 | val = (unsigned long) arg; |
| 114 | if (val != 0) |
| 115 | return -EINVAL; |
| 116 | return 0; |
| 117 | |
| 118 | default: |
| 119 | rc = -ENOTTY; |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | out: |
| 124 | return rc; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * ata_scsi_qc_new - acquire new ata_queued_cmd reference |
| 129 | * @ap: ATA port to which the new command is attached |
| 130 | * @dev: ATA device to which the new command is attached |
| 131 | * @cmd: SCSI command that originated this ATA command |
| 132 | * @done: SCSI command completion function |
| 133 | * |
| 134 | * Obtain a reference to an unused ata_queued_cmd structure, |
| 135 | * which is the basic libata structure representing a single |
| 136 | * ATA command sent to the hardware. |
| 137 | * |
| 138 | * If a command was available, fill in the SCSI-specific |
| 139 | * portions of the structure with information on the |
| 140 | * current command. |
| 141 | * |
| 142 | * LOCKING: |
| 143 | * spin_lock_irqsave(host_set lock) |
| 144 | * |
| 145 | * RETURNS: |
| 146 | * Command allocated, or %NULL if none available. |
| 147 | */ |
| 148 | struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap, |
| 149 | struct ata_device *dev, |
| 150 | struct scsi_cmnd *cmd, |
| 151 | void (*done)(struct scsi_cmnd *)) |
| 152 | { |
| 153 | struct ata_queued_cmd *qc; |
| 154 | |
| 155 | qc = ata_qc_new_init(ap, dev); |
| 156 | if (qc) { |
| 157 | qc->scsicmd = cmd; |
| 158 | qc->scsidone = done; |
| 159 | |
| 160 | if (cmd->use_sg) { |
| 161 | qc->sg = (struct scatterlist *) cmd->request_buffer; |
| 162 | qc->n_elem = cmd->use_sg; |
| 163 | } else { |
| 164 | qc->sg = &qc->sgent; |
| 165 | qc->n_elem = 1; |
| 166 | } |
| 167 | } else { |
| 168 | cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1); |
| 169 | done(cmd); |
| 170 | } |
| 171 | |
| 172 | return qc; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * ata_to_sense_error - convert ATA error to SCSI error |
| 177 | * @qc: Command that we are erroring out |
| 178 | * @drv_stat: value contained in ATA status register |
| 179 | * |
| 180 | * Converts an ATA error into a SCSI error. While we are at it |
| 181 | * we decode and dump the ATA error for the user so that they |
| 182 | * have some idea what really happened at the non make-believe |
| 183 | * layer. |
| 184 | * |
| 185 | * LOCKING: |
| 186 | * spin_lock_irqsave(host_set lock) |
| 187 | */ |
| 188 | |
| 189 | void ata_to_sense_error(struct ata_queued_cmd *qc, u8 drv_stat) |
| 190 | { |
| 191 | struct scsi_cmnd *cmd = qc->scsicmd; |
| 192 | u8 err = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | /* Based on the 3ware driver translation table */ |
| 194 | static unsigned char sense_table[][4] = { |
| 195 | /* BBD|ECC|ID|MAR */ |
| 196 | {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command |
| 197 | /* BBD|ECC|ID */ |
| 198 | {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command |
| 199 | /* ECC|MC|MARK */ |
| 200 | {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error |
| 201 | /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */ |
| 202 | {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error |
| 203 | /* MC|ID|ABRT|TRK0|MARK */ |
| 204 | {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready |
| 205 | /* MCR|MARK */ |
| 206 | {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready |
| 207 | /* Bad address mark */ |
| 208 | {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field |
| 209 | /* TRK0 */ |
| 210 | {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error |
| 211 | /* Abort & !ICRC */ |
| 212 | {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command |
| 213 | /* Media change request */ |
| 214 | {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline |
| 215 | /* SRV */ |
| 216 | {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found |
| 217 | /* Media change */ |
| 218 | {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline |
| 219 | /* ECC */ |
| 220 | {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error |
| 221 | /* BBD - block marked bad */ |
| 222 | {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error |
| 223 | {0xFF, 0xFF, 0xFF, 0xFF}, // END mark |
| 224 | }; |
| 225 | static unsigned char stat_table[][4] = { |
| 226 | /* Must be first because BUSY means no other bits valid */ |
| 227 | {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now |
| 228 | {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault |
| 229 | {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now |
| 230 | {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered |
| 231 | {0xFF, 0xFF, 0xFF, 0xFF}, // END mark |
| 232 | }; |
| 233 | int i = 0; |
| 234 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | /* |
| 236 | * Is this an error we can process/parse |
| 237 | */ |
| 238 | |
| 239 | if(drv_stat & ATA_ERR) |
| 240 | /* Read the err bits */ |
| 241 | err = ata_chk_err(qc->ap); |
| 242 | |
| 243 | /* Display the ATA level error info */ |
| 244 | |
| 245 | printk(KERN_WARNING "ata%u: status=0x%02x { ", qc->ap->id, drv_stat); |
| 246 | if(drv_stat & 0x80) |
| 247 | { |
| 248 | printk("Busy "); |
| 249 | err = 0; /* Data is not valid in this case */ |
| 250 | } |
| 251 | else { |
| 252 | if(drv_stat & 0x40) printk("DriveReady "); |
| 253 | if(drv_stat & 0x20) printk("DeviceFault "); |
| 254 | if(drv_stat & 0x10) printk("SeekComplete "); |
| 255 | if(drv_stat & 0x08) printk("DataRequest "); |
| 256 | if(drv_stat & 0x04) printk("CorrectedError "); |
| 257 | if(drv_stat & 0x02) printk("Index "); |
| 258 | if(drv_stat & 0x01) printk("Error "); |
| 259 | } |
| 260 | printk("}\n"); |
| 261 | |
| 262 | if(err) |
| 263 | { |
| 264 | printk(KERN_WARNING "ata%u: error=0x%02x { ", qc->ap->id, err); |
| 265 | if(err & 0x04) printk("DriveStatusError "); |
| 266 | if(err & 0x80) |
| 267 | { |
| 268 | if(err & 0x04) |
| 269 | printk("BadCRC "); |
| 270 | else |
| 271 | printk("Sector "); |
| 272 | } |
| 273 | if(err & 0x40) printk("UncorrectableError "); |
| 274 | if(err & 0x10) printk("SectorIdNotFound "); |
| 275 | if(err & 0x02) printk("TrackZeroNotFound "); |
| 276 | if(err & 0x01) printk("AddrMarkNotFound "); |
| 277 | printk("}\n"); |
| 278 | |
| 279 | /* Should we dump sector info here too ?? */ |
| 280 | } |
| 281 | |
| 282 | |
| 283 | /* Look for err */ |
| 284 | while(sense_table[i][0] != 0xFF) |
| 285 | { |
| 286 | /* Look for best matches first */ |
| 287 | if((sense_table[i][0] & err) == sense_table[i][0]) |
| 288 | { |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 289 | ata_scsi_set_sense(cmd, sense_table[i][1] /* sk */, |
| 290 | sense_table[i][2] /* asc */, |
| 291 | sense_table[i][3] /* ascq */ ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | return; |
| 293 | } |
| 294 | i++; |
| 295 | } |
| 296 | /* No immediate match */ |
| 297 | if(err) |
| 298 | printk(KERN_DEBUG "ata%u: no sense translation for 0x%02x\n", qc->ap->id, err); |
| 299 | |
| 300 | i = 0; |
| 301 | /* Fall back to interpreting status bits */ |
| 302 | while(stat_table[i][0] != 0xFF) |
| 303 | { |
| 304 | if(stat_table[i][0] & drv_stat) |
| 305 | { |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 306 | ata_scsi_set_sense(cmd, sense_table[i][1] /* sk */, |
| 307 | sense_table[i][2] /* asc */, |
| 308 | sense_table[i][3] /* ascq */ ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | return; |
| 310 | } |
| 311 | i++; |
| 312 | } |
| 313 | /* No error ?? */ |
| 314 | printk(KERN_ERR "ata%u: called with no error (%02X)!\n", qc->ap->id, drv_stat); |
| 315 | /* additional-sense-code[-qualifier] */ |
| 316 | |
| be7db05 | 2005-04-17 15:26:13 -0500 | [diff] [blame] | 317 | if (cmd->sc_data_direction == DMA_FROM_DEVICE) { |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 318 | ata_scsi_set_sense(cmd, MEDIUM_ERROR, 0x11, 0x4); |
| 319 | /* "unrecovered read error" */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | } else { |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 321 | ata_scsi_set_sense(cmd, MEDIUM_ERROR, 0xc, 0x2); |
| 322 | /* "write error - auto-reallocation failed" */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * ata_scsi_slave_config - Set SCSI device attributes |
| 328 | * @sdev: SCSI device to examine |
| 329 | * |
| 330 | * This is called before we actually start reading |
| 331 | * and writing to the device, to configure certain |
| 332 | * SCSI mid-layer behaviors. |
| 333 | * |
| 334 | * LOCKING: |
| 335 | * Defined by SCSI layer. We don't really care. |
| 336 | */ |
| 337 | |
| 338 | int ata_scsi_slave_config(struct scsi_device *sdev) |
| 339 | { |
| 340 | sdev->use_10_for_rw = 1; |
| 341 | sdev->use_10_for_ms = 1; |
| 342 | |
| 343 | blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD); |
| 344 | |
| 345 | if (sdev->id < ATA_MAX_DEVICES) { |
| 346 | struct ata_port *ap; |
| 347 | struct ata_device *dev; |
| 348 | |
| 349 | ap = (struct ata_port *) &sdev->host->hostdata[0]; |
| 350 | dev = &ap->device[sdev->id]; |
| 351 | |
| 352 | /* TODO: 1024 is an arbitrary number, not the |
| 353 | * hardware maximum. This should be increased to |
| 354 | * 65534 when Jens Axboe's patch for dynamically |
| 355 | * determining max_sectors is merged. |
| 356 | */ |
| 357 | if ((dev->flags & ATA_DFLAG_LBA48) && |
| 358 | ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) { |
John W. Linville | f85bdb9 | 2005-05-12 15:49:54 -0400 | [diff] [blame] | 359 | /* |
| 360 | * do not overwrite sdev->host->max_sectors, since |
| 361 | * other drives on this host may not support LBA48 |
| 362 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | blk_queue_max_sectors(sdev->request_queue, 2048); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | return 0; /* scsi layer doesn't check return value, sigh */ |
| 368 | } |
| 369 | |
| 370 | /** |
| 371 | * ata_scsi_error - SCSI layer error handler callback |
| 372 | * @host: SCSI host on which error occurred |
| 373 | * |
| 374 | * Handles SCSI-layer-thrown error events. |
| 375 | * |
| 376 | * LOCKING: |
| 377 | * Inherited from SCSI layer (none, can sleep) |
| 378 | * |
| 379 | * RETURNS: |
| 380 | * Zero. |
| 381 | */ |
| 382 | |
| 383 | int ata_scsi_error(struct Scsi_Host *host) |
| 384 | { |
| 385 | struct ata_port *ap; |
| 386 | |
| 387 | DPRINTK("ENTER\n"); |
| 388 | |
| 389 | ap = (struct ata_port *) &host->hostdata[0]; |
| 390 | ap->ops->eng_timeout(ap); |
| 391 | |
| 392 | /* TODO: this is per-command; when queueing is supported |
| 393 | * this code will either change or move to a more |
| 394 | * appropriate place |
| 395 | */ |
| 396 | host->host_failed--; |
Tejun Heo | 4251743 | 2005-08-10 13:38:27 -0400 | [diff] [blame] | 397 | INIT_LIST_HEAD(&host->eh_cmd_q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | |
| 399 | DPRINTK("EXIT\n"); |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | /** |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 404 | * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command |
| 405 | * @qc: Storage for translated ATA taskfile |
| 406 | * @scsicmd: SCSI command to translate |
| 407 | * |
| 408 | * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY |
| 409 | * (to start). Perhaps these commands should be preceded by |
| 410 | * CHECK POWER MODE to see what power mode the device is already in. |
| 411 | * [See SAT revision 5 at www.t10.org] |
| 412 | * |
| 413 | * LOCKING: |
| 414 | * spin_lock_irqsave(host_set lock) |
| 415 | * |
| 416 | * RETURNS: |
| 417 | * Zero on success, non-zero on error. |
| 418 | */ |
| 419 | |
| 420 | static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc, |
| 421 | u8 *scsicmd) |
| 422 | { |
| 423 | struct ata_taskfile *tf = &qc->tf; |
| 424 | |
| 425 | tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; |
| 426 | tf->protocol = ATA_PROT_NODATA; |
| 427 | if (scsicmd[1] & 0x1) { |
| 428 | ; /* ignore IMMED bit, violates sat-r05 */ |
| 429 | } |
| 430 | if (scsicmd[4] & 0x2) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 431 | goto invalid_fld; /* LOEJ bit set not supported */ |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 432 | if (((scsicmd[4] >> 4) & 0xf) != 0) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 433 | goto invalid_fld; /* power conditions not supported */ |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 434 | if (scsicmd[4] & 0x1) { |
| 435 | tf->nsect = 1; /* 1 sector, lba=0 */ |
Albert Lee | 9d5b130 | 2005-10-04 08:48:17 -0400 | [diff] [blame] | 436 | |
| 437 | if (qc->dev->flags & ATA_DFLAG_LBA) { |
| 438 | qc->tf.flags |= ATA_TFLAG_LBA; |
| 439 | |
| 440 | tf->lbah = 0x0; |
| 441 | tf->lbam = 0x0; |
| 442 | tf->lbal = 0x0; |
| 443 | tf->device |= ATA_LBA; |
| 444 | } else { |
| 445 | /* CHS */ |
| 446 | tf->lbal = 0x1; /* sect */ |
| 447 | tf->lbam = 0x0; /* cyl low */ |
| 448 | tf->lbah = 0x0; /* cyl high */ |
| 449 | } |
| 450 | |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 451 | tf->command = ATA_CMD_VERIFY; /* READ VERIFY */ |
| 452 | } else { |
| 453 | tf->nsect = 0; /* time period value (0 implies now) */ |
| 454 | tf->command = ATA_CMD_STANDBY; |
| 455 | /* Consider: ATA STANDBY IMMEDIATE command */ |
| 456 | } |
| 457 | /* |
| 458 | * Standby and Idle condition timers could be implemented but that |
| 459 | * would require libata to implement the Power condition mode page |
| 460 | * and allow the user to change it. Changing mode pages requires |
| 461 | * MODE SELECT to be implemented. |
| 462 | */ |
| 463 | |
| 464 | return 0; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 465 | |
| 466 | invalid_fld: |
| 467 | ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0); |
| 468 | /* "Invalid field in cbd" */ |
| 469 | return 1; |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | |
| 473 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command |
| 475 | * @qc: Storage for translated ATA taskfile |
| 476 | * @scsicmd: SCSI command to translate (ignored) |
| 477 | * |
| 478 | * Sets up an ATA taskfile to issue FLUSH CACHE or |
| 479 | * FLUSH CACHE EXT. |
| 480 | * |
| 481 | * LOCKING: |
| 482 | * spin_lock_irqsave(host_set lock) |
| 483 | * |
| 484 | * RETURNS: |
| 485 | * Zero on success, non-zero on error. |
| 486 | */ |
| 487 | |
| 488 | static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd) |
| 489 | { |
| 490 | struct ata_taskfile *tf = &qc->tf; |
| 491 | |
| 492 | tf->flags |= ATA_TFLAG_DEVICE; |
| 493 | tf->protocol = ATA_PROT_NODATA; |
| 494 | |
Albert Lee | 0750669 | 2005-10-12 15:04:18 +0800 | [diff] [blame^] | 495 | if ((qc->dev->flags & ATA_DFLAG_LBA48) && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | (ata_id_has_flush_ext(qc->dev->id))) |
| 497 | tf->command = ATA_CMD_FLUSH_EXT; |
| 498 | else |
| 499 | tf->command = ATA_CMD_FLUSH; |
| 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | |
| 504 | /** |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 505 | * scsi_6_lba_len - Get LBA and transfer length |
| 506 | * @scsicmd: SCSI command to translate |
| 507 | * |
| 508 | * Calculate LBA and transfer length for 6-byte commands. |
| 509 | * |
| 510 | * RETURNS: |
| 511 | * @plba: the LBA |
| 512 | * @plen: the transfer length |
| 513 | */ |
| 514 | |
| 515 | static void scsi_6_lba_len(u8 *scsicmd, u64 *plba, u32 *plen) |
| 516 | { |
| 517 | u64 lba = 0; |
| 518 | u32 len = 0; |
| 519 | |
| 520 | VPRINTK("six-byte command\n"); |
| 521 | |
| 522 | lba |= ((u64)scsicmd[2]) << 8; |
| 523 | lba |= ((u64)scsicmd[3]); |
| 524 | |
| 525 | len |= ((u32)scsicmd[4]); |
| 526 | |
| 527 | *plba = lba; |
| 528 | *plen = len; |
| 529 | } |
| 530 | |
| 531 | /** |
| 532 | * scsi_10_lba_len - Get LBA and transfer length |
| 533 | * @scsicmd: SCSI command to translate |
| 534 | * |
| 535 | * Calculate LBA and transfer length for 10-byte commands. |
| 536 | * |
| 537 | * RETURNS: |
| 538 | * @plba: the LBA |
| 539 | * @plen: the transfer length |
| 540 | */ |
| 541 | |
| 542 | static void scsi_10_lba_len(u8 *scsicmd, u64 *plba, u32 *plen) |
| 543 | { |
| 544 | u64 lba = 0; |
| 545 | u32 len = 0; |
| 546 | |
| 547 | VPRINTK("ten-byte command\n"); |
| 548 | |
| 549 | lba |= ((u64)scsicmd[2]) << 24; |
| 550 | lba |= ((u64)scsicmd[3]) << 16; |
| 551 | lba |= ((u64)scsicmd[4]) << 8; |
| 552 | lba |= ((u64)scsicmd[5]); |
| 553 | |
| 554 | len |= ((u32)scsicmd[7]) << 8; |
| 555 | len |= ((u32)scsicmd[8]); |
| 556 | |
| 557 | *plba = lba; |
| 558 | *plen = len; |
| 559 | } |
| 560 | |
| 561 | /** |
| 562 | * scsi_16_lba_len - Get LBA and transfer length |
| 563 | * @scsicmd: SCSI command to translate |
| 564 | * |
| 565 | * Calculate LBA and transfer length for 16-byte commands. |
| 566 | * |
| 567 | * RETURNS: |
| 568 | * @plba: the LBA |
| 569 | * @plen: the transfer length |
| 570 | */ |
| 571 | |
| 572 | static void scsi_16_lba_len(u8 *scsicmd, u64 *plba, u32 *plen) |
| 573 | { |
| 574 | u64 lba = 0; |
| 575 | u32 len = 0; |
| 576 | |
| 577 | VPRINTK("sixteen-byte command\n"); |
| 578 | |
| 579 | lba |= ((u64)scsicmd[2]) << 56; |
| 580 | lba |= ((u64)scsicmd[3]) << 48; |
| 581 | lba |= ((u64)scsicmd[4]) << 40; |
| 582 | lba |= ((u64)scsicmd[5]) << 32; |
| 583 | lba |= ((u64)scsicmd[6]) << 24; |
| 584 | lba |= ((u64)scsicmd[7]) << 16; |
| 585 | lba |= ((u64)scsicmd[8]) << 8; |
| 586 | lba |= ((u64)scsicmd[9]); |
| 587 | |
| 588 | len |= ((u32)scsicmd[10]) << 24; |
| 589 | len |= ((u32)scsicmd[11]) << 16; |
| 590 | len |= ((u32)scsicmd[12]) << 8; |
| 591 | len |= ((u32)scsicmd[13]); |
| 592 | |
| 593 | *plba = lba; |
| 594 | *plen = len; |
| 595 | } |
| 596 | |
| 597 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one |
| 599 | * @qc: Storage for translated ATA taskfile |
| 600 | * @scsicmd: SCSI command to translate |
| 601 | * |
| 602 | * Converts SCSI VERIFY command to an ATA READ VERIFY command. |
| 603 | * |
| 604 | * LOCKING: |
| 605 | * spin_lock_irqsave(host_set lock) |
| 606 | * |
| 607 | * RETURNS: |
| 608 | * Zero on success, non-zero on error. |
| 609 | */ |
| 610 | |
| 611 | static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd) |
| 612 | { |
| 613 | struct ata_taskfile *tf = &qc->tf; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 614 | struct ata_device *dev = qc->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | u64 dev_sectors = qc->dev->n_sectors; |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 616 | u64 block; |
| 617 | u32 n_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
| 619 | tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 620 | tf->protocol = ATA_PROT_NODATA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 622 | if (scsicmd[0] == VERIFY) |
| 623 | scsi_10_lba_len(scsicmd, &block, &n_block); |
| 624 | else if (scsicmd[0] == VERIFY_16) |
| 625 | scsi_16_lba_len(scsicmd, &block, &n_block); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | else |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 627 | goto invalid_fld; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 629 | if (!n_block) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 630 | goto nothing_to_do; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 631 | if (block >= dev_sectors) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 632 | goto out_of_range; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 633 | if ((block + n_block) > dev_sectors) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 634 | goto out_of_range; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
Albert Lee | 0750669 | 2005-10-12 15:04:18 +0800 | [diff] [blame^] | 636 | if (dev->flags & ATA_DFLAG_LBA) { |
| 637 | tf->flags |= ATA_TFLAG_LBA; |
| 638 | |
| 639 | if (dev->flags & ATA_DFLAG_LBA48) { |
| 640 | if (n_block > (64 * 1024)) |
| 641 | goto invalid_fld; |
| 642 | |
| 643 | /* use LBA48 */ |
| 644 | tf->flags |= ATA_TFLAG_LBA48; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 645 | tf->command = ATA_CMD_VERIFY_EXT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 646 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 647 | tf->hob_nsect = (n_block >> 8) & 0xff; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 648 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 649 | tf->hob_lbah = (block >> 40) & 0xff; |
| 650 | tf->hob_lbam = (block >> 32) & 0xff; |
| 651 | tf->hob_lbal = (block >> 24) & 0xff; |
| 652 | } else { |
Albert Lee | 0750669 | 2005-10-12 15:04:18 +0800 | [diff] [blame^] | 653 | if (n_block > 256) |
| 654 | goto invalid_fld; |
| 655 | |
| 656 | /* use LBA28 */ |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 657 | tf->command = ATA_CMD_VERIFY; |
| 658 | |
| 659 | tf->device |= (block >> 24) & 0xf; |
| 660 | } |
| 661 | |
| 662 | tf->nsect = n_block & 0xff; |
| 663 | |
| 664 | tf->lbah = (block >> 16) & 0xff; |
| 665 | tf->lbam = (block >> 8) & 0xff; |
| 666 | tf->lbal = block & 0xff; |
| 667 | |
| 668 | tf->device |= ATA_LBA; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | } else { |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 670 | /* CHS */ |
| 671 | u32 sect, head, cyl, track; |
| 672 | |
Albert Lee | 0750669 | 2005-10-12 15:04:18 +0800 | [diff] [blame^] | 673 | if (n_block > 256) |
| 674 | goto invalid_fld; |
| 675 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 676 | /* Convert LBA to CHS */ |
| 677 | track = (u32)block / dev->sectors; |
| 678 | cyl = track / dev->heads; |
| 679 | head = track % dev->heads; |
| 680 | sect = (u32)block % dev->sectors + 1; |
| 681 | |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 682 | DPRINTK("block %u track %u cyl %u head %u sect %u\n", |
| 683 | (u32)block, track, cyl, head, sect); |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 684 | |
| 685 | /* Check whether the converted CHS can fit. |
| 686 | Cylinder: 0-65535 |
| 687 | Head: 0-15 |
| 688 | Sector: 1-255*/ |
| 689 | if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect)) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 690 | goto out_of_range; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 691 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | tf->command = ATA_CMD_VERIFY; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 693 | tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */ |
| 694 | tf->lbal = sect; |
| 695 | tf->lbam = cyl; |
| 696 | tf->lbah = cyl >> 8; |
| 697 | tf->device |= head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 698 | } |
| 699 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 700 | return 0; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 701 | |
| 702 | invalid_fld: |
| 703 | ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0); |
| 704 | /* "Invalid field in cbd" */ |
| 705 | return 1; |
| 706 | |
| 707 | out_of_range: |
| 708 | ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0); |
| 709 | /* "Logical Block Address out of range" */ |
| 710 | return 1; |
| 711 | |
| 712 | nothing_to_do: |
| 713 | qc->scsicmd->result = SAM_STAT_GOOD; |
| 714 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | } |
| 716 | |
| 717 | /** |
| 718 | * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one |
| 719 | * @qc: Storage for translated ATA taskfile |
| 720 | * @scsicmd: SCSI command to translate |
| 721 | * |
| 722 | * Converts any of six SCSI read/write commands into the |
| 723 | * ATA counterpart, including starting sector (LBA), |
| 724 | * sector count, and taking into account the device's LBA48 |
| 725 | * support. |
| 726 | * |
| 727 | * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and |
| 728 | * %WRITE_16 are currently supported. |
| 729 | * |
| 730 | * LOCKING: |
| 731 | * spin_lock_irqsave(host_set lock) |
| 732 | * |
| 733 | * RETURNS: |
| 734 | * Zero on success, non-zero on error. |
| 735 | */ |
| 736 | |
| 737 | static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd) |
| 738 | { |
| 739 | struct ata_taskfile *tf = &qc->tf; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 740 | struct ata_device *dev = qc->dev; |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 741 | u64 block; |
| 742 | u32 n_block; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | |
| 744 | tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 745 | tf->protocol = qc->dev->xfer_protocol; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 746 | |
| 747 | if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 || |
| 748 | scsicmd[0] == READ_16) { |
| 749 | tf->command = qc->dev->read_cmd; |
| 750 | } else { |
| 751 | tf->command = qc->dev->write_cmd; |
| 752 | tf->flags |= ATA_TFLAG_WRITE; |
| 753 | } |
| 754 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 755 | /* Calculate the SCSI LBA and transfer length. */ |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 756 | switch (scsicmd[0]) { |
| 757 | case READ_10: |
| 758 | case WRITE_10: |
| 759 | scsi_10_lba_len(scsicmd, &block, &n_block); |
| 760 | break; |
| 761 | case READ_6: |
| 762 | case WRITE_6: |
| 763 | scsi_6_lba_len(scsicmd, &block, &n_block); |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 764 | |
| 765 | /* for 6-byte r/w commands, transfer length 0 |
| 766 | * means 256 blocks of data, not 0 block. |
| 767 | */ |
Jeff Garzik | 76b2bf9 | 2005-08-29 19:24:43 -0400 | [diff] [blame] | 768 | if (!n_block) |
| 769 | n_block = 256; |
Albert Lee | 3aef523 | 2005-10-04 08:47:43 -0400 | [diff] [blame] | 770 | break; |
| 771 | case READ_16: |
| 772 | case WRITE_16: |
| 773 | scsi_16_lba_len(scsicmd, &block, &n_block); |
| 774 | break; |
| 775 | default: |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 776 | DPRINTK("no-byte command\n"); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 777 | goto invalid_fld; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 778 | } |
| 779 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 780 | /* Check and compose ATA command */ |
| 781 | if (!n_block) |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 782 | /* For 10-byte and 16-byte SCSI R/W commands, transfer |
| 783 | * length 0 means transfer 0 block of data. |
| 784 | * However, for ATA R/W commands, sector count 0 means |
| 785 | * 256 or 65536 sectors, not 0 sectors as in SCSI. |
| 786 | */ |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 787 | goto nothing_to_do; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 788 | |
Albert Lee | 0750669 | 2005-10-12 15:04:18 +0800 | [diff] [blame^] | 789 | if (dev->flags & ATA_DFLAG_LBA) { |
| 790 | tf->flags |= ATA_TFLAG_LBA; |
| 791 | |
| 792 | if (dev->flags & ATA_DFLAG_LBA48) { |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 793 | /* The request -may- be too large for LBA48. */ |
| 794 | if ((block >> 48) || (n_block > 65536)) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 795 | goto out_of_range; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 796 | |
Albert Lee | 0750669 | 2005-10-12 15:04:18 +0800 | [diff] [blame^] | 797 | /* use LBA48 */ |
| 798 | tf->flags |= ATA_TFLAG_LBA48; |
| 799 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 800 | tf->hob_nsect = (n_block >> 8) & 0xff; |
| 801 | |
| 802 | tf->hob_lbah = (block >> 40) & 0xff; |
| 803 | tf->hob_lbam = (block >> 32) & 0xff; |
| 804 | tf->hob_lbal = (block >> 24) & 0xff; |
| 805 | } else { |
Albert Lee | 0750669 | 2005-10-12 15:04:18 +0800 | [diff] [blame^] | 806 | /* use LBA28 */ |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 807 | |
| 808 | /* The request -may- be too large for LBA28. */ |
| 809 | if ((block >> 28) || (n_block > 256)) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 810 | goto out_of_range; |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 811 | |
| 812 | tf->device |= (block >> 24) & 0xf; |
| 813 | } |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 814 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 815 | qc->nsect = n_block; |
| 816 | tf->nsect = n_block & 0xff; |
| 817 | |
| 818 | tf->lbah = (block >> 16) & 0xff; |
| 819 | tf->lbam = (block >> 8) & 0xff; |
| 820 | tf->lbal = block & 0xff; |
| 821 | |
| 822 | tf->device |= ATA_LBA; |
| 823 | } else { |
| 824 | /* CHS */ |
| 825 | u32 sect, head, cyl, track; |
| 826 | |
| 827 | /* The request -may- be too large for CHS addressing. */ |
| 828 | if ((block >> 28) || (n_block > 256)) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 829 | goto out_of_range; |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 830 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 831 | /* Convert LBA to CHS */ |
| 832 | track = (u32)block / dev->sectors; |
| 833 | cyl = track / dev->heads; |
| 834 | head = track % dev->heads; |
| 835 | sect = (u32)block % dev->sectors + 1; |
| 836 | |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 837 | DPRINTK("block %u track %u cyl %u head %u sect %u\n", |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 838 | (u32)block, track, cyl, head, sect); |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 839 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 840 | /* Check whether the converted CHS can fit. |
| 841 | Cylinder: 0-65535 |
| 842 | Head: 0-15 |
| 843 | Sector: 1-255*/ |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 844 | if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect)) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 845 | goto out_of_range; |
Albert Lee | c187c4b | 2005-10-04 08:46:51 -0400 | [diff] [blame] | 846 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 847 | qc->nsect = n_block; |
| 848 | tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */ |
| 849 | tf->lbal = sect; |
| 850 | tf->lbam = cyl; |
| 851 | tf->lbah = cyl >> 8; |
| 852 | tf->device |= head; |
| 853 | } |
| 854 | |
| 855 | return 0; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 856 | |
| 857 | invalid_fld: |
| 858 | ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0); |
| 859 | /* "Invalid field in cbd" */ |
| 860 | return 1; |
| 861 | |
| 862 | out_of_range: |
| 863 | ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0); |
| 864 | /* "Logical Block Address out of range" */ |
| 865 | return 1; |
| 866 | |
| 867 | nothing_to_do: |
| 868 | qc->scsicmd->result = SAM_STAT_GOOD; |
| 869 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 870 | } |
| 871 | |
| 872 | static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat) |
| 873 | { |
| 874 | struct scsi_cmnd *cmd = qc->scsicmd; |
| 875 | |
| 876 | if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) |
| 877 | ata_to_sense_error(qc, drv_stat); |
| 878 | else |
| 879 | cmd->result = SAM_STAT_GOOD; |
| 880 | |
| 881 | qc->scsidone(cmd); |
| 882 | |
| 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | /** |
| 887 | * ata_scsi_translate - Translate then issue SCSI command to ATA device |
| 888 | * @ap: ATA port to which the command is addressed |
| 889 | * @dev: ATA device to which the command is addressed |
| 890 | * @cmd: SCSI command to execute |
| 891 | * @done: SCSI command completion function |
| 892 | * @xlat_func: Actor which translates @cmd to an ATA taskfile |
| 893 | * |
| 894 | * Our ->queuecommand() function has decided that the SCSI |
| 895 | * command issued can be directly translated into an ATA |
| 896 | * command, rather than handled internally. |
| 897 | * |
| 898 | * This function sets up an ata_queued_cmd structure for the |
| 899 | * SCSI command, and sends that ata_queued_cmd to the hardware. |
| 900 | * |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 901 | * The xlat_func argument (actor) returns 0 if ready to execute |
| 902 | * ATA command, else 1 to finish translation. If 1 is returned |
| 903 | * then cmd->result (and possibly cmd->sense_buffer) are assumed |
| 904 | * to be set reflecting an error condition or clean (early) |
| 905 | * termination. |
| 906 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 907 | * LOCKING: |
| 908 | * spin_lock_irqsave(host_set lock) |
| 909 | */ |
| 910 | |
| 911 | static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev, |
| 912 | struct scsi_cmnd *cmd, |
| 913 | void (*done)(struct scsi_cmnd *), |
| 914 | ata_xlat_func_t xlat_func) |
| 915 | { |
| 916 | struct ata_queued_cmd *qc; |
| 917 | u8 *scsicmd = cmd->cmnd; |
| 918 | |
| 919 | VPRINTK("ENTER\n"); |
| 920 | |
| 921 | qc = ata_scsi_qc_new(ap, dev, cmd, done); |
| 922 | if (!qc) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 923 | goto err_mem; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 924 | |
| 925 | /* data is present; dma-map it */ |
| be7db05 | 2005-04-17 15:26:13 -0500 | [diff] [blame] | 926 | if (cmd->sc_data_direction == DMA_FROM_DEVICE || |
| 927 | cmd->sc_data_direction == DMA_TO_DEVICE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 928 | if (unlikely(cmd->request_bufflen < 1)) { |
| 929 | printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n", |
| 930 | ap->id, dev->devno); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 931 | goto err_did; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | if (cmd->use_sg) |
| 935 | ata_sg_init(qc, cmd->request_buffer, cmd->use_sg); |
| 936 | else |
| 937 | ata_sg_init_one(qc, cmd->request_buffer, |
| 938 | cmd->request_bufflen); |
| 939 | |
| 940 | qc->dma_dir = cmd->sc_data_direction; |
| 941 | } |
| 942 | |
| 943 | qc->complete_fn = ata_scsi_qc_complete; |
| 944 | |
| 945 | if (xlat_func(qc, scsicmd)) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 946 | goto early_finish; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 947 | |
| 948 | /* select device, send command to hardware */ |
| 949 | if (ata_qc_issue(qc)) |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 950 | goto err_did; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 951 | |
| 952 | VPRINTK("EXIT\n"); |
| 953 | return; |
| 954 | |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 955 | early_finish: |
| 956 | ata_qc_free(qc); |
| 957 | done(cmd); |
| 958 | DPRINTK("EXIT - early finish (good or error)\n"); |
| 959 | return; |
| 960 | |
| 961 | err_did: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 962 | ata_qc_free(qc); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 963 | err_mem: |
| 964 | cmd->result = (DID_ERROR << 16); |
| 965 | done(cmd); |
| 966 | DPRINTK("EXIT - internal\n"); |
| 967 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | /** |
| 971 | * ata_scsi_rbuf_get - Map response buffer. |
| 972 | * @cmd: SCSI command containing buffer to be mapped. |
| 973 | * @buf_out: Pointer to mapped area. |
| 974 | * |
| 975 | * Maps buffer contained within SCSI command @cmd. |
| 976 | * |
| 977 | * LOCKING: |
| 978 | * spin_lock_irqsave(host_set lock) |
| 979 | * |
| 980 | * RETURNS: |
| 981 | * Length of response buffer. |
| 982 | */ |
| 983 | |
| 984 | static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out) |
| 985 | { |
| 986 | u8 *buf; |
| 987 | unsigned int buflen; |
| 988 | |
| 989 | if (cmd->use_sg) { |
| 990 | struct scatterlist *sg; |
| 991 | |
| 992 | sg = (struct scatterlist *) cmd->request_buffer; |
| 993 | buf = kmap_atomic(sg->page, KM_USER0) + sg->offset; |
| 994 | buflen = sg->length; |
| 995 | } else { |
| 996 | buf = cmd->request_buffer; |
| 997 | buflen = cmd->request_bufflen; |
| 998 | } |
| 999 | |
| 1000 | *buf_out = buf; |
| 1001 | return buflen; |
| 1002 | } |
| 1003 | |
| 1004 | /** |
| 1005 | * ata_scsi_rbuf_put - Unmap response buffer. |
| 1006 | * @cmd: SCSI command containing buffer to be unmapped. |
| 1007 | * @buf: buffer to unmap |
| 1008 | * |
| 1009 | * Unmaps response buffer contained within @cmd. |
| 1010 | * |
| 1011 | * LOCKING: |
| 1012 | * spin_lock_irqsave(host_set lock) |
| 1013 | */ |
| 1014 | |
| 1015 | static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf) |
| 1016 | { |
| 1017 | if (cmd->use_sg) { |
| 1018 | struct scatterlist *sg; |
| 1019 | |
| 1020 | sg = (struct scatterlist *) cmd->request_buffer; |
| 1021 | kunmap_atomic(buf - sg->offset, KM_USER0); |
| 1022 | } |
| 1023 | } |
| 1024 | |
| 1025 | /** |
| 1026 | * ata_scsi_rbuf_fill - wrapper for SCSI command simulators |
| 1027 | * @args: device IDENTIFY data / SCSI command of interest. |
| 1028 | * @actor: Callback hook for desired SCSI command simulator |
| 1029 | * |
| 1030 | * Takes care of the hard work of simulating a SCSI command... |
| 1031 | * Mapping the response buffer, calling the command's handler, |
| 1032 | * and handling the handler's return value. This return value |
| 1033 | * indicates whether the handler wishes the SCSI command to be |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1034 | * completed successfully (0), or not (in which case cmd->result |
| 1035 | * and sense buffer are assumed to be set). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | * |
| 1037 | * LOCKING: |
| 1038 | * spin_lock_irqsave(host_set lock) |
| 1039 | */ |
| 1040 | |
| 1041 | void ata_scsi_rbuf_fill(struct ata_scsi_args *args, |
| 1042 | unsigned int (*actor) (struct ata_scsi_args *args, |
| 1043 | u8 *rbuf, unsigned int buflen)) |
| 1044 | { |
| 1045 | u8 *rbuf; |
| 1046 | unsigned int buflen, rc; |
| 1047 | struct scsi_cmnd *cmd = args->cmd; |
| 1048 | |
| 1049 | buflen = ata_scsi_rbuf_get(cmd, &rbuf); |
| 1050 | memset(rbuf, 0, buflen); |
| 1051 | rc = actor(args, rbuf, buflen); |
| 1052 | ata_scsi_rbuf_put(cmd, rbuf); |
| 1053 | |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1054 | if (rc == 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1055 | cmd->result = SAM_STAT_GOOD; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1056 | args->done(cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | } |
| 1058 | |
| 1059 | /** |
| 1060 | * ata_scsiop_inq_std - Simulate INQUIRY command |
| 1061 | * @args: device IDENTIFY data / SCSI command of interest. |
| 1062 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
| 1063 | * @buflen: Response buffer length. |
| 1064 | * |
| 1065 | * Returns standard device identification data associated |
| 1066 | * with non-EVPD INQUIRY command output. |
| 1067 | * |
| 1068 | * LOCKING: |
| 1069 | * spin_lock_irqsave(host_set lock) |
| 1070 | */ |
| 1071 | |
| 1072 | unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf, |
| 1073 | unsigned int buflen) |
| 1074 | { |
| 1075 | u8 hdr[] = { |
| 1076 | TYPE_DISK, |
| 1077 | 0, |
| 1078 | 0x5, /* claim SPC-3 version compatibility */ |
| 1079 | 2, |
| 1080 | 95 - 4 |
| 1081 | }; |
| 1082 | |
| 1083 | /* set scsi removeable (RMB) bit per ata bit */ |
| 1084 | if (ata_id_removeable(args->id)) |
| 1085 | hdr[1] |= (1 << 7); |
| 1086 | |
| 1087 | VPRINTK("ENTER\n"); |
| 1088 | |
| 1089 | memcpy(rbuf, hdr, sizeof(hdr)); |
| 1090 | |
| 1091 | if (buflen > 35) { |
| 1092 | memcpy(&rbuf[8], "ATA ", 8); |
| 1093 | ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16); |
| 1094 | ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4); |
| 1095 | if (rbuf[32] == 0 || rbuf[32] == ' ') |
| 1096 | memcpy(&rbuf[32], "n/a ", 4); |
| 1097 | } |
| 1098 | |
| 1099 | if (buflen > 63) { |
| 1100 | const u8 versions[] = { |
| 1101 | 0x60, /* SAM-3 (no version claimed) */ |
| 1102 | |
| 1103 | 0x03, |
| 1104 | 0x20, /* SBC-2 (no version claimed) */ |
| 1105 | |
| 1106 | 0x02, |
| 1107 | 0x60 /* SPC-3 (no version claimed) */ |
| 1108 | }; |
| 1109 | |
| 1110 | memcpy(rbuf + 59, versions, sizeof(versions)); |
| 1111 | } |
| 1112 | |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
| 1116 | /** |
| 1117 | * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages |
| 1118 | * @args: device IDENTIFY data / SCSI command of interest. |
| 1119 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
| 1120 | * @buflen: Response buffer length. |
| 1121 | * |
| 1122 | * Returns list of inquiry EVPD pages available. |
| 1123 | * |
| 1124 | * LOCKING: |
| 1125 | * spin_lock_irqsave(host_set lock) |
| 1126 | */ |
| 1127 | |
| 1128 | unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf, |
| 1129 | unsigned int buflen) |
| 1130 | { |
| 1131 | const u8 pages[] = { |
| 1132 | 0x00, /* page 0x00, this page */ |
| 1133 | 0x80, /* page 0x80, unit serial no page */ |
| 1134 | 0x83 /* page 0x83, device ident page */ |
| 1135 | }; |
| 1136 | rbuf[3] = sizeof(pages); /* number of supported EVPD pages */ |
| 1137 | |
| 1138 | if (buflen > 6) |
| 1139 | memcpy(rbuf + 4, pages, sizeof(pages)); |
| 1140 | |
| 1141 | return 0; |
| 1142 | } |
| 1143 | |
| 1144 | /** |
| 1145 | * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number |
| 1146 | * @args: device IDENTIFY data / SCSI command of interest. |
| 1147 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
| 1148 | * @buflen: Response buffer length. |
| 1149 | * |
| 1150 | * Returns ATA device serial number. |
| 1151 | * |
| 1152 | * LOCKING: |
| 1153 | * spin_lock_irqsave(host_set lock) |
| 1154 | */ |
| 1155 | |
| 1156 | unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf, |
| 1157 | unsigned int buflen) |
| 1158 | { |
| 1159 | const u8 hdr[] = { |
| 1160 | 0, |
| 1161 | 0x80, /* this page code */ |
| 1162 | 0, |
| 1163 | ATA_SERNO_LEN, /* page len */ |
| 1164 | }; |
| 1165 | memcpy(rbuf, hdr, sizeof(hdr)); |
| 1166 | |
| 1167 | if (buflen > (ATA_SERNO_LEN + 4 - 1)) |
| 1168 | ata_dev_id_string(args->id, (unsigned char *) &rbuf[4], |
| 1169 | ATA_ID_SERNO_OFS, ATA_SERNO_LEN); |
| 1170 | |
| 1171 | return 0; |
| 1172 | } |
| 1173 | |
| 1174 | static const char *inq_83_str = "Linux ATA-SCSI simulator"; |
| 1175 | |
| 1176 | /** |
| 1177 | * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity |
| 1178 | * @args: device IDENTIFY data / SCSI command of interest. |
| 1179 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
| 1180 | * @buflen: Response buffer length. |
| 1181 | * |
| 1182 | * Returns device identification. Currently hardcoded to |
| 1183 | * return "Linux ATA-SCSI simulator". |
| 1184 | * |
| 1185 | * LOCKING: |
| 1186 | * spin_lock_irqsave(host_set lock) |
| 1187 | */ |
| 1188 | |
| 1189 | unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf, |
| 1190 | unsigned int buflen) |
| 1191 | { |
| 1192 | rbuf[1] = 0x83; /* this page code */ |
| 1193 | rbuf[3] = 4 + strlen(inq_83_str); /* page len */ |
| 1194 | |
| 1195 | /* our one and only identification descriptor (vendor-specific) */ |
| 1196 | if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) { |
| 1197 | rbuf[4 + 0] = 2; /* code set: ASCII */ |
| 1198 | rbuf[4 + 3] = strlen(inq_83_str); |
| 1199 | memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str)); |
| 1200 | } |
| 1201 | |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
| 1205 | /** |
Jeff Garzik | 0cba632 | 2005-05-30 19:49:12 -0400 | [diff] [blame] | 1206 | * ata_scsiop_noop - Command handler that simply returns success. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1207 | * @args: device IDENTIFY data / SCSI command of interest. |
| 1208 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
| 1209 | * @buflen: Response buffer length. |
| 1210 | * |
| 1211 | * No operation. Simply returns success to caller, to indicate |
| 1212 | * that the caller should successfully complete this SCSI command. |
| 1213 | * |
| 1214 | * LOCKING: |
| 1215 | * spin_lock_irqsave(host_set lock) |
| 1216 | */ |
| 1217 | |
| 1218 | unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf, |
| 1219 | unsigned int buflen) |
| 1220 | { |
| 1221 | VPRINTK("ENTER\n"); |
| 1222 | return 0; |
| 1223 | } |
| 1224 | |
| 1225 | /** |
| 1226 | * ata_msense_push - Push data onto MODE SENSE data output buffer |
| 1227 | * @ptr_io: (input/output) Location to store more output data |
| 1228 | * @last: End of output data buffer |
| 1229 | * @buf: Pointer to BLOB being added to output buffer |
| 1230 | * @buflen: Length of BLOB |
| 1231 | * |
| 1232 | * Store MODE SENSE data on an output buffer. |
| 1233 | * |
| 1234 | * LOCKING: |
| 1235 | * None. |
| 1236 | */ |
| 1237 | |
| 1238 | static void ata_msense_push(u8 **ptr_io, const u8 *last, |
| 1239 | const u8 *buf, unsigned int buflen) |
| 1240 | { |
| 1241 | u8 *ptr = *ptr_io; |
| 1242 | |
| 1243 | if ((ptr + buflen - 1) > last) |
| 1244 | return; |
| 1245 | |
| 1246 | memcpy(ptr, buf, buflen); |
| 1247 | |
| 1248 | ptr += buflen; |
| 1249 | |
| 1250 | *ptr_io = ptr; |
| 1251 | } |
| 1252 | |
| 1253 | /** |
| 1254 | * ata_msense_caching - Simulate MODE SENSE caching info page |
| 1255 | * @id: device IDENTIFY data |
| 1256 | * @ptr_io: (input/output) Location to store more output data |
| 1257 | * @last: End of output data buffer |
| 1258 | * |
| 1259 | * Generate a caching info page, which conditionally indicates |
| 1260 | * write caching to the SCSI layer, depending on device |
| 1261 | * capabilities. |
| 1262 | * |
| 1263 | * LOCKING: |
| 1264 | * None. |
| 1265 | */ |
| 1266 | |
| 1267 | static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io, |
| 1268 | const u8 *last) |
| 1269 | { |
| 1270 | u8 page[] = { |
| 1271 | 0x8, /* page code */ |
| 1272 | 0x12, /* page length */ |
| 1273 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 zeroes */ |
| 1274 | 0, 0, 0, 0, 0, 0, 0, 0 /* 8 zeroes */ |
| 1275 | }; |
| 1276 | |
| 1277 | if (ata_id_wcache_enabled(id)) |
| 1278 | page[2] |= (1 << 2); /* write cache enable */ |
| 1279 | if (!ata_id_rahead_enabled(id)) |
| 1280 | page[12] |= (1 << 5); /* disable read ahead */ |
| 1281 | |
| 1282 | ata_msense_push(ptr_io, last, page, sizeof(page)); |
| 1283 | return sizeof(page); |
| 1284 | } |
| 1285 | |
| 1286 | /** |
| 1287 | * ata_msense_ctl_mode - Simulate MODE SENSE control mode page |
| 1288 | * @dev: Device associated with this MODE SENSE command |
| 1289 | * @ptr_io: (input/output) Location to store more output data |
| 1290 | * @last: End of output data buffer |
| 1291 | * |
| 1292 | * Generate a generic MODE SENSE control mode page. |
| 1293 | * |
| 1294 | * LOCKING: |
| 1295 | * None. |
| 1296 | */ |
| 1297 | |
| 1298 | static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last) |
| 1299 | { |
| 1300 | const u8 page[] = {0xa, 0xa, 6, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30}; |
| 1301 | |
| 1302 | /* byte 2: set the descriptor format sense data bit (bit 2) |
| 1303 | * since we need to support returning this format for SAT |
| 1304 | * commands and any SCSI commands against a 48b LBA device. |
| 1305 | */ |
| 1306 | |
| 1307 | ata_msense_push(ptr_io, last, page, sizeof(page)); |
| 1308 | return sizeof(page); |
| 1309 | } |
| 1310 | |
| 1311 | /** |
| 1312 | * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page |
| 1313 | * @dev: Device associated with this MODE SENSE command |
| 1314 | * @ptr_io: (input/output) Location to store more output data |
| 1315 | * @last: End of output data buffer |
| 1316 | * |
| 1317 | * Generate a generic MODE SENSE r/w error recovery page. |
| 1318 | * |
| 1319 | * LOCKING: |
| 1320 | * None. |
| 1321 | */ |
| 1322 | |
| 1323 | static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last) |
| 1324 | { |
| 1325 | const u8 page[] = { |
| 1326 | 0x1, /* page code */ |
| 1327 | 0xa, /* page length */ |
| 1328 | (1 << 7) | (1 << 6), /* note auto r/w reallocation */ |
| 1329 | 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */ |
| 1330 | }; |
| 1331 | |
| 1332 | ata_msense_push(ptr_io, last, page, sizeof(page)); |
| 1333 | return sizeof(page); |
| 1334 | } |
| 1335 | |
| 1336 | /** |
| 1337 | * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands |
| 1338 | * @args: device IDENTIFY data / SCSI command of interest. |
| 1339 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
| 1340 | * @buflen: Response buffer length. |
| 1341 | * |
| 1342 | * Simulate MODE SENSE commands. |
| 1343 | * |
| 1344 | * LOCKING: |
| 1345 | * spin_lock_irqsave(host_set lock) |
| 1346 | */ |
| 1347 | |
| 1348 | unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf, |
| 1349 | unsigned int buflen) |
| 1350 | { |
| 1351 | u8 *scsicmd = args->cmd->cmnd, *p, *last; |
| 1352 | unsigned int page_control, six_byte, output_len; |
| 1353 | |
| 1354 | VPRINTK("ENTER\n"); |
| 1355 | |
| 1356 | six_byte = (scsicmd[0] == MODE_SENSE); |
| 1357 | |
| 1358 | /* we only support saved and current values (which we treat |
| 1359 | * in the same manner) |
| 1360 | */ |
| 1361 | page_control = scsicmd[2] >> 6; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1362 | switch (page_control) { |
| 1363 | case 0: /* current */ |
| 1364 | break; /* supported */ |
| 1365 | case 3: /* saved */ |
| 1366 | goto saving_not_supp; |
| 1367 | case 1: /* changeable */ |
| 1368 | case 2: /* defaults */ |
| 1369 | default: |
| 1370 | goto invalid_fld; |
| 1371 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | |
| 1373 | if (six_byte) |
| 1374 | output_len = 4; |
| 1375 | else |
| 1376 | output_len = 8; |
| 1377 | |
| 1378 | p = rbuf + output_len; |
| 1379 | last = rbuf + buflen - 1; |
| 1380 | |
| 1381 | switch(scsicmd[2] & 0x3f) { |
| 1382 | case 0x01: /* r/w error recovery */ |
| 1383 | output_len += ata_msense_rw_recovery(&p, last); |
| 1384 | break; |
| 1385 | |
| 1386 | case 0x08: /* caching */ |
| 1387 | output_len += ata_msense_caching(args->id, &p, last); |
| 1388 | break; |
| 1389 | |
| 1390 | case 0x0a: { /* control mode */ |
| 1391 | output_len += ata_msense_ctl_mode(&p, last); |
| 1392 | break; |
| 1393 | } |
| 1394 | |
| 1395 | case 0x3f: /* all pages */ |
| 1396 | output_len += ata_msense_rw_recovery(&p, last); |
| 1397 | output_len += ata_msense_caching(args->id, &p, last); |
| 1398 | output_len += ata_msense_ctl_mode(&p, last); |
| 1399 | break; |
| 1400 | |
| 1401 | default: /* invalid page code */ |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1402 | goto invalid_fld; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | if (six_byte) { |
| 1406 | output_len--; |
| 1407 | rbuf[0] = output_len; |
| 1408 | } else { |
| 1409 | output_len -= 2; |
| 1410 | rbuf[0] = output_len >> 8; |
| 1411 | rbuf[1] = output_len; |
| 1412 | } |
| 1413 | |
| 1414 | return 0; |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1415 | |
| 1416 | invalid_fld: |
| 1417 | ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0); |
| 1418 | /* "Invalid field in cbd" */ |
| 1419 | return 1; |
| 1420 | |
| 1421 | saving_not_supp: |
| 1422 | ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0); |
| 1423 | /* "Saving parameters not supported" */ |
| 1424 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1425 | } |
| 1426 | |
| 1427 | /** |
| 1428 | * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands |
| 1429 | * @args: device IDENTIFY data / SCSI command of interest. |
| 1430 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
| 1431 | * @buflen: Response buffer length. |
| 1432 | * |
| 1433 | * Simulate READ CAPACITY commands. |
| 1434 | * |
| 1435 | * LOCKING: |
| 1436 | * spin_lock_irqsave(host_set lock) |
| 1437 | */ |
| 1438 | |
| 1439 | unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf, |
| 1440 | unsigned int buflen) |
| 1441 | { |
| 1442 | u64 n_sectors; |
| 1443 | u32 tmp; |
| 1444 | |
| 1445 | VPRINTK("ENTER\n"); |
| 1446 | |
Albert Lee | 8bf62ece | 2005-05-12 15:29:42 -0400 | [diff] [blame] | 1447 | if (ata_id_has_lba(args->id)) { |
| 1448 | if (ata_id_has_lba48(args->id)) |
| 1449 | n_sectors = ata_id_u64(args->id, 100); |
| 1450 | else |
| 1451 | n_sectors = ata_id_u32(args->id, 60); |
| 1452 | } else { |
| 1453 | /* CHS default translation */ |
| 1454 | n_sectors = args->id[1] * args->id[3] * args->id[6]; |
| 1455 | |
| 1456 | if (ata_id_current_chs_valid(args->id)) |
| 1457 | /* CHS current translation */ |
| 1458 | n_sectors = ata_id_u32(args->id, 57); |
| 1459 | } |
| 1460 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1461 | n_sectors--; /* ATA TotalUserSectors - 1 */ |
| 1462 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1463 | if (args->cmd->cmnd[0] == READ_CAPACITY) { |
Philip Pokorny | 0c144d0 | 2005-05-28 01:24:47 -0700 | [diff] [blame] | 1464 | if( n_sectors >= 0xffffffffULL ) |
| 1465 | tmp = 0xffffffff ; /* Return max count on overflow */ |
| 1466 | else |
| 1467 | tmp = n_sectors ; |
| 1468 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1469 | /* sector count, 32-bit */ |
| 1470 | rbuf[0] = tmp >> (8 * 3); |
| 1471 | rbuf[1] = tmp >> (8 * 2); |
| 1472 | rbuf[2] = tmp >> (8 * 1); |
| 1473 | rbuf[3] = tmp; |
| 1474 | |
| 1475 | /* sector size */ |
| 1476 | tmp = ATA_SECT_SIZE; |
| 1477 | rbuf[6] = tmp >> 8; |
| 1478 | rbuf[7] = tmp; |
| 1479 | |
| 1480 | } else { |
| 1481 | /* sector count, 64-bit */ |
Philip Pokorny | 0c144d0 | 2005-05-28 01:24:47 -0700 | [diff] [blame] | 1482 | tmp = n_sectors >> (8 * 4); |
| 1483 | rbuf[2] = tmp >> (8 * 3); |
| 1484 | rbuf[3] = tmp >> (8 * 2); |
| 1485 | rbuf[4] = tmp >> (8 * 1); |
| 1486 | rbuf[5] = tmp; |
| 1487 | tmp = n_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | rbuf[6] = tmp >> (8 * 3); |
| 1489 | rbuf[7] = tmp >> (8 * 2); |
| 1490 | rbuf[8] = tmp >> (8 * 1); |
| 1491 | rbuf[9] = tmp; |
| 1492 | |
| 1493 | /* sector size */ |
| 1494 | tmp = ATA_SECT_SIZE; |
| 1495 | rbuf[12] = tmp >> 8; |
| 1496 | rbuf[13] = tmp; |
| 1497 | } |
| 1498 | |
| 1499 | return 0; |
| 1500 | } |
| 1501 | |
| 1502 | /** |
| 1503 | * ata_scsiop_report_luns - Simulate REPORT LUNS command |
| 1504 | * @args: device IDENTIFY data / SCSI command of interest. |
| 1505 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. |
| 1506 | * @buflen: Response buffer length. |
| 1507 | * |
| 1508 | * Simulate REPORT LUNS command. |
| 1509 | * |
| 1510 | * LOCKING: |
| 1511 | * spin_lock_irqsave(host_set lock) |
| 1512 | */ |
| 1513 | |
| 1514 | unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf, |
| 1515 | unsigned int buflen) |
| 1516 | { |
| 1517 | VPRINTK("ENTER\n"); |
| 1518 | rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */ |
| 1519 | |
| 1520 | return 0; |
| 1521 | } |
| 1522 | |
| 1523 | /** |
Douglas Gilbert | 845c583 | 2005-10-09 08:55:41 -0400 | [diff] [blame] | 1524 | * ata_scsi_set_sense - Set SCSI sense data and status |
| 1525 | * @cmd: SCSI request to be handled |
| 1526 | * @sk: SCSI-defined sense key |
| 1527 | * @asc: SCSI-defined additional sense code |
| 1528 | * @ascq: SCSI-defined additional sense code qualifier |
| 1529 | * |
| 1530 | * Helper function that builds a valid fixed format, current |
| 1531 | * response code and the given sense key (sk), additional sense |
| 1532 | * code (asc) and additional sense code qualifier (ascq) with |
| 1533 | * a SCSI command status of %SAM_STAT_CHECK_CONDITION and |
| 1534 | * DRIVER_SENSE set in the upper bits of scsi_cmnd::result . |
| 1535 | * |
| 1536 | * LOCKING: |
| 1537 | * Not required |
| 1538 | */ |
| 1539 | |
| 1540 | void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq) |
| 1541 | { |
| 1542 | cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; |
| 1543 | |
| 1544 | cmd->sense_buffer[0] = 0x70; /* fixed format, current */ |
| 1545 | cmd->sense_buffer[2] = sk; |
| 1546 | cmd->sense_buffer[7] = 18 - 8; /* additional sense length */ |
| 1547 | cmd->sense_buffer[12] = asc; |
| 1548 | cmd->sense_buffer[13] = ascq; |
| 1549 | } |
| 1550 | |
| 1551 | /** |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | * ata_scsi_badcmd - End a SCSI request with an error |
| 1553 | * @cmd: SCSI request to be handled |
| 1554 | * @done: SCSI command completion function |
| 1555 | * @asc: SCSI-defined additional sense code |
| 1556 | * @ascq: SCSI-defined additional sense code qualifier |
| 1557 | * |
| 1558 | * Helper function that completes a SCSI command with |
| 1559 | * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST |
| 1560 | * and the specified additional sense codes. |
| 1561 | * |
| 1562 | * LOCKING: |
| 1563 | * spin_lock_irqsave(host_set lock) |
| 1564 | */ |
| 1565 | |
| 1566 | void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq) |
| 1567 | { |
| 1568 | DPRINTK("ENTER\n"); |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1569 | ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1570 | |
| 1571 | done(cmd); |
| 1572 | } |
| 1573 | |
Jeff Garzik | a939c96 | 2005-10-05 17:09:16 -0400 | [diff] [blame] | 1574 | void atapi_request_sense(struct ata_port *ap, struct ata_device *dev, |
| 1575 | struct scsi_cmnd *cmd) |
| 1576 | { |
| 1577 | DECLARE_COMPLETION(wait); |
| 1578 | struct ata_queued_cmd *qc; |
| 1579 | unsigned long flags; |
| 1580 | int rc; |
| 1581 | |
| 1582 | DPRINTK("ATAPI request sense\n"); |
| 1583 | |
| 1584 | qc = ata_qc_new_init(ap, dev); |
| 1585 | BUG_ON(qc == NULL); |
| 1586 | |
| 1587 | /* FIXME: is this needed? */ |
| 1588 | memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer)); |
| 1589 | |
| 1590 | ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer)); |
| 1591 | qc->dma_dir = DMA_FROM_DEVICE; |
| 1592 | |
| 1593 | memset(&qc->cdb, 0, ap->cdb_len); |
| 1594 | qc->cdb[0] = REQUEST_SENSE; |
| 1595 | qc->cdb[4] = SCSI_SENSE_BUFFERSIZE; |
| 1596 | |
| 1597 | qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| 1598 | qc->tf.command = ATA_CMD_PACKET; |
| 1599 | |
| 1600 | qc->tf.protocol = ATA_PROT_ATAPI; |
| 1601 | qc->tf.lbam = (8 * 1024) & 0xff; |
| 1602 | qc->tf.lbah = (8 * 1024) >> 8; |
| 1603 | qc->nbytes = SCSI_SENSE_BUFFERSIZE; |
| 1604 | |
| 1605 | qc->waiting = &wait; |
| 1606 | qc->complete_fn = ata_qc_complete_noop; |
| 1607 | |
| 1608 | spin_lock_irqsave(&ap->host_set->lock, flags); |
| 1609 | rc = ata_qc_issue(qc); |
| 1610 | spin_unlock_irqrestore(&ap->host_set->lock, flags); |
| 1611 | |
| 1612 | if (rc) |
| 1613 | ata_port_disable(ap); |
| 1614 | else |
| 1615 | wait_for_completion(&wait); |
| 1616 | |
| 1617 | DPRINTK("EXIT\n"); |
| 1618 | } |
| 1619 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat) |
| 1621 | { |
| 1622 | struct scsi_cmnd *cmd = qc->scsicmd; |
| 1623 | |
Jeff Garzik | e12669e | 2005-10-05 18:39:23 -0400 | [diff] [blame] | 1624 | VPRINTK("ENTER, drv_stat == 0x%x\n", drv_stat); |
| 1625 | |
Jeff Garzik | a15dbeb | 2005-10-05 15:02:14 -0400 | [diff] [blame] | 1626 | if (unlikely(drv_stat & (ATA_BUSY | ATA_DRQ))) |
| 1627 | ata_to_sense_error(qc, drv_stat); |
Jeff Garzik | e12669e | 2005-10-05 18:39:23 -0400 | [diff] [blame] | 1628 | |
Jeff Garzik | a15dbeb | 2005-10-05 15:02:14 -0400 | [diff] [blame] | 1629 | else if (unlikely(drv_stat & ATA_ERR)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1630 | DPRINTK("request check condition\n"); |
| 1631 | |
Jeff Garzik | a15dbeb | 2005-10-05 15:02:14 -0400 | [diff] [blame] | 1632 | /* FIXME: command completion with check condition |
| 1633 | * but no sense causes the error handler to run, |
| 1634 | * which then issues REQUEST SENSE, fills in the sense |
| 1635 | * buffer, and completes the command (for the second |
| 1636 | * time). We need to issue REQUEST SENSE some other |
| 1637 | * way, to avoid completing the command twice. |
| 1638 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1639 | cmd->result = SAM_STAT_CHECK_CONDITION; |
| 1640 | |
| 1641 | qc->scsidone(cmd); |
| 1642 | |
| 1643 | return 1; |
Jeff Garzik | e12669e | 2005-10-05 18:39:23 -0400 | [diff] [blame] | 1644 | } |
| 1645 | |
| 1646 | else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1647 | u8 *scsicmd = cmd->cmnd; |
| 1648 | |
| 1649 | if (scsicmd[0] == INQUIRY) { |
| 1650 | u8 *buf = NULL; |
| 1651 | unsigned int buflen; |
| 1652 | |
| 1653 | buflen = ata_scsi_rbuf_get(cmd, &buf); |
Jeff Garzik | a15dbeb | 2005-10-05 15:02:14 -0400 | [diff] [blame] | 1654 | |
| 1655 | /* ATAPI devices typically report zero for their SCSI version, |
| 1656 | * and sometimes deviate from the spec WRT response data |
| 1657 | * format. If SCSI version is reported as zero like normal, |
| 1658 | * then we make the following fixups: 1) Fake MMC-5 version, |
| 1659 | * to indicate to the Linux scsi midlayer this is a modern |
| 1660 | * device. 2) Ensure response data format / ATAPI information |
| 1661 | * are always correct. |
| 1662 | */ |
| 1663 | /* FIXME: do we ever override EVPD pages and the like, with |
| 1664 | * this code? |
| 1665 | */ |
| 1666 | if (buf[2] == 0) { |
| 1667 | buf[2] = 0x5; |
| 1668 | buf[3] = 0x32; |
| 1669 | } |
| 1670 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | ata_scsi_rbuf_put(cmd, buf); |
| 1672 | } |
Jeff Garzik | a15dbeb | 2005-10-05 15:02:14 -0400 | [diff] [blame] | 1673 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | cmd->result = SAM_STAT_GOOD; |
| 1675 | } |
| 1676 | |
| 1677 | qc->scsidone(cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | return 0; |
| 1679 | } |
| 1680 | /** |
| 1681 | * atapi_xlat - Initialize PACKET taskfile |
| 1682 | * @qc: command structure to be initialized |
| 1683 | * @scsicmd: SCSI CDB associated with this PACKET command |
| 1684 | * |
| 1685 | * LOCKING: |
| 1686 | * spin_lock_irqsave(host_set lock) |
| 1687 | * |
| 1688 | * RETURNS: |
| 1689 | * Zero on success, non-zero on failure. |
| 1690 | */ |
| 1691 | |
| 1692 | static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd) |
| 1693 | { |
| 1694 | struct scsi_cmnd *cmd = qc->scsicmd; |
| 1695 | struct ata_device *dev = qc->dev; |
| 1696 | int using_pio = (dev->flags & ATA_DFLAG_PIO); |
| be7db05 | 2005-04-17 15:26:13 -0500 | [diff] [blame] | 1697 | int nodata = (cmd->sc_data_direction == DMA_NONE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1698 | |
| 1699 | if (!using_pio) |
| 1700 | /* Check whether ATAPI DMA is safe */ |
| 1701 | if (ata_check_atapi_dma(qc)) |
| 1702 | using_pio = 1; |
| 1703 | |
| 1704 | memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len); |
| 1705 | |
| 1706 | qc->complete_fn = atapi_qc_complete; |
| 1707 | |
| 1708 | qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
| be7db05 | 2005-04-17 15:26:13 -0500 | [diff] [blame] | 1709 | if (cmd->sc_data_direction == DMA_TO_DEVICE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1710 | qc->tf.flags |= ATA_TFLAG_WRITE; |
| 1711 | DPRINTK("direction: write\n"); |
| 1712 | } |
| 1713 | |
| 1714 | qc->tf.command = ATA_CMD_PACKET; |
| 1715 | |
| 1716 | /* no data, or PIO data xfer */ |
| 1717 | if (using_pio || nodata) { |
| 1718 | if (nodata) |
| 1719 | qc->tf.protocol = ATA_PROT_ATAPI_NODATA; |
| 1720 | else |
| 1721 | qc->tf.protocol = ATA_PROT_ATAPI; |
| 1722 | qc->tf.lbam = (8 * 1024) & 0xff; |
| 1723 | qc->tf.lbah = (8 * 1024) >> 8; |
| 1724 | } |
| 1725 | |
| 1726 | /* DMA data xfer */ |
| 1727 | else { |
| 1728 | qc->tf.protocol = ATA_PROT_ATAPI_DMA; |
| 1729 | qc->tf.feature |= ATAPI_PKT_DMA; |
| 1730 | |
| 1731 | #ifdef ATAPI_ENABLE_DMADIR |
| 1732 | /* some SATA bridges need us to indicate data xfer direction */ |
| be7db05 | 2005-04-17 15:26:13 -0500 | [diff] [blame] | 1733 | if (cmd->sc_data_direction != DMA_TO_DEVICE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | qc->tf.feature |= ATAPI_DMADIR; |
| 1735 | #endif |
| 1736 | } |
| 1737 | |
| 1738 | qc->nbytes = cmd->bufflen; |
| 1739 | |
| 1740 | return 0; |
| 1741 | } |
| 1742 | |
| 1743 | /** |
| 1744 | * ata_scsi_find_dev - lookup ata_device from scsi_cmnd |
| 1745 | * @ap: ATA port to which the device is attached |
| 1746 | * @scsidev: SCSI device from which we derive the ATA device |
| 1747 | * |
| 1748 | * Given various information provided in struct scsi_cmnd, |
| 1749 | * map that onto an ATA bus, and using that mapping |
| 1750 | * determine which ata_device is associated with the |
| 1751 | * SCSI command to be sent. |
| 1752 | * |
| 1753 | * LOCKING: |
| 1754 | * spin_lock_irqsave(host_set lock) |
| 1755 | * |
| 1756 | * RETURNS: |
| 1757 | * Associated ATA device, or %NULL if not found. |
| 1758 | */ |
| 1759 | |
| 1760 | static struct ata_device * |
| 1761 | ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev) |
| 1762 | { |
| 1763 | struct ata_device *dev; |
| 1764 | |
| 1765 | /* skip commands not addressed to targets we simulate */ |
| 1766 | if (likely(scsidev->id < ATA_MAX_DEVICES)) |
| 1767 | dev = &ap->device[scsidev->id]; |
| 1768 | else |
| 1769 | return NULL; |
| 1770 | |
| 1771 | if (unlikely((scsidev->channel != 0) || |
| 1772 | (scsidev->lun != 0))) |
| 1773 | return NULL; |
| 1774 | |
| 1775 | if (unlikely(!ata_dev_present(dev))) |
| 1776 | return NULL; |
| 1777 | |
Jeff Garzik | 6f10623 | 2005-08-30 21:52:18 -0400 | [diff] [blame] | 1778 | if (!atapi_enabled) { |
Jeff Garzik | 1623c81 | 2005-08-30 03:37:42 -0400 | [diff] [blame] | 1779 | if (unlikely(dev->class == ATA_DEV_ATAPI)) |
| 1780 | return NULL; |
| 1781 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1782 | |
| 1783 | return dev; |
| 1784 | } |
| 1785 | |
| 1786 | /** |
| 1787 | * ata_get_xlat_func - check if SCSI to ATA translation is possible |
| 1788 | * @dev: ATA device |
| 1789 | * @cmd: SCSI command opcode to consider |
| 1790 | * |
| 1791 | * Look up the SCSI command given, and determine whether the |
| 1792 | * SCSI command is to be translated or simulated. |
| 1793 | * |
| 1794 | * RETURNS: |
| 1795 | * Pointer to translation function if possible, %NULL if not. |
| 1796 | */ |
| 1797 | |
| 1798 | static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd) |
| 1799 | { |
| 1800 | switch (cmd) { |
| 1801 | case READ_6: |
| 1802 | case READ_10: |
| 1803 | case READ_16: |
| 1804 | |
| 1805 | case WRITE_6: |
| 1806 | case WRITE_10: |
| 1807 | case WRITE_16: |
| 1808 | return ata_scsi_rw_xlat; |
| 1809 | |
| 1810 | case SYNCHRONIZE_CACHE: |
| 1811 | if (ata_try_flush_cache(dev)) |
| 1812 | return ata_scsi_flush_xlat; |
| 1813 | break; |
| 1814 | |
| 1815 | case VERIFY: |
| 1816 | case VERIFY_16: |
| 1817 | return ata_scsi_verify_xlat; |
Douglas Gilbert | 972dcaf | 2005-08-11 03:35:53 -0400 | [diff] [blame] | 1818 | case START_STOP: |
| 1819 | return ata_scsi_start_stop_xlat; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | return NULL; |
| 1823 | } |
| 1824 | |
| 1825 | /** |
| 1826 | * ata_scsi_dump_cdb - dump SCSI command contents to dmesg |
| 1827 | * @ap: ATA port to which the command was being sent |
| 1828 | * @cmd: SCSI command to dump |
| 1829 | * |
| 1830 | * Prints the contents of a SCSI command via printk(). |
| 1831 | */ |
| 1832 | |
| 1833 | static inline void ata_scsi_dump_cdb(struct ata_port *ap, |
| 1834 | struct scsi_cmnd *cmd) |
| 1835 | { |
| 1836 | #ifdef ATA_DEBUG |
| 1837 | struct scsi_device *scsidev = cmd->device; |
| 1838 | u8 *scsicmd = cmd->cmnd; |
| 1839 | |
| 1840 | DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", |
| 1841 | ap->id, |
| 1842 | scsidev->channel, scsidev->id, scsidev->lun, |
| 1843 | scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3], |
| 1844 | scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7], |
| 1845 | scsicmd[8]); |
| 1846 | #endif |
| 1847 | } |
| 1848 | |
| 1849 | /** |
| 1850 | * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device |
| 1851 | * @cmd: SCSI command to be sent |
| 1852 | * @done: Completion function, called when command is complete |
| 1853 | * |
| 1854 | * In some cases, this function translates SCSI commands into |
| 1855 | * ATA taskfiles, and queues the taskfiles to be sent to |
| 1856 | * hardware. In other cases, this function simulates a |
| 1857 | * SCSI device by evaluating and responding to certain |
| 1858 | * SCSI commands. This creates the overall effect of |
| 1859 | * ATA and ATAPI devices appearing as SCSI devices. |
| 1860 | * |
| 1861 | * LOCKING: |
| 1862 | * Releases scsi-layer-held lock, and obtains host_set lock. |
| 1863 | * |
| 1864 | * RETURNS: |
| 1865 | * Zero. |
| 1866 | */ |
| 1867 | |
| 1868 | int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *)) |
| 1869 | { |
| 1870 | struct ata_port *ap; |
| 1871 | struct ata_device *dev; |
| 1872 | struct scsi_device *scsidev = cmd->device; |
| 1873 | |
| 1874 | ap = (struct ata_port *) &scsidev->host->hostdata[0]; |
| 1875 | |
| 1876 | ata_scsi_dump_cdb(ap, cmd); |
| 1877 | |
| 1878 | dev = ata_scsi_find_dev(ap, scsidev); |
| 1879 | if (unlikely(!dev)) { |
| 1880 | cmd->result = (DID_BAD_TARGET << 16); |
| 1881 | done(cmd); |
| 1882 | goto out_unlock; |
| 1883 | } |
| 1884 | |
| 1885 | if (dev->class == ATA_DEV_ATA) { |
| 1886 | ata_xlat_func_t xlat_func = ata_get_xlat_func(dev, |
| 1887 | cmd->cmnd[0]); |
| 1888 | |
| 1889 | if (xlat_func) |
| 1890 | ata_scsi_translate(ap, dev, cmd, done, xlat_func); |
| 1891 | else |
| 1892 | ata_scsi_simulate(dev->id, cmd, done); |
| 1893 | } else |
| 1894 | ata_scsi_translate(ap, dev, cmd, done, atapi_xlat); |
| 1895 | |
| 1896 | out_unlock: |
| 1897 | return 0; |
| 1898 | } |
| 1899 | |
| 1900 | /** |
| 1901 | * ata_scsi_simulate - simulate SCSI command on ATA device |
| 1902 | * @id: current IDENTIFY data for target device. |
| 1903 | * @cmd: SCSI command being sent to device. |
| 1904 | * @done: SCSI command completion function. |
| 1905 | * |
| 1906 | * Interprets and directly executes a select list of SCSI commands |
| 1907 | * that can be handled internally. |
| 1908 | * |
| 1909 | * LOCKING: |
| 1910 | * spin_lock_irqsave(host_set lock) |
| 1911 | */ |
| 1912 | |
| 1913 | void ata_scsi_simulate(u16 *id, |
| 1914 | struct scsi_cmnd *cmd, |
| 1915 | void (*done)(struct scsi_cmnd *)) |
| 1916 | { |
| 1917 | struct ata_scsi_args args; |
| 1918 | u8 *scsicmd = cmd->cmnd; |
| 1919 | |
| 1920 | args.id = id; |
| 1921 | args.cmd = cmd; |
| 1922 | args.done = done; |
| 1923 | |
| 1924 | switch(scsicmd[0]) { |
| 1925 | /* no-op's, complete with success */ |
| 1926 | case SYNCHRONIZE_CACHE: |
| 1927 | case REZERO_UNIT: |
| 1928 | case SEEK_6: |
| 1929 | case SEEK_10: |
| 1930 | case TEST_UNIT_READY: |
| 1931 | case FORMAT_UNIT: /* FIXME: correct? */ |
| 1932 | case SEND_DIAGNOSTIC: /* FIXME: correct? */ |
| 1933 | ata_scsi_rbuf_fill(&args, ata_scsiop_noop); |
| 1934 | break; |
| 1935 | |
| 1936 | case INQUIRY: |
| 1937 | if (scsicmd[1] & 2) /* is CmdDt set? */ |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1938 | ata_scsi_invalid_field(cmd, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1939 | else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */ |
| 1940 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std); |
| 1941 | else if (scsicmd[2] == 0x00) |
| 1942 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00); |
| 1943 | else if (scsicmd[2] == 0x80) |
| 1944 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80); |
| 1945 | else if (scsicmd[2] == 0x83) |
| 1946 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83); |
| 1947 | else |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1948 | ata_scsi_invalid_field(cmd, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | break; |
| 1950 | |
| 1951 | case MODE_SENSE: |
| 1952 | case MODE_SENSE_10: |
| 1953 | ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense); |
| 1954 | break; |
| 1955 | |
| 1956 | case MODE_SELECT: /* unconditionally return */ |
| 1957 | case MODE_SELECT_10: /* bad-field-in-cdb */ |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1958 | ata_scsi_invalid_field(cmd, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1959 | break; |
| 1960 | |
| 1961 | case READ_CAPACITY: |
| 1962 | ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap); |
| 1963 | break; |
| 1964 | |
| 1965 | case SERVICE_ACTION_IN: |
| 1966 | if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16) |
| 1967 | ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap); |
| 1968 | else |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1969 | ata_scsi_invalid_field(cmd, done); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | break; |
| 1971 | |
| 1972 | case REPORT_LUNS: |
| 1973 | ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns); |
| 1974 | break; |
| 1975 | |
| 1976 | /* mandantory commands we haven't implemented yet */ |
| 1977 | case REQUEST_SENSE: |
| 1978 | |
| 1979 | /* all other commands */ |
| 1980 | default: |
Douglas Gilbert | ae00651 | 2005-10-09 09:09:35 -0400 | [diff] [blame] | 1981 | ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0); |
| 1982 | /* "Invalid command operation code" */ |
| 1983 | done(cmd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | break; |
| 1985 | } |
| 1986 | } |
| 1987 | |
Jeff Garzik | 644dd0c | 2005-10-03 15:55:19 -0400 | [diff] [blame] | 1988 | void ata_scsi_scan_host(struct ata_port *ap) |
| 1989 | { |
Jeff Garzik | 3f19ee8 | 2005-10-03 21:36:41 -0400 | [diff] [blame] | 1990 | struct ata_device *dev; |
Jeff Garzik | 644dd0c | 2005-10-03 15:55:19 -0400 | [diff] [blame] | 1991 | unsigned int i; |
| 1992 | |
| 1993 | if (ap->flags & ATA_FLAG_PORT_DISABLED) |
| 1994 | return; |
| 1995 | |
Jeff Garzik | 3f19ee8 | 2005-10-03 21:36:41 -0400 | [diff] [blame] | 1996 | for (i = 0; i < ATA_MAX_DEVICES; i++) { |
| 1997 | dev = &ap->device[i]; |
| 1998 | |
| 1999 | if (ata_dev_present(dev)) |
| 2000 | scsi_scan_target(&ap->host->shost_gendev, 0, i, 0, 0); |
| 2001 | } |
Jeff Garzik | 644dd0c | 2005-10-03 15:55:19 -0400 | [diff] [blame] | 2002 | } |
| 2003 | |