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