1 /*
2 * libata-scsi.c - helper library for ATA
3 *
4 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
7 *
8 * Copyright 2003-2004 Red Hat, Inc. All rights reserved.
9 * Copyright 2003-2004 Jeff Garzik
10 *
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
15 * any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; see the file COPYING. If not, write to
24 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 *
26 *
27 * libata documentation is available via 'make {ps|pdf}docs',
28 * as Documentation/DocBook/libata.*
29 *
30 * Hardware documentation available from
31 * - http://www.t10.org/
32 * - http://www.t13.org/
33 *
34 */
35
36 #include <linux/kernel.h>
37 #include <linux/blk.h>
38 #include <linux/blkdev.h>
39 #include <linux/spinlock.h>
40 #include <scsi/scsi.h>
41 #include "scsi.h"
42 #include <scsi/scsi_host.h>
43 #include "sd.h"
44 #include <linux/libata.h>
45 #include <linux/hdreg.h>
46 #include <asm/uaccess.h>
47
48 #include "libata.h"
49
50 #define SECTOR_SIZE 512
51
52 typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, const u8 *scsicmd);
53 static struct ata_device *
54 ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev);
55
56 #define RW_RECOVERY_MPAGE 0x1
57 #define RW_RECOVERY_MPAGE_LEN 12
58 #define CACHE_MPAGE 0x8
59 #define CACHE_MPAGE_LEN 20
60 #define CONTROL_MPAGE 0xa
61 #define CONTROL_MPAGE_LEN 12
62 #define ALL_MPAGES 0x3f
63 #define ALL_SUB_MPAGES 0xff
64
65
66 static const u8 def_rw_recovery_mpage[] = {
67 RW_RECOVERY_MPAGE,
68 RW_RECOVERY_MPAGE_LEN - 2,
69 (1 << 7) | /* AWRE, sat-r06 say it shall be 0 */
70 (1 << 6), /* ARRE (auto read reallocation) */
71 0, /* read retry count */
72 0, 0, 0, 0,
73 0, /* write retry count */
74 0, 0, 0
75 };
76
77 static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
78 CACHE_MPAGE,
79 CACHE_MPAGE_LEN - 2,
80 0, /* contains WCE, needs to be 0 for logic */
81 0, 0, 0, 0, 0, 0, 0, 0, 0,
82 0, /* contains DRA, needs to be 0 for logic */
83 0, 0, 0, 0, 0, 0, 0
84 };
85
86 static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
87 CONTROL_MPAGE,
88 CONTROL_MPAGE_LEN - 2,
89 2, /* DSENSE=0, GLTSD=1 */
90 0, /* [QAM+QERR may be 1, see 05-359r1] */
91 0, 0, 0, 0, 0xff, 0xff,
92 0, 30 /* extended self test time, see 05-359r1 */
93 };
94
95
ata_scsi_invalid_field(struct scsi_cmnd * cmd,void (* done)(struct scsi_cmnd *))96 static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
97 void (*done)(struct scsi_cmnd *))
98 {
99 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
100 /* "Invalid field in cbd" */
101 done(cmd);
102 }
103
104 /**
105 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
106 * @disk: SCSI device for which BIOS geometry is to be determined
107 * @bdev: device major/minor
108 * @geom: location to which geometry will be output
109 *
110 * Generic bios head/sector/cylinder calculator
111 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
112 * mapping. Some situations may arise where the disk is not
113 * bootable if this is not used.
114 *
115 * LOCKING:
116 * Defined by the SCSI layer. We don't really care.
117 *
118 * RETURNS:
119 * Zero.
120 */
ata_std_bios_param(Disk * disk,kdev_t bdev,int * geom)121 int ata_std_bios_param(Disk * disk, /* SCSI disk */
122 kdev_t bdev, /* Device major, minor */
123 int *geom /* Heads, sectors, cylinders in that order */ )
124 {
125 geom[0] = 255;
126 geom[1] = 63;
127 geom[2] = disk->capacity / (geom[0] * geom[1]);
128
129 return 0;
130 }
131
132 /**
133 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
134 * @scsidev: Device to which we are issuing command
135 * @arg: User provided data for issuing command
136 *
137 * LOCKING:
138 * Defined by the SCSI layer. We don't really care.
139 *
140 * RETURNS:
141 * Zero on success, negative errno on error.
142 */
143
ata_cmd_ioctl(struct scsi_device * scsidev,void __user * arg)144 int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
145 {
146 int rc = 0;
147 u8 scsi_cmd[MAX_COMMAND_SIZE];
148 u8 args[4], *argbuf = NULL;
149 int argsize = 0;
150 struct scsi_request *sreq;
151
152 if (NULL == (void *)arg)
153 return -EINVAL;
154
155 if (copy_from_user(args, arg, sizeof(args)))
156 return -EFAULT;
157
158 sreq = scsi_allocate_request(scsidev);
159 if (!sreq)
160 return -EINTR;
161
162 memset(scsi_cmd, 0, sizeof(scsi_cmd));
163
164 if (args[3]) {
165 argsize = SECTOR_SIZE * args[3];
166 argbuf = kmalloc(argsize, GFP_KERNEL);
167 if (argbuf == NULL) {
168 rc = -ENOMEM;
169 goto error;
170 }
171
172 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
173 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
174 block count in sector count field */
175 sreq->sr_data_direction = DMA_FROM_DEVICE;
176 } else {
177 scsi_cmd[1] = (3 << 1); /* Non-data */
178 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
179 sreq->sr_data_direction = DMA_NONE;
180 }
181
182 scsi_cmd[0] = ATA_16;
183
184 scsi_cmd[4] = args[2];
185 if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
186 scsi_cmd[6] = args[3];
187 scsi_cmd[8] = args[1];
188 scsi_cmd[10] = 0x4f;
189 scsi_cmd[12] = 0xc2;
190 } else {
191 scsi_cmd[6] = args[1];
192 }
193 scsi_cmd[14] = args[0];
194
195 /* Good values for timeout and retries? Values below
196 from scsi_ioctl_send_command() for default case... */
197 scsi_wait_req(sreq, scsi_cmd, argbuf, argsize, (10*HZ), 5);
198
199 if (sreq->sr_result) {
200 rc = -EIO;
201 goto error;
202 }
203
204 /* Need code to retrieve data from check condition? */
205
206 if ((argbuf)
207 && copy_to_user((void *)(arg + sizeof(args)), argbuf, argsize))
208 rc = -EFAULT;
209 error:
210 scsi_release_request(sreq);
211
212 if (argbuf)
213 kfree(argbuf);
214
215 return rc;
216 }
217
218 /**
219 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
220 * @scsidev: Device to which we are issuing command
221 * @arg: User provided data for issuing command
222 *
223 * LOCKING:
224 * Defined by the SCSI layer. We don't really care.
225 *
226 * RETURNS:
227 * Zero on success, negative errno on error.
228 */
ata_task_ioctl(struct scsi_device * scsidev,void __user * arg)229 int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
230 {
231 int rc = 0;
232 u8 scsi_cmd[MAX_COMMAND_SIZE];
233 u8 args[7];
234 struct scsi_request *sreq;
235
236 if (NULL == (void *)arg)
237 return -EINVAL;
238
239 if (copy_from_user(args, arg, sizeof(args)))
240 return -EFAULT;
241
242 memset(scsi_cmd, 0, sizeof(scsi_cmd));
243 scsi_cmd[0] = ATA_16;
244 scsi_cmd[1] = (3 << 1); /* Non-data */
245 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
246 scsi_cmd[4] = args[1];
247 scsi_cmd[6] = args[2];
248 scsi_cmd[8] = args[3];
249 scsi_cmd[10] = args[4];
250 scsi_cmd[12] = args[5];
251 scsi_cmd[14] = args[0];
252
253 sreq = scsi_allocate_request(scsidev);
254 if (!sreq) {
255 rc = -EINTR;
256 goto error;
257 }
258
259 sreq->sr_data_direction = DMA_NONE;
260 /* Good values for timeout and retries? Values below
261 from scsi_ioctl_send_command() for default case... */
262 scsi_wait_req(sreq, scsi_cmd, NULL, 0, (10*HZ), 5);
263
264 if (sreq->sr_result) {
265 rc = -EIO;
266 goto error;
267 }
268
269 /* Need code to retrieve data from check condition? */
270
271 error:
272 scsi_release_request(sreq);
273 return rc;
274 }
275
ata_scsi_ioctl(struct scsi_device * scsidev,int cmd,void __user * arg)276 int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
277 {
278 struct ata_port *ap;
279 struct ata_device *dev;
280 int val = -EINVAL, rc = -EINVAL;
281
282 ap = (struct ata_port *) &scsidev->host->hostdata[0];
283 if (!ap)
284 goto out;
285
286 dev = ata_scsi_find_dev(ap, scsidev);
287 if (!dev) {
288 rc = -ENODEV;
289 goto out;
290 }
291
292 switch (cmd) {
293 case ATA_IOC_GET_IO32:
294 val = 0;
295 if (copy_to_user(arg, &val, 1))
296 return -EFAULT;
297 return 0;
298
299 case ATA_IOC_SET_IO32:
300 val = (unsigned long) arg;
301 if (val != 0)
302 return -EINVAL;
303 return 0;
304
305 case HDIO_DRIVE_CMD:
306 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
307 return -EACCES;
308 return ata_cmd_ioctl(scsidev, arg);
309
310 case HDIO_DRIVE_TASK:
311 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
312 return -EACCES;
313 return ata_task_ioctl(scsidev, arg);
314
315 default:
316 rc = -ENOTTY;
317 break;
318 }
319
320 out:
321 return rc;
322 }
323
324 /**
325 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
326 * @ap: ATA port to which the new command is attached
327 * @dev: ATA device to which the new command is attached
328 * @cmd: SCSI command that originated this ATA command
329 * @done: SCSI command completion function
330 *
331 * Obtain a reference to an unused ata_queued_cmd structure,
332 * which is the basic libata structure representing a single
333 * ATA command sent to the hardware.
334 *
335 * If a command was available, fill in the SCSI-specific
336 * portions of the structure with information on the
337 * current command.
338 *
339 * LOCKING:
340 * spin_lock_irqsave(host_set lock)
341 *
342 * RETURNS:
343 * Command allocated, or %NULL if none available.
344 */
ata_scsi_qc_new(struct ata_port * ap,struct ata_device * dev,struct scsi_cmnd * cmd,void (* done)(struct scsi_cmnd *))345 struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
346 struct ata_device *dev,
347 struct scsi_cmnd *cmd,
348 void (*done)(struct scsi_cmnd *))
349 {
350 struct ata_queued_cmd *qc;
351
352 qc = ata_qc_new_init(ap, dev);
353 if (qc) {
354 qc->scsicmd = cmd;
355 qc->scsidone = done;
356
357 if (cmd->use_sg) {
358 qc->__sg = (struct scatterlist *) cmd->request_buffer;
359 qc->n_elem = cmd->use_sg;
360 } else {
361 qc->__sg = &qc->sgent;
362 qc->n_elem = 1;
363 }
364 } else {
365 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
366 done(cmd);
367 }
368
369 return qc;
370 }
371
372 /**
373 * ata_dump_status - user friendly display of error info
374 * @id: id of the port in question
375 * @tf: ptr to filled out taskfile
376 *
377 * Decode and dump the ATA error/status registers for the user so
378 * that they have some idea what really happened at the non
379 * make-believe layer.
380 *
381 * LOCKING:
382 * inherited from caller
383 */
ata_dump_status(unsigned id,struct ata_taskfile * tf)384 void ata_dump_status(unsigned id, struct ata_taskfile *tf)
385 {
386 u8 stat = tf->command, err = tf->feature;
387
388 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
389 if (stat & ATA_BUSY) {
390 printk("Busy }\n"); /* Data is not valid in this case */
391 } else {
392 if (stat & 0x40) printk("DriveReady ");
393 if (stat & 0x20) printk("DeviceFault ");
394 if (stat & 0x10) printk("SeekComplete ");
395 if (stat & 0x08) printk("DataRequest ");
396 if (stat & 0x04) printk("CorrectedError ");
397 if (stat & 0x02) printk("Index ");
398 if (stat & 0x01) printk("Error ");
399 printk("}\n");
400
401 if (err) {
402 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
403 if (err & 0x04) printk("DriveStatusError ");
404 if (err & 0x80) {
405 if (err & 0x04) printk("BadCRC ");
406 else printk("Sector ");
407 }
408 if (err & 0x40) printk("UncorrectableError ");
409 if (err & 0x10) printk("SectorIdNotFound ");
410 if (err & 0x02) printk("TrackZeroNotFound ");
411 if (err & 0x01) printk("AddrMarkNotFound ");
412 printk("}\n");
413 }
414 }
415 }
416
417 /**
418 * ata_to_sense_error - convert ATA error to SCSI error
419 * @id: ATA device number
420 * @drv_stat: value contained in ATA status register
421 * @drv_err: value contained in ATA error register
422 * @sk: the sense key we'll fill out
423 * @asc: the additional sense code we'll fill out
424 * @ascq: the additional sense code qualifier we'll fill out
425 *
426 * Converts an ATA error into a SCSI error. Fill out pointers to
427 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
428 * format sense blocks.
429 *
430 * LOCKING:
431 * spin_lock_irqsave(host_set lock)
432 */
ata_to_sense_error(unsigned id,u8 drv_stat,u8 drv_err,u8 * sk,u8 * asc,u8 * ascq)433 void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
434 u8 *ascq)
435 {
436 int i;
437
438 /* Based on the 3ware driver translation table */
439 static unsigned char sense_table[][4] = {
440 /* BBD|ECC|ID|MAR */
441 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
442 /* BBD|ECC|ID */
443 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
444 /* ECC|MC|MARK */
445 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
446 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
447 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
448 /* MC|ID|ABRT|TRK0|MARK */
449 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
450 /* MCR|MARK */
451 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
452 /* Bad address mark */
453 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
454 /* TRK0 */
455 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
456 /* Abort & !ICRC */
457 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
458 /* Media change request */
459 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
460 /* SRV */
461 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
462 /* Media change */
463 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
464 /* ECC */
465 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
466 /* BBD - block marked bad */
467 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
468 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
469 };
470 static unsigned char stat_table[][4] = {
471 /* Must be first because BUSY means no other bits valid */
472 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
473 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
474 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
475 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
476 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
477 };
478
479 /*
480 * Is this an error we can process/parse
481 */
482 if (drv_stat & ATA_BUSY) {
483 drv_err = 0; /* Ignore the err bits, they're invalid */
484 }
485
486 if (drv_err) {
487 /* Look for drv_err */
488 for (i = 0; sense_table[i][0] != 0xFF; i++) {
489 /* Look for best matches first */
490 if ((sense_table[i][0] & drv_err) ==
491 sense_table[i][0]) {
492 *sk = sense_table[i][1];
493 *asc = sense_table[i][2];
494 *ascq = sense_table[i][3];
495 goto translate_done;
496 }
497 }
498 /* No immediate match */
499 printk(KERN_WARNING "ata%u: no sense translation for "
500 "error 0x%02x\n", id, drv_err);
501 }
502
503 /* Fall back to interpreting status bits */
504 for (i = 0; stat_table[i][0] != 0xFF; i++) {
505 if (stat_table[i][0] & drv_stat) {
506 *sk = stat_table[i][1];
507 *asc = stat_table[i][2];
508 *ascq = stat_table[i][3];
509 goto translate_done;
510 }
511 }
512 /* No error? Undecoded? */
513 printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n",
514 id, drv_stat);
515
516 /* For our last chance pick, use medium read error because
517 * it's much more common than an ATA drive telling you a write
518 * has failed.
519 */
520 *sk = MEDIUM_ERROR;
521 *asc = 0x11; /* "unrecovered read error" */
522 *ascq = 0x04; /* "auto-reallocation failed" */
523
524 translate_done:
525 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to "
526 "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err,
527 *sk, *asc, *ascq);
528 return;
529 }
530
531 /*
532 * ata_gen_ata_desc_sense - Generate check condition sense block.
533 * @qc: Command that completed.
534 *
535 * This function is specific to the ATA descriptor format sense
536 * block specified for the ATA pass through commands. Regardless
537 * of whether the command errored or not, return a sense
538 * block. Copy all controller registers into the sense
539 * block. Clear sense key, ASC & ASCQ if there is no error.
540 *
541 * LOCKING:
542 * spin_lock_irqsave(host_set lock)
543 */
ata_gen_ata_desc_sense(struct ata_queued_cmd * qc)544 void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
545 {
546 struct scsi_cmnd *cmd = qc->scsicmd;
547 struct ata_taskfile *tf = &qc->tf;
548 unsigned char *sb = cmd->sense_buffer;
549 unsigned char *desc = sb + 8;
550
551 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
552
553 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
554
555 /*
556 * Read the controller registers.
557 */
558 assert(NULL != qc->ap->ops->tf_read);
559 qc->ap->ops->tf_read(qc->ap, tf);
560
561 /*
562 * Use ata_to_sense_error() to map status register bits
563 * onto sense key, asc & ascq.
564 */
565 if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
566 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
567 &sb[1], &sb[2], &sb[3]);
568 sb[1] &= 0x0f;
569 }
570
571 /*
572 * Sense data is current and format is descriptor.
573 */
574 sb[0] = 0x72;
575
576 desc[0] = 0x09;
577
578 /*
579 * Set length of additional sense data.
580 * Since we only populate descriptor 0, the total
581 * length is the same (fixed) length as descriptor 0.
582 */
583 desc[1] = sb[7] = 14;
584
585 /*
586 * Copy registers into sense buffer.
587 */
588 desc[2] = 0x00;
589 desc[3] = tf->feature; /* == error reg */
590 desc[5] = tf->nsect;
591 desc[7] = tf->lbal;
592 desc[9] = tf->lbam;
593 desc[11] = tf->lbah;
594 desc[12] = tf->device;
595 desc[13] = tf->command; /* == status reg */
596
597 /*
598 * Fill in Extend bit, and the high order bytes
599 * if applicable.
600 */
601 if (tf->flags & ATA_TFLAG_LBA48) {
602 desc[2] |= 0x01;
603 desc[4] = tf->hob_nsect;
604 desc[6] = tf->hob_lbal;
605 desc[8] = tf->hob_lbam;
606 desc[10] = tf->hob_lbah;
607 }
608 }
609
610 /**
611 * ata_gen_fixed_sense - generate a SCSI fixed sense block
612 * @qc: Command that we are erroring out
613 *
614 * Leverage ata_to_sense_error() to give us the codes. Fit our
615 * LBA in here if there's room.
616 *
617 * LOCKING:
618 * inherited from caller
619 */
ata_gen_fixed_sense(struct ata_queued_cmd * qc)620 void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
621 {
622 struct scsi_cmnd *cmd = qc->scsicmd;
623 struct ata_taskfile *tf = &qc->tf;
624 unsigned char *sb = cmd->sense_buffer;
625
626 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
627
628 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
629
630 /*
631 * Read the controller registers.
632 */
633 assert(NULL != qc->ap->ops->tf_read);
634 qc->ap->ops->tf_read(qc->ap, tf);
635
636 /*
637 * Use ata_to_sense_error() to map status register bits
638 * onto sense key, asc & ascq.
639 */
640 if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
641 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
642 &sb[2], &sb[12], &sb[13]);
643 sb[2] &= 0x0f;
644 }
645
646 sb[0] = 0x70;
647 sb[7] = 0x0a;
648
649 if (tf->flags & ATA_TFLAG_LBA48) {
650 /* TODO: find solution for LBA48 descriptors */
651 }
652
653 else if (tf->flags & ATA_TFLAG_LBA) {
654 /* A small (28b) LBA will fit in the 32b info field */
655 sb[0] |= 0x80; /* set valid bit */
656 sb[3] = tf->device & 0x0f;
657 sb[4] = tf->lbah;
658 sb[5] = tf->lbam;
659 sb[6] = tf->lbal;
660 }
661
662 else {
663 /* TODO: C/H/S */
664 }
665 }
666
667 /**
668 * ata_scsi_error - SCSI layer error handler callback
669 * @host: SCSI host on which error occurred
670 *
671 * Handles SCSI-layer-thrown error events.
672 *
673 * LOCKING:
674 * Inherited from SCSI layer (none, can sleep)
675 *
676 * RETURNS:
677 * Zero.
678 */
679
ata_scsi_error(struct Scsi_Host * host)680 int ata_scsi_error(struct Scsi_Host *host)
681 {
682 struct ata_port *ap;
683
684 DPRINTK("ENTER\n");
685
686 ap = (struct ata_port *) &host->hostdata[0];
687 ap->ops->eng_timeout(ap);
688
689 host->in_recovery = 0;
690
691 /* TODO: this is per-command; when queueing is supported
692 * this code will either change or move to a more
693 * appropriate place
694 */
695 host->host_failed--;
696
697 DPRINTK("EXIT\n");
698 return 0;
699 }
700
701 /**
702 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
703 * @qc: Storage for translated ATA taskfile
704 * @scsicmd: SCSI command to translate
705 *
706 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
707 * (to start). Perhaps these commands should be preceded by
708 * CHECK POWER MODE to see what power mode the device is already in.
709 * [See SAT revision 5 at www.t10.org]
710 *
711 * LOCKING:
712 * spin_lock_irqsave(host_set lock)
713 *
714 * RETURNS:
715 * Zero on success, non-zero on error.
716 */
717
ata_scsi_start_stop_xlat(struct ata_queued_cmd * qc,const u8 * scsicmd)718 static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
719 const u8 *scsicmd)
720 {
721 struct ata_taskfile *tf = &qc->tf;
722
723 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
724 tf->protocol = ATA_PROT_NODATA;
725 if (scsicmd[1] & 0x1) {
726 ; /* ignore IMMED bit, violates sat-r05 */
727 }
728 if (scsicmd[4] & 0x2)
729 goto invalid_fld; /* LOEJ bit set not supported */
730 if (((scsicmd[4] >> 4) & 0xf) != 0)
731 goto invalid_fld; /* power conditions not supported */
732 if (scsicmd[4] & 0x1) {
733 tf->nsect = 1; /* 1 sector, lba=0 */
734
735 if (qc->dev->flags & ATA_DFLAG_LBA) {
736 qc->tf.flags |= ATA_TFLAG_LBA;
737
738 tf->lbah = 0x0;
739 tf->lbam = 0x0;
740 tf->lbal = 0x0;
741 tf->device |= ATA_LBA;
742 } else {
743 /* CHS */
744 tf->lbal = 0x1; /* sect */
745 tf->lbam = 0x0; /* cyl low */
746 tf->lbah = 0x0; /* cyl high */
747 }
748
749 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
750 } else {
751 tf->nsect = 0; /* time period value (0 implies now) */
752 tf->command = ATA_CMD_STANDBY;
753 /* Consider: ATA STANDBY IMMEDIATE command */
754 }
755 /*
756 * Standby and Idle condition timers could be implemented but that
757 * would require libata to implement the Power condition mode page
758 * and allow the user to change it. Changing mode pages requires
759 * MODE SELECT to be implemented.
760 */
761
762 return 0;
763
764 invalid_fld:
765 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
766 /* "Invalid field in cbd" */
767 return 1;
768 }
769
770
771 /**
772 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
773 * @qc: Storage for translated ATA taskfile
774 * @scsicmd: SCSI command to translate (ignored)
775 *
776 * Sets up an ATA taskfile to issue FLUSH CACHE or
777 * FLUSH CACHE EXT.
778 *
779 * LOCKING:
780 * spin_lock_irqsave(host_set lock)
781 *
782 * RETURNS:
783 * Zero on success, non-zero on error.
784 */
785
ata_scsi_flush_xlat(struct ata_queued_cmd * qc,const u8 * scsicmd)786 static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
787 {
788 struct ata_taskfile *tf = &qc->tf;
789
790 tf->flags |= ATA_TFLAG_DEVICE;
791 tf->protocol = ATA_PROT_NODATA;
792
793 if ((qc->dev->flags & ATA_DFLAG_LBA48) &&
794 (ata_id_has_flush_ext(qc->dev->id)))
795 tf->command = ATA_CMD_FLUSH_EXT;
796 else
797 tf->command = ATA_CMD_FLUSH;
798
799 return 0;
800 }
801
802 /**
803 * scsi_6_lba_len - Get LBA and transfer length
804 * @scsicmd: SCSI command to translate
805 *
806 * Calculate LBA and transfer length for 6-byte commands.
807 *
808 * RETURNS:
809 * @plba: the LBA
810 * @plen: the transfer length
811 */
812
scsi_6_lba_len(const u8 * scsicmd,u64 * plba,u32 * plen)813 static void scsi_6_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
814 {
815 u64 lba = 0;
816 u32 len = 0;
817
818 VPRINTK("six-byte command\n");
819
820 lba |= ((u64)scsicmd[2]) << 8;
821 lba |= ((u64)scsicmd[3]);
822
823 len |= ((u32)scsicmd[4]);
824
825 *plba = lba;
826 *plen = len;
827 }
828
829 /**
830 * scsi_10_lba_len - Get LBA and transfer length
831 * @scsicmd: SCSI command to translate
832 *
833 * Calculate LBA and transfer length for 10-byte commands.
834 *
835 * RETURNS:
836 * @plba: the LBA
837 * @plen: the transfer length
838 */
839
scsi_10_lba_len(const u8 * scsicmd,u64 * plba,u32 * plen)840 static void scsi_10_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
841 {
842 u64 lba = 0;
843 u32 len = 0;
844
845 VPRINTK("ten-byte command\n");
846
847 lba |= ((u64)scsicmd[2]) << 24;
848 lba |= ((u64)scsicmd[3]) << 16;
849 lba |= ((u64)scsicmd[4]) << 8;
850 lba |= ((u64)scsicmd[5]);
851
852 len |= ((u32)scsicmd[7]) << 8;
853 len |= ((u32)scsicmd[8]);
854
855 *plba = lba;
856 *plen = len;
857 }
858
859 /**
860 * scsi_16_lba_len - Get LBA and transfer length
861 * @scsicmd: SCSI command to translate
862 *
863 * Calculate LBA and transfer length for 16-byte commands.
864 *
865 * RETURNS:
866 * @plba: the LBA
867 * @plen: the transfer length
868 */
869
scsi_16_lba_len(const u8 * scsicmd,u64 * plba,u32 * plen)870 static void scsi_16_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
871 {
872 u64 lba = 0;
873 u32 len = 0;
874
875 VPRINTK("sixteen-byte command\n");
876
877 lba |= ((u64)scsicmd[2]) << 56;
878 lba |= ((u64)scsicmd[3]) << 48;
879 lba |= ((u64)scsicmd[4]) << 40;
880 lba |= ((u64)scsicmd[5]) << 32;
881 lba |= ((u64)scsicmd[6]) << 24;
882 lba |= ((u64)scsicmd[7]) << 16;
883 lba |= ((u64)scsicmd[8]) << 8;
884 lba |= ((u64)scsicmd[9]);
885
886 len |= ((u32)scsicmd[10]) << 24;
887 len |= ((u32)scsicmd[11]) << 16;
888 len |= ((u32)scsicmd[12]) << 8;
889 len |= ((u32)scsicmd[13]);
890
891 *plba = lba;
892 *plen = len;
893 }
894
895 /**
896 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
897 * @qc: Storage for translated ATA taskfile
898 * @scsicmd: SCSI command to translate
899 *
900 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
901 *
902 * LOCKING:
903 * spin_lock_irqsave(host_set lock)
904 *
905 * RETURNS:
906 * Zero on success, non-zero on error.
907 */
908
ata_scsi_verify_xlat(struct ata_queued_cmd * qc,const u8 * scsicmd)909 static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
910 {
911 struct ata_taskfile *tf = &qc->tf;
912 struct ata_device *dev = qc->dev;
913 u64 dev_sectors = qc->dev->n_sectors;
914 u64 block;
915 u32 n_block;
916
917 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
918 tf->protocol = ATA_PROT_NODATA;
919
920 if (scsicmd[0] == VERIFY)
921 scsi_10_lba_len(scsicmd, &block, &n_block);
922 else if (scsicmd[0] == VERIFY_16)
923 scsi_16_lba_len(scsicmd, &block, &n_block);
924 else
925 goto invalid_fld;
926
927 if (!n_block)
928 goto nothing_to_do;
929 if (block >= dev_sectors)
930 goto out_of_range;
931 if ((block + n_block) > dev_sectors)
932 goto out_of_range;
933
934 if (dev->flags & ATA_DFLAG_LBA) {
935 tf->flags |= ATA_TFLAG_LBA;
936
937 if (dev->flags & ATA_DFLAG_LBA48) {
938 if (n_block > (64 * 1024))
939 goto invalid_fld;
940
941 /* use LBA48 */
942 tf->flags |= ATA_TFLAG_LBA48;
943 tf->command = ATA_CMD_VERIFY_EXT;
944
945 tf->hob_nsect = (n_block >> 8) & 0xff;
946
947 tf->hob_lbah = (block >> 40) & 0xff;
948 tf->hob_lbam = (block >> 32) & 0xff;
949 tf->hob_lbal = (block >> 24) & 0xff;
950 } else {
951 if (n_block > 256)
952 goto invalid_fld;
953
954 /* use LBA28 */
955 tf->command = ATA_CMD_VERIFY;
956
957 tf->device |= (block >> 24) & 0xf;
958 }
959
960 tf->nsect = n_block & 0xff;
961
962 tf->lbah = (block >> 16) & 0xff;
963 tf->lbam = (block >> 8) & 0xff;
964 tf->lbal = block & 0xff;
965
966 tf->device |= ATA_LBA;
967 } else {
968 /* CHS */
969 u32 sect, head, cyl, track;
970
971 if (n_block > 256)
972 goto invalid_fld;
973
974 /* Convert LBA to CHS */
975 track = (u32)block / dev->sectors;
976 cyl = track / dev->heads;
977 head = track % dev->heads;
978 sect = (u32)block % dev->sectors + 1;
979
980 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
981 (u32)block, track, cyl, head, sect);
982
983 /* Check whether the converted CHS can fit.
984 Cylinder: 0-65535
985 Head: 0-15
986 Sector: 1-255*/
987 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
988 goto out_of_range;
989
990 tf->command = ATA_CMD_VERIFY;
991 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
992 tf->lbal = sect;
993 tf->lbam = cyl;
994 tf->lbah = cyl >> 8;
995 tf->device |= head;
996 }
997
998 return 0;
999
1000 invalid_fld:
1001 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
1002 /* "Invalid field in cbd" */
1003 return 1;
1004
1005 out_of_range:
1006 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
1007 /* "Logical Block Address out of range" */
1008 return 1;
1009
1010 nothing_to_do:
1011 qc->scsicmd->result = SAM_STAT_GOOD;
1012 return 1;
1013 }
1014
1015 /**
1016 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1017 * @qc: Storage for translated ATA taskfile
1018 * @scsicmd: SCSI command to translate
1019 *
1020 * Converts any of six SCSI read/write commands into the
1021 * ATA counterpart, including starting sector (LBA),
1022 * sector count, and taking into account the device's LBA48
1023 * support.
1024 *
1025 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1026 * %WRITE_16 are currently supported.
1027 *
1028 * LOCKING:
1029 * spin_lock_irqsave(host_set lock)
1030 *
1031 * RETURNS:
1032 * Zero on success, non-zero on error.
1033 */
1034
ata_scsi_rw_xlat(struct ata_queued_cmd * qc,const u8 * scsicmd)1035 static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
1036 {
1037 struct ata_taskfile *tf = &qc->tf;
1038 struct ata_device *dev = qc->dev;
1039 u64 block;
1040 u32 n_block;
1041
1042 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1043
1044 if (scsicmd[0] == WRITE_10 || scsicmd[0] == WRITE_6 ||
1045 scsicmd[0] == WRITE_16)
1046 tf->flags |= ATA_TFLAG_WRITE;
1047
1048 /* Calculate the SCSI LBA and transfer length. */
1049 switch (scsicmd[0]) {
1050 case READ_10:
1051 case WRITE_10:
1052 scsi_10_lba_len(scsicmd, &block, &n_block);
1053 break;
1054 case READ_6:
1055 case WRITE_6:
1056 scsi_6_lba_len(scsicmd, &block, &n_block);
1057
1058 /* for 6-byte r/w commands, transfer length 0
1059 * means 256 blocks of data, not 0 block.
1060 */
1061 if (!n_block)
1062 n_block = 256;
1063 break;
1064 case READ_16:
1065 case WRITE_16:
1066 scsi_16_lba_len(scsicmd, &block, &n_block);
1067 break;
1068 default:
1069 DPRINTK("no-byte command\n");
1070 goto invalid_fld;
1071 }
1072
1073 /* Check and compose ATA command */
1074 if (!n_block)
1075 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1076 * length 0 means transfer 0 block of data.
1077 * However, for ATA R/W commands, sector count 0 means
1078 * 256 or 65536 sectors, not 0 sectors as in SCSI.
1079 *
1080 * WARNING: one or two older ATA drives treat 0 as 0...
1081 */
1082 goto nothing_to_do;
1083
1084 if (dev->flags & ATA_DFLAG_LBA) {
1085 tf->flags |= ATA_TFLAG_LBA;
1086
1087 if (dev->flags & ATA_DFLAG_LBA48) {
1088 /* The request -may- be too large for LBA48. */
1089 if ((block >> 48) || (n_block > 65536))
1090 goto out_of_range;
1091
1092 /* use LBA48 */
1093 tf->flags |= ATA_TFLAG_LBA48;
1094
1095 tf->hob_nsect = (n_block >> 8) & 0xff;
1096
1097 tf->hob_lbah = (block >> 40) & 0xff;
1098 tf->hob_lbam = (block >> 32) & 0xff;
1099 tf->hob_lbal = (block >> 24) & 0xff;
1100 } else {
1101 /* use LBA28 */
1102
1103 /* The request -may- be too large for LBA28. */
1104 if ((block >> 28) || (n_block > 256))
1105 goto out_of_range;
1106
1107 tf->device |= (block >> 24) & 0xf;
1108 }
1109
1110 ata_rwcmd_protocol(qc);
1111
1112 qc->nsect = n_block;
1113 tf->nsect = n_block & 0xff;
1114
1115 tf->lbah = (block >> 16) & 0xff;
1116 tf->lbam = (block >> 8) & 0xff;
1117 tf->lbal = block & 0xff;
1118
1119 tf->device |= ATA_LBA;
1120 } else {
1121 /* CHS */
1122 u32 sect, head, cyl, track;
1123
1124 /* The request -may- be too large for CHS addressing. */
1125 if ((block >> 28) || (n_block > 256))
1126 goto out_of_range;
1127
1128 ata_rwcmd_protocol(qc);
1129
1130 /* Convert LBA to CHS */
1131 track = (u32)block / dev->sectors;
1132 cyl = track / dev->heads;
1133 head = track % dev->heads;
1134 sect = (u32)block % dev->sectors + 1;
1135
1136 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1137 (u32)block, track, cyl, head, sect);
1138
1139 /* Check whether the converted CHS can fit.
1140 Cylinder: 0-65535
1141 Head: 0-15
1142 Sector: 1-255*/
1143 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
1144 goto out_of_range;
1145
1146 qc->nsect = n_block;
1147 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1148 tf->lbal = sect;
1149 tf->lbam = cyl;
1150 tf->lbah = cyl >> 8;
1151 tf->device |= head;
1152 }
1153
1154 return 0;
1155
1156 invalid_fld:
1157 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
1158 /* "Invalid field in cbd" */
1159 return 1;
1160
1161 out_of_range:
1162 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
1163 /* "Logical Block Address out of range" */
1164 return 1;
1165
1166 nothing_to_do:
1167 qc->scsicmd->result = SAM_STAT_GOOD;
1168 return 1;
1169 }
1170
ata_scsi_qc_complete(struct ata_queued_cmd * qc,unsigned int err_mask)1171 static int ata_scsi_qc_complete(struct ata_queued_cmd *qc,
1172 unsigned int err_mask)
1173 {
1174 struct scsi_cmnd *cmd = qc->scsicmd;
1175 u8 *cdb = cmd->cmnd;
1176 int need_sense = (err_mask != 0);
1177
1178 /* For ATA pass thru (SAT) commands, generate a sense block if
1179 * user mandated it or if there's an error. Note that if we
1180 * generate because the user forced us to, a check condition
1181 * is generated and the ATA register values are returned
1182 * whether the command completed successfully or not. If there
1183 * was no error, SK, ASC and ASCQ will all be zero.
1184 */
1185 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
1186 ((cdb[2] & 0x20) || need_sense)) {
1187 ata_gen_ata_desc_sense(qc);
1188 } else {
1189 if (!need_sense) {
1190 cmd->result = SAM_STAT_GOOD;
1191 } else {
1192 /* TODO: decide which descriptor format to use
1193 * for 48b LBA devices and call that here
1194 * instead of the fixed desc, which is only
1195 * good for smaller LBA (and maybe CHS?)
1196 * devices.
1197 */
1198 ata_gen_fixed_sense(qc);
1199 }
1200 }
1201
1202 if (need_sense) {
1203 /* The ata_gen_..._sense routines fill in tf */
1204 ata_dump_status(qc->ap->id, &qc->tf);
1205 }
1206
1207 qc->scsidone(cmd);
1208
1209 return 0;
1210 }
1211
1212 /**
1213 * ata_scsi_translate - Translate then issue SCSI command to ATA device
1214 * @ap: ATA port to which the command is addressed
1215 * @dev: ATA device to which the command is addressed
1216 * @cmd: SCSI command to execute
1217 * @done: SCSI command completion function
1218 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1219 *
1220 * Our ->queuecommand() function has decided that the SCSI
1221 * command issued can be directly translated into an ATA
1222 * command, rather than handled internally.
1223 *
1224 * This function sets up an ata_queued_cmd structure for the
1225 * SCSI command, and sends that ata_queued_cmd to the hardware.
1226 *
1227 * The xlat_func argument (actor) returns 0 if ready to execute
1228 * ATA command, else 1 to finish translation. If 1 is returned
1229 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1230 * to be set reflecting an error condition or clean (early)
1231 * termination.
1232 *
1233 * LOCKING:
1234 * spin_lock_irqsave(host_set lock)
1235 */
1236
ata_scsi_translate(struct ata_port * ap,struct ata_device * dev,struct scsi_cmnd * cmd,void (* done)(struct scsi_cmnd *),ata_xlat_func_t xlat_func)1237 static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
1238 struct scsi_cmnd *cmd,
1239 void (*done)(struct scsi_cmnd *),
1240 ata_xlat_func_t xlat_func)
1241 {
1242 struct ata_queued_cmd *qc;
1243 u8 *scsicmd = cmd->cmnd;
1244
1245 VPRINTK("ENTER\n");
1246
1247 qc = ata_scsi_qc_new(ap, dev, cmd, done);
1248 if (!qc)
1249 goto err_mem;
1250
1251 /* data is present; dma-map it */
1252 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1253 cmd->sc_data_direction == DMA_TO_DEVICE) {
1254 if (unlikely(cmd->request_bufflen < 1)) {
1255 printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
1256 ap->id, dev->devno);
1257 goto err_did;
1258 }
1259
1260 if (cmd->use_sg)
1261 ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
1262 else
1263 ata_sg_init_one(qc, cmd->request_buffer,
1264 cmd->request_bufflen);
1265
1266 qc->dma_dir = cmd->sc_data_direction;
1267 }
1268
1269 qc->complete_fn = ata_scsi_qc_complete;
1270
1271 if (xlat_func(qc, scsicmd))
1272 goto early_finish;
1273
1274 /* select device, send command to hardware */
1275 if (ata_qc_issue(qc))
1276 goto err_did;
1277
1278 VPRINTK("EXIT\n");
1279 return;
1280
1281 early_finish:
1282 ata_qc_free(qc);
1283 done(cmd);
1284 DPRINTK("EXIT - early finish (good or error)\n");
1285 return;
1286
1287 err_did:
1288 ata_qc_free(qc);
1289 err_mem:
1290 cmd->result = (DID_ERROR << 16);
1291 done(cmd);
1292 DPRINTK("EXIT - internal\n");
1293 return;
1294 }
1295
1296 /**
1297 * ata_scsi_rbuf_get - Map response buffer.
1298 * @cmd: SCSI command containing buffer to be mapped.
1299 * @buf_out: Pointer to mapped area.
1300 *
1301 * Maps buffer contained within SCSI command @cmd.
1302 *
1303 * LOCKING:
1304 * spin_lock_irqsave(host_set lock)
1305 *
1306 * RETURNS:
1307 * Length of response buffer.
1308 */
1309
ata_scsi_rbuf_get(struct scsi_cmnd * cmd,u8 ** buf_out)1310 static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
1311 {
1312 u8 *buf;
1313 unsigned int buflen;
1314
1315 if (cmd->use_sg) {
1316 struct scatterlist *sg;
1317
1318 sg = (struct scatterlist *) cmd->request_buffer;
1319 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
1320 buflen = sg->length;
1321 } else {
1322 buf = cmd->request_buffer;
1323 buflen = cmd->request_bufflen;
1324 }
1325
1326 *buf_out = buf;
1327 return buflen;
1328 }
1329
1330 /**
1331 * ata_scsi_rbuf_put - Unmap response buffer.
1332 * @cmd: SCSI command containing buffer to be unmapped.
1333 * @buf: buffer to unmap
1334 *
1335 * Unmaps response buffer contained within @cmd.
1336 *
1337 * LOCKING:
1338 * spin_lock_irqsave(host_set lock)
1339 */
1340
ata_scsi_rbuf_put(struct scsi_cmnd * cmd,u8 * buf)1341 static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
1342 {
1343 if (cmd->use_sg) {
1344 struct scatterlist *sg;
1345
1346 sg = (struct scatterlist *) cmd->request_buffer;
1347 kunmap_atomic(buf - sg->offset, KM_USER0);
1348 }
1349 }
1350
1351 /**
1352 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1353 * @args: device IDENTIFY data / SCSI command of interest.
1354 * @actor: Callback hook for desired SCSI command simulator
1355 *
1356 * Takes care of the hard work of simulating a SCSI command...
1357 * Mapping the response buffer, calling the command's handler,
1358 * and handling the handler's return value. This return value
1359 * indicates whether the handler wishes the SCSI command to be
1360 * completed successfully (0), or not (in which case cmd->result
1361 * and sense buffer are assumed to be set).
1362 *
1363 * LOCKING:
1364 * spin_lock_irqsave(host_set lock)
1365 */
1366
ata_scsi_rbuf_fill(struct ata_scsi_args * args,unsigned int (* actor)(struct ata_scsi_args * args,u8 * rbuf,unsigned int buflen))1367 void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
1368 unsigned int (*actor) (struct ata_scsi_args *args,
1369 u8 *rbuf, unsigned int buflen))
1370 {
1371 u8 *rbuf;
1372 unsigned int buflen, rc;
1373 struct scsi_cmnd *cmd = args->cmd;
1374
1375 buflen = ata_scsi_rbuf_get(cmd, &rbuf);
1376 memset(rbuf, 0, buflen);
1377 rc = actor(args, rbuf, buflen);
1378 ata_scsi_rbuf_put(cmd, rbuf);
1379
1380 if (rc == 0)
1381 cmd->result = SAM_STAT_GOOD;
1382 args->done(cmd);
1383 }
1384
1385 /**
1386 * ata_scsiop_inq_std - Simulate INQUIRY command
1387 * @args: device IDENTIFY data / SCSI command of interest.
1388 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1389 * @buflen: Response buffer length.
1390 *
1391 * Returns standard device identification data associated
1392 * with non-EVPD INQUIRY command output.
1393 *
1394 * LOCKING:
1395 * spin_lock_irqsave(host_set lock)
1396 */
1397
ata_scsiop_inq_std(struct ata_scsi_args * args,u8 * rbuf,unsigned int buflen)1398 unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
1399 unsigned int buflen)
1400 {
1401 u8 hdr[] = {
1402 TYPE_DISK,
1403 0,
1404 0x5, /* claim SPC-3 version compatibility */
1405 2,
1406 95 - 4
1407 };
1408
1409 /* set scsi removeable (RMB) bit per ata bit */
1410 if (ata_id_removeable(args->id))
1411 hdr[1] |= (1 << 7);
1412
1413 VPRINTK("ENTER\n");
1414
1415 memcpy(rbuf, hdr, sizeof(hdr));
1416
1417 if (buflen > 35) {
1418 memcpy(&rbuf[8], "ATA ", 8);
1419 ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
1420 ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
1421 if (rbuf[32] == 0 || rbuf[32] == ' ')
1422 memcpy(&rbuf[32], "n/a ", 4);
1423 }
1424
1425 if (buflen > 63) {
1426 const u8 versions[] = {
1427 0x60, /* SAM-3 (no version claimed) */
1428
1429 0x03,
1430 0x20, /* SBC-2 (no version claimed) */
1431
1432 0x02,
1433 0x60 /* SPC-3 (no version claimed) */
1434 };
1435
1436 memcpy(rbuf + 59, versions, sizeof(versions));
1437 }
1438
1439 return 0;
1440 }
1441
1442 /**
1443 * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
1444 * @args: device IDENTIFY data / SCSI command of interest.
1445 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1446 * @buflen: Response buffer length.
1447 *
1448 * Returns list of inquiry EVPD pages available.
1449 *
1450 * LOCKING:
1451 * spin_lock_irqsave(host_set lock)
1452 */
1453
ata_scsiop_inq_00(struct ata_scsi_args * args,u8 * rbuf,unsigned int buflen)1454 unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
1455 unsigned int buflen)
1456 {
1457 const u8 pages[] = {
1458 0x00, /* page 0x00, this page */
1459 0x80, /* page 0x80, unit serial no page */
1460 0x83 /* page 0x83, device ident page */
1461 };
1462 rbuf[3] = sizeof(pages); /* number of supported EVPD pages */
1463
1464 if (buflen > 6)
1465 memcpy(rbuf + 4, pages, sizeof(pages));
1466
1467 return 0;
1468 }
1469
1470 /**
1471 * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
1472 * @args: device IDENTIFY data / SCSI command of interest.
1473 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1474 * @buflen: Response buffer length.
1475 *
1476 * Returns ATA device serial number.
1477 *
1478 * LOCKING:
1479 * spin_lock_irqsave(host_set lock)
1480 */
1481
ata_scsiop_inq_80(struct ata_scsi_args * args,u8 * rbuf,unsigned int buflen)1482 unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
1483 unsigned int buflen)
1484 {
1485 const u8 hdr[] = {
1486 0,
1487 0x80, /* this page code */
1488 0,
1489 ATA_SERNO_LEN, /* page len */
1490 };
1491 memcpy(rbuf, hdr, sizeof(hdr));
1492
1493 if (buflen > (ATA_SERNO_LEN + 4 - 1))
1494 ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
1495 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1496
1497 return 0;
1498 }
1499
1500 static const char *inq_83_str = "Linux ATA-SCSI simulator";
1501
1502 /**
1503 * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
1504 * @args: device IDENTIFY data / SCSI command of interest.
1505 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1506 * @buflen: Response buffer length.
1507 *
1508 * Returns device identification. Currently hardcoded to
1509 * return "Linux ATA-SCSI simulator".
1510 *
1511 * LOCKING:
1512 * spin_lock_irqsave(host_set lock)
1513 */
1514
ata_scsiop_inq_83(struct ata_scsi_args * args,u8 * rbuf,unsigned int buflen)1515 unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
1516 unsigned int buflen)
1517 {
1518 rbuf[1] = 0x83; /* this page code */
1519 rbuf[3] = 4 + strlen(inq_83_str); /* page len */
1520
1521 /* our one and only identification descriptor (vendor-specific) */
1522 if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
1523 rbuf[4 + 0] = 2; /* code set: ASCII */
1524 rbuf[4 + 3] = strlen(inq_83_str);
1525 memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
1526 }
1527
1528 return 0;
1529 }
1530
1531 /**
1532 * ata_scsiop_noop - Command handler that simply returns success.
1533 * @args: device IDENTIFY data / SCSI command of interest.
1534 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1535 * @buflen: Response buffer length.
1536 *
1537 * No operation. Simply returns success to caller, to indicate
1538 * that the caller should successfully complete this SCSI command.
1539 *
1540 * LOCKING:
1541 * spin_lock_irqsave(host_set lock)
1542 */
1543
ata_scsiop_noop(struct ata_scsi_args * args,u8 * rbuf,unsigned int buflen)1544 unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
1545 unsigned int buflen)
1546 {
1547 VPRINTK("ENTER\n");
1548 return 0;
1549 }
1550
1551 /**
1552 * ata_msense_push - Push data onto MODE SENSE data output buffer
1553 * @ptr_io: (input/output) Location to store more output data
1554 * @last: End of output data buffer
1555 * @buf: Pointer to BLOB being added to output buffer
1556 * @buflen: Length of BLOB
1557 *
1558 * Store MODE SENSE data on an output buffer.
1559 *
1560 * LOCKING:
1561 * None.
1562 */
1563
ata_msense_push(u8 ** ptr_io,const u8 * last,const u8 * buf,unsigned int buflen)1564 static void ata_msense_push(u8 **ptr_io, const u8 *last,
1565 const u8 *buf, unsigned int buflen)
1566 {
1567 u8 *ptr = *ptr_io;
1568
1569 if ((ptr + buflen - 1) > last)
1570 return;
1571
1572 memcpy(ptr, buf, buflen);
1573
1574 ptr += buflen;
1575
1576 *ptr_io = ptr;
1577 }
1578
1579 /**
1580 * ata_msense_caching - Simulate MODE SENSE caching info page
1581 * @id: device IDENTIFY data
1582 * @ptr_io: (input/output) Location to store more output data
1583 * @last: End of output data buffer
1584 *
1585 * Generate a caching info page, which conditionally indicates
1586 * write caching to the SCSI layer, depending on device
1587 * capabilities.
1588 *
1589 * LOCKING:
1590 * None.
1591 */
1592
ata_msense_caching(u16 * id,u8 ** ptr_io,const u8 * last)1593 static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1594 const u8 *last)
1595 {
1596 u8 page[CACHE_MPAGE_LEN];
1597
1598 memcpy(page, def_cache_mpage, sizeof(page));
1599 if (ata_id_wcache_enabled(id))
1600 page[2] |= (1 << 2); /* write cache enable */
1601 if (!ata_id_rahead_enabled(id))
1602 page[12] |= (1 << 5); /* disable read ahead */
1603
1604 ata_msense_push(ptr_io, last, page, sizeof(page));
1605 return sizeof(page);
1606 }
1607
1608 /**
1609 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1610 * @dev: Device associated with this MODE SENSE command
1611 * @ptr_io: (input/output) Location to store more output data
1612 * @last: End of output data buffer
1613 *
1614 * Generate a generic MODE SENSE control mode page.
1615 *
1616 * LOCKING:
1617 * None.
1618 */
1619
ata_msense_ctl_mode(u8 ** ptr_io,const u8 * last)1620 static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1621 {
1622 ata_msense_push(ptr_io, last, def_control_mpage,
1623 sizeof(def_control_mpage));
1624 return sizeof(def_control_mpage);
1625 }
1626
1627 /**
1628 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1629 * @dev: Device associated with this MODE SENSE command
1630 * @ptr_io: (input/output) Location to store more output data
1631 * @last: End of output data buffer
1632 *
1633 * Generate a generic MODE SENSE r/w error recovery page.
1634 *
1635 * LOCKING:
1636 * None.
1637 */
1638
ata_msense_rw_recovery(u8 ** ptr_io,const u8 * last)1639 static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1640 {
1641
1642 ata_msense_push(ptr_io, last, def_rw_recovery_mpage,
1643 sizeof(def_rw_recovery_mpage));
1644 return sizeof(def_rw_recovery_mpage);
1645 }
1646
1647 /**
1648 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1649 * @args: device IDENTIFY data / SCSI command of interest.
1650 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1651 * @buflen: Response buffer length.
1652 *
1653 * Simulate MODE SENSE commands. Assume this is invoked for direct
1654 * access devices (e.g. disks) only. There should be no block
1655 * descriptor for other device types.
1656 *
1657 * LOCKING:
1658 * spin_lock_irqsave(host_set lock)
1659 */
1660
ata_scsiop_mode_sense(struct ata_scsi_args * args,u8 * rbuf,unsigned int buflen)1661 unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1662 unsigned int buflen)
1663 {
1664 u8 *scsicmd = args->cmd->cmnd, *p, *last;
1665 const u8 sat_blk_desc[] = {
1666 0, 0, 0, 0, /* number of blocks: sat unspecified */
1667 0,
1668 0, 0x2, 0x0 /* block length: 512 bytes */
1669 };
1670 u8 pg, spg;
1671 unsigned int ebd, page_control, six_byte, output_len, alloc_len, minlen;
1672
1673 VPRINTK("ENTER\n");
1674
1675 six_byte = (scsicmd[0] == MODE_SENSE);
1676 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
1677 /*
1678 * LLBA bit in msense(10) ignored (compliant)
1679 */
1680
1681 page_control = scsicmd[2] >> 6;
1682 switch (page_control) {
1683 case 0: /* current */
1684 break; /* supported */
1685 case 3: /* saved */
1686 goto saving_not_supp;
1687 case 1: /* changeable */
1688 case 2: /* defaults */
1689 default:
1690 goto invalid_fld;
1691 }
1692
1693 if (six_byte) {
1694 output_len = 4 + (ebd ? 8 : 0);
1695 alloc_len = scsicmd[4];
1696 } else {
1697 output_len = 8 + (ebd ? 8 : 0);
1698 alloc_len = (scsicmd[7] << 8) + scsicmd[8];
1699 }
1700 minlen = (alloc_len < buflen) ? alloc_len : buflen;
1701
1702 p = rbuf + output_len;
1703 last = rbuf + minlen - 1;
1704
1705 pg = scsicmd[2] & 0x3f;
1706 spg = scsicmd[3];
1707 /*
1708 * No mode subpages supported (yet) but asking for _all_
1709 * subpages may be valid
1710 */
1711 if (spg && (spg != ALL_SUB_MPAGES))
1712 goto invalid_fld;
1713
1714 switch(pg) {
1715 case RW_RECOVERY_MPAGE:
1716 output_len += ata_msense_rw_recovery(&p, last);
1717 break;
1718
1719 case CACHE_MPAGE:
1720 output_len += ata_msense_caching(args->id, &p, last);
1721 break;
1722
1723 case CONTROL_MPAGE: {
1724 output_len += ata_msense_ctl_mode(&p, last);
1725 break;
1726 }
1727
1728 case ALL_MPAGES:
1729 output_len += ata_msense_rw_recovery(&p, last);
1730 output_len += ata_msense_caching(args->id, &p, last);
1731 output_len += ata_msense_ctl_mode(&p, last);
1732 break;
1733
1734 default: /* invalid page code */
1735 goto invalid_fld;
1736 }
1737
1738 if (minlen < 1)
1739 return 0;
1740 if (six_byte) {
1741 output_len--;
1742 rbuf[0] = output_len;
1743 if (ebd) {
1744 if (minlen > 3)
1745 rbuf[3] = sizeof(sat_blk_desc);
1746 if (minlen > 11)
1747 memcpy(rbuf + 4, sat_blk_desc,
1748 sizeof(sat_blk_desc));
1749 }
1750 } else {
1751 output_len -= 2;
1752 rbuf[0] = output_len >> 8;
1753 if (minlen > 1)
1754 rbuf[1] = output_len;
1755 if (ebd) {
1756 if (minlen > 7)
1757 rbuf[7] = sizeof(sat_blk_desc);
1758 if (minlen > 15)
1759 memcpy(rbuf + 8, sat_blk_desc,
1760 sizeof(sat_blk_desc));
1761 }
1762 }
1763 return 0;
1764
1765 invalid_fld:
1766 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
1767 /* "Invalid field in cbd" */
1768 return 1;
1769
1770 saving_not_supp:
1771 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
1772 /* "Saving parameters not supported" */
1773 return 1;
1774 }
1775
1776 /**
1777 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
1778 * @args: device IDENTIFY data / SCSI command of interest.
1779 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1780 * @buflen: Response buffer length.
1781 *
1782 * Simulate READ CAPACITY commands.
1783 *
1784 * LOCKING:
1785 * spin_lock_irqsave(host_set lock)
1786 */
1787
ata_scsiop_read_cap(struct ata_scsi_args * args,u8 * rbuf,unsigned int buflen)1788 unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
1789 unsigned int buflen)
1790 {
1791 u64 n_sectors;
1792 u32 tmp;
1793
1794 VPRINTK("ENTER\n");
1795
1796 if (ata_id_has_lba(args->id)) {
1797 if (ata_id_has_lba48(args->id))
1798 n_sectors = ata_id_u64(args->id, 100);
1799 else
1800 n_sectors = ata_id_u32(args->id, 60);
1801 } else {
1802 /* CHS default translation */
1803 n_sectors = args->id[1] * args->id[3] * args->id[6];
1804
1805 if (ata_id_current_chs_valid(args->id))
1806 /* CHS current translation */
1807 n_sectors = ata_id_u32(args->id, 57);
1808 }
1809
1810 n_sectors--; /* ATA TotalUserSectors - 1 */
1811
1812 if (args->cmd->cmnd[0] == READ_CAPACITY) {
1813 if( n_sectors >= 0xffffffffULL )
1814 tmp = 0xffffffff ; /* Return max count on overflow */
1815 else
1816 tmp = n_sectors ;
1817
1818 /* sector count, 32-bit */
1819 rbuf[0] = tmp >> (8 * 3);
1820 rbuf[1] = tmp >> (8 * 2);
1821 rbuf[2] = tmp >> (8 * 1);
1822 rbuf[3] = tmp;
1823
1824 /* sector size */
1825 tmp = ATA_SECT_SIZE;
1826 rbuf[6] = tmp >> 8;
1827 rbuf[7] = tmp;
1828
1829 } else {
1830 /* sector count, 64-bit */
1831 tmp = n_sectors >> (8 * 4);
1832 rbuf[2] = tmp >> (8 * 3);
1833 rbuf[3] = tmp >> (8 * 2);
1834 rbuf[4] = tmp >> (8 * 1);
1835 rbuf[5] = tmp;
1836 tmp = n_sectors;
1837 rbuf[6] = tmp >> (8 * 3);
1838 rbuf[7] = tmp >> (8 * 2);
1839 rbuf[8] = tmp >> (8 * 1);
1840 rbuf[9] = tmp;
1841
1842 /* sector size */
1843 tmp = ATA_SECT_SIZE;
1844 rbuf[12] = tmp >> 8;
1845 rbuf[13] = tmp;
1846 }
1847
1848 return 0;
1849 }
1850
1851 /**
1852 * ata_scsiop_report_luns - Simulate REPORT LUNS command
1853 * @args: device IDENTIFY data / SCSI command of interest.
1854 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1855 * @buflen: Response buffer length.
1856 *
1857 * Simulate REPORT LUNS command.
1858 *
1859 * LOCKING:
1860 * spin_lock_irqsave(host_set lock)
1861 */
1862
ata_scsiop_report_luns(struct ata_scsi_args * args,u8 * rbuf,unsigned int buflen)1863 unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1864 unsigned int buflen)
1865 {
1866 VPRINTK("ENTER\n");
1867 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
1868
1869 return 0;
1870 }
1871
1872 /**
1873 * ata_scsi_set_sense - Set SCSI sense data and status
1874 * @cmd: SCSI request to be handled
1875 * @sk: SCSI-defined sense key
1876 * @asc: SCSI-defined additional sense code
1877 * @ascq: SCSI-defined additional sense code qualifier
1878 *
1879 * Helper function that builds a valid fixed format, current
1880 * response code and the given sense key (sk), additional sense
1881 * code (asc) and additional sense code qualifier (ascq) with
1882 * a SCSI command status of %SAM_STAT_CHECK_CONDITION and
1883 * DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
1884 *
1885 * LOCKING:
1886 * Not required
1887 */
1888
ata_scsi_set_sense(struct scsi_cmnd * cmd,u8 sk,u8 asc,u8 ascq)1889 void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
1890 {
1891 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
1892
1893 cmd->sense_buffer[0] = 0x70; /* fixed format, current */
1894 cmd->sense_buffer[2] = sk;
1895 cmd->sense_buffer[7] = 18 - 8; /* additional sense length */
1896 cmd->sense_buffer[12] = asc;
1897 cmd->sense_buffer[13] = ascq;
1898 }
1899
1900 /**
1901 * ata_scsi_badcmd - End a SCSI request with an error
1902 * @cmd: SCSI request to be handled
1903 * @done: SCSI command completion function
1904 * @asc: SCSI-defined additional sense code
1905 * @ascq: SCSI-defined additional sense code qualifier
1906 *
1907 * Helper function that completes a SCSI command with
1908 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
1909 * and the specified additional sense codes.
1910 *
1911 * LOCKING:
1912 * spin_lock_irqsave(host_set lock)
1913 */
1914
ata_scsi_badcmd(struct scsi_cmnd * cmd,void (* done)(struct scsi_cmnd *),u8 asc,u8 ascq)1915 void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
1916 {
1917 DPRINTK("ENTER\n");
1918 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
1919
1920 done(cmd);
1921 }
1922
atapi_sense_complete(struct ata_queued_cmd * qc,unsigned int err_mask)1923 static int atapi_sense_complete(struct ata_queued_cmd *qc,unsigned int err_mask)
1924 {
1925 if (err_mask && ((err_mask & AC_ERR_DEV) == 0))
1926 /* FIXME: not quite right; we don't want the
1927 * translation of taskfile registers into
1928 * a sense descriptors, since that's only
1929 * correct for ATA, not ATAPI
1930 */
1931 ata_gen_ata_desc_sense(qc);
1932
1933 qc->scsidone(qc->scsicmd);
1934 return 0;
1935 }
1936
1937 /* is it pointless to prefer PIO for "safety reasons"? */
ata_pio_use_silly(struct ata_port * ap)1938 static inline int ata_pio_use_silly(struct ata_port *ap)
1939 {
1940 return (ap->flags & ATA_FLAG_PIO_DMA);
1941 }
1942
atapi_request_sense(struct ata_queued_cmd * qc)1943 static void atapi_request_sense(struct ata_queued_cmd *qc)
1944 {
1945 struct ata_port *ap = qc->ap;
1946 struct scsi_cmnd *cmd = qc->scsicmd;
1947
1948 DPRINTK("ATAPI request sense\n");
1949
1950 /* FIXME: is this needed? */
1951 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
1952
1953 ap->ops->tf_read(ap, &qc->tf);
1954
1955 /* fill these in, for the case where they are -not- overwritten */
1956 cmd->sense_buffer[0] = 0x70;
1957 cmd->sense_buffer[2] = qc->tf.feature >> 4;
1958
1959 ata_qc_reinit(qc);
1960
1961 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
1962 qc->dma_dir = DMA_FROM_DEVICE;
1963
1964 memset(&qc->cdb, 0, ap->cdb_len);
1965 qc->cdb[0] = REQUEST_SENSE;
1966 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
1967
1968 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1969 qc->tf.command = ATA_CMD_PACKET;
1970
1971 if (ata_pio_use_silly(ap)) {
1972 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
1973 qc->tf.feature |= ATAPI_PKT_DMA;
1974 } else {
1975 qc->tf.protocol = ATA_PROT_ATAPI;
1976 qc->tf.lbam = (8 * 1024) & 0xff;
1977 qc->tf.lbah = (8 * 1024) >> 8;
1978 }
1979 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
1980
1981 qc->complete_fn = atapi_sense_complete;
1982
1983 if (ata_qc_issue(qc))
1984 ata_qc_complete(qc, AC_ERR_OTHER);
1985
1986 DPRINTK("EXIT\n");
1987 }
1988
atapi_qc_complete(struct ata_queued_cmd * qc,unsigned int err_mask)1989 static int atapi_qc_complete(struct ata_queued_cmd *qc, unsigned int err_mask)
1990 {
1991 struct scsi_cmnd *cmd = qc->scsicmd;
1992
1993 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
1994
1995 if (unlikely(err_mask & AC_ERR_DEV)) {
1996 cmd->result = SAM_STAT_CHECK_CONDITION;
1997 atapi_request_sense(qc);
1998 return 1;
1999 }
2000
2001 else if (unlikely(err_mask))
2002 /* FIXME: not quite right; we don't want the
2003 * translation of taskfile registers into
2004 * a sense descriptors, since that's only
2005 * correct for ATA, not ATAPI
2006 */
2007 ata_gen_ata_desc_sense(qc);
2008
2009 else {
2010 u8 *scsicmd = cmd->cmnd;
2011
2012 if (scsicmd[0] == INQUIRY) {
2013 u8 *buf = NULL;
2014 unsigned int buflen;
2015
2016 buflen = ata_scsi_rbuf_get(cmd, &buf);
2017
2018 /* ATAPI devices typically report zero for their SCSI version,
2019 * and sometimes deviate from the spec WRT response data
2020 * format. If SCSI version is reported as zero like normal,
2021 * then we make the following fixups: 1) Fake MMC-5 version,
2022 * to indicate to the Linux scsi midlayer this is a modern
2023 * device. 2) Ensure response data format / ATAPI information
2024 * are always correct.
2025 */
2026 /* FIXME: do we ever override EVPD pages and the like, with
2027 * this code?
2028 */
2029 if (buf[2] == 0) {
2030 buf[2] = 0x5;
2031 buf[3] = 0x32;
2032 }
2033
2034 ata_scsi_rbuf_put(cmd, buf);
2035 }
2036
2037 cmd->result = SAM_STAT_GOOD;
2038 }
2039
2040 qc->scsidone(cmd);
2041 return 0;
2042 }
2043 /**
2044 * atapi_xlat - Initialize PACKET taskfile
2045 * @qc: command structure to be initialized
2046 * @scsicmd: SCSI CDB associated with this PACKET command
2047 *
2048 * LOCKING:
2049 * spin_lock_irqsave(host_set lock)
2050 *
2051 * RETURNS:
2052 * Zero on success, non-zero on failure.
2053 */
2054
atapi_xlat(struct ata_queued_cmd * qc,const u8 * scsicmd)2055 static unsigned int atapi_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
2056 {
2057 struct scsi_cmnd *cmd = qc->scsicmd;
2058 struct ata_device *dev = qc->dev;
2059 int using_pio = (dev->flags & ATA_DFLAG_PIO);
2060 int nodata = (cmd->sc_data_direction == DMA_NONE);
2061
2062 if (!using_pio)
2063 /* Check whether ATAPI DMA is safe */
2064 if (ata_check_atapi_dma(qc))
2065 using_pio = 1;
2066
2067 memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
2068
2069 qc->complete_fn = atapi_qc_complete;
2070
2071 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2072 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
2073 qc->tf.flags |= ATA_TFLAG_WRITE;
2074 DPRINTK("direction: write\n");
2075 }
2076
2077 qc->tf.command = ATA_CMD_PACKET;
2078
2079 /* no data, or PIO data xfer */
2080 if (using_pio || nodata) {
2081 if (nodata)
2082 qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
2083 else
2084 qc->tf.protocol = ATA_PROT_ATAPI;
2085 qc->tf.lbam = (8 * 1024) & 0xff;
2086 qc->tf.lbah = (8 * 1024) >> 8;
2087 }
2088
2089 /* DMA data xfer */
2090 else {
2091 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
2092 qc->tf.feature |= ATAPI_PKT_DMA;
2093
2094 #ifdef ATAPI_ENABLE_DMADIR
2095 /* some SATA bridges need us to indicate data xfer direction */
2096 if (cmd->sc_data_direction != DMA_TO_DEVICE)
2097 qc->tf.feature |= ATAPI_DMADIR;
2098 #endif
2099 }
2100
2101 qc->nbytes = cmd->bufflen;
2102
2103 return 0;
2104 }
2105
2106 /**
2107 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2108 * @ap: ATA port to which the device is attached
2109 * @scsidev: SCSI device from which we derive the ATA device
2110 *
2111 * Given various information provided in struct scsi_cmnd,
2112 * map that onto an ATA bus, and using that mapping
2113 * determine which ata_device is associated with the
2114 * SCSI command to be sent.
2115 *
2116 * LOCKING:
2117 * spin_lock_irqsave(host_set lock)
2118 *
2119 * RETURNS:
2120 * Associated ATA device, or %NULL if not found.
2121 */
2122
2123 static struct ata_device *
ata_scsi_find_dev(struct ata_port * ap,const struct scsi_device * scsidev)2124 ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
2125 {
2126 struct ata_device *dev;
2127
2128 /* skip commands not addressed to targets we simulate */
2129 if (likely(scsidev->id < ATA_MAX_DEVICES))
2130 dev = &ap->device[scsidev->id];
2131 else
2132 return NULL;
2133
2134 if (unlikely((scsidev->channel != 0) ||
2135 (scsidev->lun != 0)))
2136 return NULL;
2137
2138 if (unlikely(!ata_dev_present(dev)))
2139 return NULL;
2140
2141 if (!atapi_enabled) {
2142 if (unlikely(dev->class == ATA_DEV_ATAPI))
2143 return NULL;
2144 }
2145
2146 return dev;
2147 }
2148
2149 /*
2150 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2151 * @byte1: Byte 1 from pass-thru CDB.
2152 *
2153 * RETURNS:
2154 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2155 */
2156 static u8
ata_scsi_map_proto(u8 byte1)2157 ata_scsi_map_proto(u8 byte1)
2158 {
2159 switch((byte1 & 0x1e) >> 1) {
2160 case 3: /* Non-data */
2161 return ATA_PROT_NODATA;
2162
2163 case 6: /* DMA */
2164 return ATA_PROT_DMA;
2165
2166 case 4: /* PIO Data-in */
2167 case 5: /* PIO Data-out */
2168 if (byte1 & 0xe0) {
2169 return ATA_PROT_PIO_MULT;
2170 }
2171 return ATA_PROT_PIO;
2172
2173 case 10: /* Device Reset */
2174 case 0: /* Hard Reset */
2175 case 1: /* SRST */
2176 case 2: /* Bus Idle */
2177 case 7: /* Packet */
2178 case 8: /* DMA Queued */
2179 case 9: /* Device Diagnostic */
2180 case 11: /* UDMA Data-in */
2181 case 12: /* UDMA Data-Out */
2182 case 13: /* FPDMA */
2183 default: /* Reserved */
2184 break;
2185 }
2186
2187 return ATA_PROT_UNKNOWN;
2188 }
2189
2190 /**
2191 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
2192 * @qc: command structure to be initialized
2193 * @scsicmd: SCSI command to convert
2194 *
2195 * Handles either 12 or 16-byte versions of the CDB.
2196 *
2197 * RETURNS:
2198 * Zero on success, non-zero on failure.
2199 */
2200 static unsigned int
ata_scsi_pass_thru(struct ata_queued_cmd * qc,const u8 * scsicmd)2201 ata_scsi_pass_thru(struct ata_queued_cmd *qc, const u8 *scsicmd)
2202 {
2203 struct ata_taskfile *tf = &(qc->tf);
2204 struct scsi_cmnd *cmd = qc->scsicmd;
2205
2206 if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
2207 return 1;
2208
2209 /*
2210 * 12 and 16 byte CDBs use different offsets to
2211 * provide the various register values.
2212 */
2213 if (scsicmd[0] == ATA_16) {
2214 /*
2215 * 16-byte CDB - may contain extended commands.
2216 *
2217 * If that is the case, copy the upper byte register values.
2218 */
2219 if (scsicmd[1] & 0x01) {
2220 tf->hob_feature = scsicmd[3];
2221 tf->hob_nsect = scsicmd[5];
2222 tf->hob_lbal = scsicmd[7];
2223 tf->hob_lbam = scsicmd[9];
2224 tf->hob_lbah = scsicmd[11];
2225 tf->flags |= ATA_TFLAG_LBA48;
2226 } else
2227 tf->flags &= ~ATA_TFLAG_LBA48;
2228
2229 /*
2230 * Always copy low byte, device and command registers.
2231 */
2232 tf->feature = scsicmd[4];
2233 tf->nsect = scsicmd[6];
2234 tf->lbal = scsicmd[8];
2235 tf->lbam = scsicmd[10];
2236 tf->lbah = scsicmd[12];
2237 tf->device = scsicmd[13];
2238 tf->command = scsicmd[14];
2239 } else {
2240 /*
2241 * 12-byte CDB - incapable of extended commands.
2242 */
2243 tf->flags &= ~ATA_TFLAG_LBA48;
2244
2245 tf->feature = scsicmd[3];
2246 tf->nsect = scsicmd[4];
2247 tf->lbal = scsicmd[5];
2248 tf->lbam = scsicmd[6];
2249 tf->lbah = scsicmd[7];
2250 tf->device = scsicmd[8];
2251 tf->command = scsicmd[9];
2252 }
2253 /*
2254 * If slave is possible, enforce correct master/slave bit
2255 */
2256 if (qc->ap->flags & ATA_FLAG_SLAVE_POSS)
2257 tf->device = qc->dev->devno ?
2258 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
2259
2260 /*
2261 * Filter SET_FEATURES - XFER MODE command -- otherwise,
2262 * SET_FEATURES - XFER MODE must be preceded/succeeded
2263 * by an update to hardware-specific registers for each
2264 * controller (i.e. the reason for ->set_piomode(),
2265 * ->set_dmamode(), and ->post_set_mode() hooks).
2266 */
2267 if ((tf->command == ATA_CMD_SET_FEATURES)
2268 && (tf->feature == SETFEATURES_XFER))
2269 return 1;
2270
2271 /*
2272 * Set flags so that all registers will be written,
2273 * and pass on write indication (used for PIO/DMA
2274 * setup.)
2275 */
2276 tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
2277
2278 if (cmd->sc_data_direction == DMA_TO_DEVICE)
2279 tf->flags |= ATA_TFLAG_WRITE;
2280
2281 /*
2282 * Set transfer length.
2283 *
2284 * TODO: find out if we need to do more here to
2285 * cover scatter/gather case.
2286 */
2287 qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
2288
2289 return 0;
2290 }
2291
2292 /**
2293 * ata_get_xlat_func - check if SCSI to ATA translation is possible
2294 * @dev: ATA device
2295 * @cmd: SCSI command opcode to consider
2296 *
2297 * Look up the SCSI command given, and determine whether the
2298 * SCSI command is to be translated or simulated.
2299 *
2300 * RETURNS:
2301 * Pointer to translation function if possible, %NULL if not.
2302 */
2303
ata_get_xlat_func(struct ata_device * dev,u8 cmd)2304 static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
2305 {
2306 switch (cmd) {
2307 case READ_6:
2308 case READ_10:
2309 case READ_16:
2310
2311 case WRITE_6:
2312 case WRITE_10:
2313 case WRITE_16:
2314 return ata_scsi_rw_xlat;
2315
2316 case SYNCHRONIZE_CACHE:
2317 if (ata_try_flush_cache(dev))
2318 return ata_scsi_flush_xlat;
2319 break;
2320
2321 case VERIFY:
2322 case VERIFY_16:
2323 return ata_scsi_verify_xlat;
2324
2325 case ATA_12:
2326 case ATA_16:
2327 return ata_scsi_pass_thru;
2328
2329 case START_STOP:
2330 return ata_scsi_start_stop_xlat;
2331 }
2332
2333 return NULL;
2334 }
2335
2336 /**
2337 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
2338 * @ap: ATA port to which the command was being sent
2339 * @cmd: SCSI command to dump
2340 *
2341 * Prints the contents of a SCSI command via printk().
2342 */
2343
ata_scsi_dump_cdb(struct ata_port * ap,struct scsi_cmnd * cmd)2344 static inline void ata_scsi_dump_cdb(struct ata_port *ap,
2345 struct scsi_cmnd *cmd)
2346 {
2347 #ifdef ATA_DEBUG
2348 struct scsi_device *scsidev = cmd->device;
2349 u8 *scsicmd = cmd->cmnd;
2350
2351 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
2352 ap->id,
2353 scsidev->channel, scsidev->id, scsidev->lun,
2354 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
2355 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
2356 scsicmd[8]);
2357 #endif
2358 }
2359
2360 /**
2361 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
2362 * @cmd: SCSI command to be sent
2363 * @done: Completion function, called when command is complete
2364 *
2365 * In some cases, this function translates SCSI commands into
2366 * ATA taskfiles, and queues the taskfiles to be sent to
2367 * hardware. In other cases, this function simulates a
2368 * SCSI device by evaluating and responding to certain
2369 * SCSI commands. This creates the overall effect of
2370 * ATA and ATAPI devices appearing as SCSI devices.
2371 *
2372 * LOCKING:
2373 * Releases scsi-layer-held lock, and obtains host_set lock.
2374 *
2375 * RETURNS:
2376 * Zero.
2377 */
2378
ata_scsi_queuecmd(struct scsi_cmnd * cmd,void (* done)(struct scsi_cmnd *))2379 int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
2380 {
2381 struct ata_port *ap;
2382 struct ata_device *dev;
2383 struct scsi_device *scsidev = cmd->device;
2384
2385 ap = (struct ata_port *) &scsidev->host->hostdata[0];
2386
2387 spin_unlock(&io_request_lock);
2388 spin_lock(&ap->host_set->lock);
2389
2390 ata_scsi_dump_cdb(ap, cmd);
2391
2392 dev = ata_scsi_find_dev(ap, scsidev);
2393 if (unlikely(!dev)) {
2394 cmd->result = (DID_BAD_TARGET << 16);
2395 done(cmd);
2396 goto out_unlock;
2397 }
2398
2399 if (dev->class == ATA_DEV_ATA) {
2400 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
2401 cmd->cmnd[0]);
2402
2403 if (xlat_func)
2404 ata_scsi_translate(ap, dev, cmd, done, xlat_func);
2405 else
2406 ata_scsi_simulate(dev->id, cmd, done);
2407 } else
2408 ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
2409
2410 out_unlock:
2411 spin_unlock(&ap->host_set->lock);
2412 spin_lock(&io_request_lock);
2413 return 0;
2414 }
2415
2416 /**
2417 * ata_scsi_simulate - simulate SCSI command on ATA device
2418 * @id: current IDENTIFY data for target device.
2419 * @cmd: SCSI command being sent to device.
2420 * @done: SCSI command completion function.
2421 *
2422 * Interprets and directly executes a select list of SCSI commands
2423 * that can be handled internally.
2424 *
2425 * LOCKING:
2426 * spin_lock_irqsave(host_set lock)
2427 */
2428
ata_scsi_simulate(u16 * id,struct scsi_cmnd * cmd,void (* done)(struct scsi_cmnd *))2429 void ata_scsi_simulate(u16 *id,
2430 struct scsi_cmnd *cmd,
2431 void (*done)(struct scsi_cmnd *))
2432 {
2433 struct ata_scsi_args args;
2434 const u8 *scsicmd = cmd->cmnd;
2435
2436 args.id = id;
2437 args.cmd = cmd;
2438 args.done = done;
2439
2440 switch(scsicmd[0]) {
2441 /* no-op's, complete with success */
2442 case SYNCHRONIZE_CACHE:
2443 case REZERO_UNIT:
2444 case SEEK_6:
2445 case SEEK_10:
2446 case TEST_UNIT_READY:
2447 case FORMAT_UNIT: /* FIXME: correct? */
2448 case SEND_DIAGNOSTIC: /* FIXME: correct? */
2449 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
2450 break;
2451
2452 case INQUIRY:
2453 if (scsicmd[1] & 2) /* is CmdDt set? */
2454 ata_scsi_invalid_field(cmd, done);
2455 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
2456 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
2457 else if (scsicmd[2] == 0x00)
2458 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
2459 else if (scsicmd[2] == 0x80)
2460 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
2461 else if (scsicmd[2] == 0x83)
2462 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
2463 else
2464 ata_scsi_invalid_field(cmd, done);
2465 break;
2466
2467 case MODE_SENSE:
2468 case MODE_SENSE_10:
2469 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
2470 break;
2471
2472 case MODE_SELECT: /* unconditionally return */
2473 case MODE_SELECT_10: /* bad-field-in-cdb */
2474 ata_scsi_invalid_field(cmd, done);
2475 break;
2476
2477 case READ_CAPACITY:
2478 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2479 break;
2480
2481 case SERVICE_ACTION_IN:
2482 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
2483 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2484 else
2485 ata_scsi_invalid_field(cmd, done);
2486 break;
2487
2488 case REPORT_LUNS:
2489 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
2490 break;
2491
2492 /* mandatory commands we haven't implemented yet */
2493 case REQUEST_SENSE:
2494
2495 /* all other commands */
2496 default:
2497 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
2498 /* "Invalid command operation code" */
2499 done(cmd);
2500 break;
2501 }
2502 }
2503
2504