aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/libata-scsi.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/scsi/libata-scsi.c')
-rw-r--r--drivers/scsi/libata-scsi.c1506
1 files changed, 1182 insertions, 324 deletions
diff --git a/drivers/scsi/libata-scsi.c b/drivers/scsi/libata-scsi.c
index 104fd9a63e73..1e3792f86fcf 100644
--- a/drivers/scsi/libata-scsi.c
+++ b/drivers/scsi/libata-scsi.c
@@ -40,14 +40,64 @@
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
47typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, u8 *scsicmd); 48#define SECTOR_SIZE 512
48static struct ata_device *
49ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev);
50 49
50typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, const u8 *scsicmd);
51static struct ata_device *
52ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev);
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
93
94static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
95 void (*done)(struct scsi_cmnd *))
96{
97 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
98 /* "Invalid field in cbd" */
99 done(cmd);
100}
51 101
52/** 102/**
53 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd. 103 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
@@ -78,6 +128,150 @@ int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
78 return 0; 128 return 0;
79} 129}
80 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
81int 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)
82{ 276{
83 struct ata_port *ap; 277 struct ata_port *ap;
@@ -107,6 +301,16 @@ int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
107 return -EINVAL; 301 return -EINVAL;
108 return 0; 302 return 0;
109 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
110 default: 314 default:
111 rc = -ENOTTY; 315 rc = -ENOTTY;
112 break; 316 break;
@@ -165,24 +369,70 @@ struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
165} 369}
166 370
167/** 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/**
168 * ata_to_sense_error - convert ATA error to SCSI error 417 * 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 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
171 * 423 *
172 * 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
173 * 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
174 * have some idea what really happened at the non make-believe 426 * format sense blocks.
175 * layer.
176 * 427 *
177 * LOCKING: 428 * LOCKING:
178 * spin_lock_irqsave(host_set lock) 429 * spin_lock_irqsave(host_set lock)
179 */ 430 */
180 431void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
181void ata_to_sense_error(struct ata_queued_cmd *qc, u8 drv_stat) 432 u8 *ascq)
182{ 433{
183 struct scsi_cmnd *cmd = qc->scsicmd; 434 int i;
184 u8 err = 0; 435
185 unsigned char *sb = cmd->sense_buffer;
186 /* Based on the 3ware driver translation table */ 436 /* Based on the 3ware driver translation table */
187 static unsigned char sense_table[][4] = { 437 static unsigned char sense_table[][4] = {
188 /* BBD|ECC|ID|MAR */ 438 /* BBD|ECC|ID|MAR */
@@ -223,105 +473,192 @@ void ata_to_sense_error(struct ata_queued_cmd *qc, u8 drv_stat)
223 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered 473 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
224 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark 474 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
225 }; 475 };
226 int i = 0;
227
228 cmd->result = SAM_STAT_CHECK_CONDITION;
229 476
230 /* 477 /*
231 * Is this an error we can process/parse 478 * Is this an error we can process/parse
232 */ 479 */
480 if (drv_stat & ATA_BUSY) {
481 drv_err = 0; /* Ignore the err bits, they're invalid */
482 }
483
484 if (drv_err) {
485 /* Look for drv_err */
486 for (i = 0; sense_table[i][0] != 0xFF; i++) {
487 /* Look for best matches first */
488 if ((sense_table[i][0] & drv_err) ==
489 sense_table[i][0]) {
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);
499 }
500
501 /* Fall back to interpreting status bits */
502 for (i = 0; stat_table[i][0] != 0xFF; i++) {
503 if (stat_table[i][0] & drv_stat) {
504 *sk = stat_table[i][1];
505 *asc = stat_table[i][2];
506 *ascq = stat_table[i][3];
507 goto translate_done;
508 }
509 }
510 /* No error? Undecoded? */
511 printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n",
512 id, drv_stat);
513
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;
233 548
234 if(drv_stat & ATA_ERR) 549 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
235 /* Read the err bits */
236 err = ata_chk_err(qc->ap);
237 550
238 /* Display the ATA level error info */ 551 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
239 552
240 printk(KERN_WARNING "ata%u: status=0x%02x { ", qc->ap->id, drv_stat); 553 /*
241 if(drv_stat & 0x80) 554 * Read the controller registers.
242 { 555 */
243 printk("Busy "); 556 assert(NULL != qc->ap->ops->tf_read);
244 err = 0; /* Data is not valid in this case */ 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 (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;
245 } 567 }
246 else { 568
247 if(drv_stat & 0x40) printk("DriveReady "); 569 /*
248 if(drv_stat & 0x20) printk("DeviceFault "); 570 * Sense data is current and format is descriptor.
249 if(drv_stat & 0x10) printk("SeekComplete "); 571 */
250 if(drv_stat & 0x08) printk("DataRequest "); 572 sb[0] = 0x72;
251 if(drv_stat & 0x04) printk("CorrectedError "); 573
252 if(drv_stat & 0x02) printk("Index "); 574 desc[0] = 0x09;
253 if(drv_stat & 0x01) printk("Error "); 575
576 /*
577 * Set length of additional sense data.
578 * Since we only populate descriptor 0, the total
579 * length is the same (fixed) length as descriptor 0.
580 */
581 desc[1] = sb[7] = 14;
582
583 /*
584 * Copy registers into sense buffer.
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;
254 } 605 }
255 printk("}\n"); 606}
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 607
274 /* Should we dump sector info here too ?? */ 608/**
609 * ata_gen_fixed_sense - generate a SCSI fixed sense block
610 * @qc: Command that we are erroring out
611 *
612 * Leverage ata_to_sense_error() to give us the codes. Fit our
613 * LBA in here if there's room.
614 *
615 * LOCKING:
616 * inherited from caller
617 */
618void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
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 (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;
275 } 642 }
276 643
644 sb[0] = 0x70;
645 sb[7] = 0x0a;
277 646
278 /* Look for err */ 647 if (tf->flags & ATA_TFLAG_LBA48) {
279 while(sense_table[i][0] != 0xFF) 648 /* TODO: find solution for LBA48 descriptors */
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 } 649 }
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 650
297 i = 0; 651 else if (tf->flags & ATA_TFLAG_LBA) {
298 /* Fall back to interpreting status bits */ 652 /* A small (28b) LBA will fit in the 32b info field */
299 while(stat_table[i][0] != 0xFF) 653 sb[0] |= 0x80; /* set valid bit */
300 { 654 sb[3] = tf->device & 0x0f;
301 if(stat_table[i][0] & drv_stat) 655 sb[4] = tf->lbah;
302 { 656 sb[5] = tf->lbam;
303 sb[0] = 0x70; 657 sb[6] = tf->lbal;
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 } 658 }
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 659
316 sb[0] = 0x70; 660 else {
317 sb[2] = MEDIUM_ERROR; 661 /* TODO: C/H/S */
318 sb[7] = 0x0A;
319 if (cmd->sc_data_direction == DMA_FROM_DEVICE) {
320 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 } 662 }
326} 663}
327 664
@@ -420,7 +757,7 @@ int ata_scsi_error(struct Scsi_Host *host)
420 */ 757 */
421 758
422static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc, 759static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
423 u8 *scsicmd) 760 const u8 *scsicmd)
424{ 761{
425 struct ata_taskfile *tf = &qc->tf; 762 struct ata_taskfile *tf = &qc->tf;
426 763
@@ -430,15 +767,26 @@ static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
430 ; /* ignore IMMED bit, violates sat-r05 */ 767 ; /* ignore IMMED bit, violates sat-r05 */
431 } 768 }
432 if (scsicmd[4] & 0x2) 769 if (scsicmd[4] & 0x2)
433 return 1; /* LOEJ bit set not supported */ 770 goto invalid_fld; /* LOEJ bit set not supported */
434 if (((scsicmd[4] >> 4) & 0xf) != 0) 771 if (((scsicmd[4] >> 4) & 0xf) != 0)
435 return 1; /* power conditions not supported */ 772 goto invalid_fld; /* power conditions not supported */
436 if (scsicmd[4] & 0x1) { 773 if (scsicmd[4] & 0x1) {
437 tf->nsect = 1; /* 1 sector, lba=0 */ 774 tf->nsect = 1; /* 1 sector, lba=0 */
438 tf->lbah = 0x0; 775
439 tf->lbam = 0x0; 776 if (qc->dev->flags & ATA_DFLAG_LBA) {
440 tf->lbal = 0x0; 777 qc->tf.flags |= ATA_TFLAG_LBA;
441 tf->device |= ATA_LBA; 778
779 tf->lbah = 0x0;
780 tf->lbam = 0x0;
781 tf->lbal = 0x0;
782 tf->device |= ATA_LBA;
783 } else {
784 /* CHS */
785 tf->lbal = 0x1; /* sect */
786 tf->lbam = 0x0; /* cyl low */
787 tf->lbah = 0x0; /* cyl high */
788 }
789
442 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */ 790 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
443 } else { 791 } else {
444 tf->nsect = 0; /* time period value (0 implies now) */ 792 tf->nsect = 0; /* time period value (0 implies now) */
@@ -453,6 +801,11 @@ static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
453 */ 801 */
454 802
455 return 0; 803 return 0;
804
805invalid_fld:
806 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
807 /* "Invalid field in cbd" */
808 return 1;
456} 809}
457 810
458 811
@@ -471,14 +824,14 @@ static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
471 * Zero on success, non-zero on error. 824 * Zero on success, non-zero on error.
472 */ 825 */
473 826
474static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd) 827static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
475{ 828{
476 struct ata_taskfile *tf = &qc->tf; 829 struct ata_taskfile *tf = &qc->tf;
477 830
478 tf->flags |= ATA_TFLAG_DEVICE; 831 tf->flags |= ATA_TFLAG_DEVICE;
479 tf->protocol = ATA_PROT_NODATA; 832 tf->protocol = ATA_PROT_NODATA;
480 833
481 if ((tf->flags & ATA_TFLAG_LBA48) && 834 if ((qc->dev->flags & ATA_DFLAG_LBA48) &&
482 (ata_id_has_flush_ext(qc->dev->id))) 835 (ata_id_has_flush_ext(qc->dev->id)))
483 tf->command = ATA_CMD_FLUSH_EXT; 836 tf->command = ATA_CMD_FLUSH_EXT;
484 else 837 else
@@ -488,6 +841,99 @@ static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
488} 841}
489 842
490/** 843/**
844 * scsi_6_lba_len - Get LBA and transfer length
845 * @scsicmd: SCSI command to translate
846 *
847 * Calculate LBA and transfer length for 6-byte commands.
848 *
849 * RETURNS:
850 * @plba: the LBA
851 * @plen: the transfer length
852 */
853
854static void scsi_6_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
855{
856 u64 lba = 0;
857 u32 len = 0;
858
859 VPRINTK("six-byte command\n");
860
861 lba |= ((u64)scsicmd[2]) << 8;
862 lba |= ((u64)scsicmd[3]);
863
864 len |= ((u32)scsicmd[4]);
865
866 *plba = lba;
867 *plen = len;
868}
869
870/**
871 * scsi_10_lba_len - Get LBA and transfer length
872 * @scsicmd: SCSI command to translate
873 *
874 * Calculate LBA and transfer length for 10-byte commands.
875 *
876 * RETURNS:
877 * @plba: the LBA
878 * @plen: the transfer length
879 */
880
881static void scsi_10_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
882{
883 u64 lba = 0;
884 u32 len = 0;
885
886 VPRINTK("ten-byte command\n");
887
888 lba |= ((u64)scsicmd[2]) << 24;
889 lba |= ((u64)scsicmd[3]) << 16;
890 lba |= ((u64)scsicmd[4]) << 8;
891 lba |= ((u64)scsicmd[5]);
892
893 len |= ((u32)scsicmd[7]) << 8;
894 len |= ((u32)scsicmd[8]);
895
896 *plba = lba;
897 *plen = len;
898}
899
900/**
901 * scsi_16_lba_len - Get LBA and transfer length
902 * @scsicmd: SCSI command to translate
903 *
904 * Calculate LBA and transfer length for 16-byte commands.
905 *
906 * RETURNS:
907 * @plba: the LBA
908 * @plen: the transfer length
909 */
910
911static void scsi_16_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
912{
913 u64 lba = 0;
914 u32 len = 0;
915
916 VPRINTK("sixteen-byte command\n");
917
918 lba |= ((u64)scsicmd[2]) << 56;
919 lba |= ((u64)scsicmd[3]) << 48;
920 lba |= ((u64)scsicmd[4]) << 40;
921 lba |= ((u64)scsicmd[5]) << 32;
922 lba |= ((u64)scsicmd[6]) << 24;
923 lba |= ((u64)scsicmd[7]) << 16;
924 lba |= ((u64)scsicmd[8]) << 8;
925 lba |= ((u64)scsicmd[9]);
926
927 len |= ((u32)scsicmd[10]) << 24;
928 len |= ((u32)scsicmd[11]) << 16;
929 len |= ((u32)scsicmd[12]) << 8;
930 len |= ((u32)scsicmd[13]);
931
932 *plba = lba;
933 *plen = len;
934}
935
936/**
491 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one 937 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
492 * @qc: Storage for translated ATA taskfile 938 * @qc: Storage for translated ATA taskfile
493 * @scsicmd: SCSI command to translate 939 * @scsicmd: SCSI command to translate
@@ -501,82 +947,110 @@ static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
501 * Zero on success, non-zero on error. 947 * Zero on success, non-zero on error.
502 */ 948 */
503 949
504static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd) 950static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
505{ 951{
506 struct ata_taskfile *tf = &qc->tf; 952 struct ata_taskfile *tf = &qc->tf;
507 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48; 953 struct ata_device *dev = qc->dev;
508 u64 dev_sectors = qc->dev->n_sectors; 954 u64 dev_sectors = qc->dev->n_sectors;
509 u64 sect = 0; 955 u64 block;
510 u32 n_sect = 0; 956 u32 n_block;
511 957
512 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 958 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
513 tf->protocol = ATA_PROT_NODATA; 959 tf->protocol = ATA_PROT_NODATA;
514 tf->device |= ATA_LBA;
515 960
516 if (scsicmd[0] == VERIFY) { 961 if (scsicmd[0] == VERIFY)
517 sect |= ((u64)scsicmd[2]) << 24; 962 scsi_10_lba_len(scsicmd, &block, &n_block);
518 sect |= ((u64)scsicmd[3]) << 16; 963 else if (scsicmd[0] == VERIFY_16)
519 sect |= ((u64)scsicmd[4]) << 8; 964 scsi_16_lba_len(scsicmd, &block, &n_block);
520 sect |= ((u64)scsicmd[5]); 965 else
966 goto invalid_fld;
521 967
522 n_sect |= ((u32)scsicmd[7]) << 8; 968 if (!n_block)
523 n_sect |= ((u32)scsicmd[8]); 969 goto nothing_to_do;
524 } 970 if (block >= dev_sectors)
971 goto out_of_range;
972 if ((block + n_block) > dev_sectors)
973 goto out_of_range;
525 974
526 else if (scsicmd[0] == VERIFY_16) { 975 if (dev->flags & ATA_DFLAG_LBA) {
527 sect |= ((u64)scsicmd[2]) << 56; 976 tf->flags |= ATA_TFLAG_LBA;
528 sect |= ((u64)scsicmd[3]) << 48;
529 sect |= ((u64)scsicmd[4]) << 40;
530 sect |= ((u64)scsicmd[5]) << 32;
531 sect |= ((u64)scsicmd[6]) << 24;
532 sect |= ((u64)scsicmd[7]) << 16;
533 sect |= ((u64)scsicmd[8]) << 8;
534 sect |= ((u64)scsicmd[9]);
535
536 n_sect |= ((u32)scsicmd[10]) << 24;
537 n_sect |= ((u32)scsicmd[11]) << 16;
538 n_sect |= ((u32)scsicmd[12]) << 8;
539 n_sect |= ((u32)scsicmd[13]);
540 }
541 977
542 else 978 if (dev->flags & ATA_DFLAG_LBA48) {
543 return 1; 979 if (n_block > (64 * 1024))
980 goto invalid_fld;
544 981
545 if (!n_sect) 982 /* use LBA48 */
546 return 1; 983 tf->flags |= ATA_TFLAG_LBA48;
547 if (sect >= dev_sectors) 984 tf->command = ATA_CMD_VERIFY_EXT;
548 return 1;
549 if ((sect + n_sect) > dev_sectors)
550 return 1;
551 if (lba48) {
552 if (n_sect > (64 * 1024))
553 return 1;
554 } else {
555 if (n_sect > 256)
556 return 1;
557 }
558 985
559 if (lba48) { 986 tf->hob_nsect = (n_block >> 8) & 0xff;
560 tf->command = ATA_CMD_VERIFY_EXT; 987
988 tf->hob_lbah = (block >> 40) & 0xff;
989 tf->hob_lbam = (block >> 32) & 0xff;
990 tf->hob_lbal = (block >> 24) & 0xff;
991 } else {
992 if (n_block > 256)
993 goto invalid_fld;
994
995 /* use LBA28 */
996 tf->command = ATA_CMD_VERIFY;
997
998 tf->device |= (block >> 24) & 0xf;
999 }
561 1000
562 tf->hob_nsect = (n_sect >> 8) & 0xff; 1001 tf->nsect = n_block & 0xff;
563 1002
564 tf->hob_lbah = (sect >> 40) & 0xff; 1003 tf->lbah = (block >> 16) & 0xff;
565 tf->hob_lbam = (sect >> 32) & 0xff; 1004 tf->lbam = (block >> 8) & 0xff;
566 tf->hob_lbal = (sect >> 24) & 0xff; 1005 tf->lbal = block & 0xff;
1006
1007 tf->device |= ATA_LBA;
567 } else { 1008 } else {
1009 /* CHS */
1010 u32 sect, head, cyl, track;
1011
1012 if (n_block > 256)
1013 goto invalid_fld;
1014
1015 /* Convert LBA to CHS */
1016 track = (u32)block / dev->sectors;
1017 cyl = track / dev->heads;
1018 head = track % dev->heads;
1019 sect = (u32)block % dev->sectors + 1;
1020
1021 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1022 (u32)block, track, cyl, head, sect);
1023
1024 /* Check whether the converted CHS can fit.
1025 Cylinder: 0-65535
1026 Head: 0-15
1027 Sector: 1-255*/
1028 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
1029 goto out_of_range;
1030
568 tf->command = ATA_CMD_VERIFY; 1031 tf->command = ATA_CMD_VERIFY;
569 1032 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
570 tf->device |= (sect >> 24) & 0xf; 1033 tf->lbal = sect;
1034 tf->lbam = cyl;
1035 tf->lbah = cyl >> 8;
1036 tf->device |= head;
571 } 1037 }
572 1038
573 tf->nsect = n_sect & 0xff; 1039 return 0;
574 1040
575 tf->lbah = (sect >> 16) & 0xff; 1041invalid_fld:
576 tf->lbam = (sect >> 8) & 0xff; 1042 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
577 tf->lbal = sect & 0xff; 1043 /* "Invalid field in cbd" */
1044 return 1;
578 1045
579 return 0; 1046out_of_range:
1047 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
1048 /* "Logical Block Address out of range" */
1049 return 1;
1050
1051nothing_to_do:
1052 qc->scsicmd->result = SAM_STAT_GOOD;
1053 return 1;
580} 1054}
581 1055
582/** 1056/**
@@ -599,117 +1073,175 @@ static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
599 * Zero on success, non-zero on error. 1073 * Zero on success, non-zero on error.
600 */ 1074 */
601 1075
602static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd) 1076static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
603{ 1077{
604 struct ata_taskfile *tf = &qc->tf; 1078 struct ata_taskfile *tf = &qc->tf;
605 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48; 1079 struct ata_device *dev = qc->dev;
1080 u64 block;
1081 u32 n_block;
606 1082
607 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 1083 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
608 tf->protocol = qc->dev->xfer_protocol;
609 tf->device |= ATA_LBA;
610 1084
611 if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 || 1085 if (scsicmd[0] == WRITE_10 || scsicmd[0] == WRITE_6 ||
612 scsicmd[0] == READ_16) { 1086 scsicmd[0] == WRITE_16)
613 tf->command = qc->dev->read_cmd;
614 } else {
615 tf->command = qc->dev->write_cmd;
616 tf->flags |= ATA_TFLAG_WRITE; 1087 tf->flags |= ATA_TFLAG_WRITE;
617 }
618 1088
619 if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) { 1089 /* Calculate the SCSI LBA and transfer length. */
620 if (lba48) { 1090 switch (scsicmd[0]) {
621 tf->hob_nsect = scsicmd[7]; 1091 case READ_10:
622 tf->hob_lbal = scsicmd[2]; 1092 case WRITE_10:
1093 scsi_10_lba_len(scsicmd, &block, &n_block);
1094 break;
1095 case READ_6:
1096 case WRITE_6:
1097 scsi_6_lba_len(scsicmd, &block, &n_block);
623 1098
624 qc->nsect = ((unsigned int)scsicmd[7] << 8) | 1099 /* for 6-byte r/w commands, transfer length 0
625 scsicmd[8]; 1100 * means 256 blocks of data, not 0 block.
626 } else { 1101 */
627 /* if we don't support LBA48 addressing, the request 1102 if (!n_block)
628 * -may- be too large. */ 1103 n_block = 256;
629 if ((scsicmd[2] & 0xf0) || scsicmd[7]) 1104 break;
630 return 1; 1105 case READ_16:
1106 case WRITE_16:
1107 scsi_16_lba_len(scsicmd, &block, &n_block);
1108 break;
1109 default:
1110 DPRINTK("no-byte command\n");
1111 goto invalid_fld;
1112 }
631 1113
632 /* stores LBA27:24 in lower 4 bits of device reg */ 1114 /* Check and compose ATA command */
633 tf->device |= scsicmd[2]; 1115 if (!n_block)
1116 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1117 * length 0 means transfer 0 block of data.
1118 * However, for ATA R/W commands, sector count 0 means
1119 * 256 or 65536 sectors, not 0 sectors as in SCSI.
1120 */
1121 goto nothing_to_do;
634 1122
635 qc->nsect = scsicmd[8]; 1123 if (dev->flags & ATA_DFLAG_LBA) {
636 } 1124 tf->flags |= ATA_TFLAG_LBA;
637 1125
638 tf->nsect = scsicmd[8]; 1126 if (dev->flags & ATA_DFLAG_LBA48) {
639 tf->lbal = scsicmd[5]; 1127 /* The request -may- be too large for LBA48. */
640 tf->lbam = scsicmd[4]; 1128 if ((block >> 48) || (n_block > 65536))
641 tf->lbah = scsicmd[3]; 1129 goto out_of_range;
642 1130
643 VPRINTK("ten-byte command\n"); 1131 /* use LBA48 */
644 if (qc->nsect == 0) /* we don't support length==0 cmds */ 1132 tf->flags |= ATA_TFLAG_LBA48;
645 return 1;
646 return 0;
647 }
648 1133
649 if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) { 1134 tf->hob_nsect = (n_block >> 8) & 0xff;
650 qc->nsect = tf->nsect = scsicmd[4];
651 if (!qc->nsect) {
652 qc->nsect = 256;
653 if (lba48)
654 tf->hob_nsect = 1;
655 }
656 1135
657 tf->lbal = scsicmd[3]; 1136 tf->hob_lbah = (block >> 40) & 0xff;
658 tf->lbam = scsicmd[2]; 1137 tf->hob_lbam = (block >> 32) & 0xff;
659 tf->lbah = scsicmd[1] & 0x1f; /* mask out reserved bits */ 1138 tf->hob_lbal = (block >> 24) & 0xff;
1139 } else {
1140 /* use LBA28 */
660 1141
661 VPRINTK("six-byte command\n"); 1142 /* The request -may- be too large for LBA28. */
662 return 0; 1143 if ((block >> 28) || (n_block > 256))
663 } 1144 goto out_of_range;
664 1145
665 if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) { 1146 tf->device |= (block >> 24) & 0xf;
666 /* rule out impossible LBAs and sector counts */ 1147 }
667 if (scsicmd[2] || scsicmd[3] || scsicmd[10] || scsicmd[11])
668 return 1;
669 1148
670 if (lba48) { 1149 ata_rwcmd_protocol(qc);
671 tf->hob_nsect = scsicmd[12];
672 tf->hob_lbal = scsicmd[6];
673 tf->hob_lbam = scsicmd[5];
674 tf->hob_lbah = scsicmd[4];
675 1150
676 qc->nsect = ((unsigned int)scsicmd[12] << 8) | 1151 qc->nsect = n_block;
677 scsicmd[13]; 1152 tf->nsect = n_block & 0xff;
678 } else {
679 /* once again, filter out impossible non-zero values */
680 if (scsicmd[4] || scsicmd[5] || scsicmd[12] ||
681 (scsicmd[6] & 0xf0))
682 return 1;
683 1153
684 /* stores LBA27:24 in lower 4 bits of device reg */ 1154 tf->lbah = (block >> 16) & 0xff;
685 tf->device |= scsicmd[6]; 1155 tf->lbam = (block >> 8) & 0xff;
1156 tf->lbal = block & 0xff;
686 1157
687 qc->nsect = scsicmd[13]; 1158 tf->device |= ATA_LBA;
688 } 1159 } else {
1160 /* CHS */
1161 u32 sect, head, cyl, track;
1162
1163 /* The request -may- be too large for CHS addressing. */
1164 if ((block >> 28) || (n_block > 256))
1165 goto out_of_range;
1166
1167 ata_rwcmd_protocol(qc);
1168
1169 /* Convert LBA to CHS */
1170 track = (u32)block / dev->sectors;
1171 cyl = track / dev->heads;
1172 head = track % dev->heads;
1173 sect = (u32)block % dev->sectors + 1;
1174
1175 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1176 (u32)block, track, cyl, head, sect);
1177
1178 /* Check whether the converted CHS can fit.
1179 Cylinder: 0-65535
1180 Head: 0-15
1181 Sector: 1-255*/
1182 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
1183 goto out_of_range;
1184
1185 qc->nsect = n_block;
1186 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1187 tf->lbal = sect;
1188 tf->lbam = cyl;
1189 tf->lbah = cyl >> 8;
1190 tf->device |= head;
1191 }
689 1192
690 tf->nsect = scsicmd[13]; 1193 return 0;
691 tf->lbal = scsicmd[9];
692 tf->lbam = scsicmd[8];
693 tf->lbah = scsicmd[7];
694 1194
695 VPRINTK("sixteen-byte command\n"); 1195invalid_fld:
696 if (qc->nsect == 0) /* we don't support length==0 cmds */ 1196 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
697 return 1; 1197 /* "Invalid field in cbd" */
698 return 0; 1198 return 1;
699 }
700 1199
701 DPRINTK("no-byte command\n"); 1200out_of_range:
1201 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
1202 /* "Logical Block Address out of range" */
1203 return 1;
1204
1205nothing_to_do:
1206 qc->scsicmd->result = SAM_STAT_GOOD;
702 return 1; 1207 return 1;
703} 1208}
704 1209
705static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat) 1210static int ata_scsi_qc_complete(struct ata_queued_cmd *qc,
1211 unsigned int err_mask)
706{ 1212{
707 struct scsi_cmnd *cmd = qc->scsicmd; 1213 struct scsi_cmnd *cmd = qc->scsicmd;
1214 u8 *cdb = cmd->cmnd;
1215 int need_sense = (err_mask != 0);
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 (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
1225 ((cdb[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 }
708 1240
709 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) 1241 if (need_sense) {
710 ata_to_sense_error(qc, drv_stat); 1242 /* The ata_gen_..._sense routines fill in tf */
711 else 1243 ata_dump_status(qc->ap->id, &qc->tf);
712 cmd->result = SAM_STAT_GOOD; 1244 }
713 1245
714 qc->scsidone(cmd); 1246 qc->scsidone(cmd);
715 1247
@@ -731,6 +1263,12 @@ static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
731 * This function sets up an ata_queued_cmd structure for the 1263 * This function sets up an ata_queued_cmd structure for the
732 * SCSI command, and sends that ata_queued_cmd to the hardware. 1264 * SCSI command, and sends that ata_queued_cmd to the hardware.
733 * 1265 *
1266 * The xlat_func argument (actor) returns 0 if ready to execute
1267 * ATA command, else 1 to finish translation. If 1 is returned
1268 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1269 * to be set reflecting an error condition or clean (early)
1270 * termination.
1271 *
734 * LOCKING: 1272 * LOCKING:
735 * spin_lock_irqsave(host_set lock) 1273 * spin_lock_irqsave(host_set lock)
736 */ 1274 */
@@ -747,7 +1285,7 @@ static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
747 1285
748 qc = ata_scsi_qc_new(ap, dev, cmd, done); 1286 qc = ata_scsi_qc_new(ap, dev, cmd, done);
749 if (!qc) 1287 if (!qc)
750 return; 1288 goto err_mem;
751 1289
752 /* data is present; dma-map it */ 1290 /* data is present; dma-map it */
753 if (cmd->sc_data_direction == DMA_FROM_DEVICE || 1291 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
@@ -755,7 +1293,7 @@ static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
755 if (unlikely(cmd->request_bufflen < 1)) { 1293 if (unlikely(cmd->request_bufflen < 1)) {
756 printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n", 1294 printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
757 ap->id, dev->devno); 1295 ap->id, dev->devno);
758 goto err_out; 1296 goto err_did;
759 } 1297 }
760 1298
761 if (cmd->use_sg) 1299 if (cmd->use_sg)
@@ -770,19 +1308,28 @@ static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
770 qc->complete_fn = ata_scsi_qc_complete; 1308 qc->complete_fn = ata_scsi_qc_complete;
771 1309
772 if (xlat_func(qc, scsicmd)) 1310 if (xlat_func(qc, scsicmd))
773 goto err_out; 1311 goto early_finish;
774 1312
775 /* select device, send command to hardware */ 1313 /* select device, send command to hardware */
776 if (ata_qc_issue(qc)) 1314 if (ata_qc_issue(qc))
777 goto err_out; 1315 goto err_did;
778 1316
779 VPRINTK("EXIT\n"); 1317 VPRINTK("EXIT\n");
780 return; 1318 return;
781 1319
782err_out: 1320early_finish:
1321 ata_qc_free(qc);
1322 done(cmd);
1323 DPRINTK("EXIT - early finish (good or error)\n");
1324 return;
1325
1326err_did:
783 ata_qc_free(qc); 1327 ata_qc_free(qc);
784 ata_bad_cdb(cmd, done); 1328err_mem:
785 DPRINTK("EXIT - badcmd\n"); 1329 cmd->result = (DID_ERROR << 16);
1330 done(cmd);
1331 DPRINTK("EXIT - internal\n");
1332 return;
786} 1333}
787 1334
788/** 1335/**
@@ -849,7 +1396,8 @@ static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
849 * Mapping the response buffer, calling the command's handler, 1396 * Mapping the response buffer, calling the command's handler,
850 * and handling the handler's return value. This return value 1397 * and handling the handler's return value. This return value
851 * indicates whether the handler wishes the SCSI command to be 1398 * indicates whether the handler wishes the SCSI command to be
852 * completed successfully, or not. 1399 * completed successfully (0), or not (in which case cmd->result
1400 * and sense buffer are assumed to be set).
853 * 1401 *
854 * LOCKING: 1402 * LOCKING:
855 * spin_lock_irqsave(host_set lock) 1403 * spin_lock_irqsave(host_set lock)
@@ -868,12 +1416,9 @@ void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
868 rc = actor(args, rbuf, buflen); 1416 rc = actor(args, rbuf, buflen);
869 ata_scsi_rbuf_put(cmd, rbuf); 1417 ata_scsi_rbuf_put(cmd, rbuf);
870 1418
871 if (rc) 1419 if (rc == 0)
872 ata_bad_cdb(cmd, args->done);
873 else {
874 cmd->result = SAM_STAT_GOOD; 1420 cmd->result = SAM_STAT_GOOD;
875 args->done(cmd); 1421 args->done(cmd);
876 }
877} 1422}
878 1423
879/** 1424/**
@@ -1087,13 +1632,9 @@ static void ata_msense_push(u8 **ptr_io, const u8 *last,
1087static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io, 1632static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1088 const u8 *last) 1633 const u8 *last)
1089{ 1634{
1090 u8 page[] = { 1635 u8 page[CACHE_MPAGE_LEN];
1091 0x8, /* page code */
1092 0x12, /* page length */
1093 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 10 zeroes */
1094 0, 0, 0, 0, 0, 0, 0, 0 /* 8 zeroes */
1095 };
1096 1636
1637 memcpy(page, def_cache_mpage, sizeof(page));
1097 if (ata_id_wcache_enabled(id)) 1638 if (ata_id_wcache_enabled(id))
1098 page[2] |= (1 << 2); /* write cache enable */ 1639 page[2] |= (1 << 2); /* write cache enable */
1099 if (!ata_id_rahead_enabled(id)) 1640 if (!ata_id_rahead_enabled(id))
@@ -1117,15 +1658,9 @@ static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1117 1658
1118static 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)
1119{ 1660{
1120 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,
1121 1662 sizeof(def_control_mpage));
1122 /* byte 2: set the descriptor format sense data bit (bit 2) 1663 return sizeof(def_control_mpage);
1123 * since we need to support returning this format for SAT
1124 * commands and any SCSI commands against a 48b LBA device.
1125 */
1126
1127 ata_msense_push(ptr_io, last, page, sizeof(page));
1128 return sizeof(page);
1129} 1664}
1130 1665
1131/** 1666/**
@@ -1142,15 +1677,10 @@ static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1142 1677
1143static 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)
1144{ 1679{
1145 const u8 page[] = {
1146 0x1, /* page code */
1147 0xa, /* page length */
1148 (1 << 7) | (1 << 6), /* note auto r/w reallocation */
1149 0, 0, 0, 0, 0, 0, 0, 0, 0 /* 9 zeroes */
1150 };
1151 1680
1152 ata_msense_push(ptr_io, last, page, sizeof(page)); 1681 ata_msense_push(ptr_io, last, def_rw_recovery_mpage,
1153 return sizeof(page); 1682 sizeof(def_rw_recovery_mpage));
1683 return sizeof(def_rw_recovery_mpage);
1154} 1684}
1155 1685
1156/** 1686/**
@@ -1159,7 +1689,9 @@ static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1159 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. 1689 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1160 * @buflen: Response buffer length. 1690 * @buflen: Response buffer length.
1161 * 1691 *
1162 * 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.
1163 * 1695 *
1164 * LOCKING: 1696 * LOCKING:
1165 * spin_lock_irqsave(host_set lock) 1697 * spin_lock_irqsave(host_set lock)
@@ -1169,61 +1701,115 @@ unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1169 unsigned int buflen) 1701 unsigned int buflen)
1170{ 1702{
1171 u8 *scsicmd = args->cmd->cmnd, *p, *last; 1703 u8 *scsicmd = args->cmd->cmnd, *p, *last;
1172 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;
1173 1711
1174 VPRINTK("ENTER\n"); 1712 VPRINTK("ENTER\n");
1175 1713
1176 six_byte = (scsicmd[0] == MODE_SENSE); 1714 six_byte = (scsicmd[0] == MODE_SENSE);
1177 1715 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
1178 /* we only support saved and current values (which we treat 1716 /*
1179 * in the same manner) 1717 * LLBA bit in msense(10) ignored (compliant)
1180 */ 1718 */
1719
1181 page_control = scsicmd[2] >> 6; 1720 page_control = scsicmd[2] >> 6;
1182 if ((page_control != 0) && (page_control != 3)) 1721 switch (page_control) {
1183 return 1; 1722 case 0: /* current */
1723 break; /* supported */
1724 case 3: /* saved */
1725 goto saving_not_supp;
1726 case 1: /* changeable */
1727 case 2: /* defaults */
1728 default:
1729 goto invalid_fld;
1730 }
1184 1731
1185 if (six_byte) 1732 if (six_byte) {
1186 output_len = 4; 1733 output_len = 4 + (ebd ? 8 : 0);
1187 else 1734 alloc_len = scsicmd[4];
1188 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;
1189 1740
1190 p = rbuf + output_len; 1741 p = rbuf + output_len;
1191 last = rbuf + buflen - 1; 1742 last = rbuf + minlen - 1;
1192 1743
1193 switch(scsicmd[2] & 0x3f) { 1744 pg = scsicmd[2] & 0x3f;
1194 case 0x01: /* r/w error recovery */ 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;
1752
1753 switch(pg) {
1754 case RW_RECOVERY_MPAGE:
1195 output_len += ata_msense_rw_recovery(&p, last); 1755 output_len += ata_msense_rw_recovery(&p, last);
1196 break; 1756 break;
1197 1757
1198 case 0x08: /* caching */ 1758 case CACHE_MPAGE:
1199 output_len += ata_msense_caching(args->id, &p, last); 1759 output_len += ata_msense_caching(args->id, &p, last);
1200 break; 1760 break;
1201 1761
1202 case 0x0a: { /* control mode */ 1762 case CONTROL_MPAGE: {
1203 output_len += ata_msense_ctl_mode(&p, last); 1763 output_len += ata_msense_ctl_mode(&p, last);
1204 break; 1764 break;
1205 } 1765 }
1206 1766
1207 case 0x3f: /* all pages */ 1767 case ALL_MPAGES:
1208 output_len += ata_msense_rw_recovery(&p, last); 1768 output_len += ata_msense_rw_recovery(&p, last);
1209 output_len += ata_msense_caching(args->id, &p, last); 1769 output_len += ata_msense_caching(args->id, &p, last);
1210 output_len += ata_msense_ctl_mode(&p, last); 1770 output_len += ata_msense_ctl_mode(&p, last);
1211 break; 1771 break;
1212 1772
1213 default: /* invalid page code */ 1773 default: /* invalid page code */
1214 return 1; 1774 goto invalid_fld;
1215 } 1775 }
1216 1776
1777 if (minlen < 1)
1778 return 0;
1217 if (six_byte) { 1779 if (six_byte) {
1218 output_len--; 1780 output_len--;
1219 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 }
1220 } else { 1789 } else {
1221 output_len -= 2; 1790 output_len -= 2;
1222 rbuf[0] = output_len >> 8; 1791 rbuf[0] = output_len >> 8;
1223 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 }
1224 } 1801 }
1225
1226 return 0; 1802 return 0;
1803
1804invalid_fld:
1805 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
1806 /* "Invalid field in cbd" */
1807 return 1;
1808
1809saving_not_supp:
1810 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
1811 /* "Saving parameters not supported" */
1812 return 1;
1227} 1813}
1228 1814
1229/** 1815/**
@@ -1246,10 +1832,20 @@ unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
1246 1832
1247 VPRINTK("ENTER\n"); 1833 VPRINTK("ENTER\n");
1248 1834
1249 if (ata_id_has_lba48(args->id)) 1835 if (ata_id_has_lba(args->id)) {
1250 n_sectors = ata_id_u64(args->id, 100); 1836 if (ata_id_has_lba48(args->id))
1251 else 1837 n_sectors = ata_id_u64(args->id, 100);
1252 n_sectors = ata_id_u32(args->id, 60); 1838 else
1839 n_sectors = ata_id_u32(args->id, 60);
1840 } else {
1841 /* CHS default translation */
1842 n_sectors = args->id[1] * args->id[3] * args->id[6];
1843
1844 if (ata_id_current_chs_valid(args->id))
1845 /* CHS current translation */
1846 n_sectors = ata_id_u32(args->id, 57);
1847 }
1848
1253 n_sectors--; /* ATA TotalUserSectors - 1 */ 1849 n_sectors--; /* ATA TotalUserSectors - 1 */
1254 1850
1255 if (args->cmd->cmnd[0] == READ_CAPACITY) { 1851 if (args->cmd->cmnd[0] == READ_CAPACITY) {
@@ -1313,6 +1909,34 @@ unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1313} 1909}
1314 1910
1315/** 1911/**
1912 * ata_scsi_set_sense - Set SCSI sense data and status
1913 * @cmd: SCSI request to be handled
1914 * @sk: SCSI-defined sense key
1915 * @asc: SCSI-defined additional sense code
1916 * @ascq: SCSI-defined additional sense code qualifier
1917 *
1918 * Helper function that builds a valid fixed format, current
1919 * response code and the given sense key (sk), additional sense
1920 * code (asc) and additional sense code qualifier (ascq) with
1921 * a SCSI command status of %SAM_STAT_CHECK_CONDITION and
1922 * DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
1923 *
1924 * LOCKING:
1925 * Not required
1926 */
1927
1928void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
1929{
1930 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
1931
1932 cmd->sense_buffer[0] = 0x70; /* fixed format, current */
1933 cmd->sense_buffer[2] = sk;
1934 cmd->sense_buffer[7] = 18 - 8; /* additional sense length */
1935 cmd->sense_buffer[12] = asc;
1936 cmd->sense_buffer[13] = ascq;
1937}
1938
1939/**
1316 * ata_scsi_badcmd - End a SCSI request with an error 1940 * ata_scsi_badcmd - End a SCSI request with an error
1317 * @cmd: SCSI request to be handled 1941 * @cmd: SCSI request to be handled
1318 * @done: SCSI command completion function 1942 * @done: SCSI command completion function
@@ -1330,30 +1954,89 @@ unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1330void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq) 1954void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
1331{ 1955{
1332 DPRINTK("ENTER\n"); 1956 DPRINTK("ENTER\n");
1333 cmd->result = SAM_STAT_CHECK_CONDITION; 1957 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
1334
1335 cmd->sense_buffer[0] = 0x70;
1336 cmd->sense_buffer[2] = ILLEGAL_REQUEST;
1337 cmd->sense_buffer[7] = 14 - 8; /* addnl. sense len. FIXME: correct? */
1338 cmd->sense_buffer[12] = asc;
1339 cmd->sense_buffer[13] = ascq;
1340 1958
1341 done(cmd); 1959 done(cmd);
1342} 1960}
1343 1961
1344static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat) 1962void atapi_request_sense(struct ata_port *ap, struct ata_device *dev,
1963 struct scsi_cmnd *cmd)
1964{
1965 DECLARE_COMPLETION(wait);
1966 struct ata_queued_cmd *qc;
1967 unsigned long flags;
1968 int rc;
1969
1970 DPRINTK("ATAPI request sense\n");
1971
1972 qc = ata_qc_new_init(ap, dev);
1973 BUG_ON(qc == NULL);
1974
1975 /* FIXME: is this needed? */
1976 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
1977
1978 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
1979 qc->dma_dir = DMA_FROM_DEVICE;
1980
1981 memset(&qc->cdb, 0, ap->cdb_len);
1982 qc->cdb[0] = REQUEST_SENSE;
1983 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
1984
1985 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1986 qc->tf.command = ATA_CMD_PACKET;
1987
1988 qc->tf.protocol = ATA_PROT_ATAPI;
1989 qc->tf.lbam = (8 * 1024) & 0xff;
1990 qc->tf.lbah = (8 * 1024) >> 8;
1991 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
1992
1993 qc->waiting = &wait;
1994 qc->complete_fn = ata_qc_complete_noop;
1995
1996 spin_lock_irqsave(&ap->host_set->lock, flags);
1997 rc = ata_qc_issue(qc);
1998 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1999
2000 if (rc)
2001 ata_port_disable(ap);
2002 else
2003 wait_for_completion(&wait);
2004
2005 DPRINTK("EXIT\n");
2006}
2007
2008static int atapi_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
1345{ 2009{
1346 struct scsi_cmnd *cmd = qc->scsicmd; 2010 struct scsi_cmnd *cmd = qc->scsicmd;
1347 2011
1348 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) { 2012 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
2013
2014 if (unlikely(err_mask & AC_ERR_DEV)) {
1349 DPRINTK("request check condition\n"); 2015 DPRINTK("request check condition\n");
1350 2016
2017 /* FIXME: command completion with check condition
2018 * but no sense causes the error handler to run,
2019 * which then issues REQUEST SENSE, fills in the sense
2020 * buffer, and completes the command (for the second
2021 * time). We need to issue REQUEST SENSE some other
2022 * way, to avoid completing the command twice.
2023 */
1351 cmd->result = SAM_STAT_CHECK_CONDITION; 2024 cmd->result = SAM_STAT_CHECK_CONDITION;
1352 2025
1353 qc->scsidone(cmd); 2026 qc->scsidone(cmd);
1354 2027
1355 return 1; 2028 return 1;
1356 } else { 2029 }
2030
2031 else if (unlikely(err_mask))
2032 /* FIXME: not quite right; we don't want the
2033 * translation of taskfile registers into
2034 * a sense descriptors, since that's only
2035 * correct for ATA, not ATAPI
2036 */
2037 ata_gen_ata_desc_sense(qc);
2038
2039 else {
1357 u8 *scsicmd = cmd->cmnd; 2040 u8 *scsicmd = cmd->cmnd;
1358 2041
1359 if (scsicmd[0] == INQUIRY) { 2042 if (scsicmd[0] == INQUIRY) {
@@ -1361,15 +2044,30 @@ static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
1361 unsigned int buflen; 2044 unsigned int buflen;
1362 2045
1363 buflen = ata_scsi_rbuf_get(cmd, &buf); 2046 buflen = ata_scsi_rbuf_get(cmd, &buf);
1364 buf[2] = 0x5; 2047
1365 buf[3] = (buf[3] & 0xf0) | 2; 2048 /* ATAPI devices typically report zero for their SCSI version,
2049 * and sometimes deviate from the spec WRT response data
2050 * format. If SCSI version is reported as zero like normal,
2051 * then we make the following fixups: 1) Fake MMC-5 version,
2052 * to indicate to the Linux scsi midlayer this is a modern
2053 * device. 2) Ensure response data format / ATAPI information
2054 * are always correct.
2055 */
2056 /* FIXME: do we ever override EVPD pages and the like, with
2057 * this code?
2058 */
2059 if (buf[2] == 0) {
2060 buf[2] = 0x5;
2061 buf[3] = 0x32;
2062 }
2063
1366 ata_scsi_rbuf_put(cmd, buf); 2064 ata_scsi_rbuf_put(cmd, buf);
1367 } 2065 }
2066
1368 cmd->result = SAM_STAT_GOOD; 2067 cmd->result = SAM_STAT_GOOD;
1369 } 2068 }
1370 2069
1371 qc->scsidone(cmd); 2070 qc->scsidone(cmd);
1372
1373 return 0; 2071 return 0;
1374} 2072}
1375/** 2073/**
@@ -1384,7 +2082,7 @@ static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
1384 * Zero on success, non-zero on failure. 2082 * Zero on success, non-zero on failure.
1385 */ 2083 */
1386 2084
1387static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd) 2085static unsigned int atapi_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
1388{ 2086{
1389 struct scsi_cmnd *cmd = qc->scsicmd; 2087 struct scsi_cmnd *cmd = qc->scsicmd;
1390 struct ata_device *dev = qc->dev; 2088 struct ata_device *dev = qc->dev;
@@ -1453,7 +2151,7 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
1453 */ 2151 */
1454 2152
1455static struct ata_device * 2153static struct ata_device *
1456ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev) 2154ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
1457{ 2155{
1458 struct ata_device *dev; 2156 struct ata_device *dev;
1459 2157
@@ -1478,6 +2176,143 @@ ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev)
1478 return dev; 2176 return dev;
1479} 2177}
1480 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
1481/** 2316/**
1482 * 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
1483 * @dev: ATA device 2318 * @dev: ATA device
@@ -1510,6 +2345,11 @@ static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
1510 case VERIFY: 2345 case VERIFY:
1511 case VERIFY_16: 2346 case VERIFY_16:
1512 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
1513 case START_STOP: 2353 case START_STOP:
1514 return ata_scsi_start_stop_xlat; 2354 return ata_scsi_start_stop_xlat;
1515 } 2355 }
@@ -1610,7 +2450,7 @@ void ata_scsi_simulate(u16 *id,
1610 void (*done)(struct scsi_cmnd *)) 2450 void (*done)(struct scsi_cmnd *))
1611{ 2451{
1612 struct ata_scsi_args args; 2452 struct ata_scsi_args args;
1613 u8 *scsicmd = cmd->cmnd; 2453 const u8 *scsicmd = cmd->cmnd;
1614 2454
1615 args.id = id; 2455 args.id = id;
1616 args.cmd = cmd; 2456 args.cmd = cmd;
@@ -1630,7 +2470,7 @@ void ata_scsi_simulate(u16 *id,
1630 2470
1631 case INQUIRY: 2471 case INQUIRY:
1632 if (scsicmd[1] & 2) /* is CmdDt set? */ 2472 if (scsicmd[1] & 2) /* is CmdDt set? */
1633 ata_bad_cdb(cmd, done); 2473 ata_scsi_invalid_field(cmd, done);
1634 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */ 2474 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
1635 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std); 2475 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
1636 else if (scsicmd[2] == 0x00) 2476 else if (scsicmd[2] == 0x00)
@@ -1640,7 +2480,7 @@ void ata_scsi_simulate(u16 *id,
1640 else if (scsicmd[2] == 0x83) 2480 else if (scsicmd[2] == 0x83)
1641 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83); 2481 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
1642 else 2482 else
1643 ata_bad_cdb(cmd, done); 2483 ata_scsi_invalid_field(cmd, done);
1644 break; 2484 break;
1645 2485
1646 case MODE_SENSE: 2486 case MODE_SENSE:
@@ -1650,7 +2490,7 @@ void ata_scsi_simulate(u16 *id,
1650 2490
1651 case MODE_SELECT: /* unconditionally return */ 2491 case MODE_SELECT: /* unconditionally return */
1652 case MODE_SELECT_10: /* bad-field-in-cdb */ 2492 case MODE_SELECT_10: /* bad-field-in-cdb */
1653 ata_bad_cdb(cmd, done); 2493 ata_scsi_invalid_field(cmd, done);
1654 break; 2494 break;
1655 2495
1656 case READ_CAPACITY: 2496 case READ_CAPACITY:
@@ -1661,20 +2501,38 @@ void ata_scsi_simulate(u16 *id,
1661 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16) 2501 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
1662 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap); 2502 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
1663 else 2503 else
1664 ata_bad_cdb(cmd, done); 2504 ata_scsi_invalid_field(cmd, done);
1665 break; 2505 break;
1666 2506
1667 case REPORT_LUNS: 2507 case REPORT_LUNS:
1668 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns); 2508 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
1669 break; 2509 break;
1670 2510
1671 /* mandantory commands we haven't implemented yet */ 2511 /* mandatory commands we haven't implemented yet */
1672 case REQUEST_SENSE: 2512 case REQUEST_SENSE:
1673 2513
1674 /* all other commands */ 2514 /* all other commands */
1675 default: 2515 default:
1676 ata_bad_scsiop(cmd, done); 2516 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
2517 /* "Invalid command operation code" */
2518 done(cmd);
1677 break; 2519 break;
1678 } 2520 }
1679} 2521}
1680 2522
2523void ata_scsi_scan_host(struct ata_port *ap)
2524{
2525 struct ata_device *dev;
2526 unsigned int i;
2527
2528 if (ap->flags & ATA_FLAG_PORT_DISABLED)
2529 return;
2530
2531 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2532 dev = &ap->device[i];
2533
2534 if (ata_dev_present(dev))
2535 scsi_scan_target(&ap->host->shost_gendev, 0, i, 0, 0);
2536 }
2537}
2538