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