diff options
author | Jeff Garzik <jgarzik@pobox.com> | 2005-05-12 15:45:22 -0400 |
---|---|---|
committer | Jeff Garzik <jgarzik@pobox.com> | 2005-05-12 15:45:22 -0400 |
commit | b095518ef51c37658c58367bd19240b8a113f25c (patch) | |
tree | fc65e55b8786c48067338b8097bae1cbc13fbaac | |
parent | 88d7bd8cb9eb8d64bf7997600b0d64f7834047c5 (diff) |
[libata] ATA passthru (arbitrary ATA command execution)
Authors:
Brett Russ <russb@emc.com>
John W. Linville <linville@tuxdriver.com>
Andy Warner <andyw@pobox.com>
-rw-r--r-- | drivers/scsi/libata-scsi.c | 651 | ||||
-rw-r--r-- | drivers/scsi/libata.h | 3 | ||||
-rw-r--r-- | include/scsi/scsi.h | 3 |
3 files changed, 552 insertions, 105 deletions
diff --git a/drivers/scsi/libata-scsi.c b/drivers/scsi/libata-scsi.c index 4c96df060c3b..22c77a113536 100644 --- a/drivers/scsi/libata-scsi.c +++ b/drivers/scsi/libata-scsi.c | |||
@@ -29,10 +29,13 @@ | |||
29 | #include "scsi.h" | 29 | #include "scsi.h" |
30 | #include <scsi/scsi_host.h> | 30 | #include <scsi/scsi_host.h> |
31 | #include <linux/libata.h> | 31 | #include <linux/libata.h> |
32 | #include <linux/hdreg.h> | ||
32 | #include <asm/uaccess.h> | 33 | #include <asm/uaccess.h> |
33 | 34 | ||
34 | #include "libata.h" | 35 | #include "libata.h" |
35 | 36 | ||
37 | #define SECTOR_SIZE 512 | ||
38 | |||
36 | typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, u8 *scsicmd); | 39 | typedef unsigned int (*ata_xlat_func_t)(struct ata_queued_cmd *qc, u8 *scsicmd); |
37 | static struct ata_device * | 40 | static struct ata_device * |
38 | ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev); | 41 | ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev); |
@@ -67,6 +70,148 @@ int ata_std_bios_param(struct scsi_device *sdev, struct block_device *bdev, | |||
67 | return 0; | 70 | return 0; |
68 | } | 71 | } |
69 | 72 | ||
73 | /** | ||
74 | * ata_cmd_ioctl - Handler for HDIO_DRIVE_CMD ioctl | ||
75 | * @dev: Device to whom we are issuing command | ||
76 | * @arg: User provided data for issuing command | ||
77 | * | ||
78 | * LOCKING: | ||
79 | * Defined by the SCSI layer. We don't really care. | ||
80 | * | ||
81 | * RETURNS: | ||
82 | * Zero on success, negative errno on error. | ||
83 | */ | ||
84 | |||
85 | int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg) | ||
86 | { | ||
87 | int rc = 0; | ||
88 | u8 scsi_cmd[MAX_COMMAND_SIZE]; | ||
89 | u8 args[4], *argbuf = NULL; | ||
90 | int argsize = 0; | ||
91 | struct scsi_request *sreq; | ||
92 | |||
93 | if (NULL == (void *)arg) | ||
94 | return -EINVAL; | ||
95 | |||
96 | if (copy_from_user(args, arg, sizeof(args))) | ||
97 | return -EFAULT; | ||
98 | |||
99 | sreq = scsi_allocate_request(scsidev, GFP_KERNEL); | ||
100 | if (!sreq) | ||
101 | return -EINTR; | ||
102 | |||
103 | memset(scsi_cmd, 0, sizeof(scsi_cmd)); | ||
104 | |||
105 | if (args[3]) { | ||
106 | argsize = SECTOR_SIZE * args[3]; | ||
107 | argbuf = kmalloc(argsize, GFP_KERNEL); | ||
108 | if (argbuf == NULL) | ||
109 | return -ENOMEM; | ||
110 | |||
111 | scsi_cmd[1] = (4 << 1); /* PIO Data-in */ | ||
112 | scsi_cmd[2] = 0x0e; /* no off.line or cc, read from dev, | ||
113 | block count in sector count field */ | ||
114 | sreq->sr_data_direction = DMA_FROM_DEVICE; | ||
115 | } else { | ||
116 | scsi_cmd[1] = (3 << 1); /* Non-data */ | ||
117 | /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */ | ||
118 | sreq->sr_data_direction = DMA_NONE; | ||
119 | } | ||
120 | |||
121 | scsi_cmd[0] = ATA_16; | ||
122 | |||
123 | scsi_cmd[4] = args[2]; | ||
124 | if (args[0] == WIN_SMART) { /* hack -- ide driver does this too... */ | ||
125 | scsi_cmd[6] = args[3]; | ||
126 | scsi_cmd[8] = args[1]; | ||
127 | scsi_cmd[10] = 0x4f; | ||
128 | scsi_cmd[12] = 0xc2; | ||
129 | } else { | ||
130 | scsi_cmd[6] = args[1]; | ||
131 | } | ||
132 | scsi_cmd[14] = args[0]; | ||
133 | |||
134 | /* Good values for timeout and retries? Values below | ||
135 | from scsi_ioctl_send_command() for default case... */ | ||
136 | scsi_wait_req(sreq, scsi_cmd, argbuf, argsize, (10*HZ), 5); | ||
137 | |||
138 | if (sreq->sr_result) { | ||
139 | rc = -EIO; | ||
140 | goto error; | ||
141 | } | ||
142 | |||
143 | /* Need code to retrieve data from check condition? */ | ||
144 | |||
145 | if ((argbuf) | ||
146 | && copy_to_user((void *)(arg + sizeof(args)), argbuf, argsize)) | ||
147 | rc = -EFAULT; | ||
148 | error: | ||
149 | scsi_release_request(sreq); | ||
150 | |||
151 | if (argbuf) | ||
152 | kfree(argbuf); | ||
153 | |||
154 | return rc; | ||
155 | } | ||
156 | |||
157 | /** | ||
158 | * ata_task_ioctl - Handler for HDIO_DRIVE_TASK ioctl | ||
159 | * @dev: Device to whom we are issuing command | ||
160 | * @arg: User provided data for issuing command | ||
161 | * | ||
162 | * LOCKING: | ||
163 | * Defined by the SCSI layer. We don't really care. | ||
164 | * | ||
165 | * RETURNS: | ||
166 | * Zero on success, negative errno on error. | ||
167 | */ | ||
168 | int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg) | ||
169 | { | ||
170 | int rc = 0; | ||
171 | u8 scsi_cmd[MAX_COMMAND_SIZE]; | ||
172 | u8 args[7]; | ||
173 | struct scsi_request *sreq; | ||
174 | |||
175 | if (NULL == (void *)arg) | ||
176 | return -EINVAL; | ||
177 | |||
178 | if (copy_from_user(args, arg, sizeof(args))) | ||
179 | return -EFAULT; | ||
180 | |||
181 | memset(scsi_cmd, 0, sizeof(scsi_cmd)); | ||
182 | scsi_cmd[0] = ATA_16; | ||
183 | scsi_cmd[1] = (3 << 1); /* Non-data */ | ||
184 | /* scsi_cmd[2] is already 0 -- no off.line, cc, or data xfer */ | ||
185 | scsi_cmd[4] = args[1]; | ||
186 | scsi_cmd[6] = args[2]; | ||
187 | scsi_cmd[8] = args[3]; | ||
188 | scsi_cmd[10] = args[4]; | ||
189 | scsi_cmd[12] = args[5]; | ||
190 | scsi_cmd[14] = args[0]; | ||
191 | |||
192 | sreq = scsi_allocate_request(scsidev, GFP_KERNEL); | ||
193 | if (!sreq) { | ||
194 | rc = -EINTR; | ||
195 | goto error; | ||
196 | } | ||
197 | |||
198 | sreq->sr_data_direction = DMA_NONE; | ||
199 | /* Good values for timeout and retries? Values below | ||
200 | from scsi_ioctl_send_command() for default case... */ | ||
201 | scsi_wait_req(sreq, scsi_cmd, NULL, 0, (10*HZ), 5); | ||
202 | |||
203 | if (sreq->sr_result) { | ||
204 | rc = -EIO; | ||
205 | goto error; | ||
206 | } | ||
207 | |||
208 | /* Need code to retrieve data from check condition? */ | ||
209 | |||
210 | error: | ||
211 | scsi_release_request(sreq); | ||
212 | return rc; | ||
213 | } | ||
214 | |||
70 | int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg) | 215 | int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg) |
71 | { | 216 | { |
72 | struct ata_port *ap; | 217 | struct ata_port *ap; |
@@ -96,6 +241,16 @@ int ata_scsi_ioctl(struct scsi_device *scsidev, int cmd, void __user *arg) | |||
96 | return -EINVAL; | 241 | return -EINVAL; |
97 | return 0; | 242 | return 0; |
98 | 243 | ||
244 | case HDIO_DRIVE_CMD: | ||
245 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) | ||
246 | return -EACCES; | ||
247 | return ata_cmd_ioctl(scsidev, arg); | ||
248 | |||
249 | case HDIO_DRIVE_TASK: | ||
250 | if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO)) | ||
251 | return -EACCES; | ||
252 | return ata_task_ioctl(scsidev, arg); | ||
253 | |||
99 | default: | 254 | default: |
100 | rc = -ENOTTY; | 255 | rc = -ENOTTY; |
101 | break; | 256 | break; |
@@ -154,24 +309,69 @@ struct ata_queued_cmd *ata_scsi_qc_new(struct ata_port *ap, | |||
154 | } | 309 | } |
155 | 310 | ||
156 | /** | 311 | /** |
312 | * ata_dump_status - user friendly display of error info | ||
313 | * @id: id of the port in question | ||
314 | * @tf: ptr to filled out taskfile | ||
315 | * | ||
316 | * Decode and dump the ATA error/status registers for the user so | ||
317 | * that they have some idea what really happened at the non | ||
318 | * make-believe layer. | ||
319 | * | ||
320 | * LOCKING: | ||
321 | * inherited from caller | ||
322 | */ | ||
323 | void ata_dump_status(unsigned id, struct ata_taskfile *tf) | ||
324 | { | ||
325 | u8 stat = tf->command, err = tf->feature; | ||
326 | |||
327 | printk(KERN_WARNING "ata%u: status=0x%02x { ", id, stat); | ||
328 | if (stat & ATA_BUSY) { | ||
329 | printk("Busy }\n"); /* Data is not valid in this case */ | ||
330 | } else { | ||
331 | if (stat & 0x40) printk("DriveReady "); | ||
332 | if (stat & 0x20) printk("DeviceFault "); | ||
333 | if (stat & 0x10) printk("SeekComplete "); | ||
334 | if (stat & 0x08) printk("DataRequest "); | ||
335 | if (stat & 0x04) printk("CorrectedError "); | ||
336 | if (stat & 0x02) printk("Index "); | ||
337 | if (stat & 0x01) printk("Error "); | ||
338 | printk("}\n"); | ||
339 | |||
340 | if (err) { | ||
341 | printk(KERN_WARNING "ata%u: error=0x%02x { ", id, err); | ||
342 | if (err & 0x04) printk("DriveStatusError "); | ||
343 | if (err & 0x80) { | ||
344 | if (err & 0x04) printk("BadCRC "); | ||
345 | else printk("Sector "); | ||
346 | } | ||
347 | if (err & 0x40) printk("UncorrectableError "); | ||
348 | if (err & 0x10) printk("SectorIdNotFound "); | ||
349 | if (err & 0x02) printk("TrackZeroNotFound "); | ||
350 | if (err & 0x01) printk("AddrMarkNotFound "); | ||
351 | printk("}\n"); | ||
352 | } | ||
353 | } | ||
354 | } | ||
355 | |||
356 | /** | ||
157 | * ata_to_sense_error - convert ATA error to SCSI error | 357 | * ata_to_sense_error - convert ATA error to SCSI error |
158 | * @qc: Command that we are erroring out | ||
159 | * @drv_stat: value contained in ATA status register | 358 | * @drv_stat: value contained in ATA status register |
359 | * @drv_err: value contained in ATA error register | ||
360 | * @sk: the sense key we'll fill out | ||
361 | * @asc: the additional sense code we'll fill out | ||
362 | * @ascq: the additional sense code qualifier we'll fill out | ||
160 | * | 363 | * |
161 | * Converts an ATA error into a SCSI error. While we are at it | 364 | * Converts an ATA error into a SCSI error. Fill out pointers to |
162 | * we decode and dump the ATA error for the user so that they | 365 | * SK, ASC, and ASCQ bytes for later use in fixed or descriptor |
163 | * have some idea what really happened at the non make-believe | 366 | * format sense blocks. |
164 | * layer. | ||
165 | * | 367 | * |
166 | * LOCKING: | 368 | * LOCKING: |
167 | * spin_lock_irqsave(host_set lock) | 369 | * spin_lock_irqsave(host_set lock) |
168 | */ | 370 | */ |
169 | 371 | void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc, | |
170 | void ata_to_sense_error(struct ata_queued_cmd *qc, u8 drv_stat) | 372 | u8 *ascq) |
171 | { | 373 | { |
172 | struct scsi_cmnd *cmd = qc->scsicmd; | 374 | int i; |
173 | u8 err = 0; | ||
174 | unsigned char *sb = cmd->sense_buffer; | ||
175 | /* Based on the 3ware driver translation table */ | 375 | /* Based on the 3ware driver translation table */ |
176 | static unsigned char sense_table[][4] = { | 376 | static unsigned char sense_table[][4] = { |
177 | /* BBD|ECC|ID|MAR */ | 377 | /* BBD|ECC|ID|MAR */ |
@@ -212,105 +412,183 @@ void ata_to_sense_error(struct ata_queued_cmd *qc, u8 drv_stat) | |||
212 | {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered | 412 | {0x04, RECOVERED_ERROR, 0x11, 0x00}, // Recovered ECC error Medium error, recovered |
213 | {0xFF, 0xFF, 0xFF, 0xFF}, // END mark | 413 | {0xFF, 0xFF, 0xFF, 0xFF}, // END mark |
214 | }; | 414 | }; |
215 | int i = 0; | ||
216 | |||
217 | cmd->result = SAM_STAT_CHECK_CONDITION; | ||
218 | 415 | ||
219 | /* | 416 | /* |
220 | * Is this an error we can process/parse | 417 | * Is this an error we can process/parse |
221 | */ | 418 | */ |
222 | 419 | if (drv_stat & ATA_BUSY) { | |
223 | if(drv_stat & ATA_ERR) | 420 | drv_err = 0; /* Ignore the err bits, they're invalid */ |
224 | /* Read the err bits */ | ||
225 | err = ata_chk_err(qc->ap); | ||
226 | |||
227 | /* Display the ATA level error info */ | ||
228 | |||
229 | printk(KERN_WARNING "ata%u: status=0x%02x { ", qc->ap->id, drv_stat); | ||
230 | if(drv_stat & 0x80) | ||
231 | { | ||
232 | printk("Busy "); | ||
233 | err = 0; /* Data is not valid in this case */ | ||
234 | } | 421 | } |
235 | else { | 422 | |
236 | if(drv_stat & 0x40) printk("DriveReady "); | 423 | if (drv_err) { |
237 | if(drv_stat & 0x20) printk("DeviceFault "); | 424 | /* Look for drv_err */ |
238 | if(drv_stat & 0x10) printk("SeekComplete "); | 425 | for (i = 0; sense_table[i][0] != 0xFF; i++) { |
239 | if(drv_stat & 0x08) printk("DataRequest "); | 426 | /* Look for best matches first */ |
240 | if(drv_stat & 0x04) printk("CorrectedError "); | 427 | if ((sense_table[i][0] & drv_err) == |
241 | if(drv_stat & 0x02) printk("Index "); | 428 | sense_table[i][0]) { |
242 | if(drv_stat & 0x01) printk("Error "); | 429 | *sk = sense_table[i][1]; |
430 | *asc = sense_table[i][2]; | ||
431 | *ascq = sense_table[i][3]; | ||
432 | goto translate_done; | ||
433 | } | ||
434 | } | ||
435 | /* No immediate match */ | ||
436 | printk(KERN_WARNING "ata%u: no sense translation for " | ||
437 | "error 0x%02x\n", id, drv_err); | ||
243 | } | 438 | } |
244 | printk("}\n"); | 439 | |
245 | 440 | /* Fall back to interpreting status bits */ | |
246 | if(err) | 441 | for (i = 0; stat_table[i][0] != 0xFF; i++) { |
247 | { | 442 | if (stat_table[i][0] & drv_stat) { |
248 | printk(KERN_WARNING "ata%u: error=0x%02x { ", qc->ap->id, err); | 443 | *sk = stat_table[i][1]; |
249 | if(err & 0x04) printk("DriveStatusError "); | 444 | *asc = stat_table[i][2]; |
250 | if(err & 0x80) | 445 | *ascq = stat_table[i][3]; |
251 | { | 446 | goto translate_done; |
252 | if(err & 0x04) | ||
253 | printk("BadCRC "); | ||
254 | else | ||
255 | printk("Sector "); | ||
256 | } | 447 | } |
257 | if(err & 0x40) printk("UncorrectableError "); | 448 | } |
258 | if(err & 0x10) printk("SectorIdNotFound "); | 449 | /* No error? Undecoded? */ |
259 | if(err & 0x02) printk("TrackZeroNotFound "); | 450 | printk(KERN_WARNING "ata%u: no sense translation for status: 0x%02x\n", |
260 | if(err & 0x01) printk("AddrMarkNotFound "); | 451 | id, drv_stat); |
261 | printk("}\n"); | 452 | |
453 | /* For our last chance pick, use medium read error because | ||
454 | * it's much more common than an ATA drive telling you a write | ||
455 | * has failed. | ||
456 | */ | ||
457 | *sk = MEDIUM_ERROR; | ||
458 | *asc = 0x11; /* "unrecovered read error" */ | ||
459 | *ascq = 0x04; /* "auto-reallocation failed" */ | ||
460 | |||
461 | translate_done: | ||
462 | printk(KERN_ERR "ata%u: translated ATA stat/err 0x%02x/%02x to " | ||
463 | "SCSI SK/ASC/ASCQ 0x%x/%02x/%02x\n", id, drv_stat, drv_err, | ||
464 | *sk, *asc, *ascq); | ||
465 | return; | ||
466 | } | ||
467 | |||
468 | /* | ||
469 | * ata_gen_ata_desc_sense - Generate check condition sense block. | ||
470 | * @qc: Command that completed. | ||
471 | * | ||
472 | * This function is specific to the ATA descriptor format sense | ||
473 | * block specified for the ATA pass through commands. Regardless | ||
474 | * of whether the command errored or not, return a sense | ||
475 | * block. Copy all controller registers into the sense | ||
476 | * block. Clear sense key, ASC & ASCQ if there is no error. | ||
477 | * | ||
478 | * LOCKING: | ||
479 | * spin_lock_irqsave(host_set lock) | ||
480 | */ | ||
481 | void ata_gen_ata_desc_sense(struct ata_queued_cmd *qc) | ||
482 | { | ||
483 | struct scsi_cmnd *cmd = qc->scsicmd; | ||
484 | struct ata_taskfile *tf = &qc->tf; | ||
485 | unsigned char *sb = cmd->sense_buffer; | ||
486 | unsigned char *desc = sb + 8; | ||
487 | |||
488 | memset(sb, 0, SCSI_SENSE_BUFFERSIZE); | ||
489 | |||
490 | cmd->result = SAM_STAT_CHECK_CONDITION; | ||
491 | |||
492 | /* | ||
493 | * Read the controller registers. | ||
494 | */ | ||
495 | assert(NULL != qc->ap->ops->tf_read); | ||
496 | qc->ap->ops->tf_read(qc->ap, tf); | ||
262 | 497 | ||
263 | /* Should we dump sector info here too ?? */ | 498 | /* |
499 | * Use ata_to_sense_error() to map status register bits | ||
500 | * onto sense key, asc & ascq. | ||
501 | */ | ||
502 | if (unlikely(tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ))) { | ||
503 | ata_to_sense_error(qc->ap->id, tf->command, tf->feature, | ||
504 | &sb[1], &sb[2], &sb[3]); | ||
505 | sb[1] &= 0x0f; | ||
264 | } | 506 | } |
265 | 507 | ||
508 | /* | ||
509 | * Sense data is current and format is descriptor. | ||
510 | */ | ||
511 | sb[0] = 0x72; | ||
266 | 512 | ||
267 | /* Look for err */ | 513 | desc[0] = 0x09; |
268 | while(sense_table[i][0] != 0xFF) | 514 | |
269 | { | 515 | /* |
270 | /* Look for best matches first */ | 516 | * Set length of additional sense data. |
271 | if((sense_table[i][0] & err) == sense_table[i][0]) | 517 | * Since we only populate descriptor 0, the total |
272 | { | 518 | * length is the same (fixed) length as descriptor 0. |
273 | sb[0] = 0x70; | 519 | */ |
274 | sb[2] = sense_table[i][1]; | 520 | desc[1] = sb[7] = 14; |
275 | sb[7] = 0x0a; | 521 | |
276 | sb[12] = sense_table[i][2]; | 522 | /* |
277 | sb[13] = sense_table[i][3]; | 523 | * Copy registers into sense buffer. |
278 | return; | 524 | */ |
279 | } | 525 | desc[2] = 0x00; |
280 | i++; | 526 | desc[3] = tf->feature; /* == error reg */ |
527 | desc[5] = tf->nsect; | ||
528 | desc[7] = tf->lbal; | ||
529 | desc[9] = tf->lbam; | ||
530 | desc[11] = tf->lbah; | ||
531 | desc[12] = tf->device; | ||
532 | desc[13] = tf->command; /* == status reg */ | ||
533 | |||
534 | /* | ||
535 | * Fill in Extend bit, and the high order bytes | ||
536 | * if applicable. | ||
537 | */ | ||
538 | if (tf->flags & ATA_TFLAG_LBA48) { | ||
539 | desc[2] |= 0x01; | ||
540 | desc[4] = tf->hob_nsect; | ||
541 | desc[6] = tf->hob_lbal; | ||
542 | desc[8] = tf->hob_lbam; | ||
543 | desc[10] = tf->hob_lbah; | ||
281 | } | 544 | } |
282 | /* No immediate match */ | 545 | } |
283 | if(err) | ||
284 | printk(KERN_DEBUG "ata%u: no sense translation for 0x%02x\n", qc->ap->id, err); | ||
285 | 546 | ||
286 | i = 0; | 547 | /** |
287 | /* Fall back to interpreting status bits */ | 548 | * ata_gen_fixed_sense - generate a SCSI fixed sense block |
288 | while(stat_table[i][0] != 0xFF) | 549 | * @qc: Command that we are erroring out |
289 | { | 550 | * |
290 | if(stat_table[i][0] & drv_stat) | 551 | * Leverage ata_to_sense_error() to give us the codes. Fit our |
291 | { | 552 | * LBA in here if there's room. |
292 | sb[0] = 0x70; | 553 | * |
293 | sb[2] = stat_table[i][1]; | 554 | * LOCKING: |
294 | sb[7] = 0x0a; | 555 | * inherited from caller |
295 | sb[12] = stat_table[i][2]; | 556 | */ |
296 | sb[13] = stat_table[i][3]; | 557 | void ata_gen_fixed_sense(struct ata_queued_cmd *qc) |
297 | return; | 558 | { |
298 | } | 559 | struct scsi_cmnd *cmd = qc->scsicmd; |
299 | i++; | 560 | struct ata_taskfile *tf = &qc->tf; |
561 | unsigned char *sb = cmd->sense_buffer; | ||
562 | |||
563 | memset(sb, 0, SCSI_SENSE_BUFFERSIZE); | ||
564 | |||
565 | cmd->result = SAM_STAT_CHECK_CONDITION; | ||
566 | |||
567 | /* | ||
568 | * Read the controller registers. | ||
569 | */ | ||
570 | assert(NULL != qc->ap->ops->tf_read); | ||
571 | qc->ap->ops->tf_read(qc->ap, tf); | ||
572 | |||
573 | /* | ||
574 | * Use ata_to_sense_error() to map status register bits | ||
575 | * onto sense key, asc & ascq. | ||
576 | */ | ||
577 | if (unlikely(tf->command & (ATA_BUSY | ATA_DF | ATA_ERR | ATA_DRQ))) { | ||
578 | ata_to_sense_error(qc->ap->id, tf->command, tf->feature, | ||
579 | &sb[2], &sb[12], &sb[13]); | ||
580 | sb[2] &= 0x0f; | ||
300 | } | 581 | } |
301 | /* No error ?? */ | ||
302 | printk(KERN_ERR "ata%u: called with no error (%02X)!\n", qc->ap->id, drv_stat); | ||
303 | /* additional-sense-code[-qualifier] */ | ||
304 | 582 | ||
305 | sb[0] = 0x70; | 583 | sb[0] = 0x70; |
306 | sb[2] = MEDIUM_ERROR; | 584 | sb[7] = 0x0a; |
307 | sb[7] = 0x0A; | 585 | if (tf->flags & ATA_TFLAG_LBA && !(tf->flags & ATA_TFLAG_LBA48)) { |
308 | if (cmd->sc_data_direction == DMA_FROM_DEVICE) { | 586 | /* A small (28b) LBA will fit in the 32b info field */ |
309 | sb[12] = 0x11; /* "unrecovered read error" */ | 587 | sb[0] |= 0x80; /* set valid bit */ |
310 | sb[13] = 0x04; | 588 | sb[3] = tf->device & 0x0f; |
311 | } else { | 589 | sb[4] = tf->lbah; |
312 | sb[12] = 0x0C; /* "write error - */ | 590 | sb[5] = tf->lbam; |
313 | sb[13] = 0x02; /* auto-reallocation failed" */ | 591 | sb[6] = tf->lbal; |
314 | } | 592 | } |
315 | } | 593 | } |
316 | 594 | ||
@@ -626,11 +904,36 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd) | |||
626 | static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat) | 904 | static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat) |
627 | { | 905 | { |
628 | struct scsi_cmnd *cmd = qc->scsicmd; | 906 | struct scsi_cmnd *cmd = qc->scsicmd; |
907 | int need_sense = drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ); | ||
908 | |||
909 | /* For ATA pass thru (SAT) commands, generate a sense block if | ||
910 | * user mandated it or if there's an error. Note that if we | ||
911 | * generate because the user forced us to, a check condition | ||
912 | * is generated and the ATA register values are returned | ||
913 | * whether the command completed successfully or not. If there | ||
914 | * was no error, SK, ASC and ASCQ will all be zero. | ||
915 | */ | ||
916 | if (((cmd->cmnd[0] == ATA_16) || (cmd->cmnd[0] == ATA_12)) && | ||
917 | ((cmd->cmnd[2] & 0x20) || need_sense)) { | ||
918 | ata_gen_ata_desc_sense(qc); | ||
919 | } else { | ||
920 | if (!need_sense) { | ||
921 | cmd->result = SAM_STAT_GOOD; | ||
922 | } else { | ||
923 | /* TODO: decide which descriptor format to use | ||
924 | * for 48b LBA devices and call that here | ||
925 | * instead of the fixed desc, which is only | ||
926 | * good for smaller LBA (and maybe CHS?) | ||
927 | * devices. | ||
928 | */ | ||
929 | ata_gen_fixed_sense(qc); | ||
930 | } | ||
931 | } | ||
629 | 932 | ||
630 | if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) | 933 | if (need_sense) { |
631 | ata_to_sense_error(qc, drv_stat); | 934 | /* The ata_gen_..._sense routines fill in tf */ |
632 | else | 935 | ata_dump_status(qc->ap->id, &qc->tf); |
633 | cmd->result = SAM_STAT_GOOD; | 936 | } |
634 | 937 | ||
635 | qc->scsidone(cmd); | 938 | qc->scsidone(cmd); |
636 | 939 | ||
@@ -671,8 +974,8 @@ static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev, | |||
671 | return; | 974 | return; |
672 | 975 | ||
673 | /* data is present; dma-map it */ | 976 | /* data is present; dma-map it */ |
674 | if (cmd->sc_data_direction == DMA_FROM_DEVICE || | 977 | if (cmd->sc_data_direction == SCSI_DATA_READ || |
675 | cmd->sc_data_direction == DMA_TO_DEVICE) { | 978 | cmd->sc_data_direction == SCSI_DATA_WRITE) { |
676 | if (unlikely(cmd->request_bufflen < 1)) { | 979 | if (unlikely(cmd->request_bufflen < 1)) { |
677 | printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n", | 980 | printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n", |
678 | ap->id, dev->devno); | 981 | ap->id, dev->devno); |
@@ -692,7 +995,6 @@ static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev, | |||
692 | 995 | ||
693 | if (xlat_func(qc, scsicmd)) | 996 | if (xlat_func(qc, scsicmd)) |
694 | goto err_out; | 997 | goto err_out; |
695 | |||
696 | /* select device, send command to hardware */ | 998 | /* select device, send command to hardware */ |
697 | if (ata_qc_issue(qc)) | 999 | if (ata_qc_issue(qc)) |
698 | goto err_out; | 1000 | goto err_out; |
@@ -1304,7 +1606,7 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd) | |||
1304 | struct scsi_cmnd *cmd = qc->scsicmd; | 1606 | struct scsi_cmnd *cmd = qc->scsicmd; |
1305 | struct ata_device *dev = qc->dev; | 1607 | struct ata_device *dev = qc->dev; |
1306 | int using_pio = (dev->flags & ATA_DFLAG_PIO); | 1608 | int using_pio = (dev->flags & ATA_DFLAG_PIO); |
1307 | int nodata = (cmd->sc_data_direction == DMA_NONE); | 1609 | int nodata = (cmd->sc_data_direction == SCSI_DATA_NONE); |
1308 | 1610 | ||
1309 | if (!using_pio) | 1611 | if (!using_pio) |
1310 | /* Check whether ATAPI DMA is safe */ | 1612 | /* Check whether ATAPI DMA is safe */ |
@@ -1316,7 +1618,7 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd) | |||
1316 | qc->complete_fn = atapi_qc_complete; | 1618 | qc->complete_fn = atapi_qc_complete; |
1317 | 1619 | ||
1318 | qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; | 1620 | qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; |
1319 | if (cmd->sc_data_direction == DMA_TO_DEVICE) { | 1621 | if (cmd->sc_data_direction == SCSI_DATA_WRITE) { |
1320 | qc->tf.flags |= ATA_TFLAG_WRITE; | 1622 | qc->tf.flags |= ATA_TFLAG_WRITE; |
1321 | DPRINTK("direction: write\n"); | 1623 | DPRINTK("direction: write\n"); |
1322 | } | 1624 | } |
@@ -1340,7 +1642,7 @@ static unsigned int atapi_xlat(struct ata_queued_cmd *qc, u8 *scsicmd) | |||
1340 | 1642 | ||
1341 | #ifdef ATAPI_ENABLE_DMADIR | 1643 | #ifdef ATAPI_ENABLE_DMADIR |
1342 | /* some SATA bridges need us to indicate data xfer direction */ | 1644 | /* some SATA bridges need us to indicate data xfer direction */ |
1343 | if (cmd->sc_data_direction != DMA_TO_DEVICE) | 1645 | if (cmd->sc_data_direction != SCSI_DATA_WRITE) |
1344 | qc->tf.feature |= ATAPI_DMADIR; | 1646 | qc->tf.feature |= ATAPI_DMADIR; |
1345 | #endif | 1647 | #endif |
1346 | } | 1648 | } |
@@ -1393,6 +1695,143 @@ ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev) | |||
1393 | return dev; | 1695 | return dev; |
1394 | } | 1696 | } |
1395 | 1697 | ||
1698 | /* | ||
1699 | * ata_scsi_map_proto - Map pass-thru protocol value to taskfile value. | ||
1700 | * @byte1: Byte 1 from pass-thru CDB. | ||
1701 | * | ||
1702 | * RETURNS: | ||
1703 | * ATA_PROT_UNKNOWN if mapping failed/unimplemented, protocol otherwise. | ||
1704 | */ | ||
1705 | static u8 | ||
1706 | ata_scsi_map_proto(u8 byte1) | ||
1707 | { | ||
1708 | switch((byte1 & 0x1e) >> 1) { | ||
1709 | case 3: /* Non-data */ | ||
1710 | return ATA_PROT_NODATA; | ||
1711 | |||
1712 | case 6: /* DMA */ | ||
1713 | return ATA_PROT_DMA; | ||
1714 | |||
1715 | case 4: /* PIO Data-in */ | ||
1716 | case 5: /* PIO Data-out */ | ||
1717 | if (byte1 & 0xe0) { | ||
1718 | return ATA_PROT_PIO_MULT; | ||
1719 | } | ||
1720 | return ATA_PROT_PIO; | ||
1721 | |||
1722 | case 10: /* Device Reset */ | ||
1723 | case 0: /* Hard Reset */ | ||
1724 | case 1: /* SRST */ | ||
1725 | case 2: /* Bus Idle */ | ||
1726 | case 7: /* Packet */ | ||
1727 | case 8: /* DMA Queued */ | ||
1728 | case 9: /* Device Diagnostic */ | ||
1729 | case 11: /* UDMA Data-in */ | ||
1730 | case 12: /* UDMA Data-Out */ | ||
1731 | case 13: /* FPDMA */ | ||
1732 | default: /* Reserved */ | ||
1733 | break; | ||
1734 | } | ||
1735 | |||
1736 | return ATA_PROT_UNKNOWN; | ||
1737 | } | ||
1738 | |||
1739 | /** | ||
1740 | * ata_scsi_pass_thru - convert ATA pass-thru CDB to taskfile | ||
1741 | * @qc: command structure to be initialized | ||
1742 | * @cmd: SCSI command to convert | ||
1743 | * | ||
1744 | * Handles either 12 or 16-byte versions of the CDB. | ||
1745 | * | ||
1746 | * RETURNS: | ||
1747 | * Zero on success, non-zero on failure. | ||
1748 | */ | ||
1749 | static unsigned int | ||
1750 | ata_scsi_pass_thru(struct ata_queued_cmd *qc, u8 *scsicmd) | ||
1751 | { | ||
1752 | struct ata_taskfile *tf = &(qc->tf); | ||
1753 | struct scsi_cmnd *cmd = qc->scsicmd; | ||
1754 | |||
1755 | if ((tf->protocol = ata_scsi_map_proto(scsicmd[1])) == ATA_PROT_UNKNOWN) | ||
1756 | return 1; | ||
1757 | |||
1758 | /* | ||
1759 | * 12 and 16 byte CDBs use different offsets to | ||
1760 | * provide the various register values. | ||
1761 | */ | ||
1762 | if (scsicmd[0] == ATA_16) { | ||
1763 | /* | ||
1764 | * 16-byte CDB - may contain extended commands. | ||
1765 | * | ||
1766 | * If that is the case, copy the upper byte register values. | ||
1767 | */ | ||
1768 | if (scsicmd[1] & 0x01) { | ||
1769 | tf->hob_feature = scsicmd[3]; | ||
1770 | tf->hob_nsect = scsicmd[5]; | ||
1771 | tf->hob_lbal = scsicmd[7]; | ||
1772 | tf->hob_lbam = scsicmd[9]; | ||
1773 | tf->hob_lbah = scsicmd[11]; | ||
1774 | tf->flags |= ATA_TFLAG_LBA48; | ||
1775 | } else | ||
1776 | tf->flags &= ~ATA_TFLAG_LBA48; | ||
1777 | |||
1778 | /* | ||
1779 | * Always copy low byte, device and command registers. | ||
1780 | */ | ||
1781 | tf->feature = scsicmd[4]; | ||
1782 | tf->nsect = scsicmd[6]; | ||
1783 | tf->lbal = scsicmd[8]; | ||
1784 | tf->lbam = scsicmd[10]; | ||
1785 | tf->lbah = scsicmd[12]; | ||
1786 | tf->device = scsicmd[13]; | ||
1787 | tf->command = scsicmd[14]; | ||
1788 | } else { | ||
1789 | /* | ||
1790 | * 12-byte CDB - incapable of extended commands. | ||
1791 | */ | ||
1792 | tf->flags &= ~ATA_TFLAG_LBA48; | ||
1793 | |||
1794 | tf->feature = scsicmd[3]; | ||
1795 | tf->nsect = scsicmd[4]; | ||
1796 | tf->lbal = scsicmd[5]; | ||
1797 | tf->lbam = scsicmd[6]; | ||
1798 | tf->lbah = scsicmd[7]; | ||
1799 | tf->device = scsicmd[8]; | ||
1800 | tf->command = scsicmd[9]; | ||
1801 | } | ||
1802 | |||
1803 | /* | ||
1804 | * Filter SET_FEATURES - XFER MODE command -- otherwise, | ||
1805 | * SET_FEATURES - XFER MODE must be preceded/succeeded | ||
1806 | * by an update to hardware-specific registers for each | ||
1807 | * controller (i.e. the reason for ->set_piomode(), | ||
1808 | * ->set_dmamode(), and ->post_set_mode() hooks). | ||
1809 | */ | ||
1810 | if ((tf->command == ATA_CMD_SET_FEATURES) | ||
1811 | && (tf->feature == SETFEATURES_XFER)) | ||
1812 | return 1; | ||
1813 | |||
1814 | /* | ||
1815 | * Set flags so that all registers will be written, | ||
1816 | * and pass on write indication (used for PIO/DMA | ||
1817 | * setup.) | ||
1818 | */ | ||
1819 | tf->flags |= (ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE); | ||
1820 | |||
1821 | if (cmd->sc_data_direction == SCSI_DATA_WRITE) | ||
1822 | tf->flags |= ATA_TFLAG_WRITE; | ||
1823 | |||
1824 | /* | ||
1825 | * Set transfer length. | ||
1826 | * | ||
1827 | * TODO: find out if we need to do more here to | ||
1828 | * cover scatter/gather case. | ||
1829 | */ | ||
1830 | qc->nsect = cmd->bufflen / ATA_SECT_SIZE; | ||
1831 | |||
1832 | return 0; | ||
1833 | } | ||
1834 | |||
1396 | /** | 1835 | /** |
1397 | * ata_get_xlat_func - check if SCSI to ATA translation is possible | 1836 | * ata_get_xlat_func - check if SCSI to ATA translation is possible |
1398 | * @dev: ATA device | 1837 | * @dev: ATA device |
@@ -1425,6 +1864,10 @@ static inline ata_xlat_func_t ata_get_xlat_func(struct ata_device *dev, u8 cmd) | |||
1425 | case VERIFY: | 1864 | case VERIFY: |
1426 | case VERIFY_16: | 1865 | case VERIFY_16: |
1427 | return ata_scsi_verify_xlat; | 1866 | return ata_scsi_verify_xlat; |
1867 | |||
1868 | case ATA_12: | ||
1869 | case ATA_16: | ||
1870 | return ata_scsi_pass_thru; | ||
1428 | } | 1871 | } |
1429 | 1872 | ||
1430 | return NULL; | 1873 | return NULL; |
@@ -1581,7 +2024,7 @@ void ata_scsi_simulate(u16 *id, | |||
1581 | ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns); | 2024 | ata_scsi_rbuf_fill(&args, ata_scsiop_report_luns); |
1582 | break; | 2025 | break; |
1583 | 2026 | ||
1584 | /* mandantory commands we haven't implemented yet */ | 2027 | /* mandatory commands we haven't implemented yet */ |
1585 | case REQUEST_SENSE: | 2028 | case REQUEST_SENSE: |
1586 | 2029 | ||
1587 | /* all other commands */ | 2030 | /* all other commands */ |
diff --git a/drivers/scsi/libata.h b/drivers/scsi/libata.h index 6518226b8f87..c595f5f58c16 100644 --- a/drivers/scsi/libata.h +++ b/drivers/scsi/libata.h | |||
@@ -44,10 +44,11 @@ extern void ata_dev_select(struct ata_port *ap, unsigned int device, | |||
44 | unsigned int wait, unsigned int can_sleep); | 44 | unsigned int wait, unsigned int can_sleep); |
45 | extern void ata_tf_to_host_nolock(struct ata_port *ap, struct ata_taskfile *tf); | 45 | extern void ata_tf_to_host_nolock(struct ata_port *ap, struct ata_taskfile *tf); |
46 | extern void swap_buf_le16(u16 *buf, unsigned int buf_words); | 46 | extern void swap_buf_le16(u16 *buf, unsigned int buf_words); |
47 | extern int ata_task_ioctl(struct scsi_device *scsidev, void __user *arg); | ||
48 | extern int ata_cmd_ioctl(struct scsi_device *scsidev, void __user *arg); | ||
47 | 49 | ||
48 | 50 | ||
49 | /* libata-scsi.c */ | 51 | /* libata-scsi.c */ |
50 | extern void ata_to_sense_error(struct ata_queued_cmd *qc, u8 drv_stat); | ||
51 | extern int ata_scsi_error(struct Scsi_Host *host); | 52 | extern int ata_scsi_error(struct Scsi_Host *host); |
52 | extern unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf, | 53 | extern unsigned int ata_scsiop_inq_std(struct ata_scsi_args *args, u8 *rbuf, |
53 | unsigned int buflen); | 54 | unsigned int buflen); |
diff --git a/include/scsi/scsi.h b/include/scsi/scsi.h index 659ecf48fb4a..27d4d9ea6c6d 100644 --- a/include/scsi/scsi.h +++ b/include/scsi/scsi.h | |||
@@ -113,6 +113,9 @@ extern const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE]; | |||
113 | /* values for service action in */ | 113 | /* values for service action in */ |
114 | #define SAI_READ_CAPACITY_16 0x10 | 114 | #define SAI_READ_CAPACITY_16 0x10 |
115 | 115 | ||
116 | /* Values for T10/04-262r7 */ | ||
117 | #define ATA_16 0x85 /* 16-byte pass-thru */ | ||
118 | #define ATA_12 0xa1 /* 12-byte pass-thru */ | ||
116 | 119 | ||
117 | /* | 120 | /* |
118 | * SCSI Architecture Model (SAM) Status codes. Taken from SAM-3 draft | 121 | * SCSI Architecture Model (SAM) Status codes. Taken from SAM-3 draft |