~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Linux/drivers/block/acsi.c

Version: ~ [ 2.2.5 ] ~ [ 2.4.1 ] ~ [ 2.4.9 ] ~ [ 2.6.17.10 ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.