blob: aa56681f68dbe95194086bee551fddf0e5230dc0 [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"
Gwendal Grignoud9027472010-05-25 12:31:38 -070054#include "libata-transport.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Tejun Heo87340e92008-04-28 17:48:51 +090056#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);
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040068#define RW_RECOVERY_MPAGE 0x1
69#define RW_RECOVERY_MPAGE_LEN 12
70#define CACHE_MPAGE 0x8
71#define CACHE_MPAGE_LEN 20
72#define CONTROL_MPAGE 0xa
73#define CONTROL_MPAGE_LEN 12
74#define ALL_MPAGES 0x3f
75#define ALL_SUB_MPAGES 0xff
76
77
Jeff Garzik24f75682007-09-21 07:54:49 -040078static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040079 RW_RECOVERY_MPAGE,
80 RW_RECOVERY_MPAGE_LEN - 2,
Jeff Garzik24f75682007-09-21 07:54:49 -040081 (1 << 7), /* AWRE */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -040082 0, /* read retry count */
83 0, 0, 0, 0,
84 0, /* write retry count */
85 0, 0, 0
86};
87
88static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
89 CACHE_MPAGE,
90 CACHE_MPAGE_LEN - 2,
91 0, /* contains WCE, needs to be 0 for logic */
92 0, 0, 0, 0, 0, 0, 0, 0, 0,
93 0, /* contains DRA, needs to be 0 for logic */
94 0, 0, 0, 0, 0, 0, 0
95};
96
97static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
98 CONTROL_MPAGE,
99 CONTROL_MPAGE_LEN - 2,
100 2, /* DSENSE=0, GLTSD=1 */
101 0, /* [QAM+QERR may be 1, see 05-359r1] */
102 0, 0, 0, 0, 0xff, 0xff,
103 0, 30 /* extended self test time, see 05-359r1 */
104};
105
Tejun Heoc93b2632010-09-01 17:50:04 +0200106static const char *ata_lpm_policy_names[] = {
107 [ATA_LPM_UNKNOWN] = "max_performance",
108 [ATA_LPM_MAX_POWER] = "max_performance",
109 [ATA_LPM_MED_POWER] = "medium_power",
110 [ATA_LPM_MIN_POWER] = "min_power",
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400111};
112
Tejun Heoc93b2632010-09-01 17:50:04 +0200113static ssize_t ata_scsi_lpm_store(struct device *dev,
114 struct device_attribute *attr,
115 const char *buf, size_t count)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400116{
Tony Jonesee959b02008-02-22 00:13:36 +0100117 struct Scsi_Host *shost = class_to_shost(dev);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400118 struct ata_port *ap = ata_shost_to_port(shost);
Tejun Heoc93b2632010-09-01 17:50:04 +0200119 enum ata_lpm_policy policy;
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400120
Tejun Heoc93b2632010-09-01 17:50:04 +0200121 /* UNKNOWN is internal state, iterate from MAX_POWER */
122 for (policy = ATA_LPM_MAX_POWER;
123 policy < ARRAY_SIZE(ata_lpm_policy_names); policy++) {
124 const char *name = ata_lpm_policy_names[policy];
125
126 if (strncmp(name, buf, strlen(name)) == 0)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400127 break;
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400128 }
Tejun Heoc93b2632010-09-01 17:50:04 +0200129 if (policy == ARRAY_SIZE(ata_lpm_policy_names))
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400130 return -EINVAL;
131
132 ata_lpm_schedule(ap, policy);
133 return count;
134}
135
Tejun Heoc93b2632010-09-01 17:50:04 +0200136static ssize_t ata_scsi_lpm_show(struct device *dev,
137 struct device_attribute *attr, char *buf)
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400138{
Tony Jonesee959b02008-02-22 00:13:36 +0100139 struct Scsi_Host *shost = class_to_shost(dev);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400140 struct ata_port *ap = ata_shost_to_port(shost);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400141
Tejun Heoc93b2632010-09-01 17:50:04 +0200142 if (ap->lpm_policy >= ARRAY_SIZE(ata_lpm_policy_names))
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400143 return -EINVAL;
144
Tejun Heoc93b2632010-09-01 17:50:04 +0200145 return snprintf(buf, PAGE_SIZE, "%s\n",
146 ata_lpm_policy_names[ap->lpm_policy]);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400147}
Tony Jonesee959b02008-02-22 00:13:36 +0100148DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
Tejun Heoc93b2632010-09-01 17:50:04 +0200149 ata_scsi_lpm_show, ata_scsi_lpm_store);
Tony Jonesee959b02008-02-22 00:13:36 +0100150EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
Kristen Carlson Accardica773292007-10-25 00:58:59 -0400151
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200152static ssize_t ata_scsi_park_show(struct device *device,
153 struct device_attribute *attr, char *buf)
154{
155 struct scsi_device *sdev = to_scsi_device(device);
156 struct ata_port *ap;
157 struct ata_link *link;
158 struct ata_device *dev;
Elias Oltmannsa4641892008-11-03 19:01:08 +0900159 unsigned long flags, now;
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200160 unsigned int uninitialized_var(msecs);
161 int rc = 0;
162
163 ap = ata_shost_to_port(sdev->host);
164
165 spin_lock_irqsave(ap->lock, flags);
166 dev = ata_scsi_find_dev(ap, sdev);
167 if (!dev) {
168 rc = -ENODEV;
169 goto unlock;
170 }
171 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
172 rc = -EOPNOTSUPP;
173 goto unlock;
174 }
175
176 link = dev->link;
Elias Oltmannsa4641892008-11-03 19:01:08 +0900177 now = jiffies;
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200178 if (ap->pflags & ATA_PFLAG_EH_IN_PROGRESS &&
179 link->eh_context.unloaded_mask & (1 << dev->devno) &&
Elias Oltmannsa4641892008-11-03 19:01:08 +0900180 time_after(dev->unpark_deadline, now))
181 msecs = jiffies_to_msecs(dev->unpark_deadline - now);
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200182 else
183 msecs = 0;
184
185unlock:
186 spin_unlock_irq(ap->lock);
187
188 return rc ? rc : snprintf(buf, 20, "%u\n", msecs);
189}
190
191static ssize_t ata_scsi_park_store(struct device *device,
192 struct device_attribute *attr,
193 const char *buf, size_t len)
194{
195 struct scsi_device *sdev = to_scsi_device(device);
196 struct ata_port *ap;
197 struct ata_device *dev;
198 long int input;
199 unsigned long flags;
200 int rc;
201
202 rc = strict_strtol(buf, 10, &input);
203 if (rc || input < -2)
204 return -EINVAL;
205 if (input > ATA_TMOUT_MAX_PARK) {
206 rc = -EOVERFLOW;
207 input = ATA_TMOUT_MAX_PARK;
208 }
209
210 ap = ata_shost_to_port(sdev->host);
211
212 spin_lock_irqsave(ap->lock, flags);
213 dev = ata_scsi_find_dev(ap, sdev);
214 if (unlikely(!dev)) {
215 rc = -ENODEV;
216 goto unlock;
217 }
218 if (dev->class != ATA_DEV_ATA) {
219 rc = -EOPNOTSUPP;
220 goto unlock;
221 }
222
223 if (input >= 0) {
224 if (dev->flags & ATA_DFLAG_NO_UNLOAD) {
225 rc = -EOPNOTSUPP;
226 goto unlock;
227 }
228
229 dev->unpark_deadline = ata_deadline(jiffies, input);
230 dev->link->eh_info.dev_action[dev->devno] |= ATA_EH_PARK;
231 ata_port_schedule_eh(ap);
232 complete(&ap->park_req_pending);
233 } else {
234 switch (input) {
235 case -1:
236 dev->flags &= ~ATA_DFLAG_NO_UNLOAD;
237 break;
238 case -2:
239 dev->flags |= ATA_DFLAG_NO_UNLOAD;
240 break;
241 }
242 }
243unlock:
244 spin_unlock_irqrestore(ap->lock, flags);
245
246 return rc ? rc : len;
247}
248DEVICE_ATTR(unload_heads, S_IRUGO | S_IWUSR,
249 ata_scsi_park_show, ata_scsi_park_store);
250EXPORT_SYMBOL_GPL(dev_attr_unload_heads);
251
Tejun Heof0761be2008-04-28 17:16:52 +0900252static void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
253{
254 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
255
256 scsi_build_sense_buffer(0, cmd->sense_buffer, sk, asc, ascq);
257}
258
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700259static ssize_t
260ata_scsi_em_message_store(struct device *dev, struct device_attribute *attr,
261 const char *buf, size_t count)
262{
263 struct Scsi_Host *shost = class_to_shost(dev);
264 struct ata_port *ap = ata_shost_to_port(shost);
265 if (ap->ops->em_store && (ap->flags & ATA_FLAG_EM))
266 return ap->ops->em_store(ap, buf, count);
267 return -EINVAL;
268}
269
270static ssize_t
271ata_scsi_em_message_show(struct device *dev, struct device_attribute *attr,
272 char *buf)
273{
274 struct Scsi_Host *shost = class_to_shost(dev);
275 struct ata_port *ap = ata_shost_to_port(shost);
276
277 if (ap->ops->em_show && (ap->flags & ATA_FLAG_EM))
278 return ap->ops->em_show(ap, buf);
279 return -EINVAL;
280}
Vitaly Mayatskikhea7a5ed2009-05-04 15:48:45 +0200281DEVICE_ATTR(em_message, S_IRUGO | S_IWUSR,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700282 ata_scsi_em_message_show, ata_scsi_em_message_store);
283EXPORT_SYMBOL_GPL(dev_attr_em_message);
284
285static ssize_t
286ata_scsi_em_message_type_show(struct device *dev, struct device_attribute *attr,
287 char *buf)
288{
289 struct Scsi_Host *shost = class_to_shost(dev);
290 struct ata_port *ap = ata_shost_to_port(shost);
291
292 return snprintf(buf, 23, "%d\n", ap->em_message_type);
293}
294DEVICE_ATTR(em_message_type, S_IRUGO,
295 ata_scsi_em_message_type_show, NULL);
296EXPORT_SYMBOL_GPL(dev_attr_em_message_type);
297
298static ssize_t
299ata_scsi_activity_show(struct device *dev, struct device_attribute *attr,
300 char *buf)
301{
302 struct scsi_device *sdev = to_scsi_device(dev);
303 struct ata_port *ap = ata_shost_to_port(sdev->host);
304 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
305
306 if (ap->ops->sw_activity_show && (ap->flags & ATA_FLAG_SW_ACTIVITY))
307 return ap->ops->sw_activity_show(atadev, buf);
308 return -EINVAL;
309}
310
311static ssize_t
312ata_scsi_activity_store(struct device *dev, struct device_attribute *attr,
313 const char *buf, size_t count)
314{
315 struct scsi_device *sdev = to_scsi_device(dev);
316 struct ata_port *ap = ata_shost_to_port(sdev->host);
317 struct ata_device *atadev = ata_scsi_find_dev(ap, sdev);
318 enum sw_activity val;
319 int rc;
320
321 if (ap->ops->sw_activity_store && (ap->flags & ATA_FLAG_SW_ACTIVITY)) {
322 val = simple_strtoul(buf, NULL, 0);
323 switch (val) {
324 case OFF: case BLINK_ON: case BLINK_OFF:
325 rc = ap->ops->sw_activity_store(atadev, val);
326 if (!rc)
327 return count;
328 else
329 return rc;
330 }
331 }
332 return -EINVAL;
333}
Vitaly Mayatskikhea7a5ed2009-05-04 15:48:45 +0200334DEVICE_ATTR(sw_activity, S_IWUSR | S_IRUGO, ata_scsi_activity_show,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700335 ata_scsi_activity_store);
336EXPORT_SYMBOL_GPL(dev_attr_sw_activity);
337
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200338struct device_attribute *ata_common_sdev_attrs[] = {
339 &dev_attr_unload_heads,
340 NULL
341};
342EXPORT_SYMBOL_GPL(ata_common_sdev_attrs);
343
Douglas Gilbertae006512005-10-09 09:09:35 -0400344static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
345 void (*done)(struct scsi_cmnd *))
346{
347 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
348 /* "Invalid field in cbd" */
349 done(cmd);
350}
351
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352/**
353 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
354 * @sdev: SCSI device for which BIOS geometry is to be determined
355 * @bdev: block device associated with @sdev
356 * @capacity: capacity of SCSI device
357 * @geom: location to which geometry will be output
358 *
359 * Generic bios head/sector/cylinder calculator
360 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
361 * mapping. Some situations may arise where the disk is not
362 * bootable if this is not used.
363 *
364 * LOCKING:
365 * Defined by the SCSI layer. We don't really care.
366 *
367 * RETURNS:
368 * Zero.
369 */
370int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
371 sector_t capacity, int geom[])
372{
373 geom[0] = 255;
374 geom[1] = 63;
375 sector_div(capacity, 255*63);
376 geom[2] = capacity;
377
378 return 0;
379}
380
Jeff Garzikb0955182005-05-12 15:45:22 -0400381/**
Tejun Heod8d91292010-05-15 20:09:34 +0200382 * ata_scsi_unlock_native_capacity - unlock native capacity
383 * @sdev: SCSI device to adjust device capacity for
384 *
385 * This function is called if a partition on @sdev extends beyond
386 * the end of the device. It requests EH to unlock HPA.
387 *
388 * LOCKING:
389 * Defined by the SCSI layer. Might sleep.
390 */
391void ata_scsi_unlock_native_capacity(struct scsi_device *sdev)
392{
393 struct ata_port *ap = ata_shost_to_port(sdev->host);
394 struct ata_device *dev;
395 unsigned long flags;
396
397 spin_lock_irqsave(ap->lock, flags);
398
399 dev = ata_scsi_find_dev(ap, sdev);
400 if (dev && dev->n_sectors < dev->n_native_sectors) {
401 dev->flags |= ATA_DFLAG_UNLOCK_HPA;
402 dev->link->eh_info.action |= ATA_EH_RESET;
403 ata_port_schedule_eh(ap);
404 }
405
406 spin_unlock_irqrestore(ap->lock, flags);
407 ata_port_wait_eh(ap);
408}
409
410/**
Tejun Heo5924b742007-01-02 20:20:07 +0900411 * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
Randy Dunlap5eb66fe2009-01-20 16:28:59 -0800412 * @ap: target port
Tejun Heo5924b742007-01-02 20:20:07 +0900413 * @sdev: SCSI device to get identify data for
414 * @arg: User buffer area for identify data
415 *
416 * LOCKING:
417 * Defined by the SCSI layer. We don't really care.
418 *
419 * RETURNS:
420 * Zero on success, negative errno on error.
421 */
Jeff Garzik94be9a52009-01-16 10:17:09 -0500422static int ata_get_identity(struct ata_port *ap, struct scsi_device *sdev,
423 void __user *arg)
Tejun Heo5924b742007-01-02 20:20:07 +0900424{
Tejun Heo5924b742007-01-02 20:20:07 +0900425 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
426 u16 __user *dst = arg;
427 char buf[40];
428
429 if (!dev)
430 return -ENOMSG;
431
432 if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
433 return -EFAULT;
434
435 ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
436 if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
437 return -EFAULT;
438
439 ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
440 if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
441 return -EFAULT;
442
443 ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
444 if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
445 return -EFAULT;
446
447 return 0;
448}
449
450/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400451 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800452 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400453 * @arg: User provided data for issuing command
454 *
455 * LOCKING:
456 * Defined by the SCSI layer. We don't really care.
457 *
458 * RETURNS:
459 * Zero on success, negative errno on error.
460 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400461int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
462{
463 int rc = 0;
464 u8 scsi_cmd[MAX_COMMAND_SIZE];
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700465 u8 args[4], *argbuf = NULL, *sensebuf = NULL;
Jeff Garzikb0955182005-05-12 15:45:22 -0400466 int argsize = 0;
Mike Christie85837eb2005-11-11 16:38:53 -0600467 enum dma_data_direction data_dir;
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700468 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400469
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500470 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400471 return -EINVAL;
472
473 if (copy_from_user(args, arg, sizeof(args)))
474 return -EFAULT;
475
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700476 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
477 if (!sensebuf)
478 return -ENOMEM;
479
Jeff Garzikb0955182005-05-12 15:45:22 -0400480 memset(scsi_cmd, 0, sizeof(scsi_cmd));
481
482 if (args[3]) {
Grant Grundler295124d2010-08-17 10:56:53 -0700483 argsize = ATA_SECT_SIZE * args[3];
Jeff Garzikb0955182005-05-12 15:45:22 -0400484 argbuf = kmalloc(argsize, GFP_KERNEL);
Jeff Raubitschek54dac832005-10-04 10:21:19 -0400485 if (argbuf == NULL) {
486 rc = -ENOMEM;
487 goto error;
488 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400489
490 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
491 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400492 block count in sector count field */
Mike Christie85837eb2005-11-11 16:38:53 -0600493 data_dir = DMA_FROM_DEVICE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400494 } else {
495 scsi_cmd[1] = (3 << 1); /* Non-data */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700496 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Mike Christie85837eb2005-11-11 16:38:53 -0600497 data_dir = DMA_NONE;
Jeff Garzikb0955182005-05-12 15:45:22 -0400498 }
499
500 scsi_cmd[0] = ATA_16;
501
502 scsi_cmd[4] = args[2];
Bartlomiej Zolnierkiewicza4f19042008-10-10 22:39:20 +0200503 if (args[0] == ATA_CMD_SMART) { /* hack -- ide driver does this too */
Jeff Garzikb0955182005-05-12 15:45:22 -0400504 scsi_cmd[6] = args[3];
505 scsi_cmd[8] = args[1];
506 scsi_cmd[10] = 0x4f;
507 scsi_cmd[12] = 0xc2;
508 } else {
509 scsi_cmd[6] = args[1];
510 }
511 scsi_cmd[14] = args[0];
512
513 /* Good values for timeout and retries? Values below
514 from scsi_ioctl_send_command() for default case... */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700515 cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900516 sensebuf, (10*HZ), 5, 0, NULL);
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700517
518 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
519 u8 *desc = sensebuf + 8;
520 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
521
522 /* If we set cc then ATA pass-through will cause a
523 * check condition even if no error. Filter that. */
524 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
525 struct scsi_sense_hdr sshdr;
526 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400527 &sshdr);
528 if (sshdr.sense_key == 0 &&
529 sshdr.asc == 0 && sshdr.ascq == 0)
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700530 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
531 }
532
533 /* Send userspace a few ATA registers (same as drivers/ide) */
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400534 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
535 desc[0] == 0x09) { /* code is "ATA Descriptor" */
536 args[0] = desc[13]; /* status */
537 args[1] = desc[3]; /* error */
538 args[2] = desc[5]; /* sector count (0:7) */
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700539 if (copy_to_user(arg, args, sizeof(args)))
540 rc = -EFAULT;
541 }
542 }
543
544
545 if (cmd_result) {
Jeff Garzikb0955182005-05-12 15:45:22 -0400546 rc = -EIO;
547 goto error;
548 }
549
Jeff Garzikb0955182005-05-12 15:45:22 -0400550 if ((argbuf)
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500551 && copy_to_user(arg + sizeof(args), argbuf, argsize))
Jeff Garzikb0955182005-05-12 15:45:22 -0400552 rc = -EFAULT;
553error:
Eran Tromerbbe1fe72006-10-10 14:29:25 -0700554 kfree(sensebuf);
Jesper Juhl8f760782006-06-27 02:55:06 -0700555 kfree(argbuf);
Jeff Garzikb0955182005-05-12 15:45:22 -0400556 return rc;
557}
558
559/**
560 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800561 * @scsidev: Device to which we are issuing command
Jeff Garzikb0955182005-05-12 15:45:22 -0400562 * @arg: User provided data for issuing command
563 *
564 * LOCKING:
565 * Defined by the SCSI layer. We don't really care.
566 *
567 * RETURNS:
568 * Zero on success, negative errno on error.
569 */
570int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
571{
572 int rc = 0;
573 u8 scsi_cmd[MAX_COMMAND_SIZE];
David Milburnaf068bd2007-01-30 00:59:15 -0800574 u8 args[7], *sensebuf = NULL;
575 int cmd_result;
Jeff Garzikb0955182005-05-12 15:45:22 -0400576
Randy Dunlapc893a3a2006-01-28 13:15:32 -0500577 if (arg == NULL)
Jeff Garzikb0955182005-05-12 15:45:22 -0400578 return -EINVAL;
579
580 if (copy_from_user(args, arg, sizeof(args)))
581 return -EFAULT;
582
David Milburnaf068bd2007-01-30 00:59:15 -0800583 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
584 if (!sensebuf)
585 return -ENOMEM;
586
Jeff Garzikb0955182005-05-12 15:45:22 -0400587 memset(scsi_cmd, 0, sizeof(scsi_cmd));
588 scsi_cmd[0] = ATA_16;
589 scsi_cmd[1] = (3 << 1); /* Non-data */
David Milburnaf068bd2007-01-30 00:59:15 -0800590 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
Jeff Garzikb0955182005-05-12 15:45:22 -0400591 scsi_cmd[4] = args[1];
592 scsi_cmd[6] = args[2];
593 scsi_cmd[8] = args[3];
594 scsi_cmd[10] = args[4];
595 scsi_cmd[12] = args[5];
Mark Lord277239f2007-03-30 17:45:52 -0400596 scsi_cmd[13] = args[6] & 0x4f;
Jeff Garzikb0955182005-05-12 15:45:22 -0400597 scsi_cmd[14] = args[0];
598
Jeff Garzikb0955182005-05-12 15:45:22 -0400599 /* Good values for timeout and retries? Values below
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500600 from scsi_ioctl_send_command() for default case... */
David Milburnaf068bd2007-01-30 00:59:15 -0800601 cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
FUJITA Tomonorif4f4e472008-12-04 14:24:39 +0900602 sensebuf, (10*HZ), 5, 0, NULL);
Jeff Garzikb0955182005-05-12 15:45:22 -0400603
David Milburnaf068bd2007-01-30 00:59:15 -0800604 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
605 u8 *desc = sensebuf + 8;
606 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
607
608 /* If we set cc then ATA pass-through will cause a
609 * check condition even if no error. Filter that. */
610 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
611 struct scsi_sense_hdr sshdr;
612 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
613 &sshdr);
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400614 if (sshdr.sense_key == 0 &&
615 sshdr.asc == 0 && sshdr.ascq == 0)
David Milburnaf068bd2007-01-30 00:59:15 -0800616 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
617 }
618
619 /* Send userspace ATA registers */
620 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
621 desc[0] == 0x09) {/* code is "ATA Descriptor" */
622 args[0] = desc[13]; /* status */
623 args[1] = desc[3]; /* error */
624 args[2] = desc[5]; /* sector count (0:7) */
625 args[3] = desc[7]; /* lbal */
626 args[4] = desc[9]; /* lbam */
627 args[5] = desc[11]; /* lbah */
628 args[6] = desc[12]; /* select */
629 if (copy_to_user(arg, args, sizeof(args)))
630 rc = -EFAULT;
631 }
632 }
633
634 if (cmd_result) {
635 rc = -EIO;
636 goto error;
637 }
638
639 error:
640 kfree(sensebuf);
Jeff Garzikb0955182005-05-12 15:45:22 -0400641 return rc;
642}
643
Alan Coxe3cf95d2009-04-09 17:31:17 +0100644static int ata_ioc32(struct ata_port *ap)
645{
646 if (ap->flags & ATA_FLAG_PIO_DMA)
647 return 1;
648 if (ap->pflags & ATA_PFLAG_PIO32)
649 return 1;
650 return 0;
651}
652
Jeff Garzik94be9a52009-01-16 10:17:09 -0500653int ata_sas_scsi_ioctl(struct ata_port *ap, struct scsi_device *scsidev,
654 int cmd, void __user *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 int val = -EINVAL, rc = -EINVAL;
Alan Coxe3cf95d2009-04-09 17:31:17 +0100657 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 switch (cmd) {
660 case ATA_IOC_GET_IO32:
Alan Coxe3cf95d2009-04-09 17:31:17 +0100661 spin_lock_irqsave(ap->lock, flags);
662 val = ata_ioc32(ap);
663 spin_unlock_irqrestore(ap->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 if (copy_to_user(arg, &val, 1))
665 return -EFAULT;
666 return 0;
667
668 case ATA_IOC_SET_IO32:
669 val = (unsigned long) arg;
Alan Coxe3cf95d2009-04-09 17:31:17 +0100670 rc = 0;
671 spin_lock_irqsave(ap->lock, flags);
672 if (ap->pflags & ATA_PFLAG_PIO32CHANGE) {
673 if (val)
674 ap->pflags |= ATA_PFLAG_PIO32;
675 else
676 ap->pflags &= ~ATA_PFLAG_PIO32;
677 } else {
678 if (val != ata_ioc32(ap))
679 rc = -EINVAL;
680 }
681 spin_unlock_irqrestore(ap->lock, flags);
682 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Tejun Heo5924b742007-01-02 20:20:07 +0900684 case HDIO_GET_IDENTITY:
Jeff Garzik94be9a52009-01-16 10:17:09 -0500685 return ata_get_identity(ap, scsidev, arg);
Tejun Heo5924b742007-01-02 20:20:07 +0900686
Jeff Garzikb0955182005-05-12 15:45:22 -0400687 case HDIO_DRIVE_CMD:
688 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
689 return -EACCES;
690 return ata_cmd_ioctl(scsidev, arg);
691
692 case HDIO_DRIVE_TASK:
693 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
694 return -EACCES;
695 return ata_task_ioctl(scsidev, arg);
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 default:
698 rc = -ENOTTY;
699 break;
700 }
701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return rc;
703}
Jeff Garzik94be9a52009-01-16 10:17:09 -0500704EXPORT_SYMBOL_GPL(ata_sas_scsi_ioctl);
705
706int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
707{
708 return ata_sas_scsi_ioctl(ata_shost_to_port(scsidev->host),
709 scsidev, cmd, arg);
710}
711EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712
713/**
714 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 * @dev: ATA device to which the new command is attached
716 * @cmd: SCSI command that originated this ATA command
717 * @done: SCSI command completion function
718 *
719 * Obtain a reference to an unused ata_queued_cmd structure,
720 * which is the basic libata structure representing a single
721 * ATA command sent to the hardware.
722 *
723 * If a command was available, fill in the SCSI-specific
724 * portions of the structure with information on the
725 * current command.
726 *
727 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400728 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 *
730 * RETURNS:
731 * Command allocated, or %NULL if none available.
732 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100733static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
734 struct scsi_cmnd *cmd,
735 void (*done)(struct scsi_cmnd *))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 struct ata_queued_cmd *qc;
738
Tejun Heo8a8bc222008-11-10 14:48:21 +0900739 qc = ata_qc_new_init(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 if (qc) {
741 qc->scsicmd = cmd;
742 qc->scsidone = done;
743
Tejun Heoff2aeb12007-12-05 16:43:11 +0900744 qc->sg = scsi_sglist(cmd);
Boaz Harrosh71201652007-09-18 17:48:50 +0200745 qc->n_elem = scsi_sg_count(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 } else {
747 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
748 done(cmd);
749 }
750
751 return qc;
752}
753
Tejun Heoaacda372008-03-18 17:47:43 +0900754static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
755{
756 struct scsi_cmnd *scmd = qc->scsicmd;
757
758 qc->extrabytes = scmd->request->extra_len;
759 qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
760}
761
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762/**
Jeff Garzikb0955182005-05-12 15:45:22 -0400763 * ata_dump_status - user friendly display of error info
764 * @id: id of the port in question
765 * @tf: ptr to filled out taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 *
Jeff Garzikb0955182005-05-12 15:45:22 -0400767 * Decode and dump the ATA error/status registers for the user so
768 * that they have some idea what really happened at the non
769 * make-believe layer.
770 *
771 * LOCKING:
772 * inherited from caller
773 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100774static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
Jeff Garzikb0955182005-05-12 15:45:22 -0400775{
776 u8 stat = tf->command, err = tf->feature;
777
778 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
779 if (stat & ATA_BUSY) {
780 printk("Busy }\n"); /* Data is not valid in this case */
781 } else {
782 if (stat & 0x40) printk("DriveReady ");
783 if (stat & 0x20) printk("DeviceFault ");
784 if (stat & 0x10) printk("SeekComplete ");
785 if (stat & 0x08) printk("DataRequest ");
786 if (stat & 0x04) printk("CorrectedError ");
787 if (stat & 0x02) printk("Index ");
788 if (stat & 0x01) printk("Error ");
789 printk("}\n");
790
791 if (err) {
792 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
793 if (err & 0x04) printk("DriveStatusError ");
794 if (err & 0x80) {
795 if (err & 0x04) printk("BadCRC ");
796 else printk("Sector ");
797 }
798 if (err & 0x40) printk("UncorrectableError ");
799 if (err & 0x10) printk("SectorIdNotFound ");
800 if (err & 0x02) printk("TrackZeroNotFound ");
801 if (err & 0x01) printk("AddrMarkNotFound ");
802 printk("}\n");
803 }
804 }
805}
806
807/**
808 * ata_to_sense_error - convert ATA error to SCSI error
Randy Dunlap8e8b77d2005-11-01 21:29:27 -0800809 * @id: ATA device number
Jeff Garzikb0955182005-05-12 15:45:22 -0400810 * @drv_stat: value contained in ATA status register
811 * @drv_err: value contained in ATA error register
812 * @sk: the sense key we'll fill out
813 * @asc: the additional sense code we'll fill out
814 * @ascq: the additional sense code qualifier we'll fill out
Tejun Heo246619d2006-05-15 20:58:16 +0900815 * @verbose: be verbose
Jeff Garzikb0955182005-05-12 15:45:22 -0400816 *
817 * Converts an ATA error into a SCSI error. Fill out pointers to
818 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
819 * format sense blocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 *
821 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -0400822 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 */
Adrian Bunk7102d232007-01-04 00:09:36 +0100824static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
825 u8 *asc, u8 *ascq, int verbose)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826{
Jeff Garzikb0955182005-05-12 15:45:22 -0400827 int i;
Jeff Garzikffe75ef2005-10-09 10:40:44 -0400828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /* Based on the 3ware driver translation table */
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100830 static const unsigned char sense_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /* BBD|ECC|ID|MAR */
832 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
833 /* BBD|ECC|ID */
834 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
835 /* ECC|MC|MARK */
836 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
837 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
838 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
839 /* MC|ID|ABRT|TRK0|MARK */
840 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
841 /* MCR|MARK */
842 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
843 /* Bad address mark */
844 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
845 /* TRK0 */
846 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
847 /* Abort & !ICRC */
848 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
849 /* Media change request */
850 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
851 /* SRV */
852 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
853 /* Media change */
854 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
855 /* ECC */
856 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
857 /* BBD - block marked bad */
858 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
859 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
860 };
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100861 static const unsigned char stat_table[][4] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 /* Must be first because BUSY means no other bits valid */
863 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
864 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
865 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
866 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
867 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
868 };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870 /*
871 * Is this an error we can process/parse
872 */
Jeff Garzikb0955182005-05-12 15:45:22 -0400873 if (drv_stat & ATA_BUSY) {
874 drv_err = 0; /* Ignore the err bits, they're invalid */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Jeff Garzikb0955182005-05-12 15:45:22 -0400877 if (drv_err) {
878 /* Look for drv_err */
879 for (i = 0; sense_table[i][0] != 0xFF; i++) {
880 /* Look for best matches first */
Jeff Garzik2e9edbf2006-03-24 09:56:57 -0500881 if ((sense_table[i][0] & drv_err) ==
Jeff Garzikb0955182005-05-12 15:45:22 -0400882 sense_table[i][0]) {
883 *sk = sense_table[i][1];
884 *asc = sense_table[i][2];
885 *ascq = sense_table[i][3];
886 goto translate_done;
887 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400889 /* No immediate match */
Tejun Heo246619d2006-05-15 20:58:16 +0900890 if (verbose)
891 printk(KERN_WARNING "ata%u: no sense translation for "
892 "error 0x%02x\n", id, drv_err);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 }
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 /* Fall back to interpreting status bits */
Jeff Garzikb0955182005-05-12 15:45:22 -0400896 for (i = 0; stat_table[i][0] != 0xFF; i++) {
897 if (stat_table[i][0] & drv_stat) {
898 *sk = stat_table[i][1];
899 *asc = stat_table[i][2];
900 *ascq = stat_table[i][3];
901 goto translate_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
Jeff Garzikb0955182005-05-12 15:45:22 -0400904 /* No error? Undecoded? */
Tejun Heo246619d2006-05-15 20:58:16 +0900905 if (verbose)
906 printk(KERN_WARNING "ata%u: no sense translation for "
907 "status: 0x%02x\n", id, drv_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908
Alan Cox2d202022006-03-21 15:53:46 +0000909 /* We need a sensible error return here, which is tricky, and one
910 that won't cause people to do things like return a disk wrongly */
911 *sk = ABORTED_COMMAND;
912 *asc = 0x00;
913 *ascq = 0x00;
Jeff Garzikb0955182005-05-12 15:45:22 -0400914
915 translate_done:
Tejun Heo246619d2006-05-15 20:58:16 +0900916 if (verbose)
917 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x "
918 "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
919 id, drv_stat, drv_err, *sk, *asc, *ascq);
Jeff Garzikb0955182005-05-12 15:45:22 -0400920 return;
921}
922
923/*
Tejun Heo750426a2006-11-14 22:37:35 +0900924 * ata_gen_passthru_sense - Generate check condition sense block.
Jeff Garzikb0955182005-05-12 15:45:22 -0400925 * @qc: Command that completed.
926 *
927 * This function is specific to the ATA descriptor format sense
928 * block specified for the ATA pass through commands. Regardless
929 * of whether the command errored or not, return a sense
930 * block. Copy all controller registers into the sense
931 * block. Clear sense key, ASC & ASCQ if there is no error.
932 *
933 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +0900934 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -0400935 */
Tejun Heo750426a2006-11-14 22:37:35 +0900936static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -0400937{
938 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +0900939 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -0400940 unsigned char *sb = cmd->sense_buffer;
941 unsigned char *desc = sb + 8;
Tejun Heo246619d2006-05-15 20:58:16 +0900942 int verbose = qc->ap->ops->error_handler == NULL;
Jeff Garzikb0955182005-05-12 15:45:22 -0400943
944 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
945
Jeff Garzik0e5dec42005-10-06 09:40:20 -0400946 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -0400947
948 /*
Jeff Garzikb0955182005-05-12 15:45:22 -0400949 * Use ata_to_sense_error() to map status register bits
950 * onto sense key, asc & ascq.
951 */
Tejun Heo058e55e2006-04-02 18:51:53 +0900952 if (qc->err_mask ||
953 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo44877b42007-02-21 01:06:51 +0900954 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
Tejun Heo246619d2006-05-15 20:58:16 +0900955 &sb[1], &sb[2], &sb[3], verbose);
Jeff Garzikb0955182005-05-12 15:45:22 -0400956 sb[1] &= 0x0f;
957 }
958
959 /*
960 * Sense data is current and format is descriptor.
961 */
962 sb[0] = 0x72;
963
964 desc[0] = 0x09;
965
Tejun Heof38621b2006-11-14 22:37:35 +0900966 /* set length of additional sense data */
967 sb[7] = 14;
968 desc[1] = 12;
Jeff Garzikb0955182005-05-12 15:45:22 -0400969
970 /*
971 * Copy registers into sense buffer.
972 */
973 desc[2] = 0x00;
974 desc[3] = tf->feature; /* == error reg */
975 desc[5] = tf->nsect;
976 desc[7] = tf->lbal;
977 desc[9] = tf->lbam;
978 desc[11] = tf->lbah;
979 desc[12] = tf->device;
980 desc[13] = tf->command; /* == status reg */
981
982 /*
983 * Fill in Extend bit, and the high order bytes
984 * if applicable.
985 */
986 if (tf->flags & ATA_TFLAG_LBA48) {
987 desc[2] |= 0x01;
988 desc[4] = tf->hob_nsect;
989 desc[6] = tf->hob_lbal;
990 desc[8] = tf->hob_lbam;
991 desc[10] = tf->hob_lbah;
992 }
993}
994
995/**
Tejun Heo750426a2006-11-14 22:37:35 +0900996 * ata_gen_ata_sense - generate a SCSI fixed sense block
Jeff Garzikb0955182005-05-12 15:45:22 -0400997 * @qc: Command that we are erroring out
998 *
Tejun Heod25614b2006-11-14 22:37:35 +0900999 * Generate sense block for a failed ATA command @qc. Descriptor
1000 * format is used to accomodate LBA48 block address.
Jeff Garzikb0955182005-05-12 15:45:22 -04001001 *
1002 * LOCKING:
Tejun Heo750426a2006-11-14 22:37:35 +09001003 * None.
Jeff Garzikb0955182005-05-12 15:45:22 -04001004 */
Tejun Heo750426a2006-11-14 22:37:35 +09001005static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04001006{
Tejun Heod25614b2006-11-14 22:37:35 +09001007 struct ata_device *dev = qc->dev;
Jeff Garzikb0955182005-05-12 15:45:22 -04001008 struct scsi_cmnd *cmd = qc->scsicmd;
Tejun Heoe61e0672006-05-15 20:57:40 +09001009 struct ata_taskfile *tf = &qc->result_tf;
Jeff Garzikb0955182005-05-12 15:45:22 -04001010 unsigned char *sb = cmd->sense_buffer;
Tejun Heod25614b2006-11-14 22:37:35 +09001011 unsigned char *desc = sb + 8;
Tejun Heo246619d2006-05-15 20:58:16 +09001012 int verbose = qc->ap->ops->error_handler == NULL;
Tejun Heod25614b2006-11-14 22:37:35 +09001013 u64 block;
Jeff Garzikb0955182005-05-12 15:45:22 -04001014
1015 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
1016
Jeff Garzik0e5dec42005-10-06 09:40:20 -04001017 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
Jeff Garzikb0955182005-05-12 15:45:22 -04001018
Tejun Heod25614b2006-11-14 22:37:35 +09001019 /* sense data is current and format is descriptor */
1020 sb[0] = 0x72;
1021
1022 /* Use ata_to_sense_error() to map status register bits
Jeff Garzikb0955182005-05-12 15:45:22 -04001023 * onto sense key, asc & ascq.
1024 */
Tejun Heo058e55e2006-04-02 18:51:53 +09001025 if (qc->err_mask ||
1026 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
Tejun Heo44877b42007-02-21 01:06:51 +09001027 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
Tejun Heod25614b2006-11-14 22:37:35 +09001028 &sb[1], &sb[2], &sb[3], verbose);
1029 sb[1] &= 0x0f;
Jeff Garzikb0955182005-05-12 15:45:22 -04001030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Tejun Heod25614b2006-11-14 22:37:35 +09001032 block = ata_tf_read_block(&qc->result_tf, dev);
Jeff Garzik0274aa22005-06-22 13:50:56 -04001033
Tejun Heod25614b2006-11-14 22:37:35 +09001034 /* information sense data descriptor */
1035 sb[7] = 12;
1036 desc[0] = 0x00;
1037 desc[1] = 10;
Jeff Garzika7dac442005-10-30 04:44:42 -05001038
Tejun Heod25614b2006-11-14 22:37:35 +09001039 desc[2] |= 0x80; /* valid */
1040 desc[6] = block >> 40;
1041 desc[7] = block >> 32;
1042 desc[8] = block >> 24;
1043 desc[9] = block >> 16;
1044 desc[10] = block >> 8;
1045 desc[11] = block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046}
1047
Brian Kinga6cce2a2006-03-17 17:04:15 -06001048static void ata_scsi_sdev_config(struct scsi_device *sdev)
1049{
1050 sdev->use_10_for_rw = 1;
1051 sdev->use_10_for_ms = 1;
Tejun Heo31cc23b2007-09-23 13:14:12 +09001052
1053 /* Schedule policy is determined by ->qc_defer() callback and
1054 * it needs to see every deferred qc. Set dev_blocked to 1 to
1055 * prevent SCSI midlayer from automatically deferring
1056 * requests.
1057 */
1058 sdev->max_device_blocked = 1;
Brian Kinga6cce2a2006-03-17 17:04:15 -06001059}
1060
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001061/**
1062 * atapi_drain_needed - Check whether data transfer may overflow
Randy Dunlap73fd8b62008-02-19 13:43:21 -08001063 * @rq: request to be checked
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001064 *
1065 * ATAPI commands which transfer variable length data to host
1066 * might overflow due to application error or hardare bug. This
1067 * function checks whether overflow should be drained and ignored
1068 * for @request.
1069 *
1070 * LOCKING:
1071 * None.
1072 *
1073 * RETURNS:
1074 * 1 if ; otherwise, 0.
1075 */
1076static int atapi_drain_needed(struct request *rq)
1077{
Christoph Hellwig33659eb2010-08-07 18:17:56 +02001078 if (likely(rq->cmd_type != REQ_TYPE_BLOCK_PC))
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001079 return 0;
1080
Christoph Hellwig7b6d91d2010-08-07 18:20:39 +02001081 if (!blk_rq_bytes(rq) || (rq->cmd_flags & REQ_WRITE))
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001082 return 0;
1083
1084 return atapi_cmd_type(rq->cmd[0]) == ATAPI_MISC;
1085}
1086
1087static int ata_scsi_dev_config(struct scsi_device *sdev,
1088 struct ata_device *dev)
Brian Kinga6cce2a2006-03-17 17:04:15 -06001089{
Elias Oltmanns45fabbb2008-09-21 11:54:08 +02001090 if (!ata_id_has_unload(dev->id))
1091 dev->flags |= ATA_DFLAG_NO_UNLOAD;
1092
Tejun Heo914ed352006-11-01 18:39:55 +09001093 /* configure max sectors */
Martin K. Petersen086fa5f2010-02-26 00:20:38 -05001094 blk_queue_max_hw_sectors(sdev->request_queue, dev->max_sectors);
Brian Kinga6cce2a2006-03-17 17:04:15 -06001095
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001096 if (dev->class == ATA_DEV_ATAPI) {
1097 struct request_queue *q = sdev->request_queue;
1098 void *buf;
1099
Tejun Heoe3790c72008-03-04 11:18:17 +01001100 /* set the min alignment and padding */
James Bottomleyd0ad3bc2008-01-22 13:43:48 -06001101 blk_queue_update_dma_alignment(sdev->request_queue,
1102 ATA_DMA_PAD_SZ - 1);
FUJITA Tomonori27f82212008-07-04 09:30:03 +02001103 blk_queue_update_dma_pad(sdev->request_queue,
1104 ATA_DMA_PAD_SZ - 1);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001105
1106 /* configure draining */
1107 buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
1108 if (!buf) {
1109 ata_dev_printk(dev, KERN_ERR,
1110 "drain buffer allocation failed\n");
1111 return -ENOMEM;
1112 }
1113
1114 blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
1115 } else {
James Bottomleyd0ad3bc2008-01-22 13:43:48 -06001116 /* ATA devices must be sector aligned */
Grant Grundler295124d2010-08-17 10:56:53 -07001117 sdev->sector_size = ata_id_logical_sector_size(dev->id);
James Bottomleyd0ad3bc2008-01-22 13:43:48 -06001118 blk_queue_update_dma_alignment(sdev->request_queue,
Grant Grundler295124d2010-08-17 10:56:53 -07001119 sdev->sector_size - 1);
Tejun Heod8cf5382008-01-15 08:46:59 +09001120 sdev->manage_start_stop = 1;
James Bottomleydde20202008-02-19 11:36:56 +01001121 }
Tejun Heod8cf5382008-01-15 08:46:59 +09001122
Jeff Garzikf26792d2007-10-29 17:18:39 -04001123 if (dev->flags & ATA_DFLAG_AN)
1124 set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
1125
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001126 if (dev->flags & ATA_DFLAG_NCQ) {
1127 int depth;
1128
1129 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
1130 depth = min(ATA_MAX_QUEUE - 1, depth);
Tejun Heo8a8bc222008-11-10 14:48:21 +09001131 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001132 }
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001133
Grant Grundler295124d2010-08-17 10:56:53 -07001134 dev->sdev = sdev;
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001135 return 0;
Brian Kinga6cce2a2006-03-17 17:04:15 -06001136}
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138/**
1139 * ata_scsi_slave_config - Set SCSI device attributes
1140 * @sdev: SCSI device to examine
1141 *
1142 * This is called before we actually start reading
1143 * and writing to the device, to configure certain
1144 * SCSI mid-layer behaviors.
1145 *
1146 * LOCKING:
1147 * Defined by SCSI layer. We don't really care.
1148 */
1149
1150int ata_scsi_slave_config(struct scsi_device *sdev)
1151{
Tejun Heo31534362006-05-31 18:27:36 +09001152 struct ata_port *ap = ata_shost_to_port(sdev->host);
1153 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001154 int rc = 0;
Tejun Heo31534362006-05-31 18:27:36 +09001155
Brian Kinga6cce2a2006-03-17 17:04:15 -06001156 ata_scsi_sdev_config(sdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Tejun Heo31534362006-05-31 18:27:36 +09001158 if (dev)
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001159 rc = ata_scsi_dev_config(sdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001161 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162}
1163
1164/**
Tejun Heo83c47bc2006-05-31 18:28:07 +09001165 * ata_scsi_slave_destroy - SCSI device is about to be destroyed
1166 * @sdev: SCSI device to be destroyed
1167 *
1168 * @sdev is about to be destroyed for hot/warm unplugging. If
1169 * this unplugging was initiated by libata as indicated by NULL
1170 * dev->sdev, this function doesn't have to do anything.
1171 * Otherwise, SCSI layer initiated warm-unplug is in progress.
1172 * Clear dev->sdev, schedule the device for ATA detach and invoke
1173 * EH.
1174 *
1175 * LOCKING:
1176 * Defined by SCSI layer. We don't really care.
1177 */
1178void ata_scsi_slave_destroy(struct scsi_device *sdev)
1179{
1180 struct ata_port *ap = ata_shost_to_port(sdev->host);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001181 struct request_queue *q = sdev->request_queue;
Tejun Heo83c47bc2006-05-31 18:28:07 +09001182 unsigned long flags;
1183 struct ata_device *dev;
1184
1185 if (!ap->ops->error_handler)
1186 return;
1187
Jeff Garzikba6a1302006-06-22 23:46:10 -04001188 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09001189 dev = __ata_scsi_find_dev(ap, sdev);
1190 if (dev && dev->sdev) {
1191 /* SCSI device already in CANCEL state, no need to offline it */
1192 dev->sdev = NULL;
1193 dev->flags |= ATA_DFLAG_DETACH;
1194 ata_port_schedule_eh(ap);
1195 }
Jeff Garzikba6a1302006-06-22 23:46:10 -04001196 spin_unlock_irqrestore(ap->lock, flags);
James Bottomleyfa2fc7f2008-02-19 11:36:57 +01001197
1198 kfree(q->dma_drain_buffer);
1199 q->dma_drain_buffer = NULL;
1200 q->dma_drain_size = 0;
Tejun Heo83c47bc2006-05-31 18:28:07 +09001201}
1202
1203/**
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001204 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
1205 * @sdev: SCSI device to configure queue depth for
1206 * @queue_depth: new queue depth
Mike Christiee881a172009-10-15 17:46:39 -07001207 * @reason: calling context
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001208 *
1209 * This is libata standard hostt->change_queue_depth callback.
1210 * SCSI will call into this callback when user tries to set queue
1211 * depth via sysfs.
1212 *
1213 * LOCKING:
1214 * SCSI layer (we don't care)
1215 *
1216 * RETURNS:
1217 * Newly configured queue depth.
1218 */
Mike Christiee881a172009-10-15 17:46:39 -07001219int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth,
1220 int reason)
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001221{
1222 struct ata_port *ap = ata_shost_to_port(sdev->host);
1223 struct ata_device *dev;
Tejun Heo360f6542006-09-30 19:45:00 +09001224 unsigned long flags;
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001225
Mike Christiee881a172009-10-15 17:46:39 -07001226 if (reason != SCSI_QDEPTH_DEFAULT)
1227 return -EOPNOTSUPP;
1228
Tejun Heoc3c70c42007-02-20 23:31:22 +09001229 if (queue_depth < 1 || queue_depth == sdev->queue_depth)
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001230 return sdev->queue_depth;
1231
1232 dev = ata_scsi_find_dev(ap, sdev);
1233 if (!dev || !ata_dev_enabled(dev))
1234 return sdev->queue_depth;
1235
Tejun Heoc3c70c42007-02-20 23:31:22 +09001236 /* NCQ enabled? */
Tejun Heo360f6542006-09-30 19:45:00 +09001237 spin_lock_irqsave(ap->lock, flags);
Tejun Heoc3c70c42007-02-20 23:31:22 +09001238 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1239 if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
Tejun Heo360f6542006-09-30 19:45:00 +09001240 dev->flags |= ATA_DFLAG_NCQ_OFF;
Tejun Heoc3c70c42007-02-20 23:31:22 +09001241 queue_depth = 1;
1242 }
Tejun Heo360f6542006-09-30 19:45:00 +09001243 spin_unlock_irqrestore(ap->lock, flags);
1244
Tejun Heoc3c70c42007-02-20 23:31:22 +09001245 /* limit and apply queue depth */
1246 queue_depth = min(queue_depth, sdev->host->can_queue);
1247 queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
1248 queue_depth = min(queue_depth, ATA_MAX_QUEUE - 1);
1249
1250 if (sdev->queue_depth == queue_depth)
1251 return -EINVAL;
1252
1253 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, queue_depth);
Tejun Heoa6e6ce82006-05-15 21:03:48 +09001254 return queue_depth;
1255}
1256
1257/**
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001258 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
1259 * @qc: Storage for translated ATA taskfile
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001260 *
1261 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
1262 * (to start). Perhaps these commands should be preceded by
1263 * CHECK POWER MODE to see what power mode the device is already in.
1264 * [See SAT revision 5 at www.t10.org]
1265 *
1266 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001267 * spin_lock_irqsave(host lock)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001268 *
1269 * RETURNS:
1270 * Zero on success, non-zero on error.
1271 */
Tejun Heoad706992006-12-17 10:45:57 +09001272static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001273{
Tejun Heo542b1442006-12-17 10:45:08 +09001274 struct scsi_cmnd *scmd = qc->scsicmd;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001275 struct ata_taskfile *tf = &qc->tf;
Tejun Heoad706992006-12-17 10:45:57 +09001276 const u8 *cdb = scmd->cmnd;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001277
Tejun Heo2e5704f2006-12-17 10:46:33 +09001278 if (scmd->cmd_len < 5)
1279 goto invalid_fld;
1280
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001281 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1282 tf->protocol = ATA_PROT_NODATA;
Tejun Heo542b1442006-12-17 10:45:08 +09001283 if (cdb[1] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001284 ; /* ignore IMMED bit, violates sat-r05 */
1285 }
Tejun Heo542b1442006-12-17 10:45:08 +09001286 if (cdb[4] & 0x2)
Douglas Gilbertae006512005-10-09 09:09:35 -04001287 goto invalid_fld; /* LOEJ bit set not supported */
Tejun Heo542b1442006-12-17 10:45:08 +09001288 if (((cdb[4] >> 4) & 0xf) != 0)
Douglas Gilbertae006512005-10-09 09:09:35 -04001289 goto invalid_fld; /* power conditions not supported */
Tejun Heoe31e8532007-09-23 13:14:13 +09001290
Tejun Heo542b1442006-12-17 10:45:08 +09001291 if (cdb[4] & 0x1) {
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001292 tf->nsect = 1; /* 1 sector, lba=0 */
Albert Lee9d5b1302005-10-04 08:48:17 -04001293
1294 if (qc->dev->flags & ATA_DFLAG_LBA) {
Tejun Heoc44078c2006-05-15 20:57:21 +09001295 tf->flags |= ATA_TFLAG_LBA;
Albert Lee9d5b1302005-10-04 08:48:17 -04001296
1297 tf->lbah = 0x0;
1298 tf->lbam = 0x0;
1299 tf->lbal = 0x0;
1300 tf->device |= ATA_LBA;
1301 } else {
1302 /* CHS */
1303 tf->lbal = 0x1; /* sect */
1304 tf->lbam = 0x0; /* cyl low */
1305 tf->lbah = 0x0; /* cyl high */
1306 }
1307
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001308 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
Tejun Heo920a4b12007-05-04 21:28:48 +02001309 } else {
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001310 /* Some odd clown BIOSen issue spindown on power off (ACPI S4
1311 * or S5) causing some drives to spin up and down again.
1312 */
1313 if ((qc->ap->flags & ATA_FLAG_NO_POWEROFF_SPINDOWN) &&
1314 system_state == SYSTEM_POWER_OFF)
1315 goto skip;
1316
1317 if ((qc->ap->flags & ATA_FLAG_NO_HIBERNATE_SPINDOWN) &&
1318 system_entering_hibernation())
1319 goto skip;
1320
Robert Hancock78981a72007-01-30 00:59:18 -08001321 /* Issue ATA STANDBY IMMEDIATE command */
1322 tf->command = ATA_CMD_STANDBYNOW1;
Tejun Heo920a4b12007-05-04 21:28:48 +02001323 }
Robert Hancock78981a72007-01-30 00:59:18 -08001324
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001325 /*
1326 * Standby and Idle condition timers could be implemented but that
1327 * would require libata to implement the Power condition mode page
1328 * and allow the user to change it. Changing mode pages requires
1329 * MODE SELECT to be implemented.
1330 */
1331
1332 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001333
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001334 invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +09001335 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001336 /* "Invalid field in cbd" */
1337 return 1;
Rafael J. Wysocki2a6e58d2009-01-19 20:56:43 +01001338 skip:
1339 scmd->result = SAM_STAT_GOOD;
1340 return 1;
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04001341}
1342
1343
1344/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
1346 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 *
1348 * Sets up an ATA taskfile to issue FLUSH CACHE or
1349 * FLUSH CACHE EXT.
1350 *
1351 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001352 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 *
1354 * RETURNS:
1355 * Zero on success, non-zero on error.
1356 */
Tejun Heoad706992006-12-17 10:45:57 +09001357static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358{
1359 struct ata_taskfile *tf = &qc->tf;
1360
1361 tf->flags |= ATA_TFLAG_DEVICE;
1362 tf->protocol = ATA_PROT_NODATA;
1363
Tejun Heo6fc49ad2006-11-11 20:10:45 +09001364 if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 tf->command = ATA_CMD_FLUSH_EXT;
1366 else
1367 tf->command = ATA_CMD_FLUSH;
1368
Tejun Heob666da32007-10-26 15:53:59 +09001369 /* flush is critical for IO integrity, consider it an IO command */
1370 qc->flags |= ATA_QCFLAG_IO;
1371
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372 return 0;
1373}
1374
1375/**
Albert Lee3aef5232005-10-04 08:47:43 -04001376 * scsi_6_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001377 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001378 *
1379 * Calculate LBA and transfer length for 6-byte commands.
1380 *
1381 * RETURNS:
1382 * @plba: the LBA
1383 * @plen: the transfer length
1384 */
Tejun Heo542b1442006-12-17 10:45:08 +09001385static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001386{
1387 u64 lba = 0;
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001388 u32 len;
Albert Lee3aef5232005-10-04 08:47:43 -04001389
1390 VPRINTK("six-byte command\n");
1391
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001392 lba |= ((u64)(cdb[1] & 0x1f)) << 16;
Tejun Heo542b1442006-12-17 10:45:08 +09001393 lba |= ((u64)cdb[2]) << 8;
1394 lba |= ((u64)cdb[3]);
Albert Lee3aef5232005-10-04 08:47:43 -04001395
Jeff Garzik6c7b7d22007-05-25 04:39:39 -04001396 len = cdb[4];
Albert Lee3aef5232005-10-04 08:47:43 -04001397
1398 *plba = lba;
1399 *plen = len;
1400}
1401
1402/**
1403 * scsi_10_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001404 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001405 *
1406 * Calculate LBA and transfer length for 10-byte commands.
1407 *
1408 * RETURNS:
1409 * @plba: the LBA
1410 * @plen: the transfer length
1411 */
Tejun Heo542b1442006-12-17 10:45:08 +09001412static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001413{
1414 u64 lba = 0;
1415 u32 len = 0;
1416
1417 VPRINTK("ten-byte command\n");
1418
Tejun Heo542b1442006-12-17 10:45:08 +09001419 lba |= ((u64)cdb[2]) << 24;
1420 lba |= ((u64)cdb[3]) << 16;
1421 lba |= ((u64)cdb[4]) << 8;
1422 lba |= ((u64)cdb[5]);
Albert Lee3aef5232005-10-04 08:47:43 -04001423
Tejun Heo542b1442006-12-17 10:45:08 +09001424 len |= ((u32)cdb[7]) << 8;
1425 len |= ((u32)cdb[8]);
Albert Lee3aef5232005-10-04 08:47:43 -04001426
1427 *plba = lba;
1428 *plen = len;
1429}
1430
1431/**
1432 * scsi_16_lba_len - Get LBA and transfer length
Tejun Heo542b1442006-12-17 10:45:08 +09001433 * @cdb: SCSI command to translate
Albert Lee3aef5232005-10-04 08:47:43 -04001434 *
1435 * Calculate LBA and transfer length for 16-byte commands.
1436 *
1437 * RETURNS:
1438 * @plba: the LBA
1439 * @plen: the transfer length
1440 */
Tejun Heo542b1442006-12-17 10:45:08 +09001441static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
Albert Lee3aef5232005-10-04 08:47:43 -04001442{
1443 u64 lba = 0;
1444 u32 len = 0;
1445
1446 VPRINTK("sixteen-byte command\n");
1447
Tejun Heo542b1442006-12-17 10:45:08 +09001448 lba |= ((u64)cdb[2]) << 56;
1449 lba |= ((u64)cdb[3]) << 48;
1450 lba |= ((u64)cdb[4]) << 40;
1451 lba |= ((u64)cdb[5]) << 32;
1452 lba |= ((u64)cdb[6]) << 24;
1453 lba |= ((u64)cdb[7]) << 16;
1454 lba |= ((u64)cdb[8]) << 8;
1455 lba |= ((u64)cdb[9]);
Albert Lee3aef5232005-10-04 08:47:43 -04001456
Tejun Heo542b1442006-12-17 10:45:08 +09001457 len |= ((u32)cdb[10]) << 24;
1458 len |= ((u32)cdb[11]) << 16;
1459 len |= ((u32)cdb[12]) << 8;
1460 len |= ((u32)cdb[13]);
Albert Lee3aef5232005-10-04 08:47:43 -04001461
1462 *plba = lba;
1463 *plen = len;
1464}
1465
1466/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001467 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1468 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469 *
1470 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1471 *
1472 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001473 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 *
1475 * RETURNS:
1476 * Zero on success, non-zero on error.
1477 */
Tejun Heoad706992006-12-17 10:45:57 +09001478static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001479{
Tejun Heo542b1442006-12-17 10:45:08 +09001480 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 struct ata_taskfile *tf = &qc->tf;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001482 struct ata_device *dev = qc->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 u64 dev_sectors = qc->dev->n_sectors;
Tejun Heoad706992006-12-17 10:45:57 +09001484 const u8 *cdb = scmd->cmnd;
Albert Lee3aef5232005-10-04 08:47:43 -04001485 u64 block;
1486 u32 n_block;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487
1488 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1489 tf->protocol = ATA_PROT_NODATA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490
Tejun Heo2e5704f2006-12-17 10:46:33 +09001491 if (cdb[0] == VERIFY) {
1492 if (scmd->cmd_len < 10)
1493 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001494 scsi_10_lba_len(cdb, &block, &n_block);
Tejun Heo2e5704f2006-12-17 10:46:33 +09001495 } else if (cdb[0] == VERIFY_16) {
1496 if (scmd->cmd_len < 16)
1497 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001498 scsi_16_lba_len(cdb, &block, &n_block);
Tejun Heo2e5704f2006-12-17 10:46:33 +09001499 } else
Douglas Gilbertae006512005-10-09 09:09:35 -04001500 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Albert Lee8bf62ec2005-05-12 15:29:42 -04001502 if (!n_block)
Douglas Gilbertae006512005-10-09 09:09:35 -04001503 goto nothing_to_do;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001504 if (block >= dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001505 goto out_of_range;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001506 if ((block + n_block) > dev_sectors)
Douglas Gilbertae006512005-10-09 09:09:35 -04001507 goto out_of_range;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508
Albert Lee07506692005-10-12 15:04:18 +08001509 if (dev->flags & ATA_DFLAG_LBA) {
1510 tf->flags |= ATA_TFLAG_LBA;
1511
Albert Leec6a33e22005-10-12 15:12:26 +08001512 if (lba_28_ok(block, n_block)) {
1513 /* use LBA28 */
1514 tf->command = ATA_CMD_VERIFY;
1515 tf->device |= (block >> 24) & 0xf;
1516 } else if (lba_48_ok(block, n_block)) {
1517 if (!(dev->flags & ATA_DFLAG_LBA48))
1518 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001519
1520 /* use LBA48 */
1521 tf->flags |= ATA_TFLAG_LBA48;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001522 tf->command = ATA_CMD_VERIFY_EXT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523
Albert Lee8bf62ec2005-05-12 15:29:42 -04001524 tf->hob_nsect = (n_block >> 8) & 0xff;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
Albert Lee8bf62ec2005-05-12 15:29:42 -04001526 tf->hob_lbah = (block >> 40) & 0xff;
1527 tf->hob_lbam = (block >> 32) & 0xff;
1528 tf->hob_lbal = (block >> 24) & 0xff;
Albert Leec6a33e22005-10-12 15:12:26 +08001529 } else
1530 /* request too large even for LBA48 */
1531 goto out_of_range;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001532
1533 tf->nsect = n_block & 0xff;
1534
1535 tf->lbah = (block >> 16) & 0xff;
1536 tf->lbam = (block >> 8) & 0xff;
1537 tf->lbal = block & 0xff;
1538
1539 tf->device |= ATA_LBA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 } else {
Albert Lee8bf62ec2005-05-12 15:29:42 -04001541 /* CHS */
1542 u32 sect, head, cyl, track;
1543
Albert Leec6a33e22005-10-12 15:12:26 +08001544 if (!lba_28_ok(block, n_block))
1545 goto out_of_range;
Albert Lee07506692005-10-12 15:04:18 +08001546
Albert Lee8bf62ec2005-05-12 15:29:42 -04001547 /* Convert LBA to CHS */
1548 track = (u32)block / dev->sectors;
1549 cyl = track / dev->heads;
1550 head = track % dev->heads;
1551 sect = (u32)block % dev->sectors + 1;
1552
Albert Leec187c4b2005-10-04 08:46:51 -04001553 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1554 (u32)block, track, cyl, head, sect);
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001555
1556 /* Check whether the converted CHS can fit.
1557 Cylinder: 0-65535
Albert Lee8bf62ec2005-05-12 15:29:42 -04001558 Head: 0-15
1559 Sector: 1-255*/
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001560 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
Douglas Gilbertae006512005-10-09 09:09:35 -04001561 goto out_of_range;
Jeff Garzik2e9edbf2006-03-24 09:56:57 -05001562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 tf->command = ATA_CMD_VERIFY;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001564 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1565 tf->lbal = sect;
1566 tf->lbam = cyl;
1567 tf->lbah = cyl >> 8;
1568 tf->device |= head;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 }
1570
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001572
1573invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +09001574 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001575 /* "Invalid field in cbd" */
1576 return 1;
1577
1578out_of_range:
Tejun Heo542b1442006-12-17 10:45:08 +09001579 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001580 /* "Logical Block Address out of range" */
1581 return 1;
1582
1583nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001584 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001585 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586}
1587
1588/**
1589 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1590 * @qc: Storage for translated ATA taskfile
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 *
1592 * Converts any of six SCSI read/write commands into the
1593 * ATA counterpart, including starting sector (LBA),
1594 * sector count, and taking into account the device's LBA48
1595 * support.
1596 *
1597 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1598 * %WRITE_16 are currently supported.
1599 *
1600 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001601 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 *
1603 * RETURNS:
1604 * Zero on success, non-zero on error.
1605 */
Tejun Heoad706992006-12-17 10:45:57 +09001606static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001607{
Tejun Heo542b1442006-12-17 10:45:08 +09001608 struct scsi_cmnd *scmd = qc->scsicmd;
Tejun Heoad706992006-12-17 10:45:57 +09001609 const u8 *cdb = scmd->cmnd;
Tejun Heobd056d72006-11-14 22:47:10 +09001610 unsigned int tf_flags = 0;
Albert Lee3aef5232005-10-04 08:47:43 -04001611 u64 block;
1612 u32 n_block;
Tejun Heobd056d72006-11-14 22:47:10 +09001613 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001614
Tejun Heo542b1442006-12-17 10:45:08 +09001615 if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
Tejun Heobd056d72006-11-14 22:47:10 +09001616 tf_flags |= ATA_TFLAG_WRITE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001617
Tejun Heo9a3dccc2006-01-06 09:56:18 +01001618 /* Calculate the SCSI LBA, transfer length and FUA. */
Tejun Heo542b1442006-12-17 10:45:08 +09001619 switch (cdb[0]) {
Albert Lee3aef5232005-10-04 08:47:43 -04001620 case READ_10:
1621 case WRITE_10:
Tejun Heo2e5704f2006-12-17 10:46:33 +09001622 if (unlikely(scmd->cmd_len < 10))
1623 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001624 scsi_10_lba_len(cdb, &block, &n_block);
1625 if (unlikely(cdb[1] & (1 << 3)))
Tejun Heobd056d72006-11-14 22:47:10 +09001626 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001627 break;
1628 case READ_6:
1629 case WRITE_6:
Tejun Heo2e5704f2006-12-17 10:46:33 +09001630 if (unlikely(scmd->cmd_len < 6))
1631 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001632 scsi_6_lba_len(cdb, &block, &n_block);
Albert Leec187c4b2005-10-04 08:46:51 -04001633
1634 /* for 6-byte r/w commands, transfer length 0
1635 * means 256 blocks of data, not 0 block.
1636 */
Jeff Garzik76b2bf92005-08-29 19:24:43 -04001637 if (!n_block)
1638 n_block = 256;
Albert Lee3aef5232005-10-04 08:47:43 -04001639 break;
1640 case READ_16:
1641 case WRITE_16:
Tejun Heo2e5704f2006-12-17 10:46:33 +09001642 if (unlikely(scmd->cmd_len < 16))
1643 goto invalid_fld;
Tejun Heo542b1442006-12-17 10:45:08 +09001644 scsi_16_lba_len(cdb, &block, &n_block);
1645 if (unlikely(cdb[1] & (1 << 3)))
Tejun Heobd056d72006-11-14 22:47:10 +09001646 tf_flags |= ATA_TFLAG_FUA;
Albert Lee3aef5232005-10-04 08:47:43 -04001647 break;
1648 default:
Albert Lee8bf62ec2005-05-12 15:29:42 -04001649 DPRINTK("no-byte command\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04001650 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 }
1652
Albert Lee8bf62ec2005-05-12 15:29:42 -04001653 /* Check and compose ATA command */
1654 if (!n_block)
Albert Leec187c4b2005-10-04 08:46:51 -04001655 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1656 * length 0 means transfer 0 block of data.
1657 * However, for ATA R/W commands, sector count 0 means
1658 * 256 or 65536 sectors, not 0 sectors as in SCSI.
Alan Coxf51750d2005-11-07 17:06:33 +00001659 *
1660 * WARNING: one or two older ATA drives treat 0 as 0...
Albert Leec187c4b2005-10-04 08:46:51 -04001661 */
Douglas Gilbertae006512005-10-09 09:09:35 -04001662 goto nothing_to_do;
Albert Lee8bf62ec2005-05-12 15:29:42 -04001663
Tejun Heobd056d72006-11-14 22:47:10 +09001664 qc->flags |= ATA_QCFLAG_IO;
Grant Grundler295124d2010-08-17 10:56:53 -07001665 qc->nbytes = n_block * scmd->device->sector_size;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001666
Tejun Heobd056d72006-11-14 22:47:10 +09001667 rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
1668 qc->tag);
1669 if (likely(rc == 0))
1670 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001671
Tejun Heobd056d72006-11-14 22:47:10 +09001672 if (rc == -ERANGE)
1673 goto out_of_range;
1674 /* treat all other errors as -EINVAL, fall through */
Douglas Gilbertae006512005-10-09 09:09:35 -04001675invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +09001676 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001677 /* "Invalid field in cbd" */
1678 return 1;
1679
1680out_of_range:
Tejun Heo542b1442006-12-17 10:45:08 +09001681 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
Douglas Gilbertae006512005-10-09 09:09:35 -04001682 /* "Logical Block Address out of range" */
1683 return 1;
1684
1685nothing_to_do:
Tejun Heo542b1442006-12-17 10:45:08 +09001686 scmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001687 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688}
1689
Tejun Heo77853bf2006-01-23 13:09:36 +09001690static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691{
Tejun Heoc31f5712006-11-22 12:39:43 +09001692 struct ata_port *ap = qc->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika7dac442005-10-30 04:44:42 -05001694 u8 *cdb = cmd->cmnd;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001695 int need_sense = (qc->err_mask != 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Jeff Garzikb0955182005-05-12 15:45:22 -04001697 /* For ATA pass thru (SAT) commands, generate a sense block if
1698 * user mandated it or if there's an error. Note that if we
1699 * generate because the user forced us to, a check condition
1700 * is generated and the ATA register values are returned
1701 * whether the command completed successfully or not. If there
1702 * was no error, SK, ASC and ASCQ will all be zero.
1703 */
Jeff Garzika7dac442005-10-30 04:44:42 -05001704 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001705 ((cdb[2] & 0x20) || need_sense)) {
Tejun Heo750426a2006-11-14 22:37:35 +09001706 ata_gen_passthru_sense(qc);
Jeff Garzikb0955182005-05-12 15:45:22 -04001707 } else {
1708 if (!need_sense) {
1709 cmd->result = SAM_STAT_GOOD;
1710 } else {
1711 /* TODO: decide which descriptor format to use
1712 * for 48b LBA devices and call that here
1713 * instead of the fixed desc, which is only
1714 * good for smaller LBA (and maybe CHS?)
1715 * devices.
1716 */
Tejun Heo750426a2006-11-14 22:37:35 +09001717 ata_gen_ata_sense(qc);
Jeff Garzikb0955182005-05-12 15:45:22 -04001718 }
1719 }
1720
Tejun Heoc31f5712006-11-22 12:39:43 +09001721 if (need_sense && !ap->ops->error_handler)
Tejun Heo44877b42007-02-21 01:06:51 +09001722 ata_dump_status(ap->print_id, &qc->result_tf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 qc->scsidone(cmd);
1725
Tejun Heo77853bf2006-01-23 13:09:36 +09001726 ata_qc_free(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727}
1728
1729/**
1730 * ata_scsi_translate - Translate then issue SCSI command to ATA device
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 * @dev: ATA device to which the command is addressed
1732 * @cmd: SCSI command to execute
1733 * @done: SCSI command completion function
1734 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1735 *
1736 * Our ->queuecommand() function has decided that the SCSI
1737 * command issued can be directly translated into an ATA
1738 * command, rather than handled internally.
1739 *
1740 * This function sets up an ata_queued_cmd structure for the
1741 * SCSI command, and sends that ata_queued_cmd to the hardware.
1742 *
Douglas Gilbertae006512005-10-09 09:09:35 -04001743 * The xlat_func argument (actor) returns 0 if ready to execute
1744 * ATA command, else 1 to finish translation. If 1 is returned
1745 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1746 * to be set reflecting an error condition or clean (early)
1747 * termination.
1748 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001750 * spin_lock_irqsave(host lock)
Tejun Heo2115ea92006-05-15 21:03:39 +09001751 *
1752 * RETURNS:
1753 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
1754 * needs to be deferred.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 */
Tejun Heo2115ea92006-05-15 21:03:39 +09001756static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
1757 void (*done)(struct scsi_cmnd *),
1758 ata_xlat_func_t xlat_func)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759{
Tejun Heo31cc23b2007-09-23 13:14:12 +09001760 struct ata_port *ap = dev->link->ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761 struct ata_queued_cmd *qc;
Tejun Heo31cc23b2007-09-23 13:14:12 +09001762 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763
1764 VPRINTK("ENTER\n");
1765
Tejun Heo3373efd2006-05-15 20:57:53 +09001766 qc = ata_scsi_qc_new(dev, cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 if (!qc)
Douglas Gilbertae006512005-10-09 09:09:35 -04001768 goto err_mem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001769
1770 /* data is present; dma-map it */
be7db052005-04-17 15:26:13 -05001771 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1772 cmd->sc_data_direction == DMA_TO_DEVICE) {
Boaz Harrosh71201652007-09-18 17:48:50 +02001773 if (unlikely(scsi_bufflen(cmd) < 1)) {
Tejun Heof15a1da2006-05-15 20:57:56 +09001774 ata_dev_printk(dev, KERN_WARNING,
1775 "WARNING: zero len r/w req\n");
Douglas Gilbertae006512005-10-09 09:09:35 -04001776 goto err_did;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 }
1778
Boaz Harrosh71201652007-09-18 17:48:50 +02001779 ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781 qc->dma_dir = cmd->sc_data_direction;
1782 }
1783
1784 qc->complete_fn = ata_scsi_qc_complete;
1785
Tejun Heoad706992006-12-17 10:45:57 +09001786 if (xlat_func(qc))
Douglas Gilbertae006512005-10-09 09:09:35 -04001787 goto early_finish;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Tejun Heo31cc23b2007-09-23 13:14:12 +09001789 if (ap->ops->qc_defer) {
1790 if ((rc = ap->ops->qc_defer(qc)))
1791 goto defer;
1792 }
1793
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794 /* select device, send command to hardware */
Tejun Heo8e0e6942006-03-31 20:41:11 +09001795 ata_qc_issue(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796
1797 VPRINTK("EXIT\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001798 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799
Douglas Gilbertae006512005-10-09 09:09:35 -04001800early_finish:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001801 ata_qc_free(qc);
Tejun Heoda071b42007-05-14 17:26:18 +02001802 qc->scsidone(cmd);
Douglas Gilbertae006512005-10-09 09:09:35 -04001803 DPRINTK("EXIT - early finish (good or error)\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001804 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04001805
1806err_did:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 ata_qc_free(qc);
Douglas Gilbertae006512005-10-09 09:09:35 -04001808 cmd->result = (DID_ERROR << 16);
Tejun Heoda071b42007-05-14 17:26:18 +02001809 qc->scsidone(cmd);
Darrick J. Wong253b92e2006-11-14 09:55:41 -05001810err_mem:
Douglas Gilbertae006512005-10-09 09:09:35 -04001811 DPRINTK("EXIT - internal\n");
Tejun Heo2115ea92006-05-15 21:03:39 +09001812 return 0;
Tejun Heo3dc1d882006-05-15 21:03:45 +09001813
1814defer:
Tejun Heo31cc23b2007-09-23 13:14:12 +09001815 ata_qc_free(qc);
Tejun Heo3dc1d882006-05-15 21:03:45 +09001816 DPRINTK("EXIT - defer\n");
Tejun Heo31cc23b2007-09-23 13:14:12 +09001817 if (rc == ATA_DEFER_LINK)
1818 return SCSI_MLQUEUE_DEVICE_BUSY;
1819 else
1820 return SCSI_MLQUEUE_HOST_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821}
1822
1823/**
1824 * ata_scsi_rbuf_get - Map response buffer.
Randy Dunlapec2a20e2008-04-30 12:57:00 -07001825 * @cmd: SCSI command containing buffer to be mapped.
Tejun Heo87340e92008-04-28 17:48:51 +09001826 * @flags: unsigned long variable to store irq enable status
1827 * @copy_in: copy in from user buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 *
Tejun Heo87340e92008-04-28 17:48:51 +09001829 * Prepare buffer for simulated SCSI commands.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 *
1831 * LOCKING:
Tejun Heo87340e92008-04-28 17:48:51 +09001832 * spin_lock_irqsave(ata_scsi_rbuf_lock) on success
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 *
1834 * RETURNS:
Tejun Heo87340e92008-04-28 17:48:51 +09001835 * Pointer to response buffer.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 */
Tejun Heo87340e92008-04-28 17:48:51 +09001837static void *ata_scsi_rbuf_get(struct scsi_cmnd *cmd, bool copy_in,
1838 unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839{
Tejun Heo87340e92008-04-28 17:48:51 +09001840 spin_lock_irqsave(&ata_scsi_rbuf_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Tejun Heo87340e92008-04-28 17:48:51 +09001842 memset(ata_scsi_rbuf, 0, ATA_SCSI_RBUF_SIZE);
1843 if (copy_in)
1844 sg_copy_to_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
1845 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
1846 return ata_scsi_rbuf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847}
1848
1849/**
1850 * ata_scsi_rbuf_put - Unmap response buffer.
1851 * @cmd: SCSI command containing buffer to be unmapped.
Tejun Heo87340e92008-04-28 17:48:51 +09001852 * @copy_out: copy out result
1853 * @flags: @flags passed to ata_scsi_rbuf_get()
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 *
Tejun Heo87340e92008-04-28 17:48:51 +09001855 * Returns rbuf buffer. The result is copied to @cmd's buffer if
1856 * @copy_back is true.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857 *
1858 * LOCKING:
Tejun Heo87340e92008-04-28 17:48:51 +09001859 * Unlocks ata_scsi_rbuf_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001860 */
Tejun Heo87340e92008-04-28 17:48:51 +09001861static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, bool copy_out,
1862 unsigned long *flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863{
Tejun Heo87340e92008-04-28 17:48:51 +09001864 if (copy_out)
1865 sg_copy_from_buffer(scsi_sglist(cmd), scsi_sg_count(cmd),
1866 ata_scsi_rbuf, ATA_SCSI_RBUF_SIZE);
1867 spin_unlock_irqrestore(&ata_scsi_rbuf_lock, *flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
1870/**
1871 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1872 * @args: device IDENTIFY data / SCSI command of interest.
1873 * @actor: Callback hook for desired SCSI command simulator
1874 *
1875 * Takes care of the hard work of simulating a SCSI command...
1876 * Mapping the response buffer, calling the command's handler,
1877 * and handling the handler's return value. This return value
1878 * indicates whether the handler wishes the SCSI command to be
Douglas Gilbertae006512005-10-09 09:09:35 -04001879 * completed successfully (0), or not (in which case cmd->result
1880 * and sense buffer are assumed to be set).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 *
1882 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001883 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884 */
Tejun Heof0761be2008-04-28 17:16:52 +09001885static void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
Tejun Heo87340e92008-04-28 17:48:51 +09001886 unsigned int (*actor)(struct ata_scsi_args *args, u8 *rbuf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887{
1888 u8 *rbuf;
Tejun Heo87340e92008-04-28 17:48:51 +09001889 unsigned int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 struct scsi_cmnd *cmd = args->cmd;
Jeff Garzikb445c562008-02-29 19:10:51 -05001891 unsigned long flags;
1892
Tejun Heo87340e92008-04-28 17:48:51 +09001893 rbuf = ata_scsi_rbuf_get(cmd, false, &flags);
1894 rc = actor(args, rbuf);
1895 ata_scsi_rbuf_put(cmd, rc == 0, &flags);
Jeff Garzikb445c562008-02-29 19:10:51 -05001896
Douglas Gilbertae006512005-10-09 09:09:35 -04001897 if (rc == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 cmd->result = SAM_STAT_GOOD;
Douglas Gilbertae006512005-10-09 09:09:35 -04001899 args->done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900}
1901
1902/**
1903 * ata_scsiop_inq_std - Simulate INQUIRY command
1904 * @args: device IDENTIFY data / SCSI command of interest.
1905 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906 *
1907 * Returns standard device identification data associated
Jeff Garzikb142eb62006-03-21 20:37:47 -05001908 * with non-VPD INQUIRY command output.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909 *
1910 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001911 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001912 */
Tejun Heo87340e92008-04-28 17:48:51 +09001913static unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914{
Tejun Heo87340e92008-04-28 17:48:51 +09001915 const u8 versions[] = {
1916 0x60, /* SAM-3 (no version claimed) */
1917
1918 0x03,
1919 0x20, /* SBC-2 (no version claimed) */
1920
1921 0x02,
1922 0x60 /* SPC-3 (no version claimed) */
1923 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 u8 hdr[] = {
1925 TYPE_DISK,
1926 0,
1927 0x5, /* claim SPC-3 version compatibility */
1928 2,
1929 95 - 4
1930 };
1931
Tejun Heo87340e92008-04-28 17:48:51 +09001932 VPRINTK("ENTER\n");
1933
Linus Torvalds1da177e2005-04-16 15:20:36 -07001934 /* set scsi removeable (RMB) bit per ata bit */
1935 if (ata_id_removeable(args->id))
1936 hdr[1] |= (1 << 7);
1937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 memcpy(rbuf, hdr, sizeof(hdr));
Tejun Heo87340e92008-04-28 17:48:51 +09001939 memcpy(&rbuf[8], "ATA ", 8);
1940 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
1941 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942
Tejun Heo87340e92008-04-28 17:48:51 +09001943 if (rbuf[32] == 0 || rbuf[32] == ' ')
1944 memcpy(&rbuf[32], "n/a ", 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945
Tejun Heo87340e92008-04-28 17:48:51 +09001946 memcpy(rbuf + 59, versions, sizeof(versions));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947
1948 return 0;
1949}
1950
1951/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05001952 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
Linus Torvalds1da177e2005-04-16 15:20:36 -07001953 * @args: device IDENTIFY data / SCSI command of interest.
1954 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05001956 * Returns list of inquiry VPD pages available.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 *
1958 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001959 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 */
Tejun Heo87340e92008-04-28 17:48:51 +09001961static unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
1963 const u8 pages[] = {
1964 0x00, /* page 0x00, this page */
1965 0x80, /* page 0x80, unit serial no page */
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06001966 0x83, /* page 0x83, device ident page */
1967 0x89, /* page 0x89, ata info page */
Christoph Hellwig18f0f972009-11-17 10:00:47 -05001968 0xb0, /* page 0xb0, block limits page */
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06001969 0xb1, /* page 0xb1, block device characteristics page */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970 };
Tejun Heo87340e92008-04-28 17:48:51 +09001971
Jeff Garzikb142eb62006-03-21 20:37:47 -05001972 rbuf[3] = sizeof(pages); /* number of supported VPD pages */
Tejun Heo87340e92008-04-28 17:48:51 +09001973 memcpy(rbuf + 4, pages, sizeof(pages));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 return 0;
1975}
1976
1977/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05001978 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 * @args: device IDENTIFY data / SCSI command of interest.
1980 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 *
1982 * Returns ATA device serial number.
1983 *
1984 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001985 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001986 */
Tejun Heo87340e92008-04-28 17:48:51 +09001987static unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001988{
1989 const u8 hdr[] = {
1990 0,
1991 0x80, /* this page code */
1992 0,
Tejun Heoa0cf7332007-01-02 20:18:49 +09001993 ATA_ID_SERNO_LEN, /* page len */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 };
Tejun Heo87340e92008-04-28 17:48:51 +09001995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 memcpy(rbuf, hdr, sizeof(hdr));
Tejun Heo87340e92008-04-28 17:48:51 +09001997 ata_id_string(args->id, (unsigned char *) &rbuf[4],
1998 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 return 0;
2000}
2001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002/**
Jeff Garzikb142eb62006-03-21 20:37:47 -05002003 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 * @args: device IDENTIFY data / SCSI command of interest.
2005 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 *
Jeff Garzikb142eb62006-03-21 20:37:47 -05002007 * Yields two logical unit device identification designators:
2008 * - vendor specific ASCII containing the ATA serial number
2009 * - SAT defined "t10 vendor id based" containing ASCII vendor
2010 * name ("ATA "), model and serial numbers.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011 *
2012 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002013 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 */
Tejun Heo87340e92008-04-28 17:48:51 +09002015static unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016{
Jeff Garzikb142eb62006-03-21 20:37:47 -05002017 const int sat_model_serial_desc_len = 68;
Tejun Heo87340e92008-04-28 17:48:51 +09002018 int num;
Jeff Garzikb142eb62006-03-21 20:37:47 -05002019
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 rbuf[1] = 0x83; /* this page code */
Jeff Garzikb142eb62006-03-21 20:37:47 -05002021 num = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022
Tejun Heo87340e92008-04-28 17:48:51 +09002023 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
2024 rbuf[num + 0] = 2;
2025 rbuf[num + 3] = ATA_ID_SERNO_LEN;
2026 num += 4;
2027 ata_id_string(args->id, (unsigned char *) rbuf + num,
2028 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
2029 num += ATA_ID_SERNO_LEN;
2030
2031 /* SAT defined lu model and serial numbers descriptor */
2032 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
2033 rbuf[num + 0] = 2;
2034 rbuf[num + 1] = 1;
2035 rbuf[num + 3] = sat_model_serial_desc_len;
2036 num += 4;
2037 memcpy(rbuf + num, "ATA ", 8);
2038 num += 8;
2039 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_PROD,
2040 ATA_ID_PROD_LEN);
2041 num += ATA_ID_PROD_LEN;
2042 ata_id_string(args->id, (unsigned char *) rbuf + num, ATA_ID_SERNO,
2043 ATA_ID_SERNO_LEN);
2044 num += ATA_ID_SERNO_LEN;
2045
Jeff Garzikb142eb62006-03-21 20:37:47 -05002046 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 return 0;
2048}
2049
2050/**
Jeff Garzikad355b42007-09-21 20:38:03 -04002051 * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
2052 * @args: device IDENTIFY data / SCSI command of interest.
2053 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Jeff Garzikad355b42007-09-21 20:38:03 -04002054 *
2055 * Yields SAT-specified ATA VPD page.
2056 *
2057 * LOCKING:
2058 * spin_lock_irqsave(host lock)
2059 */
Tejun Heo87340e92008-04-28 17:48:51 +09002060static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf)
Jeff Garzikad355b42007-09-21 20:38:03 -04002061{
Jeff Garzikad355b42007-09-21 20:38:03 -04002062 struct ata_taskfile tf;
Jeff Garzikad355b42007-09-21 20:38:03 -04002063
Jeff Garzikad355b42007-09-21 20:38:03 -04002064 memset(&tf, 0, sizeof(tf));
2065
Tejun Heo87340e92008-04-28 17:48:51 +09002066 rbuf[1] = 0x89; /* our page code */
2067 rbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
2068 rbuf[3] = (0x238 & 0xff);
Jeff Garzikad355b42007-09-21 20:38:03 -04002069
Tejun Heo87340e92008-04-28 17:48:51 +09002070 memcpy(&rbuf[8], "linux ", 8);
2071 memcpy(&rbuf[16], "libata ", 16);
2072 memcpy(&rbuf[32], DRV_VERSION, 4);
2073 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
Jeff Garzikad355b42007-09-21 20:38:03 -04002074
2075 /* we don't store the ATA device signature, so we fake it */
2076
2077 tf.command = ATA_DRDY; /* really, this is Status reg */
2078 tf.lbal = 0x1;
2079 tf.nsect = 0x1;
2080
Tejun Heo87340e92008-04-28 17:48:51 +09002081 ata_tf_to_fis(&tf, 0, 1, &rbuf[36]); /* TODO: PMP? */
2082 rbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
Jeff Garzikad355b42007-09-21 20:38:03 -04002083
Tejun Heo87340e92008-04-28 17:48:51 +09002084 rbuf[56] = ATA_CMD_ID_ATA;
Jeff Garzikad355b42007-09-21 20:38:03 -04002085
Tejun Heo87340e92008-04-28 17:48:51 +09002086 memcpy(&rbuf[60], &args->id[0], 512);
Jeff Garzikad355b42007-09-21 20:38:03 -04002087 return 0;
2088}
2089
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002090static unsigned int ata_scsiop_inq_b0(struct ata_scsi_args *args, u8 *rbuf)
2091{
Grant Grundler295124d2010-08-17 10:56:53 -07002092 u16 min_io_sectors;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002093
2094 rbuf[1] = 0xb0;
2095 rbuf[3] = 0x3c; /* required VPD size with unmap support */
2096
2097 /*
2098 * Optimal transfer length granularity.
2099 *
2100 * This is always one physical block, but for disks with a smaller
2101 * logical than physical sector size we need to figure out what the
2102 * latter is.
2103 */
Grant Grundler295124d2010-08-17 10:56:53 -07002104 min_io_sectors = 1 << ata_id_log2_per_physical_sector(args->id);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002105 put_unaligned_be16(min_io_sectors, &rbuf[6]);
2106
2107 /*
2108 * Optimal unmap granularity.
2109 *
2110 * The ATA spec doesn't even know about a granularity or alignment
2111 * for the TRIM command. We can leave away most of the unmap related
2112 * VPD page entries, but we have specifify a granularity to signal
2113 * that we support some form of unmap - in thise case via WRITE SAME
2114 * with the unmap bit set.
2115 */
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002116 if (ata_id_has_trim(args->id)) {
2117 put_unaligned_be32(65535 * 512 / 8, &rbuf[20]);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002118 put_unaligned_be32(1, &rbuf[28]);
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002119 }
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002120
2121 return 0;
2122}
2123
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002124static unsigned int ata_scsiop_inq_b1(struct ata_scsi_args *args, u8 *rbuf)
2125{
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002126 int form_factor = ata_id_form_factor(args->id);
2127 int media_rotation_rate = ata_id_rotation_rate(args->id);
2128
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002129 rbuf[1] = 0xb1;
2130 rbuf[3] = 0x3c;
Martin K. Petersen4bca3282009-05-15 00:40:35 -04002131 rbuf[4] = media_rotation_rate >> 8;
2132 rbuf[5] = media_rotation_rate;
2133 rbuf[7] = form_factor;
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06002134
2135 return 0;
2136}
2137
Jeff Garzikad355b42007-09-21 20:38:03 -04002138/**
Jeff Garzik0cba6322005-05-30 19:49:12 -04002139 * ata_scsiop_noop - Command handler that simply returns success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 * @args: device IDENTIFY data / SCSI command of interest.
2141 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 *
2143 * No operation. Simply returns success to caller, to indicate
2144 * that the caller should successfully complete this SCSI command.
2145 *
2146 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002147 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002148 */
Tejun Heo87340e92008-04-28 17:48:51 +09002149static unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002150{
2151 VPRINTK("ENTER\n");
2152 return 0;
2153}
2154
2155/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 * ata_msense_caching - Simulate MODE SENSE caching info page
2157 * @id: device IDENTIFY data
Tejun Heo87340e92008-04-28 17:48:51 +09002158 * @buf: output buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002159 *
2160 * Generate a caching info page, which conditionally indicates
2161 * write caching to the SCSI layer, depending on device
2162 * capabilities.
2163 *
2164 * LOCKING:
2165 * None.
2166 */
Tejun Heo87340e92008-04-28 17:48:51 +09002167static unsigned int ata_msense_caching(u16 *id, u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168{
Tejun Heo87340e92008-04-28 17:48:51 +09002169 memcpy(buf, def_cache_mpage, sizeof(def_cache_mpage));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002170 if (ata_id_wcache_enabled(id))
Tejun Heo87340e92008-04-28 17:48:51 +09002171 buf[2] |= (1 << 2); /* write cache enable */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172 if (!ata_id_rahead_enabled(id))
Tejun Heo87340e92008-04-28 17:48:51 +09002173 buf[12] |= (1 << 5); /* disable read ahead */
2174 return sizeof(def_cache_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002175}
2176
2177/**
2178 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
Tejun Heo87340e92008-04-28 17:48:51 +09002179 * @buf: output buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 *
2181 * Generate a generic MODE SENSE control mode page.
2182 *
2183 * LOCKING:
2184 * None.
2185 */
Tejun Heo87340e92008-04-28 17:48:51 +09002186static unsigned int ata_msense_ctl_mode(u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187{
Tejun Heo87340e92008-04-28 17:48:51 +09002188 memcpy(buf, def_control_mpage, sizeof(def_control_mpage));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002189 return sizeof(def_control_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190}
2191
2192/**
2193 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
Randy Dunlapec2a20e2008-04-30 12:57:00 -07002194 * @buf: output buffer
Linus Torvalds1da177e2005-04-16 15:20:36 -07002195 *
2196 * Generate a generic MODE SENSE r/w error recovery page.
2197 *
2198 * LOCKING:
2199 * None.
2200 */
Tejun Heo87340e92008-04-28 17:48:51 +09002201static unsigned int ata_msense_rw_recovery(u8 *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202{
Tejun Heo87340e92008-04-28 17:48:51 +09002203 memcpy(buf, def_rw_recovery_mpage, sizeof(def_rw_recovery_mpage));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002204 return sizeof(def_rw_recovery_mpage);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205}
2206
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002207/*
2208 * We can turn this into a real blacklist if it's needed, for now just
2209 * blacklist any Maxtor BANC1G10 revision firmware
2210 */
2211static int ata_dev_supports_fua(u16 *id)
2212{
Tejun Heoa0cf7332007-01-02 20:18:49 +09002213 unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002214
Jeff Garzikc3c013a2006-02-27 22:31:19 -05002215 if (!libata_fua)
2216 return 0;
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002217 if (!ata_id_has_fua(id))
2218 return 0;
2219
Tejun Heoa0cf7332007-01-02 20:18:49 +09002220 ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
2221 ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002222
Tejun Heo2e026712006-02-12 22:47:04 +09002223 if (strcmp(model, "Maxtor"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002224 return 1;
Tejun Heo2e026712006-02-12 22:47:04 +09002225 if (strcmp(fw, "BANC1G10"))
Jens Axboe48bdc8e2006-01-30 16:09:35 +01002226 return 1;
2227
2228 return 0; /* blacklisted */
2229}
2230
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231/**
2232 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
2233 * @args: device IDENTIFY data / SCSI command of interest.
2234 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 *
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002236 * Simulate MODE SENSE commands. Assume this is invoked for direct
2237 * access devices (e.g. disks) only. There should be no block
2238 * descriptor for other device types.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002239 *
2240 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002241 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 */
Tejun Heo87340e92008-04-28 17:48:51 +09002243static unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002244{
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002245 struct ata_device *dev = args->dev;
Tejun Heo87340e92008-04-28 17:48:51 +09002246 u8 *scsicmd = args->cmd->cmnd, *p = rbuf;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002247 const u8 sat_blk_desc[] = {
2248 0, 0, 0, 0, /* number of blocks: sat unspecified */
2249 0,
2250 0, 0x2, 0x0 /* block length: 512 bytes */
2251 };
2252 u8 pg, spg;
Tejun Heo87340e92008-04-28 17:48:51 +09002253 unsigned int ebd, page_control, six_byte;
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002254 u8 dpofua;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255
2256 VPRINTK("ENTER\n");
2257
2258 six_byte = (scsicmd[0] == MODE_SENSE);
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002259 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
2260 /*
2261 * LLBA bit in msense(10) ignored (compliant)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262 */
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002263
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 page_control = scsicmd[2] >> 6;
Douglas Gilbertae006512005-10-09 09:09:35 -04002265 switch (page_control) {
2266 case 0: /* current */
2267 break; /* supported */
2268 case 3: /* saved */
2269 goto saving_not_supp;
2270 case 1: /* changeable */
2271 case 2: /* defaults */
2272 default:
2273 goto invalid_fld;
2274 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Tejun Heo87340e92008-04-28 17:48:51 +09002276 if (six_byte)
2277 p += 4 + (ebd ? 8 : 0);
2278 else
2279 p += 8 + (ebd ? 8 : 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002281 pg = scsicmd[2] & 0x3f;
2282 spg = scsicmd[3];
2283 /*
2284 * No mode subpages supported (yet) but asking for _all_
2285 * subpages may be valid
2286 */
2287 if (spg && (spg != ALL_SUB_MPAGES))
2288 goto invalid_fld;
2289
2290 switch(pg) {
2291 case RW_RECOVERY_MPAGE:
Tejun Heo87340e92008-04-28 17:48:51 +09002292 p += ata_msense_rw_recovery(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002293 break;
2294
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002295 case CACHE_MPAGE:
Tejun Heo87340e92008-04-28 17:48:51 +09002296 p += ata_msense_caching(args->id, p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 break;
2298
Tejun Heo87340e92008-04-28 17:48:51 +09002299 case CONTROL_MPAGE:
2300 p += ata_msense_ctl_mode(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002301 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002303 case ALL_MPAGES:
Tejun Heo87340e92008-04-28 17:48:51 +09002304 p += ata_msense_rw_recovery(p);
2305 p += ata_msense_caching(args->id, p);
2306 p += ata_msense_ctl_mode(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307 break;
2308
2309 default: /* invalid page code */
Douglas Gilbertae006512005-10-09 09:09:35 -04002310 goto invalid_fld;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 }
2312
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002313 dpofua = 0;
Alan Coxf79d4092006-05-22 16:55:11 +01002314 if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
Tejun Heo9a3dccc2006-01-06 09:56:18 +01002315 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
2316 dpofua = 1 << 4;
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 if (six_byte) {
Tejun Heo87340e92008-04-28 17:48:51 +09002319 rbuf[0] = p - rbuf - 1;
2320 rbuf[2] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002321 if (ebd) {
Tejun Heo87340e92008-04-28 17:48:51 +09002322 rbuf[3] = sizeof(sat_blk_desc);
2323 memcpy(rbuf + 4, sat_blk_desc, sizeof(sat_blk_desc));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325 } else {
Tejun Heo87340e92008-04-28 17:48:51 +09002326 unsigned int output_len = p - rbuf - 2;
2327
Linus Torvalds1da177e2005-04-16 15:20:36 -07002328 rbuf[0] = output_len >> 8;
Tejun Heo87340e92008-04-28 17:48:51 +09002329 rbuf[1] = output_len;
2330 rbuf[3] |= dpofua;
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002331 if (ebd) {
Tejun Heo87340e92008-04-28 17:48:51 +09002332 rbuf[7] = sizeof(sat_blk_desc);
2333 memcpy(rbuf + 8, sat_blk_desc, sizeof(sat_blk_desc));
Douglas Gilbert00ac37f2005-10-28 15:58:28 -04002334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002336 return 0;
Douglas Gilbertae006512005-10-09 09:09:35 -04002337
2338invalid_fld:
2339 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
2340 /* "Invalid field in cbd" */
2341 return 1;
2342
2343saving_not_supp:
2344 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
2345 /* "Saving parameters not supported" */
2346 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347}
2348
2349/**
2350 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2351 * @args: device IDENTIFY data / SCSI command of interest.
2352 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 *
2354 * Simulate READ CAPACITY commands.
2355 *
2356 * LOCKING:
Tejun Heo6a362612006-11-20 11:15:47 +09002357 * None.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358 */
Tejun Heo87340e92008-04-28 17:48:51 +09002359static unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360{
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002361 struct ata_device *dev = args->dev;
2362 u64 last_lba = dev->n_sectors - 1; /* LBA of the last block */
Grant Grundler295124d2010-08-17 10:56:53 -07002363 u32 sector_size; /* physical sector size in bytes */
2364 u8 log2_per_phys;
2365 u16 lowest_aligned;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002366
Grant Grundler295124d2010-08-17 10:56:53 -07002367 sector_size = ata_id_logical_sector_size(dev->id);
2368 log2_per_phys = ata_id_log2_per_physical_sector(dev->id);
2369 lowest_aligned = ata_id_logical_sector_offset(dev->id, log2_per_phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
2371 VPRINTK("ENTER\n");
2372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 if (args->cmd->cmnd[0] == READ_CAPACITY) {
Tejun Heo6a362612006-11-20 11:15:47 +09002374 if (last_lba >= 0xffffffffULL)
2375 last_lba = 0xffffffff;
Philip Pokorny0c144d02005-05-28 01:24:47 -07002376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 /* sector count, 32-bit */
Tejun Heo87340e92008-04-28 17:48:51 +09002378 rbuf[0] = last_lba >> (8 * 3);
2379 rbuf[1] = last_lba >> (8 * 2);
2380 rbuf[2] = last_lba >> (8 * 1);
2381 rbuf[3] = last_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002382
2383 /* sector size */
Grant Grundler295124d2010-08-17 10:56:53 -07002384 rbuf[4] = sector_size >> (8 * 3);
2385 rbuf[5] = sector_size >> (8 * 2);
2386 rbuf[6] = sector_size >> (8 * 1);
2387 rbuf[7] = sector_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388 } else {
2389 /* sector count, 64-bit */
Tejun Heo87340e92008-04-28 17:48:51 +09002390 rbuf[0] = last_lba >> (8 * 7);
2391 rbuf[1] = last_lba >> (8 * 6);
2392 rbuf[2] = last_lba >> (8 * 5);
2393 rbuf[3] = last_lba >> (8 * 4);
2394 rbuf[4] = last_lba >> (8 * 3);
2395 rbuf[5] = last_lba >> (8 * 2);
2396 rbuf[6] = last_lba >> (8 * 1);
2397 rbuf[7] = last_lba;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398
2399 /* sector size */
Grant Grundler295124d2010-08-17 10:56:53 -07002400 rbuf[ 8] = sector_size >> (8 * 3);
2401 rbuf[ 9] = sector_size >> (8 * 2);
2402 rbuf[10] = sector_size >> (8 * 1);
2403 rbuf[11] = sector_size;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002404
2405 rbuf[12] = 0;
Grant Grundler295124d2010-08-17 10:56:53 -07002406 rbuf[13] = log2_per_phys;
Martin K. Petersen61d79a82009-05-15 00:40:34 -04002407 rbuf[14] = (lowest_aligned >> 8) & 0x3f;
2408 rbuf[15] = lowest_aligned;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002409
Martin K. Petersene78db4d2009-11-26 22:46:03 -05002410 if (ata_id_has_trim(args->id)) {
2411 rbuf[14] |= 0x80; /* TPE */
2412
2413 if (ata_id_has_zero_after_trim(args->id))
2414 rbuf[14] |= 0x40; /* TPRZ */
2415 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 }
2417
2418 return 0;
2419}
2420
2421/**
2422 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2423 * @args: device IDENTIFY data / SCSI command of interest.
2424 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 *
2426 * Simulate REPORT LUNS command.
2427 *
2428 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002429 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430 */
Tejun Heo87340e92008-04-28 17:48:51 +09002431static unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432{
2433 VPRINTK("ENTER\n");
2434 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2435
2436 return 0;
2437}
2438
Tejun Heo77853bf2006-01-23 13:09:36 +09002439static void atapi_sense_complete(struct ata_queued_cmd *qc)
Jeff Garzika939c962005-10-05 17:09:16 -04002440{
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002441 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002442 /* FIXME: not quite right; we don't want the
2443 * translation of taskfile registers into
2444 * a sense descriptors, since that's only
2445 * correct for ATA, not ATAPI
2446 */
Tejun Heo750426a2006-11-14 22:37:35 +09002447 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002448 }
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002449
2450 qc->scsidone(qc->scsicmd);
Tejun Heo77853bf2006-01-23 13:09:36 +09002451 ata_qc_free(qc);
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002452}
2453
2454/* is it pointless to prefer PIO for "safety reasons"? */
2455static inline int ata_pio_use_silly(struct ata_port *ap)
2456{
2457 return (ap->flags & ATA_FLAG_PIO_DMA);
2458}
2459
2460static void atapi_request_sense(struct ata_queued_cmd *qc)
2461{
2462 struct ata_port *ap = qc->ap;
2463 struct scsi_cmnd *cmd = qc->scsicmd;
Jeff Garzika939c962005-10-05 17:09:16 -04002464
2465 DPRINTK("ATAPI request sense\n");
2466
Jeff Garzika939c962005-10-05 17:09:16 -04002467 /* FIXME: is this needed? */
James Bottomley7ccd7202008-01-17 11:56:24 -06002468 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
Jeff Garzika939c962005-10-05 17:09:16 -04002469
Tejun Heo127102a2008-04-07 22:47:21 +09002470#ifdef CONFIG_ATA_SFF
James Bottomley855d8542008-04-18 13:18:48 -05002471 if (ap->ops->sff_tf_read)
2472 ap->ops->sff_tf_read(ap, &qc->tf);
Tejun Heo127102a2008-04-07 22:47:21 +09002473#endif
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002474
2475 /* fill these in, for the case where they are -not- overwritten */
2476 cmd->sense_buffer[0] = 0x70;
2477 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2478
2479 ata_qc_reinit(qc);
2480
Tejun Heo93f8fec2007-12-05 16:43:01 +09002481 /* setup sg table and init transfer direction */
akpm@linux-foundation.orgcadb7342008-01-15 16:01:52 -08002482 sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
Tejun Heo93f8fec2007-12-05 16:43:01 +09002483 ata_sg_init(qc, &qc->sgent, 1);
Jeff Garzika939c962005-10-05 17:09:16 -04002484 qc->dma_dir = DMA_FROM_DEVICE;
2485
Tejun Heo6e7846e2006-02-12 23:32:58 +09002486 memset(&qc->cdb, 0, qc->dev->cdb_len);
Jeff Garzika939c962005-10-05 17:09:16 -04002487 qc->cdb[0] = REQUEST_SENSE;
2488 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2489
2490 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2491 qc->tf.command = ATA_CMD_PACKET;
2492
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002493 if (ata_pio_use_silly(ap)) {
Tejun Heo0dc36882007-12-18 16:34:43 -05002494 qc->tf.protocol = ATAPI_PROT_DMA;
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002495 qc->tf.feature |= ATAPI_PKT_DMA;
2496 } else {
Tejun Heo0dc36882007-12-18 16:34:43 -05002497 qc->tf.protocol = ATAPI_PROT_PIO;
Alan Cox2db78dd2007-10-02 13:53:04 -07002498 qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
2499 qc->tf.lbah = 0;
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002500 }
Jeff Garzika939c962005-10-05 17:09:16 -04002501 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2502
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002503 qc->complete_fn = atapi_sense_complete;
Jeff Garzika939c962005-10-05 17:09:16 -04002504
Tejun Heo8e0e6942006-03-31 20:41:11 +09002505 ata_qc_issue(qc);
Jeff Garzika939c962005-10-05 17:09:16 -04002506
2507 DPRINTK("EXIT\n");
2508}
2509
Tejun Heo77853bf2006-01-23 13:09:36 +09002510static void atapi_qc_complete(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002511{
2512 struct scsi_cmnd *cmd = qc->scsicmd;
Albert Leea22e2eb2005-12-05 15:38:02 +08002513 unsigned int err_mask = qc->err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002514
Jeff Garzika7dac442005-10-30 04:44:42 -05002515 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
Jeff Garzike12669e2005-10-05 18:39:23 -04002516
Tejun Heo246619d2006-05-15 20:58:16 +09002517 /* handle completion from new EH */
2518 if (unlikely(qc->ap->ops->error_handler &&
2519 (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
2520
2521 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
2522 /* FIXME: not quite right; we don't want the
2523 * translation of taskfile registers into a
2524 * sense descriptors, since that's only
2525 * correct for ATA, not ATAPI
2526 */
Tejun Heo750426a2006-11-14 22:37:35 +09002527 ata_gen_passthru_sense(qc);
Tejun Heo246619d2006-05-15 20:58:16 +09002528 }
2529
Tejun Heo22aac082006-08-08 14:08:59 +09002530 /* SCSI EH automatically locks door if sdev->locked is
2531 * set. Sometimes door lock request continues to
2532 * fail, for example, when no media is present. This
2533 * creates a loop - SCSI EH issues door lock which
2534 * fails and gets invoked again to acquire sense data
2535 * for the failed command.
2536 *
2537 * If door lock fails, always clear sdev->locked to
2538 * avoid this infinite loop.
2539 */
2540 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL)
2541 qc->dev->sdev->locked = 0;
2542
Tejun Heo246619d2006-05-15 20:58:16 +09002543 qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
2544 qc->scsidone(cmd);
2545 ata_qc_free(qc);
2546 return;
2547 }
2548
2549 /* successful completion or old EH failure path */
Jeff Garzika7dac442005-10-30 04:44:42 -05002550 if (unlikely(err_mask & AC_ERR_DEV)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551 cmd->result = SAM_STAT_CHECK_CONDITION;
Jeff Garzikc6e6e662005-11-14 14:50:05 -05002552 atapi_request_sense(qc);
Tejun Heo77853bf2006-01-23 13:09:36 +09002553 return;
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002554 } else if (unlikely(err_mask)) {
Jeff Garzika7dac442005-10-30 04:44:42 -05002555 /* FIXME: not quite right; we don't want the
2556 * translation of taskfile registers into
2557 * a sense descriptors, since that's only
2558 * correct for ATA, not ATAPI
2559 */
Tejun Heo750426a2006-11-14 22:37:35 +09002560 ata_gen_passthru_sense(qc);
Tejun Heo74e6c8c2006-04-02 18:51:53 +09002561 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 u8 *scsicmd = cmd->cmnd;
2563
Tony Battersbyfd71da42005-12-21 16:35:44 -05002564 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
Jeff Garzikb445c562008-02-29 19:10:51 -05002565 unsigned long flags;
Tejun Heo87340e92008-04-28 17:48:51 +09002566 u8 *buf;
Jeff Garzikb445c562008-02-29 19:10:51 -05002567
Tejun Heo87340e92008-04-28 17:48:51 +09002568 buf = ata_scsi_rbuf_get(cmd, true, &flags);
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002569
2570 /* ATAPI devices typically report zero for their SCSI version,
2571 * and sometimes deviate from the spec WRT response data
2572 * format. If SCSI version is reported as zero like normal,
2573 * then we make the following fixups: 1) Fake MMC-5 version,
2574 * to indicate to the Linux scsi midlayer this is a modern
2575 * device. 2) Ensure response data format / ATAPI information
2576 * are always correct.
2577 */
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002578 if (buf[2] == 0) {
2579 buf[2] = 0x5;
2580 buf[3] = 0x32;
2581 }
2582
Tejun Heo87340e92008-04-28 17:48:51 +09002583 ata_scsi_rbuf_put(cmd, true, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584 }
Jeff Garzika15dbeb2005-10-05 15:02:14 -04002585
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 cmd->result = SAM_STAT_GOOD;
2587 }
2588
2589 qc->scsidone(cmd);
Tejun Heo77853bf2006-01-23 13:09:36 +09002590 ata_qc_free(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002591}
2592/**
2593 * atapi_xlat - Initialize PACKET taskfile
2594 * @qc: command structure to be initialized
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595 *
2596 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002597 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 *
2599 * RETURNS:
2600 * Zero on success, non-zero on failure.
2601 */
Tejun Heoad706992006-12-17 10:45:57 +09002602static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603{
Tejun Heo542b1442006-12-17 10:45:08 +09002604 struct scsi_cmnd *scmd = qc->scsicmd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002605 struct ata_device *dev = qc->dev;
Tejun Heo542b1442006-12-17 10:45:08 +09002606 int nodata = (scmd->sc_data_direction == DMA_NONE);
Tejun Heo5895ef92008-06-17 12:36:26 +09002607 int using_pio = !nodata && (dev->flags & ATA_DFLAG_PIO);
Alan Cox2db78dd2007-10-02 13:53:04 -07002608 unsigned int nbytes;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609
Tejun Heo2e5704f2006-12-17 10:46:33 +09002610 memset(qc->cdb, 0, dev->cdb_len);
2611 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
2613 qc->complete_fn = atapi_qc_complete;
2614
2615 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heo542b1442006-12-17 10:45:08 +09002616 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 qc->tf.flags |= ATA_TFLAG_WRITE;
2618 DPRINTK("direction: write\n");
2619 }
2620
2621 qc->tf.command = ATA_CMD_PACKET;
Tejun Heoaacda372008-03-18 17:47:43 +09002622 ata_qc_set_pc_nbytes(qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002623
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002624 /* check whether ATAPI DMA is safe */
Tejun Heo5895ef92008-06-17 12:36:26 +09002625 if (!nodata && !using_pio && atapi_check_dma(qc))
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002626 using_pio = 1;
2627
Tejun Heoe1902222007-11-26 20:58:02 +09002628 /* Some controller variants snoop this value for Packet
2629 * transfers to do state machine and FIFO management. Thus we
2630 * want to set it properly, and for DMA where it is
2631 * effectively meaningless.
2632 */
Tejun Heoaacda372008-03-18 17:47:43 +09002633 nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
Alan Cox2db78dd2007-10-02 13:53:04 -07002634
Tejun Heoe1902222007-11-26 20:58:02 +09002635 /* Most ATAPI devices which honor transfer chunk size don't
2636 * behave according to the spec when odd chunk size which
2637 * matches the transfer length is specified. If the number of
2638 * bytes to transfer is 2n+1. According to the spec, what
2639 * should happen is to indicate that 2n+1 is going to be
2640 * transferred and transfer 2n+2 bytes where the last byte is
2641 * padding.
2642 *
2643 * In practice, this doesn't happen. ATAPI devices first
2644 * indicate and transfer 2n bytes and then indicate and
2645 * transfer 2 bytes where the last byte is padding.
2646 *
2647 * This inconsistency confuses several controllers which
2648 * perform PIO using DMA such as Intel AHCIs and sil3124/32.
2649 * These controllers use actual number of transferred bytes to
2650 * update DMA poitner and transfer of 4n+2 bytes make those
2651 * controller push DMA pointer by 4n+4 bytes because SATA data
2652 * FISes are aligned to 4 bytes. This causes data corruption
2653 * and buffer overrun.
2654 *
2655 * Always setting nbytes to even number solves this problem
2656 * because then ATAPI devices don't have to split data at 2n
2657 * boundaries.
2658 */
2659 if (nbytes & 0x1)
2660 nbytes++;
2661
Alan Cox2db78dd2007-10-02 13:53:04 -07002662 qc->tf.lbam = (nbytes & 0xFF);
2663 qc->tf.lbah = (nbytes >> 8);
2664
Tejun Heo5895ef92008-06-17 12:36:26 +09002665 if (nodata)
2666 qc->tf.protocol = ATAPI_PROT_NODATA;
2667 else if (using_pio)
2668 qc->tf.protocol = ATAPI_PROT_PIO;
2669 else {
Tejun Heoe00f1ff2007-06-27 02:47:35 +09002670 /* DMA data xfer */
Tejun Heo0dc36882007-12-18 16:34:43 -05002671 qc->tf.protocol = ATAPI_PROT_DMA;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002672 qc->tf.feature |= ATAPI_PKT_DMA;
2673
Tejun Heo91163002008-02-21 13:25:50 +09002674 if ((dev->flags & ATA_DFLAG_DMADIR) &&
2675 (scmd->sc_data_direction != DMA_TO_DEVICE))
Albert Lee95de7192006-04-04 10:57:18 +08002676 /* some SATA bridges need us to indicate data xfer direction */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 qc->tf.feature |= ATAPI_DMADIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 }
2679
Alan Cox2db78dd2007-10-02 13:53:04 -07002680
2681 /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
2682 as ATAPI tape drives don't get this right otherwise */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002683 return 0;
2684}
2685
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002686static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
Tejun Heoab5b3a52006-05-31 18:27:34 +09002687{
Tejun Heo071f44b2008-04-07 22:47:22 +09002688 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09002689 if (likely(devno < ata_link_max_devices(&ap->link)))
2690 return &ap->link.device[devno];
2691 } else {
2692 if (likely(devno < ap->nr_pmp_links))
2693 return &ap->pmp_link[devno].device[0];
2694 }
2695
Tejun Heoab5b3a52006-05-31 18:27:34 +09002696 return NULL;
2697}
2698
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002699static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
2700 const struct scsi_device *scsidev)
Tejun Heoab5b3a52006-05-31 18:27:34 +09002701{
Tejun Heo41bda9c2007-08-06 18:36:24 +09002702 int devno;
Tejun Heoab5b3a52006-05-31 18:27:34 +09002703
Tejun Heo41bda9c2007-08-06 18:36:24 +09002704 /* skip commands not addressed to targets we simulate */
Tejun Heo071f44b2008-04-07 22:47:22 +09002705 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09002706 if (unlikely(scsidev->channel || scsidev->lun))
2707 return NULL;
2708 devno = scsidev->id;
2709 } else {
2710 if (unlikely(scsidev->id || scsidev->lun))
2711 return NULL;
2712 devno = scsidev->channel;
2713 }
2714
2715 return ata_find_dev(ap, devno);
Tejun Heoab5b3a52006-05-31 18:27:34 +09002716}
2717
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718/**
2719 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2720 * @ap: ATA port to which the device is attached
2721 * @scsidev: SCSI device from which we derive the ATA device
2722 *
2723 * Given various information provided in struct scsi_cmnd,
2724 * map that onto an ATA bus, and using that mapping
2725 * determine which ata_device is associated with the
2726 * SCSI command to be sent.
2727 *
2728 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002729 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 *
2731 * RETURNS:
2732 * Associated ATA device, or %NULL if not found.
2733 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734static struct ata_device *
Jeff Garzik057ace52005-10-22 14:27:05 -04002735ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736{
Tejun Heoab5b3a52006-05-31 18:27:34 +09002737 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002738
Tejun Heo2486fa52008-07-31 07:52:40 +09002739 if (unlikely(!dev || !ata_dev_enabled(dev)))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 return NULL;
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 return dev;
2743}
2744
Jeff Garzikb0955182005-05-12 15:45:22 -04002745/*
2746 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2747 * @byte1: Byte 1 from pass-thru CDB.
2748 *
2749 * RETURNS:
2750 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2751 */
2752static u8
2753ata_scsi_map_proto(u8 byte1)
2754{
2755 switch((byte1 & 0x1e) >> 1) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002756 case 3: /* Non-data */
2757 return ATA_PROT_NODATA;
Jeff Garzikb0955182005-05-12 15:45:22 -04002758
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002759 case 6: /* DMA */
2760 case 10: /* UDMA Data-in */
2761 case 11: /* UDMA Data-Out */
2762 return ATA_PROT_DMA;
Jeff Garzikb0955182005-05-12 15:45:22 -04002763
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002764 case 4: /* PIO Data-in */
2765 case 5: /* PIO Data-out */
2766 return ATA_PROT_PIO;
Jeff Garzikb0955182005-05-12 15:45:22 -04002767
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002768 case 0: /* Hard Reset */
2769 case 1: /* SRST */
2770 case 8: /* Device Diagnostic */
2771 case 9: /* Device Reset */
2772 case 7: /* DMA Queued */
2773 case 12: /* FPDMA */
2774 case 15: /* Return Response Info */
2775 default: /* Reserved */
2776 break;
Jeff Garzikb0955182005-05-12 15:45:22 -04002777 }
2778
2779 return ATA_PROT_UNKNOWN;
2780}
2781
2782/**
2783 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
2784 * @qc: command structure to be initialized
Jeff Garzikb0955182005-05-12 15:45:22 -04002785 *
2786 * Handles either 12 or 16-byte versions of the CDB.
2787 *
2788 * RETURNS:
2789 * Zero on success, non-zero on failure.
2790 */
Tejun Heoad706992006-12-17 10:45:57 +09002791static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
Jeff Garzikb0955182005-05-12 15:45:22 -04002792{
2793 struct ata_taskfile *tf = &(qc->tf);
Tejun Heo542b1442006-12-17 10:45:08 +09002794 struct scsi_cmnd *scmd = qc->scsicmd;
Alan Coxf79d4092006-05-22 16:55:11 +01002795 struct ata_device *dev = qc->dev;
Tejun Heoad706992006-12-17 10:45:57 +09002796 const u8 *cdb = scmd->cmnd;
Jeff Garzikb0955182005-05-12 15:45:22 -04002797
Tejun Heo542b1442006-12-17 10:45:08 +09002798 if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN)
Tejun Heo9a405252005-12-02 11:49:11 +09002799 goto invalid_fld;
Jeff Garzik8190bdb2006-05-24 01:53:39 -04002800
Alan Coxae8d4ee2007-11-04 22:05:49 -05002801 /*
Jeff Garzikb0955182005-05-12 15:45:22 -04002802 * 12 and 16 byte CDBs use different offsets to
2803 * provide the various register values.
2804 */
Tejun Heo542b1442006-12-17 10:45:08 +09002805 if (cdb[0] == ATA_16) {
Jeff Garzikb0955182005-05-12 15:45:22 -04002806 /*
2807 * 16-byte CDB - may contain extended commands.
2808 *
2809 * If that is the case, copy the upper byte register values.
2810 */
Tejun Heo542b1442006-12-17 10:45:08 +09002811 if (cdb[1] & 0x01) {
2812 tf->hob_feature = cdb[3];
2813 tf->hob_nsect = cdb[5];
2814 tf->hob_lbal = cdb[7];
2815 tf->hob_lbam = cdb[9];
2816 tf->hob_lbah = cdb[11];
Jeff Garzikb0955182005-05-12 15:45:22 -04002817 tf->flags |= ATA_TFLAG_LBA48;
2818 } else
2819 tf->flags &= ~ATA_TFLAG_LBA48;
2820
2821 /*
2822 * Always copy low byte, device and command registers.
2823 */
Tejun Heo542b1442006-12-17 10:45:08 +09002824 tf->feature = cdb[4];
2825 tf->nsect = cdb[6];
2826 tf->lbal = cdb[8];
2827 tf->lbam = cdb[10];
2828 tf->lbah = cdb[12];
2829 tf->device = cdb[13];
2830 tf->command = cdb[14];
Jeff Garzikb0955182005-05-12 15:45:22 -04002831 } else {
2832 /*
2833 * 12-byte CDB - incapable of extended commands.
2834 */
2835 tf->flags &= ~ATA_TFLAG_LBA48;
2836
Tejun Heo542b1442006-12-17 10:45:08 +09002837 tf->feature = cdb[3];
2838 tf->nsect = cdb[4];
2839 tf->lbal = cdb[5];
2840 tf->lbam = cdb[6];
2841 tf->lbah = cdb[7];
2842 tf->device = cdb[8];
2843 tf->command = cdb[9];
Jeff Garzikb0955182005-05-12 15:45:22 -04002844 }
Albert Leefa4453c2007-06-07 15:52:07 +08002845
2846 /* enforce correct master/slave bit */
2847 tf->device = dev->devno ?
2848 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
Jeff Garzikb0955182005-05-12 15:45:22 -04002849
Tejun Heobd30add2009-09-03 16:08:11 +09002850 switch (tf->command) {
Grant Grundler295124d2010-08-17 10:56:53 -07002851 /* READ/WRITE LONG use a non-standard sect_size */
Tejun Heobd30add2009-09-03 16:08:11 +09002852 case ATA_CMD_READ_LONG:
2853 case ATA_CMD_READ_LONG_ONCE:
2854 case ATA_CMD_WRITE_LONG:
2855 case ATA_CMD_WRITE_LONG_ONCE:
2856 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1)
2857 goto invalid_fld;
2858 qc->sect_size = scsi_bufflen(scmd);
Grant Grundler295124d2010-08-17 10:56:53 -07002859 break;
2860
2861 /* commands using reported Logical Block size (e.g. 512 or 4K) */
2862 case ATA_CMD_CFA_WRITE_NE:
2863 case ATA_CMD_CFA_TRANS_SECT:
2864 case ATA_CMD_CFA_WRITE_MULT_NE:
2865 /* XXX: case ATA_CMD_CFA_WRITE_SECTORS_WITHOUT_ERASE: */
2866 case ATA_CMD_READ:
2867 case ATA_CMD_READ_EXT:
2868 case ATA_CMD_READ_QUEUED:
2869 /* XXX: case ATA_CMD_READ_QUEUED_EXT: */
2870 case ATA_CMD_FPDMA_READ:
2871 case ATA_CMD_READ_MULTI:
2872 case ATA_CMD_READ_MULTI_EXT:
2873 case ATA_CMD_PIO_READ:
2874 case ATA_CMD_PIO_READ_EXT:
2875 case ATA_CMD_READ_STREAM_DMA_EXT:
2876 case ATA_CMD_READ_STREAM_EXT:
2877 case ATA_CMD_VERIFY:
2878 case ATA_CMD_VERIFY_EXT:
2879 case ATA_CMD_WRITE:
2880 case ATA_CMD_WRITE_EXT:
2881 case ATA_CMD_WRITE_FUA_EXT:
2882 case ATA_CMD_WRITE_QUEUED:
2883 case ATA_CMD_WRITE_QUEUED_FUA_EXT:
2884 case ATA_CMD_FPDMA_WRITE:
2885 case ATA_CMD_WRITE_MULTI:
2886 case ATA_CMD_WRITE_MULTI_EXT:
2887 case ATA_CMD_WRITE_MULTI_FUA_EXT:
2888 case ATA_CMD_PIO_WRITE:
2889 case ATA_CMD_PIO_WRITE_EXT:
2890 case ATA_CMD_WRITE_STREAM_DMA_EXT:
2891 case ATA_CMD_WRITE_STREAM_EXT:
2892 qc->sect_size = scmd->device->sector_size;
2893 break;
2894
2895 /* Everything else uses 512 byte "sectors" */
2896 default:
2897 qc->sect_size = ATA_SECT_SIZE;
Tejun Heobd30add2009-09-03 16:08:11 +09002898 }
2899
2900 /*
2901 * Set flags so that all registers will be written, pass on
2902 * write indication (used for PIO/DMA setup), result TF is
2903 * copied back and we don't whine too much about its failure.
2904 */
Douglas Gilbertbc496ed2010-02-01 13:11:38 -05002905 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
Tejun Heobd30add2009-09-03 16:08:11 +09002906 if (scmd->sc_data_direction == DMA_TO_DEVICE)
2907 tf->flags |= ATA_TFLAG_WRITE;
2908
2909 qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
2910
2911 /*
2912 * Set transfer length.
2913 *
2914 * TODO: find out if we need to do more here to
2915 * cover scatter/gather case.
2916 */
2917 ata_qc_set_pc_nbytes(qc);
2918
2919 /* We may not issue DMA commands if no DMA mode is set */
2920 if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0)
2921 goto invalid_fld;
2922
Albert Lee1dce5892007-06-07 15:49:22 +08002923 /* sanity check for pio multi commands */
2924 if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf))
2925 goto invalid_fld;
2926
2927 if (is_multi_taskfile(tf)) {
2928 unsigned int multi_count = 1 << (cdb[1] >> 5);
2929
2930 /* compare the passed through multi_count
2931 * with the cached multi_count of libata
2932 */
2933 if (multi_count != dev->multi_count)
2934 ata_dev_printk(dev, KERN_WARNING,
2935 "invalid multi_count %u ignored\n",
2936 multi_count);
Alan Coxd26fc952007-07-06 19:13:52 -04002937 }
Albert Lee1dce5892007-06-07 15:49:22 +08002938
Jeff Garzikb0955182005-05-12 15:45:22 -04002939 /*
2940 * Filter SET_FEATURES - XFER MODE command -- otherwise,
2941 * SET_FEATURES - XFER MODE must be preceded/succeeded
2942 * by an update to hardware-specific registers for each
2943 * controller (i.e. the reason for ->set_piomode(),
2944 * ->set_dmamode(), and ->post_set_mode() hooks).
2945 */
Tejun Heobd30add2009-09-03 16:08:11 +09002946 if (tf->command == ATA_CMD_SET_FEATURES &&
2947 tf->feature == SETFEATURES_XFER)
Tejun Heo9a405252005-12-02 11:49:11 +09002948 goto invalid_fld;
Jeff Garzikb0955182005-05-12 15:45:22 -04002949
2950 /*
Tejun Heobd30add2009-09-03 16:08:11 +09002951 * Filter TPM commands by default. These provide an
2952 * essentially uncontrolled encrypted "back door" between
2953 * applications and the disk. Set libata.allow_tpm=1 if you
2954 * have a real reason for wanting to use them. This ensures
2955 * that installed software cannot easily mess stuff up without
2956 * user intent. DVR type users will probably ship with this enabled
2957 * for movie content management.
Jeff Garzikb0955182005-05-12 15:45:22 -04002958 *
Tejun Heobd30add2009-09-03 16:08:11 +09002959 * Note that for ATA8 we can issue a DCS change and DCS freeze lock
2960 * for this and should do in future but that it is not sufficient as
2961 * DCS is an optional feature set. Thus we also do the software filter
2962 * so that we comply with the TC consortium stated goal that the user
2963 * can turn off TC features of their system.
Jeff Garzikb0955182005-05-12 15:45:22 -04002964 */
Tejun Heobd30add2009-09-03 16:08:11 +09002965 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm)
2966 goto invalid_fld;
Tejun Heoe61e0672006-05-15 20:57:40 +09002967
Jeff Garzikb0955182005-05-12 15:45:22 -04002968 return 0;
Tejun Heo9a405252005-12-02 11:49:11 +09002969
2970 invalid_fld:
Tejun Heo542b1442006-12-17 10:45:08 +09002971 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
Tejun Heo9a405252005-12-02 11:49:11 +09002972 /* "Invalid field in cdb" */
2973 return 1;
Jeff Garzikb0955182005-05-12 15:45:22 -04002974}
2975
Christoph Hellwig18f0f972009-11-17 10:00:47 -05002976static unsigned int ata_scsi_write_same_xlat(struct ata_queued_cmd *qc)
2977{
2978 struct ata_taskfile *tf = &qc->tf;
2979 struct scsi_cmnd *scmd = qc->scsicmd;
2980 struct ata_device *dev = qc->dev;
2981 const u8 *cdb = scmd->cmnd;
2982 u64 block;
2983 u32 n_block;
2984 u32 size;
2985 void *buf;
2986
2987 /* we may not issue DMA commands if no DMA mode is set */
2988 if (unlikely(!dev->dma_mode))
2989 goto invalid_fld;
2990
2991 if (unlikely(scmd->cmd_len < 16))
2992 goto invalid_fld;
2993 scsi_16_lba_len(cdb, &block, &n_block);
2994
2995 /* for now we only support WRITE SAME with the unmap bit set */
2996 if (unlikely(!(cdb[1] & 0x8)))
2997 goto invalid_fld;
2998
2999 /*
3000 * WRITE SAME always has a sector sized buffer as payload, this
3001 * should never be a multiple entry S/G list.
3002 */
3003 if (!scsi_sg_count(scmd))
3004 goto invalid_fld;
3005
3006 buf = page_address(sg_page(scsi_sglist(scmd)));
Martin K. Petersend0634c42009-11-26 12:00:43 -05003007 size = ata_set_lba_range_entries(buf, 512, block, n_block);
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003008
3009 tf->protocol = ATA_PROT_DMA;
3010 tf->hob_feature = 0;
3011 tf->feature = ATA_DSM_TRIM;
3012 tf->hob_nsect = (size / 512) >> 8;
3013 tf->nsect = size / 512;
3014 tf->command = ATA_CMD_DSM;
3015 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_LBA48 |
3016 ATA_TFLAG_WRITE;
3017
3018 ata_qc_set_pc_nbytes(qc);
3019
3020 return 0;
3021
3022 invalid_fld:
3023 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
3024 /* "Invalid field in cdb" */
3025 return 1;
3026}
3027
Linus Torvalds1da177e2005-04-16 15:20:36 -07003028/**
3029 * ata_get_xlat_func - check if SCSI to ATA translation is possible
3030 * @dev: ATA device
3031 * @cmd: SCSI command opcode to consider
3032 *
3033 * Look up the SCSI command given, and determine whether the
3034 * SCSI command is to be translated or simulated.
3035 *
3036 * RETURNS:
3037 * Pointer to translation function if possible, %NULL if not.
3038 */
3039
3040static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
3041{
3042 switch (cmd) {
3043 case READ_6:
3044 case READ_10:
3045 case READ_16:
3046
3047 case WRITE_6:
3048 case WRITE_10:
3049 case WRITE_16:
3050 return ata_scsi_rw_xlat;
3051
Christoph Hellwig0cdd6eb2009-12-10 10:36:01 +01003052 case WRITE_SAME_16:
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003053 return ata_scsi_write_same_xlat;
3054
Linus Torvalds1da177e2005-04-16 15:20:36 -07003055 case SYNCHRONIZE_CACHE:
3056 if (ata_try_flush_cache(dev))
3057 return ata_scsi_flush_xlat;
3058 break;
3059
3060 case VERIFY:
3061 case VERIFY_16:
3062 return ata_scsi_verify_xlat;
Jeff Garzikb0955182005-05-12 15:45:22 -04003063
3064 case ATA_12:
3065 case ATA_16:
3066 return ata_scsi_pass_thru;
Jeff Garzikda613962005-08-29 19:01:43 -04003067
Douglas Gilbert972dcaf2005-08-11 03:35:53 -04003068 case START_STOP:
3069 return ata_scsi_start_stop_xlat;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003070 }
3071
3072 return NULL;
3073}
3074
3075/**
3076 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
3077 * @ap: ATA port to which the command was being sent
3078 * @cmd: SCSI command to dump
3079 *
3080 * Prints the contents of a SCSI command via printk().
3081 */
3082
3083static inline void ata_scsi_dump_cdb(struct ata_port *ap,
3084 struct scsi_cmnd *cmd)
3085{
3086#ifdef ATA_DEBUG
3087 struct scsi_device *scsidev = cmd->device;
3088 u8 *scsicmd = cmd->cmnd;
3089
3090 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
Tejun Heo44877b42007-02-21 01:06:51 +09003091 ap->print_id,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 scsidev->channel, scsidev->id, scsidev->lun,
3093 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
3094 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
3095 scsicmd[8]);
3096#endif
3097}
3098
Tejun Heo542b1442006-12-17 10:45:08 +09003099static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
Tejun Heo2115ea92006-05-15 21:03:39 +09003100 void (*done)(struct scsi_cmnd *),
3101 struct ata_device *dev)
Brian Kingeb3f0f92006-03-23 17:30:02 -06003102{
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003103 u8 scsi_op = scmd->cmnd[0];
3104 ata_xlat_func_t xlat_func;
Tejun Heo2115ea92006-05-15 21:03:39 +09003105 int rc = 0;
3106
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003107 if (dev->class == ATA_DEV_ATA) {
3108 if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
3109 goto bad_cdb_len;
3110
3111 xlat_func = ata_get_xlat_func(dev, scsi_op);
3112 } else {
3113 if (unlikely(!scmd->cmd_len))
3114 goto bad_cdb_len;
3115
3116 xlat_func = NULL;
3117 if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
3118 /* relay SCSI command to ATAPI device */
Mark Lord607126c2007-11-15 13:13:59 +09003119 int len = COMMAND_SIZE(scsi_op);
3120 if (unlikely(len > scmd->cmd_len || len > dev->cdb_len))
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003121 goto bad_cdb_len;
3122
3123 xlat_func = atapi_xlat;
3124 } else {
3125 /* ATA_16 passthru, treat as an ATA command */
3126 if (unlikely(scmd->cmd_len > 16))
3127 goto bad_cdb_len;
3128
3129 xlat_func = ata_get_xlat_func(dev, scsi_op);
3130 }
Tejun Heo2e5704f2006-12-17 10:46:33 +09003131 }
3132
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003133 if (xlat_func)
3134 rc = ata_scsi_translate(dev, scmd, done, xlat_func);
3135 else
3136 ata_scsi_simulate(dev, scmd, done);
Tejun Heo2115ea92006-05-15 21:03:39 +09003137
3138 return rc;
Mark Lordbaf4fdf2007-08-08 01:08:45 +09003139
3140 bad_cdb_len:
3141 DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
3142 scmd->cmd_len, scsi_op, dev->cdb_len);
3143 scmd->result = DID_ERROR << 16;
3144 done(scmd);
3145 return 0;
Brian Kingeb3f0f92006-03-23 17:30:02 -06003146}
3147
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148/**
3149 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
3150 * @cmd: SCSI command to be sent
3151 * @done: Completion function, called when command is complete
3152 *
3153 * In some cases, this function translates SCSI commands into
3154 * ATA taskfiles, and queues the taskfiles to be sent to
3155 * hardware. In other cases, this function simulates a
3156 * SCSI device by evaluating and responding to certain
3157 * SCSI commands. This creates the overall effect of
3158 * ATA and ATAPI devices appearing as SCSI devices.
3159 *
3160 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003161 * Releases scsi-layer-held lock, and obtains host lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003162 *
3163 * RETURNS:
Tejun Heo2115ea92006-05-15 21:03:39 +09003164 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
3165 * 0 otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003166 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
3168{
3169 struct ata_port *ap;
3170 struct ata_device *dev;
3171 struct scsi_device *scsidev = cmd->device;
Jeff Garzik005a5a02005-10-30 23:31:48 -05003172 struct Scsi_Host *shost = scsidev->host;
Tejun Heo2115ea92006-05-15 21:03:39 +09003173 int rc = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174
Jeff Garzik35bb94b2006-04-11 13:12:34 -04003175 ap = ata_shost_to_port(shost);
Jeff Garzik005a5a02005-10-30 23:31:48 -05003176
3177 spin_unlock(shost->host_lock);
Jeff Garzikba6a1302006-06-22 23:46:10 -04003178 spin_lock(ap->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
3180 ata_scsi_dump_cdb(ap, cmd);
3181
3182 dev = ata_scsi_find_dev(ap, scsidev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06003183 if (likely(dev))
Tejun Heo2115ea92006-05-15 21:03:39 +09003184 rc = __ata_scsi_queuecmd(cmd, done, dev);
Brian Kingeb3f0f92006-03-23 17:30:02 -06003185 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186 cmd->result = (DID_BAD_TARGET << 16);
3187 done(cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 }
3189
Jeff Garzikba6a1302006-06-22 23:46:10 -04003190 spin_unlock(ap->lock);
Jeff Garzik005a5a02005-10-30 23:31:48 -05003191 spin_lock(shost->host_lock);
Tejun Heo2115ea92006-05-15 21:03:39 +09003192 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193}
3194
3195/**
3196 * ata_scsi_simulate - simulate SCSI command on ATA device
Randy Dunlapc893a3a2006-01-28 13:15:32 -05003197 * @dev: the target device
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 * @cmd: SCSI command being sent to device.
3199 * @done: SCSI command completion function.
3200 *
3201 * Interprets and directly executes a select list of SCSI commands
3202 * that can be handled internally.
3203 *
3204 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003205 * spin_lock_irqsave(host lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206 */
3207
Tejun Heo3373efd2006-05-15 20:57:53 +09003208void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 void (*done)(struct scsi_cmnd *))
3210{
3211 struct ata_scsi_args args;
Jeff Garzik057ace52005-10-22 14:27:05 -04003212 const u8 *scsicmd = cmd->cmnd;
Jeff Garzik45394142007-09-21 06:23:42 -04003213 u8 tmp8;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003214
Tejun Heo9a3dccc2006-01-06 09:56:18 +01003215 args.dev = dev;
3216 args.id = dev->id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 args.cmd = cmd;
3218 args.done = done;
3219
3220 switch(scsicmd[0]) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003221 /* TODO: worth improving? */
3222 case FORMAT_UNIT:
3223 ata_scsi_invalid_field(cmd, done);
3224 break;
3225
3226 case INQUIRY:
3227 if (scsicmd[1] & 2) /* is CmdDt set? */
Jeff Garzik00bd0202007-09-21 07:26:08 -04003228 ata_scsi_invalid_field(cmd, done);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003229 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
3230 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
3231 else switch (scsicmd[2]) {
3232 case 0x00:
3233 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003235 case 0x80:
3236 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003238 case 0x83:
3239 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003240 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003241 case 0x89:
3242 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 break;
Christoph Hellwig18f0f972009-11-17 10:00:47 -05003244 case 0xb0:
3245 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b0);
3246 break;
Matthew Wilcox1e9dbc92008-06-19 13:13:38 -06003247 case 0xb1:
3248 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_b1);
3249 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 default:
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003251 ata_scsi_invalid_field(cmd, done);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252 break;
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003253 }
3254 break;
3255
3256 case MODE_SENSE:
3257 case MODE_SENSE_10:
3258 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
3259 break;
3260
3261 case MODE_SELECT: /* unconditionally return */
3262 case MODE_SELECT_10: /* bad-field-in-cdb */
3263 ata_scsi_invalid_field(cmd, done);
3264 break;
3265
3266 case READ_CAPACITY:
3267 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
3268 break;
3269
3270 case SERVICE_ACTION_IN:
3271 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
3272 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
3273 else
3274 ata_scsi_invalid_field(cmd, done);
3275 break;
3276
3277 case REPORT_LUNS:
3278 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
3279 break;
3280
3281 case REQUEST_SENSE:
3282 ata_scsi_set_sense(cmd, 0, 0, 0);
3283 cmd->result = (DRIVER_SENSE << 24);
3284 done(cmd);
3285 break;
3286
3287 /* if we reach this, then writeback caching is disabled,
3288 * turning this into a no-op.
3289 */
3290 case SYNCHRONIZE_CACHE:
3291 /* fall through */
3292
3293 /* no-op's, complete with success */
3294 case REZERO_UNIT:
3295 case SEEK_6:
3296 case SEEK_10:
3297 case TEST_UNIT_READY:
3298 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
3299 break;
3300
3301 case SEND_DIAGNOSTIC:
3302 tmp8 = scsicmd[1] & ~(1 << 3);
3303 if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4]))
3304 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
3305 else
3306 ata_scsi_invalid_field(cmd, done);
3307 break;
3308
3309 /* all other commands */
3310 default:
3311 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
3312 /* "Invalid command operation code" */
3313 done(cmd);
3314 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 }
3316}
3317
Tejun Heof3187192007-04-17 23:44:07 +09003318int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
3319{
3320 int i, rc;
3321
3322 for (i = 0; i < host->n_ports; i++) {
3323 struct ata_port *ap = host->ports[i];
3324 struct Scsi_Host *shost;
3325
3326 rc = -ENOMEM;
3327 shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
3328 if (!shost)
3329 goto err_alloc;
3330
3331 *(struct ata_port **)&shost->hostdata[0] = ap;
3332 ap->scsi_host = shost;
3333
Gwendal Grignoud9027472010-05-25 12:31:38 -07003334 shost->transportt = ata_scsi_transport_template;
Tejun Heof3187192007-04-17 23:44:07 +09003335 shost->unique_id = ap->print_id;
3336 shost->max_id = 16;
3337 shost->max_lun = 1;
3338 shost->max_channel = 1;
3339 shost->max_cmd_len = 16;
3340
Tejun Heo31cc23b2007-09-23 13:14:12 +09003341 /* Schedule policy is determined by ->qc_defer()
3342 * callback and it needs to see every deferred qc.
3343 * Set host_blocked to 1 to prevent SCSI midlayer from
3344 * automatically deferring requests.
3345 */
3346 shost->max_host_blocked = 1;
3347
Tejun Heof3187192007-04-17 23:44:07 +09003348 rc = scsi_add_host(ap->scsi_host, ap->host->dev);
3349 if (rc)
3350 goto err_add;
3351 }
3352
3353 return 0;
3354
3355 err_add:
3356 scsi_host_put(host->ports[i]->scsi_host);
3357 err_alloc:
3358 while (--i >= 0) {
3359 struct Scsi_Host *shost = host->ports[i]->scsi_host;
3360
3361 scsi_remove_host(shost);
3362 scsi_host_put(shost);
3363 }
3364 return rc;
3365}
3366
Tejun Heo1ae46312007-07-16 14:29:40 +09003367void ata_scsi_scan_host(struct ata_port *ap, int sync)
Jeff Garzik644dd0c2005-10-03 15:55:19 -04003368{
Tejun Heo1ae46312007-07-16 14:29:40 +09003369 int tries = 5;
3370 struct ata_device *last_failed_dev = NULL;
Tejun Heo41bda9c2007-08-06 18:36:24 +09003371 struct ata_link *link;
Tejun Heo1ae46312007-07-16 14:29:40 +09003372 struct ata_device *dev;
Jeff Garzik644dd0c2005-10-03 15:55:19 -04003373
Tejun Heo1ae46312007-07-16 14:29:40 +09003374 repeat:
Tejun Heo1eca4362008-11-03 20:03:17 +09003375 ata_for_each_link(link, ap, EDGE) {
3376 ata_for_each_dev(dev, link, ENABLED) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003377 struct scsi_device *sdev;
3378 int channel = 0, id = 0;
Jeff Garzik3f19ee82005-10-03 21:36:41 -04003379
Tejun Heo1eca4362008-11-03 20:03:17 +09003380 if (dev->sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09003381 continue;
Tejun Heo3edebac2006-05-31 18:27:40 +09003382
Tejun Heo41bda9c2007-08-06 18:36:24 +09003383 if (ata_is_host_link(link))
3384 id = dev->devno;
3385 else
3386 channel = link->pmp;
3387
3388 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
3389 NULL);
3390 if (!IS_ERR(sdev)) {
3391 dev->sdev = sdev;
3392 scsi_device_put(sdev);
Grant Grundler295124d2010-08-17 10:56:53 -07003393 } else {
3394 dev->sdev = NULL;
Tejun Heo41bda9c2007-08-06 18:36:24 +09003395 }
Tejun Heo3edebac2006-05-31 18:27:40 +09003396 }
Jeff Garzik3f19ee82005-10-03 21:36:41 -04003397 }
Tejun Heo1ae46312007-07-16 14:29:40 +09003398
3399 /* If we scanned while EH was in progress or allocation
3400 * failure occurred, scan would have failed silently. Check
3401 * whether all devices are attached.
3402 */
Tejun Heo1eca4362008-11-03 20:03:17 +09003403 ata_for_each_link(link, ap, EDGE) {
3404 ata_for_each_dev(dev, link, ENABLED) {
3405 if (!dev->sdev)
Tejun Heo41bda9c2007-08-06 18:36:24 +09003406 goto exit_loop;
3407 }
Tejun Heo1ae46312007-07-16 14:29:40 +09003408 }
Tejun Heo41bda9c2007-08-06 18:36:24 +09003409 exit_loop:
3410 if (!link)
Tejun Heo1ae46312007-07-16 14:29:40 +09003411 return;
3412
3413 /* we're missing some SCSI devices */
3414 if (sync) {
3415 /* If caller requested synchrnous scan && we've made
3416 * any progress, sleep briefly and repeat.
3417 */
3418 if (dev != last_failed_dev) {
3419 msleep(100);
3420 last_failed_dev = dev;
3421 goto repeat;
3422 }
3423
3424 /* We might be failing to detect boot device, give it
3425 * a few more chances.
3426 */
3427 if (--tries) {
3428 msleep(100);
3429 goto repeat;
3430 }
3431
3432 ata_port_printk(ap, KERN_ERR, "WARNING: synchronous SCSI scan "
3433 "failed without making any progress,\n"
3434 " switching to async\n");
3435 }
3436
Tejun Heoad72cf92010-07-02 10:03:52 +02003437 queue_delayed_work(system_long_wq, &ap->hotplug_task,
Tejun Heo1ae46312007-07-16 14:29:40 +09003438 round_jiffies_relative(HZ));
Jeff Garzik644dd0c2005-10-03 15:55:19 -04003439}
Tejun Heo0ea035a2006-05-31 18:28:01 +09003440
3441/**
3442 * ata_scsi_offline_dev - offline attached SCSI device
3443 * @dev: ATA device to offline attached SCSI device for
3444 *
3445 * This function is called from ata_eh_hotplug() and responsible
3446 * for taking the SCSI device attached to @dev offline. This
Jeff Garzikcca39742006-08-24 03:19:22 -04003447 * function is called with host lock which protects dev->sdev
Tejun Heo0ea035a2006-05-31 18:28:01 +09003448 * against clearing.
3449 *
3450 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04003451 * spin_lock_irqsave(host lock)
Tejun Heo0ea035a2006-05-31 18:28:01 +09003452 *
3453 * RETURNS:
3454 * 1 if attached SCSI device exists, 0 otherwise.
3455 */
3456int ata_scsi_offline_dev(struct ata_device *dev)
3457{
3458 if (dev->sdev) {
3459 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
3460 return 1;
3461 }
3462 return 0;
3463}
Tejun Heo580b2102006-05-31 18:28:05 +09003464
3465/**
3466 * ata_scsi_remove_dev - remove attached SCSI device
3467 * @dev: ATA device to remove attached SCSI device for
3468 *
3469 * This function is called from ata_eh_scsi_hotplug() and
3470 * responsible for removing the SCSI device attached to @dev.
3471 *
3472 * LOCKING:
3473 * Kernel thread context (may sleep).
3474 */
3475static void ata_scsi_remove_dev(struct ata_device *dev)
3476{
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003477 struct ata_port *ap = dev->link->ap;
Tejun Heo580b2102006-05-31 18:28:05 +09003478 struct scsi_device *sdev;
3479 unsigned long flags;
3480
3481 /* Alas, we need to grab scan_mutex to ensure SCSI device
3482 * state doesn't change underneath us and thus
3483 * scsi_device_get() always succeeds. The mutex locking can
3484 * be removed if there is __scsi_device_get() interface which
3485 * increments reference counts regardless of device state.
3486 */
Jeff Garzikcca39742006-08-24 03:19:22 -04003487 mutex_lock(&ap->scsi_host->scan_mutex);
Jeff Garzikba6a1302006-06-22 23:46:10 -04003488 spin_lock_irqsave(ap->lock, flags);
Tejun Heo580b2102006-05-31 18:28:05 +09003489
Jeff Garzikcca39742006-08-24 03:19:22 -04003490 /* clearing dev->sdev is protected by host lock */
Tejun Heo580b2102006-05-31 18:28:05 +09003491 sdev = dev->sdev;
3492 dev->sdev = NULL;
3493
3494 if (sdev) {
3495 /* If user initiated unplug races with us, sdev can go
Jeff Garzikcca39742006-08-24 03:19:22 -04003496 * away underneath us after the host lock and
Tejun Heo580b2102006-05-31 18:28:05 +09003497 * scan_mutex are released. Hold onto it.
3498 */
3499 if (scsi_device_get(sdev) == 0) {
3500 /* The following ensures the attached sdev is
3501 * offline on return from ata_scsi_offline_dev()
3502 * regardless it wins or loses the race
3503 * against this function.
3504 */
3505 scsi_device_set_state(sdev, SDEV_OFFLINE);
3506 } else {
3507 WARN_ON(1);
3508 sdev = NULL;
3509 }
3510 }
3511
Jeff Garzikba6a1302006-06-22 23:46:10 -04003512 spin_unlock_irqrestore(ap->lock, flags);
Jeff Garzikcca39742006-08-24 03:19:22 -04003513 mutex_unlock(&ap->scsi_host->scan_mutex);
Tejun Heo580b2102006-05-31 18:28:05 +09003514
3515 if (sdev) {
3516 ata_dev_printk(dev, KERN_INFO, "detaching (SCSI %s)\n",
Kay Sieversb9d5fc42009-01-06 10:44:43 -08003517 dev_name(&sdev->sdev_gendev));
Tejun Heo580b2102006-05-31 18:28:05 +09003518
3519 scsi_remove_device(sdev);
3520 scsi_device_put(sdev);
3521 }
3522}
3523
Tejun Heo41bda9c2007-08-06 18:36:24 +09003524static void ata_scsi_handle_link_detach(struct ata_link *link)
3525{
3526 struct ata_port *ap = link->ap;
3527 struct ata_device *dev;
3528
Tejun Heo1eca4362008-11-03 20:03:17 +09003529 ata_for_each_dev(dev, link, ALL) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003530 unsigned long flags;
3531
3532 if (!(dev->flags & ATA_DFLAG_DETACHED))
3533 continue;
3534
3535 spin_lock_irqsave(ap->lock, flags);
3536 dev->flags &= ~ATA_DFLAG_DETACHED;
3537 spin_unlock_irqrestore(ap->lock, flags);
3538
3539 ata_scsi_remove_dev(dev);
3540 }
3541}
3542
Tejun Heo580b2102006-05-31 18:28:05 +09003543/**
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003544 * ata_scsi_media_change_notify - send media change event
Randy Dunlapc5d0e6a2007-10-15 17:29:46 -07003545 * @dev: Pointer to the disk device with media change event
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003546 *
3547 * Tell the block layer to send a media change notification
3548 * event.
3549 *
3550 * LOCKING:
Tejun Heo854c73a2007-09-23 13:14:11 +09003551 * spin_lock_irqsave(host lock)
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003552 */
Tejun Heo854c73a2007-09-23 13:14:11 +09003553void ata_scsi_media_change_notify(struct ata_device *dev)
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003554{
Tejun Heo854c73a2007-09-23 13:14:11 +09003555 if (dev->sdev)
Jeff Garzikf26792d2007-10-29 17:18:39 -04003556 sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
3557 GFP_ATOMIC);
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003558}
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04003559
3560/**
Tejun Heo580b2102006-05-31 18:28:05 +09003561 * ata_scsi_hotplug - SCSI part of hotplug
David Howells65f27f32006-11-22 14:55:48 +00003562 * @work: Pointer to ATA port to perform SCSI hotplug on
Tejun Heo580b2102006-05-31 18:28:05 +09003563 *
3564 * Perform SCSI part of hotplug. It's executed from a separate
3565 * workqueue after EH completes. This is necessary because SCSI
3566 * hot plugging requires working EH and hot unplugging is
3567 * synchronized with hot plugging with a mutex.
3568 *
3569 * LOCKING:
3570 * Kernel thread context (may sleep).
3571 */
David Howells65f27f32006-11-22 14:55:48 +00003572void ata_scsi_hotplug(struct work_struct *work)
Tejun Heo580b2102006-05-31 18:28:05 +09003573{
David Howells65f27f32006-11-22 14:55:48 +00003574 struct ata_port *ap =
3575 container_of(work, struct ata_port, hotplug_task.work);
Tejun Heo41bda9c2007-08-06 18:36:24 +09003576 int i;
Tejun Heo580b2102006-05-31 18:28:05 +09003577
Tejun Heob51e9e52006-06-29 01:29:30 +09003578 if (ap->pflags & ATA_PFLAG_UNLOADING) {
Tejun Heo580b2102006-05-31 18:28:05 +09003579 DPRINTK("ENTER/EXIT - unloading\n");
3580 return;
3581 }
3582
3583 DPRINTK("ENTER\n");
Tejun Heoad72cf92010-07-02 10:03:52 +02003584 mutex_lock(&ap->scsi_scan_mutex);
Tejun Heo580b2102006-05-31 18:28:05 +09003585
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
Tejun Heoad72cf92010-07-02 10:03:52 +02003598 mutex_unlock(&ap->scsi_scan_mutex);
Tejun Heo580b2102006-05-31 18:28:05 +09003599 DPRINTK("EXIT\n");
3600}
Tejun Heo83c47bc2006-05-31 18:28:07 +09003601
3602/**
3603 * ata_scsi_user_scan - indication for user-initiated bus scan
3604 * @shost: SCSI host to scan
3605 * @channel: Channel to scan
3606 * @id: ID to scan
3607 * @lun: LUN to scan
3608 *
3609 * This function is called when user explicitly requests bus
3610 * scan. Set probe pending flag and invoke EH.
3611 *
3612 * LOCKING:
3613 * SCSI layer (we don't care)
3614 *
3615 * RETURNS:
3616 * Zero.
3617 */
Gwendal Grignoud9027472010-05-25 12:31:38 -07003618int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
3619 unsigned int id, unsigned int lun)
Tejun Heo83c47bc2006-05-31 18:28:07 +09003620{
3621 struct ata_port *ap = ata_shost_to_port(shost);
3622 unsigned long flags;
Tejun Heo41bda9c2007-08-06 18:36:24 +09003623 int devno, rc = 0;
Tejun Heo83c47bc2006-05-31 18:28:07 +09003624
3625 if (!ap->ops->error_handler)
3626 return -EOPNOTSUPP;
3627
Tejun Heo41bda9c2007-08-06 18:36:24 +09003628 if (lun != SCAN_WILD_CARD && lun)
Tejun Heo83c47bc2006-05-31 18:28:07 +09003629 return -EINVAL;
3630
Tejun Heo071f44b2008-04-07 22:47:22 +09003631 if (!sata_pmp_attached(ap)) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003632 if (channel != SCAN_WILD_CARD && channel)
3633 return -EINVAL;
3634 devno = id;
3635 } else {
3636 if (id != SCAN_WILD_CARD && id)
3637 return -EINVAL;
3638 devno = channel;
3639 }
3640
Jeff Garzikba6a1302006-06-22 23:46:10 -04003641 spin_lock_irqsave(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09003642
Tejun Heo41bda9c2007-08-06 18:36:24 +09003643 if (devno == SCAN_WILD_CARD) {
3644 struct ata_link *link;
3645
Tejun Heo1eca4362008-11-03 20:03:17 +09003646 ata_for_each_link(link, ap, EDGE) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003647 struct ata_eh_info *ehi = &link->eh_info;
Tejun Heob558edd2008-01-24 00:05:14 +09003648 ehi->probe_mask |= ATA_ALL_DEVICES;
Tejun Heocf480622008-01-24 00:05:14 +09003649 ehi->action |= ATA_EH_RESET;
Tejun Heo41bda9c2007-08-06 18:36:24 +09003650 }
Tejun Heo83c47bc2006-05-31 18:28:07 +09003651 } else {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003652 struct ata_device *dev = ata_find_dev(ap, devno);
Tejun Heo83c47bc2006-05-31 18:28:07 +09003653
3654 if (dev) {
Tejun Heo41bda9c2007-08-06 18:36:24 +09003655 struct ata_eh_info *ehi = &dev->link->eh_info;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003656 ehi->probe_mask |= 1 << dev->devno;
Tejun Heocf480622008-01-24 00:05:14 +09003657 ehi->action |= ATA_EH_RESET;
Tejun Heo83c47bc2006-05-31 18:28:07 +09003658 } else
3659 rc = -EINVAL;
3660 }
3661
Tejun Heo309afcb2006-09-30 18:07:17 +09003662 if (rc == 0) {
Tejun Heo83c47bc2006-05-31 18:28:07 +09003663 ata_port_schedule_eh(ap);
Tejun Heo309afcb2006-09-30 18:07:17 +09003664 spin_unlock_irqrestore(ap->lock, flags);
3665 ata_port_wait_eh(ap);
3666 } else
3667 spin_unlock_irqrestore(ap->lock, flags);
Tejun Heo83c47bc2006-05-31 18:28:07 +09003668
3669 return rc;
3670}
zhao, forrest3057ac32006-06-12 12:01:34 +08003671
3672/**
Tejun Heod0171262006-06-12 22:51:14 +09003673 * ata_scsi_dev_rescan - initiate scsi_rescan_device()
David Howells65f27f32006-11-22 14:55:48 +00003674 * @work: Pointer to ATA port to perform scsi_rescan_device()
zhao, forrest3057ac32006-06-12 12:01:34 +08003675 *
Tejun Heod0171262006-06-12 22:51:14 +09003676 * After ATA pass thru (SAT) commands are executed successfully,
Tejun Heoad72cf92010-07-02 10:03:52 +02003677 * libata need to propagate the changes to SCSI layer.
zhao, forrest3057ac32006-06-12 12:01:34 +08003678 *
Tejun Heod0171262006-06-12 22:51:14 +09003679 * LOCKING:
3680 * Kernel thread context (may sleep).
zhao, forrest3057ac32006-06-12 12:01:34 +08003681 */
David Howells65f27f32006-11-22 14:55:48 +00003682void ata_scsi_dev_rescan(struct work_struct *work)
zhao, forrest3057ac32006-06-12 12:01:34 +08003683{
David Howells65f27f32006-11-22 14:55:48 +00003684 struct ata_port *ap =
3685 container_of(work, struct ata_port, scsi_rescan_task);
Tejun Heo41bda9c2007-08-06 18:36:24 +09003686 struct ata_link *link;
Tejun Heof58229f2007-08-06 18:36:23 +09003687 struct ata_device *dev;
Tejun Heof84e7e42006-11-22 11:21:31 +09003688 unsigned long flags;
zhao, forrest3057ac32006-06-12 12:01:34 +08003689
Tejun Heoad72cf92010-07-02 10:03:52 +02003690 mutex_lock(&ap->scsi_scan_mutex);
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);
Tejun Heoad72cf92010-07-02 10:03:52 +02003710 mutex_unlock(&ap->scsi_scan_mutex);
zhao, forrest3057ac32006-06-12 12:01:34 +08003711}
Brian King80289162006-08-07 14:27:31 -05003712
3713/**
3714 * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
Jeff Garzik4f931372006-09-29 05:07:25 -04003715 * @host: ATA host container for all SAS ports
Brian King80289162006-08-07 14:27:31 -05003716 * @port_info: Information from low-level host driver
Jeff Garzikcca39742006-08-24 03:19:22 -04003717 * @shost: SCSI host that the scsi device is attached to
Brian King80289162006-08-07 14:27:31 -05003718 *
3719 * LOCKING:
3720 * PCI/etc. bus probe sem.
3721 *
3722 * RETURNS:
3723 * ata_port pointer on success / NULL on failure.
3724 */
3725
Jeff Garzikcca39742006-08-24 03:19:22 -04003726struct ata_port *ata_sas_port_alloc(struct ata_host *host,
Brian King80289162006-08-07 14:27:31 -05003727 struct ata_port_info *port_info,
Jeff Garzikcca39742006-08-24 03:19:22 -04003728 struct Scsi_Host *shost)
Brian King80289162006-08-07 14:27:31 -05003729{
Tejun Heof3187192007-04-17 23:44:07 +09003730 struct ata_port *ap;
Brian King80289162006-08-07 14:27:31 -05003731
Tejun Heof3187192007-04-17 23:44:07 +09003732 ap = ata_port_alloc(host);
Brian King80289162006-08-07 14:27:31 -05003733 if (!ap)
3734 return NULL;
3735
Tejun Heof3187192007-04-17 23:44:07 +09003736 ap->port_no = 0;
Jeff Garzikcca39742006-08-24 03:19:22 -04003737 ap->lock = shost->host_lock;
Tejun Heof3187192007-04-17 23:44:07 +09003738 ap->pio_mask = port_info->pio_mask;
3739 ap->mwdma_mask = port_info->mwdma_mask;
3740 ap->udma_mask = port_info->udma_mask;
3741 ap->flags |= port_info->flags;
3742 ap->ops = port_info->port_ops;
3743 ap->cbl = ATA_CBL_SATA;
3744
Brian King80289162006-08-07 14:27:31 -05003745 return ap;
3746}
3747EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
3748
3749/**
3750 * ata_sas_port_start - Set port up for dma.
3751 * @ap: Port to initialize
3752 *
3753 * Called just after data structures for each port are
James Bottomleydde20202008-02-19 11:36:56 +01003754 * initialized.
Brian King80289162006-08-07 14:27:31 -05003755 *
3756 * May be used as the port_start() entry in ata_port_operations.
3757 *
3758 * LOCKING:
3759 * Inherited from caller.
3760 */
3761int ata_sas_port_start(struct ata_port *ap)
3762{
James Bottomleydde20202008-02-19 11:36:56 +01003763 return 0;
Brian King80289162006-08-07 14:27:31 -05003764}
3765EXPORT_SYMBOL_GPL(ata_sas_port_start);
3766
3767/**
3768 * ata_port_stop - Undo ata_sas_port_start()
3769 * @ap: Port to shut down
3770 *
Brian King80289162006-08-07 14:27:31 -05003771 * May be used as the port_stop() entry in ata_port_operations.
3772 *
3773 * LOCKING:
3774 * Inherited from caller.
3775 */
3776
3777void ata_sas_port_stop(struct ata_port *ap)
3778{
Brian King80289162006-08-07 14:27:31 -05003779}
3780EXPORT_SYMBOL_GPL(ata_sas_port_stop);
3781
3782/**
3783 * ata_sas_port_init - Initialize a SATA device
3784 * @ap: SATA port to initialize
3785 *
3786 * LOCKING:
3787 * PCI/etc. bus probe sem.
3788 *
3789 * RETURNS:
3790 * Zero on success, non-zero on error.
3791 */
3792
3793int ata_sas_port_init(struct ata_port *ap)
3794{
3795 int rc = ap->ops->port_start(ap);
3796
Tejun Heof3187192007-04-17 23:44:07 +09003797 if (!rc) {
3798 ap->print_id = ata_print_id++;
Brian King80289162006-08-07 14:27:31 -05003799 rc = ata_bus_probe(ap);
Tejun Heof3187192007-04-17 23:44:07 +09003800 }
Brian King80289162006-08-07 14:27:31 -05003801
3802 return rc;
3803}
3804EXPORT_SYMBOL_GPL(ata_sas_port_init);
3805
3806/**
3807 * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
3808 * @ap: SATA port to destroy
3809 *
3810 */
3811
3812void ata_sas_port_destroy(struct ata_port *ap)
3813{
Tejun Heof0d36ef2007-01-20 16:00:28 +09003814 if (ap->ops->port_stop)
3815 ap->ops->port_stop(ap);
Brian King80289162006-08-07 14:27:31 -05003816 kfree(ap);
3817}
3818EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
3819
3820/**
3821 * ata_sas_slave_configure - Default slave_config routine for libata devices
3822 * @sdev: SCSI device to configure
3823 * @ap: ATA port to which SCSI device is attached
3824 *
3825 * RETURNS:
3826 * Zero.
3827 */
3828
3829int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
3830{
3831 ata_scsi_sdev_config(sdev);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003832 ata_scsi_dev_config(sdev, ap->link.device);
Brian King80289162006-08-07 14:27:31 -05003833 return 0;
3834}
3835EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
3836
3837/**
3838 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
3839 * @cmd: SCSI command to be sent
3840 * @done: Completion function, called when command is complete
3841 * @ap: ATA port to which the command is being sent
3842 *
3843 * RETURNS:
Brian King08475a12006-11-20 13:51:56 -06003844 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
3845 * 0 otherwise.
Brian King80289162006-08-07 14:27:31 -05003846 */
3847
3848int ata_sas_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *),
3849 struct ata_port *ap)
3850{
Brian King08475a12006-11-20 13:51:56 -06003851 int rc = 0;
3852
Brian King80289162006-08-07 14:27:31 -05003853 ata_scsi_dump_cdb(ap, cmd);
3854
Tejun Heo2486fa52008-07-31 07:52:40 +09003855 if (likely(ata_dev_enabled(ap->link.device)))
Tejun Heo9af5c9c2007-08-06 18:36:22 +09003856 rc = __ata_scsi_queuecmd(cmd, done, ap->link.device);
Brian King80289162006-08-07 14:27:31 -05003857 else {
3858 cmd->result = (DID_BAD_TARGET << 16);
3859 done(cmd);
3860 }
Brian King08475a12006-11-20 13:51:56 -06003861 return rc;
Brian King80289162006-08-07 14:27:31 -05003862}
3863EXPORT_SYMBOL_GPL(ata_sas_queuecmd);