blob: 0a8cd34847915295d04867d1fd576517c91e8646 [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
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090036#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/kernel.h>
38#include <linux/blkdev.h>
39#include <linux/spinlock.h>
40#include <scsi/scsi.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070041#include <scsi/scsi_host.h>
Christoph Hellwigbeb40482006-06-10 18:01:03 +020042#include <scsi/scsi_cmnd.h>
Mike Christie85837eb2005-11-11 16:38:53 -060043#include <scsi/scsi_eh.h>
Jeff Garzik005a5a02005-10-30 23:31:48 -050044#include <scsi/scsi_device.h>
Tejun Heoa6e6ce82006-05-15 21:03:48 +090045#include <scsi/scsi_tcq.h>
Tejun Heo30afc842006-03-18 18:40:14 +090046#include <scsi/scsi_transport.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/libata.h>
Jeff Garzikb0955182005-05-12 15:45:22 -040048#include <linux/hdreg.h>
Jeff Garzik2dcb4072007-10-19 06:42:56 -040049#include <linux/uaccess.h>
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +010050#include <linux/suspend.h>
Christoph Hellwig18f0f972009-11-17 10:00:47 -050051#include <asm/unaligned.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53#include "libata.h"
54
Tejun Heo87340e92008-04-28 17:48:51 +090055#define SECTOR_SIZE 512
56#define ATA_SCSI_RBUF_SIZE 4096
57
58static DEFINE_SPINLOCK(ata_scsi_rbuf_lock);
59static u8 ata_scsi_rbuf[ATA_SCSI_RBUF_SIZE];
Jeff Garzikb0955182005-05-12 15:45:22 -040060
Tejun Heoad706992006-12-17 10:45:57 +090061typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
Tejun Heoab5b3a52006-05-31 18:27:34 +090062
Jeff Garzik2dcb4072007-10-19 06:42:56 -040063static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
Tejun Heoab5b3a52006-05-31 18:27:34 +090064 const struct scsi_device *scsidev);
Jeff Garzik2dcb4072007-10-19 06:42:56 -040065static struct ata_device *ata_scsi_find_dev(struct ata_port *ap,
Tejun Heoab5b3a52006-05-31 18:27:34 +090066 const struct scsi_device *scsidev);
Tejun Heo83c47bc2006-05-31 18:28:07 +090067static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
68 unsigned int id, unsigned int lun);
Tejun Heoab5b3a52006-05-31 18:27:34 +090069
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040071#define RW_RECOVERY_MPAGE 0x1
72#define RW_RECOVERY_MPAGE_LEN 12
73#define CACHE_MPAGE 0x8
74#define CACHE_MPAGE_LEN 20
75#define CONTROL_MPAGE 0xa
76#define CONTROL_MPAGE_LEN 12
77#define ALL_MPAGES 0x3f
78#define ALL_SUB_MPAGES 0xff
79
80
Jeff Garzik24f75682007-09-21 07:54:49 -040081static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040082 RW_RECOVERY_MPAGE,
83 RW_RECOVERY_MPAGE_LEN - 2,
Jeff Garzik24f75682007-09-21 07:54:49 -040084 (1 << 7), /* AWRE */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040085 0, /* read retry count */
86 0, 0, 0, 0,
87 0, /* write retry count */
88 0, 0, 0
89};
90
91static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
92 CACHE_MPAGE,
93 CACHE_MPAGE_LEN - 2,
94 0, /* contains WCE, needs to be 0 for logic */
95 0, 0, 0, 0, 0, 0, 0, 0, 0,
96 0, /* contains DRA, needs to be 0 for logic */
97 0, 0, 0, 0, 0, 0, 0
98};
99
100static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
101 CONTROL_MPAGE,
102 CONTROL_MPAGE_LEN - 2,
103 2, /* DSENSE=0, GLTSD=1 */
104 0, /* [QAM+QERR may be 1, see 05-359r1] */
105 0, 0, 0, 0, 0xff, 0xff,
106 0, 30 /* extended self test time, see 05-359r1 */
107};
108
Tejun Heo30afc842006-03-18 18:40:14 +0900109/*
110 * libata transport template. libata doesn't do real transport stuff.
111 * It just needs the eh_timed_out hook.
112 */
Tejun Heof3187192007-04-17 23:44:07 +0900113static struct scsi_transport_template ata_scsi_transport_template = {
Christoph Hellwig9227c332006-04-01 19:21:04 +0200114 .eh_strategy_handler = ata_scsi_error,
Tejun Heo30afc842006-03-18 18:40:14 +0900115 .eh_timed_out = ata_scsi_timed_out,
Tejun Heoccf68c32006-05-31 18:28:09 +0900116 .user_scan = ata_scsi_user_scan,
Tejun Heo30afc842006-03-18 18:40:14 +0900117};
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400120static const struct {
121 enum link_pm value;
122 const char *name;
123} link_pm_policy[] = {
124 { NOT_AVAILABLE, "max_performance" },
125 { MIN_POWER, "min_power" },
126 { MAX_PERFORMANCE, "max_performance" },
127 { MEDIUM_POWER, "medium_power" },
128};
129
Adrian Bunka2d6ed12007-10-31 00:50:23 +0100130static const char *ata_scsi_lpm_get(enum link_pm policy)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400131{
132 int i;
133
134 for (i = 0; i < ARRAY_SIZE(link_pm_policy); i++)
135 if (link_pm_policy[i].value == policy)
136 return link_pm_policy[i].name;
137
138 return NULL;
139}
140
Tony Jonesee959b02008-02-22 00:13:36 +0100141static ssize_t ata_scsi_lpm_put(struct device *dev,
142 struct device_attribute *attr,
143 const char *buf, size_t count)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400144{
Tony Jonesee959b02008-02-22 00:13:36 +0100145 struct Scsi_Host *shost = class_to_shost(dev);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400146 struct ata_port *ap = ata_shost_to_port(shost);
147 enum link_pm policy = 0;
148 int i;
149
150 /*
151 * we are skipping array location 0 on purpose - this
152 * is because a value of NOT_AVAILABLE is displayed
153 * to the user as max_performance, but when the user
154 * writes "max_performance", they actually want the
155 * value to match MAX_PERFORMANCE.
156 */
157 for (i = 1; i < ARRAY_SIZE(link_pm_policy); i++) {
158 const int len = strlen(link_pm_policy[i].name);
Matthew Garrett6a744632009-11-17 11:09:03 -0500159 if (strncmp(link_pm_policy[i].name, buf, len) == 0) {
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400160 policy = link_pm_policy[i].value;
161 break;
162 }
163 }
164 if (!policy)
165 return -EINVAL;
166
167 ata_lpm_schedule(ap, policy);
168 return count;
169}
170
171static ssize_t
Tony Jonesee959b02008-02-22 00:13:36 +0100172ata_scsi_lpm_show(struct device *dev, struct device_attribute *attr, char *buf)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400173{
Tony Jonesee959b02008-02-22 00:13:36 +0100174 struct Scsi_Host *shost = class_to_shost(dev);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400175 struct ata_port *ap = ata_shost_to_port(shost);
176 const char *policy =
177 ata_scsi_lpm_get(ap->pm_policy);
178
179 if (!policy)
180 return -EINVAL;
181
182 return snprintf(buf, 23, "%s\n", policy);
183}
Tony Jonesee959b02008-02-22 00:13:36 +0100184DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400185 ata_scsi_lpm_show, ata_scsi_lpm_put);
Tony Jonesee959b02008-02-22 00:13:36 +0100186EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400187
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200188static ssize_t ata_scsi_park_show(struct device *device,
189 struct device_attribute *attr, char *buf)
190{
191 struct scsi_device *sdev = to_scsi_device(device);
192 struct ata_port *ap;
193 struct ata_link *link;
194 struct ata_device *dev;
Elias Oltmannsa4641892008-11-03 19:01:08 +0900195 unsigned long flags, now;
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200196 unsigned int uninitialized_var(msecs);
197 int rc = 0;
198
199 ap = ata_shost_to_port(sdev->host);
200
201 spin_lock_irqsave(ap->lock, flags);
202 dev = ata_scsi_find_dev(ap, sdev);
203 if (!dev) {
204 rc = -ENODEV;
205 goto unlock;
206 }
207 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
208 rc = -EOPNOTSUPP;
209 goto unlock;
210 }
211
212 link = dev->link;
Elias Oltmannsa4641892008-11-03 19:01:08 +0900213 now = jiffies;
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200214 if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS &&
215 link->eh_context.unloaded_mask & (1 << dev->devno) &&
Elias Oltmannsa4641892008-11-03 19:01:08 +0900216 time_after(dev->unpark_deadline, now))
217 msecs = jiffies_to_msecs(dev->unpark_deadline - now);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200218 else
219 msecs = 0;
220
221unlock:
222 spin_unlock_irq(ap->lock);
223
224 return rc ? rc : snprintf(buf, 20, "%u\n", msecs);
225}
226
227static ssize_t ata_scsi_park_store(struct device *device,
228 struct device_attribute *attr,
229 const char *buf, size_t len)
230{
231 struct scsi_device *sdev = to_scsi_device(device);
232 struct ata_port *ap;
233 struct ata_device *dev;
234 long int input;
235 unsigned long flags;
236 int rc;
237
238 rc = strict_strtol(buf, 10, &input);
239 if (rc || input < -2)
240 return -EINVAL;
241 if (input > ATA_TMOUT_MAX_PARK) {
242 rc = -EOVERFLOW;
243 input = ATA_TMOUT_MAX_PARK;
244 }
245
246 ap = ata_shost_to_port(sdev->host);
247
248 spin_lock_irqsave(ap->lock, flags);
249 dev = ata_scsi_find_dev(ap, sdev);
250 if (unlikely(!dev)) {
251 rc = -ENODEV;
252 goto unlock;
253 }
254 if (dev->class != ATA_DEV_ATA) {
255 rc = -EOPNOTSUPP;
256 goto unlock;
257 }
258
259 if (input >= 0) {
260 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
261 rc = -EOPNOTSUPP;
262 goto unlock;
263 }
264
265 dev->unpark_deadline = ata_deadline(jiffies, input);
266 dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK;
267 ata_port_schedule_eh(ap);
268 complete(&ap->park_req_pending);
269 } else {
270 switch (input) {
271 case -1:
272 dev->flags &= ~ATA_DFLAG_NO_UNLOAD;
273 break;
274 case -2:
275 dev->flags |= ATA_DFLAG_NO_UNLOAD;
276 break;
277 }
278 }
279unlock:
280 spin_unlock_irqrestore(ap->lock, flags);
281
282 return rc ? rc : len;
283}
284DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR,
285 ata_scsi_park_show, ata_scsi_park_store);
286EXPORT_SYMBOL_GPL(dev_attr_unload_heads);
287
Tejun Heof0761be2008-04-28 17:16:52 +0900288static void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
289{
290 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
291
292 scsi_build_sense_buffer(0, cmd->sense_buffer, sk, asc, ascq);
293}
294
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700295static ssize_t
296ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
297 const char *buf, size_t count)
298{
299 struct Scsi_Host *shost = class_to_shost(dev);
300 struct ata_port *ap = ata_shost_to_port(shost);
301 if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
302 return ap->ops->em_store(ap, buf, count);
303 return -EINVAL;
304}
305
306static ssize_t
307ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
308 char *buf)
309{
310 struct Scsi_Host *shost = class_to_shost(dev);
311 struct ata_port *ap = ata_shost_to_port(shost);
312
313 if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
314 return ap->ops->em_show(ap, buf);
315 return -EINVAL;
316}
Vitaly Mayatskikhea7a5ed2009-05-04 15:48:45 +0200317DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700318 ata_scsi_em_message_show, ata_scsi_em_message_store);
319EXPORT_SYMBOL_GPL(dev_attr_em_message);
320
321static ssize_t
322ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
323 char *buf)
324{
325 struct Scsi_Host *shost = class_to_shost(dev);
326 struct ata_port *ap = ata_shost_to_port(shost);
327
328 return snprintf(buf, 23, "%d\n", ap->em_message_type);
329}
330DEVICE_ATTR(em_message_type, S_IRUGO,
331 ata_scsi_em_message_type_show, NULL);
332EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
333
334static ssize_t
335ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
336 char *buf)
337{
338 struct scsi_device *sdev = to_scsi_device(dev);
339 struct ata_port *ap = ata_shost_to_port(sdev->host);
340 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
341
342 if (ap->ops->sw_activity_show && (ap->flags & ATA_FLAG_SW_ACTIVITY))
343 return ap->ops->sw_activity_show(atadev, buf);
344 return -EINVAL;
345}
346
347static ssize_t
348ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
349 const char *buf, size_t count)
350{
351 struct scsi_device *sdev = to_scsi_device(dev);
352 struct ata_port *ap = ata_shost_to_port(sdev->host);
353 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
354 enum sw_activity val;
355 int rc;
356
357 if (ap->ops->sw_activity_store && (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
358 val = simple_strtoul(buf, NULL, 0);
359 switch (val) {
360 case OFF: case BLINK_ON: case BLINK_OFF:
361 rc = ap->ops->sw_activity_store(atadev, val);
362 if (!rc)
363 return count;
364 else
365 return rc;
366 }
367 }
368 return -EINVAL;
369}
Vitaly Mayatskikhea7a5ed2009-05-04 15:48:45 +0200370DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700371 ata_scsi_activity_store);
372EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
373
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200374struct device_attribute *ata_common_sdev_attrs[] = {
375 &dev_attr_unload_heads,
376 NULL
377};
378EXPORT_SYMBOL_GPL(ata_common_sdev_attrs);
379
Douglas Gilbertae006512005-10-09 09:09:35 -0400380static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
381 void (*done)(struct scsi_cmnd *))
382{
383 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
384 /* "Invalid field in cbd" */
385 done(cmd);
386}
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388/**
389 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
390 * @sdev: SCSI device for which BIOS geometry is to be determined
391 * @bdev: block device associated with @sdev
392 * @capacity: capacity of SCSI device
393 * @geom: location to which geometry will be output
394 *
395 * Generic bios head/sector/cylinder calculator
396 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
397 * mapping. Some situations may arise where the disk is not
398 * bootable if this is not used.
399 *
400 * LOCKING:
401 * Defined by the SCSI layer. We don't really care.
402 *
403 * RETURNS:
404 * Zero.
405 */
406int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
407 sector_t capacity, int geom[])
408{
409 geom[0] = 255;
410 geom[1] = 63;
411 sector_div(capacity, 255*63);
412 geom[2] = capacity;
413
414 return 0;
415}
416
Jeff Garzikb0955182005-05-12 15:45:22 -0400417/**
Tejun Heod8d91292010-05-15 20:09:34 +0200418 * ata_scsi_unlock_native_capacity - unlock native capacity
419 * @sdev: SCSI device to adjust device capacity for
420 *
421 * This function is called if a partition on @sdev extends beyond
422 * the end of the device. It requests EH to unlock HPA.
423 *
424 * LOCKING:
425 * Defined by the SCSI layer. Might sleep.
426 */
427void ata_scsi_unlock_native_capacity(struct scsi_device *sdev)
428{
429 struct ata_port *ap = ata_shost_to_port(sdev->host);
430 struct ata_device *dev;
431 unsigned long flags;
432
433 spin_lock_irqsave(ap->lock, flags);
434
435 dev = ata_scsi_find_dev(ap, sdev);
436 if (dev && dev->n_sectors < dev->n_native_sectors) {
437 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
438 dev->link->eh_info.action |= ATA_EH_RESET;
439 ata_port_schedule_eh(ap);
440 }
441
442 spin_unlock_irqrestore(ap->lock, flags);
443 ata_port_wait_eh(ap);
444}
445
446/**
Tejun Heo5924b742007-01-02 20:20:07 +0900447 * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
Randy Dunlap5eb66fe2009-01-20 16:28:59 -0800448 * @ap: target port
Tejun Heo5924b742007-01-02 20:20:07 +0900449 * @sdev: SCSI device to get identify data for
450 * @arg: User buffer area for identify data
451 *
452 * LOCKING:
453 * Defined by the SCSI layer. We don't really care.
454 *
455 * RETURNS:
456 * Zero on success, negative errno on error.
457 */
Jeff Garzik94be9a52009-01-16 10:17:09 -0500458static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
459 void __user *arg)
Tejun Heo5924b742007-01-02 20:20:07 +0900460{
Tejun Heo5924b742007-01-02 20:20:07 +0900461 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
462 u16 __user *dst = arg;
463 char buf[40];
464
465 if (!dev)
466 return -ENOMSG;
467
468 if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
469 return -EFAULT;
470
471 ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
472 if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
473 return -EFAULT;
474
475 ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
476 if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
477 return -EFAULT;
478
479 ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
480 if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
481 return -EFAULT;
482
483 return 0;
484}
485
486/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400487 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800488 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400489 * @arg: User provided data for issuing command
490 *
491 * LOCKING:
492 * Defined by the SCSI layer. We don't really care.
493 *
494 * RETURNS:
495 * Zero on success, negative errno on error.
496 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400497int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
498{
499 int rc = 0;
500 u8 scsi_cmd[MAX_COMMAND_SIZE];
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700501 u8 args[4], *argbuf = NULL, *sensebuf = NULL;
Jeff Garzikb0955182005-05-12 15:45:22 -0400502 int argsize = 0;
Mike Christie85837eb2005-11-11 16:38:53 -0600503 enum dma_data_direction data_dir;
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700504 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400505
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500506 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400507 return -EINVAL;
508
509 if (copy_from_user(args, arg, sizeof(args)))
510 return -EFAULT;
511
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700512 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
513 if (!sensebuf)
514 return -ENOMEM;
515
Jeff Garzikb0955182005-05-12 15:45:22 -0400516 memset(scsi_cmd, 0, sizeof(scsi_cmd));
517
518 if (args[3]) {
519 argsize = SECTOR_SIZE * args[3];
520 argbuf = kmalloc(argsize, GFP_KERNEL);
Jeff Raubitschek54dac832005-10-04 10:21:19 -0400521 if (argbuf == NULL) {
522 rc = -ENOMEM;
523 goto error;
524 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400525
526 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
527 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400528 block count in sector count field */
Mike Christie85837eb2005-11-11 16:38:53 -0600529 data_dir = DMA_FROM_DEVICE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400530 } else {
531 scsi_cmd[1] = (3 << 1); /* Non-data */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700532 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Mike Christie85837eb2005-11-11 16:38:53 -0600533 data_dir = DMA_NONE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400534 }
535
536 scsi_cmd[0] = ATA_16;
537
538 scsi_cmd[4] = args[2];
Bartlomiej Zolnierkiewicza4f19042008-10-10 22:39:20 +0200539 if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
Jeff Garzikb0955182005-05-12 15:45:22 -0400540 scsi_cmd[6] = args[3];
541 scsi_cmd[8] = args[1];
542 scsi_cmd[10] = 0x4f;
543 scsi_cmd[12] = 0xc2;
544 } else {
545 scsi_cmd[6] = args[1];
546 }
547 scsi_cmd[14] = args[0];
548
549 /* Good values for timeout and retries? Values below
550 from scsi_ioctl_send_command() for default case... */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700551 cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900552 sensebuf, (10*HZ), 5, 0, NULL);
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700553
554 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
555 u8 *desc = sensebuf + 8;
556 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
557
558 /* If we set cc then ATA pass-through will cause a
559 * check condition even if no error. Filter that. */
560 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
561 struct scsi_sense_hdr sshdr;
562 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400563 &sshdr);
564 if (sshdr.sense_key == 0 &&
565 sshdr.asc == 0 && sshdr.ascq == 0)
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700566 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
567 }
568
569 /* Send userspace a few ATA registers (same as drivers/ide) */
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400570 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
571 desc[0] == 0x09) { /* code is "ATA Descriptor" */
572 args[0] = desc[13]; /* status */
573 args[1] = desc[3]; /* error */
574 args[2] = desc[5]; /* sector count (0:7) */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700575 if (copy_to_user(arg, args, sizeof(args)))
576 rc = -EFAULT;
577 }
578 }
579
580
581 if (cmd_result) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400582 rc = -EIO;
583 goto error;
584 }
585
Jeff Garzikb0955182005-05-12 15:45:22 -0400586 if ((argbuf)
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500587 && copy_to_user(arg + sizeof(args), argbuf, argsize))
Jeff Garzikb0955182005-05-12 15:45:22 -0400588 rc = -EFAULT;
589error:
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700590 kfree(sensebuf);
Jesper Juhl8f760782006-06-27 02:55:06 -0700591 kfree(argbuf);
Jeff Garzikb0955182005-05-12 15:45:22 -0400592 return rc;
593}
594
595/**
596 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800597 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400598 * @arg: User provided data for issuing command
599 *
600 * LOCKING:
601 * Defined by the SCSI layer. We don't really care.
602 *
603 * RETURNS:
604 * Zero on success, negative errno on error.
605 */
606int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
607{
608 int rc = 0;
609 u8 scsi_cmd[MAX_COMMAND_SIZE];
David Milburnaf068bd2007-01-30 00:59:15 -0800610 u8 args[7], *sensebuf = NULL;
611 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400612
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500613 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400614 return -EINVAL;
615
616 if (copy_from_user(args, arg, sizeof(args)))
617 return -EFAULT;
618
David Milburnaf068bd2007-01-30 00:59:15 -0800619 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
620 if (!sensebuf)
621 return -ENOMEM;
622
Jeff Garzikb0955182005-05-12 15:45:22 -0400623 memset(scsi_cmd, 0, sizeof(scsi_cmd));
624 scsi_cmd[0] = ATA_16;
625 scsi_cmd[1] = (3 << 1); /* Non-data */
David Milburnaf068bd2007-01-30 00:59:15 -0800626 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Jeff Garzikb0955182005-05-12 15:45:22 -0400627 scsi_cmd[4] = args[1];
628 scsi_cmd[6] = args[2];
629 scsi_cmd[8] = args[3];
630 scsi_cmd[10] = args[4];
631 scsi_cmd[12] = args[5];
Mark Lord277239f2007-03-30 17:45:52 -0400632 scsi_cmd[13] = args[6] & 0x4f;
Jeff Garzikb0955182005-05-12 15:45:22 -0400633 scsi_cmd[14] = args[0];
634
Jeff Garzikb0955182005-05-12 15:45:22 -0400635 /* Good values for timeout and retries? Values below
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500636 from scsi_ioctl_send_command() for default case... */
David Milburnaf068bd2007-01-30 00:59:15 -0800637 cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900638 sensebuf, (10*HZ), 5, 0, NULL);
Jeff Garzikb0955182005-05-12 15:45:22 -0400639
David Milburnaf068bd2007-01-30 00:59:15 -0800640 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
641 u8 *desc = sensebuf + 8;
642 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
643
644 /* If we set cc then ATA pass-through will cause a
645 * check condition even if no error. Filter that. */
646 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
647 struct scsi_sense_hdr sshdr;
648 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
649 &sshdr);
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400650 if (sshdr.sense_key == 0 &&
651 sshdr.asc == 0 && sshdr.ascq == 0)
David Milburnaf068bd2007-01-30 00:59:15 -0800652 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
653 }
654
655 /* Send userspace ATA registers */
656 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
657 desc[0] == 0x09) {/* code is "ATA Descriptor" */
658 args[0] = desc[13]; /* status */
659 args[1] = desc[3]; /* error */
660 args[2] = desc[5]; /* sector count (0:7) */
661 args[3] = desc[7]; /* lbal */
662 args[4] = desc[9]; /* lbam */
663 args[5] = desc[11]; /* lbah */
664 args[6] = desc[12]; /* select */
665 if (copy_to_user(arg, args, sizeof(args)))
666 rc = -EFAULT;
667 }
668 }
669
670 if (cmd_result) {
671 rc = -EIO;
672 goto error;
673 }
674
675 error:
676 kfree(sensebuf);
Jeff Garzikb0955182005-05-12 15:45:22 -0400677 return rc;
678}
679
Alan Coxe3cf95d2009-04-09 17:31:17 +0100680static int ata_ioc32(struct ata_port *ap)
681{
682 if (ap->flags & ATA_FLAG_PIO_DMA)
683 return 1;
684 if (ap->pflags & ATA_PFLAG_PIO32)
685 return 1;
686 return 0;
687}
688
Jeff Garzik94be9a52009-01-16 10:17:09 -0500689int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
690 int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 int val = -EINVAL, rc = -EINVAL;
Alan Coxe3cf95d2009-04-09 17:31:17 +0100693 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 switch (cmd) {
696 case ATA_IOC_GET_IO32:
Alan Coxe3cf95d2009-04-09 17:31:17 +0100697 spin_lock_irqsave(ap->lock, flags);
698 val = ata_ioc32(ap);
699 spin_unlock_irqrestore(ap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 if (copy_to_user(arg, &val, 1))
701 return -EFAULT;
702 return 0;
703
704 case ATA_IOC_SET_IO32:
705 val = (unsigned long) arg;
Alan Coxe3cf95d2009-04-09 17:31:17 +0100706 rc = 0;
707 spin_lock_irqsave(ap->lock, flags);
708 if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
709 if (val)
710 ap->pflags |= ATA_PFLAG_PIO32;
711 else
712 ap->pflags &= ~ATA_PFLAG_PIO32;
713 } else {
714 if (val != ata_ioc32(ap))
715 rc = -EINVAL;
716 }
717 spin_unlock_irqrestore(ap->lock, flags);
718 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Tejun Heo5924b742007-01-02 20:20:07 +0900720 case HDIO_GET_IDENTITY:
Jeff Garzik94be9a52009-01-16 10:17:09 -0500721 return ata_get_identity(ap, scsidev, arg);
Tejun Heo5924b742007-01-02 20:20:07 +0900722
Jeff Garzikb0955182005-05-12 15:45:22 -0400723 case HDIO_DRIVE_CMD:
724 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
725 return -EACCES;
726 return ata_cmd_ioctl(scsidev, arg);
727
728 case HDIO_DRIVE_TASK:
729 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
730 return -EACCES;
731 return ata_task_ioctl(scsidev, arg);
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 default:
734 rc = -ENOTTY;
735 break;
736 }
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 return rc;
739}
Jeff Garzik94be9a52009-01-16 10:17:09 -0500740EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
741
742int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
743{
744 return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
745 scsidev, cmd, arg);
746}
747EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
749/**
750 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 * @dev: ATA device to which the new command is attached
752 * @cmd: SCSI command that originated this ATA command
753 * @done: SCSI command completion function
754 *
755 * Obtain a reference to an unused ata_queued_cmd structure,
756 * which is the basic libata structure representing a single
757 * ATA command sent to the hardware.
758 *
759 * If a command was available, fill in the SCSI-specific
760 * portions of the structure with information on the
761 * current command.
762 *
763 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400764 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 *
766 * RETURNS:
767 * Command allocated, or %NULL if none available.
768 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100769static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
770 struct scsi_cmnd *cmd,
771 void (*done)(struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772{
773 struct ata_queued_cmd *qc;
774
Tejun Heo8a8bc222008-11-10 14:48:21 +0900775 qc = ata_qc_new_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 if (qc) {
777 qc->scsicmd = cmd;
778 qc->scsidone = done;
779
Tejun Heoff2aeb12007-12-05 16:43:11 +0900780 qc->sg = scsi_sglist(cmd);
Boaz Harrosh71201652007-09-18 17:48:50 +0200781 qc->n_elem = scsi_sg_count(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 } else {
783 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
784 done(cmd);
785 }
786
787 return qc;
788}
789
Tejun Heoaacda372008-03-18 17:47:43 +0900790static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
791{
792 struct scsi_cmnd *scmd = qc->scsicmd;
793
794 qc->extrabytes = scmd->request->extra_len;
795 qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
796}
797
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400799 * ata_dump_status - user friendly display of error info
800 * @id: id of the port in question
801 * @tf: ptr to filled out taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 *
Jeff Garzikb0955182005-05-12 15:45:22 -0400803 * Decode and dump the ATA error/status registers for the user so
804 * that they have some idea what really happened at the non
805 * make-believe layer.
806 *
807 * LOCKING:
808 * inherited from caller
809 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100810static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
Jeff Garzikb0955182005-05-12 15:45:22 -0400811{
812 u8 stat = tf->command, err = tf->feature;
813
814 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
815 if (stat & ATA_BUSY) {
816 printk("Busy }\n"); /* Data is not valid in this case */
817 } else {
818 if (stat & 0x40) printk("DriveReady ");
819 if (stat & 0x20) printk("DeviceFault ");
820 if (stat & 0x10) printk("SeekComplete ");
821 if (stat & 0x08) printk("DataRequest ");
822 if (stat & 0x04) printk("CorrectedError ");
823 if (stat & 0x02) printk("Index ");
824 if (stat & 0x01) printk("Error ");
825 printk("}\n");
826
827 if (err) {
828 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
829 if (err & 0x04) printk("DriveStatusError ");
830 if (err & 0x80) {
831 if (err & 0x04) printk("BadCRC ");
832 else printk("Sector ");
833 }
834 if (err & 0x40) printk("UncorrectableError ");
835 if (err & 0x10) printk("SectorIdNotFound ");
836 if (err & 0x02) printk("TrackZeroNotFound ");
837 if (err & 0x01) printk("AddrMarkNotFound ");
838 printk("}\n");
839 }
840 }
841}
842
843/**
844 * ata_to_sense_error - convert ATA error to SCSI error
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800845 * @id: ATA device number
Jeff Garzikb0955182005-05-12 15:45:22 -0400846 * @drv_stat: value contained in ATA status register
847 * @drv_err: value contained in ATA error register
848 * @sk: the sense key we'll fill out
849 * @asc: the additional sense code we'll fill out
850 * @ascq: the additional sense code qualifier we'll fill out
Tejun Heo246619d2006-05-15 20:58:16 +0900851 * @verbose: be verbose
Jeff Garzikb0955182005-05-12 15:45:22 -0400852 *
853 * Converts an ATA error into a SCSI error. Fill out pointers to
854 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
855 * format sense blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 *
857 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400858 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100860static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
861 u8 *asc, u8 *ascq, int verbose)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Jeff Garzikb0955182005-05-12 15:45:22 -0400863 int i;
Jeff Garzikffe75ef2005-10-09 10:40:44 -0400864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 /* Based on the 3ware driver translation table */
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100866 static const unsigned char sense_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 /* BBD|ECC|ID|MAR */
868 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
869 /* BBD|ECC|ID */
870 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
871 /* ECC|MC|MARK */
872 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
873 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
874 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
875 /* MC|ID|ABRT|TRK0|MARK */
876 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
877 /* MCR|MARK */
878 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
879 /* Bad address mark */
880 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
881 /* TRK0 */
882 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
883 /* Abort & !ICRC */
884 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
885 /* Media change request */
886 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
887 /* SRV */
888 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
889 /* Media change */
890 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
891 /* ECC */
892 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
893 /* BBD - block marked bad */
894 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
895 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
896 };
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100897 static const unsigned char stat_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /* Must be first because BUSY means no other bits valid */
899 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
900 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
901 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
902 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
903 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
904 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 /*
907 * Is this an error we can process/parse
908 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400909 if (drv_stat & ATA_BUSY) {
910 drv_err = 0; /* Ignore the err bits, they're invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Jeff Garzikb0955182005-05-12 15:45:22 -0400913 if (drv_err) {
914 /* Look for drv_err */
915 for (i = 0; sense_table[i][0] != 0xFF; i++) {
916 /* Look for best matches first */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500917 if ((sense_table[i][0] & drv_err) ==
Jeff Garzikb0955182005-05-12 15:45:22 -0400918 sense_table[i][0]) {
919 *sk = sense_table[i][1];
920 *asc = sense_table[i][2];
921 *ascq = sense_table[i][3];
922 goto translate_done;
923 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400925 /* No immediate match */
Tejun Heo246619d2006-05-15 20:58:16 +0900926 if (verbose)
927 printk(KERN_WARNING "ata%u: no sense translation for "
928 "error 0x%02x\n", id, drv_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 }
930
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 /* Fall back to interpreting status bits */
Jeff Garzikb0955182005-05-12 15:45:22 -0400932 for (i = 0; stat_table[i][0] != 0xFF; i++) {
933 if (stat_table[i][0] & drv_stat) {
934 *sk = stat_table[i][1];
935 *asc = stat_table[i][2];
936 *ascq = stat_table[i][3];
937 goto translate_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400940 /* No error? Undecoded? */
Tejun Heo246619d2006-05-15 20:58:16 +0900941 if (verbose)
942 printk(KERN_WARNING "ata%u: no sense translation for "
943 "status: 0x%02x\n", id, drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Alan Cox2d202022006-03-21 15:53:46 +0000945 /* We need a sensible error return here, which is tricky, and one
946 that won't cause people to do things like return a disk wrongly */
947 *sk = ABORTED_COMMAND;
948 *asc = 0x00;
949 *ascq = 0x00;
Jeff Garzikb0955182005-05-12 15:45:22 -0400950
951 translate_done:
Tejun Heo246619d2006-05-15 20:58:16 +0900952 if (verbose)
953 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x "
954 "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
955 id, drv_stat, drv_err, *sk, *asc, *ascq);
Jeff Garzikb0955182005-05-12 15:45:22 -0400956 return;
957}
958
959/*
Tejun Heo750426a2006-11-14 22:37:35 +0900960 * ata_gen_passthru_sense - Generate check condition sense block.
Jeff Garzikb0955182005-05-12 15:45:22 -0400961 * @qc: Command that completed.
962 *
963 * This function is specific to the ATA descriptor format sense
964 * block specified for the ATA pass through commands. Regardless
965 * of whether the command errored or not, return a sense
966 * block. Copy all controller registers into the sense
967 * block. Clear sense key, ASC & ASCQ if there is no error.
968 *
969 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +0900970 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -0400971 */
Tejun Heo750426a2006-11-14 22:37:35 +0900972static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -0400973{
974 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +0900975 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -0400976 unsigned char *sb = cmd->sense_buffer;
977 unsigned char *desc = sb + 8;
Tejun Heo246619d2006-05-15 20:58:16 +0900978 int verbose = qc->ap->ops->error_handler == NULL;
Jeff Garzikb0955182005-05-12 15:45:22 -0400979
980 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
981
Jeff Garzik0e5dec42005-10-06 09:40:20 -0400982 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -0400983
984 /*
Jeff Garzikb0955182005-05-12 15:45:22 -0400985 * Use ata_to_sense_error() to map status register bits
986 * onto sense key, asc & ascq.
987 */
Tejun Heo058e55e2006-04-02 18:51:53 +0900988 if (qc->err_mask ||
989 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo44877b42007-02-21 01:06:51 +0900990 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
Tejun Heo246619d2006-05-15 20:58:16 +0900991 &sb[1], &sb[2], &sb[3], verbose);
Jeff Garzikb0955182005-05-12 15:45:22 -0400992 sb[1] &= 0x0f;
993 }
994
995 /*
996 * Sense data is current and format is descriptor.
997 */
998 sb[0] = 0x72;
999
1000 desc[0] = 0x09;
1001
Tejun Heof38621b2006-11-14 22:37:35 +09001002 /* set length of additional sense data */
1003 sb[7] = 14;
1004 desc[1] = 12;
Jeff Garzikb0955182005-05-12 15:45:22 -04001005
1006 /*
1007 * Copy registers into sense buffer.
1008 */
1009 desc[2] = 0x00;
1010 desc[3] = tf->feature; /* == error reg */
1011 desc[5] = tf->nsect;
1012 desc[7] = tf->lbal;
1013 desc[9] = tf->lbam;
1014 desc[11] = tf->lbah;
1015 desc[12] = tf->device;
1016 desc[13] = tf->command; /* == status reg */
1017
1018 /*
1019 * Fill in Extend bit, and the high order bytes
1020 * if applicable.
1021 */
1022 if (tf->flags & ATA_TFLAG_LBA48) {
1023 desc[2] |= 0x01;
1024 desc[4] = tf->hob_nsect;
1025 desc[6] = tf->hob_lbal;
1026 desc[8] = tf->hob_lbam;
1027 desc[10] = tf->hob_lbah;
1028 }
1029}
1030
1031/**
Tejun Heo750426a2006-11-14 22:37:35 +09001032 * ata_gen_ata_sense - generate a SCSI fixed sense block
Jeff Garzikb0955182005-05-12 15:45:22 -04001033 * @qc: Command that we are erroring out
1034 *
Tejun Heod25614b2006-11-14 22:37:35 +09001035 * Generate sense block for a failed ATA command @qc. Descriptor
1036 * format is used to accomodate LBA48 block address.
Jeff Garzikb0955182005-05-12 15:45:22 -04001037 *
1038 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +09001039 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -04001040 */
Tejun Heo750426a2006-11-14 22:37:35 +09001041static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04001042{
Tejun Heod25614b2006-11-14 22:37:35 +09001043 struct ata_device *dev = qc->dev;
Jeff Garzikb0955182005-05-12 15:45:22 -04001044 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +09001045 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -04001046 unsigned char *sb = cmd->sense_buffer;
Tejun Heod25614b2006-11-14 22:37:35 +09001047 unsigned char *desc = sb + 8;
Tejun Heo246619d2006-05-15 20:58:16 +09001048 int verbose = qc->ap->ops->error_handler == NULL;
Tejun Heod25614b2006-11-14 22:37:35 +09001049 u64 block;
Jeff Garzikb0955182005-05-12 15:45:22 -04001050
1051 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1052
Jeff Garzik0e5dec42005-10-06 09:40:20 -04001053 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -04001054
Tejun Heod25614b2006-11-14 22:37:35 +09001055 /* sense data is current and format is descriptor */
1056 sb[0] = 0x72;
1057
1058 /* Use ata_to_sense_error() to map status register bits
Jeff Garzikb0955182005-05-12 15:45:22 -04001059 * onto sense key, asc & ascq.
1060 */
Tejun Heo058e55e2006-04-02 18:51:53 +09001061 if (qc->err_mask ||
1062 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001063 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
Tejun Heod25614b2006-11-14 22:37:35 +09001064 &sb[1], &sb[2], &sb[3], verbose);
1065 sb[1] &= 0x0f;
Jeff Garzikb0955182005-05-12 15:45:22 -04001066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Tejun Heod25614b2006-11-14 22:37:35 +09001068 block = ata_tf_read_block(&qc->result_tf, dev);
Jeff Garzik0274aa22005-06-22 13:50:56 -04001069
Tejun Heod25614b2006-11-14 22:37:35 +09001070 /* information sense data descriptor */
1071 sb[7] = 12;
1072 desc[0] = 0x00;
1073 desc[1] = 10;
Jeff Garzika7dac442005-10-30 04:44:42 -05001074
Tejun Heod25614b2006-11-14 22:37:35 +09001075 desc[2] |= 0x80; /* valid */
1076 desc[6] = block >> 40;
1077 desc[7] = block >> 32;
1078 desc[8] = block >> 24;
1079 desc[9] = block >> 16;
1080 desc[10] = block >> 8;
1081 desc[11] = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082}
1083
Brian Kinga6cce2a2006-03-17 17:04:15 -06001084static void ata_scsi_sdev_config(struct scsi_device *sdev)
1085{
1086 sdev->use_10_for_rw = 1;
1087 sdev->use_10_for_ms = 1;
Tejun Heo31cc23b2007-09-23 13:14:12 +09001088
1089 /* Schedule policy is determined by ->qc_defer() callback and
1090 * it needs to see every deferred qc. Set dev_blocked to 1 to
1091 * prevent SCSI midlayer from automatically deferring
1092 * requests.
1093 */
1094 sdev->max_device_blocked = 1;
Brian Kinga6cce2a2006-03-17 17:04:15 -06001095}
1096
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001097/**
1098 * atapi_drain_needed - Check whether data transfer may overflow
Randy Dunlap73fd8b62008-02-19 13:43:21 -08001099 * @rq: request to be checked
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001100 *
1101 * ATAPI commands which transfer variable length data to host
1102 * might overflow due to application error or hardare bug. This
1103 * function checks whether overflow should be drained and ignored
1104 * for @request.
1105 *
1106 * LOCKING:
1107 * None.
1108 *
1109 * RETURNS:
1110 * 1 if ; otherwise, 0.
1111 */
1112static int atapi_drain_needed(struct request *rq)
1113{
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001114 if (likely(rq->cmd_type != REQ_TYPE_BLOCK_PC))
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001115 return 0;
1116
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001117 if (!blk_rq_bytes(rq) || (rq->cmd_flags & REQ_WRITE))
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001118 return 0;
1119
1120 return atapi_cmd_type(rq->cmd[0]) == ATAPI_MISC;
1121}
1122
1123static int ata_scsi_dev_config(struct scsi_device *sdev,
1124 struct ata_device *dev)
Brian Kinga6cce2a2006-03-17 17:04:15 -06001125{
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02001126 if (!ata_id_has_unload(dev->id))
1127 dev->flags |= ATA_DFLAG_NO_UNLOAD;
1128
Tejun Heo914ed352006-11-01 18:39:55 +09001129 /* configure max sectors */
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05001130 blk_queue_max_hw_sectors(sdev->request_queue, dev->max_sectors);
Brian Kinga6cce2a2006-03-17 17:04:15 -06001131
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001132 if (dev->class == ATA_DEV_ATAPI) {
1133 struct request_queue *q = sdev->request_queue;
1134 void *buf;
1135
Tejun Heoe3790c72008-03-04 11:18:17 +01001136 /* set the min alignment and padding */
James Bottomleyd0ad3bc2008-01-22 13:43:48 -06001137 blk_queue_update_dma_alignment(sdev->request_queue,
1138 ATA_DMA_PAD_SZ - 1);
FUJITA Tomonori27f82212008-07-04 09:30:03 +02001139 blk_queue_update_dma_pad(sdev->request_queue,
1140 ATA_DMA_PAD_SZ - 1);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001141
1142 /* configure draining */
1143 buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
1144 if (!buf) {
1145 ata_dev_printk(dev, KERN_ERR,
1146 "drain buffer allocation failed\n");
1147 return -ENOMEM;
1148 }
1149
1150 blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
1151 } else {
James Bottomleyd0ad3bc2008-01-22 13:43:48 -06001152 /* ATA devices must be sector aligned */
1153 blk_queue_update_dma_alignment(sdev->request_queue,
1154 ATA_SECT_SIZE - 1);
Tejun Heod8cf5382008-01-15 08:46:59 +09001155 sdev->manage_start_stop = 1;
James Bottomleydde20202008-02-19 11:36:56 +01001156 }
Tejun Heod8cf5382008-01-15 08:46:59 +09001157
Jeff Garzikf26792d2007-10-29 17:18:39 -04001158 if (dev->flags & ATA_DFLAG_AN)
1159 set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
1160
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001161 if (dev->flags & ATA_DFLAG_NCQ) {
1162 int depth;
1163
1164 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
1165 depth = min(ATA_MAX_QUEUE - 1, depth);
Tejun Heo8a8bc222008-11-10 14:48:21 +09001166 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001167 }
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001168
1169 return 0;
Brian Kinga6cce2a2006-03-17 17:04:15 -06001170}
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172/**
1173 * ata_scsi_slave_config - Set SCSI device attributes
1174 * @sdev: SCSI device to examine
1175 *
1176 * This is called before we actually start reading
1177 * and writing to the device, to configure certain
1178 * SCSI mid-layer behaviors.
1179 *
1180 * LOCKING:
1181 * Defined by SCSI layer. We don't really care.
1182 */
1183
1184int ata_scsi_slave_config(struct scsi_device *sdev)
1185{
Tejun Heo31534362006-05-31 18:27:36 +09001186 struct ata_port *ap = ata_shost_to_port(sdev->host);
1187 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001188 int rc = 0;
Tejun Heo31534362006-05-31 18:27:36 +09001189
Brian Kinga6cce2a2006-03-17 17:04:15 -06001190 ata_scsi_sdev_config(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Tejun Heo31534362006-05-31 18:27:36 +09001192 if (dev)
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001193 rc = ata_scsi_dev_config(sdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001195 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196}
1197
1198/**
Tejun Heo83c47bc2006-05-31 18:28:07 +09001199 * ata_scsi_slave_destroy - SCSI device is about to be destroyed
1200 * @sdev: SCSI device to be destroyed
1201 *
1202 * @sdev is about to be destroyed for hot/warm unplugging. If
1203 * this unplugging was initiated by libata as indicated by NULL
1204 * dev->sdev, this function doesn't have to do anything.
1205 * Otherwise, SCSI layer initiated warm-unplug is in progress.
1206 * Clear dev->sdev, schedule the device for ATA detach and invoke
1207 * EH.
1208 *
1209 * LOCKING:
1210 * Defined by SCSI layer. We don't really care.
1211 */
1212void ata_scsi_slave_destroy(struct scsi_device *sdev)
1213{
1214 struct ata_port *ap = ata_shost_to_port(sdev->host);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001215 struct request_queue *q = sdev->request_queue;
Tejun Heo83c47bc2006-05-31 18:28:07 +09001216 unsigned long flags;
1217 struct ata_device *dev;
1218
1219 if (!ap->ops->error_handler)
1220 return;
1221
Jeff Garzikba6a1302006-06-22 23:46:10 -04001222 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09001223 dev = __ata_scsi_find_dev(ap, sdev);
1224 if (dev && dev->sdev) {
1225 /* SCSI device already in CANCEL state, no need to offline it */
1226 dev->sdev = NULL;
1227 dev->flags |= ATA_DFLAG_DETACH;
1228 ata_port_schedule_eh(ap);
1229 }
Jeff Garzikba6a1302006-06-22 23:46:10 -04001230 spin_unlock_irqrestore(ap->lock, flags);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001231
1232 kfree(q->dma_drain_buffer);
1233 q->dma_drain_buffer = NULL;
1234 q->dma_drain_size = 0;
Tejun Heo83c47bc2006-05-31 18:28:07 +09001235}
1236
1237/**
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001238 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
1239 * @sdev: SCSI device to configure queue depth for
1240 * @queue_depth: new queue depth
Mike Christiee881a172009-10-15 17:46:39 -07001241 * @reason: calling context
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001242 *
1243 * This is libata standard hostt->change_queue_depth callback.
1244 * SCSI will call into this callback when user tries to set queue
1245 * depth via sysfs.
1246 *
1247 * LOCKING:
1248 * SCSI layer (we don't care)
1249 *
1250 * RETURNS:
1251 * Newly configured queue depth.
1252 */
Mike Christiee881a172009-10-15 17:46:39 -07001253int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth,
1254 int reason)
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001255{
1256 struct ata_port *ap = ata_shost_to_port(sdev->host);
1257 struct ata_device *dev;
Tejun Heo360f6542006-09-30 19:45:00 +09001258 unsigned long flags;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001259
Mike Christiee881a172009-10-15 17:46:39 -07001260 if (reason != SCSI_QDEPTH_DEFAULT)
1261 return -EOPNOTSUPP;
1262
Tejun Heoc3c70c42007-02-20 23:31:22 +09001263 if (queue_depth < 1 || queue_depth == sdev->queue_depth)
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001264 return sdev->queue_depth;
1265
1266 dev = ata_scsi_find_dev(ap, sdev);
1267 if (!dev || !ata_dev_enabled(dev))
1268 return sdev->queue_depth;
1269
Tejun Heoc3c70c42007-02-20 23:31:22 +09001270 /* NCQ enabled? */
Tejun Heo360f6542006-09-30 19:45:00 +09001271 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc3c70c42007-02-20 23:31:22 +09001272 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1273 if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
Tejun Heo360f6542006-09-30 19:45:00 +09001274 dev->flags |= ATA_DFLAG_NCQ_OFF;
Tejun Heoc3c70c42007-02-20 23:31:22 +09001275 queue_depth = 1;
1276 }
Tejun Heo360f6542006-09-30 19:45:00 +09001277 spin_unlock_irqrestore(ap->lock, flags);
1278
Tejun Heoc3c70c42007-02-20 23:31:22 +09001279 /* limit and apply queue depth */
1280 queue_depth = min(queue_depth, sdev->host->can_queue);
1281 queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
1282 queue_depth = min(queue_depth, ATA_MAX_QUEUE - 1);
1283
1284 if (sdev->queue_depth == queue_depth)
1285 return -EINVAL;
1286
1287 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, queue_depth);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001288 return queue_depth;
1289}
1290
1291/**
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001292 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
1293 * @qc: Storage for translated ATA taskfile
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001294 *
1295 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
1296 * (to start). Perhaps these commands should be preceded by
1297 * CHECK POWER MODE to see what power mode the device is already in.
1298 * [See SAT revision 5 at www.t10.org]
1299 *
1300 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001301 * spin_lock_irqsave(host lock)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001302 *
1303 * RETURNS:
1304 * Zero on success, non-zero on error.
1305 */
Tejun Heoad706992006-12-17 10:45:57 +09001306static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001307{
Tejun Heo542b1442006-12-17 10:45:08 +09001308 struct scsi_cmnd *scmd = qc->scsicmd;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001309 struct ata_taskfile *tf = &qc->tf;
Tejun Heoad706992006-12-17 10:45:57 +09001310 const u8 *cdb = scmd->cmnd;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001311
Tejun Heo2e5704f2006-12-17 10:46:33 +09001312 if (scmd->cmd_len < 5)
1313 goto invalid_fld;
1314
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001315 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1316 tf->protocol = ATA_PROT_NODATA;
Tejun Heo542b1442006-12-17 10:45:08 +09001317 if (cdb[1] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001318 ; /* ignore IMMED bit, violates sat-r05 */
1319 }
Tejun Heo542b1442006-12-17 10:45:08 +09001320 if (cdb[4] & 0x2)
Douglas Gilbertae006512005-10-09 09:09:35 -04001321 goto invalid_fld; /* LOEJ bit set not supported */
Tejun Heo542b1442006-12-17 10:45:08 +09001322 if (((cdb[4] >> 4) & 0xf) != 0)
Douglas Gilbertae006512005-10-09 09:09:35 -04001323 goto invalid_fld; /* power conditions not supported */
Tejun Heoe31e8532007-09-23 13:14:13 +09001324
Tejun Heo542b1442006-12-17 10:45:08 +09001325 if (cdb[4] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001326 tf->nsect = 1; /* 1 sector, lba=0 */
Albert Lee9d5b1302005-10-04 08:48:17 -04001327
1328 if (qc->dev->flags & ATA_DFLAG_LBA) {
Tejun Heoc44078c2006-05-15 20:57:21 +09001329 tf->flags |= ATA_TFLAG_LBA;
Albert Lee9d5b1302005-10-04 08:48:17 -04001330
1331 tf->lbah = 0x0;
1332 tf->lbam = 0x0;
1333 tf->lbal = 0x0;
1334 tf->device |= ATA_LBA;
1335 } else {
1336 /* CHS */
1337 tf->lbal = 0x1; /* sect */
1338 tf->lbam = 0x0; /* cyl low */
1339 tf->lbah = 0x0; /* cyl high */
1340 }
1341
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001342 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
Tejun Heo920a4b12007-05-04 21:28:48 +02001343 } else {
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001344 /* Some odd clown BIOSen issue spindown on power off (ACPI S4
1345 * or S5) causing some drives to spin up and down again.
1346 */
1347 if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
1348 system_state == SYSTEM_POWER_OFF)
1349 goto skip;
1350
1351 if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
1352 system_entering_hibernation())
1353 goto skip;
1354
Robert Hancock78981a72007-01-30 00:59:18 -08001355 /* Issue ATA STANDBY IMMEDIATE command */
1356 tf->command = ATA_CMD_STANDBYNOW1;
Tejun Heo920a4b12007-05-04 21:28:48 +02001357 }
Robert Hancock78981a72007-01-30 00:59:18 -08001358
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001359 /*
1360 * Standby and Idle condition timers could be implemented but that
1361 * would require libata to implement the Power condition mode page
1362 * and allow the user to change it. Changing mode pages requires
1363 * MODE SELECT to be implemented.
1364 */
1365
1366 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001367
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001368 invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +09001369 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001370 /* "Invalid field in cbd" */
1371 return 1;
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001372 skip:
1373 scmd->result = SAM_STAT_GOOD;
1374 return 1;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001375}
1376
1377
1378/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
1380 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 *
1382 * Sets up an ATA taskfile to issue FLUSH CACHE or
1383 * FLUSH CACHE EXT.
1384 *
1385 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001386 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387 *
1388 * RETURNS:
1389 * Zero on success, non-zero on error.
1390 */
Tejun Heoad706992006-12-17 10:45:57 +09001391static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001392{
1393 struct ata_taskfile *tf = &qc->tf;
1394
1395 tf->flags |= ATA_TFLAG_DEVICE;
1396 tf->protocol = ATA_PROT_NODATA;
1397
Tejun Heo6fc49ad2006-11-11 20:10:45 +09001398 if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399 tf->command = ATA_CMD_FLUSH_EXT;
1400 else
1401 tf->command = ATA_CMD_FLUSH;
1402
Tejun Heob666da32007-10-26 15:53:59 +09001403 /* flush is critical for IO integrity, consider it an IO command */
1404 qc->flags |= ATA_QCFLAG_IO;
1405
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406 return 0;
1407}
1408
1409/**
Albert Lee3aef5232005-10-04 08:47:43 -04001410 * scsi_6_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001411 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001412 *
1413 * Calculate LBA and transfer length for 6-byte commands.
1414 *
1415 * RETURNS:
1416 * @plba: the LBA
1417 * @plen: the transfer length
1418 */
Tejun Heo542b1442006-12-17 10:45:08 +09001419static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001420{
1421 u64 lba = 0;
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001422 u32 len;
Albert Lee3aef5232005-10-04 08:47:43 -04001423
1424 VPRINTK("six-byte command\n");
1425
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001426 lba |= ((u64)(cdb[1] & 0x1f)) << 16;
Tejun Heo542b1442006-12-17 10:45:08 +09001427 lba |= ((u64)cdb[2]) << 8;
1428 lba |= ((u64)cdb[3]);
Albert Lee3aef5232005-10-04 08:47:43 -04001429
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001430 len = cdb[4];
Albert Lee3aef5232005-10-04 08:47:43 -04001431
1432 *plba = lba;
1433 *plen = len;
1434}
1435
1436/**
1437 * scsi_10_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001438 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001439 *
1440 * Calculate LBA and transfer length for 10-byte commands.
1441 *
1442 * RETURNS:
1443 * @plba: the LBA
1444 * @plen: the transfer length
1445 */
Tejun Heo542b1442006-12-17 10:45:08 +09001446static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001447{
1448 u64 lba = 0;
1449 u32 len = 0;
1450
1451 VPRINTK("ten-byte command\n");
1452
Tejun Heo542b1442006-12-17 10:45:08 +09001453 lba |= ((u64)cdb[2]) << 24;
1454 lba |= ((u64)cdb[3]) << 16;
1455 lba |= ((u64)cdb[4]) << 8;
1456 lba |= ((u64)cdb[5]);
Albert Lee3aef5232005-10-04 08:47:43 -04001457
Tejun Heo542b1442006-12-17 10:45:08 +09001458 len |= ((u32)cdb[7]) << 8;
1459 len |= ((u32)cdb[8]);
Albert Lee3aef5232005-10-04 08:47:43 -04001460
1461 *plba = lba;
1462 *plen = len;
1463}
1464
1465/**
1466 * scsi_16_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001467 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001468 *
1469 * Calculate LBA and transfer length for 16-byte commands.
1470 *
1471 * RETURNS:
1472 * @plba: the LBA
1473 * @plen: the transfer length
1474 */
Tejun Heo542b1442006-12-17 10:45:08 +09001475static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001476{
1477 u64 lba = 0;
1478 u32 len = 0;
1479
1480 VPRINTK("sixteen-byte command\n");
1481
Tejun Heo542b1442006-12-17 10:45:08 +09001482 lba |= ((u64)cdb[2]) << 56;
1483 lba |= ((u64)cdb[3]) << 48;
1484 lba |= ((u64)cdb[4]) << 40;
1485 lba |= ((u64)cdb[5]) << 32;
1486 lba |= ((u64)cdb[6]) << 24;
1487 lba |= ((u64)cdb[7]) << 16;
1488 lba |= ((u64)cdb[8]) << 8;
1489 lba |= ((u64)cdb[9]);
Albert Lee3aef5232005-10-04 08:47:43 -04001490
Tejun Heo542b1442006-12-17 10:45:08 +09001491 len |= ((u32)cdb[10]) << 24;
1492 len |= ((u32)cdb[11]) << 16;
1493 len |= ((u32)cdb[12]) << 8;
1494 len |= ((u32)cdb[13]);
Albert Lee3aef5232005-10-04 08:47:43 -04001495
1496 *plba = lba;
1497 *plen = len;
1498}
1499
1500/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1502 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 *
1504 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1505 *
1506 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001507 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 *
1509 * RETURNS:
1510 * Zero on success, non-zero on error.
1511 */
Tejun Heoad706992006-12-17 10:45:57 +09001512static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513{
Tejun Heo542b1442006-12-17 10:45:08 +09001514 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001515 struct ata_taskfile *tf = &qc->tf;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001516 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001517 u64 dev_sectors = qc->dev->n_sectors;
Tejun Heoad706992006-12-17 10:45:57 +09001518 const u8 *cdb = scmd->cmnd;
Albert Lee3aef5232005-10-04 08:47:43 -04001519 u64 block;
1520 u32 n_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521
1522 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1523 tf->protocol = ATA_PROT_NODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001524
Tejun Heo2e5704f2006-12-17 10:46:33 +09001525 if (cdb[0] == VERIFY) {
1526 if (scmd->cmd_len < 10)
1527 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001528 scsi_10_lba_len(cdb, &block, &n_block);
Tejun Heo2e5704f2006-12-17 10:46:33 +09001529 } else if (cdb[0] == VERIFY_16) {
1530 if (scmd->cmd_len < 16)
1531 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001532 scsi_16_lba_len(cdb, &block, &n_block);
Tejun Heo2e5704f2006-12-17 10:46:33 +09001533 } else
Douglas Gilbertae006512005-10-09 09:09:35 -04001534 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
Albert Lee8bf62ec2005-05-12 15:29:42 -04001536 if (!n_block)
Douglas Gilbertae006512005-10-09 09:09:35 -04001537 goto nothing_to_do;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001538 if (block >= dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001539 goto out_of_range;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001540 if ((block + n_block) > dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001541 goto out_of_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542
Albert Lee07506692005-10-12 15:04:18 +08001543 if (dev->flags & ATA_DFLAG_LBA) {
1544 tf->flags |= ATA_TFLAG_LBA;
1545
Albert Leec6a33e22005-10-12 15:12:26 +08001546 if (lba_28_ok(block, n_block)) {
1547 /* use LBA28 */
1548 tf->command = ATA_CMD_VERIFY;
1549 tf->device |= (block >> 24) & 0xf;
1550 } else if (lba_48_ok(block, n_block)) {
1551 if (!(dev->flags & ATA_DFLAG_LBA48))
1552 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001553
1554 /* use LBA48 */
1555 tf->flags |= ATA_TFLAG_LBA48;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001556 tf->command = ATA_CMD_VERIFY_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557
Albert Lee8bf62ec2005-05-12 15:29:42 -04001558 tf->hob_nsect = (n_block >> 8) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
Albert Lee8bf62ec2005-05-12 15:29:42 -04001560 tf->hob_lbah = (block >> 40) & 0xff;
1561 tf->hob_lbam = (block >> 32) & 0xff;
1562 tf->hob_lbal = (block >> 24) & 0xff;
Albert Leec6a33e22005-10-12 15:12:26 +08001563 } else
1564 /* request too large even for LBA48 */
1565 goto out_of_range;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001566
1567 tf->nsect = n_block & 0xff;
1568
1569 tf->lbah = (block >> 16) & 0xff;
1570 tf->lbam = (block >> 8) & 0xff;
1571 tf->lbal = block & 0xff;
1572
1573 tf->device |= ATA_LBA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 } else {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001575 /* CHS */
1576 u32 sect, head, cyl, track;
1577
Albert Leec6a33e22005-10-12 15:12:26 +08001578 if (!lba_28_ok(block, n_block))
1579 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001580
Albert Lee8bf62ec2005-05-12 15:29:42 -04001581 /* Convert LBA to CHS */
1582 track = (u32)block / dev->sectors;
1583 cyl = track / dev->heads;
1584 head = track % dev->heads;
1585 sect = (u32)block % dev->sectors + 1;
1586
Albert Leec187c4b2005-10-04 08:46:51 -04001587 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1588 (u32)block, track, cyl, head, sect);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001589
1590 /* Check whether the converted CHS can fit.
1591 Cylinder: 0-65535
Albert Lee8bf62ec2005-05-12 15:29:42 -04001592 Head: 0-15
1593 Sector: 1-255*/
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001594 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
Douglas Gilbertae006512005-10-09 09:09:35 -04001595 goto out_of_range;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 tf->command = ATA_CMD_VERIFY;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001598 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1599 tf->lbal = sect;
1600 tf->lbam = cyl;
1601 tf->lbah = cyl >> 8;
1602 tf->device |= head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603 }
1604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001606
1607invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +09001608 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001609 /* "Invalid field in cbd" */
1610 return 1;
1611
1612out_of_range:
Tejun Heo542b1442006-12-17 10:45:08 +09001613 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001614 /* "Logical Block Address out of range" */
1615 return 1;
1616
1617nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001618 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001619 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620}
1621
1622/**
1623 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1624 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 *
1626 * Converts any of six SCSI read/write commands into the
1627 * ATA counterpart, including starting sector (LBA),
1628 * sector count, and taking into account the device's LBA48
1629 * support.
1630 *
1631 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1632 * %WRITE_16 are currently supported.
1633 *
1634 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001635 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 *
1637 * RETURNS:
1638 * Zero on success, non-zero on error.
1639 */
Tejun Heoad706992006-12-17 10:45:57 +09001640static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641{
Tejun Heo542b1442006-12-17 10:45:08 +09001642 struct scsi_cmnd *scmd = qc->scsicmd;
Tejun Heoad706992006-12-17 10:45:57 +09001643 const u8 *cdb = scmd->cmnd;
Tejun Heobd056d72006-11-14 22:47:10 +09001644 unsigned int tf_flags = 0;
Albert Lee3aef5232005-10-04 08:47:43 -04001645 u64 block;
1646 u32 n_block;
Tejun Heobd056d72006-11-14 22:47:10 +09001647 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648
Tejun Heo542b1442006-12-17 10:45:08 +09001649 if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
Tejun Heobd056d72006-11-14 22:47:10 +09001650 tf_flags |= ATA_TFLAG_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001652 /* Calculate the SCSI LBA, transfer length and FUA. */
Tejun Heo542b1442006-12-17 10:45:08 +09001653 switch (cdb[0]) {
Albert Lee3aef5232005-10-04 08:47:43 -04001654 case READ_10:
1655 case WRITE_10:
Tejun Heo2e5704f2006-12-17 10:46:33 +09001656 if (unlikely(scmd->cmd_len < 10))
1657 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001658 scsi_10_lba_len(cdb, &block, &n_block);
1659 if (unlikely(cdb[1] & (1 << 3)))
Tejun Heobd056d72006-11-14 22:47:10 +09001660 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001661 break;
1662 case READ_6:
1663 case WRITE_6:
Tejun Heo2e5704f2006-12-17 10:46:33 +09001664 if (unlikely(scmd->cmd_len < 6))
1665 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001666 scsi_6_lba_len(cdb, &block, &n_block);
Albert Leec187c4b2005-10-04 08:46:51 -04001667
1668 /* for 6-byte r/w commands, transfer length 0
1669 * means 256 blocks of data, not 0 block.
1670 */
Jeff Garzik76b2bf92005-08-29 19:24:43 -04001671 if (!n_block)
1672 n_block = 256;
Albert Lee3aef5232005-10-04 08:47:43 -04001673 break;
1674 case READ_16:
1675 case WRITE_16:
Tejun Heo2e5704f2006-12-17 10:46:33 +09001676 if (unlikely(scmd->cmd_len < 16))
1677 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001678 scsi_16_lba_len(cdb, &block, &n_block);
1679 if (unlikely(cdb[1] & (1 << 3)))
Tejun Heobd056d72006-11-14 22:47:10 +09001680 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001681 break;
1682 default:
Albert Lee8bf62ec2005-05-12 15:29:42 -04001683 DPRINTK("no-byte command\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04001684 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685 }
1686
Albert Lee8bf62ec2005-05-12 15:29:42 -04001687 /* Check and compose ATA command */
1688 if (!n_block)
Albert Leec187c4b2005-10-04 08:46:51 -04001689 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1690 * length 0 means transfer 0 block of data.
1691 * However, for ATA R/W commands, sector count 0 means
1692 * 256 or 65536 sectors, not 0 sectors as in SCSI.
Alan Coxf51750d2005-11-07 17:06:33 +00001693 *
1694 * WARNING: one or two older ATA drives treat 0 as 0...
Albert Leec187c4b2005-10-04 08:46:51 -04001695 */
Douglas Gilbertae006512005-10-09 09:09:35 -04001696 goto nothing_to_do;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001697
Tejun Heobd056d72006-11-14 22:47:10 +09001698 qc->flags |= ATA_QCFLAG_IO;
Tejun Heo726f0782007-01-03 17:30:39 +09001699 qc->nbytes = n_block * ATA_SECT_SIZE;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001700
Tejun Heobd056d72006-11-14 22:47:10 +09001701 rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
1702 qc->tag);
1703 if (likely(rc == 0))
1704 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001705
Tejun Heobd056d72006-11-14 22:47:10 +09001706 if (rc == -ERANGE)
1707 goto out_of_range;
1708 /* treat all other errors as -EINVAL, fall through */
Douglas Gilbertae006512005-10-09 09:09:35 -04001709invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +09001710 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001711 /* "Invalid field in cbd" */
1712 return 1;
1713
1714out_of_range:
Tejun Heo542b1442006-12-17 10:45:08 +09001715 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001716 /* "Logical Block Address out of range" */
1717 return 1;
1718
1719nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001720 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001721 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722}
1723
Tejun Heo77853bf2006-01-23 13:09:36 +09001724static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725{
Tejun Heoc31f5712006-11-22 12:39:43 +09001726 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika7dac442005-10-30 04:44:42 -05001728 u8 *cdb = cmd->cmnd;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001729 int need_sense = (qc->err_mask != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730
Jeff Garzikb0955182005-05-12 15:45:22 -04001731 /* For ATA pass thru (SAT) commands, generate a sense block if
1732 * user mandated it or if there's an error. Note that if we
1733 * generate because the user forced us to, a check condition
1734 * is generated and the ATA register values are returned
1735 * whether the command completed successfully or not. If there
1736 * was no error, SK, ASC and ASCQ will all be zero.
1737 */
Jeff Garzika7dac442005-10-30 04:44:42 -05001738 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001739 ((cdb[2] & 0x20) || need_sense)) {
Tejun Heo750426a2006-11-14 22:37:35 +09001740 ata_gen_passthru_sense(qc);
Jeff Garzikb0955182005-05-12 15:45:22 -04001741 } else {
1742 if (!need_sense) {
1743 cmd->result = SAM_STAT_GOOD;
1744 } else {
1745 /* TODO: decide which descriptor format to use
1746 * for 48b LBA devices and call that here
1747 * instead of the fixed desc, which is only
1748 * good for smaller LBA (and maybe CHS?)
1749 * devices.
1750 */
Tejun Heo750426a2006-11-14 22:37:35 +09001751 ata_gen_ata_sense(qc);
Jeff Garzikb0955182005-05-12 15:45:22 -04001752 }
1753 }
1754
Tejun Heoc31f5712006-11-22 12:39:43 +09001755 if (need_sense && !ap->ops->error_handler)
Tejun Heo44877b42007-02-21 01:06:51 +09001756 ata_dump_status(ap->print_id, &qc->result_tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 qc->scsidone(cmd);
1759
Tejun Heo77853bf2006-01-23 13:09:36 +09001760 ata_qc_free(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761}
1762
1763/**
1764 * ata_scsi_translate - Translate then issue SCSI command to ATA device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765 * @dev: ATA device to which the command is addressed
1766 * @cmd: SCSI command to execute
1767 * @done: SCSI command completion function
1768 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1769 *
1770 * Our ->queuecommand() function has decided that the SCSI
1771 * command issued can be directly translated into an ATA
1772 * command, rather than handled internally.
1773 *
1774 * This function sets up an ata_queued_cmd structure for the
1775 * SCSI command, and sends that ata_queued_cmd to the hardware.
1776 *
Douglas Gilbertae006512005-10-09 09:09:35 -04001777 * The xlat_func argument (actor) returns 0 if ready to execute
1778 * ATA command, else 1 to finish translation. If 1 is returned
1779 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1780 * to be set reflecting an error condition or clean (early)
1781 * termination.
1782 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001784 * spin_lock_irqsave(host lock)
Tejun Heo2115ea92006-05-15 21:03:39 +09001785 *
1786 * RETURNS:
1787 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
1788 * needs to be deferred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 */
Tejun Heo2115ea92006-05-15 21:03:39 +09001790static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
1791 void (*done)(struct scsi_cmnd *),
1792 ata_xlat_func_t xlat_func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
Tejun Heo31cc23b2007-09-23 13:14:12 +09001794 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 struct ata_queued_cmd *qc;
Tejun Heo31cc23b2007-09-23 13:14:12 +09001796 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797
1798 VPRINTK("ENTER\n");
1799
Tejun Heo3373efd2006-05-15 20:57:53 +09001800 qc = ata_scsi_qc_new(dev, cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (!qc)
Douglas Gilbertae006512005-10-09 09:09:35 -04001802 goto err_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
1804 /* data is present; dma-map it */
be7db052005-04-17 15:26:13 -05001805 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1806 cmd->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harrosh71201652007-09-18 17:48:50 +02001807 if (unlikely(scsi_bufflen(cmd) < 1)) {
Tejun Heof15a1da2006-05-15 20:57:56 +09001808 ata_dev_printk(dev, KERN_WARNING,
1809 "WARNING: zero len r/w req\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04001810 goto err_did;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 }
1812
Boaz Harrosh71201652007-09-18 17:48:50 +02001813 ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815 qc->dma_dir = cmd->sc_data_direction;
1816 }
1817
1818 qc->complete_fn = ata_scsi_qc_complete;
1819
Tejun Heoad706992006-12-17 10:45:57 +09001820 if (xlat_func(qc))
Douglas Gilbertae006512005-10-09 09:09:35 -04001821 goto early_finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822
Tejun Heo31cc23b2007-09-23 13:14:12 +09001823 if (ap->ops->qc_defer) {
1824 if ((rc = ap->ops->qc_defer(qc)))
1825 goto defer;
1826 }
1827
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 /* select device, send command to hardware */
Tejun Heo8e0e6942006-03-31 20:41:11 +09001829 ata_qc_issue(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830
1831 VPRINTK("EXIT\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001832 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833
Douglas Gilbertae006512005-10-09 09:09:35 -04001834early_finish:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001835 ata_qc_free(qc);
Tejun Heoda071b42007-05-14 17:26:18 +02001836 qc->scsidone(cmd);
Douglas Gilbertae006512005-10-09 09:09:35 -04001837 DPRINTK("EXIT - early finish (good or error)\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001838 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001839
1840err_did:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 ata_qc_free(qc);
Douglas Gilbertae006512005-10-09 09:09:35 -04001842 cmd->result = (DID_ERROR << 16);
Tejun Heoda071b42007-05-14 17:26:18 +02001843 qc->scsidone(cmd);
Darrick J. Wong253b92e2006-11-14 09:55:41 -05001844err_mem:
Douglas Gilbertae006512005-10-09 09:09:35 -04001845 DPRINTK("EXIT - internal\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001846 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001847
1848defer:
Tejun Heo31cc23b2007-09-23 13:14:12 +09001849 ata_qc_free(qc);
Tejun Heo3dc1d882006-05-15 21:03:45 +09001850 DPRINTK("EXIT - defer\n");
Tejun Heo31cc23b2007-09-23 13:14:12 +09001851 if (rc == ATA_DEFER_LINK)
1852 return SCSI_MLQUEUE_DEVICE_BUSY;
1853 else
1854 return SCSI_MLQUEUE_HOST_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
1857/**
1858 * ata_scsi_rbuf_get - Map response buffer.
Randy Dunlapec2a20e2008-04-30 12:57:00 -07001859 * @cmd: SCSI command containing buffer to be mapped.
Tejun Heo87340e92008-04-28 17:48:51 +09001860 * @flags: unsigned long variable to store irq enable status
1861 * @copy_in: copy in from user buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001862 *
Tejun Heo87340e92008-04-28 17:48:51 +09001863 * Prepare buffer for simulated SCSI commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 *
1865 * LOCKING:
Tejun Heo87340e92008-04-28 17:48:51 +09001866 * spin_lock_irqsave(ata_scsi_rbuf_lock) on success
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 *
1868 * RETURNS:
Tejun Heo87340e92008-04-28 17:48:51 +09001869 * Pointer to response buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 */
Tejun Heo87340e92008-04-28 17:48:51 +09001871static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in,
1872 unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Tejun Heo87340e92008-04-28 17:48:51 +09001874 spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
Tejun Heo87340e92008-04-28 17:48:51 +09001876 memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
1877 if (copy_in)
1878 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
1879 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
1880 return ata_scsi_rbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881}
1882
1883/**
1884 * ata_scsi_rbuf_put - Unmap response buffer.
1885 * @cmd: SCSI command containing buffer to be unmapped.
Tejun Heo87340e92008-04-28 17:48:51 +09001886 * @copy_out: copy out result
1887 * @flags: @flags passed to ata_scsi_rbuf_get()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 *
Tejun Heo87340e92008-04-28 17:48:51 +09001889 * Returns rbuf buffer. The result is copied to @cmd's buffer if
1890 * @copy_back is true.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891 *
1892 * LOCKING:
Tejun Heo87340e92008-04-28 17:48:51 +09001893 * Unlocks ata_scsi_rbuf_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 */
Tejun Heo87340e92008-04-28 17:48:51 +09001895static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out,
1896 unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897{
Tejun Heo87340e92008-04-28 17:48:51 +09001898 if (copy_out)
1899 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
1900 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
1901 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902}
1903
1904/**
1905 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1906 * @args: device IDENTIFY data / SCSI command of interest.
1907 * @actor: Callback hook for desired SCSI command simulator
1908 *
1909 * Takes care of the hard work of simulating a SCSI command...
1910 * Mapping the response buffer, calling the command's handler,
1911 * and handling the handler's return value. This return value
1912 * indicates whether the handler wishes the SCSI command to be
Douglas Gilbertae006512005-10-09 09:09:35 -04001913 * completed successfully (0), or not (in which case cmd->result
1914 * and sense buffer are assumed to be set).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 *
1916 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001917 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 */
Tejun Heof0761be2008-04-28 17:16:52 +09001919static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
Tejun Heo87340e92008-04-28 17:48:51 +09001920 unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921{
1922 u8 *rbuf;
Tejun Heo87340e92008-04-28 17:48:51 +09001923 unsigned int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 struct scsi_cmnd *cmd = args->cmd;
Jeff Garzikb445c562008-02-29 19:10:51 -05001925 unsigned long flags;
1926
Tejun Heo87340e92008-04-28 17:48:51 +09001927 rbuf = ata_scsi_rbuf_get(cmd, false, &flags);
1928 rc = actor(args, rbuf);
1929 ata_scsi_rbuf_put(cmd, rc == 0, &flags);
Jeff Garzikb445c562008-02-29 19:10:51 -05001930
Douglas Gilbertae006512005-10-09 09:09:35 -04001931 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 cmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001933 args->done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934}
1935
1936/**
1937 * ata_scsiop_inq_std - Simulate INQUIRY command
1938 * @args: device IDENTIFY data / SCSI command of interest.
1939 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 *
1941 * Returns standard device identification data associated
Jeff Garzikb142eb62006-03-21 20:37:47 -05001942 * with non-VPD INQUIRY command output.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 *
1944 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001945 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 */
Tejun Heo87340e92008-04-28 17:48:51 +09001947static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948{
Tejun Heo87340e92008-04-28 17:48:51 +09001949 const u8 versions[] = {
1950 0x60, /* SAM-3 (no version claimed) */
1951
1952 0x03,
1953 0x20, /* SBC-2 (no version claimed) */
1954
1955 0x02,
1956 0x60 /* SPC-3 (no version claimed) */
1957 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 u8 hdr[] = {
1959 TYPE_DISK,
1960 0,
1961 0x5, /* claim SPC-3 version compatibility */
1962 2,
1963 95 - 4
1964 };
1965
Tejun Heo87340e92008-04-28 17:48:51 +09001966 VPRINTK("ENTER\n");
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 /* set scsi removeable (RMB) bit per ata bit */
1969 if (ata_id_removeable(args->id))
1970 hdr[1] |= (1 << 7);
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972 memcpy(rbuf, hdr, sizeof(hdr));
Tejun Heo87340e92008-04-28 17:48:51 +09001973 memcpy(&rbuf[8], "ATA ", 8);
1974 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
1975 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001976
Tejun Heo87340e92008-04-28 17:48:51 +09001977 if (rbuf[32] == 0 || rbuf[32] == ' ')
1978 memcpy(&rbuf[32], "n/a ", 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979
Tejun Heo87340e92008-04-28 17:48:51 +09001980 memcpy(rbuf + 59, versions, sizeof(versions));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981
1982 return 0;
1983}
1984
1985/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05001986 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 * @args: device IDENTIFY data / SCSI command of interest.
1988 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05001990 * Returns list of inquiry VPD pages available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 *
1992 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001993 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 */
Tejun Heo87340e92008-04-28 17:48:51 +09001995static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996{
1997 const u8 pages[] = {
1998 0x00, /* page 0x00, this page */
1999 0x80, /* page 0x80, unit serial no page */
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002000 0x83, /* page 0x83, device ident page */
2001 0x89, /* page 0x89, ata info page */
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002002 0xb0, /* page 0xb0, block limits page */
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002003 0xb1, /* page 0xb1, block device characteristics page */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 };
Tejun Heo87340e92008-04-28 17:48:51 +09002005
Jeff Garzikb142eb62006-03-21 20:37:47 -05002006 rbuf[3] = sizeof(pages); /* number of supported VPD pages */
Tejun Heo87340e92008-04-28 17:48:51 +09002007 memcpy(rbuf + 4, pages, sizeof(pages));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 return 0;
2009}
2010
2011/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002012 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 * @args: device IDENTIFY data / SCSI command of interest.
2014 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 *
2016 * Returns ATA device serial number.
2017 *
2018 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002019 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 */
Tejun Heo87340e92008-04-28 17:48:51 +09002021static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022{
2023 const u8 hdr[] = {
2024 0,
2025 0x80, /* this page code */
2026 0,
Tejun Heoa0cf7332007-01-02 20:18:49 +09002027 ATA_ID_SERNO_LEN, /* page len */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002028 };
Tejun Heo87340e92008-04-28 17:48:51 +09002029
Linus Torvalds1da177e2005-04-16 15:20:36 -07002030 memcpy(rbuf, hdr, sizeof(hdr));
Tejun Heo87340e92008-04-28 17:48:51 +09002031 ata_id_string(args->id, (unsigned char *) &rbuf[4],
2032 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033 return 0;
2034}
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002037 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 * @args: device IDENTIFY data / SCSI command of interest.
2039 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05002041 * Yields two logical unit device identification designators:
2042 * - vendor specific ASCII containing the ATA serial number
2043 * - SAT defined "t10 vendor id based" containing ASCII vendor
2044 * name ("ATA "), model and serial numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 *
2046 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002047 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048 */
Tejun Heo87340e92008-04-28 17:48:51 +09002049static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050{
Jeff Garzikb142eb62006-03-21 20:37:47 -05002051 const int sat_model_serial_desc_len = 68;
Tejun Heo87340e92008-04-28 17:48:51 +09002052 int num;
Jeff Garzikb142eb62006-03-21 20:37:47 -05002053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 rbuf[1] = 0x83; /* this page code */
Jeff Garzikb142eb62006-03-21 20:37:47 -05002055 num = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002056
Tejun Heo87340e92008-04-28 17:48:51 +09002057 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
2058 rbuf[num + 0] = 2;
2059 rbuf[num + 3] = ATA_ID_SERNO_LEN;
2060 num += 4;
2061 ata_id_string(args->id, (unsigned char *) rbuf + num,
2062 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
2063 num += ATA_ID_SERNO_LEN;
2064
2065 /* SAT defined lu model and serial numbers descriptor */
2066 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
2067 rbuf[num + 0] = 2;
2068 rbuf[num + 1] = 1;
2069 rbuf[num + 3] = sat_model_serial_desc_len;
2070 num += 4;
2071 memcpy(rbuf + num, "ATA ", 8);
2072 num += 8;
2073 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
2074 ATA_ID_PROD_LEN);
2075 num += ATA_ID_PROD_LEN;
2076 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
2077 ATA_ID_SERNO_LEN);
2078 num += ATA_ID_SERNO_LEN;
2079
Jeff Garzikb142eb62006-03-21 20:37:47 -05002080 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 return 0;
2082}
2083
2084/**
Jeff Garzikad355b42007-09-21 20:38:03 -04002085 * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
2086 * @args: device IDENTIFY data / SCSI command of interest.
2087 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Jeff Garzikad355b42007-09-21 20:38:03 -04002088 *
2089 * Yields SAT-specified ATA VPD page.
2090 *
2091 * LOCKING:
2092 * spin_lock_irqsave(host lock)
2093 */
Tejun Heo87340e92008-04-28 17:48:51 +09002094static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
Jeff Garzikad355b42007-09-21 20:38:03 -04002095{
Jeff Garzikad355b42007-09-21 20:38:03 -04002096 struct ata_taskfile tf;
Jeff Garzikad355b42007-09-21 20:38:03 -04002097
Jeff Garzikad355b42007-09-21 20:38:03 -04002098 memset(&tf, 0, sizeof(tf));
2099
Tejun Heo87340e92008-04-28 17:48:51 +09002100 rbuf[1] = 0x89; /* our page code */
2101 rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
2102 rbuf[3] = (0x238 & 0xff);
Jeff Garzikad355b42007-09-21 20:38:03 -04002103
Tejun Heo87340e92008-04-28 17:48:51 +09002104 memcpy(&rbuf[8], "linux ", 8);
2105 memcpy(&rbuf[16], "libata ", 16);
2106 memcpy(&rbuf[32], DRV_VERSION, 4);
2107 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
Jeff Garzikad355b42007-09-21 20:38:03 -04002108
2109 /* we don't store the ATA device signature, so we fake it */
2110
2111 tf.command = ATA_DRDY; /* really, this is Status reg */
2112 tf.lbal = 0x1;
2113 tf.nsect = 0x1;
2114
Tejun Heo87340e92008-04-28 17:48:51 +09002115 ata_tf_to_fis(&tf, 0, 1, &rbuf[36]); /* TODO: PMP? */
2116 rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
Jeff Garzikad355b42007-09-21 20:38:03 -04002117
Tejun Heo87340e92008-04-28 17:48:51 +09002118 rbuf[56] = ATA_CMD_ID_ATA;
Jeff Garzikad355b42007-09-21 20:38:03 -04002119
Tejun Heo87340e92008-04-28 17:48:51 +09002120 memcpy(&rbuf[60], &args->id[0], 512);
Jeff Garzikad355b42007-09-21 20:38:03 -04002121 return 0;
2122}
2123
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002124static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
2125{
2126 u32 min_io_sectors;
2127
2128 rbuf[1] = 0xb0;
2129 rbuf[3] = 0x3c; /* required VPD size with unmap support */
2130
2131 /*
2132 * Optimal transfer length granularity.
2133 *
2134 * This is always one physical block, but for disks with a smaller
2135 * logical than physical sector size we need to figure out what the
2136 * latter is.
2137 */
2138 if (ata_id_has_large_logical_sectors(args->id))
2139 min_io_sectors = ata_id_logical_per_physical_sectors(args->id);
2140 else
2141 min_io_sectors = 1;
2142 put_unaligned_be16(min_io_sectors, &rbuf[6]);
2143
2144 /*
2145 * Optimal unmap granularity.
2146 *
2147 * The ATA spec doesn't even know about a granularity or alignment
2148 * for the TRIM command. We can leave away most of the unmap related
2149 * VPD page entries, but we have specifify a granularity to signal
2150 * that we support some form of unmap - in thise case via WRITE SAME
2151 * with the unmap bit set.
2152 */
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002153 if (ata_id_has_trim(args->id)) {
2154 put_unaligned_be32(65535 * 512 / 8, &rbuf[20]);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002155 put_unaligned_be32(1, &rbuf[28]);
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002156 }
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002157
2158 return 0;
2159}
2160
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002161static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
2162{
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002163 int form_factor = ata_id_form_factor(args->id);
2164 int media_rotation_rate = ata_id_rotation_rate(args->id);
2165
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002166 rbuf[1] = 0xb1;
2167 rbuf[3] = 0x3c;
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002168 rbuf[4] = media_rotation_rate >> 8;
2169 rbuf[5] = media_rotation_rate;
2170 rbuf[7] = form_factor;
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002171
2172 return 0;
2173}
2174
Jeff Garzikad355b42007-09-21 20:38:03 -04002175/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002176 * ata_scsiop_noop - Command handler that simply returns success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 * @args: device IDENTIFY data / SCSI command of interest.
2178 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 *
2180 * No operation. Simply returns success to caller, to indicate
2181 * that the caller should successfully complete this SCSI command.
2182 *
2183 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002184 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 */
Tejun Heo87340e92008-04-28 17:48:51 +09002186static unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
2188 VPRINTK("ENTER\n");
2189 return 0;
2190}
2191
2192/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 * ata_msense_caching - Simulate MODE SENSE caching info page
2194 * @id: device IDENTIFY data
Tejun Heo87340e92008-04-28 17:48:51 +09002195 * @buf: output buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 *
2197 * Generate a caching info page, which conditionally indicates
2198 * write caching to the SCSI layer, depending on device
2199 * capabilities.
2200 *
2201 * LOCKING:
2202 * None.
2203 */
Tejun Heo87340e92008-04-28 17:48:51 +09002204static unsigned int ata_msense_caching(u16 *id, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
Tejun Heo87340e92008-04-28 17:48:51 +09002206 memcpy(buf, def_cache_mpage, sizeof(def_cache_mpage));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 if (ata_id_wcache_enabled(id))
Tejun Heo87340e92008-04-28 17:48:51 +09002208 buf[2] |= (1 << 2); /* write cache enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (!ata_id_rahead_enabled(id))
Tejun Heo87340e92008-04-28 17:48:51 +09002210 buf[12] |= (1 << 5); /* disable read ahead */
2211 return sizeof(def_cache_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212}
2213
2214/**
2215 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
Tejun Heo87340e92008-04-28 17:48:51 +09002216 * @buf: output buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 *
2218 * Generate a generic MODE SENSE control mode page.
2219 *
2220 * LOCKING:
2221 * None.
2222 */
Tejun Heo87340e92008-04-28 17:48:51 +09002223static unsigned int ata_msense_ctl_mode(u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224{
Tejun Heo87340e92008-04-28 17:48:51 +09002225 memcpy(buf, def_control_mpage, sizeof(def_control_mpage));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002226 return sizeof(def_control_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227}
2228
2229/**
2230 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
Randy Dunlapec2a20e2008-04-30 12:57:00 -07002231 * @buf: output buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232 *
2233 * Generate a generic MODE SENSE r/w error recovery page.
2234 *
2235 * LOCKING:
2236 * None.
2237 */
Tejun Heo87340e92008-04-28 17:48:51 +09002238static unsigned int ata_msense_rw_recovery(u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239{
Tejun Heo87340e92008-04-28 17:48:51 +09002240 memcpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002241 return sizeof(def_rw_recovery_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242}
2243
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002244/*
2245 * We can turn this into a real blacklist if it's needed, for now just
2246 * blacklist any Maxtor BANC1G10 revision firmware
2247 */
2248static int ata_dev_supports_fua(u16 *id)
2249{
Tejun Heoa0cf7332007-01-02 20:18:49 +09002250 unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002251
Jeff Garzikc3c013a2006-02-27 22:31:19 -05002252 if (!libata_fua)
2253 return 0;
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002254 if (!ata_id_has_fua(id))
2255 return 0;
2256
Tejun Heoa0cf7332007-01-02 20:18:49 +09002257 ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
2258 ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002259
Tejun Heo2e026712006-02-12 22:47:04 +09002260 if (strcmp(model, "Maxtor"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002261 return 1;
Tejun Heo2e026712006-02-12 22:47:04 +09002262 if (strcmp(fw, "BANC1G10"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002263 return 1;
2264
2265 return 0; /* blacklisted */
2266}
2267
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268/**
2269 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
2270 * @args: device IDENTIFY data / SCSI command of interest.
2271 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002272 *
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002273 * Simulate MODE SENSE commands. Assume this is invoked for direct
2274 * access devices (e.g. disks) only. There should be no block
2275 * descriptor for other device types.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 *
2277 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002278 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002279 */
Tejun Heo87340e92008-04-28 17:48:51 +09002280static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281{
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002282 struct ata_device *dev = args->dev;
Tejun Heo87340e92008-04-28 17:48:51 +09002283 u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002284 const u8 sat_blk_desc[] = {
2285 0, 0, 0, 0, /* number of blocks: sat unspecified */
2286 0,
2287 0, 0x2, 0x0 /* block length: 512 bytes */
2288 };
2289 u8 pg, spg;
Tejun Heo87340e92008-04-28 17:48:51 +09002290 unsigned int ebd, page_control, six_byte;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002291 u8 dpofua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292
2293 VPRINTK("ENTER\n");
2294
2295 six_byte = (scsicmd[0] == MODE_SENSE);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002296 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
2297 /*
2298 * LLBA bit in msense(10) ignored (compliant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299 */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002300
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 page_control = scsicmd[2] >> 6;
Douglas Gilbertae006512005-10-09 09:09:35 -04002302 switch (page_control) {
2303 case 0: /* current */
2304 break; /* supported */
2305 case 3: /* saved */
2306 goto saving_not_supp;
2307 case 1: /* changeable */
2308 case 2: /* defaults */
2309 default:
2310 goto invalid_fld;
2311 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312
Tejun Heo87340e92008-04-28 17:48:51 +09002313 if (six_byte)
2314 p += 4 + (ebd ? 8 : 0);
2315 else
2316 p += 8 + (ebd ? 8 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002318 pg = scsicmd[2] & 0x3f;
2319 spg = scsicmd[3];
2320 /*
2321 * No mode subpages supported (yet) but asking for _all_
2322 * subpages may be valid
2323 */
2324 if (spg && (spg != ALL_SUB_MPAGES))
2325 goto invalid_fld;
2326
2327 switch(pg) {
2328 case RW_RECOVERY_MPAGE:
Tejun Heo87340e92008-04-28 17:48:51 +09002329 p += ata_msense_rw_recovery(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330 break;
2331
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002332 case CACHE_MPAGE:
Tejun Heo87340e92008-04-28 17:48:51 +09002333 p += ata_msense_caching(args->id, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334 break;
2335
Tejun Heo87340e92008-04-28 17:48:51 +09002336 case CONTROL_MPAGE:
2337 p += ata_msense_ctl_mode(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002338 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002339
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002340 case ALL_MPAGES:
Tejun Heo87340e92008-04-28 17:48:51 +09002341 p += ata_msense_rw_recovery(p);
2342 p += ata_msense_caching(args->id, p);
2343 p += ata_msense_ctl_mode(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 break;
2345
2346 default: /* invalid page code */
Douglas Gilbertae006512005-10-09 09:09:35 -04002347 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002348 }
2349
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002350 dpofua = 0;
Alan Coxf79d4092006-05-22 16:55:11 +01002351 if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002352 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
2353 dpofua = 1 << 4;
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355 if (six_byte) {
Tejun Heo87340e92008-04-28 17:48:51 +09002356 rbuf[0] = p - rbuf - 1;
2357 rbuf[2] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002358 if (ebd) {
Tejun Heo87340e92008-04-28 17:48:51 +09002359 rbuf[3] = sizeof(sat_blk_desc);
2360 memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002361 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362 } else {
Tejun Heo87340e92008-04-28 17:48:51 +09002363 unsigned int output_len = p - rbuf - 2;
2364
Linus Torvalds1da177e2005-04-16 15:20:36 -07002365 rbuf[0] = output_len >> 8;
Tejun Heo87340e92008-04-28 17:48:51 +09002366 rbuf[1] = output_len;
2367 rbuf[3] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002368 if (ebd) {
Tejun Heo87340e92008-04-28 17:48:51 +09002369 rbuf[7] = sizeof(sat_blk_desc);
2370 memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002371 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04002374
2375invalid_fld:
2376 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
2377 /* "Invalid field in cbd" */
2378 return 1;
2379
2380saving_not_supp:
2381 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
2382 /* "Saving parameters not supported" */
2383 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002384}
2385
2386/**
2387 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2388 * @args: device IDENTIFY data / SCSI command of interest.
2389 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 *
2391 * Simulate READ CAPACITY commands.
2392 *
2393 * LOCKING:
Tejun Heo6a362612006-11-20 11:15:47 +09002394 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 */
Tejun Heo87340e92008-04-28 17:48:51 +09002396static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002397{
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002398 struct ata_device *dev = args->dev;
2399 u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
2400 u8 log_per_phys = 0;
2401 u16 lowest_aligned = 0;
2402 u16 word_106 = dev->id[106];
2403 u16 word_209 = dev->id[209];
2404
2405 if ((word_106 & 0xc000) == 0x4000) {
2406 /* Number and offset of logical sectors per physical sector */
2407 if (word_106 & (1 << 13))
2408 log_per_phys = word_106 & 0xf;
2409 if ((word_209 & 0xc000) == 0x4000) {
2410 u16 first = dev->id[209] & 0x3fff;
2411 if (first > 0)
2412 lowest_aligned = (1 << log_per_phys) - first;
2413 }
2414 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002415
2416 VPRINTK("ENTER\n");
2417
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 if (args->cmd->cmnd[0] == READ_CAPACITY) {
Tejun Heo6a362612006-11-20 11:15:47 +09002419 if (last_lba >= 0xffffffffULL)
2420 last_lba = 0xffffffff;
Philip Pokorny0c144d02005-05-28 01:24:47 -07002421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 /* sector count, 32-bit */
Tejun Heo87340e92008-04-28 17:48:51 +09002423 rbuf[0] = last_lba >> (8 * 3);
2424 rbuf[1] = last_lba >> (8 * 2);
2425 rbuf[2] = last_lba >> (8 * 1);
2426 rbuf[3] = last_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427
2428 /* sector size */
Tejun Heo87340e92008-04-28 17:48:51 +09002429 rbuf[6] = ATA_SECT_SIZE >> 8;
2430 rbuf[7] = ATA_SECT_SIZE & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431 } else {
2432 /* sector count, 64-bit */
Tejun Heo87340e92008-04-28 17:48:51 +09002433 rbuf[0] = last_lba >> (8 * 7);
2434 rbuf[1] = last_lba >> (8 * 6);
2435 rbuf[2] = last_lba >> (8 * 5);
2436 rbuf[3] = last_lba >> (8 * 4);
2437 rbuf[4] = last_lba >> (8 * 3);
2438 rbuf[5] = last_lba >> (8 * 2);
2439 rbuf[6] = last_lba >> (8 * 1);
2440 rbuf[7] = last_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002441
2442 /* sector size */
Tejun Heo87340e92008-04-28 17:48:51 +09002443 rbuf[10] = ATA_SECT_SIZE >> 8;
2444 rbuf[11] = ATA_SECT_SIZE & 0xff;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002445
2446 rbuf[12] = 0;
2447 rbuf[13] = log_per_phys;
2448 rbuf[14] = (lowest_aligned >> 8) & 0x3f;
2449 rbuf[15] = lowest_aligned;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002450
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002451 if (ata_id_has_trim(args->id)) {
2452 rbuf[14] |= 0x80; /* TPE */
2453
2454 if (ata_id_has_zero_after_trim(args->id))
2455 rbuf[14] |= 0x40; /* TPRZ */
2456 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 }
2458
2459 return 0;
2460}
2461
2462/**
2463 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2464 * @args: device IDENTIFY data / SCSI command of interest.
2465 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 *
2467 * Simulate REPORT LUNS command.
2468 *
2469 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002470 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 */
Tejun Heo87340e92008-04-28 17:48:51 +09002472static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
2474 VPRINTK("ENTER\n");
2475 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2476
2477 return 0;
2478}
2479
Tejun Heo77853bf2006-01-23 13:09:36 +09002480static void atapi_sense_complete(struct ata_queued_cmd *qc)
Jeff Garzika939c962005-10-05 17:09:16 -04002481{
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002482 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002483 /* FIXME: not quite right; we don't want the
2484 * translation of taskfile registers into
2485 * a sense descriptors, since that's only
2486 * correct for ATA, not ATAPI
2487 */
Tejun Heo750426a2006-11-14 22:37:35 +09002488 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002489 }
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002490
2491 qc->scsidone(qc->scsicmd);
Tejun Heo77853bf2006-01-23 13:09:36 +09002492 ata_qc_free(qc);
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002493}
2494
2495/* is it pointless to prefer PIO for "safety reasons"? */
2496static inline int ata_pio_use_silly(struct ata_port *ap)
2497{
2498 return (ap->flags & ATA_FLAG_PIO_DMA);
2499}
2500
2501static void atapi_request_sense(struct ata_queued_cmd *qc)
2502{
2503 struct ata_port *ap = qc->ap;
2504 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika939c962005-10-05 17:09:16 -04002505
2506 DPRINTK("ATAPI request sense\n");
2507
Jeff Garzika939c962005-10-05 17:09:16 -04002508 /* FIXME: is this needed? */
James Bottomley7ccd7202008-01-17 11:56:24 -06002509 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Jeff Garzika939c962005-10-05 17:09:16 -04002510
Tejun Heo127102a2008-04-07 22:47:21 +09002511#ifdef CONFIG_ATA_SFF
James Bottomley855d8542008-04-18 13:18:48 -05002512 if (ap->ops->sff_tf_read)
2513 ap->ops->sff_tf_read(ap, &qc->tf);
Tejun Heo127102a2008-04-07 22:47:21 +09002514#endif
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002515
2516 /* fill these in, for the case where they are -not- overwritten */
2517 cmd->sense_buffer[0] = 0x70;
2518 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2519
2520 ata_qc_reinit(qc);
2521
Tejun Heo93f8fec2007-12-05 16:43:01 +09002522 /* setup sg table and init transfer direction */
akpm@linux-foundation.orgcadb7342008-01-15 16:01:52 -08002523 sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
Tejun Heo93f8fec2007-12-05 16:43:01 +09002524 ata_sg_init(qc, &qc->sgent, 1);
Jeff Garzika939c962005-10-05 17:09:16 -04002525 qc->dma_dir = DMA_FROM_DEVICE;
2526
Tejun Heo6e7846e2006-02-12 23:32:58 +09002527 memset(&qc->cdb, 0, qc->dev->cdb_len);
Jeff Garzika939c962005-10-05 17:09:16 -04002528 qc->cdb[0] = REQUEST_SENSE;
2529 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2530
2531 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2532 qc->tf.command = ATA_CMD_PACKET;
2533
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002534 if (ata_pio_use_silly(ap)) {
Tejun Heo0dc36882007-12-18 16:34:43 -05002535 qc->tf.protocol = ATAPI_PROT_DMA;
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002536 qc->tf.feature |= ATAPI_PKT_DMA;
2537 } else {
Tejun Heo0dc36882007-12-18 16:34:43 -05002538 qc->tf.protocol = ATAPI_PROT_PIO;
Alan Cox2db78dd2007-10-02 13:53:04 -07002539 qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
2540 qc->tf.lbah = 0;
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002541 }
Jeff Garzika939c962005-10-05 17:09:16 -04002542 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2543
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002544 qc->complete_fn = atapi_sense_complete;
Jeff Garzika939c962005-10-05 17:09:16 -04002545
Tejun Heo8e0e6942006-03-31 20:41:11 +09002546 ata_qc_issue(qc);
Jeff Garzika939c962005-10-05 17:09:16 -04002547
2548 DPRINTK("EXIT\n");
2549}
2550
Tejun Heo77853bf2006-01-23 13:09:36 +09002551static void atapi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552{
2553 struct scsi_cmnd *cmd = qc->scsicmd;
Albert Leea22e2eb2005-12-05 15:38:02 +08002554 unsigned int err_mask = qc->err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555
Jeff Garzika7dac442005-10-30 04:44:42 -05002556 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
Jeff Garzike12669e2005-10-05 18:39:23 -04002557
Tejun Heo246619d2006-05-15 20:58:16 +09002558 /* handle completion from new EH */
2559 if (unlikely(qc->ap->ops->error_handler &&
2560 (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
2561
2562 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
2563 /* FIXME: not quite right; we don't want the
2564 * translation of taskfile registers into a
2565 * sense descriptors, since that's only
2566 * correct for ATA, not ATAPI
2567 */
Tejun Heo750426a2006-11-14 22:37:35 +09002568 ata_gen_passthru_sense(qc);
Tejun Heo246619d2006-05-15 20:58:16 +09002569 }
2570
Tejun Heo22aac082006-08-08 14:08:59 +09002571 /* SCSI EH automatically locks door if sdev->locked is
2572 * set. Sometimes door lock request continues to
2573 * fail, for example, when no media is present. This
2574 * creates a loop - SCSI EH issues door lock which
2575 * fails and gets invoked again to acquire sense data
2576 * for the failed command.
2577 *
2578 * If door lock fails, always clear sdev->locked to
2579 * avoid this infinite loop.
2580 */
2581 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL)
2582 qc->dev->sdev->locked = 0;
2583
Tejun Heo246619d2006-05-15 20:58:16 +09002584 qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
2585 qc->scsidone(cmd);
2586 ata_qc_free(qc);
2587 return;
2588 }
2589
2590 /* successful completion or old EH failure path */
Jeff Garzika7dac442005-10-30 04:44:42 -05002591 if (unlikely(err_mask & AC_ERR_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002592 cmd->result = SAM_STAT_CHECK_CONDITION;
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002593 atapi_request_sense(qc);
Tejun Heo77853bf2006-01-23 13:09:36 +09002594 return;
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002595 } else if (unlikely(err_mask)) {
Jeff Garzika7dac442005-10-30 04:44:42 -05002596 /* FIXME: not quite right; we don't want the
2597 * translation of taskfile registers into
2598 * a sense descriptors, since that's only
2599 * correct for ATA, not ATAPI
2600 */
Tejun Heo750426a2006-11-14 22:37:35 +09002601 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002602 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 u8 *scsicmd = cmd->cmnd;
2604
Tony Battersbyfd71da42005-12-21 16:35:44 -05002605 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
Jeff Garzikb445c562008-02-29 19:10:51 -05002606 unsigned long flags;
Tejun Heo87340e92008-04-28 17:48:51 +09002607 u8 *buf;
Jeff Garzikb445c562008-02-29 19:10:51 -05002608
Tejun Heo87340e92008-04-28 17:48:51 +09002609 buf = ata_scsi_rbuf_get(cmd, true, &flags);
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002610
2611 /* ATAPI devices typically report zero for their SCSI version,
2612 * and sometimes deviate from the spec WRT response data
2613 * format. If SCSI version is reported as zero like normal,
2614 * then we make the following fixups: 1) Fake MMC-5 version,
2615 * to indicate to the Linux scsi midlayer this is a modern
2616 * device. 2) Ensure response data format / ATAPI information
2617 * are always correct.
2618 */
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002619 if (buf[2] == 0) {
2620 buf[2] = 0x5;
2621 buf[3] = 0x32;
2622 }
2623
Tejun Heo87340e92008-04-28 17:48:51 +09002624 ata_scsi_rbuf_put(cmd, true, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002625 }
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002626
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 cmd->result = SAM_STAT_GOOD;
2628 }
2629
2630 qc->scsidone(cmd);
Tejun Heo77853bf2006-01-23 13:09:36 +09002631 ata_qc_free(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632}
2633/**
2634 * atapi_xlat - Initialize PACKET taskfile
2635 * @qc: command structure to be initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 *
2637 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002638 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 *
2640 * RETURNS:
2641 * Zero on success, non-zero on failure.
2642 */
Tejun Heoad706992006-12-17 10:45:57 +09002643static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002644{
Tejun Heo542b1442006-12-17 10:45:08 +09002645 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646 struct ata_device *dev = qc->dev;
Tejun Heo542b1442006-12-17 10:45:08 +09002647 int nodata = (scmd->sc_data_direction == DMA_NONE);
Tejun Heo5895ef92008-06-17 12:36:26 +09002648 int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
Alan Cox2db78dd2007-10-02 13:53:04 -07002649 unsigned int nbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650
Tejun Heo2e5704f2006-12-17 10:46:33 +09002651 memset(qc->cdb, 0, dev->cdb_len);
2652 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653
2654 qc->complete_fn = atapi_qc_complete;
2655
2656 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heo542b1442006-12-17 10:45:08 +09002657 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658 qc->tf.flags |= ATA_TFLAG_WRITE;
2659 DPRINTK("direction: write\n");
2660 }
2661
2662 qc->tf.command = ATA_CMD_PACKET;
Tejun Heoaacda372008-03-18 17:47:43 +09002663 ata_qc_set_pc_nbytes(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002665 /* check whether ATAPI DMA is safe */
Tejun Heo5895ef92008-06-17 12:36:26 +09002666 if (!nodata && !using_pio && atapi_check_dma(qc))
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002667 using_pio = 1;
2668
Tejun Heoe1902222007-11-26 20:58:02 +09002669 /* Some controller variants snoop this value for Packet
2670 * transfers to do state machine and FIFO management. Thus we
2671 * want to set it properly, and for DMA where it is
2672 * effectively meaningless.
2673 */
Tejun Heoaacda372008-03-18 17:47:43 +09002674 nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
Alan Cox2db78dd2007-10-02 13:53:04 -07002675
Tejun Heoe1902222007-11-26 20:58:02 +09002676 /* Most ATAPI devices which honor transfer chunk size don't
2677 * behave according to the spec when odd chunk size which
2678 * matches the transfer length is specified. If the number of
2679 * bytes to transfer is 2n+1. According to the spec, what
2680 * should happen is to indicate that 2n+1 is going to be
2681 * transferred and transfer 2n+2 bytes where the last byte is
2682 * padding.
2683 *
2684 * In practice, this doesn't happen. ATAPI devices first
2685 * indicate and transfer 2n bytes and then indicate and
2686 * transfer 2 bytes where the last byte is padding.
2687 *
2688 * This inconsistency confuses several controllers which
2689 * perform PIO using DMA such as Intel AHCIs and sil3124/32.
2690 * These controllers use actual number of transferred bytes to
2691 * update DMA poitner and transfer of 4n+2 bytes make those
2692 * controller push DMA pointer by 4n+4 bytes because SATA data
2693 * FISes are aligned to 4 bytes. This causes data corruption
2694 * and buffer overrun.
2695 *
2696 * Always setting nbytes to even number solves this problem
2697 * because then ATAPI devices don't have to split data at 2n
2698 * boundaries.
2699 */
2700 if (nbytes & 0x1)
2701 nbytes++;
2702
Alan Cox2db78dd2007-10-02 13:53:04 -07002703 qc->tf.lbam = (nbytes & 0xFF);
2704 qc->tf.lbah = (nbytes >> 8);
2705
Tejun Heo5895ef92008-06-17 12:36:26 +09002706 if (nodata)
2707 qc->tf.protocol = ATAPI_PROT_NODATA;
2708 else if (using_pio)
2709 qc->tf.protocol = ATAPI_PROT_PIO;
2710 else {
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002711 /* DMA data xfer */
Tejun Heo0dc36882007-12-18 16:34:43 -05002712 qc->tf.protocol = ATAPI_PROT_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 qc->tf.feature |= ATAPI_PKT_DMA;
2714
Tejun Heo91163002008-02-21 13:25:50 +09002715 if ((dev->flags & ATA_DFLAG_DMADIR) &&
2716 (scmd->sc_data_direction != DMA_TO_DEVICE))
Albert Lee95de7192006-04-04 10:57:18 +08002717 /* some SATA bridges need us to indicate data xfer direction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 qc->tf.feature |= ATAPI_DMADIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 }
2720
Alan Cox2db78dd2007-10-02 13:53:04 -07002721
2722 /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
2723 as ATAPI tape drives don't get this right otherwise */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 return 0;
2725}
2726
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002727static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
Tejun Heoab5b3a52006-05-31 18:27:34 +09002728{
Tejun Heo071f44b2008-04-07 22:47:22 +09002729 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09002730 if (likely(devno < ata_link_max_devices(&ap->link)))
2731 return &ap->link.device[devno];
2732 } else {
2733 if (likely(devno < ap->nr_pmp_links))
2734 return &ap->pmp_link[devno].device[0];
2735 }
2736
Tejun Heoab5b3a52006-05-31 18:27:34 +09002737 return NULL;
2738}
2739
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002740static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
2741 const struct scsi_device *scsidev)
Tejun Heoab5b3a52006-05-31 18:27:34 +09002742{
Tejun Heo41bda9c2007-08-06 18:36:24 +09002743 int devno;
Tejun Heoab5b3a52006-05-31 18:27:34 +09002744
Tejun Heo41bda9c2007-08-06 18:36:24 +09002745 /* skip commands not addressed to targets we simulate */
Tejun Heo071f44b2008-04-07 22:47:22 +09002746 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09002747 if (unlikely(scsidev->channel || scsidev->lun))
2748 return NULL;
2749 devno = scsidev->id;
2750 } else {
2751 if (unlikely(scsidev->id || scsidev->lun))
2752 return NULL;
2753 devno = scsidev->channel;
2754 }
2755
2756 return ata_find_dev(ap, devno);
Tejun Heoab5b3a52006-05-31 18:27:34 +09002757}
2758
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759/**
2760 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2761 * @ap: ATA port to which the device is attached
2762 * @scsidev: SCSI device from which we derive the ATA device
2763 *
2764 * Given various information provided in struct scsi_cmnd,
2765 * map that onto an ATA bus, and using that mapping
2766 * determine which ata_device is associated with the
2767 * SCSI command to be sent.
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 * RETURNS:
2773 * Associated ATA device, or %NULL if not found.
2774 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775static struct ata_device *
Jeff Garzik057ace52005-10-22 14:27:05 -04002776ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
Tejun Heoab5b3a52006-05-31 18:27:34 +09002778 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779
Tejun Heo2486fa52008-07-31 07:52:40 +09002780 if (unlikely(!dev || !ata_dev_enabled(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781 return NULL;
2782
Linus Torvalds1da177e2005-04-16 15:20:36 -07002783 return dev;
2784}
2785
Jeff Garzikb0955182005-05-12 15:45:22 -04002786/*
2787 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2788 * @byte1: Byte 1 from pass-thru CDB.
2789 *
2790 * RETURNS:
2791 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2792 */
2793static u8
2794ata_scsi_map_proto(u8 byte1)
2795{
2796 switch((byte1 & 0x1e) >> 1) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002797 case 3: /* Non-data */
2798 return ATA_PROT_NODATA;
Jeff Garzikb0955182005-05-12 15:45:22 -04002799
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002800 case 6: /* DMA */
2801 case 10: /* UDMA Data-in */
2802 case 11: /* UDMA Data-Out */
2803 return ATA_PROT_DMA;
Jeff Garzikb0955182005-05-12 15:45:22 -04002804
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002805 case 4: /* PIO Data-in */
2806 case 5: /* PIO Data-out */
2807 return ATA_PROT_PIO;
Jeff Garzikb0955182005-05-12 15:45:22 -04002808
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002809 case 0: /* Hard Reset */
2810 case 1: /* SRST */
2811 case 8: /* Device Diagnostic */
2812 case 9: /* Device Reset */
2813 case 7: /* DMA Queued */
2814 case 12: /* FPDMA */
2815 case 15: /* Return Response Info */
2816 default: /* Reserved */
2817 break;
Jeff Garzikb0955182005-05-12 15:45:22 -04002818 }
2819
2820 return ATA_PROT_UNKNOWN;
2821}
2822
2823/**
2824 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
2825 * @qc: command structure to be initialized
Jeff Garzikb0955182005-05-12 15:45:22 -04002826 *
2827 * Handles either 12 or 16-byte versions of the CDB.
2828 *
2829 * RETURNS:
2830 * Zero on success, non-zero on failure.
2831 */
Tejun Heoad706992006-12-17 10:45:57 +09002832static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04002833{
2834 struct ata_taskfile *tf = &(qc->tf);
Tejun Heo542b1442006-12-17 10:45:08 +09002835 struct scsi_cmnd *scmd = qc->scsicmd;
Alan Coxf79d4092006-05-22 16:55:11 +01002836 struct ata_device *dev = qc->dev;
Tejun Heoad706992006-12-17 10:45:57 +09002837 const u8 *cdb = scmd->cmnd;
Jeff Garzikb0955182005-05-12 15:45:22 -04002838
Tejun Heo542b1442006-12-17 10:45:08 +09002839 if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN)
Tejun Heo9a405252005-12-02 11:49:11 +09002840 goto invalid_fld;
Jeff Garzik8190bdb2006-05-24 01:53:39 -04002841
Alan Coxae8d4ee2007-11-04 22:05:49 -05002842 /*
Jeff Garzikb0955182005-05-12 15:45:22 -04002843 * 12 and 16 byte CDBs use different offsets to
2844 * provide the various register values.
2845 */
Tejun Heo542b1442006-12-17 10:45:08 +09002846 if (cdb[0] == ATA_16) {
Jeff Garzikb0955182005-05-12 15:45:22 -04002847 /*
2848 * 16-byte CDB - may contain extended commands.
2849 *
2850 * If that is the case, copy the upper byte register values.
2851 */
Tejun Heo542b1442006-12-17 10:45:08 +09002852 if (cdb[1] & 0x01) {
2853 tf->hob_feature = cdb[3];
2854 tf->hob_nsect = cdb[5];
2855 tf->hob_lbal = cdb[7];
2856 tf->hob_lbam = cdb[9];
2857 tf->hob_lbah = cdb[11];
Jeff Garzikb0955182005-05-12 15:45:22 -04002858 tf->flags |= ATA_TFLAG_LBA48;
2859 } else
2860 tf->flags &= ~ATA_TFLAG_LBA48;
2861
2862 /*
2863 * Always copy low byte, device and command registers.
2864 */
Tejun Heo542b1442006-12-17 10:45:08 +09002865 tf->feature = cdb[4];
2866 tf->nsect = cdb[6];
2867 tf->lbal = cdb[8];
2868 tf->lbam = cdb[10];
2869 tf->lbah = cdb[12];
2870 tf->device = cdb[13];
2871 tf->command = cdb[14];
Jeff Garzikb0955182005-05-12 15:45:22 -04002872 } else {
2873 /*
2874 * 12-byte CDB - incapable of extended commands.
2875 */
2876 tf->flags &= ~ATA_TFLAG_LBA48;
2877
Tejun Heo542b1442006-12-17 10:45:08 +09002878 tf->feature = cdb[3];
2879 tf->nsect = cdb[4];
2880 tf->lbal = cdb[5];
2881 tf->lbam = cdb[6];
2882 tf->lbah = cdb[7];
2883 tf->device = cdb[8];
2884 tf->command = cdb[9];
Jeff Garzikb0955182005-05-12 15:45:22 -04002885 }
Albert Leefa4453c2007-06-07 15:52:07 +08002886
2887 /* enforce correct master/slave bit */
2888 tf->device = dev->devno ?
2889 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
Jeff Garzikb0955182005-05-12 15:45:22 -04002890
Tejun Heobd30add2009-09-03 16:08:11 +09002891 /* READ/WRITE LONG use a non-standard sect_size */
2892 qc->sect_size = ATA_SECT_SIZE;
2893 switch (tf->command) {
2894 case ATA_CMD_READ_LONG:
2895 case ATA_CMD_READ_LONG_ONCE:
2896 case ATA_CMD_WRITE_LONG:
2897 case ATA_CMD_WRITE_LONG_ONCE:
2898 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1)
2899 goto invalid_fld;
2900 qc->sect_size = scsi_bufflen(scmd);
2901 }
2902
2903 /*
2904 * Set flags so that all registers will be written, pass on
2905 * write indication (used for PIO/DMA setup), result TF is
2906 * copied back and we don't whine too much about its failure.
2907 */
Douglas Gilbertbc496ed2010-02-01 13:11:38 -05002908 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heobd30add2009-09-03 16:08:11 +09002909 if (scmd->sc_data_direction == DMA_TO_DEVICE)
2910 tf->flags |= ATA_TFLAG_WRITE;
2911
2912 qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
2913
2914 /*
2915 * Set transfer length.
2916 *
2917 * TODO: find out if we need to do more here to
2918 * cover scatter/gather case.
2919 */
2920 ata_qc_set_pc_nbytes(qc);
2921
2922 /* We may not issue DMA commands if no DMA mode is set */
2923 if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0)
2924 goto invalid_fld;
2925
Albert Lee1dce5892007-06-07 15:49:22 +08002926 /* sanity check for pio multi commands */
2927 if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf))
2928 goto invalid_fld;
2929
2930 if (is_multi_taskfile(tf)) {
2931 unsigned int multi_count = 1 << (cdb[1] >> 5);
2932
2933 /* compare the passed through multi_count
2934 * with the cached multi_count of libata
2935 */
2936 if (multi_count != dev->multi_count)
2937 ata_dev_printk(dev, KERN_WARNING,
2938 "invalid multi_count %u ignored\n",
2939 multi_count);
Alan Coxd26fc952007-07-06 19:13:52 -04002940 }
Albert Lee1dce5892007-06-07 15:49:22 +08002941
Jeff Garzikb0955182005-05-12 15:45:22 -04002942 /*
2943 * Filter SET_FEATURES - XFER MODE command -- otherwise,
2944 * SET_FEATURES - XFER MODE must be preceded/succeeded
2945 * by an update to hardware-specific registers for each
2946 * controller (i.e. the reason for ->set_piomode(),
2947 * ->set_dmamode(), and ->post_set_mode() hooks).
2948 */
Tejun Heobd30add2009-09-03 16:08:11 +09002949 if (tf->command == ATA_CMD_SET_FEATURES &&
2950 tf->feature == SETFEATURES_XFER)
Tejun Heo9a405252005-12-02 11:49:11 +09002951 goto invalid_fld;
Jeff Garzikb0955182005-05-12 15:45:22 -04002952
2953 /*
Tejun Heobd30add2009-09-03 16:08:11 +09002954 * Filter TPM commands by default. These provide an
2955 * essentially uncontrolled encrypted "back door" between
2956 * applications and the disk. Set libata.allow_tpm=1 if you
2957 * have a real reason for wanting to use them. This ensures
2958 * that installed software cannot easily mess stuff up without
2959 * user intent. DVR type users will probably ship with this enabled
2960 * for movie content management.
Jeff Garzikb0955182005-05-12 15:45:22 -04002961 *
Tejun Heobd30add2009-09-03 16:08:11 +09002962 * Note that for ATA8 we can issue a DCS change and DCS freeze lock
2963 * for this and should do in future but that it is not sufficient as
2964 * DCS is an optional feature set. Thus we also do the software filter
2965 * so that we comply with the TC consortium stated goal that the user
2966 * can turn off TC features of their system.
Jeff Garzikb0955182005-05-12 15:45:22 -04002967 */
Tejun Heobd30add2009-09-03 16:08:11 +09002968 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm)
2969 goto invalid_fld;
Tejun Heoe61e0672006-05-15 20:57:40 +09002970
Jeff Garzikb0955182005-05-12 15:45:22 -04002971 return 0;
Tejun Heo9a405252005-12-02 11:49:11 +09002972
2973 invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +09002974 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
Tejun Heo9a405252005-12-02 11:49:11 +09002975 /* "Invalid field in cdb" */
2976 return 1;
Jeff Garzikb0955182005-05-12 15:45:22 -04002977}
2978
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002979static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
2980{
2981 struct ata_taskfile *tf = &qc->tf;
2982 struct scsi_cmnd *scmd = qc->scsicmd;
2983 struct ata_device *dev = qc->dev;
2984 const u8 *cdb = scmd->cmnd;
2985 u64 block;
2986 u32 n_block;
2987 u32 size;
2988 void *buf;
2989
2990 /* we may not issue DMA commands if no DMA mode is set */
2991 if (unlikely(!dev->dma_mode))
2992 goto invalid_fld;
2993
2994 if (unlikely(scmd->cmd_len < 16))
2995 goto invalid_fld;
2996 scsi_16_lba_len(cdb, &block, &n_block);
2997
2998 /* for now we only support WRITE SAME with the unmap bit set */
2999 if (unlikely(!(cdb[1] & 0x8)))
3000 goto invalid_fld;
3001
3002 /*
3003 * WRITE SAME always has a sector sized buffer as payload, this
3004 * should never be a multiple entry S/G list.
3005 */
3006 if (!scsi_sg_count(scmd))
3007 goto invalid_fld;
3008
3009 buf = page_address(sg_page(scsi_sglist(scmd)));
Martin K. Petersend0634c42009-11-26 12:00:43 -05003010 size = ata_set_lba_range_entries(buf, 512, block, n_block);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003011
3012 tf->protocol = ATA_PROT_DMA;
3013 tf->hob_feature = 0;
3014 tf->feature = ATA_DSM_TRIM;
3015 tf->hob_nsect = (size / 512) >> 8;
3016 tf->nsect = size / 512;
3017 tf->command = ATA_CMD_DSM;
3018 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
3019 ATA_TFLAG_WRITE;
3020
3021 ata_qc_set_pc_nbytes(qc);
3022
3023 return 0;
3024
3025 invalid_fld:
3026 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
3027 /* "Invalid field in cdb" */
3028 return 1;
3029}
3030
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031/**
3032 * ata_get_xlat_func - check if SCSI to ATA translation is possible
3033 * @dev: ATA device
3034 * @cmd: SCSI command opcode to consider
3035 *
3036 * Look up the SCSI command given, and determine whether the
3037 * SCSI command is to be translated or simulated.
3038 *
3039 * RETURNS:
3040 * Pointer to translation function if possible, %NULL if not.
3041 */
3042
3043static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
3044{
3045 switch (cmd) {
3046 case READ_6:
3047 case READ_10:
3048 case READ_16:
3049
3050 case WRITE_6:
3051 case WRITE_10:
3052 case WRITE_16:
3053 return ata_scsi_rw_xlat;
3054
Christoph Hellwig0cdd6eb2009-12-10 10:36:01 +01003055 case WRITE_SAME_16:
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003056 return ata_scsi_write_same_xlat;
3057
Linus Torvalds1da177e2005-04-16 15:20:36 -07003058 case SYNCHRONIZE_CACHE:
3059 if (ata_try_flush_cache(dev))
3060 return ata_scsi_flush_xlat;
3061 break;
3062
3063 case VERIFY:
3064 case VERIFY_16:
3065 return ata_scsi_verify_xlat;
Jeff Garzikb0955182005-05-12 15:45:22 -04003066
3067 case ATA_12:
3068 case ATA_16:
3069 return ata_scsi_pass_thru;
Jeff Garzikda613962005-08-29 19:01:43 -04003070
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04003071 case START_STOP:
3072 return ata_scsi_start_stop_xlat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003073 }
3074
3075 return NULL;
3076}
3077
3078/**
3079 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
3080 * @ap: ATA port to which the command was being sent
3081 * @cmd: SCSI command to dump
3082 *
3083 * Prints the contents of a SCSI command via printk().
3084 */
3085
3086static inline void ata_scsi_dump_cdb(struct ata_port *ap,
3087 struct scsi_cmnd *cmd)
3088{
3089#ifdef ATA_DEBUG
3090 struct scsi_device *scsidev = cmd->device;
3091 u8 *scsicmd = cmd->cmnd;
3092
3093 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
Tejun Heo44877b42007-02-21 01:06:51 +09003094 ap->print_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 scsidev->channel, scsidev->id, scsidev->lun,
3096 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
3097 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
3098 scsicmd[8]);
3099#endif
3100}
3101
Tejun Heo542b1442006-12-17 10:45:08 +09003102static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
Tejun Heo2115ea92006-05-15 21:03:39 +09003103 void (*done)(struct scsi_cmnd *),
3104 struct ata_device *dev)
Brian Kingeb3f0f92006-03-23 17:30:02 -06003105{
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003106 u8 scsi_op = scmd->cmnd[0];
3107 ata_xlat_func_t xlat_func;
Tejun Heo2115ea92006-05-15 21:03:39 +09003108 int rc = 0;
3109
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003110 if (dev->class == ATA_DEV_ATA) {
3111 if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
3112 goto bad_cdb_len;
3113
3114 xlat_func = ata_get_xlat_func(dev, scsi_op);
3115 } else {
3116 if (unlikely(!scmd->cmd_len))
3117 goto bad_cdb_len;
3118
3119 xlat_func = NULL;
3120 if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
3121 /* relay SCSI command to ATAPI device */
Mark Lord607126c2007-11-15 13:13:59 +09003122 int len = COMMAND_SIZE(scsi_op);
3123 if (unlikely(len > scmd->cmd_len || len > dev->cdb_len))
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003124 goto bad_cdb_len;
3125
3126 xlat_func = atapi_xlat;
3127 } else {
3128 /* ATA_16 passthru, treat as an ATA command */
3129 if (unlikely(scmd->cmd_len > 16))
3130 goto bad_cdb_len;
3131
3132 xlat_func = ata_get_xlat_func(dev, scsi_op);
3133 }
Tejun Heo2e5704f2006-12-17 10:46:33 +09003134 }
3135
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003136 if (xlat_func)
3137 rc = ata_scsi_translate(dev, scmd, done, xlat_func);
3138 else
3139 ata_scsi_simulate(dev, scmd, done);
Tejun Heo2115ea92006-05-15 21:03:39 +09003140
3141 return rc;
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003142
3143 bad_cdb_len:
3144 DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
3145 scmd->cmd_len, scsi_op, dev->cdb_len);
3146 scmd->result = DID_ERROR << 16;
3147 done(scmd);
3148 return 0;
Brian Kingeb3f0f92006-03-23 17:30:02 -06003149}
3150
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151/**
3152 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
3153 * @cmd: SCSI command to be sent
3154 * @done: Completion function, called when command is complete
3155 *
3156 * In some cases, this function translates SCSI commands into
3157 * ATA taskfiles, and queues the taskfiles to be sent to
3158 * hardware. In other cases, this function simulates a
3159 * SCSI device by evaluating and responding to certain
3160 * SCSI commands. This creates the overall effect of
3161 * ATA and ATAPI devices appearing as SCSI devices.
3162 *
3163 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003164 * Releases scsi-layer-held lock, and obtains host lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165 *
3166 * RETURNS:
Tejun Heo2115ea92006-05-15 21:03:39 +09003167 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
3168 * 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
3171{
3172 struct ata_port *ap;
3173 struct ata_device *dev;
3174 struct scsi_device *scsidev = cmd->device;
Jeff Garzik005a5a02005-10-30 23:31:48 -05003175 struct Scsi_Host *shost = scsidev->host;
Tejun Heo2115ea92006-05-15 21:03:39 +09003176 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Jeff Garzik35bb94b2006-04-11 13:12:34 -04003178 ap = ata_shost_to_port(shost);
Jeff Garzik005a5a02005-10-30 23:31:48 -05003179
3180 spin_unlock(shost->host_lock);
Jeff Garzikba6a1302006-06-22 23:46:10 -04003181 spin_lock(ap->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182
3183 ata_scsi_dump_cdb(ap, cmd);
3184
3185 dev = ata_scsi_find_dev(ap, scsidev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06003186 if (likely(dev))
Tejun Heo2115ea92006-05-15 21:03:39 +09003187 rc = __ata_scsi_queuecmd(cmd, done, dev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06003188 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189 cmd->result = (DID_BAD_TARGET << 16);
3190 done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003191 }
3192
Jeff Garzikba6a1302006-06-22 23:46:10 -04003193 spin_unlock(ap->lock);
Jeff Garzik005a5a02005-10-30 23:31:48 -05003194 spin_lock(shost->host_lock);
Tejun Heo2115ea92006-05-15 21:03:39 +09003195 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196}
3197
3198/**
3199 * ata_scsi_simulate - simulate SCSI command on ATA device
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003200 * @dev: the target device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 * @cmd: SCSI command being sent to device.
3202 * @done: SCSI command completion function.
3203 *
3204 * Interprets and directly executes a select list of SCSI commands
3205 * that can be handled internally.
3206 *
3207 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003208 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 */
3210
Tejun Heo3373efd2006-05-15 20:57:53 +09003211void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003212 void (*done)(struct scsi_cmnd *))
3213{
3214 struct ata_scsi_args args;
Jeff Garzik057ace52005-10-22 14:27:05 -04003215 const u8 *scsicmd = cmd->cmnd;
Jeff Garzik45394142007-09-21 06:23:42 -04003216 u8 tmp8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217
Tejun Heo9a3dccc2006-01-06 09:56:18 +01003218 args.dev = dev;
3219 args.id = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 args.cmd = cmd;
3221 args.done = done;
3222
3223 switch(scsicmd[0]) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003224 /* TODO: worth improving? */
3225 case FORMAT_UNIT:
3226 ata_scsi_invalid_field(cmd, done);
3227 break;
3228
3229 case INQUIRY:
3230 if (scsicmd[1] & 2) /* is CmdDt set? */
Jeff Garzik00bd0202007-09-21 07:26:08 -04003231 ata_scsi_invalid_field(cmd, done);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003232 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
3233 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
3234 else switch (scsicmd[2]) {
3235 case 0x00:
3236 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003238 case 0x80:
3239 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003241 case 0x83:
3242 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003244 case 0x89:
3245 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 break;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003247 case 0xb0:
3248 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
3249 break;
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06003250 case 0xb1:
3251 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
3252 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003253 default:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003254 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003256 }
3257 break;
3258
3259 case MODE_SENSE:
3260 case MODE_SENSE_10:
3261 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
3262 break;
3263
3264 case MODE_SELECT: /* unconditionally return */
3265 case MODE_SELECT_10: /* bad-field-in-cdb */
3266 ata_scsi_invalid_field(cmd, done);
3267 break;
3268
3269 case READ_CAPACITY:
3270 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
3271 break;
3272
3273 case SERVICE_ACTION_IN:
3274 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
3275 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
3276 else
3277 ata_scsi_invalid_field(cmd, done);
3278 break;
3279
3280 case REPORT_LUNS:
3281 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
3282 break;
3283
3284 case REQUEST_SENSE:
3285 ata_scsi_set_sense(cmd, 0, 0, 0);
3286 cmd->result = (DRIVER_SENSE << 24);
3287 done(cmd);
3288 break;
3289
3290 /* if we reach this, then writeback caching is disabled,
3291 * turning this into a no-op.
3292 */
3293 case SYNCHRONIZE_CACHE:
3294 /* fall through */
3295
3296 /* no-op's, complete with success */
3297 case REZERO_UNIT:
3298 case SEEK_6:
3299 case SEEK_10:
3300 case TEST_UNIT_READY:
3301 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
3302 break;
3303
3304 case SEND_DIAGNOSTIC:
3305 tmp8 = scsicmd[1] & ~(1 << 3);
3306 if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4]))
3307 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
3308 else
3309 ata_scsi_invalid_field(cmd, done);
3310 break;
3311
3312 /* all other commands */
3313 default:
3314 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
3315 /* "Invalid command operation code" */
3316 done(cmd);
3317 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003318 }
3319}
3320
Tejun Heof3187192007-04-17 23:44:07 +09003321int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
3322{
3323 int i, rc;
3324
3325 for (i = 0; i < host->n_ports; i++) {
3326 struct ata_port *ap = host->ports[i];
3327 struct Scsi_Host *shost;
3328
3329 rc = -ENOMEM;
3330 shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
3331 if (!shost)
3332 goto err_alloc;
3333
3334 *(struct ata_port **)&shost->hostdata[0] = ap;
3335 ap->scsi_host = shost;
3336
3337 shost->transportt = &ata_scsi_transport_template;
3338 shost->unique_id = ap->print_id;
3339 shost->max_id = 16;
3340 shost->max_lun = 1;
3341 shost->max_channel = 1;
3342 shost->max_cmd_len = 16;
3343
Tejun Heo31cc23b2007-09-23 13:14:12 +09003344 /* Schedule policy is determined by ->qc_defer()
3345 * callback and it needs to see every deferred qc.
3346 * Set host_blocked to 1 to prevent SCSI midlayer from
3347 * automatically deferring requests.
3348 */
3349 shost->max_host_blocked = 1;
3350
Tejun Heof3187192007-04-17 23:44:07 +09003351 rc = scsi_add_host(ap->scsi_host, ap->host->dev);
3352 if (rc)
3353 goto err_add;
3354 }
3355
3356 return 0;
3357
3358 err_add:
3359 scsi_host_put(host->ports[i]->scsi_host);
3360 err_alloc:
3361 while (--i >= 0) {
3362 struct Scsi_Host *shost = host->ports[i]->scsi_host;
3363
3364 scsi_remove_host(shost);
3365 scsi_host_put(shost);
3366 }
3367 return rc;
3368}
3369
Tejun Heo1ae46312007-07-16 14:29:40 +09003370void ata_scsi_scan_host(struct ata_port *ap, int sync)
Jeff Garzik644dd0c2005-10-03 15:55:19 -04003371{
Tejun Heo1ae46312007-07-16 14:29:40 +09003372 int tries = 5;
3373 struct ata_device *last_failed_dev = NULL;
Tejun Heo41bda9c2007-08-06 18:36:24 +09003374 struct ata_link *link;
Tejun Heo1ae46312007-07-16 14:29:40 +09003375 struct ata_device *dev;
Jeff Garzik644dd0c2005-10-03 15:55:19 -04003376
Tejun Heo1ae46312007-07-16 14:29:40 +09003377 repeat:
Tejun Heo1eca4362008-11-03 20:03:17 +09003378 ata_for_each_link(link, ap, EDGE) {
3379 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003380 struct scsi_device *sdev;
3381 int channel = 0, id = 0;
Jeff Garzik3f19ee82005-10-03 21:36:41 -04003382
Tejun Heo1eca4362008-11-03 20:03:17 +09003383 if (dev->sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09003384 continue;
Tejun Heo3edebac2006-05-31 18:27:40 +09003385
Tejun Heo41bda9c2007-08-06 18:36:24 +09003386 if (ata_is_host_link(link))
3387 id = dev->devno;
3388 else
3389 channel = link->pmp;
3390
3391 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
3392 NULL);
3393 if (!IS_ERR(sdev)) {
3394 dev->sdev = sdev;
3395 scsi_device_put(sdev);
3396 }
Tejun Heo3edebac2006-05-31 18:27:40 +09003397 }
Jeff Garzik3f19ee82005-10-03 21:36:41 -04003398 }
Tejun Heo1ae46312007-07-16 14:29:40 +09003399
3400 /* If we scanned while EH was in progress or allocation
3401 * failure occurred, scan would have failed silently. Check
3402 * whether all devices are attached.
3403 */
Tejun Heo1eca4362008-11-03 20:03:17 +09003404 ata_for_each_link(link, ap, EDGE) {
3405 ata_for_each_dev(dev, link, ENABLED) {
3406 if (!dev->sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09003407 goto exit_loop;
3408 }
Tejun Heo1ae46312007-07-16 14:29:40 +09003409 }
Tejun Heo41bda9c2007-08-06 18:36:24 +09003410 exit_loop:
3411 if (!link)
Tejun Heo1ae46312007-07-16 14:29:40 +09003412 return;
3413
3414 /* we're missing some SCSI devices */
3415 if (sync) {
3416 /* If caller requested synchrnous scan && we've made
3417 * any progress, sleep briefly and repeat.
3418 */
3419 if (dev != last_failed_dev) {
3420 msleep(100);
3421 last_failed_dev = dev;
3422 goto repeat;
3423 }
3424
3425 /* We might be failing to detect boot device, give it
3426 * a few more chances.
3427 */
3428 if (--tries) {
3429 msleep(100);
3430 goto repeat;
3431 }
3432
3433 ata_port_printk(ap, KERN_ERR, "WARNING: synchronous SCSI scan "
3434 "failed without making any progress,\n"
3435 " switching to async\n");
3436 }
3437
3438 queue_delayed_work(ata_aux_wq, &ap->hotplug_task,
3439 round_jiffies_relative(HZ));
Jeff Garzik644dd0c2005-10-03 15:55:19 -04003440}
Tejun Heo0ea035a2006-05-31 18:28:01 +09003441
3442/**
3443 * ata_scsi_offline_dev - offline attached SCSI device
3444 * @dev: ATA device to offline attached SCSI device for
3445 *
3446 * This function is called from ata_eh_hotplug() and responsible
3447 * for taking the SCSI device attached to @dev offline. This
Jeff Garzikcca39742006-08-24 03:19:22 -04003448 * function is called with host lock which protects dev->sdev
Tejun Heo0ea035a2006-05-31 18:28:01 +09003449 * against clearing.
3450 *
3451 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003452 * spin_lock_irqsave(host lock)
Tejun Heo0ea035a2006-05-31 18:28:01 +09003453 *
3454 * RETURNS:
3455 * 1 if attached SCSI device exists, 0 otherwise.
3456 */
3457int ata_scsi_offline_dev(struct ata_device *dev)
3458{
3459 if (dev->sdev) {
3460 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
3461 return 1;
3462 }
3463 return 0;
3464}
Tejun Heo580b2102006-05-31 18:28:05 +09003465
3466/**
3467 * ata_scsi_remove_dev - remove attached SCSI device
3468 * @dev: ATA device to remove attached SCSI device for
3469 *
3470 * This function is called from ata_eh_scsi_hotplug() and
3471 * responsible for removing the SCSI device attached to @dev.
3472 *
3473 * LOCKING:
3474 * Kernel thread context (may sleep).
3475 */
3476static void ata_scsi_remove_dev(struct ata_device *dev)
3477{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003478 struct ata_port *ap = dev->link->ap;
Tejun Heo580b2102006-05-31 18:28:05 +09003479 struct scsi_device *sdev;
3480 unsigned long flags;
3481
3482 /* Alas, we need to grab scan_mutex to ensure SCSI device
3483 * state doesn't change underneath us and thus
3484 * scsi_device_get() always succeeds. The mutex locking can
3485 * be removed if there is __scsi_device_get() interface which
3486 * increments reference counts regardless of device state.
3487 */
Jeff Garzikcca39742006-08-24 03:19:22 -04003488 mutex_lock(&ap->scsi_host->scan_mutex);
Jeff Garzikba6a1302006-06-22 23:46:10 -04003489 spin_lock_irqsave(ap->lock, flags);
Tejun Heo580b2102006-05-31 18:28:05 +09003490
Jeff Garzikcca39742006-08-24 03:19:22 -04003491 /* clearing dev->sdev is protected by host lock */
Tejun Heo580b2102006-05-31 18:28:05 +09003492 sdev = dev->sdev;
3493 dev->sdev = NULL;
3494
3495 if (sdev) {
3496 /* If user initiated unplug races with us, sdev can go
Jeff Garzikcca39742006-08-24 03:19:22 -04003497 * away underneath us after the host lock and
Tejun Heo580b2102006-05-31 18:28:05 +09003498 * scan_mutex are released. Hold onto it.
3499 */
3500 if (scsi_device_get(sdev) == 0) {
3501 /* The following ensures the attached sdev is
3502 * offline on return from ata_scsi_offline_dev()
3503 * regardless it wins or loses the race
3504 * against this function.
3505 */
3506 scsi_device_set_state(sdev, SDEV_OFFLINE);
3507 } else {
3508 WARN_ON(1);
3509 sdev = NULL;
3510 }
3511 }
3512
Jeff Garzikba6a1302006-06-22 23:46:10 -04003513 spin_unlock_irqrestore(ap->lock, flags);
Jeff Garzikcca39742006-08-24 03:19:22 -04003514 mutex_unlock(&ap->scsi_host->scan_mutex);
Tejun Heo580b2102006-05-31 18:28:05 +09003515
3516 if (sdev) {
3517 ata_dev_printk(dev, KERN_INFO, "detaching (SCSI %s)\n",
Kay Sieversb9d5fc42009-01-06 10:44:43 -08003518 dev_name(&sdev->sdev_gendev));
Tejun Heo580b2102006-05-31 18:28:05 +09003519
3520 scsi_remove_device(sdev);
3521 scsi_device_put(sdev);
3522 }
3523}
3524
Tejun Heo41bda9c2007-08-06 18:36:24 +09003525static void ata_scsi_handle_link_detach(struct ata_link *link)
3526{
3527 struct ata_port *ap = link->ap;
3528 struct ata_device *dev;
3529
Tejun Heo1eca4362008-11-03 20:03:17 +09003530 ata_for_each_dev(dev, link, ALL) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003531 unsigned long flags;
3532
3533 if (!(dev->flags & ATA_DFLAG_DETACHED))
3534 continue;
3535
3536 spin_lock_irqsave(ap->lock, flags);
3537 dev->flags &= ~ATA_DFLAG_DETACHED;
3538 spin_unlock_irqrestore(ap->lock, flags);
3539
3540 ata_scsi_remove_dev(dev);
3541 }
3542}
3543
Tejun Heo580b2102006-05-31 18:28:05 +09003544/**
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003545 * ata_scsi_media_change_notify - send media change event
Randy Dunlapc5d0e6a2007-10-15 17:29:46 -07003546 * @dev: Pointer to the disk device with media change event
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003547 *
3548 * Tell the block layer to send a media change notification
3549 * event.
3550 *
3551 * LOCKING:
Tejun Heo854c73a2007-09-23 13:14:11 +09003552 * spin_lock_irqsave(host lock)
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003553 */
Tejun Heo854c73a2007-09-23 13:14:11 +09003554void ata_scsi_media_change_notify(struct ata_device *dev)
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003555{
Tejun Heo854c73a2007-09-23 13:14:11 +09003556 if (dev->sdev)
Jeff Garzikf26792d2007-10-29 17:18:39 -04003557 sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
3558 GFP_ATOMIC);
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003559}
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003560
3561/**
Tejun Heo580b2102006-05-31 18:28:05 +09003562 * ata_scsi_hotplug - SCSI part of hotplug
David Howells65f27f32006-11-22 14:55:48 +00003563 * @work: Pointer to ATA port to perform SCSI hotplug on
Tejun Heo580b2102006-05-31 18:28:05 +09003564 *
3565 * Perform SCSI part of hotplug. It's executed from a separate
3566 * workqueue after EH completes. This is necessary because SCSI
3567 * hot plugging requires working EH and hot unplugging is
3568 * synchronized with hot plugging with a mutex.
3569 *
3570 * LOCKING:
3571 * Kernel thread context (may sleep).
3572 */
David Howells65f27f32006-11-22 14:55:48 +00003573void ata_scsi_hotplug(struct work_struct *work)
Tejun Heo580b2102006-05-31 18:28:05 +09003574{
David Howells65f27f32006-11-22 14:55:48 +00003575 struct ata_port *ap =
3576 container_of(work, struct ata_port, hotplug_task.work);
Tejun Heo41bda9c2007-08-06 18:36:24 +09003577 int i;
Tejun Heo580b2102006-05-31 18:28:05 +09003578
Tejun Heob51e9e52006-06-29 01:29:30 +09003579 if (ap->pflags & ATA_PFLAG_UNLOADING) {
Tejun Heo580b2102006-05-31 18:28:05 +09003580 DPRINTK("ENTER/EXIT - unloading\n");
3581 return;
3582 }
3583
3584 DPRINTK("ENTER\n");
3585
Tejun Heo41bda9c2007-08-06 18:36:24 +09003586 /* Unplug detached devices. We cannot use link iterator here
3587 * because PMP links have to be scanned even if PMP is
3588 * currently not attached. Iterate manually.
3589 */
3590 ata_scsi_handle_link_detach(&ap->link);
3591 if (ap->pmp_link)
3592 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
3593 ata_scsi_handle_link_detach(&ap->pmp_link[i]);
Tejun Heo580b2102006-05-31 18:28:05 +09003594
3595 /* scan for new ones */
Tejun Heo1ae46312007-07-16 14:29:40 +09003596 ata_scsi_scan_host(ap, 0);
Tejun Heo580b2102006-05-31 18:28:05 +09003597
3598 DPRINTK("EXIT\n");
3599}
Tejun Heo83c47bc2006-05-31 18:28:07 +09003600
3601/**
3602 * ata_scsi_user_scan - indication for user-initiated bus scan
3603 * @shost: SCSI host to scan
3604 * @channel: Channel to scan
3605 * @id: ID to scan
3606 * @lun: LUN to scan
3607 *
3608 * This function is called when user explicitly requests bus
3609 * scan. Set probe pending flag and invoke EH.
3610 *
3611 * LOCKING:
3612 * SCSI layer (we don't care)
3613 *
3614 * RETURNS:
3615 * Zero.
3616 */
3617static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
3618 unsigned int id, unsigned int lun)
3619{
3620 struct ata_port *ap = ata_shost_to_port(shost);
3621 unsigned long flags;
Tejun Heo41bda9c2007-08-06 18:36:24 +09003622 int devno, rc = 0;
Tejun Heo83c47bc2006-05-31 18:28:07 +09003623
3624 if (!ap->ops->error_handler)
3625 return -EOPNOTSUPP;
3626
Tejun Heo41bda9c2007-08-06 18:36:24 +09003627 if (lun != SCAN_WILD_CARD && lun)
Tejun Heo83c47bc2006-05-31 18:28:07 +09003628 return -EINVAL;
3629
Tejun Heo071f44b2008-04-07 22:47:22 +09003630 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003631 if (channel != SCAN_WILD_CARD && channel)
3632 return -EINVAL;
3633 devno = id;
3634 } else {
3635 if (id != SCAN_WILD_CARD && id)
3636 return -EINVAL;
3637 devno = channel;
3638 }
3639
Jeff Garzikba6a1302006-06-22 23:46:10 -04003640 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09003641
Tejun Heo41bda9c2007-08-06 18:36:24 +09003642 if (devno == SCAN_WILD_CARD) {
3643 struct ata_link *link;
3644
Tejun Heo1eca4362008-11-03 20:03:17 +09003645 ata_for_each_link(link, ap, EDGE) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003646 struct ata_eh_info *ehi = &link->eh_info;
Tejun Heob558edd2008-01-24 00:05:14 +09003647 ehi->probe_mask |= ATA_ALL_DEVICES;
Tejun Heocf480622008-01-24 00:05:14 +09003648 ehi->action |= ATA_EH_RESET;
Tejun Heo41bda9c2007-08-06 18:36:24 +09003649 }
Tejun Heo83c47bc2006-05-31 18:28:07 +09003650 } else {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003651 struct ata_device *dev = ata_find_dev(ap, devno);
Tejun Heo83c47bc2006-05-31 18:28:07 +09003652
3653 if (dev) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003654 struct ata_eh_info *ehi = &dev->link->eh_info;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003655 ehi->probe_mask |= 1 << dev->devno;
Tejun Heocf480622008-01-24 00:05:14 +09003656 ehi->action |= ATA_EH_RESET;
Tejun Heo83c47bc2006-05-31 18:28:07 +09003657 } else
3658 rc = -EINVAL;
3659 }
3660
Tejun Heo309afcb2006-09-30 18:07:17 +09003661 if (rc == 0) {
Tejun Heo83c47bc2006-05-31 18:28:07 +09003662 ata_port_schedule_eh(ap);
Tejun Heo309afcb2006-09-30 18:07:17 +09003663 spin_unlock_irqrestore(ap->lock, flags);
3664 ata_port_wait_eh(ap);
3665 } else
3666 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09003667
3668 return rc;
3669}
zhao, forrest3057ac32006-06-12 12:01:34 +08003670
3671/**
Tejun Heod0171262006-06-12 22:51:14 +09003672 * ata_scsi_dev_rescan - initiate scsi_rescan_device()
David Howells65f27f32006-11-22 14:55:48 +00003673 * @work: Pointer to ATA port to perform scsi_rescan_device()
zhao, forrest3057ac32006-06-12 12:01:34 +08003674 *
Tejun Heod0171262006-06-12 22:51:14 +09003675 * After ATA pass thru (SAT) commands are executed successfully,
3676 * libata need to propagate the changes to SCSI layer. This
3677 * function must be executed from ata_aux_wq such that sdev
3678 * attach/detach don't race with rescan.
zhao, forrest3057ac32006-06-12 12:01:34 +08003679 *
Tejun Heod0171262006-06-12 22:51:14 +09003680 * LOCKING:
3681 * Kernel thread context (may sleep).
zhao, forrest3057ac32006-06-12 12:01:34 +08003682 */
David Howells65f27f32006-11-22 14:55:48 +00003683void ata_scsi_dev_rescan(struct work_struct *work)
zhao, forrest3057ac32006-06-12 12:01:34 +08003684{
David Howells65f27f32006-11-22 14:55:48 +00003685 struct ata_port *ap =
3686 container_of(work, struct ata_port, scsi_rescan_task);
Tejun Heo41bda9c2007-08-06 18:36:24 +09003687 struct ata_link *link;
Tejun Heof58229f2007-08-06 18:36:23 +09003688 struct ata_device *dev;
Tejun Heof84e7e42006-11-22 11:21:31 +09003689 unsigned long flags;
zhao, forrest3057ac32006-06-12 12:01:34 +08003690
Tejun Heof84e7e42006-11-22 11:21:31 +09003691 spin_lock_irqsave(ap->lock, flags);
zhao, forrest3057ac32006-06-12 12:01:34 +08003692
Tejun Heo1eca4362008-11-03 20:03:17 +09003693 ata_for_each_link(link, ap, EDGE) {
3694 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003695 struct scsi_device *sdev = dev->sdev;
Tejun Heof84e7e42006-11-22 11:21:31 +09003696
Tejun Heo1eca4362008-11-03 20:03:17 +09003697 if (!sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09003698 continue;
3699 if (scsi_device_get(sdev))
3700 continue;
Tejun Heof84e7e42006-11-22 11:21:31 +09003701
Tejun Heo41bda9c2007-08-06 18:36:24 +09003702 spin_unlock_irqrestore(ap->lock, flags);
3703 scsi_rescan_device(&(sdev->sdev_gendev));
3704 scsi_device_put(sdev);
3705 spin_lock_irqsave(ap->lock, flags);
3706 }
zhao, forrest3057ac32006-06-12 12:01:34 +08003707 }
Tejun Heof84e7e42006-11-22 11:21:31 +09003708
3709 spin_unlock_irqrestore(ap->lock, flags);
zhao, forrest3057ac32006-06-12 12:01:34 +08003710}
Brian King80289162006-08-07 14:27:31 -05003711
3712/**
3713 * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
Jeff Garzik4f931372006-09-29 05:07:25 -04003714 * @host: ATA host container for all SAS ports
Brian King80289162006-08-07 14:27:31 -05003715 * @port_info: Information from low-level host driver
Jeff Garzikcca39742006-08-24 03:19:22 -04003716 * @shost: SCSI host that the scsi device is attached to
Brian King80289162006-08-07 14:27:31 -05003717 *
3718 * LOCKING:
3719 * PCI/etc. bus probe sem.
3720 *
3721 * RETURNS:
3722 * ata_port pointer on success / NULL on failure.
3723 */
3724
Jeff Garzikcca39742006-08-24 03:19:22 -04003725struct ata_port *ata_sas_port_alloc(struct ata_host *host,
Brian King80289162006-08-07 14:27:31 -05003726 struct ata_port_info *port_info,
Jeff Garzikcca39742006-08-24 03:19:22 -04003727 struct Scsi_Host *shost)
Brian King80289162006-08-07 14:27:31 -05003728{
Tejun Heof3187192007-04-17 23:44:07 +09003729 struct ata_port *ap;
Brian King80289162006-08-07 14:27:31 -05003730
Tejun Heof3187192007-04-17 23:44:07 +09003731 ap = ata_port_alloc(host);
Brian King80289162006-08-07 14:27:31 -05003732 if (!ap)
3733 return NULL;
3734
Tejun Heof3187192007-04-17 23:44:07 +09003735 ap->port_no = 0;
Jeff Garzikcca39742006-08-24 03:19:22 -04003736 ap->lock = shost->host_lock;
Tejun Heof3187192007-04-17 23:44:07 +09003737 ap->pio_mask = port_info->pio_mask;
3738 ap->mwdma_mask = port_info->mwdma_mask;
3739 ap->udma_mask = port_info->udma_mask;
3740 ap->flags |= port_info->flags;
3741 ap->ops = port_info->port_ops;
3742 ap->cbl = ATA_CBL_SATA;
3743
Brian King80289162006-08-07 14:27:31 -05003744 return ap;
3745}
3746EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
3747
3748/**
3749 * ata_sas_port_start - Set port up for dma.
3750 * @ap: Port to initialize
3751 *
3752 * Called just after data structures for each port are
James Bottomleydde20202008-02-19 11:36:56 +01003753 * initialized.
Brian King80289162006-08-07 14:27:31 -05003754 *
3755 * May be used as the port_start() entry in ata_port_operations.
3756 *
3757 * LOCKING:
3758 * Inherited from caller.
3759 */
3760int ata_sas_port_start(struct ata_port *ap)
3761{
James Bottomleydde20202008-02-19 11:36:56 +01003762 return 0;
Brian King80289162006-08-07 14:27:31 -05003763}
3764EXPORT_SYMBOL_GPL(ata_sas_port_start);
3765
3766/**
3767 * ata_port_stop - Undo ata_sas_port_start()
3768 * @ap: Port to shut down
3769 *
Brian King80289162006-08-07 14:27:31 -05003770 * May be used as the port_stop() entry in ata_port_operations.
3771 *
3772 * LOCKING:
3773 * Inherited from caller.
3774 */
3775
3776void ata_sas_port_stop(struct ata_port *ap)
3777{
Brian King80289162006-08-07 14:27:31 -05003778}
3779EXPORT_SYMBOL_GPL(ata_sas_port_stop);
3780
3781/**
3782 * ata_sas_port_init - Initialize a SATA device
3783 * @ap: SATA port to initialize
3784 *
3785 * LOCKING:
3786 * PCI/etc. bus probe sem.
3787 *
3788 * RETURNS:
3789 * Zero on success, non-zero on error.
3790 */
3791
3792int ata_sas_port_init(struct ata_port *ap)
3793{
3794 int rc = ap->ops->port_start(ap);
3795
Tejun Heof3187192007-04-17 23:44:07 +09003796 if (!rc) {
3797 ap->print_id = ata_print_id++;
Brian King80289162006-08-07 14:27:31 -05003798 rc = ata_bus_probe(ap);
Tejun Heof3187192007-04-17 23:44:07 +09003799 }
Brian King80289162006-08-07 14:27:31 -05003800
3801 return rc;
3802}
3803EXPORT_SYMBOL_GPL(ata_sas_port_init);
3804
3805/**
3806 * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
3807 * @ap: SATA port to destroy
3808 *
3809 */
3810
3811void ata_sas_port_destroy(struct ata_port *ap)
3812{
Tejun Heof0d36ef2007-01-20 16:00:28 +09003813 if (ap->ops->port_stop)
3814 ap->ops->port_stop(ap);
Brian King80289162006-08-07 14:27:31 -05003815 kfree(ap);
3816}
3817EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
3818
3819/**
3820 * ata_sas_slave_configure - Default slave_config routine for libata devices
3821 * @sdev: SCSI device to configure
3822 * @ap: ATA port to which SCSI device is attached
3823 *
3824 * RETURNS:
3825 * Zero.
3826 */
3827
3828int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
3829{
3830 ata_scsi_sdev_config(sdev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003831 ata_scsi_dev_config(sdev, ap->link.device);
Brian King80289162006-08-07 14:27:31 -05003832 return 0;
3833}
3834EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
3835
3836/**
3837 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
3838 * @cmd: SCSI command to be sent
3839 * @done: Completion function, called when command is complete
3840 * @ap: ATA port to which the command is being sent
3841 *
3842 * RETURNS:
Brian King08475a12006-11-20 13:51:56 -06003843 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
3844 * 0 otherwise.
Brian King80289162006-08-07 14:27:31 -05003845 */
3846
3847int ata_sas_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *),
3848 struct ata_port *ap)
3849{
Brian King08475a12006-11-20 13:51:56 -06003850 int rc = 0;
3851
Brian King80289162006-08-07 14:27:31 -05003852 ata_scsi_dump_cdb(ap, cmd);
3853
Tejun Heo2486fa52008-07-31 07:52:40 +09003854 if (likely(ata_dev_enabled(ap->link.device)))
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003855 rc = __ata_scsi_queuecmd(cmd, done, ap->link.device);
Brian King80289162006-08-07 14:27:31 -05003856 else {
3857 cmd->result = (DID_BAD_TARGET << 16);
3858 done(cmd);
3859 }
Brian King08475a12006-11-20 13:51:56 -06003860 return rc;
Brian King80289162006-08-07 14:27:31 -05003861}
3862EXPORT_SYMBOL_GPL(ata_sas_queuecmd);