OSDN Git Service

Merge /spare/repo/linux-2.6/
[sagit-ice-cold/kernel_xiaomi_msm8998.git] / drivers / scsi / libata-scsi.c
1 /*
2  *  libata-scsi.c - helper library for ATA
3  *
4  *  Maintained by:  Jeff Garzik <jgarzik@pobox.com>
5  *                  Please ALWAYS copy linux-ide@vger.kernel.org
6  *                  on emails.
7  *
8  *  Copyright 2003-2004 Red Hat, Inc.  All rights reserved.
9  *  Copyright 2003-2004 Jeff Garzik
10  *
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2, or (at your option)
15  *  any later version.
16  *
17  *  This program is distributed in the hope that it will be useful,
18  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *  GNU General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License
23  *  along with this program; see the file COPYING.  If not, write to
24  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25  *
26  *
27  *  libata documentation is available via 'make {ps|pdf}docs',
28  *  as Documentation/DocBook/libata.*
29  *
30  *  Hardware documentation available from
31  *  - http://www.t10.org/
32  *  - http://www.t13.org/
33  *
34  */
35
36 #include <linux/kernel.h>
37 #include <linux/blkdev.h>
38 #include <linux/spinlock.h>
39 #include <scsi/scsi.h>
40 #include "scsi.h"
41 #include <scsi/scsi_host.h>
42 #include <linux/libata.h>
43 #include <linux/hdreg.h>
44 #include <asm/uaccess.h>
45
46 #include "libata.h"
47
48 #define SECTOR_SIZE     512
49
50 typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, u8 *scsicmd);
51 static struct ata_device *
52 ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev);
53
54
55 /**
56  *      ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
57  *      @sdev: SCSI device for which BIOS geometry is to be determined
58  *      @bdev: block device associated with @sdev
59  *      @capacity: capacity of SCSI device
60  *      @geom: location to which geometry will be output
61  *
62  *      Generic bios head/sector/cylinder calculator
63  *      used by sd. Most BIOSes nowadays expect a XXX/255/16  (CHS)
64  *      mapping. Some situations may arise where the disk is not
65  *      bootable if this is not used.
66  *
67  *      LOCKING:
68  *      Defined by the SCSI layer.  We don't really care.
69  *
70  *      RETURNS:
71  *      Zero.
72  */
73 int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
74                        sector_t capacity, int geom[])
75 {
76         geom[0] = 255;
77         geom[1] = 63;
78         sector_div(capacity, 255*63);
79         geom[2] = capacity;
80
81         return 0;
82 }
83
84 /**
85  *      ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
86  *      @dev: Device to whom we are issuing command
87  *      @arg: User provided data for issuing command
88  *
89  *      LOCKING:
90  *      Defined by the SCSI layer.  We don't really care.
91  *
92  *      RETURNS:
93  *      Zero on success, negative errno on error.
94  */
95
96 int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
97 {
98         int rc = 0;
99         u8 scsi_cmd[MAX_COMMAND_SIZE];
100         u8 args[4], *argbuf = NULL;
101         int argsize = 0;
102         struct scsi_request *sreq;
103
104         if (NULL == (void *)arg)
105                 return -EINVAL;
106
107         if (copy_from_user(args, arg, sizeof(args)))
108                 return -EFAULT;
109
110         sreq = scsi_allocate_request(scsidev, GFP_KERNEL);
111         if (!sreq)
112                 return -EINTR;
113
114         memset(scsi_cmd, 0, sizeof(scsi_cmd));
115
116         if (args[3]) {
117                 argsize = SECTOR_SIZE * args[3];
118                 argbuf = kmalloc(argsize, GFP_KERNEL);
119                 if (argbuf == NULL)
120                         return -ENOMEM;
121
122                 scsi_cmd[1]  = (4 << 1); /* PIO Data-in */
123                 scsi_cmd[2]  = 0x0e;     /* no off.line or cc, read from dev,
124                                             block count in sector count field */
125                 sreq->sr_data_direction = DMA_FROM_DEVICE;
126         } else {
127                 scsi_cmd[1]  = (3 << 1); /* Non-data */
128                 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
129                 sreq->sr_data_direction = DMA_NONE;
130         }
131
132         scsi_cmd[0] = ATA_16;
133
134         scsi_cmd[4] = args[2];
135         if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
136                 scsi_cmd[6]  = args[3];
137                 scsi_cmd[8]  = args[1];
138                 scsi_cmd[10] = 0x4f;
139                 scsi_cmd[12] = 0xc2;
140         } else {
141                 scsi_cmd[6]  = args[1];
142         }
143         scsi_cmd[14] = args[0];
144
145         /* Good values for timeout and retries?  Values below
146            from scsi_ioctl_send_command() for default case... */
147         scsi_wait_req(sreq, scsi_cmd, argbuf, argsize, (10*HZ), 5);
148
149         if (sreq->sr_result) {
150                 rc = -EIO;
151                 goto error;
152         }
153
154         /* Need code to retrieve data from check condition? */
155
156         if ((argbuf)
157          && copy_to_user((void *)(arg + sizeof(args)), argbuf, argsize))
158                 rc = -EFAULT;
159 error:
160         scsi_release_request(sreq);
161
162         if (argbuf)
163                 kfree(argbuf);
164
165         return rc;
166 }
167
168 /**
169  *      ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
170  *      @dev: Device to whom we are issuing command
171  *      @arg: User provided data for issuing command
172  *
173  *      LOCKING:
174  *      Defined by the SCSI layer.  We don't really care.
175  *
176  *      RETURNS:
177  *      Zero on success, negative errno on error.
178  */
179 int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
180 {
181         int rc = 0;
182         u8 scsi_cmd[MAX_COMMAND_SIZE];
183         u8 args[7];
184         struct scsi_request *sreq;
185
186         if (NULL == (void *)arg)
187                 return -EINVAL;
188
189         if (copy_from_user(args, arg, sizeof(args)))
190                 return -EFAULT;
191
192         memset(scsi_cmd, 0, sizeof(scsi_cmd));
193         scsi_cmd[0]  = ATA_16;
194         scsi_cmd[1]  = (3 << 1); /* Non-data */
195         /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
196         scsi_cmd[4]  = args[1];
197         scsi_cmd[6]  = args[2];
198         scsi_cmd[8]  = args[3];
199         scsi_cmd[10] = args[4];
200         scsi_cmd[12] = args[5];
201         scsi_cmd[14] = args[0];
202
203         sreq = scsi_allocate_request(scsidev, GFP_KERNEL);
204         if (!sreq) {
205                 rc = -EINTR;
206                 goto error;
207         }
208
209         sreq->sr_data_direction = DMA_NONE;
210         /* Good values for timeout and retries?  Values below
211            from scsi_ioctl_send_command() for default case... */
212         scsi_wait_req(sreq, scsi_cmd, NULL, 0, (10*HZ), 5);
213
214         if (sreq->sr_result) {
215                 rc = -EIO;
216                 goto error;
217         }
218
219         /* Need code to retrieve data from check condition? */
220
221 error:
222         scsi_release_request(sreq);
223         return rc;
224 }
225
226 int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
227 {
228         struct ata_port *ap;
229         struct ata_device *dev;
230         int val = -EINVAL, rc = -EINVAL;
231
232         ap = (struct ata_port *) &scsidev->host->hostdata[0];
233         if (!ap)
234                 goto out;
235
236         dev = ata_scsi_find_dev(ap, scsidev);
237         if (!dev) {
238                 rc = -ENODEV;
239                 goto out;
240         }
241
242         switch (cmd) {
243         case ATA_IOC_GET_IO32:
244                 val = 0;
245                 if (copy_to_user(arg, &val, 1))
246                         return -EFAULT;
247                 return 0;
248
249         case ATA_IOC_SET_IO32:
250                 val = (unsigned long) arg;
251                 if (val != 0)
252                         return -EINVAL;
253                 return 0;
254
255         case HDIO_DRIVE_CMD:
256                 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
257                         return -EACCES;
258                 return ata_cmd_ioctl(scsidev, arg);
259
260         case HDIO_DRIVE_TASK:
261                 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
262                         return -EACCES;
263                 return ata_task_ioctl(scsidev, arg);
264
265         default:
266                 rc = -ENOTTY;
267                 break;
268         }
269
270 out:
271         return rc;
272 }
273
274 /**
275  *      ata_scsi_qc_new - acquire new ata_queued_cmd reference
276  *      @ap: ATA port to which the new command is attached
277  *      @dev: ATA device to which the new command is attached
278  *      @cmd: SCSI command that originated this ATA command
279  *      @done: SCSI command completion function
280  *
281  *      Obtain a reference to an unused ata_queued_cmd structure,
282  *      which is the basic libata structure representing a single
283  *      ATA command sent to the hardware.
284  *
285  *      If a command was available, fill in the SCSI-specific
286  *      portions of the structure with information on the
287  *      current command.
288  *
289  *      LOCKING:
290  *      spin_lock_irqsave(host_set lock)
291  *
292  *      RETURNS:
293  *      Command allocated, or %NULL if none available.
294  */
295 struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
296                                        struct ata_device *dev,
297                                        struct scsi_cmnd *cmd,
298                                        void (*done)(struct scsi_cmnd *))
299 {
300         struct ata_queued_cmd *qc;
301
302         qc = ata_qc_new_init(ap, dev);
303         if (qc) {
304                 qc->scsicmd = cmd;
305                 qc->scsidone = done;
306
307                 if (cmd->use_sg) {
308                         qc->sg = (struct scatterlist *) cmd->request_buffer;
309                         qc->n_elem = cmd->use_sg;
310                 } else {
311                         qc->sg = &qc->sgent;
312                         qc->n_elem = 1;
313                 }
314         } else {
315                 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
316                 done(cmd);
317         }
318
319         return qc;
320 }
321
322 /**
323  *      ata_dump_status - user friendly display of error info
324  *      @id: id of the port in question
325  *      @tf: ptr to filled out taskfile
326  *
327  *      Decode and dump the ATA error/status registers for the user so
328  *      that they have some idea what really happened at the non
329  *      make-believe layer.
330  *
331  *      LOCKING:
332  *      inherited from caller
333  */
334 void ata_dump_status(unsigned id, struct ata_taskfile *tf)
335 {
336         u8 stat = tf->command, err = tf->feature;
337
338         printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
339         if (stat & ATA_BUSY) {
340                 printk("Busy }\n");     /* Data is not valid in this case */
341         } else {
342                 if (stat & 0x40)        printk("DriveReady ");
343                 if (stat & 0x20)        printk("DeviceFault ");
344                 if (stat & 0x10)        printk("SeekComplete ");
345                 if (stat & 0x08)        printk("DataRequest ");
346                 if (stat & 0x04)        printk("CorrectedError ");
347                 if (stat & 0x02)        printk("Index ");
348                 if (stat & 0x01)        printk("Error ");
349                 printk("}\n");
350
351                 if (err) {
352                         printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
353                         if (err & 0x04)         printk("DriveStatusError ");
354                         if (err & 0x80) {
355                                 if (err & 0x04) printk("BadCRC ");
356                                 else            printk("Sector ");
357                         }
358                         if (err & 0x40)         printk("UncorrectableError ");
359                         if (err & 0x10)         printk("SectorIdNotFound ");
360                         if (err & 0x02)         printk("TrackZeroNotFound ");
361                         if (err & 0x01)         printk("AddrMarkNotFound ");
362                         printk("}\n");
363                 }
364         }
365 }
366
367 /**
368  *      ata_to_sense_error - convert ATA error to SCSI error
369  *      @drv_stat: value contained in ATA status register
370  *      @drv_err: value contained in ATA error register
371  *      @sk: the sense key we'll fill out
372  *      @asc: the additional sense code we'll fill out
373  *      @ascq: the additional sense code qualifier we'll fill out
374  *
375  *      Converts an ATA error into a SCSI error.  Fill out pointers to
376  *      SK, ASC, and ASCQ bytes for later use in fixed or descriptor
377  *      format sense blocks.
378  *
379  *      LOCKING:
380  *      spin_lock_irqsave(host_set lock)
381  */
382 void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc, 
383                         u8 *ascq)
384 {
385         int i;
386         /* Based on the 3ware driver translation table */
387         static unsigned char sense_table[][4] = {
388                 /* BBD|ECC|ID|MAR */
389                 {0xd1,          ABORTED_COMMAND, 0x00, 0x00},   // Device busy                  Aborted command
390                 /* BBD|ECC|ID */
391                 {0xd0,          ABORTED_COMMAND, 0x00, 0x00},   // Device busy                  Aborted command
392                 /* ECC|MC|MARK */
393                 {0x61,          HARDWARE_ERROR, 0x00, 0x00},    // Device fault                 Hardware error
394                 /* ICRC|ABRT */         /* NB: ICRC & !ABRT is BBD */
395                 {0x84,          ABORTED_COMMAND, 0x47, 0x00},   // Data CRC error               SCSI parity error
396                 /* MC|ID|ABRT|TRK0|MARK */
397                 {0x37,          NOT_READY, 0x04, 0x00},         // Unit offline                 Not ready
398                 /* MCR|MARK */
399                 {0x09,          NOT_READY, 0x04, 0x00},         // Unrecovered disk error       Not ready
400                 /*  Bad address mark */
401                 {0x01,          MEDIUM_ERROR, 0x13, 0x00},      // Address mark not found       Address mark not found for data field
402                 /* TRK0 */
403                 {0x02,          HARDWARE_ERROR, 0x00, 0x00},    // Track 0 not found              Hardware error
404                 /* Abort & !ICRC */
405                 {0x04,          ABORTED_COMMAND, 0x00, 0x00},   // Aborted command              Aborted command
406                 /* Media change request */
407                 {0x08,          NOT_READY, 0x04, 0x00},         // Media change request   FIXME: faking offline
408                 /* SRV */
409                 {0x10,          ABORTED_COMMAND, 0x14, 0x00},   // ID not found                 Recorded entity not found
410                 /* Media change */
411                 {0x08,          NOT_READY, 0x04, 0x00},         // Media change           FIXME: faking offline
412                 /* ECC */
413                 {0x40,          MEDIUM_ERROR, 0x11, 0x04},      // Uncorrectable ECC error      Unrecovered read error
414                 /* BBD - block marked bad */
415                 {0x80,          MEDIUM_ERROR, 0x11, 0x04},      // Block marked bad               Medium error, unrecovered read error
416                 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
417         };
418         static unsigned char stat_table[][4] = {
419                 /* Must be first because BUSY means no other bits valid */
420                 {0x80,          ABORTED_COMMAND, 0x47, 0x00},   // Busy, fake parity for now
421                 {0x20,          HARDWARE_ERROR,  0x00, 0x00},   // Device fault
422                 {0x08,          ABORTED_COMMAND, 0x47, 0x00},   // Timed out in xfer, fake parity for now
423                 {0x04,          RECOVERED_ERROR, 0x11, 0x00},   // Recovered ECC error    Medium error, recovered
424                 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
425         };
426
427         /*
428          *      Is this an error we can process/parse
429          */
430         if (drv_stat & ATA_BUSY) {
431                 drv_err = 0;    /* Ignore the err bits, they're invalid */
432         }
433
434         if (drv_err) {
435                 /* Look for drv_err */
436                 for (i = 0; sense_table[i][0] != 0xFF; i++) {
437                         /* Look for best matches first */
438                         if ((sense_table[i][0] & drv_err) == 
439                             sense_table[i][0]) {
440                                 *sk = sense_table[i][1];
441                                 *asc = sense_table[i][2];
442                                 *ascq = sense_table[i][3];
443                                 goto translate_done;
444                         }
445                 }
446                 /* No immediate match */
447                 printk(KERN_WARNING "ata%u: no sense translation for "
448                        "error 0x%02x\n", id, drv_err);
449         }
450
451         /* Fall back to interpreting status bits */
452         for (i = 0; stat_table[i][0] != 0xFF; i++) {
453                 if (stat_table[i][0] & drv_stat) {
454                         *sk = stat_table[i][1];
455                         *asc = stat_table[i][2];
456                         *ascq = stat_table[i][3];
457                         goto translate_done;
458                 }
459         }
460         /* No error?  Undecoded? */
461         printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n", 
462                id, drv_stat);
463
464         /* For our last chance pick, use medium read error because
465          * it's much more common than an ATA drive telling you a write
466          * has failed.
467          */
468         *sk = MEDIUM_ERROR;
469         *asc = 0x11; /* "unrecovered read error" */
470         *ascq = 0x04; /*  "auto-reallocation failed" */
471
472  translate_done:
473         printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to "
474                "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err,
475                *sk, *asc, *ascq);
476         return;
477 }
478
479 /*
480  *      ata_gen_ata_desc_sense - Generate check condition sense block.
481  *      @qc: Command that completed.
482  *
483  *      This function is specific to the ATA descriptor format sense
484  *      block specified for the ATA pass through commands.  Regardless
485  *      of whether the command errored or not, return a sense
486  *      block. Copy all controller registers into the sense
487  *      block. Clear sense key, ASC & ASCQ if there is no error.
488  *
489  *      LOCKING:
490  *      spin_lock_irqsave(host_set lock)
491  */
492 void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
493 {
494         struct scsi_cmnd *cmd = qc->scsicmd;
495         struct ata_taskfile *tf = &qc->tf;
496         unsigned char *sb = cmd->sense_buffer;
497         unsigned char *desc = sb + 8;
498
499         memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
500
501         cmd->result = SAM_STAT_CHECK_CONDITION;
502
503         /*
504          * Read the controller registers.
505          */
506         assert(NULL != qc->ap->ops->tf_read);
507         qc->ap->ops->tf_read(qc->ap, tf);
508
509         /*
510          * Use ata_to_sense_error() to map status register bits
511          * onto sense key, asc & ascq.
512          */
513         if (unlikely(tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ))) {
514                 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
515                                    &sb[1], &sb[2], &sb[3]);
516                 sb[1] &= 0x0f;
517         }
518
519         /*
520          * Sense data is current and format is descriptor.
521          */
522         sb[0] = 0x72;
523
524         desc[0] = 0x09;
525
526         /*
527          * Set length of additional sense data.
528          * Since we only populate descriptor 0, the total
529          * length is the same (fixed) length as descriptor 0.
530          */
531         desc[1] = sb[7] = 14;
532
533         /*
534          * Copy registers into sense buffer.
535          */
536         desc[2] = 0x00;
537         desc[3] = tf->feature;  /* == error reg */
538         desc[5] = tf->nsect;
539         desc[7] = tf->lbal;
540         desc[9] = tf->lbam;
541         desc[11] = tf->lbah;
542         desc[12] = tf->device;
543         desc[13] = tf->command; /* == status reg */
544
545         /*
546          * Fill in Extend bit, and the high order bytes
547          * if applicable.
548          */
549         if (tf->flags & ATA_TFLAG_LBA48) {
550                 desc[2] |= 0x01;
551                 desc[4] = tf->hob_nsect;
552                 desc[6] = tf->hob_lbal;
553                 desc[8] = tf->hob_lbam;
554                 desc[10] = tf->hob_lbah;
555         }
556 }
557
558 /**
559  *      ata_gen_fixed_sense - generate a SCSI fixed sense block
560  *      @qc: Command that we are erroring out
561  *
562  *      Leverage ata_to_sense_error() to give us the codes.  Fit our
563  *      LBA in here if there's room.
564  *
565  *      LOCKING:
566  *      inherited from caller
567  */
568 void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
569 {
570         struct scsi_cmnd *cmd = qc->scsicmd;
571         struct ata_taskfile *tf = &qc->tf;
572         unsigned char *sb = cmd->sense_buffer;
573
574         memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
575
576         cmd->result = SAM_STAT_CHECK_CONDITION;
577
578         /*
579          * Read the controller registers.
580          */
581         assert(NULL != qc->ap->ops->tf_read);
582         qc->ap->ops->tf_read(qc->ap, tf);
583
584         /*
585          * Use ata_to_sense_error() to map status register bits
586          * onto sense key, asc & ascq.
587          */
588         if (unlikely(tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ))) {
589                 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
590                                    &sb[2], &sb[12], &sb[13]);
591                 sb[2] &= 0x0f;
592         }
593
594         sb[0] = 0x70;
595         sb[7] = 0x0a;
596
597 #if 0 /* when C/H/S support is merged */
598         if (tf->flags & ATA_TFLAG_LBA && !(tf->flags & ATA_TFLAG_LBA48)) {
599 #endif
600         if (!(tf->flags & ATA_TFLAG_LBA48)) {
601                 /* A small (28b) LBA will fit in the 32b info field */
602                 sb[0] |= 0x80;          /* set valid bit */
603                 sb[3] = tf->device & 0x0f;
604                 sb[4] = tf->lbah;
605                 sb[5] = tf->lbam;
606                 sb[6] = tf->lbal;
607         }
608 }
609
610 /**
611  *      ata_scsi_slave_config - Set SCSI device attributes
612  *      @sdev: SCSI device to examine
613  *
614  *      This is called before we actually start reading
615  *      and writing to the device, to configure certain
616  *      SCSI mid-layer behaviors.
617  *
618  *      LOCKING:
619  *      Defined by SCSI layer.  We don't really care.
620  */
621
622 int ata_scsi_slave_config(struct scsi_device *sdev)
623 {
624         sdev->use_10_for_rw = 1;
625         sdev->use_10_for_ms = 1;
626
627         blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
628
629         if (sdev->id < ATA_MAX_DEVICES) {
630                 struct ata_port *ap;
631                 struct ata_device *dev;
632
633                 ap = (struct ata_port *) &sdev->host->hostdata[0];
634                 dev = &ap->device[sdev->id];
635
636                 /* TODO: 1024 is an arbitrary number, not the
637                  * hardware maximum.  This should be increased to
638                  * 65534 when Jens Axboe's patch for dynamically
639                  * determining max_sectors is merged.
640                  */
641                 if ((dev->flags & ATA_DFLAG_LBA48) &&
642                     ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) {
643                         /*
644                          * do not overwrite sdev->host->max_sectors, since
645                          * other drives on this host may not support LBA48
646                          */
647                         blk_queue_max_sectors(sdev->request_queue, 2048);
648                 }
649         }
650
651         return 0;       /* scsi layer doesn't check return value, sigh */
652 }
653
654 /**
655  *      ata_scsi_error - SCSI layer error handler callback
656  *      @host: SCSI host on which error occurred
657  *
658  *      Handles SCSI-layer-thrown error events.
659  *
660  *      LOCKING:
661  *      Inherited from SCSI layer (none, can sleep)
662  *
663  *      RETURNS:
664  *      Zero.
665  */
666
667 int ata_scsi_error(struct Scsi_Host *host)
668 {
669         struct ata_port *ap;
670
671         DPRINTK("ENTER\n");
672
673         ap = (struct ata_port *) &host->hostdata[0];
674         ap->ops->eng_timeout(ap);
675
676         /* TODO: this is per-command; when queueing is supported
677          * this code will either change or move to a more
678          * appropriate place
679          */
680         host->host_failed--;
681         INIT_LIST_HEAD(&host->eh_cmd_q);
682
683         DPRINTK("EXIT\n");
684         return 0;
685 }
686
687 /**
688  *      ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
689  *      @qc: Storage for translated ATA taskfile
690  *      @scsicmd: SCSI command to translate
691  *
692  *      Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
693  *      (to start). Perhaps these commands should be preceded by
694  *      CHECK POWER MODE to see what power mode the device is already in.
695  *      [See SAT revision 5 at www.t10.org]
696  *
697  *      LOCKING:
698  *      spin_lock_irqsave(host_set lock)
699  *
700  *      RETURNS:
701  *      Zero on success, non-zero on error.
702  */
703
704 static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
705                                              u8 *scsicmd)
706 {
707         struct ata_taskfile *tf = &qc->tf;
708
709         tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
710         tf->protocol = ATA_PROT_NODATA;
711         if (scsicmd[1] & 0x1) {
712                 ;       /* ignore IMMED bit, violates sat-r05 */
713         }
714         if (scsicmd[4] & 0x2)
715                 return 1;       /* LOEJ bit set not supported */
716         if (((scsicmd[4] >> 4) & 0xf) != 0)
717                 return 1;       /* power conditions not supported */
718         if (scsicmd[4] & 0x1) {
719                 tf->nsect = 1;  /* 1 sector, lba=0 */
720                 tf->lbah = 0x0;
721                 tf->lbam = 0x0;
722                 tf->lbal = 0x0;
723                 tf->device |= ATA_LBA;
724                 tf->command = ATA_CMD_VERIFY;   /* READ VERIFY */
725         } else {
726                 tf->nsect = 0;  /* time period value (0 implies now) */
727                 tf->command = ATA_CMD_STANDBY;
728                 /* Consider: ATA STANDBY IMMEDIATE command */
729         }
730         /*
731          * Standby and Idle condition timers could be implemented but that
732          * would require libata to implement the Power condition mode page
733          * and allow the user to change it. Changing mode pages requires
734          * MODE SELECT to be implemented.
735          */
736
737         return 0;
738 }
739
740
741 /**
742  *      ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
743  *      @qc: Storage for translated ATA taskfile
744  *      @scsicmd: SCSI command to translate (ignored)
745  *
746  *      Sets up an ATA taskfile to issue FLUSH CACHE or
747  *      FLUSH CACHE EXT.
748  *
749  *      LOCKING:
750  *      spin_lock_irqsave(host_set lock)
751  *
752  *      RETURNS:
753  *      Zero on success, non-zero on error.
754  */
755
756 static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
757 {
758         struct ata_taskfile *tf = &qc->tf;
759
760         tf->flags |= ATA_TFLAG_DEVICE;
761         tf->protocol = ATA_PROT_NODATA;
762
763         if ((tf->flags & ATA_TFLAG_LBA48) &&
764             (ata_id_has_flush_ext(qc->dev->id)))
765                 tf->command = ATA_CMD_FLUSH_EXT;
766         else
767                 tf->command = ATA_CMD_FLUSH;
768
769         return 0;
770 }
771
772 /**
773  *      ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
774  *      @qc: Storage for translated ATA taskfile
775  *      @scsicmd: SCSI command to translate
776  *
777  *      Converts SCSI VERIFY command to an ATA READ VERIFY command.
778  *
779  *      LOCKING:
780  *      spin_lock_irqsave(host_set lock)
781  *
782  *      RETURNS:
783  *      Zero on success, non-zero on error.
784  */
785
786 static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
787 {
788         struct ata_taskfile *tf = &qc->tf;
789         unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
790         u64 dev_sectors = qc->dev->n_sectors;
791         u64 sect = 0;
792         u32 n_sect = 0;
793
794         tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
795         tf->protocol = ATA_PROT_NODATA;
796         tf->device |= ATA_LBA;
797
798         if (scsicmd[0] == VERIFY) {
799                 sect |= ((u64)scsicmd[2]) << 24;
800                 sect |= ((u64)scsicmd[3]) << 16;
801                 sect |= ((u64)scsicmd[4]) << 8;
802                 sect |= ((u64)scsicmd[5]);
803
804                 n_sect |= ((u32)scsicmd[7]) << 8;
805                 n_sect |= ((u32)scsicmd[8]);
806         }
807
808         else if (scsicmd[0] == VERIFY_16) {
809                 sect |= ((u64)scsicmd[2]) << 56;
810                 sect |= ((u64)scsicmd[3]) << 48;
811                 sect |= ((u64)scsicmd[4]) << 40;
812                 sect |= ((u64)scsicmd[5]) << 32;
813                 sect |= ((u64)scsicmd[6]) << 24;
814                 sect |= ((u64)scsicmd[7]) << 16;
815                 sect |= ((u64)scsicmd[8]) << 8;
816                 sect |= ((u64)scsicmd[9]);
817
818                 n_sect |= ((u32)scsicmd[10]) << 24;
819                 n_sect |= ((u32)scsicmd[11]) << 16;
820                 n_sect |= ((u32)scsicmd[12]) << 8;
821                 n_sect |= ((u32)scsicmd[13]);
822         }
823
824         else
825                 return 1;
826
827         if (!n_sect)
828                 return 1;
829         if (sect >= dev_sectors)
830                 return 1;
831         if ((sect + n_sect) > dev_sectors)
832                 return 1;
833         if (lba48) {
834                 if (n_sect > (64 * 1024))
835                         return 1;
836         } else {
837                 if (n_sect > 256)
838                         return 1;
839         }
840
841         if (lba48) {
842                 tf->command = ATA_CMD_VERIFY_EXT;
843
844                 tf->hob_nsect = (n_sect >> 8) & 0xff;
845
846                 tf->hob_lbah = (sect >> 40) & 0xff;
847                 tf->hob_lbam = (sect >> 32) & 0xff;
848                 tf->hob_lbal = (sect >> 24) & 0xff;
849         } else {
850                 tf->command = ATA_CMD_VERIFY;
851
852                 tf->device |= (sect >> 24) & 0xf;
853         }
854
855         tf->nsect = n_sect & 0xff;
856
857         tf->lbah = (sect >> 16) & 0xff;
858         tf->lbam = (sect >> 8) & 0xff;
859         tf->lbal = sect & 0xff;
860
861         return 0;
862 }
863
864 /**
865  *      ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
866  *      @qc: Storage for translated ATA taskfile
867  *      @scsicmd: SCSI command to translate
868  *
869  *      Converts any of six SCSI read/write commands into the
870  *      ATA counterpart, including starting sector (LBA),
871  *      sector count, and taking into account the device's LBA48
872  *      support.
873  *
874  *      Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
875  *      %WRITE_16 are currently supported.
876  *
877  *      LOCKING:
878  *      spin_lock_irqsave(host_set lock)
879  *
880  *      RETURNS:
881  *      Zero on success, non-zero on error.
882  */
883
884 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
885 {
886         struct ata_taskfile *tf = &qc->tf;
887         unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
888
889         tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
890         tf->protocol = qc->dev->xfer_protocol;
891         tf->device |= ATA_LBA;
892
893         if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
894             scsicmd[0] == READ_16) {
895                 tf->command = qc->dev->read_cmd;
896         } else {
897                 tf->command = qc->dev->write_cmd;
898                 tf->flags |= ATA_TFLAG_WRITE;
899         }
900
901         if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) {
902                 if (lba48) {
903                         tf->hob_nsect = scsicmd[7];
904                         tf->hob_lbal = scsicmd[2];
905
906                         qc->nsect = ((unsigned int)scsicmd[7] << 8) |
907                                         scsicmd[8];
908                 } else {
909                         /* if we don't support LBA48 addressing, the request
910                          * -may- be too large. */
911                         if ((scsicmd[2] & 0xf0) || scsicmd[7])
912                                 return 1;
913
914                         /* stores LBA27:24 in lower 4 bits of device reg */
915                         tf->device |= scsicmd[2];
916
917                         qc->nsect = scsicmd[8];
918                 }
919
920                 tf->nsect = scsicmd[8];
921                 tf->lbal = scsicmd[5];
922                 tf->lbam = scsicmd[4];
923                 tf->lbah = scsicmd[3];
924
925                 VPRINTK("ten-byte command\n");
926                 if (qc->nsect == 0) /* we don't support length==0 cmds */
927                         return 1;
928                 return 0;
929         }
930
931         if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) {
932                 qc->nsect = tf->nsect = scsicmd[4];
933                 if (!qc->nsect) {
934                         qc->nsect = 256;
935                         if (lba48)
936                                 tf->hob_nsect = 1;
937                 }
938
939                 tf->lbal = scsicmd[3];
940                 tf->lbam = scsicmd[2];
941                 tf->lbah = scsicmd[1] & 0x1f; /* mask out reserved bits */
942
943                 VPRINTK("six-byte command\n");
944                 return 0;
945         }
946
947         if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) {
948                 /* rule out impossible LBAs and sector counts */
949                 if (scsicmd[2] || scsicmd[3] || scsicmd[10] || scsicmd[11])
950                         return 1;
951
952                 if (lba48) {
953                         tf->hob_nsect = scsicmd[12];
954                         tf->hob_lbal = scsicmd[6];
955                         tf->hob_lbam = scsicmd[5];
956                         tf->hob_lbah = scsicmd[4];
957
958                         qc->nsect = ((unsigned int)scsicmd[12] << 8) |
959                                         scsicmd[13];
960                 } else {
961                         /* once again, filter out impossible non-zero values */
962                         if (scsicmd[4] || scsicmd[5] || scsicmd[12] ||
963                             (scsicmd[6] & 0xf0))
964                                 return 1;
965
966                         /* stores LBA27:24 in lower 4 bits of device reg */
967                         tf->device |= scsicmd[6];
968
969                         qc->nsect = scsicmd[13];
970                 }
971
972                 tf->nsect = scsicmd[13];
973                 tf->lbal = scsicmd[9];
974                 tf->lbam = scsicmd[8];
975                 tf->lbah = scsicmd[7];
976
977                 VPRINTK("sixteen-byte command\n");
978                 if (qc->nsect == 0) /* we don't support length==0 cmds */
979                         return 1;
980                 return 0;
981         }
982
983         DPRINTK("no-byte command\n");
984         return 1;
985 }
986
987 static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
988 {
989         struct scsi_cmnd *cmd = qc->scsicmd;
990         int need_sense = drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ);
991
992         /* For ATA pass thru (SAT) commands, generate a sense block if
993          * user mandated it or if there's an error.  Note that if we
994          * generate because the user forced us to, a check condition
995          * is generated and the ATA register values are returned
996          * whether the command completed successfully or not. If there
997          * was no error, SK, ASC and ASCQ will all be zero.
998          */
999         if (((cmd->cmnd[0] == ATA_16) || (cmd->cmnd[0] == ATA_12)) &&
1000             ((cmd->cmnd[2] & 0x20) || need_sense)) {
1001                 ata_gen_ata_desc_sense(qc);
1002         } else {
1003                 if (!need_sense) {
1004                         cmd->result = SAM_STAT_GOOD;
1005                 } else {
1006                         /* TODO: decide which descriptor format to use
1007                          * for 48b LBA devices and call that here
1008                          * instead of the fixed desc, which is only
1009                          * good for smaller LBA (and maybe CHS?)
1010                          * devices.
1011                          */
1012                         ata_gen_fixed_sense(qc);
1013                 }
1014         }
1015
1016         if (need_sense) {
1017                 /* The ata_gen_..._sense routines fill in tf */
1018                 ata_dump_status(qc->ap->id, &qc->tf);
1019         }
1020
1021         qc->scsidone(cmd);
1022
1023         return 0;
1024 }
1025
1026 /**
1027  *      ata_scsi_translate - Translate then issue SCSI command to ATA device
1028  *      @ap: ATA port to which the command is addressed
1029  *      @dev: ATA device to which the command is addressed
1030  *      @cmd: SCSI command to execute
1031  *      @done: SCSI command completion function
1032  *      @xlat_func: Actor which translates @cmd to an ATA taskfile
1033  *
1034  *      Our ->queuecommand() function has decided that the SCSI
1035  *      command issued can be directly translated into an ATA
1036  *      command, rather than handled internally.
1037  *
1038  *      This function sets up an ata_queued_cmd structure for the
1039  *      SCSI command, and sends that ata_queued_cmd to the hardware.
1040  *
1041  *      LOCKING:
1042  *      spin_lock_irqsave(host_set lock)
1043  */
1044
1045 static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
1046                               struct scsi_cmnd *cmd,
1047                               void (*done)(struct scsi_cmnd *),
1048                               ata_xlat_func_t xlat_func)
1049 {
1050         struct ata_queued_cmd *qc;
1051         u8 *scsicmd = cmd->cmnd;
1052
1053         VPRINTK("ENTER\n");
1054
1055         qc = ata_scsi_qc_new(ap, dev, cmd, done);
1056         if (!qc)
1057                 return;
1058
1059         /* data is present; dma-map it */
1060         if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1061             cmd->sc_data_direction == DMA_TO_DEVICE) {
1062                 if (unlikely(cmd->request_bufflen < 1)) {
1063                         printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
1064                                ap->id, dev->devno);
1065                         goto err_out;
1066                 }
1067
1068                 if (cmd->use_sg)
1069                         ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
1070                 else
1071                         ata_sg_init_one(qc, cmd->request_buffer,
1072                                         cmd->request_bufflen);
1073
1074                 qc->dma_dir = cmd->sc_data_direction;
1075         }
1076
1077         qc->complete_fn = ata_scsi_qc_complete;
1078
1079         if (xlat_func(qc, scsicmd))
1080                 goto err_out;
1081         /* select device, send command to hardware */
1082         if (ata_qc_issue(qc))
1083                 goto err_out;
1084
1085         VPRINTK("EXIT\n");
1086         return;
1087
1088 err_out:
1089         ata_qc_free(qc);
1090         ata_bad_cdb(cmd, done);
1091         DPRINTK("EXIT - badcmd\n");
1092 }
1093
1094 /**
1095  *      ata_scsi_rbuf_get - Map response buffer.
1096  *      @cmd: SCSI command containing buffer to be mapped.
1097  *      @buf_out: Pointer to mapped area.
1098  *
1099  *      Maps buffer contained within SCSI command @cmd.
1100  *
1101  *      LOCKING:
1102  *      spin_lock_irqsave(host_set lock)
1103  *
1104  *      RETURNS:
1105  *      Length of response buffer.
1106  */
1107
1108 static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
1109 {
1110         u8 *buf;
1111         unsigned int buflen;
1112
1113         if (cmd->use_sg) {
1114                 struct scatterlist *sg;
1115
1116                 sg = (struct scatterlist *) cmd->request_buffer;
1117                 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
1118                 buflen = sg->length;
1119         } else {
1120                 buf = cmd->request_buffer;
1121                 buflen = cmd->request_bufflen;
1122         }
1123
1124         *buf_out = buf;
1125         return buflen;
1126 }
1127
1128 /**
1129  *      ata_scsi_rbuf_put - Unmap response buffer.
1130  *      @cmd: SCSI command containing buffer to be unmapped.
1131  *      @buf: buffer to unmap
1132  *
1133  *      Unmaps response buffer contained within @cmd.
1134  *
1135  *      LOCKING:
1136  *      spin_lock_irqsave(host_set lock)
1137  */
1138
1139 static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
1140 {
1141         if (cmd->use_sg) {
1142                 struct scatterlist *sg;
1143
1144                 sg = (struct scatterlist *) cmd->request_buffer;
1145                 kunmap_atomic(buf - sg->offset, KM_USER0);
1146         }
1147 }
1148
1149 /**
1150  *      ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1151  *      @args: device IDENTIFY data / SCSI command of interest.
1152  *      @actor: Callback hook for desired SCSI command simulator
1153  *
1154  *      Takes care of the hard work of simulating a SCSI command...
1155  *      Mapping the response buffer, calling the command's handler,
1156  *      and handling the handler's return value.  This return value
1157  *      indicates whether the handler wishes the SCSI command to be
1158  *      completed successfully, or not.
1159  *
1160  *      LOCKING:
1161  *      spin_lock_irqsave(host_set lock)
1162  */
1163
1164 void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
1165                         unsigned int (*actor) (struct ata_scsi_args *args,
1166                                            u8 *rbuf, unsigned int buflen))
1167 {
1168         u8 *rbuf;
1169         unsigned int buflen, rc;
1170         struct scsi_cmnd *cmd = args->cmd;
1171
1172         buflen = ata_scsi_rbuf_get(cmd, &rbuf);
1173         memset(rbuf, 0, buflen);
1174         rc = actor(args, rbuf, buflen);
1175         ata_scsi_rbuf_put(cmd, rbuf);
1176
1177         if (rc)
1178                 ata_bad_cdb(cmd, args->done);
1179         else {
1180                 cmd->result = SAM_STAT_GOOD;
1181                 args->done(cmd);
1182         }
1183 }
1184
1185 /**
1186  *      ata_scsiop_inq_std - Simulate INQUIRY command
1187  *      @args: device IDENTIFY data / SCSI command of interest.
1188  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1189  *      @buflen: Response buffer length.
1190  *
1191  *      Returns standard device identification data associated
1192  *      with non-EVPD INQUIRY command output.
1193  *
1194  *      LOCKING:
1195  *      spin_lock_irqsave(host_set lock)
1196  */
1197
1198 unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
1199                                unsigned int buflen)
1200 {
1201         u8 hdr[] = {
1202                 TYPE_DISK,
1203                 0,
1204                 0x5,    /* claim SPC-3 version compatibility */
1205                 2,
1206                 95 - 4
1207         };
1208
1209         /* set scsi removeable (RMB) bit per ata bit */
1210         if (ata_id_removeable(args->id))
1211                 hdr[1] |= (1 << 7);
1212
1213         VPRINTK("ENTER\n");
1214
1215         memcpy(rbuf, hdr, sizeof(hdr));
1216
1217         if (buflen > 35) {
1218                 memcpy(&rbuf[8], "ATA     ", 8);
1219                 ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
1220                 ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
1221                 if (rbuf[32] == 0 || rbuf[32] == ' ')
1222                         memcpy(&rbuf[32], "n/a ", 4);
1223         }
1224
1225         if (buflen > 63) {
1226                 const u8 versions[] = {
1227                         0x60,   /* SAM-3 (no version claimed) */
1228
1229                         0x03,
1230                         0x20,   /* SBC-2 (no version claimed) */
1231
1232                         0x02,
1233                         0x60    /* SPC-3 (no version claimed) */
1234                 };
1235
1236                 memcpy(rbuf + 59, versions, sizeof(versions));
1237         }
1238
1239         return 0;
1240 }
1241
1242 /**
1243  *      ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
1244  *      @args: device IDENTIFY data / SCSI command of interest.
1245  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1246  *      @buflen: Response buffer length.
1247  *
1248  *      Returns list of inquiry EVPD pages available.
1249  *
1250  *      LOCKING:
1251  *      spin_lock_irqsave(host_set lock)
1252  */
1253
1254 unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
1255                               unsigned int buflen)
1256 {
1257         const u8 pages[] = {
1258                 0x00,   /* page 0x00, this page */
1259                 0x80,   /* page 0x80, unit serial no page */
1260                 0x83    /* page 0x83, device ident page */
1261         };
1262         rbuf[3] = sizeof(pages);        /* number of supported EVPD pages */
1263
1264         if (buflen > 6)
1265                 memcpy(rbuf + 4, pages, sizeof(pages));
1266
1267         return 0;
1268 }
1269
1270 /**
1271  *      ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
1272  *      @args: device IDENTIFY data / SCSI command of interest.
1273  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1274  *      @buflen: Response buffer length.
1275  *
1276  *      Returns ATA device serial number.
1277  *
1278  *      LOCKING:
1279  *      spin_lock_irqsave(host_set lock)
1280  */
1281
1282 unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
1283                               unsigned int buflen)
1284 {
1285         const u8 hdr[] = {
1286                 0,
1287                 0x80,                   /* this page code */
1288                 0,
1289                 ATA_SERNO_LEN,          /* page len */
1290         };
1291         memcpy(rbuf, hdr, sizeof(hdr));
1292
1293         if (buflen > (ATA_SERNO_LEN + 4 - 1))
1294                 ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
1295                                   ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1296
1297         return 0;
1298 }
1299
1300 static const char *inq_83_str = "Linux ATA-SCSI simulator";
1301
1302 /**
1303  *      ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
1304  *      @args: device IDENTIFY data / SCSI command of interest.
1305  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1306  *      @buflen: Response buffer length.
1307  *
1308  *      Returns device identification.  Currently hardcoded to
1309  *      return "Linux ATA-SCSI simulator".
1310  *
1311  *      LOCKING:
1312  *      spin_lock_irqsave(host_set lock)
1313  */
1314
1315 unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
1316                               unsigned int buflen)
1317 {
1318         rbuf[1] = 0x83;                 /* this page code */
1319         rbuf[3] = 4 + strlen(inq_83_str);       /* page len */
1320
1321         /* our one and only identification descriptor (vendor-specific) */
1322         if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
1323                 rbuf[4 + 0] = 2;        /* code set: ASCII */
1324                 rbuf[4 + 3] = strlen(inq_83_str);
1325                 memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
1326         }
1327
1328         return 0;
1329 }
1330
1331 /**
1332  *      ata_scsiop_noop - Command handler that simply returns success.
1333  *      @args: device IDENTIFY data / SCSI command of interest.
1334  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1335  *      @buflen: Response buffer length.
1336  *
1337  *      No operation.  Simply returns success to caller, to indicate
1338  *      that the caller should successfully complete this SCSI command.
1339  *
1340  *      LOCKING:
1341  *      spin_lock_irqsave(host_set lock)
1342  */
1343
1344 unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
1345                             unsigned int buflen)
1346 {
1347         VPRINTK("ENTER\n");
1348         return 0;
1349 }
1350
1351 /**
1352  *      ata_msense_push - Push data onto MODE SENSE data output buffer
1353  *      @ptr_io: (input/output) Location to store more output data
1354  *      @last: End of output data buffer
1355  *      @buf: Pointer to BLOB being added to output buffer
1356  *      @buflen: Length of BLOB
1357  *
1358  *      Store MODE SENSE data on an output buffer.
1359  *
1360  *      LOCKING:
1361  *      None.
1362  */
1363
1364 static void ata_msense_push(u8 **ptr_io, const u8 *last,
1365                             const u8 *buf, unsigned int buflen)
1366 {
1367         u8 *ptr = *ptr_io;
1368
1369         if ((ptr + buflen - 1) > last)
1370                 return;
1371
1372         memcpy(ptr, buf, buflen);
1373
1374         ptr += buflen;
1375
1376         *ptr_io = ptr;
1377 }
1378
1379 /**
1380  *      ata_msense_caching - Simulate MODE SENSE caching info page
1381  *      @id: device IDENTIFY data
1382  *      @ptr_io: (input/output) Location to store more output data
1383  *      @last: End of output data buffer
1384  *
1385  *      Generate a caching info page, which conditionally indicates
1386  *      write caching to the SCSI layer, depending on device
1387  *      capabilities.
1388  *
1389  *      LOCKING:
1390  *      None.
1391  */
1392
1393 static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1394                                        const u8 *last)
1395 {
1396         u8 page[] = {
1397                 0x8,                            /* page code */
1398                 0x12,                           /* page length */
1399                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   /* 10 zeroes */
1400                 0, 0, 0, 0, 0, 0, 0, 0          /* 8 zeroes */
1401         };
1402
1403         if (ata_id_wcache_enabled(id))
1404                 page[2] |= (1 << 2);    /* write cache enable */
1405         if (!ata_id_rahead_enabled(id))
1406                 page[12] |= (1 << 5);   /* disable read ahead */
1407
1408         ata_msense_push(ptr_io, last, page, sizeof(page));
1409         return sizeof(page);
1410 }
1411
1412 /**
1413  *      ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1414  *      @dev: Device associated with this MODE SENSE command
1415  *      @ptr_io: (input/output) Location to store more output data
1416  *      @last: End of output data buffer
1417  *
1418  *      Generate a generic MODE SENSE control mode page.
1419  *
1420  *      LOCKING:
1421  *      None.
1422  */
1423
1424 static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1425 {
1426         const u8 page[] = {0xa, 0xa, 6, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
1427
1428         /* byte 2: set the descriptor format sense data bit (bit 2)
1429          * since we need to support returning this format for SAT
1430          * commands and any SCSI commands against a 48b LBA device.
1431          */
1432
1433         ata_msense_push(ptr_io, last, page, sizeof(page));
1434         return sizeof(page);
1435 }
1436
1437 /**
1438  *      ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1439  *      @dev: Device associated with this MODE SENSE command
1440  *      @ptr_io: (input/output) Location to store more output data
1441  *      @last: End of output data buffer
1442  *
1443  *      Generate a generic MODE SENSE r/w error recovery page.
1444  *
1445  *      LOCKING:
1446  *      None.
1447  */
1448
1449 static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1450 {
1451         const u8 page[] = {
1452                 0x1,                      /* page code */
1453                 0xa,                      /* page length */
1454                 (1 << 7) | (1 << 6),      /* note auto r/w reallocation */
1455                 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
1456         };
1457
1458         ata_msense_push(ptr_io, last, page, sizeof(page));
1459         return sizeof(page);
1460 }
1461
1462 /**
1463  *      ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1464  *      @args: device IDENTIFY data / SCSI command of interest.
1465  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1466  *      @buflen: Response buffer length.
1467  *
1468  *      Simulate MODE SENSE commands.
1469  *
1470  *      LOCKING:
1471  *      spin_lock_irqsave(host_set lock)
1472  */
1473
1474 unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1475                                   unsigned int buflen)
1476 {
1477         u8 *scsicmd = args->cmd->cmnd, *p, *last;
1478         unsigned int page_control, six_byte, output_len;
1479
1480         VPRINTK("ENTER\n");
1481
1482         six_byte = (scsicmd[0] == MODE_SENSE);
1483
1484         /* we only support saved and current values (which we treat
1485          * in the same manner)
1486          */
1487         page_control = scsicmd[2] >> 6;
1488         if ((page_control != 0) && (page_control != 3))
1489                 return 1;
1490
1491         if (six_byte)
1492                 output_len = 4;
1493         else
1494                 output_len = 8;
1495
1496         p = rbuf + output_len;
1497         last = rbuf + buflen - 1;
1498
1499         switch(scsicmd[2] & 0x3f) {
1500         case 0x01:              /* r/w error recovery */
1501                 output_len += ata_msense_rw_recovery(&p, last);
1502                 break;
1503
1504         case 0x08:              /* caching */
1505                 output_len += ata_msense_caching(args->id, &p, last);
1506                 break;
1507
1508         case 0x0a: {            /* control mode */
1509                 output_len += ata_msense_ctl_mode(&p, last);
1510                 break;
1511                 }
1512
1513         case 0x3f:              /* all pages */
1514                 output_len += ata_msense_rw_recovery(&p, last);
1515                 output_len += ata_msense_caching(args->id, &p, last);
1516                 output_len += ata_msense_ctl_mode(&p, last);
1517                 break;
1518
1519         default:                /* invalid page code */
1520                 return 1;
1521         }
1522
1523         if (six_byte) {
1524                 output_len--;
1525                 rbuf[0] = output_len;
1526         } else {
1527                 output_len -= 2;
1528                 rbuf[0] = output_len >> 8;
1529                 rbuf[1] = output_len;
1530         }
1531
1532         return 0;
1533 }
1534
1535 /**
1536  *      ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
1537  *      @args: device IDENTIFY data / SCSI command of interest.
1538  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1539  *      @buflen: Response buffer length.
1540  *
1541  *      Simulate READ CAPACITY commands.
1542  *
1543  *      LOCKING:
1544  *      spin_lock_irqsave(host_set lock)
1545  */
1546
1547 unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
1548                                 unsigned int buflen)
1549 {
1550         u64 n_sectors;
1551         u32 tmp;
1552
1553         VPRINTK("ENTER\n");
1554
1555         if (ata_id_has_lba48(args->id))
1556                 n_sectors = ata_id_u64(args->id, 100);
1557         else
1558                 n_sectors = ata_id_u32(args->id, 60);
1559         n_sectors--;            /* ATA TotalUserSectors - 1 */
1560
1561         if (args->cmd->cmnd[0] == READ_CAPACITY) {
1562                 if( n_sectors >= 0xffffffffULL )
1563                         tmp = 0xffffffff ;  /* Return max count on overflow */
1564                 else
1565                         tmp = n_sectors ;
1566
1567                 /* sector count, 32-bit */
1568                 rbuf[0] = tmp >> (8 * 3);
1569                 rbuf[1] = tmp >> (8 * 2);
1570                 rbuf[2] = tmp >> (8 * 1);
1571                 rbuf[3] = tmp;
1572
1573                 /* sector size */
1574                 tmp = ATA_SECT_SIZE;
1575                 rbuf[6] = tmp >> 8;
1576                 rbuf[7] = tmp;
1577
1578         } else {
1579                 /* sector count, 64-bit */
1580                 tmp = n_sectors >> (8 * 4);
1581                 rbuf[2] = tmp >> (8 * 3);
1582                 rbuf[3] = tmp >> (8 * 2);
1583                 rbuf[4] = tmp >> (8 * 1);
1584                 rbuf[5] = tmp;
1585                 tmp = n_sectors;
1586                 rbuf[6] = tmp >> (8 * 3);
1587                 rbuf[7] = tmp >> (8 * 2);
1588                 rbuf[8] = tmp >> (8 * 1);
1589                 rbuf[9] = tmp;
1590
1591                 /* sector size */
1592                 tmp = ATA_SECT_SIZE;
1593                 rbuf[12] = tmp >> 8;
1594                 rbuf[13] = tmp;
1595         }
1596
1597         return 0;
1598 }
1599
1600 /**
1601  *      ata_scsiop_report_luns - Simulate REPORT LUNS command
1602  *      @args: device IDENTIFY data / SCSI command of interest.
1603  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1604  *      @buflen: Response buffer length.
1605  *
1606  *      Simulate REPORT LUNS command.
1607  *
1608  *      LOCKING:
1609  *      spin_lock_irqsave(host_set lock)
1610  */
1611
1612 unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1613                                    unsigned int buflen)
1614 {
1615         VPRINTK("ENTER\n");
1616         rbuf[3] = 8;    /* just one lun, LUN 0, size 8 bytes */
1617
1618         return 0;
1619 }
1620
1621 /**
1622  *      ata_scsi_badcmd - End a SCSI request with an error
1623  *      @cmd: SCSI request to be handled
1624  *      @done: SCSI command completion function
1625  *      @asc: SCSI-defined additional sense code
1626  *      @ascq: SCSI-defined additional sense code qualifier
1627  *
1628  *      Helper function that completes a SCSI command with
1629  *      %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
1630  *      and the specified additional sense codes.
1631  *
1632  *      LOCKING:
1633  *      spin_lock_irqsave(host_set lock)
1634  */
1635
1636 void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
1637 {
1638         DPRINTK("ENTER\n");
1639         cmd->result = SAM_STAT_CHECK_CONDITION;
1640
1641         cmd->sense_buffer[0] = 0x70;
1642         cmd->sense_buffer[2] = ILLEGAL_REQUEST;
1643         cmd->sense_buffer[7] = 14 - 8;  /* addnl. sense len. FIXME: correct? */
1644         cmd->sense_buffer[12] = asc;
1645         cmd->sense_buffer[13] = ascq;
1646
1647         done(cmd);
1648 }
1649
1650 static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
1651 {
1652         struct scsi_cmnd *cmd = qc->scsicmd;
1653
1654         if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) {
1655                 DPRINTK("request check condition\n");
1656
1657                 cmd->result = SAM_STAT_CHECK_CONDITION;
1658
1659                 qc->scsidone(cmd);
1660
1661                 return 1;
1662         } else {
1663                 u8 *scsicmd = cmd->cmnd;
1664
1665                 if (scsicmd[0] == INQUIRY) {
1666                         u8 *buf = NULL;
1667                         unsigned int buflen;
1668
1669                         buflen = ata_scsi_rbuf_get(cmd, &buf);
1670                         buf[2] = 0x5;
1671                         buf[3] = (buf[3] & 0xf0) | 2;
1672                         ata_scsi_rbuf_put(cmd, buf);
1673                 }
1674                 cmd->result = SAM_STAT_GOOD;
1675         }
1676
1677         qc->scsidone(cmd);
1678
1679         return 0;
1680 }
1681 /**
1682  *      atapi_xlat - Initialize PACKET taskfile
1683  *      @qc: command structure to be initialized
1684  *      @scsicmd: SCSI CDB associated with this PACKET command
1685  *
1686  *      LOCKING:
1687  *      spin_lock_irqsave(host_set lock)
1688  *
1689  *      RETURNS:
1690  *      Zero on success, non-zero on failure.
1691  */
1692
1693 static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
1694 {
1695         struct scsi_cmnd *cmd = qc->scsicmd;
1696         struct ata_device *dev = qc->dev;
1697         int using_pio = (dev->flags & ATA_DFLAG_PIO);
1698         int nodata = (cmd->sc_data_direction == DMA_NONE);
1699
1700         if (!using_pio)
1701                 /* Check whether ATAPI DMA is safe */
1702                 if (ata_check_atapi_dma(qc))
1703                         using_pio = 1;
1704
1705         memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
1706
1707         qc->complete_fn = atapi_qc_complete;
1708
1709         qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1710         if (cmd->sc_data_direction == DMA_TO_DEVICE) {
1711                 qc->tf.flags |= ATA_TFLAG_WRITE;
1712                 DPRINTK("direction: write\n");
1713         }
1714
1715         qc->tf.command = ATA_CMD_PACKET;
1716
1717         /* no data, or PIO data xfer */
1718         if (using_pio || nodata) {
1719                 if (nodata)
1720                         qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
1721                 else
1722                         qc->tf.protocol = ATA_PROT_ATAPI;
1723                 qc->tf.lbam = (8 * 1024) & 0xff;
1724                 qc->tf.lbah = (8 * 1024) >> 8;
1725         }
1726
1727         /* DMA data xfer */
1728         else {
1729                 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
1730                 qc->tf.feature |= ATAPI_PKT_DMA;
1731
1732 #ifdef ATAPI_ENABLE_DMADIR
1733                 /* some SATA bridges need us to indicate data xfer direction */
1734                 if (cmd->sc_data_direction != DMA_TO_DEVICE)
1735                         qc->tf.feature |= ATAPI_DMADIR;
1736 #endif
1737         }
1738
1739         qc->nbytes = cmd->bufflen;
1740
1741         return 0;
1742 }
1743
1744 /**
1745  *      ata_scsi_find_dev - lookup ata_device from scsi_cmnd
1746  *      @ap: ATA port to which the device is attached
1747  *      @scsidev: SCSI device from which we derive the ATA device
1748  *
1749  *      Given various information provided in struct scsi_cmnd,
1750  *      map that onto an ATA bus, and using that mapping
1751  *      determine which ata_device is associated with the
1752  *      SCSI command to be sent.
1753  *
1754  *      LOCKING:
1755  *      spin_lock_irqsave(host_set lock)
1756  *
1757  *      RETURNS:
1758  *      Associated ATA device, or %NULL if not found.
1759  */
1760
1761 static struct ata_device *
1762 ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev)
1763 {
1764         struct ata_device *dev;
1765
1766         /* skip commands not addressed to targets we simulate */
1767         if (likely(scsidev->id < ATA_MAX_DEVICES))
1768                 dev = &ap->device[scsidev->id];
1769         else
1770                 return NULL;
1771
1772         if (unlikely((scsidev->channel != 0) ||
1773                      (scsidev->lun != 0)))
1774                 return NULL;
1775
1776         if (unlikely(!ata_dev_present(dev)))
1777                 return NULL;
1778
1779         if (!atapi_enabled) {
1780                 if (unlikely(dev->class == ATA_DEV_ATAPI))
1781                         return NULL;
1782         }
1783
1784         return dev;
1785 }
1786
1787 /*
1788  *      ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
1789  *      @byte1: Byte 1 from pass-thru CDB.
1790  *
1791  *      RETURNS:
1792  *      ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
1793  */
1794 static u8
1795 ata_scsi_map_proto(u8 byte1)
1796 {
1797         switch((byte1 & 0x1e) >> 1) {
1798                 case 3:         /* Non-data */
1799                         return ATA_PROT_NODATA;
1800
1801                 case 6:         /* DMA */
1802                         return ATA_PROT_DMA;
1803
1804                 case 4:         /* PIO Data-in */
1805                 case 5:         /* PIO Data-out */
1806                         if (byte1 & 0xe0) {
1807                                 return ATA_PROT_PIO_MULT;
1808                         }
1809                         return ATA_PROT_PIO;
1810
1811                 case 10:        /* Device Reset */
1812                 case 0:         /* Hard Reset */
1813                 case 1:         /* SRST */
1814                 case 2:         /* Bus Idle */
1815                 case 7:         /* Packet */
1816                 case 8:         /* DMA Queued */
1817                 case 9:         /* Device Diagnostic */
1818                 case 11:        /* UDMA Data-in */
1819                 case 12:        /* UDMA Data-Out */
1820                 case 13:        /* FPDMA */
1821                 default:        /* Reserved */
1822                         break;
1823         }
1824
1825         return ATA_PROT_UNKNOWN;
1826 }
1827
1828 /**
1829  *      ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
1830  *      @qc: command structure to be initialized
1831  *      @cmd: SCSI command to convert
1832  *
1833  *      Handles either 12 or 16-byte versions of the CDB.
1834  *
1835  *      RETURNS:
1836  *      Zero on success, non-zero on failure.
1837  */
1838 static unsigned int
1839 ata_scsi_pass_thru(struct ata_queued_cmd *qc, u8 *scsicmd)
1840 {
1841         struct ata_taskfile *tf = &(qc->tf);
1842         struct scsi_cmnd *cmd = qc->scsicmd;
1843
1844         if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
1845                 return 1;
1846
1847         /*
1848          * 12 and 16 byte CDBs use different offsets to
1849          * provide the various register values.
1850          */
1851         if (scsicmd[0] == ATA_16) {
1852                 /*
1853                  * 16-byte CDB - may contain extended commands.
1854                  *
1855                  * If that is the case, copy the upper byte register values.
1856                  */
1857                 if (scsicmd[1] & 0x01) {
1858                         tf->hob_feature = scsicmd[3];
1859                         tf->hob_nsect = scsicmd[5];
1860                         tf->hob_lbal = scsicmd[7];
1861                         tf->hob_lbam = scsicmd[9];
1862                         tf->hob_lbah = scsicmd[11];
1863                         tf->flags |= ATA_TFLAG_LBA48;
1864                 } else
1865                         tf->flags &= ~ATA_TFLAG_LBA48;
1866
1867                 /*
1868                  * Always copy low byte, device and command registers.
1869                  */
1870                 tf->feature = scsicmd[4];
1871                 tf->nsect = scsicmd[6];
1872                 tf->lbal = scsicmd[8];
1873                 tf->lbam = scsicmd[10];
1874                 tf->lbah = scsicmd[12];
1875                 tf->device = scsicmd[13];
1876                 tf->command = scsicmd[14];
1877         } else {
1878                 /*
1879                  * 12-byte CDB - incapable of extended commands.
1880                  */
1881                 tf->flags &= ~ATA_TFLAG_LBA48;
1882
1883                 tf->feature = scsicmd[3];
1884                 tf->nsect = scsicmd[4];
1885                 tf->lbal = scsicmd[5];
1886                 tf->lbam = scsicmd[6];
1887                 tf->lbah = scsicmd[7];
1888                 tf->device = scsicmd[8];
1889                 tf->command = scsicmd[9];
1890         }
1891
1892         /*
1893          * Filter SET_FEATURES - XFER MODE command -- otherwise,
1894          * SET_FEATURES - XFER MODE must be preceded/succeeded
1895          * by an update to hardware-specific registers for each
1896          * controller (i.e. the reason for ->set_piomode(),
1897          * ->set_dmamode(), and ->post_set_mode() hooks).
1898          */
1899         if ((tf->command == ATA_CMD_SET_FEATURES)
1900          && (tf->feature == SETFEATURES_XFER))
1901                 return 1;
1902
1903         /*
1904          * Set flags so that all registers will be written,
1905          * and pass on write indication (used for PIO/DMA
1906          * setup.)
1907          */
1908         tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
1909
1910         if (cmd->sc_data_direction == DMA_TO_DEVICE)
1911                 tf->flags |= ATA_TFLAG_WRITE;
1912
1913         /*
1914          * Set transfer length.
1915          *
1916          * TODO: find out if we need to do more here to
1917          *       cover scatter/gather case.
1918          */
1919         qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
1920
1921         return 0;
1922 }
1923
1924 /**
1925  *      ata_get_xlat_func - check if SCSI to ATA translation is possible
1926  *      @dev: ATA device
1927  *      @cmd: SCSI command opcode to consider
1928  *
1929  *      Look up the SCSI command given, and determine whether the
1930  *      SCSI command is to be translated or simulated.
1931  *
1932  *      RETURNS:
1933  *      Pointer to translation function if possible, %NULL if not.
1934  */
1935
1936 static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
1937 {
1938         switch (cmd) {
1939         case READ_6:
1940         case READ_10:
1941         case READ_16:
1942
1943         case WRITE_6:
1944         case WRITE_10:
1945         case WRITE_16:
1946                 return ata_scsi_rw_xlat;
1947
1948         case SYNCHRONIZE_CACHE:
1949                 if (ata_try_flush_cache(dev))
1950                         return ata_scsi_flush_xlat;
1951                 break;
1952
1953         case VERIFY:
1954         case VERIFY_16:
1955                 return ata_scsi_verify_xlat;
1956
1957         case ATA_12:
1958         case ATA_16:
1959                 return ata_scsi_pass_thru;
1960
1961         case START_STOP:
1962                 return ata_scsi_start_stop_xlat;
1963         }
1964
1965         return NULL;
1966 }
1967
1968 /**
1969  *      ata_scsi_dump_cdb - dump SCSI command contents to dmesg
1970  *      @ap: ATA port to which the command was being sent
1971  *      @cmd: SCSI command to dump
1972  *
1973  *      Prints the contents of a SCSI command via printk().
1974  */
1975
1976 static inline void ata_scsi_dump_cdb(struct ata_port *ap,
1977                                      struct scsi_cmnd *cmd)
1978 {
1979 #ifdef ATA_DEBUG
1980         struct scsi_device *scsidev = cmd->device;
1981         u8 *scsicmd = cmd->cmnd;
1982
1983         DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
1984                 ap->id,
1985                 scsidev->channel, scsidev->id, scsidev->lun,
1986                 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
1987                 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
1988                 scsicmd[8]);
1989 #endif
1990 }
1991
1992 /**
1993  *      ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
1994  *      @cmd: SCSI command to be sent
1995  *      @done: Completion function, called when command is complete
1996  *
1997  *      In some cases, this function translates SCSI commands into
1998  *      ATA taskfiles, and queues the taskfiles to be sent to
1999  *      hardware.  In other cases, this function simulates a
2000  *      SCSI device by evaluating and responding to certain
2001  *      SCSI commands.  This creates the overall effect of
2002  *      ATA and ATAPI devices appearing as SCSI devices.
2003  *
2004  *      LOCKING:
2005  *      Releases scsi-layer-held lock, and obtains host_set lock.
2006  *
2007  *      RETURNS:
2008  *      Zero.
2009  */
2010
2011 int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
2012 {
2013         struct ata_port *ap;
2014         struct ata_device *dev;
2015         struct scsi_device *scsidev = cmd->device;
2016
2017         ap = (struct ata_port *) &scsidev->host->hostdata[0];
2018
2019         ata_scsi_dump_cdb(ap, cmd);
2020
2021         dev = ata_scsi_find_dev(ap, scsidev);
2022         if (unlikely(!dev)) {
2023                 cmd->result = (DID_BAD_TARGET << 16);
2024                 done(cmd);
2025                 goto out_unlock;
2026         }
2027
2028         if (dev->class == ATA_DEV_ATA) {
2029                 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
2030                                                               cmd->cmnd[0]);
2031
2032                 if (xlat_func)
2033                         ata_scsi_translate(ap, dev, cmd, done, xlat_func);
2034                 else
2035                         ata_scsi_simulate(dev->id, cmd, done);
2036         } else
2037                 ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
2038
2039 out_unlock:
2040         return 0;
2041 }
2042
2043 /**
2044  *      ata_scsi_simulate - simulate SCSI command on ATA device
2045  *      @id: current IDENTIFY data for target device.
2046  *      @cmd: SCSI command being sent to device.
2047  *      @done: SCSI command completion function.
2048  *
2049  *      Interprets and directly executes a select list of SCSI commands
2050  *      that can be handled internally.
2051  *
2052  *      LOCKING:
2053  *      spin_lock_irqsave(host_set lock)
2054  */
2055
2056 void ata_scsi_simulate(u16 *id,
2057                       struct scsi_cmnd *cmd,
2058                       void (*done)(struct scsi_cmnd *))
2059 {
2060         struct ata_scsi_args args;
2061         u8 *scsicmd = cmd->cmnd;
2062
2063         args.id = id;
2064         args.cmd = cmd;
2065         args.done = done;
2066
2067         switch(scsicmd[0]) {
2068                 /* no-op's, complete with success */
2069                 case SYNCHRONIZE_CACHE:
2070                 case REZERO_UNIT:
2071                 case SEEK_6:
2072                 case SEEK_10:
2073                 case TEST_UNIT_READY:
2074                 case FORMAT_UNIT:               /* FIXME: correct? */
2075                 case SEND_DIAGNOSTIC:           /* FIXME: correct? */
2076                         ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
2077                         break;
2078
2079                 case INQUIRY:
2080                         if (scsicmd[1] & 2)                /* is CmdDt set?  */
2081                                 ata_bad_cdb(cmd, done);
2082                         else if ((scsicmd[1] & 1) == 0)    /* is EVPD clear? */
2083                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
2084                         else if (scsicmd[2] == 0x00)
2085                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
2086                         else if (scsicmd[2] == 0x80)
2087                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
2088                         else if (scsicmd[2] == 0x83)
2089                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
2090                         else
2091                                 ata_bad_cdb(cmd, done);
2092                         break;
2093
2094                 case MODE_SENSE:
2095                 case MODE_SENSE_10:
2096                         ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
2097                         break;
2098
2099                 case MODE_SELECT:       /* unconditionally return */
2100                 case MODE_SELECT_10:    /* bad-field-in-cdb */
2101                         ata_bad_cdb(cmd, done);
2102                         break;
2103
2104                 case READ_CAPACITY:
2105                         ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2106                         break;
2107
2108                 case SERVICE_ACTION_IN:
2109                         if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
2110                                 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2111                         else
2112                                 ata_bad_cdb(cmd, done);
2113                         break;
2114
2115                 case REPORT_LUNS:
2116                         ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
2117                         break;
2118
2119                 /* mandatory commands we haven't implemented yet */
2120                 case REQUEST_SENSE:
2121
2122                 /* all other commands */
2123                 default:
2124                         ata_bad_scsiop(cmd, done);
2125                         break;
2126         }
2127 }
2128