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