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