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