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