upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / drivers / scsi / libata-scsi.c
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
36 typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, u8 *scsicmd);
37 static struct ata_device *
38 ata_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  */
59 int 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
70 int 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
104 out:
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  */
129 struct 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
170 void 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         /* Fall back to interpreting status bits */
287         while(stat_table[i][0] != 0xFF)
288         {
289                 if(stat_table[i][0] & drv_stat)
290                 {
291                         sb[0] = 0x70;
292                         sb[2] = stat_table[i][1];
293                         sb[7] = 0x0a;
294                         sb[12] = stat_table[i][2];
295                         sb[13] = stat_table[i][3];
296                         return;
297                 }
298                 i++;
299         }
300         /* No error ?? */
301         printk(KERN_ERR "ata%u: called with no error (%02X)!\n", qc->ap->id, drv_stat);
302         /* additional-sense-code[-qualifier] */
303         
304         sb[0] = 0x70;
305         sb[2] = MEDIUM_ERROR;
306         sb[7] = 0x0A;
307         if (cmd->sc_data_direction == SCSI_DATA_READ) {
308                 sb[12] = 0x11; /* "unrecovered read error" */
309                 sb[13] = 0x04;
310         } else {
311                 sb[12] = 0x0C; /* "write error -             */
312                 sb[13] = 0x02; /*  auto-reallocation failed" */
313         }
314 }
315
316 /**
317  *      ata_scsi_slave_config - Set SCSI device attributes
318  *      @sdev: SCSI device to examine
319  *
320  *      This is called before we actually start reading
321  *      and writing to the device, to configure certain
322  *      SCSI mid-layer behaviors.
323  *
324  *      LOCKING:
325  *      Defined by SCSI layer.  We don't really care.
326  */
327
328 int ata_scsi_slave_config(struct scsi_device *sdev)
329 {
330         sdev->use_10_for_rw = 1;
331         sdev->use_10_for_ms = 1;
332
333         blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
334
335         if (sdev->id < ATA_MAX_DEVICES) {
336                 struct ata_port *ap;
337                 struct ata_device *dev;
338
339                 ap = (struct ata_port *) &sdev->host->hostdata[0];
340                 dev = &ap->device[sdev->id];
341
342                 /* TODO: 1024 is an arbitrary number, not the
343                  * hardware maximum.  This should be increased to
344                  * 65534 when Jens Axboe's patch for dynamically
345                  * determining max_sectors is merged.
346                  */
347                 if ((dev->flags & ATA_DFLAG_LBA48) &&
348                     ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) {
349                         sdev->host->max_sectors = 2048;
350                         blk_queue_max_sectors(sdev->request_queue, 2048);
351                 }
352         }
353
354         return 0;       /* scsi layer doesn't check return value, sigh */
355 }
356
357 /**
358  *      ata_scsi_error - SCSI layer error handler callback
359  *      @host: SCSI host on which error occurred
360  *
361  *      Handles SCSI-layer-thrown error events.
362  *
363  *      LOCKING:
364  *      Inherited from SCSI layer (none, can sleep)
365  *
366  *      RETURNS:
367  *      Zero.
368  */
369
370 int ata_scsi_error(struct Scsi_Host *host)
371 {
372         struct ata_port *ap;
373
374         DPRINTK("ENTER\n");
375
376         ap = (struct ata_port *) &host->hostdata[0];
377         ap->ops->eng_timeout(ap);
378
379         /* TODO: this is per-command; when queueing is supported
380          * this code will either change or move to a more
381          * appropriate place
382          */
383         host->host_failed--;
384
385         DPRINTK("EXIT\n");
386         return 0;
387 }
388
389 /**
390  *      ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
391  *      @qc: Storage for translated ATA taskfile
392  *      @scsicmd: SCSI command to translate (ignored)
393  *
394  *      Sets up an ATA taskfile to issue FLUSH CACHE or
395  *      FLUSH CACHE EXT.
396  *
397  *      LOCKING:
398  *      spin_lock_irqsave(host_set lock)
399  *
400  *      RETURNS:
401  *      Zero on success, non-zero on error.
402  */
403
404 static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
405 {
406         struct ata_taskfile *tf = &qc->tf;
407
408         tf->flags |= ATA_TFLAG_DEVICE;
409         tf->protocol = ATA_PROT_NODATA;
410
411         if ((tf->flags & ATA_TFLAG_LBA48) &&
412             (ata_id_has_flush_ext(qc->dev->id)))
413                 tf->command = ATA_CMD_FLUSH_EXT;
414         else
415                 tf->command = ATA_CMD_FLUSH;
416
417         return 0;
418 }
419
420 /**
421  *      ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
422  *      @qc: Storage for translated ATA taskfile
423  *      @scsicmd: SCSI command to translate
424  *
425  *      Converts SCSI VERIFY command to an ATA READ VERIFY command.
426  *
427  *      LOCKING:
428  *      spin_lock_irqsave(host_set lock)
429  *
430  *      RETURNS:
431  *      Zero on success, non-zero on error.
432  */
433
434 static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
435 {
436         struct ata_taskfile *tf = &qc->tf;
437         unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
438         u64 dev_sectors = qc->dev->n_sectors;
439         u64 sect = 0;
440         u32 n_sect = 0;
441
442         tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
443         tf->protocol = ATA_PROT_NODATA;
444         tf->device |= ATA_LBA;
445
446         if (scsicmd[0] == VERIFY) {
447                 sect |= ((u64)scsicmd[2]) << 24;
448                 sect |= ((u64)scsicmd[3]) << 16;
449                 sect |= ((u64)scsicmd[4]) << 8;
450                 sect |= ((u64)scsicmd[5]);
451
452                 n_sect |= ((u32)scsicmd[7]) << 8;
453                 n_sect |= ((u32)scsicmd[8]);
454         }
455
456         else if (scsicmd[0] == VERIFY_16) {
457                 sect |= ((u64)scsicmd[2]) << 56;
458                 sect |= ((u64)scsicmd[3]) << 48;
459                 sect |= ((u64)scsicmd[4]) << 40;
460                 sect |= ((u64)scsicmd[5]) << 32;
461                 sect |= ((u64)scsicmd[6]) << 24;
462                 sect |= ((u64)scsicmd[7]) << 16;
463                 sect |= ((u64)scsicmd[8]) << 8;
464                 sect |= ((u64)scsicmd[9]);
465
466                 n_sect |= ((u32)scsicmd[10]) << 24;
467                 n_sect |= ((u32)scsicmd[11]) << 16;
468                 n_sect |= ((u32)scsicmd[12]) << 8;
469                 n_sect |= ((u32)scsicmd[13]);
470         }
471
472         else
473                 return 1;
474
475         if (!n_sect)
476                 return 1;
477         if (sect >= dev_sectors)
478                 return 1;
479         if ((sect + n_sect) > dev_sectors)
480                 return 1;
481         if (lba48) {
482                 if (n_sect > (64 * 1024))
483                         return 1;
484         } else {
485                 if (n_sect > 256)
486                         return 1;
487         }
488
489         if (lba48) {
490                 tf->hob_nsect = (n_sect >> 8) & 0xff;
491
492                 tf->hob_lbah = (sect >> 40) & 0xff;
493                 tf->hob_lbam = (sect >> 32) & 0xff;
494                 tf->hob_lbal = (sect >> 24) & 0xff;
495         } else
496                 tf->device |= (sect >> 24) & 0xf;
497
498         tf->nsect = n_sect & 0xff;
499
500         tf->hob_lbah = (sect >> 16) & 0xff;
501         tf->hob_lbam = (sect >> 8) & 0xff;
502         tf->hob_lbal = sect & 0xff;
503
504         return 0;
505 }
506
507 /**
508  *      ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
509  *      @qc: Storage for translated ATA taskfile
510  *      @scsicmd: SCSI command to translate
511  *
512  *      Converts any of six SCSI read/write commands into the
513  *      ATA counterpart, including starting sector (LBA),
514  *      sector count, and taking into account the device's LBA48
515  *      support.
516  *
517  *      Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
518  *      %WRITE_16 are currently supported.
519  *
520  *      LOCKING:
521  *      spin_lock_irqsave(host_set lock)
522  *
523  *      RETURNS:
524  *      Zero on success, non-zero on error.
525  */
526
527 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
528 {
529         struct ata_taskfile *tf = &qc->tf;
530         unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
531
532         tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
533         tf->protocol = qc->dev->xfer_protocol;
534         tf->device |= ATA_LBA;
535
536         if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
537             scsicmd[0] == READ_16) {
538                 tf->command = qc->dev->read_cmd;
539         } else {
540                 tf->command = qc->dev->write_cmd;
541                 tf->flags |= ATA_TFLAG_WRITE;
542         }
543
544         if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) {
545                 if (lba48) {
546                         tf->hob_nsect = scsicmd[7];
547                         tf->hob_lbal = scsicmd[2];
548
549                         qc->nsect = ((unsigned int)scsicmd[7] << 8) |
550                                         scsicmd[8];
551                 } else {
552                         /* if we don't support LBA48 addressing, the request
553                          * -may- be too large. */
554                         if ((scsicmd[2] & 0xf0) || scsicmd[7])
555                                 return 1;
556
557                         /* stores LBA27:24 in lower 4 bits of device reg */
558                         tf->device |= scsicmd[2];
559
560                         qc->nsect = scsicmd[8];
561                 }
562
563                 tf->nsect = scsicmd[8];
564                 tf->lbal = scsicmd[5];
565                 tf->lbam = scsicmd[4];
566                 tf->lbah = scsicmd[3];
567
568                 VPRINTK("ten-byte command\n");
569                 return 0;
570         }
571
572         if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) {
573                 qc->nsect = tf->nsect = scsicmd[4];
574                 tf->lbal = scsicmd[3];
575                 tf->lbam = scsicmd[2];
576                 tf->lbah = scsicmd[1] & 0x1f; /* mask out reserved bits */
577
578                 VPRINTK("six-byte command\n");
579                 return 0;
580         }
581
582         if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) {
583                 /* rule out impossible LBAs and sector counts */
584                 if (scsicmd[2] || scsicmd[3] || scsicmd[10] || scsicmd[11])
585                         return 1;
586
587                 if (lba48) {
588                         tf->hob_nsect = scsicmd[12];
589                         tf->hob_lbal = scsicmd[6];
590                         tf->hob_lbam = scsicmd[5];
591                         tf->hob_lbah = scsicmd[4];
592
593                         qc->nsect = ((unsigned int)scsicmd[12] << 8) |
594                                         scsicmd[13];
595                 } else {
596                         /* once again, filter out impossible non-zero values */
597                         if (scsicmd[4] || scsicmd[5] || scsicmd[12] ||
598                             (scsicmd[6] & 0xf0))
599                                 return 1;
600
601                         /* stores LBA27:24 in lower 4 bits of device reg */
602                         tf->device |= scsicmd[2];
603
604                         qc->nsect = scsicmd[13];
605                 }
606
607                 tf->nsect = scsicmd[13];
608                 tf->lbal = scsicmd[9];
609                 tf->lbam = scsicmd[8];
610                 tf->lbah = scsicmd[7];
611
612                 VPRINTK("sixteen-byte command\n");
613                 return 0;
614         }
615
616         DPRINTK("no-byte command\n");
617         return 1;
618 }
619
620 static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
621 {
622         struct scsi_cmnd *cmd = qc->scsicmd;
623
624         if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ)))
625                 ata_to_sense_error(qc, drv_stat);
626         else
627                 cmd->result = SAM_STAT_GOOD;
628
629         qc->scsidone(cmd);
630
631         return 0;
632 }
633
634 /**
635  *      ata_scsi_translate - Translate then issue SCSI command to ATA device
636  *      @ap: ATA port to which the command is addressed
637  *      @dev: ATA device to which the command is addressed
638  *      @cmd: SCSI command to execute
639  *      @done: SCSI command completion function
640  *      @xlat_func: Actor which translates @cmd to an ATA taskfile
641  *
642  *      Our ->queuecommand() function has decided that the SCSI
643  *      command issued can be directly translated into an ATA
644  *      command, rather than handled internally.
645  *
646  *      This function sets up an ata_queued_cmd structure for the
647  *      SCSI command, and sends that ata_queued_cmd to the hardware.
648  *
649  *      LOCKING:
650  *      spin_lock_irqsave(host_set lock)
651  */
652
653 static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
654                               struct scsi_cmnd *cmd,
655                               void (*done)(struct scsi_cmnd *),
656                               ata_xlat_func_t xlat_func)
657 {
658         struct ata_queued_cmd *qc;
659         u8 *scsicmd = cmd->cmnd;
660
661         VPRINTK("ENTER\n");
662
663         qc = ata_scsi_qc_new(ap, dev, cmd, done);
664         if (!qc)
665                 return;
666
667         /* data is present; dma-map it */
668         if (cmd->sc_data_direction == SCSI_DATA_READ ||
669             cmd->sc_data_direction == SCSI_DATA_WRITE) {
670                 if (unlikely(cmd->request_bufflen < 1)) {
671                         printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
672                                ap->id, dev->devno);
673                         goto err_out;
674                 }
675
676                 if (cmd->use_sg)
677                         ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
678                 else
679                         ata_sg_init_one(qc, cmd->request_buffer,
680                                         cmd->request_bufflen);
681
682                 qc->dma_dir = cmd->sc_data_direction;
683         }
684
685         qc->complete_fn = ata_scsi_qc_complete;
686
687         if (xlat_func(qc, scsicmd))
688                 goto err_out;
689
690         /* select device, send command to hardware */
691         if (ata_qc_issue(qc))
692                 goto err_out;
693
694         VPRINTK("EXIT\n");
695         return;
696
697 err_out:
698         ata_bad_cdb(cmd, done);
699         DPRINTK("EXIT - badcmd\n");
700 }
701
702 /**
703  *      ata_scsi_rbuf_get - Map response buffer.
704  *      @cmd: SCSI command containing buffer to be mapped.
705  *      @buf_out: Pointer to mapped area.
706  *
707  *      Maps buffer contained within SCSI command @cmd.
708  *
709  *      LOCKING:
710  *      spin_lock_irqsave(host_set lock)
711  *
712  *      RETURNS:
713  *      Length of response buffer.
714  */
715
716 static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
717 {
718         u8 *buf;
719         unsigned int buflen;
720
721         if (cmd->use_sg) {
722                 struct scatterlist *sg;
723
724                 sg = (struct scatterlist *) cmd->request_buffer;
725                 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
726                 buflen = sg->length;
727         } else {
728                 buf = cmd->request_buffer;
729                 buflen = cmd->request_bufflen;
730         }
731
732         *buf_out = buf;
733         return buflen;
734 }
735
736 /**
737  *      ata_scsi_rbuf_put - Unmap response buffer.
738  *      @cmd: SCSI command containing buffer to be unmapped.
739  *      @buf: buffer to unmap
740  *
741  *      Unmaps response buffer contained within @cmd.
742  *
743  *      LOCKING:
744  *      spin_lock_irqsave(host_set lock)
745  */
746
747 static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
748 {
749         if (cmd->use_sg) {
750                 struct scatterlist *sg;
751
752                 sg = (struct scatterlist *) cmd->request_buffer;
753                 kunmap_atomic(buf - sg->offset, KM_USER0);
754         }
755 }
756
757 /**
758  *      ata_scsi_rbuf_fill - wrapper for SCSI command simulators
759  *      @args: device IDENTIFY data / SCSI command of interest.
760  *      @actor: Callback hook for desired SCSI command simulator
761  *
762  *      Takes care of the hard work of simulating a SCSI command...
763  *      Mapping the response buffer, calling the command's handler,
764  *      and handling the handler's return value.  This return value
765  *      indicates whether the handler wishes the SCSI command to be
766  *      completed successfully, or not.
767  *
768  *      LOCKING:
769  *      spin_lock_irqsave(host_set lock)
770  */
771
772 void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
773                         unsigned int (*actor) (struct ata_scsi_args *args,
774                                            u8 *rbuf, unsigned int buflen))
775 {
776         u8 *rbuf;
777         unsigned int buflen, rc;
778         struct scsi_cmnd *cmd = args->cmd;
779
780         buflen = ata_scsi_rbuf_get(cmd, &rbuf);
781         memset(rbuf, 0, buflen);
782         rc = actor(args, rbuf, buflen);
783         ata_scsi_rbuf_put(cmd, rbuf);
784
785         if (rc)
786                 ata_bad_cdb(cmd, args->done);
787         else {
788                 cmd->result = SAM_STAT_GOOD;
789                 args->done(cmd);
790         }
791 }
792
793 /**
794  *      ata_scsiop_inq_std - Simulate INQUIRY command
795  *      @args: device IDENTIFY data / SCSI command of interest.
796  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
797  *      @buflen: Response buffer length.
798  *
799  *      Returns standard device identification data associated
800  *      with non-EVPD INQUIRY command output.
801  *
802  *      LOCKING:
803  *      spin_lock_irqsave(host_set lock)
804  */
805
806 unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
807                                unsigned int buflen)
808 {
809         u8 hdr[] = {
810                 TYPE_DISK,
811                 0,
812                 0x5,    /* claim SPC-3 version compatibility */
813                 2,
814                 95 - 4
815         };
816
817         /* set scsi removeable (RMB) bit per ata bit */
818         if (ata_id_removeable(args->id))
819                 hdr[1] |= (1 << 7);
820
821         VPRINTK("ENTER\n");
822
823         memcpy(rbuf, hdr, sizeof(hdr));
824
825         if (buflen > 35) {
826                 memcpy(&rbuf[8], "ATA     ", 8);
827                 ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
828                 ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
829                 if (rbuf[32] == 0 || rbuf[32] == ' ')
830                         memcpy(&rbuf[32], "n/a ", 4);
831         }
832
833         if (buflen > 63) {
834                 const u8 versions[] = {
835                         0x60,   /* SAM-3 (no version claimed) */
836
837                         0x03,
838                         0x20,   /* SBC-2 (no version claimed) */
839
840                         0x02,
841                         0x60    /* SPC-3 (no version claimed) */
842                 };
843
844                 memcpy(rbuf + 59, versions, sizeof(versions));
845         }
846
847         return 0;
848 }
849
850 /**
851  *      ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
852  *      @args: device IDENTIFY data / SCSI command of interest.
853  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
854  *      @buflen: Response buffer length.
855  *
856  *      Returns list of inquiry EVPD pages available.
857  *
858  *      LOCKING:
859  *      spin_lock_irqsave(host_set lock)
860  */
861
862 unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
863                               unsigned int buflen)
864 {
865         const u8 pages[] = {
866                 0x00,   /* page 0x00, this page */
867                 0x80,   /* page 0x80, unit serial no page */
868                 0x83    /* page 0x83, device ident page */
869         };
870         rbuf[3] = sizeof(pages);        /* number of supported EVPD pages */
871
872         if (buflen > 6)
873                 memcpy(rbuf + 4, pages, sizeof(pages));
874
875         return 0;
876 }
877
878 /**
879  *      ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
880  *      @args: device IDENTIFY data / SCSI command of interest.
881  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
882  *      @buflen: Response buffer length.
883  *
884  *      Returns ATA device serial number.
885  *
886  *      LOCKING:
887  *      spin_lock_irqsave(host_set lock)
888  */
889
890 unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
891                               unsigned int buflen)
892 {
893         const u8 hdr[] = {
894                 0,
895                 0x80,                   /* this page code */
896                 0,
897                 ATA_SERNO_LEN,          /* page len */
898         };
899         memcpy(rbuf, hdr, sizeof(hdr));
900
901         if (buflen > (ATA_SERNO_LEN + 4 - 1))
902                 ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
903                                   ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
904
905         return 0;
906 }
907
908 static const char *inq_83_str = "Linux ATA-SCSI simulator";
909
910 /**
911  *      ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
912  *      @args: device IDENTIFY data / SCSI command of interest.
913  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
914  *      @buflen: Response buffer length.
915  *
916  *      Returns device identification.  Currently hardcoded to
917  *      return "Linux ATA-SCSI simulator".
918  *
919  *      LOCKING:
920  *      spin_lock_irqsave(host_set lock)
921  */
922
923 unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
924                               unsigned int buflen)
925 {
926         rbuf[1] = 0x83;                 /* this page code */
927         rbuf[3] = 4 + strlen(inq_83_str);       /* page len */
928
929         /* our one and only identification descriptor (vendor-specific) */
930         if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
931                 rbuf[4 + 0] = 2;        /* code set: ASCII */
932                 rbuf[4 + 3] = strlen(inq_83_str);
933                 memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
934         }
935
936         return 0;
937 }
938
939 /**
940  *      ata_scsiop_noop -
941  *      @args: device IDENTIFY data / SCSI command of interest.
942  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
943  *      @buflen: Response buffer length.
944  *
945  *      No operation.  Simply returns success to caller, to indicate
946  *      that the caller should successfully complete this SCSI command.
947  *
948  *      LOCKING:
949  *      spin_lock_irqsave(host_set lock)
950  */
951
952 unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
953                             unsigned int buflen)
954 {
955         VPRINTK("ENTER\n");
956         return 0;
957 }
958
959 /**
960  *      ata_msense_push - Push data onto MODE SENSE data output buffer
961  *      @ptr_io: (input/output) Location to store more output data
962  *      @last: End of output data buffer
963  *      @buf: Pointer to BLOB being added to output buffer
964  *      @buflen: Length of BLOB
965  *
966  *      Store MODE SENSE data on an output buffer.
967  *
968  *      LOCKING:
969  *      None.
970  */
971
972 static void ata_msense_push(u8 **ptr_io, const u8 *last,
973                             const u8 *buf, unsigned int buflen)
974 {
975         u8 *ptr = *ptr_io;
976
977         if ((ptr + buflen - 1) > last)
978                 return;
979
980         memcpy(ptr, buf, buflen);
981
982         ptr += buflen;
983
984         *ptr_io = ptr;
985 }
986
987 /**
988  *      ata_msense_caching - Simulate MODE SENSE caching info page
989  *      @id: device IDENTIFY data
990  *      @ptr_io: (input/output) Location to store more output data
991  *      @last: End of output data buffer
992  *
993  *      Generate a caching info page, which conditionally indicates
994  *      write caching to the SCSI layer, depending on device
995  *      capabilities.
996  *
997  *      LOCKING:
998  *      None.
999  */
1000
1001 static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1002                                        const u8 *last)
1003 {
1004         u8 page[] = {
1005                 0x8,                            /* page code */
1006                 0x12,                           /* page length */
1007                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   /* 10 zeroes */
1008                 0, 0, 0, 0, 0, 0, 0, 0          /* 8 zeroes */
1009         };
1010
1011         if (ata_id_wcache_enabled(id))
1012                 page[2] |= (1 << 2);    /* write cache enable */
1013         if (!ata_id_rahead_enabled(id))
1014                 page[12] |= (1 << 5);   /* disable read ahead */
1015
1016         ata_msense_push(ptr_io, last, page, sizeof(page));
1017         return sizeof(page);
1018 }
1019
1020 /**
1021  *      ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1022  *      @dev: Device associated with this MODE SENSE command
1023  *      @ptr_io: (input/output) Location to store more output data
1024  *      @last: End of output data buffer
1025  *
1026  *      Generate a generic MODE SENSE control mode page.
1027  *
1028  *      LOCKING:
1029  *      None.
1030  */
1031
1032 static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1033 {
1034         const u8 page[] = {0xa, 0xa, 2, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
1035
1036         ata_msense_push(ptr_io, last, page, sizeof(page));
1037         return sizeof(page);
1038 }
1039
1040 /**
1041  *      ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1042  *      @dev: Device associated with this MODE SENSE command
1043  *      @ptr_io: (input/output) Location to store more output data
1044  *      @last: End of output data buffer
1045  *
1046  *      Generate a generic MODE SENSE r/w error recovery page.
1047  *
1048  *      LOCKING:
1049  *      None.
1050  */
1051
1052 static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1053 {
1054         const u8 page[] = {
1055                 0x1,                      /* page code */
1056                 0xa,                      /* page length */
1057                 (1 << 7) | (1 << 6),      /* note auto r/w reallocation */
1058                 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
1059         };
1060
1061         ata_msense_push(ptr_io, last, page, sizeof(page));
1062         return sizeof(page);
1063 }
1064
1065 /**
1066  *      ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1067  *      @args: device IDENTIFY data / SCSI command of interest.
1068  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1069  *      @buflen: Response buffer length.
1070  *
1071  *      Simulate MODE SENSE commands.
1072  *
1073  *      LOCKING:
1074  *      spin_lock_irqsave(host_set lock)
1075  */
1076
1077 unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1078                                   unsigned int buflen)
1079 {
1080         u8 *scsicmd = args->cmd->cmnd, *p, *last;
1081         unsigned int page_control, six_byte, output_len;
1082
1083         VPRINTK("ENTER\n");
1084
1085         six_byte = (scsicmd[0] == MODE_SENSE);
1086
1087         /* we only support saved and current values (which we treat
1088          * in the same manner)
1089          */
1090         page_control = scsicmd[2] >> 6;
1091         if ((page_control != 0) && (page_control != 3))
1092                 return 1;
1093
1094         if (six_byte)
1095                 output_len = 4;
1096         else
1097                 output_len = 8;
1098
1099         p = rbuf + output_len;
1100         last = rbuf + buflen - 1;
1101
1102         switch(scsicmd[2] & 0x3f) {
1103         case 0x01:              /* r/w error recovery */
1104                 output_len += ata_msense_rw_recovery(&p, last);
1105                 break;
1106
1107         case 0x08:              /* caching */
1108                 output_len += ata_msense_caching(args->id, &p, last);
1109                 break;
1110
1111         case 0x0a: {            /* control mode */
1112                 output_len += ata_msense_ctl_mode(&p, last);
1113                 break;
1114                 }
1115
1116         case 0x3f:              /* all pages */
1117                 output_len += ata_msense_rw_recovery(&p, last);
1118                 output_len += ata_msense_caching(args->id, &p, last);
1119                 output_len += ata_msense_ctl_mode(&p, last);
1120                 break;
1121
1122         default:                /* invalid page code */
1123                 return 1;
1124         }
1125
1126         if (six_byte) {
1127                 output_len--;
1128                 rbuf[0] = output_len;
1129         } else {
1130                 output_len -= 2;
1131                 rbuf[0] = output_len >> 8;
1132                 rbuf[1] = output_len;
1133         }
1134
1135         return 0;
1136 }
1137
1138 /**
1139  *      ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
1140  *      @args: device IDENTIFY data / SCSI command of interest.
1141  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1142  *      @buflen: Response buffer length.
1143  *
1144  *      Simulate READ CAPACITY commands.
1145  *
1146  *      LOCKING:
1147  *      spin_lock_irqsave(host_set lock)
1148  */
1149
1150 unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
1151                                 unsigned int buflen)
1152 {
1153         u64 n_sectors;
1154         u32 tmp;
1155
1156         VPRINTK("ENTER\n");
1157
1158         if (ata_id_has_lba48(args->id))
1159                 n_sectors = ata_id_u64(args->id, 100);
1160         else
1161                 n_sectors = ata_id_u32(args->id, 60);
1162         n_sectors--;            /* ATA TotalUserSectors - 1 */
1163
1164         tmp = n_sectors;        /* note: truncates, if lba48 */
1165         if (args->cmd->cmnd[0] == READ_CAPACITY) {
1166                 /* sector count, 32-bit */
1167                 rbuf[0] = tmp >> (8 * 3);
1168                 rbuf[1] = tmp >> (8 * 2);
1169                 rbuf[2] = tmp >> (8 * 1);
1170                 rbuf[3] = tmp;
1171
1172                 /* sector size */
1173                 tmp = ATA_SECT_SIZE;
1174                 rbuf[6] = tmp >> 8;
1175                 rbuf[7] = tmp;
1176
1177         } else {
1178                 /* sector count, 64-bit */
1179                 rbuf[2] = n_sectors >> (8 * 7);
1180                 rbuf[3] = n_sectors >> (8 * 6);
1181                 rbuf[4] = n_sectors >> (8 * 5);
1182                 rbuf[5] = n_sectors >> (8 * 4);
1183                 rbuf[6] = tmp >> (8 * 3);
1184                 rbuf[7] = tmp >> (8 * 2);
1185                 rbuf[8] = tmp >> (8 * 1);
1186                 rbuf[9] = tmp;
1187
1188                 /* sector size */
1189                 tmp = ATA_SECT_SIZE;
1190                 rbuf[12] = tmp >> 8;
1191                 rbuf[13] = tmp;
1192         }
1193
1194         return 0;
1195 }
1196
1197 /**
1198  *      ata_scsiop_report_luns - Simulate REPORT LUNS command
1199  *      @args: device IDENTIFY data / SCSI command of interest.
1200  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1201  *      @buflen: Response buffer length.
1202  *
1203  *      Simulate REPORT LUNS command.
1204  *
1205  *      LOCKING:
1206  *      spin_lock_irqsave(host_set lock)
1207  */
1208
1209 unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1210                                    unsigned int buflen)
1211 {
1212         VPRINTK("ENTER\n");
1213         rbuf[3] = 8;    /* just one lun, LUN 0, size 8 bytes */
1214
1215         return 0;
1216 }
1217
1218 /**
1219  *      ata_scsi_badcmd - End a SCSI request with an error
1220  *      @cmd: SCSI request to be handled
1221  *      @done: SCSI command completion function
1222  *      @asc: SCSI-defined additional sense code
1223  *      @ascq: SCSI-defined additional sense code qualifier
1224  *
1225  *      Helper function that completes a SCSI command with
1226  *      %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
1227  *      and the specified additional sense codes.
1228  *
1229  *      LOCKING:
1230  *      spin_lock_irqsave(host_set lock)
1231  */
1232
1233 void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
1234 {
1235         DPRINTK("ENTER\n");
1236         cmd->result = SAM_STAT_CHECK_CONDITION;
1237
1238         cmd->sense_buffer[0] = 0x70;
1239         cmd->sense_buffer[2] = ILLEGAL_REQUEST;
1240         cmd->sense_buffer[7] = 14 - 8;  /* addnl. sense len. FIXME: correct? */
1241         cmd->sense_buffer[12] = asc;
1242         cmd->sense_buffer[13] = ascq;
1243
1244         done(cmd);
1245 }
1246
1247 static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
1248 {
1249         struct scsi_cmnd *cmd = qc->scsicmd;
1250
1251         if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) {
1252                 DPRINTK("request check condition\n");
1253
1254                 cmd->result = SAM_STAT_CHECK_CONDITION;
1255
1256                 qc->scsidone(cmd);
1257
1258                 return 1;
1259         } else {
1260                 u8 *scsicmd = cmd->cmnd;
1261
1262                 if (scsicmd[0] == INQUIRY) {
1263                         u8 *buf = NULL;
1264                         unsigned int buflen;
1265
1266                         buflen = ata_scsi_rbuf_get(cmd, &buf);
1267                         buf[2] = 0x5;
1268                         buf[3] = (buf[3] & 0xf0) | 2;
1269                         ata_scsi_rbuf_put(cmd, buf);
1270                 }
1271                 cmd->result = SAM_STAT_GOOD;
1272         }
1273
1274         qc->scsidone(cmd);
1275
1276         return 0;
1277 }
1278 /**
1279  *      atapi_xlat - Initialize PACKET taskfile
1280  *      @qc: command structure to be initialized
1281  *      @scsicmd: SCSI CDB associated with this PACKET command
1282  *
1283  *      LOCKING:
1284  *      spin_lock_irqsave(host_set lock)
1285  *
1286  *      RETURNS:
1287  *      Zero on success, non-zero on failure.
1288  */
1289
1290 static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
1291 {
1292         struct scsi_cmnd *cmd = qc->scsicmd;
1293         struct ata_device *dev = qc->dev;
1294         int using_pio = (dev->flags & ATA_DFLAG_PIO);
1295         int nodata = (cmd->sc_data_direction == SCSI_DATA_NONE);
1296
1297         if (!using_pio)
1298                 /* Check whether ATAPI DMA is safe */
1299                 if (ata_check_atapi_dma(qc))
1300                         using_pio = 1;
1301
1302         memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
1303
1304         qc->complete_fn = atapi_qc_complete;
1305
1306         qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1307         if (cmd->sc_data_direction == SCSI_DATA_WRITE) {
1308                 qc->tf.flags |= ATA_TFLAG_WRITE;
1309                 DPRINTK("direction: write\n");
1310         }
1311
1312         qc->tf.command = ATA_CMD_PACKET;
1313
1314         /* no data, or PIO data xfer */
1315         if (using_pio || nodata) {
1316                 if (nodata)
1317                         qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
1318                 else
1319                         qc->tf.protocol = ATA_PROT_ATAPI;
1320                 qc->tf.lbam = (8 * 1024) & 0xff;
1321                 qc->tf.lbah = (8 * 1024) >> 8;
1322         }
1323
1324         /* DMA data xfer */
1325         else {
1326                 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
1327                 qc->tf.feature |= ATAPI_PKT_DMA;
1328
1329 #ifdef ATAPI_ENABLE_DMADIR
1330                 /* some SATA bridges need us to indicate data xfer direction */
1331                 if (cmd->sc_data_direction != SCSI_DATA_WRITE)
1332                         qc->tf.feature |= ATAPI_DMADIR;
1333 #endif
1334         }
1335
1336         qc->nbytes = cmd->bufflen;
1337
1338         return 0;
1339 }
1340
1341 /**
1342  *      ata_scsi_find_dev - lookup ata_device from scsi_cmnd
1343  *      @ap: ATA port to which the device is attached
1344  *      @scsidev: SCSI device from which we derive the ATA device
1345  *
1346  *      Given various information provided in struct scsi_cmnd,
1347  *      map that onto an ATA bus, and using that mapping
1348  *      determine which ata_device is associated with the
1349  *      SCSI command to be sent.
1350  *
1351  *      LOCKING:
1352  *      spin_lock_irqsave(host_set lock)
1353  *
1354  *      RETURNS:
1355  *      Associated ATA device, or %NULL if not found.
1356  */
1357
1358 static struct ata_device *
1359 ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev)
1360 {
1361         struct ata_device *dev;
1362
1363         /* skip commands not addressed to targets we simulate */
1364         if (likely(scsidev->id < ATA_MAX_DEVICES))
1365                 dev = &ap->device[scsidev->id];
1366         else
1367                 return NULL;
1368
1369         if (unlikely((scsidev->channel != 0) ||
1370                      (scsidev->lun != 0)))
1371                 return NULL;
1372
1373         if (unlikely(!ata_dev_present(dev)))
1374                 return NULL;
1375
1376 #ifndef ATA_ENABLE_ATAPI
1377         if (unlikely(dev->class == ATA_DEV_ATAPI))
1378                 return NULL;
1379 #endif
1380
1381         return dev;
1382 }
1383
1384 /**
1385  *      ata_get_xlat_func - check if SCSI to ATA translation is possible
1386  *      @dev: ATA device
1387  *      @cmd: SCSI command opcode to consider
1388  *
1389  *      Look up the SCSI command given, and determine whether the
1390  *      SCSI command is to be translated or simulated.
1391  *
1392  *      RETURNS:
1393  *      Pointer to translation function if possible, %NULL if not.
1394  */
1395
1396 static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
1397 {
1398         switch (cmd) {
1399         case READ_6:
1400         case READ_10:
1401         case READ_16:
1402
1403         case WRITE_6:
1404         case WRITE_10:
1405         case WRITE_16:
1406                 return ata_scsi_rw_xlat;
1407
1408         case SYNCHRONIZE_CACHE:
1409                 if (ata_try_flush_cache(dev))
1410                         return ata_scsi_flush_xlat;
1411                 break;
1412
1413         case VERIFY:
1414         case VERIFY_16:
1415                 return ata_scsi_verify_xlat;
1416         }
1417
1418         return NULL;
1419 }
1420
1421 /**
1422  *      ata_scsi_dump_cdb - dump SCSI command contents to dmesg
1423  *      @ap: ATA port to which the command was being sent
1424  *      @cmd: SCSI command to dump
1425  *
1426  *      Prints the contents of a SCSI command via printk().
1427  */
1428
1429 static inline void ata_scsi_dump_cdb(struct ata_port *ap,
1430                                      struct scsi_cmnd *cmd)
1431 {
1432 #ifdef ATA_DEBUG
1433         struct scsi_device *scsidev = cmd->device;
1434         u8 *scsicmd = cmd->cmnd;
1435
1436         DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
1437                 ap->id,
1438                 scsidev->channel, scsidev->id, scsidev->lun,
1439                 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
1440                 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
1441                 scsicmd[8]);
1442 #endif
1443 }
1444
1445 /**
1446  *      ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
1447  *      @cmd: SCSI command to be sent
1448  *      @done: Completion function, called when command is complete
1449  *
1450  *      In some cases, this function translates SCSI commands into
1451  *      ATA taskfiles, and queues the taskfiles to be sent to
1452  *      hardware.  In other cases, this function simulates a
1453  *      SCSI device by evaluating and responding to certain
1454  *      SCSI commands.  This creates the overall effect of
1455  *      ATA and ATAPI devices appearing as SCSI devices.
1456  *
1457  *      LOCKING:
1458  *      Releases scsi-layer-held lock, and obtains host_set lock.
1459  *
1460  *      RETURNS:
1461  *      Zero.
1462  */
1463
1464 int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
1465 {
1466         struct ata_port *ap;
1467         struct ata_device *dev;
1468         struct scsi_device *scsidev = cmd->device;
1469
1470         ap = (struct ata_port *) &scsidev->host->hostdata[0];
1471
1472         ata_scsi_dump_cdb(ap, cmd);
1473
1474         dev = ata_scsi_find_dev(ap, scsidev);
1475         if (unlikely(!dev)) {
1476                 cmd->result = (DID_BAD_TARGET << 16);
1477                 done(cmd);
1478                 goto out_unlock;
1479         }
1480
1481         if (dev->class == ATA_DEV_ATA) {
1482                 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
1483                                                               cmd->cmnd[0]);
1484
1485                 if (xlat_func)
1486                         ata_scsi_translate(ap, dev, cmd, done, xlat_func);
1487                 else
1488                         ata_scsi_simulate(dev->id, cmd, done);
1489         } else
1490                 ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
1491
1492 out_unlock:
1493         return 0;
1494 }
1495
1496 /**
1497  *      ata_scsi_simulate - simulate SCSI command on ATA device
1498  *      @id: current IDENTIFY data for target device.
1499  *      @cmd: SCSI command being sent to device.
1500  *      @done: SCSI command completion function.
1501  *
1502  *      Interprets and directly executes a select list of SCSI commands
1503  *      that can be handled internally.
1504  *
1505  *      LOCKING:
1506  *      spin_lock_irqsave(host_set lock)
1507  */
1508
1509 void ata_scsi_simulate(u16 *id,
1510                       struct scsi_cmnd *cmd,
1511                       void (*done)(struct scsi_cmnd *))
1512 {
1513         struct ata_scsi_args args;
1514         u8 *scsicmd = cmd->cmnd;
1515
1516         args.id = id;
1517         args.cmd = cmd;
1518         args.done = done;
1519
1520         switch(scsicmd[0]) {
1521                 /* no-op's, complete with success */
1522                 case SYNCHRONIZE_CACHE:
1523                 case REZERO_UNIT:
1524                 case SEEK_6:
1525                 case SEEK_10:
1526                 case TEST_UNIT_READY:
1527                 case FORMAT_UNIT:               /* FIXME: correct? */
1528                 case SEND_DIAGNOSTIC:           /* FIXME: correct? */
1529                         ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
1530                         break;
1531
1532                 case INQUIRY:
1533                         if (scsicmd[1] & 2)                /* is CmdDt set?  */
1534                                 ata_bad_cdb(cmd, done);
1535                         else if ((scsicmd[1] & 1) == 0)    /* is EVPD clear? */
1536                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
1537                         else if (scsicmd[2] == 0x00)
1538                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
1539                         else if (scsicmd[2] == 0x80)
1540                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
1541                         else if (scsicmd[2] == 0x83)
1542                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
1543                         else
1544                                 ata_bad_cdb(cmd, done);
1545                         break;
1546
1547                 case MODE_SENSE:
1548                 case MODE_SENSE_10:
1549                         ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
1550                         break;
1551
1552                 case MODE_SELECT:       /* unconditionally return */
1553                 case MODE_SELECT_10:    /* bad-field-in-cdb */
1554                         ata_bad_cdb(cmd, done);
1555                         break;
1556
1557                 case READ_CAPACITY:
1558                         ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1559                         break;
1560
1561                 case SERVICE_ACTION_IN:
1562                         if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
1563                                 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1564                         else
1565                                 ata_bad_cdb(cmd, done);
1566                         break;
1567
1568                 case REPORT_LUNS:
1569                         ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
1570                         break;
1571
1572                 /* mandantory commands we haven't implemented yet */
1573                 case REQUEST_SENSE:
1574
1575                 /* all other commands */
1576                 default:
1577                         ata_bad_scsiop(cmd, done);
1578                         break;
1579         }
1580 }
1581