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