inotify: fix race
[linux-2.6.22.y-op.git] / drivers / block / acsi.c
blobe3d9152e231a11902e5d40aba2295e9c415ad369
1 /*
2 * acsi.c -- Device driver for Atari ACSI hard disks
4 * Copyright 1994 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
6 * Some parts are based on hd.c by Linus Torvalds
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file COPYING in the main directory of this archive for
10 * more details.
15 * Still to in this file:
16 * - If a command ends with an error status (!= 0), the following
17 * REQUEST SENSE commands (4 to fill the ST-DMA FIFO) are done by
18 * polling the _IRQ signal (not interrupt-driven). This should be
19 * avoided in future because it takes up a non-neglectible time in
20 * the interrupt service routine while interrupts are disabled.
21 * Maybe a timer interrupt will get lost :-(
25 * General notes:
27 * - All ACSI devices (disks, CD-ROMs, ...) use major number 28.
28 * Minors are organized like it is with SCSI: The upper 4 bits
29 * identify the device, the lower 4 bits the partition.
30 * The device numbers (the upper 4 bits) are given in the same
31 * order as the devices are found on the bus.
32 * - Up to 8 LUNs are supported for each target (if CONFIG_ACSI_MULTI_LUN
33 * is defined), but only a total of 16 devices (due to minor
34 * numbers...). Note that Atari allows only a maximum of 4 targets
35 * (i.e. controllers, not devices) on the ACSI bus!
36 * - A optimizing scheme similar to SCSI scatter-gather is implemented.
37 * - Removable media are supported. After a medium change to device
38 * is reinitialized (partition check etc.). Also, if the device
39 * knows the PREVENT/ALLOW MEDIUM REMOVAL command, the door should
40 * be locked and unlocked when mounting the first or unmounting the
41 * last filesystem on the device. The code is untested, because I
42 * don't have a removable hard disk.
46 #include <linux/module.h>
47 #include <linux/errno.h>
48 #include <linux/signal.h>
49 #include <linux/timer.h>
50 #include <linux/fs.h>
51 #include <linux/kernel.h>
52 #include <linux/genhd.h>
53 #include <linux/delay.h>
54 #include <linux/mm.h>
55 #include <linux/major.h>
56 #include <linux/slab.h>
57 #include <linux/interrupt.h>
58 #include <scsi/scsi.h> /* for SCSI_IOCTL_GET_IDLUN */
59 #include <scsi/scsi_ioctl.h>
60 #include <linux/hdreg.h> /* for HDIO_GETGEO */
61 #include <linux/blkpg.h>
62 #include <linux/buffer_head.h>
63 #include <linux/blkdev.h>
65 #include <asm/setup.h>
66 #include <asm/pgtable.h>
67 #include <asm/system.h>
68 #include <asm/uaccess.h>
69 #include <asm/atarihw.h>
70 #include <asm/atariints.h>
71 #include <asm/atari_acsi.h>
72 #include <asm/atari_stdma.h>
73 #include <asm/atari_stram.h>
75 static void (*do_acsi)(void) = NULL;
76 static struct request_queue *acsi_queue;
77 #define QUEUE (acsi_queue)
78 #define CURRENT elv_next_request(acsi_queue)
80 #define DEBUG
81 #undef DEBUG_DETECT
82 #undef NO_WRITE
84 #define MAX_ERRORS 8 /* Max read/write errors/sector */
85 #define MAX_LUN 8 /* Max LUNs per target */
86 #define MAX_DEV 16
88 #define ACSI_BUFFER_SIZE (16*1024) /* "normal" ACSI buffer size */
89 #define ACSI_BUFFER_MINSIZE (2048) /* min. buf size if ext. DMA */
90 #define ACSI_BUFFER_SIZE_ORDER 2 /* order size for above */
91 #define ACSI_BUFFER_MINSIZE_ORDER 0 /* order size for above */
92 #define ACSI_BUFFER_SECTORS (ACSI_BUFFER_SIZE/512)
94 #define ACSI_BUFFER_ORDER \
95 (ATARIHW_PRESENT(EXTD_DMA) ? \
96 ACSI_BUFFER_MINSIZE_ORDER : \
97 ACSI_BUFFER_SIZE_ORDER)
99 #define ACSI_TIMEOUT (4*HZ)
101 /* minimum delay between two commands */
103 #define COMMAND_DELAY 500
105 typedef enum {
106 NONE, HARDDISK, CDROM
107 } ACSI_TYPE;
109 struct acsi_info_struct {
110 ACSI_TYPE type; /* type of device */
111 unsigned target; /* target number */
112 unsigned lun; /* LUN in target controller */
113 unsigned removable : 1; /* Flag for removable media */
114 unsigned read_only : 1; /* Flag for read only devices */
115 unsigned old_atari_disk : 1; /* Is an old Atari disk */
116 unsigned changed : 1; /* Medium has been changed */
117 unsigned long size; /* #blocks */
118 int access_count;
119 } acsi_info[MAX_DEV];
122 * SENSE KEYS
125 #define NO_SENSE 0x00
126 #define RECOVERED_ERROR 0x01
127 #define NOT_READY 0x02
128 #define MEDIUM_ERROR 0x03
129 #define HARDWARE_ERROR 0x04
130 #define ILLEGAL_REQUEST 0x05
131 #define UNIT_ATTENTION 0x06
132 #define DATA_PROTECT 0x07
133 #define BLANK_CHECK 0x08
134 #define COPY_ABORTED 0x0a
135 #define ABORTED_COMMAND 0x0b
136 #define VOLUME_OVERFLOW 0x0d
137 #define MISCOMPARE 0x0e
141 * DEVICE TYPES
144 #define TYPE_DISK 0x00
145 #define TYPE_TAPE 0x01
146 #define TYPE_WORM 0x04
147 #define TYPE_ROM 0x05
148 #define TYPE_MOD 0x07
149 #define TYPE_NO_LUN 0x7f
151 /* The data returned by MODE SENSE differ between the old Atari
152 * hard disks and SCSI disks connected to ACSI. In the following, both
153 * formats are defined and some macros to operate on them potably.
156 typedef struct {
157 unsigned long dummy[2];
158 unsigned long sector_size;
159 unsigned char format_code;
160 #define ATARI_SENSE_FORMAT_FIX 1
161 #define ATARI_SENSE_FORMAT_CHNG 2
162 unsigned char cylinders_h;
163 unsigned char cylinders_l;
164 unsigned char heads;
165 unsigned char reduced_h;
166 unsigned char reduced_l;
167 unsigned char precomp_h;
168 unsigned char precomp_l;
169 unsigned char landing_zone;
170 unsigned char steprate;
171 unsigned char type;
172 #define ATARI_SENSE_TYPE_FIXCHNG_MASK 4
173 #define ATARI_SENSE_TYPE_SOFTHARD_MASK 8
174 #define ATARI_SENSE_TYPE_FIX 4
175 #define ATARI_SENSE_TYPE_CHNG 0
176 #define ATARI_SENSE_TYPE_SOFT 0
177 #define ATARI_SENSE_TYPE_HARD 8
178 unsigned char sectors;
179 } ATARI_SENSE_DATA;
181 #define ATARI_CAPACITY(sd) \
182 (((int)((sd).cylinders_h<<8)|(sd).cylinders_l) * \
183 (sd).heads * (sd).sectors)
186 typedef struct {
187 unsigned char dummy1;
188 unsigned char medium_type;
189 unsigned char dummy2;
190 unsigned char descriptor_size;
191 unsigned long block_count;
192 unsigned long sector_size;
193 /* Page 0 data */
194 unsigned char page_code;
195 unsigned char page_size;
196 unsigned char page_flags;
197 unsigned char qualifier;
198 } SCSI_SENSE_DATA;
200 #define SCSI_CAPACITY(sd) ((sd).block_count & 0xffffff)
203 typedef union {
204 ATARI_SENSE_DATA atari;
205 SCSI_SENSE_DATA scsi;
206 } SENSE_DATA;
208 #define SENSE_TYPE_UNKNOWN 0
209 #define SENSE_TYPE_ATARI 1
210 #define SENSE_TYPE_SCSI 2
212 #define SENSE_TYPE(sd) \
213 (((sd).atari.dummy[0] == 8 && \
214 ((sd).atari.format_code == 1 || \
215 (sd).atari.format_code == 2)) ? SENSE_TYPE_ATARI : \
216 ((sd).scsi.dummy1 >= 11) ? SENSE_TYPE_SCSI : \
217 SENSE_TYPE_UNKNOWN)
219 #define CAPACITY(sd) \
220 (SENSE_TYPE(sd) == SENSE_TYPE_ATARI ? \
221 ATARI_CAPACITY((sd).atari) : \
222 SCSI_CAPACITY((sd).scsi))
224 #define SECTOR_SIZE(sd) \
225 (SENSE_TYPE(sd) == SENSE_TYPE_ATARI ? \
226 (sd).atari.sector_size : \
227 (sd).scsi.sector_size & 0xffffff)
229 /* Default size if capacity cannot be determined (1 GByte) */
230 #define DEFAULT_SIZE 0x1fffff
232 #define CARTRCH_STAT(aip,buf) \
233 (aip->old_atari_disk ? \
234 (((buf)[0] & 0x7f) == 0x28) : \
235 ((((buf)[0] & 0x70) == 0x70) ? \
236 (((buf)[2] & 0x0f) == 0x06) : \
237 (((buf)[0] & 0x0f) == 0x06))) \
239 /* These two are also exported to other drivers that work on the ACSI bus and
240 * need an ST-RAM buffer. */
241 char *acsi_buffer;
242 unsigned long phys_acsi_buffer;
244 static int NDevices;
246 static int CurrentNReq;
247 static int CurrentNSect;
248 static char *CurrentBuffer;
250 static DEFINE_SPINLOCK(acsi_lock);
253 #define SET_TIMER() mod_timer(&acsi_timer, jiffies + ACSI_TIMEOUT)
254 #define CLEAR_TIMER() del_timer(&acsi_timer)
256 static unsigned long STramMask;
257 #define STRAM_ADDR(a) (((a) & STramMask) == 0)
261 /* ACSI commands */
263 static char tur_cmd[6] = { 0x00, 0, 0, 0, 0, 0 };
264 static char modesense_cmd[6] = { 0x1a, 0, 0, 0, 24, 0 };
265 static char modeselect_cmd[6] = { 0x15, 0, 0, 0, 12, 0 };
266 static char inquiry_cmd[6] = { 0x12, 0, 0, 0,255, 0 };
267 static char reqsense_cmd[6] = { 0x03, 0, 0, 0, 4, 0 };
268 static char read_cmd[6] = { 0x08, 0, 0, 0, 0, 0 };
269 static char write_cmd[6] = { 0x0a, 0, 0, 0, 0, 0 };
270 static char pa_med_rem_cmd[6] = { 0x1e, 0, 0, 0, 0, 0 };
272 #define CMDSET_TARG_LUN(cmd,targ,lun) \
273 do { \
274 cmd[0] = (cmd[0] & ~0xe0) | (targ)<<5; \
275 cmd[1] = (cmd[1] & ~0xe0) | (lun)<<5; \
276 } while(0)
278 #define CMDSET_BLOCK(cmd,blk) \
279 do { \
280 unsigned long __blk = (blk); \
281 cmd[3] = __blk; __blk >>= 8; \
282 cmd[2] = __blk; __blk >>= 8; \
283 cmd[1] = (cmd[1] & 0xe0) | (__blk & 0x1f); \
284 } while(0)
286 #define CMDSET_LEN(cmd,len) \
287 do { \
288 cmd[4] = (len); \
289 } while(0)
291 /* ACSI errors (from REQUEST SENSE); There are two tables, one for the
292 * old Atari disks and one for SCSI on ACSI disks.
295 struct acsi_error {
296 unsigned char code;
297 const char *text;
298 } atari_acsi_errors[] = {
299 { 0x00, "No error (??)" },
300 { 0x01, "No index pulses" },
301 { 0x02, "Seek not complete" },
302 { 0x03, "Write fault" },
303 { 0x04, "Drive not ready" },
304 { 0x06, "No Track 00 signal" },
305 { 0x10, "ECC error in ID field" },
306 { 0x11, "Uncorrectable data error" },
307 { 0x12, "ID field address mark not found" },
308 { 0x13, "Data field address mark not found" },
309 { 0x14, "Record not found" },
310 { 0x15, "Seek error" },
311 { 0x18, "Data check in no retry mode" },
312 { 0x19, "ECC error during verify" },
313 { 0x1a, "Access to bad block" },
314 { 0x1c, "Unformatted or bad format" },
315 { 0x20, "Invalid command" },
316 { 0x21, "Invalid block address" },
317 { 0x23, "Volume overflow" },
318 { 0x24, "Invalid argument" },
319 { 0x25, "Invalid drive number" },
320 { 0x26, "Byte zero parity check" },
321 { 0x28, "Cartride changed" },
322 { 0x2c, "Error count overflow" },
323 { 0x30, "Controller selftest failed" }
326 scsi_acsi_errors[] = {
327 { 0x00, "No error (??)" },
328 { 0x01, "Recovered error" },
329 { 0x02, "Drive not ready" },
330 { 0x03, "Uncorrectable medium error" },
331 { 0x04, "Hardware error" },
332 { 0x05, "Illegal request" },
333 { 0x06, "Unit attention (Reset or cartridge changed)" },
334 { 0x07, "Data protection" },
335 { 0x08, "Blank check" },
336 { 0x0b, "Aborted Command" },
337 { 0x0d, "Volume overflow" }
342 /***************************** Prototypes *****************************/
344 static int acsicmd_dma( const char *cmd, char *buffer, int blocks, int
345 rwflag, int enable);
346 static int acsi_reqsense( char *buffer, int targ, int lun);
347 static void acsi_print_error(const unsigned char *errblk, struct acsi_info_struct *aip);
348 static irqreturn_t acsi_interrupt (int irq, void *data);
349 static void unexpected_acsi_interrupt( void );
350 static void bad_rw_intr( void );
351 static void read_intr( void );
352 static void write_intr( void);
353 static void acsi_times_out( unsigned long dummy );
354 static void copy_to_acsibuffer( void );
355 static void copy_from_acsibuffer( void );
356 static void do_end_requests( void );
357 static void do_acsi_request( request_queue_t * );
358 static void redo_acsi_request( void );
359 static int acsi_ioctl( struct inode *inode, struct file *file, unsigned int
360 cmd, unsigned long arg );
361 static int acsi_open( struct inode * inode, struct file * filp );
362 static int acsi_release( struct inode * inode, struct file * file );
363 static void acsi_prevent_removal(struct acsi_info_struct *aip, int flag );
364 static int acsi_change_blk_size( int target, int lun);
365 static int acsi_mode_sense( int target, int lun, SENSE_DATA *sd );
366 static int acsi_revalidate (struct gendisk *disk);
368 /************************* End of Prototypes **************************/
371 DEFINE_TIMER(acsi_timer, acsi_times_out, 0, 0);
374 #ifdef CONFIG_ATARI_SLM
376 extern int attach_slm( int target, int lun );
377 extern int slm_init( void );
379 #endif
383 /***********************************************************************
385 * ACSI primitives
387 **********************************************************************/
391 * The following two functions wait for _IRQ to become Low or High,
392 * resp., with a timeout. The 'timeout' parameter is in jiffies
393 * (10ms).
394 * If the functions are called with timer interrupts on (int level <
395 * 6), the timeout is based on the 'jiffies' variable to provide exact
396 * timeouts for device probing etc.
397 * If interrupts are disabled, the number of tries is based on the
398 * 'loops_per_jiffy' variable. A rough estimation is sufficient here...
401 #define INT_LEVEL \
402 ({ unsigned __sr; \
403 __asm__ __volatile__ ( "movew %/sr,%0" : "=dm" (__sr) ); \
404 (__sr >> 8) & 7; \
407 int acsi_wait_for_IRQ( unsigned timeout )
410 if (INT_LEVEL < 6) {
411 unsigned long maxjif = jiffies + timeout;
412 while (time_before(jiffies, maxjif))
413 if (!(mfp.par_dt_reg & 0x20)) return( 1 );
415 else {
416 long tries = loops_per_jiffy / 8 * timeout;
417 while( --tries >= 0 )
418 if (!(mfp.par_dt_reg & 0x20)) return( 1 );
420 return( 0 ); /* timeout! */
424 int acsi_wait_for_noIRQ( unsigned timeout )
427 if (INT_LEVEL < 6) {
428 unsigned long maxjif = jiffies + timeout;
429 while (time_before(jiffies, maxjif))
430 if (mfp.par_dt_reg & 0x20) return( 1 );
432 else {
433 long tries = loops_per_jiffy * timeout / 8;
434 while( tries-- >= 0 )
435 if (mfp.par_dt_reg & 0x20) return( 1 );
437 return( 0 ); /* timeout! */
440 static struct timeval start_time;
442 void
443 acsi_delay_start(void)
445 do_gettimeofday(&start_time);
448 /* wait from acsi_delay_start to now usec (<1E6) usec */
450 void
451 acsi_delay_end(long usec)
453 struct timeval end_time;
454 long deltau,deltas;
455 do_gettimeofday(&end_time);
456 deltau=end_time.tv_usec - start_time.tv_usec;
457 deltas=end_time.tv_sec - start_time.tv_sec;
458 if (deltas > 1 || deltas < 0)
459 return;
460 if (deltas > 0)
461 deltau += 1000*1000;
462 if (deltau >= usec)
463 return;
464 udelay(usec-deltau);
467 /* acsicmd_dma() sends an ACSI command and sets up the DMA to transfer
468 * 'blocks' blocks of 512 bytes from/to 'buffer'.
469 * Because the _IRQ signal is used for handshaking the command bytes,
470 * the ACSI interrupt has to be disabled in this function. If the end
471 * of the operation should be signalled by a real interrupt, it has to be
472 * reenabled afterwards.
475 static int acsicmd_dma( const char *cmd, char *buffer, int blocks, int rwflag, int enable)
477 { unsigned long flags, paddr;
478 int i;
480 #ifdef NO_WRITE
481 if (rwflag || *cmd == 0x0a) {
482 printk( "ACSI: Write commands disabled!\n" );
483 return( 0 );
485 #endif
487 rwflag = rwflag ? 0x100 : 0;
488 paddr = virt_to_phys( buffer );
490 acsi_delay_end(COMMAND_DELAY);
491 DISABLE_IRQ();
493 local_irq_save(flags);
494 /* Low on A1 */
495 dma_wd.dma_mode_status = 0x88 | rwflag;
496 MFPDELAY();
498 /* set DMA address */
499 dma_wd.dma_lo = (unsigned char)paddr;
500 paddr >>= 8;
501 MFPDELAY();
502 dma_wd.dma_md = (unsigned char)paddr;
503 paddr >>= 8;
504 MFPDELAY();
505 if (ATARIHW_PRESENT(EXTD_DMA))
506 st_dma_ext_dmahi = (unsigned short)paddr;
507 else
508 dma_wd.dma_hi = (unsigned char)paddr;
509 MFPDELAY();
510 local_irq_restore(flags);
512 /* send the command bytes except the last */
513 for( i = 0; i < 5; ++i ) {
514 DMA_LONG_WRITE( *cmd++, 0x8a | rwflag );
515 udelay(20);
516 if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
519 /* Clear FIFO and switch DMA to correct direction */
520 dma_wd.dma_mode_status = 0x92 | (rwflag ^ 0x100);
521 MFPDELAY();
522 dma_wd.dma_mode_status = 0x92 | rwflag;
523 MFPDELAY();
525 /* How many sectors for DMA */
526 dma_wd.fdc_acces_seccount = blocks;
527 MFPDELAY();
529 /* send last command byte */
530 dma_wd.dma_mode_status = 0x8a | rwflag;
531 MFPDELAY();
532 DMA_LONG_WRITE( *cmd++, 0x0a | rwflag );
533 if (enable)
534 ENABLE_IRQ();
535 udelay(80);
537 return( 1 );
542 * acsicmd_nodma() sends an ACSI command that requires no DMA.
545 int acsicmd_nodma( const char *cmd, int enable)
547 { int i;
549 acsi_delay_end(COMMAND_DELAY);
550 DISABLE_IRQ();
552 /* send first command byte */
553 dma_wd.dma_mode_status = 0x88;
554 MFPDELAY();
555 DMA_LONG_WRITE( *cmd++, 0x8a );
556 udelay(20);
557 if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
559 /* send the intermediate command bytes */
560 for( i = 0; i < 4; ++i ) {
561 DMA_LONG_WRITE( *cmd++, 0x8a );
562 udelay(20);
563 if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
566 /* send last command byte */
567 DMA_LONG_WRITE( *cmd++, 0x0a );
568 if (enable)
569 ENABLE_IRQ();
570 udelay(80);
572 return( 1 );
573 /* Note that the ACSI interrupt is still disabled after this
574 * function. If you want to get the IRQ delivered, enable it manually!
579 static int acsi_reqsense( char *buffer, int targ, int lun)
582 CMDSET_TARG_LUN( reqsense_cmd, targ, lun);
583 if (!acsicmd_dma( reqsense_cmd, buffer, 1, 0, 0 )) return( 0 );
584 if (!acsi_wait_for_IRQ( 10 )) return( 0 );
585 acsi_getstatus();
586 if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
587 if (!acsi_wait_for_IRQ( 10 )) return( 0 );
588 acsi_getstatus();
589 if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
590 if (!acsi_wait_for_IRQ( 10 )) return( 0 );
591 acsi_getstatus();
592 if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
593 if (!acsi_wait_for_IRQ( 10 )) return( 0 );
594 acsi_getstatus();
595 dma_cache_maintenance( virt_to_phys(buffer), 16, 0 );
597 return( 1 );
602 * ACSI status phase: get the status byte from the bus
604 * I've seen several times that a 0xff status is read, propably due to
605 * a timing error. In this case, the procedure is repeated after the
606 * next _IRQ edge.
609 int acsi_getstatus( void )
611 { int status;
613 DISABLE_IRQ();
614 for(;;) {
615 if (!acsi_wait_for_IRQ( 100 )) {
616 acsi_delay_start();
617 return( -1 );
619 dma_wd.dma_mode_status = 0x8a;
620 MFPDELAY();
621 status = dma_wd.fdc_acces_seccount;
622 if (status != 0xff) break;
623 #ifdef DEBUG
624 printk("ACSI: skipping 0xff status byte\n" );
625 #endif
626 udelay(40);
627 acsi_wait_for_noIRQ( 20 );
629 dma_wd.dma_mode_status = 0x80;
630 udelay(40);
631 acsi_wait_for_noIRQ( 20 );
633 acsi_delay_start();
634 return( status & 0x1f ); /* mask of the device# */
638 #if (defined(CONFIG_ATARI_SLM) || defined(CONFIG_ATARI_SLM_MODULE))
640 /* Receive data in an extended status phase. Needed by SLM printer. */
642 int acsi_extstatus( char *buffer, int cnt )
644 { int status;
646 DISABLE_IRQ();
647 udelay(80);
648 while( cnt-- > 0 ) {
649 if (!acsi_wait_for_IRQ( 40 )) return( 0 );
650 dma_wd.dma_mode_status = 0x8a;
651 MFPDELAY();
652 status = dma_wd.fdc_acces_seccount;
653 MFPDELAY();
654 *buffer++ = status & 0xff;
655 udelay(40);
657 return( 1 );
661 /* Finish an extended status phase */
663 void acsi_end_extstatus( void )
666 dma_wd.dma_mode_status = 0x80;
667 udelay(40);
668 acsi_wait_for_noIRQ( 20 );
669 acsi_delay_start();
673 /* Send data in an extended command phase */
675 int acsi_extcmd( unsigned char *buffer, int cnt )
678 while( cnt-- > 0 ) {
679 DMA_LONG_WRITE( *buffer++, 0x8a );
680 udelay(20);
681 if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
683 return( 1 );
686 #endif
689 static void acsi_print_error(const unsigned char *errblk, struct acsi_info_struct *aip)
691 { int atari_err, i, errcode;
692 struct acsi_error *arr;
694 atari_err = aip->old_atari_disk;
695 if (atari_err)
696 errcode = errblk[0] & 0x7f;
697 else
698 if ((errblk[0] & 0x70) == 0x70)
699 errcode = errblk[2] & 0x0f;
700 else
701 errcode = errblk[0] & 0x0f;
703 printk( KERN_ERR "ACSI error 0x%02x", errcode );
705 if (errblk[0] & 0x80)
706 printk( " for sector %d",
707 ((errblk[1] & 0x1f) << 16) |
708 (errblk[2] << 8) | errblk[0] );
710 arr = atari_err ? atari_acsi_errors : scsi_acsi_errors;
711 i = atari_err ? sizeof(atari_acsi_errors)/sizeof(*atari_acsi_errors) :
712 sizeof(scsi_acsi_errors)/sizeof(*scsi_acsi_errors);
714 for( --i; i >= 0; --i )
715 if (arr[i].code == errcode) break;
716 if (i >= 0)
717 printk( ": %s\n", arr[i].text );
720 /*******************************************************************
722 * ACSI interrupt routine
723 * Test, if this is a ACSI interrupt and call the irq handler
724 * Otherwise ignore this interrupt.
726 *******************************************************************/
728 static irqreturn_t acsi_interrupt(int irq, void *data )
730 { void (*acsi_irq_handler)(void) = do_acsi;
732 do_acsi = NULL;
733 CLEAR_TIMER();
735 if (!acsi_irq_handler)
736 acsi_irq_handler = unexpected_acsi_interrupt;
737 acsi_irq_handler();
738 return IRQ_HANDLED;
742 /******************************************************************
744 * The Interrupt handlers
746 *******************************************************************/
749 static void unexpected_acsi_interrupt( void )
752 printk( KERN_WARNING "Unexpected ACSI interrupt\n" );
756 /* This function is called in case of errors. Because we cannot reset
757 * the ACSI bus or a single device, there is no other choice than
758 * retrying several times :-(
761 static void bad_rw_intr( void )
764 if (!CURRENT)
765 return;
767 if (++CURRENT->errors >= MAX_ERRORS)
768 end_request(CURRENT, 0);
769 /* Otherwise just retry */
773 static void read_intr( void )
775 { int status;
777 status = acsi_getstatus();
778 if (status != 0) {
779 struct gendisk *disk = CURRENT->rq_disk;
780 struct acsi_info_struct *aip = disk->private_data;
781 printk(KERN_ERR "%s: ", disk->disk_name);
782 if (!acsi_reqsense(acsi_buffer, aip->target, aip->lun))
783 printk( "ACSI error and REQUEST SENSE failed (status=0x%02x)\n", status );
784 else {
785 acsi_print_error(acsi_buffer, aip);
786 if (CARTRCH_STAT(aip, acsi_buffer))
787 aip->changed = 1;
789 ENABLE_IRQ();
790 bad_rw_intr();
791 redo_acsi_request();
792 return;
795 dma_cache_maintenance( virt_to_phys(CurrentBuffer), CurrentNSect*512, 0 );
796 if (CurrentBuffer == acsi_buffer)
797 copy_from_acsibuffer();
799 do_end_requests();
800 redo_acsi_request();
804 static void write_intr(void)
806 { int status;
808 status = acsi_getstatus();
809 if (status != 0) {
810 struct gendisk *disk = CURRENT->rq_disk;
811 struct acsi_info_struct *aip = disk->private_data;
812 printk( KERN_ERR "%s: ", disk->disk_name);
813 if (!acsi_reqsense( acsi_buffer, aip->target, aip->lun))
814 printk( "ACSI error and REQUEST SENSE failed (status=0x%02x)\n", status );
815 else {
816 acsi_print_error(acsi_buffer, aip);
817 if (CARTRCH_STAT(aip, acsi_buffer))
818 aip->changed = 1;
820 bad_rw_intr();
821 redo_acsi_request();
822 return;
825 do_end_requests();
826 redo_acsi_request();
830 static void acsi_times_out( unsigned long dummy )
833 DISABLE_IRQ();
834 if (!do_acsi) return;
836 do_acsi = NULL;
837 printk( KERN_ERR "ACSI timeout\n" );
838 if (!CURRENT)
839 return;
840 if (++CURRENT->errors >= MAX_ERRORS) {
841 #ifdef DEBUG
842 printk( KERN_ERR "ACSI: too many errors.\n" );
843 #endif
844 end_request(CURRENT, 0);
847 redo_acsi_request();
852 /***********************************************************************
854 * Scatter-gather utility functions
856 ***********************************************************************/
859 static void copy_to_acsibuffer( void )
861 { int i;
862 char *src, *dst;
863 struct buffer_head *bh;
865 src = CURRENT->buffer;
866 dst = acsi_buffer;
867 bh = CURRENT->bh;
869 if (!bh)
870 memcpy( dst, src, CurrentNSect*512 );
871 else
872 for( i = 0; i < CurrentNReq; ++i ) {
873 memcpy( dst, src, bh->b_size );
874 dst += bh->b_size;
875 if ((bh = bh->b_reqnext))
876 src = bh->b_data;
881 static void copy_from_acsibuffer( void )
883 { int i;
884 char *src, *dst;
885 struct buffer_head *bh;
887 dst = CURRENT->buffer;
888 src = acsi_buffer;
889 bh = CURRENT->bh;
891 if (!bh)
892 memcpy( dst, src, CurrentNSect*512 );
893 else
894 for( i = 0; i < CurrentNReq; ++i ) {
895 memcpy( dst, src, bh->b_size );
896 src += bh->b_size;
897 if ((bh = bh->b_reqnext))
898 dst = bh->b_data;
903 static void do_end_requests( void )
905 { int i, n;
907 if (!CURRENT->bh) {
908 CURRENT->nr_sectors -= CurrentNSect;
909 CURRENT->current_nr_sectors -= CurrentNSect;
910 CURRENT->sector += CurrentNSect;
911 if (CURRENT->nr_sectors == 0)
912 end_request(CURRENT, 1);
914 else {
915 for( i = 0; i < CurrentNReq; ++i ) {
916 n = CURRENT->bh->b_size >> 9;
917 CURRENT->nr_sectors -= n;
918 CURRENT->current_nr_sectors -= n;
919 CURRENT->sector += n;
920 end_request(CURRENT, 1);
928 /***********************************************************************
930 * do_acsi_request and friends
932 ***********************************************************************/
934 static void do_acsi_request( request_queue_t * q )
937 stdma_lock( acsi_interrupt, NULL );
938 redo_acsi_request();
942 static void redo_acsi_request( void )
944 unsigned block, target, lun, nsect;
945 char *buffer;
946 unsigned long pbuffer;
947 struct buffer_head *bh;
948 struct gendisk *disk;
949 struct acsi_info_struct *aip;
951 repeat:
952 CLEAR_TIMER();
954 if (do_acsi)
955 return;
957 if (!CURRENT) {
958 do_acsi = NULL;
959 ENABLE_IRQ();
960 stdma_release();
961 return;
964 disk = CURRENT->rq_disk;
965 aip = disk->private_data;
966 if (CURRENT->bh) {
967 if (!CURRENT->bh && !buffer_locked(CURRENT->bh))
968 panic("ACSI: block not locked");
971 block = CURRENT->sector;
972 if (block+CURRENT->nr_sectors >= get_capacity(disk)) {
973 #ifdef DEBUG
974 printk( "%s: attempted access for blocks %d...%ld past end of device at block %ld.\n",
975 disk->disk_name,
976 block, block + CURRENT->nr_sectors - 1,
977 get_capacity(disk));
978 #endif
979 end_request(CURRENT, 0);
980 goto repeat;
982 if (aip->changed) {
983 printk( KERN_NOTICE "%s: request denied because cartridge has "
984 "been changed.\n", disk->disk_name);
985 end_request(CURRENT, 0);
986 goto repeat;
989 target = aip->target;
990 lun = aip->lun;
992 /* Find out how many sectors should be transferred from/to
993 * consecutive buffers and thus can be done with a single command.
995 buffer = CURRENT->buffer;
996 pbuffer = virt_to_phys(buffer);
997 nsect = CURRENT->current_nr_sectors;
998 CurrentNReq = 1;
1000 if ((bh = CURRENT->bh) && bh != CURRENT->bhtail) {
1001 if (!STRAM_ADDR(pbuffer)) {
1002 /* If transfer is done via the ACSI buffer anyway, we can
1003 * assemble as much bh's as fit in the buffer.
1005 while( (bh = bh->b_reqnext) ) {
1006 if (nsect + (bh->b_size>>9) > ACSI_BUFFER_SECTORS) break;
1007 nsect += bh->b_size >> 9;
1008 ++CurrentNReq;
1009 if (bh == CURRENT->bhtail) break;
1011 buffer = acsi_buffer;
1012 pbuffer = phys_acsi_buffer;
1014 else {
1015 unsigned long pendadr, pnewadr;
1016 pendadr = pbuffer + nsect*512;
1017 while( (bh = bh->b_reqnext) ) {
1018 pnewadr = virt_to_phys(bh->b_data);
1019 if (!STRAM_ADDR(pnewadr) || pendadr != pnewadr) break;
1020 nsect += bh->b_size >> 9;
1021 pendadr = pnewadr + bh->b_size;
1022 ++CurrentNReq;
1023 if (bh == CURRENT->bhtail) break;
1027 else {
1028 if (!STRAM_ADDR(pbuffer)) {
1029 buffer = acsi_buffer;
1030 pbuffer = phys_acsi_buffer;
1031 if (nsect > ACSI_BUFFER_SECTORS)
1032 nsect = ACSI_BUFFER_SECTORS;
1035 CurrentBuffer = buffer;
1036 CurrentNSect = nsect;
1038 if (rq_data_dir(CURRENT) == WRITE) {
1039 CMDSET_TARG_LUN( write_cmd, target, lun );
1040 CMDSET_BLOCK( write_cmd, block );
1041 CMDSET_LEN( write_cmd, nsect );
1042 if (buffer == acsi_buffer)
1043 copy_to_acsibuffer();
1044 dma_cache_maintenance( pbuffer, nsect*512, 1 );
1045 do_acsi = write_intr;
1046 if (!acsicmd_dma( write_cmd, buffer, nsect, 1, 1)) {
1047 do_acsi = NULL;
1048 printk( KERN_ERR "ACSI (write): Timeout in command block\n" );
1049 bad_rw_intr();
1050 goto repeat;
1052 SET_TIMER();
1053 return;
1055 if (rq_data_dir(CURRENT) == READ) {
1056 CMDSET_TARG_LUN( read_cmd, target, lun );
1057 CMDSET_BLOCK( read_cmd, block );
1058 CMDSET_LEN( read_cmd, nsect );
1059 do_acsi = read_intr;
1060 if (!acsicmd_dma( read_cmd, buffer, nsect, 0, 1)) {
1061 do_acsi = NULL;
1062 printk( KERN_ERR "ACSI (read): Timeout in command block\n" );
1063 bad_rw_intr();
1064 goto repeat;
1066 SET_TIMER();
1067 return;
1069 panic("unknown ACSI command");
1074 /***********************************************************************
1076 * Misc functions: ioctl, open, release, check_change, ...
1078 ***********************************************************************/
1080 static int acsi_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1082 struct acsi_info_struct *aip = bdev->bd_disk->private_data;
1085 * Just fake some geometry here, it's nonsense anyway
1086 * To make it easy, use Adaptec's usual 64/32 mapping
1088 geo->heads = 64;
1089 geo->sectors = 32;
1090 geo->cylinders = aip->size >> 11;
1091 return 0;
1094 static int acsi_ioctl( struct inode *inode, struct file *file,
1095 unsigned int cmd, unsigned long arg )
1097 struct gendisk *disk = inode->i_bdev->bd_disk;
1098 struct acsi_info_struct *aip = disk->private_data;
1099 switch (cmd) {
1100 case SCSI_IOCTL_GET_IDLUN:
1101 /* SCSI compatible GET_IDLUN call to get target's ID and LUN number */
1102 put_user( aip->target | (aip->lun << 8),
1103 &((Scsi_Idlun *) arg)->dev_id );
1104 put_user( 0, &((Scsi_Idlun *) arg)->host_unique_id );
1105 return 0;
1106 default:
1107 return -EINVAL;
1113 * Open a device, check for read-only and lock the medium if it is
1114 * removable.
1116 * Changes by Martin Rogge, 9th Aug 1995:
1117 * Check whether check_disk_change (and therefore revalidate_acsidisk)
1118 * was successful. They fail when there is no medium in the drive.
1120 * The problem of media being changed during an operation can be
1121 * ignored because of the prevent_removal code.
1123 * Added check for the validity of the device number.
1127 static int acsi_open( struct inode * inode, struct file * filp )
1129 struct gendisk *disk = inode->i_bdev->bd_disk;
1130 struct acsi_info_struct *aip = disk->private_data;
1132 if (aip->access_count == 0 && aip->removable) {
1133 #if 0
1134 aip->changed = 1; /* safety first */
1135 #endif
1136 check_disk_change( inode->i_bdev );
1137 if (aip->changed) /* revalidate was not successful (no medium) */
1138 return -ENXIO;
1139 acsi_prevent_removal(aip, 1);
1141 aip->access_count++;
1143 if (filp && filp->f_mode) {
1144 check_disk_change( inode->i_bdev );
1145 if (filp->f_mode & 2) {
1146 if (aip->read_only) {
1147 acsi_release( inode, filp );
1148 return -EROFS;
1153 return 0;
1157 * Releasing a block device means we sync() it, so that it can safely
1158 * be forgotten about...
1161 static int acsi_release( struct inode * inode, struct file * file )
1163 struct gendisk *disk = inode->i_bdev->bd_disk;
1164 struct acsi_info_struct *aip = disk->private_data;
1165 if (--aip->access_count == 0 && aip->removable)
1166 acsi_prevent_removal(aip, 0);
1167 return( 0 );
1171 * Prevent or allow a media change for removable devices.
1174 static void acsi_prevent_removal(struct acsi_info_struct *aip, int flag)
1176 stdma_lock( NULL, NULL );
1178 CMDSET_TARG_LUN(pa_med_rem_cmd, aip->target, aip->lun);
1179 CMDSET_LEN( pa_med_rem_cmd, flag );
1181 if (acsicmd_nodma(pa_med_rem_cmd, 0) && acsi_wait_for_IRQ(3*HZ))
1182 acsi_getstatus();
1183 /* Do not report errors -- some devices may not know this command. */
1185 ENABLE_IRQ();
1186 stdma_release();
1189 static int acsi_media_change(struct gendisk *disk)
1191 struct acsi_info_struct *aip = disk->private_data;
1193 if (!aip->removable)
1194 return 0;
1196 if (aip->changed)
1197 /* We can be sure that the medium has been changed -- REQUEST
1198 * SENSE has reported this earlier.
1200 return 1;
1202 /* If the flag isn't set, make a test by reading block 0.
1203 * If errors happen, it seems to be better to say "changed"...
1205 stdma_lock( NULL, NULL );
1206 CMDSET_TARG_LUN(read_cmd, aip->target, aip->lun);
1207 CMDSET_BLOCK( read_cmd, 0 );
1208 CMDSET_LEN( read_cmd, 1 );
1209 if (acsicmd_dma(read_cmd, acsi_buffer, 1, 0, 0) &&
1210 acsi_wait_for_IRQ(3*HZ)) {
1211 if (acsi_getstatus()) {
1212 if (acsi_reqsense(acsi_buffer, aip->target, aip->lun)) {
1213 if (CARTRCH_STAT(aip, acsi_buffer))
1214 aip->changed = 1;
1216 else {
1217 printk( KERN_ERR "%s: REQUEST SENSE failed in test for "
1218 "medium change; assuming a change\n", disk->disk_name );
1219 aip->changed = 1;
1223 else {
1224 printk( KERN_ERR "%s: Test for medium changed timed out; "
1225 "assuming a change\n", disk->disk_name);
1226 aip->changed = 1;
1228 ENABLE_IRQ();
1229 stdma_release();
1231 /* Now, after reading a block, the changed status is surely valid. */
1232 return aip->changed;
1236 static int acsi_change_blk_size( int target, int lun)
1238 { int i;
1240 for (i=0; i<12; i++)
1241 acsi_buffer[i] = 0;
1243 acsi_buffer[3] = 8;
1244 acsi_buffer[10] = 2;
1245 CMDSET_TARG_LUN( modeselect_cmd, target, lun);
1247 if (!acsicmd_dma( modeselect_cmd, acsi_buffer, 1,1,0) ||
1248 !acsi_wait_for_IRQ( 3*HZ ) ||
1249 acsi_getstatus() != 0 ) {
1250 return(0);
1252 return(1);
1256 static int acsi_mode_sense( int target, int lun, SENSE_DATA *sd )
1259 int page;
1261 CMDSET_TARG_LUN( modesense_cmd, target, lun );
1262 for (page=0; page<4; page++) {
1263 modesense_cmd[2] = page;
1264 if (!acsicmd_dma( modesense_cmd, acsi_buffer, 1, 0, 0 ) ||
1265 !acsi_wait_for_IRQ( 3*HZ ) ||
1266 acsi_getstatus())
1267 continue;
1269 /* read twice to jump over the second 16-byte border! */
1270 udelay(300);
1271 if (acsi_wait_for_noIRQ( 20 ) &&
1272 acsicmd_nodma( modesense_cmd, 0 ) &&
1273 acsi_wait_for_IRQ( 3*HZ ) &&
1274 acsi_getstatus() == 0)
1275 break;
1277 if (page == 4) {
1278 return(0);
1281 dma_cache_maintenance( phys_acsi_buffer, sizeof(SENSE_DATA), 0 );
1282 *sd = *(SENSE_DATA *)acsi_buffer;
1284 /* Validity check, depending on type of data */
1286 switch( SENSE_TYPE(*sd) ) {
1288 case SENSE_TYPE_ATARI:
1289 if (CAPACITY(*sd) == 0)
1290 goto invalid_sense;
1291 break;
1293 case SENSE_TYPE_SCSI:
1294 if (sd->scsi.descriptor_size != 8)
1295 goto invalid_sense;
1296 break;
1298 case SENSE_TYPE_UNKNOWN:
1300 printk( KERN_ERR "ACSI target %d, lun %d: Cannot interpret "
1301 "sense data\n", target, lun );
1303 invalid_sense:
1305 #ifdef DEBUG
1306 { int i;
1307 printk( "Mode sense data for ACSI target %d, lun %d seem not valid:",
1308 target, lun );
1309 for( i = 0; i < sizeof(SENSE_DATA); ++i )
1310 printk( "%02x ", (unsigned char)acsi_buffer[i] );
1311 printk( "\n" );
1313 #endif
1314 return( 0 );
1317 return( 1 );
1322 /*******************************************************************
1324 * Initialization
1326 ********************************************************************/
1329 extern struct block_device_operations acsi_fops;
1331 static struct gendisk *acsi_gendisk[MAX_DEV];
1333 #define MAX_SCSI_DEVICE_CODE 10
1335 static const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
1337 "Direct-Access ",
1338 "Sequential-Access",
1339 "Printer ",
1340 "Processor ",
1341 "WORM ",
1342 "CD-ROM ",
1343 "Scanner ",
1344 "Optical Device ",
1345 "Medium Changer ",
1346 "Communications "
1349 static void print_inquiry(unsigned char *data)
1351 int i;
1353 printk(KERN_INFO " Vendor: ");
1354 for (i = 8; i < 16; i++)
1356 if (data[i] >= 0x20 && i < data[4] + 5)
1357 printk("%c", data[i]);
1358 else
1359 printk(" ");
1362 printk(" Model: ");
1363 for (i = 16; i < 32; i++)
1365 if (data[i] >= 0x20 && i < data[4] + 5)
1366 printk("%c", data[i]);
1367 else
1368 printk(" ");
1371 printk(" Rev: ");
1372 for (i = 32; i < 36; i++)
1374 if (data[i] >= 0x20 && i < data[4] + 5)
1375 printk("%c", data[i]);
1376 else
1377 printk(" ");
1380 printk("\n");
1382 i = data[0] & 0x1f;
1384 printk(KERN_INFO " Type: %s ", (i < MAX_SCSI_DEVICE_CODE
1385 ? scsi_device_types[i]
1386 : "Unknown "));
1387 printk(" ANSI SCSI revision: %02x", data[2] & 0x07);
1388 if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
1389 printk(" CCS\n");
1390 else
1391 printk("\n");
1396 * Changes by Martin Rogge, 9th Aug 1995:
1397 * acsi_devinit has been taken out of acsi_geninit, because it needs
1398 * to be called from revalidate_acsidisk. The result of request sense
1399 * is now checked for DRIVE NOT READY.
1401 * The structure *aip is only valid when acsi_devinit returns
1402 * DEV_SUPPORTED.
1406 #define DEV_NONE 0
1407 #define DEV_UNKNOWN 1
1408 #define DEV_SUPPORTED 2
1409 #define DEV_SLM 3
1411 static int acsi_devinit(struct acsi_info_struct *aip)
1413 int status, got_inquiry;
1414 SENSE_DATA sense;
1415 unsigned char reqsense, extsense;
1417 /*****************************************************************/
1418 /* Do a TEST UNIT READY command to test the presence of a device */
1419 /*****************************************************************/
1421 CMDSET_TARG_LUN(tur_cmd, aip->target, aip->lun);
1422 if (!acsicmd_nodma(tur_cmd, 0)) {
1423 /* timed out -> no device here */
1424 #ifdef DEBUG_DETECT
1425 printk("target %d lun %d: timeout\n", aip->target, aip->lun);
1426 #endif
1427 return DEV_NONE;
1430 /*************************/
1431 /* Read the ACSI status. */
1432 /*************************/
1434 status = acsi_getstatus();
1435 if (status) {
1436 if (status == 0x12) {
1437 /* The SLM printer should be the only device that
1438 * responds with the error code in the status byte. In
1439 * correct status bytes, bit 4 is never set.
1441 printk( KERN_INFO "Detected SLM printer at id %d lun %d\n",
1442 aip->target, aip->lun);
1443 return DEV_SLM;
1445 /* ignore CHECK CONDITION, since some devices send a
1446 UNIT ATTENTION */
1447 if ((status & 0x1e) != 0x2) {
1448 #ifdef DEBUG_DETECT
1449 printk("target %d lun %d: status %d\n",
1450 aip->target, aip->lun, status);
1451 #endif
1452 return DEV_UNKNOWN;
1456 /*******************************/
1457 /* Do a REQUEST SENSE command. */
1458 /*******************************/
1460 if (!acsi_reqsense(acsi_buffer, aip->target, aip->lun)) {
1461 printk( KERN_WARNING "acsi_reqsense failed\n");
1462 acsi_buffer[0] = 0;
1463 acsi_buffer[2] = UNIT_ATTENTION;
1465 reqsense = acsi_buffer[0];
1466 extsense = acsi_buffer[2] & 0xf;
1467 if (status) {
1468 if ((reqsense & 0x70) == 0x70) { /* extended sense */
1469 if (extsense != UNIT_ATTENTION &&
1470 extsense != NOT_READY) {
1471 #ifdef DEBUG_DETECT
1472 printk("target %d lun %d: extended sense %d\n",
1473 aip->target, aip->lun, extsense);
1474 #endif
1475 return DEV_UNKNOWN;
1478 else {
1479 if (reqsense & 0x7f) {
1480 #ifdef DEBUG_DETECT
1481 printk("target %d lun %d: sense %d\n",
1482 aip->target, aip->lun, reqsense);
1483 #endif
1484 return DEV_UNKNOWN;
1488 else
1489 if (reqsense == 0x4) { /* SH204 Bug workaround */
1490 #ifdef DEBUG_DETECT
1491 printk("target %d lun %d status=0 sense=4\n",
1492 aip->target, aip->lun);
1493 #endif
1494 return DEV_UNKNOWN;
1497 /***********************************************************/
1498 /* Do an INQUIRY command to get more infos on this device. */
1499 /***********************************************************/
1501 /* Assume default values */
1502 aip->removable = 1;
1503 aip->read_only = 0;
1504 aip->old_atari_disk = 0;
1505 aip->changed = (extsense == NOT_READY); /* medium inserted? */
1506 aip->size = DEFAULT_SIZE;
1507 got_inquiry = 0;
1508 /* Fake inquiry result for old atari disks */
1509 memcpy(acsi_buffer, "\000\000\001\000 Adaptec 40xx"
1510 " ", 40);
1511 CMDSET_TARG_LUN(inquiry_cmd, aip->target, aip->lun);
1512 if (acsicmd_dma(inquiry_cmd, acsi_buffer, 1, 0, 0) &&
1513 acsi_getstatus() == 0) {
1514 acsicmd_nodma(inquiry_cmd, 0);
1515 acsi_getstatus();
1516 dma_cache_maintenance( phys_acsi_buffer, 256, 0 );
1517 got_inquiry = 1;
1518 aip->removable = !!(acsi_buffer[1] & 0x80);
1520 if (aip->type == NONE) /* only at boot time */
1521 print_inquiry(acsi_buffer);
1522 switch(acsi_buffer[0]) {
1523 case TYPE_DISK:
1524 aip->type = HARDDISK;
1525 break;
1526 case TYPE_ROM:
1527 aip->type = CDROM;
1528 aip->read_only = 1;
1529 break;
1530 default:
1531 return DEV_UNKNOWN;
1533 /****************************/
1534 /* Do a MODE SENSE command. */
1535 /****************************/
1537 if (!acsi_mode_sense(aip->target, aip->lun, &sense)) {
1538 printk( KERN_WARNING "No mode sense data.\n" );
1539 return DEV_UNKNOWN;
1541 if ((SECTOR_SIZE(sense) != 512) &&
1542 ((aip->type != CDROM) ||
1543 !acsi_change_blk_size(aip->target, aip->lun) ||
1544 !acsi_mode_sense(aip->target, aip->lun, &sense) ||
1545 (SECTOR_SIZE(sense) != 512))) {
1546 printk( KERN_WARNING "Sector size != 512 not supported.\n" );
1547 return DEV_UNKNOWN;
1549 /* There are disks out there that claim to have 0 sectors... */
1550 if (CAPACITY(sense))
1551 aip->size = CAPACITY(sense); /* else keep DEFAULT_SIZE */
1552 if (!got_inquiry && SENSE_TYPE(sense) == SENSE_TYPE_ATARI) {
1553 /* If INQUIRY failed and the sense data suggest an old
1554 * Atari disk (SH20x, Megafile), the disk is not removable
1556 aip->removable = 0;
1557 aip->old_atari_disk = 1;
1560 /******************/
1561 /* We've done it. */
1562 /******************/
1564 return DEV_SUPPORTED;
1567 EXPORT_SYMBOL(acsi_delay_start);
1568 EXPORT_SYMBOL(acsi_delay_end);
1569 EXPORT_SYMBOL(acsi_wait_for_IRQ);
1570 EXPORT_SYMBOL(acsi_wait_for_noIRQ);
1571 EXPORT_SYMBOL(acsicmd_nodma);
1572 EXPORT_SYMBOL(acsi_getstatus);
1573 EXPORT_SYMBOL(acsi_buffer);
1574 EXPORT_SYMBOL(phys_acsi_buffer);
1576 #ifdef CONFIG_ATARI_SLM_MODULE
1577 void acsi_attach_SLMs( int (*attach_func)( int, int ) );
1579 EXPORT_SYMBOL(acsi_extstatus);
1580 EXPORT_SYMBOL(acsi_end_extstatus);
1581 EXPORT_SYMBOL(acsi_extcmd);
1582 EXPORT_SYMBOL(acsi_attach_SLMs);
1584 /* to remember IDs of SLM devices, SLM module is loaded later
1585 * (index is target#, contents is lun#, -1 means "no SLM") */
1586 int SLM_devices[8];
1587 #endif
1589 static struct block_device_operations acsi_fops = {
1590 .owner = THIS_MODULE,
1591 .open = acsi_open,
1592 .release = acsi_release,
1593 .ioctl = acsi_ioctl,
1594 .getgeo = acsi_getgeo,
1595 .media_changed = acsi_media_change,
1596 .revalidate_disk= acsi_revalidate,
1599 #ifdef CONFIG_ATARI_SLM_MODULE
1600 /* call attach_slm() for each device that is a printer; needed for init of SLM
1601 * driver as a module, since it's not yet present if acsi.c is inited and thus
1602 * the bus gets scanned. */
1603 void acsi_attach_SLMs( int (*attach_func)( int, int ) )
1605 int i, n = 0;
1607 for( i = 0; i < 8; ++i )
1608 if (SLM_devices[i] >= 0)
1609 n += (*attach_func)( i, SLM_devices[i] );
1610 printk( KERN_INFO "Found %d SLM printer(s) total.\n", n );
1612 #endif /* CONFIG_ATARI_SLM_MODULE */
1615 int acsi_init( void )
1617 int err = 0;
1618 int i, target, lun;
1619 struct acsi_info_struct *aip;
1620 #ifdef CONFIG_ATARI_SLM
1621 int n_slm = 0;
1622 #endif
1623 if (!MACH_IS_ATARI || !ATARIHW_PRESENT(ACSI))
1624 return 0;
1625 if (register_blkdev(ACSI_MAJOR, "ad")) {
1626 err = -EBUSY;
1627 goto out1;
1629 if (!(acsi_buffer =
1630 (char *)atari_stram_alloc(ACSI_BUFFER_SIZE, "acsi"))) {
1631 err = -ENOMEM;
1632 printk( KERN_ERR "Unable to get ACSI ST-Ram buffer.\n" );
1633 goto out2;
1635 phys_acsi_buffer = virt_to_phys( acsi_buffer );
1636 STramMask = ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000 : 0xff000000;
1638 acsi_queue = blk_init_queue(do_acsi_request, &acsi_lock);
1639 if (!acsi_queue) {
1640 err = -ENOMEM;
1641 goto out2a;
1643 #ifdef CONFIG_ATARI_SLM
1644 err = slm_init();
1645 #endif
1646 if (err)
1647 goto out3;
1649 printk( KERN_INFO "Probing ACSI devices:\n" );
1650 NDevices = 0;
1651 #ifdef CONFIG_ATARI_SLM_MODULE
1652 for( i = 0; i < 8; ++i )
1653 SLM_devices[i] = -1;
1654 #endif
1655 stdma_lock(NULL, NULL);
1657 for (target = 0; target < 8 && NDevices < MAX_DEV; ++target) {
1658 lun = 0;
1659 do {
1660 aip = &acsi_info[NDevices];
1661 aip->type = NONE;
1662 aip->target = target;
1663 aip->lun = lun;
1664 i = acsi_devinit(aip);
1665 switch (i) {
1666 case DEV_SUPPORTED:
1667 printk( KERN_INFO "Detected ");
1668 switch (aip->type) {
1669 case HARDDISK:
1670 printk("disk");
1671 break;
1672 case CDROM:
1673 printk("cdrom");
1674 break;
1675 default:
1677 printk(" ad%c at id %d lun %d ",
1678 'a' + NDevices, target, lun);
1679 if (aip->removable)
1680 printk("(removable) ");
1681 if (aip->read_only)
1682 printk("(read-only) ");
1683 if (aip->size == DEFAULT_SIZE)
1684 printk(" unkown size, using default ");
1685 printk("%ld MByte\n",
1686 (aip->size*512+1024*1024/2)/(1024*1024));
1687 NDevices++;
1688 break;
1689 case DEV_SLM:
1690 #ifdef CONFIG_ATARI_SLM
1691 n_slm += attach_slm( target, lun );
1692 break;
1693 #endif
1694 #ifdef CONFIG_ATARI_SLM_MODULE
1695 SLM_devices[target] = lun;
1696 break;
1697 #endif
1698 /* neither of the above: fall through to unknown device */
1699 case DEV_UNKNOWN:
1700 printk( KERN_INFO "Detected unsupported device at "
1701 "id %d lun %d\n", target, lun);
1702 break;
1705 #ifdef CONFIG_ACSI_MULTI_LUN
1706 while (i != DEV_NONE && ++lun < MAX_LUN);
1707 #else
1708 while (0);
1709 #endif
1712 /* reenable interrupt */
1713 ENABLE_IRQ();
1714 stdma_release();
1716 #ifndef CONFIG_ATARI_SLM
1717 printk( KERN_INFO "Found %d ACSI device(s) total.\n", NDevices );
1718 #else
1719 printk( KERN_INFO "Found %d ACSI device(s) and %d SLM printer(s) total.\n",
1720 NDevices, n_slm );
1721 #endif
1722 err = -ENOMEM;
1723 for( i = 0; i < NDevices; ++i ) {
1724 acsi_gendisk[i] = alloc_disk(16);
1725 if (!acsi_gendisk[i])
1726 goto out4;
1729 for( i = 0; i < NDevices; ++i ) {
1730 struct gendisk *disk = acsi_gendisk[i];
1731 sprintf(disk->disk_name, "ad%c", 'a'+i);
1732 aip = &acsi_info[NDevices];
1733 disk->major = ACSI_MAJOR;
1734 disk->first_minor = i << 4;
1735 if (acsi_info[i].type != HARDDISK)
1736 disk->minors = 1;
1737 disk->fops = &acsi_fops;
1738 disk->private_data = &acsi_info[i];
1739 set_capacity(disk, acsi_info[i].size);
1740 disk->queue = acsi_queue;
1741 add_disk(disk);
1743 return 0;
1744 out4:
1745 while (i--)
1746 put_disk(acsi_gendisk[i]);
1747 out3:
1748 blk_cleanup_queue(acsi_queue);
1749 out2a:
1750 atari_stram_free( acsi_buffer );
1751 out2:
1752 unregister_blkdev( ACSI_MAJOR, "ad" );
1753 out1:
1754 return err;
1758 #ifdef MODULE
1760 MODULE_LICENSE("GPL");
1762 int init_module(void)
1764 int err;
1766 if ((err = acsi_init()))
1767 return( err );
1768 printk( KERN_INFO "ACSI driver loaded as module.\n");
1769 return( 0 );
1772 void cleanup_module(void)
1774 int i;
1775 del_timer( &acsi_timer );
1776 blk_cleanup_queue(acsi_queue);
1777 atari_stram_free( acsi_buffer );
1779 if (unregister_blkdev( ACSI_MAJOR, "ad" ) != 0)
1780 printk( KERN_ERR "acsi: cleanup_module failed\n");
1782 for (i = 0; i < NDevices; i++) {
1783 del_gendisk(acsi_gendisk[i]);
1784 put_disk(acsi_gendisk[i]);
1787 #endif
1790 * This routine is called to flush all partitions and partition tables
1791 * for a changed scsi disk, and then re-read the new partition table.
1792 * If we are revalidating a disk because of a media change, then we
1793 * enter with usage == 0. If we are using an ioctl, we automatically have
1794 * usage == 1 (we need an open channel to use an ioctl :-), so this
1795 * is our limit.
1797 * Changes by Martin Rogge, 9th Aug 1995:
1798 * got cd-roms to work by calling acsi_devinit. There are only two problems:
1799 * First, if there is no medium inserted, the status will remain "changed".
1800 * That is no problem at all, but our design of three-valued logic (medium
1801 * changed, medium not changed, no medium inserted).
1802 * Secondly the check could fail completely and the drive could deliver
1803 * nonsensical data, which could mess up the acsi_info[] structure. In
1804 * that case we try to make the entry safe.
1808 static int acsi_revalidate(struct gendisk *disk)
1810 struct acsi_info_struct *aip = disk->private_data;
1811 stdma_lock( NULL, NULL );
1812 if (acsi_devinit(aip) != DEV_SUPPORTED) {
1813 printk( KERN_ERR "ACSI: revalidate failed for target %d lun %d\n",
1814 aip->target, aip->lun);
1815 aip->size = 0;
1816 aip->read_only = 1;
1817 aip->removable = 1;
1818 aip->changed = 1; /* next acsi_open will try again... */
1821 ENABLE_IRQ();
1822 stdma_release();
1823 set_capacity(disk, aip->size);
1824 return 0;