blob: 8b065ef0e39abaca243d67fc9158f9b52bfbf993 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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#include <asm/uaccess.h>
33
34#include "libata.h"
35
36typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, u8 *scsicmd);
37static struct ata_device *
38ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev);
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 */
59int 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
70int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
71{
72 struct ata_port *ap;
73 struct ata_device *dev;
74 int val = -EINVAL, rc = -EINVAL;
75
76 ap = (struct ata_port *) &scsidev->host->hostdata[0];
77 if (!ap)
78 goto out;
79
80 dev = ata_scsi_find_dev(ap, scsidev);
81 if (!dev) {
82 rc = -ENODEV;
83 goto out;
84 }
85
86 switch (cmd) {
87 case ATA_IOC_GET_IO32:
88 val = 0;
89 if (copy_to_user(arg, &val, 1))
90 return -EFAULT;
91 return 0;
92
93 case ATA_IOC_SET_IO32:
94 val = (unsigned long) arg;
95 if (val != 0)
96 return -EINVAL;
97 return 0;
98
99 default:
100 rc = -ENOTTY;
101 break;
102 }
103
104out:
105 return rc;
106}
107
108/**
109 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
110 * @ap: ATA port to which the new command is attached
111 * @dev: ATA device to which the new command is attached
112 * @cmd: SCSI command that originated this ATA command
113 * @done: SCSI command completion function
114 *
115 * Obtain a reference to an unused ata_queued_cmd structure,
116 * which is the basic libata structure representing a single
117 * ATA command sent to the hardware.
118 *
119 * If a command was available, fill in the SCSI-specific
120 * portions of the structure with information on the
121 * current command.
122 *
123 * LOCKING:
124 * spin_lock_irqsave(host_set lock)
125 *
126 * RETURNS:
127 * Command allocated, or %NULL if none available.
128 */
129struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
130 struct ata_device *dev,
131 struct scsi_cmnd *cmd,
132 void (*done)(struct scsi_cmnd *))
133{
134 struct ata_queued_cmd *qc;
135
136 qc = ata_qc_new_init(ap, dev);
137 if (qc) {
138 qc->scsicmd = cmd;
139 qc->scsidone = done;
140
141 if (cmd->use_sg) {
142 qc->sg = (struct scatterlist *) cmd->request_buffer;
143 qc->n_elem = cmd->use_sg;
144 } else {
145 qc->sg = &qc->sgent;
146 qc->n_elem = 1;
147 }
148 } else {
149 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
150 done(cmd);
151 }
152
153 return qc;
154}
155
156/**
157 * ata_to_sense_error - convert ATA error to SCSI error
158 * @qc: Command that we are erroring out
159 * @drv_stat: value contained in ATA status register
160 *
161 * Converts an ATA error into a SCSI error. While we are at it
162 * we decode and dump the ATA error for the user so that they
163 * have some idea what really happened at the non make-believe
164 * layer.
165 *
166 * LOCKING:
167 * spin_lock_irqsave(host_set lock)
168 */
169
170void ata_to_sense_error(struct ata_queued_cmd *qc, u8 drv_stat)
171{
172 struct scsi_cmnd *cmd = qc->scsicmd;
173 u8 err = 0;
174 unsigned char *sb = cmd->sense_buffer;
175 /* Based on the 3ware driver translation table */
176 static unsigned char sense_table[][4] = {
177 /* BBD|ECC|ID|MAR */
178 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
179 /* BBD|ECC|ID */
180 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
181 /* ECC|MC|MARK */
182 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
183 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
184 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
185 /* MC|ID|ABRT|TRK0|MARK */
186 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
187 /* MCR|MARK */
188 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
189 /* Bad address mark */
190 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
191 /* TRK0 */
192 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
193 /* Abort & !ICRC */
194 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
195 /* Media change request */
196 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
197 /* SRV */
198 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
199 /* Media change */
200 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
201 /* ECC */
202 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
203 /* BBD - block marked bad */
204 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
205 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
206 };
207 static unsigned char stat_table[][4] = {
208 /* Must be first because BUSY means no other bits valid */
209 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
210 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
211 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
212 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
213 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
214 };
215 int i = 0;
216
217 cmd->result = SAM_STAT_CHECK_CONDITION;
218
219 /*
220 * Is this an error we can process/parse
221 */
222
223 if(drv_stat & ATA_ERR)
224 /* Read the err bits */
225 err = ata_chk_err(qc->ap);
226
227 /* Display the ATA level error info */
228
229 printk(KERN_WARNING "ata%u: status=0x%02x { ", qc->ap->id, drv_stat);
230 if(drv_stat & 0x80)
231 {
232 printk("Busy ");
233 err = 0; /* Data is not valid in this case */
234 }
235 else {
236 if(drv_stat & 0x40) printk("DriveReady ");
237 if(drv_stat & 0x20) printk("DeviceFault ");
238 if(drv_stat & 0x10) printk("SeekComplete ");
239 if(drv_stat & 0x08) printk("DataRequest ");
240 if(drv_stat & 0x04) printk("CorrectedError ");
241 if(drv_stat & 0x02) printk("Index ");
242 if(drv_stat & 0x01) printk("Error ");
243 }
244 printk("}\n");
245
246 if(err)
247 {
248 printk(KERN_WARNING "ata%u: error=0x%02x { ", qc->ap->id, err);
249 if(err & 0x04) printk("DriveStatusError ");
250 if(err & 0x80)
251 {
252 if(err & 0x04)
253 printk("BadCRC ");
254 else
255 printk("Sector ");
256 }
257 if(err & 0x40) printk("UncorrectableError ");
258 if(err & 0x10) printk("SectorIdNotFound ");
259 if(err & 0x02) printk("TrackZeroNotFound ");
260 if(err & 0x01) printk("AddrMarkNotFound ");
261 printk("}\n");
262
263 /* Should we dump sector info here too ?? */
264 }
265
266
267 /* Look for err */
268 while(sense_table[i][0] != 0xFF)
269 {
270 /* Look for best matches first */
271 if((sense_table[i][0] & err) == sense_table[i][0])
272 {
273 sb[0] = 0x70;
274 sb[2] = sense_table[i][1];
275 sb[7] = 0x0a;
276 sb[12] = sense_table[i][2];
277 sb[13] = sense_table[i][3];
278 return;
279 }
280 i++;
281 }
282 /* No immediate match */
283 if(err)
284 printk(KERN_DEBUG "ata%u: no sense translation for 0x%02x\n", qc->ap->id, err);
285
286 i = 0;
287 /* Fall back to interpreting status bits */
288 while(stat_table[i][0] != 0xFF)
289 {
290 if(stat_table[i][0] & drv_stat)
291 {
292 sb[0] = 0x70;
293 sb[2] = stat_table[i][1];
294 sb[7] = 0x0a;
295 sb[12] = stat_table[i][2];
296 sb[13] = stat_table[i][3];
297 return;
298 }
299 i++;
300 }
301 /* No error ?? */
302 printk(KERN_ERR "ata%u: called with no error (%02X)!\n", qc->ap->id, drv_stat);
303 /* additional-sense-code[-qualifier] */
304
305 sb[0] = 0x70;
306 sb[2] = MEDIUM_ERROR;
307 sb[7] = 0x0A;
be7db052005-04-17 15:26:13 -0500308 if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 sb[12] = 0x11; /* "unrecovered read error" */
310 sb[13] = 0x04;
311 } else {
312 sb[12] = 0x0C; /* "write error - */
313 sb[13] = 0x02; /* auto-reallocation failed" */
314 }
315}
316
317/**
318 * ata_scsi_slave_config - Set SCSI device attributes
319 * @sdev: SCSI device to examine
320 *
321 * This is called before we actually start reading
322 * and writing to the device, to configure certain
323 * SCSI mid-layer behaviors.
324 *
325 * LOCKING:
326 * Defined by SCSI layer. We don't really care.
327 */
328
329int ata_scsi_slave_config(struct scsi_device *sdev)
330{
331 sdev->use_10_for_rw = 1;
332 sdev->use_10_for_ms = 1;
333
334 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
335
336 if (sdev->id < ATA_MAX_DEVICES) {
337 struct ata_port *ap;
338 struct ata_device *dev;
339
340 ap = (struct ata_port *) &sdev->host->hostdata[0];
341 dev = &ap->device[sdev->id];
342
343 /* TODO: 1024 is an arbitrary number, not the
344 * hardware maximum. This should be increased to
345 * 65534 when Jens Axboe's patch for dynamically
346 * determining max_sectors is merged.
347 */
348 if ((dev->flags & ATA_DFLAG_LBA48) &&
349 ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) {
350 sdev->host->max_sectors = 2048;
351 blk_queue_max_sectors(sdev->request_queue, 2048);
352 }
353 }
354
355 return 0; /* scsi layer doesn't check return value, sigh */
356}
357
358/**
359 * ata_scsi_error - SCSI layer error handler callback
360 * @host: SCSI host on which error occurred
361 *
362 * Handles SCSI-layer-thrown error events.
363 *
364 * LOCKING:
365 * Inherited from SCSI layer (none, can sleep)
366 *
367 * RETURNS:
368 * Zero.
369 */
370
371int ata_scsi_error(struct Scsi_Host *host)
372{
373 struct ata_port *ap;
374
375 DPRINTK("ENTER\n");
376
377 ap = (struct ata_port *) &host->hostdata[0];
378 ap->ops->eng_timeout(ap);
379
380 /* TODO: this is per-command; when queueing is supported
381 * this code will either change or move to a more
382 * appropriate place
383 */
384 host->host_failed--;
385
386 DPRINTK("EXIT\n");
387 return 0;
388}
389
390/**
391 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
392 * @qc: Storage for translated ATA taskfile
393 * @scsicmd: SCSI command to translate (ignored)
394 *
395 * Sets up an ATA taskfile to issue FLUSH CACHE or
396 * FLUSH CACHE EXT.
397 *
398 * LOCKING:
399 * spin_lock_irqsave(host_set lock)
400 *
401 * RETURNS:
402 * Zero on success, non-zero on error.
403 */
404
405static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
406{
407 struct ata_taskfile *tf = &qc->tf;
408
409 tf->flags |= ATA_TFLAG_DEVICE;
410 tf->protocol = ATA_PROT_NODATA;
411
412 if ((tf->flags & ATA_TFLAG_LBA48) &&
413 (ata_id_has_flush_ext(qc->dev->id)))
414 tf->command = ATA_CMD_FLUSH_EXT;
415 else
416 tf->command = ATA_CMD_FLUSH;
417
418 return 0;
419}
420
421/**
422 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
423 * @qc: Storage for translated ATA taskfile
424 * @scsicmd: SCSI command to translate
425 *
426 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
427 *
428 * LOCKING:
429 * spin_lock_irqsave(host_set lock)
430 *
431 * RETURNS:
432 * Zero on success, non-zero on error.
433 */
434
435static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
436{
437 struct ata_taskfile *tf = &qc->tf;
Albert Lee8bf62ec2005-05-12 15:29:42 -0400438 struct ata_device *dev = qc->dev;
439 unsigned int lba = tf->flags & ATA_TFLAG_LBA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
441 u64 dev_sectors = qc->dev->n_sectors;
Albert Lee8bf62ec2005-05-12 15:29:42 -0400442 u64 block = 0;
443 u32 n_block = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
446 tf->protocol = ATA_PROT_NODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
448 if (scsicmd[0] == VERIFY) {
Albert Lee8bf62ec2005-05-12 15:29:42 -0400449 block |= ((u64)scsicmd[2]) << 24;
450 block |= ((u64)scsicmd[3]) << 16;
451 block |= ((u64)scsicmd[4]) << 8;
452 block |= ((u64)scsicmd[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Albert Lee8bf62ec2005-05-12 15:29:42 -0400454 n_block |= ((u32)scsicmd[7]) << 8;
455 n_block |= ((u32)scsicmd[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 }
457
458 else if (scsicmd[0] == VERIFY_16) {
Albert Lee8bf62ec2005-05-12 15:29:42 -0400459 block |= ((u64)scsicmd[2]) << 56;
460 block |= ((u64)scsicmd[3]) << 48;
461 block |= ((u64)scsicmd[4]) << 40;
462 block |= ((u64)scsicmd[5]) << 32;
463 block |= ((u64)scsicmd[6]) << 24;
464 block |= ((u64)scsicmd[7]) << 16;
465 block |= ((u64)scsicmd[8]) << 8;
466 block |= ((u64)scsicmd[9]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Albert Lee8bf62ec2005-05-12 15:29:42 -0400468 n_block |= ((u32)scsicmd[10]) << 24;
469 n_block |= ((u32)scsicmd[11]) << 16;
470 n_block |= ((u32)scsicmd[12]) << 8;
471 n_block |= ((u32)scsicmd[13]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473
474 else
475 return 1;
476
Albert Lee8bf62ec2005-05-12 15:29:42 -0400477 if (!n_block)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478 return 1;
Albert Lee8bf62ec2005-05-12 15:29:42 -0400479 if (block >= dev_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 return 1;
Albert Lee8bf62ec2005-05-12 15:29:42 -0400481 if ((block + n_block) > dev_sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 return 1;
483 if (lba48) {
Albert Lee8bf62ec2005-05-12 15:29:42 -0400484 if (n_block > (64 * 1024))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 return 1;
486 } else {
Albert Lee8bf62ec2005-05-12 15:29:42 -0400487 if (n_block > 256)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 return 1;
489 }
490
Albert Lee8bf62ec2005-05-12 15:29:42 -0400491 if (lba) {
492 if (lba48) {
493 tf->command = ATA_CMD_VERIFY_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Albert Lee8bf62ec2005-05-12 15:29:42 -0400495 tf->hob_nsect = (n_block >> 8) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
Albert Lee8bf62ec2005-05-12 15:29:42 -0400497 tf->hob_lbah = (block >> 40) & 0xff;
498 tf->hob_lbam = (block >> 32) & 0xff;
499 tf->hob_lbal = (block >> 24) & 0xff;
500 } else {
501 tf->command = ATA_CMD_VERIFY;
502
503 tf->device |= (block >> 24) & 0xf;
504 }
505
506 tf->nsect = n_block & 0xff;
507
508 tf->lbah = (block >> 16) & 0xff;
509 tf->lbam = (block >> 8) & 0xff;
510 tf->lbal = block & 0xff;
511
512 tf->device |= ATA_LBA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 } else {
Albert Lee8bf62ec2005-05-12 15:29:42 -0400514 /* CHS */
515 u32 sect, head, cyl, track;
516
517 /* Convert LBA to CHS */
518 track = (u32)block / dev->sectors;
519 cyl = track / dev->heads;
520 head = track % dev->heads;
521 sect = (u32)block % dev->sectors + 1;
522
523 DPRINTK("block[%u] track[%u] cyl[%u] head[%u] sect[%u] \n", (u32)block, track, cyl, head, sect);
524
525 /* Check whether the converted CHS can fit.
526 Cylinder: 0-65535
527 Head: 0-15
528 Sector: 1-255*/
529 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
530 return 1;
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532 tf->command = ATA_CMD_VERIFY;
Albert Lee8bf62ec2005-05-12 15:29:42 -0400533 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
534 tf->lbal = sect;
535 tf->lbam = cyl;
536 tf->lbah = cyl >> 8;
537 tf->device |= head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 }
539
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 return 0;
541}
542
543/**
544 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
545 * @qc: Storage for translated ATA taskfile
546 * @scsicmd: SCSI command to translate
547 *
548 * Converts any of six SCSI read/write commands into the
549 * ATA counterpart, including starting sector (LBA),
550 * sector count, and taking into account the device's LBA48
551 * support.
552 *
553 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
554 * %WRITE_16 are currently supported.
555 *
556 * LOCKING:
557 * spin_lock_irqsave(host_set lock)
558 *
559 * RETURNS:
560 * Zero on success, non-zero on error.
561 */
562
563static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
564{
565 struct ata_taskfile *tf = &qc->tf;
Albert Lee8bf62ec2005-05-12 15:29:42 -0400566 struct ata_device *dev = qc->dev;
567 unsigned int lba = tf->flags & ATA_TFLAG_LBA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
Albert Lee8bf62ec2005-05-12 15:29:42 -0400569 u64 block = 0;
570 u32 n_block = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
572 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
573 tf->protocol = qc->dev->xfer_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575 if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
576 scsicmd[0] == READ_16) {
577 tf->command = qc->dev->read_cmd;
578 } else {
579 tf->command = qc->dev->write_cmd;
580 tf->flags |= ATA_TFLAG_WRITE;
581 }
582
Albert Lee8bf62ec2005-05-12 15:29:42 -0400583 /* Calculate the SCSI LBA and transfer length. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) {
Albert Lee8bf62ec2005-05-12 15:29:42 -0400585 block |= ((u64)scsicmd[2]) << 24;
586 block |= ((u64)scsicmd[3]) << 16;
587 block |= ((u64)scsicmd[4]) << 8;
588 block |= ((u64)scsicmd[5]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Albert Lee8bf62ec2005-05-12 15:29:42 -0400590 n_block |= ((u32)scsicmd[7]) << 8;
591 n_block |= ((u32)scsicmd[8]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593 VPRINTK("ten-byte command\n");
Albert Lee8bf62ec2005-05-12 15:29:42 -0400594 } else if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) {
595 block |= ((u64)scsicmd[2]) << 8;
596 block |= ((u64)scsicmd[3]);
597 n_block |= ((u32)scsicmd[4]);
598
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 VPRINTK("six-byte command\n");
Albert Lee8bf62ec2005-05-12 15:29:42 -0400600 } else if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) {
601 block |= ((u64)scsicmd[2]) << 56;
602 block |= ((u64)scsicmd[3]) << 48;
603 block |= ((u64)scsicmd[4]) << 40;
604 block |= ((u64)scsicmd[5]) << 32;
605 block |= ((u64)scsicmd[6]) << 24;
606 block |= ((u64)scsicmd[7]) << 16;
607 block |= ((u64)scsicmd[8]) << 8;
608 block |= ((u64)scsicmd[9]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Albert Lee8bf62ec2005-05-12 15:29:42 -0400610 n_block |= ((u32)scsicmd[10]) << 24;
611 n_block |= ((u32)scsicmd[11]) << 16;
612 n_block |= ((u32)scsicmd[12]) << 8;
613 n_block |= ((u32)scsicmd[13]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615 VPRINTK("sixteen-byte command\n");
Albert Lee8bf62ec2005-05-12 15:29:42 -0400616 } else {
617 DPRINTK("no-byte command\n");
618 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620
Albert Lee8bf62ec2005-05-12 15:29:42 -0400621 /* Check and compose ATA command */
622 if (!n_block)
623 /* In ATA, sector count 0 means 256 or 65536 sectors, not 0 sectors. */
624 return 1;
625
626 if (lba) {
627 if (lba48) {
628 /* The request -may- be too large for LBA48. */
629 if ((block >> 48) || (n_block > 65536))
630 return 1;
631
632 tf->hob_nsect = (n_block >> 8) & 0xff;
633
634 tf->hob_lbah = (block >> 40) & 0xff;
635 tf->hob_lbam = (block >> 32) & 0xff;
636 tf->hob_lbal = (block >> 24) & 0xff;
637 } else {
638 /* LBA28 */
639
640 /* The request -may- be too large for LBA28. */
641 if ((block >> 28) || (n_block > 256))
642 return 1;
643
644 tf->device |= (block >> 24) & 0xf;
645 }
646
647 qc->nsect = n_block;
648 tf->nsect = n_block & 0xff;
649
650 tf->lbah = (block >> 16) & 0xff;
651 tf->lbam = (block >> 8) & 0xff;
652 tf->lbal = block & 0xff;
653
654 tf->device |= ATA_LBA;
655 } else {
656 /* CHS */
657 u32 sect, head, cyl, track;
658
659 /* The request -may- be too large for CHS addressing. */
660 if ((block >> 28) || (n_block > 256))
661 return 1;
662
663 /* Convert LBA to CHS */
664 track = (u32)block / dev->sectors;
665 cyl = track / dev->heads;
666 head = track % dev->heads;
667 sect = (u32)block % dev->sectors + 1;
668
669 DPRINTK("block[%u] track[%u] cyl[%u] head[%u] sect[%u] \n",
670 (u32)block, track, cyl, head, sect);
671
672 /* Check whether the converted CHS can fit.
673 Cylinder: 0-65535
674 Head: 0-15
675 Sector: 1-255*/
676 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
677 return 1;
678
679 qc->nsect = n_block;
680 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
681 tf->lbal = sect;
682 tf->lbam = cyl;
683 tf->lbah = cyl >> 8;
684 tf->device |= head;
685 }
686
687 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
690static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
691{
692 struct scsi_cmnd *cmd = qc->scsicmd;
693
694 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ)))
695 ata_to_sense_error(qc, drv_stat);
696 else
697 cmd->result = SAM_STAT_GOOD;
698
699 qc->scsidone(cmd);
700
701 return 0;
702}
703
704/**
705 * ata_scsi_translate - Translate then issue SCSI command to ATA device
706 * @ap: ATA port to which the command is addressed
707 * @dev: ATA device to which the command is addressed
708 * @cmd: SCSI command to execute
709 * @done: SCSI command completion function
710 * @xlat_func: Actor which translates @cmd to an ATA taskfile
711 *
712 * Our ->queuecommand() function has decided that the SCSI
713 * command issued can be directly translated into an ATA
714 * command, rather than handled internally.
715 *
716 * This function sets up an ata_queued_cmd structure for the
717 * SCSI command, and sends that ata_queued_cmd to the hardware.
718 *
719 * LOCKING:
720 * spin_lock_irqsave(host_set lock)
721 */
722
723static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
724 struct scsi_cmnd *cmd,
725 void (*done)(struct scsi_cmnd *),
726 ata_xlat_func_t xlat_func)
727{
728 struct ata_queued_cmd *qc;
729 u8 *scsicmd = cmd->cmnd;
730
731 VPRINTK("ENTER\n");
732
733 qc = ata_scsi_qc_new(ap, dev, cmd, done);
734 if (!qc)
735 return;
736
737 /* data is present; dma-map it */
be7db052005-04-17 15:26:13 -0500738 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
739 cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (unlikely(cmd->request_bufflen < 1)) {
741 printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
742 ap->id, dev->devno);
743 goto err_out;
744 }
745
746 if (cmd->use_sg)
747 ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
748 else
749 ata_sg_init_one(qc, cmd->request_buffer,
750 cmd->request_bufflen);
751
752 qc->dma_dir = cmd->sc_data_direction;
753 }
754
755 qc->complete_fn = ata_scsi_qc_complete;
756
757 if (xlat_func(qc, scsicmd))
758 goto err_out;
759
760 /* select device, send command to hardware */
761 if (ata_qc_issue(qc))
762 goto err_out;
763
764 VPRINTK("EXIT\n");
765 return;
766
767err_out:
768 ata_qc_free(qc);
769 ata_bad_cdb(cmd, done);
770 DPRINTK("EXIT - badcmd\n");
771}
772
773/**
774 * ata_scsi_rbuf_get - Map response buffer.
775 * @cmd: SCSI command containing buffer to be mapped.
776 * @buf_out: Pointer to mapped area.
777 *
778 * Maps buffer contained within SCSI command @cmd.
779 *
780 * LOCKING:
781 * spin_lock_irqsave(host_set lock)
782 *
783 * RETURNS:
784 * Length of response buffer.
785 */
786
787static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
788{
789 u8 *buf;
790 unsigned int buflen;
791
792 if (cmd->use_sg) {
793 struct scatterlist *sg;
794
795 sg = (struct scatterlist *) cmd->request_buffer;
796 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
797 buflen = sg->length;
798 } else {
799 buf = cmd->request_buffer;
800 buflen = cmd->request_bufflen;
801 }
802
803 *buf_out = buf;
804 return buflen;
805}
806
807/**
808 * ata_scsi_rbuf_put - Unmap response buffer.
809 * @cmd: SCSI command containing buffer to be unmapped.
810 * @buf: buffer to unmap
811 *
812 * Unmaps response buffer contained within @cmd.
813 *
814 * LOCKING:
815 * spin_lock_irqsave(host_set lock)
816 */
817
818static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
819{
820 if (cmd->use_sg) {
821 struct scatterlist *sg;
822
823 sg = (struct scatterlist *) cmd->request_buffer;
824 kunmap_atomic(buf - sg->offset, KM_USER0);
825 }
826}
827
828/**
829 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
830 * @args: device IDENTIFY data / SCSI command of interest.
831 * @actor: Callback hook for desired SCSI command simulator
832 *
833 * Takes care of the hard work of simulating a SCSI command...
834 * Mapping the response buffer, calling the command's handler,
835 * and handling the handler's return value. This return value
836 * indicates whether the handler wishes the SCSI command to be
837 * completed successfully, or not.
838 *
839 * LOCKING:
840 * spin_lock_irqsave(host_set lock)
841 */
842
843void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
844 unsigned int (*actor) (struct ata_scsi_args *args,
845 u8 *rbuf, unsigned int buflen))
846{
847 u8 *rbuf;
848 unsigned int buflen, rc;
849 struct scsi_cmnd *cmd = args->cmd;
850
851 buflen = ata_scsi_rbuf_get(cmd, &rbuf);
852 memset(rbuf, 0, buflen);
853 rc = actor(args, rbuf, buflen);
854 ata_scsi_rbuf_put(cmd, rbuf);
855
856 if (rc)
857 ata_bad_cdb(cmd, args->done);
858 else {
859 cmd->result = SAM_STAT_GOOD;
860 args->done(cmd);
861 }
862}
863
864/**
865 * ata_scsiop_inq_std - Simulate INQUIRY command
866 * @args: device IDENTIFY data / SCSI command of interest.
867 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
868 * @buflen: Response buffer length.
869 *
870 * Returns standard device identification data associated
871 * with non-EVPD INQUIRY command output.
872 *
873 * LOCKING:
874 * spin_lock_irqsave(host_set lock)
875 */
876
877unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
878 unsigned int buflen)
879{
880 u8 hdr[] = {
881 TYPE_DISK,
882 0,
883 0x5, /* claim SPC-3 version compatibility */
884 2,
885 95 - 4
886 };
887
888 /* set scsi removeable (RMB) bit per ata bit */
889 if (ata_id_removeable(args->id))
890 hdr[1] |= (1 << 7);
891
892 VPRINTK("ENTER\n");
893
894 memcpy(rbuf, hdr, sizeof(hdr));
895
896 if (buflen > 35) {
897 memcpy(&rbuf[8], "ATA ", 8);
898 ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
899 ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
900 if (rbuf[32] == 0 || rbuf[32] == ' ')
901 memcpy(&rbuf[32], "n/a ", 4);
902 }
903
904 if (buflen > 63) {
905 const u8 versions[] = {
906 0x60, /* SAM-3 (no version claimed) */
907
908 0x03,
909 0x20, /* SBC-2 (no version claimed) */
910
911 0x02,
912 0x60 /* SPC-3 (no version claimed) */
913 };
914
915 memcpy(rbuf + 59, versions, sizeof(versions));
916 }
917
918 return 0;
919}
920
921/**
922 * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
923 * @args: device IDENTIFY data / SCSI command of interest.
924 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
925 * @buflen: Response buffer length.
926 *
927 * Returns list of inquiry EVPD pages available.
928 *
929 * LOCKING:
930 * spin_lock_irqsave(host_set lock)
931 */
932
933unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
934 unsigned int buflen)
935{
936 const u8 pages[] = {
937 0x00, /* page 0x00, this page */
938 0x80, /* page 0x80, unit serial no page */
939 0x83 /* page 0x83, device ident page */
940 };
941 rbuf[3] = sizeof(pages); /* number of supported EVPD pages */
942
943 if (buflen > 6)
944 memcpy(rbuf + 4, pages, sizeof(pages));
945
946 return 0;
947}
948
949/**
950 * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
951 * @args: device IDENTIFY data / SCSI command of interest.
952 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
953 * @buflen: Response buffer length.
954 *
955 * Returns ATA device serial number.
956 *
957 * LOCKING:
958 * spin_lock_irqsave(host_set lock)
959 */
960
961unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
962 unsigned int buflen)
963{
964 const u8 hdr[] = {
965 0,
966 0x80, /* this page code */
967 0,
968 ATA_SERNO_LEN, /* page len */
969 };
970 memcpy(rbuf, hdr, sizeof(hdr));
971
972 if (buflen > (ATA_SERNO_LEN + 4 - 1))
973 ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
974 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
975
976 return 0;
977}
978
979static const char *inq_83_str = "Linux ATA-SCSI simulator";
980
981/**
982 * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
983 * @args: device IDENTIFY data / SCSI command of interest.
984 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
985 * @buflen: Response buffer length.
986 *
987 * Returns device identification. Currently hardcoded to
988 * return "Linux ATA-SCSI simulator".
989 *
990 * LOCKING:
991 * spin_lock_irqsave(host_set lock)
992 */
993
994unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
995 unsigned int buflen)
996{
997 rbuf[1] = 0x83; /* this page code */
998 rbuf[3] = 4 + strlen(inq_83_str); /* page len */
999
1000 /* our one and only identification descriptor (vendor-specific) */
1001 if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
1002 rbuf[4 + 0] = 2; /* code set: ASCII */
1003 rbuf[4 + 3] = strlen(inq_83_str);
1004 memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
1005 }
1006
1007 return 0;
1008}
1009
1010/**
1011 * ata_scsiop_noop -
1012 * @args: device IDENTIFY data / SCSI command of interest.
1013 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1014 * @buflen: Response buffer length.
1015 *
1016 * No operation. Simply returns success to caller, to indicate
1017 * that the caller should successfully complete this SCSI command.
1018 *
1019 * LOCKING:
1020 * spin_lock_irqsave(host_set lock)
1021 */
1022
1023unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
1024 unsigned int buflen)
1025{
1026 VPRINTK("ENTER\n");
1027 return 0;
1028}
1029
1030/**
1031 * ata_msense_push - Push data onto MODE SENSE data output buffer
1032 * @ptr_io: (input/output) Location to store more output data
1033 * @last: End of output data buffer
1034 * @buf: Pointer to BLOB being added to output buffer
1035 * @buflen: Length of BLOB
1036 *
1037 * Store MODE SENSE data on an output buffer.
1038 *
1039 * LOCKING:
1040 * None.
1041 */
1042
1043static void ata_msense_push(u8 **ptr_io, const u8 *last,
1044 const u8 *buf, unsigned int buflen)
1045{
1046 u8 *ptr = *ptr_io;
1047
1048 if ((ptr + buflen - 1) > last)
1049 return;
1050
1051 memcpy(ptr, buf, buflen);
1052
1053 ptr += buflen;
1054
1055 *ptr_io = ptr;
1056}
1057
1058/**
1059 * ata_msense_caching - Simulate MODE SENSE caching info page
1060 * @id: device IDENTIFY data
1061 * @ptr_io: (input/output) Location to store more output data
1062 * @last: End of output data buffer
1063 *
1064 * Generate a caching info page, which conditionally indicates
1065 * write caching to the SCSI layer, depending on device
1066 * capabilities.
1067 *
1068 * LOCKING:
1069 * None.
1070 */
1071
1072static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1073 const u8 *last)
1074{
1075 u8 page[] = {
1076 0x8, /* page code */
1077 0x12, /* page length */
1078 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 zeroes */
1079 0, 0, 0, 0, 0, 0, 0, 0 /* 8 zeroes */
1080 };
1081
1082 if (ata_id_wcache_enabled(id))
1083 page[2] |= (1 << 2); /* write cache enable */
1084 if (!ata_id_rahead_enabled(id))
1085 page[12] |= (1 << 5); /* disable read ahead */
1086
1087 ata_msense_push(ptr_io, last, page, sizeof(page));
1088 return sizeof(page);
1089}
1090
1091/**
1092 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1093 * @dev: Device associated with this MODE SENSE command
1094 * @ptr_io: (input/output) Location to store more output data
1095 * @last: End of output data buffer
1096 *
1097 * Generate a generic MODE SENSE control mode page.
1098 *
1099 * LOCKING:
1100 * None.
1101 */
1102
1103static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1104{
1105 const u8 page[] = {0xa, 0xa, 6, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30};
1106
1107 /* byte 2: set the descriptor format sense data bit (bit 2)
1108 * since we need to support returning this format for SAT
1109 * commands and any SCSI commands against a 48b LBA device.
1110 */
1111
1112 ata_msense_push(ptr_io, last, page, sizeof(page));
1113 return sizeof(page);
1114}
1115
1116/**
1117 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1118 * @dev: Device associated with this MODE SENSE command
1119 * @ptr_io: (input/output) Location to store more output data
1120 * @last: End of output data buffer
1121 *
1122 * Generate a generic MODE SENSE r/w error recovery page.
1123 *
1124 * LOCKING:
1125 * None.
1126 */
1127
1128static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1129{
1130 const u8 page[] = {
1131 0x1, /* page code */
1132 0xa, /* page length */
1133 (1 << 7) | (1 << 6), /* note auto r/w reallocation */
1134 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
1135 };
1136
1137 ata_msense_push(ptr_io, last, page, sizeof(page));
1138 return sizeof(page);
1139}
1140
1141/**
1142 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1143 * @args: device IDENTIFY data / SCSI command of interest.
1144 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1145 * @buflen: Response buffer length.
1146 *
1147 * Simulate MODE SENSE commands.
1148 *
1149 * LOCKING:
1150 * spin_lock_irqsave(host_set lock)
1151 */
1152
1153unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1154 unsigned int buflen)
1155{
1156 u8 *scsicmd = args->cmd->cmnd, *p, *last;
1157 unsigned int page_control, six_byte, output_len;
1158
1159 VPRINTK("ENTER\n");
1160
1161 six_byte = (scsicmd[0] == MODE_SENSE);
1162
1163 /* we only support saved and current values (which we treat
1164 * in the same manner)
1165 */
1166 page_control = scsicmd[2] >> 6;
1167 if ((page_control != 0) && (page_control != 3))
1168 return 1;
1169
1170 if (six_byte)
1171 output_len = 4;
1172 else
1173 output_len = 8;
1174
1175 p = rbuf + output_len;
1176 last = rbuf + buflen - 1;
1177
1178 switch(scsicmd[2] & 0x3f) {
1179 case 0x01: /* r/w error recovery */
1180 output_len += ata_msense_rw_recovery(&p, last);
1181 break;
1182
1183 case 0x08: /* caching */
1184 output_len += ata_msense_caching(args->id, &p, last);
1185 break;
1186
1187 case 0x0a: { /* control mode */
1188 output_len += ata_msense_ctl_mode(&p, last);
1189 break;
1190 }
1191
1192 case 0x3f: /* all pages */
1193 output_len += ata_msense_rw_recovery(&p, last);
1194 output_len += ata_msense_caching(args->id, &p, last);
1195 output_len += ata_msense_ctl_mode(&p, last);
1196 break;
1197
1198 default: /* invalid page code */
1199 return 1;
1200 }
1201
1202 if (six_byte) {
1203 output_len--;
1204 rbuf[0] = output_len;
1205 } else {
1206 output_len -= 2;
1207 rbuf[0] = output_len >> 8;
1208 rbuf[1] = output_len;
1209 }
1210
1211 return 0;
1212}
1213
1214/**
1215 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
1216 * @args: device IDENTIFY data / SCSI command of interest.
1217 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1218 * @buflen: Response buffer length.
1219 *
1220 * Simulate READ CAPACITY commands.
1221 *
1222 * LOCKING:
1223 * spin_lock_irqsave(host_set lock)
1224 */
1225
1226unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
1227 unsigned int buflen)
1228{
1229 u64 n_sectors;
1230 u32 tmp;
1231
1232 VPRINTK("ENTER\n");
1233
Albert Lee8bf62ec2005-05-12 15:29:42 -04001234 if (ata_id_has_lba(args->id)) {
1235 if (ata_id_has_lba48(args->id))
1236 n_sectors = ata_id_u64(args->id, 100);
1237 else
1238 n_sectors = ata_id_u32(args->id, 60);
1239 } else {
1240 /* CHS default translation */
1241 n_sectors = args->id[1] * args->id[3] * args->id[6];
1242
1243 if (ata_id_current_chs_valid(args->id))
1244 /* CHS current translation */
1245 n_sectors = ata_id_u32(args->id, 57);
1246 }
1247
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 n_sectors--; /* ATA TotalUserSectors - 1 */
1249
1250 tmp = n_sectors; /* note: truncates, if lba48 */
1251 if (args->cmd->cmnd[0] == READ_CAPACITY) {
1252 /* sector count, 32-bit */
1253 rbuf[0] = tmp >> (8 * 3);
1254 rbuf[1] = tmp >> (8 * 2);
1255 rbuf[2] = tmp >> (8 * 1);
1256 rbuf[3] = tmp;
1257
1258 /* sector size */
1259 tmp = ATA_SECT_SIZE;
1260 rbuf[6] = tmp >> 8;
1261 rbuf[7] = tmp;
1262
1263 } else {
1264 /* sector count, 64-bit */
1265 rbuf[2] = n_sectors >> (8 * 7);
1266 rbuf[3] = n_sectors >> (8 * 6);
1267 rbuf[4] = n_sectors >> (8 * 5);
1268 rbuf[5] = n_sectors >> (8 * 4);
1269 rbuf[6] = tmp >> (8 * 3);
1270 rbuf[7] = tmp >> (8 * 2);
1271 rbuf[8] = tmp >> (8 * 1);
1272 rbuf[9] = tmp;
1273
1274 /* sector size */
1275 tmp = ATA_SECT_SIZE;
1276 rbuf[12] = tmp >> 8;
1277 rbuf[13] = tmp;
1278 }
1279
1280 return 0;
1281}
1282
1283/**
1284 * ata_scsiop_report_luns - Simulate REPORT LUNS command
1285 * @args: device IDENTIFY data / SCSI command of interest.
1286 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1287 * @buflen: Response buffer length.
1288 *
1289 * Simulate REPORT LUNS command.
1290 *
1291 * LOCKING:
1292 * spin_lock_irqsave(host_set lock)
1293 */
1294
1295unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1296 unsigned int buflen)
1297{
1298 VPRINTK("ENTER\n");
1299 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
1300
1301 return 0;
1302}
1303
1304/**
1305 * ata_scsi_badcmd - End a SCSI request with an error
1306 * @cmd: SCSI request to be handled
1307 * @done: SCSI command completion function
1308 * @asc: SCSI-defined additional sense code
1309 * @ascq: SCSI-defined additional sense code qualifier
1310 *
1311 * Helper function that completes a SCSI command with
1312 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
1313 * and the specified additional sense codes.
1314 *
1315 * LOCKING:
1316 * spin_lock_irqsave(host_set lock)
1317 */
1318
1319void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
1320{
1321 DPRINTK("ENTER\n");
1322 cmd->result = SAM_STAT_CHECK_CONDITION;
1323
1324 cmd->sense_buffer[0] = 0x70;
1325 cmd->sense_buffer[2] = ILLEGAL_REQUEST;
1326 cmd->sense_buffer[7] = 14 - 8; /* addnl. sense len. FIXME: correct? */
1327 cmd->sense_buffer[12] = asc;
1328 cmd->sense_buffer[13] = ascq;
1329
1330 done(cmd);
1331}
1332
1333static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
1334{
1335 struct scsi_cmnd *cmd = qc->scsicmd;
1336
1337 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) {
1338 DPRINTK("request check condition\n");
1339
1340 cmd->result = SAM_STAT_CHECK_CONDITION;
1341
1342 qc->scsidone(cmd);
1343
1344 return 1;
1345 } else {
1346 u8 *scsicmd = cmd->cmnd;
1347
1348 if (scsicmd[0] == INQUIRY) {
1349 u8 *buf = NULL;
1350 unsigned int buflen;
1351
1352 buflen = ata_scsi_rbuf_get(cmd, &buf);
1353 buf[2] = 0x5;
1354 buf[3] = (buf[3] & 0xf0) | 2;
1355 ata_scsi_rbuf_put(cmd, buf);
1356 }
1357 cmd->result = SAM_STAT_GOOD;
1358 }
1359
1360 qc->scsidone(cmd);
1361
1362 return 0;
1363}
1364/**
1365 * atapi_xlat - Initialize PACKET taskfile
1366 * @qc: command structure to be initialized
1367 * @scsicmd: SCSI CDB associated with this PACKET command
1368 *
1369 * LOCKING:
1370 * spin_lock_irqsave(host_set lock)
1371 *
1372 * RETURNS:
1373 * Zero on success, non-zero on failure.
1374 */
1375
1376static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
1377{
1378 struct scsi_cmnd *cmd = qc->scsicmd;
1379 struct ata_device *dev = qc->dev;
1380 int using_pio = (dev->flags & ATA_DFLAG_PIO);
be7db052005-04-17 15:26:13 -05001381 int nodata = (cmd->sc_data_direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382
1383 if (!using_pio)
1384 /* Check whether ATAPI DMA is safe */
1385 if (ata_check_atapi_dma(qc))
1386 using_pio = 1;
1387
1388 memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
1389
1390 qc->complete_fn = atapi_qc_complete;
1391
1392 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
be7db052005-04-17 15:26:13 -05001393 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394 qc->tf.flags |= ATA_TFLAG_WRITE;
1395 DPRINTK("direction: write\n");
1396 }
1397
1398 qc->tf.command = ATA_CMD_PACKET;
1399
1400 /* no data, or PIO data xfer */
1401 if (using_pio || nodata) {
1402 if (nodata)
1403 qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
1404 else
1405 qc->tf.protocol = ATA_PROT_ATAPI;
1406 qc->tf.lbam = (8 * 1024) & 0xff;
1407 qc->tf.lbah = (8 * 1024) >> 8;
1408 }
1409
1410 /* DMA data xfer */
1411 else {
1412 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
1413 qc->tf.feature |= ATAPI_PKT_DMA;
1414
1415#ifdef ATAPI_ENABLE_DMADIR
1416 /* some SATA bridges need us to indicate data xfer direction */
be7db052005-04-17 15:26:13 -05001417 if (cmd->sc_data_direction != DMA_TO_DEVICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001418 qc->tf.feature |= ATAPI_DMADIR;
1419#endif
1420 }
1421
1422 qc->nbytes = cmd->bufflen;
1423
1424 return 0;
1425}
1426
1427/**
1428 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
1429 * @ap: ATA port to which the device is attached
1430 * @scsidev: SCSI device from which we derive the ATA device
1431 *
1432 * Given various information provided in struct scsi_cmnd,
1433 * map that onto an ATA bus, and using that mapping
1434 * determine which ata_device is associated with the
1435 * SCSI command to be sent.
1436 *
1437 * LOCKING:
1438 * spin_lock_irqsave(host_set lock)
1439 *
1440 * RETURNS:
1441 * Associated ATA device, or %NULL if not found.
1442 */
1443
1444static struct ata_device *
1445ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev)
1446{
1447 struct ata_device *dev;
1448
1449 /* skip commands not addressed to targets we simulate */
1450 if (likely(scsidev->id < ATA_MAX_DEVICES))
1451 dev = &ap->device[scsidev->id];
1452 else
1453 return NULL;
1454
1455 if (unlikely((scsidev->channel != 0) ||
1456 (scsidev->lun != 0)))
1457 return NULL;
1458
1459 if (unlikely(!ata_dev_present(dev)))
1460 return NULL;
1461
1462#ifndef ATA_ENABLE_ATAPI
1463 if (unlikely(dev->class == ATA_DEV_ATAPI))
1464 return NULL;
1465#endif
1466
1467 return dev;
1468}
1469
1470/**
1471 * ata_get_xlat_func - check if SCSI to ATA translation is possible
1472 * @dev: ATA device
1473 * @cmd: SCSI command opcode to consider
1474 *
1475 * Look up the SCSI command given, and determine whether the
1476 * SCSI command is to be translated or simulated.
1477 *
1478 * RETURNS:
1479 * Pointer to translation function if possible, %NULL if not.
1480 */
1481
1482static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
1483{
1484 switch (cmd) {
1485 case READ_6:
1486 case READ_10:
1487 case READ_16:
1488
1489 case WRITE_6:
1490 case WRITE_10:
1491 case WRITE_16:
1492 return ata_scsi_rw_xlat;
1493
1494 case SYNCHRONIZE_CACHE:
1495 if (ata_try_flush_cache(dev))
1496 return ata_scsi_flush_xlat;
1497 break;
1498
1499 case VERIFY:
1500 case VERIFY_16:
1501 return ata_scsi_verify_xlat;
1502 }
1503
1504 return NULL;
1505}
1506
1507/**
1508 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
1509 * @ap: ATA port to which the command was being sent
1510 * @cmd: SCSI command to dump
1511 *
1512 * Prints the contents of a SCSI command via printk().
1513 */
1514
1515static inline void ata_scsi_dump_cdb(struct ata_port *ap,
1516 struct scsi_cmnd *cmd)
1517{
1518#ifdef ATA_DEBUG
1519 struct scsi_device *scsidev = cmd->device;
1520 u8 *scsicmd = cmd->cmnd;
1521
1522 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
1523 ap->id,
1524 scsidev->channel, scsidev->id, scsidev->lun,
1525 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
1526 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
1527 scsicmd[8]);
1528#endif
1529}
1530
1531/**
1532 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
1533 * @cmd: SCSI command to be sent
1534 * @done: Completion function, called when command is complete
1535 *
1536 * In some cases, this function translates SCSI commands into
1537 * ATA taskfiles, and queues the taskfiles to be sent to
1538 * hardware. In other cases, this function simulates a
1539 * SCSI device by evaluating and responding to certain
1540 * SCSI commands. This creates the overall effect of
1541 * ATA and ATAPI devices appearing as SCSI devices.
1542 *
1543 * LOCKING:
1544 * Releases scsi-layer-held lock, and obtains host_set lock.
1545 *
1546 * RETURNS:
1547 * Zero.
1548 */
1549
1550int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
1551{
1552 struct ata_port *ap;
1553 struct ata_device *dev;
1554 struct scsi_device *scsidev = cmd->device;
1555
1556 ap = (struct ata_port *) &scsidev->host->hostdata[0];
1557
1558 ata_scsi_dump_cdb(ap, cmd);
1559
1560 dev = ata_scsi_find_dev(ap, scsidev);
1561 if (unlikely(!dev)) {
1562 cmd->result = (DID_BAD_TARGET << 16);
1563 done(cmd);
1564 goto out_unlock;
1565 }
1566
1567 if (dev->class == ATA_DEV_ATA) {
1568 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
1569 cmd->cmnd[0]);
1570
1571 if (xlat_func)
1572 ata_scsi_translate(ap, dev, cmd, done, xlat_func);
1573 else
1574 ata_scsi_simulate(dev->id, cmd, done);
1575 } else
1576 ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
1577
1578out_unlock:
1579 return 0;
1580}
1581
1582/**
1583 * ata_scsi_simulate - simulate SCSI command on ATA device
1584 * @id: current IDENTIFY data for target device.
1585 * @cmd: SCSI command being sent to device.
1586 * @done: SCSI command completion function.
1587 *
1588 * Interprets and directly executes a select list of SCSI commands
1589 * that can be handled internally.
1590 *
1591 * LOCKING:
1592 * spin_lock_irqsave(host_set lock)
1593 */
1594
1595void ata_scsi_simulate(u16 *id,
1596 struct scsi_cmnd *cmd,
1597 void (*done)(struct scsi_cmnd *))
1598{
1599 struct ata_scsi_args args;
1600 u8 *scsicmd = cmd->cmnd;
1601
1602 args.id = id;
1603 args.cmd = cmd;
1604 args.done = done;
1605
1606 switch(scsicmd[0]) {
1607 /* no-op's, complete with success */
1608 case SYNCHRONIZE_CACHE:
1609 case REZERO_UNIT:
1610 case SEEK_6:
1611 case SEEK_10:
1612 case TEST_UNIT_READY:
1613 case FORMAT_UNIT: /* FIXME: correct? */
1614 case SEND_DIAGNOSTIC: /* FIXME: correct? */
1615 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
1616 break;
1617
1618 case INQUIRY:
1619 if (scsicmd[1] & 2) /* is CmdDt set? */
1620 ata_bad_cdb(cmd, done);
1621 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
1622 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
1623 else if (scsicmd[2] == 0x00)
1624 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
1625 else if (scsicmd[2] == 0x80)
1626 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
1627 else if (scsicmd[2] == 0x83)
1628 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
1629 else
1630 ata_bad_cdb(cmd, done);
1631 break;
1632
1633 case MODE_SENSE:
1634 case MODE_SENSE_10:
1635 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
1636 break;
1637
1638 case MODE_SELECT: /* unconditionally return */
1639 case MODE_SELECT_10: /* bad-field-in-cdb */
1640 ata_bad_cdb(cmd, done);
1641 break;
1642
1643 case READ_CAPACITY:
1644 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1645 break;
1646
1647 case SERVICE_ACTION_IN:
1648 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
1649 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1650 else
1651 ata_bad_cdb(cmd, done);
1652 break;
1653
1654 case REPORT_LUNS:
1655 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
1656 break;
1657
1658 /* mandantory commands we haven't implemented yet */
1659 case REQUEST_SENSE:
1660
1661 /* all other commands */
1662 default:
1663 ata_bad_scsiop(cmd, done);
1664 break;
1665 }
1666}
1667