[MIPS] ATA: Rename routerboard 500 to 532
[deliverable/linux.git] / drivers / ata / libata-scsi.c
CommitLineData
1da177e4 1/*
af36d7f0
JG
2 * 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 *
1da177e4
LT
34 */
35
36#include <linux/kernel.h>
37#include <linux/blkdev.h>
38#include <linux/spinlock.h>
39#include <scsi/scsi.h>
1da177e4 40#include <scsi/scsi_host.h>
beb40487 41#include <scsi/scsi_cmnd.h>
85837ebd 42#include <scsi/scsi_eh.h>
005a5a06 43#include <scsi/scsi_device.h>
a6e6ce8e 44#include <scsi/scsi_tcq.h>
30afc84c 45#include <scsi/scsi_transport.h>
1da177e4 46#include <linux/libata.h>
b095518e 47#include <linux/hdreg.h>
2dcb407e 48#include <linux/uaccess.h>
1da177e4
LT
49
50#include "libata.h"
51
b095518e
JG
52#define SECTOR_SIZE 512
53
ad706991 54typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc);
ab5b3a5b 55
2dcb407e 56static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
ab5b3a5b 57 const struct scsi_device *scsidev);
2dcb407e 58static struct ata_device *ata_scsi_find_dev(struct ata_port *ap,
ab5b3a5b 59 const struct scsi_device *scsidev);
83c47bcb
TH
60static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
61 unsigned int id, unsigned int lun);
ab5b3a5b 62
1da177e4 63
00ac37f5
DG
64#define RW_RECOVERY_MPAGE 0x1
65#define RW_RECOVERY_MPAGE_LEN 12
66#define CACHE_MPAGE 0x8
67#define CACHE_MPAGE_LEN 20
68#define CONTROL_MPAGE 0xa
69#define CONTROL_MPAGE_LEN 12
70#define ALL_MPAGES 0x3f
71#define ALL_SUB_MPAGES 0xff
72
73
24f75686 74static const u8 def_rw_recovery_mpage[RW_RECOVERY_MPAGE_LEN] = {
00ac37f5
DG
75 RW_RECOVERY_MPAGE,
76 RW_RECOVERY_MPAGE_LEN - 2,
24f75686 77 (1 << 7), /* AWRE */
00ac37f5
DG
78 0, /* read retry count */
79 0, 0, 0, 0,
80 0, /* write retry count */
81 0, 0, 0
82};
83
84static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = {
85 CACHE_MPAGE,
86 CACHE_MPAGE_LEN - 2,
87 0, /* contains WCE, needs to be 0 for logic */
88 0, 0, 0, 0, 0, 0, 0, 0, 0,
89 0, /* contains DRA, needs to be 0 for logic */
90 0, 0, 0, 0, 0, 0, 0
91};
92
93static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = {
94 CONTROL_MPAGE,
95 CONTROL_MPAGE_LEN - 2,
96 2, /* DSENSE=0, GLTSD=1 */
97 0, /* [QAM+QERR may be 1, see 05-359r1] */
98 0, 0, 0, 0, 0xff, 0xff,
99 0, 30 /* extended self test time, see 05-359r1 */
100};
101
30afc84c
TH
102/*
103 * libata transport template. libata doesn't do real transport stuff.
104 * It just needs the eh_timed_out hook.
105 */
f3187195 106static struct scsi_transport_template ata_scsi_transport_template = {
9227c33d 107 .eh_strategy_handler = ata_scsi_error,
30afc84c 108 .eh_timed_out = ata_scsi_timed_out,
ccf68c34 109 .user_scan = ata_scsi_user_scan,
30afc84c
TH
110};
111
1da177e4 112
ca77329f
KCA
113static const struct {
114 enum link_pm value;
115 const char *name;
116} link_pm_policy[] = {
117 { NOT_AVAILABLE, "max_performance" },
118 { MIN_POWER, "min_power" },
119 { MAX_PERFORMANCE, "max_performance" },
120 { MEDIUM_POWER, "medium_power" },
121};
122
a2d6ed14 123static const char *ata_scsi_lpm_get(enum link_pm policy)
ca77329f
KCA
124{
125 int i;
126
127 for (i = 0; i < ARRAY_SIZE(link_pm_policy); i++)
128 if (link_pm_policy[i].value == policy)
129 return link_pm_policy[i].name;
130
131 return NULL;
132}
133
ee959b00
TJ
134static ssize_t ata_scsi_lpm_put(struct device *dev,
135 struct device_attribute *attr,
136 const char *buf, size_t count)
ca77329f 137{
ee959b00 138 struct Scsi_Host *shost = class_to_shost(dev);
ca77329f
KCA
139 struct ata_port *ap = ata_shost_to_port(shost);
140 enum link_pm policy = 0;
141 int i;
142
143 /*
144 * we are skipping array location 0 on purpose - this
145 * is because a value of NOT_AVAILABLE is displayed
146 * to the user as max_performance, but when the user
147 * writes "max_performance", they actually want the
148 * value to match MAX_PERFORMANCE.
149 */
150 for (i = 1; i < ARRAY_SIZE(link_pm_policy); i++) {
151 const int len = strlen(link_pm_policy[i].name);
152 if (strncmp(link_pm_policy[i].name, buf, len) == 0 &&
153 buf[len] == '\n') {
154 policy = link_pm_policy[i].value;
155 break;
156 }
157 }
158 if (!policy)
159 return -EINVAL;
160
161 ata_lpm_schedule(ap, policy);
162 return count;
163}
164
165static ssize_t
ee959b00 166ata_scsi_lpm_show(struct device *dev, struct device_attribute *attr, char *buf)
ca77329f 167{
ee959b00 168 struct Scsi_Host *shost = class_to_shost(dev);
ca77329f
KCA
169 struct ata_port *ap = ata_shost_to_port(shost);
170 const char *policy =
171 ata_scsi_lpm_get(ap->pm_policy);
172
173 if (!policy)
174 return -EINVAL;
175
176 return snprintf(buf, 23, "%s\n", policy);
177}
ee959b00 178DEVICE_ATTR(link_power_management_policy, S_IRUGO | S_IWUSR,
ca77329f 179 ata_scsi_lpm_show, ata_scsi_lpm_put);
ee959b00 180EXPORT_SYMBOL_GPL(dev_attr_link_power_management_policy);
ca77329f 181
ae006510
DG
182static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
183 void (*done)(struct scsi_cmnd *))
184{
185 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
186 /* "Invalid field in cbd" */
187 done(cmd);
188}
189
1da177e4
LT
190/**
191 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
192 * @sdev: SCSI device for which BIOS geometry is to be determined
193 * @bdev: block device associated with @sdev
194 * @capacity: capacity of SCSI device
195 * @geom: location to which geometry will be output
196 *
197 * Generic bios head/sector/cylinder calculator
198 * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS)
199 * mapping. Some situations may arise where the disk is not
200 * bootable if this is not used.
201 *
202 * LOCKING:
203 * Defined by the SCSI layer. We don't really care.
204 *
205 * RETURNS:
206 * Zero.
207 */
208int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev,
209 sector_t capacity, int geom[])
210{
211 geom[0] = 255;
212 geom[1] = 63;
213 sector_div(capacity, 255*63);
214 geom[2] = capacity;
215
216 return 0;
217}
218
5924b74c
TH
219/**
220 * ata_get_identity - Handler for HDIO_GET_IDENTITY ioctl
221 * @sdev: SCSI device to get identify data for
222 * @arg: User buffer area for identify data
223 *
224 * LOCKING:
225 * Defined by the SCSI layer. We don't really care.
226 *
227 * RETURNS:
228 * Zero on success, negative errno on error.
229 */
230static int ata_get_identity(struct scsi_device *sdev, void __user *arg)
231{
232 struct ata_port *ap = ata_shost_to_port(sdev->host);
233 struct ata_device *dev = ata_scsi_find_dev(ap, sdev);
234 u16 __user *dst = arg;
235 char buf[40];
236
237 if (!dev)
238 return -ENOMSG;
239
240 if (copy_to_user(dst, dev->id, ATA_ID_WORDS * sizeof(u16)))
241 return -EFAULT;
242
243 ata_id_string(dev->id, buf, ATA_ID_PROD, ATA_ID_PROD_LEN);
244 if (copy_to_user(dst + ATA_ID_PROD, buf, ATA_ID_PROD_LEN))
245 return -EFAULT;
246
247 ata_id_string(dev->id, buf, ATA_ID_FW_REV, ATA_ID_FW_REV_LEN);
248 if (copy_to_user(dst + ATA_ID_FW_REV, buf, ATA_ID_FW_REV_LEN))
249 return -EFAULT;
250
251 ata_id_string(dev->id, buf, ATA_ID_SERNO, ATA_ID_SERNO_LEN);
252 if (copy_to_user(dst + ATA_ID_SERNO, buf, ATA_ID_SERNO_LEN))
253 return -EFAULT;
254
255 return 0;
256}
257
b095518e
JG
258/**
259 * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl
8e8b77dd 260 * @scsidev: Device to which we are issuing command
b095518e
JG
261 * @arg: User provided data for issuing command
262 *
263 * LOCKING:
264 * Defined by the SCSI layer. We don't really care.
265 *
266 * RETURNS:
267 * Zero on success, negative errno on error.
268 */
b095518e
JG
269int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg)
270{
271 int rc = 0;
272 u8 scsi_cmd[MAX_COMMAND_SIZE];
bbe1fe7e 273 u8 args[4], *argbuf = NULL, *sensebuf = NULL;
b095518e 274 int argsize = 0;
85837ebd 275 enum dma_data_direction data_dir;
bbe1fe7e 276 int cmd_result;
b095518e 277
c893a3ae 278 if (arg == NULL)
b095518e
JG
279 return -EINVAL;
280
281 if (copy_from_user(args, arg, sizeof(args)))
282 return -EFAULT;
283
bbe1fe7e
ET
284 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
285 if (!sensebuf)
286 return -ENOMEM;
287
b095518e
JG
288 memset(scsi_cmd, 0, sizeof(scsi_cmd));
289
290 if (args[3]) {
291 argsize = SECTOR_SIZE * args[3];
292 argbuf = kmalloc(argsize, GFP_KERNEL);
54dac83c
JR
293 if (argbuf == NULL) {
294 rc = -ENOMEM;
295 goto error;
296 }
b095518e
JG
297
298 scsi_cmd[1] = (4 << 1); /* PIO Data-in */
299 scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev,
2dcb407e 300 block count in sector count field */
85837ebd 301 data_dir = DMA_FROM_DEVICE;
b095518e
JG
302 } else {
303 scsi_cmd[1] = (3 << 1); /* Non-data */
bbe1fe7e 304 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
85837ebd 305 data_dir = DMA_NONE;
b095518e
JG
306 }
307
308 scsi_cmd[0] = ATA_16;
309
310 scsi_cmd[4] = args[2];
311 if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */
312 scsi_cmd[6] = args[3];
313 scsi_cmd[8] = args[1];
314 scsi_cmd[10] = 0x4f;
315 scsi_cmd[12] = 0xc2;
316 } else {
317 scsi_cmd[6] = args[1];
318 }
319 scsi_cmd[14] = args[0];
320
321 /* Good values for timeout and retries? Values below
322 from scsi_ioctl_send_command() for default case... */
bbe1fe7e 323 cmd_result = scsi_execute(scsidev, scsi_cmd, data_dir, argbuf, argsize,
2dcb407e 324 sensebuf, (10*HZ), 5, 0);
bbe1fe7e
ET
325
326 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
327 u8 *desc = sensebuf + 8;
328 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
329
330 /* If we set cc then ATA pass-through will cause a
331 * check condition even if no error. Filter that. */
332 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
333 struct scsi_sense_hdr sshdr;
334 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
2dcb407e
JG
335 &sshdr);
336 if (sshdr.sense_key == 0 &&
337 sshdr.asc == 0 && sshdr.ascq == 0)
bbe1fe7e
ET
338 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
339 }
340
341 /* Send userspace a few ATA registers (same as drivers/ide) */
2dcb407e
JG
342 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
343 desc[0] == 0x09) { /* code is "ATA Descriptor" */
344 args[0] = desc[13]; /* status */
345 args[1] = desc[3]; /* error */
346 args[2] = desc[5]; /* sector count (0:7) */
bbe1fe7e
ET
347 if (copy_to_user(arg, args, sizeof(args)))
348 rc = -EFAULT;
349 }
350 }
351
352
353 if (cmd_result) {
b095518e
JG
354 rc = -EIO;
355 goto error;
356 }
357
b095518e 358 if ((argbuf)
c893a3ae 359 && copy_to_user(arg + sizeof(args), argbuf, argsize))
b095518e
JG
360 rc = -EFAULT;
361error:
bbe1fe7e 362 kfree(sensebuf);
8f760780 363 kfree(argbuf);
b095518e
JG
364 return rc;
365}
366
367/**
368 * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl
8e8b77dd 369 * @scsidev: Device to which we are issuing command
b095518e
JG
370 * @arg: User provided data for issuing command
371 *
372 * LOCKING:
373 * Defined by the SCSI layer. We don't really care.
374 *
375 * RETURNS:
376 * Zero on success, negative errno on error.
377 */
378int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg)
379{
380 int rc = 0;
381 u8 scsi_cmd[MAX_COMMAND_SIZE];
af068bd1
DM
382 u8 args[7], *sensebuf = NULL;
383 int cmd_result;
b095518e 384
c893a3ae 385 if (arg == NULL)
b095518e
JG
386 return -EINVAL;
387
388 if (copy_from_user(args, arg, sizeof(args)))
389 return -EFAULT;
390
af068bd1
DM
391 sensebuf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_NOIO);
392 if (!sensebuf)
393 return -ENOMEM;
394
b095518e
JG
395 memset(scsi_cmd, 0, sizeof(scsi_cmd));
396 scsi_cmd[0] = ATA_16;
397 scsi_cmd[1] = (3 << 1); /* Non-data */
af068bd1 398 scsi_cmd[2] = 0x20; /* cc but no off.line or data xfer */
b095518e
JG
399 scsi_cmd[4] = args[1];
400 scsi_cmd[6] = args[2];
401 scsi_cmd[8] = args[3];
402 scsi_cmd[10] = args[4];
403 scsi_cmd[12] = args[5];
277239f2 404 scsi_cmd[13] = args[6] & 0x4f;
b095518e
JG
405 scsi_cmd[14] = args[0];
406
b095518e 407 /* Good values for timeout and retries? Values below
2e9edbf8 408 from scsi_ioctl_send_command() for default case... */
af068bd1
DM
409 cmd_result = scsi_execute(scsidev, scsi_cmd, DMA_NONE, NULL, 0,
410 sensebuf, (10*HZ), 5, 0);
411
412 if (driver_byte(cmd_result) == DRIVER_SENSE) {/* sense data available */
413 u8 *desc = sensebuf + 8;
414 cmd_result &= ~(0xFF<<24); /* DRIVER_SENSE is not an error */
415
416 /* If we set cc then ATA pass-through will cause a
417 * check condition even if no error. Filter that. */
418 if (cmd_result & SAM_STAT_CHECK_CONDITION) {
419 struct scsi_sense_hdr sshdr;
420 scsi_normalize_sense(sensebuf, SCSI_SENSE_BUFFERSIZE,
421 &sshdr);
2dcb407e
JG
422 if (sshdr.sense_key == 0 &&
423 sshdr.asc == 0 && sshdr.ascq == 0)
af068bd1
DM
424 cmd_result &= ~SAM_STAT_CHECK_CONDITION;
425 }
426
427 /* Send userspace ATA registers */
428 if (sensebuf[0] == 0x72 && /* format is "descriptor" */
429 desc[0] == 0x09) {/* code is "ATA Descriptor" */
430 args[0] = desc[13]; /* status */
431 args[1] = desc[3]; /* error */
432 args[2] = desc[5]; /* sector count (0:7) */
433 args[3] = desc[7]; /* lbal */
434 args[4] = desc[9]; /* lbam */
435 args[5] = desc[11]; /* lbah */
436 args[6] = desc[12]; /* select */
437 if (copy_to_user(arg, args, sizeof(args)))
438 rc = -EFAULT;
439 }
440 }
441
442 if (cmd_result) {
b095518e 443 rc = -EIO;
af068bd1
DM
444 goto error;
445 }
b095518e 446
af068bd1
DM
447 error:
448 kfree(sensebuf);
b095518e
JG
449 return rc;
450}
451
1da177e4
LT
452int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg)
453{
1da177e4
LT
454 int val = -EINVAL, rc = -EINVAL;
455
1da177e4
LT
456 switch (cmd) {
457 case ATA_IOC_GET_IO32:
458 val = 0;
459 if (copy_to_user(arg, &val, 1))
460 return -EFAULT;
461 return 0;
462
463 case ATA_IOC_SET_IO32:
464 val = (unsigned long) arg;
465 if (val != 0)
466 return -EINVAL;
467 return 0;
468
5924b74c
TH
469 case HDIO_GET_IDENTITY:
470 return ata_get_identity(scsidev, arg);
471
b095518e
JG
472 case HDIO_DRIVE_CMD:
473 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
474 return -EACCES;
475 return ata_cmd_ioctl(scsidev, arg);
476
477 case HDIO_DRIVE_TASK:
478 if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
479 return -EACCES;
480 return ata_task_ioctl(scsidev, arg);
481
1da177e4
LT
482 default:
483 rc = -ENOTTY;
484 break;
485 }
486
1da177e4
LT
487 return rc;
488}
489
490/**
491 * ata_scsi_qc_new - acquire new ata_queued_cmd reference
1da177e4
LT
492 * @dev: ATA device to which the new command is attached
493 * @cmd: SCSI command that originated this ATA command
494 * @done: SCSI command completion function
495 *
496 * Obtain a reference to an unused ata_queued_cmd structure,
497 * which is the basic libata structure representing a single
498 * ATA command sent to the hardware.
499 *
500 * If a command was available, fill in the SCSI-specific
501 * portions of the structure with information on the
502 * current command.
503 *
504 * LOCKING:
cca3974e 505 * spin_lock_irqsave(host lock)
1da177e4
LT
506 *
507 * RETURNS:
508 * Command allocated, or %NULL if none available.
509 */
7102d230
AB
510static struct ata_queued_cmd *ata_scsi_qc_new(struct ata_device *dev,
511 struct scsi_cmnd *cmd,
512 void (*done)(struct scsi_cmnd *))
1da177e4
LT
513{
514 struct ata_queued_cmd *qc;
515
3373efd8 516 qc = ata_qc_new_init(dev);
1da177e4
LT
517 if (qc) {
518 qc->scsicmd = cmd;
519 qc->scsidone = done;
520
ff2aeb1e 521 qc->sg = scsi_sglist(cmd);
7120165c 522 qc->n_elem = scsi_sg_count(cmd);
1da177e4
LT
523 } else {
524 cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1);
525 done(cmd);
526 }
527
528 return qc;
529}
530
aacda375
TH
531static void ata_qc_set_pc_nbytes(struct ata_queued_cmd *qc)
532{
533 struct scsi_cmnd *scmd = qc->scsicmd;
534
535 qc->extrabytes = scmd->request->extra_len;
536 qc->nbytes = scsi_bufflen(scmd) + qc->extrabytes;
537}
538
b095518e
JG
539/**
540 * ata_dump_status - user friendly display of error info
541 * @id: id of the port in question
542 * @tf: ptr to filled out taskfile
543 *
544 * Decode and dump the ATA error/status registers for the user so
545 * that they have some idea what really happened at the non
546 * make-believe layer.
547 *
548 * LOCKING:
549 * inherited from caller
550 */
7102d230 551static void ata_dump_status(unsigned id, struct ata_taskfile *tf)
b095518e
JG
552{
553 u8 stat = tf->command, err = tf->feature;
554
555 printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat);
556 if (stat & ATA_BUSY) {
557 printk("Busy }\n"); /* Data is not valid in this case */
558 } else {
559 if (stat & 0x40) printk("DriveReady ");
560 if (stat & 0x20) printk("DeviceFault ");
561 if (stat & 0x10) printk("SeekComplete ");
562 if (stat & 0x08) printk("DataRequest ");
563 if (stat & 0x04) printk("CorrectedError ");
564 if (stat & 0x02) printk("Index ");
565 if (stat & 0x01) printk("Error ");
566 printk("}\n");
567
568 if (err) {
569 printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err);
570 if (err & 0x04) printk("DriveStatusError ");
571 if (err & 0x80) {
572 if (err & 0x04) printk("BadCRC ");
573 else printk("Sector ");
574 }
575 if (err & 0x40) printk("UncorrectableError ");
576 if (err & 0x10) printk("SectorIdNotFound ");
577 if (err & 0x02) printk("TrackZeroNotFound ");
578 if (err & 0x01) printk("AddrMarkNotFound ");
579 printk("}\n");
580 }
581 }
582}
583
1da177e4
LT
584/**
585 * ata_to_sense_error - convert ATA error to SCSI error
8e8b77dd 586 * @id: ATA device number
1da177e4 587 * @drv_stat: value contained in ATA status register
b095518e
JG
588 * @drv_err: value contained in ATA error register
589 * @sk: the sense key we'll fill out
590 * @asc: the additional sense code we'll fill out
591 * @ascq: the additional sense code qualifier we'll fill out
246619da 592 * @verbose: be verbose
1da177e4 593 *
b095518e
JG
594 * Converts an ATA error into a SCSI error. Fill out pointers to
595 * SK, ASC, and ASCQ bytes for later use in fixed or descriptor
596 * format sense blocks.
1da177e4
LT
597 *
598 * LOCKING:
cca3974e 599 * spin_lock_irqsave(host lock)
1da177e4 600 */
7102d230
AB
601static void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk,
602 u8 *asc, u8 *ascq, int verbose)
1da177e4 603{
b095518e 604 int i;
ffe75ef6 605
1da177e4 606 /* Based on the 3ware driver translation table */
98ac62de 607 static const unsigned char sense_table[][4] = {
1da177e4
LT
608 /* BBD|ECC|ID|MAR */
609 {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
610 /* BBD|ECC|ID */
611 {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command
612 /* ECC|MC|MARK */
613 {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error
614 /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */
615 {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error
616 /* MC|ID|ABRT|TRK0|MARK */
617 {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready
618 /* MCR|MARK */
619 {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready
620 /* Bad address mark */
621 {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field
622 /* TRK0 */
623 {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error
624 /* Abort & !ICRC */
625 {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command
626 /* Media change request */
627 {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline
628 /* SRV */
629 {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found
630 /* Media change */
631 {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline
632 /* ECC */
633 {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error
634 /* BBD - block marked bad */
635 {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error
636 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
637 };
98ac62de 638 static const unsigned char stat_table[][4] = {
1da177e4
LT
639 /* Must be first because BUSY means no other bits valid */
640 {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now
641 {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault
642 {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now
643 {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered
644 {0xFF, 0xFF, 0xFF, 0xFF}, // END mark
645 };
1da177e4 646
1da177e4
LT
647 /*
648 * Is this an error we can process/parse
649 */
b095518e
JG
650 if (drv_stat & ATA_BUSY) {
651 drv_err = 0; /* Ignore the err bits, they're invalid */
1da177e4 652 }
b095518e
JG
653
654 if (drv_err) {
655 /* Look for drv_err */
656 for (i = 0; sense_table[i][0] != 0xFF; i++) {
657 /* Look for best matches first */
2e9edbf8 658 if ((sense_table[i][0] & drv_err) ==
b095518e
JG
659 sense_table[i][0]) {
660 *sk = sense_table[i][1];
661 *asc = sense_table[i][2];
662 *ascq = sense_table[i][3];
663 goto translate_done;
664 }
665 }
666 /* No immediate match */
246619da
TH
667 if (verbose)
668 printk(KERN_WARNING "ata%u: no sense translation for "
669 "error 0x%02x\n", id, drv_err);
1da177e4 670 }
b095518e
JG
671
672 /* Fall back to interpreting status bits */
673 for (i = 0; stat_table[i][0] != 0xFF; i++) {
674 if (stat_table[i][0] & drv_stat) {
675 *sk = stat_table[i][1];
676 *asc = stat_table[i][2];
677 *ascq = stat_table[i][3];
678 goto translate_done;
1da177e4 679 }
b095518e
JG
680 }
681 /* No error? Undecoded? */
246619da
TH
682 if (verbose)
683 printk(KERN_WARNING "ata%u: no sense translation for "
684 "status: 0x%02x\n", id, drv_stat);
b095518e 685
2d202024
AC
686 /* We need a sensible error return here, which is tricky, and one
687 that won't cause people to do things like return a disk wrongly */
688 *sk = ABORTED_COMMAND;
689 *asc = 0x00;
690 *ascq = 0x00;
b095518e
JG
691
692 translate_done:
246619da
TH
693 if (verbose)
694 printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x "
695 "to SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n",
696 id, drv_stat, drv_err, *sk, *asc, *ascq);
b095518e
JG
697 return;
698}
699
700/*
750426aa 701 * ata_gen_passthru_sense - Generate check condition sense block.
b095518e
JG
702 * @qc: Command that completed.
703 *
704 * This function is specific to the ATA descriptor format sense
705 * block specified for the ATA pass through commands. Regardless
706 * of whether the command errored or not, return a sense
707 * block. Copy all controller registers into the sense
708 * block. Clear sense key, ASC & ASCQ if there is no error.
709 *
710 * LOCKING:
750426aa 711 * None.
b095518e 712 */
750426aa 713static void ata_gen_passthru_sense(struct ata_queued_cmd *qc)
b095518e
JG
714{
715 struct scsi_cmnd *cmd = qc->scsicmd;
e61e0672 716 struct ata_taskfile *tf = &qc->result_tf;
b095518e
JG
717 unsigned char *sb = cmd->sense_buffer;
718 unsigned char *desc = sb + 8;
246619da 719 int verbose = qc->ap->ops->error_handler == NULL;
b095518e
JG
720
721 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
722
0e5dec47 723 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
b095518e 724
b095518e
JG
725 /*
726 * Use ata_to_sense_error() to map status register bits
727 * onto sense key, asc & ascq.
728 */
058e55e1
TH
729 if (qc->err_mask ||
730 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
44877b4e 731 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
246619da 732 &sb[1], &sb[2], &sb[3], verbose);
b095518e 733 sb[1] &= 0x0f;
1da177e4
LT
734 }
735
b095518e
JG
736 /*
737 * Sense data is current and format is descriptor.
738 */
739 sb[0] = 0x72;
1da177e4 740
b095518e
JG
741 desc[0] = 0x09;
742
f38621b3
TH
743 /* set length of additional sense data */
744 sb[7] = 14;
745 desc[1] = 12;
b095518e
JG
746
747 /*
748 * Copy registers into sense buffer.
749 */
750 desc[2] = 0x00;
751 desc[3] = tf->feature; /* == error reg */
752 desc[5] = tf->nsect;
753 desc[7] = tf->lbal;
754 desc[9] = tf->lbam;
755 desc[11] = tf->lbah;
756 desc[12] = tf->device;
757 desc[13] = tf->command; /* == status reg */
758
759 /*
760 * Fill in Extend bit, and the high order bytes
761 * if applicable.
762 */
763 if (tf->flags & ATA_TFLAG_LBA48) {
764 desc[2] |= 0x01;
765 desc[4] = tf->hob_nsect;
766 desc[6] = tf->hob_lbal;
767 desc[8] = tf->hob_lbam;
768 desc[10] = tf->hob_lbah;
1da177e4 769 }
b095518e 770}
1da177e4 771
b095518e 772/**
750426aa 773 * ata_gen_ata_sense - generate a SCSI fixed sense block
b095518e
JG
774 * @qc: Command that we are erroring out
775 *
d25614ba
TH
776 * Generate sense block for a failed ATA command @qc. Descriptor
777 * format is used to accomodate LBA48 block address.
b095518e
JG
778 *
779 * LOCKING:
750426aa 780 * None.
b095518e 781 */
750426aa 782static void ata_gen_ata_sense(struct ata_queued_cmd *qc)
b095518e 783{
d25614ba 784 struct ata_device *dev = qc->dev;
b095518e 785 struct scsi_cmnd *cmd = qc->scsicmd;
e61e0672 786 struct ata_taskfile *tf = &qc->result_tf;
b095518e 787 unsigned char *sb = cmd->sense_buffer;
d25614ba 788 unsigned char *desc = sb + 8;
246619da 789 int verbose = qc->ap->ops->error_handler == NULL;
d25614ba 790 u64 block;
b095518e
JG
791
792 memset(sb, 0, SCSI_SENSE_BUFFERSIZE);
793
0e5dec47 794 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
b095518e 795
d25614ba
TH
796 /* sense data is current and format is descriptor */
797 sb[0] = 0x72;
798
799 /* Use ata_to_sense_error() to map status register bits
b095518e
JG
800 * onto sense key, asc & ascq.
801 */
058e55e1
TH
802 if (qc->err_mask ||
803 tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) {
44877b4e 804 ata_to_sense_error(qc->ap->print_id, tf->command, tf->feature,
d25614ba
TH
805 &sb[1], &sb[2], &sb[3], verbose);
806 sb[1] &= 0x0f;
1da177e4 807 }
1da177e4 808
d25614ba 809 block = ata_tf_read_block(&qc->result_tf, dev);
a7dac447 810
d25614ba
TH
811 /* information sense data descriptor */
812 sb[7] = 12;
813 desc[0] = 0x00;
814 desc[1] = 10;
a7dac447 815
d25614ba
TH
816 desc[2] |= 0x80; /* valid */
817 desc[6] = block >> 40;
818 desc[7] = block >> 32;
819 desc[8] = block >> 24;
820 desc[9] = block >> 16;
821 desc[10] = block >> 8;
822 desc[11] = block;
1da177e4
LT
823}
824
a6cce2a7
BK
825static void ata_scsi_sdev_config(struct scsi_device *sdev)
826{
827 sdev->use_10_for_rw = 1;
828 sdev->use_10_for_ms = 1;
31cc23b3
TH
829
830 /* Schedule policy is determined by ->qc_defer() callback and
831 * it needs to see every deferred qc. Set dev_blocked to 1 to
832 * prevent SCSI midlayer from automatically deferring
833 * requests.
834 */
835 sdev->max_device_blocked = 1;
a6cce2a7
BK
836}
837
fa2fc7f4
JB
838/**
839 * atapi_drain_needed - Check whether data transfer may overflow
73fd8b6d 840 * @rq: request to be checked
fa2fc7f4
JB
841 *
842 * ATAPI commands which transfer variable length data to host
843 * might overflow due to application error or hardare bug. This
844 * function checks whether overflow should be drained and ignored
845 * for @request.
846 *
847 * LOCKING:
848 * None.
849 *
850 * RETURNS:
851 * 1 if ; otherwise, 0.
852 */
853static int atapi_drain_needed(struct request *rq)
854{
855 if (likely(!blk_pc_request(rq)))
856 return 0;
857
858 if (!rq->data_len || (rq->cmd_flags & REQ_RW))
859 return 0;
860
861 return atapi_cmd_type(rq->cmd[0]) == ATAPI_MISC;
862}
863
864static int ata_scsi_dev_config(struct scsi_device *sdev,
865 struct ata_device *dev)
a6cce2a7 866{
914ed354
TH
867 /* configure max sectors */
868 blk_queue_max_sectors(sdev->request_queue, dev->max_sectors);
a6cce2a7 869
fa2fc7f4
JB
870 if (dev->class == ATA_DEV_ATAPI) {
871 struct request_queue *q = sdev->request_queue;
872 void *buf;
873
e3790c7d 874 /* set the min alignment and padding */
d0ad3bc9
JB
875 blk_queue_update_dma_alignment(sdev->request_queue,
876 ATA_DMA_PAD_SZ - 1);
e3790c7d 877 blk_queue_dma_pad(sdev->request_queue, ATA_DMA_PAD_SZ - 1);
fa2fc7f4
JB
878
879 /* configure draining */
880 buf = kmalloc(ATAPI_MAX_DRAIN, q->bounce_gfp | GFP_KERNEL);
881 if (!buf) {
882 ata_dev_printk(dev, KERN_ERR,
883 "drain buffer allocation failed\n");
884 return -ENOMEM;
885 }
886
887 blk_queue_dma_drain(q, atapi_drain_needed, buf, ATAPI_MAX_DRAIN);
888 } else {
d0ad3bc9
JB
889 /* ATA devices must be sector aligned */
890 blk_queue_update_dma_alignment(sdev->request_queue,
891 ATA_SECT_SIZE - 1);
d8cf5389 892 sdev->manage_start_stop = 1;
dde20207 893 }
d8cf5389 894
f26792d5
JG
895 if (dev->flags & ATA_DFLAG_AN)
896 set_bit(SDEV_EVT_MEDIA_CHANGE, sdev->supported_events);
897
a6e6ce8e
TH
898 if (dev->flags & ATA_DFLAG_NCQ) {
899 int depth;
900
901 depth = min(sdev->host->can_queue, ata_id_queue_depth(dev->id));
902 depth = min(ATA_MAX_QUEUE - 1, depth);
903 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, depth);
904 }
fa2fc7f4
JB
905
906 return 0;
a6cce2a7
BK
907}
908
1da177e4
LT
909/**
910 * ata_scsi_slave_config - Set SCSI device attributes
911 * @sdev: SCSI device to examine
912 *
913 * This is called before we actually start reading
914 * and writing to the device, to configure certain
915 * SCSI mid-layer behaviors.
916 *
917 * LOCKING:
918 * Defined by SCSI layer. We don't really care.
919 */
920
921int ata_scsi_slave_config(struct scsi_device *sdev)
922{
31534363
TH
923 struct ata_port *ap = ata_shost_to_port(sdev->host);
924 struct ata_device *dev = __ata_scsi_find_dev(ap, sdev);
fa2fc7f4 925 int rc = 0;
31534363 926
a6cce2a7 927 ata_scsi_sdev_config(sdev);
1da177e4 928
31534363 929 if (dev)
fa2fc7f4 930 rc = ata_scsi_dev_config(sdev, dev);
1da177e4 931
fa2fc7f4 932 return rc;
1da177e4
LT
933}
934
83c47bcb
TH
935/**
936 * ata_scsi_slave_destroy - SCSI device is about to be destroyed
937 * @sdev: SCSI device to be destroyed
938 *
939 * @sdev is about to be destroyed for hot/warm unplugging. If
940 * this unplugging was initiated by libata as indicated by NULL
941 * dev->sdev, this function doesn't have to do anything.
942 * Otherwise, SCSI layer initiated warm-unplug is in progress.
943 * Clear dev->sdev, schedule the device for ATA detach and invoke
944 * EH.
945 *
946 * LOCKING:
947 * Defined by SCSI layer. We don't really care.
948 */
949void ata_scsi_slave_destroy(struct scsi_device *sdev)
950{
951 struct ata_port *ap = ata_shost_to_port(sdev->host);
fa2fc7f4 952 struct request_queue *q = sdev->request_queue;
83c47bcb
TH
953 unsigned long flags;
954 struct ata_device *dev;
955
956 if (!ap->ops->error_handler)
957 return;
958
ba6a1308 959 spin_lock_irqsave(ap->lock, flags);
83c47bcb
TH
960 dev = __ata_scsi_find_dev(ap, sdev);
961 if (dev && dev->sdev) {
962 /* SCSI device already in CANCEL state, no need to offline it */
963 dev->sdev = NULL;
964 dev->flags |= ATA_DFLAG_DETACH;
965 ata_port_schedule_eh(ap);
966 }
ba6a1308 967 spin_unlock_irqrestore(ap->lock, flags);
fa2fc7f4
JB
968
969 kfree(q->dma_drain_buffer);
970 q->dma_drain_buffer = NULL;
971 q->dma_drain_size = 0;
83c47bcb
TH
972}
973
a6e6ce8e
TH
974/**
975 * ata_scsi_change_queue_depth - SCSI callback for queue depth config
976 * @sdev: SCSI device to configure queue depth for
977 * @queue_depth: new queue depth
978 *
979 * This is libata standard hostt->change_queue_depth callback.
980 * SCSI will call into this callback when user tries to set queue
981 * depth via sysfs.
982 *
983 * LOCKING:
984 * SCSI layer (we don't care)
985 *
986 * RETURNS:
987 * Newly configured queue depth.
988 */
989int ata_scsi_change_queue_depth(struct scsi_device *sdev, int queue_depth)
990{
991 struct ata_port *ap = ata_shost_to_port(sdev->host);
992 struct ata_device *dev;
360f654e 993 unsigned long flags;
a6e6ce8e 994
c3c70c44 995 if (queue_depth < 1 || queue_depth == sdev->queue_depth)
a6e6ce8e
TH
996 return sdev->queue_depth;
997
998 dev = ata_scsi_find_dev(ap, sdev);
999 if (!dev || !ata_dev_enabled(dev))
1000 return sdev->queue_depth;
1001
c3c70c44 1002 /* NCQ enabled? */
360f654e 1003 spin_lock_irqsave(ap->lock, flags);
c3c70c44
TH
1004 dev->flags &= ~ATA_DFLAG_NCQ_OFF;
1005 if (queue_depth == 1 || !ata_ncq_enabled(dev)) {
360f654e 1006 dev->flags |= ATA_DFLAG_NCQ_OFF;
c3c70c44
TH
1007 queue_depth = 1;
1008 }
360f654e
TH
1009 spin_unlock_irqrestore(ap->lock, flags);
1010
c3c70c44
TH
1011 /* limit and apply queue depth */
1012 queue_depth = min(queue_depth, sdev->host->can_queue);
1013 queue_depth = min(queue_depth, ata_id_queue_depth(dev->id));
1014 queue_depth = min(queue_depth, ATA_MAX_QUEUE - 1);
1015
1016 if (sdev->queue_depth == queue_depth)
1017 return -EINVAL;
1018
1019 scsi_adjust_queue_depth(sdev, MSG_SIMPLE_TAG, queue_depth);
a6e6ce8e
TH
1020 return queue_depth;
1021}
1022
d9aca22c 1023/* XXX: for spindown warning */
da071b42
TH
1024static void ata_delayed_done_timerfn(unsigned long arg)
1025{
1026 struct scsi_cmnd *scmd = (void *)arg;
1027
1028 scmd->scsi_done(scmd);
1029}
1030
d9aca22c 1031/* XXX: for spindown warning */
da071b42
TH
1032static void ata_delayed_done(struct scsi_cmnd *scmd)
1033{
1034 static struct timer_list timer;
1035
1036 setup_timer(&timer, ata_delayed_done_timerfn, (unsigned long)scmd);
1037 mod_timer(&timer, jiffies + 5 * HZ);
1038}
1039
972dcafb
DG
1040/**
1041 * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command
1042 * @qc: Storage for translated ATA taskfile
972dcafb
DG
1043 *
1044 * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY
1045 * (to start). Perhaps these commands should be preceded by
1046 * CHECK POWER MODE to see what power mode the device is already in.
1047 * [See SAT revision 5 at www.t10.org]
1048 *
1049 * LOCKING:
cca3974e 1050 * spin_lock_irqsave(host lock)
972dcafb
DG
1051 *
1052 * RETURNS:
1053 * Zero on success, non-zero on error.
1054 */
ad706991 1055static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc)
972dcafb 1056{
542b1444 1057 struct scsi_cmnd *scmd = qc->scsicmd;
972dcafb 1058 struct ata_taskfile *tf = &qc->tf;
ad706991 1059 const u8 *cdb = scmd->cmnd;
972dcafb 1060
2e5704f6
TH
1061 if (scmd->cmd_len < 5)
1062 goto invalid_fld;
1063
972dcafb
DG
1064 tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1065 tf->protocol = ATA_PROT_NODATA;
542b1444 1066 if (cdb[1] & 0x1) {
972dcafb
DG
1067 ; /* ignore IMMED bit, violates sat-r05 */
1068 }
542b1444 1069 if (cdb[4] & 0x2)
ae006510 1070 goto invalid_fld; /* LOEJ bit set not supported */
542b1444 1071 if (((cdb[4] >> 4) & 0xf) != 0)
ae006510 1072 goto invalid_fld; /* power conditions not supported */
e31e8531
TH
1073
1074 if (qc->dev->horkage & ATA_HORKAGE_SKIP_PM) {
1075 /* the device lacks PM support, finish without doing anything */
1076 scmd->result = SAM_STAT_GOOD;
1077 return 1;
1078 }
1079
542b1444 1080 if (cdb[4] & 0x1) {
972dcafb 1081 tf->nsect = 1; /* 1 sector, lba=0 */
9d5b1302
AL
1082
1083 if (qc->dev->flags & ATA_DFLAG_LBA) {
c44078c0 1084 tf->flags |= ATA_TFLAG_LBA;
9d5b1302
AL
1085
1086 tf->lbah = 0x0;
1087 tf->lbam = 0x0;
1088 tf->lbal = 0x0;
1089 tf->device |= ATA_LBA;
1090 } else {
1091 /* CHS */
1092 tf->lbal = 0x1; /* sect */
1093 tf->lbam = 0x0; /* cyl low */
1094 tf->lbah = 0x0; /* cyl high */
1095 }
1096
972dcafb 1097 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
920a4b10
TH
1098 } else {
1099 /* XXX: This is for backward compatibility, will be
1100 * removed. Read Documentation/feature-removal-schedule.txt
1101 * for more info.
1102 */
d9aca22c 1103 if ((qc->dev->flags & ATA_DFLAG_SPUNDOWN) &&
920a4b10
TH
1104 (system_state == SYSTEM_HALT ||
1105 system_state == SYSTEM_POWER_OFF)) {
2dcb407e 1106 static unsigned long warned;
920a4b10 1107
da071b42 1108 if (!test_and_set_bit(0, &warned)) {
920a4b10
TH
1109 ata_dev_printk(qc->dev, KERN_WARNING,
1110 "DISK MIGHT NOT BE SPUN DOWN PROPERLY. "
1111 "UPDATE SHUTDOWN UTILITY\n");
1112 ata_dev_printk(qc->dev, KERN_WARNING,
1113 "For more info, visit "
1114 "http://linux-ata.org/shutdown.html\n");
da071b42
TH
1115
1116 /* ->scsi_done is not used, use it for
1117 * delayed completion.
1118 */
1119 scmd->scsi_done = qc->scsidone;
1120 qc->scsidone = ata_delayed_done;
920a4b10
TH
1121 }
1122 scmd->result = SAM_STAT_GOOD;
1123 return 1;
1124 }
1125
78981a7c
RH
1126 /* Issue ATA STANDBY IMMEDIATE command */
1127 tf->command = ATA_CMD_STANDBYNOW1;
920a4b10 1128 }
78981a7c 1129
972dcafb
DG
1130 /*
1131 * Standby and Idle condition timers could be implemented but that
1132 * would require libata to implement the Power condition mode page
1133 * and allow the user to change it. Changing mode pages requires
1134 * MODE SELECT to be implemented.
1135 */
1136
1137 return 0;
ae006510
DG
1138
1139invalid_fld:
542b1444 1140 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
ae006510
DG
1141 /* "Invalid field in cbd" */
1142 return 1;
972dcafb
DG
1143}
1144
1145
1da177e4
LT
1146/**
1147 * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command
1148 * @qc: Storage for translated ATA taskfile
1da177e4
LT
1149 *
1150 * Sets up an ATA taskfile to issue FLUSH CACHE or
1151 * FLUSH CACHE EXT.
1152 *
1153 * LOCKING:
cca3974e 1154 * spin_lock_irqsave(host lock)
1da177e4
LT
1155 *
1156 * RETURNS:
1157 * Zero on success, non-zero on error.
1158 */
ad706991 1159static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc)
1da177e4
LT
1160{
1161 struct ata_taskfile *tf = &qc->tf;
1162
1163 tf->flags |= ATA_TFLAG_DEVICE;
1164 tf->protocol = ATA_PROT_NODATA;
1165
6fc49adb 1166 if (qc->dev->flags & ATA_DFLAG_FLUSH_EXT)
1da177e4
LT
1167 tf->command = ATA_CMD_FLUSH_EXT;
1168 else
1169 tf->command = ATA_CMD_FLUSH;
1170
b666da35
TH
1171 /* flush is critical for IO integrity, consider it an IO command */
1172 qc->flags |= ATA_QCFLAG_IO;
1173
1da177e4
LT
1174 return 0;
1175}
1176
3aef5231
AL
1177/**
1178 * scsi_6_lba_len - Get LBA and transfer length
542b1444 1179 * @cdb: SCSI command to translate
3aef5231
AL
1180 *
1181 * Calculate LBA and transfer length for 6-byte commands.
1182 *
1183 * RETURNS:
1184 * @plba: the LBA
1185 * @plen: the transfer length
1186 */
542b1444 1187static void scsi_6_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
3aef5231
AL
1188{
1189 u64 lba = 0;
6c7b7d2b 1190 u32 len;
3aef5231
AL
1191
1192 VPRINTK("six-byte command\n");
1193
6c7b7d2b 1194 lba |= ((u64)(cdb[1] & 0x1f)) << 16;
542b1444
TH
1195 lba |= ((u64)cdb[2]) << 8;
1196 lba |= ((u64)cdb[3]);
3aef5231 1197
6c7b7d2b 1198 len = cdb[4];
3aef5231
AL
1199
1200 *plba = lba;
1201 *plen = len;
1202}
1203
1204/**
1205 * scsi_10_lba_len - Get LBA and transfer length
542b1444 1206 * @cdb: SCSI command to translate
3aef5231
AL
1207 *
1208 * Calculate LBA and transfer length for 10-byte commands.
1209 *
1210 * RETURNS:
1211 * @plba: the LBA
1212 * @plen: the transfer length
1213 */
542b1444 1214static void scsi_10_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
3aef5231
AL
1215{
1216 u64 lba = 0;
1217 u32 len = 0;
1218
1219 VPRINTK("ten-byte command\n");
1220
542b1444
TH
1221 lba |= ((u64)cdb[2]) << 24;
1222 lba |= ((u64)cdb[3]) << 16;
1223 lba |= ((u64)cdb[4]) << 8;
1224 lba |= ((u64)cdb[5]);
3aef5231 1225
542b1444
TH
1226 len |= ((u32)cdb[7]) << 8;
1227 len |= ((u32)cdb[8]);
3aef5231
AL
1228
1229 *plba = lba;
1230 *plen = len;
1231}
1232
1233/**
1234 * scsi_16_lba_len - Get LBA and transfer length
542b1444 1235 * @cdb: SCSI command to translate
3aef5231
AL
1236 *
1237 * Calculate LBA and transfer length for 16-byte commands.
1238 *
1239 * RETURNS:
1240 * @plba: the LBA
1241 * @plen: the transfer length
1242 */
542b1444 1243static void scsi_16_lba_len(const u8 *cdb, u64 *plba, u32 *plen)
3aef5231
AL
1244{
1245 u64 lba = 0;
1246 u32 len = 0;
1247
1248 VPRINTK("sixteen-byte command\n");
1249
542b1444
TH
1250 lba |= ((u64)cdb[2]) << 56;
1251 lba |= ((u64)cdb[3]) << 48;
1252 lba |= ((u64)cdb[4]) << 40;
1253 lba |= ((u64)cdb[5]) << 32;
1254 lba |= ((u64)cdb[6]) << 24;
1255 lba |= ((u64)cdb[7]) << 16;
1256 lba |= ((u64)cdb[8]) << 8;
1257 lba |= ((u64)cdb[9]);
3aef5231 1258
542b1444
TH
1259 len |= ((u32)cdb[10]) << 24;
1260 len |= ((u32)cdb[11]) << 16;
1261 len |= ((u32)cdb[12]) << 8;
1262 len |= ((u32)cdb[13]);
3aef5231
AL
1263
1264 *plba = lba;
1265 *plen = len;
1266}
1267
1da177e4
LT
1268/**
1269 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
1270 * @qc: Storage for translated ATA taskfile
1da177e4
LT
1271 *
1272 * Converts SCSI VERIFY command to an ATA READ VERIFY command.
1273 *
1274 * LOCKING:
cca3974e 1275 * spin_lock_irqsave(host lock)
1da177e4
LT
1276 *
1277 * RETURNS:
1278 * Zero on success, non-zero on error.
1279 */
ad706991 1280static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc)
1da177e4 1281{
542b1444 1282 struct scsi_cmnd *scmd = qc->scsicmd;
1da177e4 1283 struct ata_taskfile *tf = &qc->tf;
8bf62ece 1284 struct ata_device *dev = qc->dev;
1da177e4 1285 u64 dev_sectors = qc->dev->n_sectors;
ad706991 1286 const u8 *cdb = scmd->cmnd;
3aef5231
AL
1287 u64 block;
1288 u32 n_block;
1da177e4
LT
1289
1290 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1291 tf->protocol = ATA_PROT_NODATA;
1da177e4 1292
2e5704f6
TH
1293 if (cdb[0] == VERIFY) {
1294 if (scmd->cmd_len < 10)
1295 goto invalid_fld;
542b1444 1296 scsi_10_lba_len(cdb, &block, &n_block);
2e5704f6
TH
1297 } else if (cdb[0] == VERIFY_16) {
1298 if (scmd->cmd_len < 16)
1299 goto invalid_fld;
542b1444 1300 scsi_16_lba_len(cdb, &block, &n_block);
2e5704f6 1301 } else
ae006510 1302 goto invalid_fld;
1da177e4 1303
8bf62ece 1304 if (!n_block)
ae006510 1305 goto nothing_to_do;
8bf62ece 1306 if (block >= dev_sectors)
ae006510 1307 goto out_of_range;
8bf62ece 1308 if ((block + n_block) > dev_sectors)
ae006510 1309 goto out_of_range;
1da177e4 1310
07506697
AL
1311 if (dev->flags & ATA_DFLAG_LBA) {
1312 tf->flags |= ATA_TFLAG_LBA;
1313
c6a33e24
AL
1314 if (lba_28_ok(block, n_block)) {
1315 /* use LBA28 */
1316 tf->command = ATA_CMD_VERIFY;
1317 tf->device |= (block >> 24) & 0xf;
1318 } else if (lba_48_ok(block, n_block)) {
1319 if (!(dev->flags & ATA_DFLAG_LBA48))
1320 goto out_of_range;
07506697
AL
1321
1322 /* use LBA48 */
1323 tf->flags |= ATA_TFLAG_LBA48;
8bf62ece 1324 tf->command = ATA_CMD_VERIFY_EXT;
1da177e4 1325
8bf62ece 1326 tf->hob_nsect = (n_block >> 8) & 0xff;
1da177e4 1327
8bf62ece
AL
1328 tf->hob_lbah = (block >> 40) & 0xff;
1329 tf->hob_lbam = (block >> 32) & 0xff;
1330 tf->hob_lbal = (block >> 24) & 0xff;
c6a33e24
AL
1331 } else
1332 /* request too large even for LBA48 */
1333 goto out_of_range;
8bf62ece
AL
1334
1335 tf->nsect = n_block & 0xff;
1da177e4 1336
8bf62ece
AL
1337 tf->lbah = (block >> 16) & 0xff;
1338 tf->lbam = (block >> 8) & 0xff;
1339 tf->lbal = block & 0xff;
1da177e4 1340
8bf62ece
AL
1341 tf->device |= ATA_LBA;
1342 } else {
1343 /* CHS */
1344 u32 sect, head, cyl, track;
1345
c6a33e24
AL
1346 if (!lba_28_ok(block, n_block))
1347 goto out_of_range;
07506697 1348
8bf62ece
AL
1349 /* Convert LBA to CHS */
1350 track = (u32)block / dev->sectors;
1351 cyl = track / dev->heads;
1352 head = track % dev->heads;
1353 sect = (u32)block % dev->sectors + 1;
1354
c187c4b5
AL
1355 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1356 (u32)block, track, cyl, head, sect);
2e9edbf8
JG
1357
1358 /* Check whether the converted CHS can fit.
1359 Cylinder: 0-65535
8bf62ece
AL
1360 Head: 0-15
1361 Sector: 1-255*/
2e9edbf8 1362 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
ae006510 1363 goto out_of_range;
2e9edbf8 1364
8bf62ece
AL
1365 tf->command = ATA_CMD_VERIFY;
1366 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1367 tf->lbal = sect;
1368 tf->lbam = cyl;
1369 tf->lbah = cyl >> 8;
1370 tf->device |= head;
1371 }
1da177e4
LT
1372
1373 return 0;
ae006510
DG
1374
1375invalid_fld:
542b1444 1376 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
ae006510
DG
1377 /* "Invalid field in cbd" */
1378 return 1;
1379
1380out_of_range:
542b1444 1381 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
ae006510
DG
1382 /* "Logical Block Address out of range" */
1383 return 1;
1384
1385nothing_to_do:
542b1444 1386 scmd->result = SAM_STAT_GOOD;
ae006510 1387 return 1;
1da177e4
LT
1388}
1389
1390/**
1391 * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one
1392 * @qc: Storage for translated ATA taskfile
1da177e4
LT
1393 *
1394 * Converts any of six SCSI read/write commands into the
1395 * ATA counterpart, including starting sector (LBA),
1396 * sector count, and taking into account the device's LBA48
1397 * support.
1398 *
1399 * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and
1400 * %WRITE_16 are currently supported.
1401 *
1402 * LOCKING:
cca3974e 1403 * spin_lock_irqsave(host lock)
1da177e4
LT
1404 *
1405 * RETURNS:
1406 * Zero on success, non-zero on error.
1407 */
ad706991 1408static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc)
1da177e4 1409{
542b1444 1410 struct scsi_cmnd *scmd = qc->scsicmd;
ad706991 1411 const u8 *cdb = scmd->cmnd;
bd056d7e 1412 unsigned int tf_flags = 0;
3aef5231
AL
1413 u64 block;
1414 u32 n_block;
bd056d7e 1415 int rc;
1da177e4 1416
542b1444 1417 if (cdb[0] == WRITE_10 || cdb[0] == WRITE_6 || cdb[0] == WRITE_16)
bd056d7e 1418 tf_flags |= ATA_TFLAG_WRITE;
1da177e4 1419
9a3dccc4 1420 /* Calculate the SCSI LBA, transfer length and FUA. */
542b1444 1421 switch (cdb[0]) {
3aef5231
AL
1422 case READ_10:
1423 case WRITE_10:
2e5704f6
TH
1424 if (unlikely(scmd->cmd_len < 10))
1425 goto invalid_fld;
542b1444
TH
1426 scsi_10_lba_len(cdb, &block, &n_block);
1427 if (unlikely(cdb[1] & (1 << 3)))
bd056d7e 1428 tf_flags |= ATA_TFLAG_FUA;
3aef5231
AL
1429 break;
1430 case READ_6:
1431 case WRITE_6:
2e5704f6
TH
1432 if (unlikely(scmd->cmd_len < 6))
1433 goto invalid_fld;
542b1444 1434 scsi_6_lba_len(cdb, &block, &n_block);
c187c4b5
AL
1435
1436 /* for 6-byte r/w commands, transfer length 0
1437 * means 256 blocks of data, not 0 block.
1438 */
76b2bf9b
JG
1439 if (!n_block)
1440 n_block = 256;
3aef5231
AL
1441 break;
1442 case READ_16:
1443 case WRITE_16:
2e5704f6
TH
1444 if (unlikely(scmd->cmd_len < 16))
1445 goto invalid_fld;
542b1444
TH
1446 scsi_16_lba_len(cdb, &block, &n_block);
1447 if (unlikely(cdb[1] & (1 << 3)))
bd056d7e 1448 tf_flags |= ATA_TFLAG_FUA;
3aef5231
AL
1449 break;
1450 default:
8bf62ece 1451 DPRINTK("no-byte command\n");
ae006510 1452 goto invalid_fld;
1da177e4
LT
1453 }
1454
8bf62ece
AL
1455 /* Check and compose ATA command */
1456 if (!n_block)
c187c4b5
AL
1457 /* For 10-byte and 16-byte SCSI R/W commands, transfer
1458 * length 0 means transfer 0 block of data.
1459 * However, for ATA R/W commands, sector count 0 means
1460 * 256 or 65536 sectors, not 0 sectors as in SCSI.
f51750d5
AC
1461 *
1462 * WARNING: one or two older ATA drives treat 0 as 0...
c187c4b5 1463 */
ae006510 1464 goto nothing_to_do;
1da177e4 1465
bd056d7e 1466 qc->flags |= ATA_QCFLAG_IO;
726f0785 1467 qc->nbytes = n_block * ATA_SECT_SIZE;
1da177e4 1468
bd056d7e
TH
1469 rc = ata_build_rw_tf(&qc->tf, qc->dev, block, n_block, tf_flags,
1470 qc->tag);
1471 if (likely(rc == 0))
1472 return 0;
ae006510 1473
bd056d7e
TH
1474 if (rc == -ERANGE)
1475 goto out_of_range;
1476 /* treat all other errors as -EINVAL, fall through */
ae006510 1477invalid_fld:
542b1444 1478 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x0);
ae006510
DG
1479 /* "Invalid field in cbd" */
1480 return 1;
1481
1482out_of_range:
542b1444 1483 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x21, 0x0);
ae006510
DG
1484 /* "Logical Block Address out of range" */
1485 return 1;
1486
1487nothing_to_do:
542b1444 1488 scmd->result = SAM_STAT_GOOD;
ae006510 1489 return 1;
1da177e4
LT
1490}
1491
77853bf2 1492static void ata_scsi_qc_complete(struct ata_queued_cmd *qc)
1da177e4 1493{
c31f571d 1494 struct ata_port *ap = qc->ap;
1da177e4 1495 struct scsi_cmnd *cmd = qc->scsicmd;
a7dac447 1496 u8 *cdb = cmd->cmnd;
2dcb407e 1497 int need_sense = (qc->err_mask != 0);
b095518e
JG
1498
1499 /* For ATA pass thru (SAT) commands, generate a sense block if
1500 * user mandated it or if there's an error. Note that if we
1501 * generate because the user forced us to, a check condition
1502 * is generated and the ATA register values are returned
1503 * whether the command completed successfully or not. If there
1504 * was no error, SK, ASC and ASCQ will all be zero.
1505 */
a7dac447 1506 if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) &&
2dcb407e 1507 ((cdb[2] & 0x20) || need_sense)) {
750426aa 1508 ata_gen_passthru_sense(qc);
b095518e
JG
1509 } else {
1510 if (!need_sense) {
1511 cmd->result = SAM_STAT_GOOD;
1512 } else {
1513 /* TODO: decide which descriptor format to use
1514 * for 48b LBA devices and call that here
1515 * instead of the fixed desc, which is only
1516 * good for smaller LBA (and maybe CHS?)
1517 * devices.
1518 */
750426aa 1519 ata_gen_ata_sense(qc);
b095518e
JG
1520 }
1521 }
1da177e4 1522
d9aca22c 1523 /* XXX: track spindown state for spindown skipping and warning */
13b8d09f
TH
1524 if (unlikely(qc->tf.command == ATA_CMD_STANDBY ||
1525 qc->tf.command == ATA_CMD_STANDBYNOW1))
1526 qc->dev->flags |= ATA_DFLAG_SPUNDOWN;
1527 else if (likely(system_state != SYSTEM_HALT &&
1528 system_state != SYSTEM_POWER_OFF))
1529 qc->dev->flags &= ~ATA_DFLAG_SPUNDOWN;
1530
c31f571d 1531 if (need_sense && !ap->ops->error_handler)
44877b4e 1532 ata_dump_status(ap->print_id, &qc->result_tf);
1da177e4
LT
1533
1534 qc->scsidone(cmd);
1535
77853bf2 1536 ata_qc_free(qc);
1da177e4
LT
1537}
1538
1539/**
1540 * ata_scsi_translate - Translate then issue SCSI command to ATA device
1da177e4
LT
1541 * @dev: ATA device to which the command is addressed
1542 * @cmd: SCSI command to execute
1543 * @done: SCSI command completion function
1544 * @xlat_func: Actor which translates @cmd to an ATA taskfile
1545 *
1546 * Our ->queuecommand() function has decided that the SCSI
1547 * command issued can be directly translated into an ATA
1548 * command, rather than handled internally.
1549 *
1550 * This function sets up an ata_queued_cmd structure for the
1551 * SCSI command, and sends that ata_queued_cmd to the hardware.
1552 *
ae006510
DG
1553 * The xlat_func argument (actor) returns 0 if ready to execute
1554 * ATA command, else 1 to finish translation. If 1 is returned
1555 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1556 * to be set reflecting an error condition or clean (early)
1557 * termination.
1558 *
1da177e4 1559 * LOCKING:
cca3974e 1560 * spin_lock_irqsave(host lock)
2115ea94
TH
1561 *
1562 * RETURNS:
1563 * 0 on success, SCSI_ML_QUEUE_DEVICE_BUSY if the command
1564 * needs to be deferred.
1da177e4 1565 */
2115ea94
TH
1566static int ata_scsi_translate(struct ata_device *dev, struct scsi_cmnd *cmd,
1567 void (*done)(struct scsi_cmnd *),
1568 ata_xlat_func_t xlat_func)
1da177e4 1569{
31cc23b3 1570 struct ata_port *ap = dev->link->ap;
1da177e4 1571 struct ata_queued_cmd *qc;
31cc23b3 1572 int rc;
1da177e4
LT
1573
1574 VPRINTK("ENTER\n");
1575
3373efd8 1576 qc = ata_scsi_qc_new(dev, cmd, done);
1da177e4 1577 if (!qc)
ae006510 1578 goto err_mem;
1da177e4
LT
1579
1580 /* data is present; dma-map it */
be7db055 1581 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
1582 cmd->sc_data_direction == DMA_TO_DEVICE) {
7120165c 1583 if (unlikely(scsi_bufflen(cmd) < 1)) {
f15a1daf
TH
1584 ata_dev_printk(dev, KERN_WARNING,
1585 "WARNING: zero len r/w req\n");
ae006510 1586 goto err_did;
1da177e4
LT
1587 }
1588
7120165c 1589 ata_sg_init(qc, scsi_sglist(cmd), scsi_sg_count(cmd));
1da177e4
LT
1590
1591 qc->dma_dir = cmd->sc_data_direction;
1592 }
1593
1594 qc->complete_fn = ata_scsi_qc_complete;
1595
ad706991 1596 if (xlat_func(qc))
ae006510 1597 goto early_finish;
1da177e4 1598
31cc23b3
TH
1599 if (ap->ops->qc_defer) {
1600 if ((rc = ap->ops->qc_defer(qc)))
1601 goto defer;
1602 }
1603
1da177e4 1604 /* select device, send command to hardware */
8e0e694a 1605 ata_qc_issue(qc);
1da177e4
LT
1606
1607 VPRINTK("EXIT\n");
2115ea94 1608 return 0;
1da177e4 1609
ae006510 1610early_finish:
2dcb407e 1611 ata_qc_free(qc);
da071b42 1612 qc->scsidone(cmd);
ae006510 1613 DPRINTK("EXIT - early finish (good or error)\n");
2115ea94 1614 return 0;
ae006510
DG
1615
1616err_did:
1da177e4 1617 ata_qc_free(qc);
ae006510 1618 cmd->result = (DID_ERROR << 16);
da071b42 1619 qc->scsidone(cmd);
253b92ec 1620err_mem:
ae006510 1621 DPRINTK("EXIT - internal\n");
2115ea94 1622 return 0;
3dc1d881
TH
1623
1624defer:
31cc23b3 1625 ata_qc_free(qc);
3dc1d881 1626 DPRINTK("EXIT - defer\n");
31cc23b3
TH
1627 if (rc == ATA_DEFER_LINK)
1628 return SCSI_MLQUEUE_DEVICE_BUSY;
1629 else
1630 return SCSI_MLQUEUE_HOST_BUSY;
1da177e4
LT
1631}
1632
1633/**
1634 * ata_scsi_rbuf_get - Map response buffer.
1635 * @cmd: SCSI command containing buffer to be mapped.
1636 * @buf_out: Pointer to mapped area.
1637 *
1638 * Maps buffer contained within SCSI command @cmd.
1639 *
1640 * LOCKING:
cca3974e 1641 * spin_lock_irqsave(host lock)
1da177e4
LT
1642 *
1643 * RETURNS:
1644 * Length of response buffer.
1645 */
1646
1647static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out)
1648{
1649 u8 *buf;
1650 unsigned int buflen;
1651
7120165c 1652 struct scatterlist *sg = scsi_sglist(cmd);
1da177e4 1653
e10b8c3f 1654 if (sg) {
45711f1a 1655 buf = kmap_atomic(sg_page(sg), KM_IRQ0) + sg->offset;
1da177e4
LT
1656 buflen = sg->length;
1657 } else {
e10b8c3f
BH
1658 buf = NULL;
1659 buflen = 0;
1da177e4
LT
1660 }
1661
1662 *buf_out = buf;
1663 return buflen;
1664}
1665
1666/**
1667 * ata_scsi_rbuf_put - Unmap response buffer.
1668 * @cmd: SCSI command containing buffer to be unmapped.
1669 * @buf: buffer to unmap
1670 *
1671 * Unmaps response buffer contained within @cmd.
1672 *
1673 * LOCKING:
cca3974e 1674 * spin_lock_irqsave(host lock)
1da177e4
LT
1675 */
1676
1677static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
1678{
7120165c 1679 struct scatterlist *sg = scsi_sglist(cmd);
e10b8c3f 1680 if (sg)
da02d2a1 1681 kunmap_atomic(buf - sg->offset, KM_IRQ0);
1da177e4
LT
1682}
1683
1684/**
1685 * ata_scsi_rbuf_fill - wrapper for SCSI command simulators
1686 * @args: device IDENTIFY data / SCSI command of interest.
1687 * @actor: Callback hook for desired SCSI command simulator
1688 *
1689 * Takes care of the hard work of simulating a SCSI command...
1690 * Mapping the response buffer, calling the command's handler,
1691 * and handling the handler's return value. This return value
1692 * indicates whether the handler wishes the SCSI command to be
ae006510
DG
1693 * completed successfully (0), or not (in which case cmd->result
1694 * and sense buffer are assumed to be set).
1da177e4
LT
1695 *
1696 * LOCKING:
cca3974e 1697 * spin_lock_irqsave(host lock)
1da177e4
LT
1698 */
1699
1700void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
2dcb407e
JG
1701 unsigned int (*actor) (struct ata_scsi_args *args,
1702 u8 *rbuf, unsigned int buflen))
1da177e4
LT
1703{
1704 u8 *rbuf;
1705 unsigned int buflen, rc;
1706 struct scsi_cmnd *cmd = args->cmd;
b445c568
JG
1707 unsigned long flags;
1708
1709 local_irq_save(flags);
1da177e4
LT
1710
1711 buflen = ata_scsi_rbuf_get(cmd, &rbuf);
1712 memset(rbuf, 0, buflen);
1713 rc = actor(args, rbuf, buflen);
1714 ata_scsi_rbuf_put(cmd, rbuf);
1715
b445c568
JG
1716 local_irq_restore(flags);
1717
ae006510 1718 if (rc == 0)
1da177e4 1719 cmd->result = SAM_STAT_GOOD;
ae006510 1720 args->done(cmd);
1da177e4
LT
1721}
1722
6a36261e
TH
1723/**
1724 * ATA_SCSI_RBUF_SET - helper to set values in SCSI response buffer
1725 * @idx: byte index into SCSI response buffer
1726 * @val: value to set
1727 *
1728 * To be used by SCSI command simulator functions. This macros
1729 * expects two local variables, u8 *rbuf and unsigned int buflen,
1730 * are in scope.
1731 *
1732 * LOCKING:
1733 * None.
1734 */
1735#define ATA_SCSI_RBUF_SET(idx, val) do { \
1736 if ((idx) < buflen) rbuf[(idx)] = (u8)(val); \
1737 } while (0)
1738
1da177e4
LT
1739/**
1740 * ata_scsiop_inq_std - Simulate INQUIRY command
1741 * @args: device IDENTIFY data / SCSI command of interest.
1742 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1743 * @buflen: Response buffer length.
1744 *
1745 * Returns standard device identification data associated
b142eb65 1746 * with non-VPD INQUIRY command output.
1da177e4
LT
1747 *
1748 * LOCKING:
cca3974e 1749 * spin_lock_irqsave(host lock)
1da177e4
LT
1750 */
1751
1752unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf,
1753 unsigned int buflen)
1754{
1755 u8 hdr[] = {
1756 TYPE_DISK,
1757 0,
1758 0x5, /* claim SPC-3 version compatibility */
1759 2,
1760 95 - 4
1761 };
1762
1763 /* set scsi removeable (RMB) bit per ata bit */
1764 if (ata_id_removeable(args->id))
1765 hdr[1] |= (1 << 7);
1766
1767 VPRINTK("ENTER\n");
1768
1769 memcpy(rbuf, hdr, sizeof(hdr));
1770
1771 if (buflen > 35) {
1772 memcpy(&rbuf[8], "ATA ", 8);
a0cf733b
TH
1773 ata_id_string(args->id, &rbuf[16], ATA_ID_PROD, 16);
1774 ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV, 4);
1da177e4
LT
1775 if (rbuf[32] == 0 || rbuf[32] == ' ')
1776 memcpy(&rbuf[32], "n/a ", 4);
1777 }
1778
1779 if (buflen > 63) {
1780 const u8 versions[] = {
1781 0x60, /* SAM-3 (no version claimed) */
1782
1783 0x03,
1784 0x20, /* SBC-2 (no version claimed) */
1785
1786 0x02,
1787 0x60 /* SPC-3 (no version claimed) */
1788 };
1789
1790 memcpy(rbuf + 59, versions, sizeof(versions));
1791 }
1792
1793 return 0;
1794}
1795
1796/**
b142eb65 1797 * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages
1da177e4
LT
1798 * @args: device IDENTIFY data / SCSI command of interest.
1799 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1800 * @buflen: Response buffer length.
1801 *
b142eb65 1802 * Returns list of inquiry VPD pages available.
1da177e4
LT
1803 *
1804 * LOCKING:
cca3974e 1805 * spin_lock_irqsave(host lock)
1da177e4
LT
1806 */
1807
1808unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf,
1809 unsigned int buflen)
1810{
1811 const u8 pages[] = {
1812 0x00, /* page 0x00, this page */
1813 0x80, /* page 0x80, unit serial no page */
1814 0x83 /* page 0x83, device ident page */
1815 };
b142eb65 1816 rbuf[3] = sizeof(pages); /* number of supported VPD pages */
1da177e4
LT
1817
1818 if (buflen > 6)
1819 memcpy(rbuf + 4, pages, sizeof(pages));
1820
1821 return 0;
1822}
1823
1824/**
b142eb65 1825 * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number
1da177e4
LT
1826 * @args: device IDENTIFY data / SCSI command of interest.
1827 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1828 * @buflen: Response buffer length.
1829 *
1830 * Returns ATA device serial number.
1831 *
1832 * LOCKING:
cca3974e 1833 * spin_lock_irqsave(host lock)
1da177e4
LT
1834 */
1835
1836unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf,
1837 unsigned int buflen)
1838{
1839 const u8 hdr[] = {
1840 0,
1841 0x80, /* this page code */
1842 0,
a0cf733b 1843 ATA_ID_SERNO_LEN, /* page len */
1da177e4
LT
1844 };
1845 memcpy(rbuf, hdr, sizeof(hdr));
1846
a0cf733b 1847 if (buflen > (ATA_ID_SERNO_LEN + 4 - 1))
6a62a04d 1848 ata_id_string(args->id, (unsigned char *) &rbuf[4],
a0cf733b 1849 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
1da177e4
LT
1850
1851 return 0;
1852}
1853
1da177e4 1854/**
b142eb65 1855 * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity
1da177e4
LT
1856 * @args: device IDENTIFY data / SCSI command of interest.
1857 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1858 * @buflen: Response buffer length.
1859 *
b142eb65
JG
1860 * Yields two logical unit device identification designators:
1861 * - vendor specific ASCII containing the ATA serial number
1862 * - SAT defined "t10 vendor id based" containing ASCII vendor
1863 * name ("ATA "), model and serial numbers.
1da177e4
LT
1864 *
1865 * LOCKING:
cca3974e 1866 * spin_lock_irqsave(host lock)
1da177e4
LT
1867 */
1868
1869unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf,
1870 unsigned int buflen)
1871{
b142eb65
JG
1872 int num;
1873 const int sat_model_serial_desc_len = 68;
1da177e4 1874
b142eb65
JG
1875 rbuf[1] = 0x83; /* this page code */
1876 num = 4;
1877
a0cf733b 1878 if (buflen > (ATA_ID_SERNO_LEN + num + 3)) {
b142eb65 1879 /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */
2e9edbf8 1880 rbuf[num + 0] = 2;
a0cf733b 1881 rbuf[num + 3] = ATA_ID_SERNO_LEN;
b142eb65
JG
1882 num += 4;
1883 ata_id_string(args->id, (unsigned char *) rbuf + num,
a0cf733b
TH
1884 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
1885 num += ATA_ID_SERNO_LEN;
1da177e4 1886 }
b142eb65
JG
1887 if (buflen > (sat_model_serial_desc_len + num + 3)) {
1888 /* SAT defined lu model and serial numbers descriptor */
1889 /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */
2e9edbf8
JG
1890 rbuf[num + 0] = 2;
1891 rbuf[num + 1] = 1;
b142eb65
JG
1892 rbuf[num + 3] = sat_model_serial_desc_len;
1893 num += 4;
1894 memcpy(rbuf + num, "ATA ", 8);
1895 num += 8;
1896 ata_id_string(args->id, (unsigned char *) rbuf + num,
a0cf733b
TH
1897 ATA_ID_PROD, ATA_ID_PROD_LEN);
1898 num += ATA_ID_PROD_LEN;
b142eb65 1899 ata_id_string(args->id, (unsigned char *) rbuf + num,
a0cf733b
TH
1900 ATA_ID_SERNO, ATA_ID_SERNO_LEN);
1901 num += ATA_ID_SERNO_LEN;
b142eb65
JG
1902 }
1903 rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */
1da177e4
LT
1904 return 0;
1905}
1906
ad355b46
JG
1907/**
1908 * ata_scsiop_inq_89 - Simulate INQUIRY VPD page 89, ATA info
1909 * @args: device IDENTIFY data / SCSI command of interest.
1910 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1911 * @buflen: Response buffer length.
1912 *
1913 * Yields SAT-specified ATA VPD page.
1914 *
1915 * LOCKING:
1916 * spin_lock_irqsave(host lock)
1917 */
1918
08f01058 1919static unsigned int ata_scsiop_inq_89(struct ata_scsi_args *args, u8 *rbuf,
ad355b46
JG
1920 unsigned int buflen)
1921{
1922 u8 pbuf[60];
1923 struct ata_taskfile tf;
1924 unsigned int i;
1925
1926 if (!buflen)
1927 return 0;
1928
1929 memset(&pbuf, 0, sizeof(pbuf));
1930 memset(&tf, 0, sizeof(tf));
1931
1932 pbuf[1] = 0x89; /* our page code */
1933 pbuf[2] = (0x238 >> 8); /* page size fixed at 238h */
1934 pbuf[3] = (0x238 & 0xff);
1935
c78968bb
JG
1936 memcpy(&pbuf[8], "linux ", 8);
1937 memcpy(&pbuf[16], "libata ", 16);
1938 memcpy(&pbuf[32], DRV_VERSION, 4);
ad355b46
JG
1939 ata_id_string(args->id, &pbuf[32], ATA_ID_FW_REV, 4);
1940
1941 /* we don't store the ATA device signature, so we fake it */
1942
1943 tf.command = ATA_DRDY; /* really, this is Status reg */
1944 tf.lbal = 0x1;
1945 tf.nsect = 0x1;
1946
1947 ata_tf_to_fis(&tf, 0, 1, &pbuf[36]); /* TODO: PMP? */
1948 pbuf[36] = 0x34; /* force D2H Reg FIS (34h) */
1949
1950 pbuf[56] = ATA_CMD_ID_ATA;
1951
1952 i = min(buflen, 60U);
1953 memcpy(rbuf, &pbuf[0], i);
1954 buflen -= i;
1955
1956 if (!buflen)
1957 return 0;
1958
1959 memcpy(&rbuf[60], &args->id[0], min(buflen, 512U));
1960 return 0;
1961}
1962
1da177e4 1963/**
0cba632b 1964 * ata_scsiop_noop - Command handler that simply returns success.
1da177e4
LT
1965 * @args: device IDENTIFY data / SCSI command of interest.
1966 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
1967 * @buflen: Response buffer length.
1968 *
1969 * No operation. Simply returns success to caller, to indicate
1970 * that the caller should successfully complete this SCSI command.
1971 *
1972 * LOCKING:
cca3974e 1973 * spin_lock_irqsave(host lock)
1da177e4
LT
1974 */
1975
1976unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf,
1977 unsigned int buflen)
1978{
1979 VPRINTK("ENTER\n");
1980 return 0;
1981}
1982
1983/**
1984 * ata_msense_push - Push data onto MODE SENSE data output buffer
1985 * @ptr_io: (input/output) Location to store more output data
1986 * @last: End of output data buffer
1987 * @buf: Pointer to BLOB being added to output buffer
1988 * @buflen: Length of BLOB
1989 *
1990 * Store MODE SENSE data on an output buffer.
1991 *
1992 * LOCKING:
1993 * None.
1994 */
1995
1996static void ata_msense_push(u8 **ptr_io, const u8 *last,
1997 const u8 *buf, unsigned int buflen)
1998{
1999 u8 *ptr = *ptr_io;
2000
2001 if ((ptr + buflen - 1) > last)
2002 return;
2003
2004 memcpy(ptr, buf, buflen);
2005
2006 ptr += buflen;
2007
2008 *ptr_io = ptr;
2009}
2010
2011/**
2012 * ata_msense_caching - Simulate MODE SENSE caching info page
2013 * @id: device IDENTIFY data
2014 * @ptr_io: (input/output) Location to store more output data
2015 * @last: End of output data buffer
2016 *
2017 * Generate a caching info page, which conditionally indicates
2018 * write caching to the SCSI layer, depending on device
2019 * capabilities.
2020 *
2021 * LOCKING:
2022 * None.
2023 */
2024
2025static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io,
2026 const u8 *last)
2027{
00ac37f5 2028 u8 page[CACHE_MPAGE_LEN];
1da177e4 2029
00ac37f5 2030 memcpy(page, def_cache_mpage, sizeof(page));
1da177e4
LT
2031 if (ata_id_wcache_enabled(id))
2032 page[2] |= (1 << 2); /* write cache enable */
2033 if (!ata_id_rahead_enabled(id))
2034 page[12] |= (1 << 5); /* disable read ahead */
2035
2036 ata_msense_push(ptr_io, last, page, sizeof(page));
2037 return sizeof(page);
2038}
2039
2040/**
2041 * ata_msense_ctl_mode - Simulate MODE SENSE control mode page
2042 * @dev: Device associated with this MODE SENSE command
2043 * @ptr_io: (input/output) Location to store more output data
2044 * @last: End of output data buffer
2045 *
2046 * Generate a generic MODE SENSE control mode page.
2047 *
2048 * LOCKING:
2049 * None.
2050 */
2051
2052static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last)
2053{
00ac37f5
DG
2054 ata_msense_push(ptr_io, last, def_control_mpage,
2055 sizeof(def_control_mpage));
2056 return sizeof(def_control_mpage);
1da177e4
LT
2057}
2058
2059/**
2060 * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page
2061 * @dev: Device associated with this MODE SENSE command
2062 * @ptr_io: (input/output) Location to store more output data
2063 * @last: End of output data buffer
2064 *
2065 * Generate a generic MODE SENSE r/w error recovery page.
2066 *
2067 * LOCKING:
2068 * None.
2069 */
2070
2071static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last)
2072{
1da177e4 2073
00ac37f5
DG
2074 ata_msense_push(ptr_io, last, def_rw_recovery_mpage,
2075 sizeof(def_rw_recovery_mpage));
2076 return sizeof(def_rw_recovery_mpage);
1da177e4
LT
2077}
2078
48bdc8ec
JA
2079/*
2080 * We can turn this into a real blacklist if it's needed, for now just
2081 * blacklist any Maxtor BANC1G10 revision firmware
2082 */
2083static int ata_dev_supports_fua(u16 *id)
2084{
a0cf733b 2085 unsigned char model[ATA_ID_PROD_LEN + 1], fw[ATA_ID_FW_REV_LEN + 1];
48bdc8ec 2086
c3c013a2
JG
2087 if (!libata_fua)
2088 return 0;
48bdc8ec
JA
2089 if (!ata_id_has_fua(id))
2090 return 0;
2091
a0cf733b
TH
2092 ata_id_c_string(id, model, ATA_ID_PROD, sizeof(model));
2093 ata_id_c_string(id, fw, ATA_ID_FW_REV, sizeof(fw));
48bdc8ec 2094
2e02671d 2095 if (strcmp(model, "Maxtor"))
48bdc8ec 2096 return 1;
2e02671d 2097 if (strcmp(fw, "BANC1G10"))
48bdc8ec
JA
2098 return 1;
2099
2100 return 0; /* blacklisted */
2101}
2102
1da177e4
LT
2103/**
2104 * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands
2105 * @args: device IDENTIFY data / SCSI command of interest.
2106 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2107 * @buflen: Response buffer length.
2108 *
00ac37f5
DG
2109 * Simulate MODE SENSE commands. Assume this is invoked for direct
2110 * access devices (e.g. disks) only. There should be no block
2111 * descriptor for other device types.
1da177e4
LT
2112 *
2113 * LOCKING:
cca3974e 2114 * spin_lock_irqsave(host lock)
1da177e4
LT
2115 */
2116
2117unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
2118 unsigned int buflen)
2119{
9a3dccc4 2120 struct ata_device *dev = args->dev;
1da177e4 2121 u8 *scsicmd = args->cmd->cmnd, *p, *last;
00ac37f5
DG
2122 const u8 sat_blk_desc[] = {
2123 0, 0, 0, 0, /* number of blocks: sat unspecified */
2124 0,
2125 0, 0x2, 0x0 /* block length: 512 bytes */
2126 };
2127 u8 pg, spg;
2128 unsigned int ebd, page_control, six_byte, output_len, alloc_len, minlen;
9a3dccc4 2129 u8 dpofua;
1da177e4
LT
2130
2131 VPRINTK("ENTER\n");
2132
2133 six_byte = (scsicmd[0] == MODE_SENSE);
00ac37f5
DG
2134 ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */
2135 /*
2136 * LLBA bit in msense(10) ignored (compliant)
1da177e4 2137 */
00ac37f5 2138
1da177e4 2139 page_control = scsicmd[2] >> 6;
ae006510
DG
2140 switch (page_control) {
2141 case 0: /* current */
2142 break; /* supported */
2143 case 3: /* saved */
2144 goto saving_not_supp;
2145 case 1: /* changeable */
2146 case 2: /* defaults */
2147 default:
2148 goto invalid_fld;
2149 }
1da177e4 2150
00ac37f5
DG
2151 if (six_byte) {
2152 output_len = 4 + (ebd ? 8 : 0);
2153 alloc_len = scsicmd[4];
2154 } else {
2155 output_len = 8 + (ebd ? 8 : 0);
2156 alloc_len = (scsicmd[7] << 8) + scsicmd[8];
2157 }
2158 minlen = (alloc_len < buflen) ? alloc_len : buflen;
1da177e4
LT
2159
2160 p = rbuf + output_len;
00ac37f5 2161 last = rbuf + minlen - 1;
1da177e4 2162
00ac37f5
DG
2163 pg = scsicmd[2] & 0x3f;
2164 spg = scsicmd[3];
2165 /*
2166 * No mode subpages supported (yet) but asking for _all_
2167 * subpages may be valid
2168 */
2169 if (spg && (spg != ALL_SUB_MPAGES))
2170 goto invalid_fld;
2171
2172 switch(pg) {
2173 case RW_RECOVERY_MPAGE:
1da177e4
LT
2174 output_len += ata_msense_rw_recovery(&p, last);
2175 break;
2176
00ac37f5 2177 case CACHE_MPAGE:
1da177e4
LT
2178 output_len += ata_msense_caching(args->id, &p, last);
2179 break;
2180
00ac37f5 2181 case CONTROL_MPAGE: {
1da177e4
LT
2182 output_len += ata_msense_ctl_mode(&p, last);
2183 break;
2184 }
2185
00ac37f5 2186 case ALL_MPAGES:
1da177e4
LT
2187 output_len += ata_msense_rw_recovery(&p, last);
2188 output_len += ata_msense_caching(args->id, &p, last);
2189 output_len += ata_msense_ctl_mode(&p, last);
2190 break;
2191
2192 default: /* invalid page code */
ae006510 2193 goto invalid_fld;
1da177e4
LT
2194 }
2195
00ac37f5
DG
2196 if (minlen < 1)
2197 return 0;
9a3dccc4
TH
2198
2199 dpofua = 0;
f79d409f 2200 if (ata_dev_supports_fua(args->id) && (dev->flags & ATA_DFLAG_LBA48) &&
9a3dccc4
TH
2201 (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count))
2202 dpofua = 1 << 4;
2203
1da177e4
LT
2204 if (six_byte) {
2205 output_len--;
2206 rbuf[0] = output_len;
9a3dccc4
TH
2207 if (minlen > 2)
2208 rbuf[2] |= dpofua;
00ac37f5
DG
2209 if (ebd) {
2210 if (minlen > 3)
2211 rbuf[3] = sizeof(sat_blk_desc);
2212 if (minlen > 11)
2213 memcpy(rbuf + 4, sat_blk_desc,
2214 sizeof(sat_blk_desc));
2215 }
1da177e4
LT
2216 } else {
2217 output_len -= 2;
2218 rbuf[0] = output_len >> 8;
00ac37f5
DG
2219 if (minlen > 1)
2220 rbuf[1] = output_len;
9a3dccc4
TH
2221 if (minlen > 3)
2222 rbuf[3] |= dpofua;
00ac37f5
DG
2223 if (ebd) {
2224 if (minlen > 7)
2225 rbuf[7] = sizeof(sat_blk_desc);
2226 if (minlen > 15)
2227 memcpy(rbuf + 8, sat_blk_desc,
2228 sizeof(sat_blk_desc));
2229 }
1da177e4 2230 }
1da177e4 2231 return 0;
ae006510
DG
2232
2233invalid_fld:
2234 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
2235 /* "Invalid field in cbd" */
2236 return 1;
2237
2238saving_not_supp:
2239 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
2240 /* "Saving parameters not supported" */
2241 return 1;
1da177e4
LT
2242}
2243
2244/**
2245 * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands
2246 * @args: device IDENTIFY data / SCSI command of interest.
2247 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2248 * @buflen: Response buffer length.
2249 *
2250 * Simulate READ CAPACITY commands.
2251 *
2252 * LOCKING:
6a36261e 2253 * None.
1da177e4 2254 */
1da177e4 2255unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
2dcb407e 2256 unsigned int buflen)
1da177e4 2257{
6a36261e 2258 u64 last_lba = args->dev->n_sectors - 1; /* LBA of the last block */
1da177e4
LT
2259
2260 VPRINTK("ENTER\n");
2261
1da177e4 2262 if (args->cmd->cmnd[0] == READ_CAPACITY) {
6a36261e
TH
2263 if (last_lba >= 0xffffffffULL)
2264 last_lba = 0xffffffff;
0c144d0d 2265
1da177e4 2266 /* sector count, 32-bit */
6a36261e
TH
2267 ATA_SCSI_RBUF_SET(0, last_lba >> (8 * 3));
2268 ATA_SCSI_RBUF_SET(1, last_lba >> (8 * 2));
2269 ATA_SCSI_RBUF_SET(2, last_lba >> (8 * 1));
2270 ATA_SCSI_RBUF_SET(3, last_lba);
1da177e4
LT
2271
2272 /* sector size */
6a36261e 2273 ATA_SCSI_RBUF_SET(6, ATA_SECT_SIZE >> 8);
4ca4e439 2274 ATA_SCSI_RBUF_SET(7, ATA_SECT_SIZE & 0xff);
1da177e4
LT
2275 } else {
2276 /* sector count, 64-bit */
6a36261e
TH
2277 ATA_SCSI_RBUF_SET(0, last_lba >> (8 * 7));
2278 ATA_SCSI_RBUF_SET(1, last_lba >> (8 * 6));
2279 ATA_SCSI_RBUF_SET(2, last_lba >> (8 * 5));
2280 ATA_SCSI_RBUF_SET(3, last_lba >> (8 * 4));
2281 ATA_SCSI_RBUF_SET(4, last_lba >> (8 * 3));
2282 ATA_SCSI_RBUF_SET(5, last_lba >> (8 * 2));
2283 ATA_SCSI_RBUF_SET(6, last_lba >> (8 * 1));
2284 ATA_SCSI_RBUF_SET(7, last_lba);
1da177e4
LT
2285
2286 /* sector size */
6a36261e 2287 ATA_SCSI_RBUF_SET(10, ATA_SECT_SIZE >> 8);
4ca4e439 2288 ATA_SCSI_RBUF_SET(11, ATA_SECT_SIZE & 0xff);
1da177e4
LT
2289 }
2290
2291 return 0;
2292}
2293
2294/**
2295 * ata_scsiop_report_luns - Simulate REPORT LUNS command
2296 * @args: device IDENTIFY data / SCSI command of interest.
2297 * @rbuf: Response buffer, to which simulated SCSI cmd output is sent.
2298 * @buflen: Response buffer length.
2299 *
2300 * Simulate REPORT LUNS command.
2301 *
2302 * LOCKING:
cca3974e 2303 * spin_lock_irqsave(host lock)
1da177e4
LT
2304 */
2305
2306unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
2307 unsigned int buflen)
2308{
2309 VPRINTK("ENTER\n");
2310 rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */
2311
2312 return 0;
2313}
2314
845c5834
DG
2315/**
2316 * ata_scsi_set_sense - Set SCSI sense data and status
2317 * @cmd: SCSI request to be handled
2318 * @sk: SCSI-defined sense key
2319 * @asc: SCSI-defined additional sense code
2320 * @ascq: SCSI-defined additional sense code qualifier
2321 *
2322 * Helper function that builds a valid fixed format, current
2323 * response code and the given sense key (sk), additional sense
2324 * code (asc) and additional sense code qualifier (ascq) with
2325 * a SCSI command status of %SAM_STAT_CHECK_CONDITION and
2326 * DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
2327 *
2328 * LOCKING:
2329 * Not required
2330 */
2331
2332void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
2333{
2334 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
2335
52691764 2336 scsi_build_sense_buffer(0, cmd->sense_buffer, sk, asc, ascq);
845c5834
DG
2337}
2338
1da177e4
LT
2339/**
2340 * ata_scsi_badcmd - End a SCSI request with an error
2341 * @cmd: SCSI request to be handled
2342 * @done: SCSI command completion function
2343 * @asc: SCSI-defined additional sense code
2344 * @ascq: SCSI-defined additional sense code qualifier
2345 *
2346 * Helper function that completes a SCSI command with
2347 * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST
2348 * and the specified additional sense codes.
2349 *
2350 * LOCKING:
cca3974e 2351 * spin_lock_irqsave(host lock)
1da177e4
LT
2352 */
2353
2354void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
2355{
2356 DPRINTK("ENTER\n");
ae006510 2357 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
1da177e4
LT
2358
2359 done(cmd);
2360}
2361
77853bf2 2362static void atapi_sense_complete(struct ata_queued_cmd *qc)
a939c963 2363{
74e6c8c3 2364 if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) {
c6e6e666
JG
2365 /* FIXME: not quite right; we don't want the
2366 * translation of taskfile registers into
2367 * a sense descriptors, since that's only
2368 * correct for ATA, not ATAPI
2369 */
750426aa 2370 ata_gen_passthru_sense(qc);
74e6c8c3 2371 }
a939c963 2372
c6e6e666 2373 qc->scsidone(qc->scsicmd);
77853bf2 2374 ata_qc_free(qc);
c6e6e666 2375}
a939c963 2376
c6e6e666
JG
2377/* is it pointless to prefer PIO for "safety reasons"? */
2378static inline int ata_pio_use_silly(struct ata_port *ap)
2379{
2380 return (ap->flags & ATA_FLAG_PIO_DMA);
2381}
2382
2383static void atapi_request_sense(struct ata_queued_cmd *qc)
2384{
2385 struct ata_port *ap = qc->ap;
2386 struct scsi_cmnd *cmd = qc->scsicmd;
2387
2388 DPRINTK("ATAPI request sense\n");
a939c963
JG
2389
2390 /* FIXME: is this needed? */
7ccd720d 2391 memset(cmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
a939c963 2392
127102ae 2393#ifdef CONFIG_ATA_SFF
855d854a
JB
2394 if (ap->ops->sff_tf_read)
2395 ap->ops->sff_tf_read(ap, &qc->tf);
127102ae 2396#endif
c6e6e666
JG
2397
2398 /* fill these in, for the case where they are -not- overwritten */
2399 cmd->sense_buffer[0] = 0x70;
2400 cmd->sense_buffer[2] = qc->tf.feature >> 4;
2401
2402 ata_qc_reinit(qc);
2403
93f8fecb 2404 /* setup sg table and init transfer direction */
cadb7345 2405 sg_init_one(&qc->sgent, cmd->sense_buffer, SCSI_SENSE_BUFFERSIZE);
93f8fecb 2406 ata_sg_init(qc, &qc->sgent, 1);
a939c963
JG
2407 qc->dma_dir = DMA_FROM_DEVICE;
2408
6e7846e9 2409 memset(&qc->cdb, 0, qc->dev->cdb_len);
a939c963
JG
2410 qc->cdb[0] = REQUEST_SENSE;
2411 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
2412
2413 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
2414 qc->tf.command = ATA_CMD_PACKET;
2415
c6e6e666 2416 if (ata_pio_use_silly(ap)) {
0dc36888 2417 qc->tf.protocol = ATAPI_PROT_DMA;
c6e6e666
JG
2418 qc->tf.feature |= ATAPI_PKT_DMA;
2419 } else {
0dc36888 2420 qc->tf.protocol = ATAPI_PROT_PIO;
2db78dd3
AC
2421 qc->tf.lbam = SCSI_SENSE_BUFFERSIZE;
2422 qc->tf.lbah = 0;
c6e6e666 2423 }
a939c963
JG
2424 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
2425
c6e6e666 2426 qc->complete_fn = atapi_sense_complete;
a939c963 2427
8e0e694a 2428 ata_qc_issue(qc);
a939c963
JG
2429
2430 DPRINTK("EXIT\n");
2431}
2432
77853bf2 2433static void atapi_qc_complete(struct ata_queued_cmd *qc)
1da177e4
LT
2434{
2435 struct scsi_cmnd *cmd = qc->scsicmd;
a22e2eb0 2436 unsigned int err_mask = qc->err_mask;
1da177e4 2437
a7dac447 2438 VPRINTK("ENTER, err_mask 0x%X\n", err_mask);
e12669e7 2439
246619da
TH
2440 /* handle completion from new EH */
2441 if (unlikely(qc->ap->ops->error_handler &&
2442 (err_mask || qc->flags & ATA_QCFLAG_SENSE_VALID))) {
2443
2444 if (!(qc->flags & ATA_QCFLAG_SENSE_VALID)) {
2445 /* FIXME: not quite right; we don't want the
2446 * translation of taskfile registers into a
2447 * sense descriptors, since that's only
2448 * correct for ATA, not ATAPI
2449 */
750426aa 2450 ata_gen_passthru_sense(qc);
246619da
TH
2451 }
2452
22aac089
TH
2453 /* SCSI EH automatically locks door if sdev->locked is
2454 * set. Sometimes door lock request continues to
2455 * fail, for example, when no media is present. This
2456 * creates a loop - SCSI EH issues door lock which
2457 * fails and gets invoked again to acquire sense data
2458 * for the failed command.
2459 *
2460 * If door lock fails, always clear sdev->locked to
2461 * avoid this infinite loop.
2462 */
2463 if (qc->cdb[0] == ALLOW_MEDIUM_REMOVAL)
2464 qc->dev->sdev->locked = 0;
2465
246619da
TH
2466 qc->scsicmd->result = SAM_STAT_CHECK_CONDITION;
2467 qc->scsidone(cmd);
2468 ata_qc_free(qc);
2469 return;
2470 }
2471
2472 /* successful completion or old EH failure path */
a7dac447 2473 if (unlikely(err_mask & AC_ERR_DEV)) {
1da177e4 2474 cmd->result = SAM_STAT_CHECK_CONDITION;
c6e6e666 2475 atapi_request_sense(qc);
77853bf2 2476 return;
74e6c8c3 2477 } else if (unlikely(err_mask)) {
a7dac447
JG
2478 /* FIXME: not quite right; we don't want the
2479 * translation of taskfile registers into
2480 * a sense descriptors, since that's only
2481 * correct for ATA, not ATAPI
2482 */
750426aa 2483 ata_gen_passthru_sense(qc);
74e6c8c3 2484 } else {
1da177e4
LT
2485 u8 *scsicmd = cmd->cmnd;
2486
fd71da46 2487 if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) {
1da177e4
LT
2488 u8 *buf = NULL;
2489 unsigned int buflen;
b445c568
JG
2490 unsigned long flags;
2491
2492 local_irq_save(flags);
1da177e4
LT
2493
2494 buflen = ata_scsi_rbuf_get(cmd, &buf);
a15dbeb4
JG
2495
2496 /* ATAPI devices typically report zero for their SCSI version,
2497 * and sometimes deviate from the spec WRT response data
2498 * format. If SCSI version is reported as zero like normal,
2499 * then we make the following fixups: 1) Fake MMC-5 version,
2500 * to indicate to the Linux scsi midlayer this is a modern
2501 * device. 2) Ensure response data format / ATAPI information
2502 * are always correct.
a15dbeb4
JG
2503 */
2504 if (buf[2] == 0) {
2505 buf[2] = 0x5;
2506 buf[3] = 0x32;
2507 }
2508
1da177e4 2509 ata_scsi_rbuf_put(cmd, buf);
b445c568
JG
2510
2511 local_irq_restore(flags);
1da177e4 2512 }
a15dbeb4 2513
1da177e4
LT
2514 cmd->result = SAM_STAT_GOOD;
2515 }
2516
2517 qc->scsidone(cmd);
77853bf2 2518 ata_qc_free(qc);
1da177e4
LT
2519}
2520/**
2521 * atapi_xlat - Initialize PACKET taskfile
2522 * @qc: command structure to be initialized
1da177e4
LT
2523 *
2524 * LOCKING:
cca3974e 2525 * spin_lock_irqsave(host lock)
1da177e4
LT
2526 *
2527 * RETURNS:
2528 * Zero on success, non-zero on failure.
2529 */
ad706991 2530static unsigned int atapi_xlat(struct ata_queued_cmd *qc)
1da177e4 2531{
542b1444 2532 struct scsi_cmnd *scmd = qc->scsicmd;
1da177e4
LT
2533 struct ata_device *dev = qc->dev;
2534 int using_pio = (dev->flags & ATA_DFLAG_PIO);
542b1444 2535 int nodata = (scmd->sc_data_direction == DMA_NONE);
2db78dd3 2536 unsigned int nbytes;
1da177e4 2537
2e5704f6
TH
2538 memset(qc->cdb, 0, dev->cdb_len);
2539 memcpy(qc->cdb, scmd->cmnd, scmd->cmd_len);
1da177e4
LT
2540
2541 qc->complete_fn = atapi_qc_complete;
2542
2543 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
542b1444 2544 if (scmd->sc_data_direction == DMA_TO_DEVICE) {
1da177e4
LT
2545 qc->tf.flags |= ATA_TFLAG_WRITE;
2546 DPRINTK("direction: write\n");
2547 }
2548
2549 qc->tf.command = ATA_CMD_PACKET;
aacda375 2550 ata_qc_set_pc_nbytes(qc);
e00f1ff3
TH
2551
2552 /* check whether ATAPI DMA is safe */
2553 if (!using_pio && ata_check_atapi_dma(qc))
2554 using_pio = 1;
1da177e4 2555
e190222d
TH
2556 /* Some controller variants snoop this value for Packet
2557 * transfers to do state machine and FIFO management. Thus we
2558 * want to set it properly, and for DMA where it is
2559 * effectively meaningless.
2560 */
aacda375 2561 nbytes = min(ata_qc_raw_nbytes(qc), (unsigned int)63 * 1024);
2db78dd3 2562
e190222d
TH
2563 /* Most ATAPI devices which honor transfer chunk size don't
2564 * behave according to the spec when odd chunk size which
2565 * matches the transfer length is specified. If the number of
2566 * bytes to transfer is 2n+1. According to the spec, what
2567 * should happen is to indicate that 2n+1 is going to be
2568 * transferred and transfer 2n+2 bytes where the last byte is
2569 * padding.
2570 *
2571 * In practice, this doesn't happen. ATAPI devices first
2572 * indicate and transfer 2n bytes and then indicate and
2573 * transfer 2 bytes where the last byte is padding.
2574 *
2575 * This inconsistency confuses several controllers which
2576 * perform PIO using DMA such as Intel AHCIs and sil3124/32.
2577 * These controllers use actual number of transferred bytes to
2578 * update DMA poitner and transfer of 4n+2 bytes make those
2579 * controller push DMA pointer by 4n+4 bytes because SATA data
2580 * FISes are aligned to 4 bytes. This causes data corruption
2581 * and buffer overrun.
2582 *
2583 * Always setting nbytes to even number solves this problem
2584 * because then ATAPI devices don't have to split data at 2n
2585 * boundaries.
2586 */
2587 if (nbytes & 0x1)
2588 nbytes++;
2589
2db78dd3
AC
2590 qc->tf.lbam = (nbytes & 0xFF);
2591 qc->tf.lbah = (nbytes >> 8);
2592
1da177e4 2593 if (using_pio || nodata) {
e00f1ff3 2594 /* no data, or PIO data xfer */
1da177e4 2595 if (nodata)
0dc36888 2596 qc->tf.protocol = ATAPI_PROT_NODATA;
1da177e4 2597 else
0dc36888 2598 qc->tf.protocol = ATAPI_PROT_PIO;
e00f1ff3
TH
2599 } else {
2600 /* DMA data xfer */
0dc36888 2601 qc->tf.protocol = ATAPI_PROT_DMA;
1da177e4
LT
2602 qc->tf.feature |= ATAPI_PKT_DMA;
2603
91163006
TH
2604 if ((dev->flags & ATA_DFLAG_DMADIR) &&
2605 (scmd->sc_data_direction != DMA_TO_DEVICE))
95de719a 2606 /* some SATA bridges need us to indicate data xfer direction */
1da177e4 2607 qc->tf.feature |= ATAPI_DMADIR;
1da177e4
LT
2608 }
2609
2db78dd3
AC
2610
2611 /* FIXME: We need to translate 0x05 READ_BLOCK_LIMITS to a MODE_SENSE
2612 as ATAPI tape drives don't get this right otherwise */
1da177e4
LT
2613 return 0;
2614}
2615
2dcb407e 2616static struct ata_device *ata_find_dev(struct ata_port *ap, int devno)
ab5b3a5b 2617{
071f44b1 2618 if (!sata_pmp_attached(ap)) {
41bda9c9
TH
2619 if (likely(devno < ata_link_max_devices(&ap->link)))
2620 return &ap->link.device[devno];
2621 } else {
2622 if (likely(devno < ap->nr_pmp_links))
2623 return &ap->pmp_link[devno].device[0];
2624 }
2625
ab5b3a5b
TH
2626 return NULL;
2627}
2628
2dcb407e
JG
2629static struct ata_device *__ata_scsi_find_dev(struct ata_port *ap,
2630 const struct scsi_device *scsidev)
ab5b3a5b 2631{
41bda9c9
TH
2632 int devno;
2633
ab5b3a5b 2634 /* skip commands not addressed to targets we simulate */
071f44b1 2635 if (!sata_pmp_attached(ap)) {
41bda9c9
TH
2636 if (unlikely(scsidev->channel || scsidev->lun))
2637 return NULL;
2638 devno = scsidev->id;
2639 } else {
2640 if (unlikely(scsidev->id || scsidev->lun))
2641 return NULL;
2642 devno = scsidev->channel;
2643 }
ab5b3a5b 2644
41bda9c9 2645 return ata_find_dev(ap, devno);
ab5b3a5b
TH
2646}
2647
99ba9e09
BK
2648/**
2649 * ata_scsi_dev_enabled - determine if device is enabled
2650 * @dev: ATA device
2651 *
2652 * Determine if commands should be sent to the specified device.
2653 *
2654 * LOCKING:
cca3974e 2655 * spin_lock_irqsave(host lock)
99ba9e09
BK
2656 *
2657 * RETURNS:
2658 * 0 if commands are not allowed / 1 if commands are allowed
2659 */
2660
2661static int ata_scsi_dev_enabled(struct ata_device *dev)
2662{
2663 if (unlikely(!ata_dev_enabled(dev)))
2664 return 0;
2665
9af5c9c9 2666 if (!atapi_enabled || (dev->link->ap->flags & ATA_FLAG_NO_ATAPI)) {
99ba9e09
BK
2667 if (unlikely(dev->class == ATA_DEV_ATAPI)) {
2668 ata_dev_printk(dev, KERN_WARNING,
2669 "WARNING: ATAPI is %s, device ignored.\n",
2670 atapi_enabled ? "not supported with this driver" : "disabled");
2671 return 0;
2672 }
2673 }
2674
2675 return 1;
2676}
2677
1da177e4
LT
2678/**
2679 * ata_scsi_find_dev - lookup ata_device from scsi_cmnd
2680 * @ap: ATA port to which the device is attached
2681 * @scsidev: SCSI device from which we derive the ATA device
2682 *
2683 * Given various information provided in struct scsi_cmnd,
2684 * map that onto an ATA bus, and using that mapping
2685 * determine which ata_device is associated with the
2686 * SCSI command to be sent.
2687 *
2688 * LOCKING:
cca3974e 2689 * spin_lock_irqsave(host lock)
1da177e4
LT
2690 *
2691 * RETURNS:
2692 * Associated ATA device, or %NULL if not found.
2693 */
1da177e4 2694static struct ata_device *
057ace5e 2695ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev)
1da177e4 2696{
ab5b3a5b 2697 struct ata_device *dev = __ata_scsi_find_dev(ap, scsidev);
1da177e4 2698
99ba9e09 2699 if (unlikely(!dev || !ata_scsi_dev_enabled(dev)))
1da177e4
LT
2700 return NULL;
2701
1da177e4
LT
2702 return dev;
2703}
2704
b095518e
JG
2705/*
2706 * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value.
2707 * @byte1: Byte 1 from pass-thru CDB.
2708 *
2709 * RETURNS:
2710 * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise.
2711 */
2712static u8
2713ata_scsi_map_proto(u8 byte1)
2714{
2715 switch((byte1 & 0x1e) >> 1) {
2dcb407e
JG
2716 case 3: /* Non-data */
2717 return ATA_PROT_NODATA;
2718
2719 case 6: /* DMA */
2720 case 10: /* UDMA Data-in */
2721 case 11: /* UDMA Data-Out */
2722 return ATA_PROT_DMA;
2723
2724 case 4: /* PIO Data-in */
2725 case 5: /* PIO Data-out */
2726 return ATA_PROT_PIO;
2727
2728 case 0: /* Hard Reset */
2729 case 1: /* SRST */
2730 case 8: /* Device Diagnostic */
2731 case 9: /* Device Reset */
2732 case 7: /* DMA Queued */
2733 case 12: /* FPDMA */
2734 case 15: /* Return Response Info */
2735 default: /* Reserved */
2736 break;
b095518e
JG
2737 }
2738
2739 return ATA_PROT_UNKNOWN;
2740}
2741
2742/**
2743 * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile
2744 * @qc: command structure to be initialized
b095518e
JG
2745 *
2746 * Handles either 12 or 16-byte versions of the CDB.
2747 *
2748 * RETURNS:
2749 * Zero on success, non-zero on failure.
2750 */
ad706991 2751static unsigned int ata_scsi_pass_thru(struct ata_queued_cmd *qc)
b095518e
JG
2752{
2753 struct ata_taskfile *tf = &(qc->tf);
542b1444 2754 struct scsi_cmnd *scmd = qc->scsicmd;
f79d409f 2755 struct ata_device *dev = qc->dev;
ad706991 2756 const u8 *cdb = scmd->cmnd;
b095518e 2757
542b1444 2758 if ((tf->protocol = ata_scsi_map_proto(cdb[1])) == ATA_PROT_UNKNOWN)
9a405257 2759 goto invalid_fld;
8190bdb9 2760
ae8d4ee7
AC
2761 /*
2762 * Filter TPM commands by default. These provide an
2763 * essentially uncontrolled encrypted "back door" between
2764 * applications and the disk. Set libata.allow_tpm=1 if you
2765 * have a real reason for wanting to use them. This ensures
2766 * that installed software cannot easily mess stuff up without
2767 * user intent. DVR type users will probably ship with this enabled
2768 * for movie content management.
2769 *
2770 * Note that for ATA8 we can issue a DCS change and DCS freeze lock
2771 * for this and should do in future but that it is not sufficient as
2772 * DCS is an optional feature set. Thus we also do the software filter
2773 * so that we comply with the TC consortium stated goal that the user
2774 * can turn off TC features of their system.
2775 */
2776 if (tf->command >= 0x5C && tf->command <= 0x5F && !libata_allow_tpm)
2777 goto invalid_fld;
2778
f79d409f
AC
2779 /* We may not issue DMA commands if no DMA mode is set */
2780 if (tf->protocol == ATA_PROT_DMA && dev->dma_mode == 0)
2781 goto invalid_fld;
b095518e
JG
2782
2783 /*
2784 * 12 and 16 byte CDBs use different offsets to
2785 * provide the various register values.
2786 */
542b1444 2787 if (cdb[0] == ATA_16) {
b095518e
JG
2788 /*
2789 * 16-byte CDB - may contain extended commands.
2790 *
2791 * If that is the case, copy the upper byte register values.
2792 */
542b1444
TH
2793 if (cdb[1] & 0x01) {
2794 tf->hob_feature = cdb[3];
2795 tf->hob_nsect = cdb[5];
2796 tf->hob_lbal = cdb[7];
2797 tf->hob_lbam = cdb[9];
2798 tf->hob_lbah = cdb[11];
b095518e
JG
2799 tf->flags |= ATA_TFLAG_LBA48;
2800 } else
2801 tf->flags &= ~ATA_TFLAG_LBA48;
2802
2803 /*
2804 * Always copy low byte, device and command registers.
2805 */
542b1444
TH
2806 tf->feature = cdb[4];
2807 tf->nsect = cdb[6];
2808 tf->lbal = cdb[8];
2809 tf->lbam = cdb[10];
2810 tf->lbah = cdb[12];
2811 tf->device = cdb[13];
2812 tf->command = cdb[14];
b095518e
JG
2813 } else {
2814 /*
2815 * 12-byte CDB - incapable of extended commands.
2816 */
2817 tf->flags &= ~ATA_TFLAG_LBA48;
2818
542b1444
TH
2819 tf->feature = cdb[3];
2820 tf->nsect = cdb[4];
2821 tf->lbal = cdb[5];
2822 tf->lbam = cdb[6];
2823 tf->lbah = cdb[7];
2824 tf->device = cdb[8];
2825 tf->command = cdb[9];
b095518e 2826 }
fa4453c4
AL
2827
2828 /* enforce correct master/slave bit */
2829 tf->device = dev->devno ?
2830 tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1;
b095518e 2831
1dce589c
AL
2832 /* sanity check for pio multi commands */
2833 if ((cdb[1] & 0xe0) && !is_multi_taskfile(tf))
2834 goto invalid_fld;
2835
2836 if (is_multi_taskfile(tf)) {
2837 unsigned int multi_count = 1 << (cdb[1] >> 5);
2838
2839 /* compare the passed through multi_count
2840 * with the cached multi_count of libata
2841 */
2842 if (multi_count != dev->multi_count)
2843 ata_dev_printk(dev, KERN_WARNING,
2844 "invalid multi_count %u ignored\n",
2845 multi_count);
d26fc955 2846 }
1dce589c 2847
5a5dbd18
ML
2848 /* READ/WRITE LONG use a non-standard sect_size */
2849 qc->sect_size = ATA_SECT_SIZE;
2850 switch (tf->command) {
2851 case ATA_CMD_READ_LONG:
2852 case ATA_CMD_READ_LONG_ONCE:
2853 case ATA_CMD_WRITE_LONG:
2854 case ATA_CMD_WRITE_LONG_ONCE:
2855 if (tf->protocol != ATA_PROT_PIO || tf->nsect != 1)
2856 goto invalid_fld;
7120165c 2857 qc->sect_size = scsi_bufflen(scmd);
5a5dbd18
ML
2858 }
2859
b095518e
JG
2860 /*
2861 * Filter SET_FEATURES - XFER MODE command -- otherwise,
2862 * SET_FEATURES - XFER MODE must be preceded/succeeded
2863 * by an update to hardware-specific registers for each
2864 * controller (i.e. the reason for ->set_piomode(),
2865 * ->set_dmamode(), and ->post_set_mode() hooks).
2866 */
2867 if ((tf->command == ATA_CMD_SET_FEATURES)
2868 && (tf->feature == SETFEATURES_XFER))
9a405257 2869 goto invalid_fld;
b095518e
JG
2870
2871 /*
2872 * Set flags so that all registers will be written,
2873 * and pass on write indication (used for PIO/DMA
2874 * setup.)
2875 */
2876 tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE);
2877
542b1444 2878 if (scmd->sc_data_direction == DMA_TO_DEVICE)
b095518e
JG
2879 tf->flags |= ATA_TFLAG_WRITE;
2880
2881 /*
2882 * Set transfer length.
2883 *
2884 * TODO: find out if we need to do more here to
2885 * cover scatter/gather case.
2886 */
aacda375 2887 ata_qc_set_pc_nbytes(qc);
b095518e 2888
e027bd36
TH
2889 /* request result TF and be quiet about device error */
2890 qc->flags |= ATA_QCFLAG_RESULT_TF | ATA_QCFLAG_QUIET;
e61e0672 2891
b095518e 2892 return 0;
9a405257
TH
2893
2894 invalid_fld:
542b1444 2895 ata_scsi_set_sense(scmd, ILLEGAL_REQUEST, 0x24, 0x00);
9a405257
TH
2896 /* "Invalid field in cdb" */
2897 return 1;
b095518e
JG
2898}
2899
1da177e4
LT
2900/**
2901 * ata_get_xlat_func - check if SCSI to ATA translation is possible
2902 * @dev: ATA device
2903 * @cmd: SCSI command opcode to consider
2904 *
2905 * Look up the SCSI command given, and determine whether the
2906 * SCSI command is to be translated or simulated.
2907 *
2908 * RETURNS:
2909 * Pointer to translation function if possible, %NULL if not.
2910 */
2911
2912static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd)
2913{
2914 switch (cmd) {
2915 case READ_6:
2916 case READ_10:
2917 case READ_16:
2918
2919 case WRITE_6:
2920 case WRITE_10:
2921 case WRITE_16:
2922 return ata_scsi_rw_xlat;
2923
2924 case SYNCHRONIZE_CACHE:
2925 if (ata_try_flush_cache(dev))
2926 return ata_scsi_flush_xlat;
2927 break;
2928
2929 case VERIFY:
2930 case VERIFY_16:
2931 return ata_scsi_verify_xlat;
b095518e
JG
2932
2933 case ATA_12:
2934 case ATA_16:
2935 return ata_scsi_pass_thru;
da61396d 2936
972dcafb
DG
2937 case START_STOP:
2938 return ata_scsi_start_stop_xlat;
1da177e4
LT
2939 }
2940
2941 return NULL;
2942}
2943
2944/**
2945 * ata_scsi_dump_cdb - dump SCSI command contents to dmesg
2946 * @ap: ATA port to which the command was being sent
2947 * @cmd: SCSI command to dump
2948 *
2949 * Prints the contents of a SCSI command via printk().
2950 */
2951
2952static inline void ata_scsi_dump_cdb(struct ata_port *ap,
2953 struct scsi_cmnd *cmd)
2954{
2955#ifdef ATA_DEBUG
2956 struct scsi_device *scsidev = cmd->device;
2957 u8 *scsicmd = cmd->cmnd;
2958
2959 DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n",
44877b4e 2960 ap->print_id,
1da177e4
LT
2961 scsidev->channel, scsidev->id, scsidev->lun,
2962 scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3],
2963 scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7],
2964 scsicmd[8]);
2965#endif
2966}
2967
542b1444 2968static inline int __ata_scsi_queuecmd(struct scsi_cmnd *scmd,
2115ea94
TH
2969 void (*done)(struct scsi_cmnd *),
2970 struct ata_device *dev)
eb3f0f9c 2971{
baf4fdfa
ML
2972 u8 scsi_op = scmd->cmnd[0];
2973 ata_xlat_func_t xlat_func;
2115ea94
TH
2974 int rc = 0;
2975
eb3f0f9c 2976 if (dev->class == ATA_DEV_ATA) {
baf4fdfa
ML
2977 if (unlikely(!scmd->cmd_len || scmd->cmd_len > dev->cdb_len))
2978 goto bad_cdb_len;
eb3f0f9c 2979
baf4fdfa
ML
2980 xlat_func = ata_get_xlat_func(dev, scsi_op);
2981 } else {
2982 if (unlikely(!scmd->cmd_len))
2983 goto bad_cdb_len;
2984
2985 xlat_func = NULL;
2986 if (likely((scsi_op != ATA_16) || !atapi_passthru16)) {
2987 /* relay SCSI command to ATAPI device */
607126c2
ML
2988 int len = COMMAND_SIZE(scsi_op);
2989 if (unlikely(len > scmd->cmd_len || len > dev->cdb_len))
baf4fdfa
ML
2990 goto bad_cdb_len;
2991
2992 xlat_func = atapi_xlat;
2993 } else {
2994 /* ATA_16 passthru, treat as an ATA command */
2995 if (unlikely(scmd->cmd_len > 16))
2996 goto bad_cdb_len;
2997
2998 xlat_func = ata_get_xlat_func(dev, scsi_op);
2999 }
3000 }
3001
3002 if (xlat_func)
3003 rc = ata_scsi_translate(dev, scmd, done, xlat_func);
3004 else
3005 ata_scsi_simulate(dev, scmd, done);
2115ea94
TH
3006
3007 return rc;
baf4fdfa
ML
3008
3009 bad_cdb_len:
3010 DPRINTK("bad CDB len=%u, scsi_op=0x%02x, max=%u\n",
3011 scmd->cmd_len, scsi_op, dev->cdb_len);
3012 scmd->result = DID_ERROR << 16;
3013 done(scmd);
3014 return 0;
eb3f0f9c
BK
3015}
3016
1da177e4
LT
3017/**
3018 * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device
3019 * @cmd: SCSI command to be sent
3020 * @done: Completion function, called when command is complete
3021 *
3022 * In some cases, this function translates SCSI commands into
3023 * ATA taskfiles, and queues the taskfiles to be sent to
3024 * hardware. In other cases, this function simulates a
3025 * SCSI device by evaluating and responding to certain
3026 * SCSI commands. This creates the overall effect of
3027 * ATA and ATAPI devices appearing as SCSI devices.
3028 *
3029 * LOCKING:
cca3974e 3030 * Releases scsi-layer-held lock, and obtains host lock.
1da177e4
LT
3031 *
3032 * RETURNS:
2115ea94
TH
3033 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
3034 * 0 otherwise.
1da177e4 3035 */
1da177e4
LT
3036int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *))
3037{
3038 struct ata_port *ap;
3039 struct ata_device *dev;
3040 struct scsi_device *scsidev = cmd->device;
005a5a06 3041 struct Scsi_Host *shost = scsidev->host;
2115ea94 3042 int rc = 0;
1da177e4 3043
35bb94b1 3044 ap = ata_shost_to_port(shost);
005a5a06
JG
3045
3046 spin_unlock(shost->host_lock);
ba6a1308 3047 spin_lock(ap->lock);
1da177e4
LT
3048
3049 ata_scsi_dump_cdb(ap, cmd);
3050
3051 dev = ata_scsi_find_dev(ap, scsidev);
eb3f0f9c 3052 if (likely(dev))
2115ea94 3053 rc = __ata_scsi_queuecmd(cmd, done, dev);
eb3f0f9c 3054 else {
1da177e4
LT
3055 cmd->result = (DID_BAD_TARGET << 16);
3056 done(cmd);
1da177e4
LT
3057 }
3058
ba6a1308 3059 spin_unlock(ap->lock);
005a5a06 3060 spin_lock(shost->host_lock);
2115ea94 3061 return rc;
1da177e4
LT
3062}
3063
3064/**
3065 * ata_scsi_simulate - simulate SCSI command on ATA device
c893a3ae 3066 * @dev: the target device
1da177e4
LT
3067 * @cmd: SCSI command being sent to device.
3068 * @done: SCSI command completion function.
3069 *
3070 * Interprets and directly executes a select list of SCSI commands
3071 * that can be handled internally.
3072 *
3073 * LOCKING:
cca3974e 3074 * spin_lock_irqsave(host lock)
1da177e4
LT
3075 */
3076
3373efd8 3077void ata_scsi_simulate(struct ata_device *dev, struct scsi_cmnd *cmd,
1da177e4
LT
3078 void (*done)(struct scsi_cmnd *))
3079{
3080 struct ata_scsi_args args;
057ace5e 3081 const u8 *scsicmd = cmd->cmnd;
45394145 3082 u8 tmp8;
1da177e4 3083
9a3dccc4
TH
3084 args.dev = dev;
3085 args.id = dev->id;
1da177e4
LT
3086 args.cmd = cmd;
3087 args.done = done;
3088
3089 switch(scsicmd[0]) {
2dcb407e
JG
3090 /* TODO: worth improving? */
3091 case FORMAT_UNIT:
3092 ata_scsi_invalid_field(cmd, done);
3093 break;
3094
3095 case INQUIRY:
3096 if (scsicmd[1] & 2) /* is CmdDt set? */
00bd0202 3097 ata_scsi_invalid_field(cmd, done);
2dcb407e
JG
3098 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
3099 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
3100 else switch (scsicmd[2]) {
3101 case 0x00:
3102 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00);
1da177e4 3103 break;
2dcb407e
JG
3104 case 0x80:
3105 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80);
1da177e4 3106 break;
2dcb407e
JG
3107 case 0x83:
3108 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
1da177e4 3109 break;
2dcb407e
JG
3110 case 0x89:
3111 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_89);
3112 break;
3113 default:
ae006510 3114 ata_scsi_invalid_field(cmd, done);
1da177e4 3115 break;
2dcb407e
JG
3116 }
3117 break;
3118
3119 case MODE_SENSE:
3120 case MODE_SENSE_10:
3121 ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense);
3122 break;
3123
3124 case MODE_SELECT: /* unconditionally return */
3125 case MODE_SELECT_10: /* bad-field-in-cdb */
3126 ata_scsi_invalid_field(cmd, done);
3127 break;
1da177e4 3128
2dcb407e
JG
3129 case READ_CAPACITY:
3130 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
3131 break;
3132
3133 case SERVICE_ACTION_IN:
3134 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
1da177e4 3135 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2dcb407e
JG
3136 else
3137 ata_scsi_invalid_field(cmd, done);
3138 break;
1da177e4 3139
2dcb407e
JG
3140 case REPORT_LUNS:
3141 ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns);
3142 break;
1da177e4 3143
2dcb407e
JG
3144 case REQUEST_SENSE:
3145 ata_scsi_set_sense(cmd, 0, 0, 0);
3146 cmd->result = (DRIVER_SENSE << 24);
3147 done(cmd);
3148 break;
1da177e4 3149
2dcb407e
JG
3150 /* if we reach this, then writeback caching is disabled,
3151 * turning this into a no-op.
3152 */
3153 case SYNCHRONIZE_CACHE:
3154 /* fall through */
3155
3156 /* no-op's, complete with success */
3157 case REZERO_UNIT:
3158 case SEEK_6:
3159 case SEEK_10:
3160 case TEST_UNIT_READY:
3161 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
3162 break;
45394145 3163
2dcb407e
JG
3164 case SEND_DIAGNOSTIC:
3165 tmp8 = scsicmd[1] & ~(1 << 3);
3166 if ((tmp8 == 0x4) && (!scsicmd[3]) && (!scsicmd[4]))
00bd0202 3167 ata_scsi_rbuf_fill(&args, ata_scsiop_noop);
2dcb407e
JG
3168 else
3169 ata_scsi_invalid_field(cmd, done);
3170 break;
1da177e4 3171
2dcb407e
JG
3172 /* all other commands */
3173 default:
3174 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
3175 /* "Invalid command operation code" */
3176 done(cmd);
3177 break;
1da177e4
LT
3178 }
3179}
3180
f3187195
TH
3181int ata_scsi_add_hosts(struct ata_host *host, struct scsi_host_template *sht)
3182{
3183 int i, rc;
3184
3185 for (i = 0; i < host->n_ports; i++) {
3186 struct ata_port *ap = host->ports[i];
3187 struct Scsi_Host *shost;
3188
3189 rc = -ENOMEM;
3190 shost = scsi_host_alloc(sht, sizeof(struct ata_port *));
3191 if (!shost)
3192 goto err_alloc;
3193
3194 *(struct ata_port **)&shost->hostdata[0] = ap;
3195 ap->scsi_host = shost;
3196
3197 shost->transportt = &ata_scsi_transport_template;
3198 shost->unique_id = ap->print_id;
3199 shost->max_id = 16;
3200 shost->max_lun = 1;
3201 shost->max_channel = 1;
3202 shost->max_cmd_len = 16;
3203
31cc23b3
TH
3204 /* Schedule policy is determined by ->qc_defer()
3205 * callback and it needs to see every deferred qc.
3206 * Set host_blocked to 1 to prevent SCSI midlayer from
3207 * automatically deferring requests.
3208 */
3209 shost->max_host_blocked = 1;
3210
f3187195
TH
3211 rc = scsi_add_host(ap->scsi_host, ap->host->dev);
3212 if (rc)
3213 goto err_add;
3214 }
3215
3216 return 0;
3217
3218 err_add:
3219 scsi_host_put(host->ports[i]->scsi_host);
3220 err_alloc:
3221 while (--i >= 0) {
3222 struct Scsi_Host *shost = host->ports[i]->scsi_host;
3223
3224 scsi_remove_host(shost);
3225 scsi_host_put(shost);
3226 }
3227 return rc;
3228}
3229
1ae46317 3230void ata_scsi_scan_host(struct ata_port *ap, int sync)
644dd0cc 3231{
1ae46317
TH
3232 int tries = 5;
3233 struct ata_device *last_failed_dev = NULL;
41bda9c9 3234 struct ata_link *link;
1ae46317 3235 struct ata_device *dev;
644dd0cc 3236
198e0fed 3237 if (ap->flags & ATA_FLAG_DISABLED)
644dd0cc
JG
3238 return;
3239
1ae46317 3240 repeat:
41bda9c9
TH
3241 ata_port_for_each_link(link, ap) {
3242 ata_link_for_each_dev(dev, link) {
3243 struct scsi_device *sdev;
3244 int channel = 0, id = 0;
3edebac4 3245
41bda9c9
TH
3246 if (!ata_dev_enabled(dev) || dev->sdev)
3247 continue;
3f19ee8c 3248
41bda9c9
TH
3249 if (ata_is_host_link(link))
3250 id = dev->devno;
3251 else
3252 channel = link->pmp;
3253
3254 sdev = __scsi_add_device(ap->scsi_host, channel, id, 0,
3255 NULL);
3256 if (!IS_ERR(sdev)) {
3257 dev->sdev = sdev;
3258 scsi_device_put(sdev);
3259 }
3edebac4 3260 }
3f19ee8c 3261 }
1ae46317
TH
3262
3263 /* If we scanned while EH was in progress or allocation
3264 * failure occurred, scan would have failed silently. Check
3265 * whether all devices are attached.
3266 */
41bda9c9
TH
3267 ata_port_for_each_link(link, ap) {
3268 ata_link_for_each_dev(dev, link) {
3269 if (ata_dev_enabled(dev) && !dev->sdev)
3270 goto exit_loop;
3271 }
1ae46317 3272 }
41bda9c9
TH
3273 exit_loop:
3274 if (!link)
1ae46317
TH
3275 return;
3276
3277 /* we're missing some SCSI devices */
3278 if (sync) {
3279 /* If caller requested synchrnous scan && we've made
3280 * any progress, sleep briefly and repeat.
3281 */
3282 if (dev != last_failed_dev) {
3283 msleep(100);
3284 last_failed_dev = dev;
3285 goto repeat;
3286 }
3287
3288 /* We might be failing to detect boot device, give it
3289 * a few more chances.
3290 */
3291 if (--tries) {
3292 msleep(100);
3293 goto repeat;
3294 }
3295
3296 ata_port_printk(ap, KERN_ERR, "WARNING: synchronous SCSI scan "
3297 "failed without making any progress,\n"
3298 " switching to async\n");
3299 }
3300
3301 queue_delayed_work(ata_aux_wq, &ap->hotplug_task,
3302 round_jiffies_relative(HZ));
644dd0cc 3303}
0ea035a3
TH
3304
3305/**
3306 * ata_scsi_offline_dev - offline attached SCSI device
3307 * @dev: ATA device to offline attached SCSI device for
3308 *
3309 * This function is called from ata_eh_hotplug() and responsible
3310 * for taking the SCSI device attached to @dev offline. This
cca3974e 3311 * function is called with host lock which protects dev->sdev
0ea035a3
TH
3312 * against clearing.
3313 *
3314 * LOCKING:
cca3974e 3315 * spin_lock_irqsave(host lock)
0ea035a3
TH
3316 *
3317 * RETURNS:
3318 * 1 if attached SCSI device exists, 0 otherwise.
3319 */
3320int ata_scsi_offline_dev(struct ata_device *dev)
3321{
3322 if (dev->sdev) {
3323 scsi_device_set_state(dev->sdev, SDEV_OFFLINE);
3324 return 1;
3325 }
3326 return 0;
3327}
580b2102
TH
3328
3329/**
3330 * ata_scsi_remove_dev - remove attached SCSI device
3331 * @dev: ATA device to remove attached SCSI device for
3332 *
3333 * This function is called from ata_eh_scsi_hotplug() and
3334 * responsible for removing the SCSI device attached to @dev.
3335 *
3336 * LOCKING:
3337 * Kernel thread context (may sleep).
3338 */
3339static void ata_scsi_remove_dev(struct ata_device *dev)
3340{
9af5c9c9 3341 struct ata_port *ap = dev->link->ap;
580b2102
TH
3342 struct scsi_device *sdev;
3343 unsigned long flags;
3344
3345 /* Alas, we need to grab scan_mutex to ensure SCSI device
3346 * state doesn't change underneath us and thus
3347 * scsi_device_get() always succeeds. The mutex locking can
3348 * be removed if there is __scsi_device_get() interface which
3349 * increments reference counts regardless of device state.
3350 */
cca3974e 3351 mutex_lock(&ap->scsi_host->scan_mutex);
ba6a1308 3352 spin_lock_irqsave(ap->lock, flags);
580b2102 3353
cca3974e 3354 /* clearing dev->sdev is protected by host lock */
580b2102
TH
3355 sdev = dev->sdev;
3356 dev->sdev = NULL;
3357
3358 if (sdev) {
3359 /* If user initiated unplug races with us, sdev can go
cca3974e 3360 * away underneath us after the host lock and
580b2102
TH
3361 * scan_mutex are released. Hold onto it.
3362 */
3363 if (scsi_device_get(sdev) == 0) {
3364 /* The following ensures the attached sdev is
3365 * offline on return from ata_scsi_offline_dev()
3366 * regardless it wins or loses the race
3367 * against this function.
3368 */
3369 scsi_device_set_state(sdev, SDEV_OFFLINE);
3370 } else {
3371 WARN_ON(1);
3372 sdev = NULL;
3373 }
3374 }
3375
ba6a1308 3376 spin_unlock_irqrestore(ap->lock, flags);
cca3974e 3377 mutex_unlock(&ap->scsi_host->scan_mutex);
580b2102
TH
3378
3379 if (sdev) {
3380 ata_dev_printk(dev, KERN_INFO, "detaching (SCSI %s)\n",
3381 sdev->sdev_gendev.bus_id);
3382
3383 scsi_remove_device(sdev);
3384 scsi_device_put(sdev);
3385 }
3386}
3387
41bda9c9
TH
3388static void ata_scsi_handle_link_detach(struct ata_link *link)
3389{
3390 struct ata_port *ap = link->ap;
3391 struct ata_device *dev;
3392
3393 ata_link_for_each_dev(dev, link) {
3394 unsigned long flags;
3395
3396 if (!(dev->flags & ATA_DFLAG_DETACHED))
3397 continue;
3398
3399 spin_lock_irqsave(ap->lock, flags);
3400 dev->flags &= ~ATA_DFLAG_DETACHED;
3401 spin_unlock_irqrestore(ap->lock, flags);
3402
3403 ata_scsi_remove_dev(dev);
3404 }
3405}
3406
2f294968
KCA
3407/**
3408 * ata_scsi_media_change_notify - send media change event
c5d0e6a0 3409 * @dev: Pointer to the disk device with media change event
2f294968
KCA
3410 *
3411 * Tell the block layer to send a media change notification
3412 * event.
3413 *
3414 * LOCKING:
854c73a2 3415 * spin_lock_irqsave(host lock)
2f294968 3416 */
854c73a2 3417void ata_scsi_media_change_notify(struct ata_device *dev)
2f294968 3418{
854c73a2 3419 if (dev->sdev)
f26792d5
JG
3420 sdev_evt_send_simple(dev->sdev, SDEV_EVT_MEDIA_CHANGE,
3421 GFP_ATOMIC);
2f294968 3422}
2f294968 3423
580b2102
TH
3424/**
3425 * ata_scsi_hotplug - SCSI part of hotplug
65f27f38 3426 * @work: Pointer to ATA port to perform SCSI hotplug on
580b2102
TH
3427 *
3428 * Perform SCSI part of hotplug. It's executed from a separate
3429 * workqueue after EH completes. This is necessary because SCSI
3430 * hot plugging requires working EH and hot unplugging is
3431 * synchronized with hot plugging with a mutex.
3432 *
3433 * LOCKING:
3434 * Kernel thread context (may sleep).
3435 */
65f27f38 3436void ata_scsi_hotplug(struct work_struct *work)
580b2102 3437{
65f27f38
DH
3438 struct ata_port *ap =
3439 container_of(work, struct ata_port, hotplug_task.work);
41bda9c9 3440 int i;
580b2102 3441
b51e9e5d 3442 if (ap->pflags & ATA_PFLAG_UNLOADING) {
580b2102
TH
3443 DPRINTK("ENTER/EXIT - unloading\n");
3444 return;
3445 }
3446
3447 DPRINTK("ENTER\n");
3448
41bda9c9
TH
3449 /* Unplug detached devices. We cannot use link iterator here
3450 * because PMP links have to be scanned even if PMP is
3451 * currently not attached. Iterate manually.
3452 */
3453 ata_scsi_handle_link_detach(&ap->link);
3454 if (ap->pmp_link)
3455 for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
3456 ata_scsi_handle_link_detach(&ap->pmp_link[i]);
580b2102
TH
3457
3458 /* scan for new ones */
1ae46317 3459 ata_scsi_scan_host(ap, 0);
580b2102
TH
3460
3461 DPRINTK("EXIT\n");
3462}
83c47bcb
TH
3463
3464/**
3465 * ata_scsi_user_scan - indication for user-initiated bus scan
3466 * @shost: SCSI host to scan
3467 * @channel: Channel to scan
3468 * @id: ID to scan
3469 * @lun: LUN to scan
3470 *
3471 * This function is called when user explicitly requests bus
3472 * scan. Set probe pending flag and invoke EH.
3473 *
3474 * LOCKING:
3475 * SCSI layer (we don't care)
3476 *
3477 * RETURNS:
3478 * Zero.
3479 */
3480static int ata_scsi_user_scan(struct Scsi_Host *shost, unsigned int channel,
3481 unsigned int id, unsigned int lun)
3482{
3483 struct ata_port *ap = ata_shost_to_port(shost);
3484 unsigned long flags;
41bda9c9 3485 int devno, rc = 0;
83c47bcb
TH
3486
3487 if (!ap->ops->error_handler)
3488 return -EOPNOTSUPP;
3489
41bda9c9 3490 if (lun != SCAN_WILD_CARD && lun)
83c47bcb
TH
3491 return -EINVAL;
3492
071f44b1 3493 if (!sata_pmp_attached(ap)) {
41bda9c9
TH
3494 if (channel != SCAN_WILD_CARD && channel)
3495 return -EINVAL;
3496 devno = id;
3497 } else {
3498 if (id != SCAN_WILD_CARD && id)
3499 return -EINVAL;
3500 devno = channel;
3501 }
3502
ba6a1308 3503 spin_lock_irqsave(ap->lock, flags);
83c47bcb 3504
41bda9c9
TH
3505 if (devno == SCAN_WILD_CARD) {
3506 struct ata_link *link;
3507
3508 ata_port_for_each_link(link, ap) {
3509 struct ata_eh_info *ehi = &link->eh_info;
b558eddd 3510 ehi->probe_mask |= ATA_ALL_DEVICES;
cf480626 3511 ehi->action |= ATA_EH_RESET;
41bda9c9 3512 }
83c47bcb 3513 } else {
41bda9c9 3514 struct ata_device *dev = ata_find_dev(ap, devno);
83c47bcb
TH
3515
3516 if (dev) {
41bda9c9 3517 struct ata_eh_info *ehi = &dev->link->eh_info;
9af5c9c9 3518 ehi->probe_mask |= 1 << dev->devno;
cf480626 3519 ehi->action |= ATA_EH_RESET;
83c47bcb
TH
3520 } else
3521 rc = -EINVAL;
3522 }
3523
309afcb5 3524 if (rc == 0) {
83c47bcb 3525 ata_port_schedule_eh(ap);
309afcb5
TH
3526 spin_unlock_irqrestore(ap->lock, flags);
3527 ata_port_wait_eh(ap);
3528 } else
3529 spin_unlock_irqrestore(ap->lock, flags);
83c47bcb
TH
3530
3531 return rc;
3532}
3057ac3c 3533
3534/**
d0171269 3535 * ata_scsi_dev_rescan - initiate scsi_rescan_device()
65f27f38 3536 * @work: Pointer to ATA port to perform scsi_rescan_device()
3057ac3c 3537 *
d0171269
TH
3538 * After ATA pass thru (SAT) commands are executed successfully,
3539 * libata need to propagate the changes to SCSI layer. This
3540 * function must be executed from ata_aux_wq such that sdev
3541 * attach/detach don't race with rescan.
3057ac3c 3542 *
d0171269
TH
3543 * LOCKING:
3544 * Kernel thread context (may sleep).
3057ac3c 3545 */
65f27f38 3546void ata_scsi_dev_rescan(struct work_struct *work)
3057ac3c 3547{
65f27f38
DH
3548 struct ata_port *ap =
3549 container_of(work, struct ata_port, scsi_rescan_task);
41bda9c9 3550 struct ata_link *link;
f58229f8 3551 struct ata_device *dev;
f84e7e41 3552 unsigned long flags;
3057ac3c 3553
f84e7e41
TH
3554 spin_lock_irqsave(ap->lock, flags);
3555
41bda9c9
TH
3556 ata_port_for_each_link(link, ap) {
3557 ata_link_for_each_dev(dev, link) {
3558 struct scsi_device *sdev = dev->sdev;
3057ac3c 3559
41bda9c9
TH
3560 if (!ata_dev_enabled(dev) || !sdev)
3561 continue;
3562 if (scsi_device_get(sdev))
3563 continue;
f84e7e41 3564
41bda9c9
TH
3565 spin_unlock_irqrestore(ap->lock, flags);
3566 scsi_rescan_device(&(sdev->sdev_gendev));
3567 scsi_device_put(sdev);
3568 spin_lock_irqsave(ap->lock, flags);
3569 }
3057ac3c 3570 }
f84e7e41
TH
3571
3572 spin_unlock_irqrestore(ap->lock, flags);
3057ac3c 3573}
80289167
BK
3574
3575/**
3576 * ata_sas_port_alloc - Allocate port for a SAS attached SATA device
4f931374 3577 * @host: ATA host container for all SAS ports
80289167 3578 * @port_info: Information from low-level host driver
cca3974e 3579 * @shost: SCSI host that the scsi device is attached to
80289167
BK
3580 *
3581 * LOCKING:
3582 * PCI/etc. bus probe sem.
3583 *
3584 * RETURNS:
3585 * ata_port pointer on success / NULL on failure.
3586 */
3587
cca3974e 3588struct ata_port *ata_sas_port_alloc(struct ata_host *host,
80289167 3589 struct ata_port_info *port_info,
cca3974e 3590 struct Scsi_Host *shost)
80289167 3591{
f3187195 3592 struct ata_port *ap;
80289167 3593
f3187195 3594 ap = ata_port_alloc(host);
80289167
BK
3595 if (!ap)
3596 return NULL;
3597
f3187195 3598 ap->port_no = 0;
cca3974e 3599 ap->lock = shost->host_lock;
f3187195
TH
3600 ap->pio_mask = port_info->pio_mask;
3601 ap->mwdma_mask = port_info->mwdma_mask;
3602 ap->udma_mask = port_info->udma_mask;
3603 ap->flags |= port_info->flags;
3604 ap->ops = port_info->port_ops;
3605 ap->cbl = ATA_CBL_SATA;
3606
80289167
BK
3607 return ap;
3608}
3609EXPORT_SYMBOL_GPL(ata_sas_port_alloc);
3610
3611/**
3612 * ata_sas_port_start - Set port up for dma.
3613 * @ap: Port to initialize
3614 *
3615 * Called just after data structures for each port are
dde20207 3616 * initialized.
80289167
BK
3617 *
3618 * May be used as the port_start() entry in ata_port_operations.
3619 *
3620 * LOCKING:
3621 * Inherited from caller.
3622 */
3623int ata_sas_port_start(struct ata_port *ap)
3624{
dde20207 3625 return 0;
80289167
BK
3626}
3627EXPORT_SYMBOL_GPL(ata_sas_port_start);
3628
3629/**
3630 * ata_port_stop - Undo ata_sas_port_start()
3631 * @ap: Port to shut down
3632 *
80289167
BK
3633 * May be used as the port_stop() entry in ata_port_operations.
3634 *
3635 * LOCKING:
3636 * Inherited from caller.
3637 */
3638
3639void ata_sas_port_stop(struct ata_port *ap)
3640{
80289167
BK
3641}
3642EXPORT_SYMBOL_GPL(ata_sas_port_stop);
3643
3644/**
3645 * ata_sas_port_init - Initialize a SATA device
3646 * @ap: SATA port to initialize
3647 *
3648 * LOCKING:
3649 * PCI/etc. bus probe sem.
3650 *
3651 * RETURNS:
3652 * Zero on success, non-zero on error.
3653 */
3654
3655int ata_sas_port_init(struct ata_port *ap)
3656{
3657 int rc = ap->ops->port_start(ap);
3658
f3187195
TH
3659 if (!rc) {
3660 ap->print_id = ata_print_id++;
80289167 3661 rc = ata_bus_probe(ap);
f3187195 3662 }
80289167
BK
3663
3664 return rc;
3665}
3666EXPORT_SYMBOL_GPL(ata_sas_port_init);
3667
3668/**
3669 * ata_sas_port_destroy - Destroy a SATA port allocated by ata_sas_port_alloc
3670 * @ap: SATA port to destroy
3671 *
3672 */
3673
3674void ata_sas_port_destroy(struct ata_port *ap)
3675{
f0d36efd
TH
3676 if (ap->ops->port_stop)
3677 ap->ops->port_stop(ap);
80289167
BK
3678 kfree(ap);
3679}
3680EXPORT_SYMBOL_GPL(ata_sas_port_destroy);
3681
3682/**
3683 * ata_sas_slave_configure - Default slave_config routine for libata devices
3684 * @sdev: SCSI device to configure
3685 * @ap: ATA port to which SCSI device is attached
3686 *
3687 * RETURNS:
3688 * Zero.
3689 */
3690
3691int ata_sas_slave_configure(struct scsi_device *sdev, struct ata_port *ap)
3692{
3693 ata_scsi_sdev_config(sdev);
9af5c9c9 3694 ata_scsi_dev_config(sdev, ap->link.device);
80289167
BK
3695 return 0;
3696}
3697EXPORT_SYMBOL_GPL(ata_sas_slave_configure);
3698
3699/**
3700 * ata_sas_queuecmd - Issue SCSI cdb to libata-managed device
3701 * @cmd: SCSI command to be sent
3702 * @done: Completion function, called when command is complete
3703 * @ap: ATA port to which the command is being sent
3704 *
3705 * RETURNS:
08475a19
BK
3706 * Return value from __ata_scsi_queuecmd() if @cmd can be queued,
3707 * 0 otherwise.
80289167
BK
3708 */
3709
3710int ata_sas_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *),
3711 struct ata_port *ap)
3712{
08475a19
BK
3713 int rc = 0;
3714
80289167
BK
3715 ata_scsi_dump_cdb(ap, cmd);
3716
9af5c9c9
TH
3717 if (likely(ata_scsi_dev_enabled(ap->link.device)))
3718 rc = __ata_scsi_queuecmd(cmd, done, ap->link.device);
80289167
BK
3719 else {
3720 cmd->result = (DID_BAD_TARGET << 16);
3721 done(cmd);
3722 }
08475a19 3723 return rc;
80289167
BK
3724}
3725EXPORT_SYMBOL_GPL(ata_sas_queuecmd);
This page took 0.754807 seconds and 5 git commands to generate.