blob: a9b4083a4f67903ac4826824049089392b95f7fa [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>
Tejun Heo30afc842006-03-18 18:40:14 +090044#include <scsi/scsi_transport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/libata.h>
Jeff Garzikb0955182005-05-12 15:45:22 -040046#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <asm/uaccess.h>
48
49#include "libata.h"
50
Jeff Garzikb0955182005-05-12 15:45:22 -040051#define SECTOR_SIZE 512
52
Jeff Garzik057ace52005-10-22 14:27:05 -040053typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, const u8 *scsicmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static struct ata_device *
Jeff Garzik057ace52005-10-22 14:27:05 -040055ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040057#define RW_RECOVERY_MPAGE 0x1
58#define RW_RECOVERY_MPAGE_LEN 12
59#define CACHE_MPAGE 0x8
60#define CACHE_MPAGE_LEN 20
61#define CONTROL_MPAGE 0xa
62#define CONTROL_MPAGE_LEN 12
63#define ALL_MPAGES 0x3f
64#define ALL_SUB_MPAGES 0xff
65
66
67static const u8 def_rw_recovery_mpage[] = {
68 RW_RECOVERY_MPAGE,
69 RW_RECOVERY_MPAGE_LEN - 2,
70 (1 << 7) | /* AWRE, sat-r06 say it shall be 0 */
71 (1 << 6), /* ARRE (auto read reallocation) */
72 0, /* read retry count */
73 0, 0, 0, 0,
74 0, /* write retry count */
75 0, 0, 0
76};
77
78static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
79 CACHE_MPAGE,
80 CACHE_MPAGE_LEN - 2,
81 0, /* contains WCE, needs to be 0 for logic */
82 0, 0, 0, 0, 0, 0, 0, 0, 0,
83 0, /* contains DRA, needs to be 0 for logic */
84 0, 0, 0, 0, 0, 0, 0
85};
86
87static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
88 CONTROL_MPAGE,
89 CONTROL_MPAGE_LEN - 2,
90 2, /* DSENSE=0, GLTSD=1 */
91 0, /* [QAM+QERR may be 1, see 05-359r1] */
92 0, 0, 0, 0, 0xff, 0xff,
93 0, 30 /* extended self test time, see 05-359r1 */
94};
95
Tejun Heo30afc842006-03-18 18:40:14 +090096/*
97 * libata transport template. libata doesn't do real transport stuff.
98 * It just needs the eh_timed_out hook.
99 */
100struct scsi_transport_template ata_scsi_transport_template = {
Christoph Hellwig9227c332006-04-01 19:21:04 +0200101 .eh_strategy_handler = ata_scsi_error,
Tejun Heo30afc842006-03-18 18:40:14 +0900102 .eh_timed_out = ata_scsi_timed_out,
103};
104
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Douglas Gilbertae006512005-10-09 09:09:35 -0400106static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
107 void (*done)(struct scsi_cmnd *))
108{
109 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
110 /* "Invalid field in cbd" */
111 done(cmd);
112}
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114/**
115 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
116 * @sdev: SCSI device for which BIOS geometry is to be determined
117 * @bdev: block device associated with @sdev
118 * @capacity: capacity of SCSI device
119 * @geom: location to which geometry will be output
120 *
121 * Generic bios head/sector/cylinder calculator
122 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
123 * mapping. Some situations may arise where the disk is not
124 * bootable if this is not used.
125 *
126 * LOCKING:
127 * Defined by the SCSI layer. We don't really care.
128 *
129 * RETURNS:
130 * Zero.
131 */
132int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
133 sector_t capacity, int geom[])
134{
135 geom[0] = 255;
136 geom[1] = 63;
137 sector_div(capacity, 255*63);
138 geom[2] = capacity;
139
140 return 0;
141}
142
Jeff Garzikb0955182005-05-12 15:45:22 -0400143/**
144 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800145 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400146 * @arg: User provided data for issuing command
147 *
148 * LOCKING:
149 * Defined by the SCSI layer. We don't really care.
150 *
151 * RETURNS:
152 * Zero on success, negative errno on error.
153 */
154
155int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
156{
157 int rc = 0;
158 u8 scsi_cmd[MAX_COMMAND_SIZE];
159 u8 args[4], *argbuf = NULL;
160 int argsize = 0;
Mike Christie85837eb2005-11-11 16:38:53 -0600161 struct scsi_sense_hdr sshdr;
162 enum dma_data_direction data_dir;
Jeff Garzikb0955182005-05-12 15:45:22 -0400163
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500164 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400165 return -EINVAL;
166
167 if (copy_from_user(args, arg, sizeof(args)))
168 return -EFAULT;
169
Jeff Garzikb0955182005-05-12 15:45:22 -0400170 memset(scsi_cmd, 0, sizeof(scsi_cmd));
171
172 if (args[3]) {
173 argsize = SECTOR_SIZE * args[3];
174 argbuf = kmalloc(argsize, GFP_KERNEL);
Jeff Raubitschek54dac832005-10-04 10:21:19 -0400175 if (argbuf == NULL) {
176 rc = -ENOMEM;
177 goto error;
178 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400179
180 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
181 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
182 block count in sector count field */
Mike Christie85837eb2005-11-11 16:38:53 -0600183 data_dir = DMA_FROM_DEVICE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400184 } else {
185 scsi_cmd[1] = (3 << 1); /* Non-data */
186 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
Mike Christie85837eb2005-11-11 16:38:53 -0600187 data_dir = DMA_NONE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400188 }
189
190 scsi_cmd[0] = ATA_16;
191
192 scsi_cmd[4] = args[2];
193 if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
194 scsi_cmd[6] = args[3];
195 scsi_cmd[8] = args[1];
196 scsi_cmd[10] = 0x4f;
197 scsi_cmd[12] = 0xc2;
198 } else {
199 scsi_cmd[6] = args[1];
200 }
201 scsi_cmd[14] = args[0];
202
203 /* Good values for timeout and retries? Values below
204 from scsi_ioctl_send_command() for default case... */
Mike Christie85837eb2005-11-11 16:38:53 -0600205 if (scsi_execute_req(scsidev, scsi_cmd, data_dir, argbuf, argsize,
206 &sshdr, (10*HZ), 5)) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400207 rc = -EIO;
208 goto error;
209 }
210
211 /* Need code to retrieve data from check condition? */
212
213 if ((argbuf)
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500214 && copy_to_user(arg + sizeof(args), argbuf, argsize))
Jeff Garzikb0955182005-05-12 15:45:22 -0400215 rc = -EFAULT;
216error:
Jeff Garzikb0955182005-05-12 15:45:22 -0400217 if (argbuf)
218 kfree(argbuf);
219
220 return rc;
221}
222
223/**
224 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800225 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400226 * @arg: User provided data for issuing command
227 *
228 * LOCKING:
229 * Defined by the SCSI layer. We don't really care.
230 *
231 * RETURNS:
232 * Zero on success, negative errno on error.
233 */
234int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
235{
236 int rc = 0;
237 u8 scsi_cmd[MAX_COMMAND_SIZE];
238 u8 args[7];
Mike Christie85837eb2005-11-11 16:38:53 -0600239 struct scsi_sense_hdr sshdr;
Jeff Garzikb0955182005-05-12 15:45:22 -0400240
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500241 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400242 return -EINVAL;
243
244 if (copy_from_user(args, arg, sizeof(args)))
245 return -EFAULT;
246
247 memset(scsi_cmd, 0, sizeof(scsi_cmd));
248 scsi_cmd[0] = ATA_16;
249 scsi_cmd[1] = (3 << 1); /* Non-data */
250 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
251 scsi_cmd[4] = args[1];
252 scsi_cmd[6] = args[2];
253 scsi_cmd[8] = args[3];
254 scsi_cmd[10] = args[4];
255 scsi_cmd[12] = args[5];
256 scsi_cmd[14] = args[0];
257
Jeff Garzikb0955182005-05-12 15:45:22 -0400258 /* Good values for timeout and retries? Values below
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500259 from scsi_ioctl_send_command() for default case... */
Mike Christie85837eb2005-11-11 16:38:53 -0600260 if (scsi_execute_req(scsidev, scsi_cmd, DMA_NONE, NULL, 0, &sshdr,
261 (10*HZ), 5))
Jeff Garzikb0955182005-05-12 15:45:22 -0400262 rc = -EIO;
Jeff Garzikb0955182005-05-12 15:45:22 -0400263
264 /* Need code to retrieve data from check condition? */
Jeff Garzikb0955182005-05-12 15:45:22 -0400265 return rc;
266}
267
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
269{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 int val = -EINVAL, rc = -EINVAL;
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 switch (cmd) {
273 case ATA_IOC_GET_IO32:
274 val = 0;
275 if (copy_to_user(arg, &val, 1))
276 return -EFAULT;
277 return 0;
278
279 case ATA_IOC_SET_IO32:
280 val = (unsigned long) arg;
281 if (val != 0)
282 return -EINVAL;
283 return 0;
284
Jeff Garzikb0955182005-05-12 15:45:22 -0400285 case HDIO_DRIVE_CMD:
286 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
287 return -EACCES;
288 return ata_cmd_ioctl(scsidev, arg);
289
290 case HDIO_DRIVE_TASK:
291 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
292 return -EACCES;
293 return ata_task_ioctl(scsidev, arg);
294
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 default:
296 rc = -ENOTTY;
297 break;
298 }
299
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300 return rc;
301}
302
303/**
304 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 * @dev: ATA device to which the new command is attached
306 * @cmd: SCSI command that originated this ATA command
307 * @done: SCSI command completion function
308 *
309 * Obtain a reference to an unused ata_queued_cmd structure,
310 * which is the basic libata structure representing a single
311 * ATA command sent to the hardware.
312 *
313 * If a command was available, fill in the SCSI-specific
314 * portions of the structure with information on the
315 * current command.
316 *
317 * LOCKING:
318 * spin_lock_irqsave(host_set lock)
319 *
320 * RETURNS:
321 * Command allocated, or %NULL if none available.
322 */
Tejun Heo3373efd2006-05-15 20:57:53 +0900323struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 struct scsi_cmnd *cmd,
325 void (*done)(struct scsi_cmnd *))
326{
327 struct ata_queued_cmd *qc;
328
Tejun Heo3373efd2006-05-15 20:57:53 +0900329 qc = ata_qc_new_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 if (qc) {
331 qc->scsicmd = cmd;
332 qc->scsidone = done;
333
334 if (cmd->use_sg) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -0400335 qc->__sg = (struct scatterlist *) cmd->request_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 qc->n_elem = cmd->use_sg;
337 } else {
Jeff Garzikcedc9a42005-10-05 07:13:30 -0400338 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 qc->n_elem = 1;
340 }
341 } else {
342 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
343 done(cmd);
344 }
345
346 return qc;
347}
348
349/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400350 * ata_dump_status - user friendly display of error info
351 * @id: id of the port in question
352 * @tf: ptr to filled out taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 *
Jeff Garzikb0955182005-05-12 15:45:22 -0400354 * Decode and dump the ATA error/status registers for the user so
355 * that they have some idea what really happened at the non
356 * make-believe layer.
357 *
358 * LOCKING:
359 * inherited from caller
360 */
361void ata_dump_status(unsigned id, struct ata_taskfile *tf)
362{
363 u8 stat = tf->command, err = tf->feature;
364
365 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
366 if (stat & ATA_BUSY) {
367 printk("Busy }\n"); /* Data is not valid in this case */
368 } else {
369 if (stat & 0x40) printk("DriveReady ");
370 if (stat & 0x20) printk("DeviceFault ");
371 if (stat & 0x10) printk("SeekComplete ");
372 if (stat & 0x08) printk("DataRequest ");
373 if (stat & 0x04) printk("CorrectedError ");
374 if (stat & 0x02) printk("Index ");
375 if (stat & 0x01) printk("Error ");
376 printk("}\n");
377
378 if (err) {
379 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
380 if (err & 0x04) printk("DriveStatusError ");
381 if (err & 0x80) {
382 if (err & 0x04) printk("BadCRC ");
383 else printk("Sector ");
384 }
385 if (err & 0x40) printk("UncorrectableError ");
386 if (err & 0x10) printk("SectorIdNotFound ");
387 if (err & 0x02) printk("TrackZeroNotFound ");
388 if (err & 0x01) printk("AddrMarkNotFound ");
389 printk("}\n");
390 }
391 }
392}
393
Jens Axboe9b847542006-01-06 09:28:07 +0100394int ata_scsi_device_resume(struct scsi_device *sdev)
395{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400396 struct ata_port *ap = ata_shost_to_port(sdev->host);
Jens Axboe9b847542006-01-06 09:28:07 +0100397 struct ata_device *dev = &ap->device[sdev->id];
398
Tejun Heo3373efd2006-05-15 20:57:53 +0900399 return ata_device_resume(dev);
Jens Axboe9b847542006-01-06 09:28:07 +0100400}
401
Nigel Cunningham082776e2006-03-23 23:22:16 +1000402int ata_scsi_device_suspend(struct scsi_device *sdev, pm_message_t state)
Jens Axboe9b847542006-01-06 09:28:07 +0100403{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400404 struct ata_port *ap = ata_shost_to_port(sdev->host);
Jens Axboe9b847542006-01-06 09:28:07 +0100405 struct ata_device *dev = &ap->device[sdev->id];
406
Tejun Heo3373efd2006-05-15 20:57:53 +0900407 return ata_device_suspend(dev, state);
Jens Axboe9b847542006-01-06 09:28:07 +0100408}
409
Jeff Garzikb0955182005-05-12 15:45:22 -0400410/**
411 * ata_to_sense_error - convert ATA error to SCSI error
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800412 * @id: ATA device number
Jeff Garzikb0955182005-05-12 15:45:22 -0400413 * @drv_stat: value contained in ATA status register
414 * @drv_err: value contained in ATA error register
415 * @sk: the sense key we'll fill out
416 * @asc: the additional sense code we'll fill out
417 * @ascq: the additional sense code qualifier we'll fill out
418 *
419 * Converts an ATA error into a SCSI error. Fill out pointers to
420 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
421 * format sense blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 *
423 * LOCKING:
424 * spin_lock_irqsave(host_set lock)
425 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500426void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
Jeff Garzikb0955182005-05-12 15:45:22 -0400427 u8 *ascq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428{
Jeff Garzikb0955182005-05-12 15:45:22 -0400429 int i;
Jeff Garzikffe75ef2005-10-09 10:40:44 -0400430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 /* Based on the 3ware driver translation table */
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100432 static const unsigned char sense_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 /* BBD|ECC|ID|MAR */
434 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
435 /* BBD|ECC|ID */
436 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
437 /* ECC|MC|MARK */
438 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
439 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
440 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
441 /* MC|ID|ABRT|TRK0|MARK */
442 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
443 /* MCR|MARK */
444 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
445 /* Bad address mark */
446 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
447 /* TRK0 */
448 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
449 /* Abort & !ICRC */
450 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
451 /* Media change request */
452 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
453 /* SRV */
454 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
455 /* Media change */
456 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
457 /* ECC */
458 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
459 /* BBD - block marked bad */
460 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
461 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
462 };
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100463 static const unsigned char stat_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /* Must be first because BUSY means no other bits valid */
465 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
466 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
467 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
468 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
469 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
470 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 /*
473 * Is this an error we can process/parse
474 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400475 if (drv_stat & ATA_BUSY) {
476 drv_err = 0; /* Ignore the err bits, they're invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
Jeff Garzikb0955182005-05-12 15:45:22 -0400479 if (drv_err) {
480 /* Look for drv_err */
481 for (i = 0; sense_table[i][0] != 0xFF; i++) {
482 /* Look for best matches first */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500483 if ((sense_table[i][0] & drv_err) ==
Jeff Garzikb0955182005-05-12 15:45:22 -0400484 sense_table[i][0]) {
485 *sk = sense_table[i][1];
486 *asc = sense_table[i][2];
487 *ascq = sense_table[i][3];
488 goto translate_done;
489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400491 /* No immediate match */
492 printk(KERN_WARNING "ata%u: no sense translation for "
493 "error 0x%02x\n", id, drv_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 }
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /* Fall back to interpreting status bits */
Jeff Garzikb0955182005-05-12 15:45:22 -0400497 for (i = 0; stat_table[i][0] != 0xFF; i++) {
498 if (stat_table[i][0] & drv_stat) {
499 *sk = stat_table[i][1];
500 *asc = stat_table[i][2];
501 *ascq = stat_table[i][3];
502 goto translate_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400505 /* No error? Undecoded? */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500506 printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n",
Jeff Garzikb0955182005-05-12 15:45:22 -0400507 id, drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Alan Cox2d202022006-03-21 15:53:46 +0000509 /* We need a sensible error return here, which is tricky, and one
510 that won't cause people to do things like return a disk wrongly */
511 *sk = ABORTED_COMMAND;
512 *asc = 0x00;
513 *ascq = 0x00;
Jeff Garzikb0955182005-05-12 15:45:22 -0400514
515 translate_done:
516 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to "
517 "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err,
518 *sk, *asc, *ascq);
519 return;
520}
521
522/*
523 * ata_gen_ata_desc_sense - Generate check condition sense block.
524 * @qc: Command that completed.
525 *
526 * This function is specific to the ATA descriptor format sense
527 * block specified for the ATA pass through commands. Regardless
528 * of whether the command errored or not, return a sense
529 * block. Copy all controller registers into the sense
530 * block. Clear sense key, ASC & ASCQ if there is no error.
531 *
532 * LOCKING:
533 * spin_lock_irqsave(host_set lock)
534 */
535void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
536{
537 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +0900538 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -0400539 unsigned char *sb = cmd->sense_buffer;
540 unsigned char *desc = sb + 8;
541
542 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
543
Jeff Garzik0e5dec42005-10-06 09:40:20 -0400544 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -0400545
546 /*
Jeff Garzikb0955182005-05-12 15:45:22 -0400547 * Use ata_to_sense_error() to map status register bits
548 * onto sense key, asc & ascq.
549 */
Tejun Heo058e55e2006-04-02 18:51:53 +0900550 if (qc->err_mask ||
551 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400552 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
553 &sb[1], &sb[2], &sb[3]);
554 sb[1] &= 0x0f;
555 }
556
557 /*
558 * Sense data is current and format is descriptor.
559 */
560 sb[0] = 0x72;
561
562 desc[0] = 0x09;
563
564 /*
565 * Set length of additional sense data.
566 * Since we only populate descriptor 0, the total
567 * length is the same (fixed) length as descriptor 0.
568 */
569 desc[1] = sb[7] = 14;
570
571 /*
572 * Copy registers into sense buffer.
573 */
574 desc[2] = 0x00;
575 desc[3] = tf->feature; /* == error reg */
576 desc[5] = tf->nsect;
577 desc[7] = tf->lbal;
578 desc[9] = tf->lbam;
579 desc[11] = tf->lbah;
580 desc[12] = tf->device;
581 desc[13] = tf->command; /* == status reg */
582
583 /*
584 * Fill in Extend bit, and the high order bytes
585 * if applicable.
586 */
587 if (tf->flags & ATA_TFLAG_LBA48) {
588 desc[2] |= 0x01;
589 desc[4] = tf->hob_nsect;
590 desc[6] = tf->hob_lbal;
591 desc[8] = tf->hob_lbam;
592 desc[10] = tf->hob_lbah;
593 }
594}
595
596/**
597 * ata_gen_fixed_sense - generate a SCSI fixed sense block
598 * @qc: Command that we are erroring out
599 *
600 * Leverage ata_to_sense_error() to give us the codes. Fit our
601 * LBA in here if there's room.
602 *
603 * LOCKING:
604 * inherited from caller
605 */
606void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
607{
608 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +0900609 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -0400610 unsigned char *sb = cmd->sense_buffer;
611
612 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
613
Jeff Garzik0e5dec42005-10-06 09:40:20 -0400614 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -0400615
616 /*
Jeff Garzikb0955182005-05-12 15:45:22 -0400617 * Use ata_to_sense_error() to map status register bits
618 * onto sense key, asc & ascq.
619 */
Tejun Heo058e55e2006-04-02 18:51:53 +0900620 if (qc->err_mask ||
621 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400622 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
623 &sb[2], &sb[12], &sb[13]);
624 sb[2] &= 0x0f;
625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 sb[0] = 0x70;
Jeff Garzikb0955182005-05-12 15:45:22 -0400628 sb[7] = 0x0a;
Jeff Garzik0274aa22005-06-22 13:50:56 -0400629
Jeff Garzika7dac442005-10-30 04:44:42 -0500630 if (tf->flags & ATA_TFLAG_LBA48) {
631 /* TODO: find solution for LBA48 descriptors */
632 }
633
634 else if (tf->flags & ATA_TFLAG_LBA) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400635 /* A small (28b) LBA will fit in the 32b info field */
636 sb[0] |= 0x80; /* set valid bit */
637 sb[3] = tf->device & 0x0f;
638 sb[4] = tf->lbah;
639 sb[5] = tf->lbam;
640 sb[6] = tf->lbal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 }
Jeff Garzika7dac442005-10-30 04:44:42 -0500642
643 else {
644 /* TODO: C/H/S */
645 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Brian Kinga6cce2a2006-03-17 17:04:15 -0600648static void ata_scsi_sdev_config(struct scsi_device *sdev)
649{
650 sdev->use_10_for_rw = 1;
651 sdev->use_10_for_ms = 1;
652}
653
654static void ata_scsi_dev_config(struct scsi_device *sdev,
655 struct ata_device *dev)
656{
657 unsigned int max_sectors;
658
659 /* TODO: 2048 is an arbitrary number, not the
660 * hardware maximum. This should be increased to
661 * 65534 when Jens Axboe's patch for dynamically
662 * determining max_sectors is merged.
663 */
664 max_sectors = ATA_MAX_SECTORS;
665 if (dev->flags & ATA_DFLAG_LBA48)
666 max_sectors = 2048;
667 if (dev->max_sectors)
668 max_sectors = dev->max_sectors;
669
670 blk_queue_max_sectors(sdev->request_queue, max_sectors);
671
672 /*
673 * SATA DMA transfers must be multiples of 4 byte, so
674 * we need to pad ATAPI transfers using an extra sg.
675 * Decrement max hw segments accordingly.
676 */
677 if (dev->class == ATA_DEV_ATAPI) {
678 request_queue_t *q = sdev->request_queue;
679 blk_queue_max_hw_segments(q, q->max_hw_segments - 1);
680 }
681}
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683/**
684 * ata_scsi_slave_config - Set SCSI device attributes
685 * @sdev: SCSI device to examine
686 *
687 * This is called before we actually start reading
688 * and writing to the device, to configure certain
689 * SCSI mid-layer behaviors.
690 *
691 * LOCKING:
692 * Defined by SCSI layer. We don't really care.
693 */
694
695int ata_scsi_slave_config(struct scsi_device *sdev)
696{
Brian Kinga6cce2a2006-03-17 17:04:15 -0600697 ata_scsi_sdev_config(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698
699 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
700
701 if (sdev->id < ATA_MAX_DEVICES) {
702 struct ata_port *ap;
703 struct ata_device *dev;
704
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400705 ap = ata_shost_to_port(sdev->host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 dev = &ap->device[sdev->id];
707
Brian Kinga6cce2a2006-03-17 17:04:15 -0600708 ata_scsi_dev_config(sdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
710
711 return 0; /* scsi layer doesn't check return value, sigh */
712}
713
714/**
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400715 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
716 * @qc: Storage for translated ATA taskfile
717 * @scsicmd: SCSI command to translate
718 *
719 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
720 * (to start). Perhaps these commands should be preceded by
721 * CHECK POWER MODE to see what power mode the device is already in.
722 * [See SAT revision 5 at www.t10.org]
723 *
724 * LOCKING:
725 * spin_lock_irqsave(host_set lock)
726 *
727 * RETURNS:
728 * Zero on success, non-zero on error.
729 */
730
731static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
Jeff Garzik057ace52005-10-22 14:27:05 -0400732 const u8 *scsicmd)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400733{
734 struct ata_taskfile *tf = &qc->tf;
735
736 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
737 tf->protocol = ATA_PROT_NODATA;
738 if (scsicmd[1] & 0x1) {
739 ; /* ignore IMMED bit, violates sat-r05 */
740 }
741 if (scsicmd[4] & 0x2)
Douglas Gilbertae006512005-10-09 09:09:35 -0400742 goto invalid_fld; /* LOEJ bit set not supported */
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400743 if (((scsicmd[4] >> 4) & 0xf) != 0)
Douglas Gilbertae006512005-10-09 09:09:35 -0400744 goto invalid_fld; /* power conditions not supported */
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400745 if (scsicmd[4] & 0x1) {
746 tf->nsect = 1; /* 1 sector, lba=0 */
Albert Lee9d5b1302005-10-04 08:48:17 -0400747
748 if (qc->dev->flags & ATA_DFLAG_LBA) {
Tejun Heoc44078c2006-05-15 20:57:21 +0900749 tf->flags |= ATA_TFLAG_LBA;
Albert Lee9d5b1302005-10-04 08:48:17 -0400750
751 tf->lbah = 0x0;
752 tf->lbam = 0x0;
753 tf->lbal = 0x0;
754 tf->device |= ATA_LBA;
755 } else {
756 /* CHS */
757 tf->lbal = 0x1; /* sect */
758 tf->lbam = 0x0; /* cyl low */
759 tf->lbah = 0x0; /* cyl high */
760 }
761
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400762 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
763 } else {
764 tf->nsect = 0; /* time period value (0 implies now) */
765 tf->command = ATA_CMD_STANDBY;
766 /* Consider: ATA STANDBY IMMEDIATE command */
767 }
768 /*
769 * Standby and Idle condition timers could be implemented but that
770 * would require libata to implement the Power condition mode page
771 * and allow the user to change it. Changing mode pages requires
772 * MODE SELECT to be implemented.
773 */
774
775 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -0400776
777invalid_fld:
778 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
779 /* "Invalid field in cbd" */
780 return 1;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400781}
782
783
784/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
786 * @qc: Storage for translated ATA taskfile
787 * @scsicmd: SCSI command to translate (ignored)
788 *
789 * Sets up an ATA taskfile to issue FLUSH CACHE or
790 * FLUSH CACHE EXT.
791 *
792 * LOCKING:
793 * spin_lock_irqsave(host_set lock)
794 *
795 * RETURNS:
796 * Zero on success, non-zero on error.
797 */
798
Jeff Garzik057ace52005-10-22 14:27:05 -0400799static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800{
801 struct ata_taskfile *tf = &qc->tf;
802
803 tf->flags |= ATA_TFLAG_DEVICE;
804 tf->protocol = ATA_PROT_NODATA;
805
Albert Lee07506692005-10-12 15:04:18 +0800806 if ((qc->dev->flags & ATA_DFLAG_LBA48) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 (ata_id_has_flush_ext(qc->dev->id)))
808 tf->command = ATA_CMD_FLUSH_EXT;
809 else
810 tf->command = ATA_CMD_FLUSH;
811
812 return 0;
813}
814
815/**
Albert Lee3aef5232005-10-04 08:47:43 -0400816 * scsi_6_lba_len - Get LBA and transfer length
817 * @scsicmd: SCSI command to translate
818 *
819 * Calculate LBA and transfer length for 6-byte commands.
820 *
821 * RETURNS:
822 * @plba: the LBA
823 * @plen: the transfer length
824 */
825
Jeff Garzik057ace52005-10-22 14:27:05 -0400826static void scsi_6_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -0400827{
828 u64 lba = 0;
829 u32 len = 0;
830
831 VPRINTK("six-byte command\n");
832
833 lba |= ((u64)scsicmd[2]) << 8;
834 lba |= ((u64)scsicmd[3]);
835
836 len |= ((u32)scsicmd[4]);
837
838 *plba = lba;
839 *plen = len;
840}
841
842/**
843 * scsi_10_lba_len - Get LBA and transfer length
844 * @scsicmd: SCSI command to translate
845 *
846 * Calculate LBA and transfer length for 10-byte commands.
847 *
848 * RETURNS:
849 * @plba: the LBA
850 * @plen: the transfer length
851 */
852
Jeff Garzik057ace52005-10-22 14:27:05 -0400853static void scsi_10_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -0400854{
855 u64 lba = 0;
856 u32 len = 0;
857
858 VPRINTK("ten-byte command\n");
859
860 lba |= ((u64)scsicmd[2]) << 24;
861 lba |= ((u64)scsicmd[3]) << 16;
862 lba |= ((u64)scsicmd[4]) << 8;
863 lba |= ((u64)scsicmd[5]);
864
865 len |= ((u32)scsicmd[7]) << 8;
866 len |= ((u32)scsicmd[8]);
867
868 *plba = lba;
869 *plen = len;
870}
871
872/**
873 * scsi_16_lba_len - Get LBA and transfer length
874 * @scsicmd: SCSI command to translate
875 *
876 * Calculate LBA and transfer length for 16-byte commands.
877 *
878 * RETURNS:
879 * @plba: the LBA
880 * @plen: the transfer length
881 */
882
Jeff Garzik057ace52005-10-22 14:27:05 -0400883static void scsi_16_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -0400884{
885 u64 lba = 0;
886 u32 len = 0;
887
888 VPRINTK("sixteen-byte command\n");
889
890 lba |= ((u64)scsicmd[2]) << 56;
891 lba |= ((u64)scsicmd[3]) << 48;
892 lba |= ((u64)scsicmd[4]) << 40;
893 lba |= ((u64)scsicmd[5]) << 32;
894 lba |= ((u64)scsicmd[6]) << 24;
895 lba |= ((u64)scsicmd[7]) << 16;
896 lba |= ((u64)scsicmd[8]) << 8;
897 lba |= ((u64)scsicmd[9]);
898
899 len |= ((u32)scsicmd[10]) << 24;
900 len |= ((u32)scsicmd[11]) << 16;
901 len |= ((u32)scsicmd[12]) << 8;
902 len |= ((u32)scsicmd[13]);
903
904 *plba = lba;
905 *plen = len;
906}
907
908/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
910 * @qc: Storage for translated ATA taskfile
911 * @scsicmd: SCSI command to translate
912 *
913 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
914 *
915 * LOCKING:
916 * spin_lock_irqsave(host_set lock)
917 *
918 * RETURNS:
919 * Zero on success, non-zero on error.
920 */
921
Jeff Garzik057ace52005-10-22 14:27:05 -0400922static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923{
924 struct ata_taskfile *tf = &qc->tf;
Albert Lee8bf62ece2005-05-12 15:29:42 -0400925 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 u64 dev_sectors = qc->dev->n_sectors;
Albert Lee3aef5232005-10-04 08:47:43 -0400927 u64 block;
928 u32 n_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929
930 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
931 tf->protocol = ATA_PROT_NODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932
Albert Lee3aef5232005-10-04 08:47:43 -0400933 if (scsicmd[0] == VERIFY)
934 scsi_10_lba_len(scsicmd, &block, &n_block);
935 else if (scsicmd[0] == VERIFY_16)
936 scsi_16_lba_len(scsicmd, &block, &n_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 else
Douglas Gilbertae006512005-10-09 09:09:35 -0400938 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Albert Lee8bf62ece2005-05-12 15:29:42 -0400940 if (!n_block)
Douglas Gilbertae006512005-10-09 09:09:35 -0400941 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -0400942 if (block >= dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -0400943 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -0400944 if ((block + n_block) > dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -0400945 goto out_of_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700946
Albert Lee07506692005-10-12 15:04:18 +0800947 if (dev->flags & ATA_DFLAG_LBA) {
948 tf->flags |= ATA_TFLAG_LBA;
949
Albert Leec6a33e22005-10-12 15:12:26 +0800950 if (lba_28_ok(block, n_block)) {
951 /* use LBA28 */
952 tf->command = ATA_CMD_VERIFY;
953 tf->device |= (block >> 24) & 0xf;
954 } else if (lba_48_ok(block, n_block)) {
955 if (!(dev->flags & ATA_DFLAG_LBA48))
956 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +0800957
958 /* use LBA48 */
959 tf->flags |= ATA_TFLAG_LBA48;
Albert Lee8bf62ece2005-05-12 15:29:42 -0400960 tf->command = ATA_CMD_VERIFY_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Albert Lee8bf62ece2005-05-12 15:29:42 -0400962 tf->hob_nsect = (n_block >> 8) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963
Albert Lee8bf62ece2005-05-12 15:29:42 -0400964 tf->hob_lbah = (block >> 40) & 0xff;
965 tf->hob_lbam = (block >> 32) & 0xff;
966 tf->hob_lbal = (block >> 24) & 0xff;
Albert Leec6a33e22005-10-12 15:12:26 +0800967 } else
968 /* request too large even for LBA48 */
969 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -0400970
971 tf->nsect = n_block & 0xff;
972
973 tf->lbah = (block >> 16) & 0xff;
974 tf->lbam = (block >> 8) & 0xff;
975 tf->lbal = block & 0xff;
976
977 tf->device |= ATA_LBA;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -0400979 /* CHS */
980 u32 sect, head, cyl, track;
981
Albert Leec6a33e22005-10-12 15:12:26 +0800982 if (!lba_28_ok(block, n_block))
983 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +0800984
Albert Lee8bf62ece2005-05-12 15:29:42 -0400985 /* Convert LBA to CHS */
986 track = (u32)block / dev->sectors;
987 cyl = track / dev->heads;
988 head = track % dev->heads;
989 sect = (u32)block % dev->sectors + 1;
990
Albert Leec187c4b2005-10-04 08:46:51 -0400991 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
992 (u32)block, track, cyl, head, sect);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500993
994 /* Check whether the converted CHS can fit.
995 Cylinder: 0-65535
Albert Lee8bf62ece2005-05-12 15:29:42 -0400996 Head: 0-15
997 Sector: 1-255*/
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500998 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
Douglas Gilbertae006512005-10-09 09:09:35 -0400999 goto out_of_range;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001000
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 tf->command = ATA_CMD_VERIFY;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001002 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1003 tf->lbal = sect;
1004 tf->lbam = cyl;
1005 tf->lbah = cyl >> 8;
1006 tf->device |= head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001010
1011invalid_fld:
1012 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
1013 /* "Invalid field in cbd" */
1014 return 1;
1015
1016out_of_range:
1017 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
1018 /* "Logical Block Address out of range" */
1019 return 1;
1020
1021nothing_to_do:
1022 qc->scsicmd->result = SAM_STAT_GOOD;
1023 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024}
1025
1026/**
1027 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1028 * @qc: Storage for translated ATA taskfile
1029 * @scsicmd: SCSI command to translate
1030 *
1031 * Converts any of six SCSI read/write commands into the
1032 * ATA counterpart, including starting sector (LBA),
1033 * sector count, and taking into account the device's LBA48
1034 * support.
1035 *
1036 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1037 * %WRITE_16 are currently supported.
1038 *
1039 * LOCKING:
1040 * spin_lock_irqsave(host_set lock)
1041 *
1042 * RETURNS:
1043 * Zero on success, non-zero on error.
1044 */
1045
Jeff Garzik057ace52005-10-22 14:27:05 -04001046static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047{
1048 struct ata_taskfile *tf = &qc->tf;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001049 struct ata_device *dev = qc->dev;
Albert Lee3aef5232005-10-04 08:47:43 -04001050 u64 block;
1051 u32 n_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
Tejun Heo27197362006-04-02 18:51:53 +09001053 qc->flags |= ATA_QCFLAG_IO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Albert Lee8cbd6df2005-10-12 15:06:27 +08001056 if (scsicmd[0] == WRITE_10 || scsicmd[0] == WRITE_6 ||
1057 scsicmd[0] == WRITE_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 tf->flags |= ATA_TFLAG_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001060 /* Calculate the SCSI LBA, transfer length and FUA. */
Albert Lee3aef5232005-10-04 08:47:43 -04001061 switch (scsicmd[0]) {
1062 case READ_10:
1063 case WRITE_10:
1064 scsi_10_lba_len(scsicmd, &block, &n_block);
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001065 if (unlikely(scsicmd[1] & (1 << 3)))
1066 tf->flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001067 break;
1068 case READ_6:
1069 case WRITE_6:
1070 scsi_6_lba_len(scsicmd, &block, &n_block);
Albert Leec187c4b2005-10-04 08:46:51 -04001071
1072 /* for 6-byte r/w commands, transfer length 0
1073 * means 256 blocks of data, not 0 block.
1074 */
Jeff Garzik76b2bf92005-08-29 19:24:43 -04001075 if (!n_block)
1076 n_block = 256;
Albert Lee3aef5232005-10-04 08:47:43 -04001077 break;
1078 case READ_16:
1079 case WRITE_16:
1080 scsi_16_lba_len(scsicmd, &block, &n_block);
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001081 if (unlikely(scsicmd[1] & (1 << 3)))
1082 tf->flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001083 break;
1084 default:
Albert Lee8bf62ece2005-05-12 15:29:42 -04001085 DPRINTK("no-byte command\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04001086 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088
Albert Lee8bf62ece2005-05-12 15:29:42 -04001089 /* Check and compose ATA command */
1090 if (!n_block)
Albert Leec187c4b2005-10-04 08:46:51 -04001091 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1092 * length 0 means transfer 0 block of data.
1093 * However, for ATA R/W commands, sector count 0 means
1094 * 256 or 65536 sectors, not 0 sectors as in SCSI.
Alan Coxf51750d2005-11-07 17:06:33 +00001095 *
1096 * WARNING: one or two older ATA drives treat 0 as 0...
Albert Leec187c4b2005-10-04 08:46:51 -04001097 */
Douglas Gilbertae006512005-10-09 09:09:35 -04001098 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001099
Albert Lee07506692005-10-12 15:04:18 +08001100 if (dev->flags & ATA_DFLAG_LBA) {
1101 tf->flags |= ATA_TFLAG_LBA;
1102
Albert Leec6a33e22005-10-12 15:12:26 +08001103 if (lba_28_ok(block, n_block)) {
1104 /* use LBA28 */
1105 tf->device |= (block >> 24) & 0xf;
1106 } else if (lba_48_ok(block, n_block)) {
1107 if (!(dev->flags & ATA_DFLAG_LBA48))
Douglas Gilbertae006512005-10-09 09:09:35 -04001108 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001109
Albert Lee07506692005-10-12 15:04:18 +08001110 /* use LBA48 */
1111 tf->flags |= ATA_TFLAG_LBA48;
1112
Albert Lee8bf62ece2005-05-12 15:29:42 -04001113 tf->hob_nsect = (n_block >> 8) & 0xff;
1114
1115 tf->hob_lbah = (block >> 40) & 0xff;
1116 tf->hob_lbam = (block >> 32) & 0xff;
1117 tf->hob_lbal = (block >> 24) & 0xff;
Albert Leec6a33e22005-10-12 15:12:26 +08001118 } else
1119 /* request too large even for LBA48 */
1120 goto out_of_range;
Albert Leec187c4b2005-10-04 08:46:51 -04001121
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001122 if (unlikely(ata_rwcmd_protocol(qc) < 0))
1123 goto invalid_fld;
Albert Lee8cbd6df2005-10-12 15:06:27 +08001124
Albert Lee8bf62ece2005-05-12 15:29:42 -04001125 qc->nsect = n_block;
1126 tf->nsect = n_block & 0xff;
1127
1128 tf->lbah = (block >> 16) & 0xff;
1129 tf->lbam = (block >> 8) & 0xff;
1130 tf->lbal = block & 0xff;
1131
1132 tf->device |= ATA_LBA;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001133 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001134 /* CHS */
1135 u32 sect, head, cyl, track;
1136
1137 /* The request -may- be too large for CHS addressing. */
Albert Leec6a33e22005-10-12 15:12:26 +08001138 if (!lba_28_ok(block, n_block))
Douglas Gilbertae006512005-10-09 09:09:35 -04001139 goto out_of_range;
Albert Leec187c4b2005-10-04 08:46:51 -04001140
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001141 if (unlikely(ata_rwcmd_protocol(qc) < 0))
1142 goto invalid_fld;
Albert Lee8cbd6df2005-10-12 15:06:27 +08001143
Albert Lee8bf62ece2005-05-12 15:29:42 -04001144 /* Convert LBA to CHS */
1145 track = (u32)block / dev->sectors;
1146 cyl = track / dev->heads;
1147 head = track % dev->heads;
1148 sect = (u32)block % dev->sectors + 1;
1149
Albert Leec187c4b2005-10-04 08:46:51 -04001150 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
Albert Lee8bf62ece2005-05-12 15:29:42 -04001151 (u32)block, track, cyl, head, sect);
Albert Leec187c4b2005-10-04 08:46:51 -04001152
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001153 /* Check whether the converted CHS can fit.
1154 Cylinder: 0-65535
Albert Lee8bf62ece2005-05-12 15:29:42 -04001155 Head: 0-15
1156 Sector: 1-255*/
Albert Leec187c4b2005-10-04 08:46:51 -04001157 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
Douglas Gilbertae006512005-10-09 09:09:35 -04001158 goto out_of_range;
Albert Leec187c4b2005-10-04 08:46:51 -04001159
Albert Lee8bf62ece2005-05-12 15:29:42 -04001160 qc->nsect = n_block;
1161 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1162 tf->lbal = sect;
1163 tf->lbam = cyl;
1164 tf->lbah = cyl >> 8;
1165 tf->device |= head;
1166 }
1167
1168 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001169
1170invalid_fld:
1171 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
1172 /* "Invalid field in cbd" */
1173 return 1;
1174
1175out_of_range:
1176 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
1177 /* "Logical Block Address out of range" */
1178 return 1;
1179
1180nothing_to_do:
1181 qc->scsicmd->result = SAM_STAT_GOOD;
1182 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183}
1184
Tejun Heo77853bf2006-01-23 13:09:36 +09001185static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
1187 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika7dac442005-10-30 04:44:42 -05001188 u8 *cdb = cmd->cmnd;
Albert Leea22e2eb2005-12-05 15:38:02 +08001189 int need_sense = (qc->err_mask != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
Jeff Garzikb0955182005-05-12 15:45:22 -04001191 /* For ATA pass thru (SAT) commands, generate a sense block if
1192 * user mandated it or if there's an error. Note that if we
1193 * generate because the user forced us to, a check condition
1194 * is generated and the ATA register values are returned
1195 * whether the command completed successfully or not. If there
1196 * was no error, SK, ASC and ASCQ will all be zero.
1197 */
Jeff Garzika7dac442005-10-30 04:44:42 -05001198 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
1199 ((cdb[2] & 0x20) || need_sense)) {
Jeff Garzikb0955182005-05-12 15:45:22 -04001200 ata_gen_ata_desc_sense(qc);
1201 } else {
1202 if (!need_sense) {
1203 cmd->result = SAM_STAT_GOOD;
1204 } else {
1205 /* TODO: decide which descriptor format to use
1206 * for 48b LBA devices and call that here
1207 * instead of the fixed desc, which is only
1208 * good for smaller LBA (and maybe CHS?)
1209 * devices.
1210 */
1211 ata_gen_fixed_sense(qc);
1212 }
1213 }
1214
Tejun Heoe61e0672006-05-15 20:57:40 +09001215 if (need_sense)
1216 ata_dump_status(qc->ap->id, &qc->result_tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 qc->scsidone(cmd);
1219
Tejun Heo77853bf2006-01-23 13:09:36 +09001220 ata_qc_free(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
1223/**
1224 * ata_scsi_translate - Translate then issue SCSI command to ATA device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 * @dev: ATA device to which the command is addressed
1226 * @cmd: SCSI command to execute
1227 * @done: SCSI command completion function
1228 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1229 *
1230 * Our ->queuecommand() function has decided that the SCSI
1231 * command issued can be directly translated into an ATA
1232 * command, rather than handled internally.
1233 *
1234 * This function sets up an ata_queued_cmd structure for the
1235 * SCSI command, and sends that ata_queued_cmd to the hardware.
1236 *
Douglas Gilbertae006512005-10-09 09:09:35 -04001237 * The xlat_func argument (actor) returns 0 if ready to execute
1238 * ATA command, else 1 to finish translation. If 1 is returned
1239 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1240 * to be set reflecting an error condition or clean (early)
1241 * termination.
1242 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 * LOCKING:
1244 * spin_lock_irqsave(host_set lock)
1245 */
1246
Tejun Heo3373efd2006-05-15 20:57:53 +09001247static void ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
1248 void (*done)(struct scsi_cmnd *),
1249 ata_xlat_func_t xlat_func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 struct ata_queued_cmd *qc;
1252 u8 *scsicmd = cmd->cmnd;
1253
1254 VPRINTK("ENTER\n");
1255
Tejun Heo3373efd2006-05-15 20:57:53 +09001256 qc = ata_scsi_qc_new(dev, cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257 if (!qc)
Douglas Gilbertae006512005-10-09 09:09:35 -04001258 goto err_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 /* data is present; dma-map it */
be7db052005-04-17 15:26:13 -05001261 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1262 cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 if (unlikely(cmd->request_bufflen < 1)) {
Tejun Heof15a1da2006-05-15 20:57:56 +09001264 ata_dev_printk(dev, KERN_WARNING,
1265 "WARNING: zero len r/w req\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04001266 goto err_did;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268
1269 if (cmd->use_sg)
1270 ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
1271 else
1272 ata_sg_init_one(qc, cmd->request_buffer,
1273 cmd->request_bufflen);
1274
1275 qc->dma_dir = cmd->sc_data_direction;
1276 }
1277
1278 qc->complete_fn = ata_scsi_qc_complete;
1279
1280 if (xlat_func(qc, scsicmd))
Douglas Gilbertae006512005-10-09 09:09:35 -04001281 goto early_finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
1283 /* select device, send command to hardware */
Tejun Heo8e0e6942006-03-31 20:41:11 +09001284 ata_qc_issue(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285
1286 VPRINTK("EXIT\n");
1287 return;
1288
Douglas Gilbertae006512005-10-09 09:09:35 -04001289early_finish:
1290 ata_qc_free(qc);
1291 done(cmd);
1292 DPRINTK("EXIT - early finish (good or error)\n");
1293 return;
1294
1295err_did:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 ata_qc_free(qc);
Douglas Gilbertae006512005-10-09 09:09:35 -04001297err_mem:
1298 cmd->result = (DID_ERROR << 16);
1299 done(cmd);
1300 DPRINTK("EXIT - internal\n");
1301 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
1304/**
1305 * ata_scsi_rbuf_get - Map response buffer.
1306 * @cmd: SCSI command containing buffer to be mapped.
1307 * @buf_out: Pointer to mapped area.
1308 *
1309 * Maps buffer contained within SCSI command @cmd.
1310 *
1311 * LOCKING:
1312 * spin_lock_irqsave(host_set lock)
1313 *
1314 * RETURNS:
1315 * Length of response buffer.
1316 */
1317
1318static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
1319{
1320 u8 *buf;
1321 unsigned int buflen;
1322
1323 if (cmd->use_sg) {
1324 struct scatterlist *sg;
1325
1326 sg = (struct scatterlist *) cmd->request_buffer;
1327 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
1328 buflen = sg->length;
1329 } else {
1330 buf = cmd->request_buffer;
1331 buflen = cmd->request_bufflen;
1332 }
1333
1334 *buf_out = buf;
1335 return buflen;
1336}
1337
1338/**
1339 * ata_scsi_rbuf_put - Unmap response buffer.
1340 * @cmd: SCSI command containing buffer to be unmapped.
1341 * @buf: buffer to unmap
1342 *
1343 * Unmaps response buffer contained within @cmd.
1344 *
1345 * LOCKING:
1346 * spin_lock_irqsave(host_set lock)
1347 */
1348
1349static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
1350{
1351 if (cmd->use_sg) {
1352 struct scatterlist *sg;
1353
1354 sg = (struct scatterlist *) cmd->request_buffer;
1355 kunmap_atomic(buf - sg->offset, KM_USER0);
1356 }
1357}
1358
1359/**
1360 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1361 * @args: device IDENTIFY data / SCSI command of interest.
1362 * @actor: Callback hook for desired SCSI command simulator
1363 *
1364 * Takes care of the hard work of simulating a SCSI command...
1365 * Mapping the response buffer, calling the command's handler,
1366 * and handling the handler's return value. This return value
1367 * indicates whether the handler wishes the SCSI command to be
Douglas Gilbertae006512005-10-09 09:09:35 -04001368 * completed successfully (0), or not (in which case cmd->result
1369 * and sense buffer are assumed to be set).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001370 *
1371 * LOCKING:
1372 * spin_lock_irqsave(host_set lock)
1373 */
1374
1375void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
1376 unsigned int (*actor) (struct ata_scsi_args *args,
1377 u8 *rbuf, unsigned int buflen))
1378{
1379 u8 *rbuf;
1380 unsigned int buflen, rc;
1381 struct scsi_cmnd *cmd = args->cmd;
1382
1383 buflen = ata_scsi_rbuf_get(cmd, &rbuf);
1384 memset(rbuf, 0, buflen);
1385 rc = actor(args, rbuf, buflen);
1386 ata_scsi_rbuf_put(cmd, rbuf);
1387
Douglas Gilbertae006512005-10-09 09:09:35 -04001388 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389 cmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001390 args->done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391}
1392
1393/**
1394 * ata_scsiop_inq_std - Simulate INQUIRY command
1395 * @args: device IDENTIFY data / SCSI command of interest.
1396 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1397 * @buflen: Response buffer length.
1398 *
1399 * Returns standard device identification data associated
Jeff Garzikb142eb62006-03-21 20:37:47 -05001400 * with non-VPD INQUIRY command output.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 *
1402 * LOCKING:
1403 * spin_lock_irqsave(host_set lock)
1404 */
1405
1406unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
1407 unsigned int buflen)
1408{
1409 u8 hdr[] = {
1410 TYPE_DISK,
1411 0,
1412 0x5, /* claim SPC-3 version compatibility */
1413 2,
1414 95 - 4
1415 };
1416
1417 /* set scsi removeable (RMB) bit per ata bit */
1418 if (ata_id_removeable(args->id))
1419 hdr[1] |= (1 << 7);
1420
1421 VPRINTK("ENTER\n");
1422
1423 memcpy(rbuf, hdr, sizeof(hdr));
1424
1425 if (buflen > 35) {
1426 memcpy(&rbuf[8], "ATA ", 8);
Tejun Heo6a62a042006-02-13 10:02:46 +09001427 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
1428 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (rbuf[32] == 0 || rbuf[32] == ' ')
1430 memcpy(&rbuf[32], "n/a ", 4);
1431 }
1432
1433 if (buflen > 63) {
1434 const u8 versions[] = {
1435 0x60, /* SAM-3 (no version claimed) */
1436
1437 0x03,
1438 0x20, /* SBC-2 (no version claimed) */
1439
1440 0x02,
1441 0x60 /* SPC-3 (no version claimed) */
1442 };
1443
1444 memcpy(rbuf + 59, versions, sizeof(versions));
1445 }
1446
1447 return 0;
1448}
1449
1450/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05001451 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001452 * @args: device IDENTIFY data / SCSI command of interest.
1453 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1454 * @buflen: Response buffer length.
1455 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05001456 * Returns list of inquiry VPD pages available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 *
1458 * LOCKING:
1459 * spin_lock_irqsave(host_set lock)
1460 */
1461
1462unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
1463 unsigned int buflen)
1464{
1465 const u8 pages[] = {
1466 0x00, /* page 0x00, this page */
1467 0x80, /* page 0x80, unit serial no page */
1468 0x83 /* page 0x83, device ident page */
1469 };
Jeff Garzikb142eb62006-03-21 20:37:47 -05001470 rbuf[3] = sizeof(pages); /* number of supported VPD pages */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471
1472 if (buflen > 6)
1473 memcpy(rbuf + 4, pages, sizeof(pages));
1474
1475 return 0;
1476}
1477
1478/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05001479 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 * @args: device IDENTIFY data / SCSI command of interest.
1481 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1482 * @buflen: Response buffer length.
1483 *
1484 * Returns ATA device serial number.
1485 *
1486 * LOCKING:
1487 * spin_lock_irqsave(host_set lock)
1488 */
1489
1490unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
1491 unsigned int buflen)
1492{
1493 const u8 hdr[] = {
1494 0,
1495 0x80, /* this page code */
1496 0,
1497 ATA_SERNO_LEN, /* page len */
1498 };
1499 memcpy(rbuf, hdr, sizeof(hdr));
1500
1501 if (buflen > (ATA_SERNO_LEN + 4 - 1))
Tejun Heo6a62a042006-02-13 10:02:46 +09001502 ata_id_string(args->id, (unsigned char *) &rbuf[4],
1503 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 return 0;
1506}
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05001509 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
Linus Torvalds1da177e2005-04-16 15:20:36 -07001510 * @args: device IDENTIFY data / SCSI command of interest.
1511 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1512 * @buflen: Response buffer length.
1513 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05001514 * Yields two logical unit device identification designators:
1515 * - vendor specific ASCII containing the ATA serial number
1516 * - SAT defined "t10 vendor id based" containing ASCII vendor
1517 * name ("ATA "), model and serial numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 *
1519 * LOCKING:
1520 * spin_lock_irqsave(host_set lock)
1521 */
1522
1523unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
1524 unsigned int buflen)
1525{
Jeff Garzikb142eb62006-03-21 20:37:47 -05001526 int num;
1527 const int sat_model_serial_desc_len = 68;
1528 const int ata_model_byte_len = 40;
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 rbuf[1] = 0x83; /* this page code */
Jeff Garzikb142eb62006-03-21 20:37:47 -05001531 num = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532
Jeff Garzikb142eb62006-03-21 20:37:47 -05001533 if (buflen > (ATA_SERNO_LEN + num + 3)) {
1534 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001535 rbuf[num + 0] = 2;
Jeff Garzikb142eb62006-03-21 20:37:47 -05001536 rbuf[num + 3] = ATA_SERNO_LEN;
1537 num += 4;
1538 ata_id_string(args->id, (unsigned char *) rbuf + num,
1539 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1540 num += ATA_SERNO_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
Jeff Garzikb142eb62006-03-21 20:37:47 -05001542 if (buflen > (sat_model_serial_desc_len + num + 3)) {
1543 /* SAT defined lu model and serial numbers descriptor */
1544 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001545 rbuf[num + 0] = 2;
1546 rbuf[num + 1] = 1;
Jeff Garzikb142eb62006-03-21 20:37:47 -05001547 rbuf[num + 3] = sat_model_serial_desc_len;
1548 num += 4;
1549 memcpy(rbuf + num, "ATA ", 8);
1550 num += 8;
1551 ata_id_string(args->id, (unsigned char *) rbuf + num,
1552 ATA_ID_PROD_OFS, ata_model_byte_len);
1553 num += ata_model_byte_len;
1554 ata_id_string(args->id, (unsigned char *) rbuf + num,
1555 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1556 num += ATA_SERNO_LEN;
1557 }
1558 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return 0;
1560}
1561
1562/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001563 * ata_scsiop_noop - Command handler that simply returns success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001564 * @args: device IDENTIFY data / SCSI command of interest.
1565 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1566 * @buflen: Response buffer length.
1567 *
1568 * No operation. Simply returns success to caller, to indicate
1569 * that the caller should successfully complete this SCSI command.
1570 *
1571 * LOCKING:
1572 * spin_lock_irqsave(host_set lock)
1573 */
1574
1575unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
1576 unsigned int buflen)
1577{
1578 VPRINTK("ENTER\n");
1579 return 0;
1580}
1581
1582/**
1583 * ata_msense_push - Push data onto MODE SENSE data output buffer
1584 * @ptr_io: (input/output) Location to store more output data
1585 * @last: End of output data buffer
1586 * @buf: Pointer to BLOB being added to output buffer
1587 * @buflen: Length of BLOB
1588 *
1589 * Store MODE SENSE data on an output buffer.
1590 *
1591 * LOCKING:
1592 * None.
1593 */
1594
1595static void ata_msense_push(u8 **ptr_io, const u8 *last,
1596 const u8 *buf, unsigned int buflen)
1597{
1598 u8 *ptr = *ptr_io;
1599
1600 if ((ptr + buflen - 1) > last)
1601 return;
1602
1603 memcpy(ptr, buf, buflen);
1604
1605 ptr += buflen;
1606
1607 *ptr_io = ptr;
1608}
1609
1610/**
1611 * ata_msense_caching - Simulate MODE SENSE caching info page
1612 * @id: device IDENTIFY data
1613 * @ptr_io: (input/output) Location to store more output data
1614 * @last: End of output data buffer
1615 *
1616 * Generate a caching info page, which conditionally indicates
1617 * write caching to the SCSI layer, depending on device
1618 * capabilities.
1619 *
1620 * LOCKING:
1621 * None.
1622 */
1623
1624static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1625 const u8 *last)
1626{
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001627 u8 page[CACHE_MPAGE_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001629 memcpy(page, def_cache_mpage, sizeof(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 if (ata_id_wcache_enabled(id))
1631 page[2] |= (1 << 2); /* write cache enable */
1632 if (!ata_id_rahead_enabled(id))
1633 page[12] |= (1 << 5); /* disable read ahead */
1634
1635 ata_msense_push(ptr_io, last, page, sizeof(page));
1636 return sizeof(page);
1637}
1638
1639/**
1640 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1641 * @dev: Device associated with this MODE SENSE command
1642 * @ptr_io: (input/output) Location to store more output data
1643 * @last: End of output data buffer
1644 *
1645 * Generate a generic MODE SENSE control mode page.
1646 *
1647 * LOCKING:
1648 * None.
1649 */
1650
1651static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1652{
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001653 ata_msense_push(ptr_io, last, def_control_mpage,
1654 sizeof(def_control_mpage));
1655 return sizeof(def_control_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656}
1657
1658/**
1659 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1660 * @dev: Device associated with this MODE SENSE command
1661 * @ptr_io: (input/output) Location to store more output data
1662 * @last: End of output data buffer
1663 *
1664 * Generate a generic MODE SENSE r/w error recovery page.
1665 *
1666 * LOCKING:
1667 * None.
1668 */
1669
1670static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1671{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001673 ata_msense_push(ptr_io, last, def_rw_recovery_mpage,
1674 sizeof(def_rw_recovery_mpage));
1675 return sizeof(def_rw_recovery_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676}
1677
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001678/*
1679 * We can turn this into a real blacklist if it's needed, for now just
1680 * blacklist any Maxtor BANC1G10 revision firmware
1681 */
1682static int ata_dev_supports_fua(u16 *id)
1683{
1684 unsigned char model[41], fw[9];
1685
Jeff Garzikc3c013a2006-02-27 22:31:19 -05001686 if (!libata_fua)
1687 return 0;
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001688 if (!ata_id_has_fua(id))
1689 return 0;
1690
Tejun Heo6a62a042006-02-13 10:02:46 +09001691 ata_id_c_string(id, model, ATA_ID_PROD_OFS, sizeof(model));
1692 ata_id_c_string(id, fw, ATA_ID_FW_REV_OFS, sizeof(fw));
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001693
Tejun Heo2e026712006-02-12 22:47:04 +09001694 if (strcmp(model, "Maxtor"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001695 return 1;
Tejun Heo2e026712006-02-12 22:47:04 +09001696 if (strcmp(fw, "BANC1G10"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001697 return 1;
1698
1699 return 0; /* blacklisted */
1700}
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702/**
1703 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1704 * @args: device IDENTIFY data / SCSI command of interest.
1705 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1706 * @buflen: Response buffer length.
1707 *
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001708 * Simulate MODE SENSE commands. Assume this is invoked for direct
1709 * access devices (e.g. disks) only. There should be no block
1710 * descriptor for other device types.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 *
1712 * LOCKING:
1713 * spin_lock_irqsave(host_set lock)
1714 */
1715
1716unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1717 unsigned int buflen)
1718{
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001719 struct ata_device *dev = args->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 u8 *scsicmd = args->cmd->cmnd, *p, *last;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001721 const u8 sat_blk_desc[] = {
1722 0, 0, 0, 0, /* number of blocks: sat unspecified */
1723 0,
1724 0, 0x2, 0x0 /* block length: 512 bytes */
1725 };
1726 u8 pg, spg;
1727 unsigned int ebd, page_control, six_byte, output_len, alloc_len, minlen;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001728 u8 dpofua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730 VPRINTK("ENTER\n");
1731
1732 six_byte = (scsicmd[0] == MODE_SENSE);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001733 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
1734 /*
1735 * LLBA bit in msense(10) ignored (compliant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736 */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 page_control = scsicmd[2] >> 6;
Douglas Gilbertae006512005-10-09 09:09:35 -04001739 switch (page_control) {
1740 case 0: /* current */
1741 break; /* supported */
1742 case 3: /* saved */
1743 goto saving_not_supp;
1744 case 1: /* changeable */
1745 case 2: /* defaults */
1746 default:
1747 goto invalid_fld;
1748 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001750 if (six_byte) {
1751 output_len = 4 + (ebd ? 8 : 0);
1752 alloc_len = scsicmd[4];
1753 } else {
1754 output_len = 8 + (ebd ? 8 : 0);
1755 alloc_len = (scsicmd[7] << 8) + scsicmd[8];
1756 }
1757 minlen = (alloc_len < buflen) ? alloc_len : buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758
1759 p = rbuf + output_len;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001760 last = rbuf + minlen - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001762 pg = scsicmd[2] & 0x3f;
1763 spg = scsicmd[3];
1764 /*
1765 * No mode subpages supported (yet) but asking for _all_
1766 * subpages may be valid
1767 */
1768 if (spg && (spg != ALL_SUB_MPAGES))
1769 goto invalid_fld;
1770
1771 switch(pg) {
1772 case RW_RECOVERY_MPAGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 output_len += ata_msense_rw_recovery(&p, last);
1774 break;
1775
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001776 case CACHE_MPAGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 output_len += ata_msense_caching(args->id, &p, last);
1778 break;
1779
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001780 case CONTROL_MPAGE: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 output_len += ata_msense_ctl_mode(&p, last);
1782 break;
1783 }
1784
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001785 case ALL_MPAGES:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786 output_len += ata_msense_rw_recovery(&p, last);
1787 output_len += ata_msense_caching(args->id, &p, last);
1788 output_len += ata_msense_ctl_mode(&p, last);
1789 break;
1790
1791 default: /* invalid page code */
Douglas Gilbertae006512005-10-09 09:09:35 -04001792 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001795 if (minlen < 1)
1796 return 0;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001797
1798 dpofua = 0;
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001799 if (ata_dev_supports_fua(args->id) && dev->flags & ATA_DFLAG_LBA48 &&
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001800 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
1801 dpofua = 1 << 4;
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 if (six_byte) {
1804 output_len--;
1805 rbuf[0] = output_len;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001806 if (minlen > 2)
1807 rbuf[2] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001808 if (ebd) {
1809 if (minlen > 3)
1810 rbuf[3] = sizeof(sat_blk_desc);
1811 if (minlen > 11)
1812 memcpy(rbuf + 4, sat_blk_desc,
1813 sizeof(sat_blk_desc));
1814 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 } else {
1816 output_len -= 2;
1817 rbuf[0] = output_len >> 8;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001818 if (minlen > 1)
1819 rbuf[1] = output_len;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001820 if (minlen > 3)
1821 rbuf[3] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001822 if (ebd) {
1823 if (minlen > 7)
1824 rbuf[7] = sizeof(sat_blk_desc);
1825 if (minlen > 15)
1826 memcpy(rbuf + 8, sat_blk_desc,
1827 sizeof(sat_blk_desc));
1828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001831
1832invalid_fld:
1833 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
1834 /* "Invalid field in cbd" */
1835 return 1;
1836
1837saving_not_supp:
1838 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
1839 /* "Saving parameters not supported" */
1840 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
1843/**
1844 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
1845 * @args: device IDENTIFY data / SCSI command of interest.
1846 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1847 * @buflen: Response buffer length.
1848 *
1849 * Simulate READ CAPACITY commands.
1850 *
1851 * LOCKING:
1852 * spin_lock_irqsave(host_set lock)
1853 */
1854
1855unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
1856 unsigned int buflen)
1857{
1858 u64 n_sectors;
1859 u32 tmp;
1860
1861 VPRINTK("ENTER\n");
1862
Albert Lee8bf62ece2005-05-12 15:29:42 -04001863 if (ata_id_has_lba(args->id)) {
1864 if (ata_id_has_lba48(args->id))
1865 n_sectors = ata_id_u64(args->id, 100);
1866 else
1867 n_sectors = ata_id_u32(args->id, 60);
1868 } else {
1869 /* CHS default translation */
1870 n_sectors = args->id[1] * args->id[3] * args->id[6];
1871
1872 if (ata_id_current_chs_valid(args->id))
1873 /* CHS current translation */
1874 n_sectors = ata_id_u32(args->id, 57);
1875 }
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 n_sectors--; /* ATA TotalUserSectors - 1 */
1878
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (args->cmd->cmnd[0] == READ_CAPACITY) {
Philip Pokorny0c144d02005-05-28 01:24:47 -07001880 if( n_sectors >= 0xffffffffULL )
1881 tmp = 0xffffffff ; /* Return max count on overflow */
1882 else
1883 tmp = n_sectors ;
1884
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 /* sector count, 32-bit */
1886 rbuf[0] = tmp >> (8 * 3);
1887 rbuf[1] = tmp >> (8 * 2);
1888 rbuf[2] = tmp >> (8 * 1);
1889 rbuf[3] = tmp;
1890
1891 /* sector size */
1892 tmp = ATA_SECT_SIZE;
1893 rbuf[6] = tmp >> 8;
1894 rbuf[7] = tmp;
1895
1896 } else {
1897 /* sector count, 64-bit */
Philip Pokorny0c144d02005-05-28 01:24:47 -07001898 tmp = n_sectors >> (8 * 4);
1899 rbuf[2] = tmp >> (8 * 3);
1900 rbuf[3] = tmp >> (8 * 2);
1901 rbuf[4] = tmp >> (8 * 1);
1902 rbuf[5] = tmp;
1903 tmp = n_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 rbuf[6] = tmp >> (8 * 3);
1905 rbuf[7] = tmp >> (8 * 2);
1906 rbuf[8] = tmp >> (8 * 1);
1907 rbuf[9] = tmp;
1908
1909 /* sector size */
1910 tmp = ATA_SECT_SIZE;
1911 rbuf[12] = tmp >> 8;
1912 rbuf[13] = tmp;
1913 }
1914
1915 return 0;
1916}
1917
1918/**
1919 * ata_scsiop_report_luns - Simulate REPORT LUNS command
1920 * @args: device IDENTIFY data / SCSI command of interest.
1921 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1922 * @buflen: Response buffer length.
1923 *
1924 * Simulate REPORT LUNS command.
1925 *
1926 * LOCKING:
1927 * spin_lock_irqsave(host_set lock)
1928 */
1929
1930unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1931 unsigned int buflen)
1932{
1933 VPRINTK("ENTER\n");
1934 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
1935
1936 return 0;
1937}
1938
1939/**
Douglas Gilbert845c5832005-10-09 08:55:41 -04001940 * ata_scsi_set_sense - Set SCSI sense data and status
1941 * @cmd: SCSI request to be handled
1942 * @sk: SCSI-defined sense key
1943 * @asc: SCSI-defined additional sense code
1944 * @ascq: SCSI-defined additional sense code qualifier
1945 *
1946 * Helper function that builds a valid fixed format, current
1947 * response code and the given sense key (sk), additional sense
1948 * code (asc) and additional sense code qualifier (ascq) with
1949 * a SCSI command status of %SAM_STAT_CHECK_CONDITION and
1950 * DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
1951 *
1952 * LOCKING:
1953 * Not required
1954 */
1955
1956void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
1957{
1958 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
1959
1960 cmd->sense_buffer[0] = 0x70; /* fixed format, current */
1961 cmd->sense_buffer[2] = sk;
1962 cmd->sense_buffer[7] = 18 - 8; /* additional sense length */
1963 cmd->sense_buffer[12] = asc;
1964 cmd->sense_buffer[13] = ascq;
1965}
1966
1967/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 * ata_scsi_badcmd - End a SCSI request with an error
1969 * @cmd: SCSI request to be handled
1970 * @done: SCSI command completion function
1971 * @asc: SCSI-defined additional sense code
1972 * @ascq: SCSI-defined additional sense code qualifier
1973 *
1974 * Helper function that completes a SCSI command with
1975 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
1976 * and the specified additional sense codes.
1977 *
1978 * LOCKING:
1979 * spin_lock_irqsave(host_set lock)
1980 */
1981
1982void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
1983{
1984 DPRINTK("ENTER\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04001985 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986
1987 done(cmd);
1988}
1989
Tejun Heo77853bf2006-01-23 13:09:36 +09001990static void atapi_sense_complete(struct ata_queued_cmd *qc)
Jeff Garzika939c962005-10-05 17:09:16 -04001991{
Tejun Heo74e6c8c2006-04-02 18:51:53 +09001992 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05001993 /* FIXME: not quite right; we don't want the
1994 * translation of taskfile registers into
1995 * a sense descriptors, since that's only
1996 * correct for ATA, not ATAPI
1997 */
1998 ata_gen_ata_desc_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09001999 }
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002000
2001 qc->scsidone(qc->scsicmd);
Tejun Heo77853bf2006-01-23 13:09:36 +09002002 ata_qc_free(qc);
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002003}
2004
2005/* is it pointless to prefer PIO for "safety reasons"? */
2006static inline int ata_pio_use_silly(struct ata_port *ap)
2007{
2008 return (ap->flags & ATA_FLAG_PIO_DMA);
2009}
2010
2011static void atapi_request_sense(struct ata_queued_cmd *qc)
2012{
2013 struct ata_port *ap = qc->ap;
2014 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika939c962005-10-05 17:09:16 -04002015
2016 DPRINTK("ATAPI request sense\n");
2017
Jeff Garzika939c962005-10-05 17:09:16 -04002018 /* FIXME: is this needed? */
2019 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
2020
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002021 ap->ops->tf_read(ap, &qc->tf);
2022
2023 /* fill these in, for the case where they are -not- overwritten */
2024 cmd->sense_buffer[0] = 0x70;
2025 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2026
2027 ata_qc_reinit(qc);
2028
Jeff Garzika939c962005-10-05 17:09:16 -04002029 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
2030 qc->dma_dir = DMA_FROM_DEVICE;
2031
Tejun Heo6e7846e2006-02-12 23:32:58 +09002032 memset(&qc->cdb, 0, qc->dev->cdb_len);
Jeff Garzika939c962005-10-05 17:09:16 -04002033 qc->cdb[0] = REQUEST_SENSE;
2034 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2035
2036 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2037 qc->tf.command = ATA_CMD_PACKET;
2038
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002039 if (ata_pio_use_silly(ap)) {
2040 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
2041 qc->tf.feature |= ATAPI_PKT_DMA;
2042 } else {
2043 qc->tf.protocol = ATA_PROT_ATAPI;
2044 qc->tf.lbam = (8 * 1024) & 0xff;
2045 qc->tf.lbah = (8 * 1024) >> 8;
2046 }
Jeff Garzika939c962005-10-05 17:09:16 -04002047 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2048
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002049 qc->complete_fn = atapi_sense_complete;
Jeff Garzika939c962005-10-05 17:09:16 -04002050
Tejun Heo8e0e6942006-03-31 20:41:11 +09002051 ata_qc_issue(qc);
Jeff Garzika939c962005-10-05 17:09:16 -04002052
2053 DPRINTK("EXIT\n");
2054}
2055
Tejun Heo77853bf2006-01-23 13:09:36 +09002056static void atapi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057{
2058 struct scsi_cmnd *cmd = qc->scsicmd;
Albert Leea22e2eb2005-12-05 15:38:02 +08002059 unsigned int err_mask = qc->err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Jeff Garzika7dac442005-10-30 04:44:42 -05002061 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
Jeff Garzike12669e2005-10-05 18:39:23 -04002062
Jeff Garzika7dac442005-10-30 04:44:42 -05002063 if (unlikely(err_mask & AC_ERR_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 cmd->result = SAM_STAT_CHECK_CONDITION;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002065 atapi_request_sense(qc);
Tejun Heo77853bf2006-01-23 13:09:36 +09002066 return;
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002067 } else if (unlikely(err_mask)) {
Jeff Garzika7dac442005-10-30 04:44:42 -05002068 /* FIXME: not quite right; we don't want the
2069 * translation of taskfile registers into
2070 * a sense descriptors, since that's only
2071 * correct for ATA, not ATAPI
2072 */
2073 ata_gen_ata_desc_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002074 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075 u8 *scsicmd = cmd->cmnd;
2076
Tony Battersbyfd71da42005-12-21 16:35:44 -05002077 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 u8 *buf = NULL;
2079 unsigned int buflen;
2080
2081 buflen = ata_scsi_rbuf_get(cmd, &buf);
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002082
2083 /* ATAPI devices typically report zero for their SCSI version,
2084 * and sometimes deviate from the spec WRT response data
2085 * format. If SCSI version is reported as zero like normal,
2086 * then we make the following fixups: 1) Fake MMC-5 version,
2087 * to indicate to the Linux scsi midlayer this is a modern
2088 * device. 2) Ensure response data format / ATAPI information
2089 * are always correct.
2090 */
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002091 if (buf[2] == 0) {
2092 buf[2] = 0x5;
2093 buf[3] = 0x32;
2094 }
2095
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096 ata_scsi_rbuf_put(cmd, buf);
2097 }
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002098
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 cmd->result = SAM_STAT_GOOD;
2100 }
2101
2102 qc->scsidone(cmd);
Tejun Heo77853bf2006-01-23 13:09:36 +09002103 ata_qc_free(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104}
2105/**
2106 * atapi_xlat - Initialize PACKET taskfile
2107 * @qc: command structure to be initialized
2108 * @scsicmd: SCSI CDB associated with this PACKET command
2109 *
2110 * LOCKING:
2111 * spin_lock_irqsave(host_set lock)
2112 *
2113 * RETURNS:
2114 * Zero on success, non-zero on failure.
2115 */
2116
Jeff Garzik057ace52005-10-22 14:27:05 -04002117static unsigned int atapi_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
2119 struct scsi_cmnd *cmd = qc->scsicmd;
2120 struct ata_device *dev = qc->dev;
2121 int using_pio = (dev->flags & ATA_DFLAG_PIO);
be7db052005-04-17 15:26:13 -05002122 int nodata = (cmd->sc_data_direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
2124 if (!using_pio)
2125 /* Check whether ATAPI DMA is safe */
2126 if (ata_check_atapi_dma(qc))
2127 using_pio = 1;
2128
Tejun Heo6e7846e2006-02-12 23:32:58 +09002129 memcpy(&qc->cdb, scsicmd, dev->cdb_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130
2131 qc->complete_fn = atapi_qc_complete;
2132
2133 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
be7db052005-04-17 15:26:13 -05002134 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002135 qc->tf.flags |= ATA_TFLAG_WRITE;
2136 DPRINTK("direction: write\n");
2137 }
2138
2139 qc->tf.command = ATA_CMD_PACKET;
2140
2141 /* no data, or PIO data xfer */
2142 if (using_pio || nodata) {
2143 if (nodata)
2144 qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
2145 else
2146 qc->tf.protocol = ATA_PROT_ATAPI;
2147 qc->tf.lbam = (8 * 1024) & 0xff;
2148 qc->tf.lbah = (8 * 1024) >> 8;
2149 }
2150
2151 /* DMA data xfer */
2152 else {
2153 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
2154 qc->tf.feature |= ATAPI_PKT_DMA;
2155
Albert Lee95de7192006-04-04 10:57:18 +08002156 if (atapi_dmadir && (cmd->sc_data_direction != DMA_TO_DEVICE))
2157 /* some SATA bridges need us to indicate data xfer direction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 qc->tf.feature |= ATAPI_DMADIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 }
2160
2161 qc->nbytes = cmd->bufflen;
2162
2163 return 0;
2164}
2165
2166/**
2167 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2168 * @ap: ATA port to which the device is attached
2169 * @scsidev: SCSI device from which we derive the ATA device
2170 *
2171 * Given various information provided in struct scsi_cmnd,
2172 * map that onto an ATA bus, and using that mapping
2173 * determine which ata_device is associated with the
2174 * SCSI command to be sent.
2175 *
2176 * LOCKING:
2177 * spin_lock_irqsave(host_set lock)
2178 *
2179 * RETURNS:
2180 * Associated ATA device, or %NULL if not found.
2181 */
2182
2183static struct ata_device *
Jeff Garzik057ace52005-10-22 14:27:05 -04002184ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185{
2186 struct ata_device *dev;
2187
2188 /* skip commands not addressed to targets we simulate */
2189 if (likely(scsidev->id < ATA_MAX_DEVICES))
2190 dev = &ap->device[scsidev->id];
2191 else
2192 return NULL;
2193
2194 if (unlikely((scsidev->channel != 0) ||
2195 (scsidev->lun != 0)))
2196 return NULL;
2197
Tejun Heoe1211e32006-04-01 01:38:18 +09002198 if (unlikely(!ata_dev_enabled(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002199 return NULL;
2200
Jeff Garzik50630192005-12-13 02:29:45 -05002201 if (!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) {
2202 if (unlikely(dev->class == ATA_DEV_ATAPI)) {
Tejun Heof15a1da2006-05-15 20:57:56 +09002203 ata_dev_printk(dev, KERN_WARNING,
2204 "WARNING: ATAPI is %s, device ignored.\n",
2205 atapi_enabled ? "not supported with this driver" : "disabled");
Jeff Garzik1623c812005-08-30 03:37:42 -04002206 return NULL;
Jeff Garzik50630192005-12-13 02:29:45 -05002207 }
Jeff Garzik1623c812005-08-30 03:37:42 -04002208 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209
2210 return dev;
2211}
2212
Jeff Garzikb0955182005-05-12 15:45:22 -04002213/*
2214 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2215 * @byte1: Byte 1 from pass-thru CDB.
2216 *
2217 * RETURNS:
2218 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2219 */
2220static u8
2221ata_scsi_map_proto(u8 byte1)
2222{
2223 switch((byte1 & 0x1e) >> 1) {
2224 case 3: /* Non-data */
2225 return ATA_PROT_NODATA;
2226
2227 case 6: /* DMA */
2228 return ATA_PROT_DMA;
2229
2230 case 4: /* PIO Data-in */
2231 case 5: /* PIO Data-out */
Jeff Garzikb0955182005-05-12 15:45:22 -04002232 return ATA_PROT_PIO;
2233
2234 case 10: /* Device Reset */
2235 case 0: /* Hard Reset */
2236 case 1: /* SRST */
2237 case 2: /* Bus Idle */
2238 case 7: /* Packet */
2239 case 8: /* DMA Queued */
2240 case 9: /* Device Diagnostic */
2241 case 11: /* UDMA Data-in */
2242 case 12: /* UDMA Data-Out */
2243 case 13: /* FPDMA */
2244 default: /* Reserved */
2245 break;
2246 }
2247
2248 return ATA_PROT_UNKNOWN;
2249}
2250
2251/**
2252 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
2253 * @qc: command structure to be initialized
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002254 * @scsicmd: SCSI command to convert
Jeff Garzikb0955182005-05-12 15:45:22 -04002255 *
2256 * Handles either 12 or 16-byte versions of the CDB.
2257 *
2258 * RETURNS:
2259 * Zero on success, non-zero on failure.
2260 */
2261static unsigned int
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002262ata_scsi_pass_thru(struct ata_queued_cmd *qc, const u8 *scsicmd)
Jeff Garzikb0955182005-05-12 15:45:22 -04002263{
2264 struct ata_taskfile *tf = &(qc->tf);
2265 struct scsi_cmnd *cmd = qc->scsicmd;
2266
2267 if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
Tejun Heo9a405252005-12-02 11:49:11 +09002268 goto invalid_fld;
Jeff Garzikb0955182005-05-12 15:45:22 -04002269
Albert Leef59b0cf2006-03-16 17:59:22 +08002270 if (scsicmd[1] & 0xe0)
2271 /* PIO multi not supported yet */
2272 goto invalid_fld;
2273
Jeff Garzikb0955182005-05-12 15:45:22 -04002274 /*
2275 * 12 and 16 byte CDBs use different offsets to
2276 * provide the various register values.
2277 */
2278 if (scsicmd[0] == ATA_16) {
2279 /*
2280 * 16-byte CDB - may contain extended commands.
2281 *
2282 * If that is the case, copy the upper byte register values.
2283 */
2284 if (scsicmd[1] & 0x01) {
2285 tf->hob_feature = scsicmd[3];
2286 tf->hob_nsect = scsicmd[5];
2287 tf->hob_lbal = scsicmd[7];
2288 tf->hob_lbam = scsicmd[9];
2289 tf->hob_lbah = scsicmd[11];
2290 tf->flags |= ATA_TFLAG_LBA48;
2291 } else
2292 tf->flags &= ~ATA_TFLAG_LBA48;
2293
2294 /*
2295 * Always copy low byte, device and command registers.
2296 */
2297 tf->feature = scsicmd[4];
2298 tf->nsect = scsicmd[6];
2299 tf->lbal = scsicmd[8];
2300 tf->lbam = scsicmd[10];
2301 tf->lbah = scsicmd[12];
2302 tf->device = scsicmd[13];
2303 tf->command = scsicmd[14];
2304 } else {
2305 /*
2306 * 12-byte CDB - incapable of extended commands.
2307 */
2308 tf->flags &= ~ATA_TFLAG_LBA48;
2309
2310 tf->feature = scsicmd[3];
2311 tf->nsect = scsicmd[4];
2312 tf->lbal = scsicmd[5];
2313 tf->lbam = scsicmd[6];
2314 tf->lbah = scsicmd[7];
2315 tf->device = scsicmd[8];
2316 tf->command = scsicmd[9];
2317 }
Mark Lorddcc2d1e2005-11-13 16:22:06 -05002318 /*
2319 * If slave is possible, enforce correct master/slave bit
2320 */
2321 if (qc->ap->flags & ATA_FLAG_SLAVE_POSS)
2322 tf->device = qc->dev->devno ?
2323 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
Jeff Garzikb0955182005-05-12 15:45:22 -04002324
2325 /*
2326 * Filter SET_FEATURES - XFER MODE command -- otherwise,
2327 * SET_FEATURES - XFER MODE must be preceded/succeeded
2328 * by an update to hardware-specific registers for each
2329 * controller (i.e. the reason for ->set_piomode(),
2330 * ->set_dmamode(), and ->post_set_mode() hooks).
2331 */
2332 if ((tf->command == ATA_CMD_SET_FEATURES)
2333 && (tf->feature == SETFEATURES_XFER))
Tejun Heo9a405252005-12-02 11:49:11 +09002334 goto invalid_fld;
Jeff Garzikb0955182005-05-12 15:45:22 -04002335
2336 /*
2337 * Set flags so that all registers will be written,
2338 * and pass on write indication (used for PIO/DMA
2339 * setup.)
2340 */
2341 tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
2342
Jeff Garzik0274aa22005-06-22 13:50:56 -04002343 if (cmd->sc_data_direction == DMA_TO_DEVICE)
Jeff Garzikb0955182005-05-12 15:45:22 -04002344 tf->flags |= ATA_TFLAG_WRITE;
2345
2346 /*
2347 * Set transfer length.
2348 *
2349 * TODO: find out if we need to do more here to
2350 * cover scatter/gather case.
2351 */
2352 qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
2353
Tejun Heoe61e0672006-05-15 20:57:40 +09002354 /* request result TF */
2355 qc->flags |= ATA_QCFLAG_RESULT_TF;
2356
Jeff Garzikb0955182005-05-12 15:45:22 -04002357 return 0;
Tejun Heo9a405252005-12-02 11:49:11 +09002358
2359 invalid_fld:
2360 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x00);
2361 /* "Invalid field in cdb" */
2362 return 1;
Jeff Garzikb0955182005-05-12 15:45:22 -04002363}
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365/**
2366 * ata_get_xlat_func - check if SCSI to ATA translation is possible
2367 * @dev: ATA device
2368 * @cmd: SCSI command opcode to consider
2369 *
2370 * Look up the SCSI command given, and determine whether the
2371 * SCSI command is to be translated or simulated.
2372 *
2373 * RETURNS:
2374 * Pointer to translation function if possible, %NULL if not.
2375 */
2376
2377static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
2378{
2379 switch (cmd) {
2380 case READ_6:
2381 case READ_10:
2382 case READ_16:
2383
2384 case WRITE_6:
2385 case WRITE_10:
2386 case WRITE_16:
2387 return ata_scsi_rw_xlat;
2388
2389 case SYNCHRONIZE_CACHE:
2390 if (ata_try_flush_cache(dev))
2391 return ata_scsi_flush_xlat;
2392 break;
2393
2394 case VERIFY:
2395 case VERIFY_16:
2396 return ata_scsi_verify_xlat;
Jeff Garzikb0955182005-05-12 15:45:22 -04002397
2398 case ATA_12:
2399 case ATA_16:
2400 return ata_scsi_pass_thru;
Jeff Garzikda613962005-08-29 19:01:43 -04002401
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04002402 case START_STOP:
2403 return ata_scsi_start_stop_xlat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404 }
2405
2406 return NULL;
2407}
2408
2409/**
2410 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
2411 * @ap: ATA port to which the command was being sent
2412 * @cmd: SCSI command to dump
2413 *
2414 * Prints the contents of a SCSI command via printk().
2415 */
2416
2417static inline void ata_scsi_dump_cdb(struct ata_port *ap,
2418 struct scsi_cmnd *cmd)
2419{
2420#ifdef ATA_DEBUG
2421 struct scsi_device *scsidev = cmd->device;
2422 u8 *scsicmd = cmd->cmnd;
2423
2424 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
2425 ap->id,
2426 scsidev->channel, scsidev->id, scsidev->lun,
2427 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
2428 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
2429 scsicmd[8]);
2430#endif
2431}
2432
Tejun Heo3373efd2006-05-15 20:57:53 +09002433static inline void __ata_scsi_queuecmd(struct scsi_cmnd *cmd,
2434 void (*done)(struct scsi_cmnd *),
2435 struct ata_device *dev)
Brian Kingeb3f0f92006-03-23 17:30:02 -06002436{
2437 if (dev->class == ATA_DEV_ATA) {
2438 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
2439 cmd->cmnd[0]);
2440
2441 if (xlat_func)
Tejun Heo3373efd2006-05-15 20:57:53 +09002442 ata_scsi_translate(dev, cmd, done, xlat_func);
Brian Kingeb3f0f92006-03-23 17:30:02 -06002443 else
Tejun Heo3373efd2006-05-15 20:57:53 +09002444 ata_scsi_simulate(dev, cmd, done);
Brian Kingeb3f0f92006-03-23 17:30:02 -06002445 } else
Tejun Heo3373efd2006-05-15 20:57:53 +09002446 ata_scsi_translate(dev, cmd, done, atapi_xlat);
Brian Kingeb3f0f92006-03-23 17:30:02 -06002447}
2448
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449/**
2450 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
2451 * @cmd: SCSI command to be sent
2452 * @done: Completion function, called when command is complete
2453 *
2454 * In some cases, this function translates SCSI commands into
2455 * ATA taskfiles, and queues the taskfiles to be sent to
2456 * hardware. In other cases, this function simulates a
2457 * SCSI device by evaluating and responding to certain
2458 * SCSI commands. This creates the overall effect of
2459 * ATA and ATAPI devices appearing as SCSI devices.
2460 *
2461 * LOCKING:
2462 * Releases scsi-layer-held lock, and obtains host_set lock.
2463 *
2464 * RETURNS:
2465 * Zero.
2466 */
2467
2468int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
2469{
2470 struct ata_port *ap;
2471 struct ata_device *dev;
2472 struct scsi_device *scsidev = cmd->device;
Jeff Garzik005a5a02005-10-30 23:31:48 -05002473 struct Scsi_Host *shost = scsidev->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474
Jeff Garzik35bb94b2006-04-11 13:12:34 -04002475 ap = ata_shost_to_port(shost);
Jeff Garzik005a5a02005-10-30 23:31:48 -05002476
2477 spin_unlock(shost->host_lock);
2478 spin_lock(&ap->host_set->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479
2480 ata_scsi_dump_cdb(ap, cmd);
2481
2482 dev = ata_scsi_find_dev(ap, scsidev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06002483 if (likely(dev))
Tejun Heo3373efd2006-05-15 20:57:53 +09002484 __ata_scsi_queuecmd(cmd, done, dev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06002485 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486 cmd->result = (DID_BAD_TARGET << 16);
2487 done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488 }
2489
Jeff Garzik005a5a02005-10-30 23:31:48 -05002490 spin_unlock(&ap->host_set->lock);
2491 spin_lock(shost->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492 return 0;
2493}
2494
2495/**
2496 * ata_scsi_simulate - simulate SCSI command on ATA device
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002497 * @dev: the target device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002498 * @cmd: SCSI command being sent to device.
2499 * @done: SCSI command completion function.
2500 *
2501 * Interprets and directly executes a select list of SCSI commands
2502 * that can be handled internally.
2503 *
2504 * LOCKING:
2505 * spin_lock_irqsave(host_set lock)
2506 */
2507
Tejun Heo3373efd2006-05-15 20:57:53 +09002508void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002509 void (*done)(struct scsi_cmnd *))
2510{
2511 struct ata_scsi_args args;
Jeff Garzik057ace52005-10-22 14:27:05 -04002512 const u8 *scsicmd = cmd->cmnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002514 args.dev = dev;
2515 args.id = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 args.cmd = cmd;
2517 args.done = done;
2518
2519 switch(scsicmd[0]) {
2520 /* no-op's, complete with success */
2521 case SYNCHRONIZE_CACHE:
2522 case REZERO_UNIT:
2523 case SEEK_6:
2524 case SEEK_10:
2525 case TEST_UNIT_READY:
2526 case FORMAT_UNIT: /* FIXME: correct? */
2527 case SEND_DIAGNOSTIC: /* FIXME: correct? */
2528 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
2529 break;
2530
2531 case INQUIRY:
2532 if (scsicmd[1] & 2) /* is CmdDt set? */
Douglas Gilbertae006512005-10-09 09:09:35 -04002533 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
2535 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
2536 else if (scsicmd[2] == 0x00)
2537 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
2538 else if (scsicmd[2] == 0x80)
2539 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
2540 else if (scsicmd[2] == 0x83)
2541 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
2542 else
Douglas Gilbertae006512005-10-09 09:09:35 -04002543 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544 break;
2545
2546 case MODE_SENSE:
2547 case MODE_SENSE_10:
2548 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
2549 break;
2550
2551 case MODE_SELECT: /* unconditionally return */
2552 case MODE_SELECT_10: /* bad-field-in-cdb */
Douglas Gilbertae006512005-10-09 09:09:35 -04002553 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 break;
2555
2556 case READ_CAPACITY:
2557 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2558 break;
2559
2560 case SERVICE_ACTION_IN:
2561 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
2562 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2563 else
Douglas Gilbertae006512005-10-09 09:09:35 -04002564 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565 break;
2566
2567 case REPORT_LUNS:
2568 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
2569 break;
2570
Jeff Garzikb0955182005-05-12 15:45:22 -04002571 /* mandatory commands we haven't implemented yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002572 case REQUEST_SENSE:
2573
2574 /* all other commands */
2575 default:
Douglas Gilbertae006512005-10-09 09:09:35 -04002576 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
2577 /* "Invalid command operation code" */
2578 done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002579 break;
2580 }
2581}
2582
Jeff Garzik644dd0c2005-10-03 15:55:19 -04002583void ata_scsi_scan_host(struct ata_port *ap)
2584{
Jeff Garzik3f19ee82005-10-03 21:36:41 -04002585 struct ata_device *dev;
Jeff Garzik644dd0c2005-10-03 15:55:19 -04002586 unsigned int i;
2587
Tejun Heo198e0fe2006-04-02 18:51:52 +09002588 if (ap->flags & ATA_FLAG_DISABLED)
Jeff Garzik644dd0c2005-10-03 15:55:19 -04002589 return;
2590
Jeff Garzik3f19ee82005-10-03 21:36:41 -04002591 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2592 dev = &ap->device[i];
2593
Tejun Heoe1211e32006-04-01 01:38:18 +09002594 if (ata_dev_enabled(dev))
Jeff Garzik3f19ee82005-10-03 21:36:41 -04002595 scsi_scan_target(&ap->host->shost_gendev, 0, i, 0, 0);
2596 }
Jeff Garzik644dd0c2005-10-03 15:55:19 -04002597}
2598