Merge branch 'hwmon-for-linus' of git://jdelvare.pck.nerim.net/jdelvare-2.6
[pandora-kernel.git] / drivers / block / acsi.c
1 /*
2  * acsi.c -- Device driver for Atari ACSI hard disks
3  *
4  * Copyright 1994 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
5  *
6  * Some parts are based on hd.c by Linus Torvalds
7  *
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.
11  *
12  */
13
14 /*
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 :-(
22  */
23
24 /*
25  * General notes:
26  *
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.
43  *
44  */
45
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>
64
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>
74
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)
79
80 #define DEBUG
81 #undef DEBUG_DETECT
82 #undef NO_WRITE
83
84 #define MAX_ERRORS              8       /* Max read/write errors/sector */
85 #define MAX_LUN                         8       /* Max LUNs per target */
86 #define MAX_DEV                         16
87
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)
93
94 #define ACSI_BUFFER_ORDER \
95         (ATARIHW_PRESENT(EXTD_DMA) ? \
96          ACSI_BUFFER_MINSIZE_ORDER : \
97          ACSI_BUFFER_SIZE_ORDER)
98
99 #define ACSI_TIMEOUT            (4*HZ)
100
101 /* minimum delay between two commands */
102
103 #define COMMAND_DELAY 500
104
105 typedef enum {
106         NONE, HARDDISK, CDROM
107 } ACSI_TYPE;
108
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];
120
121 /*
122  *      SENSE KEYS
123  */
124
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
138
139
140 /*
141  *      DEVICE TYPES
142  */
143
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
150
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.
154  */
155
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;
180
181 #define ATARI_CAPACITY(sd) \
182         (((int)((sd).cylinders_h<<8)|(sd).cylinders_l) * \
183          (sd).heads * (sd).sectors)
184
185
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;
199
200 #define SCSI_CAPACITY(sd)       ((sd).block_count & 0xffffff)
201
202
203 typedef union {
204         ATARI_SENSE_DATA        atari;
205         SCSI_SENSE_DATA         scsi;
206 } SENSE_DATA;
207
208 #define SENSE_TYPE_UNKNOWN      0
209 #define SENSE_TYPE_ATARI        1
210 #define SENSE_TYPE_SCSI         2
211
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)
218          
219 #define CAPACITY(sd)                                                    \
220         (SENSE_TYPE(sd) == SENSE_TYPE_ATARI ?           \
221          ATARI_CAPACITY((sd).atari) :                           \
222          SCSI_CAPACITY((sd).scsi))
223
224 #define SECTOR_SIZE(sd)                                                 \
225         (SENSE_TYPE(sd) == SENSE_TYPE_ATARI ?           \
226          (sd).atari.sector_size :                                       \
227          (sd).scsi.sector_size & 0xffffff)
228
229 /* Default size if capacity cannot be determined (1 GByte) */
230 #define DEFAULT_SIZE    0x1fffff
231
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)))                                 \
238
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;
243
244 static int NDevices;
245
246 static int                              CurrentNReq;
247 static int                              CurrentNSect;
248 static char                             *CurrentBuffer;
249
250 static DEFINE_SPINLOCK(acsi_lock);
251
252
253 #define SET_TIMER()     mod_timer(&acsi_timer, jiffies + ACSI_TIMEOUT)
254 #define CLEAR_TIMER()   del_timer(&acsi_timer)
255
256 static unsigned long    STramMask;
257 #define STRAM_ADDR(a)   (((a) & STramMask) == 0)
258
259
260
261 /* ACSI commands */
262
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 };
271
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)
277
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)
285
286 #define CMDSET_LEN(cmd,len)                                             \
287         do {                                                                            \
288                 cmd[4] = (len);                                                 \
289         } while(0)
290
291 /* ACSI errors (from REQUEST SENSE); There are two tables, one for the
292  * old Atari disks and one for SCSI on ACSI disks.
293  */
294
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" }
324 },
325
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" }
338 };
339
340
341
342 /***************************** Prototypes *****************************/
343
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);
367
368 /************************* End of Prototypes **************************/
369
370
371 DEFINE_TIMER(acsi_timer, acsi_times_out, 0, 0);
372
373
374 #ifdef CONFIG_ATARI_SLM
375
376 extern int attach_slm( int target, int lun );
377 extern int slm_init( void );
378
379 #endif
380
381
382 \f
383 /***********************************************************************
384  *
385  *   ACSI primitives
386  *
387  **********************************************************************/
388
389
390 /*
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...
399  */
400
401 #define INT_LEVEL                                                                                                       \
402         ({      unsigned __sr;                                                                                          \
403                 __asm__ __volatile__ ( "movew   %/sr,%0" : "=dm" (__sr) );      \
404                 (__sr >> 8) & 7;                                                                                        \
405         })
406
407 int acsi_wait_for_IRQ( unsigned timeout )
408
409 {
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 );
414         }
415         else {
416                 long tries = loops_per_jiffy / 8 * timeout;
417                 while( --tries >= 0 )
418                         if (!(mfp.par_dt_reg & 0x20)) return( 1 );
419         }               
420         return( 0 ); /* timeout! */
421 }
422
423
424 int acsi_wait_for_noIRQ( unsigned timeout )
425
426 {
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 );
431         }
432         else {
433                 long tries = loops_per_jiffy * timeout / 8;
434                 while( tries-- >= 0 )
435                         if (mfp.par_dt_reg & 0x20) return( 1 );
436         }               
437         return( 0 ); /* timeout! */
438 }
439
440 static struct timeval start_time;
441
442 void
443 acsi_delay_start(void)
444 {
445         do_gettimeofday(&start_time);
446 }
447
448 /* wait from acsi_delay_start to now usec (<1E6) usec */
449
450 void
451 acsi_delay_end(long usec)
452 {
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);
465 }
466
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.
473  */
474
475 static int acsicmd_dma( const char *cmd, char *buffer, int blocks, int rwflag, int enable)
476
477 {       unsigned long   flags, paddr;
478         int                             i;
479
480 #ifdef NO_WRITE
481         if (rwflag || *cmd == 0x0a) {
482                 printk( "ACSI: Write commands disabled!\n" );
483                 return( 0 );
484         }
485 #endif
486         
487         rwflag = rwflag ? 0x100 : 0;
488         paddr = virt_to_phys( buffer );
489
490         acsi_delay_end(COMMAND_DELAY);
491         DISABLE_IRQ();
492
493         local_irq_save(flags);
494         /* Low on A1 */
495         dma_wd.dma_mode_status = 0x88 | rwflag;
496         MFPDELAY();
497
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);
511
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 */
517         }
518
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();
524
525         /* How many sectors for DMA */
526         dma_wd.fdc_acces_seccount = blocks;
527         MFPDELAY();
528         
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);
536
537         return( 1 );
538 }
539
540
541 /*
542  * acsicmd_nodma() sends an ACSI command that requires no DMA.
543  */
544
545 int acsicmd_nodma( const char *cmd, int enable)
546
547 {       int     i;
548
549         acsi_delay_end(COMMAND_DELAY);
550         DISABLE_IRQ();
551
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 */
558
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 */
564         }
565
566         /* send last command byte */
567         DMA_LONG_WRITE( *cmd++, 0x0a );
568         if (enable)
569                 ENABLE_IRQ();
570         udelay(80);
571         
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!
575          */
576 }
577
578
579 static int acsi_reqsense( char *buffer, int targ, int lun)
580
581 {
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 );
596         
597         return( 1 );
598 }       
599
600
601 /*
602  * ACSI status phase: get the status byte from the bus
603  *
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.
607  */
608
609 int acsi_getstatus( void )
610
611 {       int     status;
612
613         DISABLE_IRQ();
614         for(;;) {
615                 if (!acsi_wait_for_IRQ( 100 )) {
616                         acsi_delay_start();
617                         return( -1 );
618                 }
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 );
628         }
629         dma_wd.dma_mode_status = 0x80;
630         udelay(40);
631         acsi_wait_for_noIRQ( 20 );
632
633         acsi_delay_start();
634         return( status & 0x1f ); /* mask of the device# */
635 }
636
637
638 #if (defined(CONFIG_ATARI_SLM) || defined(CONFIG_ATARI_SLM_MODULE))
639
640 /* Receive data in an extended status phase. Needed by SLM printer. */
641
642 int acsi_extstatus( char *buffer, int cnt )
643
644 {       int     status;
645
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);
656         }
657         return( 1 );
658 }
659
660
661 /* Finish an extended status phase */
662
663 void acsi_end_extstatus( void )
664
665 {
666         dma_wd.dma_mode_status = 0x80;
667         udelay(40);
668         acsi_wait_for_noIRQ( 20 );
669         acsi_delay_start();
670 }
671
672
673 /* Send data in an extended command phase */
674
675 int acsi_extcmd( unsigned char *buffer, int cnt )
676
677 {
678         while( cnt-- > 0 ) {
679                 DMA_LONG_WRITE( *buffer++, 0x8a );
680                 udelay(20);
681                 if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
682         }
683         return( 1 );
684 }
685
686 #endif
687
688
689 static void acsi_print_error(const unsigned char *errblk, struct acsi_info_struct *aip)
690
691 {       int atari_err, i, errcode;
692         struct acsi_error *arr;
693
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;
702         
703         printk( KERN_ERR "ACSI error 0x%02x", errcode );
704
705         if (errblk[0] & 0x80)
706                 printk( " for sector %d",
707                                 ((errblk[1] & 0x1f) << 16) |
708                                 (errblk[2] << 8) | errblk[0] );
709
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);
713         
714         for( --i; i >= 0; --i )
715                 if (arr[i].code == errcode) break;
716         if (i >= 0)
717                 printk( ": %s\n", arr[i].text );
718 }
719
720 /*******************************************************************
721  *
722  * ACSI interrupt routine
723  *   Test, if this is a ACSI interrupt and call the irq handler
724  *   Otherwise ignore this interrupt.
725  *
726  *******************************************************************/
727
728 static irqreturn_t acsi_interrupt(int irq, void *data )
729
730 {       void (*acsi_irq_handler)(void) = do_acsi;
731
732         do_acsi = NULL;
733         CLEAR_TIMER();
734
735         if (!acsi_irq_handler)
736                 acsi_irq_handler = unexpected_acsi_interrupt;
737         acsi_irq_handler();
738         return IRQ_HANDLED;
739 }
740
741
742 /******************************************************************
743  *
744  * The Interrupt handlers
745  *
746  *******************************************************************/
747
748
749 static void unexpected_acsi_interrupt( void )
750
751 {
752         printk( KERN_WARNING "Unexpected ACSI interrupt\n" );
753 }
754
755
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 :-(
759  */
760
761 static void bad_rw_intr( void )
762
763 {
764         if (!CURRENT)
765                 return;
766
767         if (++CURRENT->errors >= MAX_ERRORS)
768                 end_request(CURRENT, 0);
769         /* Otherwise just retry */
770 }
771
772
773 static void read_intr( void )
774
775 {       int             status;
776         
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;
788                 }
789                 ENABLE_IRQ();
790                 bad_rw_intr();
791                 redo_acsi_request();
792                 return;
793         }
794
795         dma_cache_maintenance( virt_to_phys(CurrentBuffer), CurrentNSect*512, 0 );
796         if (CurrentBuffer == acsi_buffer)
797                 copy_from_acsibuffer();
798
799         do_end_requests();
800         redo_acsi_request();
801 }
802
803
804 static void write_intr(void)
805
806 {       int     status;
807
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;
819                 }
820                 bad_rw_intr();
821                 redo_acsi_request();
822                 return;
823         }
824
825         do_end_requests();
826         redo_acsi_request();
827 }
828
829
830 static void acsi_times_out( unsigned long dummy )
831
832 {
833         DISABLE_IRQ();
834         if (!do_acsi) return;
835
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);
845         }
846
847         redo_acsi_request();
848 }
849
850
851 \f
852 /***********************************************************************
853  *
854  *  Scatter-gather utility functions
855  *
856  ***********************************************************************/
857
858
859 static void copy_to_acsibuffer( void )
860
861 {       int                                     i;
862         char                            *src, *dst;
863         struct buffer_head      *bh;
864         
865         src = CURRENT->buffer;
866         dst = acsi_buffer;
867         bh = CURRENT->bh;
868
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;
877                 }
878 }
879
880
881 static void copy_from_acsibuffer( void )
882
883 {       int                                     i;
884         char                            *src, *dst;
885         struct buffer_head      *bh;
886         
887         dst = CURRENT->buffer;
888         src = acsi_buffer;
889         bh = CURRENT->bh;
890
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;
899                 }
900 }
901
902
903 static void do_end_requests( void )
904
905 {       int             i, n;
906
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);
913         }
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);
921                 }
922         }
923 }
924
925
926
927 \f
928 /***********************************************************************
929  *
930  *  do_acsi_request and friends
931  *
932  ***********************************************************************/
933
934 static void do_acsi_request( request_queue_t * q )
935
936 {
937         stdma_lock( acsi_interrupt, NULL );
938         redo_acsi_request();
939 }
940
941
942 static void redo_acsi_request( void )
943 {
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;
950
951   repeat:
952         CLEAR_TIMER();
953
954         if (do_acsi)
955                 return;
956
957         if (!CURRENT) {
958                 do_acsi = NULL;
959                 ENABLE_IRQ();
960                 stdma_release();
961                 return;
962         }
963
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");
969         }
970
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;
981         }
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;
987         }
988         
989         target = aip->target;
990         lun    = aip->lun;
991
992         /* Find out how many sectors should be transferred from/to
993          * consecutive buffers and thus can be done with a single command.
994          */
995         buffer      = CURRENT->buffer;
996         pbuffer     = virt_to_phys(buffer);
997         nsect       = CURRENT->current_nr_sectors;
998         CurrentNReq = 1;
999
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.
1004                          */
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;
1010                         }
1011                         buffer = acsi_buffer;
1012                         pbuffer = phys_acsi_buffer;
1013                 }
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;
1024                         }
1025                 }
1026         }
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;
1033                 }
1034         }
1035         CurrentBuffer = buffer;
1036         CurrentNSect  = nsect;
1037
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;
1051                 }
1052                 SET_TIMER();
1053                 return;
1054         }
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;
1065                 }
1066                 SET_TIMER();
1067                 return;
1068         }
1069         panic("unknown ACSI command");
1070 }
1071
1072
1073 \f
1074 /***********************************************************************
1075  *
1076  *  Misc functions: ioctl, open, release, check_change, ...
1077  *
1078  ***********************************************************************/
1079
1080 static int acsi_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1081 {
1082         struct acsi_info_struct *aip = bdev->bd_disk->private_data;
1083
1084         /*
1085          * Just fake some geometry here, it's nonsense anyway
1086          * To make it easy, use Adaptec's usual 64/32 mapping
1087          */
1088         geo->heads = 64;
1089         geo->sectors = 32;
1090         geo->cylinders = aip->size >> 11;
1091         return 0;
1092 }
1093
1094 static int acsi_ioctl( struct inode *inode, struct file *file,
1095                                            unsigned int cmd, unsigned long arg )
1096 {
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;
1108         }
1109 }
1110
1111
1112 /*
1113  * Open a device, check for read-only and lock the medium if it is
1114  * removable.
1115  *
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.
1119  *
1120  * The problem of media being changed during an operation can be 
1121  * ignored because of the prevent_removal code.
1122  *
1123  * Added check for the validity of the device number.
1124  *
1125  */
1126
1127 static int acsi_open( struct inode * inode, struct file * filp )
1128 {
1129         struct gendisk *disk = inode->i_bdev->bd_disk;
1130         struct acsi_info_struct *aip = disk->private_data;
1131
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);
1140         }
1141         aip->access_count++;
1142
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;
1149                         }
1150                 }
1151         }
1152
1153         return 0;
1154 }
1155
1156 /*
1157  * Releasing a block device means we sync() it, so that it can safely
1158  * be forgotten about...
1159  */
1160
1161 static int acsi_release( struct inode * inode, struct file * file )
1162 {
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 );
1168 }
1169
1170 /*
1171  * Prevent or allow a media change for removable devices.
1172  */
1173
1174 static void acsi_prevent_removal(struct acsi_info_struct *aip, int flag)
1175 {
1176         stdma_lock( NULL, NULL );
1177         
1178         CMDSET_TARG_LUN(pa_med_rem_cmd, aip->target, aip->lun);
1179         CMDSET_LEN( pa_med_rem_cmd, flag );
1180         
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. */
1184
1185         ENABLE_IRQ();
1186         stdma_release();
1187 }
1188
1189 static int acsi_media_change(struct gendisk *disk)
1190 {
1191         struct acsi_info_struct *aip = disk->private_data;
1192
1193         if (!aip->removable) 
1194                 return 0;
1195
1196         if (aip->changed)
1197                 /* We can be sure that the medium has been changed -- REQUEST
1198                  * SENSE has reported this earlier.
1199                  */
1200                 return 1;
1201
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"...
1204          */
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;
1215                         }
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;
1220                         }
1221                 }
1222         }
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;
1227         }
1228         ENABLE_IRQ();
1229         stdma_release();
1230
1231         /* Now, after reading a block, the changed status is surely valid. */
1232         return aip->changed;
1233 }
1234
1235
1236 static int acsi_change_blk_size( int target, int lun)
1237
1238 {       int i;
1239
1240         for (i=0; i<12; i++)
1241                 acsi_buffer[i] = 0;
1242
1243         acsi_buffer[3] = 8;
1244         acsi_buffer[10] = 2;
1245         CMDSET_TARG_LUN( modeselect_cmd, target, lun);
1246
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);
1251         }
1252         return(1);
1253 }
1254
1255
1256 static int acsi_mode_sense( int target, int lun, SENSE_DATA *sd )
1257
1258 {
1259         int page;
1260
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;
1268
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;
1276         }
1277         if (page == 4) {
1278                 return(0);
1279         }
1280
1281         dma_cache_maintenance( phys_acsi_buffer, sizeof(SENSE_DATA), 0 );
1282         *sd = *(SENSE_DATA *)acsi_buffer;
1283
1284         /* Validity check, depending on type of data */
1285         
1286         switch( SENSE_TYPE(*sd) ) {
1287
1288           case SENSE_TYPE_ATARI:
1289                 if (CAPACITY(*sd) == 0)
1290                         goto invalid_sense;
1291                 break;
1292
1293           case SENSE_TYPE_SCSI:
1294                 if (sd->scsi.descriptor_size != 8)
1295                         goto invalid_sense;
1296                 break;
1297
1298           case SENSE_TYPE_UNKNOWN:
1299
1300                 printk( KERN_ERR "ACSI target %d, lun %d: Cannot interpret "
1301                                 "sense data\n", target, lun ); 
1302                 
1303           invalid_sense:
1304
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" );
1312                 }
1313 #endif
1314                 return( 0 );
1315         }
1316                 
1317         return( 1 );
1318 }
1319
1320
1321
1322 /*******************************************************************
1323  *
1324  *  Initialization
1325  *
1326  ********************************************************************/
1327
1328
1329 extern struct block_device_operations acsi_fops;
1330
1331 static struct gendisk *acsi_gendisk[MAX_DEV];
1332
1333 #define MAX_SCSI_DEVICE_CODE 10
1334
1335 static const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
1336 {
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   "
1347 };
1348
1349 static void print_inquiry(unsigned char *data)
1350 {
1351         int i;
1352
1353         printk(KERN_INFO "  Vendor: ");
1354         for (i = 8; i < 16; i++)
1355                 {
1356                 if (data[i] >= 0x20 && i < data[4] + 5)
1357                         printk("%c", data[i]);
1358                 else
1359                         printk(" ");
1360                 }
1361
1362         printk("  Model: ");
1363         for (i = 16; i < 32; i++)
1364                 {
1365                 if (data[i] >= 0x20 && i < data[4] + 5)
1366                         printk("%c", data[i]);
1367                 else
1368                         printk(" ");
1369                 }
1370
1371         printk("  Rev: ");
1372         for (i = 32; i < 36; i++)
1373                 {
1374                 if (data[i] >= 0x20 && i < data[4] + 5)
1375                         printk("%c", data[i]);
1376                 else
1377                         printk(" ");
1378                 }
1379
1380         printk("\n");
1381
1382         i = data[0] & 0x1f;
1383
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");
1392 }
1393
1394
1395 /* 
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.
1400  *
1401  * The structure *aip is only valid when acsi_devinit returns 
1402  * DEV_SUPPORTED. 
1403  *
1404  */
1405         
1406 #define DEV_NONE        0
1407 #define DEV_UNKNOWN     1
1408 #define DEV_SUPPORTED   2
1409 #define DEV_SLM         3
1410
1411 static int acsi_devinit(struct acsi_info_struct *aip)
1412 {
1413         int status, got_inquiry;
1414         SENSE_DATA sense;
1415         unsigned char reqsense, extsense;
1416
1417         /*****************************************************************/
1418         /* Do a TEST UNIT READY command to test the presence of a device */
1419         /*****************************************************************/
1420
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;
1428         }
1429                 
1430         /*************************/
1431         /* Read the ACSI status. */
1432         /*************************/
1433
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.
1440                          */
1441                         printk( KERN_INFO "Detected SLM printer at id %d lun %d\n",
1442                                aip->target, aip->lun);
1443                         return DEV_SLM;
1444                 }
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;
1453                 }
1454         }
1455
1456         /*******************************/
1457         /* Do a REQUEST SENSE command. */
1458         /*******************************/
1459
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;
1464         }
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;
1476                         }
1477                 }
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;
1485                         }
1486                 }
1487         }
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;
1495                 }
1496
1497         /***********************************************************/
1498         /* Do an INQUIRY command to get more infos on this device. */
1499         /***********************************************************/
1500
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);
1519         }
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;
1532         }
1533         /****************************/
1534         /* Do a MODE SENSE command. */
1535         /****************************/
1536
1537         if (!acsi_mode_sense(aip->target, aip->lun, &sense)) {
1538                 printk( KERN_WARNING "No mode sense data.\n" );
1539                 return DEV_UNKNOWN;
1540         }
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;
1548         }
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
1555                  */
1556                 aip->removable = 0;
1557                 aip->old_atari_disk = 1;
1558         }
1559         
1560         /******************/
1561         /* We've done it. */
1562         /******************/
1563         
1564         return DEV_SUPPORTED;
1565 }
1566
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);
1575
1576 #ifdef CONFIG_ATARI_SLM_MODULE
1577 void acsi_attach_SLMs( int (*attach_func)( int, int ) );
1578
1579 EXPORT_SYMBOL(acsi_extstatus);
1580 EXPORT_SYMBOL(acsi_end_extstatus);
1581 EXPORT_SYMBOL(acsi_extcmd);
1582 EXPORT_SYMBOL(acsi_attach_SLMs);
1583
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
1588
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,
1597 };
1598
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 ) )
1604 {
1605         int i, n = 0;
1606
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 );
1611 }
1612 #endif /* CONFIG_ATARI_SLM_MODULE */
1613
1614
1615 int acsi_init( void )
1616 {
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;
1628         }
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;
1634         }
1635         phys_acsi_buffer = virt_to_phys( acsi_buffer );
1636         STramMask = ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000 : 0xff000000;
1637         
1638         acsi_queue = blk_init_queue(do_acsi_request, &acsi_lock);
1639         if (!acsi_queue) {
1640                 err = -ENOMEM;
1641                 goto out2a;
1642         }
1643 #ifdef CONFIG_ATARI_SLM
1644         err = slm_init();
1645 #endif
1646         if (err)
1647                 goto out3;
1648
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);
1656
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:
1676                                 }
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;
1703                         }
1704                 }
1705 #ifdef CONFIG_ACSI_MULTI_LUN
1706                 while (i != DEV_NONE && ++lun < MAX_LUN);
1707 #else
1708                 while (0);
1709 #endif
1710         }
1711
1712         /* reenable interrupt */
1713         ENABLE_IRQ();
1714         stdma_release();
1715
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;
1727         }
1728
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);
1742         }
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;
1755 }
1756
1757
1758 #ifdef MODULE
1759
1760 MODULE_LICENSE("GPL");
1761
1762 int init_module(void)
1763 {
1764         int err;
1765
1766         if ((err = acsi_init()))
1767                 return( err );
1768         printk( KERN_INFO "ACSI driver loaded as module.\n");
1769         return( 0 );
1770 }
1771
1772 void cleanup_module(void)
1773 {
1774         int i;
1775         del_timer( &acsi_timer );
1776         blk_cleanup_queue(acsi_queue);
1777         atari_stram_free( acsi_buffer );
1778
1779         if (unregister_blkdev( ACSI_MAJOR, "ad" ) != 0)
1780                 printk( KERN_ERR "acsi: cleanup_module failed\n");
1781
1782         for (i = 0; i < NDevices; i++) {
1783                 del_gendisk(acsi_gendisk[i]);
1784                 put_disk(acsi_gendisk[i]);
1785         }
1786 }
1787 #endif
1788
1789 /*
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.
1796  *
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.
1805  *
1806  */
1807
1808 static int acsi_revalidate(struct gendisk *disk)
1809 {
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... */
1819         }
1820
1821         ENABLE_IRQ();
1822         stdma_release();
1823         set_capacity(disk, aip->size);
1824         return 0;
1825 }