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