[PATCH] libata: use dev->ap
[pandora-kernel.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/scsi_host.h>
41 #include <scsi/scsi_eh.h>
42 #include <scsi/scsi_device.h>
43 #include <scsi/scsi_request.h>
44 #include <scsi/scsi_transport.h>
45 #include <linux/libata.h>
46 #include <linux/hdreg.h>
47 #include <asm/uaccess.h>
48
49 #include "libata.h"
50
51 #define SECTOR_SIZE     512
52
53 typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, const u8 *scsicmd);
54 static struct ata_device *
55 ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev);
56
57 #define RW_RECOVERY_MPAGE 0x1
58 #define RW_RECOVERY_MPAGE_LEN 12
59 #define CACHE_MPAGE 0x8
60 #define CACHE_MPAGE_LEN 20
61 #define CONTROL_MPAGE 0xa
62 #define CONTROL_MPAGE_LEN 12
63 #define ALL_MPAGES 0x3f
64 #define ALL_SUB_MPAGES 0xff
65
66
67 static const u8 def_rw_recovery_mpage[] = {
68         RW_RECOVERY_MPAGE,
69         RW_RECOVERY_MPAGE_LEN - 2,
70         (1 << 7) |      /* AWRE, sat-r06 say it shall be 0 */
71             (1 << 6),   /* ARRE (auto read reallocation) */
72         0,              /* read retry count */
73         0, 0, 0, 0,
74         0,              /* write retry count */
75         0, 0, 0
76 };
77
78 static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
79         CACHE_MPAGE,
80         CACHE_MPAGE_LEN - 2,
81         0,              /* contains WCE, needs to be 0 for logic */
82         0, 0, 0, 0, 0, 0, 0, 0, 0,
83         0,              /* contains DRA, needs to be 0 for logic */
84         0, 0, 0, 0, 0, 0, 0
85 };
86
87 static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
88         CONTROL_MPAGE,
89         CONTROL_MPAGE_LEN - 2,
90         2,      /* DSENSE=0, GLTSD=1 */
91         0,      /* [QAM+QERR may be 1, see 05-359r1] */
92         0, 0, 0, 0, 0xff, 0xff,
93         0, 30   /* extended self test time, see 05-359r1 */
94 };
95
96 /*
97  * libata transport template.  libata doesn't do real transport stuff.
98  * It just needs the eh_timed_out hook.
99  */
100 struct scsi_transport_template ata_scsi_transport_template = {
101         .eh_strategy_handler    = ata_scsi_error,
102         .eh_timed_out           = ata_scsi_timed_out,
103 };
104
105
106 static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
107                                    void (*done)(struct scsi_cmnd *))
108 {
109         ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
110         /* "Invalid field in cbd" */
111         done(cmd);
112 }
113
114 /**
115  *      ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
116  *      @sdev: SCSI device for which BIOS geometry is to be determined
117  *      @bdev: block device associated with @sdev
118  *      @capacity: capacity of SCSI device
119  *      @geom: location to which geometry will be output
120  *
121  *      Generic bios head/sector/cylinder calculator
122  *      used by sd. Most BIOSes nowadays expect a XXX/255/16  (CHS)
123  *      mapping. Some situations may arise where the disk is not
124  *      bootable if this is not used.
125  *
126  *      LOCKING:
127  *      Defined by the SCSI layer.  We don't really care.
128  *
129  *      RETURNS:
130  *      Zero.
131  */
132 int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
133                        sector_t capacity, int geom[])
134 {
135         geom[0] = 255;
136         geom[1] = 63;
137         sector_div(capacity, 255*63);
138         geom[2] = capacity;
139
140         return 0;
141 }
142
143 /**
144  *      ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
145  *      @scsidev: Device to which we are issuing command
146  *      @arg: User provided data for issuing command
147  *
148  *      LOCKING:
149  *      Defined by the SCSI layer.  We don't really care.
150  *
151  *      RETURNS:
152  *      Zero on success, negative errno on error.
153  */
154
155 int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
156 {
157         int rc = 0;
158         u8 scsi_cmd[MAX_COMMAND_SIZE];
159         u8 args[4], *argbuf = NULL;
160         int argsize = 0;
161         struct scsi_sense_hdr sshdr;
162         enum dma_data_direction data_dir;
163
164         if (arg == NULL)
165                 return -EINVAL;
166
167         if (copy_from_user(args, arg, sizeof(args)))
168                 return -EFAULT;
169
170         memset(scsi_cmd, 0, sizeof(scsi_cmd));
171
172         if (args[3]) {
173                 argsize = SECTOR_SIZE * args[3];
174                 argbuf = kmalloc(argsize, GFP_KERNEL);
175                 if (argbuf == NULL) {
176                         rc = -ENOMEM;
177                         goto error;
178                 }
179
180                 scsi_cmd[1]  = (4 << 1); /* PIO Data-in */
181                 scsi_cmd[2]  = 0x0e;     /* no off.line or cc, read from dev,
182                                             block count in sector count field */
183                 data_dir = DMA_FROM_DEVICE;
184         } else {
185                 scsi_cmd[1]  = (3 << 1); /* Non-data */
186                 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
187                 data_dir = DMA_NONE;
188         }
189
190         scsi_cmd[0] = ATA_16;
191
192         scsi_cmd[4] = args[2];
193         if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
194                 scsi_cmd[6]  = args[3];
195                 scsi_cmd[8]  = args[1];
196                 scsi_cmd[10] = 0x4f;
197                 scsi_cmd[12] = 0xc2;
198         } else {
199                 scsi_cmd[6]  = args[1];
200         }
201         scsi_cmd[14] = args[0];
202
203         /* Good values for timeout and retries?  Values below
204            from scsi_ioctl_send_command() for default case... */
205         if (scsi_execute_req(scsidev, scsi_cmd, data_dir, argbuf, argsize,
206                              &sshdr, (10*HZ), 5)) {
207                 rc = -EIO;
208                 goto error;
209         }
210
211         /* Need code to retrieve data from check condition? */
212
213         if ((argbuf)
214          && copy_to_user(arg + sizeof(args), argbuf, argsize))
215                 rc = -EFAULT;
216 error:
217         if (argbuf)
218                 kfree(argbuf);
219
220         return rc;
221 }
222
223 /**
224  *      ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
225  *      @scsidev: Device to which we are issuing command
226  *      @arg: User provided data for issuing command
227  *
228  *      LOCKING:
229  *      Defined by the SCSI layer.  We don't really care.
230  *
231  *      RETURNS:
232  *      Zero on success, negative errno on error.
233  */
234 int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
235 {
236         int rc = 0;
237         u8 scsi_cmd[MAX_COMMAND_SIZE];
238         u8 args[7];
239         struct scsi_sense_hdr sshdr;
240
241         if (arg == NULL)
242                 return -EINVAL;
243
244         if (copy_from_user(args, arg, sizeof(args)))
245                 return -EFAULT;
246
247         memset(scsi_cmd, 0, sizeof(scsi_cmd));
248         scsi_cmd[0]  = ATA_16;
249         scsi_cmd[1]  = (3 << 1); /* Non-data */
250         /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
251         scsi_cmd[4]  = args[1];
252         scsi_cmd[6]  = args[2];
253         scsi_cmd[8]  = args[3];
254         scsi_cmd[10] = args[4];
255         scsi_cmd[12] = args[5];
256         scsi_cmd[14] = args[0];
257
258         /* Good values for timeout and retries?  Values below
259            from scsi_ioctl_send_command() for default case... */
260         if (scsi_execute_req(scsidev, scsi_cmd, DMA_NONE, NULL, 0, &sshdr,
261                              (10*HZ), 5))
262                 rc = -EIO;
263
264         /* Need code to retrieve data from check condition? */
265         return rc;
266 }
267
268 int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
269 {
270         int val = -EINVAL, rc = -EINVAL;
271
272         switch (cmd) {
273         case ATA_IOC_GET_IO32:
274                 val = 0;
275                 if (copy_to_user(arg, &val, 1))
276                         return -EFAULT;
277                 return 0;
278
279         case ATA_IOC_SET_IO32:
280                 val = (unsigned long) arg;
281                 if (val != 0)
282                         return -EINVAL;
283                 return 0;
284
285         case HDIO_DRIVE_CMD:
286                 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
287                         return -EACCES;
288                 return ata_cmd_ioctl(scsidev, arg);
289
290         case HDIO_DRIVE_TASK:
291                 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
292                         return -EACCES;
293                 return ata_task_ioctl(scsidev, arg);
294
295         default:
296                 rc = -ENOTTY;
297                 break;
298         }
299
300         return rc;
301 }
302
303 /**
304  *      ata_scsi_qc_new - acquire new ata_queued_cmd reference
305  *      @dev: ATA device to which the new command is attached
306  *      @cmd: SCSI command that originated this ATA command
307  *      @done: SCSI command completion function
308  *
309  *      Obtain a reference to an unused ata_queued_cmd structure,
310  *      which is the basic libata structure representing a single
311  *      ATA command sent to the hardware.
312  *
313  *      If a command was available, fill in the SCSI-specific
314  *      portions of the structure with information on the
315  *      current command.
316  *
317  *      LOCKING:
318  *      spin_lock_irqsave(host_set lock)
319  *
320  *      RETURNS:
321  *      Command allocated, or %NULL if none available.
322  */
323 struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
324                                        struct scsi_cmnd *cmd,
325                                        void (*done)(struct scsi_cmnd *))
326 {
327         struct ata_queued_cmd *qc;
328
329         qc = ata_qc_new_init(dev);
330         if (qc) {
331                 qc->scsicmd = cmd;
332                 qc->scsidone = done;
333
334                 if (cmd->use_sg) {
335                         qc->__sg = (struct scatterlist *) cmd->request_buffer;
336                         qc->n_elem = cmd->use_sg;
337                 } else {
338                         qc->__sg = &qc->sgent;
339                         qc->n_elem = 1;
340                 }
341         } else {
342                 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
343                 done(cmd);
344         }
345
346         return qc;
347 }
348
349 /**
350  *      ata_dump_status - user friendly display of error info
351  *      @id: id of the port in question
352  *      @tf: ptr to filled out taskfile
353  *
354  *      Decode and dump the ATA error/status registers for the user so
355  *      that they have some idea what really happened at the non
356  *      make-believe layer.
357  *
358  *      LOCKING:
359  *      inherited from caller
360  */
361 void ata_dump_status(unsigned id, struct ata_taskfile *tf)
362 {
363         u8 stat = tf->command, err = tf->feature;
364
365         printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
366         if (stat & ATA_BUSY) {
367                 printk("Busy }\n");     /* Data is not valid in this case */
368         } else {
369                 if (stat & 0x40)        printk("DriveReady ");
370                 if (stat & 0x20)        printk("DeviceFault ");
371                 if (stat & 0x10)        printk("SeekComplete ");
372                 if (stat & 0x08)        printk("DataRequest ");
373                 if (stat & 0x04)        printk("CorrectedError ");
374                 if (stat & 0x02)        printk("Index ");
375                 if (stat & 0x01)        printk("Error ");
376                 printk("}\n");
377
378                 if (err) {
379                         printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
380                         if (err & 0x04)         printk("DriveStatusError ");
381                         if (err & 0x80) {
382                                 if (err & 0x04) printk("BadCRC ");
383                                 else            printk("Sector ");
384                         }
385                         if (err & 0x40)         printk("UncorrectableError ");
386                         if (err & 0x10)         printk("SectorIdNotFound ");
387                         if (err & 0x02)         printk("TrackZeroNotFound ");
388                         if (err & 0x01)         printk("AddrMarkNotFound ");
389                         printk("}\n");
390                 }
391         }
392 }
393
394 int ata_scsi_device_resume(struct scsi_device *sdev)
395 {
396         struct ata_port *ap = ata_shost_to_port(sdev->host);
397         struct ata_device *dev = &ap->device[sdev->id];
398
399         return ata_device_resume(dev);
400 }
401
402 int ata_scsi_device_suspend(struct scsi_device *sdev, pm_message_t state)
403 {
404         struct ata_port *ap = ata_shost_to_port(sdev->host);
405         struct ata_device *dev = &ap->device[sdev->id];
406
407         return ata_device_suspend(dev, state);
408 }
409
410 /**
411  *      ata_to_sense_error - convert ATA error to SCSI error
412  *      @id: ATA device number
413  *      @drv_stat: value contained in ATA status register
414  *      @drv_err: value contained in ATA error register
415  *      @sk: the sense key we'll fill out
416  *      @asc: the additional sense code we'll fill out
417  *      @ascq: the additional sense code qualifier we'll fill out
418  *
419  *      Converts an ATA error into a SCSI error.  Fill out pointers to
420  *      SK, ASC, and ASCQ bytes for later use in fixed or descriptor
421  *      format sense blocks.
422  *
423  *      LOCKING:
424  *      spin_lock_irqsave(host_set lock)
425  */
426 void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
427                         u8 *ascq)
428 {
429         int i;
430
431         /* Based on the 3ware driver translation table */
432         static const unsigned char sense_table[][4] = {
433                 /* BBD|ECC|ID|MAR */
434                 {0xd1,          ABORTED_COMMAND, 0x00, 0x00},   // Device busy                  Aborted command
435                 /* BBD|ECC|ID */
436                 {0xd0,          ABORTED_COMMAND, 0x00, 0x00},   // Device busy                  Aborted command
437                 /* ECC|MC|MARK */
438                 {0x61,          HARDWARE_ERROR, 0x00, 0x00},    // Device fault                 Hardware error
439                 /* ICRC|ABRT */         /* NB: ICRC & !ABRT is BBD */
440                 {0x84,          ABORTED_COMMAND, 0x47, 0x00},   // Data CRC error               SCSI parity error
441                 /* MC|ID|ABRT|TRK0|MARK */
442                 {0x37,          NOT_READY, 0x04, 0x00},         // Unit offline                 Not ready
443                 /* MCR|MARK */
444                 {0x09,          NOT_READY, 0x04, 0x00},         // Unrecovered disk error       Not ready
445                 /*  Bad address mark */
446                 {0x01,          MEDIUM_ERROR, 0x13, 0x00},      // Address mark not found       Address mark not found for data field
447                 /* TRK0 */
448                 {0x02,          HARDWARE_ERROR, 0x00, 0x00},    // Track 0 not found              Hardware error
449                 /* Abort & !ICRC */
450                 {0x04,          ABORTED_COMMAND, 0x00, 0x00},   // Aborted command              Aborted command
451                 /* Media change request */
452                 {0x08,          NOT_READY, 0x04, 0x00},         // Media change request   FIXME: faking offline
453                 /* SRV */
454                 {0x10,          ABORTED_COMMAND, 0x14, 0x00},   // ID not found                 Recorded entity not found
455                 /* Media change */
456                 {0x08,          NOT_READY, 0x04, 0x00},         // Media change           FIXME: faking offline
457                 /* ECC */
458                 {0x40,          MEDIUM_ERROR, 0x11, 0x04},      // Uncorrectable ECC error      Unrecovered read error
459                 /* BBD - block marked bad */
460                 {0x80,          MEDIUM_ERROR, 0x11, 0x04},      // Block marked bad               Medium error, unrecovered read error
461                 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
462         };
463         static const unsigned char stat_table[][4] = {
464                 /* Must be first because BUSY means no other bits valid */
465                 {0x80,          ABORTED_COMMAND, 0x47, 0x00},   // Busy, fake parity for now
466                 {0x20,          HARDWARE_ERROR,  0x00, 0x00},   // Device fault
467                 {0x08,          ABORTED_COMMAND, 0x47, 0x00},   // Timed out in xfer, fake parity for now
468                 {0x04,          RECOVERED_ERROR, 0x11, 0x00},   // Recovered ECC error    Medium error, recovered
469                 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
470         };
471
472         /*
473          *      Is this an error we can process/parse
474          */
475         if (drv_stat & ATA_BUSY) {
476                 drv_err = 0;    /* Ignore the err bits, they're invalid */
477         }
478
479         if (drv_err) {
480                 /* Look for drv_err */
481                 for (i = 0; sense_table[i][0] != 0xFF; i++) {
482                         /* Look for best matches first */
483                         if ((sense_table[i][0] & drv_err) ==
484                             sense_table[i][0]) {
485                                 *sk = sense_table[i][1];
486                                 *asc = sense_table[i][2];
487                                 *ascq = sense_table[i][3];
488                                 goto translate_done;
489                         }
490                 }
491                 /* No immediate match */
492                 printk(KERN_WARNING "ata%u: no sense translation for "
493                        "error 0x%02x\n", id, drv_err);
494         }
495
496         /* Fall back to interpreting status bits */
497         for (i = 0; stat_table[i][0] != 0xFF; i++) {
498                 if (stat_table[i][0] & drv_stat) {
499                         *sk = stat_table[i][1];
500                         *asc = stat_table[i][2];
501                         *ascq = stat_table[i][3];
502                         goto translate_done;
503                 }
504         }
505         /* No error?  Undecoded? */
506         printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n",
507                id, drv_stat);
508
509         /* We need a sensible error return here, which is tricky, and one
510            that won't cause people to do things like return a disk wrongly */
511         *sk = ABORTED_COMMAND;
512         *asc = 0x00;
513         *ascq = 0x00;
514
515  translate_done:
516         printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to "
517                "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err,
518                *sk, *asc, *ascq);
519         return;
520 }
521
522 /*
523  *      ata_gen_ata_desc_sense - Generate check condition sense block.
524  *      @qc: Command that completed.
525  *
526  *      This function is specific to the ATA descriptor format sense
527  *      block specified for the ATA pass through commands.  Regardless
528  *      of whether the command errored or not, return a sense
529  *      block. Copy all controller registers into the sense
530  *      block. Clear sense key, ASC & ASCQ if there is no error.
531  *
532  *      LOCKING:
533  *      spin_lock_irqsave(host_set lock)
534  */
535 void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
536 {
537         struct scsi_cmnd *cmd = qc->scsicmd;
538         struct ata_taskfile *tf = &qc->result_tf;
539         unsigned char *sb = cmd->sense_buffer;
540         unsigned char *desc = sb + 8;
541
542         memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
543
544         cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
545
546         /*
547          * Use ata_to_sense_error() to map status register bits
548          * onto sense key, asc & ascq.
549          */
550         if (qc->err_mask ||
551             tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
552                 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
553                                    &sb[1], &sb[2], &sb[3]);
554                 sb[1] &= 0x0f;
555         }
556
557         /*
558          * Sense data is current and format is descriptor.
559          */
560         sb[0] = 0x72;
561
562         desc[0] = 0x09;
563
564         /*
565          * Set length of additional sense data.
566          * Since we only populate descriptor 0, the total
567          * length is the same (fixed) length as descriptor 0.
568          */
569         desc[1] = sb[7] = 14;
570
571         /*
572          * Copy registers into sense buffer.
573          */
574         desc[2] = 0x00;
575         desc[3] = tf->feature;  /* == error reg */
576         desc[5] = tf->nsect;
577         desc[7] = tf->lbal;
578         desc[9] = tf->lbam;
579         desc[11] = tf->lbah;
580         desc[12] = tf->device;
581         desc[13] = tf->command; /* == status reg */
582
583         /*
584          * Fill in Extend bit, and the high order bytes
585          * if applicable.
586          */
587         if (tf->flags & ATA_TFLAG_LBA48) {
588                 desc[2] |= 0x01;
589                 desc[4] = tf->hob_nsect;
590                 desc[6] = tf->hob_lbal;
591                 desc[8] = tf->hob_lbam;
592                 desc[10] = tf->hob_lbah;
593         }
594 }
595
596 /**
597  *      ata_gen_fixed_sense - generate a SCSI fixed sense block
598  *      @qc: Command that we are erroring out
599  *
600  *      Leverage ata_to_sense_error() to give us the codes.  Fit our
601  *      LBA in here if there's room.
602  *
603  *      LOCKING:
604  *      inherited from caller
605  */
606 void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
607 {
608         struct scsi_cmnd *cmd = qc->scsicmd;
609         struct ata_taskfile *tf = &qc->result_tf;
610         unsigned char *sb = cmd->sense_buffer;
611
612         memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
613
614         cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
615
616         /*
617          * Use ata_to_sense_error() to map status register bits
618          * onto sense key, asc & ascq.
619          */
620         if (qc->err_mask ||
621             tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
622                 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
623                                    &sb[2], &sb[12], &sb[13]);
624                 sb[2] &= 0x0f;
625         }
626
627         sb[0] = 0x70;
628         sb[7] = 0x0a;
629
630         if (tf->flags & ATA_TFLAG_LBA48) {
631                 /* TODO: find solution for LBA48 descriptors */
632         }
633
634         else if (tf->flags & ATA_TFLAG_LBA) {
635                 /* A small (28b) LBA will fit in the 32b info field */
636                 sb[0] |= 0x80;          /* set valid bit */
637                 sb[3] = tf->device & 0x0f;
638                 sb[4] = tf->lbah;
639                 sb[5] = tf->lbam;
640                 sb[6] = tf->lbal;
641         }
642
643         else {
644                 /* TODO: C/H/S */
645         }
646 }
647
648 static void ata_scsi_sdev_config(struct scsi_device *sdev)
649 {
650         sdev->use_10_for_rw = 1;
651         sdev->use_10_for_ms = 1;
652 }
653
654 static void ata_scsi_dev_config(struct scsi_device *sdev,
655                                 struct ata_device *dev)
656 {
657         unsigned int max_sectors;
658
659         /* TODO: 2048 is an arbitrary number, not the
660          * hardware maximum.  This should be increased to
661          * 65534 when Jens Axboe's patch for dynamically
662          * determining max_sectors is merged.
663          */
664         max_sectors = ATA_MAX_SECTORS;
665         if (dev->flags & ATA_DFLAG_LBA48)
666                 max_sectors = 2048;
667         if (dev->max_sectors)
668                 max_sectors = dev->max_sectors;
669
670         blk_queue_max_sectors(sdev->request_queue, max_sectors);
671
672         /*
673          * SATA DMA transfers must be multiples of 4 byte, so
674          * we need to pad ATAPI transfers using an extra sg.
675          * Decrement max hw segments accordingly.
676          */
677         if (dev->class == ATA_DEV_ATAPI) {
678                 request_queue_t *q = sdev->request_queue;
679                 blk_queue_max_hw_segments(q, q->max_hw_segments - 1);
680         }
681 }
682
683 /**
684  *      ata_scsi_slave_config - Set SCSI device attributes
685  *      @sdev: SCSI device to examine
686  *
687  *      This is called before we actually start reading
688  *      and writing to the device, to configure certain
689  *      SCSI mid-layer behaviors.
690  *
691  *      LOCKING:
692  *      Defined by SCSI layer.  We don't really care.
693  */
694
695 int ata_scsi_slave_config(struct scsi_device *sdev)
696 {
697         ata_scsi_sdev_config(sdev);
698
699         blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
700
701         if (sdev->id < ATA_MAX_DEVICES) {
702                 struct ata_port *ap;
703                 struct ata_device *dev;
704
705                 ap = ata_shost_to_port(sdev->host);
706                 dev = &ap->device[sdev->id];
707
708                 ata_scsi_dev_config(sdev, dev);
709         }
710
711         return 0;       /* scsi layer doesn't check return value, sigh */
712 }
713
714 /**
715  *      ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
716  *      @qc: Storage for translated ATA taskfile
717  *      @scsicmd: SCSI command to translate
718  *
719  *      Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
720  *      (to start). Perhaps these commands should be preceded by
721  *      CHECK POWER MODE to see what power mode the device is already in.
722  *      [See SAT revision 5 at www.t10.org]
723  *
724  *      LOCKING:
725  *      spin_lock_irqsave(host_set lock)
726  *
727  *      RETURNS:
728  *      Zero on success, non-zero on error.
729  */
730
731 static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
732                                              const u8 *scsicmd)
733 {
734         struct ata_taskfile *tf = &qc->tf;
735
736         tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
737         tf->protocol = ATA_PROT_NODATA;
738         if (scsicmd[1] & 0x1) {
739                 ;       /* ignore IMMED bit, violates sat-r05 */
740         }
741         if (scsicmd[4] & 0x2)
742                 goto invalid_fld;       /* LOEJ bit set not supported */
743         if (((scsicmd[4] >> 4) & 0xf) != 0)
744                 goto invalid_fld;       /* power conditions not supported */
745         if (scsicmd[4] & 0x1) {
746                 tf->nsect = 1;  /* 1 sector, lba=0 */
747
748                 if (qc->dev->flags & ATA_DFLAG_LBA) {
749                         tf->flags |= ATA_TFLAG_LBA;
750
751                         tf->lbah = 0x0;
752                         tf->lbam = 0x0;
753                         tf->lbal = 0x0;
754                         tf->device |= ATA_LBA;
755                 } else {
756                         /* CHS */
757                         tf->lbal = 0x1; /* sect */
758                         tf->lbam = 0x0; /* cyl low */
759                         tf->lbah = 0x0; /* cyl high */
760                 }
761
762                 tf->command = ATA_CMD_VERIFY;   /* READ VERIFY */
763         } else {
764                 tf->nsect = 0;  /* time period value (0 implies now) */
765                 tf->command = ATA_CMD_STANDBY;
766                 /* Consider: ATA STANDBY IMMEDIATE command */
767         }
768         /*
769          * Standby and Idle condition timers could be implemented but that
770          * would require libata to implement the Power condition mode page
771          * and allow the user to change it. Changing mode pages requires
772          * MODE SELECT to be implemented.
773          */
774
775         return 0;
776
777 invalid_fld:
778         ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
779         /* "Invalid field in cbd" */
780         return 1;
781 }
782
783
784 /**
785  *      ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
786  *      @qc: Storage for translated ATA taskfile
787  *      @scsicmd: SCSI command to translate (ignored)
788  *
789  *      Sets up an ATA taskfile to issue FLUSH CACHE or
790  *      FLUSH CACHE EXT.
791  *
792  *      LOCKING:
793  *      spin_lock_irqsave(host_set lock)
794  *
795  *      RETURNS:
796  *      Zero on success, non-zero on error.
797  */
798
799 static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
800 {
801         struct ata_taskfile *tf = &qc->tf;
802
803         tf->flags |= ATA_TFLAG_DEVICE;
804         tf->protocol = ATA_PROT_NODATA;
805
806         if ((qc->dev->flags & ATA_DFLAG_LBA48) &&
807             (ata_id_has_flush_ext(qc->dev->id)))
808                 tf->command = ATA_CMD_FLUSH_EXT;
809         else
810                 tf->command = ATA_CMD_FLUSH;
811
812         return 0;
813 }
814
815 /**
816  *      scsi_6_lba_len - Get LBA and transfer length
817  *      @scsicmd: SCSI command to translate
818  *
819  *      Calculate LBA and transfer length for 6-byte commands.
820  *
821  *      RETURNS:
822  *      @plba: the LBA
823  *      @plen: the transfer length
824  */
825
826 static void scsi_6_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
827 {
828         u64 lba = 0;
829         u32 len = 0;
830
831         VPRINTK("six-byte command\n");
832
833         lba |= ((u64)scsicmd[2]) << 8;
834         lba |= ((u64)scsicmd[3]);
835
836         len |= ((u32)scsicmd[4]);
837
838         *plba = lba;
839         *plen = len;
840 }
841
842 /**
843  *      scsi_10_lba_len - Get LBA and transfer length
844  *      @scsicmd: SCSI command to translate
845  *
846  *      Calculate LBA and transfer length for 10-byte commands.
847  *
848  *      RETURNS:
849  *      @plba: the LBA
850  *      @plen: the transfer length
851  */
852
853 static void scsi_10_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
854 {
855         u64 lba = 0;
856         u32 len = 0;
857
858         VPRINTK("ten-byte command\n");
859
860         lba |= ((u64)scsicmd[2]) << 24;
861         lba |= ((u64)scsicmd[3]) << 16;
862         lba |= ((u64)scsicmd[4]) << 8;
863         lba |= ((u64)scsicmd[5]);
864
865         len |= ((u32)scsicmd[7]) << 8;
866         len |= ((u32)scsicmd[8]);
867
868         *plba = lba;
869         *plen = len;
870 }
871
872 /**
873  *      scsi_16_lba_len - Get LBA and transfer length
874  *      @scsicmd: SCSI command to translate
875  *
876  *      Calculate LBA and transfer length for 16-byte commands.
877  *
878  *      RETURNS:
879  *      @plba: the LBA
880  *      @plen: the transfer length
881  */
882
883 static void scsi_16_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
884 {
885         u64 lba = 0;
886         u32 len = 0;
887
888         VPRINTK("sixteen-byte command\n");
889
890         lba |= ((u64)scsicmd[2]) << 56;
891         lba |= ((u64)scsicmd[3]) << 48;
892         lba |= ((u64)scsicmd[4]) << 40;
893         lba |= ((u64)scsicmd[5]) << 32;
894         lba |= ((u64)scsicmd[6]) << 24;
895         lba |= ((u64)scsicmd[7]) << 16;
896         lba |= ((u64)scsicmd[8]) << 8;
897         lba |= ((u64)scsicmd[9]);
898
899         len |= ((u32)scsicmd[10]) << 24;
900         len |= ((u32)scsicmd[11]) << 16;
901         len |= ((u32)scsicmd[12]) << 8;
902         len |= ((u32)scsicmd[13]);
903
904         *plba = lba;
905         *plen = len;
906 }
907
908 /**
909  *      ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
910  *      @qc: Storage for translated ATA taskfile
911  *      @scsicmd: SCSI command to translate
912  *
913  *      Converts SCSI VERIFY command to an ATA READ VERIFY command.
914  *
915  *      LOCKING:
916  *      spin_lock_irqsave(host_set lock)
917  *
918  *      RETURNS:
919  *      Zero on success, non-zero on error.
920  */
921
922 static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
923 {
924         struct ata_taskfile *tf = &qc->tf;
925         struct ata_device *dev = qc->dev;
926         u64 dev_sectors = qc->dev->n_sectors;
927         u64 block;
928         u32 n_block;
929
930         tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
931         tf->protocol = ATA_PROT_NODATA;
932
933         if (scsicmd[0] == VERIFY)
934                 scsi_10_lba_len(scsicmd, &block, &n_block);
935         else if (scsicmd[0] == VERIFY_16)
936                 scsi_16_lba_len(scsicmd, &block, &n_block);
937         else
938                 goto invalid_fld;
939
940         if (!n_block)
941                 goto nothing_to_do;
942         if (block >= dev_sectors)
943                 goto out_of_range;
944         if ((block + n_block) > dev_sectors)
945                 goto out_of_range;
946
947         if (dev->flags & ATA_DFLAG_LBA) {
948                 tf->flags |= ATA_TFLAG_LBA;
949
950                 if (lba_28_ok(block, n_block)) {
951                         /* use LBA28 */
952                         tf->command = ATA_CMD_VERIFY;
953                         tf->device |= (block >> 24) & 0xf;
954                 } else if (lba_48_ok(block, n_block)) {
955                         if (!(dev->flags & ATA_DFLAG_LBA48))
956                                 goto out_of_range;
957
958                         /* use LBA48 */
959                         tf->flags |= ATA_TFLAG_LBA48;
960                         tf->command = ATA_CMD_VERIFY_EXT;
961
962                         tf->hob_nsect = (n_block >> 8) & 0xff;
963
964                         tf->hob_lbah = (block >> 40) & 0xff;
965                         tf->hob_lbam = (block >> 32) & 0xff;
966                         tf->hob_lbal = (block >> 24) & 0xff;
967                 } else
968                         /* request too large even for LBA48 */
969                         goto out_of_range;
970
971                 tf->nsect = n_block & 0xff;
972
973                 tf->lbah = (block >> 16) & 0xff;
974                 tf->lbam = (block >> 8) & 0xff;
975                 tf->lbal = block & 0xff;
976
977                 tf->device |= ATA_LBA;
978         } else {
979                 /* CHS */
980                 u32 sect, head, cyl, track;
981
982                 if (!lba_28_ok(block, n_block))
983                         goto out_of_range;
984
985                 /* Convert LBA to CHS */
986                 track = (u32)block / dev->sectors;
987                 cyl   = track / dev->heads;
988                 head  = track % dev->heads;
989                 sect  = (u32)block % dev->sectors + 1;
990
991                 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
992                         (u32)block, track, cyl, head, sect);
993
994                 /* Check whether the converted CHS can fit.
995                    Cylinder: 0-65535
996                    Head: 0-15
997                    Sector: 1-255*/
998                 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
999                         goto out_of_range;
1000
1001                 tf->command = ATA_CMD_VERIFY;
1002                 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1003                 tf->lbal = sect;
1004                 tf->lbam = cyl;
1005                 tf->lbah = cyl >> 8;
1006                 tf->device |= head;
1007         }
1008
1009         return 0;
1010
1011 invalid_fld:
1012         ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
1013         /* "Invalid field in cbd" */
1014         return 1;
1015
1016 out_of_range:
1017         ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
1018         /* "Logical Block Address out of range" */
1019         return 1;
1020
1021 nothing_to_do:
1022         qc->scsicmd->result = SAM_STAT_GOOD;
1023         return 1;
1024 }
1025
1026 /**
1027  *      ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1028  *      @qc: Storage for translated ATA taskfile
1029  *      @scsicmd: SCSI command to translate
1030  *
1031  *      Converts any of six SCSI read/write commands into the
1032  *      ATA counterpart, including starting sector (LBA),
1033  *      sector count, and taking into account the device's LBA48
1034  *      support.
1035  *
1036  *      Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1037  *      %WRITE_16 are currently supported.
1038  *
1039  *      LOCKING:
1040  *      spin_lock_irqsave(host_set lock)
1041  *
1042  *      RETURNS:
1043  *      Zero on success, non-zero on error.
1044  */
1045
1046 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
1047 {
1048         struct ata_taskfile *tf = &qc->tf;
1049         struct ata_device *dev = qc->dev;
1050         u64 block;
1051         u32 n_block;
1052
1053         qc->flags |= ATA_QCFLAG_IO;
1054         tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1055
1056         if (scsicmd[0] == WRITE_10 || scsicmd[0] == WRITE_6 ||
1057             scsicmd[0] == WRITE_16)
1058                 tf->flags |= ATA_TFLAG_WRITE;
1059
1060         /* Calculate the SCSI LBA, transfer length and FUA. */
1061         switch (scsicmd[0]) {
1062         case READ_10:
1063         case WRITE_10:
1064                 scsi_10_lba_len(scsicmd, &block, &n_block);
1065                 if (unlikely(scsicmd[1] & (1 << 3)))
1066                         tf->flags |= ATA_TFLAG_FUA;
1067                 break;
1068         case READ_6:
1069         case WRITE_6:
1070                 scsi_6_lba_len(scsicmd, &block, &n_block);
1071
1072                 /* for 6-byte r/w commands, transfer length 0
1073                  * means 256 blocks of data, not 0 block.
1074                  */
1075                 if (!n_block)
1076                         n_block = 256;
1077                 break;
1078         case READ_16:
1079         case WRITE_16:
1080                 scsi_16_lba_len(scsicmd, &block, &n_block);
1081                 if (unlikely(scsicmd[1] & (1 << 3)))
1082                         tf->flags |= ATA_TFLAG_FUA;
1083                 break;
1084         default:
1085                 DPRINTK("no-byte command\n");
1086                 goto invalid_fld;
1087         }
1088
1089         /* Check and compose ATA command */
1090         if (!n_block)
1091                 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1092                  * length 0 means transfer 0 block of data.
1093                  * However, for ATA R/W commands, sector count 0 means
1094                  * 256 or 65536 sectors, not 0 sectors as in SCSI.
1095                  *
1096                  * WARNING: one or two older ATA drives treat 0 as 0...
1097                  */
1098                 goto nothing_to_do;
1099
1100         if (dev->flags & ATA_DFLAG_LBA) {
1101                 tf->flags |= ATA_TFLAG_LBA;
1102
1103                 if (lba_28_ok(block, n_block)) {
1104                         /* use LBA28 */
1105                         tf->device |= (block >> 24) & 0xf;
1106                 } else if (lba_48_ok(block, n_block)) {
1107                         if (!(dev->flags & ATA_DFLAG_LBA48))
1108                                 goto out_of_range;
1109
1110                         /* use LBA48 */
1111                         tf->flags |= ATA_TFLAG_LBA48;
1112
1113                         tf->hob_nsect = (n_block >> 8) & 0xff;
1114
1115                         tf->hob_lbah = (block >> 40) & 0xff;
1116                         tf->hob_lbam = (block >> 32) & 0xff;
1117                         tf->hob_lbal = (block >> 24) & 0xff;
1118                 } else
1119                         /* request too large even for LBA48 */
1120                         goto out_of_range;
1121
1122                 if (unlikely(ata_rwcmd_protocol(qc) < 0))
1123                         goto invalid_fld;
1124
1125                 qc->nsect = n_block;
1126                 tf->nsect = n_block & 0xff;
1127
1128                 tf->lbah = (block >> 16) & 0xff;
1129                 tf->lbam = (block >> 8) & 0xff;
1130                 tf->lbal = block & 0xff;
1131
1132                 tf->device |= ATA_LBA;
1133         } else {
1134                 /* CHS */
1135                 u32 sect, head, cyl, track;
1136
1137                 /* The request -may- be too large for CHS addressing. */
1138                 if (!lba_28_ok(block, n_block))
1139                         goto out_of_range;
1140
1141                 if (unlikely(ata_rwcmd_protocol(qc) < 0))
1142                         goto invalid_fld;
1143
1144                 /* Convert LBA to CHS */
1145                 track = (u32)block / dev->sectors;
1146                 cyl   = track / dev->heads;
1147                 head  = track % dev->heads;
1148                 sect  = (u32)block % dev->sectors + 1;
1149
1150                 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1151                         (u32)block, track, cyl, head, sect);
1152
1153                 /* Check whether the converted CHS can fit.
1154                    Cylinder: 0-65535
1155                    Head: 0-15
1156                    Sector: 1-255*/
1157                 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
1158                         goto out_of_range;
1159
1160                 qc->nsect = n_block;
1161                 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1162                 tf->lbal = sect;
1163                 tf->lbam = cyl;
1164                 tf->lbah = cyl >> 8;
1165                 tf->device |= head;
1166         }
1167
1168         return 0;
1169
1170 invalid_fld:
1171         ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
1172         /* "Invalid field in cbd" */
1173         return 1;
1174
1175 out_of_range:
1176         ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
1177         /* "Logical Block Address out of range" */
1178         return 1;
1179
1180 nothing_to_do:
1181         qc->scsicmd->result = SAM_STAT_GOOD;
1182         return 1;
1183 }
1184
1185 static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
1186 {
1187         struct scsi_cmnd *cmd = qc->scsicmd;
1188         u8 *cdb = cmd->cmnd;
1189         int need_sense = (qc->err_mask != 0);
1190
1191         /* For ATA pass thru (SAT) commands, generate a sense block if
1192          * user mandated it or if there's an error.  Note that if we
1193          * generate because the user forced us to, a check condition
1194          * is generated and the ATA register values are returned
1195          * whether the command completed successfully or not. If there
1196          * was no error, SK, ASC and ASCQ will all be zero.
1197          */
1198         if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
1199             ((cdb[2] & 0x20) || need_sense)) {
1200                 ata_gen_ata_desc_sense(qc);
1201         } else {
1202                 if (!need_sense) {
1203                         cmd->result = SAM_STAT_GOOD;
1204                 } else {
1205                         /* TODO: decide which descriptor format to use
1206                          * for 48b LBA devices and call that here
1207                          * instead of the fixed desc, which is only
1208                          * good for smaller LBA (and maybe CHS?)
1209                          * devices.
1210                          */
1211                         ata_gen_fixed_sense(qc);
1212                 }
1213         }
1214
1215         if (need_sense)
1216                 ata_dump_status(qc->ap->id, &qc->result_tf);
1217
1218         qc->scsidone(cmd);
1219
1220         ata_qc_free(qc);
1221 }
1222
1223 /**
1224  *      ata_scsi_translate - Translate then issue SCSI command to ATA device
1225  *      @dev: ATA device to which the command is addressed
1226  *      @cmd: SCSI command to execute
1227  *      @done: SCSI command completion function
1228  *      @xlat_func: Actor which translates @cmd to an ATA taskfile
1229  *
1230  *      Our ->queuecommand() function has decided that the SCSI
1231  *      command issued can be directly translated into an ATA
1232  *      command, rather than handled internally.
1233  *
1234  *      This function sets up an ata_queued_cmd structure for the
1235  *      SCSI command, and sends that ata_queued_cmd to the hardware.
1236  *
1237  *      The xlat_func argument (actor) returns 0 if ready to execute
1238  *      ATA command, else 1 to finish translation. If 1 is returned
1239  *      then cmd->result (and possibly cmd->sense_buffer) are assumed
1240  *      to be set reflecting an error condition or clean (early)
1241  *      termination.
1242  *
1243  *      LOCKING:
1244  *      spin_lock_irqsave(host_set lock)
1245  */
1246
1247 static void ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
1248                                void (*done)(struct scsi_cmnd *),
1249                                ata_xlat_func_t xlat_func)
1250 {
1251         struct ata_queued_cmd *qc;
1252         u8 *scsicmd = cmd->cmnd;
1253
1254         VPRINTK("ENTER\n");
1255
1256         qc = ata_scsi_qc_new(dev, cmd, done);
1257         if (!qc)
1258                 goto err_mem;
1259
1260         /* data is present; dma-map it */
1261         if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1262             cmd->sc_data_direction == DMA_TO_DEVICE) {
1263                 if (unlikely(cmd->request_bufflen < 1)) {
1264                         printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
1265                                dev->ap->id, dev->devno);
1266                         goto err_did;
1267                 }
1268
1269                 if (cmd->use_sg)
1270                         ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
1271                 else
1272                         ata_sg_init_one(qc, cmd->request_buffer,
1273                                         cmd->request_bufflen);
1274
1275                 qc->dma_dir = cmd->sc_data_direction;
1276         }
1277
1278         qc->complete_fn = ata_scsi_qc_complete;
1279
1280         if (xlat_func(qc, scsicmd))
1281                 goto early_finish;
1282
1283         /* select device, send command to hardware */
1284         ata_qc_issue(qc);
1285
1286         VPRINTK("EXIT\n");
1287         return;
1288
1289 early_finish:
1290         ata_qc_free(qc);
1291         done(cmd);
1292         DPRINTK("EXIT - early finish (good or error)\n");
1293         return;
1294
1295 err_did:
1296         ata_qc_free(qc);
1297 err_mem:
1298         cmd->result = (DID_ERROR << 16);
1299         done(cmd);
1300         DPRINTK("EXIT - internal\n");
1301         return;
1302 }
1303
1304 /**
1305  *      ata_scsi_rbuf_get - Map response buffer.
1306  *      @cmd: SCSI command containing buffer to be mapped.
1307  *      @buf_out: Pointer to mapped area.
1308  *
1309  *      Maps buffer contained within SCSI command @cmd.
1310  *
1311  *      LOCKING:
1312  *      spin_lock_irqsave(host_set lock)
1313  *
1314  *      RETURNS:
1315  *      Length of response buffer.
1316  */
1317
1318 static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
1319 {
1320         u8 *buf;
1321         unsigned int buflen;
1322
1323         if (cmd->use_sg) {
1324                 struct scatterlist *sg;
1325
1326                 sg = (struct scatterlist *) cmd->request_buffer;
1327                 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
1328                 buflen = sg->length;
1329         } else {
1330                 buf = cmd->request_buffer;
1331                 buflen = cmd->request_bufflen;
1332         }
1333
1334         *buf_out = buf;
1335         return buflen;
1336 }
1337
1338 /**
1339  *      ata_scsi_rbuf_put - Unmap response buffer.
1340  *      @cmd: SCSI command containing buffer to be unmapped.
1341  *      @buf: buffer to unmap
1342  *
1343  *      Unmaps response buffer contained within @cmd.
1344  *
1345  *      LOCKING:
1346  *      spin_lock_irqsave(host_set lock)
1347  */
1348
1349 static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
1350 {
1351         if (cmd->use_sg) {
1352                 struct scatterlist *sg;
1353
1354                 sg = (struct scatterlist *) cmd->request_buffer;
1355                 kunmap_atomic(buf - sg->offset, KM_USER0);
1356         }
1357 }
1358
1359 /**
1360  *      ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1361  *      @args: device IDENTIFY data / SCSI command of interest.
1362  *      @actor: Callback hook for desired SCSI command simulator
1363  *
1364  *      Takes care of the hard work of simulating a SCSI command...
1365  *      Mapping the response buffer, calling the command's handler,
1366  *      and handling the handler's return value.  This return value
1367  *      indicates whether the handler wishes the SCSI command to be
1368  *      completed successfully (0), or not (in which case cmd->result
1369  *      and sense buffer are assumed to be set).
1370  *
1371  *      LOCKING:
1372  *      spin_lock_irqsave(host_set lock)
1373  */
1374
1375 void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
1376                         unsigned int (*actor) (struct ata_scsi_args *args,
1377                                            u8 *rbuf, unsigned int buflen))
1378 {
1379         u8 *rbuf;
1380         unsigned int buflen, rc;
1381         struct scsi_cmnd *cmd = args->cmd;
1382
1383         buflen = ata_scsi_rbuf_get(cmd, &rbuf);
1384         memset(rbuf, 0, buflen);
1385         rc = actor(args, rbuf, buflen);
1386         ata_scsi_rbuf_put(cmd, rbuf);
1387
1388         if (rc == 0)
1389                 cmd->result = SAM_STAT_GOOD;
1390         args->done(cmd);
1391 }
1392
1393 /**
1394  *      ata_scsiop_inq_std - Simulate INQUIRY command
1395  *      @args: device IDENTIFY data / SCSI command of interest.
1396  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1397  *      @buflen: Response buffer length.
1398  *
1399  *      Returns standard device identification data associated
1400  *      with non-VPD INQUIRY command output.
1401  *
1402  *      LOCKING:
1403  *      spin_lock_irqsave(host_set lock)
1404  */
1405
1406 unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
1407                                unsigned int buflen)
1408 {
1409         u8 hdr[] = {
1410                 TYPE_DISK,
1411                 0,
1412                 0x5,    /* claim SPC-3 version compatibility */
1413                 2,
1414                 95 - 4
1415         };
1416
1417         /* set scsi removeable (RMB) bit per ata bit */
1418         if (ata_id_removeable(args->id))
1419                 hdr[1] |= (1 << 7);
1420
1421         VPRINTK("ENTER\n");
1422
1423         memcpy(rbuf, hdr, sizeof(hdr));
1424
1425         if (buflen > 35) {
1426                 memcpy(&rbuf[8], "ATA     ", 8);
1427                 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
1428                 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
1429                 if (rbuf[32] == 0 || rbuf[32] == ' ')
1430                         memcpy(&rbuf[32], "n/a ", 4);
1431         }
1432
1433         if (buflen > 63) {
1434                 const u8 versions[] = {
1435                         0x60,   /* SAM-3 (no version claimed) */
1436
1437                         0x03,
1438                         0x20,   /* SBC-2 (no version claimed) */
1439
1440                         0x02,
1441                         0x60    /* SPC-3 (no version claimed) */
1442                 };
1443
1444                 memcpy(rbuf + 59, versions, sizeof(versions));
1445         }
1446
1447         return 0;
1448 }
1449
1450 /**
1451  *      ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
1452  *      @args: device IDENTIFY data / SCSI command of interest.
1453  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1454  *      @buflen: Response buffer length.
1455  *
1456  *      Returns list of inquiry VPD pages available.
1457  *
1458  *      LOCKING:
1459  *      spin_lock_irqsave(host_set lock)
1460  */
1461
1462 unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
1463                               unsigned int buflen)
1464 {
1465         const u8 pages[] = {
1466                 0x00,   /* page 0x00, this page */
1467                 0x80,   /* page 0x80, unit serial no page */
1468                 0x83    /* page 0x83, device ident page */
1469         };
1470         rbuf[3] = sizeof(pages);        /* number of supported VPD pages */
1471
1472         if (buflen > 6)
1473                 memcpy(rbuf + 4, pages, sizeof(pages));
1474
1475         return 0;
1476 }
1477
1478 /**
1479  *      ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
1480  *      @args: device IDENTIFY data / SCSI command of interest.
1481  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1482  *      @buflen: Response buffer length.
1483  *
1484  *      Returns ATA device serial number.
1485  *
1486  *      LOCKING:
1487  *      spin_lock_irqsave(host_set lock)
1488  */
1489
1490 unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
1491                               unsigned int buflen)
1492 {
1493         const u8 hdr[] = {
1494                 0,
1495                 0x80,                   /* this page code */
1496                 0,
1497                 ATA_SERNO_LEN,          /* page len */
1498         };
1499         memcpy(rbuf, hdr, sizeof(hdr));
1500
1501         if (buflen > (ATA_SERNO_LEN + 4 - 1))
1502                 ata_id_string(args->id, (unsigned char *) &rbuf[4],
1503                               ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1504
1505         return 0;
1506 }
1507
1508 /**
1509  *      ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
1510  *      @args: device IDENTIFY data / SCSI command of interest.
1511  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1512  *      @buflen: Response buffer length.
1513  *
1514  *      Yields two logical unit device identification designators:
1515  *       - vendor specific ASCII containing the ATA serial number
1516  *       - SAT defined "t10 vendor id based" containing ASCII vendor
1517  *         name ("ATA     "), model and serial numbers.
1518  *
1519  *      LOCKING:
1520  *      spin_lock_irqsave(host_set lock)
1521  */
1522
1523 unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
1524                               unsigned int buflen)
1525 {
1526         int num;
1527         const int sat_model_serial_desc_len = 68;
1528         const int ata_model_byte_len = 40;
1529
1530         rbuf[1] = 0x83;                 /* this page code */
1531         num = 4;
1532
1533         if (buflen > (ATA_SERNO_LEN + num + 3)) {
1534                 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
1535                 rbuf[num + 0] = 2;
1536                 rbuf[num + 3] = ATA_SERNO_LEN;
1537                 num += 4;
1538                 ata_id_string(args->id, (unsigned char *) rbuf + num,
1539                               ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1540                 num += ATA_SERNO_LEN;
1541         }
1542         if (buflen > (sat_model_serial_desc_len + num + 3)) {
1543                 /* SAT defined lu model and serial numbers descriptor */
1544                 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
1545                 rbuf[num + 0] = 2;
1546                 rbuf[num + 1] = 1;
1547                 rbuf[num + 3] = sat_model_serial_desc_len;
1548                 num += 4;
1549                 memcpy(rbuf + num, "ATA     ", 8);
1550                 num += 8;
1551                 ata_id_string(args->id, (unsigned char *) rbuf + num,
1552                               ATA_ID_PROD_OFS, ata_model_byte_len);
1553                 num += ata_model_byte_len;
1554                 ata_id_string(args->id, (unsigned char *) rbuf + num,
1555                               ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1556                 num += ATA_SERNO_LEN;
1557         }
1558         rbuf[3] = num - 4;    /* page len (assume less than 256 bytes) */
1559         return 0;
1560 }
1561
1562 /**
1563  *      ata_scsiop_noop - Command handler that simply returns success.
1564  *      @args: device IDENTIFY data / SCSI command of interest.
1565  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1566  *      @buflen: Response buffer length.
1567  *
1568  *      No operation.  Simply returns success to caller, to indicate
1569  *      that the caller should successfully complete this SCSI command.
1570  *
1571  *      LOCKING:
1572  *      spin_lock_irqsave(host_set lock)
1573  */
1574
1575 unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
1576                             unsigned int buflen)
1577 {
1578         VPRINTK("ENTER\n");
1579         return 0;
1580 }
1581
1582 /**
1583  *      ata_msense_push - Push data onto MODE SENSE data output buffer
1584  *      @ptr_io: (input/output) Location to store more output data
1585  *      @last: End of output data buffer
1586  *      @buf: Pointer to BLOB being added to output buffer
1587  *      @buflen: Length of BLOB
1588  *
1589  *      Store MODE SENSE data on an output buffer.
1590  *
1591  *      LOCKING:
1592  *      None.
1593  */
1594
1595 static void ata_msense_push(u8 **ptr_io, const u8 *last,
1596                             const u8 *buf, unsigned int buflen)
1597 {
1598         u8 *ptr = *ptr_io;
1599
1600         if ((ptr + buflen - 1) > last)
1601                 return;
1602
1603         memcpy(ptr, buf, buflen);
1604
1605         ptr += buflen;
1606
1607         *ptr_io = ptr;
1608 }
1609
1610 /**
1611  *      ata_msense_caching - Simulate MODE SENSE caching info page
1612  *      @id: device IDENTIFY data
1613  *      @ptr_io: (input/output) Location to store more output data
1614  *      @last: End of output data buffer
1615  *
1616  *      Generate a caching info page, which conditionally indicates
1617  *      write caching to the SCSI layer, depending on device
1618  *      capabilities.
1619  *
1620  *      LOCKING:
1621  *      None.
1622  */
1623
1624 static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1625                                        const u8 *last)
1626 {
1627         u8 page[CACHE_MPAGE_LEN];
1628
1629         memcpy(page, def_cache_mpage, sizeof(page));
1630         if (ata_id_wcache_enabled(id))
1631                 page[2] |= (1 << 2);    /* write cache enable */
1632         if (!ata_id_rahead_enabled(id))
1633                 page[12] |= (1 << 5);   /* disable read ahead */
1634
1635         ata_msense_push(ptr_io, last, page, sizeof(page));
1636         return sizeof(page);
1637 }
1638
1639 /**
1640  *      ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1641  *      @dev: Device associated with this MODE SENSE command
1642  *      @ptr_io: (input/output) Location to store more output data
1643  *      @last: End of output data buffer
1644  *
1645  *      Generate a generic MODE SENSE control mode page.
1646  *
1647  *      LOCKING:
1648  *      None.
1649  */
1650
1651 static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1652 {
1653         ata_msense_push(ptr_io, last, def_control_mpage,
1654                         sizeof(def_control_mpage));
1655         return sizeof(def_control_mpage);
1656 }
1657
1658 /**
1659  *      ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1660  *      @dev: Device associated with this MODE SENSE command
1661  *      @ptr_io: (input/output) Location to store more output data
1662  *      @last: End of output data buffer
1663  *
1664  *      Generate a generic MODE SENSE r/w error recovery page.
1665  *
1666  *      LOCKING:
1667  *      None.
1668  */
1669
1670 static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1671 {
1672
1673         ata_msense_push(ptr_io, last, def_rw_recovery_mpage,
1674                         sizeof(def_rw_recovery_mpage));
1675         return sizeof(def_rw_recovery_mpage);
1676 }
1677
1678 /*
1679  * We can turn this into a real blacklist if it's needed, for now just
1680  * blacklist any Maxtor BANC1G10 revision firmware
1681  */
1682 static int ata_dev_supports_fua(u16 *id)
1683 {
1684         unsigned char model[41], fw[9];
1685
1686         if (!libata_fua)
1687                 return 0;
1688         if (!ata_id_has_fua(id))
1689                 return 0;
1690
1691         ata_id_c_string(id, model, ATA_ID_PROD_OFS, sizeof(model));
1692         ata_id_c_string(id, fw, ATA_ID_FW_REV_OFS, sizeof(fw));
1693
1694         if (strcmp(model, "Maxtor"))
1695                 return 1;
1696         if (strcmp(fw, "BANC1G10"))
1697                 return 1;
1698
1699         return 0; /* blacklisted */
1700 }
1701
1702 /**
1703  *      ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1704  *      @args: device IDENTIFY data / SCSI command of interest.
1705  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1706  *      @buflen: Response buffer length.
1707  *
1708  *      Simulate MODE SENSE commands. Assume this is invoked for direct
1709  *      access devices (e.g. disks) only. There should be no block
1710  *      descriptor for other device types.
1711  *
1712  *      LOCKING:
1713  *      spin_lock_irqsave(host_set lock)
1714  */
1715
1716 unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1717                                   unsigned int buflen)
1718 {
1719         struct ata_device *dev = args->dev;
1720         u8 *scsicmd = args->cmd->cmnd, *p, *last;
1721         const u8 sat_blk_desc[] = {
1722                 0, 0, 0, 0,     /* number of blocks: sat unspecified */
1723                 0,
1724                 0, 0x2, 0x0     /* block length: 512 bytes */
1725         };
1726         u8 pg, spg;
1727         unsigned int ebd, page_control, six_byte, output_len, alloc_len, minlen;
1728         u8 dpofua;
1729
1730         VPRINTK("ENTER\n");
1731
1732         six_byte = (scsicmd[0] == MODE_SENSE);
1733         ebd = !(scsicmd[1] & 0x8);      /* dbd bit inverted == edb */
1734         /*
1735          * LLBA bit in msense(10) ignored (compliant)
1736          */
1737
1738         page_control = scsicmd[2] >> 6;
1739         switch (page_control) {
1740         case 0: /* current */
1741                 break;  /* supported */
1742         case 3: /* saved */
1743                 goto saving_not_supp;
1744         case 1: /* changeable */
1745         case 2: /* defaults */
1746         default:
1747                 goto invalid_fld;
1748         }
1749
1750         if (six_byte) {
1751                 output_len = 4 + (ebd ? 8 : 0);
1752                 alloc_len = scsicmd[4];
1753         } else {
1754                 output_len = 8 + (ebd ? 8 : 0);
1755                 alloc_len = (scsicmd[7] << 8) + scsicmd[8];
1756         }
1757         minlen = (alloc_len < buflen) ? alloc_len : buflen;
1758
1759         p = rbuf + output_len;
1760         last = rbuf + minlen - 1;
1761
1762         pg = scsicmd[2] & 0x3f;
1763         spg = scsicmd[3];
1764         /*
1765          * No mode subpages supported (yet) but asking for _all_
1766          * subpages may be valid
1767          */
1768         if (spg && (spg != ALL_SUB_MPAGES))
1769                 goto invalid_fld;
1770
1771         switch(pg) {
1772         case RW_RECOVERY_MPAGE:
1773                 output_len += ata_msense_rw_recovery(&p, last);
1774                 break;
1775
1776         case CACHE_MPAGE:
1777                 output_len += ata_msense_caching(args->id, &p, last);
1778                 break;
1779
1780         case CONTROL_MPAGE: {
1781                 output_len += ata_msense_ctl_mode(&p, last);
1782                 break;
1783                 }
1784
1785         case ALL_MPAGES:
1786                 output_len += ata_msense_rw_recovery(&p, last);
1787                 output_len += ata_msense_caching(args->id, &p, last);
1788                 output_len += ata_msense_ctl_mode(&p, last);
1789                 break;
1790
1791         default:                /* invalid page code */
1792                 goto invalid_fld;
1793         }
1794
1795         if (minlen < 1)
1796                 return 0;
1797
1798         dpofua = 0;
1799         if (ata_dev_supports_fua(args->id) && dev->flags & ATA_DFLAG_LBA48 &&
1800             (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
1801                 dpofua = 1 << 4;
1802
1803         if (six_byte) {
1804                 output_len--;
1805                 rbuf[0] = output_len;
1806                 if (minlen > 2)
1807                         rbuf[2] |= dpofua;
1808                 if (ebd) {
1809                         if (minlen > 3)
1810                                 rbuf[3] = sizeof(sat_blk_desc);
1811                         if (minlen > 11)
1812                                 memcpy(rbuf + 4, sat_blk_desc,
1813                                        sizeof(sat_blk_desc));
1814                 }
1815         } else {
1816                 output_len -= 2;
1817                 rbuf[0] = output_len >> 8;
1818                 if (minlen > 1)
1819                         rbuf[1] = output_len;
1820                 if (minlen > 3)
1821                         rbuf[3] |= dpofua;
1822                 if (ebd) {
1823                         if (minlen > 7)
1824                                 rbuf[7] = sizeof(sat_blk_desc);
1825                         if (minlen > 15)
1826                                 memcpy(rbuf + 8, sat_blk_desc,
1827                                        sizeof(sat_blk_desc));
1828                 }
1829         }
1830         return 0;
1831
1832 invalid_fld:
1833         ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
1834         /* "Invalid field in cbd" */
1835         return 1;
1836
1837 saving_not_supp:
1838         ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
1839          /* "Saving parameters not supported" */
1840         return 1;
1841 }
1842
1843 /**
1844  *      ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
1845  *      @args: device IDENTIFY data / SCSI command of interest.
1846  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1847  *      @buflen: Response buffer length.
1848  *
1849  *      Simulate READ CAPACITY commands.
1850  *
1851  *      LOCKING:
1852  *      spin_lock_irqsave(host_set lock)
1853  */
1854
1855 unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
1856                                 unsigned int buflen)
1857 {
1858         u64 n_sectors;
1859         u32 tmp;
1860
1861         VPRINTK("ENTER\n");
1862
1863         if (ata_id_has_lba(args->id)) {
1864                 if (ata_id_has_lba48(args->id))
1865                         n_sectors = ata_id_u64(args->id, 100);
1866                 else
1867                         n_sectors = ata_id_u32(args->id, 60);
1868         } else {
1869                 /* CHS default translation */
1870                 n_sectors = args->id[1] * args->id[3] * args->id[6];
1871
1872                 if (ata_id_current_chs_valid(args->id))
1873                         /* CHS current translation */
1874                         n_sectors = ata_id_u32(args->id, 57);
1875         }
1876
1877         n_sectors--;            /* ATA TotalUserSectors - 1 */
1878
1879         if (args->cmd->cmnd[0] == READ_CAPACITY) {
1880                 if( n_sectors >= 0xffffffffULL )
1881                         tmp = 0xffffffff ;  /* Return max count on overflow */
1882                 else
1883                         tmp = n_sectors ;
1884
1885                 /* sector count, 32-bit */
1886                 rbuf[0] = tmp >> (8 * 3);
1887                 rbuf[1] = tmp >> (8 * 2);
1888                 rbuf[2] = tmp >> (8 * 1);
1889                 rbuf[3] = tmp;
1890
1891                 /* sector size */
1892                 tmp = ATA_SECT_SIZE;
1893                 rbuf[6] = tmp >> 8;
1894                 rbuf[7] = tmp;
1895
1896         } else {
1897                 /* sector count, 64-bit */
1898                 tmp = n_sectors >> (8 * 4);
1899                 rbuf[2] = tmp >> (8 * 3);
1900                 rbuf[3] = tmp >> (8 * 2);
1901                 rbuf[4] = tmp >> (8 * 1);
1902                 rbuf[5] = tmp;
1903                 tmp = n_sectors;
1904                 rbuf[6] = tmp >> (8 * 3);
1905                 rbuf[7] = tmp >> (8 * 2);
1906                 rbuf[8] = tmp >> (8 * 1);
1907                 rbuf[9] = tmp;
1908
1909                 /* sector size */
1910                 tmp = ATA_SECT_SIZE;
1911                 rbuf[12] = tmp >> 8;
1912                 rbuf[13] = tmp;
1913         }
1914
1915         return 0;
1916 }
1917
1918 /**
1919  *      ata_scsiop_report_luns - Simulate REPORT LUNS command
1920  *      @args: device IDENTIFY data / SCSI command of interest.
1921  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1922  *      @buflen: Response buffer length.
1923  *
1924  *      Simulate REPORT LUNS command.
1925  *
1926  *      LOCKING:
1927  *      spin_lock_irqsave(host_set lock)
1928  */
1929
1930 unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1931                                    unsigned int buflen)
1932 {
1933         VPRINTK("ENTER\n");
1934         rbuf[3] = 8;    /* just one lun, LUN 0, size 8 bytes */
1935
1936         return 0;
1937 }
1938
1939 /**
1940  *      ata_scsi_set_sense - Set SCSI sense data and status
1941  *      @cmd: SCSI request to be handled
1942  *      @sk: SCSI-defined sense key
1943  *      @asc: SCSI-defined additional sense code
1944  *      @ascq: SCSI-defined additional sense code qualifier
1945  *
1946  *      Helper function that builds a valid fixed format, current
1947  *      response code and the given sense key (sk), additional sense
1948  *      code (asc) and additional sense code qualifier (ascq) with
1949  *      a SCSI command status of %SAM_STAT_CHECK_CONDITION and
1950  *      DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
1951  *
1952  *      LOCKING:
1953  *      Not required
1954  */
1955
1956 void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
1957 {
1958         cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
1959
1960         cmd->sense_buffer[0] = 0x70;    /* fixed format, current */
1961         cmd->sense_buffer[2] = sk;
1962         cmd->sense_buffer[7] = 18 - 8;  /* additional sense length */
1963         cmd->sense_buffer[12] = asc;
1964         cmd->sense_buffer[13] = ascq;
1965 }
1966
1967 /**
1968  *      ata_scsi_badcmd - End a SCSI request with an error
1969  *      @cmd: SCSI request to be handled
1970  *      @done: SCSI command completion function
1971  *      @asc: SCSI-defined additional sense code
1972  *      @ascq: SCSI-defined additional sense code qualifier
1973  *
1974  *      Helper function that completes a SCSI command with
1975  *      %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
1976  *      and the specified additional sense codes.
1977  *
1978  *      LOCKING:
1979  *      spin_lock_irqsave(host_set lock)
1980  */
1981
1982 void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
1983 {
1984         DPRINTK("ENTER\n");
1985         ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
1986
1987         done(cmd);
1988 }
1989
1990 static void atapi_sense_complete(struct ata_queued_cmd *qc)
1991 {
1992         if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
1993                 /* FIXME: not quite right; we don't want the
1994                  * translation of taskfile registers into
1995                  * a sense descriptors, since that's only
1996                  * correct for ATA, not ATAPI
1997                  */
1998                 ata_gen_ata_desc_sense(qc);
1999         }
2000
2001         qc->scsidone(qc->scsicmd);
2002         ata_qc_free(qc);
2003 }
2004
2005 /* is it pointless to prefer PIO for "safety reasons"? */
2006 static inline int ata_pio_use_silly(struct ata_port *ap)
2007 {
2008         return (ap->flags & ATA_FLAG_PIO_DMA);
2009 }
2010
2011 static void atapi_request_sense(struct ata_queued_cmd *qc)
2012 {
2013         struct ata_port *ap = qc->ap;
2014         struct scsi_cmnd *cmd = qc->scsicmd;
2015
2016         DPRINTK("ATAPI request sense\n");
2017
2018         /* FIXME: is this needed? */
2019         memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
2020
2021         ap->ops->tf_read(ap, &qc->tf);
2022
2023         /* fill these in, for the case where they are -not- overwritten */
2024         cmd->sense_buffer[0] = 0x70;
2025         cmd->sense_buffer[2] = qc->tf.feature >> 4;
2026
2027         ata_qc_reinit(qc);
2028
2029         ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
2030         qc->dma_dir = DMA_FROM_DEVICE;
2031
2032         memset(&qc->cdb, 0, qc->dev->cdb_len);
2033         qc->cdb[0] = REQUEST_SENSE;
2034         qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2035
2036         qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2037         qc->tf.command = ATA_CMD_PACKET;
2038
2039         if (ata_pio_use_silly(ap)) {
2040                 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
2041                 qc->tf.feature |= ATAPI_PKT_DMA;
2042         } else {
2043                 qc->tf.protocol = ATA_PROT_ATAPI;
2044                 qc->tf.lbam = (8 * 1024) & 0xff;
2045                 qc->tf.lbah = (8 * 1024) >> 8;
2046         }
2047         qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2048
2049         qc->complete_fn = atapi_sense_complete;
2050
2051         ata_qc_issue(qc);
2052
2053         DPRINTK("EXIT\n");
2054 }
2055
2056 static void atapi_qc_complete(struct ata_queued_cmd *qc)
2057 {
2058         struct scsi_cmnd *cmd = qc->scsicmd;
2059         unsigned int err_mask = qc->err_mask;
2060
2061         VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
2062
2063         if (unlikely(err_mask & AC_ERR_DEV)) {
2064                 cmd->result = SAM_STAT_CHECK_CONDITION;
2065                 atapi_request_sense(qc);
2066                 return;
2067         } else if (unlikely(err_mask)) {
2068                 /* FIXME: not quite right; we don't want the
2069                  * translation of taskfile registers into
2070                  * a sense descriptors, since that's only
2071                  * correct for ATA, not ATAPI
2072                  */
2073                 ata_gen_ata_desc_sense(qc);
2074         } else {
2075                 u8 *scsicmd = cmd->cmnd;
2076
2077                 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
2078                         u8 *buf = NULL;
2079                         unsigned int buflen;
2080
2081                         buflen = ata_scsi_rbuf_get(cmd, &buf);
2082
2083         /* ATAPI devices typically report zero for their SCSI version,
2084          * and sometimes deviate from the spec WRT response data
2085          * format.  If SCSI version is reported as zero like normal,
2086          * then we make the following fixups:  1) Fake MMC-5 version,
2087          * to indicate to the Linux scsi midlayer this is a modern
2088          * device.  2) Ensure response data format / ATAPI information
2089          * are always correct.
2090          */
2091                         if (buf[2] == 0) {
2092                                 buf[2] = 0x5;
2093                                 buf[3] = 0x32;
2094                         }
2095
2096                         ata_scsi_rbuf_put(cmd, buf);
2097                 }
2098
2099                 cmd->result = SAM_STAT_GOOD;
2100         }
2101
2102         qc->scsidone(cmd);
2103         ata_qc_free(qc);
2104 }
2105 /**
2106  *      atapi_xlat - Initialize PACKET taskfile
2107  *      @qc: command structure to be initialized
2108  *      @scsicmd: SCSI CDB associated with this PACKET command
2109  *
2110  *      LOCKING:
2111  *      spin_lock_irqsave(host_set lock)
2112  *
2113  *      RETURNS:
2114  *      Zero on success, non-zero on failure.
2115  */
2116
2117 static unsigned int atapi_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
2118 {
2119         struct scsi_cmnd *cmd = qc->scsicmd;
2120         struct ata_device *dev = qc->dev;
2121         int using_pio = (dev->flags & ATA_DFLAG_PIO);
2122         int nodata = (cmd->sc_data_direction == DMA_NONE);
2123
2124         if (!using_pio)
2125                 /* Check whether ATAPI DMA is safe */
2126                 if (ata_check_atapi_dma(qc))
2127                         using_pio = 1;
2128
2129         memcpy(&qc->cdb, scsicmd, dev->cdb_len);
2130
2131         qc->complete_fn = atapi_qc_complete;
2132
2133         qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2134         if (cmd->sc_data_direction == DMA_TO_DEVICE) {
2135                 qc->tf.flags |= ATA_TFLAG_WRITE;
2136                 DPRINTK("direction: write\n");
2137         }
2138
2139         qc->tf.command = ATA_CMD_PACKET;
2140
2141         /* no data, or PIO data xfer */
2142         if (using_pio || nodata) {
2143                 if (nodata)
2144                         qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
2145                 else
2146                         qc->tf.protocol = ATA_PROT_ATAPI;
2147                 qc->tf.lbam = (8 * 1024) & 0xff;
2148                 qc->tf.lbah = (8 * 1024) >> 8;
2149         }
2150
2151         /* DMA data xfer */
2152         else {
2153                 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
2154                 qc->tf.feature |= ATAPI_PKT_DMA;
2155
2156                 if (atapi_dmadir && (cmd->sc_data_direction != DMA_TO_DEVICE))
2157                         /* some SATA bridges need us to indicate data xfer direction */
2158                         qc->tf.feature |= ATAPI_DMADIR;
2159         }
2160
2161         qc->nbytes = cmd->bufflen;
2162
2163         return 0;
2164 }
2165
2166 /**
2167  *      ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2168  *      @ap: ATA port to which the device is attached
2169  *      @scsidev: SCSI device from which we derive the ATA device
2170  *
2171  *      Given various information provided in struct scsi_cmnd,
2172  *      map that onto an ATA bus, and using that mapping
2173  *      determine which ata_device is associated with the
2174  *      SCSI command to be sent.
2175  *
2176  *      LOCKING:
2177  *      spin_lock_irqsave(host_set lock)
2178  *
2179  *      RETURNS:
2180  *      Associated ATA device, or %NULL if not found.
2181  */
2182
2183 static struct ata_device *
2184 ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
2185 {
2186         struct ata_device *dev;
2187
2188         /* skip commands not addressed to targets we simulate */
2189         if (likely(scsidev->id < ATA_MAX_DEVICES))
2190                 dev = &ap->device[scsidev->id];
2191         else
2192                 return NULL;
2193
2194         if (unlikely((scsidev->channel != 0) ||
2195                      (scsidev->lun != 0)))
2196                 return NULL;
2197
2198         if (unlikely(!ata_dev_enabled(dev)))
2199                 return NULL;
2200
2201         if (!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) {
2202                 if (unlikely(dev->class == ATA_DEV_ATAPI)) {
2203                         printk(KERN_WARNING "ata%u(%u): WARNING: ATAPI is %s, device ignored.\n",
2204                                ap->id, dev->devno, atapi_enabled ? "not supported with this driver" : "disabled");
2205                         return NULL;
2206                 }
2207         }
2208
2209         return dev;
2210 }
2211
2212 /*
2213  *      ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2214  *      @byte1: Byte 1 from pass-thru CDB.
2215  *
2216  *      RETURNS:
2217  *      ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2218  */
2219 static u8
2220 ata_scsi_map_proto(u8 byte1)
2221 {
2222         switch((byte1 & 0x1e) >> 1) {
2223                 case 3:         /* Non-data */
2224                         return ATA_PROT_NODATA;
2225
2226                 case 6:         /* DMA */
2227                         return ATA_PROT_DMA;
2228
2229                 case 4:         /* PIO Data-in */
2230                 case 5:         /* PIO Data-out */
2231                         return ATA_PROT_PIO;
2232
2233                 case 10:        /* Device Reset */
2234                 case 0:         /* Hard Reset */
2235                 case 1:         /* SRST */
2236                 case 2:         /* Bus Idle */
2237                 case 7:         /* Packet */
2238                 case 8:         /* DMA Queued */
2239                 case 9:         /* Device Diagnostic */
2240                 case 11:        /* UDMA Data-in */
2241                 case 12:        /* UDMA Data-Out */
2242                 case 13:        /* FPDMA */
2243                 default:        /* Reserved */
2244                         break;
2245         }
2246
2247         return ATA_PROT_UNKNOWN;
2248 }
2249
2250 /**
2251  *      ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
2252  *      @qc: command structure to be initialized
2253  *      @scsicmd: SCSI command to convert
2254  *
2255  *      Handles either 12 or 16-byte versions of the CDB.
2256  *
2257  *      RETURNS:
2258  *      Zero on success, non-zero on failure.
2259  */
2260 static unsigned int
2261 ata_scsi_pass_thru(struct ata_queued_cmd *qc, const u8 *scsicmd)
2262 {
2263         struct ata_taskfile *tf = &(qc->tf);
2264         struct scsi_cmnd *cmd = qc->scsicmd;
2265
2266         if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
2267                 goto invalid_fld;
2268
2269         if (scsicmd[1] & 0xe0)
2270                 /* PIO multi not supported yet */
2271                 goto invalid_fld;
2272
2273         /*
2274          * 12 and 16 byte CDBs use different offsets to
2275          * provide the various register values.
2276          */
2277         if (scsicmd[0] == ATA_16) {
2278                 /*
2279                  * 16-byte CDB - may contain extended commands.
2280                  *
2281                  * If that is the case, copy the upper byte register values.
2282                  */
2283                 if (scsicmd[1] & 0x01) {
2284                         tf->hob_feature = scsicmd[3];
2285                         tf->hob_nsect = scsicmd[5];
2286                         tf->hob_lbal = scsicmd[7];
2287                         tf->hob_lbam = scsicmd[9];
2288                         tf->hob_lbah = scsicmd[11];
2289                         tf->flags |= ATA_TFLAG_LBA48;
2290                 } else
2291                         tf->flags &= ~ATA_TFLAG_LBA48;
2292
2293                 /*
2294                  * Always copy low byte, device and command registers.
2295                  */
2296                 tf->feature = scsicmd[4];
2297                 tf->nsect = scsicmd[6];
2298                 tf->lbal = scsicmd[8];
2299                 tf->lbam = scsicmd[10];
2300                 tf->lbah = scsicmd[12];
2301                 tf->device = scsicmd[13];
2302                 tf->command = scsicmd[14];
2303         } else {
2304                 /*
2305                  * 12-byte CDB - incapable of extended commands.
2306                  */
2307                 tf->flags &= ~ATA_TFLAG_LBA48;
2308
2309                 tf->feature = scsicmd[3];
2310                 tf->nsect = scsicmd[4];
2311                 tf->lbal = scsicmd[5];
2312                 tf->lbam = scsicmd[6];
2313                 tf->lbah = scsicmd[7];
2314                 tf->device = scsicmd[8];
2315                 tf->command = scsicmd[9];
2316         }
2317         /*
2318          * If slave is possible, enforce correct master/slave bit
2319         */
2320         if (qc->ap->flags & ATA_FLAG_SLAVE_POSS)
2321                 tf->device = qc->dev->devno ?
2322                         tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
2323
2324         /*
2325          * Filter SET_FEATURES - XFER MODE command -- otherwise,
2326          * SET_FEATURES - XFER MODE must be preceded/succeeded
2327          * by an update to hardware-specific registers for each
2328          * controller (i.e. the reason for ->set_piomode(),
2329          * ->set_dmamode(), and ->post_set_mode() hooks).
2330          */
2331         if ((tf->command == ATA_CMD_SET_FEATURES)
2332          && (tf->feature == SETFEATURES_XFER))
2333                 goto invalid_fld;
2334
2335         /*
2336          * Set flags so that all registers will be written,
2337          * and pass on write indication (used for PIO/DMA
2338          * setup.)
2339          */
2340         tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
2341
2342         if (cmd->sc_data_direction == DMA_TO_DEVICE)
2343                 tf->flags |= ATA_TFLAG_WRITE;
2344
2345         /*
2346          * Set transfer length.
2347          *
2348          * TODO: find out if we need to do more here to
2349          *       cover scatter/gather case.
2350          */
2351         qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
2352
2353         /* request result TF */
2354         qc->flags |= ATA_QCFLAG_RESULT_TF;
2355
2356         return 0;
2357
2358  invalid_fld:
2359         ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x00);
2360         /* "Invalid field in cdb" */
2361         return 1;
2362 }
2363
2364 /**
2365  *      ata_get_xlat_func - check if SCSI to ATA translation is possible
2366  *      @dev: ATA device
2367  *      @cmd: SCSI command opcode to consider
2368  *
2369  *      Look up the SCSI command given, and determine whether the
2370  *      SCSI command is to be translated or simulated.
2371  *
2372  *      RETURNS:
2373  *      Pointer to translation function if possible, %NULL if not.
2374  */
2375
2376 static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
2377 {
2378         switch (cmd) {
2379         case READ_6:
2380         case READ_10:
2381         case READ_16:
2382
2383         case WRITE_6:
2384         case WRITE_10:
2385         case WRITE_16:
2386                 return ata_scsi_rw_xlat;
2387
2388         case SYNCHRONIZE_CACHE:
2389                 if (ata_try_flush_cache(dev))
2390                         return ata_scsi_flush_xlat;
2391                 break;
2392
2393         case VERIFY:
2394         case VERIFY_16:
2395                 return ata_scsi_verify_xlat;
2396
2397         case ATA_12:
2398         case ATA_16:
2399                 return ata_scsi_pass_thru;
2400
2401         case START_STOP:
2402                 return ata_scsi_start_stop_xlat;
2403         }
2404
2405         return NULL;
2406 }
2407
2408 /**
2409  *      ata_scsi_dump_cdb - dump SCSI command contents to dmesg
2410  *      @ap: ATA port to which the command was being sent
2411  *      @cmd: SCSI command to dump
2412  *
2413  *      Prints the contents of a SCSI command via printk().
2414  */
2415
2416 static inline void ata_scsi_dump_cdb(struct ata_port *ap,
2417                                      struct scsi_cmnd *cmd)
2418 {
2419 #ifdef ATA_DEBUG
2420         struct scsi_device *scsidev = cmd->device;
2421         u8 *scsicmd = cmd->cmnd;
2422
2423         DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
2424                 ap->id,
2425                 scsidev->channel, scsidev->id, scsidev->lun,
2426                 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
2427                 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
2428                 scsicmd[8]);
2429 #endif
2430 }
2431
2432 static inline void __ata_scsi_queuecmd(struct scsi_cmnd *cmd,
2433                                        void (*done)(struct scsi_cmnd *),
2434                                        struct ata_device *dev)
2435 {
2436         if (dev->class == ATA_DEV_ATA) {
2437                 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
2438                                                               cmd->cmnd[0]);
2439
2440                 if (xlat_func)
2441                         ata_scsi_translate(dev, cmd, done, xlat_func);
2442                 else
2443                         ata_scsi_simulate(dev, cmd, done);
2444         } else
2445                 ata_scsi_translate(dev, cmd, done, atapi_xlat);
2446 }
2447
2448 /**
2449  *      ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
2450  *      @cmd: SCSI command to be sent
2451  *      @done: Completion function, called when command is complete
2452  *
2453  *      In some cases, this function translates SCSI commands into
2454  *      ATA taskfiles, and queues the taskfiles to be sent to
2455  *      hardware.  In other cases, this function simulates a
2456  *      SCSI device by evaluating and responding to certain
2457  *      SCSI commands.  This creates the overall effect of
2458  *      ATA and ATAPI devices appearing as SCSI devices.
2459  *
2460  *      LOCKING:
2461  *      Releases scsi-layer-held lock, and obtains host_set lock.
2462  *
2463  *      RETURNS:
2464  *      Zero.
2465  */
2466
2467 int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
2468 {
2469         struct ata_port *ap;
2470         struct ata_device *dev;
2471         struct scsi_device *scsidev = cmd->device;
2472         struct Scsi_Host *shost = scsidev->host;
2473
2474         ap = ata_shost_to_port(shost);
2475
2476         spin_unlock(shost->host_lock);
2477         spin_lock(&ap->host_set->lock);
2478
2479         ata_scsi_dump_cdb(ap, cmd);
2480
2481         dev = ata_scsi_find_dev(ap, scsidev);
2482         if (likely(dev))
2483                 __ata_scsi_queuecmd(cmd, done, dev);
2484         else {
2485                 cmd->result = (DID_BAD_TARGET << 16);
2486                 done(cmd);
2487         }
2488
2489         spin_unlock(&ap->host_set->lock);
2490         spin_lock(shost->host_lock);
2491         return 0;
2492 }
2493
2494 /**
2495  *      ata_scsi_simulate - simulate SCSI command on ATA device
2496  *      @dev: the target device
2497  *      @cmd: SCSI command being sent to device.
2498  *      @done: SCSI command completion function.
2499  *
2500  *      Interprets and directly executes a select list of SCSI commands
2501  *      that can be handled internally.
2502  *
2503  *      LOCKING:
2504  *      spin_lock_irqsave(host_set lock)
2505  */
2506
2507 void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
2508                       void (*done)(struct scsi_cmnd *))
2509 {
2510         struct ata_scsi_args args;
2511         const u8 *scsicmd = cmd->cmnd;
2512
2513         args.dev = dev;
2514         args.id = dev->id;
2515         args.cmd = cmd;
2516         args.done = done;
2517
2518         switch(scsicmd[0]) {
2519                 /* no-op's, complete with success */
2520                 case SYNCHRONIZE_CACHE:
2521                 case REZERO_UNIT:
2522                 case SEEK_6:
2523                 case SEEK_10:
2524                 case TEST_UNIT_READY:
2525                 case FORMAT_UNIT:               /* FIXME: correct? */
2526                 case SEND_DIAGNOSTIC:           /* FIXME: correct? */
2527                         ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
2528                         break;
2529
2530                 case INQUIRY:
2531                         if (scsicmd[1] & 2)                /* is CmdDt set?  */
2532                                 ata_scsi_invalid_field(cmd, done);
2533                         else if ((scsicmd[1] & 1) == 0)    /* is EVPD clear? */
2534                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
2535                         else if (scsicmd[2] == 0x00)
2536                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
2537                         else if (scsicmd[2] == 0x80)
2538                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
2539                         else if (scsicmd[2] == 0x83)
2540                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
2541                         else
2542                                 ata_scsi_invalid_field(cmd, done);
2543                         break;
2544
2545                 case MODE_SENSE:
2546                 case MODE_SENSE_10:
2547                         ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
2548                         break;
2549
2550                 case MODE_SELECT:       /* unconditionally return */
2551                 case MODE_SELECT_10:    /* bad-field-in-cdb */
2552                         ata_scsi_invalid_field(cmd, done);
2553                         break;
2554
2555                 case READ_CAPACITY:
2556                         ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2557                         break;
2558
2559                 case SERVICE_ACTION_IN:
2560                         if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
2561                                 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2562                         else
2563                                 ata_scsi_invalid_field(cmd, done);
2564                         break;
2565
2566                 case REPORT_LUNS:
2567                         ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
2568                         break;
2569
2570                 /* mandatory commands we haven't implemented yet */
2571                 case REQUEST_SENSE:
2572
2573                 /* all other commands */
2574                 default:
2575                         ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
2576                         /* "Invalid command operation code" */
2577                         done(cmd);
2578                         break;
2579         }
2580 }
2581
2582 void ata_scsi_scan_host(struct ata_port *ap)
2583 {
2584         struct ata_device *dev;
2585         unsigned int i;
2586
2587         if (ap->flags & ATA_FLAG_DISABLED)
2588                 return;
2589
2590         for (i = 0; i < ATA_MAX_DEVICES; i++) {
2591                 dev = &ap->device[i];
2592
2593                 if (ata_dev_enabled(dev))
2594                         scsi_scan_target(&ap->host->shost_gendev, 0, i, 0, 0);
2595         }
2596 }
2597