]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/scsi/libata-scsi.c
Merge /spare/repo/linux-2.6/
[linux-2.6-omap-h63xx.git] / drivers / scsi / libata-scsi.c
1 /*
2  *  libata-scsi.c - helper library for ATA
3  *
4  *  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_start_stop_xlat - Translate SCSI START STOP UNIT command
407  *      @qc: Storage for translated ATA taskfile
408  *      @scsicmd: SCSI command to translate
409  *
410  *      Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
411  *      (to start). Perhaps these commands should be preceded by
412  *      CHECK POWER MODE to see what power mode the device is already in.
413  *      [See SAT revision 5 at www.t10.org]
414  *
415  *      LOCKING:
416  *      spin_lock_irqsave(host_set lock)
417  *
418  *      RETURNS:
419  *      Zero on success, non-zero on error.
420  */
421
422 static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
423                                              u8 *scsicmd)
424 {
425         struct ata_taskfile *tf = &qc->tf;
426
427         tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
428         tf->protocol = ATA_PROT_NODATA;
429         if (scsicmd[1] & 0x1) {
430                 ;       /* ignore IMMED bit, violates sat-r05 */
431         }
432         if (scsicmd[4] & 0x2)
433                 return 1;       /* LOEJ bit set not supported */
434         if (((scsicmd[4] >> 4) & 0xf) != 0)
435                 return 1;       /* power conditions not supported */
436         if (scsicmd[4] & 0x1) {
437                 tf->nsect = 1;  /* 1 sector, lba=0 */
438                 tf->lbah = 0x0;
439                 tf->lbam = 0x0;
440                 tf->lbal = 0x0;
441                 tf->device |= ATA_LBA;
442                 tf->command = ATA_CMD_VERIFY;   /* READ VERIFY */
443         } else {
444                 tf->nsect = 0;  /* time period value (0 implies now) */
445                 tf->command = ATA_CMD_STANDBY;
446                 /* Consider: ATA STANDBY IMMEDIATE command */
447         }
448         /*
449          * Standby and Idle condition timers could be implemented but that
450          * would require libata to implement the Power condition mode page
451          * and allow the user to change it. Changing mode pages requires
452          * MODE SELECT to be implemented.
453          */
454
455         return 0;
456 }
457
458
459 /**
460  *      ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
461  *      @qc: Storage for translated ATA taskfile
462  *      @scsicmd: SCSI command to translate (ignored)
463  *
464  *      Sets up an ATA taskfile to issue FLUSH CACHE or
465  *      FLUSH CACHE EXT.
466  *
467  *      LOCKING:
468  *      spin_lock_irqsave(host_set lock)
469  *
470  *      RETURNS:
471  *      Zero on success, non-zero on error.
472  */
473
474 static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
475 {
476         struct ata_taskfile *tf = &qc->tf;
477
478         tf->flags |= ATA_TFLAG_DEVICE;
479         tf->protocol = ATA_PROT_NODATA;
480
481         if ((tf->flags & ATA_TFLAG_LBA48) &&
482             (ata_id_has_flush_ext(qc->dev->id)))
483                 tf->command = ATA_CMD_FLUSH_EXT;
484         else
485                 tf->command = ATA_CMD_FLUSH;
486
487         return 0;
488 }
489
490 /**
491  *      ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
492  *      @qc: Storage for translated ATA taskfile
493  *      @scsicmd: SCSI command to translate
494  *
495  *      Converts SCSI VERIFY command to an ATA READ VERIFY command.
496  *
497  *      LOCKING:
498  *      spin_lock_irqsave(host_set lock)
499  *
500  *      RETURNS:
501  *      Zero on success, non-zero on error.
502  */
503
504 static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
505 {
506         struct ata_taskfile *tf = &qc->tf;
507         struct ata_device *dev = qc->dev;
508         unsigned int lba   = tf->flags & ATA_TFLAG_LBA;
509         unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
510         u64 dev_sectors = qc->dev->n_sectors;
511         u64 block = 0;
512         u32 n_block = 0;
513
514         tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
515         tf->protocol = ATA_PROT_NODATA;
516
517         if (scsicmd[0] == VERIFY) {
518                 block |= ((u64)scsicmd[2]) << 24;
519                 block |= ((u64)scsicmd[3]) << 16;
520                 block |= ((u64)scsicmd[4]) << 8;
521                 block |= ((u64)scsicmd[5]);
522
523                 n_block |= ((u32)scsicmd[7]) << 8;
524                 n_block |= ((u32)scsicmd[8]);
525         }
526
527         else if (scsicmd[0] == VERIFY_16) {
528                 block |= ((u64)scsicmd[2]) << 56;
529                 block |= ((u64)scsicmd[3]) << 48;
530                 block |= ((u64)scsicmd[4]) << 40;
531                 block |= ((u64)scsicmd[5]) << 32;
532                 block |= ((u64)scsicmd[6]) << 24;
533                 block |= ((u64)scsicmd[7]) << 16;
534                 block |= ((u64)scsicmd[8]) << 8;
535                 block |= ((u64)scsicmd[9]);
536
537                 n_block |= ((u32)scsicmd[10]) << 24;
538                 n_block |= ((u32)scsicmd[11]) << 16;
539                 n_block |= ((u32)scsicmd[12]) << 8;
540                 n_block |= ((u32)scsicmd[13]);
541         }
542
543         else
544                 return 1;
545
546         if (!n_block)
547                 return 1;
548         if (block >= dev_sectors)
549                 return 1;
550         if ((block + n_block) > dev_sectors)
551                 return 1;
552         if (lba48) {
553                 if (n_block > (64 * 1024))
554                         return 1;
555         } else {
556                 if (n_block > 256)
557                         return 1;
558         }
559
560         if (lba) {
561                 if (lba48) {
562                         tf->command = ATA_CMD_VERIFY_EXT;
563
564                         tf->hob_nsect = (n_block >> 8) & 0xff;
565
566                         tf->hob_lbah = (block >> 40) & 0xff;
567                         tf->hob_lbam = (block >> 32) & 0xff;
568                         tf->hob_lbal = (block >> 24) & 0xff;
569                 } else {
570                         tf->command = ATA_CMD_VERIFY;
571
572                         tf->device |= (block >> 24) & 0xf;
573                 }
574
575                 tf->nsect = n_block & 0xff;
576
577                 tf->lbah = (block >> 16) & 0xff;
578                 tf->lbam = (block >> 8) & 0xff;
579                 tf->lbal = block & 0xff;
580
581                 tf->device |= ATA_LBA;
582         } else {
583                 /* CHS */
584                 u32 sect, head, cyl, track;
585
586                 /* Convert LBA to CHS */
587                 track = (u32)block / dev->sectors;
588                 cyl   = track / dev->heads;
589                 head  = track % dev->heads;
590                 sect  = (u32)block % dev->sectors + 1;
591
592                 DPRINTK("block[%u] track[%u] cyl[%u] head[%u] sect[%u] \n", (u32)block, track, cyl, head, sect);
593                 
594                 /* Check whether the converted CHS can fit. 
595                    Cylinder: 0-65535 
596                    Head: 0-15
597                    Sector: 1-255*/
598                 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect)) 
599                         return 1;
600                 
601                 tf->command = ATA_CMD_VERIFY;
602                 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
603                 tf->lbal = sect;
604                 tf->lbam = cyl;
605                 tf->lbah = cyl >> 8;
606                 tf->device |= head;
607         }
608
609         return 0;
610 }
611
612 /**
613  *      ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
614  *      @qc: Storage for translated ATA taskfile
615  *      @scsicmd: SCSI command to translate
616  *
617  *      Converts any of six SCSI read/write commands into the
618  *      ATA counterpart, including starting sector (LBA),
619  *      sector count, and taking into account the device's LBA48
620  *      support.
621  *
622  *      Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
623  *      %WRITE_16 are currently supported.
624  *
625  *      LOCKING:
626  *      spin_lock_irqsave(host_set lock)
627  *
628  *      RETURNS:
629  *      Zero on success, non-zero on error.
630  */
631
632 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
633 {
634         struct ata_taskfile *tf = &qc->tf;
635         struct ata_device *dev = qc->dev;
636         unsigned int lba   = tf->flags & ATA_TFLAG_LBA;
637         unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
638         u64 block = 0;
639         u32 n_block = 0;
640
641         tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
642         tf->protocol = qc->dev->xfer_protocol;
643
644         if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
645             scsicmd[0] == READ_16) {
646                 tf->command = qc->dev->read_cmd;
647         } else {
648                 tf->command = qc->dev->write_cmd;
649                 tf->flags |= ATA_TFLAG_WRITE;
650         }
651
652         /* Calculate the SCSI LBA and transfer length. */
653         if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) {
654                 block |= ((u64)scsicmd[2]) << 24;
655                 block |= ((u64)scsicmd[3]) << 16;
656                 block |= ((u64)scsicmd[4]) << 8;
657                 block |= ((u64)scsicmd[5]);
658
659                 n_block |= ((u32)scsicmd[7]) << 8;
660                 n_block |= ((u32)scsicmd[8]);
661
662                 VPRINTK("ten-byte command\n");
663         } else if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) {
664                 block |= ((u64)scsicmd[2]) << 8;
665                 block |= ((u64)scsicmd[3]);
666
667                 n_block |= ((u32)scsicmd[4]);
668                 if (!n_block)
669                         n_block = 256;
670         
671                 VPRINTK("six-byte command\n");
672         } else if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) {
673                 block |= ((u64)scsicmd[2]) << 56;
674                 block |= ((u64)scsicmd[3]) << 48;
675                 block |= ((u64)scsicmd[4]) << 40;
676                 block |= ((u64)scsicmd[5]) << 32;
677                 block |= ((u64)scsicmd[6]) << 24;
678                 block |= ((u64)scsicmd[7]) << 16;
679                 block |= ((u64)scsicmd[8]) << 8;
680                 block |= ((u64)scsicmd[9]);
681
682                 n_block |= ((u32)scsicmd[10]) << 24;
683                 n_block |= ((u32)scsicmd[11]) << 16;
684                 n_block |= ((u32)scsicmd[12]) << 8;
685                 n_block |= ((u32)scsicmd[13]);
686
687                 VPRINTK("sixteen-byte command\n");
688         } else {
689                 DPRINTK("no-byte command\n");
690                 return 1;
691         }
692
693         /* Check and compose ATA command */
694         if (!n_block)
695                 /* In ATA, sector count 0 means 256 or 65536 sectors, not 0 sectors. */
696                 return 1;
697
698         if (lba) {
699                 if (lba48) {
700                         /* The request -may- be too large for LBA48. */
701                         if ((block >> 48) || (n_block > 65536))
702                                 return 1;
703
704                         tf->hob_nsect = (n_block >> 8) & 0xff;
705
706                         tf->hob_lbah = (block >> 40) & 0xff;
707                         tf->hob_lbam = (block >> 32) & 0xff;
708                         tf->hob_lbal = (block >> 24) & 0xff;
709                 } else { 
710                         /* LBA28 */
711
712                         /* The request -may- be too large for LBA28. */
713                         if ((block >> 28) || (n_block > 256))
714                                 return 1;
715
716                         tf->device |= (block >> 24) & 0xf;
717                 }
718         
719                 qc->nsect = n_block;
720                 tf->nsect = n_block & 0xff;
721
722                 tf->lbah = (block >> 16) & 0xff;
723                 tf->lbam = (block >> 8) & 0xff;
724                 tf->lbal = block & 0xff;
725
726                 tf->device |= ATA_LBA;
727         } else { 
728                 /* CHS */
729                 u32 sect, head, cyl, track;
730
731                 /* The request -may- be too large for CHS addressing. */
732                 if ((block >> 28) || (n_block > 256))
733                         return 1;
734                         
735                 /* Convert LBA to CHS */
736                 track = (u32)block / dev->sectors;
737                 cyl   = track / dev->heads;
738                 head  = track % dev->heads;
739                 sect  = (u32)block % dev->sectors + 1;
740
741                 DPRINTK("block[%u] track[%u] cyl[%u] head[%u] sect[%u] \n", 
742                         (u32)block, track, cyl, head, sect);
743                 
744                 /* Check whether the converted CHS can fit. 
745                    Cylinder: 0-65535 
746                    Head: 0-15
747                    Sector: 1-255*/
748                 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect)) 
749                         return 1;
750                 
751                 qc->nsect = n_block;
752                 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
753                 tf->lbal = sect;
754                 tf->lbam = cyl;
755                 tf->lbah = cyl >> 8;
756                 tf->device |= head;
757         }
758
759         return 0;
760 }
761
762 static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
763 {
764         struct scsi_cmnd *cmd = qc->scsicmd;
765
766         if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ)))
767                 ata_to_sense_error(qc, drv_stat);
768         else
769                 cmd->result = SAM_STAT_GOOD;
770
771         qc->scsidone(cmd);
772
773         return 0;
774 }
775
776 /**
777  *      ata_scsi_translate - Translate then issue SCSI command to ATA device
778  *      @ap: ATA port to which the command is addressed
779  *      @dev: ATA device to which the command is addressed
780  *      @cmd: SCSI command to execute
781  *      @done: SCSI command completion function
782  *      @xlat_func: Actor which translates @cmd to an ATA taskfile
783  *
784  *      Our ->queuecommand() function has decided that the SCSI
785  *      command issued can be directly translated into an ATA
786  *      command, rather than handled internally.
787  *
788  *      This function sets up an ata_queued_cmd structure for the
789  *      SCSI command, and sends that ata_queued_cmd to the hardware.
790  *
791  *      LOCKING:
792  *      spin_lock_irqsave(host_set lock)
793  */
794
795 static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
796                               struct scsi_cmnd *cmd,
797                               void (*done)(struct scsi_cmnd *),
798                               ata_xlat_func_t xlat_func)
799 {
800         struct ata_queued_cmd *qc;
801         u8 *scsicmd = cmd->cmnd;
802
803         VPRINTK("ENTER\n");
804
805         qc = ata_scsi_qc_new(ap, dev, cmd, done);
806         if (!qc)
807                 return;
808
809         /* data is present; dma-map it */
810         if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
811             cmd->sc_data_direction == DMA_TO_DEVICE) {
812                 if (unlikely(cmd->request_bufflen < 1)) {
813                         printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
814                                ap->id, dev->devno);
815                         goto err_out;
816                 }
817
818                 if (cmd->use_sg)
819                         ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
820                 else
821                         ata_sg_init_one(qc, cmd->request_buffer,
822                                         cmd->request_bufflen);
823
824                 qc->dma_dir = cmd->sc_data_direction;
825         }
826
827         qc->complete_fn = ata_scsi_qc_complete;
828
829         if (xlat_func(qc, scsicmd))
830                 goto err_out;
831
832         /* select device, send command to hardware */
833         if (ata_qc_issue(qc))
834                 goto err_out;
835
836         VPRINTK("EXIT\n");
837         return;
838
839 err_out:
840         ata_qc_free(qc);
841         ata_bad_cdb(cmd, done);
842         DPRINTK("EXIT - badcmd\n");
843 }
844
845 /**
846  *      ata_scsi_rbuf_get - Map response buffer.
847  *      @cmd: SCSI command containing buffer to be mapped.
848  *      @buf_out: Pointer to mapped area.
849  *
850  *      Maps buffer contained within SCSI command @cmd.
851  *
852  *      LOCKING:
853  *      spin_lock_irqsave(host_set lock)
854  *
855  *      RETURNS:
856  *      Length of response buffer.
857  */
858
859 static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
860 {
861         u8 *buf;
862         unsigned int buflen;
863
864         if (cmd->use_sg) {
865                 struct scatterlist *sg;
866
867                 sg = (struct scatterlist *) cmd->request_buffer;
868                 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
869                 buflen = sg->length;
870         } else {
871                 buf = cmd->request_buffer;
872                 buflen = cmd->request_bufflen;
873         }
874
875         *buf_out = buf;
876         return buflen;
877 }
878
879 /**
880  *      ata_scsi_rbuf_put - Unmap response buffer.
881  *      @cmd: SCSI command containing buffer to be unmapped.
882  *      @buf: buffer to unmap
883  *
884  *      Unmaps response buffer contained within @cmd.
885  *
886  *      LOCKING:
887  *      spin_lock_irqsave(host_set lock)
888  */
889
890 static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
891 {
892         if (cmd->use_sg) {
893                 struct scatterlist *sg;
894
895                 sg = (struct scatterlist *) cmd->request_buffer;
896                 kunmap_atomic(buf - sg->offset, KM_USER0);
897         }
898 }
899
900 /**
901  *      ata_scsi_rbuf_fill - wrapper for SCSI command simulators
902  *      @args: device IDENTIFY data / SCSI command of interest.
903  *      @actor: Callback hook for desired SCSI command simulator
904  *
905  *      Takes care of the hard work of simulating a SCSI command...
906  *      Mapping the response buffer, calling the command's handler,
907  *      and handling the handler's return value.  This return value
908  *      indicates whether the handler wishes the SCSI command to be
909  *      completed successfully, or not.
910  *
911  *      LOCKING:
912  *      spin_lock_irqsave(host_set lock)
913  */
914
915 void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
916                         unsigned int (*actor) (struct ata_scsi_args *args,
917                                            u8 *rbuf, unsigned int buflen))
918 {
919         u8 *rbuf;
920         unsigned int buflen, rc;
921         struct scsi_cmnd *cmd = args->cmd;
922
923         buflen = ata_scsi_rbuf_get(cmd, &rbuf);
924         memset(rbuf, 0, buflen);
925         rc = actor(args, rbuf, buflen);
926         ata_scsi_rbuf_put(cmd, rbuf);
927
928         if (rc)
929                 ata_bad_cdb(cmd, args->done);
930         else {
931                 cmd->result = SAM_STAT_GOOD;
932                 args->done(cmd);
933         }
934 }
935
936 /**
937  *      ata_scsiop_inq_std - Simulate INQUIRY command
938  *      @args: device IDENTIFY data / SCSI command of interest.
939  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
940  *      @buflen: Response buffer length.
941  *
942  *      Returns standard device identification data associated
943  *      with non-EVPD INQUIRY command output.
944  *
945  *      LOCKING:
946  *      spin_lock_irqsave(host_set lock)
947  */
948
949 unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
950                                unsigned int buflen)
951 {
952         u8 hdr[] = {
953                 TYPE_DISK,
954                 0,
955                 0x5,    /* claim SPC-3 version compatibility */
956                 2,
957                 95 - 4
958         };
959
960         /* set scsi removeable (RMB) bit per ata bit */
961         if (ata_id_removeable(args->id))
962                 hdr[1] |= (1 << 7);
963
964         VPRINTK("ENTER\n");
965
966         memcpy(rbuf, hdr, sizeof(hdr));
967
968         if (buflen > 35) {
969                 memcpy(&rbuf[8], "ATA     ", 8);
970                 ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
971                 ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
972                 if (rbuf[32] == 0 || rbuf[32] == ' ')
973                         memcpy(&rbuf[32], "n/a ", 4);
974         }
975
976         if (buflen > 63) {
977                 const u8 versions[] = {
978                         0x60,   /* SAM-3 (no version claimed) */
979
980                         0x03,
981                         0x20,   /* SBC-2 (no version claimed) */
982
983                         0x02,
984                         0x60    /* SPC-3 (no version claimed) */
985                 };
986
987                 memcpy(rbuf + 59, versions, sizeof(versions));
988         }
989
990         return 0;
991 }
992
993 /**
994  *      ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
995  *      @args: device IDENTIFY data / SCSI command of interest.
996  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
997  *      @buflen: Response buffer length.
998  *
999  *      Returns list of inquiry EVPD pages available.
1000  *
1001  *      LOCKING:
1002  *      spin_lock_irqsave(host_set lock)
1003  */
1004
1005 unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
1006                               unsigned int buflen)
1007 {
1008         const u8 pages[] = {
1009                 0x00,   /* page 0x00, this page */
1010                 0x80,   /* page 0x80, unit serial no page */
1011                 0x83    /* page 0x83, device ident page */
1012         };
1013         rbuf[3] = sizeof(pages);        /* number of supported EVPD pages */
1014
1015         if (buflen > 6)
1016                 memcpy(rbuf + 4, pages, sizeof(pages));
1017
1018         return 0;
1019 }
1020
1021 /**
1022  *      ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
1023  *      @args: device IDENTIFY data / SCSI command of interest.
1024  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1025  *      @buflen: Response buffer length.
1026  *
1027  *      Returns ATA device serial number.
1028  *
1029  *      LOCKING:
1030  *      spin_lock_irqsave(host_set lock)
1031  */
1032
1033 unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
1034                               unsigned int buflen)
1035 {
1036         const u8 hdr[] = {
1037                 0,
1038                 0x80,                   /* this page code */
1039                 0,
1040                 ATA_SERNO_LEN,          /* page len */
1041         };
1042         memcpy(rbuf, hdr, sizeof(hdr));
1043
1044         if (buflen > (ATA_SERNO_LEN + 4 - 1))
1045                 ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
1046                                   ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1047
1048         return 0;
1049 }
1050
1051 static const char *inq_83_str = "Linux ATA-SCSI simulator";
1052
1053 /**
1054  *      ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
1055  *      @args: device IDENTIFY data / SCSI command of interest.
1056  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1057  *      @buflen: Response buffer length.
1058  *
1059  *      Returns device identification.  Currently hardcoded to
1060  *      return "Linux ATA-SCSI simulator".
1061  *
1062  *      LOCKING:
1063  *      spin_lock_irqsave(host_set lock)
1064  */
1065
1066 unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
1067                               unsigned int buflen)
1068 {
1069         rbuf[1] = 0x83;                 /* this page code */
1070         rbuf[3] = 4 + strlen(inq_83_str);       /* page len */
1071
1072         /* our one and only identification descriptor (vendor-specific) */
1073         if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
1074                 rbuf[4 + 0] = 2;        /* code set: ASCII */
1075                 rbuf[4 + 3] = strlen(inq_83_str);
1076                 memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
1077         }
1078
1079         return 0;
1080 }
1081
1082 /**
1083  *      ata_scsiop_noop - Command handler that simply returns success.
1084  *      @args: device IDENTIFY data / SCSI command of interest.
1085  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1086  *      @buflen: Response buffer length.
1087  *
1088  *      No operation.  Simply returns success to caller, to indicate
1089  *      that the caller should successfully complete this SCSI command.
1090  *
1091  *      LOCKING:
1092  *      spin_lock_irqsave(host_set lock)
1093  */
1094
1095 unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
1096                             unsigned int buflen)
1097 {
1098         VPRINTK("ENTER\n");
1099         return 0;
1100 }
1101
1102 /**
1103  *      ata_msense_push - Push data onto MODE SENSE data output buffer
1104  *      @ptr_io: (input/output) Location to store more output data
1105  *      @last: End of output data buffer
1106  *      @buf: Pointer to BLOB being added to output buffer
1107  *      @buflen: Length of BLOB
1108  *
1109  *      Store MODE SENSE data on an output buffer.
1110  *
1111  *      LOCKING:
1112  *      None.
1113  */
1114
1115 static void ata_msense_push(u8 **ptr_io, const u8 *last,
1116                             const u8 *buf, unsigned int buflen)
1117 {
1118         u8 *ptr = *ptr_io;
1119
1120         if ((ptr + buflen - 1) > last)
1121                 return;
1122
1123         memcpy(ptr, buf, buflen);
1124
1125         ptr += buflen;
1126
1127         *ptr_io = ptr;
1128 }
1129
1130 /**
1131  *      ata_msense_caching - Simulate MODE SENSE caching info page
1132  *      @id: device IDENTIFY data
1133  *      @ptr_io: (input/output) Location to store more output data
1134  *      @last: End of output data buffer
1135  *
1136  *      Generate a caching info page, which conditionally indicates
1137  *      write caching to the SCSI layer, depending on device
1138  *      capabilities.
1139  *
1140  *      LOCKING:
1141  *      None.
1142  */
1143
1144 static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1145                                        const u8 *last)
1146 {
1147         u8 page[] = {
1148                 0x8,                            /* page code */
1149                 0x12,                           /* page length */
1150                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   /* 10 zeroes */
1151                 0, 0, 0, 0, 0, 0, 0, 0          /* 8 zeroes */
1152         };
1153
1154         if (ata_id_wcache_enabled(id))
1155                 page[2] |= (1 << 2);    /* write cache enable */
1156         if (!ata_id_rahead_enabled(id))
1157                 page[12] |= (1 << 5);   /* disable read ahead */
1158
1159         ata_msense_push(ptr_io, last, page, sizeof(page));
1160         return sizeof(page);
1161 }
1162
1163 /**
1164  *      ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1165  *      @dev: Device associated with this MODE SENSE command
1166  *      @ptr_io: (input/output) Location to store more output data
1167  *      @last: End of output data buffer
1168  *
1169  *      Generate a generic MODE SENSE control mode page.
1170  *
1171  *      LOCKING:
1172  *      None.
1173  */
1174
1175 static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1176 {
1177         const u8 page[] = {0xa, 0xa, 6, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
1178
1179         /* byte 2: set the descriptor format sense data bit (bit 2)
1180          * since we need to support returning this format for SAT
1181          * commands and any SCSI commands against a 48b LBA device.
1182          */
1183
1184         ata_msense_push(ptr_io, last, page, sizeof(page));
1185         return sizeof(page);
1186 }
1187
1188 /**
1189  *      ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1190  *      @dev: Device associated with this MODE SENSE command
1191  *      @ptr_io: (input/output) Location to store more output data
1192  *      @last: End of output data buffer
1193  *
1194  *      Generate a generic MODE SENSE r/w error recovery page.
1195  *
1196  *      LOCKING:
1197  *      None.
1198  */
1199
1200 static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1201 {
1202         const u8 page[] = {
1203                 0x1,                      /* page code */
1204                 0xa,                      /* page length */
1205                 (1 << 7) | (1 << 6),      /* note auto r/w reallocation */
1206                 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
1207         };
1208
1209         ata_msense_push(ptr_io, last, page, sizeof(page));
1210         return sizeof(page);
1211 }
1212
1213 /**
1214  *      ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1215  *      @args: device IDENTIFY data / SCSI command of interest.
1216  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1217  *      @buflen: Response buffer length.
1218  *
1219  *      Simulate MODE SENSE commands.
1220  *
1221  *      LOCKING:
1222  *      spin_lock_irqsave(host_set lock)
1223  */
1224
1225 unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1226                                   unsigned int buflen)
1227 {
1228         u8 *scsicmd = args->cmd->cmnd, *p, *last;
1229         unsigned int page_control, six_byte, output_len;
1230
1231         VPRINTK("ENTER\n");
1232
1233         six_byte = (scsicmd[0] == MODE_SENSE);
1234
1235         /* we only support saved and current values (which we treat
1236          * in the same manner)
1237          */
1238         page_control = scsicmd[2] >> 6;
1239         if ((page_control != 0) && (page_control != 3))
1240                 return 1;
1241
1242         if (six_byte)
1243                 output_len = 4;
1244         else
1245                 output_len = 8;
1246
1247         p = rbuf + output_len;
1248         last = rbuf + buflen - 1;
1249
1250         switch(scsicmd[2] & 0x3f) {
1251         case 0x01:              /* r/w error recovery */
1252                 output_len += ata_msense_rw_recovery(&p, last);
1253                 break;
1254
1255         case 0x08:              /* caching */
1256                 output_len += ata_msense_caching(args->id, &p, last);
1257                 break;
1258
1259         case 0x0a: {            /* control mode */
1260                 output_len += ata_msense_ctl_mode(&p, last);
1261                 break;
1262                 }
1263
1264         case 0x3f:              /* all pages */
1265                 output_len += ata_msense_rw_recovery(&p, last);
1266                 output_len += ata_msense_caching(args->id, &p, last);
1267                 output_len += ata_msense_ctl_mode(&p, last);
1268                 break;
1269
1270         default:                /* invalid page code */
1271                 return 1;
1272         }
1273
1274         if (six_byte) {
1275                 output_len--;
1276                 rbuf[0] = output_len;
1277         } else {
1278                 output_len -= 2;
1279                 rbuf[0] = output_len >> 8;
1280                 rbuf[1] = output_len;
1281         }
1282
1283         return 0;
1284 }
1285
1286 /**
1287  *      ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
1288  *      @args: device IDENTIFY data / SCSI command of interest.
1289  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1290  *      @buflen: Response buffer length.
1291  *
1292  *      Simulate READ CAPACITY commands.
1293  *
1294  *      LOCKING:
1295  *      spin_lock_irqsave(host_set lock)
1296  */
1297
1298 unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
1299                                 unsigned int buflen)
1300 {
1301         u64 n_sectors;
1302         u32 tmp;
1303
1304         VPRINTK("ENTER\n");
1305
1306         if (ata_id_has_lba(args->id)) {
1307                 if (ata_id_has_lba48(args->id))
1308                         n_sectors = ata_id_u64(args->id, 100);
1309                 else
1310                         n_sectors = ata_id_u32(args->id, 60);
1311         } else {
1312                 /* CHS default translation */
1313                 n_sectors = args->id[1] * args->id[3] * args->id[6];
1314
1315                 if (ata_id_current_chs_valid(args->id))
1316                         /* CHS current translation */
1317                         n_sectors = ata_id_u32(args->id, 57);
1318         }
1319
1320         n_sectors--;            /* ATA TotalUserSectors - 1 */
1321
1322         if (args->cmd->cmnd[0] == READ_CAPACITY) {
1323                 if( n_sectors >= 0xffffffffULL )
1324                         tmp = 0xffffffff ;  /* Return max count on overflow */
1325                 else
1326                         tmp = n_sectors ;
1327
1328                 /* sector count, 32-bit */
1329                 rbuf[0] = tmp >> (8 * 3);
1330                 rbuf[1] = tmp >> (8 * 2);
1331                 rbuf[2] = tmp >> (8 * 1);
1332                 rbuf[3] = tmp;
1333
1334                 /* sector size */
1335                 tmp = ATA_SECT_SIZE;
1336                 rbuf[6] = tmp >> 8;
1337                 rbuf[7] = tmp;
1338
1339         } else {
1340                 /* sector count, 64-bit */
1341                 tmp = n_sectors >> (8 * 4);
1342                 rbuf[2] = tmp >> (8 * 3);
1343                 rbuf[3] = tmp >> (8 * 2);
1344                 rbuf[4] = tmp >> (8 * 1);
1345                 rbuf[5] = tmp;
1346                 tmp = n_sectors;
1347                 rbuf[6] = tmp >> (8 * 3);
1348                 rbuf[7] = tmp >> (8 * 2);
1349                 rbuf[8] = tmp >> (8 * 1);
1350                 rbuf[9] = tmp;
1351
1352                 /* sector size */
1353                 tmp = ATA_SECT_SIZE;
1354                 rbuf[12] = tmp >> 8;
1355                 rbuf[13] = tmp;
1356         }
1357
1358         return 0;
1359 }
1360
1361 /**
1362  *      ata_scsiop_report_luns - Simulate REPORT LUNS command
1363  *      @args: device IDENTIFY data / SCSI command of interest.
1364  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1365  *      @buflen: Response buffer length.
1366  *
1367  *      Simulate REPORT LUNS command.
1368  *
1369  *      LOCKING:
1370  *      spin_lock_irqsave(host_set lock)
1371  */
1372
1373 unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1374                                    unsigned int buflen)
1375 {
1376         VPRINTK("ENTER\n");
1377         rbuf[3] = 8;    /* just one lun, LUN 0, size 8 bytes */
1378
1379         return 0;
1380 }
1381
1382 /**
1383  *      ata_scsi_badcmd - End a SCSI request with an error
1384  *      @cmd: SCSI request to be handled
1385  *      @done: SCSI command completion function
1386  *      @asc: SCSI-defined additional sense code
1387  *      @ascq: SCSI-defined additional sense code qualifier
1388  *
1389  *      Helper function that completes a SCSI command with
1390  *      %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
1391  *      and the specified additional sense codes.
1392  *
1393  *      LOCKING:
1394  *      spin_lock_irqsave(host_set lock)
1395  */
1396
1397 void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
1398 {
1399         DPRINTK("ENTER\n");
1400         cmd->result = SAM_STAT_CHECK_CONDITION;
1401
1402         cmd->sense_buffer[0] = 0x70;
1403         cmd->sense_buffer[2] = ILLEGAL_REQUEST;
1404         cmd->sense_buffer[7] = 14 - 8;  /* addnl. sense len. FIXME: correct? */
1405         cmd->sense_buffer[12] = asc;
1406         cmd->sense_buffer[13] = ascq;
1407
1408         done(cmd);
1409 }
1410
1411 static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
1412 {
1413         struct scsi_cmnd *cmd = qc->scsicmd;
1414
1415         if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) {
1416                 DPRINTK("request check condition\n");
1417
1418                 cmd->result = SAM_STAT_CHECK_CONDITION;
1419
1420                 qc->scsidone(cmd);
1421
1422                 return 1;
1423         } else {
1424                 u8 *scsicmd = cmd->cmnd;
1425
1426                 if (scsicmd[0] == INQUIRY) {
1427                         u8 *buf = NULL;
1428                         unsigned int buflen;
1429
1430                         buflen = ata_scsi_rbuf_get(cmd, &buf);
1431                         buf[2] = 0x5;
1432                         buf[3] = (buf[3] & 0xf0) | 2;
1433                         ata_scsi_rbuf_put(cmd, buf);
1434                 }
1435                 cmd->result = SAM_STAT_GOOD;
1436         }
1437
1438         qc->scsidone(cmd);
1439
1440         return 0;
1441 }
1442 /**
1443  *      atapi_xlat - Initialize PACKET taskfile
1444  *      @qc: command structure to be initialized
1445  *      @scsicmd: SCSI CDB associated with this PACKET command
1446  *
1447  *      LOCKING:
1448  *      spin_lock_irqsave(host_set lock)
1449  *
1450  *      RETURNS:
1451  *      Zero on success, non-zero on failure.
1452  */
1453
1454 static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
1455 {
1456         struct scsi_cmnd *cmd = qc->scsicmd;
1457         struct ata_device *dev = qc->dev;
1458         int using_pio = (dev->flags & ATA_DFLAG_PIO);
1459         int nodata = (cmd->sc_data_direction == DMA_NONE);
1460
1461         if (!using_pio)
1462                 /* Check whether ATAPI DMA is safe */
1463                 if (ata_check_atapi_dma(qc))
1464                         using_pio = 1;
1465
1466         memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
1467
1468         qc->complete_fn = atapi_qc_complete;
1469
1470         qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1471         if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1472                 qc->tf.flags |= ATA_TFLAG_WRITE;
1473                 DPRINTK("direction: write\n");
1474         }
1475
1476         qc->tf.command = ATA_CMD_PACKET;
1477
1478         /* no data, or PIO data xfer */
1479         if (using_pio || nodata) {
1480                 if (nodata)
1481                         qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
1482                 else
1483                         qc->tf.protocol = ATA_PROT_ATAPI;
1484                 qc->tf.lbam = (8 * 1024) & 0xff;
1485                 qc->tf.lbah = (8 * 1024) >> 8;
1486         }
1487
1488         /* DMA data xfer */
1489         else {
1490                 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
1491                 qc->tf.feature |= ATAPI_PKT_DMA;
1492
1493 #ifdef ATAPI_ENABLE_DMADIR
1494                 /* some SATA bridges need us to indicate data xfer direction */
1495                 if (cmd->sc_data_direction != DMA_TO_DEVICE)
1496                         qc->tf.feature |= ATAPI_DMADIR;
1497 #endif
1498         }
1499
1500         qc->nbytes = cmd->bufflen;
1501
1502         return 0;
1503 }
1504
1505 /**
1506  *      ata_scsi_find_dev - lookup ata_device from scsi_cmnd
1507  *      @ap: ATA port to which the device is attached
1508  *      @scsidev: SCSI device from which we derive the ATA device
1509  *
1510  *      Given various information provided in struct scsi_cmnd,
1511  *      map that onto an ATA bus, and using that mapping
1512  *      determine which ata_device is associated with the
1513  *      SCSI command to be sent.
1514  *
1515  *      LOCKING:
1516  *      spin_lock_irqsave(host_set lock)
1517  *
1518  *      RETURNS:
1519  *      Associated ATA device, or %NULL if not found.
1520  */
1521
1522 static struct ata_device *
1523 ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev)
1524 {
1525         struct ata_device *dev;
1526
1527         /* skip commands not addressed to targets we simulate */
1528         if (likely(scsidev->id < ATA_MAX_DEVICES))
1529                 dev = &ap->device[scsidev->id];
1530         else
1531                 return NULL;
1532
1533         if (unlikely((scsidev->channel != 0) ||
1534                      (scsidev->lun != 0)))
1535                 return NULL;
1536
1537         if (unlikely(!ata_dev_present(dev)))
1538                 return NULL;
1539
1540         if (!atapi_enabled) {
1541                 if (unlikely(dev->class == ATA_DEV_ATAPI))
1542                         return NULL;
1543         }
1544
1545         return dev;
1546 }
1547
1548 /**
1549  *      ata_get_xlat_func - check if SCSI to ATA translation is possible
1550  *      @dev: ATA device
1551  *      @cmd: SCSI command opcode to consider
1552  *
1553  *      Look up the SCSI command given, and determine whether the
1554  *      SCSI command is to be translated or simulated.
1555  *
1556  *      RETURNS:
1557  *      Pointer to translation function if possible, %NULL if not.
1558  */
1559
1560 static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
1561 {
1562         switch (cmd) {
1563         case READ_6:
1564         case READ_10:
1565         case READ_16:
1566
1567         case WRITE_6:
1568         case WRITE_10:
1569         case WRITE_16:
1570                 return ata_scsi_rw_xlat;
1571
1572         case SYNCHRONIZE_CACHE:
1573                 if (ata_try_flush_cache(dev))
1574                         return ata_scsi_flush_xlat;
1575                 break;
1576
1577         case VERIFY:
1578         case VERIFY_16:
1579                 return ata_scsi_verify_xlat;
1580         case START_STOP:
1581                 return ata_scsi_start_stop_xlat;
1582         }
1583
1584         return NULL;
1585 }
1586
1587 /**
1588  *      ata_scsi_dump_cdb - dump SCSI command contents to dmesg
1589  *      @ap: ATA port to which the command was being sent
1590  *      @cmd: SCSI command to dump
1591  *
1592  *      Prints the contents of a SCSI command via printk().
1593  */
1594
1595 static inline void ata_scsi_dump_cdb(struct ata_port *ap,
1596                                      struct scsi_cmnd *cmd)
1597 {
1598 #ifdef ATA_DEBUG
1599         struct scsi_device *scsidev = cmd->device;
1600         u8 *scsicmd = cmd->cmnd;
1601
1602         DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
1603                 ap->id,
1604                 scsidev->channel, scsidev->id, scsidev->lun,
1605                 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
1606                 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
1607                 scsicmd[8]);
1608 #endif
1609 }
1610
1611 /**
1612  *      ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
1613  *      @cmd: SCSI command to be sent
1614  *      @done: Completion function, called when command is complete
1615  *
1616  *      In some cases, this function translates SCSI commands into
1617  *      ATA taskfiles, and queues the taskfiles to be sent to
1618  *      hardware.  In other cases, this function simulates a
1619  *      SCSI device by evaluating and responding to certain
1620  *      SCSI commands.  This creates the overall effect of
1621  *      ATA and ATAPI devices appearing as SCSI devices.
1622  *
1623  *      LOCKING:
1624  *      Releases scsi-layer-held lock, and obtains host_set lock.
1625  *
1626  *      RETURNS:
1627  *      Zero.
1628  */
1629
1630 int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
1631 {
1632         struct ata_port *ap;
1633         struct ata_device *dev;
1634         struct scsi_device *scsidev = cmd->device;
1635
1636         ap = (struct ata_port *) &scsidev->host->hostdata[0];
1637
1638         ata_scsi_dump_cdb(ap, cmd);
1639
1640         dev = ata_scsi_find_dev(ap, scsidev);
1641         if (unlikely(!dev)) {
1642                 cmd->result = (DID_BAD_TARGET << 16);
1643                 done(cmd);
1644                 goto out_unlock;
1645         }
1646
1647         if (dev->class == ATA_DEV_ATA) {
1648                 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
1649                                                               cmd->cmnd[0]);
1650
1651                 if (xlat_func)
1652                         ata_scsi_translate(ap, dev, cmd, done, xlat_func);
1653                 else
1654                         ata_scsi_simulate(dev->id, cmd, done);
1655         } else
1656                 ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
1657
1658 out_unlock:
1659         return 0;
1660 }
1661
1662 /**
1663  *      ata_scsi_simulate - simulate SCSI command on ATA device
1664  *      @id: current IDENTIFY data for target device.
1665  *      @cmd: SCSI command being sent to device.
1666  *      @done: SCSI command completion function.
1667  *
1668  *      Interprets and directly executes a select list of SCSI commands
1669  *      that can be handled internally.
1670  *
1671  *      LOCKING:
1672  *      spin_lock_irqsave(host_set lock)
1673  */
1674
1675 void ata_scsi_simulate(u16 *id,
1676                       struct scsi_cmnd *cmd,
1677                       void (*done)(struct scsi_cmnd *))
1678 {
1679         struct ata_scsi_args args;
1680         u8 *scsicmd = cmd->cmnd;
1681
1682         args.id = id;
1683         args.cmd = cmd;
1684         args.done = done;
1685
1686         switch(scsicmd[0]) {
1687                 /* no-op's, complete with success */
1688                 case SYNCHRONIZE_CACHE:
1689                 case REZERO_UNIT:
1690                 case SEEK_6:
1691                 case SEEK_10:
1692                 case TEST_UNIT_READY:
1693                 case FORMAT_UNIT:               /* FIXME: correct? */
1694                 case SEND_DIAGNOSTIC:           /* FIXME: correct? */
1695                         ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
1696                         break;
1697
1698                 case INQUIRY:
1699                         if (scsicmd[1] & 2)                /* is CmdDt set?  */
1700                                 ata_bad_cdb(cmd, done);
1701                         else if ((scsicmd[1] & 1) == 0)    /* is EVPD clear? */
1702                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
1703                         else if (scsicmd[2] == 0x00)
1704                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
1705                         else if (scsicmd[2] == 0x80)
1706                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
1707                         else if (scsicmd[2] == 0x83)
1708                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
1709                         else
1710                                 ata_bad_cdb(cmd, done);
1711                         break;
1712
1713                 case MODE_SENSE:
1714                 case MODE_SENSE_10:
1715                         ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
1716                         break;
1717
1718                 case MODE_SELECT:       /* unconditionally return */
1719                 case MODE_SELECT_10:    /* bad-field-in-cdb */
1720                         ata_bad_cdb(cmd, done);
1721                         break;
1722
1723                 case READ_CAPACITY:
1724                         ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1725                         break;
1726
1727                 case SERVICE_ACTION_IN:
1728                         if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
1729                                 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1730                         else
1731                                 ata_bad_cdb(cmd, done);
1732                         break;
1733
1734                 case REPORT_LUNS:
1735                         ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
1736                         break;
1737
1738                 /* mandantory commands we haven't implemented yet */
1739                 case REQUEST_SENSE:
1740
1741                 /* all other commands */
1742                 default:
1743                         ata_bad_scsiop(cmd, done);
1744                         break;
1745         }
1746 }
1747