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