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