blob: 1fd3826da97e46cb56d15569645b6502611a548d [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);
Tejun Heo30afc842006-03-18 18:40:14 +090056enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040058#define RW_RECOVERY_MPAGE 0x1
59#define RW_RECOVERY_MPAGE_LEN 12
60#define CACHE_MPAGE 0x8
61#define CACHE_MPAGE_LEN 20
62#define CONTROL_MPAGE 0xa
63#define CONTROL_MPAGE_LEN 12
64#define ALL_MPAGES 0x3f
65#define ALL_SUB_MPAGES 0xff
66
67
68static const u8 def_rw_recovery_mpage[] = {
69 RW_RECOVERY_MPAGE,
70 RW_RECOVERY_MPAGE_LEN - 2,
71 (1 << 7) | /* AWRE, sat-r06 say it shall be 0 */
72 (1 << 6), /* ARRE (auto read reallocation) */
73 0, /* read retry count */
74 0, 0, 0, 0,
75 0, /* write retry count */
76 0, 0, 0
77};
78
79static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
80 CACHE_MPAGE,
81 CACHE_MPAGE_LEN - 2,
82 0, /* contains WCE, needs to be 0 for logic */
83 0, 0, 0, 0, 0, 0, 0, 0, 0,
84 0, /* contains DRA, needs to be 0 for logic */
85 0, 0, 0, 0, 0, 0, 0
86};
87
88static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
89 CONTROL_MPAGE,
90 CONTROL_MPAGE_LEN - 2,
91 2, /* DSENSE=0, GLTSD=1 */
92 0, /* [QAM+QERR may be 1, see 05-359r1] */
93 0, 0, 0, 0, 0xff, 0xff,
94 0, 30 /* extended self test time, see 05-359r1 */
95};
96
Tejun Heo30afc842006-03-18 18:40:14 +090097/*
98 * libata transport template. libata doesn't do real transport stuff.
99 * It just needs the eh_timed_out hook.
100 */
101struct scsi_transport_template ata_scsi_transport_template = {
102 .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
Mike Christie85837eb2005-11-11 16:38:53 -0600259 from scsi_ioctl_send_command() for default case... */
260 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{
270 struct ata_port *ap;
271 struct ata_device *dev;
272 int val = -EINVAL, rc = -EINVAL;
273
274 ap = (struct ata_port *) &scsidev->host->hostdata[0];
275 if (!ap)
276 goto out;
277
278 dev = ata_scsi_find_dev(ap, scsidev);
279 if (!dev) {
280 rc = -ENODEV;
281 goto out;
282 }
283
284 switch (cmd) {
285 case ATA_IOC_GET_IO32:
286 val = 0;
287 if (copy_to_user(arg, &val, 1))
288 return -EFAULT;
289 return 0;
290
291 case ATA_IOC_SET_IO32:
292 val = (unsigned long) arg;
293 if (val != 0)
294 return -EINVAL;
295 return 0;
296
Jeff Garzikb0955182005-05-12 15:45:22 -0400297 case HDIO_DRIVE_CMD:
298 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
299 return -EACCES;
300 return ata_cmd_ioctl(scsidev, arg);
301
302 case HDIO_DRIVE_TASK:
303 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
304 return -EACCES;
305 return ata_task_ioctl(scsidev, arg);
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 default:
308 rc = -ENOTTY;
309 break;
310 }
311
312out:
313 return rc;
314}
315
316/**
317 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
318 * @ap: ATA port to which the new command is attached
319 * @dev: ATA device to which the new command is attached
320 * @cmd: SCSI command that originated this ATA command
321 * @done: SCSI command completion function
322 *
323 * Obtain a reference to an unused ata_queued_cmd structure,
324 * which is the basic libata structure representing a single
325 * ATA command sent to the hardware.
326 *
327 * If a command was available, fill in the SCSI-specific
328 * portions of the structure with information on the
329 * current command.
330 *
331 * LOCKING:
332 * spin_lock_irqsave(host_set lock)
333 *
334 * RETURNS:
335 * Command allocated, or %NULL if none available.
336 */
337struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap,
338 struct ata_device *dev,
339 struct scsi_cmnd *cmd,
340 void (*done)(struct scsi_cmnd *))
341{
342 struct ata_queued_cmd *qc;
343
344 qc = ata_qc_new_init(ap, dev);
345 if (qc) {
346 qc->scsicmd = cmd;
347 qc->scsidone = done;
348
349 if (cmd->use_sg) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -0400350 qc->__sg = (struct scatterlist *) cmd->request_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 qc->n_elem = cmd->use_sg;
352 } else {
Jeff Garzikcedc9a42005-10-05 07:13:30 -0400353 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 qc->n_elem = 1;
355 }
356 } else {
357 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
358 done(cmd);
359 }
360
361 return qc;
362}
363
364/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400365 * ata_dump_status - user friendly display of error info
366 * @id: id of the port in question
367 * @tf: ptr to filled out taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 *
Jeff Garzikb0955182005-05-12 15:45:22 -0400369 * Decode and dump the ATA error/status registers for the user so
370 * that they have some idea what really happened at the non
371 * make-believe layer.
372 *
373 * LOCKING:
374 * inherited from caller
375 */
376void ata_dump_status(unsigned id, struct ata_taskfile *tf)
377{
378 u8 stat = tf->command, err = tf->feature;
379
380 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
381 if (stat & ATA_BUSY) {
382 printk("Busy }\n"); /* Data is not valid in this case */
383 } else {
384 if (stat & 0x40) printk("DriveReady ");
385 if (stat & 0x20) printk("DeviceFault ");
386 if (stat & 0x10) printk("SeekComplete ");
387 if (stat & 0x08) printk("DataRequest ");
388 if (stat & 0x04) printk("CorrectedError ");
389 if (stat & 0x02) printk("Index ");
390 if (stat & 0x01) printk("Error ");
391 printk("}\n");
392
393 if (err) {
394 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
395 if (err & 0x04) printk("DriveStatusError ");
396 if (err & 0x80) {
397 if (err & 0x04) printk("BadCRC ");
398 else printk("Sector ");
399 }
400 if (err & 0x40) printk("UncorrectableError ");
401 if (err & 0x10) printk("SectorIdNotFound ");
402 if (err & 0x02) printk("TrackZeroNotFound ");
403 if (err & 0x01) printk("AddrMarkNotFound ");
404 printk("}\n");
405 }
406 }
407}
408
Jens Axboe9b847542006-01-06 09:28:07 +0100409int ata_scsi_device_resume(struct scsi_device *sdev)
410{
411 struct ata_port *ap = (struct ata_port *) &sdev->host->hostdata[0];
412 struct ata_device *dev = &ap->device[sdev->id];
413
414 return ata_device_resume(ap, dev);
415}
416
Nigel Cunningham082776e2006-03-23 23:22:16 +1000417int ata_scsi_device_suspend(struct scsi_device *sdev, pm_message_t state)
Jens Axboe9b847542006-01-06 09:28:07 +0100418{
419 struct ata_port *ap = (struct ata_port *) &sdev->host->hostdata[0];
420 struct ata_device *dev = &ap->device[sdev->id];
421
Nigel Cunningham082776e2006-03-23 23:22:16 +1000422 return ata_device_suspend(ap, dev, state);
Jens Axboe9b847542006-01-06 09:28:07 +0100423}
424
Jeff Garzikb0955182005-05-12 15:45:22 -0400425/**
426 * ata_to_sense_error - convert ATA error to SCSI error
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800427 * @id: ATA device number
Jeff Garzikb0955182005-05-12 15:45:22 -0400428 * @drv_stat: value contained in ATA status register
429 * @drv_err: value contained in ATA error register
430 * @sk: the sense key we'll fill out
431 * @asc: the additional sense code we'll fill out
432 * @ascq: the additional sense code qualifier we'll fill out
433 *
434 * Converts an ATA error into a SCSI error. Fill out pointers to
435 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
436 * format sense blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 *
438 * LOCKING:
439 * spin_lock_irqsave(host_set lock)
440 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400441void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
442 u8 *ascq)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
Jeff Garzikb0955182005-05-12 15:45:22 -0400444 int i;
Jeff Garzikffe75ef2005-10-09 10:40:44 -0400445
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 /* Based on the 3ware driver translation table */
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100447 static const unsigned char sense_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 /* BBD|ECC|ID|MAR */
449 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
450 /* BBD|ECC|ID */
451 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
452 /* ECC|MC|MARK */
453 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
454 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
455 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
456 /* MC|ID|ABRT|TRK0|MARK */
457 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
458 /* MCR|MARK */
459 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
460 /* Bad address mark */
461 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
462 /* TRK0 */
463 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
464 /* Abort & !ICRC */
465 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
466 /* Media change request */
467 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
468 /* SRV */
469 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
470 /* Media change */
471 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
472 /* ECC */
473 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
474 /* BBD - block marked bad */
475 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
476 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
477 };
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100478 static const unsigned char stat_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /* Must be first because BUSY means no other bits valid */
480 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
481 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
482 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
483 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
484 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
485 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /*
488 * Is this an error we can process/parse
489 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400490 if (drv_stat & ATA_BUSY) {
491 drv_err = 0; /* Ignore the err bits, they're invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Jeff Garzikb0955182005-05-12 15:45:22 -0400494 if (drv_err) {
495 /* Look for drv_err */
496 for (i = 0; sense_table[i][0] != 0xFF; i++) {
497 /* Look for best matches first */
498 if ((sense_table[i][0] & drv_err) ==
499 sense_table[i][0]) {
500 *sk = sense_table[i][1];
501 *asc = sense_table[i][2];
502 *ascq = sense_table[i][3];
503 goto translate_done;
504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400506 /* No immediate match */
507 printk(KERN_WARNING "ata%u: no sense translation for "
508 "error 0x%02x\n", id, drv_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 /* Fall back to interpreting status bits */
Jeff Garzikb0955182005-05-12 15:45:22 -0400512 for (i = 0; stat_table[i][0] != 0xFF; i++) {
513 if (stat_table[i][0] & drv_stat) {
514 *sk = stat_table[i][1];
515 *asc = stat_table[i][2];
516 *ascq = stat_table[i][3];
517 goto translate_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400520 /* No error? Undecoded? */
521 printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n",
522 id, drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
Alan Cox2d202022006-03-21 15:53:46 +0000524 /* We need a sensible error return here, which is tricky, and one
525 that won't cause people to do things like return a disk wrongly */
526 *sk = ABORTED_COMMAND;
527 *asc = 0x00;
528 *ascq = 0x00;
Jeff Garzikb0955182005-05-12 15:45:22 -0400529
530 translate_done:
531 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to "
532 "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err,
533 *sk, *asc, *ascq);
534 return;
535}
536
537/*
538 * ata_gen_ata_desc_sense - Generate check condition sense block.
539 * @qc: Command that completed.
540 *
541 * This function is specific to the ATA descriptor format sense
542 * block specified for the ATA pass through commands. Regardless
543 * of whether the command errored or not, return a sense
544 * block. Copy all controller registers into the sense
545 * block. Clear sense key, ASC & ASCQ if there is no error.
546 *
547 * LOCKING:
548 * spin_lock_irqsave(host_set lock)
549 */
550void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc)
551{
552 struct scsi_cmnd *cmd = qc->scsicmd;
553 struct ata_taskfile *tf = &qc->tf;
554 unsigned char *sb = cmd->sense_buffer;
555 unsigned char *desc = sb + 8;
556
557 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
558
Jeff Garzik0e5dec42005-10-06 09:40:20 -0400559 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -0400560
561 /*
562 * Read the controller registers.
563 */
Tejun Heoa46314742006-02-11 19:11:13 +0900564 WARN_ON(qc->ap->ops->tf_read == NULL);
Jeff Garzikb0955182005-05-12 15:45:22 -0400565 qc->ap->ops->tf_read(qc->ap, tf);
566
567 /*
568 * Use ata_to_sense_error() to map status register bits
569 * onto sense key, asc & ascq.
570 */
Jeff Garzika7dac442005-10-30 04:44:42 -0500571 if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400572 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
573 &sb[1], &sb[2], &sb[3]);
574 sb[1] &= 0x0f;
575 }
576
577 /*
578 * Sense data is current and format is descriptor.
579 */
580 sb[0] = 0x72;
581
582 desc[0] = 0x09;
583
584 /*
585 * Set length of additional sense data.
586 * Since we only populate descriptor 0, the total
587 * length is the same (fixed) length as descriptor 0.
588 */
589 desc[1] = sb[7] = 14;
590
591 /*
592 * Copy registers into sense buffer.
593 */
594 desc[2] = 0x00;
595 desc[3] = tf->feature; /* == error reg */
596 desc[5] = tf->nsect;
597 desc[7] = tf->lbal;
598 desc[9] = tf->lbam;
599 desc[11] = tf->lbah;
600 desc[12] = tf->device;
601 desc[13] = tf->command; /* == status reg */
602
603 /*
604 * Fill in Extend bit, and the high order bytes
605 * if applicable.
606 */
607 if (tf->flags & ATA_TFLAG_LBA48) {
608 desc[2] |= 0x01;
609 desc[4] = tf->hob_nsect;
610 desc[6] = tf->hob_lbal;
611 desc[8] = tf->hob_lbam;
612 desc[10] = tf->hob_lbah;
613 }
614}
615
616/**
617 * ata_gen_fixed_sense - generate a SCSI fixed sense block
618 * @qc: Command that we are erroring out
619 *
620 * Leverage ata_to_sense_error() to give us the codes. Fit our
621 * LBA in here if there's room.
622 *
623 * LOCKING:
624 * inherited from caller
625 */
626void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
627{
628 struct scsi_cmnd *cmd = qc->scsicmd;
629 struct ata_taskfile *tf = &qc->tf;
630 unsigned char *sb = cmd->sense_buffer;
631
632 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
633
Jeff Garzik0e5dec42005-10-06 09:40:20 -0400634 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -0400635
636 /*
637 * Read the controller registers.
638 */
Tejun Heoa46314742006-02-11 19:11:13 +0900639 WARN_ON(qc->ap->ops->tf_read == NULL);
Jeff Garzikb0955182005-05-12 15:45:22 -0400640 qc->ap->ops->tf_read(qc->ap, tf);
641
642 /*
643 * Use ata_to_sense_error() to map status register bits
644 * onto sense key, asc & ascq.
645 */
Jeff Garzika7dac442005-10-30 04:44:42 -0500646 if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400647 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
648 &sb[2], &sb[12], &sb[13]);
649 sb[2] &= 0x0f;
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 sb[0] = 0x70;
Jeff Garzikb0955182005-05-12 15:45:22 -0400653 sb[7] = 0x0a;
Jeff Garzik0274aa22005-06-22 13:50:56 -0400654
Jeff Garzika7dac442005-10-30 04:44:42 -0500655 if (tf->flags & ATA_TFLAG_LBA48) {
656 /* TODO: find solution for LBA48 descriptors */
657 }
658
659 else if (tf->flags & ATA_TFLAG_LBA) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400660 /* A small (28b) LBA will fit in the 32b info field */
661 sb[0] |= 0x80; /* set valid bit */
662 sb[3] = tf->device & 0x0f;
663 sb[4] = tf->lbah;
664 sb[5] = tf->lbam;
665 sb[6] = tf->lbal;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 }
Jeff Garzika7dac442005-10-30 04:44:42 -0500667
668 else {
669 /* TODO: C/H/S */
670 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671}
672
Brian Kinga6cce2a2006-03-17 17:04:15 -0600673static void ata_scsi_sdev_config(struct scsi_device *sdev)
674{
675 sdev->use_10_for_rw = 1;
676 sdev->use_10_for_ms = 1;
677}
678
679static void ata_scsi_dev_config(struct scsi_device *sdev,
680 struct ata_device *dev)
681{
682 unsigned int max_sectors;
683
684 /* TODO: 2048 is an arbitrary number, not the
685 * hardware maximum. This should be increased to
686 * 65534 when Jens Axboe's patch for dynamically
687 * determining max_sectors is merged.
688 */
689 max_sectors = ATA_MAX_SECTORS;
690 if (dev->flags & ATA_DFLAG_LBA48)
691 max_sectors = 2048;
692 if (dev->max_sectors)
693 max_sectors = dev->max_sectors;
694
695 blk_queue_max_sectors(sdev->request_queue, max_sectors);
696
697 /*
698 * SATA DMA transfers must be multiples of 4 byte, so
699 * we need to pad ATAPI transfers using an extra sg.
700 * Decrement max hw segments accordingly.
701 */
702 if (dev->class == ATA_DEV_ATAPI) {
703 request_queue_t *q = sdev->request_queue;
704 blk_queue_max_hw_segments(q, q->max_hw_segments - 1);
705 }
706}
707
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708/**
709 * ata_scsi_slave_config - Set SCSI device attributes
710 * @sdev: SCSI device to examine
711 *
712 * This is called before we actually start reading
713 * and writing to the device, to configure certain
714 * SCSI mid-layer behaviors.
715 *
716 * LOCKING:
717 * Defined by SCSI layer. We don't really care.
718 */
719
720int ata_scsi_slave_config(struct scsi_device *sdev)
721{
Brian Kinga6cce2a2006-03-17 17:04:15 -0600722 ata_scsi_sdev_config(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
725
726 if (sdev->id < ATA_MAX_DEVICES) {
727 struct ata_port *ap;
728 struct ata_device *dev;
729
730 ap = (struct ata_port *) &sdev->host->hostdata[0];
731 dev = &ap->device[sdev->id];
732
Brian Kinga6cce2a2006-03-17 17:04:15 -0600733 ata_scsi_dev_config(sdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
735
736 return 0; /* scsi layer doesn't check return value, sigh */
737}
738
739/**
Tejun Heof29841e2006-02-10 15:10:48 +0900740 * ata_scsi_timed_out - SCSI layer time out callback
741 * @cmd: timed out SCSI command
742 *
743 * Handles SCSI layer timeout. We race with normal completion of
744 * the qc for @cmd. If the qc is already gone, we lose and let
745 * the scsi command finish (EH_HANDLED). Otherwise, the qc has
746 * timed out and EH should be invoked. Prevent ata_qc_complete()
747 * from finishing it by setting EH_SCHEDULED and return
748 * EH_NOT_HANDLED.
749 *
750 * LOCKING:
751 * Called from timer context
752 *
753 * RETURNS:
754 * EH_HANDLED or EH_NOT_HANDLED
755 */
756enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
757{
758 struct Scsi_Host *host = cmd->device->host;
759 struct ata_port *ap = (struct ata_port *) &host->hostdata[0];
760 unsigned long flags;
761 struct ata_queued_cmd *qc;
762 enum scsi_eh_timer_return ret = EH_HANDLED;
763
764 DPRINTK("ENTER\n");
765
766 spin_lock_irqsave(&ap->host_set->lock, flags);
767 qc = ata_qc_from_tag(ap, ap->active_tag);
768 if (qc) {
Tejun Heoa46314742006-02-11 19:11:13 +0900769 WARN_ON(qc->scsicmd != cmd);
Tejun Heof29841e2006-02-10 15:10:48 +0900770 qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
771 qc->err_mask |= AC_ERR_TIMEOUT;
772 ret = EH_NOT_HANDLED;
773 }
774 spin_unlock_irqrestore(&ap->host_set->lock, flags);
775
776 DPRINTK("EXIT, ret=%d\n", ret);
777 return ret;
778}
779
780/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 * ata_scsi_error - SCSI layer error handler callback
782 * @host: SCSI host on which error occurred
783 *
784 * Handles SCSI-layer-thrown error events.
785 *
786 * LOCKING:
787 * Inherited from SCSI layer (none, can sleep)
788 *
789 * RETURNS:
790 * Zero.
791 */
792
793int ata_scsi_error(struct Scsi_Host *host)
794{
795 struct ata_port *ap;
Jeff Garzik51407882006-02-09 01:56:05 -0500796 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 DPRINTK("ENTER\n");
799
Jeff Garzik51407882006-02-09 01:56:05 -0500800 ap = (struct ata_port *) &host->hostdata[0];
801
Tejun Heodde44582006-02-02 00:56:10 +0900802 spin_lock_irqsave(&ap->host_set->lock, flags);
Tejun Heoa46314742006-02-11 19:11:13 +0900803 WARN_ON(ap->flags & ATA_FLAG_IN_EH);
Tejun Heodde44582006-02-02 00:56:10 +0900804 ap->flags |= ATA_FLAG_IN_EH;
Tejun Heoa46314742006-02-11 19:11:13 +0900805 WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
Tejun Heodde44582006-02-02 00:56:10 +0900806 spin_unlock_irqrestore(&ap->host_set->lock, flags);
807
Tejun Heo86e45b62006-03-05 15:29:09 +0900808 ata_port_flush_task(ap);
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 ap->ops->eng_timeout(ap);
811
Tejun Heoa46314742006-02-11 19:11:13 +0900812 WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
Tejun Heoa72ec4c2006-01-23 13:09:37 +0900813
814 scsi_eh_flush_done_q(&ap->eh_done_q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
Tejun Heodde44582006-02-02 00:56:10 +0900816 spin_lock_irqsave(&ap->host_set->lock, flags);
817 ap->flags &= ~ATA_FLAG_IN_EH;
818 spin_unlock_irqrestore(&ap->host_set->lock, flags);
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 DPRINTK("EXIT\n");
821 return 0;
822}
823
Tejun Heoa72ec4c2006-01-23 13:09:37 +0900824static void ata_eh_scsidone(struct scsi_cmnd *scmd)
825{
826 /* nada */
827}
828
829static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
830{
831 struct ata_port *ap = qc->ap;
832 struct scsi_cmnd *scmd = qc->scsicmd;
833 unsigned long flags;
834
835 spin_lock_irqsave(&ap->host_set->lock, flags);
836 qc->scsidone = ata_eh_scsidone;
Tejun Heo341963b2006-02-10 15:10:48 +0900837 __ata_qc_complete(qc);
Tejun Heoa46314742006-02-11 19:11:13 +0900838 WARN_ON(ata_tag_valid(qc->tag));
Tejun Heoa72ec4c2006-01-23 13:09:37 +0900839 spin_unlock_irqrestore(&ap->host_set->lock, flags);
840
841 scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
842}
843
844/**
845 * ata_eh_qc_complete - Complete an active ATA command from EH
846 * @qc: Command to complete
847 *
848 * Indicate to the mid and upper layers that an ATA command has
849 * completed. To be used from EH.
850 */
851void ata_eh_qc_complete(struct ata_queued_cmd *qc)
852{
853 struct scsi_cmnd *scmd = qc->scsicmd;
854 scmd->retries = scmd->allowed;
855 __ata_eh_qc_complete(qc);
856}
857
858/**
859 * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
860 * @qc: Command to retry
861 *
862 * Indicate to the mid and upper layers that an ATA command
863 * should be retried. To be used from EH.
864 *
865 * SCSI midlayer limits the number of retries to scmd->allowed.
866 * This function might need to adjust scmd->retries for commands
867 * which get retried due to unrelated NCQ failures.
868 */
869void ata_eh_qc_retry(struct ata_queued_cmd *qc)
870{
871 __ata_eh_qc_complete(qc);
872}
873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874/**
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400875 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
876 * @qc: Storage for translated ATA taskfile
877 * @scsicmd: SCSI command to translate
878 *
879 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
880 * (to start). Perhaps these commands should be preceded by
881 * CHECK POWER MODE to see what power mode the device is already in.
882 * [See SAT revision 5 at www.t10.org]
883 *
884 * LOCKING:
885 * spin_lock_irqsave(host_set lock)
886 *
887 * RETURNS:
888 * Zero on success, non-zero on error.
889 */
890
891static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
Jeff Garzik057ace52005-10-22 14:27:05 -0400892 const u8 *scsicmd)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400893{
894 struct ata_taskfile *tf = &qc->tf;
895
896 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
897 tf->protocol = ATA_PROT_NODATA;
898 if (scsicmd[1] & 0x1) {
899 ; /* ignore IMMED bit, violates sat-r05 */
900 }
901 if (scsicmd[4] & 0x2)
Douglas Gilbertae006512005-10-09 09:09:35 -0400902 goto invalid_fld; /* LOEJ bit set not supported */
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400903 if (((scsicmd[4] >> 4) & 0xf) != 0)
Douglas Gilbertae006512005-10-09 09:09:35 -0400904 goto invalid_fld; /* power conditions not supported */
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400905 if (scsicmd[4] & 0x1) {
906 tf->nsect = 1; /* 1 sector, lba=0 */
Albert Lee9d5b1302005-10-04 08:48:17 -0400907
908 if (qc->dev->flags & ATA_DFLAG_LBA) {
909 qc->tf.flags |= ATA_TFLAG_LBA;
910
911 tf->lbah = 0x0;
912 tf->lbam = 0x0;
913 tf->lbal = 0x0;
914 tf->device |= ATA_LBA;
915 } else {
916 /* CHS */
917 tf->lbal = 0x1; /* sect */
918 tf->lbam = 0x0; /* cyl low */
919 tf->lbah = 0x0; /* cyl high */
920 }
921
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400922 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
923 } else {
924 tf->nsect = 0; /* time period value (0 implies now) */
925 tf->command = ATA_CMD_STANDBY;
926 /* Consider: ATA STANDBY IMMEDIATE command */
927 }
928 /*
929 * Standby and Idle condition timers could be implemented but that
930 * would require libata to implement the Power condition mode page
931 * and allow the user to change it. Changing mode pages requires
932 * MODE SELECT to be implemented.
933 */
934
935 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -0400936
937invalid_fld:
938 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
939 /* "Invalid field in cbd" */
940 return 1;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400941}
942
943
944/**
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
946 * @qc: Storage for translated ATA taskfile
947 * @scsicmd: SCSI command to translate (ignored)
948 *
949 * Sets up an ATA taskfile to issue FLUSH CACHE or
950 * FLUSH CACHE EXT.
951 *
952 * LOCKING:
953 * spin_lock_irqsave(host_set lock)
954 *
955 * RETURNS:
956 * Zero on success, non-zero on error.
957 */
958
Jeff Garzik057ace52005-10-22 14:27:05 -0400959static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
961 struct ata_taskfile *tf = &qc->tf;
962
963 tf->flags |= ATA_TFLAG_DEVICE;
964 tf->protocol = ATA_PROT_NODATA;
965
Albert Lee07506692005-10-12 15:04:18 +0800966 if ((qc->dev->flags & ATA_DFLAG_LBA48) &&
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 (ata_id_has_flush_ext(qc->dev->id)))
968 tf->command = ATA_CMD_FLUSH_EXT;
969 else
970 tf->command = ATA_CMD_FLUSH;
971
972 return 0;
973}
974
975/**
Albert Lee3aef5232005-10-04 08:47:43 -0400976 * scsi_6_lba_len - Get LBA and transfer length
977 * @scsicmd: SCSI command to translate
978 *
979 * Calculate LBA and transfer length for 6-byte commands.
980 *
981 * RETURNS:
982 * @plba: the LBA
983 * @plen: the transfer length
984 */
985
Jeff Garzik057ace52005-10-22 14:27:05 -0400986static void scsi_6_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -0400987{
988 u64 lba = 0;
989 u32 len = 0;
990
991 VPRINTK("six-byte command\n");
992
993 lba |= ((u64)scsicmd[2]) << 8;
994 lba |= ((u64)scsicmd[3]);
995
996 len |= ((u32)scsicmd[4]);
997
998 *plba = lba;
999 *plen = len;
1000}
1001
1002/**
1003 * scsi_10_lba_len - Get LBA and transfer length
1004 * @scsicmd: SCSI command to translate
1005 *
1006 * Calculate LBA and transfer length for 10-byte commands.
1007 *
1008 * RETURNS:
1009 * @plba: the LBA
1010 * @plen: the transfer length
1011 */
1012
Jeff Garzik057ace52005-10-22 14:27:05 -04001013static void scsi_10_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001014{
1015 u64 lba = 0;
1016 u32 len = 0;
1017
1018 VPRINTK("ten-byte command\n");
1019
1020 lba |= ((u64)scsicmd[2]) << 24;
1021 lba |= ((u64)scsicmd[3]) << 16;
1022 lba |= ((u64)scsicmd[4]) << 8;
1023 lba |= ((u64)scsicmd[5]);
1024
1025 len |= ((u32)scsicmd[7]) << 8;
1026 len |= ((u32)scsicmd[8]);
1027
1028 *plba = lba;
1029 *plen = len;
1030}
1031
1032/**
1033 * scsi_16_lba_len - Get LBA and transfer length
1034 * @scsicmd: SCSI command to translate
1035 *
1036 * Calculate LBA and transfer length for 16-byte commands.
1037 *
1038 * RETURNS:
1039 * @plba: the LBA
1040 * @plen: the transfer length
1041 */
1042
Jeff Garzik057ace52005-10-22 14:27:05 -04001043static void scsi_16_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001044{
1045 u64 lba = 0;
1046 u32 len = 0;
1047
1048 VPRINTK("sixteen-byte command\n");
1049
1050 lba |= ((u64)scsicmd[2]) << 56;
1051 lba |= ((u64)scsicmd[3]) << 48;
1052 lba |= ((u64)scsicmd[4]) << 40;
1053 lba |= ((u64)scsicmd[5]) << 32;
1054 lba |= ((u64)scsicmd[6]) << 24;
1055 lba |= ((u64)scsicmd[7]) << 16;
1056 lba |= ((u64)scsicmd[8]) << 8;
1057 lba |= ((u64)scsicmd[9]);
1058
1059 len |= ((u32)scsicmd[10]) << 24;
1060 len |= ((u32)scsicmd[11]) << 16;
1061 len |= ((u32)scsicmd[12]) << 8;
1062 len |= ((u32)scsicmd[13]);
1063
1064 *plba = lba;
1065 *plen = len;
1066}
1067
1068/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1070 * @qc: Storage for translated ATA taskfile
1071 * @scsicmd: SCSI command to translate
1072 *
1073 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1074 *
1075 * LOCKING:
1076 * spin_lock_irqsave(host_set lock)
1077 *
1078 * RETURNS:
1079 * Zero on success, non-zero on error.
1080 */
1081
Jeff Garzik057ace52005-10-22 14:27:05 -04001082static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083{
1084 struct ata_taskfile *tf = &qc->tf;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001085 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 u64 dev_sectors = qc->dev->n_sectors;
Albert Lee3aef5232005-10-04 08:47:43 -04001087 u64 block;
1088 u32 n_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089
1090 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1091 tf->protocol = ATA_PROT_NODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092
Albert Lee3aef5232005-10-04 08:47:43 -04001093 if (scsicmd[0] == VERIFY)
1094 scsi_10_lba_len(scsicmd, &block, &n_block);
1095 else if (scsicmd[0] == VERIFY_16)
1096 scsi_16_lba_len(scsicmd, &block, &n_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 else
Douglas Gilbertae006512005-10-09 09:09:35 -04001098 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Albert Lee8bf62ece2005-05-12 15:29:42 -04001100 if (!n_block)
Douglas Gilbertae006512005-10-09 09:09:35 -04001101 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001102 if (block >= dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001103 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001104 if ((block + n_block) > dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001105 goto out_of_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Albert Lee07506692005-10-12 15:04:18 +08001107 if (dev->flags & ATA_DFLAG_LBA) {
1108 tf->flags |= ATA_TFLAG_LBA;
1109
Albert Leec6a33e22005-10-12 15:12:26 +08001110 if (lba_28_ok(block, n_block)) {
1111 /* use LBA28 */
1112 tf->command = ATA_CMD_VERIFY;
1113 tf->device |= (block >> 24) & 0xf;
1114 } else if (lba_48_ok(block, n_block)) {
1115 if (!(dev->flags & ATA_DFLAG_LBA48))
1116 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001117
1118 /* use LBA48 */
1119 tf->flags |= ATA_TFLAG_LBA48;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001120 tf->command = ATA_CMD_VERIFY_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Albert Lee8bf62ece2005-05-12 15:29:42 -04001122 tf->hob_nsect = (n_block >> 8) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Albert Lee8bf62ece2005-05-12 15:29:42 -04001124 tf->hob_lbah = (block >> 40) & 0xff;
1125 tf->hob_lbam = (block >> 32) & 0xff;
1126 tf->hob_lbal = (block >> 24) & 0xff;
Albert Leec6a33e22005-10-12 15:12:26 +08001127 } else
1128 /* request too large even for LBA48 */
1129 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001130
1131 tf->nsect = n_block & 0xff;
1132
1133 tf->lbah = (block >> 16) & 0xff;
1134 tf->lbam = (block >> 8) & 0xff;
1135 tf->lbal = block & 0xff;
1136
1137 tf->device |= ATA_LBA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 } else {
Albert Lee8bf62ece2005-05-12 15:29:42 -04001139 /* CHS */
1140 u32 sect, head, cyl, track;
1141
Albert Leec6a33e22005-10-12 15:12:26 +08001142 if (!lba_28_ok(block, n_block))
1143 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001144
Albert Lee8bf62ece2005-05-12 15:29:42 -04001145 /* Convert LBA to CHS */
1146 track = (u32)block / dev->sectors;
1147 cyl = track / dev->heads;
1148 head = track % dev->heads;
1149 sect = (u32)block % dev->sectors + 1;
1150
Albert Leec187c4b2005-10-04 08:46:51 -04001151 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1152 (u32)block, track, cyl, head, sect);
Albert Lee8bf62ece2005-05-12 15:29:42 -04001153
1154 /* Check whether the converted CHS can fit.
1155 Cylinder: 0-65535
1156 Head: 0-15
1157 Sector: 1-255*/
1158 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
Douglas Gilbertae006512005-10-09 09:09:35 -04001159 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 tf->command = ATA_CMD_VERIFY;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001162 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1163 tf->lbal = sect;
1164 tf->lbam = cyl;
1165 tf->lbah = cyl >> 8;
1166 tf->device |= head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 }
1168
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001170
1171invalid_fld:
1172 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
1173 /* "Invalid field in cbd" */
1174 return 1;
1175
1176out_of_range:
1177 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
1178 /* "Logical Block Address out of range" */
1179 return 1;
1180
1181nothing_to_do:
1182 qc->scsicmd->result = SAM_STAT_GOOD;
1183 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184}
1185
1186/**
1187 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1188 * @qc: Storage for translated ATA taskfile
1189 * @scsicmd: SCSI command to translate
1190 *
1191 * Converts any of six SCSI read/write commands into the
1192 * ATA counterpart, including starting sector (LBA),
1193 * sector count, and taking into account the device's LBA48
1194 * support.
1195 *
1196 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1197 * %WRITE_16 are currently supported.
1198 *
1199 * LOCKING:
1200 * spin_lock_irqsave(host_set lock)
1201 *
1202 * RETURNS:
1203 * Zero on success, non-zero on error.
1204 */
1205
Jeff Garzik057ace52005-10-22 14:27:05 -04001206static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207{
1208 struct ata_taskfile *tf = &qc->tf;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001209 struct ata_device *dev = qc->dev;
Albert Lee3aef5232005-10-04 08:47:43 -04001210 u64 block;
1211 u32 n_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
1213 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Albert Lee8cbd6df2005-10-12 15:06:27 +08001215 if (scsicmd[0] == WRITE_10 || scsicmd[0] == WRITE_6 ||
1216 scsicmd[0] == WRITE_16)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 tf->flags |= ATA_TFLAG_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001219 /* Calculate the SCSI LBA, transfer length and FUA. */
Albert Lee3aef5232005-10-04 08:47:43 -04001220 switch (scsicmd[0]) {
1221 case READ_10:
1222 case WRITE_10:
1223 scsi_10_lba_len(scsicmd, &block, &n_block);
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001224 if (unlikely(scsicmd[1] & (1 << 3)))
1225 tf->flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001226 break;
1227 case READ_6:
1228 case WRITE_6:
1229 scsi_6_lba_len(scsicmd, &block, &n_block);
Albert Leec187c4b2005-10-04 08:46:51 -04001230
1231 /* for 6-byte r/w commands, transfer length 0
1232 * means 256 blocks of data, not 0 block.
1233 */
Jeff Garzik76b2bf92005-08-29 19:24:43 -04001234 if (!n_block)
1235 n_block = 256;
Albert Lee3aef5232005-10-04 08:47:43 -04001236 break;
1237 case READ_16:
1238 case WRITE_16:
1239 scsi_16_lba_len(scsicmd, &block, &n_block);
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001240 if (unlikely(scsicmd[1] & (1 << 3)))
1241 tf->flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001242 break;
1243 default:
Albert Lee8bf62ece2005-05-12 15:29:42 -04001244 DPRINTK("no-byte command\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04001245 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247
Albert Lee8bf62ece2005-05-12 15:29:42 -04001248 /* Check and compose ATA command */
1249 if (!n_block)
Albert Leec187c4b2005-10-04 08:46:51 -04001250 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1251 * length 0 means transfer 0 block of data.
1252 * However, for ATA R/W commands, sector count 0 means
1253 * 256 or 65536 sectors, not 0 sectors as in SCSI.
Alan Coxf51750d2005-11-07 17:06:33 +00001254 *
1255 * WARNING: one or two older ATA drives treat 0 as 0...
Albert Leec187c4b2005-10-04 08:46:51 -04001256 */
Douglas Gilbertae006512005-10-09 09:09:35 -04001257 goto nothing_to_do;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001258
Albert Lee07506692005-10-12 15:04:18 +08001259 if (dev->flags & ATA_DFLAG_LBA) {
1260 tf->flags |= ATA_TFLAG_LBA;
1261
Albert Leec6a33e22005-10-12 15:12:26 +08001262 if (lba_28_ok(block, n_block)) {
1263 /* use LBA28 */
1264 tf->device |= (block >> 24) & 0xf;
1265 } else if (lba_48_ok(block, n_block)) {
1266 if (!(dev->flags & ATA_DFLAG_LBA48))
Douglas Gilbertae006512005-10-09 09:09:35 -04001267 goto out_of_range;
Albert Lee8bf62ece2005-05-12 15:29:42 -04001268
Albert Lee07506692005-10-12 15:04:18 +08001269 /* use LBA48 */
1270 tf->flags |= ATA_TFLAG_LBA48;
1271
Albert Lee8bf62ece2005-05-12 15:29:42 -04001272 tf->hob_nsect = (n_block >> 8) & 0xff;
1273
1274 tf->hob_lbah = (block >> 40) & 0xff;
1275 tf->hob_lbam = (block >> 32) & 0xff;
1276 tf->hob_lbal = (block >> 24) & 0xff;
Albert Leec6a33e22005-10-12 15:12:26 +08001277 } else
1278 /* request too large even for LBA48 */
1279 goto out_of_range;
Albert Leec187c4b2005-10-04 08:46:51 -04001280
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001281 if (unlikely(ata_rwcmd_protocol(qc) < 0))
1282 goto invalid_fld;
Albert Lee8cbd6df2005-10-12 15:06:27 +08001283
Albert Lee8bf62ece2005-05-12 15:29:42 -04001284 qc->nsect = n_block;
1285 tf->nsect = n_block & 0xff;
1286
1287 tf->lbah = (block >> 16) & 0xff;
1288 tf->lbam = (block >> 8) & 0xff;
1289 tf->lbal = block & 0xff;
1290
1291 tf->device |= ATA_LBA;
1292 } else {
1293 /* CHS */
1294 u32 sect, head, cyl, track;
1295
1296 /* The request -may- be too large for CHS addressing. */
Albert Leec6a33e22005-10-12 15:12:26 +08001297 if (!lba_28_ok(block, n_block))
Douglas Gilbertae006512005-10-09 09:09:35 -04001298 goto out_of_range;
Albert Leec187c4b2005-10-04 08:46:51 -04001299
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001300 if (unlikely(ata_rwcmd_protocol(qc) < 0))
1301 goto invalid_fld;
Albert Lee8cbd6df2005-10-12 15:06:27 +08001302
Albert Lee8bf62ece2005-05-12 15:29:42 -04001303 /* Convert LBA to CHS */
1304 track = (u32)block / dev->sectors;
1305 cyl = track / dev->heads;
1306 head = track % dev->heads;
1307 sect = (u32)block % dev->sectors + 1;
1308
Albert Leec187c4b2005-10-04 08:46:51 -04001309 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
Albert Lee8bf62ece2005-05-12 15:29:42 -04001310 (u32)block, track, cyl, head, sect);
Albert Leec187c4b2005-10-04 08:46:51 -04001311
Albert Lee8bf62ece2005-05-12 15:29:42 -04001312 /* Check whether the converted CHS can fit.
1313 Cylinder: 0-65535
1314 Head: 0-15
1315 Sector: 1-255*/
Albert Leec187c4b2005-10-04 08:46:51 -04001316 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
Douglas Gilbertae006512005-10-09 09:09:35 -04001317 goto out_of_range;
Albert Leec187c4b2005-10-04 08:46:51 -04001318
Albert Lee8bf62ece2005-05-12 15:29:42 -04001319 qc->nsect = n_block;
1320 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1321 tf->lbal = sect;
1322 tf->lbam = cyl;
1323 tf->lbah = cyl >> 8;
1324 tf->device |= head;
1325 }
1326
1327 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001328
1329invalid_fld:
1330 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
1331 /* "Invalid field in cbd" */
1332 return 1;
1333
1334out_of_range:
1335 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
1336 /* "Logical Block Address out of range" */
1337 return 1;
1338
1339nothing_to_do:
1340 qc->scsicmd->result = SAM_STAT_GOOD;
1341 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342}
1343
Tejun Heo77853bf2006-01-23 13:09:36 +09001344static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345{
1346 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika7dac442005-10-30 04:44:42 -05001347 u8 *cdb = cmd->cmnd;
Albert Leea22e2eb2005-12-05 15:38:02 +08001348 int need_sense = (qc->err_mask != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Jeff Garzikb0955182005-05-12 15:45:22 -04001350 /* For ATA pass thru (SAT) commands, generate a sense block if
1351 * user mandated it or if there's an error. Note that if we
1352 * generate because the user forced us to, a check condition
1353 * is generated and the ATA register values are returned
1354 * whether the command completed successfully or not. If there
1355 * was no error, SK, ASC and ASCQ will all be zero.
1356 */
Jeff Garzika7dac442005-10-30 04:44:42 -05001357 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
1358 ((cdb[2] & 0x20) || need_sense)) {
Jeff Garzikb0955182005-05-12 15:45:22 -04001359 ata_gen_ata_desc_sense(qc);
1360 } else {
1361 if (!need_sense) {
1362 cmd->result = SAM_STAT_GOOD;
1363 } else {
1364 /* TODO: decide which descriptor format to use
1365 * for 48b LBA devices and call that here
1366 * instead of the fixed desc, which is only
1367 * good for smaller LBA (and maybe CHS?)
1368 * devices.
1369 */
1370 ata_gen_fixed_sense(qc);
1371 }
1372 }
1373
1374 if (need_sense) {
1375 /* The ata_gen_..._sense routines fill in tf */
1376 ata_dump_status(qc->ap->id, &qc->tf);
1377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378
1379 qc->scsidone(cmd);
1380
Tejun Heo77853bf2006-01-23 13:09:36 +09001381 ata_qc_free(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001382}
1383
1384/**
1385 * ata_scsi_translate - Translate then issue SCSI command to ATA device
1386 * @ap: ATA port to which the command is addressed
1387 * @dev: ATA device to which the command is addressed
1388 * @cmd: SCSI command to execute
1389 * @done: SCSI command completion function
1390 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1391 *
1392 * Our ->queuecommand() function has decided that the SCSI
1393 * command issued can be directly translated into an ATA
1394 * command, rather than handled internally.
1395 *
1396 * This function sets up an ata_queued_cmd structure for the
1397 * SCSI command, and sends that ata_queued_cmd to the hardware.
1398 *
Douglas Gilbertae006512005-10-09 09:09:35 -04001399 * The xlat_func argument (actor) returns 0 if ready to execute
1400 * ATA command, else 1 to finish translation. If 1 is returned
1401 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1402 * to be set reflecting an error condition or clean (early)
1403 * termination.
1404 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 * LOCKING:
1406 * spin_lock_irqsave(host_set lock)
1407 */
1408
1409static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
1410 struct scsi_cmnd *cmd,
1411 void (*done)(struct scsi_cmnd *),
1412 ata_xlat_func_t xlat_func)
1413{
1414 struct ata_queued_cmd *qc;
1415 u8 *scsicmd = cmd->cmnd;
1416
1417 VPRINTK("ENTER\n");
1418
1419 qc = ata_scsi_qc_new(ap, dev, cmd, done);
1420 if (!qc)
Douglas Gilbertae006512005-10-09 09:09:35 -04001421 goto err_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422
1423 /* data is present; dma-map it */
be7db052005-04-17 15:26:13 -05001424 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1425 cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 if (unlikely(cmd->request_bufflen < 1)) {
1427 printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
1428 ap->id, dev->devno);
Douglas Gilbertae006512005-10-09 09:09:35 -04001429 goto err_did;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430 }
1431
1432 if (cmd->use_sg)
1433 ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
1434 else
1435 ata_sg_init_one(qc, cmd->request_buffer,
1436 cmd->request_bufflen);
1437
1438 qc->dma_dir = cmd->sc_data_direction;
1439 }
1440
1441 qc->complete_fn = ata_scsi_qc_complete;
1442
1443 if (xlat_func(qc, scsicmd))
Douglas Gilbertae006512005-10-09 09:09:35 -04001444 goto early_finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445
1446 /* select device, send command to hardware */
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09001447 qc->err_mask = ata_qc_issue(qc);
1448 if (qc->err_mask)
Tejun Heo8e436af2006-01-23 13:09:36 +09001449 ata_qc_complete(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450
1451 VPRINTK("EXIT\n");
1452 return;
1453
Douglas Gilbertae006512005-10-09 09:09:35 -04001454early_finish:
1455 ata_qc_free(qc);
1456 done(cmd);
1457 DPRINTK("EXIT - early finish (good or error)\n");
1458 return;
1459
1460err_did:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461 ata_qc_free(qc);
Douglas Gilbertae006512005-10-09 09:09:35 -04001462err_mem:
1463 cmd->result = (DID_ERROR << 16);
1464 done(cmd);
1465 DPRINTK("EXIT - internal\n");
1466 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467}
1468
1469/**
1470 * ata_scsi_rbuf_get - Map response buffer.
1471 * @cmd: SCSI command containing buffer to be mapped.
1472 * @buf_out: Pointer to mapped area.
1473 *
1474 * Maps buffer contained within SCSI command @cmd.
1475 *
1476 * LOCKING:
1477 * spin_lock_irqsave(host_set lock)
1478 *
1479 * RETURNS:
1480 * Length of response buffer.
1481 */
1482
1483static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
1484{
1485 u8 *buf;
1486 unsigned int buflen;
1487
1488 if (cmd->use_sg) {
1489 struct scatterlist *sg;
1490
1491 sg = (struct scatterlist *) cmd->request_buffer;
1492 buf = kmap_atomic(sg->page, KM_USER0) + sg->offset;
1493 buflen = sg->length;
1494 } else {
1495 buf = cmd->request_buffer;
1496 buflen = cmd->request_bufflen;
1497 }
1498
1499 *buf_out = buf;
1500 return buflen;
1501}
1502
1503/**
1504 * ata_scsi_rbuf_put - Unmap response buffer.
1505 * @cmd: SCSI command containing buffer to be unmapped.
1506 * @buf: buffer to unmap
1507 *
1508 * Unmaps response buffer contained within @cmd.
1509 *
1510 * LOCKING:
1511 * spin_lock_irqsave(host_set lock)
1512 */
1513
1514static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
1515{
1516 if (cmd->use_sg) {
1517 struct scatterlist *sg;
1518
1519 sg = (struct scatterlist *) cmd->request_buffer;
1520 kunmap_atomic(buf - sg->offset, KM_USER0);
1521 }
1522}
1523
1524/**
1525 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1526 * @args: device IDENTIFY data / SCSI command of interest.
1527 * @actor: Callback hook for desired SCSI command simulator
1528 *
1529 * Takes care of the hard work of simulating a SCSI command...
1530 * Mapping the response buffer, calling the command's handler,
1531 * and handling the handler's return value. This return value
1532 * indicates whether the handler wishes the SCSI command to be
Douglas Gilbertae006512005-10-09 09:09:35 -04001533 * completed successfully (0), or not (in which case cmd->result
1534 * and sense buffer are assumed to be set).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 *
1536 * LOCKING:
1537 * spin_lock_irqsave(host_set lock)
1538 */
1539
1540void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
1541 unsigned int (*actor) (struct ata_scsi_args *args,
1542 u8 *rbuf, unsigned int buflen))
1543{
1544 u8 *rbuf;
1545 unsigned int buflen, rc;
1546 struct scsi_cmnd *cmd = args->cmd;
1547
1548 buflen = ata_scsi_rbuf_get(cmd, &rbuf);
1549 memset(rbuf, 0, buflen);
1550 rc = actor(args, rbuf, buflen);
1551 ata_scsi_rbuf_put(cmd, rbuf);
1552
Douglas Gilbertae006512005-10-09 09:09:35 -04001553 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 cmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001555 args->done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556}
1557
1558/**
1559 * ata_scsiop_inq_std - Simulate INQUIRY command
1560 * @args: device IDENTIFY data / SCSI command of interest.
1561 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1562 * @buflen: Response buffer length.
1563 *
1564 * Returns standard device identification data associated
Jeff Garzikb142eb62006-03-21 20:37:47 -05001565 * with non-VPD INQUIRY command output.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001566 *
1567 * LOCKING:
1568 * spin_lock_irqsave(host_set lock)
1569 */
1570
1571unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
1572 unsigned int buflen)
1573{
1574 u8 hdr[] = {
1575 TYPE_DISK,
1576 0,
1577 0x5, /* claim SPC-3 version compatibility */
1578 2,
1579 95 - 4
1580 };
1581
1582 /* set scsi removeable (RMB) bit per ata bit */
1583 if (ata_id_removeable(args->id))
1584 hdr[1] |= (1 << 7);
1585
1586 VPRINTK("ENTER\n");
1587
1588 memcpy(rbuf, hdr, sizeof(hdr));
1589
1590 if (buflen > 35) {
1591 memcpy(&rbuf[8], "ATA ", 8);
Tejun Heo6a62a042006-02-13 10:02:46 +09001592 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
1593 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (rbuf[32] == 0 || rbuf[32] == ' ')
1595 memcpy(&rbuf[32], "n/a ", 4);
1596 }
1597
1598 if (buflen > 63) {
1599 const u8 versions[] = {
1600 0x60, /* SAM-3 (no version claimed) */
1601
1602 0x03,
1603 0x20, /* SBC-2 (no version claimed) */
1604
1605 0x02,
1606 0x60 /* SPC-3 (no version claimed) */
1607 };
1608
1609 memcpy(rbuf + 59, versions, sizeof(versions));
1610 }
1611
1612 return 0;
1613}
1614
1615/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05001616 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 * @args: device IDENTIFY data / SCSI command of interest.
1618 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1619 * @buflen: Response buffer length.
1620 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05001621 * Returns list of inquiry VPD pages available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001622 *
1623 * LOCKING:
1624 * spin_lock_irqsave(host_set lock)
1625 */
1626
1627unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
1628 unsigned int buflen)
1629{
1630 const u8 pages[] = {
1631 0x00, /* page 0x00, this page */
1632 0x80, /* page 0x80, unit serial no page */
1633 0x83 /* page 0x83, device ident page */
1634 };
Jeff Garzikb142eb62006-03-21 20:37:47 -05001635 rbuf[3] = sizeof(pages); /* number of supported VPD pages */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
1637 if (buflen > 6)
1638 memcpy(rbuf + 4, pages, sizeof(pages));
1639
1640 return 0;
1641}
1642
1643/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05001644 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 * @args: device IDENTIFY data / SCSI command of interest.
1646 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1647 * @buflen: Response buffer length.
1648 *
1649 * Returns ATA device serial number.
1650 *
1651 * LOCKING:
1652 * spin_lock_irqsave(host_set lock)
1653 */
1654
1655unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
1656 unsigned int buflen)
1657{
1658 const u8 hdr[] = {
1659 0,
1660 0x80, /* this page code */
1661 0,
1662 ATA_SERNO_LEN, /* page len */
1663 };
1664 memcpy(rbuf, hdr, sizeof(hdr));
1665
1666 if (buflen > (ATA_SERNO_LEN + 4 - 1))
Tejun Heo6a62a042006-02-13 10:02:46 +09001667 ata_id_string(args->id, (unsigned char *) &rbuf[4],
1668 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669
1670 return 0;
1671}
1672
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05001674 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 * @args: device IDENTIFY data / SCSI command of interest.
1676 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1677 * @buflen: Response buffer length.
1678 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05001679 * Yields two logical unit device identification designators:
1680 * - vendor specific ASCII containing the ATA serial number
1681 * - SAT defined "t10 vendor id based" containing ASCII vendor
1682 * name ("ATA "), model and serial numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 *
1684 * LOCKING:
1685 * spin_lock_irqsave(host_set lock)
1686 */
1687
1688unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
1689 unsigned int buflen)
1690{
Jeff Garzikb142eb62006-03-21 20:37:47 -05001691 int num;
1692 const int sat_model_serial_desc_len = 68;
1693 const int ata_model_byte_len = 40;
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 rbuf[1] = 0x83; /* this page code */
Jeff Garzikb142eb62006-03-21 20:37:47 -05001696 num = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
Jeff Garzikb142eb62006-03-21 20:37:47 -05001698 if (buflen > (ATA_SERNO_LEN + num + 3)) {
1699 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
1700 rbuf[num + 0] = 2;
1701 rbuf[num + 3] = ATA_SERNO_LEN;
1702 num += 4;
1703 ata_id_string(args->id, (unsigned char *) rbuf + num,
1704 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1705 num += ATA_SERNO_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001706 }
Jeff Garzikb142eb62006-03-21 20:37:47 -05001707 if (buflen > (sat_model_serial_desc_len + num + 3)) {
1708 /* SAT defined lu model and serial numbers descriptor */
1709 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
1710 rbuf[num + 0] = 2;
1711 rbuf[num + 1] = 1;
1712 rbuf[num + 3] = sat_model_serial_desc_len;
1713 num += 4;
1714 memcpy(rbuf + num, "ATA ", 8);
1715 num += 8;
1716 ata_id_string(args->id, (unsigned char *) rbuf + num,
1717 ATA_ID_PROD_OFS, ata_model_byte_len);
1718 num += ata_model_byte_len;
1719 ata_id_string(args->id, (unsigned char *) rbuf + num,
1720 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1721 num += ATA_SERNO_LEN;
1722 }
1723 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 return 0;
1725}
1726
1727/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001728 * ata_scsiop_noop - Command handler that simply returns success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729 * @args: device IDENTIFY data / SCSI command of interest.
1730 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1731 * @buflen: Response buffer length.
1732 *
1733 * No operation. Simply returns success to caller, to indicate
1734 * that the caller should successfully complete this SCSI command.
1735 *
1736 * LOCKING:
1737 * spin_lock_irqsave(host_set lock)
1738 */
1739
1740unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
1741 unsigned int buflen)
1742{
1743 VPRINTK("ENTER\n");
1744 return 0;
1745}
1746
1747/**
1748 * ata_msense_push - Push data onto MODE SENSE data output buffer
1749 * @ptr_io: (input/output) Location to store more output data
1750 * @last: End of output data buffer
1751 * @buf: Pointer to BLOB being added to output buffer
1752 * @buflen: Length of BLOB
1753 *
1754 * Store MODE SENSE data on an output buffer.
1755 *
1756 * LOCKING:
1757 * None.
1758 */
1759
1760static void ata_msense_push(u8 **ptr_io, const u8 *last,
1761 const u8 *buf, unsigned int buflen)
1762{
1763 u8 *ptr = *ptr_io;
1764
1765 if ((ptr + buflen - 1) > last)
1766 return;
1767
1768 memcpy(ptr, buf, buflen);
1769
1770 ptr += buflen;
1771
1772 *ptr_io = ptr;
1773}
1774
1775/**
1776 * ata_msense_caching - Simulate MODE SENSE caching info page
1777 * @id: device IDENTIFY data
1778 * @ptr_io: (input/output) Location to store more output data
1779 * @last: End of output data buffer
1780 *
1781 * Generate a caching info page, which conditionally indicates
1782 * write caching to the SCSI layer, depending on device
1783 * capabilities.
1784 *
1785 * LOCKING:
1786 * None.
1787 */
1788
1789static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1790 const u8 *last)
1791{
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001792 u8 page[CACHE_MPAGE_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001794 memcpy(page, def_cache_mpage, sizeof(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (ata_id_wcache_enabled(id))
1796 page[2] |= (1 << 2); /* write cache enable */
1797 if (!ata_id_rahead_enabled(id))
1798 page[12] |= (1 << 5); /* disable read ahead */
1799
1800 ata_msense_push(ptr_io, last, page, sizeof(page));
1801 return sizeof(page);
1802}
1803
1804/**
1805 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1806 * @dev: Device associated with this MODE SENSE command
1807 * @ptr_io: (input/output) Location to store more output data
1808 * @last: End of output data buffer
1809 *
1810 * Generate a generic MODE SENSE control mode page.
1811 *
1812 * LOCKING:
1813 * None.
1814 */
1815
1816static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1817{
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001818 ata_msense_push(ptr_io, last, def_control_mpage,
1819 sizeof(def_control_mpage));
1820 return sizeof(def_control_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
1822
1823/**
1824 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1825 * @dev: Device associated with this MODE SENSE command
1826 * @ptr_io: (input/output) Location to store more output data
1827 * @last: End of output data buffer
1828 *
1829 * Generate a generic MODE SENSE r/w error recovery page.
1830 *
1831 * LOCKING:
1832 * None.
1833 */
1834
1835static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1836{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001838 ata_msense_push(ptr_io, last, def_rw_recovery_mpage,
1839 sizeof(def_rw_recovery_mpage));
1840 return sizeof(def_rw_recovery_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841}
1842
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001843/*
1844 * We can turn this into a real blacklist if it's needed, for now just
1845 * blacklist any Maxtor BANC1G10 revision firmware
1846 */
1847static int ata_dev_supports_fua(u16 *id)
1848{
1849 unsigned char model[41], fw[9];
1850
Jeff Garzikc3c013a2006-02-27 22:31:19 -05001851 if (!libata_fua)
1852 return 0;
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001853 if (!ata_id_has_fua(id))
1854 return 0;
1855
Tejun Heo6a62a042006-02-13 10:02:46 +09001856 ata_id_c_string(id, model, ATA_ID_PROD_OFS, sizeof(model));
1857 ata_id_c_string(id, fw, ATA_ID_FW_REV_OFS, sizeof(fw));
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001858
Tejun Heo2e026712006-02-12 22:47:04 +09001859 if (strcmp(model, "Maxtor"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001860 return 1;
Tejun Heo2e026712006-02-12 22:47:04 +09001861 if (strcmp(fw, "BANC1G10"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001862 return 1;
1863
1864 return 0; /* blacklisted */
1865}
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867/**
1868 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1869 * @args: device IDENTIFY data / SCSI command of interest.
1870 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1871 * @buflen: Response buffer length.
1872 *
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001873 * Simulate MODE SENSE commands. Assume this is invoked for direct
1874 * access devices (e.g. disks) only. There should be no block
1875 * descriptor for other device types.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 *
1877 * LOCKING:
1878 * spin_lock_irqsave(host_set lock)
1879 */
1880
1881unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1882 unsigned int buflen)
1883{
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001884 struct ata_device *dev = args->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 u8 *scsicmd = args->cmd->cmnd, *p, *last;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001886 const u8 sat_blk_desc[] = {
1887 0, 0, 0, 0, /* number of blocks: sat unspecified */
1888 0,
1889 0, 0x2, 0x0 /* block length: 512 bytes */
1890 };
1891 u8 pg, spg;
1892 unsigned int ebd, page_control, six_byte, output_len, alloc_len, minlen;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001893 u8 dpofua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
1895 VPRINTK("ENTER\n");
1896
1897 six_byte = (scsicmd[0] == MODE_SENSE);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001898 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
1899 /*
1900 * LLBA bit in msense(10) ignored (compliant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 page_control = scsicmd[2] >> 6;
Douglas Gilbertae006512005-10-09 09:09:35 -04001904 switch (page_control) {
1905 case 0: /* current */
1906 break; /* supported */
1907 case 3: /* saved */
1908 goto saving_not_supp;
1909 case 1: /* changeable */
1910 case 2: /* defaults */
1911 default:
1912 goto invalid_fld;
1913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001915 if (six_byte) {
1916 output_len = 4 + (ebd ? 8 : 0);
1917 alloc_len = scsicmd[4];
1918 } else {
1919 output_len = 8 + (ebd ? 8 : 0);
1920 alloc_len = (scsicmd[7] << 8) + scsicmd[8];
1921 }
1922 minlen = (alloc_len < buflen) ? alloc_len : buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 p = rbuf + output_len;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001925 last = rbuf + minlen - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001927 pg = scsicmd[2] & 0x3f;
1928 spg = scsicmd[3];
1929 /*
1930 * No mode subpages supported (yet) but asking for _all_
1931 * subpages may be valid
1932 */
1933 if (spg && (spg != ALL_SUB_MPAGES))
1934 goto invalid_fld;
1935
1936 switch(pg) {
1937 case RW_RECOVERY_MPAGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 output_len += ata_msense_rw_recovery(&p, last);
1939 break;
1940
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001941 case CACHE_MPAGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 output_len += ata_msense_caching(args->id, &p, last);
1943 break;
1944
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001945 case CONTROL_MPAGE: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 output_len += ata_msense_ctl_mode(&p, last);
1947 break;
1948 }
1949
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001950 case ALL_MPAGES:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 output_len += ata_msense_rw_recovery(&p, last);
1952 output_len += ata_msense_caching(args->id, &p, last);
1953 output_len += ata_msense_ctl_mode(&p, last);
1954 break;
1955
1956 default: /* invalid page code */
Douglas Gilbertae006512005-10-09 09:09:35 -04001957 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001960 if (minlen < 1)
1961 return 0;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001962
1963 dpofua = 0;
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001964 if (ata_dev_supports_fua(args->id) && dev->flags & ATA_DFLAG_LBA48 &&
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001965 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
1966 dpofua = 1 << 4;
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 if (six_byte) {
1969 output_len--;
1970 rbuf[0] = output_len;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001971 if (minlen > 2)
1972 rbuf[2] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001973 if (ebd) {
1974 if (minlen > 3)
1975 rbuf[3] = sizeof(sat_blk_desc);
1976 if (minlen > 11)
1977 memcpy(rbuf + 4, sat_blk_desc,
1978 sizeof(sat_blk_desc));
1979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 } else {
1981 output_len -= 2;
1982 rbuf[0] = output_len >> 8;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001983 if (minlen > 1)
1984 rbuf[1] = output_len;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001985 if (minlen > 3)
1986 rbuf[3] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001987 if (ebd) {
1988 if (minlen > 7)
1989 rbuf[7] = sizeof(sat_blk_desc);
1990 if (minlen > 15)
1991 memcpy(rbuf + 8, sat_blk_desc,
1992 sizeof(sat_blk_desc));
1993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001996
1997invalid_fld:
1998 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
1999 /* "Invalid field in cbd" */
2000 return 1;
2001
2002saving_not_supp:
2003 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
2004 /* "Saving parameters not supported" */
2005 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006}
2007
2008/**
2009 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2010 * @args: device IDENTIFY data / SCSI command of interest.
2011 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2012 * @buflen: Response buffer length.
2013 *
2014 * Simulate READ CAPACITY commands.
2015 *
2016 * LOCKING:
2017 * spin_lock_irqsave(host_set lock)
2018 */
2019
2020unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
2021 unsigned int buflen)
2022{
2023 u64 n_sectors;
2024 u32 tmp;
2025
2026 VPRINTK("ENTER\n");
2027
Albert Lee8bf62ece2005-05-12 15:29:42 -04002028 if (ata_id_has_lba(args->id)) {
2029 if (ata_id_has_lba48(args->id))
2030 n_sectors = ata_id_u64(args->id, 100);
2031 else
2032 n_sectors = ata_id_u32(args->id, 60);
2033 } else {
2034 /* CHS default translation */
2035 n_sectors = args->id[1] * args->id[3] * args->id[6];
2036
2037 if (ata_id_current_chs_valid(args->id))
2038 /* CHS current translation */
2039 n_sectors = ata_id_u32(args->id, 57);
2040 }
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 n_sectors--; /* ATA TotalUserSectors - 1 */
2043
Linus Torvalds1da177e2005-04-16 15:20:36 -07002044 if (args->cmd->cmnd[0] == READ_CAPACITY) {
Philip Pokorny0c144d02005-05-28 01:24:47 -07002045 if( n_sectors >= 0xffffffffULL )
2046 tmp = 0xffffffff ; /* Return max count on overflow */
2047 else
2048 tmp = n_sectors ;
2049
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 /* sector count, 32-bit */
2051 rbuf[0] = tmp >> (8 * 3);
2052 rbuf[1] = tmp >> (8 * 2);
2053 rbuf[2] = tmp >> (8 * 1);
2054 rbuf[3] = tmp;
2055
2056 /* sector size */
2057 tmp = ATA_SECT_SIZE;
2058 rbuf[6] = tmp >> 8;
2059 rbuf[7] = tmp;
2060
2061 } else {
2062 /* sector count, 64-bit */
Philip Pokorny0c144d02005-05-28 01:24:47 -07002063 tmp = n_sectors >> (8 * 4);
2064 rbuf[2] = tmp >> (8 * 3);
2065 rbuf[3] = tmp >> (8 * 2);
2066 rbuf[4] = tmp >> (8 * 1);
2067 rbuf[5] = tmp;
2068 tmp = n_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 rbuf[6] = tmp >> (8 * 3);
2070 rbuf[7] = tmp >> (8 * 2);
2071 rbuf[8] = tmp >> (8 * 1);
2072 rbuf[9] = tmp;
2073
2074 /* sector size */
2075 tmp = ATA_SECT_SIZE;
2076 rbuf[12] = tmp >> 8;
2077 rbuf[13] = tmp;
2078 }
2079
2080 return 0;
2081}
2082
2083/**
2084 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2085 * @args: device IDENTIFY data / SCSI command of interest.
2086 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2087 * @buflen: Response buffer length.
2088 *
2089 * Simulate REPORT LUNS command.
2090 *
2091 * LOCKING:
2092 * spin_lock_irqsave(host_set lock)
2093 */
2094
2095unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
2096 unsigned int buflen)
2097{
2098 VPRINTK("ENTER\n");
2099 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2100
2101 return 0;
2102}
2103
2104/**
Douglas Gilbert845c5832005-10-09 08:55:41 -04002105 * ata_scsi_set_sense - Set SCSI sense data and status
2106 * @cmd: SCSI request to be handled
2107 * @sk: SCSI-defined sense key
2108 * @asc: SCSI-defined additional sense code
2109 * @ascq: SCSI-defined additional sense code qualifier
2110 *
2111 * Helper function that builds a valid fixed format, current
2112 * response code and the given sense key (sk), additional sense
2113 * code (asc) and additional sense code qualifier (ascq) with
2114 * a SCSI command status of %SAM_STAT_CHECK_CONDITION and
2115 * DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
2116 *
2117 * LOCKING:
2118 * Not required
2119 */
2120
2121void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
2122{
2123 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
2124
2125 cmd->sense_buffer[0] = 0x70; /* fixed format, current */
2126 cmd->sense_buffer[2] = sk;
2127 cmd->sense_buffer[7] = 18 - 8; /* additional sense length */
2128 cmd->sense_buffer[12] = asc;
2129 cmd->sense_buffer[13] = ascq;
2130}
2131
2132/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133 * ata_scsi_badcmd - End a SCSI request with an error
2134 * @cmd: SCSI request to be handled
2135 * @done: SCSI command completion function
2136 * @asc: SCSI-defined additional sense code
2137 * @ascq: SCSI-defined additional sense code qualifier
2138 *
2139 * Helper function that completes a SCSI command with
2140 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
2141 * and the specified additional sense codes.
2142 *
2143 * LOCKING:
2144 * spin_lock_irqsave(host_set lock)
2145 */
2146
2147void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
2148{
2149 DPRINTK("ENTER\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04002150 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
2152 done(cmd);
2153}
2154
Tejun Heo77853bf2006-01-23 13:09:36 +09002155static void atapi_sense_complete(struct ata_queued_cmd *qc)
Jeff Garzika939c962005-10-05 17:09:16 -04002156{
Albert Leea22e2eb2005-12-05 15:38:02 +08002157 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0))
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002158 /* FIXME: not quite right; we don't want the
2159 * translation of taskfile registers into
2160 * a sense descriptors, since that's only
2161 * correct for ATA, not ATAPI
2162 */
2163 ata_gen_ata_desc_sense(qc);
2164
2165 qc->scsidone(qc->scsicmd);
Tejun Heo77853bf2006-01-23 13:09:36 +09002166 ata_qc_free(qc);
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002167}
2168
2169/* is it pointless to prefer PIO for "safety reasons"? */
2170static inline int ata_pio_use_silly(struct ata_port *ap)
2171{
2172 return (ap->flags & ATA_FLAG_PIO_DMA);
2173}
2174
2175static void atapi_request_sense(struct ata_queued_cmd *qc)
2176{
2177 struct ata_port *ap = qc->ap;
2178 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika939c962005-10-05 17:09:16 -04002179
2180 DPRINTK("ATAPI request sense\n");
2181
Jeff Garzika939c962005-10-05 17:09:16 -04002182 /* FIXME: is this needed? */
2183 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
2184
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002185 ap->ops->tf_read(ap, &qc->tf);
2186
2187 /* fill these in, for the case where they are -not- overwritten */
2188 cmd->sense_buffer[0] = 0x70;
2189 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2190
2191 ata_qc_reinit(qc);
2192
Jeff Garzika939c962005-10-05 17:09:16 -04002193 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
2194 qc->dma_dir = DMA_FROM_DEVICE;
2195
Tejun Heo6e7846e2006-02-12 23:32:58 +09002196 memset(&qc->cdb, 0, qc->dev->cdb_len);
Jeff Garzika939c962005-10-05 17:09:16 -04002197 qc->cdb[0] = REQUEST_SENSE;
2198 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2199
2200 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2201 qc->tf.command = ATA_CMD_PACKET;
2202
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002203 if (ata_pio_use_silly(ap)) {
2204 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
2205 qc->tf.feature |= ATAPI_PKT_DMA;
2206 } else {
2207 qc->tf.protocol = ATA_PROT_ATAPI;
2208 qc->tf.lbam = (8 * 1024) & 0xff;
2209 qc->tf.lbah = (8 * 1024) >> 8;
2210 }
Jeff Garzika939c962005-10-05 17:09:16 -04002211 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2212
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002213 qc->complete_fn = atapi_sense_complete;
Jeff Garzika939c962005-10-05 17:09:16 -04002214
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09002215 qc->err_mask = ata_qc_issue(qc);
2216 if (qc->err_mask)
Albert Leea22e2eb2005-12-05 15:38:02 +08002217 ata_qc_complete(qc);
Jeff Garzika939c962005-10-05 17:09:16 -04002218
2219 DPRINTK("EXIT\n");
2220}
2221
Tejun Heo77853bf2006-01-23 13:09:36 +09002222static void atapi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002223{
2224 struct scsi_cmnd *cmd = qc->scsicmd;
Albert Leea22e2eb2005-12-05 15:38:02 +08002225 unsigned int err_mask = qc->err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002226
Jeff Garzika7dac442005-10-30 04:44:42 -05002227 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
Jeff Garzike12669e2005-10-05 18:39:23 -04002228
Jeff Garzika7dac442005-10-30 04:44:42 -05002229 if (unlikely(err_mask & AC_ERR_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 cmd->result = SAM_STAT_CHECK_CONDITION;
Jeff Garzikc6e6e6662005-11-14 14:50:05 -05002231 atapi_request_sense(qc);
Tejun Heo77853bf2006-01-23 13:09:36 +09002232 return;
Jeff Garzike12669e2005-10-05 18:39:23 -04002233 }
2234
Jeff Garzika7dac442005-10-30 04:44:42 -05002235 else if (unlikely(err_mask))
2236 /* FIXME: not quite right; we don't want the
2237 * translation of taskfile registers into
2238 * a sense descriptors, since that's only
2239 * correct for ATA, not ATAPI
2240 */
2241 ata_gen_ata_desc_sense(qc);
2242
Jeff Garzike12669e2005-10-05 18:39:23 -04002243 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244 u8 *scsicmd = cmd->cmnd;
2245
Tony Battersbyfd71da42005-12-21 16:35:44 -05002246 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002247 u8 *buf = NULL;
2248 unsigned int buflen;
2249
2250 buflen = ata_scsi_rbuf_get(cmd, &buf);
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002251
2252 /* ATAPI devices typically report zero for their SCSI version,
2253 * and sometimes deviate from the spec WRT response data
2254 * format. If SCSI version is reported as zero like normal,
2255 * then we make the following fixups: 1) Fake MMC-5 version,
2256 * to indicate to the Linux scsi midlayer this is a modern
2257 * device. 2) Ensure response data format / ATAPI information
2258 * are always correct.
2259 */
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002260 if (buf[2] == 0) {
2261 buf[2] = 0x5;
2262 buf[3] = 0x32;
2263 }
2264
Linus Torvalds1da177e2005-04-16 15:20:36 -07002265 ata_scsi_rbuf_put(cmd, buf);
2266 }
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268 cmd->result = SAM_STAT_GOOD;
2269 }
2270
2271 qc->scsidone(cmd);
Tejun Heo77853bf2006-01-23 13:09:36 +09002272 ata_qc_free(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273}
2274/**
2275 * atapi_xlat - Initialize PACKET taskfile
2276 * @qc: command structure to be initialized
2277 * @scsicmd: SCSI CDB associated with this PACKET command
2278 *
2279 * LOCKING:
2280 * spin_lock_irqsave(host_set lock)
2281 *
2282 * RETURNS:
2283 * Zero on success, non-zero on failure.
2284 */
2285
Jeff Garzik057ace52005-10-22 14:27:05 -04002286static unsigned int atapi_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002287{
2288 struct scsi_cmnd *cmd = qc->scsicmd;
2289 struct ata_device *dev = qc->dev;
2290 int using_pio = (dev->flags & ATA_DFLAG_PIO);
be7db052005-04-17 15:26:13 -05002291 int nodata = (cmd->sc_data_direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293 if (!using_pio)
2294 /* Check whether ATAPI DMA is safe */
2295 if (ata_check_atapi_dma(qc))
2296 using_pio = 1;
2297
Tejun Heo6e7846e2006-02-12 23:32:58 +09002298 memcpy(&qc->cdb, scsicmd, dev->cdb_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
2300 qc->complete_fn = atapi_qc_complete;
2301
2302 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
be7db052005-04-17 15:26:13 -05002303 if (cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 qc->tf.flags |= ATA_TFLAG_WRITE;
2305 DPRINTK("direction: write\n");
2306 }
2307
2308 qc->tf.command = ATA_CMD_PACKET;
2309
2310 /* no data, or PIO data xfer */
2311 if (using_pio || nodata) {
2312 if (nodata)
2313 qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
2314 else
2315 qc->tf.protocol = ATA_PROT_ATAPI;
2316 qc->tf.lbam = (8 * 1024) & 0xff;
2317 qc->tf.lbah = (8 * 1024) >> 8;
2318 }
2319
2320 /* DMA data xfer */
2321 else {
2322 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
2323 qc->tf.feature |= ATAPI_PKT_DMA;
2324
2325#ifdef ATAPI_ENABLE_DMADIR
2326 /* some SATA bridges need us to indicate data xfer direction */
be7db052005-04-17 15:26:13 -05002327 if (cmd->sc_data_direction != DMA_TO_DEVICE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 qc->tf.feature |= ATAPI_DMADIR;
2329#endif
2330 }
2331
2332 qc->nbytes = cmd->bufflen;
2333
2334 return 0;
2335}
2336
2337/**
2338 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2339 * @ap: ATA port to which the device is attached
2340 * @scsidev: SCSI device from which we derive the ATA device
2341 *
2342 * Given various information provided in struct scsi_cmnd,
2343 * map that onto an ATA bus, and using that mapping
2344 * determine which ata_device is associated with the
2345 * SCSI command to be sent.
2346 *
2347 * LOCKING:
2348 * spin_lock_irqsave(host_set lock)
2349 *
2350 * RETURNS:
2351 * Associated ATA device, or %NULL if not found.
2352 */
2353
2354static struct ata_device *
Jeff Garzik057ace52005-10-22 14:27:05 -04002355ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356{
2357 struct ata_device *dev;
2358
2359 /* skip commands not addressed to targets we simulate */
2360 if (likely(scsidev->id < ATA_MAX_DEVICES))
2361 dev = &ap->device[scsidev->id];
2362 else
2363 return NULL;
2364
2365 if (unlikely((scsidev->channel != 0) ||
2366 (scsidev->lun != 0)))
2367 return NULL;
2368
2369 if (unlikely(!ata_dev_present(dev)))
2370 return NULL;
2371
Jeff Garzik50630192005-12-13 02:29:45 -05002372 if (!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) {
2373 if (unlikely(dev->class == ATA_DEV_ATAPI)) {
2374 printk(KERN_WARNING "ata%u(%u): WARNING: ATAPI is %s, device ignored.\n",
2375 ap->id, dev->devno, atapi_enabled ? "not supported with this driver" : "disabled");
Jeff Garzik1623c812005-08-30 03:37:42 -04002376 return NULL;
Jeff Garzik50630192005-12-13 02:29:45 -05002377 }
Jeff Garzik1623c812005-08-30 03:37:42 -04002378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379
2380 return dev;
2381}
2382
Jeff Garzikb0955182005-05-12 15:45:22 -04002383/*
2384 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2385 * @byte1: Byte 1 from pass-thru CDB.
2386 *
2387 * RETURNS:
2388 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2389 */
2390static u8
2391ata_scsi_map_proto(u8 byte1)
2392{
2393 switch((byte1 & 0x1e) >> 1) {
2394 case 3: /* Non-data */
2395 return ATA_PROT_NODATA;
2396
2397 case 6: /* DMA */
2398 return ATA_PROT_DMA;
2399
2400 case 4: /* PIO Data-in */
2401 case 5: /* PIO Data-out */
Jeff Garzikb0955182005-05-12 15:45:22 -04002402 return ATA_PROT_PIO;
2403
2404 case 10: /* Device Reset */
2405 case 0: /* Hard Reset */
2406 case 1: /* SRST */
2407 case 2: /* Bus Idle */
2408 case 7: /* Packet */
2409 case 8: /* DMA Queued */
2410 case 9: /* Device Diagnostic */
2411 case 11: /* UDMA Data-in */
2412 case 12: /* UDMA Data-Out */
2413 case 13: /* FPDMA */
2414 default: /* Reserved */
2415 break;
2416 }
2417
2418 return ATA_PROT_UNKNOWN;
2419}
2420
2421/**
2422 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
2423 * @qc: command structure to be initialized
Randy Dunlap8e8b77d2005-11-01 21:29:27 -08002424 * @scsicmd: SCSI command to convert
Jeff Garzikb0955182005-05-12 15:45:22 -04002425 *
2426 * Handles either 12 or 16-byte versions of the CDB.
2427 *
2428 * RETURNS:
2429 * Zero on success, non-zero on failure.
2430 */
2431static unsigned int
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002432ata_scsi_pass_thru(struct ata_queued_cmd *qc, const u8 *scsicmd)
Jeff Garzikb0955182005-05-12 15:45:22 -04002433{
2434 struct ata_taskfile *tf = &(qc->tf);
2435 struct scsi_cmnd *cmd = qc->scsicmd;
2436
2437 if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN)
Tejun Heo9a405252005-12-02 11:49:11 +09002438 goto invalid_fld;
Jeff Garzikb0955182005-05-12 15:45:22 -04002439
Albert Leef59b0cf2006-03-16 17:59:22 +08002440 if (scsicmd[1] & 0xe0)
2441 /* PIO multi not supported yet */
2442 goto invalid_fld;
2443
Jeff Garzikb0955182005-05-12 15:45:22 -04002444 /*
2445 * 12 and 16 byte CDBs use different offsets to
2446 * provide the various register values.
2447 */
2448 if (scsicmd[0] == ATA_16) {
2449 /*
2450 * 16-byte CDB - may contain extended commands.
2451 *
2452 * If that is the case, copy the upper byte register values.
2453 */
2454 if (scsicmd[1] & 0x01) {
2455 tf->hob_feature = scsicmd[3];
2456 tf->hob_nsect = scsicmd[5];
2457 tf->hob_lbal = scsicmd[7];
2458 tf->hob_lbam = scsicmd[9];
2459 tf->hob_lbah = scsicmd[11];
2460 tf->flags |= ATA_TFLAG_LBA48;
2461 } else
2462 tf->flags &= ~ATA_TFLAG_LBA48;
2463
2464 /*
2465 * Always copy low byte, device and command registers.
2466 */
2467 tf->feature = scsicmd[4];
2468 tf->nsect = scsicmd[6];
2469 tf->lbal = scsicmd[8];
2470 tf->lbam = scsicmd[10];
2471 tf->lbah = scsicmd[12];
2472 tf->device = scsicmd[13];
2473 tf->command = scsicmd[14];
2474 } else {
2475 /*
2476 * 12-byte CDB - incapable of extended commands.
2477 */
2478 tf->flags &= ~ATA_TFLAG_LBA48;
2479
2480 tf->feature = scsicmd[3];
2481 tf->nsect = scsicmd[4];
2482 tf->lbal = scsicmd[5];
2483 tf->lbam = scsicmd[6];
2484 tf->lbah = scsicmd[7];
2485 tf->device = scsicmd[8];
2486 tf->command = scsicmd[9];
2487 }
Mark Lorddcc2d1e2005-11-13 16:22:06 -05002488 /*
2489 * If slave is possible, enforce correct master/slave bit
2490 */
2491 if (qc->ap->flags & ATA_FLAG_SLAVE_POSS)
2492 tf->device = qc->dev->devno ?
2493 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
Jeff Garzikb0955182005-05-12 15:45:22 -04002494
2495 /*
2496 * Filter SET_FEATURES - XFER MODE command -- otherwise,
2497 * SET_FEATURES - XFER MODE must be preceded/succeeded
2498 * by an update to hardware-specific registers for each
2499 * controller (i.e. the reason for ->set_piomode(),
2500 * ->set_dmamode(), and ->post_set_mode() hooks).
2501 */
2502 if ((tf->command == ATA_CMD_SET_FEATURES)
2503 && (tf->feature == SETFEATURES_XFER))
Tejun Heo9a405252005-12-02 11:49:11 +09002504 goto invalid_fld;
Jeff Garzikb0955182005-05-12 15:45:22 -04002505
2506 /*
2507 * Set flags so that all registers will be written,
2508 * and pass on write indication (used for PIO/DMA
2509 * setup.)
2510 */
2511 tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
2512
Jeff Garzik0274aa22005-06-22 13:50:56 -04002513 if (cmd->sc_data_direction == DMA_TO_DEVICE)
Jeff Garzikb0955182005-05-12 15:45:22 -04002514 tf->flags |= ATA_TFLAG_WRITE;
2515
2516 /*
2517 * Set transfer length.
2518 *
2519 * TODO: find out if we need to do more here to
2520 * cover scatter/gather case.
2521 */
2522 qc->nsect = cmd->bufflen / ATA_SECT_SIZE;
2523
2524 return 0;
Tejun Heo9a405252005-12-02 11:49:11 +09002525
2526 invalid_fld:
2527 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x00);
2528 /* "Invalid field in cdb" */
2529 return 1;
Jeff Garzikb0955182005-05-12 15:45:22 -04002530}
2531
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532/**
2533 * ata_get_xlat_func - check if SCSI to ATA translation is possible
2534 * @dev: ATA device
2535 * @cmd: SCSI command opcode to consider
2536 *
2537 * Look up the SCSI command given, and determine whether the
2538 * SCSI command is to be translated or simulated.
2539 *
2540 * RETURNS:
2541 * Pointer to translation function if possible, %NULL if not.
2542 */
2543
2544static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
2545{
2546 switch (cmd) {
2547 case READ_6:
2548 case READ_10:
2549 case READ_16:
2550
2551 case WRITE_6:
2552 case WRITE_10:
2553 case WRITE_16:
2554 return ata_scsi_rw_xlat;
2555
2556 case SYNCHRONIZE_CACHE:
2557 if (ata_try_flush_cache(dev))
2558 return ata_scsi_flush_xlat;
2559 break;
2560
2561 case VERIFY:
2562 case VERIFY_16:
2563 return ata_scsi_verify_xlat;
Jeff Garzikb0955182005-05-12 15:45:22 -04002564
2565 case ATA_12:
2566 case ATA_16:
2567 return ata_scsi_pass_thru;
Jeff Garzikda613962005-08-29 19:01:43 -04002568
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04002569 case START_STOP:
2570 return ata_scsi_start_stop_xlat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 }
2572
2573 return NULL;
2574}
2575
2576/**
2577 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
2578 * @ap: ATA port to which the command was being sent
2579 * @cmd: SCSI command to dump
2580 *
2581 * Prints the contents of a SCSI command via printk().
2582 */
2583
2584static inline void ata_scsi_dump_cdb(struct ata_port *ap,
2585 struct scsi_cmnd *cmd)
2586{
2587#ifdef ATA_DEBUG
2588 struct scsi_device *scsidev = cmd->device;
2589 u8 *scsicmd = cmd->cmnd;
2590
2591 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
2592 ap->id,
2593 scsidev->channel, scsidev->id, scsidev->lun,
2594 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
2595 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
2596 scsicmd[8]);
2597#endif
2598}
2599
2600/**
2601 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
2602 * @cmd: SCSI command to be sent
2603 * @done: Completion function, called when command is complete
2604 *
2605 * In some cases, this function translates SCSI commands into
2606 * ATA taskfiles, and queues the taskfiles to be sent to
2607 * hardware. In other cases, this function simulates a
2608 * SCSI device by evaluating and responding to certain
2609 * SCSI commands. This creates the overall effect of
2610 * ATA and ATAPI devices appearing as SCSI devices.
2611 *
2612 * LOCKING:
2613 * Releases scsi-layer-held lock, and obtains host_set lock.
2614 *
2615 * RETURNS:
2616 * Zero.
2617 */
2618
2619int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
2620{
2621 struct ata_port *ap;
2622 struct ata_device *dev;
2623 struct scsi_device *scsidev = cmd->device;
Jeff Garzik005a5a02005-10-30 23:31:48 -05002624 struct Scsi_Host *shost = scsidev->host;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625
Jeff Garzik005a5a02005-10-30 23:31:48 -05002626 ap = (struct ata_port *) &shost->hostdata[0];
2627
2628 spin_unlock(shost->host_lock);
2629 spin_lock(&ap->host_set->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630
2631 ata_scsi_dump_cdb(ap, cmd);
2632
2633 dev = ata_scsi_find_dev(ap, scsidev);
2634 if (unlikely(!dev)) {
2635 cmd->result = (DID_BAD_TARGET << 16);
2636 done(cmd);
2637 goto out_unlock;
2638 }
2639
2640 if (dev->class == ATA_DEV_ATA) {
2641 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
2642 cmd->cmnd[0]);
2643
2644 if (xlat_func)
2645 ata_scsi_translate(ap, dev, cmd, done, xlat_func);
2646 else
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002647 ata_scsi_simulate(ap, dev, cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648 } else
2649 ata_scsi_translate(ap, dev, cmd, done, atapi_xlat);
2650
2651out_unlock:
Jeff Garzik005a5a02005-10-30 23:31:48 -05002652 spin_unlock(&ap->host_set->lock);
2653 spin_lock(shost->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 return 0;
2655}
2656
2657/**
2658 * ata_scsi_simulate - simulate SCSI command on ATA device
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002659 * @ap: port the device is connected to
2660 * @dev: the target device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 * @cmd: SCSI command being sent to device.
2662 * @done: SCSI command completion function.
2663 *
2664 * Interprets and directly executes a select list of SCSI commands
2665 * that can be handled internally.
2666 *
2667 * LOCKING:
2668 * spin_lock_irqsave(host_set lock)
2669 */
2670
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002671void ata_scsi_simulate(struct ata_port *ap, struct ata_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 struct scsi_cmnd *cmd,
2673 void (*done)(struct scsi_cmnd *))
2674{
2675 struct ata_scsi_args args;
Jeff Garzik057ace52005-10-22 14:27:05 -04002676 const u8 *scsicmd = cmd->cmnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002678 args.ap = ap;
2679 args.dev = dev;
2680 args.id = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002681 args.cmd = cmd;
2682 args.done = done;
2683
2684 switch(scsicmd[0]) {
2685 /* no-op's, complete with success */
2686 case SYNCHRONIZE_CACHE:
2687 case REZERO_UNIT:
2688 case SEEK_6:
2689 case SEEK_10:
2690 case TEST_UNIT_READY:
2691 case FORMAT_UNIT: /* FIXME: correct? */
2692 case SEND_DIAGNOSTIC: /* FIXME: correct? */
2693 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
2694 break;
2695
2696 case INQUIRY:
2697 if (scsicmd[1] & 2) /* is CmdDt set? */
Douglas Gilbertae006512005-10-09 09:09:35 -04002698 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
2700 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
2701 else if (scsicmd[2] == 0x00)
2702 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
2703 else if (scsicmd[2] == 0x80)
2704 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
2705 else if (scsicmd[2] == 0x83)
2706 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
2707 else
Douglas Gilbertae006512005-10-09 09:09:35 -04002708 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002709 break;
2710
2711 case MODE_SENSE:
2712 case MODE_SENSE_10:
2713 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
2714 break;
2715
2716 case MODE_SELECT: /* unconditionally return */
2717 case MODE_SELECT_10: /* bad-field-in-cdb */
Douglas Gilbertae006512005-10-09 09:09:35 -04002718 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 break;
2720
2721 case READ_CAPACITY:
2722 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2723 break;
2724
2725 case SERVICE_ACTION_IN:
2726 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
2727 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2728 else
Douglas Gilbertae006512005-10-09 09:09:35 -04002729 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 break;
2731
2732 case REPORT_LUNS:
2733 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
2734 break;
2735
Jeff Garzikb0955182005-05-12 15:45:22 -04002736 /* mandatory commands we haven't implemented yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002737 case REQUEST_SENSE:
2738
2739 /* all other commands */
2740 default:
Douglas Gilbertae006512005-10-09 09:09:35 -04002741 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
2742 /* "Invalid command operation code" */
2743 done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 break;
2745 }
2746}
2747
Jeff Garzik644dd0c2005-10-03 15:55:19 -04002748void ata_scsi_scan_host(struct ata_port *ap)
2749{
Jeff Garzik3f19ee82005-10-03 21:36:41 -04002750 struct ata_device *dev;
Jeff Garzik644dd0c2005-10-03 15:55:19 -04002751 unsigned int i;
2752
2753 if (ap->flags & ATA_FLAG_PORT_DISABLED)
2754 return;
2755
Jeff Garzik3f19ee82005-10-03 21:36:41 -04002756 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2757 dev = &ap->device[i];
2758
2759 if (ata_dev_present(dev))
2760 scsi_scan_target(&ap->host->shost_gendev, 0, i, 0, 0);
2761 }
Jeff Garzik644dd0c2005-10-03 15:55:19 -04002762}
2763