blob: c888af4a45629cf8fa138dabcc99646f8199723d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
37#include <linux/config.h>
38#include <linux/types.h>
39#include <linux/string.h>
40#include <linux/kernel.h>
41#include <linux/mm.h>
42#include <linux/ioport.h>
43#include <linux/blkdev.h>
44#include <linux/errno.h>
45#include <linux/hdreg.h>
46#include <linux/slab.h>
47#include <linux/ide.h>
48#include <linux/scatterlist.h>
Jeff Garzik df0ae242005-05-28 07:57:14 -040049#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51#include <asm/io.h>
52#include <asm/bitops.h>
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 */
73 int b_count; /* Bytes transferred from current entry */
74 struct scsi_cmnd *scsi_cmd; /* SCSI command */
75 void (*done)(struct scsi_cmnd *); /* Scsi completion routine */
76 unsigned long flags; /* Status/Action flags */
77 unsigned long timeout; /* Command timeout */
78} idescsi_pc_t;
79
80/*
81 * Packet command status bits.
82 */
83#define PC_DMA_IN_PROGRESS 0 /* 1 while DMA in progress */
84#define PC_WRITING 1 /* Data direction */
85#define PC_TRANSFORM 2 /* transform SCSI commands */
86#define PC_TIMEDOUT 3 /* command timed out */
87#define PC_DMA_OK 4 /* Use DMA */
88
89/*
90 * SCSI command transformation layer
91 */
92#define IDESCSI_TRANSFORM 0 /* Enable/Disable transformation */
93#define IDESCSI_SG_TRANSFORM 1 /* /dev/sg transformation */
94
95/*
96 * Log flags
97 */
98#define IDESCSI_LOG_CMD 0 /* Log SCSI commands */
99
100typedef struct ide_scsi_obj {
101 ide_drive_t *drive;
102 ide_driver_t *driver;
103 struct gendisk *disk;
104 struct Scsi_Host *host;
105
106 idescsi_pc_t *pc; /* Current packet command */
107 unsigned long flags; /* Status/Action flags */
108 unsigned long transform; /* SCSI cmd translation layer */
109 unsigned long log; /* log flags */
110} idescsi_scsi_t;
111
112static DECLARE_MUTEX(idescsi_ref_sem);
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
121 down(&idescsi_ref_sem);
122 scsi = ide_scsi_g(disk);
123 if (scsi)
124 scsi_host_get(scsi->host);
125 up(&idescsi_ref_sem);
126 return scsi;
127}
128
129static void ide_scsi_put(struct ide_scsi_obj *scsi)
130{
131 down(&idescsi_ref_sem);
132 scsi_host_put(scsi->host);
133 up(&idescsi_ref_sem);
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) {
177 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
178 printk (KERN_ERR "ide-scsi: scatter gather table too small, discarding data\n");
179 idescsi_discard_data (drive, bcount);
180 return;
181 }
182 count = min(pc->sg->length - pc->b_count, bcount);
Andrew Mortona717f772005-10-31 14:08:53 -0800183 if (PageHighMem(pc->sg->page)) {
184 unsigned long flags;
185
186 local_irq_save(flags);
187 buf = kmap_atomic(pc->sg->page, KM_IRQ0) +
188 pc->sg->offset;
189 drive->hwif->atapi_input_bytes(drive,
190 buf + pc->b_count, count);
191 kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
192 local_irq_restore(flags);
193 } else {
194 buf = page_address(pc->sg->page) + pc->sg->offset;
195 drive->hwif->atapi_input_bytes(drive,
196 buf + pc->b_count, count);
197 }
198 bcount -= count; pc->b_count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (pc->b_count == pc->sg->length) {
200 pc->sg++;
201 pc->b_count = 0;
202 }
203 }
204}
205
206static void idescsi_output_buffers (ide_drive_t *drive, idescsi_pc_t *pc, unsigned int bcount)
207{
208 int count;
209 char *buf;
210
211 while (bcount) {
212 if (pc->sg - (struct scatterlist *) pc->scsi_cmd->request_buffer > pc->scsi_cmd->use_sg) {
213 printk (KERN_ERR "ide-scsi: scatter gather table too small, padding with zeros\n");
214 idescsi_output_zeros (drive, bcount);
215 return;
216 }
217 count = min(pc->sg->length - pc->b_count, bcount);
Andrew Mortona717f772005-10-31 14:08:53 -0800218 if (PageHighMem(pc->sg->page)) {
219 unsigned long flags;
220
221 local_irq_save(flags);
222 buf = kmap_atomic(pc->sg->page, KM_IRQ0) +
223 pc->sg->offset;
224 drive->hwif->atapi_output_bytes(drive,
225 buf + pc->b_count, count);
226 kunmap_atomic(buf - pc->sg->offset, KM_IRQ0);
227 local_irq_restore(flags);
228 } else {
229 buf = page_address(pc->sg->page) + pc->sg->offset;
230 drive->hwif->atapi_output_bytes(drive,
231 buf + pc->b_count, count);
232 }
233 bcount -= count; pc->b_count += count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 if (pc->b_count == pc->sg->length) {
235 pc->sg++;
236 pc->b_count = 0;
237 }
238 }
239}
240
241/*
242 * Most of the SCSI commands are supported directly by ATAPI devices.
243 * idescsi_transform_pc handles the few exceptions.
244 */
245static inline void idescsi_transform_pc1 (ide_drive_t *drive, idescsi_pc_t *pc)
246{
247 u8 *c = pc->c, *scsi_buf = pc->buffer, *sc = pc->scsi_cmd->cmnd;
248 char *atapi_buf;
249
250 if (!test_bit(PC_TRANSFORM, &pc->flags))
251 return;
252 if (drive->media == ide_cdrom || drive->media == ide_optical) {
253 if (c[0] == READ_6 || c[0] == WRITE_6) {
254 c[8] = c[4]; c[5] = c[3]; c[4] = c[2];
255 c[3] = c[1] & 0x1f; c[2] = 0; c[1] &= 0xe0;
256 c[0] += (READ_10 - READ_6);
257 }
258 if (c[0] == MODE_SENSE || c[0] == MODE_SELECT) {
259 unsigned short new_len;
260 if (!scsi_buf)
261 return;
262 if ((atapi_buf = kmalloc(pc->buffer_size + 4, GFP_ATOMIC)) == NULL)
263 return;
264 memset(atapi_buf, 0, pc->buffer_size + 4);
265 memset (c, 0, 12);
266 c[0] = sc[0] | 0x40;
267 c[1] = sc[1];
268 c[2] = sc[2];
269 new_len = sc[4] + 4;
270 c[8] = new_len;
271 c[7] = new_len >> 8;
272 c[9] = sc[5];
273 if (c[0] == MODE_SELECT_10) {
274 atapi_buf[1] = scsi_buf[0]; /* Mode data length */
275 atapi_buf[2] = scsi_buf[1]; /* Medium type */
276 atapi_buf[3] = scsi_buf[2]; /* Device specific parameter */
277 atapi_buf[7] = scsi_buf[3]; /* Block descriptor length */
278 memcpy(atapi_buf + 8, scsi_buf + 4, pc->buffer_size - 4);
279 }
280 pc->buffer = atapi_buf;
281 pc->request_transfer += 4;
282 pc->buffer_size += 4;
283 }
284 }
285}
286
287static inline void idescsi_transform_pc2 (ide_drive_t *drive, idescsi_pc_t *pc)
288{
289 u8 *atapi_buf = pc->buffer;
290 u8 *sc = pc->scsi_cmd->cmnd;
291 u8 *scsi_buf = pc->scsi_cmd->request_buffer;
292
293 if (!test_bit(PC_TRANSFORM, &pc->flags))
294 return;
295 if (drive->media == ide_cdrom || drive->media == ide_optical) {
296 if (pc->c[0] == MODE_SENSE_10 && sc[0] == MODE_SENSE) {
297 scsi_buf[0] = atapi_buf[1]; /* Mode data length */
298 scsi_buf[1] = atapi_buf[2]; /* Medium type */
299 scsi_buf[2] = atapi_buf[3]; /* Device specific parameter */
300 scsi_buf[3] = atapi_buf[7]; /* Block descriptor length */
301 memcpy(scsi_buf + 4, atapi_buf + 8, pc->request_transfer - 8);
302 }
303 if (pc->c[0] == INQUIRY) {
304 scsi_buf[2] |= 2; /* ansi_revision */
305 scsi_buf[3] = (scsi_buf[3] & 0xf0) | 2; /* response data format */
306 }
307 }
308 if (atapi_buf && atapi_buf != scsi_buf)
309 kfree(atapi_buf);
310}
311
312static void hexdump(u8 *x, int len)
313{
314 int i;
315
316 printk("[ ");
317 for (i = 0; i < len; i++)
318 printk("%x ", x[i]);
319 printk("]\n");
320}
321
322static int idescsi_check_condition(ide_drive_t *drive, struct request *failed_command)
323{
324 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
325 idescsi_pc_t *pc;
326 struct request *rq;
327 u8 *buf;
328
329 /* stuff a sense request in front of our current request */
330 pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
331 rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
332 buf = kmalloc(SCSI_SENSE_BUFFERSIZE, GFP_ATOMIC);
333 if (pc == NULL || rq == NULL || buf == NULL) {
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800334 kfree(buf);
335 kfree(rq);
336 kfree(pc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 return -ENOMEM;
338 }
339 memset (pc, 0, sizeof (idescsi_pc_t));
340 memset (buf, 0, SCSI_SENSE_BUFFERSIZE);
341 ide_init_drive_cmd(rq);
342 rq->special = (char *) pc;
343 pc->rq = rq;
344 pc->buffer = buf;
345 pc->c[0] = REQUEST_SENSE;
346 pc->c[4] = pc->request_transfer = pc->buffer_size = SCSI_SENSE_BUFFERSIZE;
347 rq->flags = REQ_SENSE;
348 pc->timeout = jiffies + WAIT_READY;
349 /* NOTE! Save the failed packet command in "rq->buffer" */
350 rq->buffer = (void *) failed_command->special;
351 pc->scsi_cmd = ((idescsi_pc_t *) failed_command->special)->scsi_cmd;
352 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
353 printk ("ide-scsi: %s: queue cmd = ", drive->name);
354 hexdump(pc->c, 6);
355 }
356 rq->rq_disk = scsi->disk;
357 return ide_do_drive_cmd(drive, rq, ide_preempt);
358}
359
360static int idescsi_end_request(ide_drive_t *, int, int);
361
362static ide_startstop_t
363idescsi_atapi_error(ide_drive_t *drive, struct request *rq, u8 stat, u8 err)
364{
365 if (HWIF(drive)->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT))
366 /* force an abort */
367 HWIF(drive)->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
368
369 rq->errors++;
370
371 idescsi_end_request(drive, 0, 0);
372
373 return ide_stopped;
374}
375
376static ide_startstop_t
377idescsi_atapi_abort(ide_drive_t *drive, struct request *rq)
378{
379#if IDESCSI_DEBUG_LOG
380 printk(KERN_WARNING "idescsi_atapi_abort called for %lu\n",
381 ((idescsi_pc_t *) rq->special)->scsi_cmd->serial_number);
382#endif
383 rq->errors |= ERROR_MAX;
384
385 idescsi_end_request(drive, 0, 0);
386
387 return ide_stopped;
388}
389
390static int idescsi_end_request (ide_drive_t *drive, int uptodate, int nrsecs)
391{
392 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
393 struct request *rq = HWGROUP(drive)->rq;
394 idescsi_pc_t *pc = (idescsi_pc_t *) rq->special;
395 int log = test_bit(IDESCSI_LOG_CMD, &scsi->log);
396 struct Scsi_Host *host;
397 u8 *scsi_buf;
398 unsigned long flags;
399
400 if (!(rq->flags & (REQ_SPECIAL|REQ_SENSE))) {
401 ide_end_request(drive, uptodate, nrsecs);
402 return 0;
403 }
404 ide_end_drive_cmd (drive, 0, 0);
405 if (rq->flags & REQ_SENSE) {
406 idescsi_pc_t *opc = (idescsi_pc_t *) rq->buffer;
407 if (log) {
408 printk ("ide-scsi: %s: wrap up check %lu, rst = ", drive->name, opc->scsi_cmd->serial_number);
409 hexdump(pc->buffer,16);
410 }
411 memcpy((void *) opc->scsi_cmd->sense_buffer, pc->buffer, SCSI_SENSE_BUFFERSIZE);
412 kfree(pc->buffer);
413 kfree(pc);
414 kfree(rq);
415 pc = opc;
416 rq = pc->rq;
417 pc->scsi_cmd->result = (CHECK_CONDITION << 1) |
418 ((test_bit(PC_TIMEDOUT, &pc->flags)?DID_TIME_OUT:DID_OK) << 16);
419 } else if (test_bit(PC_TIMEDOUT, &pc->flags)) {
420 if (log)
421 printk (KERN_WARNING "ide-scsi: %s: timed out for %lu\n",
422 drive->name, pc->scsi_cmd->serial_number);
423 pc->scsi_cmd->result = DID_TIME_OUT << 16;
424 } else if (rq->errors >= ERROR_MAX) {
425 pc->scsi_cmd->result = DID_ERROR << 16;
426 if (log)
427 printk ("ide-scsi: %s: I/O error for %lu\n", drive->name, pc->scsi_cmd->serial_number);
428 } else if (rq->errors) {
429 if (log)
430 printk ("ide-scsi: %s: check condition for %lu\n", drive->name, pc->scsi_cmd->serial_number);
431 if (!idescsi_check_condition(drive, rq))
432 /* we started a request sense, so we'll be back, exit for now */
433 return 0;
434 pc->scsi_cmd->result = (CHECK_CONDITION << 1) | (DID_OK << 16);
435 } else {
436 pc->scsi_cmd->result = DID_OK << 16;
437 idescsi_transform_pc2 (drive, pc);
438 if (log) {
439 printk ("ide-scsi: %s: suc %lu", drive->name, pc->scsi_cmd->serial_number);
440 if (!test_bit(PC_WRITING, &pc->flags) && pc->actually_transferred && pc->actually_transferred <= 1024 && pc->buffer) {
441 printk(", rst = ");
442 scsi_buf = pc->scsi_cmd->request_buffer;
443 hexdump(scsi_buf, min_t(unsigned, 16, pc->scsi_cmd->request_bufflen));
444 } else printk("\n");
445 }
446 }
447 host = pc->scsi_cmd->device->host;
448 spin_lock_irqsave(host->host_lock, flags);
449 pc->done(pc->scsi_cmd);
450 spin_unlock_irqrestore(host->host_lock, flags);
451 kfree(pc);
452 kfree(rq);
453 scsi->pc = NULL;
454 return 0;
455}
456
457static inline unsigned long get_timeout(idescsi_pc_t *pc)
458{
459 return max_t(unsigned long, WAIT_CMD, pc->timeout - jiffies);
460}
461
462static int idescsi_expiry(ide_drive_t *drive)
463{
464 idescsi_scsi_t *scsi = drive->driver_data;
465 idescsi_pc_t *pc = scsi->pc;
466
467#if IDESCSI_DEBUG_LOG
468 printk(KERN_WARNING "idescsi_expiry called for %lu at %lu\n", pc->scsi_cmd->serial_number, jiffies);
469#endif
470 set_bit(PC_TIMEDOUT, &pc->flags);
471
472 return 0; /* we do not want the ide subsystem to retry */
473}
474
475/*
476 * Our interrupt handler.
477 */
478static ide_startstop_t idescsi_pc_intr (ide_drive_t *drive)
479{
480 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
481 idescsi_pc_t *pc=scsi->pc;
482 struct request *rq = pc->rq;
483 atapi_bcount_t bcount;
484 atapi_status_t status;
485 atapi_ireason_t ireason;
486 atapi_feature_t feature;
487
488 unsigned int temp;
489
490#if IDESCSI_DEBUG_LOG
491 printk (KERN_INFO "ide-scsi: Reached idescsi_pc_intr interrupt handler\n");
492#endif /* IDESCSI_DEBUG_LOG */
493
494 if (test_bit(PC_TIMEDOUT, &pc->flags)){
495#if IDESCSI_DEBUG_LOG
496 printk(KERN_WARNING "idescsi_pc_intr: got timed out packet %lu at %lu\n",
497 pc->scsi_cmd->serial_number, jiffies);
498#endif
499 /* end this request now - scsi should retry it*/
500 idescsi_end_request (drive, 1, 0);
501 return ide_stopped;
502 }
503 if (test_and_clear_bit (PC_DMA_IN_PROGRESS, &pc->flags)) {
504#if IDESCSI_DEBUG_LOG
505 printk ("ide-scsi: %s: DMA complete\n", drive->name);
506#endif /* IDESCSI_DEBUG_LOG */
507 pc->actually_transferred=pc->request_transfer;
508 (void) HWIF(drive)->ide_dma_end(drive);
509 }
510
511 feature.all = 0;
512 /* Clear the interrupt */
513 status.all = HWIF(drive)->INB(IDE_STATUS_REG);
514
515 if (!status.b.drq) {
516 /* No more interrupts */
517 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
518 printk (KERN_INFO "Packet command completed, %d bytes transferred\n", pc->actually_transferred);
519 local_irq_enable();
520 if (status.b.check)
521 rq->errors++;
522 idescsi_end_request (drive, 1, 0);
523 return ide_stopped;
524 }
525 bcount.b.low = HWIF(drive)->INB(IDE_BCOUNTL_REG);
526 bcount.b.high = HWIF(drive)->INB(IDE_BCOUNTH_REG);
527 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
528
529 if (ireason.b.cod) {
530 printk(KERN_ERR "ide-scsi: CoD != 0 in idescsi_pc_intr\n");
531 return ide_do_reset (drive);
532 }
533 if (ireason.b.io) {
534 temp = pc->actually_transferred + bcount.all;
535 if (temp > pc->request_transfer) {
536 if (temp > pc->buffer_size) {
537 printk(KERN_ERR "ide-scsi: The scsi wants to "
538 "send us more data than expected "
539 "- discarding data\n");
540 temp = pc->buffer_size - pc->actually_transferred;
541 if (temp) {
542 clear_bit(PC_WRITING, &pc->flags);
543 if (pc->sg)
544 idescsi_input_buffers(drive, pc, temp);
545 else
546 drive->hwif->atapi_input_bytes(drive, pc->current_position, temp);
547 printk(KERN_ERR "ide-scsi: transferred %d of %d bytes\n", temp, bcount.all);
548 }
549 pc->actually_transferred += temp;
550 pc->current_position += temp;
551 idescsi_discard_data(drive, bcount.all - temp);
552 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
553 return ide_started;
554 }
555#if IDESCSI_DEBUG_LOG
556 printk (KERN_NOTICE "ide-scsi: The scsi wants to send us more data than expected - allowing transfer\n");
557#endif /* IDESCSI_DEBUG_LOG */
558 }
559 }
560 if (ireason.b.io) {
561 clear_bit(PC_WRITING, &pc->flags);
562 if (pc->sg)
563 idescsi_input_buffers(drive, pc, bcount.all);
564 else
565 HWIF(drive)->atapi_input_bytes(drive, pc->current_position, bcount.all);
566 } else {
567 set_bit(PC_WRITING, &pc->flags);
568 if (pc->sg)
569 idescsi_output_buffers (drive, pc, bcount.all);
570 else
571 HWIF(drive)->atapi_output_bytes(drive, pc->current_position, bcount.all);
572 }
573 /* Update the current position */
574 pc->actually_transferred += bcount.all;
575 pc->current_position += bcount.all;
576
577 /* And set the interrupt handler again */
578 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
579 return ide_started;
580}
581
582static ide_startstop_t idescsi_transfer_pc(ide_drive_t *drive)
583{
584 ide_hwif_t *hwif = drive->hwif;
585 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
586 idescsi_pc_t *pc = scsi->pc;
587 atapi_ireason_t ireason;
588 ide_startstop_t startstop;
589
590 if (ide_wait_stat(&startstop,drive,DRQ_STAT,BUSY_STAT,WAIT_READY)) {
591 printk(KERN_ERR "ide-scsi: Strange, packet command "
592 "initiated yet DRQ isn't asserted\n");
593 return startstop;
594 }
595 ireason.all = HWIF(drive)->INB(IDE_IREASON_REG);
596 if (!ireason.b.cod || ireason.b.io) {
597 printk(KERN_ERR "ide-scsi: (IO,CoD) != (0,1) while "
598 "issuing a packet command\n");
599 return ide_do_reset (drive);
600 }
601 if (HWGROUP(drive)->handler != NULL)
602 BUG();
603 /* Set the interrupt routine */
604 ide_set_handler(drive, &idescsi_pc_intr, get_timeout(pc), idescsi_expiry);
605 /* Send the actual packet */
606 drive->hwif->atapi_output_bytes(drive, scsi->pc->c, 12);
607 if (test_bit (PC_DMA_OK, &pc->flags)) {
608 set_bit (PC_DMA_IN_PROGRESS, &pc->flags);
609 hwif->dma_start(drive);
610 }
611 return ide_started;
612}
613
614static inline int idescsi_set_direction(idescsi_pc_t *pc)
615{
616 switch (pc->c[0]) {
617 case READ_6: case READ_10: case READ_12:
618 clear_bit(PC_WRITING, &pc->flags);
619 return 0;
620 case WRITE_6: case WRITE_10: case WRITE_12:
621 set_bit(PC_WRITING, &pc->flags);
622 return 0;
623 default:
624 return 1;
625 }
626}
627
628static int idescsi_map_sg(ide_drive_t *drive, idescsi_pc_t *pc)
629{
630 ide_hwif_t *hwif = drive->hwif;
631 struct scatterlist *sg, *scsi_sg;
632 int segments;
633
634 if (!pc->request_transfer || pc->request_transfer % 1024)
635 return 1;
636
637 if (idescsi_set_direction(pc))
638 return 1;
639
640 sg = hwif->sg_table;
641 scsi_sg = pc->scsi_cmd->request_buffer;
642 segments = pc->scsi_cmd->use_sg;
643
644 if (segments > hwif->sg_max_nents)
645 return 1;
646
647 if (!segments) {
648 hwif->sg_nents = 1;
649 sg_init_one(sg, pc->scsi_cmd->request_buffer, pc->request_transfer);
650 } else {
651 hwif->sg_nents = segments;
652 memcpy(sg, scsi_sg, sizeof(*sg) * segments);
653 }
654
655 return 0;
656}
657
658/*
659 * Issue a packet command
660 */
661static ide_startstop_t idescsi_issue_pc (ide_drive_t *drive, idescsi_pc_t *pc)
662{
663 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
664 ide_hwif_t *hwif = drive->hwif;
665 atapi_feature_t feature;
666 atapi_bcount_t bcount;
667
668 scsi->pc=pc; /* Set the current packet command */
669 pc->actually_transferred=0; /* We haven't transferred any data yet */
670 pc->current_position=pc->buffer;
671 bcount.all = min(pc->request_transfer, 63 * 1024); /* Request to transfer the entire buffer at once */
672
673 feature.all = 0;
674 if (drive->using_dma && !idescsi_map_sg(drive, pc)) {
675 hwif->sg_mapped = 1;
676 feature.b.dma = !hwif->dma_setup(drive);
677 hwif->sg_mapped = 0;
678 }
679
680 SELECT_DRIVE(drive);
681 if (IDE_CONTROL_REG)
682 HWIF(drive)->OUTB(drive->ctl, IDE_CONTROL_REG);
683
684 HWIF(drive)->OUTB(feature.all, IDE_FEATURE_REG);
685 HWIF(drive)->OUTB(bcount.b.high, IDE_BCOUNTH_REG);
686 HWIF(drive)->OUTB(bcount.b.low, IDE_BCOUNTL_REG);
687
688 if (feature.b.dma)
689 set_bit(PC_DMA_OK, &pc->flags);
690
691 if (test_bit(IDESCSI_DRQ_INTERRUPT, &scsi->flags)) {
692 if (HWGROUP(drive)->handler != NULL)
693 BUG();
694 ide_set_handler(drive, &idescsi_transfer_pc,
695 get_timeout(pc), idescsi_expiry);
696 /* Issue the packet command */
697 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
698 return ide_started;
699 } else {
700 /* Issue the packet command */
701 HWIF(drive)->OUTB(WIN_PACKETCMD, IDE_COMMAND_REG);
702 return idescsi_transfer_pc(drive);
703 }
704}
705
706/*
707 * idescsi_do_request is our request handling function.
708 */
709static ide_startstop_t idescsi_do_request (ide_drive_t *drive, struct request *rq, sector_t block)
710{
711#if IDESCSI_DEBUG_LOG
712 printk (KERN_INFO "rq_status: %d, dev: %s, cmd: %x, errors: %d\n",rq->rq_status, rq->rq_disk->disk_name,rq->cmd[0],rq->errors);
713 printk (KERN_INFO "sector: %ld, nr_sectors: %ld, current_nr_sectors: %d\n",rq->sector,rq->nr_sectors,rq->current_nr_sectors);
714#endif /* IDESCSI_DEBUG_LOG */
715
716 if (rq->flags & (REQ_SPECIAL|REQ_SENSE)) {
717 return idescsi_issue_pc (drive, (idescsi_pc_t *) rq->special);
718 }
719 blk_dump_rq_flags(rq, "ide-scsi: unsup command");
720 idescsi_end_request (drive, 0, 0);
721 return ide_stopped;
722}
723
724static void idescsi_add_settings(ide_drive_t *drive)
725{
726 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
727
728/*
729 * drive setting name read/write ioctl ioctl data type min max mul_factor div_factor data pointer set function
730 */
731 ide_add_setting(drive, "bios_cyl", SETTING_RW, -1, -1, TYPE_INT, 0, 1023, 1, 1, &drive->bios_cyl, NULL);
732 ide_add_setting(drive, "bios_head", SETTING_RW, -1, -1, TYPE_BYTE, 0, 255, 1, 1, &drive->bios_head, NULL);
733 ide_add_setting(drive, "bios_sect", SETTING_RW, -1, -1, TYPE_BYTE, 0, 63, 1, 1, &drive->bios_sect, NULL);
734 ide_add_setting(drive, "transform", SETTING_RW, -1, -1, TYPE_INT, 0, 3, 1, 1, &scsi->transform, NULL);
735 ide_add_setting(drive, "log", SETTING_RW, -1, -1, TYPE_INT, 0, 1, 1, 1, &scsi->log, NULL);
736}
737
738/*
739 * Driver initialization.
740 */
741static void idescsi_setup (ide_drive_t *drive, idescsi_scsi_t *scsi)
742{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 if (drive->id && (drive->id->config & 0x0060) == 0x20)
744 set_bit (IDESCSI_DRQ_INTERRUPT, &scsi->flags);
745 set_bit(IDESCSI_TRANSFORM, &scsi->transform);
746 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
747#if IDESCSI_DEBUG_LOG
748 set_bit(IDESCSI_LOG_CMD, &scsi->log);
749#endif /* IDESCSI_DEBUG_LOG */
750 idescsi_add_settings(drive);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751}
752
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200753static int ide_scsi_remove(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200755 ide_drive_t *drive = to_ide_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 struct Scsi_Host *scsihost = drive->driver_data;
757 struct ide_scsi_obj *scsi = scsihost_to_idescsi(scsihost);
758 struct gendisk *g = scsi->disk;
759
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200760 ide_unregister_subdriver(drive, scsi->driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 ide_unregister_region(g);
763
764 drive->driver_data = NULL;
765 g->private_data = NULL;
766 put_disk(g);
767
768 scsi_remove_host(scsihost);
769 ide_scsi_put(scsi);
770
771 return 0;
772}
773
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200774static int ide_scsi_probe(struct device *);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
776#ifdef CONFIG_PROC_FS
777static ide_proc_entry_t idescsi_proc[] = {
778 { "capacity", S_IFREG|S_IRUGO, proc_ide_read_capacity, NULL },
779 { NULL, 0, NULL, NULL }
780};
781#else
782# define idescsi_proc NULL
783#endif
784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785static ide_driver_t idescsi_driver = {
786 .owner = THIS_MODULE,
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +0200787 .gen_driver = {
788 .name = "ide-scsi",
789 .bus = &ide_bus_type,
790 .probe = ide_scsi_probe,
791 .remove = ide_scsi_remove,
792 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 .version = IDESCSI_VERSION,
794 .media = ide_scsi,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 .supports_dsc_overlap = 0,
796 .proc = idescsi_proc,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 .do_request = idescsi_do_request,
798 .end_request = idescsi_end_request,
799 .error = idescsi_atapi_error,
800 .abort = idescsi_atapi_abort,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801};
802
803static int idescsi_ide_open(struct inode *inode, struct file *filp)
804{
805 struct gendisk *disk = inode->i_bdev->bd_disk;
806 struct ide_scsi_obj *scsi;
807 ide_drive_t *drive;
808
809 if (!(scsi = ide_scsi_get(disk)))
810 return -ENXIO;
811
812 drive = scsi->drive;
813
814 drive->usage++;
815
816 return 0;
817}
818
819static int idescsi_ide_release(struct inode *inode, struct file *filp)
820{
821 struct gendisk *disk = inode->i_bdev->bd_disk;
822 struct ide_scsi_obj *scsi = ide_scsi_g(disk);
823 ide_drive_t *drive = scsi->drive;
824
825 drive->usage--;
826
827 ide_scsi_put(scsi);
828
829 return 0;
830}
831
832static int idescsi_ide_ioctl(struct inode *inode, struct file *file,
833 unsigned int cmd, unsigned long arg)
834{
835 struct block_device *bdev = inode->i_bdev;
836 struct ide_scsi_obj *scsi = ide_scsi_g(bdev->bd_disk);
837 return generic_ide_ioctl(scsi->drive, file, bdev, cmd, arg);
838}
839
840static struct block_device_operations idescsi_ops = {
841 .owner = THIS_MODULE,
842 .open = idescsi_ide_open,
843 .release = idescsi_ide_release,
844 .ioctl = idescsi_ide_ioctl,
845};
846
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847static int idescsi_slave_configure(struct scsi_device * sdp)
848{
849 /* Configure detected device */
850 scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, sdp->host->cmd_per_lun);
851 return 0;
852}
853
854static const char *idescsi_info (struct Scsi_Host *host)
855{
856 return "SCSI host adapter emulation for IDE ATAPI devices";
857}
858
859static int idescsi_ioctl (struct scsi_device *dev, int cmd, void __user *arg)
860{
861 idescsi_scsi_t *scsi = scsihost_to_idescsi(dev->host);
862
863 if (cmd == SG_SET_TRANSFORM) {
864 if (arg)
865 set_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
866 else
867 clear_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
868 return 0;
869 } else if (cmd == SG_GET_TRANSFORM)
870 return put_user(test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform), (int __user *) arg);
871 return -EINVAL;
872}
873
874static inline int should_transform(ide_drive_t *drive, struct scsi_cmnd *cmd)
875{
876 idescsi_scsi_t *scsi = drive_to_idescsi(drive);
877
878 /* this was a layering violation and we can't support it
879 anymore, sorry. */
880#if 0
881 struct gendisk *disk = cmd->request->rq_disk;
882
883 if (disk) {
884 struct Scsi_Device_Template **p = disk->private_data;
885 if (strcmp((*p)->scsi_driverfs_driver.name, "sg") == 0)
886 return test_bit(IDESCSI_SG_TRANSFORM, &scsi->transform);
887 }
888#endif
889 return test_bit(IDESCSI_TRANSFORM, &scsi->transform);
890}
891
892static int idescsi_queue (struct scsi_cmnd *cmd,
893 void (*done)(struct scsi_cmnd *))
894{
895 struct Scsi_Host *host = cmd->device->host;
896 idescsi_scsi_t *scsi = scsihost_to_idescsi(host);
897 ide_drive_t *drive = scsi->drive;
898 struct request *rq = NULL;
899 idescsi_pc_t *pc = NULL;
900
901 if (!drive) {
Jeff Garzik017560f2005-10-24 18:04:36 -0400902 scmd_printk (KERN_ERR, cmd, "drive not present\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 goto abort;
904 }
905 scsi = drive_to_idescsi(drive);
906 pc = kmalloc (sizeof (idescsi_pc_t), GFP_ATOMIC);
907 rq = kmalloc (sizeof (struct request), GFP_ATOMIC);
908 if (rq == NULL || pc == NULL) {
909 printk (KERN_ERR "ide-scsi: %s: out of memory\n", drive->name);
910 goto abort;
911 }
912
913 memset (pc->c, 0, 12);
914 pc->flags = 0;
915 pc->rq = rq;
916 memcpy (pc->c, cmd->cmnd, cmd->cmd_len);
917 if (cmd->use_sg) {
918 pc->buffer = NULL;
919 pc->sg = cmd->request_buffer;
920 } else {
921 pc->buffer = cmd->request_buffer;
922 pc->sg = NULL;
923 }
924 pc->b_count = 0;
925 pc->request_transfer = pc->buffer_size = cmd->request_bufflen;
926 pc->scsi_cmd = cmd;
927 pc->done = done;
928 pc->timeout = jiffies + cmd->timeout_per_command;
929
930 if (should_transform(drive, cmd))
931 set_bit(PC_TRANSFORM, &pc->flags);
932 idescsi_transform_pc1 (drive, pc);
933
934 if (test_bit(IDESCSI_LOG_CMD, &scsi->log)) {
935 printk ("ide-scsi: %s: que %lu, cmd = ", drive->name, cmd->serial_number);
936 hexdump(cmd->cmnd, cmd->cmd_len);
937 if (memcmp(pc->c, cmd->cmnd, cmd->cmd_len)) {
938 printk ("ide-scsi: %s: que %lu, tsl = ", drive->name, cmd->serial_number);
939 hexdump(pc->c, 12);
940 }
941 }
942
943 ide_init_drive_cmd (rq);
944 rq->special = (char *) pc;
945 rq->flags = REQ_SPECIAL;
946 spin_unlock_irq(host->host_lock);
947 rq->rq_disk = scsi->disk;
948 (void) ide_do_drive_cmd (drive, rq, ide_end);
949 spin_lock_irq(host->host_lock);
950 return 0;
951abort:
Jesper Juhlc9475cb2005-11-07 01:01:26 -0800952 kfree (pc);
953 kfree (rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700954 cmd->result = DID_ERROR << 16;
955 done(cmd);
956 return 0;
957}
958
959static int idescsi_eh_abort (struct scsi_cmnd *cmd)
960{
961 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
962 ide_drive_t *drive = scsi->drive;
963 int busy;
964 int ret = FAILED;
965
966 /* In idescsi_eh_abort we try to gently pry our command from the ide subsystem */
967
968 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
969 printk (KERN_WARNING "ide-scsi: abort called for %lu\n", cmd->serial_number);
970
971 if (!drive) {
972 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_abort\n");
973 WARN_ON(1);
974 goto no_drive;
975 }
976
977 /* First give it some more time, how much is "right" is hard to say :-( */
978
979 busy = ide_wait_not_busy(HWIF(drive), 100); /* FIXME - uses mdelay which causes latency? */
980 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
981 printk (KERN_WARNING "ide-scsi: drive did%s become ready\n", busy?" not":"");
982
983 spin_lock_irq(&ide_lock);
984
985 /* If there is no pc running we're done (our interrupt took care of it) */
986 if (!scsi->pc) {
987 ret = SUCCESS;
988 goto ide_unlock;
989 }
990
991 /* It's somewhere in flight. Does ide subsystem agree? */
992 if (scsi->pc->scsi_cmd->serial_number == cmd->serial_number && !busy &&
993 elv_queue_empty(drive->queue) && HWGROUP(drive)->rq != scsi->pc->rq) {
994 /*
995 * FIXME - not sure this condition can ever occur
996 */
997 printk (KERN_ERR "ide-scsi: cmd aborted!\n");
998
999 if (scsi->pc->rq->flags & REQ_SENSE)
1000 kfree(scsi->pc->buffer);
1001 kfree(scsi->pc->rq);
1002 kfree(scsi->pc);
1003 scsi->pc = NULL;
1004
1005 ret = SUCCESS;
1006 }
1007
1008ide_unlock:
1009 spin_unlock_irq(&ide_lock);
1010no_drive:
1011 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
1012 printk (KERN_WARNING "ide-scsi: abort returns %s\n", ret == SUCCESS?"success":"failed");
1013
1014 return ret;
1015}
1016
1017static int idescsi_eh_reset (struct scsi_cmnd *cmd)
1018{
1019 struct request *req;
1020 idescsi_scsi_t *scsi = scsihost_to_idescsi(cmd->device->host);
1021 ide_drive_t *drive = scsi->drive;
1022 int ready = 0;
1023 int ret = SUCCESS;
1024
1025 /* In idescsi_eh_reset we forcefully remove the command from the ide subsystem and reset the device. */
1026
1027 if (test_bit(IDESCSI_LOG_CMD, &scsi->log))
1028 printk (KERN_WARNING "ide-scsi: reset called for %lu\n", cmd->serial_number);
1029
1030 if (!drive) {
1031 printk (KERN_WARNING "ide-scsi: Drive not set in idescsi_eh_reset\n");
1032 WARN_ON(1);
1033 return FAILED;
1034 }
1035
Jeff Garzik df0ae242005-05-28 07:57:14 -04001036 spin_lock_irq(cmd->device->host->host_lock);
1037 spin_lock(&ide_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038
1039 if (!scsi->pc || (req = scsi->pc->rq) != HWGROUP(drive)->rq || !HWGROUP(drive)->handler) {
1040 printk (KERN_WARNING "ide-scsi: No active request in idescsi_eh_reset\n");
1041 spin_unlock(&ide_lock);
Jeff Garzik df0ae242005-05-28 07:57:14 -04001042 spin_unlock_irq(cmd->device->host->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 return FAILED;
1044 }
1045
1046 /* kill current request */
1047 blkdev_dequeue_request(req);
1048 end_that_request_last(req);
1049 if (req->flags & REQ_SENSE)
1050 kfree(scsi->pc->buffer);
1051 kfree(scsi->pc);
1052 scsi->pc = NULL;
1053 kfree(req);
1054
1055 /* now nuke the drive queue */
1056 while ((req = elv_next_request(drive->queue))) {
1057 blkdev_dequeue_request(req);
1058 end_that_request_last(req);
1059 }
1060
1061 HWGROUP(drive)->rq = NULL;
1062 HWGROUP(drive)->handler = NULL;
1063 HWGROUP(drive)->busy = 1; /* will set this to zero when ide reset finished */
Jeff Garzik df0ae242005-05-28 07:57:14 -04001064 spin_unlock(&ide_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 ide_do_reset(drive);
1067
1068 /* ide_do_reset starts a polling handler which restarts itself every 50ms until the reset finishes */
1069
1070 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071 spin_unlock_irq(cmd->device->host->host_lock);
Jeff Garzik df0ae242005-05-28 07:57:14 -04001072 msleep(50);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 spin_lock_irq(cmd->device->host->host_lock);
1074 } while ( HWGROUP(drive)->handler );
1075
1076 ready = drive_is_ready(drive);
1077 HWGROUP(drive)->busy--;
1078 if (!ready) {
1079 printk (KERN_ERR "ide-scsi: reset failed!\n");
1080 ret = FAILED;
1081 }
1082
Jeff Garzik df0ae242005-05-28 07:57:14 -04001083 spin_unlock_irq(cmd->device->host->host_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 return ret;
1085}
1086
1087static int idescsi_bios(struct scsi_device *sdev, struct block_device *bdev,
1088 sector_t capacity, int *parm)
1089{
1090 idescsi_scsi_t *idescsi = scsihost_to_idescsi(sdev->host);
1091 ide_drive_t *drive = idescsi->drive;
1092
1093 if (drive->bios_cyl && drive->bios_head && drive->bios_sect) {
1094 parm[0] = drive->bios_head;
1095 parm[1] = drive->bios_sect;
1096 parm[2] = drive->bios_cyl;
1097 }
1098 return 0;
1099}
1100
1101static struct scsi_host_template idescsi_template = {
1102 .module = THIS_MODULE,
1103 .name = "idescsi",
1104 .info = idescsi_info,
1105 .slave_configure = idescsi_slave_configure,
1106 .ioctl = idescsi_ioctl,
1107 .queuecommand = idescsi_queue,
1108 .eh_abort_handler = idescsi_eh_abort,
1109 .eh_host_reset_handler = idescsi_eh_reset,
1110 .bios_param = idescsi_bios,
1111 .can_queue = 40,
1112 .this_id = -1,
1113 .sg_tablesize = 256,
1114 .cmd_per_lun = 5,
1115 .max_sectors = 128,
1116 .use_clustering = DISABLE_CLUSTERING,
1117 .emulated = 1,
1118 .proc_name = "ide-scsi",
1119};
1120
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001121static int ide_scsi_probe(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001123 ide_drive_t *drive = to_ide_device(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 idescsi_scsi_t *idescsi;
1125 struct Scsi_Host *host;
1126 struct gendisk *g;
1127 static int warned;
1128 int err = -ENOMEM;
1129
1130 if (!warned && drive->media == ide_cdrom) {
1131 printk(KERN_WARNING "ide-scsi is deprecated for cd burning! Use ide-cd and give dev=/dev/hdX as device\n");
1132 warned = 1;
1133 }
1134
1135 if (!strstr("ide-scsi", drive->driver_req) ||
1136 !drive->present ||
1137 drive->media == ide_disk ||
1138 !(host = scsi_host_alloc(&idescsi_template,sizeof(idescsi_scsi_t))))
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001139 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
1141 g = alloc_disk(1 << PARTN_BITS);
1142 if (!g)
1143 goto out_host_put;
1144
1145 ide_init_disk(g, drive);
1146
1147 host->max_id = 1;
1148
1149#if IDESCSI_DEBUG_LOG
1150 if (drive->id->last_lun)
1151 printk(KERN_NOTICE "%s: id->last_lun=%u\n", drive->name, drive->id->last_lun);
1152#endif
1153 if ((drive->id->last_lun & 0x7) != 7)
1154 host->max_lun = (drive->id->last_lun & 0x7) + 1;
1155 else
1156 host->max_lun = 1;
1157
1158 drive->driver_data = host;
1159 idescsi = scsihost_to_idescsi(host);
1160 idescsi->drive = drive;
1161 idescsi->driver = &idescsi_driver;
1162 idescsi->host = host;
1163 idescsi->disk = g;
1164 g->private_data = &idescsi->driver;
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001165 ide_register_subdriver(drive, &idescsi_driver);
1166 err = 0;
1167 idescsi_setup(drive, idescsi);
1168 g->fops = &idescsi_ops;
1169 ide_register_region(g);
1170 err = scsi_add_host(host, &drive->gendev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 if (!err) {
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001172 scsi_scan_host(host);
1173 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 }
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001175 /* fall through on error */
1176 ide_unregister_region(g);
1177 ide_unregister_subdriver(drive, &idescsi_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178
1179 put_disk(g);
1180out_host_put:
1181 scsi_host_put(host);
1182 return err;
1183}
1184
1185static int __init init_idescsi_module(void)
1186{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001187 return driver_register(&idescsi_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188}
1189
1190static void __exit exit_idescsi_module(void)
1191{
Bartlomiej Zolnierkiewicz8604aff2005-05-26 14:55:34 +02001192 driver_unregister(&idescsi_driver.gen_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
1195module_init(init_idescsi_module);
1196module_exit(exit_idescsi_module);
1197MODULE_LICENSE("GPL");