]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/block/acsi.c
[PATCH] docs: updated some code docs
[mirror_ubuntu-artful-kernel.git] / drivers / block / acsi.c
CommitLineData
1da177e4
LT
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 */
1da177e4
LT
61#include <scsi/scsi_ioctl.h>
62#include <linux/hdreg.h> /* for HDIO_GETGEO */
63#include <linux/blkpg.h>
64#include <linux/buffer_head.h>
65#include <linux/blkdev.h>
66
67#include <asm/setup.h>
68#include <asm/pgtable.h>
69#include <asm/system.h>
70#include <asm/uaccess.h>
71#include <asm/atarihw.h>
72#include <asm/atariints.h>
73#include <asm/atari_acsi.h>
74#include <asm/atari_stdma.h>
75#include <asm/atari_stram.h>
76
77static void (*do_acsi)(void) = NULL;
78static struct request_queue *acsi_queue;
79#define QUEUE (acsi_queue)
80#define CURRENT elv_next_request(acsi_queue)
81
82#define DEBUG
83#undef DEBUG_DETECT
84#undef NO_WRITE
85
86#define MAX_ERRORS 8 /* Max read/write errors/sector */
87#define MAX_LUN 8 /* Max LUNs per target */
88#define MAX_DEV 16
89
90#define ACSI_BUFFER_SIZE (16*1024) /* "normal" ACSI buffer size */
91#define ACSI_BUFFER_MINSIZE (2048) /* min. buf size if ext. DMA */
92#define ACSI_BUFFER_SIZE_ORDER 2 /* order size for above */
93#define ACSI_BUFFER_MINSIZE_ORDER 0 /* order size for above */
94#define ACSI_BUFFER_SECTORS (ACSI_BUFFER_SIZE/512)
95
96#define ACSI_BUFFER_ORDER \
97 (ATARIHW_PRESENT(EXTD_DMA) ? \
98 ACSI_BUFFER_MINSIZE_ORDER : \
99 ACSI_BUFFER_SIZE_ORDER)
100
101#define ACSI_TIMEOUT (4*HZ)
102
103/* minimum delay between two commands */
104
105#define COMMAND_DELAY 500
106
107typedef enum {
108 NONE, HARDDISK, CDROM
109} ACSI_TYPE;
110
111struct acsi_info_struct {
112 ACSI_TYPE type; /* type of device */
113 unsigned target; /* target number */
114 unsigned lun; /* LUN in target controller */
115 unsigned removable : 1; /* Flag for removable media */
116 unsigned read_only : 1; /* Flag for read only devices */
117 unsigned old_atari_disk : 1; /* Is an old Atari disk */
118 unsigned changed : 1; /* Medium has been changed */
119 unsigned long size; /* #blocks */
120 int access_count;
121} acsi_info[MAX_DEV];
122
123/*
124 * SENSE KEYS
125 */
126
127#define NO_SENSE 0x00
128#define RECOVERED_ERROR 0x01
129#define NOT_READY 0x02
130#define MEDIUM_ERROR 0x03
131#define HARDWARE_ERROR 0x04
132#define ILLEGAL_REQUEST 0x05
133#define UNIT_ATTENTION 0x06
134#define DATA_PROTECT 0x07
135#define BLANK_CHECK 0x08
136#define COPY_ABORTED 0x0a
137#define ABORTED_COMMAND 0x0b
138#define VOLUME_OVERFLOW 0x0d
139#define MISCOMPARE 0x0e
140
141
142/*
143 * DEVICE TYPES
144 */
145
146#define TYPE_DISK 0x00
147#define TYPE_TAPE 0x01
148#define TYPE_WORM 0x04
149#define TYPE_ROM 0x05
150#define TYPE_MOD 0x07
151#define TYPE_NO_LUN 0x7f
152
153/* The data returned by MODE SENSE differ between the old Atari
154 * hard disks and SCSI disks connected to ACSI. In the following, both
155 * formats are defined and some macros to operate on them potably.
156 */
157
158typedef struct {
159 unsigned long dummy[2];
160 unsigned long sector_size;
161 unsigned char format_code;
162#define ATARI_SENSE_FORMAT_FIX 1
163#define ATARI_SENSE_FORMAT_CHNG 2
164 unsigned char cylinders_h;
165 unsigned char cylinders_l;
166 unsigned char heads;
167 unsigned char reduced_h;
168 unsigned char reduced_l;
169 unsigned char precomp_h;
170 unsigned char precomp_l;
171 unsigned char landing_zone;
172 unsigned char steprate;
173 unsigned char type;
174#define ATARI_SENSE_TYPE_FIXCHNG_MASK 4
175#define ATARI_SENSE_TYPE_SOFTHARD_MASK 8
176#define ATARI_SENSE_TYPE_FIX 4
177#define ATARI_SENSE_TYPE_CHNG 0
178#define ATARI_SENSE_TYPE_SOFT 0
179#define ATARI_SENSE_TYPE_HARD 8
180 unsigned char sectors;
181} ATARI_SENSE_DATA;
182
183#define ATARI_CAPACITY(sd) \
184 (((int)((sd).cylinders_h<<8)|(sd).cylinders_l) * \
185 (sd).heads * (sd).sectors)
186
187
188typedef struct {
189 unsigned char dummy1;
190 unsigned char medium_type;
191 unsigned char dummy2;
192 unsigned char descriptor_size;
193 unsigned long block_count;
194 unsigned long sector_size;
195 /* Page 0 data */
196 unsigned char page_code;
197 unsigned char page_size;
198 unsigned char page_flags;
199 unsigned char qualifier;
200} SCSI_SENSE_DATA;
201
202#define SCSI_CAPACITY(sd) ((sd).block_count & 0xffffff)
203
204
205typedef union {
206 ATARI_SENSE_DATA atari;
207 SCSI_SENSE_DATA scsi;
208} SENSE_DATA;
209
210#define SENSE_TYPE_UNKNOWN 0
211#define SENSE_TYPE_ATARI 1
212#define SENSE_TYPE_SCSI 2
213
214#define SENSE_TYPE(sd) \
215 (((sd).atari.dummy[0] == 8 && \
216 ((sd).atari.format_code == 1 || \
217 (sd).atari.format_code == 2)) ? SENSE_TYPE_ATARI : \
218 ((sd).scsi.dummy1 >= 11) ? SENSE_TYPE_SCSI : \
219 SENSE_TYPE_UNKNOWN)
220
221#define CAPACITY(sd) \
222 (SENSE_TYPE(sd) == SENSE_TYPE_ATARI ? \
223 ATARI_CAPACITY((sd).atari) : \
224 SCSI_CAPACITY((sd).scsi))
225
226#define SECTOR_SIZE(sd) \
227 (SENSE_TYPE(sd) == SENSE_TYPE_ATARI ? \
228 (sd).atari.sector_size : \
229 (sd).scsi.sector_size & 0xffffff)
230
231/* Default size if capacity cannot be determined (1 GByte) */
232#define DEFAULT_SIZE 0x1fffff
233
234#define CARTRCH_STAT(aip,buf) \
235 (aip->old_atari_disk ? \
236 (((buf)[0] & 0x7f) == 0x28) : \
237 ((((buf)[0] & 0x70) == 0x70) ? \
238 (((buf)[2] & 0x0f) == 0x06) : \
239 (((buf)[0] & 0x0f) == 0x06))) \
240
241/* These two are also exported to other drivers that work on the ACSI bus and
242 * need an ST-RAM buffer. */
243char *acsi_buffer;
244unsigned long phys_acsi_buffer;
245
246static int NDevices;
247
248static int CurrentNReq;
249static int CurrentNSect;
250static char *CurrentBuffer;
251
252static DEFINE_SPINLOCK(acsi_lock);
253
254
255#define SET_TIMER() mod_timer(&acsi_timer, jiffies + ACSI_TIMEOUT)
256#define CLEAR_TIMER() del_timer(&acsi_timer)
257
258static unsigned long STramMask;
259#define STRAM_ADDR(a) (((a) & STramMask) == 0)
260
261
262
263/* ACSI commands */
264
265static char tur_cmd[6] = { 0x00, 0, 0, 0, 0, 0 };
266static char modesense_cmd[6] = { 0x1a, 0, 0, 0, 24, 0 };
267static char modeselect_cmd[6] = { 0x15, 0, 0, 0, 12, 0 };
268static char inquiry_cmd[6] = { 0x12, 0, 0, 0,255, 0 };
269static char reqsense_cmd[6] = { 0x03, 0, 0, 0, 4, 0 };
270static char read_cmd[6] = { 0x08, 0, 0, 0, 0, 0 };
271static char write_cmd[6] = { 0x0a, 0, 0, 0, 0, 0 };
272static char pa_med_rem_cmd[6] = { 0x1e, 0, 0, 0, 0, 0 };
273
274#define CMDSET_TARG_LUN(cmd,targ,lun) \
275 do { \
276 cmd[0] = (cmd[0] & ~0xe0) | (targ)<<5; \
277 cmd[1] = (cmd[1] & ~0xe0) | (lun)<<5; \
278 } while(0)
279
280#define CMDSET_BLOCK(cmd,blk) \
281 do { \
282 unsigned long __blk = (blk); \
283 cmd[3] = __blk; __blk >>= 8; \
284 cmd[2] = __blk; __blk >>= 8; \
285 cmd[1] = (cmd[1] & 0xe0) | (__blk & 0x1f); \
286 } while(0)
287
288#define CMDSET_LEN(cmd,len) \
289 do { \
290 cmd[4] = (len); \
291 } while(0)
292
293/* ACSI errors (from REQUEST SENSE); There are two tables, one for the
294 * old Atari disks and one for SCSI on ACSI disks.
295 */
296
297struct acsi_error {
298 unsigned char code;
299 const char *text;
300} atari_acsi_errors[] = {
301 { 0x00, "No error (??)" },
302 { 0x01, "No index pulses" },
303 { 0x02, "Seek not complete" },
304 { 0x03, "Write fault" },
305 { 0x04, "Drive not ready" },
306 { 0x06, "No Track 00 signal" },
307 { 0x10, "ECC error in ID field" },
308 { 0x11, "Uncorrectable data error" },
309 { 0x12, "ID field address mark not found" },
310 { 0x13, "Data field address mark not found" },
311 { 0x14, "Record not found" },
312 { 0x15, "Seek error" },
313 { 0x18, "Data check in no retry mode" },
314 { 0x19, "ECC error during verify" },
315 { 0x1a, "Access to bad block" },
316 { 0x1c, "Unformatted or bad format" },
317 { 0x20, "Invalid command" },
318 { 0x21, "Invalid block address" },
319 { 0x23, "Volume overflow" },
320 { 0x24, "Invalid argument" },
321 { 0x25, "Invalid drive number" },
322 { 0x26, "Byte zero parity check" },
323 { 0x28, "Cartride changed" },
324 { 0x2c, "Error count overflow" },
325 { 0x30, "Controller selftest failed" }
326},
327
328 scsi_acsi_errors[] = {
329 { 0x00, "No error (??)" },
330 { 0x01, "Recovered error" },
331 { 0x02, "Drive not ready" },
332 { 0x03, "Uncorrectable medium error" },
333 { 0x04, "Hardware error" },
334 { 0x05, "Illegal request" },
335 { 0x06, "Unit attention (Reset or cartridge changed)" },
336 { 0x07, "Data protection" },
337 { 0x08, "Blank check" },
338 { 0x0b, "Aborted Command" },
339 { 0x0d, "Volume overflow" }
340};
341
342
343
344/***************************** Prototypes *****************************/
345
346static int acsicmd_dma( const char *cmd, char *buffer, int blocks, int
347 rwflag, int enable);
348static int acsi_reqsense( char *buffer, int targ, int lun);
349static void acsi_print_error(const unsigned char *errblk, struct acsi_info_struct *aip);
350static irqreturn_t acsi_interrupt (int irq, void *data, struct pt_regs *fp);
351static void unexpected_acsi_interrupt( void );
352static void bad_rw_intr( void );
353static void read_intr( void );
354static void write_intr( void);
355static void acsi_times_out( unsigned long dummy );
356static void copy_to_acsibuffer( void );
357static void copy_from_acsibuffer( void );
358static void do_end_requests( void );
359static void do_acsi_request( request_queue_t * );
360static void redo_acsi_request( void );
361static int acsi_ioctl( struct inode *inode, struct file *file, unsigned int
362 cmd, unsigned long arg );
363static int acsi_open( struct inode * inode, struct file * filp );
364static int acsi_release( struct inode * inode, struct file * file );
365static void acsi_prevent_removal(struct acsi_info_struct *aip, int flag );
366static int acsi_change_blk_size( int target, int lun);
367static int acsi_mode_sense( int target, int lun, SENSE_DATA *sd );
368static int acsi_revalidate (struct gendisk *disk);
369
370/************************* End of Prototypes **************************/
371
372
8d06afab 373DEFINE_TIMER(acsi_timer, acsi_times_out, 0, 0);
1da177e4
LT
374
375
376#ifdef CONFIG_ATARI_SLM
377
378extern int attach_slm( int target, int lun );
379extern int slm_init( void );
380
381#endif
382
383
384\f
385/***********************************************************************
386 *
387 * ACSI primitives
388 *
389 **********************************************************************/
390
391
392/*
393 * The following two functions wait for _IRQ to become Low or High,
394 * resp., with a timeout. The 'timeout' parameter is in jiffies
395 * (10ms).
396 * If the functions are called with timer interrupts on (int level <
397 * 6), the timeout is based on the 'jiffies' variable to provide exact
398 * timeouts for device probing etc.
399 * If interrupts are disabled, the number of tries is based on the
400 * 'loops_per_jiffy' variable. A rough estimation is sufficient here...
401 */
402
403#define INT_LEVEL \
404 ({ unsigned __sr; \
405 __asm__ __volatile__ ( "movew %/sr,%0" : "=dm" (__sr) ); \
406 (__sr >> 8) & 7; \
407 })
408
409int acsi_wait_for_IRQ( unsigned timeout )
410
411{
412 if (INT_LEVEL < 6) {
413 unsigned long maxjif = jiffies + timeout;
414 while (time_before(jiffies, maxjif))
415 if (!(mfp.par_dt_reg & 0x20)) return( 1 );
416 }
417 else {
418 long tries = loops_per_jiffy / 8 * timeout;
419 while( --tries >= 0 )
420 if (!(mfp.par_dt_reg & 0x20)) return( 1 );
421 }
422 return( 0 ); /* timeout! */
423}
424
425
426int acsi_wait_for_noIRQ( unsigned timeout )
427
428{
429 if (INT_LEVEL < 6) {
430 unsigned long maxjif = jiffies + timeout;
431 while (time_before(jiffies, maxjif))
432 if (mfp.par_dt_reg & 0x20) return( 1 );
433 }
434 else {
435 long tries = loops_per_jiffy * timeout / 8;
436 while( tries-- >= 0 )
437 if (mfp.par_dt_reg & 0x20) return( 1 );
438 }
439 return( 0 ); /* timeout! */
440}
441
442static struct timeval start_time;
443
444void
445acsi_delay_start(void)
446{
447 do_gettimeofday(&start_time);
448}
449
450/* wait from acsi_delay_start to now usec (<1E6) usec */
451
452void
453acsi_delay_end(long usec)
454{
455 struct timeval end_time;
456 long deltau,deltas;
457 do_gettimeofday(&end_time);
458 deltau=end_time.tv_usec - start_time.tv_usec;
459 deltas=end_time.tv_sec - start_time.tv_sec;
460 if (deltas > 1 || deltas < 0)
461 return;
462 if (deltas > 0)
463 deltau += 1000*1000;
464 if (deltau >= usec)
465 return;
466 udelay(usec-deltau);
467}
468
469/* acsicmd_dma() sends an ACSI command and sets up the DMA to transfer
470 * 'blocks' blocks of 512 bytes from/to 'buffer'.
471 * Because the _IRQ signal is used for handshaking the command bytes,
472 * the ACSI interrupt has to be disabled in this function. If the end
473 * of the operation should be signalled by a real interrupt, it has to be
474 * reenabled afterwards.
475 */
476
477static int acsicmd_dma( const char *cmd, char *buffer, int blocks, int rwflag, int enable)
478
479{ unsigned long flags, paddr;
480 int i;
481
482#ifdef NO_WRITE
483 if (rwflag || *cmd == 0x0a) {
484 printk( "ACSI: Write commands disabled!\n" );
485 return( 0 );
486 }
487#endif
488
489 rwflag = rwflag ? 0x100 : 0;
490 paddr = virt_to_phys( buffer );
491
492 acsi_delay_end(COMMAND_DELAY);
493 DISABLE_IRQ();
494
495 local_irq_save(flags);
496 /* Low on A1 */
497 dma_wd.dma_mode_status = 0x88 | rwflag;
498 MFPDELAY();
499
500 /* set DMA address */
501 dma_wd.dma_lo = (unsigned char)paddr;
502 paddr >>= 8;
503 MFPDELAY();
504 dma_wd.dma_md = (unsigned char)paddr;
505 paddr >>= 8;
506 MFPDELAY();
507 if (ATARIHW_PRESENT(EXTD_DMA))
508 st_dma_ext_dmahi = (unsigned short)paddr;
509 else
510 dma_wd.dma_hi = (unsigned char)paddr;
511 MFPDELAY();
512 local_irq_restore(flags);
513
514 /* send the command bytes except the last */
515 for( i = 0; i < 5; ++i ) {
516 DMA_LONG_WRITE( *cmd++, 0x8a | rwflag );
517 udelay(20);
518 if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
519 }
520
521 /* Clear FIFO and switch DMA to correct direction */
522 dma_wd.dma_mode_status = 0x92 | (rwflag ^ 0x100);
523 MFPDELAY();
524 dma_wd.dma_mode_status = 0x92 | rwflag;
525 MFPDELAY();
526
527 /* How many sectors for DMA */
528 dma_wd.fdc_acces_seccount = blocks;
529 MFPDELAY();
530
531 /* send last command byte */
532 dma_wd.dma_mode_status = 0x8a | rwflag;
533 MFPDELAY();
534 DMA_LONG_WRITE( *cmd++, 0x0a | rwflag );
535 if (enable)
536 ENABLE_IRQ();
537 udelay(80);
538
539 return( 1 );
540}
541
542
543/*
544 * acsicmd_nodma() sends an ACSI command that requires no DMA.
545 */
546
547int acsicmd_nodma( const char *cmd, int enable)
548
549{ int i;
550
551 acsi_delay_end(COMMAND_DELAY);
552 DISABLE_IRQ();
553
554 /* send first command byte */
555 dma_wd.dma_mode_status = 0x88;
556 MFPDELAY();
557 DMA_LONG_WRITE( *cmd++, 0x8a );
558 udelay(20);
559 if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
560
561 /* send the intermediate command bytes */
562 for( i = 0; i < 4; ++i ) {
563 DMA_LONG_WRITE( *cmd++, 0x8a );
564 udelay(20);
565 if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
566 }
567
568 /* send last command byte */
569 DMA_LONG_WRITE( *cmd++, 0x0a );
570 if (enable)
571 ENABLE_IRQ();
572 udelay(80);
573
574 return( 1 );
575 /* Note that the ACSI interrupt is still disabled after this
576 * function. If you want to get the IRQ delivered, enable it manually!
577 */
578}
579
580
581static int acsi_reqsense( char *buffer, int targ, int lun)
582
583{
584 CMDSET_TARG_LUN( reqsense_cmd, targ, lun);
585 if (!acsicmd_dma( reqsense_cmd, buffer, 1, 0, 0 )) return( 0 );
586 if (!acsi_wait_for_IRQ( 10 )) return( 0 );
587 acsi_getstatus();
588 if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
589 if (!acsi_wait_for_IRQ( 10 )) return( 0 );
590 acsi_getstatus();
591 if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
592 if (!acsi_wait_for_IRQ( 10 )) return( 0 );
593 acsi_getstatus();
594 if (!acsicmd_nodma( reqsense_cmd, 0 )) return( 0 );
595 if (!acsi_wait_for_IRQ( 10 )) return( 0 );
596 acsi_getstatus();
597 dma_cache_maintenance( virt_to_phys(buffer), 16, 0 );
598
599 return( 1 );
600}
601
602
603/*
604 * ACSI status phase: get the status byte from the bus
605 *
606 * I've seen several times that a 0xff status is read, propably due to
607 * a timing error. In this case, the procedure is repeated after the
608 * next _IRQ edge.
609 */
610
611int acsi_getstatus( void )
612
613{ int status;
614
615 DISABLE_IRQ();
616 for(;;) {
617 if (!acsi_wait_for_IRQ( 100 )) {
618 acsi_delay_start();
619 return( -1 );
620 }
621 dma_wd.dma_mode_status = 0x8a;
622 MFPDELAY();
623 status = dma_wd.fdc_acces_seccount;
624 if (status != 0xff) break;
625#ifdef DEBUG
626 printk("ACSI: skipping 0xff status byte\n" );
627#endif
628 udelay(40);
629 acsi_wait_for_noIRQ( 20 );
630 }
631 dma_wd.dma_mode_status = 0x80;
632 udelay(40);
633 acsi_wait_for_noIRQ( 20 );
634
635 acsi_delay_start();
636 return( status & 0x1f ); /* mask of the device# */
637}
638
639
640#if (defined(CONFIG_ATARI_SLM) || defined(CONFIG_ATARI_SLM_MODULE))
641
642/* Receive data in an extended status phase. Needed by SLM printer. */
643
644int acsi_extstatus( char *buffer, int cnt )
645
646{ int status;
647
648 DISABLE_IRQ();
649 udelay(80);
650 while( cnt-- > 0 ) {
651 if (!acsi_wait_for_IRQ( 40 )) return( 0 );
652 dma_wd.dma_mode_status = 0x8a;
653 MFPDELAY();
654 status = dma_wd.fdc_acces_seccount;
655 MFPDELAY();
656 *buffer++ = status & 0xff;
657 udelay(40);
658 }
659 return( 1 );
660}
661
662
663/* Finish an extended status phase */
664
665void acsi_end_extstatus( void )
666
667{
668 dma_wd.dma_mode_status = 0x80;
669 udelay(40);
670 acsi_wait_for_noIRQ( 20 );
671 acsi_delay_start();
672}
673
674
675/* Send data in an extended command phase */
676
677int acsi_extcmd( unsigned char *buffer, int cnt )
678
679{
680 while( cnt-- > 0 ) {
681 DMA_LONG_WRITE( *buffer++, 0x8a );
682 udelay(20);
683 if (!acsi_wait_for_IRQ( HZ/2 )) return( 0 ); /* timeout */
684 }
685 return( 1 );
686}
687
688#endif
689
690
691static void acsi_print_error(const unsigned char *errblk, struct acsi_info_struct *aip)
692
693{ int atari_err, i, errcode;
694 struct acsi_error *arr;
695
696 atari_err = aip->old_atari_disk;
697 if (atari_err)
698 errcode = errblk[0] & 0x7f;
699 else
700 if ((errblk[0] & 0x70) == 0x70)
701 errcode = errblk[2] & 0x0f;
702 else
703 errcode = errblk[0] & 0x0f;
704
705 printk( KERN_ERR "ACSI error 0x%02x", errcode );
706
707 if (errblk[0] & 0x80)
708 printk( " for sector %d",
709 ((errblk[1] & 0x1f) << 16) |
710 (errblk[2] << 8) | errblk[0] );
711
712 arr = atari_err ? atari_acsi_errors : scsi_acsi_errors;
713 i = atari_err ? sizeof(atari_acsi_errors)/sizeof(*atari_acsi_errors) :
714 sizeof(scsi_acsi_errors)/sizeof(*scsi_acsi_errors);
715
716 for( --i; i >= 0; --i )
717 if (arr[i].code == errcode) break;
718 if (i >= 0)
719 printk( ": %s\n", arr[i].text );
720}
721
722/*******************************************************************
723 *
724 * ACSI interrupt routine
725 * Test, if this is a ACSI interrupt and call the irq handler
726 * Otherwise ignore this interrupt.
727 *
728 *******************************************************************/
729
730static irqreturn_t acsi_interrupt(int irq, void *data, struct pt_regs *fp )
731
732{ void (*acsi_irq_handler)(void) = do_acsi;
733
734 do_acsi = NULL;
735 CLEAR_TIMER();
736
737 if (!acsi_irq_handler)
738 acsi_irq_handler = unexpected_acsi_interrupt;
739 acsi_irq_handler();
740 return IRQ_HANDLED;
741}
742
743
744/******************************************************************
745 *
746 * The Interrupt handlers
747 *
748 *******************************************************************/
749
750
751static void unexpected_acsi_interrupt( void )
752
753{
754 printk( KERN_WARNING "Unexpected ACSI interrupt\n" );
755}
756
757
758/* This function is called in case of errors. Because we cannot reset
759 * the ACSI bus or a single device, there is no other choice than
760 * retrying several times :-(
761 */
762
763static void bad_rw_intr( void )
764
765{
766 if (!CURRENT)
767 return;
768
769 if (++CURRENT->errors >= MAX_ERRORS)
770 end_request(CURRENT, 0);
771 /* Otherwise just retry */
772}
773
774
775static void read_intr( void )
776
777{ int status;
778
779 status = acsi_getstatus();
780 if (status != 0) {
781 struct gendisk *disk = CURRENT->rq_disk;
782 struct acsi_info_struct *aip = disk->private_data;
783 printk(KERN_ERR "%s: ", disk->disk_name);
784 if (!acsi_reqsense(acsi_buffer, aip->target, aip->lun))
785 printk( "ACSI error and REQUEST SENSE failed (status=0x%02x)\n", status );
786 else {
787 acsi_print_error(acsi_buffer, aip);
788 if (CARTRCH_STAT(aip, acsi_buffer))
789 aip->changed = 1;
790 }
791 ENABLE_IRQ();
792 bad_rw_intr();
793 redo_acsi_request();
794 return;
795 }
796
797 dma_cache_maintenance( virt_to_phys(CurrentBuffer), CurrentNSect*512, 0 );
798 if (CurrentBuffer == acsi_buffer)
799 copy_from_acsibuffer();
800
801 do_end_requests();
802 redo_acsi_request();
803}
804
805
806static void write_intr(void)
807
808{ int status;
809
810 status = acsi_getstatus();
811 if (status != 0) {
812 struct gendisk *disk = CURRENT->rq_disk;
813 struct acsi_info_struct *aip = disk->private_data;
814 printk( KERN_ERR "%s: ", disk->disk_name);
815 if (!acsi_reqsense( acsi_buffer, aip->target, aip->lun))
816 printk( "ACSI error and REQUEST SENSE failed (status=0x%02x)\n", status );
817 else {
818 acsi_print_error(acsi_buffer, aip);
819 if (CARTRCH_STAT(aip, acsi_buffer))
820 aip->changed = 1;
821 }
822 bad_rw_intr();
823 redo_acsi_request();
824 return;
825 }
826
827 do_end_requests();
828 redo_acsi_request();
829}
830
831
832static void acsi_times_out( unsigned long dummy )
833
834{
835 DISABLE_IRQ();
836 if (!do_acsi) return;
837
838 do_acsi = NULL;
839 printk( KERN_ERR "ACSI timeout\n" );
840 if (!CURRENT)
841 return;
842 if (++CURRENT->errors >= MAX_ERRORS) {
843#ifdef DEBUG
844 printk( KERN_ERR "ACSI: too many errors.\n" );
845#endif
846 end_request(CURRENT, 0);
847 }
848
849 redo_acsi_request();
850}
851
852
853\f
854/***********************************************************************
855 *
856 * Scatter-gather utility functions
857 *
858 ***********************************************************************/
859
860
861static void copy_to_acsibuffer( void )
862
863{ int i;
864 char *src, *dst;
865 struct buffer_head *bh;
866
867 src = CURRENT->buffer;
868 dst = acsi_buffer;
869 bh = CURRENT->bh;
870
871 if (!bh)
872 memcpy( dst, src, CurrentNSect*512 );
873 else
874 for( i = 0; i < CurrentNReq; ++i ) {
875 memcpy( dst, src, bh->b_size );
876 dst += bh->b_size;
877 if ((bh = bh->b_reqnext))
878 src = bh->b_data;
879 }
880}
881
882
883static void copy_from_acsibuffer( void )
884
885{ int i;
886 char *src, *dst;
887 struct buffer_head *bh;
888
889 dst = CURRENT->buffer;
890 src = acsi_buffer;
891 bh = CURRENT->bh;
892
893 if (!bh)
894 memcpy( dst, src, CurrentNSect*512 );
895 else
896 for( i = 0; i < CurrentNReq; ++i ) {
897 memcpy( dst, src, bh->b_size );
898 src += bh->b_size;
899 if ((bh = bh->b_reqnext))
900 dst = bh->b_data;
901 }
902}
903
904
905static void do_end_requests( void )
906
907{ int i, n;
908
909 if (!CURRENT->bh) {
910 CURRENT->nr_sectors -= CurrentNSect;
911 CURRENT->current_nr_sectors -= CurrentNSect;
912 CURRENT->sector += CurrentNSect;
913 if (CURRENT->nr_sectors == 0)
914 end_request(CURRENT, 1);
915 }
916 else {
917 for( i = 0; i < CurrentNReq; ++i ) {
918 n = CURRENT->bh->b_size >> 9;
919 CURRENT->nr_sectors -= n;
920 CURRENT->current_nr_sectors -= n;
921 CURRENT->sector += n;
922 end_request(CURRENT, 1);
923 }
924 }
925}
926
927
928
929\f
930/***********************************************************************
931 *
932 * do_acsi_request and friends
933 *
934 ***********************************************************************/
935
936static void do_acsi_request( request_queue_t * q )
937
938{
939 stdma_lock( acsi_interrupt, NULL );
940 redo_acsi_request();
941}
942
943
944static void redo_acsi_request( void )
945{
946 unsigned block, target, lun, nsect;
947 char *buffer;
948 unsigned long pbuffer;
949 struct buffer_head *bh;
950 struct gendisk *disk;
951 struct acsi_info_struct *aip;
952
953 repeat:
954 CLEAR_TIMER();
955
956 if (do_acsi)
957 return;
958
959 if (!CURRENT) {
960 do_acsi = NULL;
961 ENABLE_IRQ();
962 stdma_release();
963 return;
964 }
965
966 disk = CURRENT->rq_disk;
967 aip = disk->private_data;
968 if (CURRENT->bh) {
969 if (!CURRENT->bh && !buffer_locked(CURRENT->bh))
970 panic("ACSI: block not locked");
971 }
972
973 block = CURRENT->sector;
974 if (block+CURRENT->nr_sectors >= get_capacity(disk)) {
975#ifdef DEBUG
976 printk( "%s: attempted access for blocks %d...%ld past end of device at block %ld.\n",
977 disk->disk_name,
978 block, block + CURRENT->nr_sectors - 1,
979 get_capacity(disk));
980#endif
981 end_request(CURRENT, 0);
982 goto repeat;
983 }
984 if (aip->changed) {
985 printk( KERN_NOTICE "%s: request denied because cartridge has "
986 "been changed.\n", disk->disk_name);
987 end_request(CURRENT, 0);
988 goto repeat;
989 }
990
991 target = aip->target;
992 lun = aip->lun;
993
994 /* Find out how many sectors should be transferred from/to
995 * consecutive buffers and thus can be done with a single command.
996 */
997 buffer = CURRENT->buffer;
998 pbuffer = virt_to_phys(buffer);
999 nsect = CURRENT->current_nr_sectors;
1000 CurrentNReq = 1;
1001
1002 if ((bh = CURRENT->bh) && bh != CURRENT->bhtail) {
1003 if (!STRAM_ADDR(pbuffer)) {
1004 /* If transfer is done via the ACSI buffer anyway, we can
1005 * assemble as much bh's as fit in the buffer.
1006 */
1007 while( (bh = bh->b_reqnext) ) {
1008 if (nsect + (bh->b_size>>9) > ACSI_BUFFER_SECTORS) break;
1009 nsect += bh->b_size >> 9;
1010 ++CurrentNReq;
1011 if (bh == CURRENT->bhtail) break;
1012 }
1013 buffer = acsi_buffer;
1014 pbuffer = phys_acsi_buffer;
1015 }
1016 else {
1017 unsigned long pendadr, pnewadr;
1018 pendadr = pbuffer + nsect*512;
1019 while( (bh = bh->b_reqnext) ) {
1020 pnewadr = virt_to_phys(bh->b_data);
1021 if (!STRAM_ADDR(pnewadr) || pendadr != pnewadr) break;
1022 nsect += bh->b_size >> 9;
1023 pendadr = pnewadr + bh->b_size;
1024 ++CurrentNReq;
1025 if (bh == CURRENT->bhtail) break;
1026 }
1027 }
1028 }
1029 else {
1030 if (!STRAM_ADDR(pbuffer)) {
1031 buffer = acsi_buffer;
1032 pbuffer = phys_acsi_buffer;
1033 if (nsect > ACSI_BUFFER_SECTORS)
1034 nsect = ACSI_BUFFER_SECTORS;
1035 }
1036 }
1037 CurrentBuffer = buffer;
1038 CurrentNSect = nsect;
1039
1040 if (rq_data_dir(CURRENT) == WRITE) {
1041 CMDSET_TARG_LUN( write_cmd, target, lun );
1042 CMDSET_BLOCK( write_cmd, block );
1043 CMDSET_LEN( write_cmd, nsect );
1044 if (buffer == acsi_buffer)
1045 copy_to_acsibuffer();
1046 dma_cache_maintenance( pbuffer, nsect*512, 1 );
1047 do_acsi = write_intr;
1048 if (!acsicmd_dma( write_cmd, buffer, nsect, 1, 1)) {
1049 do_acsi = NULL;
1050 printk( KERN_ERR "ACSI (write): Timeout in command block\n" );
1051 bad_rw_intr();
1052 goto repeat;
1053 }
1054 SET_TIMER();
1055 return;
1056 }
1057 if (rq_data_dir(CURRENT) == READ) {
1058 CMDSET_TARG_LUN( read_cmd, target, lun );
1059 CMDSET_BLOCK( read_cmd, block );
1060 CMDSET_LEN( read_cmd, nsect );
1061 do_acsi = read_intr;
1062 if (!acsicmd_dma( read_cmd, buffer, nsect, 0, 1)) {
1063 do_acsi = NULL;
1064 printk( KERN_ERR "ACSI (read): Timeout in command block\n" );
1065 bad_rw_intr();
1066 goto repeat;
1067 }
1068 SET_TIMER();
1069 return;
1070 }
1071 panic("unknown ACSI command");
1072}
1073
1074
1075\f
1076/***********************************************************************
1077 *
1078 * Misc functions: ioctl, open, release, check_change, ...
1079 *
1080 ***********************************************************************/
1081
1082
1083static int acsi_ioctl( struct inode *inode, struct file *file,
1084 unsigned int cmd, unsigned long arg )
1085{
1086 struct gendisk *disk = inode->i_bdev->bd_disk;
1087 struct acsi_info_struct *aip = disk->private_data;
1088 switch (cmd) {
1089 case HDIO_GETGEO:
1090 /* HDIO_GETGEO is supported more for getting the partition's
1091 * start sector... */
1092 { struct hd_geometry *geo = (struct hd_geometry *)arg;
1093 /* just fake some geometry here, it's nonsense anyway; to make it
1094 * easy, use Adaptec's usual 64/32 mapping */
1095 put_user( 64, &geo->heads );
1096 put_user( 32, &geo->sectors );
1097 put_user( aip->size >> 11, &geo->cylinders );
1098 put_user(get_start_sect(inode->i_bdev), &geo->start);
1099 return 0;
1100 }
1101 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,
1595 .media_changed = acsi_media_change,
1596 .revalidate_disk= acsi_revalidate,
1597};
1598
1599#ifdef CONFIG_ATARI_SLM_MODULE
1600/* call attach_slm() for each device that is a printer; needed for init of SLM
1601 * driver as a module, since it's not yet present if acsi.c is inited and thus
1602 * the bus gets scanned. */
1603void acsi_attach_SLMs( int (*attach_func)( int, int ) )
1604{
1605 int i, n = 0;
1606
1607 for( i = 0; i < 8; ++i )
1608 if (SLM_devices[i] >= 0)
1609 n += (*attach_func)( i, SLM_devices[i] );
1610 printk( KERN_INFO "Found %d SLM printer(s) total.\n", n );
1611}
1612#endif /* CONFIG_ATARI_SLM_MODULE */
1613
1614
1615int acsi_init( void )
1616{
1617 int err = 0;
1618 int i, target, lun;
1619 struct acsi_info_struct *aip;
1620#ifdef CONFIG_ATARI_SLM
1621 int n_slm = 0;
1622#endif
1623 if (!MACH_IS_ATARI || !ATARIHW_PRESENT(ACSI))
1624 return 0;
1625 if (register_blkdev(ACSI_MAJOR, "ad")) {
1626 err = -EBUSY;
1627 goto out1;
1628 }
1629 if (!(acsi_buffer =
1630 (char *)atari_stram_alloc(ACSI_BUFFER_SIZE, "acsi"))) {
1631 err = -ENOMEM;
1632 printk( KERN_ERR "Unable to get ACSI ST-Ram buffer.\n" );
1633 goto out2;
1634 }
1635 phys_acsi_buffer = virt_to_phys( acsi_buffer );
1636 STramMask = ATARIHW_PRESENT(EXTD_DMA) ? 0x00000000 : 0xff000000;
1637
1638 acsi_queue = blk_init_queue(do_acsi_request, &acsi_lock);
1639 if (!acsi_queue) {
1640 err = -ENOMEM;
1641 goto out2a;
1642 }
1643#ifdef CONFIG_ATARI_SLM
1644 err = slm_init();
1645#endif
1646 if (err)
1647 goto out3;
1648
1649 printk( KERN_INFO "Probing ACSI devices:\n" );
1650 NDevices = 0;
1651#ifdef CONFIG_ATARI_SLM_MODULE
1652 for( i = 0; i < 8; ++i )
1653 SLM_devices[i] = -1;
1654#endif
1655 stdma_lock(NULL, NULL);
1656
1657 for (target = 0; target < 8 && NDevices < MAX_DEV; ++target) {
1658 lun = 0;
1659 do {
1660 aip = &acsi_info[NDevices];
1661 aip->type = NONE;
1662 aip->target = target;
1663 aip->lun = lun;
1664 i = acsi_devinit(aip);
1665 switch (i) {
1666 case DEV_SUPPORTED:
1667 printk( KERN_INFO "Detected ");
1668 switch (aip->type) {
1669 case HARDDISK:
1670 printk("disk");
1671 break;
1672 case CDROM:
1673 printk("cdrom");
1674 break;
1675 default:
1676 }
1677 printk(" ad%c at id %d lun %d ",
1678 'a' + NDevices, target, lun);
1679 if (aip->removable)
1680 printk("(removable) ");
1681 if (aip->read_only)
1682 printk("(read-only) ");
1683 if (aip->size == DEFAULT_SIZE)
1684 printk(" unkown size, using default ");
1685 printk("%ld MByte\n",
1686 (aip->size*512+1024*1024/2)/(1024*1024));
1687 NDevices++;
1688 break;
1689 case DEV_SLM:
1690#ifdef CONFIG_ATARI_SLM
1691 n_slm += attach_slm( target, lun );
1692 break;
1693#endif
1694#ifdef CONFIG_ATARI_SLM_MODULE
1695 SLM_devices[target] = lun;
1696 break;
1697#endif
1698 /* neither of the above: fall through to unknown device */
1699 case DEV_UNKNOWN:
1700 printk( KERN_INFO "Detected unsupported device at "
1701 "id %d lun %d\n", target, lun);
1702 break;
1703 }
1704 }
1705#ifdef CONFIG_ACSI_MULTI_LUN
1706 while (i != DEV_NONE && ++lun < MAX_LUN);
1707#else
1708 while (0);
1709#endif
1710 }
1711
1712 /* reenable interrupt */
1713 ENABLE_IRQ();
1714 stdma_release();
1715
1716#ifndef CONFIG_ATARI_SLM
1717 printk( KERN_INFO "Found %d ACSI device(s) total.\n", NDevices );
1718#else
1719 printk( KERN_INFO "Found %d ACSI device(s) and %d SLM printer(s) total.\n",
1720 NDevices, n_slm );
1721#endif
1722 err = -ENOMEM;
1723 for( i = 0; i < NDevices; ++i ) {
1724 acsi_gendisk[i] = alloc_disk(16);
1725 if (!acsi_gendisk[i])
1726 goto out4;
1727 }
1728
1729 for( i = 0; i < NDevices; ++i ) {
1730 struct gendisk *disk = acsi_gendisk[i];
1731 sprintf(disk->disk_name, "ad%c", 'a'+i);
1732 aip = &acsi_info[NDevices];
1733 sprintf(disk->devfs_name, "ad/target%d/lun%d", aip->target, aip->lun);
1734 disk->major = ACSI_MAJOR;
1735 disk->first_minor = i << 4;
1736 if (acsi_info[i].type != HARDDISK) {
1737 disk->minors = 1;
1738 strcat(disk->devfs_name, "/disc");
1739 }
1740 disk->fops = &acsi_fops;
1741 disk->private_data = &acsi_info[i];
1742 set_capacity(disk, acsi_info[i].size);
1743 disk->queue = acsi_queue;
1744 add_disk(disk);
1745 }
1746 return 0;
1747out4:
1748 while (i--)
1749 put_disk(acsi_gendisk[i]);
1750out3:
1751 blk_cleanup_queue(acsi_queue);
1752out2a:
1753 atari_stram_free( acsi_buffer );
1754out2:
1755 unregister_blkdev( ACSI_MAJOR, "ad" );
1756out1:
1757 return err;
1758}
1759
1760
1761#ifdef MODULE
1762
1763MODULE_LICENSE("GPL");
1764
1765int init_module(void)
1766{
1767 int err;
1768
1769 if ((err = acsi_init()))
1770 return( err );
1771 printk( KERN_INFO "ACSI driver loaded as module.\n");
1772 return( 0 );
1773}
1774
1775void cleanup_module(void)
1776{
1777 int i;
1778 del_timer( &acsi_timer );
1779 blk_cleanup_queue(acsi_queue);
1780 atari_stram_free( acsi_buffer );
1781
1782 if (unregister_blkdev( ACSI_MAJOR, "ad" ) != 0)
1783 printk( KERN_ERR "acsi: cleanup_module failed\n");
1784
1785 for (i = 0; i < NDevices; i++) {
1786 del_gendisk(acsi_gendisk[i]);
1787 put_disk(acsi_gendisk[i]);
1788 }
1789}
1790#endif
1791
1792/*
1793 * This routine is called to flush all partitions and partition tables
1794 * for a changed scsi disk, and then re-read the new partition table.
1795 * If we are revalidating a disk because of a media change, then we
1796 * enter with usage == 0. If we are using an ioctl, we automatically have
1797 * usage == 1 (we need an open channel to use an ioctl :-), so this
1798 * is our limit.
1799 *
1800 * Changes by Martin Rogge, 9th Aug 1995:
1801 * got cd-roms to work by calling acsi_devinit. There are only two problems:
1802 * First, if there is no medium inserted, the status will remain "changed".
1803 * That is no problem at all, but our design of three-valued logic (medium
1804 * changed, medium not changed, no medium inserted).
1805 * Secondly the check could fail completely and the drive could deliver
1806 * nonsensical data, which could mess up the acsi_info[] structure. In
1807 * that case we try to make the entry safe.
1808 *
1809 */
1810
1811static int acsi_revalidate(struct gendisk *disk)
1812{
1813 struct acsi_info_struct *aip = disk->private_data;
1814 stdma_lock( NULL, NULL );
1815 if (acsi_devinit(aip) != DEV_SUPPORTED) {
1816 printk( KERN_ERR "ACSI: revalidate failed for target %d lun %d\n",
1817 aip->target, aip->lun);
1818 aip->size = 0;
1819 aip->read_only = 1;
1820 aip->removable = 1;
1821 aip->changed = 1; /* next acsi_open will try again... */
1822 }
1823
1824 ENABLE_IRQ();
1825 stdma_release();
1826 set_capacity(disk, aip->size);
1827 return 0;
1828}