blob: 3a4f40b251fb20575d05a97aa88e750a4866d855 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <scsi/scsi_host.h>
Mike Christie85837eb2005-11-11 16:38:53 -060041#include <scsi/scsi_eh.h>
Jeff Garzik005a5a02005-10-30 23:31:48 -050042#include <scsi/scsi_device.h>
Jeff Garzik193515d2005-11-07 00:59:37 -050043#include <scsi/scsi_request.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <linux/libata.h>
Jeff Garzikb0955182005-05-12 15:45:22 -040045#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <asm/uaccess.h>
47
48#include "libata.h"
49
Jeff Garzikb0955182005-05-12 15:45:22 -040050#define SECTOR_SIZE 512
51
Jeff Garzik057ace52005-10-22 14:27:05 -040052typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, const u8 *scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static struct ata_device *
Jeff Garzik057ace52005-10-22 14:27:05 -040054ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040056#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
66static 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
77static 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
86static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Douglas Gilbertae006512005-10-09 09:09:35 -040096static 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104/**
105 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
106 * @sdev: SCSI device for which BIOS geometry is to be determined
107 * @bdev: block device associated with @sdev
108 * @capacity: capacity of SCSI device
109 * @geom: location to which geometry will be output
110 *
111 * Generic bios head/sector/cylinder calculator
112 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
113 * mapping. Some situations may arise where the disk is not
114 * bootable if this is not used.
115 *
116 * LOCKING:
117 * Defined by the SCSI layer. We don't really care.
118 *
119 * RETURNS:
120 * Zero.
121 */
122int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
123 sector_t capacity, int geom[])
124{
125 geom[0] = 255;
126 geom[1] = 63;
127 sector_div(capacity, 255*63);
128 geom[2] = capacity;
129
130 return 0;
131}
132
Jeff Garzikb0955182005-05-12 15:45:22 -0400133/**
134 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800135 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400136 * @arg: User provided data for issuing command
137 *
138 * LOCKING:
139 * Defined by the SCSI layer. We don't really care.
140 *
141 * RETURNS:
142 * Zero on success, negative errno on error.
143 */
144
145int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
146{
147 int rc = 0;
148 u8 scsi_cmd[MAX_COMMAND_SIZE];
149 u8 args[4], *argbuf = NULL;
150 int argsize = 0;
Mike Christie85837eb2005-11-11 16:38:53 -0600151 struct scsi_sense_hdr sshdr;
152 enum dma_data_direction data_dir;
Jeff Garzikb0955182005-05-12 15:45:22 -0400153
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500154 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400155 return -EINVAL;
156
157 if (copy_from_user(args, arg, sizeof(args)))
158 return -EFAULT;
159
Jeff Garzikb0955182005-05-12 15:45:22 -0400160 memset(scsi_cmd, 0, sizeof(scsi_cmd));
161
162 if (args[3]) {
163 argsize = SECTOR_SIZE * args[3];
164 argbuf = kmalloc(argsize, GFP_KERNEL);
Jeff Raubitschek54dac832005-10-04 10:21:19 -0400165 if (argbuf == NULL) {
166 rc = -ENOMEM;
167 goto error;
168 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400169
170 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
171 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
172 block count in sector count field */
Mike Christie85837eb2005-11-11 16:38:53 -0600173 data_dir = DMA_FROM_DEVICE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400174 } else {
175 scsi_cmd[1] = (3 << 1); /* Non-data */
176 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
Mike Christie85837eb2005-11-11 16:38:53 -0600177 data_dir = DMA_NONE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400178 }
179
180 scsi_cmd[0] = ATA_16;
181
182 scsi_cmd[4] = args[2];
183 if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
184 scsi_cmd[6] = args[3];
185 scsi_cmd[8] = args[1];
186 scsi_cmd[10] = 0x4f;
187 scsi_cmd[12] = 0xc2;
188 } else {
189 scsi_cmd[6] = args[1];
190 }
191 scsi_cmd[14] = args[0];
192
193 /* Good values for timeout and retries? Values below
194 from scsi_ioctl_send_command() for default case... */
Mike Christie85837eb2005-11-11 16:38:53 -0600195 if (scsi_execute_req(scsidev, scsi_cmd, data_dir, argbuf, argsize,
196 &sshdr, (10*HZ), 5)) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400197 rc = -EIO;
198 goto error;
199 }
200
201 /* Need code to retrieve data from check condition? */
202
203 if ((argbuf)
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500204 && copy_to_user(arg + sizeof(args), argbuf, argsize))
Jeff Garzikb0955182005-05-12 15:45:22 -0400205 rc = -EFAULT;
206error:
Jeff Garzikb0955182005-05-12 15:45:22 -0400207 if (argbuf)
208 kfree(argbuf);
209
210 return rc;
211}
212
213/**
214 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800215 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400216 * @arg: User provided data for issuing command
217 *
218 * LOCKING:
219 * Defined by the SCSI layer. We don't really care.
220 *
221 * RETURNS:
222 * Zero on success, negative errno on error.
223 */
224int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
225{
226 int rc = 0;
227 u8 scsi_cmd[MAX_COMMAND_SIZE];
228 u8 args[7];
Mike Christie85837eb2005-11-11 16:38:53 -0600229 struct scsi_sense_hdr sshdr;
Jeff Garzikb0955182005-05-12 15:45:22 -0400230
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500231 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400232 return -EINVAL;
233
234 if (copy_from_user(args, arg, sizeof(args)))
235 return -EFAULT;
236
237 memset(scsi_cmd, 0, sizeof(scsi_cmd));
238 scsi_cmd[0] = ATA_16;
239 scsi_cmd[1] = (3 << 1); /* Non-data */
240 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
241 scsi_cmd[4] = args[1];
242 scsi_cmd[6] = args[2];
243 scsi_cmd[8] = args[3];
244 scsi_cmd[10] = args[4];
245 scsi_cmd[12] = args[5];
246 scsi_cmd[14] = args[0];
247
Jeff Garzikb0955182005-05-12 15:45:22 -0400248 /* Good values for timeout and retries? Values below
Mike Christie85837eb2005-11-11 16:38:53 -0600249 from scsi_ioctl_send_command() for default case... */
250 if (scsi_execute_req(scsidev, scsi_cmd, DMA_NONE, NULL, 0, &sshdr,
251 (10*HZ), 5))
Jeff Garzikb0955182005-05-12 15:45:22 -0400252 rc = -EIO;
Jeff Garzikb0955182005-05-12 15:45:22 -0400253
254 /* Need code to retrieve data from check condition? */
Jeff Garzikb0955182005-05-12 15:45:22 -0400255 return rc;
256}
257
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
259{
260 struct ata_port *ap;
261 struct ata_device *dev;
262 int val = -EINVAL, rc = -EINVAL;
263
264 ap = (struct ata_port *) &scsidev->host->hostdata[0];
265 if (!ap)
266 goto out;
267
268 dev = ata_scsi_find_dev(ap, scsidev);
269 if (!dev) {
270 rc = -ENODEV;
271 goto out;
272 }
273
274 switch (cmd) {
275 case ATA_IOC_GET_IO32:
276 val = 0;
277 if (copy_to_user(arg, &val, 1))
278 return -EFAULT;
279 return 0;
280
281 case ATA_IOC_SET_IO32:
282 val = (unsigned long) arg;
283 if (val != 0)
284 return -EINVAL;
285 return 0;
286
Jeff Garzikb0955182005-05-12 15:45:22 -0400287 case HDIO_DRIVE_CMD:
288 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
289 return -EACCES;
290 return ata_cmd_ioctl(scsidev, arg);
291
292 case HDIO_DRIVE_TASK:
293 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
294 return -EACCES;
295 return ata_task_ioctl(scsidev, arg);
296
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 default:
298 rc = -ENOTTY;
299 break;
300 }
301
302out:
303 return rc;
304}
305
306/**
307 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
308 * @ap: ATA port to which the new command is attached
309 * @dev: ATA device to which the new command is attached
310 * @cmd: SCSI command that originated this ATA command
311 * @done: SCSI command completion function
312 *
313 * Obtain a reference to an unused ata_queued_cmd structure,
314 * which is the basic libata structure representing a single
315 * ATA command sent to the hardware.
316 *
317 * If a command was available, fill in the SCSI-specific
318 * portions of the structure with information on the
319 * current command.
320 *
321 * LOCKING:
322 * spin_lock_irqsave(host_set lock)
323 *
324 * RETURNS:
325 * Command allocated, or %NULL if none available.
326 */
327struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
328 struct ata_device *dev,
329 struct scsi_cmnd *cmd,
330 void (*done)(struct scsi_cmnd *))
331{
332 struct ata_queued_cmd *qc;
333
334 qc = ata_qc_new_init(ap, dev);
335 if (qc) {
336 qc->scsicmd = cmd;
337 qc->scsidone = done;
338
339 if (cmd->use_sg) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -0400340 qc->__sg = (struct scatterlist *) cmd->request_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 qc->n_elem = cmd->use_sg;
342 } else {
Jeff Garzikcedc9a42005-10-05 07:13:30 -0400343 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 qc->n_elem = 1;
345 }
346 } else {
347 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
348 done(cmd);
349 }
350
351 return qc;
352}
353
354/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400355 * ata_dump_status - user friendly display of error info
356 * @id: id of the port in question
357 * @tf: ptr to filled out taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 *
Jeff Garzikb0955182005-05-12 15:45:22 -0400359 * Decode and dump the ATA error/status registers for the user so
360 * that they have some idea what really happened at the non
361 * make-believe layer.
362 *
363 * LOCKING:
364 * inherited from caller
365 */
366void ata_dump_status(unsigned id, struct ata_taskfile *tf)
367{
368 u8 stat = tf->command, err = tf->feature;
369
370 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
371 if (stat & ATA_BUSY) {
372 printk("Busy }\n"); /* Data is not valid in this case */
373 } else {
374 if (stat & 0x40) printk("DriveReady ");
375 if (stat & 0x20) printk("DeviceFault ");
376 if (stat & 0x10) printk("SeekComplete ");
377 if (stat & 0x08) printk("DataRequest ");
378 if (stat & 0x04) printk("CorrectedError ");
379 if (stat & 0x02) printk("Index ");
380 if (stat & 0x01) printk("Error ");
381 printk("}\n");
382
383 if (err) {
384 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
385 if (err & 0x04) printk("DriveStatusError ");
386 if (err & 0x80) {
387 if (err & 0x04) printk("BadCRC ");
388 else printk("Sector ");
389 }
390 if (err & 0x40) printk("UncorrectableError ");
391 if (err & 0x10) printk("SectorIdNotFound ");
392 if (err & 0x02) printk("TrackZeroNotFound ");
393 if (err & 0x01) printk("AddrMarkNotFound ");
394 printk("}\n");
395 }
396 }
397}
398
Jens Axboe9b847542006-01-06 09:28:07 +0100399int ata_scsi_device_resume(struct scsi_device *sdev)
400{
401 struct ata_port *ap = (struct ata_port *) &sdev->host->hostdata[0];
402 struct ata_device *dev = &ap->device[sdev->id];
403
404 return ata_device_resume(ap, dev);
405}
406
407int ata_scsi_device_suspend(struct scsi_device *sdev)
408{
409 struct ata_port *ap = (struct ata_port *) &sdev->host->hostdata[0];
410 struct ata_device *dev = &ap->device[sdev->id];
411
412 return ata_device_suspend(ap, dev);
413}
414
Jeff Garzikb0955182005-05-12 15:45:22 -0400415/**
416 * ata_to_sense_error - convert ATA error to SCSI error
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800417 * @id: ATA device number
Jeff Garzikb0955182005-05-12 15:45:22 -0400418 * @drv_stat: value contained in ATA status register
419 * @drv_err: value contained in ATA error register
420 * @sk: the sense key we'll fill out
421 * @asc: the additional sense code we'll fill out
422 * @ascq: the additional sense code qualifier we'll fill out
423 *
424 * Converts an ATA error into a SCSI error. Fill out pointers to
425 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
426 * format sense blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 *
428 * LOCKING:
429 * spin_lock_irqsave(host_set lock)
430 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400431void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
432 u8 *ascq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
Jeff Garzikb0955182005-05-12 15:45:22 -0400434 int i;
Jeff Garzikffe75ef2005-10-09 10:40:44 -0400435
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 /* Based on the 3ware driver translation table */
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100437 static const unsigned char sense_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 /* BBD|ECC|ID|MAR */
439 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
440 /* BBD|ECC|ID */
441 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
442 /* ECC|MC|MARK */
443 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
444 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
445 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
446 /* MC|ID|ABRT|TRK0|MARK */
447 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
448 /* MCR|MARK */
449 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
450 /* Bad address mark */
451 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
452 /* TRK0 */
453 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
454 /* Abort & !ICRC */
455 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
456 /* Media change request */
457 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
458 /* SRV */
459 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
460 /* Media change */
461 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
462 /* ECC */
463 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
464 /* BBD - block marked bad */
465 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
466 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
467 };
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100468 static const unsigned char stat_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 /* Must be first because BUSY means no other bits valid */
470 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
471 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
472 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
473 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
474 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
475 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /*
478 * Is this an error we can process/parse
479 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400480 if (drv_stat & ATA_BUSY) {
481 drv_err = 0; /* Ignore the err bits, they're invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
Jeff Garzikb0955182005-05-12 15:45:22 -0400484 if (drv_err) {
485 /* Look for drv_err */
486 for (i = 0; sense_table[i][0] != 0xFF; i++) {
487 /* Look for best matches first */
488 if ((sense_table[i][0] & drv_err) ==
489 sense_table[i][0]) {
490 *sk = sense_table[i][1];
491 *asc = sense_table[i][2];
492 *ascq = sense_table[i][3];
493 goto translate_done;
494 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400496 /* No immediate match */
497 printk(KERN_WARNING "ata%u: no sense translation for "
498 "error 0x%02x\n", id, drv_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 }
500
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /* Fall back to interpreting status bits */
Jeff Garzikb0955182005-05-12 15:45:22 -0400502 for (i = 0; stat_table[i][0] != 0xFF; i++) {
503 if (stat_table[i][0] & drv_stat) {
504 *sk = stat_table[i][1];
505 *asc = stat_table[i][2];
506 *ascq = stat_table[i][3];
507 goto translate_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400510 /* No error? Undecoded? */
511 printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n",
512 id, drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Jeff Garzikb0955182005-05-12 15:45:22 -0400514 /* For our last chance pick, use medium read error because
515 * it's much more common than an ATA drive telling you a write
516 * has failed.
517 */
518 *sk = MEDIUM_ERROR;
519 *asc = 0x11; /* "unrecovered read error" */
520 *ascq = 0x04; /* "auto-reallocation failed" */
521
522 translate_done:
523 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to "
524 "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err,
525 *sk, *asc, *ascq);
526 return;
527}
528
529/*
530 * ata_gen_ata_desc_sense - Generate check condition sense block.
531 * @qc: Command that completed.
532 *
533 * This function is specific to the ATA descriptor format sense
534 * block specified for the ATA pass through commands. Regardless
535 * of whether the command errored or not, return a sense
536 * block. Copy all controller registers into the sense
537 * block. Clear sense key, ASC & ASCQ if there is no error.
538 *
539 * LOCKING:
540 * spin_lock_irqsave(host_set lock)
541 */
542void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
543{
544 struct scsi_cmnd *cmd = qc->scsicmd;
545 struct ata_taskfile *tf = &qc->tf;
546 unsigned char *sb = cmd->sense_buffer;
547 unsigned char *desc = sb + 8;
548
549 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
550
Jeff Garzik0e5dec42005-10-06 09:40:20 -0400551 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -0400552
553 /*
554 * Read the controller registers.
555 */
556 assert(NULL != qc->ap->ops->tf_read);
557 qc->ap->ops->tf_read(qc->ap, tf);
558
559 /*
560 * Use ata_to_sense_error() to map status register bits
561 * onto sense key, asc & ascq.
562 */
Jeff Garzika7dac442005-10-30 04:44:42 -0500563 if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400564 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
565 &sb[1], &sb[2], &sb[3]);
566 sb[1] &= 0x0f;
567 }
568
569 /*
570 * Sense data is current and format is descriptor.
571 */
572 sb[0] = 0x72;
573
574 desc[0] = 0x09;
575
576 /*
577 * Set length of additional sense data.
578 * Since we only populate descriptor 0, the total
579 * length is the same (fixed) length as descriptor 0.
580 */
581 desc[1] = sb[7] = 14;
582
583 /*
584 * Copy registers into sense buffer.
585 */
586 desc[2] = 0x00;
587 desc[3] = tf->feature; /* == error reg */
588 desc[5] = tf->nsect;
589 desc[7] = tf->lbal;
590 desc[9] = tf->lbam;
591 desc[11] = tf->lbah;
592 desc[12] = tf->device;
593 desc[13] = tf->command; /* == status reg */
594
595 /*
596 * Fill in Extend bit, and the high order bytes
597 * if applicable.
598 */
599 if (tf->flags & ATA_TFLAG_LBA48) {
600 desc[2] |= 0x01;
601 desc[4] = tf->hob_nsect;
602 desc[6] = tf->hob_lbal;
603 desc[8] = tf->hob_lbam;
604 desc[10] = tf->hob_lbah;
605 }
606}
607
608/**
609 * ata_gen_fixed_sense - generate a SCSI fixed sense block
610 * @qc: Command that we are erroring out
611 *
612 * Leverage ata_to_sense_error() to give us the codes. Fit our
613 * LBA in here if there's room.
614 *
615 * LOCKING:
616 * inherited from caller
617 */
618void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
619{
620 struct scsi_cmnd *cmd = qc->scsicmd;
621 struct ata_taskfile *tf = &qc->tf;
622 unsigned char *sb = cmd->sense_buffer;
623
624 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
625
Jeff Garzik0e5dec42005-10-06 09:40:20 -0400626 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -0400627
628 /*
629 * Read the controller registers.
630 */
631 assert(NULL != qc->ap->ops->tf_read);
632 qc->ap->ops->tf_read(qc->ap, tf);
633
634 /*
635 * Use ata_to_sense_error() to map status register bits
636 * onto sense key, asc & ascq.
637 */
Jeff Garzika7dac442005-10-30 04:44:42 -0500638 if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400639 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
640 &sb[2], &sb[12], &sb[13]);
641 sb[2] &= 0x0f;
642 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 sb[0] = 0x70;
Jeff Garzikb0955182005-05-12 15:45:22 -0400645 sb[7] = 0x0a;
Jeff Garzik0274aa22005-06-22 13:50:56 -0400646
Jeff Garzika7dac442005-10-30 04:44:42 -0500647 if (tf->flags & ATA_TFLAG_LBA48) {
648 /* TODO: find solution for LBA48 descriptors */
649 }
650
651 else if (tf->flags & ATA_TFLAG_LBA) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400652 /* A small (28b) LBA will fit in the 32b info field */
653 sb[0] |= 0x80; /* set valid bit */
654 sb[3] = tf->device & 0x0f;
655 sb[4] = tf->lbah;
656 sb[5] = tf->lbam;
657 sb[6] = tf->lbal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 }
Jeff Garzika7dac442005-10-30 04:44:42 -0500659
660 else {
661 /* TODO: C/H/S */
662 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663}
664
665/**
666 * ata_scsi_slave_config - Set SCSI device attributes
667 * @sdev: SCSI device to examine
668 *
669 * This is called before we actually start reading
670 * and writing to the device, to configure certain
671 * SCSI mid-layer behaviors.
672 *
673 * LOCKING:
674 * Defined by SCSI layer. We don't really care.
675 */
676
677int ata_scsi_slave_config(struct scsi_device *sdev)
678{
679 sdev->use_10_for_rw = 1;
680 sdev->use_10_for_ms = 1;
681
682 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
683
684 if (sdev->id < ATA_MAX_DEVICES) {
685 struct ata_port *ap;
686 struct ata_device *dev;
687
688 ap = (struct ata_port *) &sdev->host->hostdata[0];
689 dev = &ap->device[sdev->id];
690
691 /* TODO: 1024 is an arbitrary number, not the
692 * hardware maximum. This should be increased to
693 * 65534 when Jens Axboe's patch for dynamically
694 * determining max_sectors is merged.
695 */
696 if ((dev->flags & ATA_DFLAG_LBA48) &&
697 ((dev->flags & ATA_DFLAG_LOCK_SECTORS) == 0)) {
John W. Linvillef85bdb92005-05-12 15:49:54 -0400698 /*
699 * do not overwrite sdev->host->max_sectors, since
700 * other drives on this host may not support LBA48
701 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 blk_queue_max_sectors(sdev->request_queue, 2048);
703 }
Jeff Garzikcedc9a42005-10-05 07:13:30 -0400704
705 /*
706 * SATA DMA transfers must be multiples of 4 byte, so
707 * we need to pad ATAPI transfers using an extra sg.
708 * Decrement max hw segments accordingly.
709 */
710 if (dev->class == ATA_DEV_ATAPI) {
711 request_queue_t *q = sdev->request_queue;
712 blk_queue_max_hw_segments(q, q->max_hw_segments - 1);
713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715
716 return 0; /* scsi layer doesn't check return value, sigh */
717}
718
719/**
720 * ata_scsi_error - SCSI layer error handler callback
721 * @host: SCSI host on which error occurred
722 *
723 * Handles SCSI-layer-thrown error events.
724 *
725 * LOCKING:
726 * Inherited from SCSI layer (none, can sleep)
727 *
728 * RETURNS:
729 * Zero.
730 */
731
732int ata_scsi_error(struct Scsi_Host *host)
733{
734 struct ata_port *ap;
735
736 DPRINTK("ENTER\n");
737
Tejun Heodde44582006-02-02 00:56:10 +0900738 spin_lock_irqsave(&ap->host_set->lock, flags);
739 assert(!(ap->flags & ATA_FLAG_IN_EH));
740 ap->flags |= ATA_FLAG_IN_EH;
741 spin_unlock_irqrestore(&ap->host_set->lock, flags);
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 ap = (struct ata_port *) &host->hostdata[0];
744 ap->ops->eng_timeout(ap);
745
Tejun Heoa72ec4c2006-01-23 13:09:37 +0900746 assert(host->host_failed == 0 && list_empty(&host->eh_cmd_q));
747
748 scsi_eh_flush_done_q(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749
Tejun Heodde44582006-02-02 00:56:10 +0900750 spin_lock_irqsave(&ap->host_set->lock, flags);
751 ap->flags &= ~ATA_FLAG_IN_EH;
752 spin_unlock_irqrestore(&ap->host_set->lock, flags);
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 DPRINTK("EXIT\n");
755 return 0;
756}
757
Tejun Heoa72ec4c2006-01-23 13:09:37 +0900758static void ata_eh_scsidone(struct scsi_cmnd *scmd)
759{
760 /* nada */
761}
762
763static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
764{
765 struct ata_port *ap = qc->ap;
766 struct scsi_cmnd *scmd = qc->scsicmd;
767 unsigned long flags;
768
769 spin_lock_irqsave(&ap->host_set->lock, flags);
770 qc->scsidone = ata_eh_scsidone;
771 ata_qc_complete(qc);
772 assert(!ata_tag_valid(qc->tag));
773 spin_unlock_irqrestore(&ap->host_set->lock, flags);
774
775 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
776}
777
778/**
779 * ata_eh_qc_complete - Complete an active ATA command from EH
780 * @qc: Command to complete
781 *
782 * Indicate to the mid and upper layers that an ATA command has
783 * completed. To be used from EH.
784 */
785void ata_eh_qc_complete(struct ata_queued_cmd *qc)
786{
787 struct scsi_cmnd *scmd = qc->scsicmd;
788 scmd->retries = scmd->allowed;
789 __ata_eh_qc_complete(qc);
790}
791
792/**
793 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
794 * @qc: Command to retry
795 *
796 * Indicate to the mid and upper layers that an ATA command
797 * should be retried. To be used from EH.
798 *
799 * SCSI midlayer limits the number of retries to scmd->allowed.
800 * This function might need to adjust scmd->retries for commands
801 * which get retried due to unrelated NCQ failures.
802 */
803void ata_eh_qc_retry(struct ata_queued_cmd *qc)
804{
805 __ata_eh_qc_complete(qc);
806}
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/**
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400809 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
810 * @qc: Storage for translated ATA taskfile
811 * @scsicmd: SCSI command to translate
812 *
813 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
814 * (to start). Perhaps these commands should be preceded by
815 * CHECK POWER MODE to see what power mode the device is already in.
816 * [See SAT revision 5 at www.t10.org]
817 *
818 * LOCKING:
819 * spin_lock_irqsave(host_set lock)
820 *
821 * RETURNS:
822 * Zero on success, non-zero on error.
823 */
824
825static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
Jeff Garzik057ace52005-10-22 14:27:05 -0400826 const u8 *scsicmd)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400827{
828 struct ata_taskfile *tf = &qc->tf;
829
830 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
831 tf->protocol = ATA_PROT_NODATA;
832 if (scsicmd[1] & 0x1) {
833 ; /* ignore IMMED bit, violates sat-r05 */
834 }
835 if (scsicmd[4] & 0x2)
Douglas Gilbertae006512005-10-09 09:09:35 -0400836 goto invalid_fld; /* LOEJ bit set not supported */
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400837 if (((scsicmd[4] >> 4) & 0xf) != 0)
Douglas Gilbertae006512005-10-09 09:09:35 -0400838 goto invalid_fld; /* power conditions not supported */
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400839 if (scsicmd[4] & 0x1) {
840 tf->nsect = 1; /* 1 sector, lba=0 */
Albert Lee9d5b1302005-10-04 08:48:17 -0400841
842 if (qc->dev->flags & ATA_DFLAG_LBA) {
843 qc->tf.flags |= ATA_TFLAG_LBA;
844
845 tf->lbah = 0x0;
846 tf->lbam = 0x0;
847 tf->lbal = 0x0;
848 tf->device |= ATA_LBA;
849 } else {
850 /* CHS */
851 tf->lbal = 0x1; /* sect */
852 tf->lbam = 0x0; /* cyl low */
853 tf->lbah = 0x0; /* cyl high */
854 }
855
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400856 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
857 } else {
858 tf->nsect = 0; /* time period value (0 implies now) */
859 tf->command = ATA_CMD_STANDBY;
860 /* Consider: ATA STANDBY IMMEDIATE command */
861 }
862 /*
863 * Standby and Idle condition timers could be implemented but that
864 * would require libata to implement the Power condition mode page
865 * and allow the user to change it. Changing mode pages requires
866 * MODE SELECT to be implemented.
867 */
868
869 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -0400870
871invalid_fld:
872 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
873 /* "Invalid field in cbd" */
874 return 1;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400875}
876
877
878/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
880 * @qc: Storage for translated ATA taskfile
881 * @scsicmd: SCSI command to translate (ignored)
882 *
883 * Sets up an ATA taskfile to issue FLUSH CACHE or
884 * FLUSH CACHE EXT.
885 *
886 * LOCKING:
887 * spin_lock_irqsave(host_set lock)
888 *
889 * RETURNS:
890 * Zero on success, non-zero on error.
891 */
892
Jeff Garzik057ace52005-10-22 14:27:05 -0400893static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894{
895 struct ata_taskfile *tf = &qc->tf;
896
897 tf->flags |= ATA_TFLAG_DEVICE;
898 tf->protocol = ATA_PROT_NODATA;
899
Albert Lee07506692005-10-12 15:04:18 +0800900 if ((qc->dev->flags & ATA_DFLAG_LBA48) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 (ata_id_has_flush_ext(qc->dev->id)))
902 tf->command = ATA_CMD_FLUSH_EXT;
903 else
904 tf->command = ATA_CMD_FLUSH;
905
906 return 0;
907}
908
909/**
Albert Lee3aef5232005-10-04 08:47:43 -0400910 * scsi_6_lba_len - Get LBA and transfer length
911 * @scsicmd: SCSI command to translate
912 *
913 * Calculate LBA and transfer length for 6-byte commands.
914 *
915 * RETURNS:
916 * @plba: the LBA
917 * @plen: the transfer length
918 */
919
Jeff Garzik057ace52005-10-22 14:27:05 -0400920static void scsi_6_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -0400921{
922 u64 lba = 0;
923 u32 len = 0;
924
925 VPRINTK("six-byte command\n");
926
927 lba |= ((u64)scsicmd[2]) << 8;
928 lba |= ((u64)scsicmd[3]);
929
930 len |= ((u32)scsicmd[4]);
931
932 *plba = lba;
933 *plen = len;
934}
935
936/**
937 * scsi_10_lba_len - Get LBA and transfer length
938 * @scsicmd: SCSI command to translate
939 *
940 * Calculate LBA and transfer length for 10-byte commands.
941 *
942 * RETURNS:
943 * @plba: the LBA
944 * @plen: the transfer length
945 */
946
Jeff Garzik057ace52005-10-22 14:27:05 -0400947static void scsi_10_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -0400948{
949 u64 lba = 0;
950 u32 len = 0;
951
952 VPRINTK("ten-byte command\n");
953
954 lba |= ((u64)scsicmd[2]) << 24;
955 lba |= ((u64)scsicmd[3]) << 16;
956 lba |= ((u64)scsicmd[4]) << 8;
957 lba |= ((u64)scsicmd[5]);
958
959 len |= ((u32)scsicmd[7]) << 8;
960 len |= ((u32)scsicmd[8]);
961
962 *plba = lba;
963 *plen = len;
964}
965
966/**
967 * scsi_16_lba_len - Get LBA and transfer length
968 * @scsicmd: SCSI command to translate
969 *
970 * Calculate LBA and transfer length for 16-byte commands.
971 *
972 * RETURNS:
973 * @plba: the LBA
974 * @plen: the transfer length
975 */
976
Jeff Garzik057ace52005-10-22 14:27:05 -0400977static void scsi_16_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -0400978{
979 u64 lba = 0;
980 u32 len = 0;
981
982 VPRINTK("sixteen-byte command\n");
983
984 lba |= ((u64)scsicmd[2]) << 56;
985 lba |= ((u64)scsicmd[3]) << 48;
986 lba |= ((u64)scsicmd[4]) << 40;
987 lba |= ((u64)scsicmd[5]) << 32;
988 lba |= ((u64)scsicmd[6]) << 24;
989 lba |= ((u64)scsicmd[7]) << 16;
990 lba |= ((u64)scsicmd[8]) << 8;
991 lba |= ((u64)scsicmd[9]);
992
993 len |= ((u32)scsicmd[10]) << 24;
994 len |= ((u32)scsicmd[11]) << 16;
995 len |= ((u32)scsicmd[12]) << 8;
996 len |= ((u32)scsicmd[13]);
997
998 *plba = lba;
999 *plen = len;
1000}
1001
1002/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1004 * @qc: Storage for translated ATA taskfile
1005 * @scsicmd: SCSI command to translate
1006 *
1007 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1008 *
1009 * LOCKING:
1010 * spin_lock_irqsave(host_set lock)
1011 *
1012 * RETURNS:
1013 * Zero on success, non-zero on error.
1014 */
1015
Jeff Garzik057ace52005-10-22 14:27:05 -04001016static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017{
1018 struct ata_taskfile *tf = &qc->tf;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001019 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 u64 dev_sectors = qc->dev->n_sectors;
Albert Lee3aef5232005-10-04 08:47:43 -04001021 u64 block;
1022 u32 n_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023
1024 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1025 tf->protocol = ATA_PROT_NODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026
Albert Lee3aef5232005-10-04 08:47:43 -04001027 if (scsicmd[0] == VERIFY)
1028 scsi_10_lba_len(scsicmd, &block, &n_block);
1029 else if (scsicmd[0] == VERIFY_16)
1030 scsi_16_lba_len(scsicmd, &block, &n_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 else
Douglas Gilbertae006512005-10-09 09:09:35 -04001032 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Albert Lee8bf62ece2005-05-12 15:29:42 -04001034 if (!n_block)
Douglas Gilbertae006512005-10-09 09:09:35 -04001035 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001036 if (block >= dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001037 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001038 if ((block + n_block) > dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001039 goto out_of_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Albert Lee07506692005-10-12 15:04:18 +08001041 if (dev->flags & ATA_DFLAG_LBA) {
1042 tf->flags |= ATA_TFLAG_LBA;
1043
Albert Leec6a33e22005-10-12 15:12:26 +08001044 if (lba_28_ok(block, n_block)) {
1045 /* use LBA28 */
1046 tf->command = ATA_CMD_VERIFY;
1047 tf->device |= (block >> 24) & 0xf;
1048 } else if (lba_48_ok(block, n_block)) {
1049 if (!(dev->flags & ATA_DFLAG_LBA48))
1050 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001051
1052 /* use LBA48 */
1053 tf->flags |= ATA_TFLAG_LBA48;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001054 tf->command = ATA_CMD_VERIFY_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Albert Lee8bf62ece2005-05-12 15:29:42 -04001056 tf->hob_nsect = (n_block >> 8) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057
Albert Lee8bf62ece2005-05-12 15:29:42 -04001058 tf->hob_lbah = (block >> 40) & 0xff;
1059 tf->hob_lbam = (block >> 32) & 0xff;
1060 tf->hob_lbal = (block >> 24) & 0xff;
Albert Leec6a33e22005-10-12 15:12:26 +08001061 } else
1062 /* request too large even for LBA48 */
1063 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001064
1065 tf->nsect = n_block & 0xff;
1066
1067 tf->lbah = (block >> 16) & 0xff;
1068 tf->lbam = (block >> 8) & 0xff;
1069 tf->lbal = block & 0xff;
1070
1071 tf->device |= ATA_LBA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001073 /* CHS */
1074 u32 sect, head, cyl, track;
1075
Albert Leec6a33e22005-10-12 15:12:26 +08001076 if (!lba_28_ok(block, n_block))
1077 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001078
Albert Lee8bf62ece2005-05-12 15:29:42 -04001079 /* Convert LBA to CHS */
1080 track = (u32)block / dev->sectors;
1081 cyl = track / dev->heads;
1082 head = track % dev->heads;
1083 sect = (u32)block % dev->sectors + 1;
1084
Albert Leec187c4b2005-10-04 08:46:51 -04001085 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1086 (u32)block, track, cyl, head, sect);
Albert Lee8bf62ece2005-05-12 15:29:42 -04001087
1088 /* Check whether the converted CHS can fit.
1089 Cylinder: 0-65535
1090 Head: 0-15
1091 Sector: 1-255*/
1092 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
Douglas Gilbertae006512005-10-09 09:09:35 -04001093 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001094
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 tf->command = ATA_CMD_VERIFY;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001096 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1097 tf->lbal = sect;
1098 tf->lbam = cyl;
1099 tf->lbah = cyl >> 8;
1100 tf->device |= head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 }
1102
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001104
1105invalid_fld:
1106 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
1107 /* "Invalid field in cbd" */
1108 return 1;
1109
1110out_of_range:
1111 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
1112 /* "Logical Block Address out of range" */
1113 return 1;
1114
1115nothing_to_do:
1116 qc->scsicmd->result = SAM_STAT_GOOD;
1117 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118}
1119
1120/**
1121 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1122 * @qc: Storage for translated ATA taskfile
1123 * @scsicmd: SCSI command to translate
1124 *
1125 * Converts any of six SCSI read/write commands into the
1126 * ATA counterpart, including starting sector (LBA),
1127 * sector count, and taking into account the device's LBA48
1128 * support.
1129 *
1130 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1131 * %WRITE_16 are currently supported.
1132 *
1133 * LOCKING:
1134 * spin_lock_irqsave(host_set lock)
1135 *
1136 * RETURNS:
1137 * Zero on success, non-zero on error.
1138 */
1139
Jeff Garzik057ace52005-10-22 14:27:05 -04001140static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141{
1142 struct ata_taskfile *tf = &qc->tf;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001143 struct ata_device *dev = qc->dev;
Albert Lee3aef5232005-10-04 08:47:43 -04001144 u64 block;
1145 u32 n_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
1147 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148
Albert Lee8cbd6df2005-10-12 15:06:27 +08001149 if (scsicmd[0] == WRITE_10 || scsicmd[0] == WRITE_6 ||
1150 scsicmd[0] == WRITE_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 tf->flags |= ATA_TFLAG_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001153 /* Calculate the SCSI LBA, transfer length and FUA. */
Albert Lee3aef5232005-10-04 08:47:43 -04001154 switch (scsicmd[0]) {
1155 case READ_10:
1156 case WRITE_10:
1157 scsi_10_lba_len(scsicmd, &block, &n_block);
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001158 if (unlikely(scsicmd[1] & (1 << 3)))
1159 tf->flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001160 break;
1161 case READ_6:
1162 case WRITE_6:
1163 scsi_6_lba_len(scsicmd, &block, &n_block);
Albert Leec187c4b2005-10-04 08:46:51 -04001164
1165 /* for 6-byte r/w commands, transfer length 0
1166 * means 256 blocks of data, not 0 block.
1167 */
Jeff Garzik76b2bf92005-08-29 19:24:43 -04001168 if (!n_block)
1169 n_block = 256;
Albert Lee3aef5232005-10-04 08:47:43 -04001170 break;
1171 case READ_16:
1172 case WRITE_16:
1173 scsi_16_lba_len(scsicmd, &block, &n_block);
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001174 if (unlikely(scsicmd[1] & (1 << 3)))
1175 tf->flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001176 break;
1177 default:
Albert Lee8bf62ece2005-05-12 15:29:42 -04001178 DPRINTK("no-byte command\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04001179 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180 }
1181
Albert Lee8bf62ece2005-05-12 15:29:42 -04001182 /* Check and compose ATA command */
1183 if (!n_block)
Albert Leec187c4b2005-10-04 08:46:51 -04001184 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1185 * length 0 means transfer 0 block of data.
1186 * However, for ATA R/W commands, sector count 0 means
1187 * 256 or 65536 sectors, not 0 sectors as in SCSI.
Alan Coxf51750d2005-11-07 17:06:33 +00001188 *
1189 * WARNING: one or two older ATA drives treat 0 as 0...
Albert Leec187c4b2005-10-04 08:46:51 -04001190 */
Douglas Gilbertae006512005-10-09 09:09:35 -04001191 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001192
Albert Lee07506692005-10-12 15:04:18 +08001193 if (dev->flags & ATA_DFLAG_LBA) {
1194 tf->flags |= ATA_TFLAG_LBA;
1195
Albert Leec6a33e22005-10-12 15:12:26 +08001196 if (lba_28_ok(block, n_block)) {
1197 /* use LBA28 */
1198 tf->device |= (block >> 24) & 0xf;
1199 } else if (lba_48_ok(block, n_block)) {
1200 if (!(dev->flags & ATA_DFLAG_LBA48))
Douglas Gilbertae006512005-10-09 09:09:35 -04001201 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001202
Albert Lee07506692005-10-12 15:04:18 +08001203 /* use LBA48 */
1204 tf->flags |= ATA_TFLAG_LBA48;
1205
Albert Lee8bf62ece2005-05-12 15:29:42 -04001206 tf->hob_nsect = (n_block >> 8) & 0xff;
1207
1208 tf->hob_lbah = (block >> 40) & 0xff;
1209 tf->hob_lbam = (block >> 32) & 0xff;
1210 tf->hob_lbal = (block >> 24) & 0xff;
Albert Leec6a33e22005-10-12 15:12:26 +08001211 } else
1212 /* request too large even for LBA48 */
1213 goto out_of_range;
Albert Leec187c4b2005-10-04 08:46:51 -04001214
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001215 if (unlikely(ata_rwcmd_protocol(qc) < 0))
1216 goto invalid_fld;
Albert Lee8cbd6df2005-10-12 15:06:27 +08001217
Albert Lee8bf62ece2005-05-12 15:29:42 -04001218 qc->nsect = n_block;
1219 tf->nsect = n_block & 0xff;
1220
1221 tf->lbah = (block >> 16) & 0xff;
1222 tf->lbam = (block >> 8) & 0xff;
1223 tf->lbal = block & 0xff;
1224
1225 tf->device |= ATA_LBA;
1226 } else {
1227 /* CHS */
1228 u32 sect, head, cyl, track;
1229
1230 /* The request -may- be too large for CHS addressing. */
Albert Leec6a33e22005-10-12 15:12:26 +08001231 if (!lba_28_ok(block, n_block))
Douglas Gilbertae006512005-10-09 09:09:35 -04001232 goto out_of_range;
Albert Leec187c4b2005-10-04 08:46:51 -04001233
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001234 if (unlikely(ata_rwcmd_protocol(qc) < 0))
1235 goto invalid_fld;
Albert Lee8cbd6df2005-10-12 15:06:27 +08001236
Albert Lee8bf62ece2005-05-12 15:29:42 -04001237 /* Convert LBA to CHS */
1238 track = (u32)block / dev->sectors;
1239 cyl = track / dev->heads;
1240 head = track % dev->heads;
1241 sect = (u32)block % dev->sectors + 1;
1242
Albert Leec187c4b2005-10-04 08:46:51 -04001243 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
Albert Lee8bf62ece2005-05-12 15:29:42 -04001244 (u32)block, track, cyl, head, sect);
Albert Leec187c4b2005-10-04 08:46:51 -04001245
Albert Lee8bf62ece2005-05-12 15:29:42 -04001246 /* Check whether the converted CHS can fit.
1247 Cylinder: 0-65535
1248 Head: 0-15
1249 Sector: 1-255*/
Albert Leec187c4b2005-10-04 08:46:51 -04001250 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
Douglas Gilbertae006512005-10-09 09:09:35 -04001251 goto out_of_range;
Albert Leec187c4b2005-10-04 08:46:51 -04001252
Albert Lee8bf62ece2005-05-12 15:29:42 -04001253 qc->nsect = n_block;
1254 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1255 tf->lbal = sect;
1256 tf->lbam = cyl;
1257 tf->lbah = cyl >> 8;
1258 tf->device |= head;
1259 }
1260
1261 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001262
1263invalid_fld:
1264 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
1265 /* "Invalid field in cbd" */
1266 return 1;
1267
1268out_of_range:
1269 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
1270 /* "Logical Block Address out of range" */
1271 return 1;
1272
1273nothing_to_do:
1274 qc->scsicmd->result = SAM_STAT_GOOD;
1275 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276}
1277
Tejun Heo77853bf2006-01-23 13:09:36 +09001278static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279{
1280 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika7dac442005-10-30 04:44:42 -05001281 u8 *cdb = cmd->cmnd;
Albert Leea22e2eb2005-12-05 15:38:02 +08001282 int need_sense = (qc->err_mask != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
Jeff Garzikb0955182005-05-12 15:45:22 -04001284 /* For ATA pass thru (SAT) commands, generate a sense block if
1285 * user mandated it or if there's an error. Note that if we
1286 * generate because the user forced us to, a check condition
1287 * is generated and the ATA register values are returned
1288 * whether the command completed successfully or not. If there
1289 * was no error, SK, ASC and ASCQ will all be zero.
1290 */
Jeff Garzika7dac442005-10-30 04:44:42 -05001291 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
1292 ((cdb[2] & 0x20) || need_sense)) {
Jeff Garzikb0955182005-05-12 15:45:22 -04001293 ata_gen_ata_desc_sense(qc);
1294 } else {
1295 if (!need_sense) {
1296 cmd->result = SAM_STAT_GOOD;
1297 } else {
1298 /* TODO: decide which descriptor format to use
1299 * for 48b LBA devices and call that here
1300 * instead of the fixed desc, which is only
1301 * good for smaller LBA (and maybe CHS?)
1302 * devices.
1303 */
1304 ata_gen_fixed_sense(qc);
1305 }
1306 }
1307
1308 if (need_sense) {
1309 /* The ata_gen_..._sense routines fill in tf */
1310 ata_dump_status(qc->ap->id, &qc->tf);
1311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
1313 qc->scsidone(cmd);
1314
Tejun Heo77853bf2006-01-23 13:09:36 +09001315 ata_qc_free(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316}
1317
1318/**
1319 * ata_scsi_translate - Translate then issue SCSI command to ATA device
1320 * @ap: ATA port to which the command is addressed
1321 * @dev: ATA device to which the command is addressed
1322 * @cmd: SCSI command to execute
1323 * @done: SCSI command completion function
1324 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1325 *
1326 * Our ->queuecommand() function has decided that the SCSI
1327 * command issued can be directly translated into an ATA
1328 * command, rather than handled internally.
1329 *
1330 * This function sets up an ata_queued_cmd structure for the
1331 * SCSI command, and sends that ata_queued_cmd to the hardware.
1332 *
Douglas Gilbertae006512005-10-09 09:09:35 -04001333 * The xlat_func argument (actor) returns 0 if ready to execute
1334 * ATA command, else 1 to finish translation. If 1 is returned
1335 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1336 * to be set reflecting an error condition or clean (early)
1337 * termination.
1338 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 * LOCKING:
1340 * spin_lock_irqsave(host_set lock)
1341 */
1342
1343static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
1344 struct scsi_cmnd *cmd,
1345 void (*done)(struct scsi_cmnd *),
1346 ata_xlat_func_t xlat_func)
1347{
1348 struct ata_queued_cmd *qc;
1349 u8 *scsicmd = cmd->cmnd;
1350
1351 VPRINTK("ENTER\n");
1352
1353 qc = ata_scsi_qc_new(ap, dev, cmd, done);
1354 if (!qc)
Douglas Gilbertae006512005-10-09 09:09:35 -04001355 goto err_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
1357 /* data is present; dma-map it */
be7db052005-04-17 15:26:13 -05001358 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1359 cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 if (unlikely(cmd->request_bufflen < 1)) {
1361 printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
1362 ap->id, dev->devno);
Douglas Gilbertae006512005-10-09 09:09:35 -04001363 goto err_did;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 }
1365
1366 if (cmd->use_sg)
1367 ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
1368 else
1369 ata_sg_init_one(qc, cmd->request_buffer,
1370 cmd->request_bufflen);
1371
1372 qc->dma_dir = cmd->sc_data_direction;
1373 }
1374
1375 qc->complete_fn = ata_scsi_qc_complete;
1376
1377 if (xlat_func(qc, scsicmd))
Douglas Gilbertae006512005-10-09 09:09:35 -04001378 goto early_finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 /* select device, send command to hardware */
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09001381 qc->err_mask = ata_qc_issue(qc);
1382 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +09001383 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 VPRINTK("EXIT\n");
1386 return;
1387
Douglas Gilbertae006512005-10-09 09:09:35 -04001388early_finish:
1389 ata_qc_free(qc);
1390 done(cmd);
1391 DPRINTK("EXIT - early finish (good or error)\n");
1392 return;
1393
1394err_did:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 ata_qc_free(qc);
Douglas Gilbertae006512005-10-09 09:09:35 -04001396err_mem:
1397 cmd->result = (DID_ERROR << 16);
1398 done(cmd);
1399 DPRINTK("EXIT - internal\n");
1400 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401}
1402
1403/**
1404 * ata_scsi_rbuf_get - Map response buffer.
1405 * @cmd: SCSI command containing buffer to be mapped.
1406 * @buf_out: Pointer to mapped area.
1407 *
1408 * Maps buffer contained within SCSI command @cmd.
1409 *
1410 * LOCKING:
1411 * spin_lock_irqsave(host_set lock)
1412 *
1413 * RETURNS:
1414 * Length of response buffer.
1415 */
1416
1417static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
1418{
1419 u8 *buf;
1420 unsigned int buflen;
1421
1422 if (cmd->use_sg) {
1423 struct scatterlist *sg;
1424
1425 sg = (struct scatterlist *) cmd->request_buffer;
1426 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
1427 buflen = sg->length;
1428 } else {
1429 buf = cmd->request_buffer;
1430 buflen = cmd->request_bufflen;
1431 }
1432
1433 *buf_out = buf;
1434 return buflen;
1435}
1436
1437/**
1438 * ata_scsi_rbuf_put - Unmap response buffer.
1439 * @cmd: SCSI command containing buffer to be unmapped.
1440 * @buf: buffer to unmap
1441 *
1442 * Unmaps response buffer contained within @cmd.
1443 *
1444 * LOCKING:
1445 * spin_lock_irqsave(host_set lock)
1446 */
1447
1448static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
1449{
1450 if (cmd->use_sg) {
1451 struct scatterlist *sg;
1452
1453 sg = (struct scatterlist *) cmd->request_buffer;
1454 kunmap_atomic(buf - sg->offset, KM_USER0);
1455 }
1456}
1457
1458/**
1459 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1460 * @args: device IDENTIFY data / SCSI command of interest.
1461 * @actor: Callback hook for desired SCSI command simulator
1462 *
1463 * Takes care of the hard work of simulating a SCSI command...
1464 * Mapping the response buffer, calling the command's handler,
1465 * and handling the handler's return value. This return value
1466 * indicates whether the handler wishes the SCSI command to be
Douglas Gilbertae006512005-10-09 09:09:35 -04001467 * completed successfully (0), or not (in which case cmd->result
1468 * and sense buffer are assumed to be set).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 *
1470 * LOCKING:
1471 * spin_lock_irqsave(host_set lock)
1472 */
1473
1474void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
1475 unsigned int (*actor) (struct ata_scsi_args *args,
1476 u8 *rbuf, unsigned int buflen))
1477{
1478 u8 *rbuf;
1479 unsigned int buflen, rc;
1480 struct scsi_cmnd *cmd = args->cmd;
1481
1482 buflen = ata_scsi_rbuf_get(cmd, &rbuf);
1483 memset(rbuf, 0, buflen);
1484 rc = actor(args, rbuf, buflen);
1485 ata_scsi_rbuf_put(cmd, rbuf);
1486
Douglas Gilbertae006512005-10-09 09:09:35 -04001487 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 cmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001489 args->done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490}
1491
1492/**
1493 * ata_scsiop_inq_std - Simulate INQUIRY command
1494 * @args: device IDENTIFY data / SCSI command of interest.
1495 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1496 * @buflen: Response buffer length.
1497 *
1498 * Returns standard device identification data associated
1499 * with non-EVPD INQUIRY command output.
1500 *
1501 * LOCKING:
1502 * spin_lock_irqsave(host_set lock)
1503 */
1504
1505unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
1506 unsigned int buflen)
1507{
1508 u8 hdr[] = {
1509 TYPE_DISK,
1510 0,
1511 0x5, /* claim SPC-3 version compatibility */
1512 2,
1513 95 - 4
1514 };
1515
1516 /* set scsi removeable (RMB) bit per ata bit */
1517 if (ata_id_removeable(args->id))
1518 hdr[1] |= (1 << 7);
1519
1520 VPRINTK("ENTER\n");
1521
1522 memcpy(rbuf, hdr, sizeof(hdr));
1523
1524 if (buflen > 35) {
1525 memcpy(&rbuf[8], "ATA ", 8);
1526 ata_dev_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
1527 ata_dev_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
1528 if (rbuf[32] == 0 || rbuf[32] == ' ')
1529 memcpy(&rbuf[32], "n/a ", 4);
1530 }
1531
1532 if (buflen > 63) {
1533 const u8 versions[] = {
1534 0x60, /* SAM-3 (no version claimed) */
1535
1536 0x03,
1537 0x20, /* SBC-2 (no version claimed) */
1538
1539 0x02,
1540 0x60 /* SPC-3 (no version claimed) */
1541 };
1542
1543 memcpy(rbuf + 59, versions, sizeof(versions));
1544 }
1545
1546 return 0;
1547}
1548
1549/**
1550 * ata_scsiop_inq_00 - Simulate INQUIRY EVPD page 0, list of pages
1551 * @args: device IDENTIFY data / SCSI command of interest.
1552 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1553 * @buflen: Response buffer length.
1554 *
1555 * Returns list of inquiry EVPD pages available.
1556 *
1557 * LOCKING:
1558 * spin_lock_irqsave(host_set lock)
1559 */
1560
1561unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
1562 unsigned int buflen)
1563{
1564 const u8 pages[] = {
1565 0x00, /* page 0x00, this page */
1566 0x80, /* page 0x80, unit serial no page */
1567 0x83 /* page 0x83, device ident page */
1568 };
1569 rbuf[3] = sizeof(pages); /* number of supported EVPD pages */
1570
1571 if (buflen > 6)
1572 memcpy(rbuf + 4, pages, sizeof(pages));
1573
1574 return 0;
1575}
1576
1577/**
1578 * ata_scsiop_inq_80 - Simulate INQUIRY EVPD page 80, device serial number
1579 * @args: device IDENTIFY data / SCSI command of interest.
1580 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1581 * @buflen: Response buffer length.
1582 *
1583 * Returns ATA device serial number.
1584 *
1585 * LOCKING:
1586 * spin_lock_irqsave(host_set lock)
1587 */
1588
1589unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
1590 unsigned int buflen)
1591{
1592 const u8 hdr[] = {
1593 0,
1594 0x80, /* this page code */
1595 0,
1596 ATA_SERNO_LEN, /* page len */
1597 };
1598 memcpy(rbuf, hdr, sizeof(hdr));
1599
1600 if (buflen > (ATA_SERNO_LEN + 4 - 1))
1601 ata_dev_id_string(args->id, (unsigned char *) &rbuf[4],
1602 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1603
1604 return 0;
1605}
1606
Arjan van de Ven98ac62d2005-11-28 10:06:23 +01001607static const char * const inq_83_str = "Linux ATA-SCSI simulator";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609/**
1610 * ata_scsiop_inq_83 - Simulate INQUIRY EVPD page 83, device identity
1611 * @args: device IDENTIFY data / SCSI command of interest.
1612 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1613 * @buflen: Response buffer length.
1614 *
1615 * Returns device identification. Currently hardcoded to
1616 * return "Linux ATA-SCSI simulator".
1617 *
1618 * LOCKING:
1619 * spin_lock_irqsave(host_set lock)
1620 */
1621
1622unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
1623 unsigned int buflen)
1624{
1625 rbuf[1] = 0x83; /* this page code */
1626 rbuf[3] = 4 + strlen(inq_83_str); /* page len */
1627
1628 /* our one and only identification descriptor (vendor-specific) */
1629 if (buflen > (strlen(inq_83_str) + 4 + 4 - 1)) {
1630 rbuf[4 + 0] = 2; /* code set: ASCII */
1631 rbuf[4 + 3] = strlen(inq_83_str);
1632 memcpy(rbuf + 4 + 4, inq_83_str, strlen(inq_83_str));
1633 }
1634
1635 return 0;
1636}
1637
1638/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001639 * ata_scsiop_noop - Command handler that simply returns success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 * @args: device IDENTIFY data / SCSI command of interest.
1641 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1642 * @buflen: Response buffer length.
1643 *
1644 * No operation. Simply returns success to caller, to indicate
1645 * that the caller should successfully complete this SCSI command.
1646 *
1647 * LOCKING:
1648 * spin_lock_irqsave(host_set lock)
1649 */
1650
1651unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
1652 unsigned int buflen)
1653{
1654 VPRINTK("ENTER\n");
1655 return 0;
1656}
1657
1658/**
1659 * ata_msense_push - Push data onto MODE SENSE data output buffer
1660 * @ptr_io: (input/output) Location to store more output data
1661 * @last: End of output data buffer
1662 * @buf: Pointer to BLOB being added to output buffer
1663 * @buflen: Length of BLOB
1664 *
1665 * Store MODE SENSE data on an output buffer.
1666 *
1667 * LOCKING:
1668 * None.
1669 */
1670
1671static void ata_msense_push(u8 **ptr_io, const u8 *last,
1672 const u8 *buf, unsigned int buflen)
1673{
1674 u8 *ptr = *ptr_io;
1675
1676 if ((ptr + buflen - 1) > last)
1677 return;
1678
1679 memcpy(ptr, buf, buflen);
1680
1681 ptr += buflen;
1682
1683 *ptr_io = ptr;
1684}
1685
1686/**
1687 * ata_msense_caching - Simulate MODE SENSE caching info page
1688 * @id: device IDENTIFY data
1689 * @ptr_io: (input/output) Location to store more output data
1690 * @last: End of output data buffer
1691 *
1692 * Generate a caching info page, which conditionally indicates
1693 * write caching to the SCSI layer, depending on device
1694 * capabilities.
1695 *
1696 * LOCKING:
1697 * None.
1698 */
1699
1700static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1701 const u8 *last)
1702{
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001703 u8 page[CACHE_MPAGE_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001705 memcpy(page, def_cache_mpage, sizeof(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 if (ata_id_wcache_enabled(id))
1707 page[2] |= (1 << 2); /* write cache enable */
1708 if (!ata_id_rahead_enabled(id))
1709 page[12] |= (1 << 5); /* disable read ahead */
1710
1711 ata_msense_push(ptr_io, last, page, sizeof(page));
1712 return sizeof(page);
1713}
1714
1715/**
1716 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1717 * @dev: Device associated with this MODE SENSE command
1718 * @ptr_io: (input/output) Location to store more output data
1719 * @last: End of output data buffer
1720 *
1721 * Generate a generic MODE SENSE control mode page.
1722 *
1723 * LOCKING:
1724 * None.
1725 */
1726
1727static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1728{
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001729 ata_msense_push(ptr_io, last, def_control_mpage,
1730 sizeof(def_control_mpage));
1731 return sizeof(def_control_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732}
1733
1734/**
1735 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1736 * @dev: Device associated with this MODE SENSE command
1737 * @ptr_io: (input/output) Location to store more output data
1738 * @last: End of output data buffer
1739 *
1740 * Generate a generic MODE SENSE r/w error recovery page.
1741 *
1742 * LOCKING:
1743 * None.
1744 */
1745
1746static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1747{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001749 ata_msense_push(ptr_io, last, def_rw_recovery_mpage,
1750 sizeof(def_rw_recovery_mpage));
1751 return sizeof(def_rw_recovery_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752}
1753
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001754/*
1755 * We can turn this into a real blacklist if it's needed, for now just
1756 * blacklist any Maxtor BANC1G10 revision firmware
1757 */
1758static int ata_dev_supports_fua(u16 *id)
1759{
1760 unsigned char model[41], fw[9];
1761
1762 if (!ata_id_has_fua(id))
1763 return 0;
1764
1765 model[40] = '\0';
1766 fw[8] = '\0';
1767
1768 ata_dev_id_string(id, model, ATA_ID_PROD_OFS, sizeof(model) - 1);
1769 ata_dev_id_string(id, fw, ATA_ID_FW_REV_OFS, sizeof(fw) - 1);
1770
1771 if (strncmp(model, "Maxtor", 6))
1772 return 1;
1773 if (strncmp(fw, "BANC1G10", 8))
1774 return 1;
1775
1776 return 0; /* blacklisted */
1777}
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779/**
1780 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1781 * @args: device IDENTIFY data / SCSI command of interest.
1782 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1783 * @buflen: Response buffer length.
1784 *
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001785 * Simulate MODE SENSE commands. Assume this is invoked for direct
1786 * access devices (e.g. disks) only. There should be no block
1787 * descriptor for other device types.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 *
1789 * LOCKING:
1790 * spin_lock_irqsave(host_set lock)
1791 */
1792
1793unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1794 unsigned int buflen)
1795{
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001796 struct ata_device *dev = args->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797 u8 *scsicmd = args->cmd->cmnd, *p, *last;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001798 const u8 sat_blk_desc[] = {
1799 0, 0, 0, 0, /* number of blocks: sat unspecified */
1800 0,
1801 0, 0x2, 0x0 /* block length: 512 bytes */
1802 };
1803 u8 pg, spg;
1804 unsigned int ebd, page_control, six_byte, output_len, alloc_len, minlen;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001805 u8 dpofua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
1807 VPRINTK("ENTER\n");
1808
1809 six_byte = (scsicmd[0] == MODE_SENSE);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001810 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
1811 /*
1812 * LLBA bit in msense(10) ignored (compliant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001814
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 page_control = scsicmd[2] >> 6;
Douglas Gilbertae006512005-10-09 09:09:35 -04001816 switch (page_control) {
1817 case 0: /* current */
1818 break; /* supported */
1819 case 3: /* saved */
1820 goto saving_not_supp;
1821 case 1: /* changeable */
1822 case 2: /* defaults */
1823 default:
1824 goto invalid_fld;
1825 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001827 if (six_byte) {
1828 output_len = 4 + (ebd ? 8 : 0);
1829 alloc_len = scsicmd[4];
1830 } else {
1831 output_len = 8 + (ebd ? 8 : 0);
1832 alloc_len = (scsicmd[7] << 8) + scsicmd[8];
1833 }
1834 minlen = (alloc_len < buflen) ? alloc_len : buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 p = rbuf + output_len;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001837 last = rbuf + minlen - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001839 pg = scsicmd[2] & 0x3f;
1840 spg = scsicmd[3];
1841 /*
1842 * No mode subpages supported (yet) but asking for _all_
1843 * subpages may be valid
1844 */
1845 if (spg && (spg != ALL_SUB_MPAGES))
1846 goto invalid_fld;
1847
1848 switch(pg) {
1849 case RW_RECOVERY_MPAGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 output_len += ata_msense_rw_recovery(&p, last);
1851 break;
1852
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001853 case CACHE_MPAGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 output_len += ata_msense_caching(args->id, &p, last);
1855 break;
1856
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001857 case CONTROL_MPAGE: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 output_len += ata_msense_ctl_mode(&p, last);
1859 break;
1860 }
1861
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001862 case ALL_MPAGES:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 output_len += ata_msense_rw_recovery(&p, last);
1864 output_len += ata_msense_caching(args->id, &p, last);
1865 output_len += ata_msense_ctl_mode(&p, last);
1866 break;
1867
1868 default: /* invalid page code */
Douglas Gilbertae006512005-10-09 09:09:35 -04001869 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 }
1871
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001872 if (minlen < 1)
1873 return 0;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001874
1875 dpofua = 0;
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001876 if (ata_dev_supports_fua(args->id) && dev->flags & ATA_DFLAG_LBA48 &&
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001877 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
1878 dpofua = 1 << 4;
1879
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880 if (six_byte) {
1881 output_len--;
1882 rbuf[0] = output_len;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001883 if (minlen > 2)
1884 rbuf[2] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001885 if (ebd) {
1886 if (minlen > 3)
1887 rbuf[3] = sizeof(sat_blk_desc);
1888 if (minlen > 11)
1889 memcpy(rbuf + 4, sat_blk_desc,
1890 sizeof(sat_blk_desc));
1891 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 } else {
1893 output_len -= 2;
1894 rbuf[0] = output_len >> 8;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001895 if (minlen > 1)
1896 rbuf[1] = output_len;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001897 if (minlen > 3)
1898 rbuf[3] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001899 if (ebd) {
1900 if (minlen > 7)
1901 rbuf[7] = sizeof(sat_blk_desc);
1902 if (minlen > 15)
1903 memcpy(rbuf + 8, sat_blk_desc,
1904 sizeof(sat_blk_desc));
1905 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001908
1909invalid_fld:
1910 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
1911 /* "Invalid field in cbd" */
1912 return 1;
1913
1914saving_not_supp:
1915 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
1916 /* "Saving parameters not supported" */
1917 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918}
1919
1920/**
1921 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
1922 * @args: device IDENTIFY data / SCSI command of interest.
1923 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1924 * @buflen: Response buffer length.
1925 *
1926 * Simulate READ CAPACITY commands.
1927 *
1928 * LOCKING:
1929 * spin_lock_irqsave(host_set lock)
1930 */
1931
1932unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
1933 unsigned int buflen)
1934{
1935 u64 n_sectors;
1936 u32 tmp;
1937
1938 VPRINTK("ENTER\n");
1939
Albert Lee8bf62ece2005-05-12 15:29:42 -04001940 if (ata_id_has_lba(args->id)) {
1941 if (ata_id_has_lba48(args->id))
1942 n_sectors = ata_id_u64(args->id, 100);
1943 else
1944 n_sectors = ata_id_u32(args->id, 60);
1945 } else {
1946 /* CHS default translation */
1947 n_sectors = args->id[1] * args->id[3] * args->id[6];
1948
1949 if (ata_id_current_chs_valid(args->id))
1950 /* CHS current translation */
1951 n_sectors = ata_id_u32(args->id, 57);
1952 }
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954 n_sectors--; /* ATA TotalUserSectors - 1 */
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (args->cmd->cmnd[0] == READ_CAPACITY) {
Philip Pokorny0c144d02005-05-28 01:24:47 -07001957 if( n_sectors >= 0xffffffffULL )
1958 tmp = 0xffffffff ; /* Return max count on overflow */
1959 else
1960 tmp = n_sectors ;
1961
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 /* sector count, 32-bit */
1963 rbuf[0] = tmp >> (8 * 3);
1964 rbuf[1] = tmp >> (8 * 2);
1965 rbuf[2] = tmp >> (8 * 1);
1966 rbuf[3] = tmp;
1967
1968 /* sector size */
1969 tmp = ATA_SECT_SIZE;
1970 rbuf[6] = tmp >> 8;
1971 rbuf[7] = tmp;
1972
1973 } else {
1974 /* sector count, 64-bit */
Philip Pokorny0c144d02005-05-28 01:24:47 -07001975 tmp = n_sectors >> (8 * 4);
1976 rbuf[2] = tmp >> (8 * 3);
1977 rbuf[3] = tmp >> (8 * 2);
1978 rbuf[4] = tmp >> (8 * 1);
1979 rbuf[5] = tmp;
1980 tmp = n_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 rbuf[6] = tmp >> (8 * 3);
1982 rbuf[7] = tmp >> (8 * 2);
1983 rbuf[8] = tmp >> (8 * 1);
1984 rbuf[9] = tmp;
1985
1986 /* sector size */
1987 tmp = ATA_SECT_SIZE;
1988 rbuf[12] = tmp >> 8;
1989 rbuf[13] = tmp;
1990 }
1991
1992 return 0;
1993}
1994
1995/**
1996 * ata_scsiop_report_luns - Simulate REPORT LUNS command
1997 * @args: device IDENTIFY data / SCSI command of interest.
1998 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1999 * @buflen: Response buffer length.
2000 *
2001 * Simulate REPORT LUNS command.
2002 *
2003 * LOCKING:
2004 * spin_lock_irqsave(host_set lock)
2005 */
2006
2007unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
2008 unsigned int buflen)
2009{
2010 VPRINTK("ENTER\n");
2011 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2012
2013 return 0;
2014}
2015
2016/**
Douglas Gilbert845c5832005-10-09 08:55:41 -04002017 * ata_scsi_set_sense - Set SCSI sense data and status
2018 * @cmd: SCSI request to be handled
2019 * @sk: SCSI-defined sense key
2020 * @asc: SCSI-defined additional sense code
2021 * @ascq: SCSI-defined additional sense code qualifier
2022 *
2023 * Helper function that builds a valid fixed format, current
2024 * response code and the given sense key (sk), additional sense
2025 * code (asc) and additional sense code qualifier (ascq) with
2026 * a SCSI command status of %SAM_STAT_CHECK_CONDITION and
2027 * DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
2028 *
2029 * LOCKING:
2030 * Not required
2031 */
2032
2033void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
2034{
2035 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
2036
2037 cmd->sense_buffer[0] = 0x70; /* fixed format, current */
2038 cmd->sense_buffer[2] = sk;
2039 cmd->sense_buffer[7] = 18 - 8; /* additional sense length */
2040 cmd->sense_buffer[12] = asc;
2041 cmd->sense_buffer[13] = ascq;
2042}
2043
2044/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 * ata_scsi_badcmd - End a SCSI request with an error
2046 * @cmd: SCSI request to be handled
2047 * @done: SCSI command completion function
2048 * @asc: SCSI-defined additional sense code
2049 * @ascq: SCSI-defined additional sense code qualifier
2050 *
2051 * Helper function that completes a SCSI command with
2052 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
2053 * and the specified additional sense codes.
2054 *
2055 * LOCKING:
2056 * spin_lock_irqsave(host_set lock)
2057 */
2058
2059void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
2060{
2061 DPRINTK("ENTER\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04002062 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063
2064 done(cmd);
2065}
2066
Tejun Heo77853bf2006-01-23 13:09:36 +09002067static void atapi_sense_complete(struct ata_queued_cmd *qc)
Jeff Garzika939c962005-10-05 17:09:16 -04002068{
Albert Leea22e2eb2005-12-05 15:38:02 +08002069 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0))
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002070 /* FIXME: not quite right; we don't want the
2071 * translation of taskfile registers into
2072 * a sense descriptors, since that's only
2073 * correct for ATA, not ATAPI
2074 */
2075 ata_gen_ata_desc_sense(qc);
2076
2077 qc->scsidone(qc->scsicmd);
Tejun Heo77853bf2006-01-23 13:09:36 +09002078 ata_qc_free(qc);
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002079}
2080
2081/* is it pointless to prefer PIO for "safety reasons"? */
2082static inline int ata_pio_use_silly(struct ata_port *ap)
2083{
2084 return (ap->flags & ATA_FLAG_PIO_DMA);
2085}
2086
2087static void atapi_request_sense(struct ata_queued_cmd *qc)
2088{
2089 struct ata_port *ap = qc->ap;
2090 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika939c962005-10-05 17:09:16 -04002091
2092 DPRINTK("ATAPI request sense\n");
2093
Jeff Garzika939c962005-10-05 17:09:16 -04002094 /* FIXME: is this needed? */
2095 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
2096
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002097 ap->ops->tf_read(ap, &qc->tf);
2098
2099 /* fill these in, for the case where they are -not- overwritten */
2100 cmd->sense_buffer[0] = 0x70;
2101 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2102
2103 ata_qc_reinit(qc);
2104
Jeff Garzika939c962005-10-05 17:09:16 -04002105 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
2106 qc->dma_dir = DMA_FROM_DEVICE;
2107
2108 memset(&qc->cdb, 0, ap->cdb_len);
2109 qc->cdb[0] = REQUEST_SENSE;
2110 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2111
2112 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2113 qc->tf.command = ATA_CMD_PACKET;
2114
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002115 if (ata_pio_use_silly(ap)) {
2116 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
2117 qc->tf.feature |= ATAPI_PKT_DMA;
2118 } else {
2119 qc->tf.protocol = ATA_PROT_ATAPI;
2120 qc->tf.lbam = (8 * 1024) & 0xff;
2121 qc->tf.lbah = (8 * 1024) >> 8;
2122 }
Jeff Garzika939c962005-10-05 17:09:16 -04002123 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2124
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002125 qc->complete_fn = atapi_sense_complete;
Jeff Garzika939c962005-10-05 17:09:16 -04002126
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09002127 qc->err_mask = ata_qc_issue(qc);
2128 if (qc->err_mask)
Albert Leea22e2eb2005-12-05 15:38:02 +08002129 ata_qc_complete(qc);
Jeff Garzika939c962005-10-05 17:09:16 -04002130
2131 DPRINTK("EXIT\n");
2132}
2133
Tejun Heo77853bf2006-01-23 13:09:36 +09002134static void atapi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135{
2136 struct scsi_cmnd *cmd = qc->scsicmd;
Albert Leea22e2eb2005-12-05 15:38:02 +08002137 unsigned int err_mask = qc->err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
Jeff Garzika7dac442005-10-30 04:44:42 -05002139 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
Jeff Garzike12669e2005-10-05 18:39:23 -04002140
Jeff Garzika7dac442005-10-30 04:44:42 -05002141 if (unlikely(err_mask & AC_ERR_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 cmd->result = SAM_STAT_CHECK_CONDITION;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002143 atapi_request_sense(qc);
Tejun Heo77853bf2006-01-23 13:09:36 +09002144 return;
Jeff Garzike12669e2005-10-05 18:39:23 -04002145 }
2146
Jeff Garzika7dac442005-10-30 04:44:42 -05002147 else if (unlikely(err_mask))
2148 /* FIXME: not quite right; we don't want the
2149 * translation of taskfile registers into
2150 * a sense descriptors, since that's only
2151 * correct for ATA, not ATAPI
2152 */
2153 ata_gen_ata_desc_sense(qc);
2154
Jeff Garzike12669e2005-10-05 18:39:23 -04002155 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 u8 *scsicmd = cmd->cmnd;
2157
Tony Battersbyfd71da42005-12-21 16:35:44 -05002158 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 u8 *buf = NULL;
2160 unsigned int buflen;
2161
2162 buflen = ata_scsi_rbuf_get(cmd, &buf);
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002163
2164 /* ATAPI devices typically report zero for their SCSI version,
2165 * and sometimes deviate from the spec WRT response data
2166 * format. If SCSI version is reported as zero like normal,
2167 * then we make the following fixups: 1) Fake MMC-5 version,
2168 * to indicate to the Linux scsi midlayer this is a modern
2169 * device. 2) Ensure response data format / ATAPI information
2170 * are always correct.
2171 */
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002172 if (buf[2] == 0) {
2173 buf[2] = 0x5;
2174 buf[3] = 0x32;
2175 }
2176
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 ata_scsi_rbuf_put(cmd, buf);
2178 }
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002179
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 cmd->result = SAM_STAT_GOOD;
2181 }
2182
2183 qc->scsidone(cmd);
Tejun Heo77853bf2006-01-23 13:09:36 +09002184 ata_qc_free(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185}
2186/**
2187 * atapi_xlat - Initialize PACKET taskfile
2188 * @qc: command structure to be initialized
2189 * @scsicmd: SCSI CDB associated with this PACKET command
2190 *
2191 * LOCKING:
2192 * spin_lock_irqsave(host_set lock)
2193 *
2194 * RETURNS:
2195 * Zero on success, non-zero on failure.
2196 */
2197
Jeff Garzik057ace52005-10-22 14:27:05 -04002198static unsigned int atapi_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199{
2200 struct scsi_cmnd *cmd = qc->scsicmd;
2201 struct ata_device *dev = qc->dev;
2202 int using_pio = (dev->flags & ATA_DFLAG_PIO);
be7db052005-04-17 15:26:13 -05002203 int nodata = (cmd->sc_data_direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
2205 if (!using_pio)
2206 /* Check whether ATAPI DMA is safe */
2207 if (ata_check_atapi_dma(qc))
2208 using_pio = 1;
2209
2210 memcpy(&qc->cdb, scsicmd, qc->ap->cdb_len);
2211
2212 qc->complete_fn = atapi_qc_complete;
2213
2214 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
be7db052005-04-17 15:26:13 -05002215 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002216 qc->tf.flags |= ATA_TFLAG_WRITE;
2217 DPRINTK("direction: write\n");
2218 }
2219
2220 qc->tf.command = ATA_CMD_PACKET;
2221
2222 /* no data, or PIO data xfer */
2223 if (using_pio || nodata) {
2224 if (nodata)
2225 qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
2226 else
2227 qc->tf.protocol = ATA_PROT_ATAPI;
2228 qc->tf.lbam = (8 * 1024) & 0xff;
2229 qc->tf.lbah = (8 * 1024) >> 8;
2230 }
2231
2232 /* DMA data xfer */
2233 else {
2234 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
2235 qc->tf.feature |= ATAPI_PKT_DMA;
2236
2237#ifdef ATAPI_ENABLE_DMADIR
2238 /* some SATA bridges need us to indicate data xfer direction */
be7db052005-04-17 15:26:13 -05002239 if (cmd->sc_data_direction != DMA_TO_DEVICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 qc->tf.feature |= ATAPI_DMADIR;
2241#endif
2242 }
2243
2244 qc->nbytes = cmd->bufflen;
2245
2246 return 0;
2247}
2248
2249/**
2250 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2251 * @ap: ATA port to which the device is attached
2252 * @scsidev: SCSI device from which we derive the ATA device
2253 *
2254 * Given various information provided in struct scsi_cmnd,
2255 * map that onto an ATA bus, and using that mapping
2256 * determine which ata_device is associated with the
2257 * SCSI command to be sent.
2258 *
2259 * LOCKING:
2260 * spin_lock_irqsave(host_set lock)
2261 *
2262 * RETURNS:
2263 * Associated ATA device, or %NULL if not found.
2264 */
2265
2266static struct ata_device *
Jeff Garzik057ace52005-10-22 14:27:05 -04002267ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268{
2269 struct ata_device *dev;
2270
2271 /* skip commands not addressed to targets we simulate */
2272 if (likely(scsidev->id < ATA_MAX_DEVICES))
2273 dev = &ap->device[scsidev->id];
2274 else
2275 return NULL;
2276
2277 if (unlikely((scsidev->channel != 0) ||
2278 (scsidev->lun != 0)))
2279 return NULL;
2280
2281 if (unlikely(!ata_dev_present(dev)))
2282 return NULL;
2283
Jeff Garzik50630192005-12-13 02:29:45 -05002284 if (!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) {
2285 if (unlikely(dev->class == ATA_DEV_ATAPI)) {
2286 printk(KERN_WARNING "ata%u(%u): WARNING: ATAPI is %s, device ignored.\n",
2287 ap->id, dev->devno, atapi_enabled ? "not supported with this driver" : "disabled");
Jeff Garzik1623c812005-08-30 03:37:42 -04002288 return NULL;
Jeff Garzik50630192005-12-13 02:29:45 -05002289 }
Jeff Garzik1623c812005-08-30 03:37:42 -04002290 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291
2292 return dev;
2293}
2294
Jeff Garzikb0955182005-05-12 15:45:22 -04002295/*
2296 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2297 * @byte1: Byte 1 from pass-thru CDB.
2298 *
2299 * RETURNS:
2300 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2301 */
2302static u8
2303ata_scsi_map_proto(u8 byte1)
2304{
2305 switch((byte1 & 0x1e) >> 1) {
2306 case 3: /* Non-data */
2307 return ATA_PROT_NODATA;
2308
2309 case 6: /* DMA */
2310 return ATA_PROT_DMA;
2311
2312 case 4: /* PIO Data-in */
2313 case 5: /* PIO Data-out */
2314 if (byte1 & 0xe0) {
2315 return ATA_PROT_PIO_MULT;
2316 }
2317 return ATA_PROT_PIO;
2318
2319 case 10: /* Device Reset */
2320 case 0: /* Hard Reset */
2321 case 1: /* SRST */
2322 case 2: /* Bus Idle */
2323 case 7: /* Packet */
2324 case 8: /* DMA Queued */
2325 case 9: /* Device Diagnostic */
2326 case 11: /* UDMA Data-in */
2327 case 12: /* UDMA Data-Out */
2328 case 13: /* FPDMA */
2329 default: /* Reserved */
2330 break;
2331 }
2332
2333 return ATA_PROT_UNKNOWN;
2334}
2335
2336/**
2337 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
2338 * @qc: command structure to be initialized
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002339 * @scsicmd: SCSI command to convert
Jeff Garzikb0955182005-05-12 15:45:22 -04002340 *
2341 * Handles either 12 or 16-byte versions of the CDB.
2342 *
2343 * RETURNS:
2344 * Zero on success, non-zero on failure.
2345 */
2346static unsigned int
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002347ata_scsi_pass_thru(struct ata_queued_cmd *qc, const u8 *scsicmd)
Jeff Garzikb0955182005-05-12 15:45:22 -04002348{
2349 struct ata_taskfile *tf = &(qc->tf);
2350 struct scsi_cmnd *cmd = qc->scsicmd;
2351
2352 if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
Tejun Heo9a405252005-12-02 11:49:11 +09002353 goto invalid_fld;
Jeff Garzikb0955182005-05-12 15:45:22 -04002354
2355 /*
2356 * 12 and 16 byte CDBs use different offsets to
2357 * provide the various register values.
2358 */
2359 if (scsicmd[0] == ATA_16) {
2360 /*
2361 * 16-byte CDB - may contain extended commands.
2362 *
2363 * If that is the case, copy the upper byte register values.
2364 */
2365 if (scsicmd[1] & 0x01) {
2366 tf->hob_feature = scsicmd[3];
2367 tf->hob_nsect = scsicmd[5];
2368 tf->hob_lbal = scsicmd[7];
2369 tf->hob_lbam = scsicmd[9];
2370 tf->hob_lbah = scsicmd[11];
2371 tf->flags |= ATA_TFLAG_LBA48;
2372 } else
2373 tf->flags &= ~ATA_TFLAG_LBA48;
2374
2375 /*
2376 * Always copy low byte, device and command registers.
2377 */
2378 tf->feature = scsicmd[4];
2379 tf->nsect = scsicmd[6];
2380 tf->lbal = scsicmd[8];
2381 tf->lbam = scsicmd[10];
2382 tf->lbah = scsicmd[12];
2383 tf->device = scsicmd[13];
2384 tf->command = scsicmd[14];
2385 } else {
2386 /*
2387 * 12-byte CDB - incapable of extended commands.
2388 */
2389 tf->flags &= ~ATA_TFLAG_LBA48;
2390
2391 tf->feature = scsicmd[3];
2392 tf->nsect = scsicmd[4];
2393 tf->lbal = scsicmd[5];
2394 tf->lbam = scsicmd[6];
2395 tf->lbah = scsicmd[7];
2396 tf->device = scsicmd[8];
2397 tf->command = scsicmd[9];
2398 }
Mark Lorddcc2d1e2005-11-13 16:22:06 -05002399 /*
2400 * If slave is possible, enforce correct master/slave bit
2401 */
2402 if (qc->ap->flags & ATA_FLAG_SLAVE_POSS)
2403 tf->device = qc->dev->devno ?
2404 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
Jeff Garzikb0955182005-05-12 15:45:22 -04002405
2406 /*
2407 * Filter SET_FEATURES - XFER MODE command -- otherwise,
2408 * SET_FEATURES - XFER MODE must be preceded/succeeded
2409 * by an update to hardware-specific registers for each
2410 * controller (i.e. the reason for ->set_piomode(),
2411 * ->set_dmamode(), and ->post_set_mode() hooks).
2412 */
2413 if ((tf->command == ATA_CMD_SET_FEATURES)
2414 && (tf->feature == SETFEATURES_XFER))
Tejun Heo9a405252005-12-02 11:49:11 +09002415 goto invalid_fld;
Jeff Garzikb0955182005-05-12 15:45:22 -04002416
2417 /*
2418 * Set flags so that all registers will be written,
2419 * and pass on write indication (used for PIO/DMA
2420 * setup.)
2421 */
2422 tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
2423
Jeff Garzik0274aa22005-06-22 13:50:56 -04002424 if (cmd->sc_data_direction == DMA_TO_DEVICE)
Jeff Garzikb0955182005-05-12 15:45:22 -04002425 tf->flags |= ATA_TFLAG_WRITE;
2426
2427 /*
2428 * Set transfer length.
2429 *
2430 * TODO: find out if we need to do more here to
2431 * cover scatter/gather case.
2432 */
2433 qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
2434
2435 return 0;
Tejun Heo9a405252005-12-02 11:49:11 +09002436
2437 invalid_fld:
2438 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x00);
2439 /* "Invalid field in cdb" */
2440 return 1;
Jeff Garzikb0955182005-05-12 15:45:22 -04002441}
2442
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443/**
2444 * ata_get_xlat_func - check if SCSI to ATA translation is possible
2445 * @dev: ATA device
2446 * @cmd: SCSI command opcode to consider
2447 *
2448 * Look up the SCSI command given, and determine whether the
2449 * SCSI command is to be translated or simulated.
2450 *
2451 * RETURNS:
2452 * Pointer to translation function if possible, %NULL if not.
2453 */
2454
2455static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
2456{
2457 switch (cmd) {
2458 case READ_6:
2459 case READ_10:
2460 case READ_16:
2461
2462 case WRITE_6:
2463 case WRITE_10:
2464 case WRITE_16:
2465 return ata_scsi_rw_xlat;
2466
2467 case SYNCHRONIZE_CACHE:
2468 if (ata_try_flush_cache(dev))
2469 return ata_scsi_flush_xlat;
2470 break;
2471
2472 case VERIFY:
2473 case VERIFY_16:
2474 return ata_scsi_verify_xlat;
Jeff Garzikb0955182005-05-12 15:45:22 -04002475
2476 case ATA_12:
2477 case ATA_16:
2478 return ata_scsi_pass_thru;
Jeff Garzikda613962005-08-29 19:01:43 -04002479
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04002480 case START_STOP:
2481 return ata_scsi_start_stop_xlat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482 }
2483
2484 return NULL;
2485}
2486
2487/**
2488 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
2489 * @ap: ATA port to which the command was being sent
2490 * @cmd: SCSI command to dump
2491 *
2492 * Prints the contents of a SCSI command via printk().
2493 */
2494
2495static inline void ata_scsi_dump_cdb(struct ata_port *ap,
2496 struct scsi_cmnd *cmd)
2497{
2498#ifdef ATA_DEBUG
2499 struct scsi_device *scsidev = cmd->device;
2500 u8 *scsicmd = cmd->cmnd;
2501
2502 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
2503 ap->id,
2504 scsidev->channel, scsidev->id, scsidev->lun,
2505 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
2506 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
2507 scsicmd[8]);
2508#endif
2509}
2510
2511/**
2512 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
2513 * @cmd: SCSI command to be sent
2514 * @done: Completion function, called when command is complete
2515 *
2516 * In some cases, this function translates SCSI commands into
2517 * ATA taskfiles, and queues the taskfiles to be sent to
2518 * hardware. In other cases, this function simulates a
2519 * SCSI device by evaluating and responding to certain
2520 * SCSI commands. This creates the overall effect of
2521 * ATA and ATAPI devices appearing as SCSI devices.
2522 *
2523 * LOCKING:
2524 * Releases scsi-layer-held lock, and obtains host_set lock.
2525 *
2526 * RETURNS:
2527 * Zero.
2528 */
2529
2530int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
2531{
2532 struct ata_port *ap;
2533 struct ata_device *dev;
2534 struct scsi_device *scsidev = cmd->device;
Jeff Garzik005a5a02005-10-30 23:31:48 -05002535 struct Scsi_Host *shost = scsidev->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536
Jeff Garzik005a5a02005-10-30 23:31:48 -05002537 ap = (struct ata_port *) &shost->hostdata[0];
2538
2539 spin_unlock(shost->host_lock);
2540 spin_lock(&ap->host_set->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002541
2542 ata_scsi_dump_cdb(ap, cmd);
2543
2544 dev = ata_scsi_find_dev(ap, scsidev);
2545 if (unlikely(!dev)) {
2546 cmd->result = (DID_BAD_TARGET << 16);
2547 done(cmd);
2548 goto out_unlock;
2549 }
2550
2551 if (dev->class == ATA_DEV_ATA) {
2552 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
2553 cmd->cmnd[0]);
2554
2555 if (xlat_func)
2556 ata_scsi_translate(ap, dev, cmd, done, xlat_func);
2557 else
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002558 ata_scsi_simulate(ap, dev, cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002559 } else
2560 ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
2561
2562out_unlock:
Jeff Garzik005a5a02005-10-30 23:31:48 -05002563 spin_unlock(&ap->host_set->lock);
2564 spin_lock(shost->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 return 0;
2566}
2567
2568/**
2569 * ata_scsi_simulate - simulate SCSI command on ATA device
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002570 * @ap: port the device is connected to
2571 * @dev: the target device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 * @cmd: SCSI command being sent to device.
2573 * @done: SCSI command completion function.
2574 *
2575 * Interprets and directly executes a select list of SCSI commands
2576 * that can be handled internally.
2577 *
2578 * LOCKING:
2579 * spin_lock_irqsave(host_set lock)
2580 */
2581
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002582void ata_scsi_simulate(struct ata_port *ap, struct ata_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 struct scsi_cmnd *cmd,
2584 void (*done)(struct scsi_cmnd *))
2585{
2586 struct ata_scsi_args args;
Jeff Garzik057ace52005-10-22 14:27:05 -04002587 const u8 *scsicmd = cmd->cmnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002588
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002589 args.ap = ap;
2590 args.dev = dev;
2591 args.id = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 args.cmd = cmd;
2593 args.done = done;
2594
2595 switch(scsicmd[0]) {
2596 /* no-op's, complete with success */
2597 case SYNCHRONIZE_CACHE:
2598 case REZERO_UNIT:
2599 case SEEK_6:
2600 case SEEK_10:
2601 case TEST_UNIT_READY:
2602 case FORMAT_UNIT: /* FIXME: correct? */
2603 case SEND_DIAGNOSTIC: /* FIXME: correct? */
2604 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
2605 break;
2606
2607 case INQUIRY:
2608 if (scsicmd[1] & 2) /* is CmdDt set? */
Douglas Gilbertae006512005-10-09 09:09:35 -04002609 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
2611 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
2612 else if (scsicmd[2] == 0x00)
2613 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
2614 else if (scsicmd[2] == 0x80)
2615 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
2616 else if (scsicmd[2] == 0x83)
2617 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
2618 else
Douglas Gilbertae006512005-10-09 09:09:35 -04002619 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 break;
2621
2622 case MODE_SENSE:
2623 case MODE_SENSE_10:
2624 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
2625 break;
2626
2627 case MODE_SELECT: /* unconditionally return */
2628 case MODE_SELECT_10: /* bad-field-in-cdb */
Douglas Gilbertae006512005-10-09 09:09:35 -04002629 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 break;
2631
2632 case READ_CAPACITY:
2633 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2634 break;
2635
2636 case SERVICE_ACTION_IN:
2637 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
2638 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2639 else
Douglas Gilbertae006512005-10-09 09:09:35 -04002640 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002641 break;
2642
2643 case REPORT_LUNS:
2644 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
2645 break;
2646
Jeff Garzikb0955182005-05-12 15:45:22 -04002647 /* mandatory commands we haven't implemented yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 case REQUEST_SENSE:
2649
2650 /* all other commands */
2651 default:
Douglas Gilbertae006512005-10-09 09:09:35 -04002652 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
2653 /* "Invalid command operation code" */
2654 done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002655 break;
2656 }
2657}
2658
Jeff Garzik644dd0c2005-10-03 15:55:19 -04002659void ata_scsi_scan_host(struct ata_port *ap)
2660{
Jeff Garzik3f19ee82005-10-03 21:36:41 -04002661 struct ata_device *dev;
Jeff Garzik644dd0c2005-10-03 15:55:19 -04002662 unsigned int i;
2663
2664 if (ap->flags & ATA_FLAG_PORT_DISABLED)
2665 return;
2666
Jeff Garzik3f19ee82005-10-03 21:36:41 -04002667 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2668 dev = &ap->device[i];
2669
2670 if (ata_dev_present(dev))
2671 scsi_scan_target(&ap->host->shost_gendev, 0, i, 0, 0);
2672 }
Jeff Garzik644dd0c2005-10-03 15:55:19 -04002673}
2674