aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/libata-scsi.c
diff options
context:
space:
mode:
authorJeff Garzik <jgarzik@pobox.com>2005-10-28 21:32:01 -0400
committerJeff Garzik <jgarzik@pobox.com>2005-10-28 21:32:01 -0400
commit5615ca7906aefbdc3318604c89db5931d0a25910 (patch)
treec34bcc7e314f49005ad88ac84c908128729c0329 /drivers/scsi/libata-scsi.c
parent7a9f8f93d2dad38f30fbc79d8a1e6517373aa4b6 (diff)
parent9dfb7808fb05643b0d06df7411b94d9546696bf1 (diff)
Merge branch 'upstream'
Diffstat (limited to 'drivers/scsi/libata-scsi.c')
-rw-r--r--drivers/scsi/libata-scsi.c786
1 files changed, 656 insertions, 130 deletions
diff --git a/drivers/scsi/libata-scsi.c b/drivers/scsi/libata-scsi.c
index a68c0341dc82..69058510f43a 100644
--- a/drivers/scsi/libata-scsi.c
+++ b/drivers/scsi/libata-scsi.c
@@ -40,14 +40,56 @@
40#include "scsi.h" 40#include "scsi.h"
41#include <scsi/scsi_host.h> 41#include <scsi/scsi_host.h>
42#include <linux/libata.h> 42#include <linux/libata.h>
43#include <linux/hdreg.h>
43#include <asm/uaccess.h> 44#include <asm/uaccess.h>
44 45
45#include "libata.h" 46#include "libata.h"
46 47
48#define SECTOR_SIZE 512
49
47typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, const u8 *scsicmd); 50typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, const u8 *scsicmd);
48static struct ata_device * 51static struct ata_device *
49ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev); 52ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev);
50 53
54#define RW_RECOVERY_MPAGE 0x1
55#define RW_RECOVERY_MPAGE_LEN 12
56#define CACHE_MPAGE 0x8
57#define CACHE_MPAGE_LEN 20
58#define CONTROL_MPAGE 0xa
59#define CONTROL_MPAGE_LEN 12
60#define ALL_MPAGES 0x3f
61#define ALL_SUB_MPAGES 0xff
62
63
64static const u8 def_rw_recovery_mpage[] = {
65 RW_RECOVERY_MPAGE,
66 RW_RECOVERY_MPAGE_LEN - 2,
67 (1 << 7) | /* AWRE, sat-r06 say it shall be 0 */
68 (1 << 6), /* ARRE (auto read reallocation) */
69 0, /* read retry count */
70 0, 0, 0, 0,
71 0, /* write retry count */
72 0, 0, 0
73};
74
75static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
76 CACHE_MPAGE,
77 CACHE_MPAGE_LEN - 2,
78 0, /* contains WCE, needs to be 0 for logic */
79 0, 0, 0, 0, 0, 0, 0, 0, 0,
80 0, /* contains DRA, needs to be 0 for logic */
81 0, 0, 0, 0, 0, 0, 0
82};
83
84static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
85 CONTROL_MPAGE,
86 CONTROL_MPAGE_LEN - 2,
87 2, /* DSENSE=0, GLTSD=1 */
88 0, /* [QAM+QERR may be 1, see 05-359r1] */
89 0, 0, 0, 0, 0xff, 0xff,
90 0, 30 /* extended self test time, see 05-359r1 */
91};
92
51 93
52static void ata_scsi_invalid_field(struct scsi_cmnd *cmd, 94static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
53 void (*done)(struct scsi_cmnd *)) 95 void (*done)(struct scsi_cmnd *))
@@ -86,6 +128,150 @@ int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
86 return 0; 128 return 0;
87} 129}
88 130
131/**
132 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
133 * @dev: Device to whom we are issuing command
134 * @arg: User provided data for issuing command
135 *
136 * LOCKING:
137 * Defined by the SCSI layer. We don't really care.
138 *
139 * RETURNS:
140 * Zero on success, negative errno on error.
141 */
142
143int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
144{
145 int rc = 0;
146 u8 scsi_cmd[MAX_COMMAND_SIZE];
147 u8 args[4], *argbuf = NULL;
148 int argsize = 0;
149 struct scsi_request *sreq;
150
151 if (NULL == (void *)arg)
152 return -EINVAL;
153
154 if (copy_from_user(args, arg, sizeof(args)))
155 return -EFAULT;
156
157 sreq = scsi_allocate_request(scsidev, GFP_KERNEL);
158 if (!sreq)
159 return -EINTR;
160
161 memset(scsi_cmd, 0, sizeof(scsi_cmd));
162
163 if (args[3]) {
164 argsize = SECTOR_SIZE * args[3];
165 argbuf = kmalloc(argsize, GFP_KERNEL);
166 if (argbuf == NULL) {
167 rc = -ENOMEM;
168 goto error;
169 }
170
171 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
172 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
173 block count in sector count field */
174 sreq->sr_data_direction = DMA_FROM_DEVICE;
175 } else {
176 scsi_cmd[1] = (3 << 1); /* Non-data */
177 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
178 sreq->sr_data_direction = DMA_NONE;
179 }
180
181 scsi_cmd[0] = ATA_16;
182
183 scsi_cmd[4] = args[2];
184 if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
185 scsi_cmd[6] = args[3];
186 scsi_cmd[8] = args[1];
187 scsi_cmd[10] = 0x4f;
188 scsi_cmd[12] = 0xc2;
189 } else {
190 scsi_cmd[6] = args[1];
191 }
192 scsi_cmd[14] = args[0];
193
194 /* Good values for timeout and retries? Values below
195 from scsi_ioctl_send_command() for default case... */
196 scsi_wait_req(sreq, scsi_cmd, argbuf, argsize, (10*HZ), 5);
197
198 if (sreq->sr_result) {
199 rc = -EIO;
200 goto error;
201 }
202
203 /* Need code to retrieve data from check condition? */
204
205 if ((argbuf)
206 && copy_to_user((void *)(arg + sizeof(args)), argbuf, argsize))
207 rc = -EFAULT;
208error:
209 scsi_release_request(sreq);
210
211 if (argbuf)
212 kfree(argbuf);
213
214 return rc;
215}
216
217/**
218 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
219 * @dev: Device to whom we are issuing command
220 * @arg: User provided data for issuing command
221 *
222 * LOCKING:
223 * Defined by the SCSI layer. We don't really care.
224 *
225 * RETURNS:
226 * Zero on success, negative errno on error.
227 */
228int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
229{
230 int rc = 0;
231 u8 scsi_cmd[MAX_COMMAND_SIZE];
232 u8 args[7];
233 struct scsi_request *sreq;
234
235 if (NULL == (void *)arg)
236 return -EINVAL;
237
238 if (copy_from_user(args, arg, sizeof(args)))
239 return -EFAULT;
240
241 memset(scsi_cmd, 0, sizeof(scsi_cmd));
242 scsi_cmd[0] = ATA_16;
243 scsi_cmd[1] = (3 << 1); /* Non-data */
244 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
245 scsi_cmd[4] = args[1];
246 scsi_cmd[6] = args[2];
247 scsi_cmd[8] = args[3];
248 scsi_cmd[10] = args[4];
249 scsi_cmd[12] = args[5];
250 scsi_cmd[14] = args[0];
251
252 sreq = scsi_allocate_request(scsidev, GFP_KERNEL);
253 if (!sreq) {
254 rc = -EINTR;
255 goto error;
256 }
257
258 sreq->sr_data_direction = DMA_NONE;
259 /* Good values for timeout and retries? Values below
260 from scsi_ioctl_send_command() for default case... */
261 scsi_wait_req(sreq, scsi_cmd, NULL, 0, (10*HZ), 5);
262
263 if (sreq->sr_result) {
264 rc = -EIO;
265 goto error;
266 }
267
268 /* Need code to retrieve data from check condition? */
269
270error:
271 scsi_release_request(sreq);
272 return rc;
273}
274
89int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg) 275int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
90{ 276{
91 struct ata_port *ap; 277 struct ata_port *ap;
@@ -115,6 +301,16 @@ int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
115 return -EINVAL; 301 return -EINVAL;
116 return 0; 302 return 0;
117 303
304 case HDIO_DRIVE_CMD:
305 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
306 return -EACCES;
307 return ata_cmd_ioctl(scsidev, arg);
308
309 case HDIO_DRIVE_TASK:
310 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
311 return -EACCES;
312 return ata_task_ioctl(scsidev, arg);
313
118 default: 314 default:
119 rc = -ENOTTY; 315 rc = -ENOTTY;
120 break; 316 break;
@@ -173,23 +369,70 @@ struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
173} 369}
174 370
175/** 371/**
372 * ata_dump_status - user friendly display of error info
373 * @id: id of the port in question
374 * @tf: ptr to filled out taskfile
375 *
376 * Decode and dump the ATA error/status registers for the user so
377 * that they have some idea what really happened at the non
378 * make-believe layer.
379 *
380 * LOCKING:
381 * inherited from caller
382 */
383void ata_dump_status(unsigned id, struct ata_taskfile *tf)
384{
385 u8 stat = tf->command, err = tf->feature;
386
387 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
388 if (stat & ATA_BUSY) {
389 printk("Busy }\n"); /* Data is not valid in this case */
390 } else {
391 if (stat & 0x40) printk("DriveReady ");
392 if (stat & 0x20) printk("DeviceFault ");
393 if (stat & 0x10) printk("SeekComplete ");
394 if (stat & 0x08) printk("DataRequest ");
395 if (stat & 0x04) printk("CorrectedError ");
396 if (stat & 0x02) printk("Index ");
397 if (stat & 0x01) printk("Error ");
398 printk("}\n");
399
400 if (err) {
401 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
402 if (err & 0x04) printk("DriveStatusError ");
403 if (err & 0x80) {
404 if (err & 0x04) printk("BadCRC ");
405 else printk("Sector ");
406 }
407 if (err & 0x40) printk("UncorrectableError ");
408 if (err & 0x10) printk("SectorIdNotFound ");
409 if (err & 0x02) printk("TrackZeroNotFound ");
410 if (err & 0x01) printk("AddrMarkNotFound ");
411 printk("}\n");
412 }
413 }
414}
415
416/**
176 * ata_to_sense_error - convert ATA error to SCSI error 417 * ata_to_sense_error - convert ATA error to SCSI error
177 * @qc: Command that we are erroring out
178 * @drv_stat: value contained in ATA status register 418 * @drv_stat: value contained in ATA status register
419 * @drv_err: value contained in ATA error register
420 * @sk: the sense key we'll fill out
421 * @asc: the additional sense code we'll fill out
422 * @ascq: the additional sense code qualifier we'll fill out
179 * 423 *
180 * Converts an ATA error into a SCSI error. While we are at it 424 * Converts an ATA error into a SCSI error. Fill out pointers to
181 * we decode and dump the ATA error for the user so that they 425 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
182 * have some idea what really happened at the non make-believe 426 * format sense blocks.
183 * layer.
184 * 427 *
185 * LOCKING: 428 * LOCKING:
186 * spin_lock_irqsave(host_set lock) 429 * spin_lock_irqsave(host_set lock)
187 */ 430 */
188 431void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
189void ata_to_sense_error(struct ata_queued_cmd *qc, u8 drv_stat) 432 u8 *ascq)
190{ 433{
191 struct scsi_cmnd *cmd = qc->scsicmd; 434 int i;
192 u8 err = 0; 435
193 /* Based on the 3ware driver translation table */ 436 /* Based on the 3ware driver translation table */
194 static unsigned char sense_table[][4] = { 437 static unsigned char sense_table[][4] = {
195 /* BBD|ECC|ID|MAR */ 438 /* BBD|ECC|ID|MAR */
@@ -230,96 +473,184 @@ void ata_to_sense_error(struct ata_queued_cmd *qc, u8 drv_stat)
230 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered 473 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
231 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark 474 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
232 }; 475 };
233 int i = 0;
234 476
235 /* 477 /*
236 * Is this an error we can process/parse 478 * Is this an error we can process/parse
237 */ 479 */
238 480 if (drv_stat & ATA_BUSY) {
239 if(drv_stat & ATA_ERR) 481 drv_err = 0; /* Ignore the err bits, they're invalid */
240 /* Read the err bits */
241 err = ata_chk_err(qc->ap);
242
243 /* Display the ATA level error info */
244
245 printk(KERN_WARNING "ata%u: status=0x%02x { ", qc->ap->id, drv_stat);
246 if(drv_stat & 0x80)
247 {
248 printk("Busy ");
249 err = 0; /* Data is not valid in this case */
250 } 482 }
251 else { 483
252 if(drv_stat & 0x40) printk("DriveReady "); 484 if (drv_err) {
253 if(drv_stat & 0x20) printk("DeviceFault "); 485 /* Look for drv_err */
254 if(drv_stat & 0x10) printk("SeekComplete "); 486 for (i = 0; sense_table[i][0] != 0xFF; i++) {
255 if(drv_stat & 0x08) printk("DataRequest "); 487 /* Look for best matches first */
256 if(drv_stat & 0x04) printk("CorrectedError "); 488 if ((sense_table[i][0] & drv_err) ==
257 if(drv_stat & 0x02) printk("Index "); 489 sense_table[i][0]) {
258 if(drv_stat & 0x01) printk("Error "); 490 *sk = sense_table[i][1];
491 *asc = sense_table[i][2];
492 *ascq = sense_table[i][3];
493 goto translate_done;
494 }
495 }
496 /* No immediate match */
497 printk(KERN_WARNING "ata%u: no sense translation for "
498 "error 0x%02x\n", id, drv_err);
259 } 499 }
260 printk("}\n"); 500
261 501 /* Fall back to interpreting status bits */
262 if(err) 502 for (i = 0; stat_table[i][0] != 0xFF; i++) {
263 { 503 if (stat_table[i][0] & drv_stat) {
264 printk(KERN_WARNING "ata%u: error=0x%02x { ", qc->ap->id, err); 504 *sk = stat_table[i][1];
265 if(err & 0x04) printk("DriveStatusError "); 505 *asc = stat_table[i][2];
266 if(err & 0x80) 506 *ascq = stat_table[i][3];
267 { 507 goto translate_done;
268 if(err & 0x04)
269 printk("BadCRC ");
270 else
271 printk("Sector ");
272 } 508 }
273 if(err & 0x40) printk("UncorrectableError "); 509 }
274 if(err & 0x10) printk("SectorIdNotFound "); 510 /* No error? Undecoded? */
275 if(err & 0x02) printk("TrackZeroNotFound "); 511 printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n",
276 if(err & 0x01) printk("AddrMarkNotFound "); 512 id, drv_stat);
277 printk("}\n");
278 513
279 /* Should we dump sector info here too ?? */ 514 /* For our last chance pick, use medium read error because
515 * it's much more common than an ATA drive telling you a write
516 * has failed.
517 */
518 *sk = MEDIUM_ERROR;
519 *asc = 0x11; /* "unrecovered read error" */
520 *ascq = 0x04; /* "auto-reallocation failed" */
521
522 translate_done:
523 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to "
524 "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err,
525 *sk, *asc, *ascq);
526 return;
527}
528
529/*
530 * ata_gen_ata_desc_sense - Generate check condition sense block.
531 * @qc: Command that completed.
532 *
533 * This function is specific to the ATA descriptor format sense
534 * block specified for the ATA pass through commands. Regardless
535 * of whether the command errored or not, return a sense
536 * block. Copy all controller registers into the sense
537 * block. Clear sense key, ASC & ASCQ if there is no error.
538 *
539 * LOCKING:
540 * spin_lock_irqsave(host_set lock)
541 */
542void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
543{
544 struct scsi_cmnd *cmd = qc->scsicmd;
545 struct ata_taskfile *tf = &qc->tf;
546 unsigned char *sb = cmd->sense_buffer;
547 unsigned char *desc = sb + 8;
548
549 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
550
551 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
552
553 /*
554 * Read the controller registers.
555 */
556 assert(NULL != qc->ap->ops->tf_read);
557 qc->ap->ops->tf_read(qc->ap, tf);
558
559 /*
560 * Use ata_to_sense_error() to map status register bits
561 * onto sense key, asc & ascq.
562 */
563 if (unlikely(tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ))) {
564 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
565 &sb[1], &sb[2], &sb[3]);
566 sb[1] &= 0x0f;
280 } 567 }
281 568
569 /*
570 * Sense data is current and format is descriptor.
571 */
572 sb[0] = 0x72;
282 573
283 /* Look for err */ 574 desc[0] = 0x09;
284 while(sense_table[i][0] != 0xFF) 575
285 { 576 /*
286 /* Look for best matches first */ 577 * Set length of additional sense data.
287 if((sense_table[i][0] & err) == sense_table[i][0]) 578 * Since we only populate descriptor 0, the total
288 { 579 * length is the same (fixed) length as descriptor 0.
289 ata_scsi_set_sense(cmd, sense_table[i][1] /* sk */, 580 */
290 sense_table[i][2] /* asc */, 581 desc[1] = sb[7] = 14;
291 sense_table[i][3] /* ascq */ ); 582
292 return; 583 /*
293 } 584 * Copy registers into sense buffer.
294 i++; 585 */
586 desc[2] = 0x00;
587 desc[3] = tf->feature; /* == error reg */
588 desc[5] = tf->nsect;
589 desc[7] = tf->lbal;
590 desc[9] = tf->lbam;
591 desc[11] = tf->lbah;
592 desc[12] = tf->device;
593 desc[13] = tf->command; /* == status reg */
594
595 /*
596 * Fill in Extend bit, and the high order bytes
597 * if applicable.
598 */
599 if (tf->flags & ATA_TFLAG_LBA48) {
600 desc[2] |= 0x01;
601 desc[4] = tf->hob_nsect;
602 desc[6] = tf->hob_lbal;
603 desc[8] = tf->hob_lbam;
604 desc[10] = tf->hob_lbah;
295 } 605 }
296 /* No immediate match */ 606}
297 if(err)
298 printk(KERN_DEBUG "ata%u: no sense translation for 0x%02x\n", qc->ap->id, err);
299 607
300 i = 0; 608/**
301 /* Fall back to interpreting status bits */ 609 * ata_gen_fixed_sense - generate a SCSI fixed sense block
302 while(stat_table[i][0] != 0xFF) 610 * @qc: Command that we are erroring out
303 { 611 *
304 if(stat_table[i][0] & drv_stat) 612 * Leverage ata_to_sense_error() to give us the codes. Fit our
305 { 613 * LBA in here if there's room.
306 ata_scsi_set_sense(cmd, sense_table[i][1] /* sk */, 614 *
307 sense_table[i][2] /* asc */, 615 * LOCKING:
308 sense_table[i][3] /* ascq */ ); 616 * inherited from caller
309 return; 617 */
310 } 618void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
311 i++; 619{
620 struct scsi_cmnd *cmd = qc->scsicmd;
621 struct ata_taskfile *tf = &qc->tf;
622 unsigned char *sb = cmd->sense_buffer;
623
624 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
625
626 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
627
628 /*
629 * Read the controller registers.
630 */
631 assert(NULL != qc->ap->ops->tf_read);
632 qc->ap->ops->tf_read(qc->ap, tf);
633
634 /*
635 * Use ata_to_sense_error() to map status register bits
636 * onto sense key, asc & ascq.
637 */
638 if (unlikely(tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ))) {
639 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
640 &sb[2], &sb[12], &sb[13]);
641 sb[2] &= 0x0f;
312 } 642 }
313 /* No error ?? */
314 printk(KERN_ERR "ata%u: called with no error (%02X)!\n", qc->ap->id, drv_stat);
315 /* additional-sense-code[-qualifier] */
316 643
317 if (cmd->sc_data_direction == DMA_FROM_DEVICE) { 644 sb[0] = 0x70;
318 ata_scsi_set_sense(cmd, MEDIUM_ERROR, 0x11, 0x4); 645 sb[7] = 0x0a;
319 /* "unrecovered read error" */ 646
320 } else { 647 if (tf->flags & ATA_TFLAG_LBA && !(tf->flags & ATA_TFLAG_LBA48)) {
321 ata_scsi_set_sense(cmd, MEDIUM_ERROR, 0xc, 0x2); 648 /* A small (28b) LBA will fit in the 32b info field */
322 /* "write error - auto-reallocation failed" */ 649 sb[0] |= 0x80; /* set valid bit */
650 sb[3] = tf->device & 0x0f;
651 sb[4] = tf->lbah;
652 sb[5] = tf->lbam;
653 sb[6] = tf->lbal;
323 } 654 }
324} 655}
325 656
@@ -881,11 +1212,36 @@ nothing_to_do:
881static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat) 1212static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
882{ 1213{
883 struct scsi_cmnd *cmd = qc->scsicmd; 1214 struct scsi_cmnd *cmd = qc->scsicmd;
1215 int need_sense = drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ);
1216
1217 /* For ATA pass thru (SAT) commands, generate a sense block if
1218 * user mandated it or if there's an error. Note that if we
1219 * generate because the user forced us to, a check condition
1220 * is generated and the ATA register values are returned
1221 * whether the command completed successfully or not. If there
1222 * was no error, SK, ASC and ASCQ will all be zero.
1223 */
1224 if (((cmd->cmnd[0] == ATA_16) || (cmd->cmnd[0] == ATA_12)) &&
1225 ((cmd->cmnd[2] & 0x20) || need_sense)) {
1226 ata_gen_ata_desc_sense(qc);
1227 } else {
1228 if (!need_sense) {
1229 cmd->result = SAM_STAT_GOOD;
1230 } else {
1231 /* TODO: decide which descriptor format to use
1232 * for 48b LBA devices and call that here
1233 * instead of the fixed desc, which is only
1234 * good for smaller LBA (and maybe CHS?)
1235 * devices.
1236 */
1237 ata_gen_fixed_sense(qc);
1238 }
1239 }
884 1240
885 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) 1241 if (need_sense) {
886 ata_to_sense_error(qc, drv_stat); 1242 /* The ata_gen_..._sense routines fill in tf */
887 else 1243 ata_dump_status(qc->ap->id, &qc->tf);
888 cmd->result = SAM_STAT_GOOD; 1244 }
889 1245
890 qc->scsidone(cmd); 1246 qc->scsidone(cmd);
891 1247
@@ -1276,13 +1632,9 @@ static void ata_msense_push(u8 **ptr_io, const u8 *last,
1276static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io, 1632static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1277 const u8 *last) 1633 const u8 *last)
1278{ 1634{
1279 u8 page[] = { 1635 u8 page[CACHE_MPAGE_LEN];
1280 0x8, /* page code */
1281 0x12, /* page length */
1282 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 zeroes */
1283 0, 0, 0, 0, 0, 0, 0, 0 /* 8 zeroes */
1284 };
1285 1636
1637 memcpy(page, def_cache_mpage, sizeof(page));
1286 if (ata_id_wcache_enabled(id)) 1638 if (ata_id_wcache_enabled(id))
1287 page[2] |= (1 << 2); /* write cache enable */ 1639 page[2] |= (1 << 2); /* write cache enable */
1288 if (!ata_id_rahead_enabled(id)) 1640 if (!ata_id_rahead_enabled(id))
@@ -1306,15 +1658,9 @@ static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1306 1658
1307static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last) 1659static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1308{ 1660{
1309 const u8 page[] = {0xa, 0xa, 6, 0, 0, 0, 0, 0, 0xff, 0xff, 0, 30}; 1661 ata_msense_push(ptr_io, last, def_control_mpage,
1310 1662 sizeof(def_control_mpage));
1311 /* byte 2: set the descriptor format sense data bit (bit 2) 1663 return sizeof(def_control_mpage);
1312 * since we need to support returning this format for SAT
1313 * commands and any SCSI commands against a 48b LBA device.
1314 */
1315
1316 ata_msense_push(ptr_io, last, page, sizeof(page));
1317 return sizeof(page);
1318} 1664}
1319 1665
1320/** 1666/**
@@ -1331,15 +1677,10 @@ static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1331 1677
1332static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last) 1678static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1333{ 1679{
1334 const u8 page[] = {
1335 0x1, /* page code */
1336 0xa, /* page length */
1337 (1 << 7) | (1 << 6), /* note auto r/w reallocation */
1338 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
1339 };
1340 1680
1341 ata_msense_push(ptr_io, last, page, sizeof(page)); 1681 ata_msense_push(ptr_io, last, def_rw_recovery_mpage,
1342 return sizeof(page); 1682 sizeof(def_rw_recovery_mpage));
1683 return sizeof(def_rw_recovery_mpage);
1343} 1684}
1344 1685
1345/** 1686/**
@@ -1348,7 +1689,9 @@ static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1348 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 1689 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1349 * @buflen: Response buffer length. 1690 * @buflen: Response buffer length.
1350 * 1691 *
1351 * Simulate MODE SENSE commands. 1692 * Simulate MODE SENSE commands. Assume this is invoked for direct
1693 * access devices (e.g. disks) only. There should be no block
1694 * descriptor for other device types.
1352 * 1695 *
1353 * LOCKING: 1696 * LOCKING:
1354 * spin_lock_irqsave(host_set lock) 1697 * spin_lock_irqsave(host_set lock)
@@ -1358,15 +1701,22 @@ unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1358 unsigned int buflen) 1701 unsigned int buflen)
1359{ 1702{
1360 u8 *scsicmd = args->cmd->cmnd, *p, *last; 1703 u8 *scsicmd = args->cmd->cmnd, *p, *last;
1361 unsigned int page_control, six_byte, output_len; 1704 const u8 sat_blk_desc[] = {
1705 0, 0, 0, 0, /* number of blocks: sat unspecified */
1706 0,
1707 0, 0x2, 0x0 /* block length: 512 bytes */
1708 };
1709 u8 pg, spg;
1710 unsigned int ebd, page_control, six_byte, output_len, alloc_len, minlen;
1362 1711
1363 VPRINTK("ENTER\n"); 1712 VPRINTK("ENTER\n");
1364 1713
1365 six_byte = (scsicmd[0] == MODE_SENSE); 1714 six_byte = (scsicmd[0] == MODE_SENSE);
1366 1715 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
1367 /* we only support saved and current values (which we treat 1716 /*
1368 * in the same manner) 1717 * LLBA bit in msense(10) ignored (compliant)
1369 */ 1718 */
1719
1370 page_control = scsicmd[2] >> 6; 1720 page_control = scsicmd[2] >> 6;
1371 switch (page_control) { 1721 switch (page_control) {
1372 case 0: /* current */ 1722 case 0: /* current */
@@ -1379,29 +1729,42 @@ unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1379 goto invalid_fld; 1729 goto invalid_fld;
1380 } 1730 }
1381 1731
1382 if (six_byte) 1732 if (six_byte) {
1383 output_len = 4; 1733 output_len = 4 + (ebd ? 8 : 0);
1384 else 1734 alloc_len = scsicmd[4];
1385 output_len = 8; 1735 } else {
1736 output_len = 8 + (ebd ? 8 : 0);
1737 alloc_len = (scsicmd[7] << 8) + scsicmd[8];
1738 }
1739 minlen = (alloc_len < buflen) ? alloc_len : buflen;
1386 1740
1387 p = rbuf + output_len; 1741 p = rbuf + output_len;
1388 last = rbuf + buflen - 1; 1742 last = rbuf + minlen - 1;
1743
1744 pg = scsicmd[2] & 0x3f;
1745 spg = scsicmd[3];
1746 /*
1747 * No mode subpages supported (yet) but asking for _all_
1748 * subpages may be valid
1749 */
1750 if (spg && (spg != ALL_SUB_MPAGES))
1751 goto invalid_fld;
1389 1752
1390 switch(scsicmd[2] & 0x3f) { 1753 switch(pg) {
1391 case 0x01: /* r/w error recovery */ 1754 case RW_RECOVERY_MPAGE:
1392 output_len += ata_msense_rw_recovery(&p, last); 1755 output_len += ata_msense_rw_recovery(&p, last);
1393 break; 1756 break;
1394 1757
1395 case 0x08: /* caching */ 1758 case CACHE_MPAGE:
1396 output_len += ata_msense_caching(args->id, &p, last); 1759 output_len += ata_msense_caching(args->id, &p, last);
1397 break; 1760 break;
1398 1761
1399 case 0x0a: { /* control mode */ 1762 case CONTROL_MPAGE: {
1400 output_len += ata_msense_ctl_mode(&p, last); 1763 output_len += ata_msense_ctl_mode(&p, last);
1401 break; 1764 break;
1402 } 1765 }
1403 1766
1404 case 0x3f: /* all pages */ 1767 case ALL_MPAGES:
1405 output_len += ata_msense_rw_recovery(&p, last); 1768 output_len += ata_msense_rw_recovery(&p, last);
1406 output_len += ata_msense_caching(args->id, &p, last); 1769 output_len += ata_msense_caching(args->id, &p, last);
1407 output_len += ata_msense_ctl_mode(&p, last); 1770 output_len += ata_msense_ctl_mode(&p, last);
@@ -1411,15 +1774,31 @@ unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1411 goto invalid_fld; 1774 goto invalid_fld;
1412 } 1775 }
1413 1776
1777 if (minlen < 1)
1778 return 0;
1414 if (six_byte) { 1779 if (six_byte) {
1415 output_len--; 1780 output_len--;
1416 rbuf[0] = output_len; 1781 rbuf[0] = output_len;
1782 if (ebd) {
1783 if (minlen > 3)
1784 rbuf[3] = sizeof(sat_blk_desc);
1785 if (minlen > 11)
1786 memcpy(rbuf + 4, sat_blk_desc,
1787 sizeof(sat_blk_desc));
1788 }
1417 } else { 1789 } else {
1418 output_len -= 2; 1790 output_len -= 2;
1419 rbuf[0] = output_len >> 8; 1791 rbuf[0] = output_len >> 8;
1420 rbuf[1] = output_len; 1792 if (minlen > 1)
1793 rbuf[1] = output_len;
1794 if (ebd) {
1795 if (minlen > 7)
1796 rbuf[7] = sizeof(sat_blk_desc);
1797 if (minlen > 15)
1798 memcpy(rbuf + 8, sat_blk_desc,
1799 sizeof(sat_blk_desc));
1800 }
1421 } 1801 }
1422
1423 return 0; 1802 return 0;
1424 1803
1425invalid_fld: 1804invalid_fld:
@@ -1633,7 +2012,12 @@ static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
1633 VPRINTK("ENTER, drv_stat == 0x%x\n", drv_stat); 2012 VPRINTK("ENTER, drv_stat == 0x%x\n", drv_stat);
1634 2013
1635 if (unlikely(drv_stat & (ATA_BUSY | ATA_DRQ))) 2014 if (unlikely(drv_stat & (ATA_BUSY | ATA_DRQ)))
1636 ata_to_sense_error(qc, drv_stat); 2015 /* FIXME: not quite right; we don't want the
2016 * translation of taskfile registers into
2017 * a sense descriptors, since that's only
2018 * correct for ATA, not ATAPI
2019 */
2020 ata_gen_ata_desc_sense(qc);
1637 2021
1638 else if (unlikely(drv_stat & ATA_ERR)) { 2022 else if (unlikely(drv_stat & ATA_ERR)) {
1639 DPRINTK("request check condition\n"); 2023 DPRINTK("request check condition\n");
@@ -1792,6 +2176,143 @@ ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
1792 return dev; 2176 return dev;
1793} 2177}
1794 2178
2179/*
2180 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2181 * @byte1: Byte 1 from pass-thru CDB.
2182 *
2183 * RETURNS:
2184 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2185 */
2186static u8
2187ata_scsi_map_proto(u8 byte1)
2188{
2189 switch((byte1 & 0x1e) >> 1) {
2190 case 3: /* Non-data */
2191 return ATA_PROT_NODATA;
2192
2193 case 6: /* DMA */
2194 return ATA_PROT_DMA;
2195
2196 case 4: /* PIO Data-in */
2197 case 5: /* PIO Data-out */
2198 if (byte1 & 0xe0) {
2199 return ATA_PROT_PIO_MULT;
2200 }
2201 return ATA_PROT_PIO;
2202
2203 case 10: /* Device Reset */
2204 case 0: /* Hard Reset */
2205 case 1: /* SRST */
2206 case 2: /* Bus Idle */
2207 case 7: /* Packet */
2208 case 8: /* DMA Queued */
2209 case 9: /* Device Diagnostic */
2210 case 11: /* UDMA Data-in */
2211 case 12: /* UDMA Data-Out */
2212 case 13: /* FPDMA */
2213 default: /* Reserved */
2214 break;
2215 }
2216
2217 return ATA_PROT_UNKNOWN;
2218}
2219
2220/**
2221 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
2222 * @qc: command structure to be initialized
2223 * @cmd: SCSI command to convert
2224 *
2225 * Handles either 12 or 16-byte versions of the CDB.
2226 *
2227 * RETURNS:
2228 * Zero on success, non-zero on failure.
2229 */
2230static unsigned int
2231ata_scsi_pass_thru(struct ata_queued_cmd *qc, const u8 *scsicmd)
2232{
2233 struct ata_taskfile *tf = &(qc->tf);
2234 struct scsi_cmnd *cmd = qc->scsicmd;
2235
2236 if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
2237 return 1;
2238
2239 /*
2240 * 12 and 16 byte CDBs use different offsets to
2241 * provide the various register values.
2242 */
2243 if (scsicmd[0] == ATA_16) {
2244 /*
2245 * 16-byte CDB - may contain extended commands.
2246 *
2247 * If that is the case, copy the upper byte register values.
2248 */
2249 if (scsicmd[1] & 0x01) {
2250 tf->hob_feature = scsicmd[3];
2251 tf->hob_nsect = scsicmd[5];
2252 tf->hob_lbal = scsicmd[7];
2253 tf->hob_lbam = scsicmd[9];
2254 tf->hob_lbah = scsicmd[11];
2255 tf->flags |= ATA_TFLAG_LBA48;
2256 } else
2257 tf->flags &= ~ATA_TFLAG_LBA48;
2258
2259 /*
2260 * Always copy low byte, device and command registers.
2261 */
2262 tf->feature = scsicmd[4];
2263 tf->nsect = scsicmd[6];
2264 tf->lbal = scsicmd[8];
2265 tf->lbam = scsicmd[10];
2266 tf->lbah = scsicmd[12];
2267 tf->device = scsicmd[13];
2268 tf->command = scsicmd[14];
2269 } else {
2270 /*
2271 * 12-byte CDB - incapable of extended commands.
2272 */
2273 tf->flags &= ~ATA_TFLAG_LBA48;
2274
2275 tf->feature = scsicmd[3];
2276 tf->nsect = scsicmd[4];
2277 tf->lbal = scsicmd[5];
2278 tf->lbam = scsicmd[6];
2279 tf->lbah = scsicmd[7];
2280 tf->device = scsicmd[8];
2281 tf->command = scsicmd[9];
2282 }
2283
2284 /*
2285 * Filter SET_FEATURES - XFER MODE command -- otherwise,
2286 * SET_FEATURES - XFER MODE must be preceded/succeeded
2287 * by an update to hardware-specific registers for each
2288 * controller (i.e. the reason for ->set_piomode(),
2289 * ->set_dmamode(), and ->post_set_mode() hooks).
2290 */
2291 if ((tf->command == ATA_CMD_SET_FEATURES)
2292 && (tf->feature == SETFEATURES_XFER))
2293 return 1;
2294
2295 /*
2296 * Set flags so that all registers will be written,
2297 * and pass on write indication (used for PIO/DMA
2298 * setup.)
2299 */
2300 tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
2301
2302 if (cmd->sc_data_direction == DMA_TO_DEVICE)
2303 tf->flags |= ATA_TFLAG_WRITE;
2304
2305 /*
2306 * Set transfer length.
2307 *
2308 * TODO: find out if we need to do more here to
2309 * cover scatter/gather case.
2310 */
2311 qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
2312
2313 return 0;
2314}
2315
1795/** 2316/**
1796 * ata_get_xlat_func - check if SCSI to ATA translation is possible 2317 * ata_get_xlat_func - check if SCSI to ATA translation is possible
1797 * @dev: ATA device 2318 * @dev: ATA device
@@ -1824,6 +2345,11 @@ static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
1824 case VERIFY: 2345 case VERIFY:
1825 case VERIFY_16: 2346 case VERIFY_16:
1826 return ata_scsi_verify_xlat; 2347 return ata_scsi_verify_xlat;
2348
2349 case ATA_12:
2350 case ATA_16:
2351 return ata_scsi_pass_thru;
2352
1827 case START_STOP: 2353 case START_STOP:
1828 return ata_scsi_start_stop_xlat; 2354 return ata_scsi_start_stop_xlat;
1829 } 2355 }
@@ -1982,7 +2508,7 @@ void ata_scsi_simulate(u16 *id,
1982 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns); 2508 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
1983 break; 2509 break;
1984 2510
1985 /* mandantory commands we haven't implemented yet */ 2511 /* mandatory commands we haven't implemented yet */
1986 case REQUEST_SENSE: 2512 case REQUEST_SENSE:
1987 2513
1988 /* all other commands */ 2514 /* all other commands */