Merge master.kernel.org:/pub/scm/linux/kernel/git/davem/sparc-2.6
[deliverable/linux.git] / drivers / scsi / ide-scsi.c
CommitLineData
1da177e4
LT
1/*
2 * linux/drivers/scsi/ide-scsi.c Version 0.9 Jul 4, 1999
3 *
4 * Copyright (C) 1996 - 1999 Gadi Oxman <gadio@netvision.net.il>
5 */
6
7/*
8 * Emulation of a SCSI host adapter for IDE ATAPI devices.
9 *
10 * With this driver, one can use the Linux SCSI drivers instead of the
11 * native IDE ATAPI drivers.
12 *
13 * Ver 0.1 Dec 3 96 Initial version.
14 * Ver 0.2 Jan 26 97 Fixed bug in cleanup_module() and added emulation
15 * of MODE_SENSE_6/MODE_SELECT_6 for cdroms. Thanks
16 * to Janos Farkas for pointing this out.
17 * Avoid using bitfields in structures for m68k.
18 * Added Scatter/Gather and DMA support.
19 * Ver 0.4 Dec 7 97 Add support for ATAPI PD/CD drives.
20 * Use variable timeout for each command.
21 * Ver 0.5 Jan 2 98 Fix previous PD/CD support.
22 * Allow disabling of SCSI-6 to SCSI-10 transformation.
23 * Ver 0.6 Jan 27 98 Allow disabling of SCSI command translation layer
24 * for access through /dev/sg.
25 * Fix MODE_SENSE_6/MODE_SELECT_6/INQUIRY translation.
26 * Ver 0.7 Dec 04 98 Ignore commands where lun != 0 to avoid multiple
27 * detection of devices with CONFIG_SCSI_MULTI_LUN
28 * Ver 0.8 Feb 05 99 Optical media need translation too. Reverse 0.7.
29 * Ver 0.9 Jul 04 99 Fix a bug in SG_SET_TRANSFORM.
30 * Ver 0.91 Jun 10 02 Fix "off by one" error in transforms
31 * Ver 0.92 Dec 31 02 Implement new SCSI mid level API
32 */
33
34#define IDESCSI_VERSION "0.92"
35
36#include <linux/module.h>
1da177e4
LT
37#include <linux/types.h>
38#include <linux/string.h>
39#include <linux/kernel.h>
40#include <linux/mm.h>
41#include <linux/ioport.h>
42#include <linux/blkdev.h>
43#include <linux/errno.h>
44#include <linux/hdreg.h>
45#include <linux/slab.h>
46#include <linux/ide.h>
47#include <linux/scatterlist.h>
df0ae249 48#include <linux/delay.h>
e723ccd8 49#include <linux/mutex.h>
1977f032 50#include <linux/bitops.h>
1da177e4
LT
51
52#include <asm/io.h>
1da177e4
LT
53#include <asm/uaccess.h>
54
55#include <scsi/scsi.h>
56#include <scsi/scsi_cmnd.h>
57#include <scsi/scsi_device.h>
58#include <scsi/scsi_host.h>
59#include <scsi/scsi_tcq.h>
60#include <scsi/sg.h>
61
62#define IDESCSI_DEBUG_LOG 0
63
64typedef struct idescsi_pc_s {
65 u8 c[12]; /* Actual packet bytes */
66 int request_transfer; /* Bytes to transfer */
67 int actually_transferred; /* Bytes actually transferred */
68 int buffer_size; /* Size of our data buffer */
69 struct request *rq; /* The corresponding request */
70 u8 *buffer; /* Data buffer */
71 u8 *current_position; /* Pointer into the above buffer */
72 struct scatterlist *sg; /* Scatter gather table */
c79d88b7 73 unsigned int sg_cnt; /* Number of entries in sg */
1da177e4
LT
74 int b_count; /* Bytes transferred from current entry */
75 struct scsi_cmnd *scsi_cmd; /* SCSI command */
76 void (*done)(struct scsi_cmnd *); /* Scsi completion routine */
77 unsigned long flags; /* Status/Action flags */
78 unsigned long timeout; /* Command timeout */
79} idescsi_pc_t;
80
81/*
82 * Packet command status bits.
83 */
84#define PC_DMA_IN_PROGRESS 0 /* 1 while DMA in progress */
85#define PC_WRITING 1 /* Data direction */
1da177e4
LT
86#define PC_TIMEDOUT 3 /* command timed out */
87#define PC_DMA_OK 4 /* Use DMA */
88
89/*
90 * SCSI command transformation layer
91 */
1da177e4
LT
92#define IDESCSI_SG_TRANSFORM 1 /* /dev/sg transformation */
93
94/*
95 * Log flags
96 */
97#define IDESCSI_LOG_CMD 0 /* Log SCSI commands */
98
99typedef struct ide_scsi_obj {
100 ide_drive_t *drive;
101 ide_driver_t *driver;
102 struct gendisk *disk;
103 struct Scsi_Host *host;
104
105 idescsi_pc_t *pc; /* Current packet command */
106 unsigned long flags; /* Status/Action flags */
107 unsigned long transform; /* SCSI cmd translation layer */
108 unsigned long log; /* log flags */
109} idescsi_scsi_t;
110
e723ccd8 111static DEFINE_MUTEX(idescsi_ref_mutex);
aaeab80b 112static int idescsi_nocd; /* Set by module param to skip cd */
1da177e4
LT
113
114#define ide_scsi_g(disk) \
115 container_of((disk)->private_data, struct ide_scsi_obj, driver)
116
117static struct ide_scsi_obj *ide_scsi_get(struct gendisk *disk)
118{
119 struct ide_scsi_obj *scsi = NULL;
120
e723ccd8 121 mutex_lock(&idescsi_ref_mutex);
1da177e4
LT
122 scsi = ide_scsi_g(disk);
123 if (scsi)
124 scsi_host_get(scsi->host);
e723ccd8 125 mutex_unlock(&idescsi_ref_mutex);
1da177e4
LT
126 return scsi;
127}
128
129static void ide_scsi_put(struct ide_scsi_obj *scsi)
130{
e723ccd8 131 mutex_lock(&idescsi_ref_mutex);
1da177e4 132 scsi_host_put(scsi->host);
e723ccd8 133 mutex_unlock(&idescsi_ref_mutex);
1da177e4
LT
134}
135
136static inline idescsi_scsi_t *scsihost_to_idescsi(struct Scsi_Host *host)
137{
138 return (idescsi_scsi_t*) (&host[1]);
139}
140
141static inline idescsi_scsi_t *drive_to_idescsi(ide_drive_t *ide_drive)
142{
143 return scsihost_to_idescsi(ide_drive->driver_data);
144}
145
146/*
147 * Per ATAPI device status bits.
148 */
149#define IDESCSI_DRQ_INTERRUPT 0 /* DRQ interrupt device */
150
151/*
152 * ide-scsi requests.
153 */
154#define IDESCSI_PC_RQ 90
155
156static void idescsi_discard_data (ide_drive_t *drive, unsigned int bcount)
157{
158 while (bcount--)
159 (void) HWIF(drive)->INB(IDE_DATA_REG);
160}
161
162static void idescsi_output_zeros (ide_drive_t *drive, unsigned int bcount)
163{
164 while (bcount--)
165 HWIF(drive)->OUTB(0, IDE_DATA_REG);
166}
167
168/*
169 * PIO data transfer routines using the scatter gather table.
170 */
171static void idescsi_input_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
172{
173 int count;
174 char *buf;
175
176 while (bcount) {
1da177e4 177 count = min(pc->sg->length - pc->b_count, bcount);
45711f1a 178 if (PageHighMem(sg_page(pc->sg))) {
a717f773
AM
179 unsigned long flags;
180
181 local_irq_save(flags);
45711f1a 182 buf = kmap_atomic(sg_page(pc->sg), KM_IRQ0) +
a717f773
AM
183 pc->sg->offset;
184 drive->hwif->atapi_input_bytes(drive,
185 buf + pc->b_count, count);
186 kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
187 local_irq_restore(flags);
188 } else {
45711f1a 189 buf = sg_virt(pc->sg);
a717f773
AM
190 drive->hwif->atapi_input_bytes(drive,
191 buf + pc->b_count, count);
192 }
193 bcount -= count; pc->b_count += count;
1da177e4 194 if (pc->b_count == pc->sg->length) {
c79d88b7 195 if (!--pc->sg_cnt)
d274a987
JA
196 break;
197 pc->sg = sg_next(pc->sg);
1da177e4
LT
198 pc->b_count = 0;
199 }
200 }
d274a987
JA
201
202 if (bcount) {
203 printk (KERN_ERR "ide-scsi: scatter gather table too small, discarding data\n");
204 idescsi_discard_data (drive, bcount);
205 }
1da177e4
LT
206}
207
208static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
209{
210 int count;
211 char *buf;
212
213 while (bcount) {
1da177e4 214 count = min(pc->sg->length - pc->b_count, bcount);
45711f1a 215 if (PageHighMem(sg_page(pc->sg))) {
a717f773
AM
216 unsigned long flags;
217
218 local_irq_save(flags);
45711f1a 219 buf = kmap_atomic(sg_page(pc->sg), KM_IRQ0) +
a717f773
AM
220 pc->sg->offset;
221 drive->hwif->atapi_output_bytes(drive,
222 buf + pc->b_count, count);
223 kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
224 local_irq_restore(flags);
225 } else {
45711f1a 226 buf = sg_virt(pc->sg);
a717f773
AM
227 drive->hwif->atapi_output_bytes(drive,
228 buf + pc->b_count, count);
229 }
230 bcount -= count; pc->b_count += count;
1da177e4 231 if (pc->b_count == pc->sg->length) {
c79d88b7 232 if (!--pc->sg_cnt)
d274a987
JA
233 break;
234 pc->sg = sg_next(pc->sg);
1da177e4
LT
235 pc->b_count = 0;
236 }
237 }
d274a987
JA
238
239 if (bcount) {
240 printk (KERN_ERR "ide-scsi: scatter gather table too small, padding with zeros\n");
241 idescsi_output_zeros (drive, bcount);
242 }
1da177e4
LT
243}
244
1da177e4
LT
245static int idescsi_check_condition(ide_drive_t *drive, struct request *failed_command)
246{
247 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
248 idescsi_pc_t *pc;
249 struct request *rq;
250 u8 *buf;
251
252 /* stuff a sense request in front of our current request */
41ead3c9
MK
253 pc = kzalloc(sizeof(idescsi_pc_t), GFP_ATOMIC);
254 rq = kmalloc(sizeof(struct request), GFP_ATOMIC);
255 buf = kzalloc(SCSI_SENSE_BUFFERSIZE, GFP_ATOMIC);
256 if (!pc || !rq || !buf) {
c9475cb0
JJ
257 kfree(buf);
258 kfree(rq);
259 kfree(pc);
1da177e4
LT
260 return -ENOMEM;
261 }
1da177e4
LT
262 ide_init_drive_cmd(rq);
263 rq->special = (char *) pc;
264 pc->rq = rq;
265 pc->buffer = buf;
266 pc->c[0] = REQUEST_SENSE;
267 pc->c[4] = pc->request_transfer = pc->buffer_size = SCSI_SENSE_BUFFERSIZE;
4aff5e23 268 rq->cmd_type = REQ_TYPE_SENSE;
1da177e4
LT
269 pc->timeout = jiffies + WAIT_READY;
270 /* NOTE! Save the failed packet command in "rq->buffer" */
271 rq->buffer = (void *) failed_command->special;
272 pc->scsi_cmd = ((idescsi_pc_t *) failed_command->special)->scsi_cmd;
273 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
274 printk ("ide-scsi: %s: queue cmd = ", drive->name);
6413f086
DC
275 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_NONE, 16, 1, pc->c,
276 6, 0);
1da177e4
LT
277 }
278 rq->rq_disk = scsi->disk;
279 return ide_do_drive_cmd(drive, rq, ide_preempt);
280}
281
282static int idescsi_end_request(ide_drive_t *, int, int);
283
284static ide_startstop_t
285idescsi_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
286{
287 if (HWIF(drive)->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
288 /* force an abort */
289 HWIF(drive)->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
290
291 rq->errors++;
292
293 idescsi_end_request(drive, 0, 0);
294
295 return ide_stopped;
296}
297
298static ide_startstop_t
299idescsi_atapi_abort(ide_drive_t *drive, struct request *rq)
300{
301#if IDESCSI_DEBUG_LOG
302 printk(KERN_WARNING "idescsi_atapi_abort called for %lu\n",
303 ((idescsi_pc_t *) rq->special)->scsi_cmd->serial_number);
304#endif
305 rq->errors |= ERROR_MAX;
306
307 idescsi_end_request(drive, 0, 0);
308
309 return ide_stopped;
310}
311
312static int idescsi_end_request (ide_drive_t *drive, int uptodate, int nrsecs)
313{
314 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
315 struct request *rq = HWGROUP(drive)->rq;
316 idescsi_pc_t *pc = (idescsi_pc_t *) rq->special;
317 int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
318 struct Scsi_Host *host;
32565347 319 int errors = rq->errors;
1da177e4
LT
320 unsigned long flags;
321
4aff5e23 322 if (!blk_special_request(rq) && !blk_sense_request(rq)) {
1da177e4
LT
323 ide_end_request(drive, uptodate, nrsecs);
324 return 0;
325 }
326 ide_end_drive_cmd (drive, 0, 0);
4aff5e23 327 if (blk_sense_request(rq)) {
1da177e4
LT
328 idescsi_pc_t *opc = (idescsi_pc_t *) rq->buffer;
329 if (log) {
330 printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive->name, opc->scsi_cmd->serial_number);
6413f086
DC
331 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_NONE, 16, 1,
332 pc->buffer, 16, 0);
1da177e4
LT
333 }
334 memcpy((void *) opc->scsi_cmd->sense_buffer, pc->buffer, SCSI_SENSE_BUFFERSIZE);
335 kfree(pc->buffer);
336 kfree(pc);
337 kfree(rq);
338 pc = opc;
339 rq = pc->rq;
340 pc->scsi_cmd->result = (CHECK_CONDITION << 1) |
341 ((test_bit(PC_TIMEDOUT, &pc->flags)?DID_TIME_OUT:DID_OK) << 16);
342 } else if (test_bit(PC_TIMEDOUT, &pc->flags)) {
343 if (log)
344 printk (KERN_WARNING "ide-scsi: %s: timed out for %lu\n",
345 drive->name, pc->scsi_cmd->serial_number);
346 pc->scsi_cmd->result = DID_TIME_OUT << 16;
32565347 347 } else if (errors >= ERROR_MAX) {
1da177e4
LT
348 pc->scsi_cmd->result = DID_ERROR << 16;
349 if (log)
350 printk ("ide-scsi: %s: I/O error for %lu\n", drive->name, pc->scsi_cmd->serial_number);
32565347 351 } else if (errors) {
1da177e4
LT
352 if (log)
353 printk ("ide-scsi: %s: check condition for %lu\n", drive->name, pc->scsi_cmd->serial_number);
354 if (!idescsi_check_condition(drive, rq))
355 /* we started a request sense, so we'll be back, exit for now */
356 return 0;
357 pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
358 } else {
359 pc->scsi_cmd->result = DID_OK << 16;
1da177e4
LT
360 }
361 host = pc->scsi_cmd->device->host;
362 spin_lock_irqsave(host->host_lock, flags);
363 pc->done(pc->scsi_cmd);
364 spin_unlock_irqrestore(host->host_lock, flags);
365 kfree(pc);
366 kfree(rq);
367 scsi->pc = NULL;
368 return 0;
369}
370
371static inline unsigned long get_timeout(idescsi_pc_t *pc)
372{
373 return max_t(unsigned long, WAIT_CMD, pc->timeout - jiffies);
374}
375
376static int idescsi_expiry(ide_drive_t *drive)
377{
d1be0a82 378 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
1da177e4
LT
379 idescsi_pc_t *pc = scsi->pc;
380
381#if IDESCSI_DEBUG_LOG
382 printk(KERN_WARNING "idescsi_expiry called for %lu at %lu\n", pc->scsi_cmd->serial_number, jiffies);
383#endif
384 set_bit(PC_TIMEDOUT, &pc->flags);
385
386 return 0; /* we do not want the ide subsystem to retry */
387}
388
389/*
390 * Our interrupt handler.
391 */
392static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
393{
394 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
395 idescsi_pc_t *pc=scsi->pc;
396 struct request *rq = pc->rq;
397 atapi_bcount_t bcount;
398 atapi_status_t status;
399 atapi_ireason_t ireason;
400 atapi_feature_t feature;
401
402 unsigned int temp;
403
404#if IDESCSI_DEBUG_LOG
405 printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handler\n");
406#endif /* IDESCSI_DEBUG_LOG */
407
408 if (test_bit(PC_TIMEDOUT, &pc->flags)){
409#if IDESCSI_DEBUG_LOG
410 printk(KERN_WARNING "idescsi_pc_intr: got timed out packet %lu at %lu\n",
411 pc->scsi_cmd->serial_number, jiffies);
412#endif
413 /* end this request now - scsi should retry it*/
414 idescsi_end_request (drive, 1, 0);
415 return ide_stopped;
416 }
417 if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
418#if IDESCSI_DEBUG_LOG
419 printk ("ide-scsi: %s: DMA complete\n", drive->name);
420#endif /* IDESCSI_DEBUG_LOG */
421 pc->actually_transferred=pc->request_transfer;
422 (void) HWIF(drive)->ide_dma_end(drive);
423 }
424
425 feature.all = 0;
426 /* Clear the interrupt */
427 status.all = HWIF(drive)->INB(IDE_STATUS_REG);
428
429 if (!status.b.drq) {
430 /* No more interrupts */
431 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
432 printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
36e8e578 433 local_irq_enable_in_hardirq();
1da177e4
LT
434 if (status.b.check)
435 rq->errors++;
436 idescsi_end_request (drive, 1, 0);
437 return ide_stopped;
438 }
439 bcount.b.low = HWIF(drive)->INB(IDE_BCOUNTL_REG);
440 bcount.b.high = HWIF(drive)->INB(IDE_BCOUNTH_REG);
441 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
442
443 if (ireason.b.cod) {
444 printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
445 return ide_do_reset (drive);
446 }
447 if (ireason.b.io) {
448 temp = pc->actually_transferred + bcount.all;
449 if (temp > pc->request_transfer) {
450 if (temp > pc->buffer_size) {
451 printk(KERN_ERR "ide-scsi: The scsi wants to "
452 "send us more data than expected "
453 "- discarding data\n");
454 temp = pc->buffer_size - pc->actually_transferred;
455 if (temp) {
456 clear_bit(PC_WRITING, &pc->flags);
457 if (pc->sg)
458 idescsi_input_buffers(drive, pc, temp);
459 else
460 drive->hwif->atapi_input_bytes(drive, pc->current_position, temp);
461 printk(KERN_ERR "ide-scsi: transferred %d of %d bytes\n", temp, bcount.all);
462 }
463 pc->actually_transferred += temp;
464 pc->current_position += temp;
465 idescsi_discard_data(drive, bcount.all - temp);
466 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
467 return ide_started;
468 }
469#if IDESCSI_DEBUG_LOG
470 printk (KERN_NOTICE "ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n");
471#endif /* IDESCSI_DEBUG_LOG */
472 }
473 }
474 if (ireason.b.io) {
475 clear_bit(PC_WRITING, &pc->flags);
476 if (pc->sg)
477 idescsi_input_buffers(drive, pc, bcount.all);
478 else
479 HWIF(drive)->atapi_input_bytes(drive, pc->current_position, bcount.all);
480 } else {
481 set_bit(PC_WRITING, &pc->flags);
482 if (pc->sg)
483 idescsi_output_buffers (drive, pc, bcount.all);
484 else
485 HWIF(drive)->atapi_output_bytes(drive, pc->current_position, bcount.all);
486 }
487 /* Update the current position */
488 pc->actually_transferred += bcount.all;
489 pc->current_position += bcount.all;
490
491 /* And set the interrupt handler again */
492 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
493 return ide_started;
494}
495
496static ide_startstop_t idescsi_transfer_pc(ide_drive_t *drive)
497{
498 ide_hwif_t *hwif = drive->hwif;
499 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
500 idescsi_pc_t *pc = scsi->pc;
501 atapi_ireason_t ireason;
502 ide_startstop_t startstop;
503
504 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
505 printk(KERN_ERR "ide-scsi: Strange, packet command "
506 "initiated yet DRQ isn't asserted\n");
507 return startstop;
508 }
509 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
510 if (!ireason.b.cod || ireason.b.io) {
511 printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while "
512 "issuing a packet command\n");
513 return ide_do_reset (drive);
514 }
125e1874 515 BUG_ON(HWGROUP(drive)->handler != NULL);
1da177e4
LT
516 /* Set the interrupt routine */
517 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
518 /* Send the actual packet */
519 drive->hwif->atapi_output_bytes(drive, scsi->pc->c, 12);
520 if (test_bit (PC_DMA_OK, &pc->flags)) {
521 set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
522 hwif->dma_start(drive);
523 }
524 return ide_started;
525}
526
527static inline int idescsi_set_direction(idescsi_pc_t *pc)
528{
529 switch (pc->c[0]) {
530 case READ_6: case READ_10: case READ_12:
531 clear_bit(PC_WRITING, &pc->flags);
532 return 0;
533 case WRITE_6: case WRITE_10: case WRITE_12:
534 set_bit(PC_WRITING, &pc->flags);
535 return 0;
536 default:
537 return 1;
538 }
539}
540
541static int idescsi_map_sg(ide_drive_t *drive, idescsi_pc_t *pc)
542{
543 ide_hwif_t *hwif = drive->hwif;
544 struct scatterlist *sg, *scsi_sg;
545 int segments;
546
547 if (!pc->request_transfer || pc->request_transfer % 1024)
548 return 1;
549
550 if (idescsi_set_direction(pc))
551 return 1;
552
553 sg = hwif->sg_table;
427d0bd4
BH
554 scsi_sg = scsi_sglist(pc->scsi_cmd);
555 segments = scsi_sg_count(pc->scsi_cmd);
1da177e4
LT
556
557 if (segments > hwif->sg_max_nents)
558 return 1;
559
427d0bd4
BH
560 hwif->sg_nents = segments;
561 memcpy(sg, scsi_sg, sizeof(*sg) * segments);
1da177e4
LT
562
563 return 0;
564}
565
566/*
567 * Issue a packet command
568 */
569static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
570{
571 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
572 ide_hwif_t *hwif = drive->hwif;
573 atapi_feature_t feature;
574 atapi_bcount_t bcount;
575
576 scsi->pc=pc; /* Set the current packet command */
577 pc->actually_transferred=0; /* We haven't transferred any data yet */
578 pc->current_position=pc->buffer;
579 bcount.all = min(pc->request_transfer, 63 * 1024); /* Request to transfer the entire buffer at once */
580
581 feature.all = 0;
582 if (drive->using_dma && !idescsi_map_sg(drive, pc)) {
583 hwif->sg_mapped = 1;
584 feature.b.dma = !hwif->dma_setup(drive);
585 hwif->sg_mapped = 0;
586 }
587
588 SELECT_DRIVE(drive);
589 if (IDE_CONTROL_REG)
590 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
591
592 HWIF(drive)->OUTB(feature.all, IDE_FEATURE_REG);
593 HWIF(drive)->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
594 HWIF(drive)->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
595
596 if (feature.b.dma)
597 set_bit(PC_DMA_OK, &pc->flags);
598
599 if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
125e1874 600 BUG_ON(HWGROUP(drive)->handler != NULL);
1da177e4
LT
601 ide_set_handler(drive, &idescsi_transfer_pc,
602 get_timeout(pc), idescsi_expiry);
603 /* Issue the packet command */
604 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
605 return ide_started;
606 } else {
607 /* Issue the packet command */
608 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
609 return idescsi_transfer_pc(drive);
610 }
611}
612
613/*
614 * idescsi_do_request is our request handling function.
615 */
616static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
617{
618#if IDESCSI_DEBUG_LOG
cdd60262 619 printk (KERN_INFO "dev: %s, cmd: %x, errors: %d\n", rq->rq_disk->disk_name,rq->cmd[0],rq->errors);
1da177e4
LT
620 printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
621#endif /* IDESCSI_DEBUG_LOG */
622
4aff5e23 623 if (blk_sense_request(rq) || blk_special_request(rq)) {
1da177e4
LT
624 return idescsi_issue_pc (drive, (idescsi_pc_t *) rq->special);
625 }
626 blk_dump_rq_flags(rq, "ide-scsi: unsup command");
627 idescsi_end_request (drive, 0, 0);
628 return ide_stopped;
629}
630
7662d046 631#ifdef CONFIG_IDE_PROC_FS
1da177e4
LT
632static void idescsi_add_settings(ide_drive_t *drive)
633{
634 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
635
636/*
1497943e 637 * drive setting name read/write data type min max mul_factor div_factor data pointer set function
1da177e4 638 */
1497943e
BZ
639 ide_add_setting(drive, "bios_cyl", SETTING_RW, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
640 ide_add_setting(drive, "bios_head", SETTING_RW, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
641 ide_add_setting(drive, "bios_sect", SETTING_RW, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
642 ide_add_setting(drive, "transform", SETTING_RW, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
643 ide_add_setting(drive, "log", SETTING_RW, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL);
1da177e4 644}
7662d046
BZ
645#else
646static inline void idescsi_add_settings(ide_drive_t *drive) { ; }
647#endif
1da177e4
LT
648
649/*
650 * Driver initialization.
651 */
652static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi)
653{
1da177e4
LT
654 if (drive->id && (drive->id->config & 0x0060) == 0x20)
655 set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags);
1da177e4
LT
656 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
657#if IDESCSI_DEBUG_LOG
658 set_bit(IDESCSI_LOG_CMD, &scsi->log);
659#endif /* IDESCSI_DEBUG_LOG */
660 idescsi_add_settings(drive);
1da177e4
LT
661}
662
b62735d9 663static void ide_scsi_remove(ide_drive_t *drive)
1da177e4
LT
664{
665 struct Scsi_Host *scsihost = drive->driver_data;
666 struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost);
667 struct gendisk *g = scsi->disk;
668
6fdea8db 669 scsi_remove_host(scsihost);
7662d046 670 ide_proc_unregister_driver(drive, scsi->driver);
1da177e4
LT
671
672 ide_unregister_region(g);
673
674 drive->driver_data = NULL;
675 g->private_data = NULL;
676 put_disk(g);
677
1da177e4 678 ide_scsi_put(scsi);
1da177e4
LT
679}
680
b62735d9 681static int ide_scsi_probe(ide_drive_t *);
1da177e4 682
ecfd80e4 683#ifdef CONFIG_IDE_PROC_FS
1da177e4
LT
684static ide_proc_entry_t idescsi_proc[] = {
685 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
686 { NULL, 0, NULL, NULL }
687};
1da177e4
LT
688#endif
689
1da177e4 690static ide_driver_t idescsi_driver = {
8604affd 691 .gen_driver = {
4ef3b8f4 692 .owner = THIS_MODULE,
8604affd
BZ
693 .name = "ide-scsi",
694 .bus = &ide_bus_type,
8604affd 695 },
b62735d9
MP
696 .probe = ide_scsi_probe,
697 .remove = ide_scsi_remove,
1da177e4
LT
698 .version = IDESCSI_VERSION,
699 .media = ide_scsi,
1da177e4 700 .supports_dsc_overlap = 0,
1da177e4
LT
701 .do_request = idescsi_do_request,
702 .end_request = idescsi_end_request,
703 .error = idescsi_atapi_error,
704 .abort = idescsi_atapi_abort,
7662d046
BZ
705#ifdef CONFIG_IDE_PROC_FS
706 .proc = idescsi_proc,
707#endif
1da177e4
LT
708};
709
710static int idescsi_ide_open(struct inode *inode, struct file *filp)
711{
712 struct gendisk *disk = inode->i_bdev->bd_disk;
713 struct ide_scsi_obj *scsi;
1da177e4
LT
714
715 if (!(scsi = ide_scsi_get(disk)))
716 return -ENXIO;
717
1da177e4
LT
718 return 0;
719}
720
721static int idescsi_ide_release(struct inode *inode, struct file *filp)
722{
723 struct gendisk *disk = inode->i_bdev->bd_disk;
724 struct ide_scsi_obj *scsi = ide_scsi_g(disk);
1da177e4
LT
725
726 ide_scsi_put(scsi);
727
728 return 0;
729}
730
731static int idescsi_ide_ioctl(struct inode *inode, struct file *file,
732 unsigned int cmd, unsigned long arg)
733{
734 struct block_device *bdev = inode->i_bdev;
735 struct ide_scsi_obj *scsi = ide_scsi_g(bdev->bd_disk);
736 return generic_ide_ioctl(scsi->drive, file, bdev, cmd, arg);
737}
738
739static struct block_device_operations idescsi_ops = {
740 .owner = THIS_MODULE,
741 .open = idescsi_ide_open,
742 .release = idescsi_ide_release,
743 .ioctl = idescsi_ide_ioctl,
744};
745
1da177e4
LT
746static int idescsi_slave_configure(struct scsi_device * sdp)
747{
748 /* Configure detected device */
427d0bd4
BH
749 sdp->use_10_for_rw = 1;
750 sdp->use_10_for_ms = 1;
1da177e4
LT
751 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun);
752 return 0;
753}
754
755static const char *idescsi_info (struct Scsi_Host *host)
756{
757 return "SCSI host adapter emulation for IDE ATAPI devices";
758}
759
760static int idescsi_ioctl (struct scsi_device *dev, int cmd, void __user *arg)
761{
762 idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host);
763
764 if (cmd == SG_SET_TRANSFORM) {
765 if (arg)
766 set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
767 else
768 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
769 return 0;
770 } else if (cmd == SG_GET_TRANSFORM)
771 return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int __user *) arg);
772 return -EINVAL;
773}
774
1da177e4
LT
775static int idescsi_queue (struct scsi_cmnd *cmd,
776 void (*done)(struct scsi_cmnd *))
777{
778 struct Scsi_Host *host = cmd->device->host;
779 idescsi_scsi_t *scsi = scsihost_to_idescsi(host);
780 ide_drive_t *drive = scsi->drive;
781 struct request *rq = NULL;
782 idescsi_pc_t *pc = NULL;
783
784 if (!drive) {
017560fc 785 scmd_printk (KERN_ERR, cmd, "drive not present\n");
1da177e4
LT
786 goto abort;
787 }
788 scsi = drive_to_idescsi(drive);
789 pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
790 rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
791 if (rq == NULL || pc == NULL) {
792 printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
793 goto abort;
794 }
795
796 memset (pc->c, 0, 12);
797 pc->flags = 0;
798 pc->rq = rq;
799 memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
427d0bd4
BH
800 pc->buffer = NULL;
801 pc->sg = scsi_sglist(cmd);
c79d88b7 802 pc->sg_cnt = scsi_sg_count(cmd);
1da177e4 803 pc->b_count = 0;
427d0bd4 804 pc->request_transfer = pc->buffer_size = scsi_bufflen(cmd);
1da177e4
LT
805 pc->scsi_cmd = cmd;
806 pc->done = done;
807 pc->timeout = jiffies + cmd->timeout_per_command;
808
1da177e4
LT
809 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
810 printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
6413f086
DC
811 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_NONE, 16, 1,
812 cmd->cmnd, cmd->cmd_len, 0);
1da177e4
LT
813 if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
814 printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
6413f086
DC
815 print_hex_dump(KERN_CONT, "", DUMP_PREFIX_NONE, 16, 1,
816 pc->c, 12, 0);
1da177e4
LT
817 }
818 }
819
820 ide_init_drive_cmd (rq);
821 rq->special = (char *) pc;
4aff5e23 822 rq->cmd_type = REQ_TYPE_SPECIAL;
1da177e4
LT
823 spin_unlock_irq(host->host_lock);
824 rq->rq_disk = scsi->disk;
825 (void) ide_do_drive_cmd (drive, rq, ide_end);
826 spin_lock_irq(host->host_lock);
827 return 0;
828abort:
c9475cb0
JJ
829 kfree (pc);
830 kfree (rq);
1da177e4
LT
831 cmd->result = DID_ERROR << 16;
832 done(cmd);
833 return 0;
834}
835
836static int idescsi_eh_abort (struct scsi_cmnd *cmd)
837{
838 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
839 ide_drive_t *drive = scsi->drive;
840 int busy;
841 int ret = FAILED;
842
843 /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
844
845 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
846 printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number);
847
848 if (!drive) {
849 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n");
850 WARN_ON(1);
851 goto no_drive;
852 }
853
854 /* First give it some more time, how much is "right" is hard to say :-( */
855
856 busy = ide_wait_not_busy(HWIF(drive), 100); /* FIXME - uses mdelay which causes latency? */
857 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
858 printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");
859
860 spin_lock_irq(&ide_lock);
861
862 /* If there is no pc running we're done (our interrupt took care of it) */
863 if (!scsi->pc) {
864 ret = SUCCESS;
865 goto ide_unlock;
866 }
867
868 /* It's somewhere in flight. Does ide subsystem agree? */
869 if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
870 elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) {
871 /*
872 * FIXME - not sure this condition can ever occur
873 */
874 printk (KERN_ERR "ide-scsi: cmd aborted!\n");
875
4aff5e23 876 if (blk_sense_request(scsi->pc->rq))
1da177e4
LT
877 kfree(scsi->pc->buffer);
878 kfree(scsi->pc->rq);
879 kfree(scsi->pc);
880 scsi->pc = NULL;
881
882 ret = SUCCESS;
883 }
884
885ide_unlock:
886 spin_unlock_irq(&ide_lock);
887no_drive:
888 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
889 printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed");
890
891 return ret;
892}
893
894static int idescsi_eh_reset (struct scsi_cmnd *cmd)
895{
896 struct request *req;
897 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
898 ide_drive_t *drive = scsi->drive;
899 int ready = 0;
900 int ret = SUCCESS;
901
902 /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
903
904 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
905 printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number);
906
907 if (!drive) {
908 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n");
909 WARN_ON(1);
910 return FAILED;
911 }
912
df0ae249
JG
913 spin_lock_irq(cmd->device->host->host_lock);
914 spin_lock(&ide_lock);
1da177e4
LT
915
916 if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) {
917 printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
918 spin_unlock(&ide_lock);
df0ae249 919 spin_unlock_irq(cmd->device->host->host_lock);
1da177e4
LT
920 return FAILED;
921 }
922
923 /* kill current request */
924 blkdev_dequeue_request(req);
8ffdc655 925 end_that_request_last(req, 0);
4aff5e23 926 if (blk_sense_request(req))
1da177e4
LT
927 kfree(scsi->pc->buffer);
928 kfree(scsi->pc);
929 scsi->pc = NULL;
930 kfree(req);
931
932 /* now nuke the drive queue */
933 while ((req = elv_next_request(drive->queue))) {
934 blkdev_dequeue_request(req);
8ffdc655 935 end_that_request_last(req, 0);
1da177e4
LT
936 }
937
938 HWGROUP(drive)->rq = NULL;
939 HWGROUP(drive)->handler = NULL;
940 HWGROUP(drive)->busy = 1; /* will set this to zero when ide reset finished */
df0ae249 941 spin_unlock(&ide_lock);
1da177e4
LT
942
943 ide_do_reset(drive);
944
945 /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
946
947 do {
1da177e4 948 spin_unlock_irq(cmd->device->host->host_lock);
df0ae249 949 msleep(50);
1da177e4
LT
950 spin_lock_irq(cmd->device->host->host_lock);
951 } while ( HWGROUP(drive)->handler );
952
953 ready = drive_is_ready(drive);
954 HWGROUP(drive)->busy--;
955 if (!ready) {
956 printk (KERN_ERR "ide-scsi: reset failed!\n");
957 ret = FAILED;
958 }
959
df0ae249 960 spin_unlock_irq(cmd->device->host->host_lock);
1da177e4
LT
961 return ret;
962}
963
964static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev,
965 sector_t capacity, int *parm)
966{
967 idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host);
968 ide_drive_t *drive = idescsi->drive;
969
970 if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
971 parm[0] = drive->bios_head;
972 parm[1] = drive->bios_sect;
973 parm[2] = drive->bios_cyl;
974 }
975 return 0;
976}
977
978static struct scsi_host_template idescsi_template = {
979 .module = THIS_MODULE,
980 .name = "idescsi",
981 .info = idescsi_info,
982 .slave_configure = idescsi_slave_configure,
983 .ioctl = idescsi_ioctl,
984 .queuecommand = idescsi_queue,
985 .eh_abort_handler = idescsi_eh_abort,
986 .eh_host_reset_handler = idescsi_eh_reset,
987 .bios_param = idescsi_bios,
988 .can_queue = 40,
989 .this_id = -1,
990 .sg_tablesize = 256,
991 .cmd_per_lun = 5,
992 .max_sectors = 128,
993 .use_clustering = DISABLE_CLUSTERING,
994 .emulated = 1,
995 .proc_name = "ide-scsi",
996};
997
b62735d9 998static int ide_scsi_probe(ide_drive_t *drive)
1da177e4
LT
999{
1000 idescsi_scsi_t *idescsi;
1001 struct Scsi_Host *host;
1002 struct gendisk *g;
1003 static int warned;
1004 int err = -ENOMEM;
1005
1006 if (!warned && drive->media == ide_cdrom) {
1007 printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
1008 warned = 1;
1009 }
1010
aaeab80b
AC
1011 if (idescsi_nocd && drive->media == ide_cdrom)
1012 return -ENODEV;
1013
1da177e4
LT
1014 if (!strstr("ide-scsi", drive->driver_req) ||
1015 !drive->present ||
1016 drive->media == ide_disk ||
1017 !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
8604affd 1018 return -ENODEV;
1da177e4
LT
1019
1020 g = alloc_disk(1 << PARTN_BITS);
1021 if (!g)
1022 goto out_host_put;
1023
1024 ide_init_disk(g, drive);
1025
1026 host->max_id = 1;
1027
1028#if IDESCSI_DEBUG_LOG
1029 if (drive->id->last_lun)
1030 printk(KERN_NOTICE "%s: id->last_lun=%u\n", drive->name, drive->id->last_lun);
1031#endif
1032 if ((drive->id->last_lun & 0x7) != 7)
1033 host->max_lun = (drive->id->last_lun & 0x7) + 1;
1034 else
1035 host->max_lun = 1;
1036
1037 drive->driver_data = host;
1038 idescsi = scsihost_to_idescsi(host);
1039 idescsi->drive = drive;
1040 idescsi->driver = &idescsi_driver;
1041 idescsi->host = host;
1042 idescsi->disk = g;
1043 g->private_data = &idescsi->driver;
7662d046 1044 ide_proc_register_driver(drive, &idescsi_driver);
8604affd
BZ
1045 err = 0;
1046 idescsi_setup(drive, idescsi);
1047 g->fops = &idescsi_ops;
1048 ide_register_region(g);
1049 err = scsi_add_host(host, &drive->gendev);
1da177e4 1050 if (!err) {
8604affd
BZ
1051 scsi_scan_host(host);
1052 return 0;
1da177e4 1053 }
8604affd
BZ
1054 /* fall through on error */
1055 ide_unregister_region(g);
7662d046 1056 ide_proc_unregister_driver(drive, &idescsi_driver);
1da177e4
LT
1057
1058 put_disk(g);
1059out_host_put:
1060 scsi_host_put(host);
1061 return err;
1062}
1063
1064static int __init init_idescsi_module(void)
1065{
8604affd 1066 return driver_register(&idescsi_driver.gen_driver);
1da177e4
LT
1067}
1068
1069static void __exit exit_idescsi_module(void)
1070{
8604affd 1071 driver_unregister(&idescsi_driver.gen_driver);
1da177e4
LT
1072}
1073
aaeab80b
AC
1074module_param(idescsi_nocd, int, 0600);
1075MODULE_PARM_DESC(idescsi_nocd, "Disable handling of CD-ROMs so they may be driven by ide-cd");
1da177e4
LT
1076module_init(init_idescsi_module);
1077module_exit(exit_idescsi_module);
1078MODULE_LICENSE("GPL");
This page took 0.376503 seconds and 5 git commands to generate.