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