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