]>
Commit | Line | Data |
---|---|---|
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> |
85837ebd | 41 | #include <scsi/scsi_eh.h> |
005a5a06 | 42 | #include <scsi/scsi_device.h> |
193515d5 | 43 | #include <scsi/scsi_request.h> |
30afc84c | 44 | #include <scsi/scsi_transport.h> |
1da177e4 | 45 | #include <linux/libata.h> |
b095518e | 46 | #include <linux/hdreg.h> |
1da177e4 LT |
47 | #include <asm/uaccess.h> |
48 | ||
49 | #include "libata.h" | |
50 | ||
b095518e JG |
51 | #define SECTOR_SIZE 512 |
52 | ||
057ace5e | 53 | typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, const u8 *scsicmd); |
1da177e4 | 54 | static struct ata_device * |
057ace5e | 55 | ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev); |
30afc84c | 56 | enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd); |
1da177e4 | 57 | |
00ac37f5 DG |
58 | #define RW_RECOVERY_MPAGE 0x1 |
59 | #define RW_RECOVERY_MPAGE_LEN 12 | |
60 | #define CACHE_MPAGE 0x8 | |
61 | #define CACHE_MPAGE_LEN 20 | |
62 | #define CONTROL_MPAGE 0xa | |
63 | #define CONTROL_MPAGE_LEN 12 | |
64 | #define ALL_MPAGES 0x3f | |
65 | #define ALL_SUB_MPAGES 0xff | |
66 | ||
67 | ||
68 | static const u8 def_rw_recovery_mpage[] = { | |
69 | RW_RECOVERY_MPAGE, | |
70 | RW_RECOVERY_MPAGE_LEN - 2, | |
71 | (1 << 7) | /* AWRE, sat-r06 say it shall be 0 */ | |
72 | (1 << 6), /* ARRE (auto read reallocation) */ | |
73 | 0, /* read retry count */ | |
74 | 0, 0, 0, 0, | |
75 | 0, /* write retry count */ | |
76 | 0, 0, 0 | |
77 | }; | |
78 | ||
79 | static const u8 def_cache_mpage[CACHE_MPAGE_LEN] = { | |
80 | CACHE_MPAGE, | |
81 | CACHE_MPAGE_LEN - 2, | |
82 | 0, /* contains WCE, needs to be 0 for logic */ | |
83 | 0, 0, 0, 0, 0, 0, 0, 0, 0, | |
84 | 0, /* contains DRA, needs to be 0 for logic */ | |
85 | 0, 0, 0, 0, 0, 0, 0 | |
86 | }; | |
87 | ||
88 | static const u8 def_control_mpage[CONTROL_MPAGE_LEN] = { | |
89 | CONTROL_MPAGE, | |
90 | CONTROL_MPAGE_LEN - 2, | |
91 | 2, /* DSENSE=0, GLTSD=1 */ | |
92 | 0, /* [QAM+QERR may be 1, see 05-359r1] */ | |
93 | 0, 0, 0, 0, 0xff, 0xff, | |
94 | 0, 30 /* extended self test time, see 05-359r1 */ | |
95 | }; | |
96 | ||
30afc84c TH |
97 | /* |
98 | * libata transport template. libata doesn't do real transport stuff. | |
99 | * It just needs the eh_timed_out hook. | |
100 | */ | |
101 | struct scsi_transport_template ata_scsi_transport_template = { | |
102 | .eh_timed_out = ata_scsi_timed_out, | |
103 | }; | |
104 | ||
1da177e4 | 105 | |
ae006510 DG |
106 | static void ata_scsi_invalid_field(struct scsi_cmnd *cmd, |
107 | void (*done)(struct scsi_cmnd *)) | |
108 | { | |
109 | ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0); | |
110 | /* "Invalid field in cbd" */ | |
111 | done(cmd); | |
112 | } | |
113 | ||
1da177e4 LT |
114 | /** |
115 | * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd. | |
116 | * @sdev: SCSI device for which BIOS geometry is to be determined | |
117 | * @bdev: block device associated with @sdev | |
118 | * @capacity: capacity of SCSI device | |
119 | * @geom: location to which geometry will be output | |
120 | * | |
121 | * Generic bios head/sector/cylinder calculator | |
122 | * used by sd. Most BIOSes nowadays expect a XXX/255/16 (CHS) | |
123 | * mapping. Some situations may arise where the disk is not | |
124 | * bootable if this is not used. | |
125 | * | |
126 | * LOCKING: | |
127 | * Defined by the SCSI layer. We don't really care. | |
128 | * | |
129 | * RETURNS: | |
130 | * Zero. | |
131 | */ | |
132 | int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev, | |
133 | sector_t capacity, int geom[]) | |
134 | { | |
135 | geom[0] = 255; | |
136 | geom[1] = 63; | |
137 | sector_div(capacity, 255*63); | |
138 | geom[2] = capacity; | |
139 | ||
140 | return 0; | |
141 | } | |
142 | ||
b095518e JG |
143 | /** |
144 | * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl | |
8e8b77dd | 145 | * @scsidev: Device to which we are issuing command |
b095518e JG |
146 | * @arg: User provided data for issuing command |
147 | * | |
148 | * LOCKING: | |
149 | * Defined by the SCSI layer. We don't really care. | |
150 | * | |
151 | * RETURNS: | |
152 | * Zero on success, negative errno on error. | |
153 | */ | |
154 | ||
155 | int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg) | |
156 | { | |
157 | int rc = 0; | |
158 | u8 scsi_cmd[MAX_COMMAND_SIZE]; | |
159 | u8 args[4], *argbuf = NULL; | |
160 | int argsize = 0; | |
85837ebd MC |
161 | struct scsi_sense_hdr sshdr; |
162 | enum dma_data_direction data_dir; | |
b095518e | 163 | |
c893a3ae | 164 | if (arg == NULL) |
b095518e JG |
165 | return -EINVAL; |
166 | ||
167 | if (copy_from_user(args, arg, sizeof(args))) | |
168 | return -EFAULT; | |
169 | ||
b095518e JG |
170 | memset(scsi_cmd, 0, sizeof(scsi_cmd)); |
171 | ||
172 | if (args[3]) { | |
173 | argsize = SECTOR_SIZE * args[3]; | |
174 | argbuf = kmalloc(argsize, GFP_KERNEL); | |
54dac83c JR |
175 | if (argbuf == NULL) { |
176 | rc = -ENOMEM; | |
177 | goto error; | |
178 | } | |
b095518e JG |
179 | |
180 | scsi_cmd[1] = (4 << 1); /* PIO Data-in */ | |
181 | scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev, | |
182 | block count in sector count field */ | |
85837ebd | 183 | data_dir = DMA_FROM_DEVICE; |
b095518e JG |
184 | } else { |
185 | scsi_cmd[1] = (3 << 1); /* Non-data */ | |
186 | /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */ | |
85837ebd | 187 | data_dir = DMA_NONE; |
b095518e JG |
188 | } |
189 | ||
190 | scsi_cmd[0] = ATA_16; | |
191 | ||
192 | scsi_cmd[4] = args[2]; | |
193 | if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */ | |
194 | scsi_cmd[6] = args[3]; | |
195 | scsi_cmd[8] = args[1]; | |
196 | scsi_cmd[10] = 0x4f; | |
197 | scsi_cmd[12] = 0xc2; | |
198 | } else { | |
199 | scsi_cmd[6] = args[1]; | |
200 | } | |
201 | scsi_cmd[14] = args[0]; | |
202 | ||
203 | /* Good values for timeout and retries? Values below | |
204 | from scsi_ioctl_send_command() for default case... */ | |
85837ebd MC |
205 | if (scsi_execute_req(scsidev, scsi_cmd, data_dir, argbuf, argsize, |
206 | &sshdr, (10*HZ), 5)) { | |
b095518e JG |
207 | rc = -EIO; |
208 | goto error; | |
209 | } | |
210 | ||
211 | /* Need code to retrieve data from check condition? */ | |
212 | ||
213 | if ((argbuf) | |
c893a3ae | 214 | && copy_to_user(arg + sizeof(args), argbuf, argsize)) |
b095518e JG |
215 | rc = -EFAULT; |
216 | error: | |
b095518e JG |
217 | if (argbuf) |
218 | kfree(argbuf); | |
219 | ||
220 | return rc; | |
221 | } | |
222 | ||
223 | /** | |
224 | * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl | |
8e8b77dd | 225 | * @scsidev: Device to which we are issuing command |
b095518e JG |
226 | * @arg: User provided data for issuing command |
227 | * | |
228 | * LOCKING: | |
229 | * Defined by the SCSI layer. We don't really care. | |
230 | * | |
231 | * RETURNS: | |
232 | * Zero on success, negative errno on error. | |
233 | */ | |
234 | int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg) | |
235 | { | |
236 | int rc = 0; | |
237 | u8 scsi_cmd[MAX_COMMAND_SIZE]; | |
238 | u8 args[7]; | |
85837ebd | 239 | struct scsi_sense_hdr sshdr; |
b095518e | 240 | |
c893a3ae | 241 | if (arg == NULL) |
b095518e JG |
242 | return -EINVAL; |
243 | ||
244 | if (copy_from_user(args, arg, sizeof(args))) | |
245 | return -EFAULT; | |
246 | ||
247 | memset(scsi_cmd, 0, sizeof(scsi_cmd)); | |
248 | scsi_cmd[0] = ATA_16; | |
249 | scsi_cmd[1] = (3 << 1); /* Non-data */ | |
250 | /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */ | |
251 | scsi_cmd[4] = args[1]; | |
252 | scsi_cmd[6] = args[2]; | |
253 | scsi_cmd[8] = args[3]; | |
254 | scsi_cmd[10] = args[4]; | |
255 | scsi_cmd[12] = args[5]; | |
256 | scsi_cmd[14] = args[0]; | |
257 | ||
b095518e | 258 | /* Good values for timeout and retries? Values below |
85837ebd MC |
259 | from scsi_ioctl_send_command() for default case... */ |
260 | if (scsi_execute_req(scsidev, scsi_cmd, DMA_NONE, NULL, 0, &sshdr, | |
261 | (10*HZ), 5)) | |
b095518e | 262 | rc = -EIO; |
b095518e JG |
263 | |
264 | /* Need code to retrieve data from check condition? */ | |
b095518e JG |
265 | return rc; |
266 | } | |
267 | ||
1da177e4 LT |
268 | int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg) |
269 | { | |
270 | struct ata_port *ap; | |
271 | struct ata_device *dev; | |
272 | int val = -EINVAL, rc = -EINVAL; | |
273 | ||
274 | ap = (struct ata_port *) &scsidev->host->hostdata[0]; | |
275 | if (!ap) | |
276 | goto out; | |
277 | ||
278 | dev = ata_scsi_find_dev(ap, scsidev); | |
279 | if (!dev) { | |
280 | rc = -ENODEV; | |
281 | goto out; | |
282 | } | |
283 | ||
284 | switch (cmd) { | |
285 | case ATA_IOC_GET_IO32: | |
286 | val = 0; | |
287 | if (copy_to_user(arg, &val, 1)) | |
288 | return -EFAULT; | |
289 | return 0; | |
290 | ||
291 | case ATA_IOC_SET_IO32: | |
292 | val = (unsigned long) arg; | |
293 | if (val != 0) | |
294 | return -EINVAL; | |
295 | return 0; | |
296 | ||
b095518e JG |
297 | case HDIO_DRIVE_CMD: |
298 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) | |
299 | return -EACCES; | |
300 | return ata_cmd_ioctl(scsidev, arg); | |
301 | ||
302 | case HDIO_DRIVE_TASK: | |
303 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) | |
304 | return -EACCES; | |
305 | return ata_task_ioctl(scsidev, arg); | |
306 | ||
1da177e4 LT |
307 | default: |
308 | rc = -ENOTTY; | |
309 | break; | |
310 | } | |
311 | ||
312 | out: | |
313 | return rc; | |
314 | } | |
315 | ||
316 | /** | |
317 | * ata_scsi_qc_new - acquire new ata_queued_cmd reference | |
318 | * @ap: ATA port to which the new command is attached | |
319 | * @dev: ATA device to which the new command is attached | |
320 | * @cmd: SCSI command that originated this ATA command | |
321 | * @done: SCSI command completion function | |
322 | * | |
323 | * Obtain a reference to an unused ata_queued_cmd structure, | |
324 | * which is the basic libata structure representing a single | |
325 | * ATA command sent to the hardware. | |
326 | * | |
327 | * If a command was available, fill in the SCSI-specific | |
328 | * portions of the structure with information on the | |
329 | * current command. | |
330 | * | |
331 | * LOCKING: | |
332 | * spin_lock_irqsave(host_set lock) | |
333 | * | |
334 | * RETURNS: | |
335 | * Command allocated, or %NULL if none available. | |
336 | */ | |
337 | struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap, | |
338 | struct ata_device *dev, | |
339 | struct scsi_cmnd *cmd, | |
340 | void (*done)(struct scsi_cmnd *)) | |
341 | { | |
342 | struct ata_queued_cmd *qc; | |
343 | ||
344 | qc = ata_qc_new_init(ap, dev); | |
345 | if (qc) { | |
346 | qc->scsicmd = cmd; | |
347 | qc->scsidone = done; | |
348 | ||
349 | if (cmd->use_sg) { | |
cedc9a47 | 350 | qc->__sg = (struct scatterlist *) cmd->request_buffer; |
1da177e4 LT |
351 | qc->n_elem = cmd->use_sg; |
352 | } else { | |
cedc9a47 | 353 | qc->__sg = &qc->sgent; |
1da177e4 LT |
354 | qc->n_elem = 1; |
355 | } | |
356 | } else { | |
357 | cmd->result = (DID_OK << 16) | (QUEUE_FULL << 1); | |
358 | done(cmd); | |
359 | } | |
360 | ||
361 | return qc; | |
362 | } | |
363 | ||
b095518e JG |
364 | /** |
365 | * ata_dump_status - user friendly display of error info | |
366 | * @id: id of the port in question | |
367 | * @tf: ptr to filled out taskfile | |
368 | * | |
369 | * Decode and dump the ATA error/status registers for the user so | |
370 | * that they have some idea what really happened at the non | |
371 | * make-believe layer. | |
372 | * | |
373 | * LOCKING: | |
374 | * inherited from caller | |
375 | */ | |
376 | void ata_dump_status(unsigned id, struct ata_taskfile *tf) | |
377 | { | |
378 | u8 stat = tf->command, err = tf->feature; | |
379 | ||
380 | printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat); | |
381 | if (stat & ATA_BUSY) { | |
382 | printk("Busy }\n"); /* Data is not valid in this case */ | |
383 | } else { | |
384 | if (stat & 0x40) printk("DriveReady "); | |
385 | if (stat & 0x20) printk("DeviceFault "); | |
386 | if (stat & 0x10) printk("SeekComplete "); | |
387 | if (stat & 0x08) printk("DataRequest "); | |
388 | if (stat & 0x04) printk("CorrectedError "); | |
389 | if (stat & 0x02) printk("Index "); | |
390 | if (stat & 0x01) printk("Error "); | |
391 | printk("}\n"); | |
392 | ||
393 | if (err) { | |
394 | printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err); | |
395 | if (err & 0x04) printk("DriveStatusError "); | |
396 | if (err & 0x80) { | |
397 | if (err & 0x04) printk("BadCRC "); | |
398 | else printk("Sector "); | |
399 | } | |
400 | if (err & 0x40) printk("UncorrectableError "); | |
401 | if (err & 0x10) printk("SectorIdNotFound "); | |
402 | if (err & 0x02) printk("TrackZeroNotFound "); | |
403 | if (err & 0x01) printk("AddrMarkNotFound "); | |
404 | printk("}\n"); | |
405 | } | |
406 | } | |
407 | } | |
408 | ||
9b847548 JA |
409 | int ata_scsi_device_resume(struct scsi_device *sdev) |
410 | { | |
411 | struct ata_port *ap = (struct ata_port *) &sdev->host->hostdata[0]; | |
412 | struct ata_device *dev = &ap->device[sdev->id]; | |
413 | ||
414 | return ata_device_resume(ap, dev); | |
415 | } | |
416 | ||
417 | int ata_scsi_device_suspend(struct scsi_device *sdev) | |
418 | { | |
419 | struct ata_port *ap = (struct ata_port *) &sdev->host->hostdata[0]; | |
420 | struct ata_device *dev = &ap->device[sdev->id]; | |
421 | ||
422 | return ata_device_suspend(ap, dev); | |
423 | } | |
424 | ||
1da177e4 LT |
425 | /** |
426 | * ata_to_sense_error - convert ATA error to SCSI error | |
8e8b77dd | 427 | * @id: ATA device number |
1da177e4 | 428 | * @drv_stat: value contained in ATA status register |
b095518e JG |
429 | * @drv_err: value contained in ATA error register |
430 | * @sk: the sense key we'll fill out | |
431 | * @asc: the additional sense code we'll fill out | |
432 | * @ascq: the additional sense code qualifier we'll fill out | |
1da177e4 | 433 | * |
b095518e JG |
434 | * Converts an ATA error into a SCSI error. Fill out pointers to |
435 | * SK, ASC, and ASCQ bytes for later use in fixed or descriptor | |
436 | * format sense blocks. | |
1da177e4 LT |
437 | * |
438 | * LOCKING: | |
439 | * spin_lock_irqsave(host_set lock) | |
440 | */ | |
b095518e JG |
441 | void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc, |
442 | u8 *ascq) | |
1da177e4 | 443 | { |
b095518e | 444 | int i; |
ffe75ef6 | 445 | |
1da177e4 | 446 | /* Based on the 3ware driver translation table */ |
98ac62de | 447 | static const unsigned char sense_table[][4] = { |
1da177e4 LT |
448 | /* BBD|ECC|ID|MAR */ |
449 | {0xd1, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command | |
450 | /* BBD|ECC|ID */ | |
451 | {0xd0, ABORTED_COMMAND, 0x00, 0x00}, // Device busy Aborted command | |
452 | /* ECC|MC|MARK */ | |
453 | {0x61, HARDWARE_ERROR, 0x00, 0x00}, // Device fault Hardware error | |
454 | /* ICRC|ABRT */ /* NB: ICRC & !ABRT is BBD */ | |
455 | {0x84, ABORTED_COMMAND, 0x47, 0x00}, // Data CRC error SCSI parity error | |
456 | /* MC|ID|ABRT|TRK0|MARK */ | |
457 | {0x37, NOT_READY, 0x04, 0x00}, // Unit offline Not ready | |
458 | /* MCR|MARK */ | |
459 | {0x09, NOT_READY, 0x04, 0x00}, // Unrecovered disk error Not ready | |
460 | /* Bad address mark */ | |
461 | {0x01, MEDIUM_ERROR, 0x13, 0x00}, // Address mark not found Address mark not found for data field | |
462 | /* TRK0 */ | |
463 | {0x02, HARDWARE_ERROR, 0x00, 0x00}, // Track 0 not found Hardware error | |
464 | /* Abort & !ICRC */ | |
465 | {0x04, ABORTED_COMMAND, 0x00, 0x00}, // Aborted command Aborted command | |
466 | /* Media change request */ | |
467 | {0x08, NOT_READY, 0x04, 0x00}, // Media change request FIXME: faking offline | |
468 | /* SRV */ | |
469 | {0x10, ABORTED_COMMAND, 0x14, 0x00}, // ID not found Recorded entity not found | |
470 | /* Media change */ | |
471 | {0x08, NOT_READY, 0x04, 0x00}, // Media change FIXME: faking offline | |
472 | /* ECC */ | |
473 | {0x40, MEDIUM_ERROR, 0x11, 0x04}, // Uncorrectable ECC error Unrecovered read error | |
474 | /* BBD - block marked bad */ | |
475 | {0x80, MEDIUM_ERROR, 0x11, 0x04}, // Block marked bad Medium error, unrecovered read error | |
476 | {0xFF, 0xFF, 0xFF, 0xFF}, // END mark | |
477 | }; | |
98ac62de | 478 | static const unsigned char stat_table[][4] = { |
1da177e4 LT |
479 | /* Must be first because BUSY means no other bits valid */ |
480 | {0x80, ABORTED_COMMAND, 0x47, 0x00}, // Busy, fake parity for now | |
481 | {0x20, HARDWARE_ERROR, 0x00, 0x00}, // Device fault | |
482 | {0x08, ABORTED_COMMAND, 0x47, 0x00}, // Timed out in xfer, fake parity for now | |
483 | {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered | |
484 | {0xFF, 0xFF, 0xFF, 0xFF}, // END mark | |
485 | }; | |
1da177e4 | 486 | |
1da177e4 LT |
487 | /* |
488 | * Is this an error we can process/parse | |
489 | */ | |
b095518e JG |
490 | if (drv_stat & ATA_BUSY) { |
491 | drv_err = 0; /* Ignore the err bits, they're invalid */ | |
1da177e4 | 492 | } |
b095518e JG |
493 | |
494 | if (drv_err) { | |
495 | /* Look for drv_err */ | |
496 | for (i = 0; sense_table[i][0] != 0xFF; i++) { | |
497 | /* Look for best matches first */ | |
498 | if ((sense_table[i][0] & drv_err) == | |
499 | sense_table[i][0]) { | |
500 | *sk = sense_table[i][1]; | |
501 | *asc = sense_table[i][2]; | |
502 | *ascq = sense_table[i][3]; | |
503 | goto translate_done; | |
504 | } | |
505 | } | |
506 | /* No immediate match */ | |
507 | printk(KERN_WARNING "ata%u: no sense translation for " | |
508 | "error 0x%02x\n", id, drv_err); | |
1da177e4 | 509 | } |
b095518e JG |
510 | |
511 | /* Fall back to interpreting status bits */ | |
512 | for (i = 0; stat_table[i][0] != 0xFF; i++) { | |
513 | if (stat_table[i][0] & drv_stat) { | |
514 | *sk = stat_table[i][1]; | |
515 | *asc = stat_table[i][2]; | |
516 | *ascq = stat_table[i][3]; | |
517 | goto translate_done; | |
1da177e4 | 518 | } |
b095518e JG |
519 | } |
520 | /* No error? Undecoded? */ | |
521 | printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n", | |
522 | id, drv_stat); | |
523 | ||
2d202024 AC |
524 | /* We need a sensible error return here, which is tricky, and one |
525 | that won't cause people to do things like return a disk wrongly */ | |
526 | *sk = ABORTED_COMMAND; | |
527 | *asc = 0x00; | |
528 | *ascq = 0x00; | |
b095518e JG |
529 | |
530 | translate_done: | |
531 | printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to " | |
532 | "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err, | |
533 | *sk, *asc, *ascq); | |
534 | return; | |
535 | } | |
536 | ||
537 | /* | |
538 | * ata_gen_ata_desc_sense - Generate check condition sense block. | |
539 | * @qc: Command that completed. | |
540 | * | |
541 | * This function is specific to the ATA descriptor format sense | |
542 | * block specified for the ATA pass through commands. Regardless | |
543 | * of whether the command errored or not, return a sense | |
544 | * block. Copy all controller registers into the sense | |
545 | * block. Clear sense key, ASC & ASCQ if there is no error. | |
546 | * | |
547 | * LOCKING: | |
548 | * spin_lock_irqsave(host_set lock) | |
549 | */ | |
550 | void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc) | |
551 | { | |
552 | struct scsi_cmnd *cmd = qc->scsicmd; | |
553 | struct ata_taskfile *tf = &qc->tf; | |
554 | unsigned char *sb = cmd->sense_buffer; | |
555 | unsigned char *desc = sb + 8; | |
556 | ||
557 | memset(sb, 0, SCSI_SENSE_BUFFERSIZE); | |
558 | ||
0e5dec47 | 559 | cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; |
b095518e JG |
560 | |
561 | /* | |
562 | * Read the controller registers. | |
563 | */ | |
a4631474 | 564 | WARN_ON(qc->ap->ops->tf_read == NULL); |
b095518e | 565 | qc->ap->ops->tf_read(qc->ap, tf); |
1da177e4 | 566 | |
b095518e JG |
567 | /* |
568 | * Use ata_to_sense_error() to map status register bits | |
569 | * onto sense key, asc & ascq. | |
570 | */ | |
a7dac447 | 571 | if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) { |
b095518e JG |
572 | ata_to_sense_error(qc->ap->id, tf->command, tf->feature, |
573 | &sb[1], &sb[2], &sb[3]); | |
574 | sb[1] &= 0x0f; | |
1da177e4 LT |
575 | } |
576 | ||
b095518e JG |
577 | /* |
578 | * Sense data is current and format is descriptor. | |
579 | */ | |
580 | sb[0] = 0x72; | |
1da177e4 | 581 | |
b095518e JG |
582 | desc[0] = 0x09; |
583 | ||
584 | /* | |
585 | * Set length of additional sense data. | |
586 | * Since we only populate descriptor 0, the total | |
587 | * length is the same (fixed) length as descriptor 0. | |
588 | */ | |
589 | desc[1] = sb[7] = 14; | |
590 | ||
591 | /* | |
592 | * Copy registers into sense buffer. | |
593 | */ | |
594 | desc[2] = 0x00; | |
595 | desc[3] = tf->feature; /* == error reg */ | |
596 | desc[5] = tf->nsect; | |
597 | desc[7] = tf->lbal; | |
598 | desc[9] = tf->lbam; | |
599 | desc[11] = tf->lbah; | |
600 | desc[12] = tf->device; | |
601 | desc[13] = tf->command; /* == status reg */ | |
602 | ||
603 | /* | |
604 | * Fill in Extend bit, and the high order bytes | |
605 | * if applicable. | |
606 | */ | |
607 | if (tf->flags & ATA_TFLAG_LBA48) { | |
608 | desc[2] |= 0x01; | |
609 | desc[4] = tf->hob_nsect; | |
610 | desc[6] = tf->hob_lbal; | |
611 | desc[8] = tf->hob_lbam; | |
612 | desc[10] = tf->hob_lbah; | |
1da177e4 | 613 | } |
b095518e | 614 | } |
1da177e4 | 615 | |
b095518e JG |
616 | /** |
617 | * ata_gen_fixed_sense - generate a SCSI fixed sense block | |
618 | * @qc: Command that we are erroring out | |
619 | * | |
620 | * Leverage ata_to_sense_error() to give us the codes. Fit our | |
621 | * LBA in here if there's room. | |
622 | * | |
623 | * LOCKING: | |
624 | * inherited from caller | |
625 | */ | |
626 | void ata_gen_fixed_sense(struct ata_queued_cmd *qc) | |
627 | { | |
628 | struct scsi_cmnd *cmd = qc->scsicmd; | |
629 | struct ata_taskfile *tf = &qc->tf; | |
630 | unsigned char *sb = cmd->sense_buffer; | |
631 | ||
632 | memset(sb, 0, SCSI_SENSE_BUFFERSIZE); | |
633 | ||
0e5dec47 | 634 | cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; |
b095518e JG |
635 | |
636 | /* | |
637 | * Read the controller registers. | |
638 | */ | |
a4631474 | 639 | WARN_ON(qc->ap->ops->tf_read == NULL); |
b095518e JG |
640 | qc->ap->ops->tf_read(qc->ap, tf); |
641 | ||
642 | /* | |
643 | * Use ata_to_sense_error() to map status register bits | |
644 | * onto sense key, asc & ascq. | |
645 | */ | |
a7dac447 | 646 | if (tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ)) { |
b095518e JG |
647 | ata_to_sense_error(qc->ap->id, tf->command, tf->feature, |
648 | &sb[2], &sb[12], &sb[13]); | |
649 | sb[2] &= 0x0f; | |
1da177e4 | 650 | } |
1da177e4 LT |
651 | |
652 | sb[0] = 0x70; | |
b095518e | 653 | sb[7] = 0x0a; |
0274aa25 | 654 | |
a7dac447 JG |
655 | if (tf->flags & ATA_TFLAG_LBA48) { |
656 | /* TODO: find solution for LBA48 descriptors */ | |
657 | } | |
658 | ||
659 | else if (tf->flags & ATA_TFLAG_LBA) { | |
b095518e JG |
660 | /* A small (28b) LBA will fit in the 32b info field */ |
661 | sb[0] |= 0x80; /* set valid bit */ | |
662 | sb[3] = tf->device & 0x0f; | |
663 | sb[4] = tf->lbah; | |
664 | sb[5] = tf->lbam; | |
665 | sb[6] = tf->lbal; | |
1da177e4 | 666 | } |
a7dac447 JG |
667 | |
668 | else { | |
669 | /* TODO: C/H/S */ | |
670 | } | |
1da177e4 LT |
671 | } |
672 | ||
a6cce2a7 BK |
673 | static void ata_scsi_sdev_config(struct scsi_device *sdev) |
674 | { | |
675 | sdev->use_10_for_rw = 1; | |
676 | sdev->use_10_for_ms = 1; | |
677 | } | |
678 | ||
679 | static void ata_scsi_dev_config(struct scsi_device *sdev, | |
680 | struct ata_device *dev) | |
681 | { | |
682 | unsigned int max_sectors; | |
683 | ||
684 | /* TODO: 2048 is an arbitrary number, not the | |
685 | * hardware maximum. This should be increased to | |
686 | * 65534 when Jens Axboe's patch for dynamically | |
687 | * determining max_sectors is merged. | |
688 | */ | |
689 | max_sectors = ATA_MAX_SECTORS; | |
690 | if (dev->flags & ATA_DFLAG_LBA48) | |
691 | max_sectors = 2048; | |
692 | if (dev->max_sectors) | |
693 | max_sectors = dev->max_sectors; | |
694 | ||
695 | blk_queue_max_sectors(sdev->request_queue, max_sectors); | |
696 | ||
697 | /* | |
698 | * SATA DMA transfers must be multiples of 4 byte, so | |
699 | * we need to pad ATAPI transfers using an extra sg. | |
700 | * Decrement max hw segments accordingly. | |
701 | */ | |
702 | if (dev->class == ATA_DEV_ATAPI) { | |
703 | request_queue_t *q = sdev->request_queue; | |
704 | blk_queue_max_hw_segments(q, q->max_hw_segments - 1); | |
705 | } | |
706 | } | |
707 | ||
1da177e4 LT |
708 | /** |
709 | * ata_scsi_slave_config - Set SCSI device attributes | |
710 | * @sdev: SCSI device to examine | |
711 | * | |
712 | * This is called before we actually start reading | |
713 | * and writing to the device, to configure certain | |
714 | * SCSI mid-layer behaviors. | |
715 | * | |
716 | * LOCKING: | |
717 | * Defined by SCSI layer. We don't really care. | |
718 | */ | |
719 | ||
720 | int ata_scsi_slave_config(struct scsi_device *sdev) | |
721 | { | |
a6cce2a7 | 722 | ata_scsi_sdev_config(sdev); |
1da177e4 LT |
723 | |
724 | blk_queue_max_phys_segments(sdev->request_queue, LIBATA_MAX_PRD); | |
725 | ||
726 | if (sdev->id < ATA_MAX_DEVICES) { | |
727 | struct ata_port *ap; | |
728 | struct ata_device *dev; | |
729 | ||
730 | ap = (struct ata_port *) &sdev->host->hostdata[0]; | |
731 | dev = &ap->device[sdev->id]; | |
732 | ||
a6cce2a7 | 733 | ata_scsi_dev_config(sdev, dev); |
1da177e4 LT |
734 | } |
735 | ||
736 | return 0; /* scsi layer doesn't check return value, sigh */ | |
737 | } | |
738 | ||
f29841e0 TH |
739 | /** |
740 | * ata_scsi_timed_out - SCSI layer time out callback | |
741 | * @cmd: timed out SCSI command | |
742 | * | |
743 | * Handles SCSI layer timeout. We race with normal completion of | |
744 | * the qc for @cmd. If the qc is already gone, we lose and let | |
745 | * the scsi command finish (EH_HANDLED). Otherwise, the qc has | |
746 | * timed out and EH should be invoked. Prevent ata_qc_complete() | |
747 | * from finishing it by setting EH_SCHEDULED and return | |
748 | * EH_NOT_HANDLED. | |
749 | * | |
750 | * LOCKING: | |
751 | * Called from timer context | |
752 | * | |
753 | * RETURNS: | |
754 | * EH_HANDLED or EH_NOT_HANDLED | |
755 | */ | |
756 | enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd) | |
757 | { | |
758 | struct Scsi_Host *host = cmd->device->host; | |
759 | struct ata_port *ap = (struct ata_port *) &host->hostdata[0]; | |
760 | unsigned long flags; | |
761 | struct ata_queued_cmd *qc; | |
762 | enum scsi_eh_timer_return ret = EH_HANDLED; | |
763 | ||
764 | DPRINTK("ENTER\n"); | |
765 | ||
766 | spin_lock_irqsave(&ap->host_set->lock, flags); | |
767 | qc = ata_qc_from_tag(ap, ap->active_tag); | |
768 | if (qc) { | |
a4631474 | 769 | WARN_ON(qc->scsicmd != cmd); |
f29841e0 TH |
770 | qc->flags |= ATA_QCFLAG_EH_SCHEDULED; |
771 | qc->err_mask |= AC_ERR_TIMEOUT; | |
772 | ret = EH_NOT_HANDLED; | |
773 | } | |
774 | spin_unlock_irqrestore(&ap->host_set->lock, flags); | |
775 | ||
776 | DPRINTK("EXIT, ret=%d\n", ret); | |
777 | return ret; | |
778 | } | |
779 | ||
1da177e4 LT |
780 | /** |
781 | * ata_scsi_error - SCSI layer error handler callback | |
782 | * @host: SCSI host on which error occurred | |
783 | * | |
784 | * Handles SCSI-layer-thrown error events. | |
785 | * | |
786 | * LOCKING: | |
787 | * Inherited from SCSI layer (none, can sleep) | |
788 | * | |
789 | * RETURNS: | |
790 | * Zero. | |
791 | */ | |
792 | ||
793 | int ata_scsi_error(struct Scsi_Host *host) | |
794 | { | |
795 | struct ata_port *ap; | |
5140788f | 796 | unsigned long flags; |
1da177e4 LT |
797 | |
798 | DPRINTK("ENTER\n"); | |
799 | ||
5140788f JG |
800 | ap = (struct ata_port *) &host->hostdata[0]; |
801 | ||
dde44589 | 802 | spin_lock_irqsave(&ap->host_set->lock, flags); |
a4631474 | 803 | WARN_ON(ap->flags & ATA_FLAG_IN_EH); |
dde44589 | 804 | ap->flags |= ATA_FLAG_IN_EH; |
a4631474 | 805 | WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL); |
dde44589 TH |
806 | spin_unlock_irqrestore(&ap->host_set->lock, flags); |
807 | ||
86e45b6b TH |
808 | ata_port_flush_task(ap); |
809 | ||
1da177e4 LT |
810 | ap->ops->eng_timeout(ap); |
811 | ||
a4631474 | 812 | WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q)); |
a72ec4ce TH |
813 | |
814 | scsi_eh_flush_done_q(&ap->eh_done_q); | |
1da177e4 | 815 | |
dde44589 TH |
816 | spin_lock_irqsave(&ap->host_set->lock, flags); |
817 | ap->flags &= ~ATA_FLAG_IN_EH; | |
818 | spin_unlock_irqrestore(&ap->host_set->lock, flags); | |
819 | ||
1da177e4 LT |
820 | DPRINTK("EXIT\n"); |
821 | return 0; | |
822 | } | |
823 | ||
a72ec4ce TH |
824 | static void ata_eh_scsidone(struct scsi_cmnd *scmd) |
825 | { | |
826 | /* nada */ | |
827 | } | |
828 | ||
829 | static void __ata_eh_qc_complete(struct ata_queued_cmd *qc) | |
830 | { | |
831 | struct ata_port *ap = qc->ap; | |
832 | struct scsi_cmnd *scmd = qc->scsicmd; | |
833 | unsigned long flags; | |
834 | ||
835 | spin_lock_irqsave(&ap->host_set->lock, flags); | |
836 | qc->scsidone = ata_eh_scsidone; | |
341963b9 | 837 | __ata_qc_complete(qc); |
a4631474 | 838 | WARN_ON(ata_tag_valid(qc->tag)); |
a72ec4ce TH |
839 | spin_unlock_irqrestore(&ap->host_set->lock, flags); |
840 | ||
841 | scsi_eh_finish_cmd(scmd, &ap->eh_done_q); | |
842 | } | |
843 | ||
844 | /** | |
845 | * ata_eh_qc_complete - Complete an active ATA command from EH | |
846 | * @qc: Command to complete | |
847 | * | |
848 | * Indicate to the mid and upper layers that an ATA command has | |
849 | * completed. To be used from EH. | |
850 | */ | |
851 | void ata_eh_qc_complete(struct ata_queued_cmd *qc) | |
852 | { | |
853 | struct scsi_cmnd *scmd = qc->scsicmd; | |
854 | scmd->retries = scmd->allowed; | |
855 | __ata_eh_qc_complete(qc); | |
856 | } | |
857 | ||
858 | /** | |
859 | * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH | |
860 | * @qc: Command to retry | |
861 | * | |
862 | * Indicate to the mid and upper layers that an ATA command | |
863 | * should be retried. To be used from EH. | |
864 | * | |
865 | * SCSI midlayer limits the number of retries to scmd->allowed. | |
866 | * This function might need to adjust scmd->retries for commands | |
867 | * which get retried due to unrelated NCQ failures. | |
868 | */ | |
869 | void ata_eh_qc_retry(struct ata_queued_cmd *qc) | |
870 | { | |
871 | __ata_eh_qc_complete(qc); | |
872 | } | |
873 | ||
972dcafb DG |
874 | /** |
875 | * ata_scsi_start_stop_xlat - Translate SCSI START STOP UNIT command | |
876 | * @qc: Storage for translated ATA taskfile | |
877 | * @scsicmd: SCSI command to translate | |
878 | * | |
879 | * Sets up an ATA taskfile to issue STANDBY (to stop) or READ VERIFY | |
880 | * (to start). Perhaps these commands should be preceded by | |
881 | * CHECK POWER MODE to see what power mode the device is already in. | |
882 | * [See SAT revision 5 at www.t10.org] | |
883 | * | |
884 | * LOCKING: | |
885 | * spin_lock_irqsave(host_set lock) | |
886 | * | |
887 | * RETURNS: | |
888 | * Zero on success, non-zero on error. | |
889 | */ | |
890 | ||
891 | static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc, | |
057ace5e | 892 | const u8 *scsicmd) |
972dcafb DG |
893 | { |
894 | struct ata_taskfile *tf = &qc->tf; | |
895 | ||
896 | tf->flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR; | |
897 | tf->protocol = ATA_PROT_NODATA; | |
898 | if (scsicmd[1] & 0x1) { | |
899 | ; /* ignore IMMED bit, violates sat-r05 */ | |
900 | } | |
901 | if (scsicmd[4] & 0x2) | |
ae006510 | 902 | goto invalid_fld; /* LOEJ bit set not supported */ |
972dcafb | 903 | if (((scsicmd[4] >> 4) & 0xf) != 0) |
ae006510 | 904 | goto invalid_fld; /* power conditions not supported */ |
972dcafb DG |
905 | if (scsicmd[4] & 0x1) { |
906 | tf->nsect = 1; /* 1 sector, lba=0 */ | |
9d5b1302 AL |
907 | |
908 | if (qc->dev->flags & ATA_DFLAG_LBA) { | |
909 | qc->tf.flags |= ATA_TFLAG_LBA; | |
910 | ||
911 | tf->lbah = 0x0; | |
912 | tf->lbam = 0x0; | |
913 | tf->lbal = 0x0; | |
914 | tf->device |= ATA_LBA; | |
915 | } else { | |
916 | /* CHS */ | |
917 | tf->lbal = 0x1; /* sect */ | |
918 | tf->lbam = 0x0; /* cyl low */ | |
919 | tf->lbah = 0x0; /* cyl high */ | |
920 | } | |
921 | ||
972dcafb DG |
922 | tf->command = ATA_CMD_VERIFY; /* READ VERIFY */ |
923 | } else { | |
924 | tf->nsect = 0; /* time period value (0 implies now) */ | |
925 | tf->command = ATA_CMD_STANDBY; | |
926 | /* Consider: ATA STANDBY IMMEDIATE command */ | |
927 | } | |
928 | /* | |
929 | * Standby and Idle condition timers could be implemented but that | |
930 | * would require libata to implement the Power condition mode page | |
931 | * and allow the user to change it. Changing mode pages requires | |
932 | * MODE SELECT to be implemented. | |
933 | */ | |
934 | ||
935 | return 0; | |
ae006510 DG |
936 | |
937 | invalid_fld: | |
938 | ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0); | |
939 | /* "Invalid field in cbd" */ | |
940 | return 1; | |
972dcafb DG |
941 | } |
942 | ||
943 | ||
1da177e4 LT |
944 | /** |
945 | * ata_scsi_flush_xlat - Translate SCSI SYNCHRONIZE CACHE command | |
946 | * @qc: Storage for translated ATA taskfile | |
947 | * @scsicmd: SCSI command to translate (ignored) | |
948 | * | |
949 | * Sets up an ATA taskfile to issue FLUSH CACHE or | |
950 | * FLUSH CACHE EXT. | |
951 | * | |
952 | * LOCKING: | |
953 | * spin_lock_irqsave(host_set lock) | |
954 | * | |
955 | * RETURNS: | |
956 | * Zero on success, non-zero on error. | |
957 | */ | |
958 | ||
057ace5e | 959 | static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd) |
1da177e4 LT |
960 | { |
961 | struct ata_taskfile *tf = &qc->tf; | |
962 | ||
963 | tf->flags |= ATA_TFLAG_DEVICE; | |
964 | tf->protocol = ATA_PROT_NODATA; | |
965 | ||
07506697 | 966 | if ((qc->dev->flags & ATA_DFLAG_LBA48) && |
1da177e4 LT |
967 | (ata_id_has_flush_ext(qc->dev->id))) |
968 | tf->command = ATA_CMD_FLUSH_EXT; | |
969 | else | |
970 | tf->command = ATA_CMD_FLUSH; | |
971 | ||
972 | return 0; | |
973 | } | |
974 | ||
3aef5231 AL |
975 | /** |
976 | * scsi_6_lba_len - Get LBA and transfer length | |
977 | * @scsicmd: SCSI command to translate | |
978 | * | |
979 | * Calculate LBA and transfer length for 6-byte commands. | |
980 | * | |
981 | * RETURNS: | |
982 | * @plba: the LBA | |
983 | * @plen: the transfer length | |
984 | */ | |
985 | ||
057ace5e | 986 | static void scsi_6_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen) |
3aef5231 AL |
987 | { |
988 | u64 lba = 0; | |
989 | u32 len = 0; | |
990 | ||
991 | VPRINTK("six-byte command\n"); | |
992 | ||
993 | lba |= ((u64)scsicmd[2]) << 8; | |
994 | lba |= ((u64)scsicmd[3]); | |
995 | ||
996 | len |= ((u32)scsicmd[4]); | |
997 | ||
998 | *plba = lba; | |
999 | *plen = len; | |
1000 | } | |
1001 | ||
1002 | /** | |
1003 | * scsi_10_lba_len - Get LBA and transfer length | |
1004 | * @scsicmd: SCSI command to translate | |
1005 | * | |
1006 | * Calculate LBA and transfer length for 10-byte commands. | |
1007 | * | |
1008 | * RETURNS: | |
1009 | * @plba: the LBA | |
1010 | * @plen: the transfer length | |
1011 | */ | |
1012 | ||
057ace5e | 1013 | static void scsi_10_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen) |
3aef5231 AL |
1014 | { |
1015 | u64 lba = 0; | |
1016 | u32 len = 0; | |
1017 | ||
1018 | VPRINTK("ten-byte command\n"); | |
1019 | ||
1020 | lba |= ((u64)scsicmd[2]) << 24; | |
1021 | lba |= ((u64)scsicmd[3]) << 16; | |
1022 | lba |= ((u64)scsicmd[4]) << 8; | |
1023 | lba |= ((u64)scsicmd[5]); | |
1024 | ||
1025 | len |= ((u32)scsicmd[7]) << 8; | |
1026 | len |= ((u32)scsicmd[8]); | |
1027 | ||
1028 | *plba = lba; | |
1029 | *plen = len; | |
1030 | } | |
1031 | ||
1032 | /** | |
1033 | * scsi_16_lba_len - Get LBA and transfer length | |
1034 | * @scsicmd: SCSI command to translate | |
1035 | * | |
1036 | * Calculate LBA and transfer length for 16-byte commands. | |
1037 | * | |
1038 | * RETURNS: | |
1039 | * @plba: the LBA | |
1040 | * @plen: the transfer length | |
1041 | */ | |
1042 | ||
057ace5e | 1043 | static void scsi_16_lba_len(const u8 *scsicmd, u64 *plba, u32 *plen) |
3aef5231 AL |
1044 | { |
1045 | u64 lba = 0; | |
1046 | u32 len = 0; | |
1047 | ||
1048 | VPRINTK("sixteen-byte command\n"); | |
1049 | ||
1050 | lba |= ((u64)scsicmd[2]) << 56; | |
1051 | lba |= ((u64)scsicmd[3]) << 48; | |
1052 | lba |= ((u64)scsicmd[4]) << 40; | |
1053 | lba |= ((u64)scsicmd[5]) << 32; | |
1054 | lba |= ((u64)scsicmd[6]) << 24; | |
1055 | lba |= ((u64)scsicmd[7]) << 16; | |
1056 | lba |= ((u64)scsicmd[8]) << 8; | |
1057 | lba |= ((u64)scsicmd[9]); | |
1058 | ||
1059 | len |= ((u32)scsicmd[10]) << 24; | |
1060 | len |= ((u32)scsicmd[11]) << 16; | |
1061 | len |= ((u32)scsicmd[12]) << 8; | |
1062 | len |= ((u32)scsicmd[13]); | |
1063 | ||
1064 | *plba = lba; | |
1065 | *plen = len; | |
1066 | } | |
1067 | ||
1da177e4 LT |
1068 | /** |
1069 | * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one | |
1070 | * @qc: Storage for translated ATA taskfile | |
1071 | * @scsicmd: SCSI command to translate | |
1072 | * | |
1073 | * Converts SCSI VERIFY command to an ATA READ VERIFY command. | |
1074 | * | |
1075 | * LOCKING: | |
1076 | * spin_lock_irqsave(host_set lock) | |
1077 | * | |
1078 | * RETURNS: | |
1079 | * Zero on success, non-zero on error. | |
1080 | */ | |
1081 | ||
057ace5e | 1082 | static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd) |
1da177e4 LT |
1083 | { |
1084 | struct ata_taskfile *tf = &qc->tf; | |
8bf62ece | 1085 | struct ata_device *dev = qc->dev; |
1da177e4 | 1086 | u64 dev_sectors = qc->dev->n_sectors; |
3aef5231 AL |
1087 | u64 block; |
1088 | u32 n_block; | |
1da177e4 LT |
1089 | |
1090 | tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; | |
1091 | tf->protocol = ATA_PROT_NODATA; | |
1da177e4 | 1092 | |
3aef5231 AL |
1093 | if (scsicmd[0] == VERIFY) |
1094 | scsi_10_lba_len(scsicmd, &block, &n_block); | |
1095 | else if (scsicmd[0] == VERIFY_16) | |
1096 | scsi_16_lba_len(scsicmd, &block, &n_block); | |
1da177e4 | 1097 | else |
ae006510 | 1098 | goto invalid_fld; |
1da177e4 | 1099 | |
8bf62ece | 1100 | if (!n_block) |
ae006510 | 1101 | goto nothing_to_do; |
8bf62ece | 1102 | if (block >= dev_sectors) |
ae006510 | 1103 | goto out_of_range; |
8bf62ece | 1104 | if ((block + n_block) > dev_sectors) |
ae006510 | 1105 | goto out_of_range; |
1da177e4 | 1106 | |
07506697 AL |
1107 | if (dev->flags & ATA_DFLAG_LBA) { |
1108 | tf->flags |= ATA_TFLAG_LBA; | |
1109 | ||
c6a33e24 AL |
1110 | if (lba_28_ok(block, n_block)) { |
1111 | /* use LBA28 */ | |
1112 | tf->command = ATA_CMD_VERIFY; | |
1113 | tf->device |= (block >> 24) & 0xf; | |
1114 | } else if (lba_48_ok(block, n_block)) { | |
1115 | if (!(dev->flags & ATA_DFLAG_LBA48)) | |
1116 | goto out_of_range; | |
07506697 AL |
1117 | |
1118 | /* use LBA48 */ | |
1119 | tf->flags |= ATA_TFLAG_LBA48; | |
8bf62ece | 1120 | tf->command = ATA_CMD_VERIFY_EXT; |
1da177e4 | 1121 | |
8bf62ece | 1122 | tf->hob_nsect = (n_block >> 8) & 0xff; |
1da177e4 | 1123 | |
8bf62ece AL |
1124 | tf->hob_lbah = (block >> 40) & 0xff; |
1125 | tf->hob_lbam = (block >> 32) & 0xff; | |
1126 | tf->hob_lbal = (block >> 24) & 0xff; | |
c6a33e24 AL |
1127 | } else |
1128 | /* request too large even for LBA48 */ | |
1129 | goto out_of_range; | |
8bf62ece AL |
1130 | |
1131 | tf->nsect = n_block & 0xff; | |
1da177e4 | 1132 | |
8bf62ece AL |
1133 | tf->lbah = (block >> 16) & 0xff; |
1134 | tf->lbam = (block >> 8) & 0xff; | |
1135 | tf->lbal = block & 0xff; | |
1da177e4 | 1136 | |
8bf62ece AL |
1137 | tf->device |= ATA_LBA; |
1138 | } else { | |
1139 | /* CHS */ | |
1140 | u32 sect, head, cyl, track; | |
1141 | ||
c6a33e24 AL |
1142 | if (!lba_28_ok(block, n_block)) |
1143 | goto out_of_range; | |
07506697 | 1144 | |
8bf62ece AL |
1145 | /* Convert LBA to CHS */ |
1146 | track = (u32)block / dev->sectors; | |
1147 | cyl = track / dev->heads; | |
1148 | head = track % dev->heads; | |
1149 | sect = (u32)block % dev->sectors + 1; | |
1150 | ||
c187c4b5 AL |
1151 | DPRINTK("block %u track %u cyl %u head %u sect %u\n", |
1152 | (u32)block, track, cyl, head, sect); | |
8bf62ece AL |
1153 | |
1154 | /* Check whether the converted CHS can fit. | |
1155 | Cylinder: 0-65535 | |
1156 | Head: 0-15 | |
1157 | Sector: 1-255*/ | |
1158 | if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect)) | |
ae006510 | 1159 | goto out_of_range; |
8bf62ece AL |
1160 | |
1161 | tf->command = ATA_CMD_VERIFY; | |
1162 | tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */ | |
1163 | tf->lbal = sect; | |
1164 | tf->lbam = cyl; | |
1165 | tf->lbah = cyl >> 8; | |
1166 | tf->device |= head; | |
1167 | } | |
1da177e4 LT |
1168 | |
1169 | return 0; | |
ae006510 DG |
1170 | |
1171 | invalid_fld: | |
1172 | ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0); | |
1173 | /* "Invalid field in cbd" */ | |
1174 | return 1; | |
1175 | ||
1176 | out_of_range: | |
1177 | ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0); | |
1178 | /* "Logical Block Address out of range" */ | |
1179 | return 1; | |
1180 | ||
1181 | nothing_to_do: | |
1182 | qc->scsicmd->result = SAM_STAT_GOOD; | |
1183 | return 1; | |
1da177e4 LT |
1184 | } |
1185 | ||
1186 | /** | |
1187 | * ata_scsi_rw_xlat - Translate SCSI r/w command into an ATA one | |
1188 | * @qc: Storage for translated ATA taskfile | |
1189 | * @scsicmd: SCSI command to translate | |
1190 | * | |
1191 | * Converts any of six SCSI read/write commands into the | |
1192 | * ATA counterpart, including starting sector (LBA), | |
1193 | * sector count, and taking into account the device's LBA48 | |
1194 | * support. | |
1195 | * | |
1196 | * Commands %READ_6, %READ_10, %READ_16, %WRITE_6, %WRITE_10, and | |
1197 | * %WRITE_16 are currently supported. | |
1198 | * | |
1199 | * LOCKING: | |
1200 | * spin_lock_irqsave(host_set lock) | |
1201 | * | |
1202 | * RETURNS: | |
1203 | * Zero on success, non-zero on error. | |
1204 | */ | |
1205 | ||
057ace5e | 1206 | static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd) |
1da177e4 LT |
1207 | { |
1208 | struct ata_taskfile *tf = &qc->tf; | |
8bf62ece | 1209 | struct ata_device *dev = qc->dev; |
3aef5231 AL |
1210 | u64 block; |
1211 | u32 n_block; | |
1da177e4 LT |
1212 | |
1213 | tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; | |
1da177e4 | 1214 | |
8cbd6df1 AL |
1215 | if (scsicmd[0] == WRITE_10 || scsicmd[0] == WRITE_6 || |
1216 | scsicmd[0] == WRITE_16) | |
1da177e4 | 1217 | tf->flags |= ATA_TFLAG_WRITE; |
1da177e4 | 1218 | |
9a3dccc4 | 1219 | /* Calculate the SCSI LBA, transfer length and FUA. */ |
3aef5231 AL |
1220 | switch (scsicmd[0]) { |
1221 | case READ_10: | |
1222 | case WRITE_10: | |
1223 | scsi_10_lba_len(scsicmd, &block, &n_block); | |
9a3dccc4 TH |
1224 | if (unlikely(scsicmd[1] & (1 << 3))) |
1225 | tf->flags |= ATA_TFLAG_FUA; | |
3aef5231 AL |
1226 | break; |
1227 | case READ_6: | |
1228 | case WRITE_6: | |
1229 | scsi_6_lba_len(scsicmd, &block, &n_block); | |
c187c4b5 AL |
1230 | |
1231 | /* for 6-byte r/w commands, transfer length 0 | |
1232 | * means 256 blocks of data, not 0 block. | |
1233 | */ | |
76b2bf9b JG |
1234 | if (!n_block) |
1235 | n_block = 256; | |
3aef5231 AL |
1236 | break; |
1237 | case READ_16: | |
1238 | case WRITE_16: | |
1239 | scsi_16_lba_len(scsicmd, &block, &n_block); | |
9a3dccc4 TH |
1240 | if (unlikely(scsicmd[1] & (1 << 3))) |
1241 | tf->flags |= ATA_TFLAG_FUA; | |
3aef5231 AL |
1242 | break; |
1243 | default: | |
8bf62ece | 1244 | DPRINTK("no-byte command\n"); |
ae006510 | 1245 | goto invalid_fld; |
1da177e4 LT |
1246 | } |
1247 | ||
8bf62ece AL |
1248 | /* Check and compose ATA command */ |
1249 | if (!n_block) | |
c187c4b5 AL |
1250 | /* For 10-byte and 16-byte SCSI R/W commands, transfer |
1251 | * length 0 means transfer 0 block of data. | |
1252 | * However, for ATA R/W commands, sector count 0 means | |
1253 | * 256 or 65536 sectors, not 0 sectors as in SCSI. | |
f51750d5 AC |
1254 | * |
1255 | * WARNING: one or two older ATA drives treat 0 as 0... | |
c187c4b5 | 1256 | */ |
ae006510 | 1257 | goto nothing_to_do; |
1da177e4 | 1258 | |
07506697 AL |
1259 | if (dev->flags & ATA_DFLAG_LBA) { |
1260 | tf->flags |= ATA_TFLAG_LBA; | |
1261 | ||
c6a33e24 AL |
1262 | if (lba_28_ok(block, n_block)) { |
1263 | /* use LBA28 */ | |
1264 | tf->device |= (block >> 24) & 0xf; | |
1265 | } else if (lba_48_ok(block, n_block)) { | |
1266 | if (!(dev->flags & ATA_DFLAG_LBA48)) | |
ae006510 | 1267 | goto out_of_range; |
1da177e4 | 1268 | |
07506697 AL |
1269 | /* use LBA48 */ |
1270 | tf->flags |= ATA_TFLAG_LBA48; | |
1271 | ||
8bf62ece AL |
1272 | tf->hob_nsect = (n_block >> 8) & 0xff; |
1273 | ||
1274 | tf->hob_lbah = (block >> 40) & 0xff; | |
1275 | tf->hob_lbam = (block >> 32) & 0xff; | |
1276 | tf->hob_lbal = (block >> 24) & 0xff; | |
c6a33e24 AL |
1277 | } else |
1278 | /* request too large even for LBA48 */ | |
1279 | goto out_of_range; | |
c187c4b5 | 1280 | |
9a3dccc4 TH |
1281 | if (unlikely(ata_rwcmd_protocol(qc) < 0)) |
1282 | goto invalid_fld; | |
8cbd6df1 | 1283 | |
8bf62ece AL |
1284 | qc->nsect = n_block; |
1285 | tf->nsect = n_block & 0xff; | |
1da177e4 | 1286 | |
8bf62ece AL |
1287 | tf->lbah = (block >> 16) & 0xff; |
1288 | tf->lbam = (block >> 8) & 0xff; | |
1289 | tf->lbal = block & 0xff; | |
1da177e4 | 1290 | |
8bf62ece AL |
1291 | tf->device |= ATA_LBA; |
1292 | } else { | |
1293 | /* CHS */ | |
1294 | u32 sect, head, cyl, track; | |
1295 | ||
1296 | /* The request -may- be too large for CHS addressing. */ | |
c6a33e24 | 1297 | if (!lba_28_ok(block, n_block)) |
ae006510 | 1298 | goto out_of_range; |
c187c4b5 | 1299 | |
9a3dccc4 TH |
1300 | if (unlikely(ata_rwcmd_protocol(qc) < 0)) |
1301 | goto invalid_fld; | |
8cbd6df1 | 1302 | |
8bf62ece AL |
1303 | /* Convert LBA to CHS */ |
1304 | track = (u32)block / dev->sectors; | |
1305 | cyl = track / dev->heads; | |
1306 | head = track % dev->heads; | |
1307 | sect = (u32)block % dev->sectors + 1; | |
1308 | ||
c187c4b5 | 1309 | DPRINTK("block %u track %u cyl %u head %u sect %u\n", |
8bf62ece | 1310 | (u32)block, track, cyl, head, sect); |
c187c4b5 | 1311 | |
8bf62ece AL |
1312 | /* Check whether the converted CHS can fit. |
1313 | Cylinder: 0-65535 | |
1314 | Head: 0-15 | |
1315 | Sector: 1-255*/ | |
c187c4b5 | 1316 | if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect)) |
ae006510 | 1317 | goto out_of_range; |
c187c4b5 | 1318 | |
8bf62ece AL |
1319 | qc->nsect = n_block; |
1320 | tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */ | |
1321 | tf->lbal = sect; | |
1322 | tf->lbam = cyl; | |
1323 | tf->lbah = cyl >> 8; | |
1324 | tf->device |= head; | |
1da177e4 LT |
1325 | } |
1326 | ||
8bf62ece | 1327 | return 0; |
ae006510 DG |
1328 | |
1329 | invalid_fld: | |
1330 | ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0); | |
1331 | /* "Invalid field in cbd" */ | |
1332 | return 1; | |
1333 | ||
1334 | out_of_range: | |
1335 | ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0); | |
1336 | /* "Logical Block Address out of range" */ | |
1337 | return 1; | |
1338 | ||
1339 | nothing_to_do: | |
1340 | qc->scsicmd->result = SAM_STAT_GOOD; | |
1341 | return 1; | |
1da177e4 LT |
1342 | } |
1343 | ||
77853bf2 | 1344 | static void ata_scsi_qc_complete(struct ata_queued_cmd *qc) |
1da177e4 LT |
1345 | { |
1346 | struct scsi_cmnd *cmd = qc->scsicmd; | |
a7dac447 | 1347 | u8 *cdb = cmd->cmnd; |
a22e2eb0 | 1348 | int need_sense = (qc->err_mask != 0); |
b095518e JG |
1349 | |
1350 | /* For ATA pass thru (SAT) commands, generate a sense block if | |
1351 | * user mandated it or if there's an error. Note that if we | |
1352 | * generate because the user forced us to, a check condition | |
1353 | * is generated and the ATA register values are returned | |
1354 | * whether the command completed successfully or not. If there | |
1355 | * was no error, SK, ASC and ASCQ will all be zero. | |
1356 | */ | |
a7dac447 JG |
1357 | if (((cdb[0] == ATA_16) || (cdb[0] == ATA_12)) && |
1358 | ((cdb[2] & 0x20) || need_sense)) { | |
b095518e JG |
1359 | ata_gen_ata_desc_sense(qc); |
1360 | } else { | |
1361 | if (!need_sense) { | |
1362 | cmd->result = SAM_STAT_GOOD; | |
1363 | } else { | |
1364 | /* TODO: decide which descriptor format to use | |
1365 | * for 48b LBA devices and call that here | |
1366 | * instead of the fixed desc, which is only | |
1367 | * good for smaller LBA (and maybe CHS?) | |
1368 | * devices. | |
1369 | */ | |
1370 | ata_gen_fixed_sense(qc); | |
1371 | } | |
1372 | } | |
1da177e4 | 1373 | |
b095518e JG |
1374 | if (need_sense) { |
1375 | /* The ata_gen_..._sense routines fill in tf */ | |
1376 | ata_dump_status(qc->ap->id, &qc->tf); | |
1377 | } | |
1da177e4 LT |
1378 | |
1379 | qc->scsidone(cmd); | |
1380 | ||
77853bf2 | 1381 | ata_qc_free(qc); |
1da177e4 LT |
1382 | } |
1383 | ||
1384 | /** | |
1385 | * ata_scsi_translate - Translate then issue SCSI command to ATA device | |
1386 | * @ap: ATA port to which the command is addressed | |
1387 | * @dev: ATA device to which the command is addressed | |
1388 | * @cmd: SCSI command to execute | |
1389 | * @done: SCSI command completion function | |
1390 | * @xlat_func: Actor which translates @cmd to an ATA taskfile | |
1391 | * | |
1392 | * Our ->queuecommand() function has decided that the SCSI | |
1393 | * command issued can be directly translated into an ATA | |
1394 | * command, rather than handled internally. | |
1395 | * | |
1396 | * This function sets up an ata_queued_cmd structure for the | |
1397 | * SCSI command, and sends that ata_queued_cmd to the hardware. | |
1398 | * | |
ae006510 DG |
1399 | * The xlat_func argument (actor) returns 0 if ready to execute |
1400 | * ATA command, else 1 to finish translation. If 1 is returned | |
1401 | * then cmd->result (and possibly cmd->sense_buffer) are assumed | |
1402 | * to be set reflecting an error condition or clean (early) | |
1403 | * termination. | |
1404 | * | |
1da177e4 LT |
1405 | * LOCKING: |
1406 | * spin_lock_irqsave(host_set lock) | |
1407 | */ | |
1408 | ||
1409 | static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev, | |
1410 | struct scsi_cmnd *cmd, | |
1411 | void (*done)(struct scsi_cmnd *), | |
1412 | ata_xlat_func_t xlat_func) | |
1413 | { | |
1414 | struct ata_queued_cmd *qc; | |
1415 | u8 *scsicmd = cmd->cmnd; | |
1416 | ||
1417 | VPRINTK("ENTER\n"); | |
1418 | ||
1419 | qc = ata_scsi_qc_new(ap, dev, cmd, done); | |
1420 | if (!qc) | |
ae006510 | 1421 | goto err_mem; |
1da177e4 LT |
1422 | |
1423 | /* data is present; dma-map it */ | |
be7db055 CH |
1424 | if (cmd->sc_data_direction == DMA_FROM_DEVICE || |
1425 | cmd->sc_data_direction == DMA_TO_DEVICE) { | |
1da177e4 LT |
1426 | if (unlikely(cmd->request_bufflen < 1)) { |
1427 | printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n", | |
1428 | ap->id, dev->devno); | |
ae006510 | 1429 | goto err_did; |
1da177e4 LT |
1430 | } |
1431 | ||
1432 | if (cmd->use_sg) | |
1433 | ata_sg_init(qc, cmd->request_buffer, cmd->use_sg); | |
1434 | else | |
1435 | ata_sg_init_one(qc, cmd->request_buffer, | |
1436 | cmd->request_bufflen); | |
1437 | ||
1438 | qc->dma_dir = cmd->sc_data_direction; | |
1439 | } | |
1440 | ||
1441 | qc->complete_fn = ata_scsi_qc_complete; | |
1442 | ||
1443 | if (xlat_func(qc, scsicmd)) | |
ae006510 | 1444 | goto early_finish; |
1da177e4 LT |
1445 | |
1446 | /* select device, send command to hardware */ | |
9a3d9eb0 TH |
1447 | qc->err_mask = ata_qc_issue(qc); |
1448 | if (qc->err_mask) | |
8e436af9 | 1449 | ata_qc_complete(qc); |
1da177e4 LT |
1450 | |
1451 | VPRINTK("EXIT\n"); | |
1452 | return; | |
1453 | ||
ae006510 DG |
1454 | early_finish: |
1455 | ata_qc_free(qc); | |
1456 | done(cmd); | |
1457 | DPRINTK("EXIT - early finish (good or error)\n"); | |
1458 | return; | |
1459 | ||
1460 | err_did: | |
1da177e4 | 1461 | ata_qc_free(qc); |
ae006510 DG |
1462 | err_mem: |
1463 | cmd->result = (DID_ERROR << 16); | |
1464 | done(cmd); | |
1465 | DPRINTK("EXIT - internal\n"); | |
1466 | return; | |
1da177e4 LT |
1467 | } |
1468 | ||
1469 | /** | |
1470 | * ata_scsi_rbuf_get - Map response buffer. | |
1471 | * @cmd: SCSI command containing buffer to be mapped. | |
1472 | * @buf_out: Pointer to mapped area. | |
1473 | * | |
1474 | * Maps buffer contained within SCSI command @cmd. | |
1475 | * | |
1476 | * LOCKING: | |
1477 | * spin_lock_irqsave(host_set lock) | |
1478 | * | |
1479 | * RETURNS: | |
1480 | * Length of response buffer. | |
1481 | */ | |
1482 | ||
1483 | static unsigned int ata_scsi_rbuf_get(struct scsi_cmnd *cmd, u8 **buf_out) | |
1484 | { | |
1485 | u8 *buf; | |
1486 | unsigned int buflen; | |
1487 | ||
1488 | if (cmd->use_sg) { | |
1489 | struct scatterlist *sg; | |
1490 | ||
1491 | sg = (struct scatterlist *) cmd->request_buffer; | |
1492 | buf = kmap_atomic(sg->page, KM_USER0) + sg->offset; | |
1493 | buflen = sg->length; | |
1494 | } else { | |
1495 | buf = cmd->request_buffer; | |
1496 | buflen = cmd->request_bufflen; | |
1497 | } | |
1498 | ||
1499 | *buf_out = buf; | |
1500 | return buflen; | |
1501 | } | |
1502 | ||
1503 | /** | |
1504 | * ata_scsi_rbuf_put - Unmap response buffer. | |
1505 | * @cmd: SCSI command containing buffer to be unmapped. | |
1506 | * @buf: buffer to unmap | |
1507 | * | |
1508 | * Unmaps response buffer contained within @cmd. | |
1509 | * | |
1510 | * LOCKING: | |
1511 | * spin_lock_irqsave(host_set lock) | |
1512 | */ | |
1513 | ||
1514 | static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf) | |
1515 | { | |
1516 | if (cmd->use_sg) { | |
1517 | struct scatterlist *sg; | |
1518 | ||
1519 | sg = (struct scatterlist *) cmd->request_buffer; | |
1520 | kunmap_atomic(buf - sg->offset, KM_USER0); | |
1521 | } | |
1522 | } | |
1523 | ||
1524 | /** | |
1525 | * ata_scsi_rbuf_fill - wrapper for SCSI command simulators | |
1526 | * @args: device IDENTIFY data / SCSI command of interest. | |
1527 | * @actor: Callback hook for desired SCSI command simulator | |
1528 | * | |
1529 | * Takes care of the hard work of simulating a SCSI command... | |
1530 | * Mapping the response buffer, calling the command's handler, | |
1531 | * and handling the handler's return value. This return value | |
1532 | * indicates whether the handler wishes the SCSI command to be | |
ae006510 DG |
1533 | * completed successfully (0), or not (in which case cmd->result |
1534 | * and sense buffer are assumed to be set). | |
1da177e4 LT |
1535 | * |
1536 | * LOCKING: | |
1537 | * spin_lock_irqsave(host_set lock) | |
1538 | */ | |
1539 | ||
1540 | void ata_scsi_rbuf_fill(struct ata_scsi_args *args, | |
1541 | unsigned int (*actor) (struct ata_scsi_args *args, | |
1542 | u8 *rbuf, unsigned int buflen)) | |
1543 | { | |
1544 | u8 *rbuf; | |
1545 | unsigned int buflen, rc; | |
1546 | struct scsi_cmnd *cmd = args->cmd; | |
1547 | ||
1548 | buflen = ata_scsi_rbuf_get(cmd, &rbuf); | |
1549 | memset(rbuf, 0, buflen); | |
1550 | rc = actor(args, rbuf, buflen); | |
1551 | ata_scsi_rbuf_put(cmd, rbuf); | |
1552 | ||
ae006510 | 1553 | if (rc == 0) |
1da177e4 | 1554 | cmd->result = SAM_STAT_GOOD; |
ae006510 | 1555 | args->done(cmd); |
1da177e4 LT |
1556 | } |
1557 | ||
1558 | /** | |
1559 | * ata_scsiop_inq_std - Simulate INQUIRY command | |
1560 | * @args: device IDENTIFY data / SCSI command of interest. | |
1561 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. | |
1562 | * @buflen: Response buffer length. | |
1563 | * | |
1564 | * Returns standard device identification data associated | |
b142eb65 | 1565 | * with non-VPD INQUIRY command output. |
1da177e4 LT |
1566 | * |
1567 | * LOCKING: | |
1568 | * spin_lock_irqsave(host_set lock) | |
1569 | */ | |
1570 | ||
1571 | unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf, | |
1572 | unsigned int buflen) | |
1573 | { | |
1574 | u8 hdr[] = { | |
1575 | TYPE_DISK, | |
1576 | 0, | |
1577 | 0x5, /* claim SPC-3 version compatibility */ | |
1578 | 2, | |
1579 | 95 - 4 | |
1580 | }; | |
1581 | ||
1582 | /* set scsi removeable (RMB) bit per ata bit */ | |
1583 | if (ata_id_removeable(args->id)) | |
1584 | hdr[1] |= (1 << 7); | |
1585 | ||
1586 | VPRINTK("ENTER\n"); | |
1587 | ||
1588 | memcpy(rbuf, hdr, sizeof(hdr)); | |
1589 | ||
1590 | if (buflen > 35) { | |
1591 | memcpy(&rbuf[8], "ATA ", 8); | |
6a62a04d TH |
1592 | ata_id_string(args->id, &rbuf[16], ATA_ID_PROD_OFS, 16); |
1593 | ata_id_string(args->id, &rbuf[32], ATA_ID_FW_REV_OFS, 4); | |
1da177e4 LT |
1594 | if (rbuf[32] == 0 || rbuf[32] == ' ') |
1595 | memcpy(&rbuf[32], "n/a ", 4); | |
1596 | } | |
1597 | ||
1598 | if (buflen > 63) { | |
1599 | const u8 versions[] = { | |
1600 | 0x60, /* SAM-3 (no version claimed) */ | |
1601 | ||
1602 | 0x03, | |
1603 | 0x20, /* SBC-2 (no version claimed) */ | |
1604 | ||
1605 | 0x02, | |
1606 | 0x60 /* SPC-3 (no version claimed) */ | |
1607 | }; | |
1608 | ||
1609 | memcpy(rbuf + 59, versions, sizeof(versions)); | |
1610 | } | |
1611 | ||
1612 | return 0; | |
1613 | } | |
1614 | ||
1615 | /** | |
b142eb65 | 1616 | * ata_scsiop_inq_00 - Simulate INQUIRY VPD page 0, list of pages |
1da177e4 LT |
1617 | * @args: device IDENTIFY data / SCSI command of interest. |
1618 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. | |
1619 | * @buflen: Response buffer length. | |
1620 | * | |
b142eb65 | 1621 | * Returns list of inquiry VPD pages available. |
1da177e4 LT |
1622 | * |
1623 | * LOCKING: | |
1624 | * spin_lock_irqsave(host_set lock) | |
1625 | */ | |
1626 | ||
1627 | unsigned int ata_scsiop_inq_00(struct ata_scsi_args *args, u8 *rbuf, | |
1628 | unsigned int buflen) | |
1629 | { | |
1630 | const u8 pages[] = { | |
1631 | 0x00, /* page 0x00, this page */ | |
1632 | 0x80, /* page 0x80, unit serial no page */ | |
1633 | 0x83 /* page 0x83, device ident page */ | |
1634 | }; | |
b142eb65 | 1635 | rbuf[3] = sizeof(pages); /* number of supported VPD pages */ |
1da177e4 LT |
1636 | |
1637 | if (buflen > 6) | |
1638 | memcpy(rbuf + 4, pages, sizeof(pages)); | |
1639 | ||
1640 | return 0; | |
1641 | } | |
1642 | ||
1643 | /** | |
b142eb65 | 1644 | * ata_scsiop_inq_80 - Simulate INQUIRY VPD page 80, device serial number |
1da177e4 LT |
1645 | * @args: device IDENTIFY data / SCSI command of interest. |
1646 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. | |
1647 | * @buflen: Response buffer length. | |
1648 | * | |
1649 | * Returns ATA device serial number. | |
1650 | * | |
1651 | * LOCKING: | |
1652 | * spin_lock_irqsave(host_set lock) | |
1653 | */ | |
1654 | ||
1655 | unsigned int ata_scsiop_inq_80(struct ata_scsi_args *args, u8 *rbuf, | |
1656 | unsigned int buflen) | |
1657 | { | |
1658 | const u8 hdr[] = { | |
1659 | 0, | |
1660 | 0x80, /* this page code */ | |
1661 | 0, | |
1662 | ATA_SERNO_LEN, /* page len */ | |
1663 | }; | |
1664 | memcpy(rbuf, hdr, sizeof(hdr)); | |
1665 | ||
1666 | if (buflen > (ATA_SERNO_LEN + 4 - 1)) | |
6a62a04d TH |
1667 | ata_id_string(args->id, (unsigned char *) &rbuf[4], |
1668 | ATA_ID_SERNO_OFS, ATA_SERNO_LEN); | |
1da177e4 LT |
1669 | |
1670 | return 0; | |
1671 | } | |
1672 | ||
1da177e4 | 1673 | /** |
b142eb65 | 1674 | * ata_scsiop_inq_83 - Simulate INQUIRY VPD page 83, device identity |
1da177e4 LT |
1675 | * @args: device IDENTIFY data / SCSI command of interest. |
1676 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. | |
1677 | * @buflen: Response buffer length. | |
1678 | * | |
b142eb65 JG |
1679 | * Yields two logical unit device identification designators: |
1680 | * - vendor specific ASCII containing the ATA serial number | |
1681 | * - SAT defined "t10 vendor id based" containing ASCII vendor | |
1682 | * name ("ATA "), model and serial numbers. | |
1da177e4 LT |
1683 | * |
1684 | * LOCKING: | |
1685 | * spin_lock_irqsave(host_set lock) | |
1686 | */ | |
1687 | ||
1688 | unsigned int ata_scsiop_inq_83(struct ata_scsi_args *args, u8 *rbuf, | |
1689 | unsigned int buflen) | |
1690 | { | |
b142eb65 JG |
1691 | int num; |
1692 | const int sat_model_serial_desc_len = 68; | |
1693 | const int ata_model_byte_len = 40; | |
1da177e4 | 1694 | |
b142eb65 JG |
1695 | rbuf[1] = 0x83; /* this page code */ |
1696 | num = 4; | |
1697 | ||
1698 | if (buflen > (ATA_SERNO_LEN + num + 3)) { | |
1699 | /* piv=0, assoc=lu, code_set=ACSII, designator=vendor */ | |
1700 | rbuf[num + 0] = 2; | |
1701 | rbuf[num + 3] = ATA_SERNO_LEN; | |
1702 | num += 4; | |
1703 | ata_id_string(args->id, (unsigned char *) rbuf + num, | |
1704 | ATA_ID_SERNO_OFS, ATA_SERNO_LEN); | |
1705 | num += ATA_SERNO_LEN; | |
1da177e4 | 1706 | } |
b142eb65 JG |
1707 | if (buflen > (sat_model_serial_desc_len + num + 3)) { |
1708 | /* SAT defined lu model and serial numbers descriptor */ | |
1709 | /* piv=0, assoc=lu, code_set=ACSII, designator=t10 vendor id */ | |
1710 | rbuf[num + 0] = 2; | |
1711 | rbuf[num + 1] = 1; | |
1712 | rbuf[num + 3] = sat_model_serial_desc_len; | |
1713 | num += 4; | |
1714 | memcpy(rbuf + num, "ATA ", 8); | |
1715 | num += 8; | |
1716 | ata_id_string(args->id, (unsigned char *) rbuf + num, | |
1717 | ATA_ID_PROD_OFS, ata_model_byte_len); | |
1718 | num += ata_model_byte_len; | |
1719 | ata_id_string(args->id, (unsigned char *) rbuf + num, | |
1720 | ATA_ID_SERNO_OFS, ATA_SERNO_LEN); | |
1721 | num += ATA_SERNO_LEN; | |
1722 | } | |
1723 | rbuf[3] = num - 4; /* page len (assume less than 256 bytes) */ | |
1da177e4 LT |
1724 | return 0; |
1725 | } | |
1726 | ||
1727 | /** | |
0cba632b | 1728 | * ata_scsiop_noop - Command handler that simply returns success. |
1da177e4 LT |
1729 | * @args: device IDENTIFY data / SCSI command of interest. |
1730 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. | |
1731 | * @buflen: Response buffer length. | |
1732 | * | |
1733 | * No operation. Simply returns success to caller, to indicate | |
1734 | * that the caller should successfully complete this SCSI command. | |
1735 | * | |
1736 | * LOCKING: | |
1737 | * spin_lock_irqsave(host_set lock) | |
1738 | */ | |
1739 | ||
1740 | unsigned int ata_scsiop_noop(struct ata_scsi_args *args, u8 *rbuf, | |
1741 | unsigned int buflen) | |
1742 | { | |
1743 | VPRINTK("ENTER\n"); | |
1744 | return 0; | |
1745 | } | |
1746 | ||
1747 | /** | |
1748 | * ata_msense_push - Push data onto MODE SENSE data output buffer | |
1749 | * @ptr_io: (input/output) Location to store more output data | |
1750 | * @last: End of output data buffer | |
1751 | * @buf: Pointer to BLOB being added to output buffer | |
1752 | * @buflen: Length of BLOB | |
1753 | * | |
1754 | * Store MODE SENSE data on an output buffer. | |
1755 | * | |
1756 | * LOCKING: | |
1757 | * None. | |
1758 | */ | |
1759 | ||
1760 | static void ata_msense_push(u8 **ptr_io, const u8 *last, | |
1761 | const u8 *buf, unsigned int buflen) | |
1762 | { | |
1763 | u8 *ptr = *ptr_io; | |
1764 | ||
1765 | if ((ptr + buflen - 1) > last) | |
1766 | return; | |
1767 | ||
1768 | memcpy(ptr, buf, buflen); | |
1769 | ||
1770 | ptr += buflen; | |
1771 | ||
1772 | *ptr_io = ptr; | |
1773 | } | |
1774 | ||
1775 | /** | |
1776 | * ata_msense_caching - Simulate MODE SENSE caching info page | |
1777 | * @id: device IDENTIFY data | |
1778 | * @ptr_io: (input/output) Location to store more output data | |
1779 | * @last: End of output data buffer | |
1780 | * | |
1781 | * Generate a caching info page, which conditionally indicates | |
1782 | * write caching to the SCSI layer, depending on device | |
1783 | * capabilities. | |
1784 | * | |
1785 | * LOCKING: | |
1786 | * None. | |
1787 | */ | |
1788 | ||
1789 | static unsigned int ata_msense_caching(u16 *id, u8 **ptr_io, | |
1790 | const u8 *last) | |
1791 | { | |
00ac37f5 | 1792 | u8 page[CACHE_MPAGE_LEN]; |
1da177e4 | 1793 | |
00ac37f5 | 1794 | memcpy(page, def_cache_mpage, sizeof(page)); |
1da177e4 LT |
1795 | if (ata_id_wcache_enabled(id)) |
1796 | page[2] |= (1 << 2); /* write cache enable */ | |
1797 | if (!ata_id_rahead_enabled(id)) | |
1798 | page[12] |= (1 << 5); /* disable read ahead */ | |
1799 | ||
1800 | ata_msense_push(ptr_io, last, page, sizeof(page)); | |
1801 | return sizeof(page); | |
1802 | } | |
1803 | ||
1804 | /** | |
1805 | * ata_msense_ctl_mode - Simulate MODE SENSE control mode page | |
1806 | * @dev: Device associated with this MODE SENSE command | |
1807 | * @ptr_io: (input/output) Location to store more output data | |
1808 | * @last: End of output data buffer | |
1809 | * | |
1810 | * Generate a generic MODE SENSE control mode page. | |
1811 | * | |
1812 | * LOCKING: | |
1813 | * None. | |
1814 | */ | |
1815 | ||
1816 | static unsigned int ata_msense_ctl_mode(u8 **ptr_io, const u8 *last) | |
1817 | { | |
00ac37f5 DG |
1818 | ata_msense_push(ptr_io, last, def_control_mpage, |
1819 | sizeof(def_control_mpage)); | |
1820 | return sizeof(def_control_mpage); | |
1da177e4 LT |
1821 | } |
1822 | ||
1823 | /** | |
1824 | * ata_msense_rw_recovery - Simulate MODE SENSE r/w error recovery page | |
1825 | * @dev: Device associated with this MODE SENSE command | |
1826 | * @ptr_io: (input/output) Location to store more output data | |
1827 | * @last: End of output data buffer | |
1828 | * | |
1829 | * Generate a generic MODE SENSE r/w error recovery page. | |
1830 | * | |
1831 | * LOCKING: | |
1832 | * None. | |
1833 | */ | |
1834 | ||
1835 | static unsigned int ata_msense_rw_recovery(u8 **ptr_io, const u8 *last) | |
1836 | { | |
1da177e4 | 1837 | |
00ac37f5 DG |
1838 | ata_msense_push(ptr_io, last, def_rw_recovery_mpage, |
1839 | sizeof(def_rw_recovery_mpage)); | |
1840 | return sizeof(def_rw_recovery_mpage); | |
1da177e4 LT |
1841 | } |
1842 | ||
48bdc8ec JA |
1843 | /* |
1844 | * We can turn this into a real blacklist if it's needed, for now just | |
1845 | * blacklist any Maxtor BANC1G10 revision firmware | |
1846 | */ | |
1847 | static int ata_dev_supports_fua(u16 *id) | |
1848 | { | |
1849 | unsigned char model[41], fw[9]; | |
1850 | ||
c3c013a2 JG |
1851 | if (!libata_fua) |
1852 | return 0; | |
48bdc8ec JA |
1853 | if (!ata_id_has_fua(id)) |
1854 | return 0; | |
1855 | ||
6a62a04d TH |
1856 | ata_id_c_string(id, model, ATA_ID_PROD_OFS, sizeof(model)); |
1857 | ata_id_c_string(id, fw, ATA_ID_FW_REV_OFS, sizeof(fw)); | |
48bdc8ec | 1858 | |
2e02671d | 1859 | if (strcmp(model, "Maxtor")) |
48bdc8ec | 1860 | return 1; |
2e02671d | 1861 | if (strcmp(fw, "BANC1G10")) |
48bdc8ec JA |
1862 | return 1; |
1863 | ||
1864 | return 0; /* blacklisted */ | |
1865 | } | |
1866 | ||
1da177e4 LT |
1867 | /** |
1868 | * ata_scsiop_mode_sense - Simulate MODE SENSE 6, 10 commands | |
1869 | * @args: device IDENTIFY data / SCSI command of interest. | |
1870 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. | |
1871 | * @buflen: Response buffer length. | |
1872 | * | |
00ac37f5 DG |
1873 | * Simulate MODE SENSE commands. Assume this is invoked for direct |
1874 | * access devices (e.g. disks) only. There should be no block | |
1875 | * descriptor for other device types. | |
1da177e4 LT |
1876 | * |
1877 | * LOCKING: | |
1878 | * spin_lock_irqsave(host_set lock) | |
1879 | */ | |
1880 | ||
1881 | unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf, | |
1882 | unsigned int buflen) | |
1883 | { | |
9a3dccc4 | 1884 | struct ata_device *dev = args->dev; |
1da177e4 | 1885 | u8 *scsicmd = args->cmd->cmnd, *p, *last; |
00ac37f5 DG |
1886 | const u8 sat_blk_desc[] = { |
1887 | 0, 0, 0, 0, /* number of blocks: sat unspecified */ | |
1888 | 0, | |
1889 | 0, 0x2, 0x0 /* block length: 512 bytes */ | |
1890 | }; | |
1891 | u8 pg, spg; | |
1892 | unsigned int ebd, page_control, six_byte, output_len, alloc_len, minlen; | |
9a3dccc4 | 1893 | u8 dpofua; |
1da177e4 LT |
1894 | |
1895 | VPRINTK("ENTER\n"); | |
1896 | ||
1897 | six_byte = (scsicmd[0] == MODE_SENSE); | |
00ac37f5 DG |
1898 | ebd = !(scsicmd[1] & 0x8); /* dbd bit inverted == edb */ |
1899 | /* | |
1900 | * LLBA bit in msense(10) ignored (compliant) | |
1da177e4 | 1901 | */ |
00ac37f5 | 1902 | |
1da177e4 | 1903 | page_control = scsicmd[2] >> 6; |
ae006510 DG |
1904 | switch (page_control) { |
1905 | case 0: /* current */ | |
1906 | break; /* supported */ | |
1907 | case 3: /* saved */ | |
1908 | goto saving_not_supp; | |
1909 | case 1: /* changeable */ | |
1910 | case 2: /* defaults */ | |
1911 | default: | |
1912 | goto invalid_fld; | |
1913 | } | |
1da177e4 | 1914 | |
00ac37f5 DG |
1915 | if (six_byte) { |
1916 | output_len = 4 + (ebd ? 8 : 0); | |
1917 | alloc_len = scsicmd[4]; | |
1918 | } else { | |
1919 | output_len = 8 + (ebd ? 8 : 0); | |
1920 | alloc_len = (scsicmd[7] << 8) + scsicmd[8]; | |
1921 | } | |
1922 | minlen = (alloc_len < buflen) ? alloc_len : buflen; | |
1da177e4 LT |
1923 | |
1924 | p = rbuf + output_len; | |
00ac37f5 | 1925 | last = rbuf + minlen - 1; |
1da177e4 | 1926 | |
00ac37f5 DG |
1927 | pg = scsicmd[2] & 0x3f; |
1928 | spg = scsicmd[3]; | |
1929 | /* | |
1930 | * No mode subpages supported (yet) but asking for _all_ | |
1931 | * subpages may be valid | |
1932 | */ | |
1933 | if (spg && (spg != ALL_SUB_MPAGES)) | |
1934 | goto invalid_fld; | |
1935 | ||
1936 | switch(pg) { | |
1937 | case RW_RECOVERY_MPAGE: | |
1da177e4 LT |
1938 | output_len += ata_msense_rw_recovery(&p, last); |
1939 | break; | |
1940 | ||
00ac37f5 | 1941 | case CACHE_MPAGE: |
1da177e4 LT |
1942 | output_len += ata_msense_caching(args->id, &p, last); |
1943 | break; | |
1944 | ||
00ac37f5 | 1945 | case CONTROL_MPAGE: { |
1da177e4 LT |
1946 | output_len += ata_msense_ctl_mode(&p, last); |
1947 | break; | |
1948 | } | |
1949 | ||
00ac37f5 | 1950 | case ALL_MPAGES: |
1da177e4 LT |
1951 | output_len += ata_msense_rw_recovery(&p, last); |
1952 | output_len += ata_msense_caching(args->id, &p, last); | |
1953 | output_len += ata_msense_ctl_mode(&p, last); | |
1954 | break; | |
1955 | ||
1956 | default: /* invalid page code */ | |
ae006510 | 1957 | goto invalid_fld; |
1da177e4 LT |
1958 | } |
1959 | ||
00ac37f5 DG |
1960 | if (minlen < 1) |
1961 | return 0; | |
9a3dccc4 TH |
1962 | |
1963 | dpofua = 0; | |
48bdc8ec | 1964 | if (ata_dev_supports_fua(args->id) && dev->flags & ATA_DFLAG_LBA48 && |
9a3dccc4 TH |
1965 | (!(dev->flags & ATA_DFLAG_PIO) || dev->multi_count)) |
1966 | dpofua = 1 << 4; | |
1967 | ||
1da177e4 LT |
1968 | if (six_byte) { |
1969 | output_len--; | |
1970 | rbuf[0] = output_len; | |
9a3dccc4 TH |
1971 | if (minlen > 2) |
1972 | rbuf[2] |= dpofua; | |
00ac37f5 DG |
1973 | if (ebd) { |
1974 | if (minlen > 3) | |
1975 | rbuf[3] = sizeof(sat_blk_desc); | |
1976 | if (minlen > 11) | |
1977 | memcpy(rbuf + 4, sat_blk_desc, | |
1978 | sizeof(sat_blk_desc)); | |
1979 | } | |
1da177e4 LT |
1980 | } else { |
1981 | output_len -= 2; | |
1982 | rbuf[0] = output_len >> 8; | |
00ac37f5 DG |
1983 | if (minlen > 1) |
1984 | rbuf[1] = output_len; | |
9a3dccc4 TH |
1985 | if (minlen > 3) |
1986 | rbuf[3] |= dpofua; | |
00ac37f5 DG |
1987 | if (ebd) { |
1988 | if (minlen > 7) | |
1989 | rbuf[7] = sizeof(sat_blk_desc); | |
1990 | if (minlen > 15) | |
1991 | memcpy(rbuf + 8, sat_blk_desc, | |
1992 | sizeof(sat_blk_desc)); | |
1993 | } | |
1da177e4 | 1994 | } |
1da177e4 | 1995 | return 0; |
ae006510 DG |
1996 | |
1997 | invalid_fld: | |
1998 | ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0); | |
1999 | /* "Invalid field in cbd" */ | |
2000 | return 1; | |
2001 | ||
2002 | saving_not_supp: | |
2003 | ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0); | |
2004 | /* "Saving parameters not supported" */ | |
2005 | return 1; | |
1da177e4 LT |
2006 | } |
2007 | ||
2008 | /** | |
2009 | * ata_scsiop_read_cap - Simulate READ CAPACITY[ 16] commands | |
2010 | * @args: device IDENTIFY data / SCSI command of interest. | |
2011 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. | |
2012 | * @buflen: Response buffer length. | |
2013 | * | |
2014 | * Simulate READ CAPACITY commands. | |
2015 | * | |
2016 | * LOCKING: | |
2017 | * spin_lock_irqsave(host_set lock) | |
2018 | */ | |
2019 | ||
2020 | unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf, | |
2021 | unsigned int buflen) | |
2022 | { | |
2023 | u64 n_sectors; | |
2024 | u32 tmp; | |
2025 | ||
2026 | VPRINTK("ENTER\n"); | |
2027 | ||
8bf62ece AL |
2028 | if (ata_id_has_lba(args->id)) { |
2029 | if (ata_id_has_lba48(args->id)) | |
2030 | n_sectors = ata_id_u64(args->id, 100); | |
2031 | else | |
2032 | n_sectors = ata_id_u32(args->id, 60); | |
2033 | } else { | |
2034 | /* CHS default translation */ | |
2035 | n_sectors = args->id[1] * args->id[3] * args->id[6]; | |
2036 | ||
2037 | if (ata_id_current_chs_valid(args->id)) | |
2038 | /* CHS current translation */ | |
2039 | n_sectors = ata_id_u32(args->id, 57); | |
2040 | } | |
2041 | ||
1da177e4 LT |
2042 | n_sectors--; /* ATA TotalUserSectors - 1 */ |
2043 | ||
1da177e4 | 2044 | if (args->cmd->cmnd[0] == READ_CAPACITY) { |
0c144d0d PP |
2045 | if( n_sectors >= 0xffffffffULL ) |
2046 | tmp = 0xffffffff ; /* Return max count on overflow */ | |
2047 | else | |
2048 | tmp = n_sectors ; | |
2049 | ||
1da177e4 LT |
2050 | /* sector count, 32-bit */ |
2051 | rbuf[0] = tmp >> (8 * 3); | |
2052 | rbuf[1] = tmp >> (8 * 2); | |
2053 | rbuf[2] = tmp >> (8 * 1); | |
2054 | rbuf[3] = tmp; | |
2055 | ||
2056 | /* sector size */ | |
2057 | tmp = ATA_SECT_SIZE; | |
2058 | rbuf[6] = tmp >> 8; | |
2059 | rbuf[7] = tmp; | |
2060 | ||
2061 | } else { | |
2062 | /* sector count, 64-bit */ | |
0c144d0d PP |
2063 | tmp = n_sectors >> (8 * 4); |
2064 | rbuf[2] = tmp >> (8 * 3); | |
2065 | rbuf[3] = tmp >> (8 * 2); | |
2066 | rbuf[4] = tmp >> (8 * 1); | |
2067 | rbuf[5] = tmp; | |
2068 | tmp = n_sectors; | |
1da177e4 LT |
2069 | rbuf[6] = tmp >> (8 * 3); |
2070 | rbuf[7] = tmp >> (8 * 2); | |
2071 | rbuf[8] = tmp >> (8 * 1); | |
2072 | rbuf[9] = tmp; | |
2073 | ||
2074 | /* sector size */ | |
2075 | tmp = ATA_SECT_SIZE; | |
2076 | rbuf[12] = tmp >> 8; | |
2077 | rbuf[13] = tmp; | |
2078 | } | |
2079 | ||
2080 | return 0; | |
2081 | } | |
2082 | ||
2083 | /** | |
2084 | * ata_scsiop_report_luns - Simulate REPORT LUNS command | |
2085 | * @args: device IDENTIFY data / SCSI command of interest. | |
2086 | * @rbuf: Response buffer, to which simulated SCSI cmd output is sent. | |
2087 | * @buflen: Response buffer length. | |
2088 | * | |
2089 | * Simulate REPORT LUNS command. | |
2090 | * | |
2091 | * LOCKING: | |
2092 | * spin_lock_irqsave(host_set lock) | |
2093 | */ | |
2094 | ||
2095 | unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf, | |
2096 | unsigned int buflen) | |
2097 | { | |
2098 | VPRINTK("ENTER\n"); | |
2099 | rbuf[3] = 8; /* just one lun, LUN 0, size 8 bytes */ | |
2100 | ||
2101 | return 0; | |
2102 | } | |
2103 | ||
845c5834 DG |
2104 | /** |
2105 | * ata_scsi_set_sense - Set SCSI sense data and status | |
2106 | * @cmd: SCSI request to be handled | |
2107 | * @sk: SCSI-defined sense key | |
2108 | * @asc: SCSI-defined additional sense code | |
2109 | * @ascq: SCSI-defined additional sense code qualifier | |
2110 | * | |
2111 | * Helper function that builds a valid fixed format, current | |
2112 | * response code and the given sense key (sk), additional sense | |
2113 | * code (asc) and additional sense code qualifier (ascq) with | |
2114 | * a SCSI command status of %SAM_STAT_CHECK_CONDITION and | |
2115 | * DRIVER_SENSE set in the upper bits of scsi_cmnd::result . | |
2116 | * | |
2117 | * LOCKING: | |
2118 | * Not required | |
2119 | */ | |
2120 | ||
2121 | void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq) | |
2122 | { | |
2123 | cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; | |
2124 | ||
2125 | cmd->sense_buffer[0] = 0x70; /* fixed format, current */ | |
2126 | cmd->sense_buffer[2] = sk; | |
2127 | cmd->sense_buffer[7] = 18 - 8; /* additional sense length */ | |
2128 | cmd->sense_buffer[12] = asc; | |
2129 | cmd->sense_buffer[13] = ascq; | |
2130 | } | |
2131 | ||
1da177e4 LT |
2132 | /** |
2133 | * ata_scsi_badcmd - End a SCSI request with an error | |
2134 | * @cmd: SCSI request to be handled | |
2135 | * @done: SCSI command completion function | |
2136 | * @asc: SCSI-defined additional sense code | |
2137 | * @ascq: SCSI-defined additional sense code qualifier | |
2138 | * | |
2139 | * Helper function that completes a SCSI command with | |
2140 | * %SAM_STAT_CHECK_CONDITION, with a sense key %ILLEGAL_REQUEST | |
2141 | * and the specified additional sense codes. | |
2142 | * | |
2143 | * LOCKING: | |
2144 | * spin_lock_irqsave(host_set lock) | |
2145 | */ | |
2146 | ||
2147 | void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq) | |
2148 | { | |
2149 | DPRINTK("ENTER\n"); | |
ae006510 | 2150 | ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq); |
1da177e4 LT |
2151 | |
2152 | done(cmd); | |
2153 | } | |
2154 | ||
77853bf2 | 2155 | static void atapi_sense_complete(struct ata_queued_cmd *qc) |
a939c963 | 2156 | { |
a22e2eb0 | 2157 | if (qc->err_mask && ((qc->err_mask & AC_ERR_DEV) == 0)) |
c6e6e666 JG |
2158 | /* FIXME: not quite right; we don't want the |
2159 | * translation of taskfile registers into | |
2160 | * a sense descriptors, since that's only | |
2161 | * correct for ATA, not ATAPI | |
2162 | */ | |
2163 | ata_gen_ata_desc_sense(qc); | |
a939c963 | 2164 | |
c6e6e666 | 2165 | qc->scsidone(qc->scsicmd); |
77853bf2 | 2166 | ata_qc_free(qc); |
c6e6e666 | 2167 | } |
a939c963 | 2168 | |
c6e6e666 JG |
2169 | /* is it pointless to prefer PIO for "safety reasons"? */ |
2170 | static inline int ata_pio_use_silly(struct ata_port *ap) | |
2171 | { | |
2172 | return (ap->flags & ATA_FLAG_PIO_DMA); | |
2173 | } | |
2174 | ||
2175 | static void atapi_request_sense(struct ata_queued_cmd *qc) | |
2176 | { | |
2177 | struct ata_port *ap = qc->ap; | |
2178 | struct scsi_cmnd *cmd = qc->scsicmd; | |
2179 | ||
2180 | DPRINTK("ATAPI request sense\n"); | |
a939c963 JG |
2181 | |
2182 | /* FIXME: is this needed? */ | |
2183 | memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer)); | |
2184 | ||
c6e6e666 JG |
2185 | ap->ops->tf_read(ap, &qc->tf); |
2186 | ||
2187 | /* fill these in, for the case where they are -not- overwritten */ | |
2188 | cmd->sense_buffer[0] = 0x70; | |
2189 | cmd->sense_buffer[2] = qc->tf.feature >> 4; | |
2190 | ||
2191 | ata_qc_reinit(qc); | |
2192 | ||
a939c963 JG |
2193 | ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer)); |
2194 | qc->dma_dir = DMA_FROM_DEVICE; | |
2195 | ||
6e7846e9 | 2196 | memset(&qc->cdb, 0, qc->dev->cdb_len); |
a939c963 JG |
2197 | qc->cdb[0] = REQUEST_SENSE; |
2198 | qc->cdb[4] = SCSI_SENSE_BUFFERSIZE; | |
2199 | ||
2200 | qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; | |
2201 | qc->tf.command = ATA_CMD_PACKET; | |
2202 | ||
c6e6e666 JG |
2203 | if (ata_pio_use_silly(ap)) { |
2204 | qc->tf.protocol = ATA_PROT_ATAPI_DMA; | |
2205 | qc->tf.feature |= ATAPI_PKT_DMA; | |
2206 | } else { | |
2207 | qc->tf.protocol = ATA_PROT_ATAPI; | |
2208 | qc->tf.lbam = (8 * 1024) & 0xff; | |
2209 | qc->tf.lbah = (8 * 1024) >> 8; | |
2210 | } | |
a939c963 JG |
2211 | qc->nbytes = SCSI_SENSE_BUFFERSIZE; |
2212 | ||
c6e6e666 | 2213 | qc->complete_fn = atapi_sense_complete; |
a939c963 | 2214 | |
9a3d9eb0 TH |
2215 | qc->err_mask = ata_qc_issue(qc); |
2216 | if (qc->err_mask) | |
a22e2eb0 | 2217 | ata_qc_complete(qc); |
a939c963 JG |
2218 | |
2219 | DPRINTK("EXIT\n"); | |
2220 | } | |
2221 | ||
77853bf2 | 2222 | static void atapi_qc_complete(struct ata_queued_cmd *qc) |
1da177e4 LT |
2223 | { |
2224 | struct scsi_cmnd *cmd = qc->scsicmd; | |
a22e2eb0 | 2225 | unsigned int err_mask = qc->err_mask; |
1da177e4 | 2226 | |
a7dac447 | 2227 | VPRINTK("ENTER, err_mask 0x%X\n", err_mask); |
e12669e7 | 2228 | |
a7dac447 | 2229 | if (unlikely(err_mask & AC_ERR_DEV)) { |
1da177e4 | 2230 | cmd->result = SAM_STAT_CHECK_CONDITION; |
c6e6e666 | 2231 | atapi_request_sense(qc); |
77853bf2 | 2232 | return; |
e12669e7 JG |
2233 | } |
2234 | ||
a7dac447 JG |
2235 | else if (unlikely(err_mask)) |
2236 | /* FIXME: not quite right; we don't want the | |
2237 | * translation of taskfile registers into | |
2238 | * a sense descriptors, since that's only | |
2239 | * correct for ATA, not ATAPI | |
2240 | */ | |
2241 | ata_gen_ata_desc_sense(qc); | |
2242 | ||
e12669e7 | 2243 | else { |
1da177e4 LT |
2244 | u8 *scsicmd = cmd->cmnd; |
2245 | ||
fd71da46 | 2246 | if ((scsicmd[0] == INQUIRY) && ((scsicmd[1] & 0x03) == 0)) { |
1da177e4 LT |
2247 | u8 *buf = NULL; |
2248 | unsigned int buflen; | |
2249 | ||
2250 | buflen = ata_scsi_rbuf_get(cmd, &buf); | |
a15dbeb4 JG |
2251 | |
2252 | /* ATAPI devices typically report zero for their SCSI version, | |
2253 | * and sometimes deviate from the spec WRT response data | |
2254 | * format. If SCSI version is reported as zero like normal, | |
2255 | * then we make the following fixups: 1) Fake MMC-5 version, | |
2256 | * to indicate to the Linux scsi midlayer this is a modern | |
2257 | * device. 2) Ensure response data format / ATAPI information | |
2258 | * are always correct. | |
a15dbeb4 JG |
2259 | */ |
2260 | if (buf[2] == 0) { | |
2261 | buf[2] = 0x5; | |
2262 | buf[3] = 0x32; | |
2263 | } | |
2264 | ||
1da177e4 LT |
2265 | ata_scsi_rbuf_put(cmd, buf); |
2266 | } | |
a15dbeb4 | 2267 | |
1da177e4 LT |
2268 | cmd->result = SAM_STAT_GOOD; |
2269 | } | |
2270 | ||
2271 | qc->scsidone(cmd); | |
77853bf2 | 2272 | ata_qc_free(qc); |
1da177e4 LT |
2273 | } |
2274 | /** | |
2275 | * atapi_xlat - Initialize PACKET taskfile | |
2276 | * @qc: command structure to be initialized | |
2277 | * @scsicmd: SCSI CDB associated with this PACKET command | |
2278 | * | |
2279 | * LOCKING: | |
2280 | * spin_lock_irqsave(host_set lock) | |
2281 | * | |
2282 | * RETURNS: | |
2283 | * Zero on success, non-zero on failure. | |
2284 | */ | |
2285 | ||
057ace5e | 2286 | static unsigned int atapi_xlat(struct ata_queued_cmd *qc, const u8 *scsicmd) |
1da177e4 LT |
2287 | { |
2288 | struct scsi_cmnd *cmd = qc->scsicmd; | |
2289 | struct ata_device *dev = qc->dev; | |
2290 | int using_pio = (dev->flags & ATA_DFLAG_PIO); | |
be7db055 | 2291 | int nodata = (cmd->sc_data_direction == DMA_NONE); |
1da177e4 LT |
2292 | |
2293 | if (!using_pio) | |
2294 | /* Check whether ATAPI DMA is safe */ | |
2295 | if (ata_check_atapi_dma(qc)) | |
2296 | using_pio = 1; | |
2297 | ||
6e7846e9 | 2298 | memcpy(&qc->cdb, scsicmd, dev->cdb_len); |
1da177e4 LT |
2299 | |
2300 | qc->complete_fn = atapi_qc_complete; | |
2301 | ||
2302 | qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; | |
be7db055 | 2303 | if (cmd->sc_data_direction == DMA_TO_DEVICE) { |
1da177e4 LT |
2304 | qc->tf.flags |= ATA_TFLAG_WRITE; |
2305 | DPRINTK("direction: write\n"); | |
2306 | } | |
2307 | ||
2308 | qc->tf.command = ATA_CMD_PACKET; | |
2309 | ||
2310 | /* no data, or PIO data xfer */ | |
2311 | if (using_pio || nodata) { | |
2312 | if (nodata) | |
2313 | qc->tf.protocol = ATA_PROT_ATAPI_NODATA; | |
2314 | else | |
2315 | qc->tf.protocol = ATA_PROT_ATAPI; | |
2316 | qc->tf.lbam = (8 * 1024) & 0xff; | |
2317 | qc->tf.lbah = (8 * 1024) >> 8; | |
2318 | } | |
2319 | ||
2320 | /* DMA data xfer */ | |
2321 | else { | |
2322 | qc->tf.protocol = ATA_PROT_ATAPI_DMA; | |
2323 | qc->tf.feature |= ATAPI_PKT_DMA; | |
2324 | ||
2325 | #ifdef ATAPI_ENABLE_DMADIR | |
2326 | /* some SATA bridges need us to indicate data xfer direction */ | |
be7db055 | 2327 | if (cmd->sc_data_direction != DMA_TO_DEVICE) |
1da177e4 LT |
2328 | qc->tf.feature |= ATAPI_DMADIR; |
2329 | #endif | |
2330 | } | |
2331 | ||
2332 | qc->nbytes = cmd->bufflen; | |
2333 | ||
2334 | return 0; | |
2335 | } | |
2336 | ||
2337 | /** | |
2338 | * ata_scsi_find_dev - lookup ata_device from scsi_cmnd | |
2339 | * @ap: ATA port to which the device is attached | |
2340 | * @scsidev: SCSI device from which we derive the ATA device | |
2341 | * | |
2342 | * Given various information provided in struct scsi_cmnd, | |
2343 | * map that onto an ATA bus, and using that mapping | |
2344 | * determine which ata_device is associated with the | |
2345 | * SCSI command to be sent. | |
2346 | * | |
2347 | * LOCKING: | |
2348 | * spin_lock_irqsave(host_set lock) | |
2349 | * | |
2350 | * RETURNS: | |
2351 | * Associated ATA device, or %NULL if not found. | |
2352 | */ | |
2353 | ||
2354 | static struct ata_device * | |
057ace5e | 2355 | ata_scsi_find_dev(struct ata_port *ap, const struct scsi_device *scsidev) |
1da177e4 LT |
2356 | { |
2357 | struct ata_device *dev; | |
2358 | ||
2359 | /* skip commands not addressed to targets we simulate */ | |
2360 | if (likely(scsidev->id < ATA_MAX_DEVICES)) | |
2361 | dev = &ap->device[scsidev->id]; | |
2362 | else | |
2363 | return NULL; | |
2364 | ||
2365 | if (unlikely((scsidev->channel != 0) || | |
2366 | (scsidev->lun != 0))) | |
2367 | return NULL; | |
2368 | ||
2369 | if (unlikely(!ata_dev_present(dev))) | |
2370 | return NULL; | |
2371 | ||
50630195 JG |
2372 | if (!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) { |
2373 | if (unlikely(dev->class == ATA_DEV_ATAPI)) { | |
2374 | printk(KERN_WARNING "ata%u(%u): WARNING: ATAPI is %s, device ignored.\n", | |
2375 | ap->id, dev->devno, atapi_enabled ? "not supported with this driver" : "disabled"); | |
1623c81e | 2376 | return NULL; |
50630195 | 2377 | } |
1623c81e | 2378 | } |
1da177e4 LT |
2379 | |
2380 | return dev; | |
2381 | } | |
2382 | ||
b095518e JG |
2383 | /* |
2384 | * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value. | |
2385 | * @byte1: Byte 1 from pass-thru CDB. | |
2386 | * | |
2387 | * RETURNS: | |
2388 | * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise. | |
2389 | */ | |
2390 | static u8 | |
2391 | ata_scsi_map_proto(u8 byte1) | |
2392 | { | |
2393 | switch((byte1 & 0x1e) >> 1) { | |
2394 | case 3: /* Non-data */ | |
2395 | return ATA_PROT_NODATA; | |
2396 | ||
2397 | case 6: /* DMA */ | |
2398 | return ATA_PROT_DMA; | |
2399 | ||
2400 | case 4: /* PIO Data-in */ | |
2401 | case 5: /* PIO Data-out */ | |
b095518e JG |
2402 | return ATA_PROT_PIO; |
2403 | ||
2404 | case 10: /* Device Reset */ | |
2405 | case 0: /* Hard Reset */ | |
2406 | case 1: /* SRST */ | |
2407 | case 2: /* Bus Idle */ | |
2408 | case 7: /* Packet */ | |
2409 | case 8: /* DMA Queued */ | |
2410 | case 9: /* Device Diagnostic */ | |
2411 | case 11: /* UDMA Data-in */ | |
2412 | case 12: /* UDMA Data-Out */ | |
2413 | case 13: /* FPDMA */ | |
2414 | default: /* Reserved */ | |
2415 | break; | |
2416 | } | |
2417 | ||
2418 | return ATA_PROT_UNKNOWN; | |
2419 | } | |
2420 | ||
2421 | /** | |
2422 | * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile | |
2423 | * @qc: command structure to be initialized | |
8e8b77dd | 2424 | * @scsicmd: SCSI command to convert |
b095518e JG |
2425 | * |
2426 | * Handles either 12 or 16-byte versions of the CDB. | |
2427 | * | |
2428 | * RETURNS: | |
2429 | * Zero on success, non-zero on failure. | |
2430 | */ | |
2431 | static unsigned int | |
00ac37f5 | 2432 | ata_scsi_pass_thru(struct ata_queued_cmd *qc, const u8 *scsicmd) |
b095518e JG |
2433 | { |
2434 | struct ata_taskfile *tf = &(qc->tf); | |
2435 | struct scsi_cmnd *cmd = qc->scsicmd; | |
2436 | ||
2437 | if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN) | |
9a405257 | 2438 | goto invalid_fld; |
b095518e | 2439 | |
f59b0cf8 AL |
2440 | if (scsicmd[1] & 0xe0) |
2441 | /* PIO multi not supported yet */ | |
2442 | goto invalid_fld; | |
2443 | ||
b095518e JG |
2444 | /* |
2445 | * 12 and 16 byte CDBs use different offsets to | |
2446 | * provide the various register values. | |
2447 | */ | |
2448 | if (scsicmd[0] == ATA_16) { | |
2449 | /* | |
2450 | * 16-byte CDB - may contain extended commands. | |
2451 | * | |
2452 | * If that is the case, copy the upper byte register values. | |
2453 | */ | |
2454 | if (scsicmd[1] & 0x01) { | |
2455 | tf->hob_feature = scsicmd[3]; | |
2456 | tf->hob_nsect = scsicmd[5]; | |
2457 | tf->hob_lbal = scsicmd[7]; | |
2458 | tf->hob_lbam = scsicmd[9]; | |
2459 | tf->hob_lbah = scsicmd[11]; | |
2460 | tf->flags |= ATA_TFLAG_LBA48; | |
2461 | } else | |
2462 | tf->flags &= ~ATA_TFLAG_LBA48; | |
2463 | ||
2464 | /* | |
2465 | * Always copy low byte, device and command registers. | |
2466 | */ | |
2467 | tf->feature = scsicmd[4]; | |
2468 | tf->nsect = scsicmd[6]; | |
2469 | tf->lbal = scsicmd[8]; | |
2470 | tf->lbam = scsicmd[10]; | |
2471 | tf->lbah = scsicmd[12]; | |
2472 | tf->device = scsicmd[13]; | |
2473 | tf->command = scsicmd[14]; | |
2474 | } else { | |
2475 | /* | |
2476 | * 12-byte CDB - incapable of extended commands. | |
2477 | */ | |
2478 | tf->flags &= ~ATA_TFLAG_LBA48; | |
2479 | ||
2480 | tf->feature = scsicmd[3]; | |
2481 | tf->nsect = scsicmd[4]; | |
2482 | tf->lbal = scsicmd[5]; | |
2483 | tf->lbam = scsicmd[6]; | |
2484 | tf->lbah = scsicmd[7]; | |
2485 | tf->device = scsicmd[8]; | |
2486 | tf->command = scsicmd[9]; | |
2487 | } | |
dcc2d1e7 ML |
2488 | /* |
2489 | * If slave is possible, enforce correct master/slave bit | |
2490 | */ | |
2491 | if (qc->ap->flags & ATA_FLAG_SLAVE_POSS) | |
2492 | tf->device = qc->dev->devno ? | |
2493 | tf->device | ATA_DEV1 : tf->device & ~ATA_DEV1; | |
b095518e JG |
2494 | |
2495 | /* | |
2496 | * Filter SET_FEATURES - XFER MODE command -- otherwise, | |
2497 | * SET_FEATURES - XFER MODE must be preceded/succeeded | |
2498 | * by an update to hardware-specific registers for each | |
2499 | * controller (i.e. the reason for ->set_piomode(), | |
2500 | * ->set_dmamode(), and ->post_set_mode() hooks). | |
2501 | */ | |
2502 | if ((tf->command == ATA_CMD_SET_FEATURES) | |
2503 | && (tf->feature == SETFEATURES_XFER)) | |
9a405257 | 2504 | goto invalid_fld; |
b095518e JG |
2505 | |
2506 | /* | |
2507 | * Set flags so that all registers will be written, | |
2508 | * and pass on write indication (used for PIO/DMA | |
2509 | * setup.) | |
2510 | */ | |
2511 | tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE); | |
2512 | ||
0274aa25 | 2513 | if (cmd->sc_data_direction == DMA_TO_DEVICE) |
b095518e JG |
2514 | tf->flags |= ATA_TFLAG_WRITE; |
2515 | ||
2516 | /* | |
2517 | * Set transfer length. | |
2518 | * | |
2519 | * TODO: find out if we need to do more here to | |
2520 | * cover scatter/gather case. | |
2521 | */ | |
2522 | qc->nsect = cmd->bufflen / ATA_SECT_SIZE; | |
2523 | ||
2524 | return 0; | |
9a405257 TH |
2525 | |
2526 | invalid_fld: | |
2527 | ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x00); | |
2528 | /* "Invalid field in cdb" */ | |
2529 | return 1; | |
b095518e JG |
2530 | } |
2531 | ||
1da177e4 LT |
2532 | /** |
2533 | * ata_get_xlat_func - check if SCSI to ATA translation is possible | |
2534 | * @dev: ATA device | |
2535 | * @cmd: SCSI command opcode to consider | |
2536 | * | |
2537 | * Look up the SCSI command given, and determine whether the | |
2538 | * SCSI command is to be translated or simulated. | |
2539 | * | |
2540 | * RETURNS: | |
2541 | * Pointer to translation function if possible, %NULL if not. | |
2542 | */ | |
2543 | ||
2544 | static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd) | |
2545 | { | |
2546 | switch (cmd) { | |
2547 | case READ_6: | |
2548 | case READ_10: | |
2549 | case READ_16: | |
2550 | ||
2551 | case WRITE_6: | |
2552 | case WRITE_10: | |
2553 | case WRITE_16: | |
2554 | return ata_scsi_rw_xlat; | |
2555 | ||
2556 | case SYNCHRONIZE_CACHE: | |
2557 | if (ata_try_flush_cache(dev)) | |
2558 | return ata_scsi_flush_xlat; | |
2559 | break; | |
2560 | ||
2561 | case VERIFY: | |
2562 | case VERIFY_16: | |
2563 | return ata_scsi_verify_xlat; | |
b095518e JG |
2564 | |
2565 | case ATA_12: | |
2566 | case ATA_16: | |
2567 | return ata_scsi_pass_thru; | |
da61396d | 2568 | |
972dcafb DG |
2569 | case START_STOP: |
2570 | return ata_scsi_start_stop_xlat; | |
1da177e4 LT |
2571 | } |
2572 | ||
2573 | return NULL; | |
2574 | } | |
2575 | ||
2576 | /** | |
2577 | * ata_scsi_dump_cdb - dump SCSI command contents to dmesg | |
2578 | * @ap: ATA port to which the command was being sent | |
2579 | * @cmd: SCSI command to dump | |
2580 | * | |
2581 | * Prints the contents of a SCSI command via printk(). | |
2582 | */ | |
2583 | ||
2584 | static inline void ata_scsi_dump_cdb(struct ata_port *ap, | |
2585 | struct scsi_cmnd *cmd) | |
2586 | { | |
2587 | #ifdef ATA_DEBUG | |
2588 | struct scsi_device *scsidev = cmd->device; | |
2589 | u8 *scsicmd = cmd->cmnd; | |
2590 | ||
2591 | DPRINTK("CDB (%u:%d,%d,%d) %02x %02x %02x %02x %02x %02x %02x %02x %02x\n", | |
2592 | ap->id, | |
2593 | scsidev->channel, scsidev->id, scsidev->lun, | |
2594 | scsicmd[0], scsicmd[1], scsicmd[2], scsicmd[3], | |
2595 | scsicmd[4], scsicmd[5], scsicmd[6], scsicmd[7], | |
2596 | scsicmd[8]); | |
2597 | #endif | |
2598 | } | |
2599 | ||
2600 | /** | |
2601 | * ata_scsi_queuecmd - Issue SCSI cdb to libata-managed device | |
2602 | * @cmd: SCSI command to be sent | |
2603 | * @done: Completion function, called when command is complete | |
2604 | * | |
2605 | * In some cases, this function translates SCSI commands into | |
2606 | * ATA taskfiles, and queues the taskfiles to be sent to | |
2607 | * hardware. In other cases, this function simulates a | |
2608 | * SCSI device by evaluating and responding to certain | |
2609 | * SCSI commands. This creates the overall effect of | |
2610 | * ATA and ATAPI devices appearing as SCSI devices. | |
2611 | * | |
2612 | * LOCKING: | |
2613 | * Releases scsi-layer-held lock, and obtains host_set lock. | |
2614 | * | |
2615 | * RETURNS: | |
2616 | * Zero. | |
2617 | */ | |
2618 | ||
2619 | int ata_scsi_queuecmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *)) | |
2620 | { | |
2621 | struct ata_port *ap; | |
2622 | struct ata_device *dev; | |
2623 | struct scsi_device *scsidev = cmd->device; | |
005a5a06 | 2624 | struct Scsi_Host *shost = scsidev->host; |
1da177e4 | 2625 | |
005a5a06 JG |
2626 | ap = (struct ata_port *) &shost->hostdata[0]; |
2627 | ||
2628 | spin_unlock(shost->host_lock); | |
2629 | spin_lock(&ap->host_set->lock); | |
1da177e4 LT |
2630 | |
2631 | ata_scsi_dump_cdb(ap, cmd); | |
2632 | ||
2633 | dev = ata_scsi_find_dev(ap, scsidev); | |
2634 | if (unlikely(!dev)) { | |
2635 | cmd->result = (DID_BAD_TARGET << 16); | |
2636 | done(cmd); | |
2637 | goto out_unlock; | |
2638 | } | |
2639 | ||
2640 | if (dev->class == ATA_DEV_ATA) { | |
2641 | ata_xlat_func_t xlat_func = ata_get_xlat_func(dev, | |
2642 | cmd->cmnd[0]); | |
2643 | ||
2644 | if (xlat_func) | |
2645 | ata_scsi_translate(ap, dev, cmd, done, xlat_func); | |
2646 | else | |
9a3dccc4 | 2647 | ata_scsi_simulate(ap, dev, cmd, done); |
1da177e4 LT |
2648 | } else |
2649 | ata_scsi_translate(ap, dev, cmd, done, atapi_xlat); | |
2650 | ||
2651 | out_unlock: | |
005a5a06 JG |
2652 | spin_unlock(&ap->host_set->lock); |
2653 | spin_lock(shost->host_lock); | |
1da177e4 LT |
2654 | return 0; |
2655 | } | |
2656 | ||
2657 | /** | |
2658 | * ata_scsi_simulate - simulate SCSI command on ATA device | |
c893a3ae RD |
2659 | * @ap: port the device is connected to |
2660 | * @dev: the target device | |
1da177e4 LT |
2661 | * @cmd: SCSI command being sent to device. |
2662 | * @done: SCSI command completion function. | |
2663 | * | |
2664 | * Interprets and directly executes a select list of SCSI commands | |
2665 | * that can be handled internally. | |
2666 | * | |
2667 | * LOCKING: | |
2668 | * spin_lock_irqsave(host_set lock) | |
2669 | */ | |
2670 | ||
9a3dccc4 | 2671 | void ata_scsi_simulate(struct ata_port *ap, struct ata_device *dev, |
1da177e4 LT |
2672 | struct scsi_cmnd *cmd, |
2673 | void (*done)(struct scsi_cmnd *)) | |
2674 | { | |
2675 | struct ata_scsi_args args; | |
057ace5e | 2676 | const u8 *scsicmd = cmd->cmnd; |
1da177e4 | 2677 | |
9a3dccc4 TH |
2678 | args.ap = ap; |
2679 | args.dev = dev; | |
2680 | args.id = dev->id; | |
1da177e4 LT |
2681 | args.cmd = cmd; |
2682 | args.done = done; | |
2683 | ||
2684 | switch(scsicmd[0]) { | |
2685 | /* no-op's, complete with success */ | |
2686 | case SYNCHRONIZE_CACHE: | |
2687 | case REZERO_UNIT: | |
2688 | case SEEK_6: | |
2689 | case SEEK_10: | |
2690 | case TEST_UNIT_READY: | |
2691 | case FORMAT_UNIT: /* FIXME: correct? */ | |
2692 | case SEND_DIAGNOSTIC: /* FIXME: correct? */ | |
2693 | ata_scsi_rbuf_fill(&args, ata_scsiop_noop); | |
2694 | break; | |
2695 | ||
2696 | case INQUIRY: | |
2697 | if (scsicmd[1] & 2) /* is CmdDt set? */ | |
ae006510 | 2698 | ata_scsi_invalid_field(cmd, done); |
1da177e4 LT |
2699 | else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */ |
2700 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std); | |
2701 | else if (scsicmd[2] == 0x00) | |
2702 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_00); | |
2703 | else if (scsicmd[2] == 0x80) | |
2704 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_80); | |
2705 | else if (scsicmd[2] == 0x83) | |
2706 | ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83); | |
2707 | else | |
ae006510 | 2708 | ata_scsi_invalid_field(cmd, done); |
1da177e4 LT |
2709 | break; |
2710 | ||
2711 | case MODE_SENSE: | |
2712 | case MODE_SENSE_10: | |
2713 | ata_scsi_rbuf_fill(&args, ata_scsiop_mode_sense); | |
2714 | break; | |
2715 | ||
2716 | case MODE_SELECT: /* unconditionally return */ | |
2717 | case MODE_SELECT_10: /* bad-field-in-cdb */ | |
ae006510 | 2718 | ata_scsi_invalid_field(cmd, done); |
1da177e4 LT |
2719 | break; |
2720 | ||
2721 | case READ_CAPACITY: | |
2722 | ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap); | |
2723 | break; | |
2724 | ||
2725 | case SERVICE_ACTION_IN: | |
2726 | if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16) | |
2727 | ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap); | |
2728 | else | |
ae006510 | 2729 | ata_scsi_invalid_field(cmd, done); |
1da177e4 LT |
2730 | break; |
2731 | ||
2732 | case REPORT_LUNS: | |
2733 | ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns); | |
2734 | break; | |
2735 | ||
b095518e | 2736 | /* mandatory commands we haven't implemented yet */ |
1da177e4 LT |
2737 | case REQUEST_SENSE: |
2738 | ||
2739 | /* all other commands */ | |
2740 | default: | |
ae006510 DG |
2741 | ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0); |
2742 | /* "Invalid command operation code" */ | |
2743 | done(cmd); | |
1da177e4 LT |
2744 | break; |
2745 | } | |
2746 | } | |
2747 | ||
644dd0cc JG |
2748 | void ata_scsi_scan_host(struct ata_port *ap) |
2749 | { | |
3f19ee8c | 2750 | struct ata_device *dev; |
644dd0cc JG |
2751 | unsigned int i; |
2752 | ||
2753 | if (ap->flags & ATA_FLAG_PORT_DISABLED) | |
2754 | return; | |
2755 | ||
3f19ee8c JG |
2756 | for (i = 0; i < ATA_MAX_DEVICES; i++) { |
2757 | dev = &ap->device[i]; | |
2758 | ||
2759 | if (ata_dev_present(dev)) | |
2760 | scsi_scan_target(&ap->host->shost_gendev, 0, i, 0, 0); | |
2761 | } | |
644dd0cc JG |
2762 | } |
2763 |