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