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