blob: 307910bd62c3474527b6d3300c0d570737aa82b7 [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];
276 u8 args[7];
Mike Christie85837eb2005-11-11 16:38:53 -0600277 struct scsi_sense_hdr sshdr;
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
285 memset(scsi_cmd, 0, sizeof(scsi_cmd));
286 scsi_cmd[0] = ATA_16;
287 scsi_cmd[1] = (3 << 1); /* Non-data */
288 /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */
289 scsi_cmd[4] = args[1];
290 scsi_cmd[6] = args[2];
291 scsi_cmd[8] = args[3];
292 scsi_cmd[10] = args[4];
293 scsi_cmd[12] = args[5];
294 scsi_cmd[14] = args[0];
295
Jeff Garzikb0955182005-05-12 15:45:22 -0400296 /* Good values for timeout and retries? Values below
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500297 from scsi_ioctl_send_command() for default case... */
Mike Christie85837eb2005-11-11 16:38:53 -0600298 if (scsi_execute_req(scsidev, scsi_cmd, DMA_NONE, NULL, 0, &sshdr,
299 (10*HZ), 5))
Jeff Garzikb0955182005-05-12 15:45:22 -0400300 rc = -EIO;
Jeff Garzikb0955182005-05-12 15:45:22 -0400301
302 /* Need code to retrieve data from check condition? */
Jeff Garzikb0955182005-05-12 15:45:22 -0400303 return rc;
304}
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
307{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 int val = -EINVAL, rc = -EINVAL;
309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 switch (cmd) {
311 case ATA_IOC_GET_IO32:
312 val = 0;
313 if (copy_to_user(arg, &val, 1))
314 return -EFAULT;
315 return 0;
316
317 case ATA_IOC_SET_IO32:
318 val = (unsigned long) arg;
319 if (val != 0)
320 return -EINVAL;
321 return 0;
322
Jeff Garzikb0955182005-05-12 15:45:22 -0400323 case HDIO_DRIVE_CMD:
324 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
325 return -EACCES;
326 return ata_cmd_ioctl(scsidev, arg);
327
328 case HDIO_DRIVE_TASK:
329 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
330 return -EACCES;
331 return ata_task_ioctl(scsidev, arg);
332
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 default:
334 rc = -ENOTTY;
335 break;
336 }
337
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 return rc;
339}
340
341/**
342 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 * @dev: ATA device to which the new command is attached
344 * @cmd: SCSI command that originated this ATA command
345 * @done: SCSI command completion function
346 *
347 * Obtain a reference to an unused ata_queued_cmd structure,
348 * which is the basic libata structure representing a single
349 * ATA command sent to the hardware.
350 *
351 * If a command was available, fill in the SCSI-specific
352 * portions of the structure with information on the
353 * current command.
354 *
355 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400356 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 *
358 * RETURNS:
359 * Command allocated, or %NULL if none available.
360 */
Tejun Heo3373efd2006-05-15 20:57:53 +0900361struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 struct scsi_cmnd *cmd,
363 void (*done)(struct scsi_cmnd *))
364{
365 struct ata_queued_cmd *qc;
366
Tejun Heo3373efd2006-05-15 20:57:53 +0900367 qc = ata_qc_new_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (qc) {
369 qc->scsicmd = cmd;
370 qc->scsidone = done;
371
372 if (cmd->use_sg) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -0400373 qc->__sg = (struct scatterlist *) cmd->request_buffer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 qc->n_elem = cmd->use_sg;
375 } else {
Jeff Garzikcedc9a42005-10-05 07:13:30 -0400376 qc->__sg = &qc->sgent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 qc->n_elem = 1;
378 }
379 } else {
380 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
381 done(cmd);
382 }
383
384 return qc;
385}
386
387/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400388 * ata_dump_status - user friendly display of error info
389 * @id: id of the port in question
390 * @tf: ptr to filled out taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 *
Jeff Garzikb0955182005-05-12 15:45:22 -0400392 * Decode and dump the ATA error/status registers for the user so
393 * that they have some idea what really happened at the non
394 * make-believe layer.
395 *
396 * LOCKING:
397 * inherited from caller
398 */
399void ata_dump_status(unsigned id, struct ata_taskfile *tf)
400{
401 u8 stat = tf->command, err = tf->feature;
402
403 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
404 if (stat & ATA_BUSY) {
405 printk("Busy }\n"); /* Data is not valid in this case */
406 } else {
407 if (stat & 0x40) printk("DriveReady ");
408 if (stat & 0x20) printk("DeviceFault ");
409 if (stat & 0x10) printk("SeekComplete ");
410 if (stat & 0x08) printk("DataRequest ");
411 if (stat & 0x04) printk("CorrectedError ");
412 if (stat & 0x02) printk("Index ");
413 if (stat & 0x01) printk("Error ");
414 printk("}\n");
415
416 if (err) {
417 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
418 if (err & 0x04) printk("DriveStatusError ");
419 if (err & 0x80) {
420 if (err & 0x04) printk("BadCRC ");
421 else printk("Sector ");
422 }
423 if (err & 0x40) printk("UncorrectableError ");
424 if (err & 0x10) printk("SectorIdNotFound ");
425 if (err & 0x02) printk("TrackZeroNotFound ");
426 if (err & 0x01) printk("AddrMarkNotFound ");
427 printk("}\n");
428 }
429 }
430}
431
Tejun Heod6f26d12006-07-03 16:07:26 +0900432/**
433 * ata_scsi_device_suspend - suspend ATA device associated with sdev
434 * @sdev: the SCSI device to suspend
Tejun Heo3c5100c2006-07-26 16:58:33 +0900435 * @mesg: target power management message
Tejun Heod6f26d12006-07-03 16:07:26 +0900436 *
437 * Request suspend EH action on the ATA device associated with
438 * @sdev and wait for the operation to complete.
439 *
440 * LOCKING:
441 * Kernel thread context (may sleep).
442 *
443 * RETURNS:
444 * 0 on success, -errno otherwise.
445 */
Tejun Heo3c5100c2006-07-26 16:58:33 +0900446int ata_scsi_device_suspend(struct scsi_device *sdev, pm_message_t mesg)
Jens Axboe9b847542006-01-06 09:28:07 +0100447{
Jeff Garzik35bb94b2006-04-11 13:12:34 -0400448 struct ata_port *ap = ata_shost_to_port(sdev->host);
Tejun Heod6f26d12006-07-03 16:07:26 +0900449 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
450 unsigned long flags;
451 unsigned int action;
452 int rc = 0;
Jens Axboe9b847542006-01-06 09:28:07 +0100453
Tejun Heod6f26d12006-07-03 16:07:26 +0900454 if (!dev)
455 goto out;
456
457 spin_lock_irqsave(ap->lock, flags);
458
459 /* wait for the previous resume to complete */
460 while (dev->flags & ATA_DFLAG_SUSPENDED) {
461 spin_unlock_irqrestore(ap->lock, flags);
462 ata_port_wait_eh(ap);
463 spin_lock_irqsave(ap->lock, flags);
464 }
465
466 /* if @sdev is already detached, nothing to do */
467 if (sdev->sdev_state == SDEV_OFFLINE ||
468 sdev->sdev_state == SDEV_CANCEL || sdev->sdev_state == SDEV_DEL)
469 goto out_unlock;
470
471 /* request suspend */
472 action = ATA_EH_SUSPEND;
Tejun Heo3c5100c2006-07-26 16:58:33 +0900473 if (mesg.event != PM_EVENT_SUSPEND)
Tejun Heod6f26d12006-07-03 16:07:26 +0900474 action |= ATA_EH_PM_FREEZE;
475 ap->eh_info.dev_action[dev->devno] |= action;
476 ap->eh_info.flags |= ATA_EHI_QUIET;
477 ata_port_schedule_eh(ap);
478
479 spin_unlock_irqrestore(ap->lock, flags);
480
481 /* wait for EH to do the job */
482 ata_port_wait_eh(ap);
483
484 spin_lock_irqsave(ap->lock, flags);
485
486 /* If @sdev is still attached but the associated ATA device
487 * isn't suspended, the operation failed.
488 */
489 if (sdev->sdev_state != SDEV_OFFLINE &&
490 sdev->sdev_state != SDEV_CANCEL && sdev->sdev_state != SDEV_DEL &&
491 !(dev->flags & ATA_DFLAG_SUSPENDED))
492 rc = -EIO;
493
494 out_unlock:
495 spin_unlock_irqrestore(ap->lock, flags);
496 out:
497 if (rc == 0)
Tejun Heo3c5100c2006-07-26 16:58:33 +0900498 sdev->sdev_gendev.power.power_state = mesg;
Tejun Heod6f26d12006-07-03 16:07:26 +0900499 return rc;
500}
501
502/**
503 * ata_scsi_device_resume - resume ATA device associated with sdev
504 * @sdev: the SCSI device to resume
505 *
506 * Request resume EH action on the ATA device associated with
507 * @sdev and return immediately. This enables parallel
508 * wakeup/spinup of devices.
509 *
510 * LOCKING:
511 * Kernel thread context (may sleep).
512 *
513 * RETURNS:
514 * 0.
515 */
516int ata_scsi_device_resume(struct scsi_device *sdev)
517{
518 struct ata_port *ap = ata_shost_to_port(sdev->host);
519 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
520 struct ata_eh_info *ehi = &ap->eh_info;
521 unsigned long flags;
522 unsigned int action;
523
524 if (!dev)
525 goto out;
526
527 spin_lock_irqsave(ap->lock, flags);
528
529 /* if @sdev is already detached, nothing to do */
530 if (sdev->sdev_state == SDEV_OFFLINE ||
531 sdev->sdev_state == SDEV_CANCEL || sdev->sdev_state == SDEV_DEL)
532 goto out_unlock;
533
534 /* request resume */
535 action = ATA_EH_RESUME;
536 if (sdev->sdev_gendev.power.power_state.event == PM_EVENT_SUSPEND)
537 __ata_ehi_hotplugged(ehi);
538 else
539 action |= ATA_EH_PM_FREEZE | ATA_EH_SOFTRESET;
540 ehi->dev_action[dev->devno] |= action;
541
542 /* We don't want autopsy and verbose EH messages. Disable
543 * those if we're the only device on this link.
544 */
545 if (ata_port_max_devices(ap) == 1)
546 ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
547
548 ata_port_schedule_eh(ap);
549
550 out_unlock:
551 spin_unlock_irqrestore(ap->lock, flags);
552 out:
553 sdev->sdev_gendev.power.power_state = PMSG_ON;
554 return 0;
Jens Axboe9b847542006-01-06 09:28:07 +0100555}
556
Jeff Garzikb0955182005-05-12 15:45:22 -0400557/**
558 * ata_to_sense_error - convert ATA error to SCSI error
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800559 * @id: ATA device number
Jeff Garzikb0955182005-05-12 15:45:22 -0400560 * @drv_stat: value contained in ATA status register
561 * @drv_err: value contained in ATA error register
562 * @sk: the sense key we'll fill out
563 * @asc: the additional sense code we'll fill out
564 * @ascq: the additional sense code qualifier we'll fill out
Tejun Heo246619d2006-05-15 20:58:16 +0900565 * @verbose: be verbose
Jeff Garzikb0955182005-05-12 15:45:22 -0400566 *
567 * Converts an ATA error into a SCSI error. Fill out pointers to
568 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
569 * format sense blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 *
571 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400572 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500574void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
Tejun Heo246619d2006-05-15 20:58:16 +0900575 u8 *ascq, int verbose)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
Jeff Garzikb0955182005-05-12 15:45:22 -0400577 int i;
Jeff Garzikffe75ef2005-10-09 10:40:44 -0400578
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 /* Based on the 3ware driver translation table */
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100580 static const unsigned char sense_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 /* BBD|ECC|ID|MAR */
582 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
583 /* BBD|ECC|ID */
584 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
585 /* ECC|MC|MARK */
586 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
587 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
588 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
589 /* MC|ID|ABRT|TRK0|MARK */
590 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
591 /* MCR|MARK */
592 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
593 /* Bad address mark */
594 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
595 /* TRK0 */
596 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
597 /* Abort & !ICRC */
598 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
599 /* Media change request */
600 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
601 /* SRV */
602 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
603 /* Media change */
604 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
605 /* ECC */
606 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
607 /* BBD - block marked bad */
608 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
609 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
610 };
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100611 static const unsigned char stat_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* Must be first because BUSY means no other bits valid */
613 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
614 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
615 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
616 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
617 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
618 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 /*
621 * Is this an error we can process/parse
622 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400623 if (drv_stat & ATA_BUSY) {
624 drv_err = 0; /* Ignore the err bits, they're invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Jeff Garzikb0955182005-05-12 15:45:22 -0400627 if (drv_err) {
628 /* Look for drv_err */
629 for (i = 0; sense_table[i][0] != 0xFF; i++) {
630 /* Look for best matches first */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500631 if ((sense_table[i][0] & drv_err) ==
Jeff Garzikb0955182005-05-12 15:45:22 -0400632 sense_table[i][0]) {
633 *sk = sense_table[i][1];
634 *asc = sense_table[i][2];
635 *ascq = sense_table[i][3];
636 goto translate_done;
637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400639 /* No immediate match */
Tejun Heo246619d2006-05-15 20:58:16 +0900640 if (verbose)
641 printk(KERN_WARNING "ata%u: no sense translation for "
642 "error 0x%02x\n", id, drv_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 /* Fall back to interpreting status bits */
Jeff Garzikb0955182005-05-12 15:45:22 -0400646 for (i = 0; stat_table[i][0] != 0xFF; i++) {
647 if (stat_table[i][0] & drv_stat) {
648 *sk = stat_table[i][1];
649 *asc = stat_table[i][2];
650 *ascq = stat_table[i][3];
651 goto translate_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400654 /* No error? Undecoded? */
Tejun Heo246619d2006-05-15 20:58:16 +0900655 if (verbose)
656 printk(KERN_WARNING "ata%u: no sense translation for "
657 "status: 0x%02x\n", id, drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Alan Cox2d202022006-03-21 15:53:46 +0000659 /* We need a sensible error return here, which is tricky, and one
660 that won't cause people to do things like return a disk wrongly */
661 *sk = ABORTED_COMMAND;
662 *asc = 0x00;
663 *ascq = 0x00;
Jeff Garzikb0955182005-05-12 15:45:22 -0400664
665 translate_done:
Tejun Heo246619d2006-05-15 20:58:16 +0900666 if (verbose)
667 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x "
668 "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
669 id, drv_stat, drv_err, *sk, *asc, *ascq);
Jeff Garzikb0955182005-05-12 15:45:22 -0400670 return;
671}
672
673/*
Tejun Heo750426a2006-11-14 22:37:35 +0900674 * ata_gen_passthru_sense - Generate check condition sense block.
Jeff Garzikb0955182005-05-12 15:45:22 -0400675 * @qc: Command that completed.
676 *
677 * This function is specific to the ATA descriptor format sense
678 * block specified for the ATA pass through commands. Regardless
679 * of whether the command errored or not, return a sense
680 * block. Copy all controller registers into the sense
681 * block. Clear sense key, ASC & ASCQ if there is no error.
682 *
683 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +0900684 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -0400685 */
Tejun Heo750426a2006-11-14 22:37:35 +0900686static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -0400687{
688 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +0900689 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -0400690 unsigned char *sb = cmd->sense_buffer;
691 unsigned char *desc = sb + 8;
Tejun Heo246619d2006-05-15 20:58:16 +0900692 int verbose = qc->ap->ops->error_handler == NULL;
Jeff Garzikb0955182005-05-12 15:45:22 -0400693
694 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
695
Jeff Garzik0e5dec42005-10-06 09:40:20 -0400696 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -0400697
698 /*
Jeff Garzikb0955182005-05-12 15:45:22 -0400699 * Use ata_to_sense_error() to map status register bits
700 * onto sense key, asc & ascq.
701 */
Tejun Heo058e55e2006-04-02 18:51:53 +0900702 if (qc->err_mask ||
703 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400704 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
Tejun Heo246619d2006-05-15 20:58:16 +0900705 &sb[1], &sb[2], &sb[3], verbose);
Jeff Garzikb0955182005-05-12 15:45:22 -0400706 sb[1] &= 0x0f;
707 }
708
709 /*
710 * Sense data is current and format is descriptor.
711 */
712 sb[0] = 0x72;
713
714 desc[0] = 0x09;
715
Tejun Heof38621b2006-11-14 22:37:35 +0900716 /* set length of additional sense data */
717 sb[7] = 14;
718 desc[1] = 12;
Jeff Garzikb0955182005-05-12 15:45:22 -0400719
720 /*
721 * Copy registers into sense buffer.
722 */
723 desc[2] = 0x00;
724 desc[3] = tf->feature; /* == error reg */
725 desc[5] = tf->nsect;
726 desc[7] = tf->lbal;
727 desc[9] = tf->lbam;
728 desc[11] = tf->lbah;
729 desc[12] = tf->device;
730 desc[13] = tf->command; /* == status reg */
731
732 /*
733 * Fill in Extend bit, and the high order bytes
734 * if applicable.
735 */
736 if (tf->flags & ATA_TFLAG_LBA48) {
737 desc[2] |= 0x01;
738 desc[4] = tf->hob_nsect;
739 desc[6] = tf->hob_lbal;
740 desc[8] = tf->hob_lbam;
741 desc[10] = tf->hob_lbah;
742 }
743}
744
745/**
Tejun Heo750426a2006-11-14 22:37:35 +0900746 * ata_gen_ata_sense - generate a SCSI fixed sense block
Jeff Garzikb0955182005-05-12 15:45:22 -0400747 * @qc: Command that we are erroring out
748 *
Tejun Heod25614b2006-11-14 22:37:35 +0900749 * Generate sense block for a failed ATA command @qc. Descriptor
750 * format is used to accomodate LBA48 block address.
Jeff Garzikb0955182005-05-12 15:45:22 -0400751 *
752 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +0900753 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -0400754 */
Tejun Heo750426a2006-11-14 22:37:35 +0900755static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -0400756{
Tejun Heod25614b2006-11-14 22:37:35 +0900757 struct ata_device *dev = qc->dev;
Jeff Garzikb0955182005-05-12 15:45:22 -0400758 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +0900759 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -0400760 unsigned char *sb = cmd->sense_buffer;
Tejun Heod25614b2006-11-14 22:37:35 +0900761 unsigned char *desc = sb + 8;
Tejun Heo246619d2006-05-15 20:58:16 +0900762 int verbose = qc->ap->ops->error_handler == NULL;
Tejun Heod25614b2006-11-14 22:37:35 +0900763 u64 block;
Jeff Garzikb0955182005-05-12 15:45:22 -0400764
765 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
766
Jeff Garzik0e5dec42005-10-06 09:40:20 -0400767 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -0400768
Tejun Heod25614b2006-11-14 22:37:35 +0900769 /* sense data is current and format is descriptor */
770 sb[0] = 0x72;
771
772 /* Use ata_to_sense_error() to map status register bits
Jeff Garzikb0955182005-05-12 15:45:22 -0400773 * onto sense key, asc & ascq.
774 */
Tejun Heo058e55e2006-04-02 18:51:53 +0900775 if (qc->err_mask ||
776 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400777 ata_to_sense_error(qc->ap->id, tf->command, tf->feature,
Tejun Heod25614b2006-11-14 22:37:35 +0900778 &sb[1], &sb[2], &sb[3], verbose);
779 sb[1] &= 0x0f;
Jeff Garzikb0955182005-05-12 15:45:22 -0400780 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781
Tejun Heod25614b2006-11-14 22:37:35 +0900782 block = ata_tf_read_block(&qc->result_tf, dev);
Jeff Garzik0274aa22005-06-22 13:50:56 -0400783
Tejun Heod25614b2006-11-14 22:37:35 +0900784 /* information sense data descriptor */
785 sb[7] = 12;
786 desc[0] = 0x00;
787 desc[1] = 10;
Jeff Garzika7dac442005-10-30 04:44:42 -0500788
Tejun Heod25614b2006-11-14 22:37:35 +0900789 desc[2] |= 0x80; /* valid */
790 desc[6] = block >> 40;
791 desc[7] = block >> 32;
792 desc[8] = block >> 24;
793 desc[9] = block >> 16;
794 desc[10] = block >> 8;
795 desc[11] = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Brian Kinga6cce2a2006-03-17 17:04:15 -0600798static void ata_scsi_sdev_config(struct scsi_device *sdev)
799{
800 sdev->use_10_for_rw = 1;
801 sdev->use_10_for_ms = 1;
802}
803
804static void ata_scsi_dev_config(struct scsi_device *sdev,
805 struct ata_device *dev)
806{
Tejun Heo914ed352006-11-01 18:39:55 +0900807 /* configure max sectors */
808 blk_queue_max_sectors(sdev->request_queue, dev->max_sectors);
Brian Kinga6cce2a2006-03-17 17:04:15 -0600809
Tejun Heo914ed352006-11-01 18:39:55 +0900810 /* SATA DMA transfers must be multiples of 4 byte, so
Brian Kinga6cce2a2006-03-17 17:04:15 -0600811 * we need to pad ATAPI transfers using an extra sg.
812 * Decrement max hw segments accordingly.
813 */
814 if (dev->class == ATA_DEV_ATAPI) {
815 request_queue_t *q = sdev->request_queue;
816 blk_queue_max_hw_segments(q, q->max_hw_segments - 1);
817 }
Tejun Heoa6e6ce82006-05-15 21:03:48 +0900818
819 if (dev->flags & ATA_DFLAG_NCQ) {
820 int depth;
821
822 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
823 depth = min(ATA_MAX_QUEUE - 1, depth);
824 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
825 }
Brian Kinga6cce2a2006-03-17 17:04:15 -0600826}
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828/**
829 * ata_scsi_slave_config - Set SCSI device attributes
830 * @sdev: SCSI device to examine
831 *
832 * This is called before we actually start reading
833 * and writing to the device, to configure certain
834 * SCSI mid-layer behaviors.
835 *
836 * LOCKING:
837 * Defined by SCSI layer. We don't really care.
838 */
839
840int ata_scsi_slave_config(struct scsi_device *sdev)
841{
Tejun Heo31534362006-05-31 18:27:36 +0900842 struct ata_port *ap = ata_shost_to_port(sdev->host);
843 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
844
Brian Kinga6cce2a2006-03-17 17:04:15 -0600845 ata_scsi_sdev_config(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846
847 blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD);
848
Tejun Heo31534362006-05-31 18:27:36 +0900849 if (dev)
Brian Kinga6cce2a2006-03-17 17:04:15 -0600850 ata_scsi_dev_config(sdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
852 return 0; /* scsi layer doesn't check return value, sigh */
853}
854
855/**
Tejun Heo83c47bc2006-05-31 18:28:07 +0900856 * ata_scsi_slave_destroy - SCSI device is about to be destroyed
857 * @sdev: SCSI device to be destroyed
858 *
859 * @sdev is about to be destroyed for hot/warm unplugging. If
860 * this unplugging was initiated by libata as indicated by NULL
861 * dev->sdev, this function doesn't have to do anything.
862 * Otherwise, SCSI layer initiated warm-unplug is in progress.
863 * Clear dev->sdev, schedule the device for ATA detach and invoke
864 * EH.
865 *
866 * LOCKING:
867 * Defined by SCSI layer. We don't really care.
868 */
869void ata_scsi_slave_destroy(struct scsi_device *sdev)
870{
871 struct ata_port *ap = ata_shost_to_port(sdev->host);
872 unsigned long flags;
873 struct ata_device *dev;
874
875 if (!ap->ops->error_handler)
876 return;
877
Jeff Garzikba6a1302006-06-22 23:46:10 -0400878 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +0900879 dev = __ata_scsi_find_dev(ap, sdev);
880 if (dev && dev->sdev) {
881 /* SCSI device already in CANCEL state, no need to offline it */
882 dev->sdev = NULL;
883 dev->flags |= ATA_DFLAG_DETACH;
884 ata_port_schedule_eh(ap);
885 }
Jeff Garzikba6a1302006-06-22 23:46:10 -0400886 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +0900887}
888
889/**
Tejun Heoa6e6ce82006-05-15 21:03:48 +0900890 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
891 * @sdev: SCSI device to configure queue depth for
892 * @queue_depth: new queue depth
893 *
894 * This is libata standard hostt->change_queue_depth callback.
895 * SCSI will call into this callback when user tries to set queue
896 * depth via sysfs.
897 *
898 * LOCKING:
899 * SCSI layer (we don't care)
900 *
901 * RETURNS:
902 * Newly configured queue depth.
903 */
904int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
905{
906 struct ata_port *ap = ata_shost_to_port(sdev->host);
907 struct ata_device *dev;
Tejun Heo360f6542006-09-30 19:45:00 +0900908 unsigned long flags;
Tejun Heoa6e6ce82006-05-15 21:03:48 +0900909 int max_depth;
910
911 if (queue_depth < 1)
912 return sdev->queue_depth;
913
914 dev = ata_scsi_find_dev(ap, sdev);
915 if (!dev || !ata_dev_enabled(dev))
916 return sdev->queue_depth;
917
918 max_depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
919 max_depth = min(ATA_MAX_QUEUE - 1, max_depth);
920 if (queue_depth > max_depth)
921 queue_depth = max_depth;
922
923 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, queue_depth);
Tejun Heo360f6542006-09-30 19:45:00 +0900924
925 spin_lock_irqsave(ap->lock, flags);
926 if (queue_depth > 1)
927 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
928 else
929 dev->flags |= ATA_DFLAG_NCQ_OFF;
930 spin_unlock_irqrestore(ap->lock, flags);
931
Tejun Heoa6e6ce82006-05-15 21:03:48 +0900932 return queue_depth;
933}
934
935/**
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400936 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
937 * @qc: Storage for translated ATA taskfile
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400938 *
939 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
940 * (to start). Perhaps these commands should be preceded by
941 * CHECK POWER MODE to see what power mode the device is already in.
942 * [See SAT revision 5 at www.t10.org]
943 *
944 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400945 * spin_lock_irqsave(host lock)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400946 *
947 * RETURNS:
948 * Zero on success, non-zero on error.
949 */
Tejun Heoad706992006-12-17 10:45:57 +0900950static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400951{
Tejun Heo542b1442006-12-17 10:45:08 +0900952 struct scsi_cmnd *scmd = qc->scsicmd;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400953 struct ata_taskfile *tf = &qc->tf;
Tejun Heoad706992006-12-17 10:45:57 +0900954 const u8 *cdb = scmd->cmnd;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400955
956 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
957 tf->protocol = ATA_PROT_NODATA;
Tejun Heo542b1442006-12-17 10:45:08 +0900958 if (cdb[1] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400959 ; /* ignore IMMED bit, violates sat-r05 */
960 }
Tejun Heo542b1442006-12-17 10:45:08 +0900961 if (cdb[4] & 0x2)
Douglas Gilbertae006512005-10-09 09:09:35 -0400962 goto invalid_fld; /* LOEJ bit set not supported */
Tejun Heo542b1442006-12-17 10:45:08 +0900963 if (((cdb[4] >> 4) & 0xf) != 0)
Douglas Gilbertae006512005-10-09 09:09:35 -0400964 goto invalid_fld; /* power conditions not supported */
Tejun Heo542b1442006-12-17 10:45:08 +0900965 if (cdb[4] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400966 tf->nsect = 1; /* 1 sector, lba=0 */
Albert Lee9d5b1302005-10-04 08:48:17 -0400967
968 if (qc->dev->flags & ATA_DFLAG_LBA) {
Tejun Heoc44078c2006-05-15 20:57:21 +0900969 tf->flags |= ATA_TFLAG_LBA;
Albert Lee9d5b1302005-10-04 08:48:17 -0400970
971 tf->lbah = 0x0;
972 tf->lbam = 0x0;
973 tf->lbal = 0x0;
974 tf->device |= ATA_LBA;
975 } else {
976 /* CHS */
977 tf->lbal = 0x1; /* sect */
978 tf->lbam = 0x0; /* cyl low */
979 tf->lbah = 0x0; /* cyl high */
980 }
981
Douglas Gilbert972dcaf2005-08-11 03:35:53 -0400982 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
983 } else {
984 tf->nsect = 0; /* time period value (0 implies now) */
985 tf->command = ATA_CMD_STANDBY;
986 /* Consider: ATA STANDBY IMMEDIATE command */
987 }
988 /*
989 * Standby and Idle condition timers could be implemented but that
990 * would require libata to implement the Power condition mode page
991 * and allow the user to change it. Changing mode pages requires
992 * MODE SELECT to be implemented.
993 */
994
995 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -0400996
997invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +0900998 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -0400999 /* "Invalid field in cbd" */
1000 return 1;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001001}
1002
1003
1004/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
1006 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 *
1008 * Sets up an ATA taskfile to issue FLUSH CACHE or
1009 * FLUSH CACHE EXT.
1010 *
1011 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001012 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 *
1014 * RETURNS:
1015 * Zero on success, non-zero on error.
1016 */
Tejun Heoad706992006-12-17 10:45:57 +09001017static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018{
1019 struct ata_taskfile *tf = &qc->tf;
1020
1021 tf->flags |= ATA_TFLAG_DEVICE;
1022 tf->protocol = ATA_PROT_NODATA;
1023
Tejun Heo6fc49ad2006-11-11 20:10:45 +09001024 if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 tf->command = ATA_CMD_FLUSH_EXT;
1026 else
1027 tf->command = ATA_CMD_FLUSH;
1028
1029 return 0;
1030}
1031
1032/**
Albert Lee3aef5232005-10-04 08:47:43 -04001033 * scsi_6_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001034 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001035 *
1036 * Calculate LBA and transfer length for 6-byte commands.
1037 *
1038 * RETURNS:
1039 * @plba: the LBA
1040 * @plen: the transfer length
1041 */
Tejun Heo542b1442006-12-17 10:45:08 +09001042static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001043{
1044 u64 lba = 0;
1045 u32 len = 0;
1046
1047 VPRINTK("six-byte command\n");
1048
Tejun Heo542b1442006-12-17 10:45:08 +09001049 lba |= ((u64)cdb[2]) << 8;
1050 lba |= ((u64)cdb[3]);
Albert Lee3aef5232005-10-04 08:47:43 -04001051
Tejun Heo542b1442006-12-17 10:45:08 +09001052 len |= ((u32)cdb[4]);
Albert Lee3aef5232005-10-04 08:47:43 -04001053
1054 *plba = lba;
1055 *plen = len;
1056}
1057
1058/**
1059 * scsi_10_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001060 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001061 *
1062 * Calculate LBA and transfer length for 10-byte commands.
1063 *
1064 * RETURNS:
1065 * @plba: the LBA
1066 * @plen: the transfer length
1067 */
Tejun Heo542b1442006-12-17 10:45:08 +09001068static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001069{
1070 u64 lba = 0;
1071 u32 len = 0;
1072
1073 VPRINTK("ten-byte command\n");
1074
Tejun Heo542b1442006-12-17 10:45:08 +09001075 lba |= ((u64)cdb[2]) << 24;
1076 lba |= ((u64)cdb[3]) << 16;
1077 lba |= ((u64)cdb[4]) << 8;
1078 lba |= ((u64)cdb[5]);
Albert Lee3aef5232005-10-04 08:47:43 -04001079
Tejun Heo542b1442006-12-17 10:45:08 +09001080 len |= ((u32)cdb[7]) << 8;
1081 len |= ((u32)cdb[8]);
Albert Lee3aef5232005-10-04 08:47:43 -04001082
1083 *plba = lba;
1084 *plen = len;
1085}
1086
1087/**
1088 * scsi_16_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001089 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001090 *
1091 * Calculate LBA and transfer length for 16-byte commands.
1092 *
1093 * RETURNS:
1094 * @plba: the LBA
1095 * @plen: the transfer length
1096 */
Tejun Heo542b1442006-12-17 10:45:08 +09001097static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001098{
1099 u64 lba = 0;
1100 u32 len = 0;
1101
1102 VPRINTK("sixteen-byte command\n");
1103
Tejun Heo542b1442006-12-17 10:45:08 +09001104 lba |= ((u64)cdb[2]) << 56;
1105 lba |= ((u64)cdb[3]) << 48;
1106 lba |= ((u64)cdb[4]) << 40;
1107 lba |= ((u64)cdb[5]) << 32;
1108 lba |= ((u64)cdb[6]) << 24;
1109 lba |= ((u64)cdb[7]) << 16;
1110 lba |= ((u64)cdb[8]) << 8;
1111 lba |= ((u64)cdb[9]);
Albert Lee3aef5232005-10-04 08:47:43 -04001112
Tejun Heo542b1442006-12-17 10:45:08 +09001113 len |= ((u32)cdb[10]) << 24;
1114 len |= ((u32)cdb[11]) << 16;
1115 len |= ((u32)cdb[12]) << 8;
1116 len |= ((u32)cdb[13]);
Albert Lee3aef5232005-10-04 08:47:43 -04001117
1118 *plba = lba;
1119 *plen = len;
1120}
1121
1122/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1124 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 *
1126 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1127 *
1128 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001129 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 *
1131 * RETURNS:
1132 * Zero on success, non-zero on error.
1133 */
Tejun Heoad706992006-12-17 10:45:57 +09001134static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Tejun Heo542b1442006-12-17 10:45:08 +09001136 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 struct ata_taskfile *tf = &qc->tf;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001138 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 u64 dev_sectors = qc->dev->n_sectors;
Tejun Heoad706992006-12-17 10:45:57 +09001140 const u8 *cdb = scmd->cmnd;
Albert Lee3aef5232005-10-04 08:47:43 -04001141 u64 block;
1142 u32 n_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
1144 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1145 tf->protocol = ATA_PROT_NODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146
Tejun Heo542b1442006-12-17 10:45:08 +09001147 if (cdb[0] == VERIFY)
1148 scsi_10_lba_len(cdb, &block, &n_block);
1149 else if (cdb[0] == VERIFY_16)
1150 scsi_16_lba_len(cdb, &block, &n_block);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 else
Douglas Gilbertae006512005-10-09 09:09:35 -04001152 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153
Albert Lee8bf62ec2005-05-12 15:29:42 -04001154 if (!n_block)
Douglas Gilbertae006512005-10-09 09:09:35 -04001155 goto nothing_to_do;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001156 if (block >= dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001157 goto out_of_range;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001158 if ((block + n_block) > dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001159 goto out_of_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Albert Lee07506692005-10-12 15:04:18 +08001161 if (dev->flags & ATA_DFLAG_LBA) {
1162 tf->flags |= ATA_TFLAG_LBA;
1163
Albert Leec6a33e22005-10-12 15:12:26 +08001164 if (lba_28_ok(block, n_block)) {
1165 /* use LBA28 */
1166 tf->command = ATA_CMD_VERIFY;
1167 tf->device |= (block >> 24) & 0xf;
1168 } else if (lba_48_ok(block, n_block)) {
1169 if (!(dev->flags & ATA_DFLAG_LBA48))
1170 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001171
1172 /* use LBA48 */
1173 tf->flags |= ATA_TFLAG_LBA48;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001174 tf->command = ATA_CMD_VERIFY_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Albert Lee8bf62ec2005-05-12 15:29:42 -04001176 tf->hob_nsect = (n_block >> 8) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Albert Lee8bf62ec2005-05-12 15:29:42 -04001178 tf->hob_lbah = (block >> 40) & 0xff;
1179 tf->hob_lbam = (block >> 32) & 0xff;
1180 tf->hob_lbal = (block >> 24) & 0xff;
Albert Leec6a33e22005-10-12 15:12:26 +08001181 } else
1182 /* request too large even for LBA48 */
1183 goto out_of_range;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001184
1185 tf->nsect = n_block & 0xff;
1186
1187 tf->lbah = (block >> 16) & 0xff;
1188 tf->lbam = (block >> 8) & 0xff;
1189 tf->lbal = block & 0xff;
1190
1191 tf->device |= ATA_LBA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 } else {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001193 /* CHS */
1194 u32 sect, head, cyl, track;
1195
Albert Leec6a33e22005-10-12 15:12:26 +08001196 if (!lba_28_ok(block, n_block))
1197 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001198
Albert Lee8bf62ec2005-05-12 15:29:42 -04001199 /* Convert LBA to CHS */
1200 track = (u32)block / dev->sectors;
1201 cyl = track / dev->heads;
1202 head = track % dev->heads;
1203 sect = (u32)block % dev->sectors + 1;
1204
Albert Leec187c4b2005-10-04 08:46:51 -04001205 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1206 (u32)block, track, cyl, head, sect);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001207
1208 /* Check whether the converted CHS can fit.
1209 Cylinder: 0-65535
Albert Lee8bf62ec2005-05-12 15:29:42 -04001210 Head: 0-15
1211 Sector: 1-255*/
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001212 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
Douglas Gilbertae006512005-10-09 09:09:35 -04001213 goto out_of_range;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001214
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 tf->command = ATA_CMD_VERIFY;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001216 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1217 tf->lbal = sect;
1218 tf->lbam = cyl;
1219 tf->lbah = cyl >> 8;
1220 tf->device |= head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 }
1222
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001224
1225invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +09001226 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001227 /* "Invalid field in cbd" */
1228 return 1;
1229
1230out_of_range:
Tejun Heo542b1442006-12-17 10:45:08 +09001231 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001232 /* "Logical Block Address out of range" */
1233 return 1;
1234
1235nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001236 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001237 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238}
1239
1240/**
1241 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1242 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 *
1244 * Converts any of six SCSI read/write commands into the
1245 * ATA counterpart, including starting sector (LBA),
1246 * sector count, and taking into account the device's LBA48
1247 * support.
1248 *
1249 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1250 * %WRITE_16 are currently supported.
1251 *
1252 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001253 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 *
1255 * RETURNS:
1256 * Zero on success, non-zero on error.
1257 */
Tejun Heoad706992006-12-17 10:45:57 +09001258static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259{
Tejun Heo542b1442006-12-17 10:45:08 +09001260 struct scsi_cmnd *scmd = qc->scsicmd;
Tejun Heoad706992006-12-17 10:45:57 +09001261 const u8 *cdb = scmd->cmnd;
Tejun Heobd056d72006-11-14 22:47:10 +09001262 unsigned int tf_flags = 0;
Albert Lee3aef5232005-10-04 08:47:43 -04001263 u64 block;
1264 u32 n_block;
Tejun Heobd056d72006-11-14 22:47:10 +09001265 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Tejun Heo542b1442006-12-17 10:45:08 +09001267 if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
Tejun Heobd056d72006-11-14 22:47:10 +09001268 tf_flags |= ATA_TFLAG_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001270 /* Calculate the SCSI LBA, transfer length and FUA. */
Tejun Heo542b1442006-12-17 10:45:08 +09001271 switch (cdb[0]) {
Albert Lee3aef5232005-10-04 08:47:43 -04001272 case READ_10:
1273 case WRITE_10:
Tejun Heo542b1442006-12-17 10:45:08 +09001274 scsi_10_lba_len(cdb, &block, &n_block);
1275 if (unlikely(cdb[1] & (1 << 3)))
Tejun Heobd056d72006-11-14 22:47:10 +09001276 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001277 break;
1278 case READ_6:
1279 case WRITE_6:
Tejun Heo542b1442006-12-17 10:45:08 +09001280 scsi_6_lba_len(cdb, &block, &n_block);
Albert Leec187c4b2005-10-04 08:46:51 -04001281
1282 /* for 6-byte r/w commands, transfer length 0
1283 * means 256 blocks of data, not 0 block.
1284 */
Jeff Garzik76b2bf92005-08-29 19:24:43 -04001285 if (!n_block)
1286 n_block = 256;
Albert Lee3aef5232005-10-04 08:47:43 -04001287 break;
1288 case READ_16:
1289 case WRITE_16:
Tejun Heo542b1442006-12-17 10:45:08 +09001290 scsi_16_lba_len(cdb, &block, &n_block);
1291 if (unlikely(cdb[1] & (1 << 3)))
Tejun Heobd056d72006-11-14 22:47:10 +09001292 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001293 break;
1294 default:
Albert Lee8bf62ec2005-05-12 15:29:42 -04001295 DPRINTK("no-byte command\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04001296 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 }
1298
Albert Lee8bf62ec2005-05-12 15:29:42 -04001299 /* Check and compose ATA command */
1300 if (!n_block)
Albert Leec187c4b2005-10-04 08:46:51 -04001301 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1302 * length 0 means transfer 0 block of data.
1303 * However, for ATA R/W commands, sector count 0 means
1304 * 256 or 65536 sectors, not 0 sectors as in SCSI.
Alan Coxf51750d2005-11-07 17:06:33 +00001305 *
1306 * WARNING: one or two older ATA drives treat 0 as 0...
Albert Leec187c4b2005-10-04 08:46:51 -04001307 */
Douglas Gilbertae006512005-10-09 09:09:35 -04001308 goto nothing_to_do;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001309
Tejun Heobd056d72006-11-14 22:47:10 +09001310 qc->flags |= ATA_QCFLAG_IO;
1311 qc->nsect = n_block;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001312
Tejun Heobd056d72006-11-14 22:47:10 +09001313 rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
1314 qc->tag);
1315 if (likely(rc == 0))
1316 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001317
Tejun Heobd056d72006-11-14 22:47:10 +09001318 if (rc == -ERANGE)
1319 goto out_of_range;
1320 /* treat all other errors as -EINVAL, fall through */
Douglas Gilbertae006512005-10-09 09:09:35 -04001321invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +09001322 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001323 /* "Invalid field in cbd" */
1324 return 1;
1325
1326out_of_range:
Tejun Heo542b1442006-12-17 10:45:08 +09001327 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001328 /* "Logical Block Address out of range" */
1329 return 1;
1330
1331nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001332 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001333 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334}
1335
Tejun Heo77853bf2006-01-23 13:09:36 +09001336static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337{
Tejun Heoc31f5712006-11-22 12:39:43 +09001338 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika7dac442005-10-30 04:44:42 -05001340 u8 *cdb = cmd->cmnd;
Albert Leea22e2eb2005-12-05 15:38:02 +08001341 int need_sense = (qc->err_mask != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342
zhao, forrest3057ac32006-06-12 12:01:34 +08001343 /* We snoop the SET_FEATURES - Write Cache ON/OFF command, and
1344 * schedule EH_REVALIDATE operation to update the IDENTIFY DEVICE
1345 * cache
1346 */
Tejun Heoc31f5712006-11-22 12:39:43 +09001347 if (ap->ops->error_handler &&
1348 !need_sense && (qc->tf.command == ATA_CMD_SET_FEATURES) &&
zhao, forrest3057ac32006-06-12 12:01:34 +08001349 ((qc->tf.feature == SETFEATURES_WC_ON) ||
1350 (qc->tf.feature == SETFEATURES_WC_OFF))) {
Tejun Heoc31f5712006-11-22 12:39:43 +09001351 ap->eh_info.action |= ATA_EH_REVALIDATE;
1352 ata_port_schedule_eh(ap);
zhao, forrest3057ac32006-06-12 12:01:34 +08001353 }
1354
Jeff Garzikb0955182005-05-12 15:45:22 -04001355 /* For ATA pass thru (SAT) commands, generate a sense block if
1356 * user mandated it or if there's an error. Note that if we
1357 * generate because the user forced us to, a check condition
1358 * is generated and the ATA register values are returned
1359 * whether the command completed successfully or not. If there
1360 * was no error, SK, ASC and ASCQ will all be zero.
1361 */
Jeff Garzika7dac442005-10-30 04:44:42 -05001362 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
1363 ((cdb[2] & 0x20) || need_sense)) {
Tejun Heo750426a2006-11-14 22:37:35 +09001364 ata_gen_passthru_sense(qc);
Jeff Garzikb0955182005-05-12 15:45:22 -04001365 } else {
1366 if (!need_sense) {
1367 cmd->result = SAM_STAT_GOOD;
1368 } else {
1369 /* TODO: decide which descriptor format to use
1370 * for 48b LBA devices and call that here
1371 * instead of the fixed desc, which is only
1372 * good for smaller LBA (and maybe CHS?)
1373 * devices.
1374 */
Tejun Heo750426a2006-11-14 22:37:35 +09001375 ata_gen_ata_sense(qc);
Jeff Garzikb0955182005-05-12 15:45:22 -04001376 }
1377 }
1378
Tejun Heoc31f5712006-11-22 12:39:43 +09001379 if (need_sense && !ap->ops->error_handler)
1380 ata_dump_status(ap->id, &qc->result_tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381
1382 qc->scsidone(cmd);
1383
Tejun Heo77853bf2006-01-23 13:09:36 +09001384 ata_qc_free(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385}
1386
1387/**
Tejun Heo3dc1d882006-05-15 21:03:45 +09001388 * ata_scmd_need_defer - Check whether we need to defer scmd
1389 * @dev: ATA device to which the command is addressed
1390 * @is_io: Is the command IO (and thus possibly NCQ)?
1391 *
1392 * NCQ and non-NCQ commands cannot run together. As upper layer
1393 * only knows the queue depth, we are responsible for maintaining
1394 * exclusion. This function checks whether a new command can be
1395 * issued to @dev.
1396 *
1397 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001398 * spin_lock_irqsave(host lock)
Tejun Heo3dc1d882006-05-15 21:03:45 +09001399 *
1400 * RETURNS:
1401 * 1 if deferring is needed, 0 otherwise.
1402 */
1403static int ata_scmd_need_defer(struct ata_device *dev, int is_io)
1404{
1405 struct ata_port *ap = dev->ap;
1406
1407 if (!(dev->flags & ATA_DFLAG_NCQ))
1408 return 0;
1409
1410 if (is_io) {
1411 if (!ata_tag_valid(ap->active_tag))
1412 return 0;
1413 } else {
1414 if (!ata_tag_valid(ap->active_tag) && !ap->sactive)
1415 return 0;
1416 }
1417 return 1;
1418}
1419
1420/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 * ata_scsi_translate - Translate then issue SCSI command to ATA device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422 * @dev: ATA device to which the command is addressed
1423 * @cmd: SCSI command to execute
1424 * @done: SCSI command completion function
1425 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1426 *
1427 * Our ->queuecommand() function has decided that the SCSI
1428 * command issued can be directly translated into an ATA
1429 * command, rather than handled internally.
1430 *
1431 * This function sets up an ata_queued_cmd structure for the
1432 * SCSI command, and sends that ata_queued_cmd to the hardware.
1433 *
Douglas Gilbertae006512005-10-09 09:09:35 -04001434 * The xlat_func argument (actor) returns 0 if ready to execute
1435 * ATA command, else 1 to finish translation. If 1 is returned
1436 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1437 * to be set reflecting an error condition or clean (early)
1438 * termination.
1439 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001440 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001441 * spin_lock_irqsave(host lock)
Tejun Heo2115ea92006-05-15 21:03:39 +09001442 *
1443 * RETURNS:
1444 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
1445 * needs to be deferred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 */
Tejun Heo2115ea92006-05-15 21:03:39 +09001447static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
1448 void (*done)(struct scsi_cmnd *),
1449 ata_xlat_func_t xlat_func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450{
1451 struct ata_queued_cmd *qc;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001452 int is_io = xlat_func == ata_scsi_rw_xlat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001453
1454 VPRINTK("ENTER\n");
1455
Tejun Heo3dc1d882006-05-15 21:03:45 +09001456 if (unlikely(ata_scmd_need_defer(dev, is_io)))
1457 goto defer;
1458
Tejun Heo3373efd2006-05-15 20:57:53 +09001459 qc = ata_scsi_qc_new(dev, cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001460 if (!qc)
Douglas Gilbertae006512005-10-09 09:09:35 -04001461 goto err_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463 /* data is present; dma-map it */
be7db052005-04-17 15:26:13 -05001464 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1465 cmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001466 if (unlikely(cmd->request_bufflen < 1)) {
Tejun Heof15a1da2006-05-15 20:57:56 +09001467 ata_dev_printk(dev, KERN_WARNING,
1468 "WARNING: zero len r/w req\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04001469 goto err_did;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001470 }
1471
1472 if (cmd->use_sg)
1473 ata_sg_init(qc, cmd->request_buffer, cmd->use_sg);
1474 else
1475 ata_sg_init_one(qc, cmd->request_buffer,
1476 cmd->request_bufflen);
1477
1478 qc->dma_dir = cmd->sc_data_direction;
1479 }
1480
1481 qc->complete_fn = ata_scsi_qc_complete;
1482
Tejun Heoad706992006-12-17 10:45:57 +09001483 if (xlat_func(qc))
Douglas Gilbertae006512005-10-09 09:09:35 -04001484 goto early_finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001485
1486 /* select device, send command to hardware */
Tejun Heo8e0e6942006-03-31 20:41:11 +09001487 ata_qc_issue(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 VPRINTK("EXIT\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001490 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001491
Douglas Gilbertae006512005-10-09 09:09:35 -04001492early_finish:
1493 ata_qc_free(qc);
1494 done(cmd);
1495 DPRINTK("EXIT - early finish (good or error)\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001496 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001497
1498err_did:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499 ata_qc_free(qc);
Douglas Gilbertae006512005-10-09 09:09:35 -04001500 cmd->result = (DID_ERROR << 16);
1501 done(cmd);
Darrick J. Wong253b92e2006-11-14 09:55:41 -05001502err_mem:
Douglas Gilbertae006512005-10-09 09:09:35 -04001503 DPRINTK("EXIT - internal\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001504 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001505
1506defer:
1507 DPRINTK("EXIT - defer\n");
1508 return SCSI_MLQUEUE_DEVICE_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509}
1510
1511/**
1512 * ata_scsi_rbuf_get - Map response buffer.
1513 * @cmd: SCSI command containing buffer to be mapped.
1514 * @buf_out: Pointer to mapped area.
1515 *
1516 * Maps buffer contained within SCSI command @cmd.
1517 *
1518 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001519 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520 *
1521 * RETURNS:
1522 * Length of response buffer.
1523 */
1524
1525static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
1526{
1527 u8 *buf;
1528 unsigned int buflen;
1529
1530 if (cmd->use_sg) {
1531 struct scatterlist *sg;
1532
1533 sg = (struct scatterlist *) cmd->request_buffer;
Jeff Garzikda02d2a2006-12-11 11:05:53 -05001534 buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 buflen = sg->length;
1536 } else {
1537 buf = cmd->request_buffer;
1538 buflen = cmd->request_bufflen;
1539 }
1540
1541 *buf_out = buf;
1542 return buflen;
1543}
1544
1545/**
1546 * ata_scsi_rbuf_put - Unmap response buffer.
1547 * @cmd: SCSI command containing buffer to be unmapped.
1548 * @buf: buffer to unmap
1549 *
1550 * Unmaps response buffer contained within @cmd.
1551 *
1552 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001553 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001554 */
1555
1556static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
1557{
1558 if (cmd->use_sg) {
1559 struct scatterlist *sg;
1560
1561 sg = (struct scatterlist *) cmd->request_buffer;
Jeff Garzikda02d2a2006-12-11 11:05:53 -05001562 kunmap_atomic(buf - sg->offset, KM_IRQ0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
1564}
1565
1566/**
1567 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1568 * @args: device IDENTIFY data / SCSI command of interest.
1569 * @actor: Callback hook for desired SCSI command simulator
1570 *
1571 * Takes care of the hard work of simulating a SCSI command...
1572 * Mapping the response buffer, calling the command's handler,
1573 * and handling the handler's return value. This return value
1574 * indicates whether the handler wishes the SCSI command to be
Douglas Gilbertae006512005-10-09 09:09:35 -04001575 * completed successfully (0), or not (in which case cmd->result
1576 * and sense buffer are assumed to be set).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 *
1578 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001579 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 */
1581
1582void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
1583 unsigned int (*actor) (struct ata_scsi_args *args,
1584 u8 *rbuf, unsigned int buflen))
1585{
1586 u8 *rbuf;
1587 unsigned int buflen, rc;
1588 struct scsi_cmnd *cmd = args->cmd;
1589
1590 buflen = ata_scsi_rbuf_get(cmd, &rbuf);
1591 memset(rbuf, 0, buflen);
1592 rc = actor(args, rbuf, buflen);
1593 ata_scsi_rbuf_put(cmd, rbuf);
1594
Douglas Gilbertae006512005-10-09 09:09:35 -04001595 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 cmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001597 args->done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598}
1599
1600/**
Tejun Heo6a362612006-11-20 11:15:47 +09001601 * ATA_SCSI_RBUF_SET - helper to set values in SCSI response buffer
1602 * @idx: byte index into SCSI response buffer
1603 * @val: value to set
1604 *
1605 * To be used by SCSI command simulator functions. This macros
1606 * expects two local variables, u8 *rbuf and unsigned int buflen,
1607 * are in scope.
1608 *
1609 * LOCKING:
1610 * None.
1611 */
1612#define ATA_SCSI_RBUF_SET(idx, val) do { \
1613 if ((idx) < buflen) rbuf[(idx)] = (u8)(val); \
1614 } while (0)
1615
1616/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617 * ata_scsiop_inq_std - Simulate INQUIRY command
1618 * @args: device IDENTIFY data / SCSI command of interest.
1619 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1620 * @buflen: Response buffer length.
1621 *
1622 * Returns standard device identification data associated
Jeff Garzikb142eb62006-03-21 20:37:47 -05001623 * with non-VPD INQUIRY command output.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 *
1625 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001626 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 */
1628
1629unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
1630 unsigned int buflen)
1631{
1632 u8 hdr[] = {
1633 TYPE_DISK,
1634 0,
1635 0x5, /* claim SPC-3 version compatibility */
1636 2,
1637 95 - 4
1638 };
1639
1640 /* set scsi removeable (RMB) bit per ata bit */
1641 if (ata_id_removeable(args->id))
1642 hdr[1] |= (1 << 7);
1643
1644 VPRINTK("ENTER\n");
1645
1646 memcpy(rbuf, hdr, sizeof(hdr));
1647
1648 if (buflen > 35) {
1649 memcpy(&rbuf[8], "ATA ", 8);
Tejun Heo6a62a042006-02-13 10:02:46 +09001650 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16);
1651 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (rbuf[32] == 0 || rbuf[32] == ' ')
1653 memcpy(&rbuf[32], "n/a ", 4);
1654 }
1655
1656 if (buflen > 63) {
1657 const u8 versions[] = {
1658 0x60, /* SAM-3 (no version claimed) */
1659
1660 0x03,
1661 0x20, /* SBC-2 (no version claimed) */
1662
1663 0x02,
1664 0x60 /* SPC-3 (no version claimed) */
1665 };
1666
1667 memcpy(rbuf + 59, versions, sizeof(versions));
1668 }
1669
1670 return 0;
1671}
1672
1673/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05001674 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 * @args: device IDENTIFY data / SCSI command of interest.
1676 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1677 * @buflen: Response buffer length.
1678 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05001679 * Returns list of inquiry VPD pages available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 *
1681 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001682 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 */
1684
1685unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
1686 unsigned int buflen)
1687{
1688 const u8 pages[] = {
1689 0x00, /* page 0x00, this page */
1690 0x80, /* page 0x80, unit serial no page */
1691 0x83 /* page 0x83, device ident page */
1692 };
Jeff Garzikb142eb62006-03-21 20:37:47 -05001693 rbuf[3] = sizeof(pages); /* number of supported VPD pages */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
1695 if (buflen > 6)
1696 memcpy(rbuf + 4, pages, sizeof(pages));
1697
1698 return 0;
1699}
1700
1701/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05001702 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703 * @args: device IDENTIFY data / SCSI command of interest.
1704 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1705 * @buflen: Response buffer length.
1706 *
1707 * Returns ATA device serial number.
1708 *
1709 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001710 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711 */
1712
1713unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
1714 unsigned int buflen)
1715{
1716 const u8 hdr[] = {
1717 0,
1718 0x80, /* this page code */
1719 0,
1720 ATA_SERNO_LEN, /* page len */
1721 };
1722 memcpy(rbuf, hdr, sizeof(hdr));
1723
1724 if (buflen > (ATA_SERNO_LEN + 4 - 1))
Tejun Heo6a62a042006-02-13 10:02:46 +09001725 ata_id_string(args->id, (unsigned char *) &rbuf[4],
1726 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 return 0;
1729}
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05001732 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 * @args: device IDENTIFY data / SCSI command of interest.
1734 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1735 * @buflen: Response buffer length.
1736 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05001737 * Yields two logical unit device identification designators:
1738 * - vendor specific ASCII containing the ATA serial number
1739 * - SAT defined "t10 vendor id based" containing ASCII vendor
1740 * name ("ATA "), model and serial numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 *
1742 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001743 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744 */
1745
1746unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
1747 unsigned int buflen)
1748{
Jeff Garzikb142eb62006-03-21 20:37:47 -05001749 int num;
1750 const int sat_model_serial_desc_len = 68;
1751 const int ata_model_byte_len = 40;
1752
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 rbuf[1] = 0x83; /* this page code */
Jeff Garzikb142eb62006-03-21 20:37:47 -05001754 num = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Jeff Garzikb142eb62006-03-21 20:37:47 -05001756 if (buflen > (ATA_SERNO_LEN + num + 3)) {
1757 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001758 rbuf[num + 0] = 2;
Jeff Garzikb142eb62006-03-21 20:37:47 -05001759 rbuf[num + 3] = ATA_SERNO_LEN;
1760 num += 4;
1761 ata_id_string(args->id, (unsigned char *) rbuf + num,
1762 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1763 num += ATA_SERNO_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764 }
Jeff Garzikb142eb62006-03-21 20:37:47 -05001765 if (buflen > (sat_model_serial_desc_len + num + 3)) {
1766 /* SAT defined lu model and serial numbers descriptor */
1767 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001768 rbuf[num + 0] = 2;
1769 rbuf[num + 1] = 1;
Jeff Garzikb142eb62006-03-21 20:37:47 -05001770 rbuf[num + 3] = sat_model_serial_desc_len;
1771 num += 4;
1772 memcpy(rbuf + num, "ATA ", 8);
1773 num += 8;
1774 ata_id_string(args->id, (unsigned char *) rbuf + num,
1775 ATA_ID_PROD_OFS, ata_model_byte_len);
1776 num += ata_model_byte_len;
1777 ata_id_string(args->id, (unsigned char *) rbuf + num,
1778 ATA_ID_SERNO_OFS, ATA_SERNO_LEN);
1779 num += ATA_SERNO_LEN;
1780 }
1781 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 return 0;
1783}
1784
1785/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04001786 * ata_scsiop_noop - Command handler that simply returns success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787 * @args: device IDENTIFY data / SCSI command of interest.
1788 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1789 * @buflen: Response buffer length.
1790 *
1791 * No operation. Simply returns success to caller, to indicate
1792 * that the caller should successfully complete this SCSI command.
1793 *
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_noop(struct ata_scsi_args *args, u8 *rbuf,
1799 unsigned int buflen)
1800{
1801 VPRINTK("ENTER\n");
1802 return 0;
1803}
1804
1805/**
1806 * ata_msense_push - Push data onto MODE SENSE data output buffer
1807 * @ptr_io: (input/output) Location to store more output data
1808 * @last: End of output data buffer
1809 * @buf: Pointer to BLOB being added to output buffer
1810 * @buflen: Length of BLOB
1811 *
1812 * Store MODE SENSE data on an output buffer.
1813 *
1814 * LOCKING:
1815 * None.
1816 */
1817
1818static void ata_msense_push(u8 **ptr_io, const u8 *last,
1819 const u8 *buf, unsigned int buflen)
1820{
1821 u8 *ptr = *ptr_io;
1822
1823 if ((ptr + buflen - 1) > last)
1824 return;
1825
1826 memcpy(ptr, buf, buflen);
1827
1828 ptr += buflen;
1829
1830 *ptr_io = ptr;
1831}
1832
1833/**
1834 * ata_msense_caching - Simulate MODE SENSE caching info page
1835 * @id: device IDENTIFY data
1836 * @ptr_io: (input/output) Location to store more output data
1837 * @last: End of output data buffer
1838 *
1839 * Generate a caching info page, which conditionally indicates
1840 * write caching to the SCSI layer, depending on device
1841 * capabilities.
1842 *
1843 * LOCKING:
1844 * None.
1845 */
1846
1847static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
1848 const u8 *last)
1849{
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001850 u8 page[CACHE_MPAGE_LEN];
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001852 memcpy(page, def_cache_mpage, sizeof(page));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (ata_id_wcache_enabled(id))
1854 page[2] |= (1 << 2); /* write cache enable */
1855 if (!ata_id_rahead_enabled(id))
1856 page[12] |= (1 << 5); /* disable read ahead */
1857
1858 ata_msense_push(ptr_io, last, page, sizeof(page));
1859 return sizeof(page);
1860}
1861
1862/**
1863 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
1864 * @dev: Device associated with this MODE SENSE command
1865 * @ptr_io: (input/output) Location to store more output data
1866 * @last: End of output data buffer
1867 *
1868 * Generate a generic MODE SENSE control mode page.
1869 *
1870 * LOCKING:
1871 * None.
1872 */
1873
1874static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
1875{
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001876 ata_msense_push(ptr_io, last, def_control_mpage,
1877 sizeof(def_control_mpage));
1878 return sizeof(def_control_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879}
1880
1881/**
1882 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
1883 * @dev: Device associated with this MODE SENSE command
1884 * @ptr_io: (input/output) Location to store more output data
1885 * @last: End of output data buffer
1886 *
1887 * Generate a generic MODE SENSE r/w error recovery page.
1888 *
1889 * LOCKING:
1890 * None.
1891 */
1892
1893static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
1894{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001896 ata_msense_push(ptr_io, last, def_rw_recovery_mpage,
1897 sizeof(def_rw_recovery_mpage));
1898 return sizeof(def_rw_recovery_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899}
1900
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001901/*
1902 * We can turn this into a real blacklist if it's needed, for now just
1903 * blacklist any Maxtor BANC1G10 revision firmware
1904 */
1905static int ata_dev_supports_fua(u16 *id)
1906{
1907 unsigned char model[41], fw[9];
1908
Jeff Garzikc3c013a2006-02-27 22:31:19 -05001909 if (!libata_fua)
1910 return 0;
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001911 if (!ata_id_has_fua(id))
1912 return 0;
1913
Tejun Heo6a62a042006-02-13 10:02:46 +09001914 ata_id_c_string(id, model, ATA_ID_PROD_OFS, sizeof(model));
1915 ata_id_c_string(id, fw, ATA_ID_FW_REV_OFS, sizeof(fw));
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001916
Tejun Heo2e026712006-02-12 22:47:04 +09001917 if (strcmp(model, "Maxtor"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001918 return 1;
Tejun Heo2e026712006-02-12 22:47:04 +09001919 if (strcmp(fw, "BANC1G10"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01001920 return 1;
1921
1922 return 0; /* blacklisted */
1923}
1924
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925/**
1926 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
1927 * @args: device IDENTIFY data / SCSI command of interest.
1928 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1929 * @buflen: Response buffer length.
1930 *
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001931 * Simulate MODE SENSE commands. Assume this is invoked for direct
1932 * access devices (e.g. disks) only. There should be no block
1933 * descriptor for other device types.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 *
1935 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001936 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 */
1938
1939unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1940 unsigned int buflen)
1941{
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001942 struct ata_device *dev = args->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 u8 *scsicmd = args->cmd->cmnd, *p, *last;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001944 const u8 sat_blk_desc[] = {
1945 0, 0, 0, 0, /* number of blocks: sat unspecified */
1946 0,
1947 0, 0x2, 0x0 /* block length: 512 bytes */
1948 };
1949 u8 pg, spg;
1950 unsigned int ebd, page_control, six_byte, output_len, alloc_len, minlen;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001951 u8 dpofua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952
1953 VPRINTK("ENTER\n");
1954
1955 six_byte = (scsicmd[0] == MODE_SENSE);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001956 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
1957 /*
1958 * LLBA bit in msense(10) ignored (compliant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001960
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961 page_control = scsicmd[2] >> 6;
Douglas Gilbertae006512005-10-09 09:09:35 -04001962 switch (page_control) {
1963 case 0: /* current */
1964 break; /* supported */
1965 case 3: /* saved */
1966 goto saving_not_supp;
1967 case 1: /* changeable */
1968 case 2: /* defaults */
1969 default:
1970 goto invalid_fld;
1971 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001973 if (six_byte) {
1974 output_len = 4 + (ebd ? 8 : 0);
1975 alloc_len = scsicmd[4];
1976 } else {
1977 output_len = 8 + (ebd ? 8 : 0);
1978 alloc_len = (scsicmd[7] << 8) + scsicmd[8];
1979 }
1980 minlen = (alloc_len < buflen) ? alloc_len : buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 p = rbuf + output_len;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001983 last = rbuf + minlen - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001985 pg = scsicmd[2] & 0x3f;
1986 spg = scsicmd[3];
1987 /*
1988 * No mode subpages supported (yet) but asking for _all_
1989 * subpages may be valid
1990 */
1991 if (spg && (spg != ALL_SUB_MPAGES))
1992 goto invalid_fld;
1993
1994 switch(pg) {
1995 case RW_RECOVERY_MPAGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 output_len += ata_msense_rw_recovery(&p, last);
1997 break;
1998
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04001999 case CACHE_MPAGE:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 output_len += ata_msense_caching(args->id, &p, last);
2001 break;
2002
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002003 case CONTROL_MPAGE: {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 output_len += ata_msense_ctl_mode(&p, last);
2005 break;
2006 }
2007
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002008 case ALL_MPAGES:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 output_len += ata_msense_rw_recovery(&p, last);
2010 output_len += ata_msense_caching(args->id, &p, last);
2011 output_len += ata_msense_ctl_mode(&p, last);
2012 break;
2013
2014 default: /* invalid page code */
Douglas Gilbertae006512005-10-09 09:09:35 -04002015 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016 }
2017
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002018 if (minlen < 1)
2019 return 0;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002020
2021 dpofua = 0;
Alan Coxf79d4092006-05-22 16:55:11 +01002022 if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002023 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
2024 dpofua = 1 << 4;
2025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 if (six_byte) {
2027 output_len--;
2028 rbuf[0] = output_len;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002029 if (minlen > 2)
2030 rbuf[2] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002031 if (ebd) {
2032 if (minlen > 3)
2033 rbuf[3] = sizeof(sat_blk_desc);
2034 if (minlen > 11)
2035 memcpy(rbuf + 4, sat_blk_desc,
2036 sizeof(sat_blk_desc));
2037 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 } else {
2039 output_len -= 2;
2040 rbuf[0] = output_len >> 8;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002041 if (minlen > 1)
2042 rbuf[1] = output_len;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002043 if (minlen > 3)
2044 rbuf[3] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002045 if (ebd) {
2046 if (minlen > 7)
2047 rbuf[7] = sizeof(sat_blk_desc);
2048 if (minlen > 15)
2049 memcpy(rbuf + 8, sat_blk_desc,
2050 sizeof(sat_blk_desc));
2051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04002054
2055invalid_fld:
2056 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
2057 /* "Invalid field in cbd" */
2058 return 1;
2059
2060saving_not_supp:
2061 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
2062 /* "Saving parameters not supported" */
2063 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064}
2065
2066/**
2067 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2068 * @args: device IDENTIFY data / SCSI command of interest.
2069 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2070 * @buflen: Response buffer length.
2071 *
2072 * Simulate READ CAPACITY commands.
2073 *
2074 * LOCKING:
Tejun Heo6a362612006-11-20 11:15:47 +09002075 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002077unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
2078 unsigned int buflen)
2079{
Tejun Heo6a362612006-11-20 11:15:47 +09002080 u64 last_lba = args->dev->n_sectors - 1; /* LBA of the last block */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081
2082 VPRINTK("ENTER\n");
2083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 if (args->cmd->cmnd[0] == READ_CAPACITY) {
Tejun Heo6a362612006-11-20 11:15:47 +09002085 if (last_lba >= 0xffffffffULL)
2086 last_lba = 0xffffffff;
Philip Pokorny0c144d02005-05-28 01:24:47 -07002087
Linus Torvalds1da177e2005-04-16 15:20:36 -07002088 /* sector count, 32-bit */
Tejun Heo6a362612006-11-20 11:15:47 +09002089 ATA_SCSI_RBUF_SET(0, last_lba >> (8 * 3));
2090 ATA_SCSI_RBUF_SET(1, last_lba >> (8 * 2));
2091 ATA_SCSI_RBUF_SET(2, last_lba >> (8 * 1));
2092 ATA_SCSI_RBUF_SET(3, last_lba);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093
2094 /* sector size */
Tejun Heo6a362612006-11-20 11:15:47 +09002095 ATA_SCSI_RBUF_SET(6, ATA_SECT_SIZE >> 8);
2096 ATA_SCSI_RBUF_SET(7, ATA_SECT_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 } else {
2098 /* sector count, 64-bit */
Tejun Heo6a362612006-11-20 11:15:47 +09002099 ATA_SCSI_RBUF_SET(0, last_lba >> (8 * 7));
2100 ATA_SCSI_RBUF_SET(1, last_lba >> (8 * 6));
2101 ATA_SCSI_RBUF_SET(2, last_lba >> (8 * 5));
2102 ATA_SCSI_RBUF_SET(3, last_lba >> (8 * 4));
2103 ATA_SCSI_RBUF_SET(4, last_lba >> (8 * 3));
2104 ATA_SCSI_RBUF_SET(5, last_lba >> (8 * 2));
2105 ATA_SCSI_RBUF_SET(6, last_lba >> (8 * 1));
2106 ATA_SCSI_RBUF_SET(7, last_lba);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107
2108 /* sector size */
Tejun Heo6a362612006-11-20 11:15:47 +09002109 ATA_SCSI_RBUF_SET(10, ATA_SECT_SIZE >> 8);
2110 ATA_SCSI_RBUF_SET(11, ATA_SECT_SIZE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002111 }
2112
2113 return 0;
2114}
2115
2116/**
2117 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2118 * @args: device IDENTIFY data / SCSI command of interest.
2119 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2120 * @buflen: Response buffer length.
2121 *
2122 * Simulate REPORT LUNS command.
2123 *
2124 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002125 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 */
2127
2128unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
2129 unsigned int buflen)
2130{
2131 VPRINTK("ENTER\n");
2132 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2133
2134 return 0;
2135}
2136
2137/**
Douglas Gilbert845c5832005-10-09 08:55:41 -04002138 * ata_scsi_set_sense - Set SCSI sense data and status
2139 * @cmd: SCSI request to be handled
2140 * @sk: SCSI-defined sense key
2141 * @asc: SCSI-defined additional sense code
2142 * @ascq: SCSI-defined additional sense code qualifier
2143 *
2144 * Helper function that builds a valid fixed format, current
2145 * response code and the given sense key (sk), additional sense
2146 * code (asc) and additional sense code qualifier (ascq) with
2147 * a SCSI command status of %SAM_STAT_CHECK_CONDITION and
2148 * DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
2149 *
2150 * LOCKING:
2151 * Not required
2152 */
2153
2154void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
2155{
2156 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
2157
2158 cmd->sense_buffer[0] = 0x70; /* fixed format, current */
2159 cmd->sense_buffer[2] = sk;
2160 cmd->sense_buffer[7] = 18 - 8; /* additional sense length */
2161 cmd->sense_buffer[12] = asc;
2162 cmd->sense_buffer[13] = ascq;
2163}
2164
2165/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166 * ata_scsi_badcmd - End a SCSI request with an error
2167 * @cmd: SCSI request to be handled
2168 * @done: SCSI command completion function
2169 * @asc: SCSI-defined additional sense code
2170 * @ascq: SCSI-defined additional sense code qualifier
2171 *
2172 * Helper function that completes a SCSI command with
2173 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
2174 * and the specified additional sense codes.
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
2180void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
2181{
2182 DPRINTK("ENTER\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04002183 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184
2185 done(cmd);
2186}
2187
Tejun Heo77853bf2006-01-23 13:09:36 +09002188static void atapi_sense_complete(struct ata_queued_cmd *qc)
Jeff Garzika939c962005-10-05 17:09:16 -04002189{
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002190 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002191 /* FIXME: not quite right; we don't want the
2192 * translation of taskfile registers into
2193 * a sense descriptors, since that's only
2194 * correct for ATA, not ATAPI
2195 */
Tejun Heo750426a2006-11-14 22:37:35 +09002196 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002197 }
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002198
2199 qc->scsidone(qc->scsicmd);
Tejun Heo77853bf2006-01-23 13:09:36 +09002200 ata_qc_free(qc);
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002201}
2202
2203/* is it pointless to prefer PIO for "safety reasons"? */
2204static inline int ata_pio_use_silly(struct ata_port *ap)
2205{
2206 return (ap->flags & ATA_FLAG_PIO_DMA);
2207}
2208
2209static void atapi_request_sense(struct ata_queued_cmd *qc)
2210{
2211 struct ata_port *ap = qc->ap;
2212 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika939c962005-10-05 17:09:16 -04002213
2214 DPRINTK("ATAPI request sense\n");
2215
Jeff Garzika939c962005-10-05 17:09:16 -04002216 /* FIXME: is this needed? */
2217 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
2218
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002219 ap->ops->tf_read(ap, &qc->tf);
2220
2221 /* fill these in, for the case where they are -not- overwritten */
2222 cmd->sense_buffer[0] = 0x70;
2223 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2224
2225 ata_qc_reinit(qc);
2226
Jeff Garzika939c962005-10-05 17:09:16 -04002227 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
2228 qc->dma_dir = DMA_FROM_DEVICE;
2229
Tejun Heo6e7846e2006-02-12 23:32:58 +09002230 memset(&qc->cdb, 0, qc->dev->cdb_len);
Jeff Garzika939c962005-10-05 17:09:16 -04002231 qc->cdb[0] = REQUEST_SENSE;
2232 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2233
2234 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2235 qc->tf.command = ATA_CMD_PACKET;
2236
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002237 if (ata_pio_use_silly(ap)) {
2238 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
2239 qc->tf.feature |= ATAPI_PKT_DMA;
2240 } else {
2241 qc->tf.protocol = ATA_PROT_ATAPI;
2242 qc->tf.lbam = (8 * 1024) & 0xff;
2243 qc->tf.lbah = (8 * 1024) >> 8;
2244 }
Jeff Garzika939c962005-10-05 17:09:16 -04002245 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2246
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002247 qc->complete_fn = atapi_sense_complete;
Jeff Garzika939c962005-10-05 17:09:16 -04002248
Tejun Heo8e0e6942006-03-31 20:41:11 +09002249 ata_qc_issue(qc);
Jeff Garzika939c962005-10-05 17:09:16 -04002250
2251 DPRINTK("EXIT\n");
2252}
2253
Tejun Heo77853bf2006-01-23 13:09:36 +09002254static void atapi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255{
2256 struct scsi_cmnd *cmd = qc->scsicmd;
Albert Leea22e2eb2005-12-05 15:38:02 +08002257 unsigned int err_mask = qc->err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002258
Jeff Garzika7dac442005-10-30 04:44:42 -05002259 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
Jeff Garzike12669e2005-10-05 18:39:23 -04002260
Tejun Heo246619d2006-05-15 20:58:16 +09002261 /* handle completion from new EH */
2262 if (unlikely(qc->ap->ops->error_handler &&
2263 (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
2264
2265 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
2266 /* FIXME: not quite right; we don't want the
2267 * translation of taskfile registers into a
2268 * sense descriptors, since that's only
2269 * correct for ATA, not ATAPI
2270 */
Tejun Heo750426a2006-11-14 22:37:35 +09002271 ata_gen_passthru_sense(qc);
Tejun Heo246619d2006-05-15 20:58:16 +09002272 }
2273
Tejun Heo22aac082006-08-08 14:08:59 +09002274 /* SCSI EH automatically locks door if sdev->locked is
2275 * set. Sometimes door lock request continues to
2276 * fail, for example, when no media is present. This
2277 * creates a loop - SCSI EH issues door lock which
2278 * fails and gets invoked again to acquire sense data
2279 * for the failed command.
2280 *
2281 * If door lock fails, always clear sdev->locked to
2282 * avoid this infinite loop.
2283 */
2284 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL)
2285 qc->dev->sdev->locked = 0;
2286
Tejun Heo246619d2006-05-15 20:58:16 +09002287 qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
2288 qc->scsidone(cmd);
2289 ata_qc_free(qc);
2290 return;
2291 }
2292
2293 /* successful completion or old EH failure path */
Jeff Garzika7dac442005-10-30 04:44:42 -05002294 if (unlikely(err_mask & AC_ERR_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002295 cmd->result = SAM_STAT_CHECK_CONDITION;
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002296 atapi_request_sense(qc);
Tejun Heo77853bf2006-01-23 13:09:36 +09002297 return;
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002298 } else if (unlikely(err_mask)) {
Jeff Garzika7dac442005-10-30 04:44:42 -05002299 /* FIXME: not quite right; we don't want the
2300 * translation of taskfile registers into
2301 * a sense descriptors, since that's only
2302 * correct for ATA, not ATAPI
2303 */
Tejun Heo750426a2006-11-14 22:37:35 +09002304 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002305 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 u8 *scsicmd = cmd->cmnd;
2307
Tony Battersbyfd71da42005-12-21 16:35:44 -05002308 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002309 u8 *buf = NULL;
2310 unsigned int buflen;
2311
2312 buflen = ata_scsi_rbuf_get(cmd, &buf);
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002313
2314 /* ATAPI devices typically report zero for their SCSI version,
2315 * and sometimes deviate from the spec WRT response data
2316 * format. If SCSI version is reported as zero like normal,
2317 * then we make the following fixups: 1) Fake MMC-5 version,
2318 * to indicate to the Linux scsi midlayer this is a modern
2319 * device. 2) Ensure response data format / ATAPI information
2320 * are always correct.
2321 */
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002322 if (buf[2] == 0) {
2323 buf[2] = 0x5;
2324 buf[3] = 0x32;
2325 }
2326
Linus Torvalds1da177e2005-04-16 15:20:36 -07002327 ata_scsi_rbuf_put(cmd, buf);
2328 }
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 cmd->result = SAM_STAT_GOOD;
2331 }
2332
2333 qc->scsidone(cmd);
Tejun Heo77853bf2006-01-23 13:09:36 +09002334 ata_qc_free(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335}
2336/**
2337 * atapi_xlat - Initialize PACKET taskfile
2338 * @qc: command structure to be initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339 *
2340 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002341 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002342 *
2343 * RETURNS:
2344 * Zero on success, non-zero on failure.
2345 */
Tejun Heoad706992006-12-17 10:45:57 +09002346static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347{
Tejun Heo542b1442006-12-17 10:45:08 +09002348 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 struct ata_device *dev = qc->dev;
2350 int using_pio = (dev->flags & ATA_DFLAG_PIO);
Tejun Heo542b1442006-12-17 10:45:08 +09002351 int nodata = (scmd->sc_data_direction == DMA_NONE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002352
2353 if (!using_pio)
2354 /* Check whether ATAPI DMA is safe */
2355 if (ata_check_atapi_dma(qc))
2356 using_pio = 1;
2357
Tejun Heoad706992006-12-17 10:45:57 +09002358 memcpy(&qc->cdb, scmd->cmnd, dev->cdb_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002359
2360 qc->complete_fn = atapi_qc_complete;
2361
2362 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heo542b1442006-12-17 10:45:08 +09002363 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 qc->tf.flags |= ATA_TFLAG_WRITE;
2365 DPRINTK("direction: write\n");
2366 }
2367
2368 qc->tf.command = ATA_CMD_PACKET;
2369
2370 /* no data, or PIO data xfer */
2371 if (using_pio || nodata) {
2372 if (nodata)
2373 qc->tf.protocol = ATA_PROT_ATAPI_NODATA;
2374 else
2375 qc->tf.protocol = ATA_PROT_ATAPI;
2376 qc->tf.lbam = (8 * 1024) & 0xff;
2377 qc->tf.lbah = (8 * 1024) >> 8;
2378 }
2379
2380 /* DMA data xfer */
2381 else {
2382 qc->tf.protocol = ATA_PROT_ATAPI_DMA;
2383 qc->tf.feature |= ATAPI_PKT_DMA;
2384
Tejun Heo542b1442006-12-17 10:45:08 +09002385 if (atapi_dmadir && (scmd->sc_data_direction != DMA_TO_DEVICE))
Albert Lee95de7192006-04-04 10:57:18 +08002386 /* some SATA bridges need us to indicate data xfer direction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 qc->tf.feature |= ATAPI_DMADIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 }
2389
Tejun Heo542b1442006-12-17 10:45:08 +09002390 qc->nbytes = scmd->request_bufflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
2392 return 0;
2393}
2394
Tejun Heoab5b3a52006-05-31 18:27:34 +09002395static struct ata_device * ata_find_dev(struct ata_port *ap, int id)
2396{
2397 if (likely(id < ATA_MAX_DEVICES))
2398 return &ap->device[id];
2399 return NULL;
2400}
2401
2402static struct ata_device * __ata_scsi_find_dev(struct ata_port *ap,
2403 const struct scsi_device *scsidev)
2404{
2405 /* skip commands not addressed to targets we simulate */
2406 if (unlikely(scsidev->channel || scsidev->lun))
2407 return NULL;
2408
2409 return ata_find_dev(ap, scsidev->id);
2410}
2411
Linus Torvalds1da177e2005-04-16 15:20:36 -07002412/**
Brian King99ba9e02006-06-12 14:54:17 -05002413 * ata_scsi_dev_enabled - determine if device is enabled
2414 * @dev: ATA device
2415 *
2416 * Determine if commands should be sent to the specified device.
2417 *
2418 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002419 * spin_lock_irqsave(host lock)
Brian King99ba9e02006-06-12 14:54:17 -05002420 *
2421 * RETURNS:
2422 * 0 if commands are not allowed / 1 if commands are allowed
2423 */
2424
2425static int ata_scsi_dev_enabled(struct ata_device *dev)
2426{
2427 if (unlikely(!ata_dev_enabled(dev)))
2428 return 0;
2429
2430 if (!atapi_enabled || (dev->ap->flags & ATA_FLAG_NO_ATAPI)) {
2431 if (unlikely(dev->class == ATA_DEV_ATAPI)) {
2432 ata_dev_printk(dev, KERN_WARNING,
2433 "WARNING: ATAPI is %s, device ignored.\n",
2434 atapi_enabled ? "not supported with this driver" : "disabled");
2435 return 0;
2436 }
2437 }
2438
2439 return 1;
2440}
2441
2442/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2444 * @ap: ATA port to which the device is attached
2445 * @scsidev: SCSI device from which we derive the ATA device
2446 *
2447 * Given various information provided in struct scsi_cmnd,
2448 * map that onto an ATA bus, and using that mapping
2449 * determine which ata_device is associated with the
2450 * SCSI command to be sent.
2451 *
2452 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002453 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002454 *
2455 * RETURNS:
2456 * Associated ATA device, or %NULL if not found.
2457 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002458static struct ata_device *
Jeff Garzik057ace52005-10-22 14:27:05 -04002459ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002460{
Tejun Heoab5b3a52006-05-31 18:27:34 +09002461 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462
Brian King99ba9e02006-06-12 14:54:17 -05002463 if (unlikely(!dev || !ata_scsi_dev_enabled(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 return NULL;
2465
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 return dev;
2467}
2468
Jeff Garzikb0955182005-05-12 15:45:22 -04002469/*
2470 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2471 * @byte1: Byte 1 from pass-thru CDB.
2472 *
2473 * RETURNS:
2474 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2475 */
2476static u8
2477ata_scsi_map_proto(u8 byte1)
2478{
2479 switch((byte1 & 0x1e) >> 1) {
2480 case 3: /* Non-data */
2481 return ATA_PROT_NODATA;
2482
2483 case 6: /* DMA */
2484 return ATA_PROT_DMA;
2485
2486 case 4: /* PIO Data-in */
2487 case 5: /* PIO Data-out */
Jeff Garzikb0955182005-05-12 15:45:22 -04002488 return ATA_PROT_PIO;
2489
2490 case 10: /* Device Reset */
2491 case 0: /* Hard Reset */
2492 case 1: /* SRST */
2493 case 2: /* Bus Idle */
2494 case 7: /* Packet */
2495 case 8: /* DMA Queued */
2496 case 9: /* Device Diagnostic */
2497 case 11: /* UDMA Data-in */
2498 case 12: /* UDMA Data-Out */
2499 case 13: /* FPDMA */
2500 default: /* Reserved */
2501 break;
2502 }
2503
2504 return ATA_PROT_UNKNOWN;
2505}
2506
2507/**
2508 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
2509 * @qc: command structure to be initialized
Jeff Garzikb0955182005-05-12 15:45:22 -04002510 *
2511 * Handles either 12 or 16-byte versions of the CDB.
2512 *
2513 * RETURNS:
2514 * Zero on success, non-zero on failure.
2515 */
Tejun Heoad706992006-12-17 10:45:57 +09002516static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04002517{
2518 struct ata_taskfile *tf = &(qc->tf);
Tejun Heo542b1442006-12-17 10:45:08 +09002519 struct scsi_cmnd *scmd = qc->scsicmd;
Alan Coxf79d4092006-05-22 16:55:11 +01002520 struct ata_device *dev = qc->dev;
Tejun Heoad706992006-12-17 10:45:57 +09002521 const u8 *cdb = scmd->cmnd;
Jeff Garzikb0955182005-05-12 15:45:22 -04002522
Tejun Heo542b1442006-12-17 10:45:08 +09002523 if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN)
Tejun Heo9a405252005-12-02 11:49:11 +09002524 goto invalid_fld;
Jeff Garzik8190bdb2006-05-24 01:53:39 -04002525
Alan Coxf79d4092006-05-22 16:55:11 +01002526 /* We may not issue DMA commands if no DMA mode is set */
2527 if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0)
2528 goto invalid_fld;
Jeff Garzikb0955182005-05-12 15:45:22 -04002529
Tejun Heo542b1442006-12-17 10:45:08 +09002530 if (cdb[1] & 0xe0)
Albert Leef59b0cf2006-03-16 17:59:22 +08002531 /* PIO multi not supported yet */
2532 goto invalid_fld;
2533
Jeff Garzikb0955182005-05-12 15:45:22 -04002534 /*
2535 * 12 and 16 byte CDBs use different offsets to
2536 * provide the various register values.
2537 */
Tejun Heo542b1442006-12-17 10:45:08 +09002538 if (cdb[0] == ATA_16) {
Jeff Garzikb0955182005-05-12 15:45:22 -04002539 /*
2540 * 16-byte CDB - may contain extended commands.
2541 *
2542 * If that is the case, copy the upper byte register values.
2543 */
Tejun Heo542b1442006-12-17 10:45:08 +09002544 if (cdb[1] & 0x01) {
2545 tf->hob_feature = cdb[3];
2546 tf->hob_nsect = cdb[5];
2547 tf->hob_lbal = cdb[7];
2548 tf->hob_lbam = cdb[9];
2549 tf->hob_lbah = cdb[11];
Jeff Garzikb0955182005-05-12 15:45:22 -04002550 tf->flags |= ATA_TFLAG_LBA48;
2551 } else
2552 tf->flags &= ~ATA_TFLAG_LBA48;
2553
2554 /*
2555 * Always copy low byte, device and command registers.
2556 */
Tejun Heo542b1442006-12-17 10:45:08 +09002557 tf->feature = cdb[4];
2558 tf->nsect = cdb[6];
2559 tf->lbal = cdb[8];
2560 tf->lbam = cdb[10];
2561 tf->lbah = cdb[12];
2562 tf->device = cdb[13];
2563 tf->command = cdb[14];
Jeff Garzikb0955182005-05-12 15:45:22 -04002564 } else {
2565 /*
2566 * 12-byte CDB - incapable of extended commands.
2567 */
2568 tf->flags &= ~ATA_TFLAG_LBA48;
2569
Tejun Heo542b1442006-12-17 10:45:08 +09002570 tf->feature = cdb[3];
2571 tf->nsect = cdb[4];
2572 tf->lbal = cdb[5];
2573 tf->lbam = cdb[6];
2574 tf->lbah = cdb[7];
2575 tf->device = cdb[8];
2576 tf->command = cdb[9];
Jeff Garzikb0955182005-05-12 15:45:22 -04002577 }
Mark Lorddcc2d1e2005-11-13 16:22:06 -05002578 /*
2579 * If slave is possible, enforce correct master/slave bit
2580 */
2581 if (qc->ap->flags & ATA_FLAG_SLAVE_POSS)
2582 tf->device = qc->dev->devno ?
2583 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
Jeff Garzikb0955182005-05-12 15:45:22 -04002584
2585 /*
2586 * Filter SET_FEATURES - XFER MODE command -- otherwise,
2587 * SET_FEATURES - XFER MODE must be preceded/succeeded
2588 * by an update to hardware-specific registers for each
2589 * controller (i.e. the reason for ->set_piomode(),
2590 * ->set_dmamode(), and ->post_set_mode() hooks).
2591 */
2592 if ((tf->command == ATA_CMD_SET_FEATURES)
2593 && (tf->feature == SETFEATURES_XFER))
Tejun Heo9a405252005-12-02 11:49:11 +09002594 goto invalid_fld;
Jeff Garzikb0955182005-05-12 15:45:22 -04002595
2596 /*
2597 * Set flags so that all registers will be written,
2598 * and pass on write indication (used for PIO/DMA
2599 * setup.)
2600 */
2601 tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
2602
Tejun Heo542b1442006-12-17 10:45:08 +09002603 if (scmd->sc_data_direction == DMA_TO_DEVICE)
Jeff Garzikb0955182005-05-12 15:45:22 -04002604 tf->flags |= ATA_TFLAG_WRITE;
2605
2606 /*
2607 * Set transfer length.
2608 *
2609 * TODO: find out if we need to do more here to
2610 * cover scatter/gather case.
2611 */
Tejun Heo542b1442006-12-17 10:45:08 +09002612 qc->nsect = scmd->request_bufflen / ATA_SECT_SIZE;
Jeff Garzikb0955182005-05-12 15:45:22 -04002613
Tejun Heoe61e0672006-05-15 20:57:40 +09002614 /* request result TF */
2615 qc->flags |= ATA_QCFLAG_RESULT_TF;
2616
Jeff Garzikb0955182005-05-12 15:45:22 -04002617 return 0;
Tejun Heo9a405252005-12-02 11:49:11 +09002618
2619 invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +09002620 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
Tejun Heo9a405252005-12-02 11:49:11 +09002621 /* "Invalid field in cdb" */
2622 return 1;
Jeff Garzikb0955182005-05-12 15:45:22 -04002623}
2624
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625/**
2626 * ata_get_xlat_func - check if SCSI to ATA translation is possible
2627 * @dev: ATA device
2628 * @cmd: SCSI command opcode to consider
2629 *
2630 * Look up the SCSI command given, and determine whether the
2631 * SCSI command is to be translated or simulated.
2632 *
2633 * RETURNS:
2634 * Pointer to translation function if possible, %NULL if not.
2635 */
2636
2637static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
2638{
2639 switch (cmd) {
2640 case READ_6:
2641 case READ_10:
2642 case READ_16:
2643
2644 case WRITE_6:
2645 case WRITE_10:
2646 case WRITE_16:
2647 return ata_scsi_rw_xlat;
2648
2649 case SYNCHRONIZE_CACHE:
2650 if (ata_try_flush_cache(dev))
2651 return ata_scsi_flush_xlat;
2652 break;
2653
2654 case VERIFY:
2655 case VERIFY_16:
2656 return ata_scsi_verify_xlat;
Jeff Garzikb0955182005-05-12 15:45:22 -04002657
2658 case ATA_12:
2659 case ATA_16:
2660 return ata_scsi_pass_thru;
Jeff Garzikda613962005-08-29 19:01:43 -04002661
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04002662 case START_STOP:
2663 return ata_scsi_start_stop_xlat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 }
2665
2666 return NULL;
2667}
2668
2669/**
2670 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
2671 * @ap: ATA port to which the command was being sent
2672 * @cmd: SCSI command to dump
2673 *
2674 * Prints the contents of a SCSI command via printk().
2675 */
2676
2677static inline void ata_scsi_dump_cdb(struct ata_port *ap,
2678 struct scsi_cmnd *cmd)
2679{
2680#ifdef ATA_DEBUG
2681 struct scsi_device *scsidev = cmd->device;
2682 u8 *scsicmd = cmd->cmnd;
2683
2684 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
2685 ap->id,
2686 scsidev->channel, scsidev->id, scsidev->lun,
2687 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
2688 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
2689 scsicmd[8]);
2690#endif
2691}
2692
Tejun Heo542b1442006-12-17 10:45:08 +09002693static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
Tejun Heo2115ea92006-05-15 21:03:39 +09002694 void (*done)(struct scsi_cmnd *),
2695 struct ata_device *dev)
Brian Kingeb3f0f92006-03-23 17:30:02 -06002696{
Tejun Heo2115ea92006-05-15 21:03:39 +09002697 int rc = 0;
2698
Brian Kingeb3f0f92006-03-23 17:30:02 -06002699 if (dev->class == ATA_DEV_ATA) {
2700 ata_xlat_func_t xlat_func = ata_get_xlat_func(dev,
Tejun Heo542b1442006-12-17 10:45:08 +09002701 scmd->cmnd[0]);
Brian Kingeb3f0f92006-03-23 17:30:02 -06002702
2703 if (xlat_func)
Tejun Heo542b1442006-12-17 10:45:08 +09002704 rc = ata_scsi_translate(dev, scmd, done, xlat_func);
Brian Kingeb3f0f92006-03-23 17:30:02 -06002705 else
Tejun Heo542b1442006-12-17 10:45:08 +09002706 ata_scsi_simulate(dev, scmd, done);
Brian Kingeb3f0f92006-03-23 17:30:02 -06002707 } else
Tejun Heo542b1442006-12-17 10:45:08 +09002708 rc = ata_scsi_translate(dev, scmd, done, atapi_xlat);
Tejun Heo2115ea92006-05-15 21:03:39 +09002709
2710 return rc;
Brian Kingeb3f0f92006-03-23 17:30:02 -06002711}
2712
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713/**
2714 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
2715 * @cmd: SCSI command to be sent
2716 * @done: Completion function, called when command is complete
2717 *
2718 * In some cases, this function translates SCSI commands into
2719 * ATA taskfiles, and queues the taskfiles to be sent to
2720 * hardware. In other cases, this function simulates a
2721 * SCSI device by evaluating and responding to certain
2722 * SCSI commands. This creates the overall effect of
2723 * ATA and ATAPI devices appearing as SCSI devices.
2724 *
2725 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002726 * Releases scsi-layer-held lock, and obtains host lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727 *
2728 * RETURNS:
Tejun Heo2115ea92006-05-15 21:03:39 +09002729 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
2730 * 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002731 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
2733{
2734 struct ata_port *ap;
2735 struct ata_device *dev;
2736 struct scsi_device *scsidev = cmd->device;
Jeff Garzik005a5a02005-10-30 23:31:48 -05002737 struct Scsi_Host *shost = scsidev->host;
Tejun Heo2115ea92006-05-15 21:03:39 +09002738 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739
Jeff Garzik35bb94b2006-04-11 13:12:34 -04002740 ap = ata_shost_to_port(shost);
Jeff Garzik005a5a02005-10-30 23:31:48 -05002741
2742 spin_unlock(shost->host_lock);
Jeff Garzikba6a1302006-06-22 23:46:10 -04002743 spin_lock(ap->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
2745 ata_scsi_dump_cdb(ap, cmd);
2746
2747 dev = ata_scsi_find_dev(ap, scsidev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06002748 if (likely(dev))
Tejun Heo2115ea92006-05-15 21:03:39 +09002749 rc = __ata_scsi_queuecmd(cmd, done, dev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06002750 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 cmd->result = (DID_BAD_TARGET << 16);
2752 done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 }
2754
Jeff Garzikba6a1302006-06-22 23:46:10 -04002755 spin_unlock(ap->lock);
Jeff Garzik005a5a02005-10-30 23:31:48 -05002756 spin_lock(shost->host_lock);
Tejun Heo2115ea92006-05-15 21:03:39 +09002757 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002758}
2759
2760/**
2761 * ata_scsi_simulate - simulate SCSI command on ATA device
Randy Dunlapc893a3a2006-01-28 13:15:32 -05002762 * @dev: the target device
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 * @cmd: SCSI command being sent to device.
2764 * @done: SCSI command completion function.
2765 *
2766 * Interprets and directly executes a select list of SCSI commands
2767 * that can be handled internally.
2768 *
2769 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002770 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771 */
2772
Tejun Heo3373efd2006-05-15 20:57:53 +09002773void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 void (*done)(struct scsi_cmnd *))
2775{
2776 struct ata_scsi_args args;
Jeff Garzik057ace52005-10-22 14:27:05 -04002777 const u8 *scsicmd = cmd->cmnd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002778
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002779 args.dev = dev;
2780 args.id = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 args.cmd = cmd;
2782 args.done = done;
2783
2784 switch(scsicmd[0]) {
2785 /* no-op's, complete with success */
2786 case SYNCHRONIZE_CACHE:
2787 case REZERO_UNIT:
2788 case SEEK_6:
2789 case SEEK_10:
2790 case TEST_UNIT_READY:
2791 case FORMAT_UNIT: /* FIXME: correct? */
2792 case SEND_DIAGNOSTIC: /* FIXME: correct? */
2793 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
2794 break;
2795
2796 case INQUIRY:
2797 if (scsicmd[1] & 2) /* is CmdDt set? */
Douglas Gilbertae006512005-10-09 09:09:35 -04002798 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
2800 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
2801 else if (scsicmd[2] == 0x00)
2802 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
2803 else if (scsicmd[2] == 0x80)
2804 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
2805 else if (scsicmd[2] == 0x83)
2806 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
2807 else
Douglas Gilbertae006512005-10-09 09:09:35 -04002808 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002809 break;
2810
2811 case MODE_SENSE:
2812 case MODE_SENSE_10:
2813 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
2814 break;
2815
2816 case MODE_SELECT: /* unconditionally return */
2817 case MODE_SELECT_10: /* bad-field-in-cdb */
Douglas Gilbertae006512005-10-09 09:09:35 -04002818 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002819 break;
2820
2821 case READ_CAPACITY:
2822 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2823 break;
2824
2825 case SERVICE_ACTION_IN:
2826 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
2827 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2828 else
Douglas Gilbertae006512005-10-09 09:09:35 -04002829 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002830 break;
2831
2832 case REPORT_LUNS:
2833 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
2834 break;
2835
Jeff Garzikb0955182005-05-12 15:45:22 -04002836 /* mandatory commands we haven't implemented yet */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 case REQUEST_SENSE:
2838
2839 /* all other commands */
2840 default:
Douglas Gilbertae006512005-10-09 09:09:35 -04002841 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
2842 /* "Invalid command operation code" */
2843 done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002844 break;
2845 }
2846}
2847
Jeff Garzik644dd0c2005-10-03 15:55:19 -04002848void ata_scsi_scan_host(struct ata_port *ap)
2849{
2850 unsigned int i;
2851
Tejun Heo198e0fe2006-04-02 18:51:52 +09002852 if (ap->flags & ATA_FLAG_DISABLED)
Jeff Garzik644dd0c2005-10-03 15:55:19 -04002853 return;
2854
Jeff Garzik3f19ee82005-10-03 21:36:41 -04002855 for (i = 0; i < ATA_MAX_DEVICES; i++) {
Tejun Heo3edebac2006-05-31 18:27:40 +09002856 struct ata_device *dev = &ap->device[i];
2857 struct scsi_device *sdev;
Jeff Garzik3f19ee82005-10-03 21:36:41 -04002858
Tejun Heo3edebac2006-05-31 18:27:40 +09002859 if (!ata_dev_enabled(dev) || dev->sdev)
2860 continue;
2861
Jeff Garzikcca39742006-08-24 03:19:22 -04002862 sdev = __scsi_add_device(ap->scsi_host, 0, i, 0, NULL);
Tejun Heo3edebac2006-05-31 18:27:40 +09002863 if (!IS_ERR(sdev)) {
2864 dev->sdev = sdev;
2865 scsi_device_put(sdev);
2866 }
Jeff Garzik3f19ee82005-10-03 21:36:41 -04002867 }
Jeff Garzik644dd0c2005-10-03 15:55:19 -04002868}
Tejun Heo0ea035a2006-05-31 18:28:01 +09002869
2870/**
2871 * ata_scsi_offline_dev - offline attached SCSI device
2872 * @dev: ATA device to offline attached SCSI device for
2873 *
2874 * This function is called from ata_eh_hotplug() and responsible
2875 * for taking the SCSI device attached to @dev offline. This
Jeff Garzikcca39742006-08-24 03:19:22 -04002876 * function is called with host lock which protects dev->sdev
Tejun Heo0ea035a2006-05-31 18:28:01 +09002877 * against clearing.
2878 *
2879 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002880 * spin_lock_irqsave(host lock)
Tejun Heo0ea035a2006-05-31 18:28:01 +09002881 *
2882 * RETURNS:
2883 * 1 if attached SCSI device exists, 0 otherwise.
2884 */
2885int ata_scsi_offline_dev(struct ata_device *dev)
2886{
2887 if (dev->sdev) {
2888 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
2889 return 1;
2890 }
2891 return 0;
2892}
Tejun Heo580b2102006-05-31 18:28:05 +09002893
2894/**
2895 * ata_scsi_remove_dev - remove attached SCSI device
2896 * @dev: ATA device to remove attached SCSI device for
2897 *
2898 * This function is called from ata_eh_scsi_hotplug() and
2899 * responsible for removing the SCSI device attached to @dev.
2900 *
2901 * LOCKING:
2902 * Kernel thread context (may sleep).
2903 */
2904static void ata_scsi_remove_dev(struct ata_device *dev)
2905{
2906 struct ata_port *ap = dev->ap;
2907 struct scsi_device *sdev;
2908 unsigned long flags;
2909
2910 /* Alas, we need to grab scan_mutex to ensure SCSI device
2911 * state doesn't change underneath us and thus
2912 * scsi_device_get() always succeeds. The mutex locking can
2913 * be removed if there is __scsi_device_get() interface which
2914 * increments reference counts regardless of device state.
2915 */
Jeff Garzikcca39742006-08-24 03:19:22 -04002916 mutex_lock(&ap->scsi_host->scan_mutex);
Jeff Garzikba6a1302006-06-22 23:46:10 -04002917 spin_lock_irqsave(ap->lock, flags);
Tejun Heo580b2102006-05-31 18:28:05 +09002918
Jeff Garzikcca39742006-08-24 03:19:22 -04002919 /* clearing dev->sdev is protected by host lock */
Tejun Heo580b2102006-05-31 18:28:05 +09002920 sdev = dev->sdev;
2921 dev->sdev = NULL;
2922
2923 if (sdev) {
2924 /* If user initiated unplug races with us, sdev can go
Jeff Garzikcca39742006-08-24 03:19:22 -04002925 * away underneath us after the host lock and
Tejun Heo580b2102006-05-31 18:28:05 +09002926 * scan_mutex are released. Hold onto it.
2927 */
2928 if (scsi_device_get(sdev) == 0) {
2929 /* The following ensures the attached sdev is
2930 * offline on return from ata_scsi_offline_dev()
2931 * regardless it wins or loses the race
2932 * against this function.
2933 */
2934 scsi_device_set_state(sdev, SDEV_OFFLINE);
2935 } else {
2936 WARN_ON(1);
2937 sdev = NULL;
2938 }
2939 }
2940
Jeff Garzikba6a1302006-06-22 23:46:10 -04002941 spin_unlock_irqrestore(ap->lock, flags);
Jeff Garzikcca39742006-08-24 03:19:22 -04002942 mutex_unlock(&ap->scsi_host->scan_mutex);
Tejun Heo580b2102006-05-31 18:28:05 +09002943
2944 if (sdev) {
2945 ata_dev_printk(dev, KERN_INFO, "detaching (SCSI %s)\n",
2946 sdev->sdev_gendev.bus_id);
2947
2948 scsi_remove_device(sdev);
2949 scsi_device_put(sdev);
2950 }
2951}
2952
2953/**
2954 * ata_scsi_hotplug - SCSI part of hotplug
David Howells65f27f32006-11-22 14:55:48 +00002955 * @work: Pointer to ATA port to perform SCSI hotplug on
Tejun Heo580b2102006-05-31 18:28:05 +09002956 *
2957 * Perform SCSI part of hotplug. It's executed from a separate
2958 * workqueue after EH completes. This is necessary because SCSI
2959 * hot plugging requires working EH and hot unplugging is
2960 * synchronized with hot plugging with a mutex.
2961 *
2962 * LOCKING:
2963 * Kernel thread context (may sleep).
2964 */
David Howells65f27f32006-11-22 14:55:48 +00002965void ata_scsi_hotplug(struct work_struct *work)
Tejun Heo580b2102006-05-31 18:28:05 +09002966{
David Howells65f27f32006-11-22 14:55:48 +00002967 struct ata_port *ap =
2968 container_of(work, struct ata_port, hotplug_task.work);
Tejun Heo580b2102006-05-31 18:28:05 +09002969 int i;
2970
Tejun Heob51e9e52006-06-29 01:29:30 +09002971 if (ap->pflags & ATA_PFLAG_UNLOADING) {
Tejun Heo580b2102006-05-31 18:28:05 +09002972 DPRINTK("ENTER/EXIT - unloading\n");
2973 return;
2974 }
2975
2976 DPRINTK("ENTER\n");
2977
2978 /* unplug detached devices */
2979 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2980 struct ata_device *dev = &ap->device[i];
2981 unsigned long flags;
2982
2983 if (!(dev->flags & ATA_DFLAG_DETACHED))
2984 continue;
2985
Jeff Garzikba6a1302006-06-22 23:46:10 -04002986 spin_lock_irqsave(ap->lock, flags);
Tejun Heo580b2102006-05-31 18:28:05 +09002987 dev->flags &= ~ATA_DFLAG_DETACHED;
Jeff Garzikba6a1302006-06-22 23:46:10 -04002988 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo580b2102006-05-31 18:28:05 +09002989
2990 ata_scsi_remove_dev(dev);
2991 }
2992
2993 /* scan for new ones */
2994 ata_scsi_scan_host(ap);
2995
2996 /* If we scanned while EH was in progress, scan would have
2997 * failed silently. Requeue if there are enabled but
2998 * unattached devices.
2999 */
3000 for (i = 0; i < ATA_MAX_DEVICES; i++) {
3001 struct ata_device *dev = &ap->device[i];
3002 if (ata_dev_enabled(dev) && !dev->sdev) {
3003 queue_delayed_work(ata_aux_wq, &ap->hotplug_task, HZ);
3004 break;
3005 }
3006 }
3007
3008 DPRINTK("EXIT\n");
3009}
Tejun Heo83c47bc2006-05-31 18:28:07 +09003010
3011/**
3012 * ata_scsi_user_scan - indication for user-initiated bus scan
3013 * @shost: SCSI host to scan
3014 * @channel: Channel to scan
3015 * @id: ID to scan
3016 * @lun: LUN to scan
3017 *
3018 * This function is called when user explicitly requests bus
3019 * scan. Set probe pending flag and invoke EH.
3020 *
3021 * LOCKING:
3022 * SCSI layer (we don't care)
3023 *
3024 * RETURNS:
3025 * Zero.
3026 */
3027static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
3028 unsigned int id, unsigned int lun)
3029{
3030 struct ata_port *ap = ata_shost_to_port(shost);
3031 unsigned long flags;
3032 int rc = 0;
3033
3034 if (!ap->ops->error_handler)
3035 return -EOPNOTSUPP;
3036
3037 if ((channel != SCAN_WILD_CARD && channel != 0) ||
3038 (lun != SCAN_WILD_CARD && lun != 0))
3039 return -EINVAL;
3040
Jeff Garzikba6a1302006-06-22 23:46:10 -04003041 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09003042
3043 if (id == SCAN_WILD_CARD) {
3044 ap->eh_info.probe_mask |= (1 << ATA_MAX_DEVICES) - 1;
3045 ap->eh_info.action |= ATA_EH_SOFTRESET;
3046 } else {
3047 struct ata_device *dev = ata_find_dev(ap, id);
3048
3049 if (dev) {
3050 ap->eh_info.probe_mask |= 1 << dev->devno;
3051 ap->eh_info.action |= ATA_EH_SOFTRESET;
Tejun Heo28324302006-07-03 16:07:26 +09003052 ap->eh_info.flags |= ATA_EHI_RESUME_LINK;
Tejun Heo83c47bc2006-05-31 18:28:07 +09003053 } else
3054 rc = -EINVAL;
3055 }
3056
Tejun Heo309afcb2006-09-30 18:07:17 +09003057 if (rc == 0) {
Tejun Heo83c47bc2006-05-31 18:28:07 +09003058 ata_port_schedule_eh(ap);
Tejun Heo309afcb2006-09-30 18:07:17 +09003059 spin_unlock_irqrestore(ap->lock, flags);
3060 ata_port_wait_eh(ap);
3061 } else
3062 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09003063
3064 return rc;
3065}
zhao, forrest3057ac32006-06-12 12:01:34 +08003066
3067/**
Tejun Heod0171262006-06-12 22:51:14 +09003068 * ata_scsi_dev_rescan - initiate scsi_rescan_device()
David Howells65f27f32006-11-22 14:55:48 +00003069 * @work: Pointer to ATA port to perform scsi_rescan_device()
zhao, forrest3057ac32006-06-12 12:01:34 +08003070 *
Tejun Heod0171262006-06-12 22:51:14 +09003071 * After ATA pass thru (SAT) commands are executed successfully,
3072 * libata need to propagate the changes to SCSI layer. This
3073 * function must be executed from ata_aux_wq such that sdev
3074 * attach/detach don't race with rescan.
zhao, forrest3057ac32006-06-12 12:01:34 +08003075 *
Tejun Heod0171262006-06-12 22:51:14 +09003076 * LOCKING:
3077 * Kernel thread context (may sleep).
zhao, forrest3057ac32006-06-12 12:01:34 +08003078 */
David Howells65f27f32006-11-22 14:55:48 +00003079void ata_scsi_dev_rescan(struct work_struct *work)
zhao, forrest3057ac32006-06-12 12:01:34 +08003080{
David Howells65f27f32006-11-22 14:55:48 +00003081 struct ata_port *ap =
3082 container_of(work, struct ata_port, scsi_rescan_task);
Tejun Heof84e7e42006-11-22 11:21:31 +09003083 unsigned long flags;
zhao, forrest3057ac32006-06-12 12:01:34 +08003084 unsigned int i;
3085
Tejun Heof84e7e42006-11-22 11:21:31 +09003086 spin_lock_irqsave(ap->lock, flags);
zhao, forrest3057ac32006-06-12 12:01:34 +08003087
Tejun Heof84e7e42006-11-22 11:21:31 +09003088 for (i = 0; i < ATA_MAX_DEVICES; i++) {
3089 struct ata_device *dev = &ap->device[i];
3090 struct scsi_device *sdev = dev->sdev;
3091
3092 if (!ata_dev_enabled(dev) || !sdev)
3093 continue;
3094 if (scsi_device_get(sdev))
3095 continue;
3096
3097 spin_unlock_irqrestore(ap->lock, flags);
3098 scsi_rescan_device(&(sdev->sdev_gendev));
3099 scsi_device_put(sdev);
3100 spin_lock_irqsave(ap->lock, flags);
zhao, forrest3057ac32006-06-12 12:01:34 +08003101 }
Tejun Heof84e7e42006-11-22 11:21:31 +09003102
3103 spin_unlock_irqrestore(ap->lock, flags);
zhao, forrest3057ac32006-06-12 12:01:34 +08003104}
Brian King80289162006-08-07 14:27:31 -05003105
3106/**
3107 * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
Jeff Garzik4f931372006-09-29 05:07:25 -04003108 * @host: ATA host container for all SAS ports
Brian King80289162006-08-07 14:27:31 -05003109 * @port_info: Information from low-level host driver
Jeff Garzikcca39742006-08-24 03:19:22 -04003110 * @shost: SCSI host that the scsi device is attached to
Brian King80289162006-08-07 14:27:31 -05003111 *
3112 * LOCKING:
3113 * PCI/etc. bus probe sem.
3114 *
3115 * RETURNS:
3116 * ata_port pointer on success / NULL on failure.
3117 */
3118
Jeff Garzikcca39742006-08-24 03:19:22 -04003119struct ata_port *ata_sas_port_alloc(struct ata_host *host,
Brian King80289162006-08-07 14:27:31 -05003120 struct ata_port_info *port_info,
Jeff Garzikcca39742006-08-24 03:19:22 -04003121 struct Scsi_Host *shost)
Brian King80289162006-08-07 14:27:31 -05003122{
3123 struct ata_port *ap = kzalloc(sizeof(*ap), GFP_KERNEL);
3124 struct ata_probe_ent *ent;
3125
3126 if (!ap)
3127 return NULL;
3128
Jeff Garzikcca39742006-08-24 03:19:22 -04003129 ent = ata_probe_ent_alloc(host->dev, port_info);
Brian King80289162006-08-07 14:27:31 -05003130 if (!ent) {
3131 kfree(ap);
3132 return NULL;
3133 }
3134
Jeff Garzikcca39742006-08-24 03:19:22 -04003135 ata_port_init(ap, host, ent, 0);
3136 ap->lock = shost->host_lock;
Brian King80289162006-08-07 14:27:31 -05003137 kfree(ent);
3138 return ap;
3139}
3140EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
3141
3142/**
3143 * ata_sas_port_start - Set port up for dma.
3144 * @ap: Port to initialize
3145 *
3146 * Called just after data structures for each port are
3147 * initialized. Allocates DMA pad.
3148 *
3149 * May be used as the port_start() entry in ata_port_operations.
3150 *
3151 * LOCKING:
3152 * Inherited from caller.
3153 */
3154int ata_sas_port_start(struct ata_port *ap)
3155{
3156 return ata_pad_alloc(ap, ap->dev);
3157}
3158EXPORT_SYMBOL_GPL(ata_sas_port_start);
3159
3160/**
3161 * ata_port_stop - Undo ata_sas_port_start()
3162 * @ap: Port to shut down
3163 *
3164 * Frees the DMA pad.
3165 *
3166 * May be used as the port_stop() entry in ata_port_operations.
3167 *
3168 * LOCKING:
3169 * Inherited from caller.
3170 */
3171
3172void ata_sas_port_stop(struct ata_port *ap)
3173{
3174 ata_pad_free(ap, ap->dev);
3175}
3176EXPORT_SYMBOL_GPL(ata_sas_port_stop);
3177
3178/**
3179 * ata_sas_port_init - Initialize a SATA device
3180 * @ap: SATA port to initialize
3181 *
3182 * LOCKING:
3183 * PCI/etc. bus probe sem.
3184 *
3185 * RETURNS:
3186 * Zero on success, non-zero on error.
3187 */
3188
3189int ata_sas_port_init(struct ata_port *ap)
3190{
3191 int rc = ap->ops->port_start(ap);
3192
3193 if (!rc)
3194 rc = ata_bus_probe(ap);
3195
3196 return rc;
3197}
3198EXPORT_SYMBOL_GPL(ata_sas_port_init);
3199
3200/**
3201 * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
3202 * @ap: SATA port to destroy
3203 *
3204 */
3205
3206void ata_sas_port_destroy(struct ata_port *ap)
3207{
3208 ap->ops->port_stop(ap);
3209 kfree(ap);
3210}
3211EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
3212
3213/**
3214 * ata_sas_slave_configure - Default slave_config routine for libata devices
3215 * @sdev: SCSI device to configure
3216 * @ap: ATA port to which SCSI device is attached
3217 *
3218 * RETURNS:
3219 * Zero.
3220 */
3221
3222int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
3223{
3224 ata_scsi_sdev_config(sdev);
3225 ata_scsi_dev_config(sdev, ap->device);
3226 return 0;
3227}
3228EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
3229
3230/**
3231 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
3232 * @cmd: SCSI command to be sent
3233 * @done: Completion function, called when command is complete
3234 * @ap: ATA port to which the command is being sent
3235 *
3236 * RETURNS:
Brian King08475a12006-11-20 13:51:56 -06003237 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
3238 * 0 otherwise.
Brian King80289162006-08-07 14:27:31 -05003239 */
3240
3241int ata_sas_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *),
3242 struct ata_port *ap)
3243{
Brian King08475a12006-11-20 13:51:56 -06003244 int rc = 0;
3245
Brian King80289162006-08-07 14:27:31 -05003246 ata_scsi_dump_cdb(ap, cmd);
3247
3248 if (likely(ata_scsi_dev_enabled(ap->device)))
Brian King08475a12006-11-20 13:51:56 -06003249 rc = __ata_scsi_queuecmd(cmd, done, ap->device);
Brian King80289162006-08-07 14:27:31 -05003250 else {
3251 cmd->result = (DID_BAD_TARGET << 16);
3252 done(cmd);
3253 }
Brian King08475a12006-11-20 13:51:56 -06003254 return rc;
Brian King80289162006-08-07 14:27:31 -05003255}
3256EXPORT_SYMBOL_GPL(ata_sas_queuecmd);