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