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