blob: d151cf057ef2f1e458df00e717ea8d999dbfe601 [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>
Christoph Hellwigbeb40482006-06-10 18:01:03 +020041#include <scsi/scsi_cmnd.h>
Mike Christie85837eb2005-11-11 16:38:53 -060042#include <scsi/scsi_eh.h>
Jeff Garzik005a5a02005-10-30 23:31:48 -050043#include <scsi/scsi_device.h>
Tejun Heoa6e6ce82006-05-15 21:03:48 +090044#include <scsi/scsi_tcq.h>
Tejun Heo30afc842006-03-18 18:40:14 +090045#include <scsi/scsi_transport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <linux/libata.h>
Jeff Garzikb0955182005-05-12 15:45:22 -040047#include <linux/hdreg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <asm/uaccess.h>
49
50#include "libata.h"
51
Jeff Garzikb0955182005-05-12 15:45:22 -040052#define SECTOR_SIZE 512
53
Tejun Heoad706992006-12-17 10:45:57 +090054typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
Tejun Heoab5b3a52006-05-31 18:27:34 +090055
56static struct ata_device * __ata_scsi_find_dev(struct ata_port *ap,
57 const struct scsi_device *scsidev);
58static struct ata_device * ata_scsi_find_dev(struct ata_port *ap,
59 const struct scsi_device *scsidev);
Tejun Heo83c47bc2006-05-31 18:28:07 +090060static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
61 unsigned int id, unsigned int lun);
Tejun Heoab5b3a52006-05-31 18:27:34 +090062
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040064#define RW_RECOVERY_MPAGE 0x1
65#define RW_RECOVERY_MPAGE_LEN 12
66#define CACHE_MPAGE 0x8
67#define CACHE_MPAGE_LEN 20
68#define CONTROL_MPAGE 0xa
69#define CONTROL_MPAGE_LEN 12
70#define ALL_MPAGES 0x3f
71#define ALL_SUB_MPAGES 0xff
72
73
74static const u8 def_rw_recovery_mpage[] = {
75 RW_RECOVERY_MPAGE,
76 RW_RECOVERY_MPAGE_LEN - 2,
77 (1 << 7) | /* AWRE, sat-r06 say it shall be 0 */
78 (1 << 6), /* ARRE (auto read reallocation) */
79 0, /* read retry count */
80 0, 0, 0, 0,
81 0, /* write retry count */
82 0, 0, 0
83};
84
85static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
86 CACHE_MPAGE,
87 CACHE_MPAGE_LEN - 2,
88 0, /* contains WCE, needs to be 0 for logic */
89 0, 0, 0, 0, 0, 0, 0, 0, 0,
90 0, /* contains DRA, needs to be 0 for logic */
91 0, 0, 0, 0, 0, 0, 0
92};
93
94static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
95 CONTROL_MPAGE,
96 CONTROL_MPAGE_LEN - 2,
97 2, /* DSENSE=0, GLTSD=1 */
98 0, /* [QAM+QERR may be 1, see 05-359r1] */
99 0, 0, 0, 0, 0xff, 0xff,
100 0, 30 /* extended self test time, see 05-359r1 */
101};
102
Tejun Heo30afc842006-03-18 18:40:14 +0900103/*
104 * libata transport template. libata doesn't do real transport stuff.
105 * It just needs the eh_timed_out hook.
106 */
107struct scsi_transport_template ata_scsi_transport_template = {
Christoph Hellwig9227c332006-04-01 19:21:04 +0200108 .eh_strategy_handler = ata_scsi_error,
Tejun Heo30afc842006-03-18 18:40:14 +0900109 .eh_timed_out = ata_scsi_timed_out,
Tejun Heoccf68c32006-05-31 18:28:09 +0900110 .user_scan = ata_scsi_user_scan,
Tejun Heo30afc842006-03-18 18:40:14 +0900111};
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Douglas Gilbertae006512005-10-09 09:09:35 -0400114static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
115 void (*done)(struct scsi_cmnd *))
116{
117 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
118 /* "Invalid field in cbd" */
119 done(cmd);
120}
121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122/**
123 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
124 * @sdev: SCSI device for which BIOS geometry is to be determined
125 * @bdev: block device associated with @sdev
126 * @capacity: capacity of SCSI device
127 * @geom: location to which geometry will be output
128 *
129 * Generic bios head/sector/cylinder calculator
130 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
131 * mapping. Some situations may arise where the disk is not
132 * bootable if this is not used.
133 *
134 * LOCKING:
135 * Defined by the SCSI layer. We don't really care.
136 *
137 * RETURNS:
138 * Zero.
139 */
140int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
141 sector_t capacity, int geom[])
142{
143 geom[0] = 255;
144 geom[1] = 63;
145 sector_div(capacity, 255*63);
146 geom[2] = capacity;
147
148 return 0;
149}
150
Jeff Garzikb0955182005-05-12 15:45:22 -0400151/**
152 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800153 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400154 * @arg: User provided data for issuing command
155 *
156 * LOCKING:
157 * Defined by the SCSI layer. We don't really care.
158 *
159 * RETURNS:
160 * Zero on success, negative errno on error.
161 */
162
163int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
164{
165 int rc = 0;
166 u8 scsi_cmd[MAX_COMMAND_SIZE];
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700167 u8 args[4], *argbuf = NULL, *sensebuf = NULL;
Jeff Garzikb0955182005-05-12 15:45:22 -0400168 int argsize = 0;
Mike Christie85837eb2005-11-11 16:38:53 -0600169 enum dma_data_direction data_dir;
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700170 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400171
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500172 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400173 return -EINVAL;
174
175 if (copy_from_user(args, arg, sizeof(args)))
176 return -EFAULT;
177
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700178 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
179 if (!sensebuf)
180 return -ENOMEM;
181
Jeff Garzikb0955182005-05-12 15:45:22 -0400182 memset(scsi_cmd, 0, sizeof(scsi_cmd));
183
184 if (args[3]) {
185 argsize = SECTOR_SIZE * args[3];
186 argbuf = kmalloc(argsize, GFP_KERNEL);
Jeff Raubitschek54dac832005-10-04 10:21:19 -0400187 if (argbuf == NULL) {
188 rc = -ENOMEM;
189 goto error;
190 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400191
192 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
193 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
194 block count in sector count field */
Mike Christie85837eb2005-11-11 16:38:53 -0600195 data_dir = DMA_FROM_DEVICE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400196 } else {
197 scsi_cmd[1] = (3 << 1); /* Non-data */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700198 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Mike Christie85837eb2005-11-11 16:38:53 -0600199 data_dir = DMA_NONE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400200 }
201
202 scsi_cmd[0] = ATA_16;
203
204 scsi_cmd[4] = args[2];
205 if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
206 scsi_cmd[6] = args[3];
207 scsi_cmd[8] = args[1];
208 scsi_cmd[10] = 0x4f;
209 scsi_cmd[12] = 0xc2;
210 } else {
211 scsi_cmd[6] = args[1];
212 }
213 scsi_cmd[14] = args[0];
214
215 /* Good values for timeout and retries? Values below
216 from scsi_ioctl_send_command() for default case... */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700217 cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
218 sensebuf, (10*HZ), 5, 0);
219
220 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
221 u8 *desc = sensebuf + 8;
222 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
223
224 /* If we set cc then ATA pass-through will cause a
225 * check condition even if no error. Filter that. */
226 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
227 struct scsi_sense_hdr sshdr;
228 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
229 &sshdr);
230 if (sshdr.sense_key==0 &&
231 sshdr.asc==0 && sshdr.ascq==0)
232 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
233 }
234
235 /* Send userspace a few ATA registers (same as drivers/ide) */
236 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
237 desc[0] == 0x09 ) { /* code is "ATA Descriptor" */
238 args[0] = desc[13]; /* status */
239 args[1] = desc[3]; /* error */
240 args[2] = desc[5]; /* sector count (0:7) */
241 if (copy_to_user(arg, args, sizeof(args)))
242 rc = -EFAULT;
243 }
244 }
245
246
247 if (cmd_result) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400248 rc = -EIO;
249 goto error;
250 }
251
Jeff Garzikb0955182005-05-12 15:45:22 -0400252 if ((argbuf)
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500253 && copy_to_user(arg + sizeof(args), argbuf, argsize))
Jeff Garzikb0955182005-05-12 15:45:22 -0400254 rc = -EFAULT;
255error:
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700256 kfree(sensebuf);
Jesper Juhl8f760782006-06-27 02:55:06 -0700257 kfree(argbuf);
Jeff Garzikb0955182005-05-12 15:45:22 -0400258 return rc;
259}
260
261/**
262 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800263 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400264 * @arg: User provided data for issuing command
265 *
266 * LOCKING:
267 * Defined by the SCSI layer. We don't really care.
268 *
269 * RETURNS:
270 * Zero on success, negative errno on error.
271 */
272int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
273{
274 int rc = 0;
275 u8 scsi_cmd[MAX_COMMAND_SIZE];
David Milburnaf068bd2007-01-30 00:59:15 -0800276 u8 args[7], *sensebuf = NULL;
277 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400278
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500279 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400280 return -EINVAL;
281
282 if (copy_from_user(args, arg, sizeof(args)))
283 return -EFAULT;
284
David Milburnaf068bd2007-01-30 00:59:15 -0800285 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
286 if (!sensebuf)
287 return -ENOMEM;
288
Jeff Garzikb0955182005-05-12 15:45:22 -0400289 memset(scsi_cmd, 0, sizeof(scsi_cmd));
290 scsi_cmd[0] = ATA_16;
291 scsi_cmd[1] = (3 << 1); /* Non-data */
David Milburnaf068bd2007-01-30 00:59:15 -0800292 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Jeff Garzikb0955182005-05-12 15:45:22 -0400293 scsi_cmd[4] = args[1];
294 scsi_cmd[6] = args[2];
295 scsi_cmd[8] = args[3];
296 scsi_cmd[10] = args[4];
297 scsi_cmd[12] = args[5];
298 scsi_cmd[14] = args[0];
299
Jeff Garzikb0955182005-05-12 15:45:22 -0400300 /* Good values for timeout and retries? Values below
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500301 from scsi_ioctl_send_command() for default case... */
David Milburnaf068bd2007-01-30 00:59:15 -0800302 cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
303 sensebuf, (10*HZ), 5, 0);
Jeff Garzikb0955182005-05-12 15:45:22 -0400304
David Milburnaf068bd2007-01-30 00:59:15 -0800305 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
306 u8 *desc = sensebuf + 8;
307 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
308
309 /* If we set cc then ATA pass-through will cause a
310 * check condition even if no error. Filter that. */
311 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
312 struct scsi_sense_hdr sshdr;
313 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
314 &sshdr);
315 if (sshdr.sense_key==0 &&
316 sshdr.asc==0 && sshdr.ascq==0)
317 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
318 }
319
320 /* Send userspace ATA registers */
321 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
322 desc[0] == 0x09) {/* code is "ATA Descriptor" */
323 args[0] = desc[13]; /* status */
324 args[1] = desc[3]; /* error */
325 args[2] = desc[5]; /* sector count (0:7) */
326 args[3] = desc[7]; /* lbal */
327 args[4] = desc[9]; /* lbam */
328 args[5] = desc[11]; /* lbah */
329 args[6] = desc[12]; /* select */
330 if (copy_to_user(arg, args, sizeof(args)))
331 rc = -EFAULT;
332 }
333 }
334
335 if (cmd_result) {
336 rc = -EIO;
337 goto error;
338 }
339
340 error:
341 kfree(sensebuf);
Jeff Garzikb0955182005-05-12 15:45:22 -0400342 return rc;
343}
344
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
346{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 int val = -EINVAL, rc = -EINVAL;
348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 switch (cmd) {
350 case ATA_IOC_GET_IO32:
351 val = 0;
352 if (copy_to_user(arg, &val, 1))
353 return -EFAULT;
354 return 0;
355
356 case ATA_IOC_SET_IO32:
357 val = (unsigned long) arg;
358 if (val != 0)
359 return -EINVAL;
360 return 0;
361
Jeff Garzikb0955182005-05-12 15:45:22 -0400362 case HDIO_DRIVE_CMD:
363 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
364 return -EACCES;
365 return ata_cmd_ioctl(scsidev, arg);
366
367 case HDIO_DRIVE_TASK:
368 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
369 return -EACCES;
370 return ata_task_ioctl(scsidev, arg);
371
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 default:
373 rc = -ENOTTY;
374 break;
375 }
376
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 return rc;
378}
379
380/**
381 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 * @dev: ATA device to which the new command is attached
383 * @cmd: SCSI command that originated this ATA command
384 * @done: SCSI command completion function
385 *
386 * Obtain a reference to an unused ata_queued_cmd structure,
387 * which is the basic libata structure representing a single
388 * ATA command sent to the hardware.
389 *
390 * If a command was available, fill in the SCSI-specific
391 * portions of the structure with information on the
392 * current command.
393 *
394 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400395 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 *
397 * RETURNS:
398 * Command allocated, or %NULL if none available.
399 */
Tejun Heo3373efd2006-05-15 20:57:53 +0900400struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct scsi_cmnd *cmd,
402 void (*done)(struct scsi_cmnd *))
403{
404 struct ata_queued_cmd *qc;
405
Tejun Heo3373efd2006-05-15 20:57:53 +0900406 qc = ata_qc_new_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (qc) {
408 qc->scsicmd = cmd;
409 qc->scsidone = done;
410
411 if (cmd->use_sg) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -0400412 qc->__sg = (struct scatterlist *) cmd->request_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 qc->n_elem = cmd->use_sg;
Brian King7a801182007-01-17 12:32:12 -0600414 } else if (cmd->request_bufflen) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -0400415 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 qc->n_elem = 1;
417 }
418 } else {
419 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
420 done(cmd);
421 }
422
423 return qc;
424}
425
426/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400427 * ata_dump_status - user friendly display of error info
428 * @id: id of the port in question
429 * @tf: ptr to filled out taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 *
Jeff Garzikb0955182005-05-12 15:45:22 -0400431 * Decode and dump the ATA error/status registers for the user so
432 * that they have some idea what really happened at the non
433 * make-believe layer.
434 *
435 * LOCKING:
436 * inherited from caller
437 */
438void ata_dump_status(unsigned id, struct ata_taskfile *tf)
439{
440 u8 stat = tf->command, err = tf->feature;
441
442 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
443 if (stat & ATA_BUSY) {
444 printk("Busy }\n"); /* Data is not valid in this case */
445 } else {
446 if (stat & 0x40) printk("DriveReady ");
447 if (stat & 0x20) printk("DeviceFault ");
448 if (stat & 0x10) printk("SeekComplete ");
449 if (stat & 0x08) printk("DataRequest ");
450 if (stat & 0x04) printk("CorrectedError ");
451 if (stat & 0x02) printk("Index ");
452 if (stat & 0x01) printk("Error ");
453 printk("}\n");
454
455 if (err) {
456 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
457 if (err & 0x04) printk("DriveStatusError ");
458 if (err & 0x80) {
459 if (err & 0x04) printk("BadCRC ");
460 else printk("Sector ");
461 }
462 if (err & 0x40) printk("UncorrectableError ");
463 if (err & 0x10) printk("SectorIdNotFound ");
464 if (err & 0x02) printk("TrackZeroNotFound ");
465 if (err & 0x01) printk("AddrMarkNotFound ");
466 printk("}\n");
467 }
468 }
469}
470
Tejun Heod6f26d12006-07-03 16:07:26 +0900471/**
472 * ata_scsi_device_suspend - suspend ATA device associated with sdev
473 * @sdev: the SCSI device to suspend
Tejun Heo3c5100c2006-07-26 16:58:33 +0900474 * @mesg: target power management message
Tejun Heod6f26d12006-07-03 16:07:26 +0900475 *
476 * Request suspend EH action on the ATA device associated with
477 * @sdev and wait for the operation to complete.
478 *
479 * LOCKING:
480 * Kernel thread context (may sleep).
481 *
482 * RETURNS:
483 * 0 on success, -errno otherwise.
484 */
Tejun Heo3c5100c2006-07-26 16:58:33 +0900485int ata_scsi_device_suspend(struct scsi_device *sdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +0100486{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400487 struct ata_port *ap = ata_shost_to_port(sdev->host);
Tejun Heod6f26d12006-07-03 16:07:26 +0900488 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
489 unsigned long flags;
490 unsigned int action;
491 int rc = 0;
Jens Axboe9b847542006-01-06 09:28:07 +0100492
Tejun Heod6f26d12006-07-03 16:07:26 +0900493 if (!dev)
494 goto out;
495
496 spin_lock_irqsave(ap->lock, flags);
497
498 /* wait for the previous resume to complete */
499 while (dev->flags & ATA_DFLAG_SUSPENDED) {
500 spin_unlock_irqrestore(ap->lock, flags);
501 ata_port_wait_eh(ap);
502 spin_lock_irqsave(ap->lock, flags);
503 }
504
505 /* if @sdev is already detached, nothing to do */
506 if (sdev->sdev_state == SDEV_OFFLINE ||
507 sdev->sdev_state == SDEV_CANCEL || sdev->sdev_state == SDEV_DEL)
508 goto out_unlock;
509
510 /* request suspend */
511 action = ATA_EH_SUSPEND;
Tejun Heo3c5100c2006-07-26 16:58:33 +0900512 if (mesg.event != PM_EVENT_SUSPEND)
Tejun Heod6f26d12006-07-03 16:07:26 +0900513 action |= ATA_EH_PM_FREEZE;
514 ap->eh_info.dev_action[dev->devno] |= action;
515 ap->eh_info.flags |= ATA_EHI_QUIET;
516 ata_port_schedule_eh(ap);
517
518 spin_unlock_irqrestore(ap->lock, flags);
519
520 /* wait for EH to do the job */
521 ata_port_wait_eh(ap);
522
523 spin_lock_irqsave(ap->lock, flags);
524
525 /* If @sdev is still attached but the associated ATA device
526 * isn't suspended, the operation failed.
527 */
528 if (sdev->sdev_state != SDEV_OFFLINE &&
529 sdev->sdev_state != SDEV_CANCEL && sdev->sdev_state != SDEV_DEL &&
530 !(dev->flags & ATA_DFLAG_SUSPENDED))
531 rc = -EIO;
532
533 out_unlock:
534 spin_unlock_irqrestore(ap->lock, flags);
535 out:
536 if (rc == 0)
Tejun Heo3c5100c2006-07-26 16:58:33 +0900537 sdev->sdev_gendev.power.power_state = mesg;
Tejun Heod6f26d12006-07-03 16:07:26 +0900538 return rc;
539}
540
541/**
542 * ata_scsi_device_resume - resume ATA device associated with sdev
543 * @sdev: the SCSI device to resume
544 *
545 * Request resume EH action on the ATA device associated with
546 * @sdev and return immediately. This enables parallel
547 * wakeup/spinup of devices.
548 *
549 * LOCKING:
550 * Kernel thread context (may sleep).
551 *
552 * RETURNS:
553 * 0.
554 */
555int ata_scsi_device_resume(struct scsi_device *sdev)
556{
557 struct ata_port *ap = ata_shost_to_port(sdev->host);
558 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
559 struct ata_eh_info *ehi = &ap->eh_info;
560 unsigned long flags;
561 unsigned int action;
562
563 if (!dev)
564 goto out;
565
566 spin_lock_irqsave(ap->lock, flags);
567
568 /* if @sdev is already detached, nothing to do */
569 if (sdev->sdev_state == SDEV_OFFLINE ||
570 sdev->sdev_state == SDEV_CANCEL || sdev->sdev_state == SDEV_DEL)
571 goto out_unlock;
572
573 /* request resume */
574 action = ATA_EH_RESUME;
575 if (sdev->sdev_gendev.power.power_state.event == PM_EVENT_SUSPEND)
576 __ata_ehi_hotplugged(ehi);
577 else
578 action |= ATA_EH_PM_FREEZE | ATA_EH_SOFTRESET;
579 ehi->dev_action[dev->devno] |= action;
580
581 /* We don't want autopsy and verbose EH messages. Disable
582 * those if we're the only device on this link.
583 */
584 if (ata_port_max_devices(ap) == 1)
585 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
586
587 ata_port_schedule_eh(ap);
588
589 out_unlock:
590 spin_unlock_irqrestore(ap->lock, flags);
591 out:
592 sdev->sdev_gendev.power.power_state = PMSG_ON;
593 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +0100594}
595
Jeff Garzikb0955182005-05-12 15:45:22 -0400596/**
597 * ata_to_sense_error - convert ATA error to SCSI error
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800598 * @id: ATA device number
Jeff Garzikb0955182005-05-12 15:45:22 -0400599 * @drv_stat: value contained in ATA status register
600 * @drv_err: value contained in ATA error register
601 * @sk: the sense key we'll fill out
602 * @asc: the additional sense code we'll fill out
603 * @ascq: the additional sense code qualifier we'll fill out
Tejun Heo246619d2006-05-15 20:58:16 +0900604 * @verbose: be verbose
Jeff Garzikb0955182005-05-12 15:45:22 -0400605 *
606 * Converts an ATA error into a SCSI error. Fill out pointers to
607 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
608 * format sense blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 *
610 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400611 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500613void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
Tejun Heo246619d2006-05-15 20:58:16 +0900614 u8 *ascq, int verbose)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Jeff Garzikb0955182005-05-12 15:45:22 -0400616 int i;
Jeff Garzikffe75ef2005-10-09 10:40:44 -0400617
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 /* Based on the 3ware driver translation table */
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100619 static const unsigned char sense_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /* BBD|ECC|ID|MAR */
621 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
622 /* BBD|ECC|ID */
623 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
624 /* ECC|MC|MARK */
625 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
626 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
627 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
628 /* MC|ID|ABRT|TRK0|MARK */
629 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
630 /* MCR|MARK */
631 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
632 /* Bad address mark */
633 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
634 /* TRK0 */
635 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
636 /* Abort & !ICRC */
637 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
638 /* Media change request */
639 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
640 /* SRV */
641 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
642 /* Media change */
643 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
644 /* ECC */
645 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
646 /* BBD - block marked bad */
647 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
648 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
649 };
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100650 static const unsigned char stat_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 /* Must be first because BUSY means no other bits valid */
652 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
653 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
654 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
655 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
656 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
657 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /*
660 * Is this an error we can process/parse
661 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400662 if (drv_stat & ATA_BUSY) {
663 drv_err = 0; /* Ignore the err bits, they're invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
Jeff Garzikb0955182005-05-12 15:45:22 -0400666 if (drv_err) {
667 /* Look for drv_err */
668 for (i = 0; sense_table[i][0] != 0xFF; i++) {
669 /* Look for best matches first */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500670 if ((sense_table[i][0] & drv_err) ==
Jeff Garzikb0955182005-05-12 15:45:22 -0400671 sense_table[i][0]) {
672 *sk = sense_table[i][1];
673 *asc = sense_table[i][2];
674 *ascq = sense_table[i][3];
675 goto translate_done;
676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400678 /* No immediate match */
Tejun Heo246619d2006-05-15 20:58:16 +0900679 if (verbose)
680 printk(KERN_WARNING "ata%u: no sense translation for "
681 "error 0x%02x\n", id, drv_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 /* Fall back to interpreting status bits */
Jeff Garzikb0955182005-05-12 15:45:22 -0400685 for (i = 0; stat_table[i][0] != 0xFF; i++) {
686 if (stat_table[i][0] & drv_stat) {
687 *sk = stat_table[i][1];
688 *asc = stat_table[i][2];
689 *ascq = stat_table[i][3];
690 goto translate_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400693 /* No error? Undecoded? */
Tejun Heo246619d2006-05-15 20:58:16 +0900694 if (verbose)
695 printk(KERN_WARNING "ata%u: no sense translation for "
696 "status: 0x%02x\n", id, drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697
Alan Cox2d202022006-03-21 15:53:46 +0000698 /* We need a sensible error return here, which is tricky, and one
699 that won't cause people to do things like return a disk wrongly */
700 *sk = ABORTED_COMMAND;
701 *asc = 0x00;
702 *ascq = 0x00;
Jeff Garzikb0955182005-05-12 15:45:22 -0400703
704 translate_done:
Tejun Heo246619d2006-05-15 20:58:16 +0900705 if (verbose)
706 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x "
707 "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
708 id, drv_stat, drv_err, *sk, *asc, *ascq);
Jeff Garzikb0955182005-05-12 15:45:22 -0400709 return;
710}
711
712/*
Tejun Heo750426a2006-11-14 22:37:35 +0900713 * ata_gen_passthru_sense - Generate check condition sense block.
Jeff Garzikb0955182005-05-12 15:45:22 -0400714 * @qc: Command that completed.
715 *
716 * This function is specific to the ATA descriptor format sense
717 * block specified for the ATA pass through commands. Regardless
718 * of whether the command errored or not, return a sense
719 * block. Copy all controller registers into the sense
720 * block. Clear sense key, ASC & ASCQ if there is no error.
721 *
722 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +0900723 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -0400724 */
Tejun Heo750426a2006-11-14 22:37:35 +0900725static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -0400726{
727 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +0900728 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -0400729 unsigned char *sb = cmd->sense_buffer;
730 unsigned char *desc = sb + 8;
Tejun Heo246619d2006-05-15 20:58:16 +0900731 int verbose = qc->ap->ops->error_handler == NULL;
Jeff Garzikb0955182005-05-12 15:45:22 -0400732
733 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
734
Jeff Garzik0e5dec42005-10-06 09:40:20 -0400735 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -0400736
737 /*
Jeff Garzikb0955182005-05-12 15:45:22 -0400738 * Use ata_to_sense_error() to map status register bits
739 * onto sense key, asc & ascq.
740 */
Tejun Heo058e55e2006-04-02 18:51:53 +0900741 if (qc->err_mask ||
742 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400743 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
Tejun Heo246619d2006-05-15 20:58:16 +0900744 &sb[1], &sb[2], &sb[3], verbose);
Jeff Garzikb0955182005-05-12 15:45:22 -0400745 sb[1] &= 0x0f;
746 }
747
748 /*
749 * Sense data is current and format is descriptor.
750 */
751 sb[0] = 0x72;
752
753 desc[0] = 0x09;
754
Tejun Heof38621b2006-11-14 22:37:35 +0900755 /* set length of additional sense data */
756 sb[7] = 14;
757 desc[1] = 12;
Jeff Garzikb0955182005-05-12 15:45:22 -0400758
759 /*
760 * Copy registers into sense buffer.
761 */
762 desc[2] = 0x00;
763 desc[3] = tf->feature; /* == error reg */
764 desc[5] = tf->nsect;
765 desc[7] = tf->lbal;
766 desc[9] = tf->lbam;
767 desc[11] = tf->lbah;
768 desc[12] = tf->device;
769 desc[13] = tf->command; /* == status reg */
770
771 /*
772 * Fill in Extend bit, and the high order bytes
773 * if applicable.
774 */
775 if (tf->flags & ATA_TFLAG_LBA48) {
776 desc[2] |= 0x01;
777 desc[4] = tf->hob_nsect;
778 desc[6] = tf->hob_lbal;
779 desc[8] = tf->hob_lbam;
780 desc[10] = tf->hob_lbah;
781 }
782}
783
784/**
Tejun Heo750426a2006-11-14 22:37:35 +0900785 * ata_gen_ata_sense - generate a SCSI fixed sense block
Jeff Garzikb0955182005-05-12 15:45:22 -0400786 * @qc: Command that we are erroring out
787 *
Tejun Heod25614b2006-11-14 22:37:35 +0900788 * Generate sense block for a failed ATA command @qc. Descriptor
789 * format is used to accomodate LBA48 block address.
Jeff Garzikb0955182005-05-12 15:45:22 -0400790 *
791 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +0900792 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -0400793 */
Tejun Heo750426a2006-11-14 22:37:35 +0900794static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -0400795{
Tejun Heod25614b2006-11-14 22:37:35 +0900796 struct ata_device *dev = qc->dev;
Jeff Garzikb0955182005-05-12 15:45:22 -0400797 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +0900798 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -0400799 unsigned char *sb = cmd->sense_buffer;
Tejun Heod25614b2006-11-14 22:37:35 +0900800 unsigned char *desc = sb + 8;
Tejun Heo246619d2006-05-15 20:58:16 +0900801 int verbose = qc->ap->ops->error_handler == NULL;
Tejun Heod25614b2006-11-14 22:37:35 +0900802 u64 block;
Jeff Garzikb0955182005-05-12 15:45:22 -0400803
804 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
805
Jeff Garzik0e5dec42005-10-06 09:40:20 -0400806 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -0400807
Tejun Heod25614b2006-11-14 22:37:35 +0900808 /* sense data is current and format is descriptor */
809 sb[0] = 0x72;
810
811 /* Use ata_to_sense_error() to map status register bits
Jeff Garzikb0955182005-05-12 15:45:22 -0400812 * onto sense key, asc & ascq.
813 */
Tejun Heo058e55e2006-04-02 18:51:53 +0900814 if (qc->err_mask ||
815 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400816 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
Tejun Heod25614b2006-11-14 22:37:35 +0900817 &sb[1], &sb[2], &sb[3], verbose);
818 sb[1] &= 0x0f;
Jeff Garzikb0955182005-05-12 15:45:22 -0400819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Tejun Heod25614b2006-11-14 22:37:35 +0900821 block = ata_tf_read_block(&qc->result_tf, dev);
Jeff Garzik0274aa22005-06-22 13:50:56 -0400822
Tejun Heod25614b2006-11-14 22:37:35 +0900823 /* information sense data descriptor */
824 sb[7] = 12;
825 desc[0] = 0x00;
826 desc[1] = 10;
Jeff Garzika7dac442005-10-30 04:44:42 -0500827
Tejun Heod25614b2006-11-14 22:37:35 +0900828 desc[2] |= 0x80; /* valid */
829 desc[6] = block >> 40;
830 desc[7] = block >> 32;
831 desc[8] = block >> 24;
832 desc[9] = block >> 16;
833 desc[10] = block >> 8;
834 desc[11] = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835}
836
Brian Kinga6cce2a2006-03-17 17:04:15 -0600837static void ata_scsi_sdev_config(struct scsi_device *sdev)
838{
839 sdev->use_10_for_rw = 1;
840 sdev->use_10_for_ms = 1;
841}
842
843static void ata_scsi_dev_config(struct scsi_device *sdev,
844 struct ata_device *dev)
845{
Tejun Heo914ed352006-11-01 18:39:55 +0900846 /* configure max sectors */
847 blk_queue_max_sectors(sdev->request_queue, dev->max_sectors);
Brian Kinga6cce2a2006-03-17 17:04:15 -0600848
Tejun Heo914ed352006-11-01 18:39:55 +0900849 /* SATA DMA transfers must be multiples of 4 byte, so
Brian Kinga6cce2a2006-03-17 17:04:15 -0600850 * we need to pad ATAPI transfers using an extra sg.
851 * Decrement max hw segments accordingly.
852 */
853 if (dev->class == ATA_DEV_ATAPI) {
854 request_queue_t *q = sdev->request_queue;
855 blk_queue_max_hw_segments(q, q->max_hw_segments - 1);
856 }
Tejun Heoa6e6ce82006-05-15 21:03:48 +0900857
858 if (dev->flags & ATA_DFLAG_NCQ) {
859 int depth;
860
861 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
862 depth = min(ATA_MAX_QUEUE - 1, depth);
863 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
864 }
Brian Kinga6cce2a2006-03-17 17:04:15 -0600865}
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867/**
868 * ata_scsi_slave_config - Set SCSI device attributes
869 * @sdev: SCSI device to examine
870 *
871 * This is called before we actually start reading
872 * and writing to the device, to configure certain
873 * SCSI mid-layer behaviors.
874 *
875 * LOCKING:
876 * Defined by SCSI layer. We don't really care.
877 */
878
879int ata_scsi_slave_config(struct scsi_device *sdev)
880{
Tejun Heo31534362006-05-31 18:27:36 +0900881 struct ata_port *ap = ata_shost_to_port(sdev->host);
882 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
883
Brian Kinga6cce2a2006-03-17 17:04:15 -0600884 ata_scsi_sdev_config(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
887
Tejun Heo31534362006-05-31 18:27:36 +0900888 if (dev)
Brian Kinga6cce2a2006-03-17 17:04:15 -0600889 ata_scsi_dev_config(sdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
891 return 0; /* scsi layer doesn't check return value, sigh */
892}
893
894/**
Tejun Heo83c47bc2006-05-31 18:28:07 +0900895 * ata_scsi_slave_destroy - SCSI device is about to be destroyed
896 * @sdev: SCSI device to be destroyed
897 *
898 * @sdev is about to be destroyed for hot/warm unplugging. If
899 * this unplugging was initiated by libata as indicated by NULL
900 * dev->sdev, this function doesn't have to do anything.
901 * Otherwise, SCSI layer initiated warm-unplug is in progress.
902 * Clear dev->sdev, schedule the device for ATA detach and invoke
903 * EH.
904 *
905 * LOCKING:
906 * Defined by SCSI layer. We don't really care.
907 */
908void ata_scsi_slave_destroy(struct scsi_device *sdev)
909{
910 struct ata_port *ap = ata_shost_to_port(sdev->host);
911 unsigned long flags;
912 struct ata_device *dev;
913
914 if (!ap->ops->error_handler)
915 return;
916
Jeff Garzikba6a1302006-06-22 23:46:10 -0400917 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +0900918 dev = __ata_scsi_find_dev(ap, sdev);
919 if (dev && dev->sdev) {
920 /* SCSI device already in CANCEL state, no need to offline it */
921 dev->sdev = NULL;
922 dev->flags |= ATA_DFLAG_DETACH;
923 ata_port_schedule_eh(ap);
924 }
Jeff Garzikba6a1302006-06-22 23:46:10 -0400925 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +0900926}
927
928/**
Tejun Heoa6e6ce82006-05-15 21:03:48 +0900929 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
930 * @sdev: SCSI device to configure queue depth for
931 * @queue_depth: new queue depth
932 *
933 * This is libata standard hostt->change_queue_depth callback.
934 * SCSI will call into this callback when user tries to set queue
935 * depth via sysfs.
936 *
937 * LOCKING:
938 * SCSI layer (we don't care)
939 *
940 * RETURNS:
941 * Newly configured queue depth.
942 */
943int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
944{
945 struct ata_port *ap = ata_shost_to_port(sdev->host);
946 struct ata_device *dev;
Tejun Heo360f6542006-09-30 19:45:00 +0900947 unsigned long flags;
Tejun Heoa6e6ce82006-05-15 21:03:48 +0900948 int max_depth;
949
950 if (queue_depth < 1)
951 return sdev->queue_depth;
952
953 dev = ata_scsi_find_dev(ap, sdev);
954 if (!dev || !ata_dev_enabled(dev))
955 return sdev->queue_depth;
956
957 max_depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
958 max_depth = min(ATA_MAX_QUEUE - 1, max_depth);
959 if (queue_depth > max_depth)
960 queue_depth = max_depth;
961
962 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, queue_depth);
Tejun Heo360f6542006-09-30 19:45:00 +0900963
964 spin_lock_irqsave(ap->lock, flags);
965 if (queue_depth > 1)
966 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
967 else
968 dev->flags |= ATA_DFLAG_NCQ_OFF;
969 spin_unlock_irqrestore(ap->lock, flags);
970
Tejun Heoa6e6ce82006-05-15 21:03:48 +0900971 return queue_depth;
972}
973
974/**
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400975 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
976 * @qc: Storage for translated ATA taskfile
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400977 *
978 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
979 * (to start). Perhaps these commands should be preceded by
980 * CHECK POWER MODE to see what power mode the device is already in.
981 * [See SAT revision 5 at www.t10.org]
982 *
983 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400984 * spin_lock_irqsave(host lock)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400985 *
986 * RETURNS:
987 * Zero on success, non-zero on error.
988 */
Tejun Heoad706992006-12-17 10:45:57 +0900989static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400990{
Tejun Heo542b1442006-12-17 10:45:08 +0900991 struct scsi_cmnd *scmd = qc->scsicmd;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400992 struct ata_taskfile *tf = &qc->tf;
Tejun Heoad706992006-12-17 10:45:57 +0900993 const u8 *cdb = scmd->cmnd;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400994
Tejun Heo2e5704f2006-12-17 10:46:33 +0900995 if (scmd->cmd_len < 5)
996 goto invalid_fld;
997
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400998 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
999 tf->protocol = ATA_PROT_NODATA;
Tejun Heo542b1442006-12-17 10:45:08 +09001000 if (cdb[1] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001001 ; /* ignore IMMED bit, violates sat-r05 */
1002 }
Tejun Heo542b1442006-12-17 10:45:08 +09001003 if (cdb[4] & 0x2)
Douglas Gilbertae006512005-10-09 09:09:35 -04001004 goto invalid_fld; /* LOEJ bit set not supported */
Tejun Heo542b1442006-12-17 10:45:08 +09001005 if (((cdb[4] >> 4) & 0xf) != 0)
Douglas Gilbertae006512005-10-09 09:09:35 -04001006 goto invalid_fld; /* power conditions not supported */
Tejun Heo542b1442006-12-17 10:45:08 +09001007 if (cdb[4] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001008 tf->nsect = 1; /* 1 sector, lba=0 */
Albert Lee9d5b1302005-10-04 08:48:17 -04001009
1010 if (qc->dev->flags & ATA_DFLAG_LBA) {
Tejun Heoc44078c2006-05-15 20:57:21 +09001011 tf->flags |= ATA_TFLAG_LBA;
Albert Lee9d5b1302005-10-04 08:48:17 -04001012
1013 tf->lbah = 0x0;
1014 tf->lbam = 0x0;
1015 tf->lbal = 0x0;
1016 tf->device |= ATA_LBA;
1017 } else {
1018 /* CHS */
1019 tf->lbal = 0x1; /* sect */
1020 tf->lbam = 0x0; /* cyl low */
1021 tf->lbah = 0x0; /* cyl high */
1022 }
1023
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001024 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
1025 } else {
1026 tf->nsect = 0; /* time period value (0 implies now) */
1027 tf->command = ATA_CMD_STANDBY;
1028 /* Consider: ATA STANDBY IMMEDIATE command */
1029 }
1030 /*
1031 * Standby and Idle condition timers could be implemented but that
1032 * would require libata to implement the Power condition mode page
1033 * and allow the user to change it. Changing mode pages requires
1034 * MODE SELECT to be implemented.
1035 */
1036
1037 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001038
1039invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +09001040 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001041 /* "Invalid field in cbd" */
1042 return 1;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001043}
1044
1045
1046/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
1048 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 *
1050 * Sets up an ATA taskfile to issue FLUSH CACHE or
1051 * FLUSH CACHE EXT.
1052 *
1053 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001054 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 *
1056 * RETURNS:
1057 * Zero on success, non-zero on error.
1058 */
Tejun Heoad706992006-12-17 10:45:57 +09001059static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060{
1061 struct ata_taskfile *tf = &qc->tf;
1062
1063 tf->flags |= ATA_TFLAG_DEVICE;
1064 tf->protocol = ATA_PROT_NODATA;
1065
Tejun Heo6fc49ad2006-11-11 20:10:45 +09001066 if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 tf->command = ATA_CMD_FLUSH_EXT;
1068 else
1069 tf->command = ATA_CMD_FLUSH;
1070
1071 return 0;
1072}
1073
1074/**
Albert Lee3aef5232005-10-04 08:47:43 -04001075 * scsi_6_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001076 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001077 *
1078 * Calculate LBA and transfer length for 6-byte commands.
1079 *
1080 * RETURNS:
1081 * @plba: the LBA
1082 * @plen: the transfer length
1083 */
Tejun Heo542b1442006-12-17 10:45:08 +09001084static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001085{
1086 u64 lba = 0;
1087 u32 len = 0;
1088
1089 VPRINTK("six-byte command\n");
1090
Tejun Heo542b1442006-12-17 10:45:08 +09001091 lba |= ((u64)cdb[2]) << 8;
1092 lba |= ((u64)cdb[3]);
Albert Lee3aef5232005-10-04 08:47:43 -04001093
Tejun Heo542b1442006-12-17 10:45:08 +09001094 len |= ((u32)cdb[4]);
Albert Lee3aef5232005-10-04 08:47:43 -04001095
1096 *plba = lba;
1097 *plen = len;
1098}
1099
1100/**
1101 * scsi_10_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001102 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001103 *
1104 * Calculate LBA and transfer length for 10-byte commands.
1105 *
1106 * RETURNS:
1107 * @plba: the LBA
1108 * @plen: the transfer length
1109 */
Tejun Heo542b1442006-12-17 10:45:08 +09001110static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001111{
1112 u64 lba = 0;
1113 u32 len = 0;
1114
1115 VPRINTK("ten-byte command\n");
1116
Tejun Heo542b1442006-12-17 10:45:08 +09001117 lba |= ((u64)cdb[2]) << 24;
1118 lba |= ((u64)cdb[3]) << 16;
1119 lba |= ((u64)cdb[4]) << 8;
1120 lba |= ((u64)cdb[5]);
Albert Lee3aef5232005-10-04 08:47:43 -04001121
Tejun Heo542b1442006-12-17 10:45:08 +09001122 len |= ((u32)cdb[7]) << 8;
1123 len |= ((u32)cdb[8]);
Albert Lee3aef5232005-10-04 08:47:43 -04001124
1125 *plba = lba;
1126 *plen = len;
1127}
1128
1129/**
1130 * scsi_16_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001131 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001132 *
1133 * Calculate LBA and transfer length for 16-byte commands.
1134 *
1135 * RETURNS:
1136 * @plba: the LBA
1137 * @plen: the transfer length
1138 */
Tejun Heo542b1442006-12-17 10:45:08 +09001139static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001140{
1141 u64 lba = 0;
1142 u32 len = 0;
1143
1144 VPRINTK("sixteen-byte command\n");
1145
Tejun Heo542b1442006-12-17 10:45:08 +09001146 lba |= ((u64)cdb[2]) << 56;
1147 lba |= ((u64)cdb[3]) << 48;
1148 lba |= ((u64)cdb[4]) << 40;
1149 lba |= ((u64)cdb[5]) << 32;
1150 lba |= ((u64)cdb[6]) << 24;
1151 lba |= ((u64)cdb[7]) << 16;
1152 lba |= ((u64)cdb[8]) << 8;
1153 lba |= ((u64)cdb[9]);
Albert Lee3aef5232005-10-04 08:47:43 -04001154
Tejun Heo542b1442006-12-17 10:45:08 +09001155 len |= ((u32)cdb[10]) << 24;
1156 len |= ((u32)cdb[11]) << 16;
1157 len |= ((u32)cdb[12]) << 8;
1158 len |= ((u32)cdb[13]);
Albert Lee3aef5232005-10-04 08:47:43 -04001159
1160 *plba = lba;
1161 *plen = len;
1162}
1163
1164/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1166 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 *
1168 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1169 *
1170 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001171 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 *
1173 * RETURNS:
1174 * Zero on success, non-zero on error.
1175 */
Tejun Heoad706992006-12-17 10:45:57 +09001176static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177{
Tejun Heo542b1442006-12-17 10:45:08 +09001178 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 struct ata_taskfile *tf = &qc->tf;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001180 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 u64 dev_sectors = qc->dev->n_sectors;
Tejun Heoad706992006-12-17 10:45:57 +09001182 const u8 *cdb = scmd->cmnd;
Albert Lee3aef5232005-10-04 08:47:43 -04001183 u64 block;
1184 u32 n_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
1186 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1187 tf->protocol = ATA_PROT_NODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Tejun Heo2e5704f2006-12-17 10:46:33 +09001189 if (cdb[0] == VERIFY) {
1190 if (scmd->cmd_len < 10)
1191 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001192 scsi_10_lba_len(cdb, &block, &n_block);
Tejun Heo2e5704f2006-12-17 10:46:33 +09001193 } else if (cdb[0] == VERIFY_16) {
1194 if (scmd->cmd_len < 16)
1195 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001196 scsi_16_lba_len(cdb, &block, &n_block);
Tejun Heo2e5704f2006-12-17 10:46:33 +09001197 } else
Douglas Gilbertae006512005-10-09 09:09:35 -04001198 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Albert Lee8bf62ec2005-05-12 15:29:42 -04001200 if (!n_block)
Douglas Gilbertae006512005-10-09 09:09:35 -04001201 goto nothing_to_do;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001202 if (block >= dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001203 goto out_of_range;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001204 if ((block + n_block) > dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001205 goto out_of_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Albert Lee07506692005-10-12 15:04:18 +08001207 if (dev->flags & ATA_DFLAG_LBA) {
1208 tf->flags |= ATA_TFLAG_LBA;
1209
Albert Leec6a33e22005-10-12 15:12:26 +08001210 if (lba_28_ok(block, n_block)) {
1211 /* use LBA28 */
1212 tf->command = ATA_CMD_VERIFY;
1213 tf->device |= (block >> 24) & 0xf;
1214 } else if (lba_48_ok(block, n_block)) {
1215 if (!(dev->flags & ATA_DFLAG_LBA48))
1216 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001217
1218 /* use LBA48 */
1219 tf->flags |= ATA_TFLAG_LBA48;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001220 tf->command = ATA_CMD_VERIFY_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
Albert Lee8bf62ec2005-05-12 15:29:42 -04001222 tf->hob_nsect = (n_block >> 8) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223
Albert Lee8bf62ec2005-05-12 15:29:42 -04001224 tf->hob_lbah = (block >> 40) & 0xff;
1225 tf->hob_lbam = (block >> 32) & 0xff;
1226 tf->hob_lbal = (block >> 24) & 0xff;
Albert Leec6a33e22005-10-12 15:12:26 +08001227 } else
1228 /* request too large even for LBA48 */
1229 goto out_of_range;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001230
1231 tf->nsect = n_block & 0xff;
1232
1233 tf->lbah = (block >> 16) & 0xff;
1234 tf->lbam = (block >> 8) & 0xff;
1235 tf->lbal = block & 0xff;
1236
1237 tf->device |= ATA_LBA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 } else {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001239 /* CHS */
1240 u32 sect, head, cyl, track;
1241
Albert Leec6a33e22005-10-12 15:12:26 +08001242 if (!lba_28_ok(block, n_block))
1243 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001244
Albert Lee8bf62ec2005-05-12 15:29:42 -04001245 /* Convert LBA to CHS */
1246 track = (u32)block / dev->sectors;
1247 cyl = track / dev->heads;
1248 head = track % dev->heads;
1249 sect = (u32)block % dev->sectors + 1;
1250
Albert Leec187c4b2005-10-04 08:46:51 -04001251 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1252 (u32)block, track, cyl, head, sect);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001253
1254 /* Check whether the converted CHS can fit.
1255 Cylinder: 0-65535
Albert Lee8bf62ec2005-05-12 15:29:42 -04001256 Head: 0-15
1257 Sector: 1-255*/
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001258 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
Douglas Gilbertae006512005-10-09 09:09:35 -04001259 goto out_of_range;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001260
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261 tf->command = ATA_CMD_VERIFY;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001262 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1263 tf->lbal = sect;
1264 tf->lbam = cyl;
1265 tf->lbah = cyl >> 8;
1266 tf->device |= head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 }
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001270
1271invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +09001272 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001273 /* "Invalid field in cbd" */
1274 return 1;
1275
1276out_of_range:
Tejun Heo542b1442006-12-17 10:45:08 +09001277 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001278 /* "Logical Block Address out of range" */
1279 return 1;
1280
1281nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001282 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001283 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284}
1285
1286/**
1287 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1288 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 *
1290 * Converts any of six SCSI read/write commands into the
1291 * ATA counterpart, including starting sector (LBA),
1292 * sector count, and taking into account the device's LBA48
1293 * support.
1294 *
1295 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1296 * %WRITE_16 are currently supported.
1297 *
1298 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001299 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300 *
1301 * RETURNS:
1302 * Zero on success, non-zero on error.
1303 */
Tejun Heoad706992006-12-17 10:45:57 +09001304static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305{
Tejun Heo542b1442006-12-17 10:45:08 +09001306 struct scsi_cmnd *scmd = qc->scsicmd;
Tejun Heoad706992006-12-17 10:45:57 +09001307 const u8 *cdb = scmd->cmnd;
Tejun Heobd056d72006-11-14 22:47:10 +09001308 unsigned int tf_flags = 0;
Albert Lee3aef5232005-10-04 08:47:43 -04001309 u64 block;
1310 u32 n_block;
Tejun Heobd056d72006-11-14 22:47:10 +09001311 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
Tejun Heo542b1442006-12-17 10:45:08 +09001313 if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
Tejun Heobd056d72006-11-14 22:47:10 +09001314 tf_flags |= ATA_TFLAG_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001316 /* Calculate the SCSI LBA, transfer length and FUA. */
Tejun Heo542b1442006-12-17 10:45:08 +09001317 switch (cdb[0]) {
Albert Lee3aef5232005-10-04 08:47:43 -04001318 case READ_10:
1319 case WRITE_10:
Tejun Heo2e5704f2006-12-17 10:46:33 +09001320 if (unlikely(scmd->cmd_len < 10))
1321 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001322 scsi_10_lba_len(cdb, &block, &n_block);
1323 if (unlikely(cdb[1] & (1 << 3)))
Tejun Heobd056d72006-11-14 22:47:10 +09001324 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001325 break;
1326 case READ_6:
1327 case WRITE_6:
Tejun Heo2e5704f2006-12-17 10:46:33 +09001328 if (unlikely(scmd->cmd_len < 6))
1329 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001330 scsi_6_lba_len(cdb, &block, &n_block);
Albert Leec187c4b2005-10-04 08:46:51 -04001331
1332 /* for 6-byte r/w commands, transfer length 0
1333 * means 256 blocks of data, not 0 block.
1334 */
Jeff Garzik76b2bf92005-08-29 19:24:43 -04001335 if (!n_block)
1336 n_block = 256;
Albert Lee3aef5232005-10-04 08:47:43 -04001337 break;
1338 case READ_16:
1339 case WRITE_16:
Tejun Heo2e5704f2006-12-17 10:46:33 +09001340 if (unlikely(scmd->cmd_len < 16))
1341 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001342 scsi_16_lba_len(cdb, &block, &n_block);
1343 if (unlikely(cdb[1] & (1 << 3)))
Tejun Heobd056d72006-11-14 22:47:10 +09001344 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001345 break;
1346 default:
Albert Lee8bf62ec2005-05-12 15:29:42 -04001347 DPRINTK("no-byte command\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04001348 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350
Albert Lee8bf62ec2005-05-12 15:29:42 -04001351 /* Check and compose ATA command */
1352 if (!n_block)
Albert Leec187c4b2005-10-04 08:46:51 -04001353 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1354 * length 0 means transfer 0 block of data.
1355 * However, for ATA R/W commands, sector count 0 means
1356 * 256 or 65536 sectors, not 0 sectors as in SCSI.
Alan Coxf51750d2005-11-07 17:06:33 +00001357 *
1358 * WARNING: one or two older ATA drives treat 0 as 0...
Albert Leec187c4b2005-10-04 08:46:51 -04001359 */
Douglas Gilbertae006512005-10-09 09:09:35 -04001360 goto nothing_to_do;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001361
Tejun Heobd056d72006-11-14 22:47:10 +09001362 qc->flags |= ATA_QCFLAG_IO;
1363 qc->nsect = n_block;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001364
Tejun Heobd056d72006-11-14 22:47:10 +09001365 rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
1366 qc->tag);
1367 if (likely(rc == 0))
1368 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001369
Tejun Heobd056d72006-11-14 22:47:10 +09001370 if (rc == -ERANGE)
1371 goto out_of_range;
1372 /* treat all other errors as -EINVAL, fall through */
Douglas Gilbertae006512005-10-09 09:09:35 -04001373invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +09001374 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001375 /* "Invalid field in cbd" */
1376 return 1;
1377
1378out_of_range:
Tejun Heo542b1442006-12-17 10:45:08 +09001379 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001380 /* "Logical Block Address out of range" */
1381 return 1;
1382
1383nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001384 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001385 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001386}
1387
Tejun Heo77853bf2006-01-23 13:09:36 +09001388static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389{
Tejun Heoc31f5712006-11-22 12:39:43 +09001390 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika7dac442005-10-30 04:44:42 -05001392 u8 *cdb = cmd->cmnd;
Albert Leea22e2eb2005-12-05 15:38:02 +08001393 int need_sense = (qc->err_mask != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001394
zhao, forrest3057ac32006-06-12 12:01:34 +08001395 /* We snoop the SET_FEATURES - Write Cache ON/OFF command, and
1396 * schedule EH_REVALIDATE operation to update the IDENTIFY DEVICE
1397 * cache
1398 */
Tejun Heoc31f5712006-11-22 12:39:43 +09001399 if (ap->ops->error_handler &&
1400 !need_sense && (qc->tf.command == ATA_CMD_SET_FEATURES) &&
zhao, forrest3057ac32006-06-12 12:01:34 +08001401 ((qc->tf.feature == SETFEATURES_WC_ON) ||
1402 (qc->tf.feature == SETFEATURES_WC_OFF))) {
Tejun Heoc31f5712006-11-22 12:39:43 +09001403 ap->eh_info.action |= ATA_EH_REVALIDATE;
1404 ata_port_schedule_eh(ap);
zhao, forrest3057ac32006-06-12 12:01:34 +08001405 }
1406
Jeff Garzikb0955182005-05-12 15:45:22 -04001407 /* For ATA pass thru (SAT) commands, generate a sense block if
1408 * user mandated it or if there's an error. Note that if we
1409 * generate because the user forced us to, a check condition
1410 * is generated and the ATA register values are returned
1411 * whether the command completed successfully or not. If there
1412 * was no error, SK, ASC and ASCQ will all be zero.
1413 */
Jeff Garzika7dac442005-10-30 04:44:42 -05001414 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
1415 ((cdb[2] & 0x20) || need_sense)) {
Tejun Heo750426a2006-11-14 22:37:35 +09001416 ata_gen_passthru_sense(qc);
Jeff Garzikb0955182005-05-12 15:45:22 -04001417 } else {
1418 if (!need_sense) {
1419 cmd->result = SAM_STAT_GOOD;
1420 } else {
1421 /* TODO: decide which descriptor format to use
1422 * for 48b LBA devices and call that here
1423 * instead of the fixed desc, which is only
1424 * good for smaller LBA (and maybe CHS?)
1425 * devices.
1426 */
Tejun Heo750426a2006-11-14 22:37:35 +09001427 ata_gen_ata_sense(qc);
Jeff Garzikb0955182005-05-12 15:45:22 -04001428 }
1429 }
1430
Tejun Heoc31f5712006-11-22 12:39:43 +09001431 if (need_sense && !ap->ops->error_handler)
1432 ata_dump_status(ap->id, &qc->result_tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001433
1434 qc->scsidone(cmd);
1435
Tejun Heo77853bf2006-01-23 13:09:36 +09001436 ata_qc_free(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437}
1438
1439/**
Tejun Heo3dc1d882006-05-15 21:03:45 +09001440 * ata_scmd_need_defer - Check whether we need to defer scmd
1441 * @dev: ATA device to which the command is addressed
1442 * @is_io: Is the command IO (and thus possibly NCQ)?
1443 *
1444 * NCQ and non-NCQ commands cannot run together. As upper layer
1445 * only knows the queue depth, we are responsible for maintaining
1446 * exclusion. This function checks whether a new command can be
1447 * issued to @dev.
1448 *
1449 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001450 * spin_lock_irqsave(host lock)
Tejun Heo3dc1d882006-05-15 21:03:45 +09001451 *
1452 * RETURNS:
1453 * 1 if deferring is needed, 0 otherwise.
1454 */
1455static int ata_scmd_need_defer(struct ata_device *dev, int is_io)
1456{
1457 struct ata_port *ap = dev->ap;
1458
1459 if (!(dev->flags & ATA_DFLAG_NCQ))
1460 return 0;
1461
1462 if (is_io) {
1463 if (!ata_tag_valid(ap->active_tag))
1464 return 0;
1465 } else {
1466 if (!ata_tag_valid(ap->active_tag) && !ap->sactive)
1467 return 0;
1468 }
1469 return 1;
1470}
1471
1472/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473 * ata_scsi_translate - Translate then issue SCSI command to ATA device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 * @dev: ATA device to which the command is addressed
1475 * @cmd: SCSI command to execute
1476 * @done: SCSI command completion function
1477 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1478 *
1479 * Our ->queuecommand() function has decided that the SCSI
1480 * command issued can be directly translated into an ATA
1481 * command, rather than handled internally.
1482 *
1483 * This function sets up an ata_queued_cmd structure for the
1484 * SCSI command, and sends that ata_queued_cmd to the hardware.
1485 *
Douglas Gilbertae006512005-10-09 09:09:35 -04001486 * The xlat_func argument (actor) returns 0 if ready to execute
1487 * ATA command, else 1 to finish translation. If 1 is returned
1488 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1489 * to be set reflecting an error condition or clean (early)
1490 * termination.
1491 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001493 * spin_lock_irqsave(host lock)
Tejun Heo2115ea92006-05-15 21:03:39 +09001494 *
1495 * RETURNS:
1496 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
1497 * needs to be deferred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001498 */
Tejun Heo2115ea92006-05-15 21:03:39 +09001499static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
1500 void (*done)(struct scsi_cmnd *),
1501 ata_xlat_func_t xlat_func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502{
1503 struct ata_queued_cmd *qc;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001504 int is_io = xlat_func == ata_scsi_rw_xlat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 VPRINTK("ENTER\n");
1507
Tejun Heo3dc1d882006-05-15 21:03:45 +09001508 if (unlikely(ata_scmd_need_defer(dev, is_io)))
1509 goto defer;
1510
Tejun Heo3373efd2006-05-15 20:57:53 +09001511 qc = ata_scsi_qc_new(dev, cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001512 if (!qc)
Douglas Gilbertae006512005-10-09 09:09:35 -04001513 goto err_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 /* data is present; dma-map it */
be7db052005-04-17 15:26:13 -05001516 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1517 cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 if (unlikely(cmd->request_bufflen < 1)) {
Tejun Heof15a1da2006-05-15 20:57:56 +09001519 ata_dev_printk(dev, KERN_WARNING,
1520 "WARNING: zero len r/w req\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04001521 goto err_did;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522 }
1523
1524 if (cmd->use_sg)
1525 ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
1526 else
1527 ata_sg_init_one(qc, cmd->request_buffer,
1528 cmd->request_bufflen);
1529
1530 qc->dma_dir = cmd->sc_data_direction;
1531 }
1532
1533 qc->complete_fn = ata_scsi_qc_complete;
1534
Tejun Heoad706992006-12-17 10:45:57 +09001535 if (xlat_func(qc))
Douglas Gilbertae006512005-10-09 09:09:35 -04001536 goto early_finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537
1538 /* select device, send command to hardware */
Tejun Heo8e0e6942006-03-31 20:41:11 +09001539 ata_qc_issue(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540
1541 VPRINTK("EXIT\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001542 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Douglas Gilbertae006512005-10-09 09:09:35 -04001544early_finish:
1545 ata_qc_free(qc);
1546 done(cmd);
1547 DPRINTK("EXIT - early finish (good or error)\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001548 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001549
1550err_did:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001551 ata_qc_free(qc);
Douglas Gilbertae006512005-10-09 09:09:35 -04001552 cmd->result = (DID_ERROR << 16);
1553 done(cmd);
Darrick J. Wong253b92e2006-11-14 09:55:41 -05001554err_mem:
Douglas Gilbertae006512005-10-09 09:09:35 -04001555 DPRINTK("EXIT - internal\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001556 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001557
1558defer:
1559 DPRINTK("EXIT - defer\n");
1560 return SCSI_MLQUEUE_DEVICE_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561}
1562
1563/**
1564 * ata_scsi_rbuf_get - Map response buffer.
1565 * @cmd: SCSI command containing buffer to be mapped.
1566 * @buf_out: Pointer to mapped area.
1567 *
1568 * Maps buffer contained within SCSI command @cmd.
1569 *
1570 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001571 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 *
1573 * RETURNS:
1574 * Length of response buffer.
1575 */
1576
1577static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
1578{
1579 u8 *buf;
1580 unsigned int buflen;
1581
1582 if (cmd->use_sg) {
1583 struct scatterlist *sg;
1584
1585 sg = (struct scatterlist *) cmd->request_buffer;
Jeff Garzikda02d2a2006-12-11 11:05:53 -05001586 buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 buflen = sg->length;
1588 } else {
1589 buf = cmd->request_buffer;
1590 buflen = cmd->request_bufflen;
1591 }
1592
1593 *buf_out = buf;
1594 return buflen;
1595}
1596
1597/**
1598 * ata_scsi_rbuf_put - Unmap response buffer.
1599 * @cmd: SCSI command containing buffer to be unmapped.
1600 * @buf: buffer to unmap
1601 *
1602 * Unmaps response buffer contained within @cmd.
1603 *
1604 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001605 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 */
1607
1608static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
1609{
1610 if (cmd->use_sg) {
1611 struct scatterlist *sg;
1612
1613 sg = (struct scatterlist *) cmd->request_buffer;
Jeff Garzikda02d2a2006-12-11 11:05:53 -05001614 kunmap_atomic(buf - sg->offset, KM_IRQ0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615 }
1616}
1617
1618/**
1619 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1620 * @args: device IDENTIFY data / SCSI command of interest.
1621 * @actor: Callback hook for desired SCSI command simulator
1622 *
1623 * Takes care of the hard work of simulating a SCSI command...
1624 * Mapping the response buffer, calling the command's handler,
1625 * and handling the handler's return value. This return value
1626 * indicates whether the handler wishes the SCSI command to be
Douglas Gilbertae006512005-10-09 09:09:35 -04001627 * completed successfully (0), or not (in which case cmd->result
1628 * and sense buffer are assumed to be set).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 *
1630 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001631 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632 */
1633
1634void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
1635 unsigned int (*actor) (struct ata_scsi_args *args,
1636 u8 *rbuf, unsigned int buflen))
1637{
1638 u8 *rbuf;
1639 unsigned int buflen, rc;
1640 struct scsi_cmnd *cmd = args->cmd;
1641
1642 buflen = ata_scsi_rbuf_get(cmd, &rbuf);
1643 memset(rbuf, 0, buflen);
1644 rc = actor(args, rbuf, buflen);
1645 ata_scsi_rbuf_put(cmd, rbuf);
1646
Douglas Gilbertae006512005-10-09 09:09:35 -04001647 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 cmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001649 args->done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650}
1651
1652/**
Tejun Heo6a362612006-11-20 11:15:47 +09001653 * ATA_SCSI_RBUF_SET - helper to set values in SCSI response buffer
1654 * @idx: byte index into SCSI response buffer
1655 * @val: value to set
1656 *
1657 * To be used by SCSI command simulator functions. This macros
1658 * expects two local variables, u8 *rbuf and unsigned int buflen,
1659 * are in scope.
1660 *
1661 * LOCKING:
1662 * None.
1663 */
1664#define ATA_SCSI_RBUF_SET(idx, val) do { \
1665 if ((idx) < buflen) rbuf[(idx)] = (u8)(val); \
1666 } while (0)
1667
1668/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 * ata_scsiop_inq_std - Simulate INQUIRY command
1670 * @args: device IDENTIFY data / SCSI command of interest.
1671 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1672 * @buflen: Response buffer length.
1673 *
1674 * Returns standard device identification data associated
Jeff Garzikb142eb62006-03-21 20:37:47 -05001675 * with non-VPD INQUIRY command output.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676 *
1677 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001678 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 */
1680
1681unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
1682 unsigned int buflen)
1683{
1684 u8 hdr[] = {
1685 TYPE_DISK,
1686 0,
1687 0x5, /* claim SPC-3 version compatibility */
1688 2,
1689 95 - 4
1690 };
1691
1692 /* set scsi removeable (RMB) bit per ata bit */
1693 if (ata_id_removeable(args->id))
1694 hdr[1] |= (1 << 7);
1695
1696 VPRINTK("ENTER\n");
1697
1698 memcpy(rbuf, hdr, sizeof(hdr));
1699
1700 if (buflen > 35) {
1701 memcpy(&rbuf[8], "ATA ", 8);
Tejun Heo6a62a042006-02-13 10:02:46 +09001702 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
1703 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 if (rbuf[32] == 0 || rbuf[32] == ' ')
1705 memcpy(&rbuf[32], "n/a ", 4);
1706 }
1707
1708 if (buflen > 63) {
1709 const u8 versions[] = {
1710 0x60, /* SAM-3 (no version claimed) */
1711
1712 0x03,
1713 0x20, /* SBC-2 (no version claimed) */
1714
1715 0x02,
1716 0x60 /* SPC-3 (no version claimed) */
1717 };
1718
1719 memcpy(rbuf + 59, versions, sizeof(versions));
1720 }
1721
1722 return 0;
1723}
1724
1725/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05001726 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 * @args: device IDENTIFY data / SCSI command of interest.
1728 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1729 * @buflen: Response buffer length.
1730 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05001731 * Returns list of inquiry VPD pages available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 *
1733 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001734 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 */
1736
1737unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
1738 unsigned int buflen)
1739{
1740 const u8 pages[] = {
1741 0x00, /* page 0x00, this page */
1742 0x80, /* page 0x80, unit serial no page */
1743 0x83 /* page 0x83, device ident page */
1744 };
Jeff Garzikb142eb62006-03-21 20:37:47 -05001745 rbuf[3] = sizeof(pages); /* number of supported VPD pages */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 if (buflen > 6)
1748 memcpy(rbuf + 4, pages, sizeof(pages));
1749
1750 return 0;
1751}
1752
1753/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05001754 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 * @args: device IDENTIFY data / SCSI command of interest.
1756 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1757 * @buflen: Response buffer length.
1758 *
1759 * Returns ATA device serial number.
1760 *
1761 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001762 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763 */
1764
1765unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
1766 unsigned int buflen)
1767{
1768 const u8 hdr[] = {
1769 0,
1770 0x80, /* this page code */
1771 0,
1772 ATA_SERNO_LEN, /* page len */
1773 };
1774 memcpy(rbuf, hdr, sizeof(hdr));
1775
1776 if (buflen > (ATA_SERNO_LEN + 4 - 1))
Tejun Heo6a62a042006-02-13 10:02:46 +09001777 ata_id_string(args->id, (unsigned char *) &rbuf[4],
1778 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779
1780 return 0;
1781}
1782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05001784 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 * @args: device IDENTIFY data / SCSI command of interest.
1786 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1787 * @buflen: Response buffer length.
1788 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05001789 * Yields two logical unit device identification designators:
1790 * - vendor specific ASCII containing the ATA serial number
1791 * - SAT defined "t10 vendor id based" containing ASCII vendor
1792 * name ("ATA "), model and serial numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 *
1794 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001795 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 */
1797
1798unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
1799 unsigned int buflen)
1800{
Jeff Garzikb142eb62006-03-21 20:37:47 -05001801 int num;
1802 const int sat_model_serial_desc_len = 68;
1803 const int ata_model_byte_len = 40;
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 rbuf[1] = 0x83; /* this page code */
Jeff Garzikb142eb62006-03-21 20:37:47 -05001806 num = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Jeff Garzikb142eb62006-03-21 20:37:47 -05001808 if (buflen > (ATA_SERNO_LEN + num + 3)) {
1809 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001810 rbuf[num + 0] = 2;
Jeff Garzikb142eb62006-03-21 20:37:47 -05001811 rbuf[num + 3] = ATA_SERNO_LEN;
1812 num += 4;
1813 ata_id_string(args->id, (unsigned char *) rbuf + num,
1814 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1815 num += ATA_SERNO_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
Jeff Garzikb142eb62006-03-21 20:37:47 -05001817 if (buflen > (sat_model_serial_desc_len + num + 3)) {
1818 /* SAT defined lu model and serial numbers descriptor */
1819 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001820 rbuf[num + 0] = 2;
1821 rbuf[num + 1] = 1;
Jeff Garzikb142eb62006-03-21 20:37:47 -05001822 rbuf[num + 3] = sat_model_serial_desc_len;
1823 num += 4;
1824 memcpy(rbuf + num, "ATA ", 8);
1825 num += 8;
1826 ata_id_string(args->id, (unsigned char *) rbuf + num,
1827 ATA_ID_PROD_OFS, ata_model_byte_len);
1828 num += ata_model_byte_len;
1829 ata_id_string(args->id, (unsigned char *) rbuf + num,
1830 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1831 num += ATA_SERNO_LEN;
1832 }
1833 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 return 0;
1835}
1836
1837/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001838 * ata_scsiop_noop - Command handler that simply returns success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 * @args: device IDENTIFY data / SCSI command of interest.
1840 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1841 * @buflen: Response buffer length.
1842 *
1843 * No operation. Simply returns success to caller, to indicate
1844 * that the caller should successfully complete this SCSI command.
1845 *
1846 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001847 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 */
1849
1850unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
1851 unsigned int buflen)
1852{
1853 VPRINTK("ENTER\n");
1854 return 0;
1855}
1856
1857/**
1858 * ata_msense_push - Push data onto MODE SENSE data output buffer
1859 * @ptr_io: (input/output) Location to store more output data
1860 * @last: End of output data buffer
1861 * @buf: Pointer to BLOB being added to output buffer
1862 * @buflen: Length of BLOB
1863 *
1864 * Store MODE SENSE data on an output buffer.
1865 *
1866 * LOCKING:
1867 * None.
1868 */
1869
1870static void ata_msense_push(u8 **ptr_io, const u8 *last,
1871 const u8 *buf, unsigned int buflen)
1872{
1873 u8 *ptr = *ptr_io;
1874
1875 if ((ptr + buflen - 1) > last)
1876 return;
1877
1878 memcpy(ptr, buf, buflen);
1879
1880 ptr += buflen;
1881
1882 *ptr_io = ptr;
1883}
1884
1885/**
1886 * ata_msense_caching - Simulate MODE SENSE caching info page
1887 * @id: device IDENTIFY data
1888 * @ptr_io: (input/output) Location to store more output data
1889 * @last: End of output data buffer
1890 *
1891 * Generate a caching info page, which conditionally indicates
1892 * write caching to the SCSI layer, depending on device
1893 * capabilities.
1894 *
1895 * LOCKING:
1896 * None.
1897 */
1898
1899static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1900 const u8 *last)
1901{
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001902 u8 page[CACHE_MPAGE_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001904 memcpy(page, def_cache_mpage, sizeof(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905 if (ata_id_wcache_enabled(id))
1906 page[2] |= (1 << 2); /* write cache enable */
1907 if (!ata_id_rahead_enabled(id))
1908 page[12] |= (1 << 5); /* disable read ahead */
1909
1910 ata_msense_push(ptr_io, last, page, sizeof(page));
1911 return sizeof(page);
1912}
1913
1914/**
1915 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1916 * @dev: Device associated with this MODE SENSE command
1917 * @ptr_io: (input/output) Location to store more output data
1918 * @last: End of output data buffer
1919 *
1920 * Generate a generic MODE SENSE control mode page.
1921 *
1922 * LOCKING:
1923 * None.
1924 */
1925
1926static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1927{
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001928 ata_msense_push(ptr_io, last, def_control_mpage,
1929 sizeof(def_control_mpage));
1930 return sizeof(def_control_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
1933/**
1934 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1935 * @dev: Device associated with this MODE SENSE command
1936 * @ptr_io: (input/output) Location to store more output data
1937 * @last: End of output data buffer
1938 *
1939 * Generate a generic MODE SENSE r/w error recovery page.
1940 *
1941 * LOCKING:
1942 * None.
1943 */
1944
1945static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1946{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001948 ata_msense_push(ptr_io, last, def_rw_recovery_mpage,
1949 sizeof(def_rw_recovery_mpage));
1950 return sizeof(def_rw_recovery_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951}
1952
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001953/*
1954 * We can turn this into a real blacklist if it's needed, for now just
1955 * blacklist any Maxtor BANC1G10 revision firmware
1956 */
1957static int ata_dev_supports_fua(u16 *id)
1958{
1959 unsigned char model[41], fw[9];
1960
Jeff Garzikc3c013a2006-02-27 22:31:19 -05001961 if (!libata_fua)
1962 return 0;
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001963 if (!ata_id_has_fua(id))
1964 return 0;
1965
Tejun Heo6a62a042006-02-13 10:02:46 +09001966 ata_id_c_string(id, model, ATA_ID_PROD_OFS, sizeof(model));
1967 ata_id_c_string(id, fw, ATA_ID_FW_REV_OFS, sizeof(fw));
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001968
Tejun Heo2e026712006-02-12 22:47:04 +09001969 if (strcmp(model, "Maxtor"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001970 return 1;
Tejun Heo2e026712006-02-12 22:47:04 +09001971 if (strcmp(fw, "BANC1G10"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001972 return 1;
1973
1974 return 0; /* blacklisted */
1975}
1976
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977/**
1978 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1979 * @args: device IDENTIFY data / SCSI command of interest.
1980 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1981 * @buflen: Response buffer length.
1982 *
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001983 * Simulate MODE SENSE commands. Assume this is invoked for direct
1984 * access devices (e.g. disks) only. There should be no block
1985 * descriptor for other device types.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 *
1987 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001988 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 */
1990
1991unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1992 unsigned int buflen)
1993{
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001994 struct ata_device *dev = args->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 u8 *scsicmd = args->cmd->cmnd, *p, *last;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001996 const u8 sat_blk_desc[] = {
1997 0, 0, 0, 0, /* number of blocks: sat unspecified */
1998 0,
1999 0, 0x2, 0x0 /* block length: 512 bytes */
2000 };
2001 u8 pg, spg;
2002 unsigned int ebd, page_control, six_byte, output_len, alloc_len, minlen;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002003 u8 dpofua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004
2005 VPRINTK("ENTER\n");
2006
2007 six_byte = (scsicmd[0] == MODE_SENSE);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002008 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
2009 /*
2010 * LLBA bit in msense(10) ignored (compliant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002012
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 page_control = scsicmd[2] >> 6;
Douglas Gilbertae006512005-10-09 09:09:35 -04002014 switch (page_control) {
2015 case 0: /* current */
2016 break; /* supported */
2017 case 3: /* saved */
2018 goto saving_not_supp;
2019 case 1: /* changeable */
2020 case 2: /* defaults */
2021 default:
2022 goto invalid_fld;
2023 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002025 if (six_byte) {
2026 output_len = 4 + (ebd ? 8 : 0);
2027 alloc_len = scsicmd[4];
2028 } else {
2029 output_len = 8 + (ebd ? 8 : 0);
2030 alloc_len = (scsicmd[7] << 8) + scsicmd[8];
2031 }
2032 minlen = (alloc_len < buflen) ? alloc_len : buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
2034 p = rbuf + output_len;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002035 last = rbuf + minlen - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002037 pg = scsicmd[2] & 0x3f;
2038 spg = scsicmd[3];
2039 /*
2040 * No mode subpages supported (yet) but asking for _all_
2041 * subpages may be valid
2042 */
2043 if (spg && (spg != ALL_SUB_MPAGES))
2044 goto invalid_fld;
2045
2046 switch(pg) {
2047 case RW_RECOVERY_MPAGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 output_len += ata_msense_rw_recovery(&p, last);
2049 break;
2050
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002051 case CACHE_MPAGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 output_len += ata_msense_caching(args->id, &p, last);
2053 break;
2054
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002055 case CONTROL_MPAGE: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056 output_len += ata_msense_ctl_mode(&p, last);
2057 break;
2058 }
2059
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002060 case ALL_MPAGES:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002061 output_len += ata_msense_rw_recovery(&p, last);
2062 output_len += ata_msense_caching(args->id, &p, last);
2063 output_len += ata_msense_ctl_mode(&p, last);
2064 break;
2065
2066 default: /* invalid page code */
Douglas Gilbertae006512005-10-09 09:09:35 -04002067 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002068 }
2069
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002070 if (minlen < 1)
2071 return 0;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002072
2073 dpofua = 0;
Alan Coxf79d4092006-05-22 16:55:11 +01002074 if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002075 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
2076 dpofua = 1 << 4;
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 if (six_byte) {
2079 output_len--;
2080 rbuf[0] = output_len;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002081 if (minlen > 2)
2082 rbuf[2] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002083 if (ebd) {
2084 if (minlen > 3)
2085 rbuf[3] = sizeof(sat_blk_desc);
2086 if (minlen > 11)
2087 memcpy(rbuf + 4, sat_blk_desc,
2088 sizeof(sat_blk_desc));
2089 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002090 } else {
2091 output_len -= 2;
2092 rbuf[0] = output_len >> 8;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002093 if (minlen > 1)
2094 rbuf[1] = output_len;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002095 if (minlen > 3)
2096 rbuf[3] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002097 if (ebd) {
2098 if (minlen > 7)
2099 rbuf[7] = sizeof(sat_blk_desc);
2100 if (minlen > 15)
2101 memcpy(rbuf + 8, sat_blk_desc,
2102 sizeof(sat_blk_desc));
2103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04002106
2107invalid_fld:
2108 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
2109 /* "Invalid field in cbd" */
2110 return 1;
2111
2112saving_not_supp:
2113 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
2114 /* "Saving parameters not supported" */
2115 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002116}
2117
2118/**
2119 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2120 * @args: device IDENTIFY data / SCSI command of interest.
2121 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2122 * @buflen: Response buffer length.
2123 *
2124 * Simulate READ CAPACITY commands.
2125 *
2126 * LOCKING:
Tejun Heo6a362612006-11-20 11:15:47 +09002127 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
2130 unsigned int buflen)
2131{
Tejun Heo6a362612006-11-20 11:15:47 +09002132 u64 last_lba = args->dev->n_sectors - 1; /* LBA of the last block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133
2134 VPRINTK("ENTER\n");
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 if (args->cmd->cmnd[0] == READ_CAPACITY) {
Tejun Heo6a362612006-11-20 11:15:47 +09002137 if (last_lba >= 0xffffffffULL)
2138 last_lba = 0xffffffff;
Philip Pokorny0c144d02005-05-28 01:24:47 -07002139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 /* sector count, 32-bit */
Tejun Heo6a362612006-11-20 11:15:47 +09002141 ATA_SCSI_RBUF_SET(0, last_lba >> (8 * 3));
2142 ATA_SCSI_RBUF_SET(1, last_lba >> (8 * 2));
2143 ATA_SCSI_RBUF_SET(2, last_lba >> (8 * 1));
2144 ATA_SCSI_RBUF_SET(3, last_lba);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002145
2146 /* sector size */
Tejun Heo6a362612006-11-20 11:15:47 +09002147 ATA_SCSI_RBUF_SET(6, ATA_SECT_SIZE >> 8);
2148 ATA_SCSI_RBUF_SET(7, ATA_SECT_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 } else {
2150 /* sector count, 64-bit */
Tejun Heo6a362612006-11-20 11:15:47 +09002151 ATA_SCSI_RBUF_SET(0, last_lba >> (8 * 7));
2152 ATA_SCSI_RBUF_SET(1, last_lba >> (8 * 6));
2153 ATA_SCSI_RBUF_SET(2, last_lba >> (8 * 5));
2154 ATA_SCSI_RBUF_SET(3, last_lba >> (8 * 4));
2155 ATA_SCSI_RBUF_SET(4, last_lba >> (8 * 3));
2156 ATA_SCSI_RBUF_SET(5, last_lba >> (8 * 2));
2157 ATA_SCSI_RBUF_SET(6, last_lba >> (8 * 1));
2158 ATA_SCSI_RBUF_SET(7, last_lba);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159
2160 /* sector size */
Tejun Heo6a362612006-11-20 11:15:47 +09002161 ATA_SCSI_RBUF_SET(10, ATA_SECT_SIZE >> 8);
2162 ATA_SCSI_RBUF_SET(11, ATA_SECT_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 }
2164
2165 return 0;
2166}
2167
2168/**
2169 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2170 * @args: device IDENTIFY data / SCSI command of interest.
2171 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2172 * @buflen: Response buffer length.
2173 *
2174 * Simulate REPORT LUNS command.
2175 *
2176 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002177 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002178 */
2179
2180unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
2181 unsigned int buflen)
2182{
2183 VPRINTK("ENTER\n");
2184 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2185
2186 return 0;
2187}
2188
2189/**
Douglas Gilbert845c5832005-10-09 08:55:41 -04002190 * ata_scsi_set_sense - Set SCSI sense data and status
2191 * @cmd: SCSI request to be handled
2192 * @sk: SCSI-defined sense key
2193 * @asc: SCSI-defined additional sense code
2194 * @ascq: SCSI-defined additional sense code qualifier
2195 *
2196 * Helper function that builds a valid fixed format, current
2197 * response code and the given sense key (sk), additional sense
2198 * code (asc) and additional sense code qualifier (ascq) with
2199 * a SCSI command status of %SAM_STAT_CHECK_CONDITION and
2200 * DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
2201 *
2202 * LOCKING:
2203 * Not required
2204 */
2205
2206void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
2207{
2208 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
2209
2210 cmd->sense_buffer[0] = 0x70; /* fixed format, current */
2211 cmd->sense_buffer[2] = sk;
2212 cmd->sense_buffer[7] = 18 - 8; /* additional sense length */
2213 cmd->sense_buffer[12] = asc;
2214 cmd->sense_buffer[13] = ascq;
2215}
2216
2217/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 * ata_scsi_badcmd - End a SCSI request with an error
2219 * @cmd: SCSI request to be handled
2220 * @done: SCSI command completion function
2221 * @asc: SCSI-defined additional sense code
2222 * @ascq: SCSI-defined additional sense code qualifier
2223 *
2224 * Helper function that completes a SCSI command with
2225 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
2226 * and the specified additional sense codes.
2227 *
2228 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002229 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 */
2231
2232void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
2233{
2234 DPRINTK("ENTER\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04002235 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236
2237 done(cmd);
2238}
2239
Tejun Heo77853bf2006-01-23 13:09:36 +09002240static void atapi_sense_complete(struct ata_queued_cmd *qc)
Jeff Garzika939c962005-10-05 17:09:16 -04002241{
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002242 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002243 /* FIXME: not quite right; we don't want the
2244 * translation of taskfile registers into
2245 * a sense descriptors, since that's only
2246 * correct for ATA, not ATAPI
2247 */
Tejun Heo750426a2006-11-14 22:37:35 +09002248 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002249 }
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002250
2251 qc->scsidone(qc->scsicmd);
Tejun Heo77853bf2006-01-23 13:09:36 +09002252 ata_qc_free(qc);
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002253}
2254
2255/* is it pointless to prefer PIO for "safety reasons"? */
2256static inline int ata_pio_use_silly(struct ata_port *ap)
2257{
2258 return (ap->flags & ATA_FLAG_PIO_DMA);
2259}
2260
2261static void atapi_request_sense(struct ata_queued_cmd *qc)
2262{
2263 struct ata_port *ap = qc->ap;
2264 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika939c962005-10-05 17:09:16 -04002265
2266 DPRINTK("ATAPI request sense\n");
2267
Jeff Garzika939c962005-10-05 17:09:16 -04002268 /* FIXME: is this needed? */
2269 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
2270
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002271 ap->ops->tf_read(ap, &qc->tf);
2272
2273 /* fill these in, for the case where they are -not- overwritten */
2274 cmd->sense_buffer[0] = 0x70;
2275 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2276
2277 ata_qc_reinit(qc);
2278
Jeff Garzika939c962005-10-05 17:09:16 -04002279 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
2280 qc->dma_dir = DMA_FROM_DEVICE;
2281
Tejun Heo6e7846e2006-02-12 23:32:58 +09002282 memset(&qc->cdb, 0, qc->dev->cdb_len);
Jeff Garzika939c962005-10-05 17:09:16 -04002283 qc->cdb[0] = REQUEST_SENSE;
2284 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2285
2286 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2287 qc->tf.command = ATA_CMD_PACKET;
2288
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002289 if (ata_pio_use_silly(ap)) {
2290 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
2291 qc->tf.feature |= ATAPI_PKT_DMA;
2292 } else {
2293 qc->tf.protocol = ATA_PROT_ATAPI;
2294 qc->tf.lbam = (8 * 1024) & 0xff;
2295 qc->tf.lbah = (8 * 1024) >> 8;
2296 }
Jeff Garzika939c962005-10-05 17:09:16 -04002297 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2298
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002299 qc->complete_fn = atapi_sense_complete;
Jeff Garzika939c962005-10-05 17:09:16 -04002300
Tejun Heo8e0e6942006-03-31 20:41:11 +09002301 ata_qc_issue(qc);
Jeff Garzika939c962005-10-05 17:09:16 -04002302
2303 DPRINTK("EXIT\n");
2304}
2305
Tejun Heo77853bf2006-01-23 13:09:36 +09002306static void atapi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307{
2308 struct scsi_cmnd *cmd = qc->scsicmd;
Albert Leea22e2eb2005-12-05 15:38:02 +08002309 unsigned int err_mask = qc->err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002310
Jeff Garzika7dac442005-10-30 04:44:42 -05002311 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
Jeff Garzike12669e2005-10-05 18:39:23 -04002312
Tejun Heo246619d2006-05-15 20:58:16 +09002313 /* handle completion from new EH */
2314 if (unlikely(qc->ap->ops->error_handler &&
2315 (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
2316
2317 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
2318 /* FIXME: not quite right; we don't want the
2319 * translation of taskfile registers into a
2320 * sense descriptors, since that's only
2321 * correct for ATA, not ATAPI
2322 */
Tejun Heo750426a2006-11-14 22:37:35 +09002323 ata_gen_passthru_sense(qc);
Tejun Heo246619d2006-05-15 20:58:16 +09002324 }
2325
Tejun Heo22aac082006-08-08 14:08:59 +09002326 /* SCSI EH automatically locks door if sdev->locked is
2327 * set. Sometimes door lock request continues to
2328 * fail, for example, when no media is present. This
2329 * creates a loop - SCSI EH issues door lock which
2330 * fails and gets invoked again to acquire sense data
2331 * for the failed command.
2332 *
2333 * If door lock fails, always clear sdev->locked to
2334 * avoid this infinite loop.
2335 */
2336 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL)
2337 qc->dev->sdev->locked = 0;
2338
Tejun Heo246619d2006-05-15 20:58:16 +09002339 qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
2340 qc->scsidone(cmd);
2341 ata_qc_free(qc);
2342 return;
2343 }
2344
2345 /* successful completion or old EH failure path */
Jeff Garzika7dac442005-10-30 04:44:42 -05002346 if (unlikely(err_mask & AC_ERR_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 cmd->result = SAM_STAT_CHECK_CONDITION;
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002348 atapi_request_sense(qc);
Tejun Heo77853bf2006-01-23 13:09:36 +09002349 return;
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002350 } else if (unlikely(err_mask)) {
Jeff Garzika7dac442005-10-30 04:44:42 -05002351 /* FIXME: not quite right; we don't want the
2352 * translation of taskfile registers into
2353 * a sense descriptors, since that's only
2354 * correct for ATA, not ATAPI
2355 */
Tejun Heo750426a2006-11-14 22:37:35 +09002356 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002357 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 u8 *scsicmd = cmd->cmnd;
2359
Tony Battersbyfd71da42005-12-21 16:35:44 -05002360 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002361 u8 *buf = NULL;
2362 unsigned int buflen;
2363
2364 buflen = ata_scsi_rbuf_get(cmd, &buf);
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002365
2366 /* ATAPI devices typically report zero for their SCSI version,
2367 * and sometimes deviate from the spec WRT response data
2368 * format. If SCSI version is reported as zero like normal,
2369 * then we make the following fixups: 1) Fake MMC-5 version,
2370 * to indicate to the Linux scsi midlayer this is a modern
2371 * device. 2) Ensure response data format / ATAPI information
2372 * are always correct.
2373 */
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002374 if (buf[2] == 0) {
2375 buf[2] = 0x5;
2376 buf[3] = 0x32;
2377 }
2378
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379 ata_scsi_rbuf_put(cmd, buf);
2380 }
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002381
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382 cmd->result = SAM_STAT_GOOD;
2383 }
2384
2385 qc->scsidone(cmd);
Tejun Heo77853bf2006-01-23 13:09:36 +09002386 ata_qc_free(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387}
2388/**
2389 * atapi_xlat - Initialize PACKET taskfile
2390 * @qc: command structure to be initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391 *
2392 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002393 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002394 *
2395 * RETURNS:
2396 * Zero on success, non-zero on failure.
2397 */
Tejun Heoad706992006-12-17 10:45:57 +09002398static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002399{
Tejun Heo542b1442006-12-17 10:45:08 +09002400 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002401 struct ata_device *dev = qc->dev;
2402 int using_pio = (dev->flags & ATA_DFLAG_PIO);
Tejun Heo542b1442006-12-17 10:45:08 +09002403 int nodata = (scmd->sc_data_direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002404
2405 if (!using_pio)
2406 /* Check whether ATAPI DMA is safe */
2407 if (ata_check_atapi_dma(qc))
2408 using_pio = 1;
2409
Tejun Heo2e5704f2006-12-17 10:46:33 +09002410 memset(qc->cdb, 0, dev->cdb_len);
2411 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412
2413 qc->complete_fn = atapi_qc_complete;
2414
2415 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heo542b1442006-12-17 10:45:08 +09002416 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002417 qc->tf.flags |= ATA_TFLAG_WRITE;
2418 DPRINTK("direction: write\n");
2419 }
2420
2421 qc->tf.command = ATA_CMD_PACKET;
2422
2423 /* no data, or PIO data xfer */
2424 if (using_pio || nodata) {
2425 if (nodata)
2426 qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
2427 else
2428 qc->tf.protocol = ATA_PROT_ATAPI;
2429 qc->tf.lbam = (8 * 1024) & 0xff;
2430 qc->tf.lbah = (8 * 1024) >> 8;
2431 }
2432
2433 /* DMA data xfer */
2434 else {
2435 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
2436 qc->tf.feature |= ATAPI_PKT_DMA;
2437
Tejun Heo542b1442006-12-17 10:45:08 +09002438 if (atapi_dmadir && (scmd->sc_data_direction != DMA_TO_DEVICE))
Albert Lee95de7192006-04-04 10:57:18 +08002439 /* some SATA bridges need us to indicate data xfer direction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002440 qc->tf.feature |= ATAPI_DMADIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441 }
2442
Tejun Heo542b1442006-12-17 10:45:08 +09002443 qc->nbytes = scmd->request_bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
2445 return 0;
2446}
2447
Tejun Heoab5b3a52006-05-31 18:27:34 +09002448static struct ata_device * ata_find_dev(struct ata_port *ap, int id)
2449{
2450 if (likely(id < ATA_MAX_DEVICES))
2451 return &ap->device[id];
2452 return NULL;
2453}
2454
2455static struct ata_device * __ata_scsi_find_dev(struct ata_port *ap,
2456 const struct scsi_device *scsidev)
2457{
2458 /* skip commands not addressed to targets we simulate */
2459 if (unlikely(scsidev->channel || scsidev->lun))
2460 return NULL;
2461
2462 return ata_find_dev(ap, scsidev->id);
2463}
2464
Linus Torvalds1da177e2005-04-16 15:20:36 -07002465/**
Brian King99ba9e02006-06-12 14:54:17 -05002466 * ata_scsi_dev_enabled - determine if device is enabled
2467 * @dev: ATA device
2468 *
2469 * Determine if commands should be sent to the specified device.
2470 *
2471 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002472 * spin_lock_irqsave(host lock)
Brian King99ba9e02006-06-12 14:54:17 -05002473 *
2474 * RETURNS:
2475 * 0 if commands are not allowed / 1 if commands are allowed
2476 */
2477
2478static int ata_scsi_dev_enabled(struct ata_device *dev)
2479{
2480 if (unlikely(!ata_dev_enabled(dev)))
2481 return 0;
2482
2483 if (!atapi_enabled || (dev->ap->flags & ATA_FLAG_NO_ATAPI)) {
2484 if (unlikely(dev->class == ATA_DEV_ATAPI)) {
2485 ata_dev_printk(dev, KERN_WARNING,
2486 "WARNING: ATAPI is %s, device ignored.\n",
2487 atapi_enabled ? "not supported with this driver" : "disabled");
2488 return 0;
2489 }
2490 }
2491
2492 return 1;
2493}
2494
2495/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002496 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2497 * @ap: ATA port to which the device is attached
2498 * @scsidev: SCSI device from which we derive the ATA device
2499 *
2500 * Given various information provided in struct scsi_cmnd,
2501 * map that onto an ATA bus, and using that mapping
2502 * determine which ata_device is associated with the
2503 * SCSI command to be sent.
2504 *
2505 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002506 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 *
2508 * RETURNS:
2509 * Associated ATA device, or %NULL if not found.
2510 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511static struct ata_device *
Jeff Garzik057ace52005-10-22 14:27:05 -04002512ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513{
Tejun Heoab5b3a52006-05-31 18:27:34 +09002514 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515
Brian King99ba9e02006-06-12 14:54:17 -05002516 if (unlikely(!dev || !ata_scsi_dev_enabled(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 return NULL;
2518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 return dev;
2520}
2521
Jeff Garzikb0955182005-05-12 15:45:22 -04002522/*
2523 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2524 * @byte1: Byte 1 from pass-thru CDB.
2525 *
2526 * RETURNS:
2527 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2528 */
2529static u8
2530ata_scsi_map_proto(u8 byte1)
2531{
2532 switch((byte1 & 0x1e) >> 1) {
2533 case 3: /* Non-data */
2534 return ATA_PROT_NODATA;
2535
2536 case 6: /* DMA */
2537 return ATA_PROT_DMA;
2538
2539 case 4: /* PIO Data-in */
2540 case 5: /* PIO Data-out */
Jeff Garzikb0955182005-05-12 15:45:22 -04002541 return ATA_PROT_PIO;
2542
2543 case 10: /* Device Reset */
2544 case 0: /* Hard Reset */
2545 case 1: /* SRST */
2546 case 2: /* Bus Idle */
2547 case 7: /* Packet */
2548 case 8: /* DMA Queued */
2549 case 9: /* Device Diagnostic */
2550 case 11: /* UDMA Data-in */
2551 case 12: /* UDMA Data-Out */
2552 case 13: /* FPDMA */
2553 default: /* Reserved */
2554 break;
2555 }
2556
2557 return ATA_PROT_UNKNOWN;
2558}
2559
2560/**
2561 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
2562 * @qc: command structure to be initialized
Jeff Garzikb0955182005-05-12 15:45:22 -04002563 *
2564 * Handles either 12 or 16-byte versions of the CDB.
2565 *
2566 * RETURNS:
2567 * Zero on success, non-zero on failure.
2568 */
Tejun Heoad706992006-12-17 10:45:57 +09002569static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04002570{
2571 struct ata_taskfile *tf = &(qc->tf);
Tejun Heo542b1442006-12-17 10:45:08 +09002572 struct scsi_cmnd *scmd = qc->scsicmd;
Alan Coxf79d4092006-05-22 16:55:11 +01002573 struct ata_device *dev = qc->dev;
Tejun Heoad706992006-12-17 10:45:57 +09002574 const u8 *cdb = scmd->cmnd;
Jeff Garzikb0955182005-05-12 15:45:22 -04002575
Tejun Heo542b1442006-12-17 10:45:08 +09002576 if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN)
Tejun Heo9a405252005-12-02 11:49:11 +09002577 goto invalid_fld;
Jeff Garzik8190bdb2006-05-24 01:53:39 -04002578
Alan Coxf79d4092006-05-22 16:55:11 +01002579 /* We may not issue DMA commands if no DMA mode is set */
2580 if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0)
2581 goto invalid_fld;
Jeff Garzikb0955182005-05-12 15:45:22 -04002582
Tejun Heo542b1442006-12-17 10:45:08 +09002583 if (cdb[1] & 0xe0)
Albert Leef59b0cf2006-03-16 17:59:22 +08002584 /* PIO multi not supported yet */
2585 goto invalid_fld;
2586
Jeff Garzikb0955182005-05-12 15:45:22 -04002587 /*
2588 * 12 and 16 byte CDBs use different offsets to
2589 * provide the various register values.
2590 */
Tejun Heo542b1442006-12-17 10:45:08 +09002591 if (cdb[0] == ATA_16) {
Jeff Garzikb0955182005-05-12 15:45:22 -04002592 /*
2593 * 16-byte CDB - may contain extended commands.
2594 *
2595 * If that is the case, copy the upper byte register values.
2596 */
Tejun Heo542b1442006-12-17 10:45:08 +09002597 if (cdb[1] & 0x01) {
2598 tf->hob_feature = cdb[3];
2599 tf->hob_nsect = cdb[5];
2600 tf->hob_lbal = cdb[7];
2601 tf->hob_lbam = cdb[9];
2602 tf->hob_lbah = cdb[11];
Jeff Garzikb0955182005-05-12 15:45:22 -04002603 tf->flags |= ATA_TFLAG_LBA48;
2604 } else
2605 tf->flags &= ~ATA_TFLAG_LBA48;
2606
2607 /*
2608 * Always copy low byte, device and command registers.
2609 */
Tejun Heo542b1442006-12-17 10:45:08 +09002610 tf->feature = cdb[4];
2611 tf->nsect = cdb[6];
2612 tf->lbal = cdb[8];
2613 tf->lbam = cdb[10];
2614 tf->lbah = cdb[12];
2615 tf->device = cdb[13];
2616 tf->command = cdb[14];
Jeff Garzikb0955182005-05-12 15:45:22 -04002617 } else {
2618 /*
2619 * 12-byte CDB - incapable of extended commands.
2620 */
2621 tf->flags &= ~ATA_TFLAG_LBA48;
2622
Tejun Heo542b1442006-12-17 10:45:08 +09002623 tf->feature = cdb[3];
2624 tf->nsect = cdb[4];
2625 tf->lbal = cdb[5];
2626 tf->lbam = cdb[6];
2627 tf->lbah = cdb[7];
2628 tf->device = cdb[8];
2629 tf->command = cdb[9];
Jeff Garzikb0955182005-05-12 15:45:22 -04002630 }
Mark Lorddcc2d1e2005-11-13 16:22:06 -05002631 /*
2632 * If slave is possible, enforce correct master/slave bit
2633 */
2634 if (qc->ap->flags & ATA_FLAG_SLAVE_POSS)
2635 tf->device = qc->dev->devno ?
2636 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
Jeff Garzikb0955182005-05-12 15:45:22 -04002637
2638 /*
2639 * Filter SET_FEATURES - XFER MODE command -- otherwise,
2640 * SET_FEATURES - XFER MODE must be preceded/succeeded
2641 * by an update to hardware-specific registers for each
2642 * controller (i.e. the reason for ->set_piomode(),
2643 * ->set_dmamode(), and ->post_set_mode() hooks).
2644 */
2645 if ((tf->command == ATA_CMD_SET_FEATURES)
2646 && (tf->feature == SETFEATURES_XFER))
Tejun Heo9a405252005-12-02 11:49:11 +09002647 goto invalid_fld;
Jeff Garzikb0955182005-05-12 15:45:22 -04002648
2649 /*
2650 * Set flags so that all registers will be written,
2651 * and pass on write indication (used for PIO/DMA
2652 * setup.)
2653 */
2654 tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
2655
Tejun Heo542b1442006-12-17 10:45:08 +09002656 if (scmd->sc_data_direction == DMA_TO_DEVICE)
Jeff Garzikb0955182005-05-12 15:45:22 -04002657 tf->flags |= ATA_TFLAG_WRITE;
2658
2659 /*
2660 * Set transfer length.
2661 *
2662 * TODO: find out if we need to do more here to
2663 * cover scatter/gather case.
2664 */
Tejun Heo542b1442006-12-17 10:45:08 +09002665 qc->nsect = scmd->request_bufflen / ATA_SECT_SIZE;
Jeff Garzikb0955182005-05-12 15:45:22 -04002666
Tejun Heoe61e0672006-05-15 20:57:40 +09002667 /* request result TF */
2668 qc->flags |= ATA_QCFLAG_RESULT_TF;
2669
Jeff Garzikb0955182005-05-12 15:45:22 -04002670 return 0;
Tejun Heo9a405252005-12-02 11:49:11 +09002671
2672 invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +09002673 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
Tejun Heo9a405252005-12-02 11:49:11 +09002674 /* "Invalid field in cdb" */
2675 return 1;
Jeff Garzikb0955182005-05-12 15:45:22 -04002676}
2677
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678/**
2679 * ata_get_xlat_func - check if SCSI to ATA translation is possible
2680 * @dev: ATA device
2681 * @cmd: SCSI command opcode to consider
2682 *
2683 * Look up the SCSI command given, and determine whether the
2684 * SCSI command is to be translated or simulated.
2685 *
2686 * RETURNS:
2687 * Pointer to translation function if possible, %NULL if not.
2688 */
2689
2690static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
2691{
2692 switch (cmd) {
2693 case READ_6:
2694 case READ_10:
2695 case READ_16:
2696
2697 case WRITE_6:
2698 case WRITE_10:
2699 case WRITE_16:
2700 return ata_scsi_rw_xlat;
2701
2702 case SYNCHRONIZE_CACHE:
2703 if (ata_try_flush_cache(dev))
2704 return ata_scsi_flush_xlat;
2705 break;
2706
2707 case VERIFY:
2708 case VERIFY_16:
2709 return ata_scsi_verify_xlat;
Jeff Garzikb0955182005-05-12 15:45:22 -04002710
2711 case ATA_12:
2712 case ATA_16:
2713 return ata_scsi_pass_thru;
Jeff Garzikda613962005-08-29 19:01:43 -04002714
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04002715 case START_STOP:
2716 return ata_scsi_start_stop_xlat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 }
2718
2719 return NULL;
2720}
2721
2722/**
2723 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
2724 * @ap: ATA port to which the command was being sent
2725 * @cmd: SCSI command to dump
2726 *
2727 * Prints the contents of a SCSI command via printk().
2728 */
2729
2730static inline void ata_scsi_dump_cdb(struct ata_port *ap,
2731 struct scsi_cmnd *cmd)
2732{
2733#ifdef ATA_DEBUG
2734 struct scsi_device *scsidev = cmd->device;
2735 u8 *scsicmd = cmd->cmnd;
2736
2737 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
2738 ap->id,
2739 scsidev->channel, scsidev->id, scsidev->lun,
2740 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
2741 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
2742 scsicmd[8]);
2743#endif
2744}
2745
Tejun Heo542b1442006-12-17 10:45:08 +09002746static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
Tejun Heo2115ea92006-05-15 21:03:39 +09002747 void (*done)(struct scsi_cmnd *),
2748 struct ata_device *dev)
Brian Kingeb3f0f92006-03-23 17:30:02 -06002749{
Tejun Heo2115ea92006-05-15 21:03:39 +09002750 int rc = 0;
2751
Tejun Heo2e5704f2006-12-17 10:46:33 +09002752 if (unlikely(!scmd->cmd_len)) {
2753 ata_dev_printk(dev, KERN_WARNING, "WARNING: zero len CDB\n");
2754 scmd->result = DID_ERROR << 16;
2755 done(scmd);
2756 return 0;
2757 }
2758
Brian Kingeb3f0f92006-03-23 17:30:02 -06002759 if (dev->class == ATA_DEV_ATA) {
2760 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
Tejun Heo542b1442006-12-17 10:45:08 +09002761 scmd->cmnd[0]);
Brian Kingeb3f0f92006-03-23 17:30:02 -06002762
2763 if (xlat_func)
Tejun Heo542b1442006-12-17 10:45:08 +09002764 rc = ata_scsi_translate(dev, scmd, done, xlat_func);
Brian Kingeb3f0f92006-03-23 17:30:02 -06002765 else
Tejun Heo542b1442006-12-17 10:45:08 +09002766 ata_scsi_simulate(dev, scmd, done);
Brian Kingeb3f0f92006-03-23 17:30:02 -06002767 } else
Tejun Heo542b1442006-12-17 10:45:08 +09002768 rc = ata_scsi_translate(dev, scmd, done, atapi_xlat);
Tejun Heo2115ea92006-05-15 21:03:39 +09002769
2770 return rc;
Brian Kingeb3f0f92006-03-23 17:30:02 -06002771}
2772
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773/**
2774 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
2775 * @cmd: SCSI command to be sent
2776 * @done: Completion function, called when command is complete
2777 *
2778 * In some cases, this function translates SCSI commands into
2779 * ATA taskfiles, and queues the taskfiles to be sent to
2780 * hardware. In other cases, this function simulates a
2781 * SCSI device by evaluating and responding to certain
2782 * SCSI commands. This creates the overall effect of
2783 * ATA and ATAPI devices appearing as SCSI devices.
2784 *
2785 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002786 * Releases scsi-layer-held lock, and obtains host lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 *
2788 * RETURNS:
Tejun Heo2115ea92006-05-15 21:03:39 +09002789 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
2790 * 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
2793{
2794 struct ata_port *ap;
2795 struct ata_device *dev;
2796 struct scsi_device *scsidev = cmd->device;
Jeff Garzik005a5a02005-10-30 23:31:48 -05002797 struct Scsi_Host *shost = scsidev->host;
Tejun Heo2115ea92006-05-15 21:03:39 +09002798 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799
Jeff Garzik35bb94b2006-04-11 13:12:34 -04002800 ap = ata_shost_to_port(shost);
Jeff Garzik005a5a02005-10-30 23:31:48 -05002801
2802 spin_unlock(shost->host_lock);
Jeff Garzikba6a1302006-06-22 23:46:10 -04002803 spin_lock(ap->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002804
2805 ata_scsi_dump_cdb(ap, cmd);
2806
2807 dev = ata_scsi_find_dev(ap, scsidev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06002808 if (likely(dev))
Tejun Heo2115ea92006-05-15 21:03:39 +09002809 rc = __ata_scsi_queuecmd(cmd, done, dev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06002810 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811 cmd->result = (DID_BAD_TARGET << 16);
2812 done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 }
2814
Jeff Garzikba6a1302006-06-22 23:46:10 -04002815 spin_unlock(ap->lock);
Jeff Garzik005a5a02005-10-30 23:31:48 -05002816 spin_lock(shost->host_lock);
Tejun Heo2115ea92006-05-15 21:03:39 +09002817 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818}
2819
2820/**
2821 * ata_scsi_simulate - simulate SCSI command on ATA device
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002822 * @dev: the target device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 * @cmd: SCSI command being sent to device.
2824 * @done: SCSI command completion function.
2825 *
2826 * Interprets and directly executes a select list of SCSI commands
2827 * that can be handled internally.
2828 *
2829 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002830 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 */
2832
Tejun Heo3373efd2006-05-15 20:57:53 +09002833void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002834 void (*done)(struct scsi_cmnd *))
2835{
2836 struct ata_scsi_args args;
Jeff Garzik057ace52005-10-22 14:27:05 -04002837 const u8 *scsicmd = cmd->cmnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002839 args.dev = dev;
2840 args.id = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 args.cmd = cmd;
2842 args.done = done;
2843
2844 switch(scsicmd[0]) {
2845 /* no-op's, complete with success */
2846 case SYNCHRONIZE_CACHE:
2847 case REZERO_UNIT:
2848 case SEEK_6:
2849 case SEEK_10:
2850 case TEST_UNIT_READY:
2851 case FORMAT_UNIT: /* FIXME: correct? */
2852 case SEND_DIAGNOSTIC: /* FIXME: correct? */
2853 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
2854 break;
2855
2856 case INQUIRY:
2857 if (scsicmd[1] & 2) /* is CmdDt set? */
Douglas Gilbertae006512005-10-09 09:09:35 -04002858 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
2860 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
2861 else if (scsicmd[2] == 0x00)
2862 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
2863 else if (scsicmd[2] == 0x80)
2864 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
2865 else if (scsicmd[2] == 0x83)
2866 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
2867 else
Douglas Gilbertae006512005-10-09 09:09:35 -04002868 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869 break;
2870
2871 case MODE_SENSE:
2872 case MODE_SENSE_10:
2873 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
2874 break;
2875
2876 case MODE_SELECT: /* unconditionally return */
2877 case MODE_SELECT_10: /* bad-field-in-cdb */
Douglas Gilbertae006512005-10-09 09:09:35 -04002878 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002879 break;
2880
2881 case READ_CAPACITY:
2882 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2883 break;
2884
2885 case SERVICE_ACTION_IN:
2886 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
2887 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2888 else
Douglas Gilbertae006512005-10-09 09:09:35 -04002889 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002890 break;
2891
2892 case REPORT_LUNS:
2893 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
2894 break;
2895
Jeff Garzikb0955182005-05-12 15:45:22 -04002896 /* mandatory commands we haven't implemented yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002897 case REQUEST_SENSE:
2898
2899 /* all other commands */
2900 default:
Douglas Gilbertae006512005-10-09 09:09:35 -04002901 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
2902 /* "Invalid command operation code" */
2903 done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904 break;
2905 }
2906}
2907
Jeff Garzik644dd0c2005-10-03 15:55:19 -04002908void ata_scsi_scan_host(struct ata_port *ap)
2909{
2910 unsigned int i;
2911
Tejun Heo198e0fe2006-04-02 18:51:52 +09002912 if (ap->flags & ATA_FLAG_DISABLED)
Jeff Garzik644dd0c2005-10-03 15:55:19 -04002913 return;
2914
Jeff Garzik3f19ee82005-10-03 21:36:41 -04002915 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heo3edebac2006-05-31 18:27:40 +09002916 struct ata_device *dev = &ap->device[i];
2917 struct scsi_device *sdev;
Jeff Garzik3f19ee82005-10-03 21:36:41 -04002918
Tejun Heo3edebac2006-05-31 18:27:40 +09002919 if (!ata_dev_enabled(dev) || dev->sdev)
2920 continue;
2921
Jeff Garzikcca39742006-08-24 03:19:22 -04002922 sdev = __scsi_add_device(ap->scsi_host, 0, i, 0, NULL);
Tejun Heo3edebac2006-05-31 18:27:40 +09002923 if (!IS_ERR(sdev)) {
2924 dev->sdev = sdev;
2925 scsi_device_put(sdev);
2926 }
Jeff Garzik3f19ee82005-10-03 21:36:41 -04002927 }
Jeff Garzik644dd0c2005-10-03 15:55:19 -04002928}
Tejun Heo0ea035a2006-05-31 18:28:01 +09002929
2930/**
2931 * ata_scsi_offline_dev - offline attached SCSI device
2932 * @dev: ATA device to offline attached SCSI device for
2933 *
2934 * This function is called from ata_eh_hotplug() and responsible
2935 * for taking the SCSI device attached to @dev offline. This
Jeff Garzikcca39742006-08-24 03:19:22 -04002936 * function is called with host lock which protects dev->sdev
Tejun Heo0ea035a2006-05-31 18:28:01 +09002937 * against clearing.
2938 *
2939 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002940 * spin_lock_irqsave(host lock)
Tejun Heo0ea035a2006-05-31 18:28:01 +09002941 *
2942 * RETURNS:
2943 * 1 if attached SCSI device exists, 0 otherwise.
2944 */
2945int ata_scsi_offline_dev(struct ata_device *dev)
2946{
2947 if (dev->sdev) {
2948 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
2949 return 1;
2950 }
2951 return 0;
2952}
Tejun Heo580b2102006-05-31 18:28:05 +09002953
2954/**
2955 * ata_scsi_remove_dev - remove attached SCSI device
2956 * @dev: ATA device to remove attached SCSI device for
2957 *
2958 * This function is called from ata_eh_scsi_hotplug() and
2959 * responsible for removing the SCSI device attached to @dev.
2960 *
2961 * LOCKING:
2962 * Kernel thread context (may sleep).
2963 */
2964static void ata_scsi_remove_dev(struct ata_device *dev)
2965{
2966 struct ata_port *ap = dev->ap;
2967 struct scsi_device *sdev;
2968 unsigned long flags;
2969
2970 /* Alas, we need to grab scan_mutex to ensure SCSI device
2971 * state doesn't change underneath us and thus
2972 * scsi_device_get() always succeeds. The mutex locking can
2973 * be removed if there is __scsi_device_get() interface which
2974 * increments reference counts regardless of device state.
2975 */
Jeff Garzikcca39742006-08-24 03:19:22 -04002976 mutex_lock(&ap->scsi_host->scan_mutex);
Jeff Garzikba6a1302006-06-22 23:46:10 -04002977 spin_lock_irqsave(ap->lock, flags);
Tejun Heo580b2102006-05-31 18:28:05 +09002978
Jeff Garzikcca39742006-08-24 03:19:22 -04002979 /* clearing dev->sdev is protected by host lock */
Tejun Heo580b2102006-05-31 18:28:05 +09002980 sdev = dev->sdev;
2981 dev->sdev = NULL;
2982
2983 if (sdev) {
2984 /* If user initiated unplug races with us, sdev can go
Jeff Garzikcca39742006-08-24 03:19:22 -04002985 * away underneath us after the host lock and
Tejun Heo580b2102006-05-31 18:28:05 +09002986 * scan_mutex are released. Hold onto it.
2987 */
2988 if (scsi_device_get(sdev) == 0) {
2989 /* The following ensures the attached sdev is
2990 * offline on return from ata_scsi_offline_dev()
2991 * regardless it wins or loses the race
2992 * against this function.
2993 */
2994 scsi_device_set_state(sdev, SDEV_OFFLINE);
2995 } else {
2996 WARN_ON(1);
2997 sdev = NULL;
2998 }
2999 }
3000
Jeff Garzikba6a1302006-06-22 23:46:10 -04003001 spin_unlock_irqrestore(ap->lock, flags);
Jeff Garzikcca39742006-08-24 03:19:22 -04003002 mutex_unlock(&ap->scsi_host->scan_mutex);
Tejun Heo580b2102006-05-31 18:28:05 +09003003
3004 if (sdev) {
3005 ata_dev_printk(dev, KERN_INFO, "detaching (SCSI %s)\n",
3006 sdev->sdev_gendev.bus_id);
3007
3008 scsi_remove_device(sdev);
3009 scsi_device_put(sdev);
3010 }
3011}
3012
3013/**
3014 * ata_scsi_hotplug - SCSI part of hotplug
David Howells65f27f32006-11-22 14:55:48 +00003015 * @work: Pointer to ATA port to perform SCSI hotplug on
Tejun Heo580b2102006-05-31 18:28:05 +09003016 *
3017 * Perform SCSI part of hotplug. It's executed from a separate
3018 * workqueue after EH completes. This is necessary because SCSI
3019 * hot plugging requires working EH and hot unplugging is
3020 * synchronized with hot plugging with a mutex.
3021 *
3022 * LOCKING:
3023 * Kernel thread context (may sleep).
3024 */
David Howells65f27f32006-11-22 14:55:48 +00003025void ata_scsi_hotplug(struct work_struct *work)
Tejun Heo580b2102006-05-31 18:28:05 +09003026{
David Howells65f27f32006-11-22 14:55:48 +00003027 struct ata_port *ap =
3028 container_of(work, struct ata_port, hotplug_task.work);
Tejun Heo580b2102006-05-31 18:28:05 +09003029 int i;
3030
Tejun Heob51e9e52006-06-29 01:29:30 +09003031 if (ap->pflags & ATA_PFLAG_UNLOADING) {
Tejun Heo580b2102006-05-31 18:28:05 +09003032 DPRINTK("ENTER/EXIT - unloading\n");
3033 return;
3034 }
3035
3036 DPRINTK("ENTER\n");
3037
3038 /* unplug detached devices */
3039 for (i = 0; i < ATA_MAX_DEVICES; i++) {
3040 struct ata_device *dev = &ap->device[i];
3041 unsigned long flags;
3042
3043 if (!(dev->flags & ATA_DFLAG_DETACHED))
3044 continue;
3045
Jeff Garzikba6a1302006-06-22 23:46:10 -04003046 spin_lock_irqsave(ap->lock, flags);
Tejun Heo580b2102006-05-31 18:28:05 +09003047 dev->flags &= ~ATA_DFLAG_DETACHED;
Jeff Garzikba6a1302006-06-22 23:46:10 -04003048 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo580b2102006-05-31 18:28:05 +09003049
3050 ata_scsi_remove_dev(dev);
3051 }
3052
3053 /* scan for new ones */
3054 ata_scsi_scan_host(ap);
3055
3056 /* If we scanned while EH was in progress, scan would have
3057 * failed silently. Requeue if there are enabled but
3058 * unattached devices.
3059 */
3060 for (i = 0; i < ATA_MAX_DEVICES; i++) {
3061 struct ata_device *dev = &ap->device[i];
3062 if (ata_dev_enabled(dev) && !dev->sdev) {
3063 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, HZ);
3064 break;
3065 }
3066 }
3067
3068 DPRINTK("EXIT\n");
3069}
Tejun Heo83c47bc2006-05-31 18:28:07 +09003070
3071/**
3072 * ata_scsi_user_scan - indication for user-initiated bus scan
3073 * @shost: SCSI host to scan
3074 * @channel: Channel to scan
3075 * @id: ID to scan
3076 * @lun: LUN to scan
3077 *
3078 * This function is called when user explicitly requests bus
3079 * scan. Set probe pending flag and invoke EH.
3080 *
3081 * LOCKING:
3082 * SCSI layer (we don't care)
3083 *
3084 * RETURNS:
3085 * Zero.
3086 */
3087static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
3088 unsigned int id, unsigned int lun)
3089{
3090 struct ata_port *ap = ata_shost_to_port(shost);
3091 unsigned long flags;
3092 int rc = 0;
3093
3094 if (!ap->ops->error_handler)
3095 return -EOPNOTSUPP;
3096
3097 if ((channel != SCAN_WILD_CARD && channel != 0) ||
3098 (lun != SCAN_WILD_CARD && lun != 0))
3099 return -EINVAL;
3100
Jeff Garzikba6a1302006-06-22 23:46:10 -04003101 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09003102
3103 if (id == SCAN_WILD_CARD) {
3104 ap->eh_info.probe_mask |= (1 << ATA_MAX_DEVICES) - 1;
3105 ap->eh_info.action |= ATA_EH_SOFTRESET;
3106 } else {
3107 struct ata_device *dev = ata_find_dev(ap, id);
3108
3109 if (dev) {
3110 ap->eh_info.probe_mask |= 1 << dev->devno;
3111 ap->eh_info.action |= ATA_EH_SOFTRESET;
Tejun Heo28324302006-07-03 16:07:26 +09003112 ap->eh_info.flags |= ATA_EHI_RESUME_LINK;
Tejun Heo83c47bc2006-05-31 18:28:07 +09003113 } else
3114 rc = -EINVAL;
3115 }
3116
Tejun Heo309afcb2006-09-30 18:07:17 +09003117 if (rc == 0) {
Tejun Heo83c47bc2006-05-31 18:28:07 +09003118 ata_port_schedule_eh(ap);
Tejun Heo309afcb2006-09-30 18:07:17 +09003119 spin_unlock_irqrestore(ap->lock, flags);
3120 ata_port_wait_eh(ap);
3121 } else
3122 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09003123
3124 return rc;
3125}
zhao, forrest3057ac32006-06-12 12:01:34 +08003126
3127/**
Tejun Heod0171262006-06-12 22:51:14 +09003128 * ata_scsi_dev_rescan - initiate scsi_rescan_device()
David Howells65f27f32006-11-22 14:55:48 +00003129 * @work: Pointer to ATA port to perform scsi_rescan_device()
zhao, forrest3057ac32006-06-12 12:01:34 +08003130 *
Tejun Heod0171262006-06-12 22:51:14 +09003131 * After ATA pass thru (SAT) commands are executed successfully,
3132 * libata need to propagate the changes to SCSI layer. This
3133 * function must be executed from ata_aux_wq such that sdev
3134 * attach/detach don't race with rescan.
zhao, forrest3057ac32006-06-12 12:01:34 +08003135 *
Tejun Heod0171262006-06-12 22:51:14 +09003136 * LOCKING:
3137 * Kernel thread context (may sleep).
zhao, forrest3057ac32006-06-12 12:01:34 +08003138 */
David Howells65f27f32006-11-22 14:55:48 +00003139void ata_scsi_dev_rescan(struct work_struct *work)
zhao, forrest3057ac32006-06-12 12:01:34 +08003140{
David Howells65f27f32006-11-22 14:55:48 +00003141 struct ata_port *ap =
3142 container_of(work, struct ata_port, scsi_rescan_task);
Tejun Heof84e7e42006-11-22 11:21:31 +09003143 unsigned long flags;
zhao, forrest3057ac32006-06-12 12:01:34 +08003144 unsigned int i;
3145
Tejun Heof84e7e42006-11-22 11:21:31 +09003146 spin_lock_irqsave(ap->lock, flags);
zhao, forrest3057ac32006-06-12 12:01:34 +08003147
Tejun Heof84e7e42006-11-22 11:21:31 +09003148 for (i = 0; i < ATA_MAX_DEVICES; i++) {
3149 struct ata_device *dev = &ap->device[i];
3150 struct scsi_device *sdev = dev->sdev;
3151
3152 if (!ata_dev_enabled(dev) || !sdev)
3153 continue;
3154 if (scsi_device_get(sdev))
3155 continue;
3156
3157 spin_unlock_irqrestore(ap->lock, flags);
3158 scsi_rescan_device(&(sdev->sdev_gendev));
3159 scsi_device_put(sdev);
3160 spin_lock_irqsave(ap->lock, flags);
zhao, forrest3057ac32006-06-12 12:01:34 +08003161 }
Tejun Heof84e7e42006-11-22 11:21:31 +09003162
3163 spin_unlock_irqrestore(ap->lock, flags);
zhao, forrest3057ac32006-06-12 12:01:34 +08003164}
Brian King80289162006-08-07 14:27:31 -05003165
3166/**
3167 * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
Jeff Garzik4f931372006-09-29 05:07:25 -04003168 * @host: ATA host container for all SAS ports
Brian King80289162006-08-07 14:27:31 -05003169 * @port_info: Information from low-level host driver
Jeff Garzikcca39742006-08-24 03:19:22 -04003170 * @shost: SCSI host that the scsi device is attached to
Brian King80289162006-08-07 14:27:31 -05003171 *
3172 * LOCKING:
3173 * PCI/etc. bus probe sem.
3174 *
3175 * RETURNS:
3176 * ata_port pointer on success / NULL on failure.
3177 */
3178
Jeff Garzikcca39742006-08-24 03:19:22 -04003179struct ata_port *ata_sas_port_alloc(struct ata_host *host,
Brian King80289162006-08-07 14:27:31 -05003180 struct ata_port_info *port_info,
Jeff Garzikcca39742006-08-24 03:19:22 -04003181 struct Scsi_Host *shost)
Brian King80289162006-08-07 14:27:31 -05003182{
3183 struct ata_port *ap = kzalloc(sizeof(*ap), GFP_KERNEL);
3184 struct ata_probe_ent *ent;
3185
3186 if (!ap)
3187 return NULL;
3188
Jeff Garzikcca39742006-08-24 03:19:22 -04003189 ent = ata_probe_ent_alloc(host->dev, port_info);
Brian King80289162006-08-07 14:27:31 -05003190 if (!ent) {
3191 kfree(ap);
3192 return NULL;
3193 }
3194
Jeff Garzikcca39742006-08-24 03:19:22 -04003195 ata_port_init(ap, host, ent, 0);
3196 ap->lock = shost->host_lock;
Brian King80289162006-08-07 14:27:31 -05003197 kfree(ent);
3198 return ap;
3199}
3200EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
3201
3202/**
3203 * ata_sas_port_start - Set port up for dma.
3204 * @ap: Port to initialize
3205 *
3206 * Called just after data structures for each port are
3207 * initialized. Allocates DMA pad.
3208 *
3209 * May be used as the port_start() entry in ata_port_operations.
3210 *
3211 * LOCKING:
3212 * Inherited from caller.
3213 */
3214int ata_sas_port_start(struct ata_port *ap)
3215{
3216 return ata_pad_alloc(ap, ap->dev);
3217}
3218EXPORT_SYMBOL_GPL(ata_sas_port_start);
3219
3220/**
3221 * ata_port_stop - Undo ata_sas_port_start()
3222 * @ap: Port to shut down
3223 *
3224 * Frees the DMA pad.
3225 *
3226 * May be used as the port_stop() entry in ata_port_operations.
3227 *
3228 * LOCKING:
3229 * Inherited from caller.
3230 */
3231
3232void ata_sas_port_stop(struct ata_port *ap)
3233{
3234 ata_pad_free(ap, ap->dev);
3235}
3236EXPORT_SYMBOL_GPL(ata_sas_port_stop);
3237
3238/**
3239 * ata_sas_port_init - Initialize a SATA device
3240 * @ap: SATA port to initialize
3241 *
3242 * LOCKING:
3243 * PCI/etc. bus probe sem.
3244 *
3245 * RETURNS:
3246 * Zero on success, non-zero on error.
3247 */
3248
3249int ata_sas_port_init(struct ata_port *ap)
3250{
3251 int rc = ap->ops->port_start(ap);
3252
3253 if (!rc)
3254 rc = ata_bus_probe(ap);
3255
3256 return rc;
3257}
3258EXPORT_SYMBOL_GPL(ata_sas_port_init);
3259
3260/**
3261 * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
3262 * @ap: SATA port to destroy
3263 *
3264 */
3265
3266void ata_sas_port_destroy(struct ata_port *ap)
3267{
3268 ap->ops->port_stop(ap);
3269 kfree(ap);
3270}
3271EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
3272
3273/**
3274 * ata_sas_slave_configure - Default slave_config routine for libata devices
3275 * @sdev: SCSI device to configure
3276 * @ap: ATA port to which SCSI device is attached
3277 *
3278 * RETURNS:
3279 * Zero.
3280 */
3281
3282int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
3283{
3284 ata_scsi_sdev_config(sdev);
3285 ata_scsi_dev_config(sdev, ap->device);
3286 return 0;
3287}
3288EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
3289
3290/**
3291 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
3292 * @cmd: SCSI command to be sent
3293 * @done: Completion function, called when command is complete
3294 * @ap: ATA port to which the command is being sent
3295 *
3296 * RETURNS:
Brian King08475a12006-11-20 13:51:56 -06003297 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
3298 * 0 otherwise.
Brian King80289162006-08-07 14:27:31 -05003299 */
3300
3301int ata_sas_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *),
3302 struct ata_port *ap)
3303{
Brian King08475a12006-11-20 13:51:56 -06003304 int rc = 0;
3305
Brian King80289162006-08-07 14:27:31 -05003306 ata_scsi_dump_cdb(ap, cmd);
3307
3308 if (likely(ata_scsi_dev_enabled(ap->device)))
Brian King08475a12006-11-20 13:51:56 -06003309 rc = __ata_scsi_queuecmd(cmd, done, ap->device);
Brian King80289162006-08-07 14:27:31 -05003310 else {
3311 cmd->result = (DID_BAD_TARGET << 16);
3312 done(cmd);
3313 }
Brian King08475a12006-11-20 13:51:56 -06003314 return rc;
Brian King80289162006-08-07 14:27:31 -05003315}
3316EXPORT_SYMBOL_GPL(ata_sas_queuecmd);