fed311e81c50440a9fbb9eb1b8cadd97bc4258f3
[linux-2.6.git] / drivers / scsi / libata-scsi.c
1 /*
2    libata-scsi.c - helper library for ATA
3
4    Copyright 2003-2004 Red Hat, Inc.  All rights reserved.
5    Copyright 2003-2004 Jeff Garzik
6
7    The contents of this file are subject to the Open
8    Software License version 1.1 that can be found at
9    http://www.opensource.org/licenses/osl-1.1.txt and is included herein
10    by reference.
11
12    Alternatively, the contents of this file may be used under the terms
13    of the GNU General Public License version 2 (the "GPL") as distributed
14    in the kernel source COPYING file, in which case the provisions of
15    the GPL are applicable instead of the above.  If you wish to allow
16    the use of your version of this file only under the terms of the
17    GPL and not to allow others to use your version of this file under
18    the OSL, indicate your decision by deleting the provisions above and
19    replace them with the notice and other provisions required by the GPL.
20    If you do not delete the provisions above, a recipient may use your
21    version of this file under either the OSL or the GPL.
22
23  */
24
25 #include <linux/kernel.h>
26 #include <linux/blkdev.h>
27 #include <linux/spinlock.h>
28 #include <scsi/scsi.h>
29 #include "scsi.h"
30 #include <scsi/scsi_host.h>
31 #include <linux/libata.h>
32
33 #include "libata.h"
34
35 typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, u8 *scsicmd);
36 static void ata_scsi_simulate(struct ata_port *ap, struct ata_device *dev,
37                               struct scsi_cmnd *cmd,
38                               void (*done)(struct scsi_cmnd *));
39
40
41 /**
42  *      ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
43  *      @sdev: SCSI device for which BIOS geometry is to be determined
44  *      @bdev: block device associated with @sdev
45  *      @capacity: capacity of SCSI device
46  *      @geom: location to which geometry will be output
47  *
48  *      Generic bios head/sector/cylinder calculator
49  *      used by sd. Most BIOSes nowadays expect a XXX/255/16  (CHS)
50  *      mapping. Some situations may arise where the disk is not
51  *      bootable if this is not used.
52  *
53  *      LOCKING:
54  *      Defined by the SCSI layer.  We don't really care.
55  *
56  *      RETURNS:
57  *      Zero.
58  */
59 int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
60                        sector_t capacity, int geom[])
61 {
62         geom[0] = 255;
63         geom[1] = 63;
64         sector_div(capacity, 255*63);
65         geom[2] = capacity;
66
67         return 0;
68 }
69
70
71 /**
72  *      ata_scsi_qc_new - acquire new ata_queued_cmd reference
73  *      @ap: ATA port to which the new command is attached
74  *      @dev: ATA device to which the new command is attached
75  *      @cmd: SCSI command that originated this ATA command
76  *      @done: SCSI command completion function
77  *
78  *      Obtain a reference to an unused ata_queued_cmd structure,
79  *      which is the basic libata structure representing a single
80  *      ATA command sent to the hardware.
81  *
82  *      If a command was available, fill in the SCSI-specific
83  *      portions of the structure with information on the
84  *      current command.
85  *
86  *      LOCKING:
87  *      spin_lock_irqsave(host_set lock)
88  *
89  *      RETURNS:
90  *      Command allocated, or %NULL if none available.
91  */
92 struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
93                                        struct ata_device *dev,
94                                        struct scsi_cmnd *cmd,
95                                        void (*done)(struct scsi_cmnd *))
96 {
97         struct ata_queued_cmd *qc;
98
99         qc = ata_qc_new_init(ap, dev);
100         if (qc) {
101                 qc->scsicmd = cmd;
102                 qc->scsidone = done;
103
104                 if (cmd->use_sg) {
105                         qc->sg = (struct scatterlist *) cmd->request_buffer;
106                         qc->n_elem = cmd->use_sg;
107                 } else {
108                         qc->sg = &qc->sgent;
109                         qc->n_elem = 1;
110                 }
111         } else {
112                 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
113                 done(cmd);
114         }
115
116         return qc;
117 }
118
119 /**
120  *      ata_to_sense_error - convert ATA error to SCSI error
121  *      @qc: Command that we are erroring out
122  *
123  *      Converts an ATA error into a SCSI error.
124  *
125  *      Right now, this routine is laughably primitive.  We
126  *      don't even examine what ATA told us, we just look at
127  *      the command data direction, and return a fatal SCSI
128  *      sense error based on that.
129  *
130  *      LOCKING:
131  *      spin_lock_irqsave(host_set lock)
132  */
133
134 void ata_to_sense_error(struct ata_queued_cmd *qc)
135 {
136         struct scsi_cmnd *cmd = qc->scsicmd;
137
138         cmd->result = SAM_STAT_CHECK_CONDITION;
139
140         cmd->sense_buffer[0] = 0x70;
141         cmd->sense_buffer[2] = MEDIUM_ERROR;
142         cmd->sense_buffer[7] = 14 - 8;  /* addnl. sense len. FIXME: correct? */
143
144         /* additional-sense-code[-qualifier] */
145         if (cmd->sc_data_direction == SCSI_DATA_READ) {
146                 cmd->sense_buffer[12] = 0x11; /* "unrecovered read error" */
147                 cmd->sense_buffer[13] = 0x04;
148         } else {
149                 cmd->sense_buffer[12] = 0x0C; /* "write error -             */
150                 cmd->sense_buffer[13] = 0x02; /*  auto-reallocation failed" */
151         }
152 }
153
154 /**
155  *      ata_scsi_slave_config - Set SCSI device attributes
156  *      @sdev: SCSI device to examine
157  *
158  *      This is called before we actually start reading
159  *      and writing to the device, to configure certain
160  *      SCSI mid-layer behaviors.
161  *
162  *      LOCKING:
163  *      Defined by SCSI layer.  We don't really care.
164  */
165
166 int ata_scsi_slave_config(struct scsi_device *sdev)
167 {
168         sdev->use_10_for_rw = 1;
169         sdev->use_10_for_ms = 1;
170
171         blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
172
173         if (sdev->id < ATA_MAX_DEVICES) {
174                 struct ata_port *ap;
175                 struct ata_device *dev;
176
177                 ap = (struct ata_port *) &sdev->host->hostdata[0];
178                 dev = &ap->device[sdev->id];
179
180                 /* TODO: 1024 is an arbitrary number, not the
181                  * hardware maximum.  This should be increased to
182                  * 65534 when Jens Axboe's patch for dynamically
183                  * determining max_sectors is merged.
184                  */
185                 if (dev->flags & ATA_DFLAG_LBA48) {
186                         sdev->host->max_sectors = 2048;
187                         blk_queue_max_sectors(sdev->request_queue, 2048);
188                 }
189         }
190
191         return 0;       /* scsi layer doesn't check return value, sigh */
192 }
193
194 /**
195  *      ata_scsi_error - SCSI layer error handler callback
196  *      @host: SCSI host on which error occurred
197  *
198  *      Handles SCSI-layer-thrown error events.
199  *
200  *      LOCKING:
201  *      Inherited from SCSI layer (none, can sleep)
202  *
203  *      RETURNS:
204  *      Zero.
205  */
206
207 int ata_scsi_error(struct Scsi_Host *host)
208 {
209         struct ata_port *ap;
210
211         DPRINTK("ENTER\n");
212
213         ap = (struct ata_port *) &host->hostdata[0];
214         ap->ops->eng_timeout(ap);
215
216         DPRINTK("EXIT\n");
217         return 0;
218 }
219
220 /**
221  *      ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
222  *      @qc: Storage for translated ATA taskfile
223  *      @scsicmd: SCSI command to translate
224  *
225  *      Converts any of six SCSI read/write commands into the
226  *      ATA counterpart, including starting sector (LBA),
227  *      sector count, and taking into account the device's LBA48
228  *      support.
229  *
230  *      Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
231  *      %WRITE_16 are currently supported.
232  *
233  *      LOCKING:
234  *      spin_lock_irqsave(host_set lock)
235  *
236  *      RETURNS:
237  *      Zero on success, non-zero on error.
238  */
239
240 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
241 {
242         struct ata_taskfile *tf = &qc->tf;
243         unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
244
245         tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
246         tf->hob_nsect = 0;
247         tf->hob_lbal = 0;
248         tf->hob_lbam = 0;
249         tf->hob_lbah = 0;
250         tf->protocol = qc->dev->xfer_protocol;
251         tf->device |= ATA_LBA;
252
253         if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
254             scsicmd[0] == READ_16) {
255                 tf->command = qc->dev->read_cmd;
256         } else {
257                 tf->command = qc->dev->write_cmd;
258                 tf->flags |= ATA_TFLAG_WRITE;
259         }
260
261         if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) {
262                 if (lba48) {
263                         tf->hob_nsect = scsicmd[7];
264                         tf->hob_lbal = scsicmd[2];
265
266                         qc->nsect = ((unsigned int)scsicmd[7] << 8) |
267                                         scsicmd[8];
268                 } else {
269                         /* if we don't support LBA48 addressing, the request
270                          * -may- be too large. */
271                         if ((scsicmd[2] & 0xf0) || scsicmd[7])
272                                 return 1;
273
274                         /* stores LBA27:24 in lower 4 bits of device reg */
275                         tf->device |= scsicmd[2];
276
277                         qc->nsect = scsicmd[8];
278                 }
279
280                 tf->nsect = scsicmd[8];
281                 tf->lbal = scsicmd[5];
282                 tf->lbam = scsicmd[4];
283                 tf->lbah = scsicmd[3];
284
285                 VPRINTK("ten-byte command\n");
286                 return 0;
287         }
288
289         if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) {
290                 qc->nsect = tf->nsect = scsicmd[4];
291                 tf->lbal = scsicmd[3];
292                 tf->lbam = scsicmd[2];
293                 tf->lbah = scsicmd[1] & 0x1f; /* mask out reserved bits */
294
295                 VPRINTK("six-byte command\n");
296                 return 0;
297         }
298
299         if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) {
300                 /* rule out impossible LBAs and sector counts */
301                 if (scsicmd[2] || scsicmd[3] || scsicmd[10] || scsicmd[11])
302                         return 1;
303
304                 if (lba48) {
305                         tf->hob_nsect = scsicmd[12];
306                         tf->hob_lbal = scsicmd[6];
307                         tf->hob_lbam = scsicmd[5];
308                         tf->hob_lbah = scsicmd[4];
309
310                         qc->nsect = ((unsigned int)scsicmd[12] << 8) |
311                                         scsicmd[13];
312                 } else {
313                         /* once again, filter out impossible non-zero values */
314                         if (scsicmd[4] || scsicmd[5] || scsicmd[12] ||
315                             (scsicmd[6] & 0xf0))
316                                 return 1;
317
318                         /* stores LBA27:24 in lower 4 bits of device reg */
319                         tf->device |= scsicmd[2];
320
321                         qc->nsect = scsicmd[13];
322                 }
323
324                 tf->nsect = scsicmd[13];
325                 tf->lbal = scsicmd[9];
326                 tf->lbam = scsicmd[8];
327                 tf->lbah = scsicmd[7];
328
329                 VPRINTK("sixteen-byte command\n");
330                 return 0;
331         }
332
333         DPRINTK("no-byte command\n");
334         return 1;
335 }
336
337 /**
338  *      ata_scsi_translate - Translate then issue SCSI command to ATA device
339  *      @ap: ATA port to which the command is addressed
340  *      @dev: ATA device to which the command is addressed
341  *      @cmd: SCSI command to execute
342  *      @done: SCSI command completion function
343  *      @xlat_func: Actor which translates @cmd to an ATA taskfile
344  *
345  *      Our ->queuecommand() function has decided that the SCSI
346  *      command issued can be directly translated into an ATA
347  *      command, rather than handled internally.
348  *
349  *      This function sets up an ata_queued_cmd structure for the
350  *      SCSI command, and sends that ata_queued_cmd to the hardware.
351  *
352  *      LOCKING:
353  *      spin_lock_irqsave(host_set lock)
354  */
355
356 static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
357                               struct scsi_cmnd *cmd,
358                               void (*done)(struct scsi_cmnd *),
359                               ata_xlat_func_t xlat_func)
360 {
361         struct ata_queued_cmd *qc;
362         u8 *scsicmd = cmd->cmnd;
363
364         VPRINTK("ENTER\n");
365
366         qc = ata_scsi_qc_new(ap, dev, cmd, done);
367         if (!qc)
368                 return;
369
370         if (cmd->sc_data_direction == SCSI_DATA_READ ||
371             cmd->sc_data_direction == SCSI_DATA_WRITE) {
372                 if (unlikely(cmd->request_bufflen < 1)) {
373                         printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
374                                ap->id, dev->devno);
375                         goto err_out;
376                 }
377
378                 qc->flags |= ATA_QCFLAG_SG; /* data is present; dma-map it */
379         }
380
381         if (xlat_func(qc, scsicmd))
382                 goto err_out;
383
384         /* select device, send command to hardware */
385         if (ata_qc_issue(qc))
386                 goto err_out;
387
388         VPRINTK("EXIT\n");
389         return;
390
391 err_out:
392         ata_bad_cdb(cmd, done);
393         DPRINTK("EXIT - badcmd\n");
394 }
395
396 /**
397  *      ata_scsi_rbuf_get - Map response buffer.
398  *      @cmd: SCSI command containing buffer to be mapped.
399  *      @buf_out: Pointer to mapped area.
400  *
401  *      Maps buffer contained within SCSI command @cmd.
402  *
403  *      LOCKING:
404  *      spin_lock_irqsave(host_set lock)
405  *
406  *      RETURNS:
407  *      Length of response buffer.
408  */
409
410 static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
411 {
412         u8 *buf;
413         unsigned int buflen;
414
415         if (cmd->use_sg) {
416                 struct scatterlist *sg;
417
418                 sg = (struct scatterlist *) cmd->request_buffer;
419                 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
420                 buflen = sg->length;
421         } else {
422                 buf = cmd->request_buffer;
423                 buflen = cmd->request_bufflen;
424         }
425
426         memset(buf, 0, buflen);
427         *buf_out = buf;
428         return buflen;
429 }
430
431 /**
432  *      ata_scsi_rbuf_put - Unmap response buffer.
433  *      @cmd: SCSI command containing buffer to be unmapped.
434  *
435  *      Unmaps response buffer contained within @cmd.
436  *
437  *      LOCKING:
438  *      spin_lock_irqsave(host_set lock)
439  */
440
441 static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd)
442 {
443         if (cmd->use_sg) {
444                 struct scatterlist *sg;
445
446                 sg = (struct scatterlist *) cmd->request_buffer;
447                 kunmap_atomic(sg->page, KM_USER0);
448         }
449 }
450
451 /**
452  *      ata_scsi_rbuf_fill - wrapper for SCSI command simulators
453  *      @args: Port / device / SCSI command of interest.
454  *      @actor: Callback hook for desired SCSI command simulator
455  *
456  *      Takes care of the hard work of simulating a SCSI command...
457  *      Mapping the response buffer, calling the command's handler,
458  *      and handling the handler's return value.  This return value
459  *      indicates whether the handler wishes the SCSI command to be
460  *      completed successfully, or not.
461  *
462  *      LOCKING:
463  *      spin_lock_irqsave(host_set lock)
464  */
465
466 void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
467                         unsigned int (*actor) (struct ata_scsi_args *args,
468                                            u8 *rbuf, unsigned int buflen))
469 {
470         u8 *rbuf;
471         unsigned int buflen, rc;
472         struct scsi_cmnd *cmd = args->cmd;
473
474         buflen = ata_scsi_rbuf_get(cmd, &rbuf);
475         rc = actor(args, rbuf, buflen);
476         ata_scsi_rbuf_put(cmd);
477
478         if (rc)
479                 ata_bad_cdb(cmd, args->done);
480         else {
481                 cmd->result = SAM_STAT_GOOD;
482                 args->done(cmd);
483         }
484 }
485
486 /**
487  *      ata_scsiop_inq_std - Simulate INQUIRY command
488  *      @args: Port / device / SCSI command of interest.
489  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
490  *      @buflen: Response buffer length.
491  *
492  *      Returns standard device identification data associated
493  *      with non-EVPD INQUIRY command output.
494  *
495  *      LOCKING:
496  *      spin_lock_irqsave(host_set lock)
497  */
498
499 unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
500                                unsigned int buflen)
501 {
502         struct ata_device *dev = args->dev;
503
504         u8 hdr[] = {
505                 TYPE_DISK,
506                 0,
507                 0x5,    /* claim SPC-3 version compatibility */
508                 2,
509                 96 - 4
510         };
511
512         /* set scsi removeable (RMB) bit per ata bit */
513         if (ata_id_removeable(dev))
514                 hdr[1] |= (1 << 7);
515
516         VPRINTK("ENTER\n");
517
518         memcpy(rbuf, hdr, sizeof(hdr));
519
520         if (buflen > 36) {
521                 memcpy(&rbuf[8], "ATA     ", 8);
522                 ata_dev_id_string(dev, &rbuf[16], ATA_ID_PROD_OFS, 16);
523                 ata_dev_id_string(dev, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
524                 if (rbuf[32] == 0 || rbuf[32] == ' ')
525                         memcpy(&rbuf[32], "n/a ", 4);
526         }
527
528         if (buflen > 63) {
529                 const u8 versions[] = {
530                         0x60,   /* SAM-3 (no version claimed) */
531
532                         0x03,
533                         0x20,   /* SBC-2 (no version claimed) */
534
535                         0x02,
536                         0x60    /* SPC-3 (no version claimed) */
537                 };
538
539                 memcpy(rbuf + 59, versions, sizeof(versions));
540         }
541
542         return 0;
543 }
544
545 /**
546  *      ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
547  *      @args: Port / device / SCSI command of interest.
548  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
549  *      @buflen: Response buffer length.
550  *
551  *      Returns list of inquiry EVPD pages available.
552  *
553  *      LOCKING:
554  *      spin_lock_irqsave(host_set lock)
555  */
556
557 unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
558                               unsigned int buflen)
559 {
560         const u8 pages[] = {
561                 0x00,   /* page 0x00, this page */
562                 0x80,   /* page 0x80, unit serial no page */
563                 0x83    /* page 0x83, device ident page */
564         };
565         rbuf[3] = sizeof(pages);        /* number of supported EVPD pages */
566
567         if (buflen > 6)
568                 memcpy(rbuf + 4, pages, sizeof(pages));
569
570         return 0;
571 }
572
573 /**
574  *      ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
575  *      @args: Port / device / SCSI command of interest.
576  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
577  *      @buflen: Response buffer length.
578  *
579  *      Returns ATA device serial number.
580  *
581  *      LOCKING:
582  *      spin_lock_irqsave(host_set lock)
583  */
584
585 unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
586                               unsigned int buflen)
587 {
588         const u8 hdr[] = {
589                 0,
590                 0x80,                   /* this page code */
591                 0,
592                 ATA_SERNO_LEN,          /* page len */
593         };
594         memcpy(rbuf, hdr, sizeof(hdr));
595
596         if (buflen > (ATA_SERNO_LEN + 4))
597                 ata_dev_id_string(args->dev, (unsigned char *) &rbuf[4],
598                                   ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
599
600         return 0;
601 }
602
603 static const char *inq_83_str = "Linux ATA-SCSI simulator";
604
605 /**
606  *      ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
607  *      @args: Port / device / SCSI command of interest.
608  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
609  *      @buflen: Response buffer length.
610  *
611  *      Returns device identification.  Currently hardcoded to
612  *      return "Linux ATA-SCSI simulator".
613  *
614  *      LOCKING:
615  *      spin_lock_irqsave(host_set lock)
616  */
617
618 unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
619                               unsigned int buflen)
620 {
621         rbuf[1] = 0x83;                 /* this page code */
622         rbuf[3] = 4 + strlen(inq_83_str);       /* page len */
623
624         /* our one and only identification descriptor (vendor-specific) */
625         if (buflen > (strlen(inq_83_str) + 4 + 4)) {
626                 rbuf[4 + 0] = 2;        /* code set: ASCII */
627                 rbuf[4 + 3] = strlen(inq_83_str);
628                 memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
629         }
630
631         return 0;
632 }
633
634 /**
635  *      ata_scsiop_noop -
636  *      @args: Port / device / SCSI command of interest.
637  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
638  *      @buflen: Response buffer length.
639  *
640  *      No operation.  Simply returns success to caller, to indicate
641  *      that the caller should successfully complete this SCSI command.
642  *
643  *      LOCKING:
644  *      spin_lock_irqsave(host_set lock)
645  */
646
647 unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
648                             unsigned int buflen)
649 {
650         VPRINTK("ENTER\n");
651         return 0;
652 }
653
654 /**
655  *      ata_msense_push - Push data onto MODE SENSE data output buffer
656  *      @ptr_io: (input/output) Location to store more output data
657  *      @last: End of output data buffer
658  *      @buf: Pointer to BLOB being added to output buffer
659  *      @buflen: Length of BLOB
660  *
661  *      Store MODE SENSE data on an output buffer.
662  *
663  *      LOCKING:
664  *      None.
665  */
666
667 static void ata_msense_push(u8 **ptr_io, const u8 *last,
668                             const u8 *buf, unsigned int buflen)
669 {
670         u8 *ptr = *ptr_io;
671
672         if ((ptr + buflen - 1) > last)
673                 return;
674
675         memcpy(ptr, buf, buflen);
676
677         ptr += buflen;
678
679         *ptr_io = ptr;
680 }
681
682 /**
683  *      ata_msense_caching - Simulate MODE SENSE caching info page
684  *      @dev: Device associated with this MODE SENSE command
685  *      @ptr_io: (input/output) Location to store more output data
686  *      @last: End of output data buffer
687  *
688  *      Generate a caching info page, which conditionally indicates
689  *      write caching to the SCSI layer, depending on device
690  *      capabilities.
691  *
692  *      LOCKING:
693  *      None.
694  */
695
696 static unsigned int ata_msense_caching(struct ata_device *dev, u8 **ptr_io,
697                                        const u8 *last)
698 {
699         u8 page[] = {
700                 0x8,                            /* page code */
701                 0x12,                           /* page length */
702                 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,   /* 10 zeroes */
703                 0, 0, 0, 0, 0, 0, 0, 0          /* 8 zeroes */
704         };
705
706         if (ata_id_wcache_enabled(dev))
707                 page[2] |= (1 << 2);    /* write cache enable */
708         if (!ata_id_rahead_enabled(dev))
709                 page[12] |= (1 << 5);   /* disable read ahead */
710
711         ata_msense_push(ptr_io, last, page, sizeof(page));
712         return sizeof(page);
713 }
714
715 /**
716  *      ata_msense_ctl_mode - Simulate MODE SENSE control mode page
717  *      @dev: Device associated with this MODE SENSE command
718  *      @ptr_io: (input/output) Location to store more output data
719  *      @last: End of output data buffer
720  *
721  *      Generate a generic MODE SENSE control mode page.
722  *
723  *      LOCKING:
724  *      None.
725  */
726
727 static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
728 {
729         const u8 page[] = {0xa, 0xa, 2, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
730
731         ata_msense_push(ptr_io, last, page, sizeof(page));
732         return sizeof(page);
733 }
734
735 /**
736  *      ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
737  *      @dev: Device associated with this MODE SENSE command
738  *      @ptr_io: (input/output) Location to store more output data
739  *      @last: End of output data buffer
740  *
741  *      Generate a generic MODE SENSE r/w error recovery page.
742  *
743  *      LOCKING:
744  *      None.
745  */
746
747 static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
748 {
749         const u8 page[] = {
750                 0x1,                      /* page code */
751                 0xa,                      /* page length */
752                 (1 << 7) | (1 << 6),      /* note auto r/w reallocation */
753                 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
754         };
755
756         ata_msense_push(ptr_io, last, page, sizeof(page));
757         return sizeof(page);
758 }
759
760 /**
761  *      ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
762  *      @args: Port / device / SCSI command of interest.
763  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
764  *      @buflen: Response buffer length.
765  *
766  *      Simulate MODE SENSE commands.
767  *
768  *      LOCKING:
769  *      spin_lock_irqsave(host_set lock)
770  */
771
772 unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
773                                   unsigned int buflen)
774 {
775         u8 *scsicmd = args->cmd->cmnd, *p, *last;
776         struct ata_device *dev = args->dev;
777         unsigned int page_control, six_byte, output_len;
778
779         VPRINTK("ENTER\n");
780
781         six_byte = (scsicmd[0] == MODE_SENSE);
782
783         /* we only support saved and current values (which we treat
784          * in the same manner)
785          */
786         page_control = scsicmd[2] >> 6;
787         if ((page_control != 0) && (page_control != 3))
788                 return 1;
789
790         if (six_byte)
791                 output_len = 4;
792         else
793                 output_len = 8;
794
795         p = rbuf + output_len;
796         last = rbuf + buflen - 1;
797
798         switch(scsicmd[2] & 0x3f) {
799         case 0x01:              /* r/w error recovery */
800                 output_len += ata_msense_rw_recovery(&p, last);
801                 break;
802
803         case 0x08:              /* caching */
804                 output_len += ata_msense_caching(dev, &p, last);
805                 break;
806
807         case 0x0a: {            /* control mode */
808                 output_len += ata_msense_ctl_mode(&p, last);
809                 break;
810                 }
811
812         case 0x3f:              /* all pages */
813                 output_len += ata_msense_rw_recovery(&p, last);
814                 output_len += ata_msense_caching(dev, &p, last);
815                 output_len += ata_msense_ctl_mode(&p, last);
816                 break;
817
818         default:                /* invalid page code */
819                 return 1;
820         }
821
822         if (six_byte) {
823                 output_len--;
824                 rbuf[0] = output_len;
825         } else {
826                 output_len -= 2;
827                 rbuf[0] = output_len >> 8;
828                 rbuf[1] = output_len;
829         }
830
831         return 0;
832 }
833
834 /**
835  *      ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
836  *      @args: Port / device / SCSI command of interest.
837  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
838  *      @buflen: Response buffer length.
839  *
840  *      Simulate READ CAPACITY commands.
841  *
842  *      LOCKING:
843  *      spin_lock_irqsave(host_set lock)
844  */
845
846 unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
847                                 unsigned int buflen)
848 {
849         u64 n_sectors = args->dev->n_sectors;
850         u32 tmp;
851
852         VPRINTK("ENTER\n");
853
854         n_sectors--;            /* ATA TotalUserSectors - 1 */
855
856         tmp = n_sectors;        /* note: truncates, if lba48 */
857         if (args->cmd->cmnd[0] == READ_CAPACITY) {
858                 /* sector count, 32-bit */
859                 rbuf[0] = tmp >> (8 * 3);
860                 rbuf[1] = tmp >> (8 * 2);
861                 rbuf[2] = tmp >> (8 * 1);
862                 rbuf[3] = tmp;
863
864                 /* sector size */
865                 tmp = ATA_SECT_SIZE;
866                 rbuf[6] = tmp >> 8;
867                 rbuf[7] = tmp;
868
869         } else {
870                 /* sector count, 64-bit */
871                 rbuf[2] = n_sectors >> (8 * 7);
872                 rbuf[3] = n_sectors >> (8 * 6);
873                 rbuf[4] = n_sectors >> (8 * 5);
874                 rbuf[5] = n_sectors >> (8 * 4);
875                 rbuf[6] = tmp >> (8 * 3);
876                 rbuf[7] = tmp >> (8 * 2);
877                 rbuf[8] = tmp >> (8 * 1);
878                 rbuf[9] = tmp;
879
880                 /* sector size */
881                 tmp = ATA_SECT_SIZE;
882                 rbuf[12] = tmp >> 8;
883                 rbuf[13] = tmp;
884         }
885
886         return 0;
887 }
888
889 /**
890  *      ata_scsiop_report_luns - Simulate REPORT LUNS command
891  *      @args: Port / device / SCSI command of interest.
892  *      @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
893  *      @buflen: Response buffer length.
894  *
895  *      Simulate REPORT LUNS command.
896  *
897  *      LOCKING:
898  *      spin_lock_irqsave(host_set lock)
899  */
900
901 unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
902                                    unsigned int buflen)
903 {
904         VPRINTK("ENTER\n");
905         rbuf[3] = 8;    /* just one lun, LUN 0, size 8 bytes */
906
907         return 0;
908 }
909
910 /**
911  *      ata_scsi_badcmd - End a SCSI request with an error
912  *      @cmd: SCSI request to be handled
913  *      @done: SCSI command completion function
914  *      @asc: SCSI-defined additional sense code
915  *      @ascq: SCSI-defined additional sense code qualifier
916  *
917  *      Helper function that completes a SCSI command with
918  *      %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
919  *      and the specified additional sense codes.
920  *
921  *      LOCKING:
922  *      spin_lock_irqsave(host_set lock)
923  */
924
925 void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
926 {
927         DPRINTK("ENTER\n");
928         cmd->result = SAM_STAT_CHECK_CONDITION;
929
930         cmd->sense_buffer[0] = 0x70;
931         cmd->sense_buffer[2] = ILLEGAL_REQUEST;
932         cmd->sense_buffer[7] = 14 - 8;  /* addnl. sense len. FIXME: correct? */
933         cmd->sense_buffer[12] = asc;
934         cmd->sense_buffer[13] = ascq;
935
936         done(cmd);
937 }
938
939 /**
940  *      atapi_xlat - Initialize PACKET taskfile
941  *      @qc: command structure to be initialized
942  *      @scsicmd: SCSI CDB associated with this PACKET command
943  *
944  *      LOCKING:
945  *      spin_lock_irqsave(host_set lock)
946  *
947  *      RETURNS:
948  *      Zero on success, non-zero on failure.
949  */
950
951 static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
952 {
953         struct scsi_cmnd *cmd = qc->scsicmd;
954
955         qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
956         if (cmd->sc_data_direction == SCSI_DATA_WRITE) {
957                 qc->tf.flags |= ATA_TFLAG_WRITE;
958                 DPRINTK("direction: write\n");
959         }
960
961         qc->tf.command = ATA_CMD_PACKET;
962
963         /* no data - interrupt-driven */
964         if (cmd->sc_data_direction == SCSI_DATA_NONE)
965                 qc->tf.protocol = ATA_PROT_ATAPI;
966
967         /* PIO data xfer - polling */
968         else if ((qc->flags & ATA_QCFLAG_DMA) == 0) {
969                 ata_qc_set_polling(qc);
970                 qc->tf.protocol = ATA_PROT_ATAPI;
971                 qc->tf.lbam = (8 * 1024) & 0xff;
972                 qc->tf.lbah = (8 * 1024) >> 8;
973
974         /* DMA data xfer - interrupt-driven */
975         } else {
976                 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
977                 qc->tf.feature |= ATAPI_PKT_DMA;
978
979 #ifdef ATAPI_ENABLE_DMADIR
980                 /* some SATA bridges need us to indicate data xfer direction */
981                 if (cmd->sc_data_direction != SCSI_DATA_WRITE)
982                         qc->tf.feature |= ATAPI_DMADIR;
983 #endif
984         }
985
986         return 0;
987 }
988
989 /**
990  *      ata_scsi_find_dev - lookup ata_device from scsi_cmnd
991  *      @ap: ATA port to which the device is attached
992  *      @cmd: SCSI command to be sent to the device
993  *
994  *      Given various information provided in struct scsi_cmnd,
995  *      map that onto an ATA bus, and using that mapping
996  *      determine which ata_device is associated with the
997  *      SCSI command to be sent.
998  *
999  *      LOCKING:
1000  *      spin_lock_irqsave(host_set lock)
1001  *
1002  *      RETURNS:
1003  *      Associated ATA device, or %NULL if not found.
1004  */
1005
1006 static inline struct ata_device *
1007 ata_scsi_find_dev(struct ata_port *ap, struct scsi_cmnd *cmd)
1008 {
1009         struct ata_device *dev;
1010
1011         /* skip commands not addressed to targets we simulate */
1012         if (likely(cmd->device->id < ATA_MAX_DEVICES))
1013                 dev = &ap->device[cmd->device->id];
1014         else
1015                 return NULL;
1016
1017         if (unlikely((cmd->device->channel != 0) ||
1018                      (cmd->device->lun != 0)))
1019                 return NULL;
1020
1021         if (unlikely(!ata_dev_present(dev)))
1022                 return NULL;
1023
1024 #ifndef ATA_ENABLE_ATAPI
1025         if (unlikely(dev->class == ATA_DEV_ATAPI))
1026                 return NULL;
1027 #endif
1028
1029         return dev;
1030 }
1031
1032 /**
1033  *      ata_get_xlat_func - check if SCSI to ATA translation is possible
1034  *      @cmd: SCSI command opcode to consider
1035  *
1036  *      Look up the SCSI command given, and determine whether the
1037  *      SCSI command is to be translated or simulated.
1038  *
1039  *      RETURNS:
1040  *      Pointer to translation function if possible, %NULL if not.
1041  */
1042
1043 static inline ata_xlat_func_t ata_get_xlat_func(u8 cmd)
1044 {
1045         switch (cmd) {
1046         case READ_6:
1047         case READ_10:
1048         case READ_16:
1049
1050         case WRITE_6:
1051         case WRITE_10:
1052         case WRITE_16:
1053                 return ata_scsi_rw_xlat;
1054         }
1055
1056         return NULL;
1057 }
1058
1059 /**
1060  *      ata_scsi_dump_cdb - dump SCSI command contents to dmesg
1061  *      @ap: ATA port to which the command was being sent
1062  *      @cmd: SCSI command to dump
1063  *
1064  *      Prints the contents of a SCSI command via printk().
1065  */
1066
1067 static inline void ata_scsi_dump_cdb(struct ata_port *ap,
1068                                      struct scsi_cmnd *cmd)
1069 {
1070 #ifdef ATA_DEBUG
1071         u8 *scsicmd = cmd->cmnd;
1072
1073         DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
1074                 ap->id,
1075                 cmd->device->channel, cmd->device->id, cmd->device->lun,
1076                 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
1077                 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
1078                 scsicmd[8]);
1079 #endif
1080 }
1081
1082 /**
1083  *      ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
1084  *      @cmd: SCSI command to be sent
1085  *      @done: Completion function, called when command is complete
1086  *
1087  *      In some cases, this function translates SCSI commands into
1088  *      ATA taskfiles, and queues the taskfiles to be sent to
1089  *      hardware.  In other cases, this function simulates a
1090  *      SCSI device by evaluating and responding to certain
1091  *      SCSI commands.  This creates the overall effect of
1092  *      ATA and ATAPI devices appearing as SCSI devices.
1093  *
1094  *      LOCKING:
1095  *      Releases scsi-layer-held lock, and obtains host_set lock.
1096  *
1097  *      RETURNS:
1098  *      Zero.
1099  */
1100
1101 int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
1102 {
1103         struct ata_port *ap;
1104         struct ata_device *dev;
1105
1106         ap = (struct ata_port *) &cmd->device->host->hostdata[0];
1107
1108         ata_scsi_dump_cdb(ap, cmd);
1109
1110         dev = ata_scsi_find_dev(ap, cmd);
1111         if (unlikely(!dev)) {
1112                 cmd->result = (DID_BAD_TARGET << 16);
1113                 done(cmd);
1114                 goto out_unlock;
1115         }
1116
1117         if (dev->class == ATA_DEV_ATA) {
1118                 ata_xlat_func_t xlat_func = ata_get_xlat_func(cmd->cmnd[0]);
1119
1120                 if (xlat_func)
1121                         ata_scsi_translate(ap, dev, cmd, done, xlat_func);
1122                 else
1123                         ata_scsi_simulate(ap, dev, cmd, done);
1124         } else
1125                 ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
1126
1127 out_unlock:
1128         return 0;
1129 }
1130
1131 /**
1132  *      ata_scsi_simulate - simulate SCSI command on ATA device
1133  *      @ap: Port to which ATA device is attached.
1134  *      @dev: Target device for CDB.
1135  *      @cmd: SCSI command being sent to device.
1136  *      @done: SCSI command completion function.
1137  *
1138  *      Interprets and directly executes a select list of SCSI commands
1139  *      that can be handled internally.
1140  *
1141  *      LOCKING:
1142  *      spin_lock_irqsave(host_set lock)
1143  */
1144
1145 static void ata_scsi_simulate(struct ata_port *ap, struct ata_device *dev,
1146                               struct scsi_cmnd *cmd,
1147                               void (*done)(struct scsi_cmnd *))
1148 {
1149         struct ata_scsi_args args;
1150         u8 *scsicmd = cmd->cmnd;
1151
1152         args.ap = ap;
1153         args.dev = dev;
1154         args.cmd = cmd;
1155         args.done = done;
1156
1157         switch(scsicmd[0]) {
1158                 /* no-op's, complete with success */
1159                 case SYNCHRONIZE_CACHE:         /* FIXME: temporary */
1160                 case REZERO_UNIT:
1161                 case SEEK_6:
1162                 case SEEK_10:
1163                 case TEST_UNIT_READY:
1164                 case FORMAT_UNIT:               /* FIXME: correct? */
1165                 case SEND_DIAGNOSTIC:           /* FIXME: correct? */
1166                         ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
1167                         break;
1168
1169                 case INQUIRY:
1170                         if (scsicmd[1] & 2)                /* is CmdDt set?  */
1171                                 ata_bad_cdb(cmd, done);
1172                         else if ((scsicmd[1] & 1) == 0)    /* is EVPD clear? */
1173                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
1174                         else if (scsicmd[2] == 0x00)
1175                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
1176                         else if (scsicmd[2] == 0x80)
1177                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
1178                         else if (scsicmd[2] == 0x83)
1179                                 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
1180                         else
1181                                 ata_bad_cdb(cmd, done);
1182                         break;
1183
1184                 case MODE_SENSE:
1185                 case MODE_SENSE_10:
1186                         ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
1187                         break;
1188
1189                 case MODE_SELECT:       /* unconditionally return */
1190                 case MODE_SELECT_10:    /* bad-field-in-cdb */
1191                         ata_bad_cdb(cmd, done);
1192                         break;
1193
1194                 case READ_CAPACITY:
1195                         ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1196                         break;
1197
1198                 case SERVICE_ACTION_IN:
1199                         if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
1200                                 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1201                         else
1202                                 ata_bad_cdb(cmd, done);
1203                         break;
1204
1205                 case REPORT_LUNS:
1206                         ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
1207                         break;
1208
1209                 /* mandantory commands we haven't implemented yet */
1210                 case REQUEST_SENSE:
1211
1212                 /* all other commands */
1213                 default:
1214                         ata_bad_scsiop(cmd, done);
1215                         break;
1216         }
1217 }
1218