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