Import 2.3.1pre2
[davej-history.git] / drivers / block / acsi.c
blobd9598fb73031c85fe210f28d7a29100221e3a4c6
1 /*
2 * acsi.c -- Device driver for Atari ACSI hard disks
4 * Copyright 1994 Roman Hodek <Roman.Hodek@informatik.uni-erlangen.de>
6 * Some parts are based on hd.c by Linus Torvalds
8 * This file is subject to the terms and conditions of the GNU General Public
9 * License. See the file COPYING in the main directory of this archive for
10 * more details.
15 * Still to in this file:
16 * - If a command ends with an error status (!= 0), the following
17 * REQUEST SENSE commands (4 to fill the ST-DMA FIFO) are done by
18 * polling the _IRQ signal (not interrupt-driven). This should be
19 * avoided in future because it takes up a non-neglectible time in
20 * the interrupt service routine while interrupts are disabled.
21 * Maybe a timer interrupt will get lost :-(
25 * General notes:
27 * - All ACSI devices (disks, CD-ROMs, ...) use major number 28.
28 * Minors are organized like it is with SCSI: The upper 4 bits
29 * identify the device, the lower 4 bits the partition.
30 * The device numbers (the upper 4 bits) are given in the same
31 * order as the devices are found on the bus.
32 * - Up to 8 LUNs are supported for each target (if CONFIG_ACSI_MULTI_LUN
33 * is defined), but only a total of 16 devices (due to minor
34 * numbers...). Note that Atari allows only a maximum of 4 targets
35 * (i.e. controllers, not devices) on the ACSI bus!
36 * - A optimizing scheme similar to SCSI scatter-gather is implemented.
37 * - Removable media are supported. After a medium change to device
38 * is reinitialized (partition check etc.). Also, if the device
39 * knows the PREVENT/ALLOW MEDIUM REMOVAL command, the door should
40 * be locked and unlocked when mounting the first or unmounting the
41 * last filesystem on the device. The code is untested, because I
42 * don't have a removable hard disk.
46 #define MAJOR_NR ACSI_MAJOR
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/delay.h>
58 #include <linux/mm.h>
59 #include <linux/major.h>
60 #include <linux/blk.h>
61 #include <linux/malloc.h>
62 #include <linux/interrupt.h>
63 #include <scsi/scsi.h> /* for SCSI_IOCTL_GET_IDLUN */
64 typedef void Scsi_Device; /* hack to avoid including scsi.h */
65 #include <scsi/scsi_ioctl.h>
66 #include <linux/hdreg.h> /* for HDIO_GETGEO */
68 #include <asm/setup.h>
69 #include <asm/pgtable.h>
70 #include <asm/system.h>
71 #include <asm/uaccess.h>
72 #include <asm/atarihw.h>
73 #include <asm/atariints.h>
74 #include <asm/atari_acsi.h>
75 #include <asm/atari_stdma.h>
76 #include <asm/atari_stram.h>
79 #define DEBUG
80 #undef DEBUG_DETECT
81 #undef NO_WRITE
83 #define MAX_ERRORS 8 /* Max read/write errors/sector */
84 #define MAX_LUN 8 /* Max LUNs per target */
85 #define MAX_DEV 16
87 #define ACSI_BUFFER_SIZE (16*1024) /* "normal" ACSI buffer size */
88 #define ACSI_BUFFER_MINSIZE (2048) /* min. buf size if ext. DMA */
89 #define ACSI_BUFFER_SIZE_ORDER 2 /* order size for above */
90 #define ACSI_BUFFER_MINSIZE_ORDER 0 /* order size for above */
91 #define ACSI_BUFFER_SECTORS (ACSI_BUFFER_SIZE/512)
93 #define ACSI_BUFFER_ORDER \
94 (ATARIHW_PRESENT(EXTD_DMA) ? \
95 ACSI_BUFFER_MINSIZE_ORDER : \
96 ACSI_BUFFER_SIZE_ORDER)
98 #define ACSI_TIMEOUT (4*HZ)
100 /* minimum delay between two commands */
102 #define COMMAND_DELAY 500
104 typedef enum {
105 NONE, HARDDISK, CDROM
106 } ACSI_TYPE;
108 struct acsi_info_struct {
109 ACSI_TYPE type; /* type of device */
110 unsigned target; /* target number */
111 unsigned lun; /* LUN in target controller */
112 unsigned removable : 1; /* Flag for removable media */
113 unsigned read_only : 1; /* Flag for read only devices */
114 unsigned old_atari_disk : 1; /* Is an old Atari disk */
115 unsigned changed : 1; /* Medium has been changed */
116 unsigned long size; /* #blocks */
117 } acsi_info[MAX_DEV];
120 * SENSE KEYS
123 #define NO_SENSE 0x00
124 #define RECOVERED_ERROR 0x01
125 #define NOT_READY 0x02
126 #define MEDIUM_ERROR 0x03
127 #define HARDWARE_ERROR 0x04
128 #define ILLEGAL_REQUEST 0x05
129 #define UNIT_ATTENTION 0x06
130 #define DATA_PROTECT 0x07
131 #define BLANK_CHECK 0x08
132 #define COPY_ABORTED 0x0a
133 #define ABORTED_COMMAND 0x0b
134 #define VOLUME_OVERFLOW 0x0d
135 #define MISCOMPARE 0x0e
139 * DEVICE TYPES
142 #define TYPE_DISK 0x00
143 #define TYPE_TAPE 0x01
144 #define TYPE_WORM 0x04
145 #define TYPE_ROM 0x05
146 #define TYPE_MOD 0x07
147 #define TYPE_NO_LUN 0x7f
149 /* The data returned by MODE SENSE differ between the old Atari
150 * hard disks and SCSI disks connected to ACSI. In the following, both
151 * formats are defined and some macros to operate on them potably.
154 typedef struct {
155 unsigned long dummy[2];
156 unsigned long sector_size;
157 unsigned char format_code;
158 #define ATARI_SENSE_FORMAT_FIX 1
159 #define ATARI_SENSE_FORMAT_CHNG 2
160 unsigned char cylinders_h;
161 unsigned char cylinders_l;
162 unsigned char heads;
163 unsigned char reduced_h;
164 unsigned char reduced_l;
165 unsigned char precomp_h;
166 unsigned char precomp_l;
167 unsigned char landing_zone;
168 unsigned char steprate;
169 unsigned char type;
170 #define ATARI_SENSE_TYPE_FIXCHNG_MASK 4
171 #define ATARI_SENSE_TYPE_SOFTHARD_MASK 8
172 #define ATARI_SENSE_TYPE_FIX 4
173 #define ATARI_SENSE_TYPE_CHNG 0
174 #define ATARI_SENSE_TYPE_SOFT 0
175 #define ATARI_SENSE_TYPE_HARD 8
176 unsigned char sectors;
177 } ATARI_SENSE_DATA;
179 #define ATARI_CAPACITY(sd) \
180 (((int)((sd).cylinders_h<<8)|(sd).cylinders_l) * \
181 (sd).heads * (sd).sectors)
184 typedef struct {
185 unsigned char dummy1;
186 unsigned char medium_type;
187 unsigned char dummy2;
188 unsigned char descriptor_size;
189 unsigned long block_count;
190 unsigned long sector_size;
191 /* Page 0 data */
192 unsigned char page_code;
193 unsigned char page_size;
194 unsigned char page_flags;
195 unsigned char qualifier;
196 } SCSI_SENSE_DATA;
198 #define SCSI_CAPACITY(sd) ((sd).block_count & 0xffffff)
201 typedef union {
202 ATARI_SENSE_DATA atari;
203 SCSI_SENSE_DATA scsi;
204 } SENSE_DATA;
206 #define SENSE_TYPE_UNKNOWN 0
207 #define SENSE_TYPE_ATARI 1
208 #define SENSE_TYPE_SCSI 2
210 #define SENSE_TYPE(sd) \
211 (((sd).atari.dummy[0] == 8 && \
212 ((sd).atari.format_code == 1 || \
213 (sd).atari.format_code == 2)) ? SENSE_TYPE_ATARI : \
214 ((sd).scsi.dummy1 >= 11) ? SENSE_TYPE_SCSI : \
215 SENSE_TYPE_UNKNOWN)
217 #define CAPACITY(sd) \
218 (SENSE_TYPE(sd) == SENSE_TYPE_ATARI ? \
219 ATARI_CAPACITY((sd).atari) : \
220 SCSI_CAPACITY((sd).scsi))
222 #define SECTOR_SIZE(sd) \
223 (SENSE_TYPE(sd) == SENSE_TYPE_ATARI ? \
224 (sd).atari.sector_size : \
225 (sd).scsi.sector_size & 0xffffff)
227 /* Default size if capacity cannot be determined (1 GByte) */
228 #define DEFAULT_SIZE 0x1fffff
230 #define CARTRCH_STAT(dev,buf) \
231 (acsi_info[(dev)].old_atari_disk ? \
232 (((buf)[0] & 0x7f) == 0x28) : \
233 ((((buf)[0] & 0x70) == 0x70) ? \
234 (((buf)[2] & 0x0f) == 0x06) : \
235 (((buf)[0] & 0x0f) == 0x06))) \
237 /* These two are also exported to other drivers that work on the ACSI bus and
238 * need an ST-RAM buffer. */
239 char *acsi_buffer;
240 unsigned long phys_acsi_buffer;
242 static int NDevices = 0;
243 static int acsi_sizes[MAX_DEV<<4] = { 0, };
244 static int acsi_blocksizes[MAX_DEV<<4] = { 0, };
245 static struct hd_struct acsi_part[MAX_DEV<<4] = { {0,0}, };
246 static int access_count[MAX_DEV] = { 0, };
247 static char busy[MAX_DEV] = { 0, };
248 static DECLARE_WAIT_QUEUE_HEAD(busy_wait);
250 static int CurrentNReq;
251 static int CurrentNSect;
252 static char *CurrentBuffer;
255 #define SET_TIMER() \
256 do { \
257 del_timer( &acsi_timer ); \
258 acsi_timer.expires = jiffies + ACSI_TIMEOUT; \
259 add_timer( &acsi_timer ); \
260 } while(0)
262 #define CLEAR_TIMER() \
263 do { \
264 del_timer( &acsi_timer ); \
265 } while(0)
267 static unsigned long STramMask;
268 #define STRAM_ADDR(a) (((a) & STramMask) == 0)
272 /* ACSI commands */
274 static char tur_cmd[6] = { 0x00, 0, 0, 0, 0, 0 };
275 static char modesense_cmd[6] = { 0x1a, 0, 0, 0, 24, 0 };
276 static char modeselect_cmd[6] = { 0x15, 0, 0, 0, 12, 0 };
277 static char inquiry_cmd[6] = { 0x12, 0, 0, 0,255, 0 };
278 static char reqsense_cmd[6] = { 0x03, 0, 0, 0, 4, 0 };
279 static char read_cmd[6] = { 0x08, 0, 0, 0, 0, 0 };
280 static char write_cmd[6] = { 0x0a, 0, 0, 0, 0, 0 };
281 static char pa_med_rem_cmd[6] = { 0x1e, 0, 0, 0, 0, 0 };
283 #define CMDSET_TARG_LUN(cmd,targ,lun) \
284 do { \
285 cmd[0] = (cmd[0] & ~0xe0) | (targ)<<5; \
286 cmd[1] = (cmd[1] & ~0xe0) | (lun)<<5; \
287 } while(0)
289 #define CMDSET_BLOCK(cmd,blk) \
290 do { \
291 unsigned long __blk = (blk); \
292 cmd[3] = __blk; __blk >>= 8; \
293 cmd[2] = __blk; __blk >>= 8; \
294 cmd[1] = (cmd[1] & 0xe0) | (__blk & 0x1f); \
295 } while(0)
297 #define CMDSET_LEN(cmd,len) \
298 do { \
299 cmd[4] = (len); \
300 } while(0)
302 #define min(a,b) (((a)<(b))?(a):(b))
305 /* ACSI errors (from REQUEST SENSE); There are two tables, one for the
306 * old Atari disks and one for SCSI on ACSI disks.
309 struct acsi_error {
310 unsigned char code;
311 const char *text;
312 } atari_acsi_errors[] = {
313 { 0x00, "No error (??)" },
314 { 0x01, "No index pulses" },
315 { 0x02, "Seek not complete" },
316 { 0x03, "Write fault" },
317 { 0x04, "Drive not ready" },
318 { 0x06, "No Track 00 signal" },
319 { 0x10, "ECC error in ID field" },
320 { 0x11, "Uncorrectable data error" },
321 { 0x12, "ID field address mark not found" },
322 { 0x13, "Data field address mark not found" },
323 { 0x14, "Record not found" },
324 { 0x15, "Seek error" },
325 { 0x18, "Data check in no retry mode" },
326 { 0x19, "ECC error during verify" },
327 { 0x1a, "Access to bad block" },
328 { 0x1c, "Unformatted or bad format" },
329 { 0x20, "Invalid command" },
330 { 0x21, "Invalid block address" },
331 { 0x23, "Volume overflow" },
332 { 0x24, "Invalid argument" },
333 { 0x25, "Invalid drive number" },
334 { 0x26, "Byte zero parity check" },
335 { 0x28, "Cartride changed" },
336 { 0x2c, "Error count overflow" },
337 { 0x30, "Controller selftest failed" }
340 scsi_acsi_errors[] = {
341 { 0x00, "No error (??)" },
342 { 0x01, "Recovered error" },
343 { 0x02, "Drive not ready" },
344 { 0x03, "Uncorrectable medium error" },
345 { 0x04, "Hardware error" },
346 { 0x05, "Illegal request" },
347 { 0x06, "Unit attention (Reset or cartridge changed)" },
348 { 0x07, "Data protection" },
349 { 0x08, "Blank check" },
350 { 0x0b, "Aborted Command" },
351 { 0x0d, "Volume overflow" }
356 /***************************** Prototypes *****************************/
358 static int acsicmd_dma( const char *cmd, char *buffer, int blocks, int
359 rwflag, int enable);
360 static int acsi_reqsense( char *buffer, int targ, int lun);
361 static void acsi_print_error( const unsigned char *errblk, int dev );
362 static void acsi_interrupt (int irq, void *data, struct pt_regs *fp);
363 static void unexpected_acsi_interrupt( void );
364 static void bad_rw_intr( void );
365 static void read_intr( void );
366 static void write_intr( void);
367 static void acsi_times_out( unsigned long dummy );
368 static void copy_to_acsibuffer( void );
369 static void copy_from_acsibuffer( void );
370 static void do_end_requests( void );
371 static void do_acsi_request( void );
372 static void redo_acsi_request( void );
373 static int acsi_ioctl( struct inode *inode, struct file *file, unsigned int
374 cmd, unsigned long arg );
375 static int acsi_open( struct inode * inode, struct file * filp );
376 static int acsi_release( struct inode * inode, struct file * file );
377 static void acsi_prevent_removal( int target, int flag );
378 static int acsi_change_blk_size( int target, int lun);
379 static int acsi_mode_sense( int target, int lun, SENSE_DATA *sd );
380 static void acsi_geninit( struct gendisk *gd );
381 static int revalidate_acsidisk( int dev, int maxusage );
382 static int acsi_revalidate (dev_t);
384 /************************* End of Prototypes **************************/
387 struct timer_list acsi_timer = { NULL, NULL, 0, 0, acsi_times_out };
390 #ifdef CONFIG_ATARI_SLM
392 extern int attach_slm( int target, int lun );
393 extern int slm_init( void );
395 #endif
399 /***********************************************************************
401 * ACSI primitives
403 **********************************************************************/
407 * The following two functions wait for _IRQ to become Low or High,
408 * resp., with a timeout. The 'timeout' parameter is in jiffies
409 * (10ms).
410 * If the functions are called with timer interrupts on (int level <
411 * 6), the timeout is based on the 'jiffies' variable to provide exact
412 * timeouts for device probing etc.
413 * If interrupts are disabled, the number of tries is based on the
414 * 'loops_per_sec' variable. A rough estimation is sufficient here...
417 #define INT_LEVEL \
418 ({ unsigned __sr; \
419 __asm__ __volatile__ ( "movew %/sr,%0" : "=dm" (__sr) ); \
420 (__sr >> 8) & 7; \
423 int acsi_wait_for_IRQ( unsigned timeout )
426 if (INT_LEVEL < 6) {
427 unsigned long maxjif;
428 for( maxjif = jiffies + timeout; time_before(jiffies, maxjif); )
429 if (!(mfp.par_dt_reg & 0x20)) return( 1 );
431 else {
432 long tries = loops_per_sec / HZ / 8 * timeout;
433 while( --tries >= 0 )
434 if (!(mfp.par_dt_reg & 0x20)) return( 1 );
436 return( 0 ); /* timeout! */
440 int acsi_wait_for_noIRQ( unsigned timeout )
443 if (INT_LEVEL < 6) {
444 unsigned long maxjif;
445 for( maxjif = jiffies + timeout; time_before(jiffies, maxjif); )
446 if (mfp.par_dt_reg & 0x20) return( 1 );
448 else {
449 long tries = loops_per_sec * timeout / HZ / 8;
450 while( tries-- >= 0 )
451 if (mfp.par_dt_reg & 0x20) return( 1 );
453 return( 0 ); /* timeout! */
456 static struct timeval start_time;
458 void
459 acsi_delay_start(void)
461 do_gettimeofday(&start_time);
464 /* wait from acsi_delay_start to now usec (<1E6) usec */
466 void
467 acsi_delay_end(long usec)
469 struct timeval end_time;
470 long deltau,deltas;
471 do_gettimeofday(&end_time);
472 deltau=end_time.tv_usec - start_time.tv_usec;
473 deltas=end_time.tv_sec - start_time.tv_sec;
474 if (deltas > 1 || deltas < 0)
475 return;
476 if (deltas > 0)
477 deltau += 1000*1000;
478 if (deltau >= usec)
479 return;
480 udelay(usec-deltau);
483 /* acsicmd_dma() sends an ACSI command and sets up the DMA to transfer
484 * 'blocks' blocks of 512 bytes from/to 'buffer'.
485 * Because the _IRQ signal is used for handshaking the command bytes,
486 * the ACSI interrupt has to be disabled in this function. If the end
487 * of the operation should be signalled by a real interrupt, it has to be
488 * reenabled afterwards.
491 static int acsicmd_dma( const char *cmd, char *buffer, int blocks, int rwflag, int enable)
493 { unsigned long flags, paddr;
494 int i;
496 #ifdef NO_WRITE
497 if (rwflag || *cmd == 0x0a) {
498 printk( "ACSI: Write commands disabled!\n" );
499 return( 0 );
501 #endif
503 rwflag = rwflag ? 0x100 : 0;
504 paddr = VTOP( buffer );
506 acsi_delay_end(COMMAND_DELAY);
507 DISABLE_IRQ();
509 save_flags(flags);
510 cli();
511 /* Low on A1 */
512 dma_wd.dma_mode_status = 0x88 | rwflag;
513 MFPDELAY();
515 /* set DMA address */
516 dma_wd.dma_lo = (unsigned char)paddr;
517 paddr >>= 8;
518 MFPDELAY();
519 dma_wd.dma_md = (unsigned char)paddr;
520 paddr >>= 8;
521 MFPDELAY();
522 if (ATARIHW_PRESENT(EXTD_DMA))
523 st_dma_ext_dmahi = (unsigned short)paddr;
524 else
525 dma_wd.dma_hi = (unsigned char)paddr;
526 MFPDELAY();
527 restore_flags(flags);
529 /* send the command bytes except the last */
530 for( i = 0; i < 5; ++i ) {
531 DMA_LONG_WRITE( *cmd++, 0x8a | rwflag );
532 udelay(20);
533 if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
536 /* Clear FIFO and switch DMA to correct direction */
537 dma_wd.dma_mode_status = 0x92 | (rwflag ^ 0x100);
538 MFPDELAY();
539 dma_wd.dma_mode_status = 0x92 | rwflag;
540 MFPDELAY();
542 /* How many sectors for DMA */
543 dma_wd.fdc_acces_seccount = blocks;
544 MFPDELAY();
546 /* send last command byte */
547 dma_wd.dma_mode_status = 0x8a | rwflag;
548 MFPDELAY();
549 DMA_LONG_WRITE( *cmd++, 0x0a | rwflag );
550 if (enable)
551 ENABLE_IRQ();
552 udelay(80);
554 return( 1 );
559 * acsicmd_nodma() sends an ACSI command that requires no DMA.
562 int acsicmd_nodma( const char *cmd, int enable)
564 { int i;
566 acsi_delay_end(COMMAND_DELAY);
567 DISABLE_IRQ();
569 /* send first command byte */
570 dma_wd.dma_mode_status = 0x88;
571 MFPDELAY();
572 DMA_LONG_WRITE( *cmd++, 0x8a );
573 udelay(20);
574 if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
576 /* send the intermediate command bytes */
577 for( i = 0; i < 4; ++i ) {
578 DMA_LONG_WRITE( *cmd++, 0x8a );
579 udelay(20);
580 if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
583 /* send last command byte */
584 DMA_LONG_WRITE( *cmd++, 0x0a );
585 if (enable)
586 ENABLE_IRQ();
587 udelay(80);
589 return( 1 );
590 /* Note that the ACSI interrupt is still disabled after this
591 * function. If you want to get the IRQ delivered, enable it manually!
596 static int acsi_reqsense( char *buffer, int targ, int lun)
599 CMDSET_TARG_LUN( reqsense_cmd, targ, lun);
600 if (!acsicmd_dma( reqsense_cmd, buffer, 1, 0, 0 )) return( 0 );
601 if (!acsi_wait_for_IRQ( 10 )) return( 0 );
602 acsi_getstatus();
603 if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
604 if (!acsi_wait_for_IRQ( 10 )) return( 0 );
605 acsi_getstatus();
606 if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
607 if (!acsi_wait_for_IRQ( 10 )) return( 0 );
608 acsi_getstatus();
609 if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
610 if (!acsi_wait_for_IRQ( 10 )) return( 0 );
611 acsi_getstatus();
612 dma_cache_maintenance( VTOP(buffer), 16, 0 );
614 return( 1 );
619 * ACSI status phase: get the status byte from the bus
621 * I've seen several times that a 0xff status is read, propably due to
622 * a timing error. In this case, the procedure is repeated after the
623 * next _IRQ edge.
626 int acsi_getstatus( void )
628 { int status;
630 DISABLE_IRQ();
631 for(;;) {
632 if (!acsi_wait_for_IRQ( 100 )) {
633 acsi_delay_start();
634 return( -1 );
636 dma_wd.dma_mode_status = 0x8a;
637 MFPDELAY();
638 status = dma_wd.fdc_acces_seccount;
639 if (status != 0xff) break;
640 #ifdef DEBUG
641 printk("ACSI: skipping 0xff status byte\n" );
642 #endif
643 udelay(40);
644 acsi_wait_for_noIRQ( 20 );
646 dma_wd.dma_mode_status = 0x80;
647 udelay(40);
648 acsi_wait_for_noIRQ( 20 );
650 acsi_delay_start();
651 return( status & 0x1f ); /* mask of the device# */
655 #if (defined(CONFIG_ATARI_SLM) || defined(CONFIG_ATARI_SLM_MODULE))
657 /* Receive data in an extended status phase. Needed by SLM printer. */
659 int acsi_extstatus( char *buffer, int cnt )
661 { int status;
663 DISABLE_IRQ();
664 udelay(80);
665 while( cnt-- > 0 ) {
666 if (!acsi_wait_for_IRQ( 40 )) return( 0 );
667 dma_wd.dma_mode_status = 0x8a;
668 MFPDELAY();
669 status = dma_wd.fdc_acces_seccount;
670 MFPDELAY();
671 *buffer++ = status & 0xff;
672 udelay(40);
674 return( 1 );
678 /* Finish an extended status phase */
680 void acsi_end_extstatus( void )
683 dma_wd.dma_mode_status = 0x80;
684 udelay(40);
685 acsi_wait_for_noIRQ( 20 );
686 acsi_delay_start();
690 /* Send data in an extended command phase */
692 int acsi_extcmd( unsigned char *buffer, int cnt )
695 while( cnt-- > 0 ) {
696 DMA_LONG_WRITE( *buffer++, 0x8a );
697 udelay(20);
698 if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
700 return( 1 );
703 #endif
706 static void acsi_print_error( const unsigned char *errblk, int dev )
708 { int atari_err, i, errcode;
709 struct acsi_error *arr;
711 atari_err = acsi_info[dev].old_atari_disk;
712 if (atari_err)
713 errcode = errblk[0] & 0x7f;
714 else
715 if ((errblk[0] & 0x70) == 0x70)
716 errcode = errblk[2] & 0x0f;
717 else
718 errcode = errblk[0] & 0x0f;
720 printk( KERN_ERR "ACSI error 0x%02x", errcode );
722 if (errblk[0] & 0x80)
723 printk( " for sector %d",
724 ((errblk[1] & 0x1f) << 16) |
725 (errblk[2] << 8) | errblk[0] );
727 arr = atari_err ? atari_acsi_errors : scsi_acsi_errors;
728 i = atari_err ? sizeof(atari_acsi_errors)/sizeof(*atari_acsi_errors) :
729 sizeof(scsi_acsi_errors)/sizeof(*scsi_acsi_errors);
731 for( --i; i >= 0; --i )
732 if (arr[i].code == errcode) break;
733 if (i >= 0)
734 printk( ": %s\n", arr[i].text );
737 /*******************************************************************
739 * ACSI interrupt routine
740 * Test, if this is a ACSI interrupt and call the irq handler
741 * Otherwise ignore this interrupt.
743 *******************************************************************/
745 static void acsi_interrupt(int irq, void *data, struct pt_regs *fp )
747 { void (*acsi_irq_handler)(void) = DEVICE_INTR;
749 DEVICE_INTR = NULL;
750 CLEAR_TIMER();
752 if (!acsi_irq_handler)
753 acsi_irq_handler = unexpected_acsi_interrupt;
754 acsi_irq_handler();
758 /******************************************************************
760 * The Interrupt handlers
762 *******************************************************************/
765 static void unexpected_acsi_interrupt( void )
768 printk( KERN_WARNING "Unexpected ACSI interrupt\n" );
772 /* This function is called in case of errors. Because we cannot reset
773 * the ACSI bus or a single device, there is no other choice than
774 * retrying several times :-(
777 static void bad_rw_intr( void )
780 if (!CURRENT)
781 return;
783 if (++CURRENT->errors >= MAX_ERRORS)
784 end_request(0);
785 /* Otherwise just retry */
789 static void read_intr( void )
791 { int status;
793 status = acsi_getstatus();
794 if (status != 0) {
795 int dev = DEVICE_NR(MINOR(CURRENT->rq_dev));
796 printk( KERN_ERR "ad%c: ", dev+'a' );
797 if (!acsi_reqsense( acsi_buffer, acsi_info[dev].target,
798 acsi_info[dev].lun))
799 printk( "ACSI error and REQUEST SENSE failed (status=0x%02x)\n", status );
800 else {
801 acsi_print_error( acsi_buffer, dev );
802 if (CARTRCH_STAT( dev, acsi_buffer ))
803 acsi_info[dev].changed = 1;
805 ENABLE_IRQ();
806 bad_rw_intr();
807 redo_acsi_request();
808 return;
811 dma_cache_maintenance( VTOP(CurrentBuffer), CurrentNSect*512, 0 );
812 if (CurrentBuffer == acsi_buffer)
813 copy_from_acsibuffer();
815 do_end_requests();
816 redo_acsi_request();
820 static void write_intr(void)
822 { int status;
824 status = acsi_getstatus();
825 if (status != 0) {
826 int dev = DEVICE_NR(MINOR(CURRENT->rq_dev));
827 printk( KERN_ERR "ad%c: ", dev+'a' );
828 if (!acsi_reqsense( acsi_buffer, acsi_info[dev].target,
829 acsi_info[dev].lun))
830 printk( "ACSI error and REQUEST SENSE failed (status=0x%02x)\n", status );
831 else {
832 acsi_print_error( acsi_buffer, dev );
833 if (CARTRCH_STAT( dev, acsi_buffer ))
834 acsi_info[dev].changed = 1;
836 bad_rw_intr();
837 redo_acsi_request();
838 return;
841 do_end_requests();
842 redo_acsi_request();
846 static void acsi_times_out( unsigned long dummy )
849 DISABLE_IRQ();
850 if (!DEVICE_INTR) return;
852 DEVICE_INTR = NULL;
853 printk( KERN_ERR "ACSI timeout\n" );
854 if (!CURRENT) return;
855 if (++CURRENT->errors >= MAX_ERRORS) {
856 #ifdef DEBUG
857 printk( KERN_ERR "ACSI: too many errors.\n" );
858 #endif
859 end_request(0);
862 redo_acsi_request();
867 /***********************************************************************
869 * Scatter-gather utility functions
871 ***********************************************************************/
874 static void copy_to_acsibuffer( void )
876 { int i;
877 char *src, *dst;
878 struct buffer_head *bh;
880 src = CURRENT->buffer;
881 dst = acsi_buffer;
882 bh = CURRENT->bh;
884 if (!bh)
885 memcpy( dst, src, CurrentNSect*512 );
886 else
887 for( i = 0; i < CurrentNReq; ++i ) {
888 memcpy( dst, src, bh->b_size );
889 dst += bh->b_size;
890 if ((bh = bh->b_reqnext))
891 src = bh->b_data;
896 static void copy_from_acsibuffer( void )
898 { int i;
899 char *src, *dst;
900 struct buffer_head *bh;
902 dst = CURRENT->buffer;
903 src = acsi_buffer;
904 bh = CURRENT->bh;
906 if (!bh)
907 memcpy( dst, src, CurrentNSect*512 );
908 else
909 for( i = 0; i < CurrentNReq; ++i ) {
910 memcpy( dst, src, bh->b_size );
911 src += bh->b_size;
912 if ((bh = bh->b_reqnext))
913 dst = bh->b_data;
918 static void do_end_requests( void )
920 { int i, n;
922 if (!CURRENT->bh) {
923 CURRENT->nr_sectors -= CurrentNSect;
924 CURRENT->current_nr_sectors -= CurrentNSect;
925 CURRENT->sector += CurrentNSect;
926 if (CURRENT->nr_sectors == 0)
927 end_request(1);
929 else {
930 for( i = 0; i < CurrentNReq; ++i ) {
931 n = CURRENT->bh->b_size >> 9;
932 CURRENT->nr_sectors -= n;
933 CURRENT->current_nr_sectors -= n;
934 CURRENT->sector += n;
935 end_request(1);
943 /***********************************************************************
945 * do_acsi_request and friends
947 ***********************************************************************/
949 static void do_acsi_request( void )
952 stdma_lock( acsi_interrupt, NULL );
953 redo_acsi_request();
957 static void redo_acsi_request( void )
959 { unsigned block, dev, target, lun, nsect;
960 char *buffer;
961 unsigned long pbuffer;
962 struct buffer_head *bh;
964 if (CURRENT && CURRENT->rq_status == RQ_INACTIVE) {
965 if (!DEVICE_INTR) {
966 ENABLE_IRQ();
967 stdma_release();
969 return;
972 if (DEVICE_INTR)
973 return;
975 repeat:
976 CLEAR_TIMER();
977 /* Another check here: An interrupt or timer event could have
978 * happened since the last check!
980 if (CURRENT && CURRENT->rq_status == RQ_INACTIVE) {
981 if (!DEVICE_INTR) {
982 ENABLE_IRQ();
983 stdma_release();
985 return;
987 if (DEVICE_INTR)
988 return;
990 if (!CURRENT) {
991 CLEAR_INTR;
992 ENABLE_IRQ();
993 stdma_release();
994 return;
997 if (MAJOR(CURRENT->rq_dev) != MAJOR_NR)
998 panic(DEVICE_NAME ": request list destroyed");
999 if (CURRENT->bh) {
1000 if (!CURRENT->bh && !buffer_locked(CURRENT->bh))
1001 panic(DEVICE_NAME ": block not locked");
1004 dev = MINOR(CURRENT->rq_dev);
1005 block = CURRENT->sector;
1006 if (DEVICE_NR(dev) >= NDevices ||
1007 block+CURRENT->nr_sectors >= acsi_part[dev].nr_sects) {
1008 #ifdef DEBUG
1009 printk( "ad%c: attempted access for blocks %d...%ld past end of device at block %ld.\n",
1010 DEVICE_NR(dev)+'a',
1011 block, block + CURRENT->nr_sectors - 1,
1012 acsi_part[dev].nr_sects);
1013 #endif
1014 end_request(0);
1015 goto repeat;
1017 if (acsi_info[DEVICE_NR(dev)].changed) {
1018 printk( KERN_NOTICE "ad%c: request denied because cartridge has "
1019 "been changed.\n", DEVICE_NR(dev)+'a' );
1020 end_request(0);
1021 goto repeat;
1024 block += acsi_part[dev].start_sect;
1025 target = acsi_info[DEVICE_NR(dev)].target;
1026 lun = acsi_info[DEVICE_NR(dev)].lun;
1028 /* Find out how many sectors should be transferred from/to
1029 * consecutive buffers and thus can be done with a single command.
1031 buffer = CURRENT->buffer;
1032 pbuffer = VTOP(buffer);
1033 nsect = CURRENT->current_nr_sectors;
1034 CurrentNReq = 1;
1036 if ((bh = CURRENT->bh) && bh != CURRENT->bhtail) {
1037 if (!STRAM_ADDR(pbuffer)) {
1038 /* If transfer is done via the ACSI buffer anyway, we can
1039 * assemble as much bh's as fit in the buffer.
1041 while( (bh = bh->b_reqnext) ) {
1042 if (nsect + (bh->b_size>>9) > ACSI_BUFFER_SECTORS) break;
1043 nsect += bh->b_size >> 9;
1044 ++CurrentNReq;
1045 if (bh == CURRENT->bhtail) break;
1047 buffer = acsi_buffer;
1048 pbuffer = phys_acsi_buffer;
1050 else {
1051 unsigned long pendadr, pnewadr;
1052 pendadr = pbuffer + nsect*512;
1053 while( (bh = bh->b_reqnext) ) {
1054 pnewadr = VTOP(bh->b_data);
1055 if (!STRAM_ADDR(pnewadr) || pendadr != pnewadr) break;
1056 nsect += bh->b_size >> 9;
1057 pendadr = pnewadr + bh->b_size;
1058 ++CurrentNReq;
1059 if (bh == CURRENT->bhtail) break;
1063 else {
1064 if (!STRAM_ADDR(pbuffer)) {
1065 buffer = acsi_buffer;
1066 pbuffer = phys_acsi_buffer;
1067 if (nsect > ACSI_BUFFER_SECTORS)
1068 nsect = ACSI_BUFFER_SECTORS;
1071 CurrentBuffer = buffer;
1072 CurrentNSect = nsect;
1074 if (CURRENT->cmd == WRITE) {
1075 CMDSET_TARG_LUN( write_cmd, target, lun );
1076 CMDSET_BLOCK( write_cmd, block );
1077 CMDSET_LEN( write_cmd, nsect );
1078 if (buffer == acsi_buffer)
1079 copy_to_acsibuffer();
1080 dma_cache_maintenance( pbuffer, nsect*512, 1 );
1081 SET_INTR(write_intr);
1082 if (!acsicmd_dma( write_cmd, buffer, nsect, 1, 1)) {
1083 CLEAR_INTR;
1084 printk( KERN_ERR "ACSI (write): Timeout in command block\n" );
1085 bad_rw_intr();
1086 goto repeat;
1088 SET_TIMER();
1089 return;
1091 if (CURRENT->cmd == READ) {
1092 CMDSET_TARG_LUN( read_cmd, target, lun );
1093 CMDSET_BLOCK( read_cmd, block );
1094 CMDSET_LEN( read_cmd, nsect );
1095 SET_INTR(read_intr);
1096 if (!acsicmd_dma( read_cmd, buffer, nsect, 0, 1)) {
1097 CLEAR_INTR;
1098 printk( KERN_ERR "ACSI (read): Timeout in command block\n" );
1099 bad_rw_intr();
1100 goto repeat;
1102 SET_TIMER();
1103 return;
1105 panic("unknown ACSI command");
1110 /***********************************************************************
1112 * Misc functions: ioctl, open, release, check_change, ...
1114 ***********************************************************************/
1117 static int acsi_ioctl( struct inode *inode, struct file *file,
1118 unsigned int cmd, unsigned long arg )
1119 { int dev;
1121 if (!inode)
1122 return -EINVAL;
1123 dev = DEVICE_NR(MINOR(inode->i_rdev));
1124 if (dev >= NDevices)
1125 return -EINVAL;
1126 switch (cmd) {
1127 case HDIO_GETGEO:
1128 /* HDIO_GETGEO is supported more for getting the partition's start
1129 * sector... */
1130 { struct hd_geometry *geo = (struct hd_geometry *)arg;
1131 /* just fake some geometry here, it's nonsense anyway; to make it
1132 * easy, use Adaptec's usual 64/32 mapping */
1133 put_user( 64, &geo->heads );
1134 put_user( 32, &geo->sectors );
1135 put_user( acsi_info[dev].size >> 11, &geo->cylinders );
1136 put_user( acsi_part[MINOR(inode->i_rdev)].start_sect, &geo->start );
1137 return 0;
1140 case SCSI_IOCTL_GET_IDLUN:
1141 /* SCSI compatible GET_IDLUN call to get target's ID and LUN number */
1142 put_user( acsi_info[dev].target | (acsi_info[dev].lun << 8),
1143 &((Scsi_Idlun *) arg)->dev_id );
1144 put_user( 0, &((Scsi_Idlun *) arg)->host_unique_id );
1145 return 0;
1147 case BLKGETSIZE: /* Return device size */
1148 return put_user(acsi_part[MINOR(inode->i_rdev)].nr_sects,
1149 (long *) arg);
1151 case BLKFLSBUF:
1152 if(!capable(CAP_SYS_ADMIN)) return -EACCES;
1153 if(!inode->i_rdev) return -EINVAL;
1154 fsync_dev(inode->i_rdev);
1155 invalidate_buffers(inode->i_rdev);
1156 return 0;
1158 case BLKRRPART: /* Re-read partition tables */
1159 if (!capable(CAP_SYS_ADMIN))
1160 return -EACCES;
1161 return revalidate_acsidisk(inode->i_rdev, 1);
1162 RO_IOCTLS(inode->i_rdev,arg);
1163 default:
1164 return -EINVAL;
1170 * Open a device, check for read-only and lock the medium if it is
1171 * removable.
1173 * Changes by Martin Rogge, 9th Aug 1995:
1174 * Check whether check_disk_change (and therefore revalidate_acsidisk)
1175 * was successful. They fail when there is no medium in the drive.
1177 * The problem of media being changed during an operation can be
1178 * ignored because of the prevent_removal code.
1180 * Added check for the validity of the device number.
1184 static int acsi_open( struct inode * inode, struct file * filp )
1186 int device;
1187 struct acsi_info_struct *aip;
1189 device = DEVICE_NR(MINOR(inode->i_rdev));
1190 if (device >= NDevices)
1191 return -ENXIO;
1192 aip = &acsi_info[device];
1193 while (busy[device])
1194 sleep_on(&busy_wait);
1196 if (access_count[device] == 0 && aip->removable) {
1197 #if 0
1198 aip->changed = 1; /* safety first */
1199 #endif
1200 check_disk_change( inode->i_rdev );
1201 if (aip->changed) /* revalidate was not successful (no medium) */
1202 return -ENXIO;
1203 acsi_prevent_removal(device, 1);
1205 access_count[device]++;
1206 MOD_INC_USE_COUNT;
1208 if (filp && filp->f_mode) {
1209 check_disk_change( inode->i_rdev );
1210 if (filp->f_mode & 2) {
1211 if (aip->read_only) {
1212 acsi_release( inode, filp );
1213 return -EROFS;
1218 return 0;
1222 * Releasing a block device means we sync() it, so that it can safely
1223 * be forgotten about...
1226 static int acsi_release( struct inode * inode, struct file * file )
1228 int device;
1230 sync_dev(inode->i_rdev);
1232 device = DEVICE_NR(MINOR(inode->i_rdev));
1233 if (--access_count[device] == 0 && acsi_info[device].removable)
1234 acsi_prevent_removal(device, 0);
1235 MOD_DEC_USE_COUNT;
1236 return( 0 );
1240 * Prevent or allow a media change for removable devices.
1243 static void acsi_prevent_removal(int device, int flag)
1245 stdma_lock( NULL, NULL );
1247 CMDSET_TARG_LUN(pa_med_rem_cmd, acsi_info[device].target,
1248 acsi_info[device].lun);
1249 CMDSET_LEN( pa_med_rem_cmd, flag );
1251 if (acsicmd_nodma(pa_med_rem_cmd, 0) && acsi_wait_for_IRQ(3*HZ))
1252 acsi_getstatus();
1253 /* Do not report errors -- some devices may not know this command. */
1255 ENABLE_IRQ();
1256 stdma_release();
1259 static int acsi_media_change (dev_t dev)
1261 int device = DEVICE_NR(MINOR(dev));
1262 struct acsi_info_struct *aip;
1264 aip = &acsi_info[device];
1265 if (!aip->removable)
1266 return 0;
1268 if (aip->changed)
1269 /* We can be sure that the medium has been changed -- REQUEST
1270 * SENSE has reported this earlier.
1272 return 1;
1274 /* If the flag isn't set, make a test by reading block 0.
1275 * If errors happen, it seems to be better to say "changed"...
1277 stdma_lock( NULL, NULL );
1278 CMDSET_TARG_LUN(read_cmd, aip->target, aip->lun);
1279 CMDSET_BLOCK( read_cmd, 0 );
1280 CMDSET_LEN( read_cmd, 1 );
1281 if (acsicmd_dma(read_cmd, acsi_buffer, 1, 0, 0) &&
1282 acsi_wait_for_IRQ(3*HZ)) {
1283 if (acsi_getstatus()) {
1284 if (acsi_reqsense(acsi_buffer, aip->target, aip->lun)) {
1285 if (CARTRCH_STAT(device, acsi_buffer))
1286 aip->changed = 1;
1288 else {
1289 printk( KERN_ERR "ad%c: REQUEST SENSE failed in test for "
1290 "medium change; assuming a change\n", device + 'a' );
1291 aip->changed = 1;
1295 else {
1296 printk( KERN_ERR "ad%c: Test for medium changed timed out; "
1297 "assuming a change\n", device + 'a');
1298 aip->changed = 1;
1300 ENABLE_IRQ();
1301 stdma_release();
1303 /* Now, after reading a block, the changed status is surely valid. */
1304 return aip->changed;
1308 static int acsi_change_blk_size( int target, int lun)
1310 { int i;
1312 for (i=0; i<12; i++)
1313 acsi_buffer[i] = 0;
1315 acsi_buffer[3] = 8;
1316 acsi_buffer[10] = 2;
1317 CMDSET_TARG_LUN( modeselect_cmd, target, lun);
1319 if (!acsicmd_dma( modeselect_cmd, acsi_buffer, 1,1,0) ||
1320 !acsi_wait_for_IRQ( 3*HZ ) ||
1321 acsi_getstatus() != 0 ) {
1322 return(0);
1324 return(1);
1328 static int acsi_mode_sense( int target, int lun, SENSE_DATA *sd )
1331 int page;
1333 CMDSET_TARG_LUN( modesense_cmd, target, lun );
1334 for (page=0; page<4; page++) {
1335 modesense_cmd[2] = page;
1336 if (!acsicmd_dma( modesense_cmd, acsi_buffer, 1, 0, 0 ) ||
1337 !acsi_wait_for_IRQ( 3*HZ ) ||
1338 acsi_getstatus())
1339 continue;
1341 /* read twice to jump over the second 16-byte border! */
1342 udelay(300);
1343 if (acsi_wait_for_noIRQ( 20 ) &&
1344 acsicmd_nodma( modesense_cmd, 0 ) &&
1345 acsi_wait_for_IRQ( 3*HZ ) &&
1346 acsi_getstatus() == 0)
1347 break;
1349 if (page == 4) {
1350 return(0);
1353 dma_cache_maintenance( phys_acsi_buffer, sizeof(SENSE_DATA), 0 );
1354 *sd = *(SENSE_DATA *)acsi_buffer;
1356 /* Validity check, depending on type of data */
1358 switch( SENSE_TYPE(*sd) ) {
1360 case SENSE_TYPE_ATARI:
1361 if (CAPACITY(*sd) == 0)
1362 goto invalid_sense;
1363 break;
1365 case SENSE_TYPE_SCSI:
1366 if (sd->scsi.descriptor_size != 8)
1367 goto invalid_sense;
1368 break;
1370 case SENSE_TYPE_UNKNOWN:
1372 printk( KERN_ERR "ACSI target %d, lun %d: Cannot interpret "
1373 "sense data\n", target, lun );
1375 invalid_sense:
1377 #ifdef DEBUG
1378 { int i;
1379 printk( "Mode sense data for ACSI target %d, lun %d seem not valid:",
1380 target, lun );
1381 for( i = 0; i < sizeof(SENSE_DATA); ++i )
1382 printk( "%02x ", (unsigned char)acsi_buffer[i] );
1383 printk( "\n" );
1385 #endif
1386 return( 0 );
1389 return( 1 );
1394 /*******************************************************************
1396 * Initialization
1398 ********************************************************************/
1401 static struct gendisk acsi_gendisk = {
1402 MAJOR_NR, /* Major number */
1403 "ad", /* Major name */
1404 4, /* Bits to shift to get real from partition */
1405 1 << 4, /* Number of partitions per real */
1406 MAX_DEV, /* maximum number of real */
1407 #ifdef MODULE
1408 NULL, /* called from init_module() */
1409 #else
1410 acsi_geninit, /* init function */
1411 #endif
1412 acsi_part, /* hd struct */
1413 acsi_sizes, /* block sizes */
1414 0, /* number */
1415 (void *)acsi_info, /* internal */
1416 NULL /* next */
1419 #define MAX_SCSI_DEVICE_CODE 10
1421 static const char *const scsi_device_types[MAX_SCSI_DEVICE_CODE] =
1423 "Direct-Access ",
1424 "Sequential-Access",
1425 "Printer ",
1426 "Processor ",
1427 "WORM ",
1428 "CD-ROM ",
1429 "Scanner ",
1430 "Optical Device ",
1431 "Medium Changer ",
1432 "Communications "
1435 static void print_inquiry(unsigned char *data)
1437 int i;
1439 printk(KERN_INFO " Vendor: ");
1440 for (i = 8; i < 16; i++)
1442 if (data[i] >= 0x20 && i < data[4] + 5)
1443 printk("%c", data[i]);
1444 else
1445 printk(" ");
1448 printk(" Model: ");
1449 for (i = 16; i < 32; i++)
1451 if (data[i] >= 0x20 && i < data[4] + 5)
1452 printk("%c", data[i]);
1453 else
1454 printk(" ");
1457 printk(" Rev: ");
1458 for (i = 32; i < 36; i++)
1460 if (data[i] >= 0x20 && i < data[4] + 5)
1461 printk("%c", data[i]);
1462 else
1463 printk(" ");
1466 printk("\n");
1468 i = data[0] & 0x1f;
1470 printk(KERN_INFO " Type: %s ", (i < MAX_SCSI_DEVICE_CODE
1471 ? scsi_device_types[i]
1472 : "Unknown "));
1473 printk(" ANSI SCSI revision: %02x", data[2] & 0x07);
1474 if ((data[2] & 0x07) == 1 && (data[3] & 0x0f) == 1)
1475 printk(" CCS\n");
1476 else
1477 printk("\n");
1482 * Changes by Martin Rogge, 9th Aug 1995:
1483 * acsi_devinit has been taken out of acsi_geninit, because it needs
1484 * to be called from revalidate_acsidisk. The result of request sense
1485 * is now checked for DRIVE NOT READY.
1487 * The structure *aip is only valid when acsi_devinit returns
1488 * DEV_SUPPORTED.
1492 #define DEV_NONE 0
1493 #define DEV_UNKNOWN 1
1494 #define DEV_SUPPORTED 2
1495 #define DEV_SLM 3
1497 static int acsi_devinit(struct acsi_info_struct *aip)
1499 int status, got_inquiry;
1500 SENSE_DATA sense;
1501 unsigned char reqsense, extsense;
1503 /*****************************************************************/
1504 /* Do a TEST UNIT READY command to test the presence of a device */
1505 /*****************************************************************/
1507 CMDSET_TARG_LUN(tur_cmd, aip->target, aip->lun);
1508 if (!acsicmd_nodma(tur_cmd, 0)) {
1509 /* timed out -> no device here */
1510 #ifdef DEBUG_DETECT
1511 printk("target %d lun %d: timeout\n", aip->target, aip->lun);
1512 #endif
1513 return DEV_NONE;
1516 /*************************/
1517 /* Read the ACSI status. */
1518 /*************************/
1520 status = acsi_getstatus();
1521 if (status) {
1522 if (status == 0x12) {
1523 /* The SLM printer should be the only device that
1524 * responds with the error code in the status byte. In
1525 * correct status bytes, bit 4 is never set.
1527 printk( KERN_INFO "Detected SLM printer at id %d lun %d\n",
1528 aip->target, aip->lun);
1529 return DEV_SLM;
1531 /* ignore CHECK CONDITION, since some devices send a
1532 UNIT ATTENTION */
1533 if ((status & 0x1e) != 0x2) {
1534 #ifdef DEBUG_DETECT
1535 printk("target %d lun %d: status %d\n",
1536 aip->target, aip->lun, status);
1537 #endif
1538 return DEV_UNKNOWN;
1542 /*******************************/
1543 /* Do a REQUEST SENSE command. */
1544 /*******************************/
1546 if (!acsi_reqsense(acsi_buffer, aip->target, aip->lun)) {
1547 printk( KERN_WARNING "acsi_reqsense failed\n");
1548 acsi_buffer[0] = 0;
1549 acsi_buffer[2] = UNIT_ATTENTION;
1551 reqsense = acsi_buffer[0];
1552 extsense = acsi_buffer[2] & 0xf;
1553 if (status) {
1554 if ((reqsense & 0x70) == 0x70) { /* extended sense */
1555 if (extsense != UNIT_ATTENTION &&
1556 extsense != NOT_READY) {
1557 #ifdef DEBUG_DETECT
1558 printk("target %d lun %d: extended sense %d\n",
1559 aip->target, aip->lun, extsense);
1560 #endif
1561 return DEV_UNKNOWN;
1564 else {
1565 if (reqsense & 0x7f) {
1566 #ifdef DEBUG_DETECT
1567 printk("target %d lun %d: sense %d\n",
1568 aip->target, aip->lun, reqsense);
1569 #endif
1570 return DEV_UNKNOWN;
1574 else
1575 if (reqsense == 0x4) { /* SH204 Bug workaround */
1576 #ifdef DEBUG_DETECT
1577 printk("target %d lun %d status=0 sense=4\n",
1578 aip->target, aip->lun);
1579 #endif
1580 return DEV_UNKNOWN;
1583 /***********************************************************/
1584 /* Do an INQUIRY command to get more infos on this device. */
1585 /***********************************************************/
1587 /* Assume default values */
1588 aip->removable = 1;
1589 aip->read_only = 0;
1590 aip->old_atari_disk = 0;
1591 aip->changed = (extsense == NOT_READY); /* medium inserted? */
1592 aip->size = DEFAULT_SIZE;
1593 got_inquiry = 0;
1594 /* Fake inquiry result for old atari disks */
1595 memcpy(acsi_buffer, "\000\000\001\000 Adaptec 40xx"
1596 " ", 40);
1597 CMDSET_TARG_LUN(inquiry_cmd, aip->target, aip->lun);
1598 if (acsicmd_dma(inquiry_cmd, acsi_buffer, 1, 0, 0) &&
1599 acsi_getstatus() == 0) {
1600 acsicmd_nodma(inquiry_cmd, 0);
1601 acsi_getstatus();
1602 dma_cache_maintenance( phys_acsi_buffer, 256, 0 );
1603 got_inquiry = 1;
1604 aip->removable = !!(acsi_buffer[1] & 0x80);
1606 if (aip->type == NONE) /* only at boot time */
1607 print_inquiry(acsi_buffer);
1608 switch(acsi_buffer[0]) {
1609 case TYPE_DISK:
1610 aip->type = HARDDISK;
1611 break;
1612 case TYPE_ROM:
1613 aip->type = CDROM;
1614 aip->read_only = 1;
1615 break;
1616 default:
1617 return DEV_UNKNOWN;
1619 /****************************/
1620 /* Do a MODE SENSE command. */
1621 /****************************/
1623 if (!acsi_mode_sense(aip->target, aip->lun, &sense)) {
1624 printk( KERN_WARNING "No mode sense data.\n" );
1625 return DEV_UNKNOWN;
1627 if ((SECTOR_SIZE(sense) != 512) &&
1628 ((aip->type != CDROM) ||
1629 !acsi_change_blk_size(aip->target, aip->lun) ||
1630 !acsi_mode_sense(aip->target, aip->lun, &sense) ||
1631 (SECTOR_SIZE(sense) != 512))) {
1632 printk( KERN_WARNING "Sector size != 512 not supported.\n" );
1633 return DEV_UNKNOWN;
1635 /* There are disks out there that claim to have 0 sectors... */
1636 if (CAPACITY(sense))
1637 aip->size = CAPACITY(sense); /* else keep DEFAULT_SIZE */
1638 if (!got_inquiry && SENSE_TYPE(sense) == SENSE_TYPE_ATARI) {
1639 /* If INQUIRY failed and the sense data suggest an old
1640 * Atari disk (SH20x, Megafile), the disk is not removable
1642 aip->removable = 0;
1643 aip->old_atari_disk = 1;
1646 /******************/
1647 /* We've done it. */
1648 /******************/
1650 return DEV_SUPPORTED;
1653 EXPORT_SYMBOL(acsi_delay_start);
1654 EXPORT_SYMBOL(acsi_delay_end);
1655 EXPORT_SYMBOL(acsi_wait_for_IRQ);
1656 EXPORT_SYMBOL(acsi_wait_for_noIRQ);
1657 EXPORT_SYMBOL(acsicmd_nodma);
1658 EXPORT_SYMBOL(acsi_getstatus);
1659 EXPORT_SYMBOL(acsi_buffer);
1660 EXPORT_SYMBOL(phys_acsi_buffer);
1662 #ifdef CONFIG_ATARI_SLM_MODULE
1663 void acsi_attach_SLMs( int (*attach_func)( int, int ) );
1665 EXPORT_SYMBOL(acsi_extstatus);
1666 EXPORT_SYMBOL(acsi_end_extstatus);
1667 EXPORT_SYMBOL(acsi_extcmd);
1668 EXPORT_SYMBOL(acsi_attach_SLMs);
1670 /* to remember IDs of SLM devices, SLM module is loaded later
1671 * (index is target#, contents is lun#, -1 means "no SLM") */
1672 int SLM_devices[8];
1673 #endif
1675 static void acsi_geninit( struct gendisk *gd )
1677 int i, target, lun;
1678 struct acsi_info_struct *aip;
1679 #ifdef CONFIG_ATARI_SLM
1680 int n_slm = 0;
1681 #endif
1683 printk( KERN_INFO "Probing ACSI devices:\n" );
1684 NDevices = 0;
1685 #ifdef CONFIG_ATARI_SLM_MODULE
1686 for( i = 0; i < 8; ++i )
1687 SLM_devices[i] = -1;
1688 #endif
1689 stdma_lock(NULL, NULL);
1691 for (target = 0; target < 8 && NDevices < MAX_DEV; ++target) {
1692 lun = 0;
1693 do {
1694 aip = &acsi_info[NDevices];
1695 aip->type = NONE;
1696 aip->target = target;
1697 aip->lun = lun;
1698 i = acsi_devinit(aip);
1699 switch (i) {
1700 case DEV_SUPPORTED:
1701 printk( KERN_INFO "Detected ");
1702 switch (aip->type) {
1703 case HARDDISK:
1704 printk("disk");
1705 break;
1706 case CDROM:
1707 printk("cdrom");
1708 break;
1709 default:
1711 printk(" ad%c at id %d lun %d ",
1712 'a' + NDevices, target, lun);
1713 if (aip->removable)
1714 printk("(removable) ");
1715 if (aip->read_only)
1716 printk("(read-only) ");
1717 if (aip->size == DEFAULT_SIZE)
1718 printk(" unkown size, using default ");
1719 printk("%ld MByte\n",
1720 (aip->size*512+1024*1024/2)/(1024*1024));
1721 NDevices++;
1722 break;
1723 case DEV_SLM:
1724 #ifdef CONFIG_ATARI_SLM
1725 n_slm += attach_slm( target, lun );
1726 break;
1727 #endif
1728 #ifdef CONFIG_ATARI_SLM_MODULE
1729 SLM_devices[target] = lun;
1730 break;
1731 #endif
1732 /* neither of the above: fall through to unknown device */
1733 case DEV_UNKNOWN:
1734 printk( KERN_INFO "Detected unsupported device at "
1735 "id %d lun %d\n", target, lun);
1736 break;
1739 #ifdef CONFIG_ACSI_MULTI_LUN
1740 while (i != DEV_NONE && ++lun < MAX_LUN);
1741 #else
1742 while (0);
1743 #endif
1746 /* reenable interrupt */
1747 ENABLE_IRQ();
1748 stdma_release();
1750 #ifndef CONFIG_ATARI_SLM
1751 printk( KERN_INFO "Found %d ACSI device(s) total.\n", NDevices );
1752 #else
1753 printk( KERN_INFO "Found %d ACSI device(s) and %d SLM printer(s) total.\n",
1754 NDevices, n_slm );
1755 #endif
1757 for( i = 0; i < NDevices; ++i ) {
1758 acsi_part[i<<4].start_sect = 0;
1759 acsi_part[i<<4].nr_sects = acsi_info[i].size;
1761 acsi_gendisk.nr_real = NDevices;
1762 for( i = 0; i < (MAX_DEV << 4); i++ )
1763 acsi_blocksizes[i] = 1024;
1764 blksize_size[MAJOR_NR] = acsi_blocksizes;
1767 #ifdef CONFIG_ATARI_SLM_MODULE
1768 /* call attach_slm() for each device that is a printer; needed for init of SLM
1769 * driver as a module, since it's not yet present if acsi.c is inited and thus
1770 * the bus gets scanned. */
1771 void acsi_attach_SLMs( int (*attach_func)( int, int ) )
1773 int i, n = 0;
1775 for( i = 0; i < 8; ++i )
1776 if (SLM_devices[i] >= 0)
1777 n += (*attach_func)( i, SLM_devices[i] );
1778 printk( KERN_INFO "Found %d SLM printer(s) total.\n", n );
1780 #endif /* CONFIG_ATARI_SLM_MODULE */
1782 static struct file_operations acsi_fops = {
1783 NULL, /* lseek - default */
1784 block_read, /* read - general block-dev read */
1785 block_write, /* write - general block-dev write */
1786 NULL, /* readdir - bad */
1787 NULL, /* select */
1788 acsi_ioctl, /* ioctl */
1789 NULL, /* mmap */
1790 acsi_open, /* open */
1791 NULL, /* flush */
1792 acsi_release, /* release */
1793 block_fsync, /* fsync */
1794 NULL, /* fasync */
1795 acsi_media_change, /* media_change */
1796 acsi_revalidate, /* revalidate */
1800 int acsi_init( void )
1803 if (!MACH_IS_ATARI || !ATARIHW_PRESENT(ACSI))
1804 return 0;
1806 if (register_blkdev( MAJOR_NR, "ad", &acsi_fops )) {
1807 printk( KERN_ERR "Unable to get major %d for ACSI\n", MAJOR_NR );
1808 return -EBUSY;
1811 if (!(acsi_buffer =
1812 (char *)atari_stram_alloc( ACSI_BUFFER_SIZE, NULL, "acsi" ))) {
1813 printk( KERN_ERR "Unable to get ACSI ST-Ram buffer.\n" );
1814 unregister_blkdev( MAJOR_NR, "ad" );
1815 return -ENOMEM;
1817 phys_acsi_buffer = VTOP( acsi_buffer );
1818 STramMask = ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000 : 0xff000000;
1820 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
1821 read_ahead[MAJOR_NR] = 8; /* 8 sector (4kB) read-ahead */
1822 acsi_gendisk.next = gendisk_head;
1823 gendisk_head = &acsi_gendisk;
1825 #ifdef CONFIG_ATARI_SLM
1826 return( slm_init() );
1827 #else
1828 return 0;
1829 #endif
1833 #ifdef MODULE
1834 int init_module(void)
1836 int err;
1838 if ((err = acsi_init()))
1839 return( err );
1840 printk( KERN_INFO "ACSI driver loaded as module.\n");
1841 acsi_geninit( &(struct gendisk){ 0,0,0,0,0,0,0,0,0,0,0 } );
1842 return( 0 );
1845 void cleanup_module(void)
1847 struct gendisk ** gdp;
1849 del_timer( &acsi_timer );
1850 blk_dev[MAJOR_NR].request_fn = 0;
1851 atari_stram_free( acsi_buffer );
1853 if (unregister_blkdev( MAJOR_NR, "ad" ) != 0)
1854 printk( KERN_ERR "acsi: cleanup_module failed\n");
1856 for (gdp = &gendisk_head; *gdp; gdp = &((*gdp)->next))
1857 if (*gdp == &acsi_gendisk)
1858 break;
1859 if (!*gdp)
1860 printk( KERN_ERR "acsi: entry in disk chain missing!\n" );
1861 else
1862 *gdp = (*gdp)->next;
1864 #endif
1866 #define DEVICE_BUSY busy[device]
1867 #define USAGE access_count[device]
1868 #define GENDISK_STRUCT acsi_gendisk
1871 * This routine is called to flush all partitions and partition tables
1872 * for a changed scsi disk, and then re-read the new partition table.
1873 * If we are revalidating a disk because of a media change, then we
1874 * enter with usage == 0. If we are using an ioctl, we automatically have
1875 * usage == 1 (we need an open channel to use an ioctl :-), so this
1876 * is our limit.
1878 * Changes by Martin Rogge, 9th Aug 1995:
1879 * got cd-roms to work by calling acsi_devinit. There are only two problems:
1880 * First, if there is no medium inserted, the status will remain "changed".
1881 * That is no problem at all, but our design of three-valued logic (medium
1882 * changed, medium not changed, no medium inserted).
1883 * Secondly the check could fail completely and the drive could deliver
1884 * nonsensical data, which could mess up the acsi_info[] structure. In
1885 * that case we try to make the entry safe.
1889 static int revalidate_acsidisk( int dev, int maxusage )
1891 int device;
1892 struct gendisk * gdev;
1893 int max_p, start, i;
1894 struct acsi_info_struct *aip;
1896 device = DEVICE_NR(MINOR(dev));
1897 aip = &acsi_info[device];
1898 gdev = &GENDISK_STRUCT;
1900 cli();
1901 if (DEVICE_BUSY || USAGE > maxusage) {
1902 sti();
1903 return -EBUSY;
1905 DEVICE_BUSY = 1;
1906 sti();
1908 max_p = gdev->max_p;
1909 start = device << gdev->minor_shift;
1911 for( i = max_p - 1; i >= 0 ; i-- ) {
1912 if (gdev->part[start + i].nr_sects != 0) {
1913 kdev_t devp = MKDEV(MAJOR_NR, start + i);
1914 struct super_block *sb = get_super(devp);
1916 fsync_dev(devp);
1917 if (sb)
1918 invalidate_inodes(sb);
1919 invalidate_buffers(devp);
1920 gdev->part[start + i].nr_sects = 0;
1922 gdev->part[start+i].start_sect = 0;
1925 stdma_lock( NULL, NULL );
1927 if (acsi_devinit(aip) != DEV_SUPPORTED) {
1928 printk( KERN_ERR "ACSI: revalidate failed for target %d lun %d\n",
1929 aip->target, aip->lun);
1930 aip->size = 0;
1931 aip->read_only = 1;
1932 aip->removable = 1;
1933 aip->changed = 1; /* next acsi_open will try again... */
1936 ENABLE_IRQ();
1937 stdma_release();
1939 gdev->part[start].nr_sects = aip->size;
1940 if (aip->type == HARDDISK && aip->size > 0)
1941 resetup_one_dev(gdev, device);
1943 DEVICE_BUSY = 0;
1944 wake_up(&busy_wait);
1945 return 0;
1949 static int acsi_revalidate (dev_t dev)
1951 return revalidate_acsidisk (dev, 0);