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