Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.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/config.h>
47 #include <linux/module.h>
48 #include <linux/errno.h>
49 #include <linux/signal.h>
50 #include <linux/sched.h>
51 #include <linux/timer.h>
52 #include <linux/fs.h>
53 #include <linux/kernel.h>
54 #include <linux/genhd.h>
55 #include <linux/delay.h>
56 #include <linux/mm.h>
57 #include <linux/major.h>
58 #include <linux/slab.h>
59 #include <linux/interrupt.h>
60 #include <scsi/scsi.h> /* for SCSI_IOCTL_GET_IDLUN */
61 #include <scsi/scsi_ioctl.h>
62 #include <linux/hdreg.h> /* for HDIO_GETGEO */
63 #include <linux/blkpg.h>
64 #include <linux/buffer_head.h>
65 #include <linux/blkdev.h>
66
67 #include <asm/setup.h>
68 #include <asm/pgtable.h>
69 #include <asm/system.h>
70 #include <asm/uaccess.h>
71 #include <asm/atarihw.h>
72 #include <asm/atariints.h>
73 #include <asm/atari_acsi.h>
74 #include <asm/atari_stdma.h>
75 #include <asm/atari_stram.h>
76
77 static void (*do_acsi)(void) = NULL;
78 static struct request_queue *acsi_queue;
79 #define QUEUE (acsi_queue)
80 #define CURRENT elv_next_request(acsi_queue)
81
82 #define DEBUG
83 #undef DEBUG_DETECT
84 #undef NO_WRITE
85
86 #define MAX_ERRORS              8       /* Max read/write errors/sector */
87 #define MAX_LUN                         8       /* Max LUNs per target */
88 #define MAX_DEV                         16
89
90 #define ACSI_BUFFER_SIZE                        (16*1024) /* "normal" ACSI buffer size */
91 #define ACSI_BUFFER_MINSIZE                     (2048)    /* min. buf size if ext. DMA */
92 #define ACSI_BUFFER_SIZE_ORDER          2                 /* order size for above */
93 #define ACSI_BUFFER_MINSIZE_ORDER       0                 /* order size for above */
94 #define ACSI_BUFFER_SECTORS     (ACSI_BUFFER_SIZE/512)
95
96 #define ACSI_BUFFER_ORDER \
97         (ATARIHW_PRESENT(EXTD_DMA) ? \
98          ACSI_BUFFER_MINSIZE_ORDER : \
99          ACSI_BUFFER_SIZE_ORDER)
100
101 #define ACSI_TIMEOUT            (4*HZ)
102
103 /* minimum delay between two commands */
104
105 #define COMMAND_DELAY 500
106
107 typedef enum {
108         NONE, HARDDISK, CDROM
109 } ACSI_TYPE;
110
111 struct acsi_info_struct {
112         ACSI_TYPE               type;                   /* type of device */
113         unsigned                target;                 /* target number */
114         unsigned                lun;                    /* LUN in target controller */
115         unsigned                removable : 1;  /* Flag for removable media */
116         unsigned                read_only : 1;  /* Flag for read only devices */
117         unsigned                old_atari_disk : 1; /* Is an old Atari disk       */
118         unsigned                changed : 1;    /* Medium has been changed */
119         unsigned long   size;                   /* #blocks */
120         int access_count;
121 } acsi_info[MAX_DEV];
122
123 /*
124  *      SENSE KEYS
125  */
126
127 #define NO_SENSE                0x00
128 #define RECOVERED_ERROR         0x01
129 #define NOT_READY               0x02
130 #define MEDIUM_ERROR            0x03
131 #define HARDWARE_ERROR          0x04
132 #define ILLEGAL_REQUEST         0x05
133 #define UNIT_ATTENTION          0x06
134 #define DATA_PROTECT            0x07
135 #define BLANK_CHECK             0x08
136 #define COPY_ABORTED            0x0a
137 #define ABORTED_COMMAND         0x0b
138 #define VOLUME_OVERFLOW         0x0d
139 #define MISCOMPARE              0x0e
140
141
142 /*
143  *      DEVICE TYPES
144  */
145
146 #define TYPE_DISK       0x00
147 #define TYPE_TAPE       0x01
148 #define TYPE_WORM       0x04
149 #define TYPE_ROM        0x05
150 #define TYPE_MOD        0x07
151 #define TYPE_NO_LUN     0x7f
152
153 /* The data returned by MODE SENSE differ between the old Atari
154  * hard disks and SCSI disks connected to ACSI. In the following, both
155  * formats are defined and some macros to operate on them potably.
156  */
157
158 typedef struct {
159         unsigned long   dummy[2];
160         unsigned long   sector_size;
161         unsigned char   format_code;
162 #define ATARI_SENSE_FORMAT_FIX  1       
163 #define ATARI_SENSE_FORMAT_CHNG 2
164         unsigned char   cylinders_h;
165         unsigned char   cylinders_l;
166         unsigned char   heads;
167         unsigned char   reduced_h;
168         unsigned char   reduced_l;
169         unsigned char   precomp_h;
170         unsigned char   precomp_l;
171         unsigned char   landing_zone;
172         unsigned char   steprate;
173         unsigned char   type;
174 #define ATARI_SENSE_TYPE_FIXCHNG_MASK           4
175 #define ATARI_SENSE_TYPE_SOFTHARD_MASK          8
176 #define ATARI_SENSE_TYPE_FIX                            4
177 #define ATARI_SENSE_TYPE_CHNG                           0
178 #define ATARI_SENSE_TYPE_SOFT                           0
179 #define ATARI_SENSE_TYPE_HARD                           8
180         unsigned char   sectors;
181 } ATARI_SENSE_DATA;
182
183 #define ATARI_CAPACITY(sd) \
184         (((int)((sd).cylinders_h<<8)|(sd).cylinders_l) * \
185          (sd).heads * (sd).sectors)
186
187
188 typedef struct {
189         unsigned char   dummy1;
190         unsigned char   medium_type;
191         unsigned char   dummy2;
192         unsigned char   descriptor_size;
193         unsigned long   block_count;
194         unsigned long   sector_size;
195         /* Page 0 data */
196         unsigned char   page_code;
197         unsigned char   page_size;
198         unsigned char   page_flags;
199         unsigned char   qualifier;
200 } SCSI_SENSE_DATA;
201
202 #define SCSI_CAPACITY(sd)       ((sd).block_count & 0xffffff)
203
204
205 typedef union {
206         ATARI_SENSE_DATA        atari;
207         SCSI_SENSE_DATA         scsi;
208 } SENSE_DATA;
209
210 #define SENSE_TYPE_UNKNOWN      0
211 #define SENSE_TYPE_ATARI        1
212 #define SENSE_TYPE_SCSI         2
213
214 #define SENSE_TYPE(sd)                                                                          \
215         (((sd).atari.dummy[0] == 8 &&                                                   \
216           ((sd).atari.format_code == 1 ||                                               \
217            (sd).atari.format_code == 2)) ? SENSE_TYPE_ATARI :   \
218          ((sd).scsi.dummy1 >= 11) ? SENSE_TYPE_SCSI :                   \
219          SENSE_TYPE_UNKNOWN)
220          
221 #define CAPACITY(sd)                                                    \
222         (SENSE_TYPE(sd) == SENSE_TYPE_ATARI ?           \
223          ATARI_CAPACITY((sd).atari) :                           \
224          SCSI_CAPACITY((sd).scsi))
225
226 #define SECTOR_SIZE(sd)                                                 \
227         (SENSE_TYPE(sd) == SENSE_TYPE_ATARI ?           \
228          (sd).atari.sector_size :                                       \
229          (sd).scsi.sector_size & 0xffffff)
230
231 /* Default size if capacity cannot be determined (1 GByte) */
232 #define DEFAULT_SIZE    0x1fffff
233
234 #define CARTRCH_STAT(aip,buf)                                           \
235         (aip->old_atari_disk ?                                          \
236          (((buf)[0] & 0x7f) == 0x28) :                                  \
237          ((((buf)[0] & 0x70) == 0x70) ?                                 \
238           (((buf)[2] & 0x0f) == 0x06) :                                 \
239           (((buf)[0] & 0x0f) == 0x06)))                                 \
240
241 /* These two are also exported to other drivers that work on the ACSI bus and
242  * need an ST-RAM buffer. */
243 char                    *acsi_buffer;
244 unsigned long   phys_acsi_buffer;
245
246 static int NDevices;
247
248 static int                              CurrentNReq;
249 static int                              CurrentNSect;
250 static char                             *CurrentBuffer;
251
252 static DEFINE_SPINLOCK(acsi_lock);
253
254
255 #define SET_TIMER()     mod_timer(&acsi_timer, jiffies + ACSI_TIMEOUT)
256 #define CLEAR_TIMER()   del_timer(&acsi_timer)
257
258 static unsigned long    STramMask;
259 #define STRAM_ADDR(a)   (((a) & STramMask) == 0)
260
261
262
263 /* ACSI commands */
264
265 static char tur_cmd[6]        = { 0x00, 0, 0, 0, 0, 0 };
266 static char modesense_cmd[6]  = { 0x1a, 0, 0, 0, 24, 0 };
267 static char modeselect_cmd[6] = { 0x15, 0, 0, 0, 12, 0 };
268 static char inquiry_cmd[6]    = { 0x12, 0, 0, 0,255, 0 };
269 static char reqsense_cmd[6]   = { 0x03, 0, 0, 0, 4, 0 };
270 static char read_cmd[6]       = { 0x08, 0, 0, 0, 0, 0 };
271 static char write_cmd[6]      = { 0x0a, 0, 0, 0, 0, 0 };
272 static char pa_med_rem_cmd[6] = { 0x1e, 0, 0, 0, 0, 0 };
273
274 #define CMDSET_TARG_LUN(cmd,targ,lun)                   \
275     do {                                                \
276                 cmd[0] = (cmd[0] & ~0xe0) | (targ)<<5;  \
277                 cmd[1] = (cmd[1] & ~0xe0) | (lun)<<5;   \
278         } while(0)
279
280 #define CMDSET_BLOCK(cmd,blk)                                           \
281     do {                                                                                        \
282                 unsigned long __blk = (blk);                            \
283                 cmd[3] = __blk; __blk >>= 8;                            \
284                 cmd[2] = __blk; __blk >>= 8;                            \
285                 cmd[1] = (cmd[1] & 0xe0) | (__blk & 0x1f);      \
286         } while(0)
287
288 #define CMDSET_LEN(cmd,len)                                             \
289         do {                                                                            \
290                 cmd[4] = (len);                                                 \
291         } while(0)
292
293 /* ACSI errors (from REQUEST SENSE); There are two tables, one for the
294  * old Atari disks and one for SCSI on ACSI disks.
295  */
296
297 struct acsi_error {
298         unsigned char   code;
299         const char              *text;
300 } atari_acsi_errors[] = {
301         { 0x00, "No error (??)" },
302         { 0x01, "No index pulses" },
303         { 0x02, "Seek not complete" },
304         { 0x03, "Write fault" },
305         { 0x04, "Drive not ready" },
306         { 0x06, "No Track 00 signal" },
307         { 0x10, "ECC error in ID field" },
308         { 0x11, "Uncorrectable data error" },
309         { 0x12, "ID field address mark not found" },
310         { 0x13, "Data field address mark not found" },
311         { 0x14, "Record not found" },
312         { 0x15, "Seek error" },
313         { 0x18, "Data check in no retry mode" },
314         { 0x19, "ECC error during verify" },
315         { 0x1a, "Access to bad block" },
316         { 0x1c, "Unformatted or bad format" },
317         { 0x20, "Invalid command" },
318         { 0x21, "Invalid block address" },
319         { 0x23, "Volume overflow" },
320         { 0x24, "Invalid argument" },
321         { 0x25, "Invalid drive number" },
322         { 0x26, "Byte zero parity check" },
323         { 0x28, "Cartride changed" },
324         { 0x2c, "Error count overflow" },
325         { 0x30, "Controller selftest failed" }
326 },
327
328         scsi_acsi_errors[] = {
329         { 0x00, "No error (??)" },
330         { 0x01, "Recovered error" },
331         { 0x02, "Drive not ready" },
332         { 0x03, "Uncorrectable medium error" },
333         { 0x04, "Hardware error" },
334         { 0x05, "Illegal request" },
335         { 0x06, "Unit attention (Reset or cartridge changed)" },
336         { 0x07, "Data protection" },
337         { 0x08, "Blank check" },
338         { 0x0b, "Aborted Command" },
339         { 0x0d, "Volume overflow" }
340 };
341
342
343
344 /***************************** Prototypes *****************************/
345
346 static int acsicmd_dma( const char *cmd, char *buffer, int blocks, int
347                         rwflag, int enable);
348 static int acsi_reqsense( char *buffer, int targ, int lun);
349 static void acsi_print_error(const unsigned char *errblk, struct acsi_info_struct *aip);
350 static irqreturn_t acsi_interrupt (int irq, void *data, struct pt_regs *fp);
351 static void unexpected_acsi_interrupt( void );
352 static void bad_rw_intr( void );
353 static void read_intr( void );
354 static void write_intr( void);
355 static void acsi_times_out( unsigned long dummy );
356 static void copy_to_acsibuffer( void );
357 static void copy_from_acsibuffer( void );
358 static void do_end_requests( void );
359 static void do_acsi_request( request_queue_t * );
360 static void redo_acsi_request( void );
361 static int acsi_ioctl( struct inode *inode, struct file *file, unsigned int
362                        cmd, unsigned long arg );
363 static int acsi_open( struct inode * inode, struct file * filp );
364 static int acsi_release( struct inode * inode, struct file * file );
365 static void acsi_prevent_removal(struct acsi_info_struct *aip, int flag );
366 static int acsi_change_blk_size( int target, int lun);
367 static int acsi_mode_sense( int target, int lun, SENSE_DATA *sd );
368 static int acsi_revalidate (struct gendisk *disk);
369
370 /************************* End of Prototypes **************************/
371
372
373 DEFINE_TIMER(acsi_timer, acsi_times_out, 0, 0);
374
375
376 #ifdef CONFIG_ATARI_SLM
377
378 extern int attach_slm( int target, int lun );
379 extern int slm_init( void );
380
381 #endif
382
383
384 \f
385 /***********************************************************************
386  *
387  *   ACSI primitives
388  *
389  **********************************************************************/
390
391
392 /*
393  * The following two functions wait for _IRQ to become Low or High,
394  * resp., with a timeout. The 'timeout' parameter is in jiffies
395  * (10ms).
396  * If the functions are called with timer interrupts on (int level <
397  * 6), the timeout is based on the 'jiffies' variable to provide exact
398  * timeouts for device probing etc.
399  * If interrupts are disabled, the number of tries is based on the
400  * 'loops_per_jiffy' variable. A rough estimation is sufficient here...
401  */
402
403 #define INT_LEVEL                                                                                                       \
404         ({      unsigned __sr;                                                                                          \
405                 __asm__ __volatile__ ( "movew   %/sr,%0" : "=dm" (__sr) );      \
406                 (__sr >> 8) & 7;                                                                                        \
407         })
408
409 int acsi_wait_for_IRQ( unsigned timeout )
410
411 {
412         if (INT_LEVEL < 6) {
413                 unsigned long maxjif = jiffies + timeout;
414                 while (time_before(jiffies, maxjif))
415                         if (!(mfp.par_dt_reg & 0x20)) return( 1 );
416         }
417         else {
418                 long tries = loops_per_jiffy / 8 * timeout;
419                 while( --tries >= 0 )
420                         if (!(mfp.par_dt_reg & 0x20)) return( 1 );
421         }               
422         return( 0 ); /* timeout! */
423 }
424
425
426 int acsi_wait_for_noIRQ( unsigned timeout )
427
428 {
429         if (INT_LEVEL < 6) {
430                 unsigned long maxjif = jiffies + timeout;
431                 while (time_before(jiffies, maxjif))
432                         if (mfp.par_dt_reg & 0x20) return( 1 );
433         }
434         else {
435                 long tries = loops_per_jiffy * timeout / 8;
436                 while( tries-- >= 0 )
437                         if (mfp.par_dt_reg & 0x20) return( 1 );
438         }               
439         return( 0 ); /* timeout! */
440 }
441
442 static struct timeval start_time;
443
444 void
445 acsi_delay_start(void)
446 {
447         do_gettimeofday(&start_time);
448 }
449
450 /* wait from acsi_delay_start to now usec (<1E6) usec */
451
452 void
453 acsi_delay_end(long usec)
454 {
455         struct timeval end_time;
456         long deltau,deltas;
457         do_gettimeofday(&end_time);
458         deltau=end_time.tv_usec - start_time.tv_usec;
459         deltas=end_time.tv_sec - start_time.tv_sec;
460         if (deltas > 1 || deltas < 0)
461                 return;
462         if (deltas > 0)
463                 deltau += 1000*1000;
464         if (deltau >= usec)
465                 return;
466         udelay(usec-deltau);
467 }
468
469 /* acsicmd_dma() sends an ACSI command and sets up the DMA to transfer
470  * 'blocks' blocks of 512 bytes from/to 'buffer'.
471  * Because the _IRQ signal is used for handshaking the command bytes,
472  * the ACSI interrupt has to be disabled in this function. If the end
473  * of the operation should be signalled by a real interrupt, it has to be
474  * reenabled afterwards.
475  */
476
477 static int acsicmd_dma( const char *cmd, char *buffer, int blocks, int rwflag, int enable)
478
479 {       unsigned long   flags, paddr;
480         int                             i;
481
482 #ifdef NO_WRITE
483         if (rwflag || *cmd == 0x0a) {
484                 printk( "ACSI: Write commands disabled!\n" );
485                 return( 0 );
486         }
487 #endif
488         
489         rwflag = rwflag ? 0x100 : 0;
490         paddr = virt_to_phys( buffer );
491
492         acsi_delay_end(COMMAND_DELAY);
493         DISABLE_IRQ();
494
495         local_irq_save(flags);
496         /* Low on A1 */
497         dma_wd.dma_mode_status = 0x88 | rwflag;
498         MFPDELAY();
499
500         /* set DMA address */
501         dma_wd.dma_lo = (unsigned char)paddr;
502         paddr >>= 8;
503         MFPDELAY();
504         dma_wd.dma_md = (unsigned char)paddr;
505         paddr >>= 8;
506         MFPDELAY();
507         if (ATARIHW_PRESENT(EXTD_DMA))
508                 st_dma_ext_dmahi = (unsigned short)paddr;
509         else
510                 dma_wd.dma_hi = (unsigned char)paddr;
511         MFPDELAY();
512         local_irq_restore(flags);
513
514         /* send the command bytes except the last */
515         for( i = 0; i < 5; ++i ) {
516                 DMA_LONG_WRITE( *cmd++, 0x8a | rwflag );
517                 udelay(20);
518                 if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
519         }
520
521         /* Clear FIFO and switch DMA to correct direction */  
522         dma_wd.dma_mode_status = 0x92 | (rwflag ^ 0x100);  
523         MFPDELAY();
524         dma_wd.dma_mode_status = 0x92 | rwflag;
525         MFPDELAY();
526
527         /* How many sectors for DMA */
528         dma_wd.fdc_acces_seccount = blocks;
529         MFPDELAY();
530         
531         /* send last command byte */
532         dma_wd.dma_mode_status = 0x8a | rwflag;
533         MFPDELAY();
534         DMA_LONG_WRITE( *cmd++, 0x0a | rwflag );
535         if (enable)
536                 ENABLE_IRQ();
537         udelay(80);
538
539         return( 1 );
540 }
541
542
543 /*
544  * acsicmd_nodma() sends an ACSI command that requires no DMA.
545  */
546
547 int acsicmd_nodma( const char *cmd, int enable)
548
549 {       int     i;
550
551         acsi_delay_end(COMMAND_DELAY);
552         DISABLE_IRQ();
553
554         /* send first command byte */
555         dma_wd.dma_mode_status = 0x88;
556         MFPDELAY();
557         DMA_LONG_WRITE( *cmd++, 0x8a );
558         udelay(20);
559         if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
560
561         /* send the intermediate command bytes */
562         for( i = 0; i < 4; ++i ) {
563                 DMA_LONG_WRITE( *cmd++, 0x8a );
564                 udelay(20);
565                 if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
566         }
567
568         /* send last command byte */
569         DMA_LONG_WRITE( *cmd++, 0x0a );
570         if (enable)
571                 ENABLE_IRQ();
572         udelay(80);
573         
574         return( 1 );
575         /* Note that the ACSI interrupt is still disabled after this
576          * function. If you want to get the IRQ delivered, enable it manually!
577          */
578 }
579
580
581 static int acsi_reqsense( char *buffer, int targ, int lun)
582
583 {
584         CMDSET_TARG_LUN( reqsense_cmd, targ, lun);
585         if (!acsicmd_dma( reqsense_cmd, buffer, 1, 0, 0 )) return( 0 );
586         if (!acsi_wait_for_IRQ( 10 )) return( 0 );
587         acsi_getstatus();
588         if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
589         if (!acsi_wait_for_IRQ( 10 )) return( 0 );
590         acsi_getstatus();
591         if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
592         if (!acsi_wait_for_IRQ( 10 )) return( 0 );
593         acsi_getstatus();
594         if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
595         if (!acsi_wait_for_IRQ( 10 )) return( 0 );
596         acsi_getstatus();
597         dma_cache_maintenance( virt_to_phys(buffer), 16, 0 );
598         
599         return( 1 );
600 }       
601
602
603 /*
604  * ACSI status phase: get the status byte from the bus
605  *
606  * I've seen several times that a 0xff status is read, propably due to
607  * a timing error. In this case, the procedure is repeated after the
608  * next _IRQ edge.
609  */
610
611 int acsi_getstatus( void )
612
613 {       int     status;
614
615         DISABLE_IRQ();
616         for(;;) {
617                 if (!acsi_wait_for_IRQ( 100 )) {
618                         acsi_delay_start();
619                         return( -1 );
620                 }
621                 dma_wd.dma_mode_status = 0x8a;
622                 MFPDELAY();
623                 status = dma_wd.fdc_acces_seccount;
624                 if (status != 0xff) break;
625 #ifdef DEBUG
626                 printk("ACSI: skipping 0xff status byte\n" );
627 #endif
628                 udelay(40);
629                 acsi_wait_for_noIRQ( 20 );
630         }
631         dma_wd.dma_mode_status = 0x80;
632         udelay(40);
633         acsi_wait_for_noIRQ( 20 );
634
635         acsi_delay_start();
636         return( status & 0x1f ); /* mask of the device# */
637 }
638
639
640 #if (defined(CONFIG_ATARI_SLM) || defined(CONFIG_ATARI_SLM_MODULE))
641
642 /* Receive data in an extended status phase. Needed by SLM printer. */
643
644 int acsi_extstatus( char *buffer, int cnt )
645
646 {       int     status;
647
648         DISABLE_IRQ();
649         udelay(80);
650         while( cnt-- > 0 ) {
651                 if (!acsi_wait_for_IRQ( 40 )) return( 0 );
652                 dma_wd.dma_mode_status = 0x8a;
653                 MFPDELAY();
654                 status = dma_wd.fdc_acces_seccount;
655                 MFPDELAY();
656                 *buffer++ = status & 0xff;
657                 udelay(40);
658         }
659         return( 1 );
660 }
661
662
663 /* Finish an extended status phase */
664
665 void acsi_end_extstatus( void )
666
667 {
668         dma_wd.dma_mode_status = 0x80;
669         udelay(40);
670         acsi_wait_for_noIRQ( 20 );
671         acsi_delay_start();
672 }
673
674
675 /* Send data in an extended command phase */
676
677 int acsi_extcmd( unsigned char *buffer, int cnt )
678
679 {
680         while( cnt-- > 0 ) {
681                 DMA_LONG_WRITE( *buffer++, 0x8a );
682                 udelay(20);
683                 if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
684         }
685         return( 1 );
686 }
687
688 #endif
689
690
691 static void acsi_print_error(const unsigned char *errblk, struct acsi_info_struct *aip)
692
693 {       int atari_err, i, errcode;
694         struct acsi_error *arr;
695
696         atari_err = aip->old_atari_disk;
697         if (atari_err)
698                 errcode = errblk[0] & 0x7f;
699         else
700                 if ((errblk[0] & 0x70) == 0x70)
701                         errcode = errblk[2] & 0x0f;
702                 else
703                         errcode = errblk[0] & 0x0f;
704         
705         printk( KERN_ERR "ACSI error 0x%02x", errcode );
706
707         if (errblk[0] & 0x80)
708                 printk( " for sector %d",
709                                 ((errblk[1] & 0x1f) << 16) |
710                                 (errblk[2] << 8) | errblk[0] );
711
712         arr = atari_err ? atari_acsi_errors : scsi_acsi_errors;
713         i = atari_err ? sizeof(atari_acsi_errors)/sizeof(*atari_acsi_errors) :
714                             sizeof(scsi_acsi_errors)/sizeof(*scsi_acsi_errors);
715         
716         for( --i; i >= 0; --i )
717                 if (arr[i].code == errcode) break;
718         if (i >= 0)
719                 printk( ": %s\n", arr[i].text );
720 }
721
722 /*******************************************************************
723  *
724  * ACSI interrupt routine
725  *   Test, if this is a ACSI interrupt and call the irq handler
726  *   Otherwise ignore this interrupt.
727  *
728  *******************************************************************/
729
730 static irqreturn_t acsi_interrupt(int irq, void *data, struct pt_regs *fp )
731
732 {       void (*acsi_irq_handler)(void) = do_acsi;
733
734         do_acsi = NULL;
735         CLEAR_TIMER();
736
737         if (!acsi_irq_handler)
738                 acsi_irq_handler = unexpected_acsi_interrupt;
739         acsi_irq_handler();
740         return IRQ_HANDLED;
741 }
742
743
744 /******************************************************************
745  *
746  * The Interrupt handlers
747  *
748  *******************************************************************/
749
750
751 static void unexpected_acsi_interrupt( void )
752
753 {
754         printk( KERN_WARNING "Unexpected ACSI interrupt\n" );
755 }
756
757
758 /* This function is called in case of errors. Because we cannot reset
759  * the ACSI bus or a single device, there is no other choice than
760  * retrying several times :-(
761  */
762
763 static void bad_rw_intr( void )
764
765 {
766         if (!CURRENT)
767                 return;
768
769         if (++CURRENT->errors >= MAX_ERRORS)
770                 end_request(CURRENT, 0);
771         /* Otherwise just retry */
772 }
773
774
775 static void read_intr( void )
776
777 {       int             status;
778         
779         status = acsi_getstatus();
780         if (status != 0) {
781                 struct gendisk *disk = CURRENT->rq_disk;
782                 struct acsi_info_struct *aip = disk->private_data;
783                 printk(KERN_ERR "%s: ", disk->disk_name);
784                 if (!acsi_reqsense(acsi_buffer, aip->target, aip->lun))
785                         printk( "ACSI error and REQUEST SENSE failed (status=0x%02x)\n", status );
786                 else {
787                         acsi_print_error(acsi_buffer, aip);
788                         if (CARTRCH_STAT(aip, acsi_buffer))
789                                 aip->changed = 1;
790                 }
791                 ENABLE_IRQ();
792                 bad_rw_intr();
793                 redo_acsi_request();
794                 return;
795         }
796
797         dma_cache_maintenance( virt_to_phys(CurrentBuffer), CurrentNSect*512, 0 );
798         if (CurrentBuffer == acsi_buffer)
799                 copy_from_acsibuffer();
800
801         do_end_requests();
802         redo_acsi_request();
803 }
804
805
806 static void write_intr(void)
807
808 {       int     status;
809
810         status = acsi_getstatus();
811         if (status != 0) {
812                 struct gendisk *disk = CURRENT->rq_disk;
813                 struct acsi_info_struct *aip = disk->private_data;
814                 printk( KERN_ERR "%s: ", disk->disk_name);
815                 if (!acsi_reqsense( acsi_buffer, aip->target, aip->lun))
816                         printk( "ACSI error and REQUEST SENSE failed (status=0x%02x)\n", status );
817                 else {
818                         acsi_print_error(acsi_buffer, aip);
819                         if (CARTRCH_STAT(aip, acsi_buffer))
820                                 aip->changed = 1;
821                 }
822                 bad_rw_intr();
823                 redo_acsi_request();
824                 return;
825         }
826
827         do_end_requests();
828         redo_acsi_request();
829 }
830
831
832 static void acsi_times_out( unsigned long dummy )
833
834 {
835         DISABLE_IRQ();
836         if (!do_acsi) return;
837
838         do_acsi = NULL;
839         printk( KERN_ERR "ACSI timeout\n" );
840         if (!CURRENT)
841             return;
842         if (++CURRENT->errors >= MAX_ERRORS) {
843 #ifdef DEBUG
844                 printk( KERN_ERR "ACSI: too many errors.\n" );
845 #endif
846                 end_request(CURRENT, 0);
847         }
848
849         redo_acsi_request();
850 }
851
852
853 \f
854 /***********************************************************************
855  *
856  *  Scatter-gather utility functions
857  *
858  ***********************************************************************/
859
860
861 static void copy_to_acsibuffer( void )
862
863 {       int                                     i;
864         char                            *src, *dst;
865         struct buffer_head      *bh;
866         
867         src = CURRENT->buffer;
868         dst = acsi_buffer;
869         bh = CURRENT->bh;
870
871         if (!bh)
872                 memcpy( dst, src, CurrentNSect*512 );
873         else
874                 for( i = 0; i < CurrentNReq; ++i ) {
875                         memcpy( dst, src, bh->b_size );
876                         dst += bh->b_size;
877                         if ((bh = bh->b_reqnext))
878                                 src = bh->b_data;
879                 }
880 }
881
882
883 static void copy_from_acsibuffer( void )
884
885 {       int                                     i;
886         char                            *src, *dst;
887         struct buffer_head      *bh;
888         
889         dst = CURRENT->buffer;
890         src = acsi_buffer;
891         bh = CURRENT->bh;
892
893         if (!bh)
894                 memcpy( dst, src, CurrentNSect*512 );
895         else
896                 for( i = 0; i < CurrentNReq; ++i ) {
897                         memcpy( dst, src, bh->b_size );
898                         src += bh->b_size;
899                         if ((bh = bh->b_reqnext))
900                                 dst = bh->b_data;
901                 }
902 }
903
904
905 static void do_end_requests( void )
906
907 {       int             i, n;
908
909         if (!CURRENT->bh) {
910                 CURRENT->nr_sectors -= CurrentNSect;
911                 CURRENT->current_nr_sectors -= CurrentNSect;
912                 CURRENT->sector += CurrentNSect;
913                 if (CURRENT->nr_sectors == 0)
914                         end_request(CURRENT, 1);
915         }
916         else {
917                 for( i = 0; i < CurrentNReq; ++i ) {
918                         n = CURRENT->bh->b_size >> 9;
919                         CURRENT->nr_sectors -= n;
920                         CURRENT->current_nr_sectors -= n;
921                         CURRENT->sector += n;
922                         end_request(CURRENT, 1);
923                 }
924         }
925 }
926
927
928
929 \f
930 /***********************************************************************
931  *
932  *  do_acsi_request and friends
933  *
934  ***********************************************************************/
935
936 static void do_acsi_request( request_queue_t * q )
937
938 {
939         stdma_lock( acsi_interrupt, NULL );
940         redo_acsi_request();
941 }
942
943
944 static void redo_acsi_request( void )
945 {
946         unsigned                        block, target, lun, nsect;
947         char                            *buffer;
948         unsigned long           pbuffer;
949         struct buffer_head      *bh;
950         struct gendisk *disk;
951         struct acsi_info_struct *aip;
952
953   repeat:
954         CLEAR_TIMER();
955
956         if (do_acsi)
957                 return;
958
959         if (!CURRENT) {
960                 do_acsi = NULL;
961                 ENABLE_IRQ();
962                 stdma_release();
963                 return;
964         }
965
966         disk = CURRENT->rq_disk;
967         aip = disk->private_data;
968         if (CURRENT->bh) {
969                 if (!CURRENT->bh && !buffer_locked(CURRENT->bh))
970                         panic("ACSI: block not locked");
971         }
972
973         block = CURRENT->sector;
974         if (block+CURRENT->nr_sectors >= get_capacity(disk)) {
975 #ifdef DEBUG
976                 printk( "%s: attempted access for blocks %d...%ld past end of device at block %ld.\n",
977                        disk->disk_name,
978                        block, block + CURRENT->nr_sectors - 1,
979                        get_capacity(disk));
980 #endif
981                 end_request(CURRENT, 0);
982                 goto repeat;
983         }
984         if (aip->changed) {
985                 printk( KERN_NOTICE "%s: request denied because cartridge has "
986                                 "been changed.\n", disk->disk_name);
987                 end_request(CURRENT, 0);
988                 goto repeat;
989         }
990         
991         target = aip->target;
992         lun    = aip->lun;
993
994         /* Find out how many sectors should be transferred from/to
995          * consecutive buffers and thus can be done with a single command.
996          */
997         buffer      = CURRENT->buffer;
998         pbuffer     = virt_to_phys(buffer);
999         nsect       = CURRENT->current_nr_sectors;
1000         CurrentNReq = 1;
1001
1002         if ((bh = CURRENT->bh) && bh != CURRENT->bhtail) {
1003                 if (!STRAM_ADDR(pbuffer)) {
1004                         /* If transfer is done via the ACSI buffer anyway, we can
1005                          * assemble as much bh's as fit in the buffer.
1006                          */
1007                         while( (bh = bh->b_reqnext) ) {
1008                                 if (nsect + (bh->b_size>>9) > ACSI_BUFFER_SECTORS) break;
1009                                 nsect += bh->b_size >> 9;
1010                                 ++CurrentNReq;
1011                                 if (bh == CURRENT->bhtail) break;
1012                         }
1013                         buffer = acsi_buffer;
1014                         pbuffer = phys_acsi_buffer;
1015                 }
1016                 else {
1017                         unsigned long pendadr, pnewadr;
1018                         pendadr = pbuffer + nsect*512;
1019                         while( (bh = bh->b_reqnext) ) {
1020                                 pnewadr = virt_to_phys(bh->b_data);
1021                                 if (!STRAM_ADDR(pnewadr) || pendadr != pnewadr) break;
1022                                 nsect += bh->b_size >> 9;
1023                                 pendadr = pnewadr + bh->b_size;
1024                                 ++CurrentNReq;
1025                                 if (bh == CURRENT->bhtail) break;
1026                         }
1027                 }
1028         }
1029         else {
1030                 if (!STRAM_ADDR(pbuffer)) {
1031                         buffer = acsi_buffer;
1032                         pbuffer = phys_acsi_buffer;
1033                         if (nsect > ACSI_BUFFER_SECTORS)
1034                                 nsect = ACSI_BUFFER_SECTORS;
1035                 }
1036         }
1037         CurrentBuffer = buffer;
1038         CurrentNSect  = nsect;
1039
1040         if (rq_data_dir(CURRENT) == WRITE) {
1041                 CMDSET_TARG_LUN( write_cmd, target, lun );
1042                 CMDSET_BLOCK( write_cmd, block );
1043                 CMDSET_LEN( write_cmd, nsect );
1044                 if (buffer == acsi_buffer)
1045                         copy_to_acsibuffer();
1046                 dma_cache_maintenance( pbuffer, nsect*512, 1 );
1047                 do_acsi = write_intr;
1048                 if (!acsicmd_dma( write_cmd, buffer, nsect, 1, 1)) {
1049                         do_acsi = NULL;
1050                         printk( KERN_ERR "ACSI (write): Timeout in command block\n" );
1051                         bad_rw_intr();
1052                         goto repeat;
1053                 }
1054                 SET_TIMER();
1055                 return;
1056         }
1057         if (rq_data_dir(CURRENT) == READ) {
1058                 CMDSET_TARG_LUN( read_cmd, target, lun );
1059                 CMDSET_BLOCK( read_cmd, block );
1060                 CMDSET_LEN( read_cmd, nsect );
1061                 do_acsi = read_intr;
1062                 if (!acsicmd_dma( read_cmd, buffer, nsect, 0, 1)) {
1063                         do_acsi = NULL;
1064                         printk( KERN_ERR "ACSI (read): Timeout in command block\n" );
1065                         bad_rw_intr();
1066                         goto repeat;
1067                 }
1068                 SET_TIMER();
1069                 return;
1070         }
1071         panic("unknown ACSI command");
1072 }
1073
1074
1075 \f
1076 /***********************************************************************
1077  *
1078  *  Misc functions: ioctl, open, release, check_change, ...
1079  *
1080  ***********************************************************************/
1081
1082 static int acsi_getgeo(struct block_device *bdev, struct hd_geometry *geo)
1083 {
1084         struct acsi_info_struct *aip = bdev->bd_disk->private_data;
1085
1086         /*
1087          * Just fake some geometry here, it's nonsense anyway
1088          * To make it easy, use Adaptec's usual 64/32 mapping
1089          */
1090         geo->heads = 64;
1091         geo->sectors = 32;
1092         geo->cylinders = aip->size >> 11;
1093         return 0;
1094 }
1095
1096 static int acsi_ioctl( struct inode *inode, struct file *file,
1097                                            unsigned int cmd, unsigned long arg )
1098 {
1099         struct gendisk *disk = inode->i_bdev->bd_disk;
1100         struct acsi_info_struct *aip = disk->private_data;
1101         switch (cmd) {
1102           case SCSI_IOCTL_GET_IDLUN:
1103                 /* SCSI compatible GET_IDLUN call to get target's ID and LUN number */
1104                 put_user( aip->target | (aip->lun << 8),
1105                                   &((Scsi_Idlun *) arg)->dev_id );
1106                 put_user( 0, &((Scsi_Idlun *) arg)->host_unique_id );
1107                 return 0;
1108           default:
1109                 return -EINVAL;
1110         }
1111 }
1112
1113
1114 /*
1115  * Open a device, check for read-only and lock the medium if it is
1116  * removable.
1117  *
1118  * Changes by Martin Rogge, 9th Aug 1995:
1119  * Check whether check_disk_change (and therefore revalidate_acsidisk)
1120  * was successful. They fail when there is no medium in the drive.
1121  *
1122  * The problem of media being changed during an operation can be 
1123  * ignored because of the prevent_removal code.
1124  *
1125  * Added check for the validity of the device number.
1126  *
1127  */
1128
1129 static int acsi_open( struct inode * inode, struct file * filp )
1130 {
1131         struct gendisk *disk = inode->i_bdev->bd_disk;
1132         struct acsi_info_struct *aip = disk->private_data;
1133
1134         if (aip->access_count == 0 && aip->removable) {
1135 #if 0
1136                 aip->changed = 1;       /* safety first */
1137 #endif
1138                 check_disk_change( inode->i_bdev );
1139                 if (aip->changed)       /* revalidate was not successful (no medium) */
1140                         return -ENXIO;
1141                 acsi_prevent_removal(aip, 1);
1142         }
1143         aip->access_count++;
1144
1145         if (filp && filp->f_mode) {
1146                 check_disk_change( inode->i_bdev );
1147                 if (filp->f_mode & 2) {
1148                         if (aip->read_only) {
1149                                 acsi_release( inode, filp );
1150                                 return -EROFS;
1151                         }
1152                 }
1153         }
1154
1155         return 0;
1156 }
1157
1158 /*
1159  * Releasing a block device means we sync() it, so that it can safely
1160  * be forgotten about...
1161  */
1162
1163 static int acsi_release( struct inode * inode, struct file * file )
1164 {
1165         struct gendisk *disk = inode->i_bdev->bd_disk;
1166         struct acsi_info_struct *aip = disk->private_data;
1167         if (--aip->access_count == 0 && aip->removable)
1168                 acsi_prevent_removal(aip, 0);
1169         return( 0 );
1170 }
1171
1172 /*
1173  * Prevent or allow a media change for removable devices.
1174  */
1175
1176 static void acsi_prevent_removal(struct acsi_info_struct *aip, int flag)
1177 {
1178         stdma_lock( NULL, NULL );
1179         
1180         CMDSET_TARG_LUN(pa_med_rem_cmd, aip->target, aip->lun);
1181         CMDSET_LEN( pa_med_rem_cmd, flag );
1182         
1183         if (acsicmd_nodma(pa_med_rem_cmd, 0) && acsi_wait_for_IRQ(3*HZ))
1184                 acsi_getstatus();
1185         /* Do not report errors -- some devices may not know this command. */
1186
1187         ENABLE_IRQ();
1188         stdma_release();
1189 }
1190
1191 static int acsi_media_change(struct gendisk *disk)
1192 {
1193         struct acsi_info_struct *aip = disk->private_data;
1194
1195         if (!aip->removable) 
1196                 return 0;
1197
1198         if (aip->changed)
1199                 /* We can be sure that the medium has been changed -- REQUEST
1200                  * SENSE has reported this earlier.
1201                  */
1202                 return 1;
1203
1204         /* If the flag isn't set, make a test by reading block 0.
1205          * If errors happen, it seems to be better to say "changed"...
1206          */
1207         stdma_lock( NULL, NULL );
1208         CMDSET_TARG_LUN(read_cmd, aip->target, aip->lun);
1209         CMDSET_BLOCK( read_cmd, 0 );
1210         CMDSET_LEN( read_cmd, 1 );
1211         if (acsicmd_dma(read_cmd, acsi_buffer, 1, 0, 0) &&
1212             acsi_wait_for_IRQ(3*HZ)) {
1213                 if (acsi_getstatus()) {
1214                         if (acsi_reqsense(acsi_buffer, aip->target, aip->lun)) {
1215                                 if (CARTRCH_STAT(aip, acsi_buffer))
1216                                         aip->changed = 1;
1217                         }
1218                         else {
1219                                 printk( KERN_ERR "%s: REQUEST SENSE failed in test for "
1220                                        "medium change; assuming a change\n", disk->disk_name );
1221                                 aip->changed = 1;
1222                         }
1223                 }
1224         }
1225         else {
1226                 printk( KERN_ERR "%s: Test for medium changed timed out; "
1227                                 "assuming a change\n", disk->disk_name);
1228                 aip->changed = 1;
1229         }
1230         ENABLE_IRQ();
1231         stdma_release();
1232
1233         /* Now, after reading a block, the changed status is surely valid. */
1234         return aip->changed;
1235 }
1236
1237
1238 static int acsi_change_blk_size( int target, int lun)
1239
1240 {       int i;
1241
1242         for (i=0; i<12; i++)
1243                 acsi_buffer[i] = 0;
1244
1245         acsi_buffer[3] = 8;
1246         acsi_buffer[10] = 2;
1247         CMDSET_TARG_LUN( modeselect_cmd, target, lun);
1248
1249         if (!acsicmd_dma( modeselect_cmd, acsi_buffer, 1,1,0) ||
1250                 !acsi_wait_for_IRQ( 3*HZ ) ||
1251                 acsi_getstatus() != 0 ) {
1252                 return(0);
1253         }
1254         return(1);
1255 }
1256
1257
1258 static int acsi_mode_sense( int target, int lun, SENSE_DATA *sd )
1259
1260 {
1261         int page;
1262
1263         CMDSET_TARG_LUN( modesense_cmd, target, lun );
1264         for (page=0; page<4; page++) {
1265                 modesense_cmd[2] = page;
1266                 if (!acsicmd_dma( modesense_cmd, acsi_buffer, 1, 0, 0 ) ||
1267                     !acsi_wait_for_IRQ( 3*HZ ) ||
1268                     acsi_getstatus())
1269                         continue;
1270
1271                 /* read twice to jump over the second 16-byte border! */
1272                 udelay(300);
1273                 if (acsi_wait_for_noIRQ( 20 ) &&
1274                     acsicmd_nodma( modesense_cmd, 0 ) &&
1275                     acsi_wait_for_IRQ( 3*HZ ) &&
1276                     acsi_getstatus() == 0)
1277                         break;
1278         }
1279         if (page == 4) {
1280                 return(0);
1281         }
1282
1283         dma_cache_maintenance( phys_acsi_buffer, sizeof(SENSE_DATA), 0 );
1284         *sd = *(SENSE_DATA *)acsi_buffer;
1285
1286         /* Validity check, depending on type of data */
1287         
1288         switch( SENSE_TYPE(*sd) ) {
1289
1290           case SENSE_TYPE_ATARI:
1291                 if (CAPACITY(*sd) == 0)
1292                         goto invalid_sense;
1293                 break;
1294
1295           case SENSE_TYPE_SCSI:
1296                 if (sd->scsi.descriptor_size != 8)
1297                         goto invalid_sense;
1298                 break;
1299
1300           case SENSE_TYPE_UNKNOWN:
1301
1302                 printk( KERN_ERR "ACSI target %d, lun %d: Cannot interpret "
1303                                 "sense data\n", target, lun ); 
1304                 
1305           invalid_sense:
1306
1307 #ifdef DEBUG
1308                 {       int i;
1309                 printk( "Mode sense data for ACSI target %d, lun %d seem not valid:",
1310                                 target, lun );
1311                 for( i = 0; i < sizeof(SENSE_DATA); ++i )
1312                         printk( "%02x ", (unsigned char)acsi_buffer[i] );
1313                 printk( "\n" );
1314                 }
1315 #endif
1316                 return( 0 );
1317         }
1318                 
1319         return( 1 );
1320 }
1321
1322
1323
1324 /*******************************************************************
1325  *
1326  *  Initialization
1327  *
1328  ********************************************************************/
1329
1330
1331 extern struct block_device_operations acsi_fops;
1332
1333 static struct gendisk *acsi_gendisk[MAX_DEV];
1334
1335 #define MAX_SCSI_DEVICE_CODE 10
1336
1337 static const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
1338 {
1339  "Direct-Access    ",
1340  "Sequential-Access",
1341  "Printer          ",
1342  "Processor        ",
1343  "WORM             ",
1344  "CD-ROM           ",
1345  "Scanner          ",
1346  "Optical Device   ",
1347  "Medium Changer   ",
1348  "Communications   "
1349 };
1350
1351 static void print_inquiry(unsigned char *data)
1352 {
1353         int i;
1354
1355         printk(KERN_INFO "  Vendor: ");
1356         for (i = 8; i < 16; i++)
1357                 {
1358                 if (data[i] >= 0x20 && i < data[4] + 5)
1359                         printk("%c", data[i]);
1360                 else
1361                         printk(" ");
1362                 }
1363
1364         printk("  Model: ");
1365         for (i = 16; i < 32; i++)
1366                 {
1367                 if (data[i] >= 0x20 && i < data[4] + 5)
1368                         printk("%c", data[i]);
1369                 else
1370                         printk(" ");
1371                 }
1372
1373         printk("  Rev: ");
1374         for (i = 32; i < 36; i++)
1375                 {
1376                 if (data[i] >= 0x20 && i < data[4] + 5)
1377                         printk("%c", data[i]);
1378                 else
1379                         printk(" ");
1380                 }
1381
1382         printk("\n");
1383
1384         i = data[0] & 0x1f;
1385
1386         printk(KERN_INFO "  Type:   %s ", (i < MAX_SCSI_DEVICE_CODE
1387                                                                            ? scsi_device_types[i]
1388                                                                            : "Unknown          "));
1389         printk("                 ANSI SCSI revision: %02x", data[2] & 0x07);
1390         if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
1391           printk(" CCS\n");
1392         else
1393           printk("\n");
1394 }
1395
1396
1397 /* 
1398  * Changes by Martin Rogge, 9th Aug 1995: 
1399  * acsi_devinit has been taken out of acsi_geninit, because it needs 
1400  * to be called from revalidate_acsidisk. The result of request sense 
1401  * is now checked for DRIVE NOT READY.
1402  *
1403  * The structure *aip is only valid when acsi_devinit returns 
1404  * DEV_SUPPORTED. 
1405  *
1406  */
1407         
1408 #define DEV_NONE        0
1409 #define DEV_UNKNOWN     1
1410 #define DEV_SUPPORTED   2
1411 #define DEV_SLM         3
1412
1413 static int acsi_devinit(struct acsi_info_struct *aip)
1414 {
1415         int status, got_inquiry;
1416         SENSE_DATA sense;
1417         unsigned char reqsense, extsense;
1418
1419         /*****************************************************************/
1420         /* Do a TEST UNIT READY command to test the presence of a device */
1421         /*****************************************************************/
1422
1423         CMDSET_TARG_LUN(tur_cmd, aip->target, aip->lun);
1424         if (!acsicmd_nodma(tur_cmd, 0)) {
1425                 /* timed out -> no device here */
1426 #ifdef DEBUG_DETECT
1427                 printk("target %d lun %d: timeout\n", aip->target, aip->lun);
1428 #endif
1429                 return DEV_NONE;
1430         }
1431                 
1432         /*************************/
1433         /* Read the ACSI status. */
1434         /*************************/
1435
1436         status = acsi_getstatus();
1437         if (status) {
1438                 if (status == 0x12) {
1439                         /* The SLM printer should be the only device that
1440                          * responds with the error code in the status byte. In
1441                          * correct status bytes, bit 4 is never set.
1442                          */
1443                         printk( KERN_INFO "Detected SLM printer at id %d lun %d\n",
1444                                aip->target, aip->lun);
1445                         return DEV_SLM;
1446                 }
1447                 /* ignore CHECK CONDITION, since some devices send a
1448                    UNIT ATTENTION */
1449                 if ((status & 0x1e) != 0x2) {
1450 #ifdef DEBUG_DETECT
1451                         printk("target %d lun %d: status %d\n",
1452                                aip->target, aip->lun, status);
1453 #endif
1454                         return DEV_UNKNOWN;
1455                 }
1456         }
1457
1458         /*******************************/
1459         /* Do a REQUEST SENSE command. */
1460         /*******************************/
1461
1462         if (!acsi_reqsense(acsi_buffer, aip->target, aip->lun)) {
1463                 printk( KERN_WARNING "acsi_reqsense failed\n");
1464                 acsi_buffer[0] = 0;
1465                 acsi_buffer[2] = UNIT_ATTENTION;
1466         }
1467         reqsense = acsi_buffer[0];
1468         extsense = acsi_buffer[2] & 0xf;
1469         if (status) {
1470                 if ((reqsense & 0x70) == 0x70) {        /* extended sense */
1471                         if (extsense != UNIT_ATTENTION &&
1472                             extsense != NOT_READY) {
1473 #ifdef DEBUG_DETECT
1474                                 printk("target %d lun %d: extended sense %d\n",
1475                                        aip->target, aip->lun, extsense);
1476 #endif
1477                                 return DEV_UNKNOWN;
1478                         }
1479                 }
1480                 else {
1481                         if (reqsense & 0x7f) {
1482 #ifdef DEBUG_DETECT
1483                                 printk("target %d lun %d: sense %d\n",
1484                                        aip->target, aip->lun, reqsense);
1485 #endif
1486                                 return DEV_UNKNOWN;
1487                         }
1488                 }
1489         }
1490         else 
1491                 if (reqsense == 0x4) {  /* SH204 Bug workaround */
1492 #ifdef DEBUG_DETECT
1493                         printk("target %d lun %d status=0 sense=4\n",
1494                                aip->target, aip->lun);
1495 #endif
1496                         return DEV_UNKNOWN;
1497                 }
1498
1499         /***********************************************************/
1500         /* Do an INQUIRY command to get more infos on this device. */
1501         /***********************************************************/
1502
1503         /* Assume default values */
1504         aip->removable = 1;
1505         aip->read_only = 0;
1506         aip->old_atari_disk = 0;
1507         aip->changed = (extsense == NOT_READY); /* medium inserted? */
1508         aip->size = DEFAULT_SIZE;
1509         got_inquiry = 0;
1510         /* Fake inquiry result for old atari disks */
1511         memcpy(acsi_buffer, "\000\000\001\000    Adaptec 40xx"
1512                "                    ", 40);
1513         CMDSET_TARG_LUN(inquiry_cmd, aip->target, aip->lun);
1514         if (acsicmd_dma(inquiry_cmd, acsi_buffer, 1, 0, 0) &&
1515             acsi_getstatus() == 0) {
1516                 acsicmd_nodma(inquiry_cmd, 0);
1517                 acsi_getstatus();
1518                 dma_cache_maintenance( phys_acsi_buffer, 256, 0 );
1519                 got_inquiry = 1;
1520                 aip->removable = !!(acsi_buffer[1] & 0x80);
1521         }
1522         if (aip->type == NONE)  /* only at boot time */
1523                 print_inquiry(acsi_buffer);
1524         switch(acsi_buffer[0]) {
1525           case TYPE_DISK:
1526                 aip->type = HARDDISK;
1527                 break;
1528           case TYPE_ROM:
1529                 aip->type = CDROM;
1530                 aip->read_only = 1;
1531                 break;
1532           default:
1533                 return DEV_UNKNOWN;
1534         }
1535         /****************************/
1536         /* Do a MODE SENSE command. */
1537         /****************************/
1538
1539         if (!acsi_mode_sense(aip->target, aip->lun, &sense)) {
1540                 printk( KERN_WARNING "No mode sense data.\n" );
1541                 return DEV_UNKNOWN;
1542         }
1543         if ((SECTOR_SIZE(sense) != 512) &&
1544             ((aip->type != CDROM) ||
1545              !acsi_change_blk_size(aip->target, aip->lun) ||
1546              !acsi_mode_sense(aip->target, aip->lun, &sense) ||
1547              (SECTOR_SIZE(sense) != 512))) {
1548                 printk( KERN_WARNING "Sector size != 512 not supported.\n" );
1549                 return DEV_UNKNOWN;
1550         }
1551         /* There are disks out there that claim to have 0 sectors... */
1552         if (CAPACITY(sense))
1553                 aip->size = CAPACITY(sense);    /* else keep DEFAULT_SIZE */
1554         if (!got_inquiry && SENSE_TYPE(sense) == SENSE_TYPE_ATARI) {
1555                 /* If INQUIRY failed and the sense data suggest an old
1556                  * Atari disk (SH20x, Megafile), the disk is not removable
1557                  */
1558                 aip->removable = 0;
1559                 aip->old_atari_disk = 1;
1560         }
1561         
1562         /******************/
1563         /* We've done it. */
1564         /******************/
1565         
1566         return DEV_SUPPORTED;
1567 }
1568
1569 EXPORT_SYMBOL(acsi_delay_start);
1570 EXPORT_SYMBOL(acsi_delay_end);
1571 EXPORT_SYMBOL(acsi_wait_for_IRQ);
1572 EXPORT_SYMBOL(acsi_wait_for_noIRQ);
1573 EXPORT_SYMBOL(acsicmd_nodma);
1574 EXPORT_SYMBOL(acsi_getstatus);
1575 EXPORT_SYMBOL(acsi_buffer);
1576 EXPORT_SYMBOL(phys_acsi_buffer);
1577
1578 #ifdef CONFIG_ATARI_SLM_MODULE
1579 void acsi_attach_SLMs( int (*attach_func)( int, int ) );
1580
1581 EXPORT_SYMBOL(acsi_extstatus);
1582 EXPORT_SYMBOL(acsi_end_extstatus);
1583 EXPORT_SYMBOL(acsi_extcmd);
1584 EXPORT_SYMBOL(acsi_attach_SLMs);
1585
1586 /* to remember IDs of SLM devices, SLM module is loaded later
1587  * (index is target#, contents is lun#, -1 means "no SLM") */
1588 int SLM_devices[8];
1589 #endif
1590
1591 static struct block_device_operations acsi_fops = {
1592         .owner          = THIS_MODULE,
1593         .open           = acsi_open,
1594         .release        = acsi_release,
1595         .ioctl          = acsi_ioctl,
1596         .getgeo         = acsi_getgeo,
1597         .media_changed  = acsi_media_change,
1598         .revalidate_disk= acsi_revalidate,
1599 };
1600
1601 #ifdef CONFIG_ATARI_SLM_MODULE
1602 /* call attach_slm() for each device that is a printer; needed for init of SLM
1603  * driver as a module, since it's not yet present if acsi.c is inited and thus
1604  * the bus gets scanned. */
1605 void acsi_attach_SLMs( int (*attach_func)( int, int ) )
1606 {
1607         int i, n = 0;
1608
1609         for( i = 0; i < 8; ++i )
1610                 if (SLM_devices[i] >= 0)
1611                         n += (*attach_func)( i, SLM_devices[i] );
1612         printk( KERN_INFO "Found %d SLM printer(s) total.\n", n );
1613 }
1614 #endif /* CONFIG_ATARI_SLM_MODULE */
1615
1616
1617 int acsi_init( void )
1618 {
1619         int err = 0;
1620         int i, target, lun;
1621         struct acsi_info_struct *aip;
1622 #ifdef CONFIG_ATARI_SLM
1623         int n_slm = 0;
1624 #endif
1625         if (!MACH_IS_ATARI || !ATARIHW_PRESENT(ACSI))
1626                 return 0;
1627         if (register_blkdev(ACSI_MAJOR, "ad")) {
1628                 err = -EBUSY;
1629                 goto out1;
1630         }
1631         if (!(acsi_buffer =
1632                   (char *)atari_stram_alloc(ACSI_BUFFER_SIZE, "acsi"))) {
1633                 err = -ENOMEM;
1634                 printk( KERN_ERR "Unable to get ACSI ST-Ram buffer.\n" );
1635                 goto out2;
1636         }
1637         phys_acsi_buffer = virt_to_phys( acsi_buffer );
1638         STramMask = ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000 : 0xff000000;
1639         
1640         acsi_queue = blk_init_queue(do_acsi_request, &acsi_lock);
1641         if (!acsi_queue) {
1642                 err = -ENOMEM;
1643                 goto out2a;
1644         }
1645 #ifdef CONFIG_ATARI_SLM
1646         err = slm_init();
1647 #endif
1648         if (err)
1649                 goto out3;
1650
1651         printk( KERN_INFO "Probing ACSI devices:\n" );
1652         NDevices = 0;
1653 #ifdef CONFIG_ATARI_SLM_MODULE
1654         for( i = 0; i < 8; ++i )
1655                 SLM_devices[i] = -1;
1656 #endif
1657         stdma_lock(NULL, NULL);
1658
1659         for (target = 0; target < 8 && NDevices < MAX_DEV; ++target) {
1660                 lun = 0;
1661                 do {
1662                         aip = &acsi_info[NDevices];
1663                         aip->type = NONE;
1664                         aip->target = target;
1665                         aip->lun = lun;
1666                         i = acsi_devinit(aip);
1667                         switch (i) {
1668                           case DEV_SUPPORTED:
1669                                 printk( KERN_INFO "Detected ");
1670                                 switch (aip->type) {
1671                                   case HARDDISK:
1672                                         printk("disk");
1673                                         break;
1674                                   case CDROM:
1675                                         printk("cdrom");
1676                                         break;
1677                                   default:
1678                                 }
1679                                 printk(" ad%c at id %d lun %d ",
1680                                        'a' + NDevices, target, lun);
1681                                 if (aip->removable) 
1682                                         printk("(removable) ");
1683                                 if (aip->read_only) 
1684                                         printk("(read-only) ");
1685                                 if (aip->size == DEFAULT_SIZE)
1686                                         printk(" unkown size, using default ");
1687                                 printk("%ld MByte\n",
1688                                        (aip->size*512+1024*1024/2)/(1024*1024));
1689                                 NDevices++;
1690                                 break;
1691                           case DEV_SLM:
1692 #ifdef CONFIG_ATARI_SLM
1693                                 n_slm += attach_slm( target, lun );
1694                                 break;
1695 #endif
1696 #ifdef CONFIG_ATARI_SLM_MODULE
1697                                 SLM_devices[target] = lun;
1698                                 break;
1699 #endif
1700                                 /* neither of the above: fall through to unknown device */
1701                           case DEV_UNKNOWN:
1702                                 printk( KERN_INFO "Detected unsupported device at "
1703                                                 "id %d lun %d\n", target, lun);
1704                                 break;
1705                         }
1706                 }
1707 #ifdef CONFIG_ACSI_MULTI_LUN
1708                 while (i != DEV_NONE && ++lun < MAX_LUN);
1709 #else
1710                 while (0);
1711 #endif
1712         }
1713
1714         /* reenable interrupt */
1715         ENABLE_IRQ();
1716         stdma_release();
1717
1718 #ifndef CONFIG_ATARI_SLM
1719         printk( KERN_INFO "Found %d ACSI device(s) total.\n", NDevices );
1720 #else
1721         printk( KERN_INFO "Found %d ACSI device(s) and %d SLM printer(s) total.\n",
1722                         NDevices, n_slm );
1723 #endif
1724         err = -ENOMEM;
1725         for( i = 0; i < NDevices; ++i ) {
1726                 acsi_gendisk[i] = alloc_disk(16);
1727                 if (!acsi_gendisk[i])
1728                         goto out4;
1729         }
1730
1731         for( i = 0; i < NDevices; ++i ) {
1732                 struct gendisk *disk = acsi_gendisk[i];
1733                 sprintf(disk->disk_name, "ad%c", 'a'+i);
1734                 aip = &acsi_info[NDevices];
1735                 sprintf(disk->devfs_name, "ad/target%d/lun%d", aip->target, aip->lun);
1736                 disk->major = ACSI_MAJOR;
1737                 disk->first_minor = i << 4;
1738                 if (acsi_info[i].type != HARDDISK) {
1739                         disk->minors = 1;
1740                         strcat(disk->devfs_name, "/disc");
1741                 }
1742                 disk->fops = &acsi_fops;
1743                 disk->private_data = &acsi_info[i];
1744                 set_capacity(disk, acsi_info[i].size);
1745                 disk->queue = acsi_queue;
1746                 add_disk(disk);
1747         }
1748         return 0;
1749 out4:
1750         while (i--)
1751                 put_disk(acsi_gendisk[i]);
1752 out3:
1753         blk_cleanup_queue(acsi_queue);
1754 out2a:
1755         atari_stram_free( acsi_buffer );
1756 out2:
1757         unregister_blkdev( ACSI_MAJOR, "ad" );
1758 out1:
1759         return err;
1760 }
1761
1762
1763 #ifdef MODULE
1764
1765 MODULE_LICENSE("GPL");
1766
1767 int init_module(void)
1768 {
1769         int err;
1770
1771         if ((err = acsi_init()))
1772                 return( err );
1773         printk( KERN_INFO "ACSI driver loaded as module.\n");
1774         return( 0 );
1775 }
1776
1777 void cleanup_module(void)
1778 {
1779         int i;
1780         del_timer( &acsi_timer );
1781         blk_cleanup_queue(acsi_queue);
1782         atari_stram_free( acsi_buffer );
1783
1784         if (unregister_blkdev( ACSI_MAJOR, "ad" ) != 0)
1785                 printk( KERN_ERR "acsi: cleanup_module failed\n");
1786
1787         for (i = 0; i < NDevices; i++) {
1788                 del_gendisk(acsi_gendisk[i]);
1789                 put_disk(acsi_gendisk[i]);
1790         }
1791 }
1792 #endif
1793
1794 /*
1795  * This routine is called to flush all partitions and partition tables
1796  * for a changed scsi disk, and then re-read the new partition table.
1797  * If we are revalidating a disk because of a media change, then we
1798  * enter with usage == 0.  If we are using an ioctl, we automatically have
1799  * usage == 1 (we need an open channel to use an ioctl :-), so this
1800  * is our limit.
1801  *
1802  * Changes by Martin Rogge, 9th Aug 1995: 
1803  * got cd-roms to work by calling acsi_devinit. There are only two problems:
1804  * First, if there is no medium inserted, the status will remain "changed".
1805  * That is no problem at all, but our design of three-valued logic (medium
1806  * changed, medium not changed, no medium inserted).
1807  * Secondly the check could fail completely and the drive could deliver
1808  * nonsensical data, which could mess up the acsi_info[] structure. In
1809  * that case we try to make the entry safe.
1810  *
1811  */
1812
1813 static int acsi_revalidate(struct gendisk *disk)
1814 {
1815         struct acsi_info_struct *aip = disk->private_data;
1816         stdma_lock( NULL, NULL );
1817         if (acsi_devinit(aip) != DEV_SUPPORTED) {
1818                 printk( KERN_ERR "ACSI: revalidate failed for target %d lun %d\n",
1819                        aip->target, aip->lun);
1820                 aip->size = 0;
1821                 aip->read_only = 1;
1822                 aip->removable = 1;
1823                 aip->changed = 1; /* next acsi_open will try again... */
1824         }
1825
1826         ENABLE_IRQ();
1827         stdma_release();
1828         set_capacity(disk, aip->size);
1829         return 0;
1830 }