1d34d71c6b0485edf769e4684bfd1dc591727726
[linux-flexiantxendom0-3.2.10.git] / drivers / block / paride / pd.c
1 /* 
2         pd.c    (c) 1997-8  Grant R. Guenther <grant@torque.net>
3                             Under the terms of the GNU General Public License.
4
5         This is the high-level driver for parallel port IDE hard
6         drives based on chips supported by the paride module.
7
8         By default, the driver will autoprobe for a single parallel
9         port IDE drive, but if their individual parameters are
10         specified, the driver can handle up to 4 drives.
11
12         The behaviour of the pd driver can be altered by setting
13         some parameters from the insmod command line.  The following
14         parameters are adjustable:
15  
16             drive0      These four arguments can be arrays of       
17             drive1      1-8 integers as follows:
18             drive2
19             drive3      <prt>,<pro>,<uni>,<mod>,<geo>,<sby>,<dly>,<slv>
20
21                         Where,
22
23                 <prt>   is the base of the parallel port address for
24                         the corresponding drive.  (required)
25
26                 <pro>   is the protocol number for the adapter that
27                         supports this drive.  These numbers are
28                         logged by 'paride' when the protocol modules
29                         are initialised.  (0 if not given)
30
31                 <uni>   for those adapters that support chained
32                         devices, this is the unit selector for the
33                         chain of devices on the given port.  It should
34                         be zero for devices that don't support chaining.
35                         (0 if not given)
36
37                 <mod>   this can be -1 to choose the best mode, or one
38                         of the mode numbers supported by the adapter.
39                         (-1 if not given)
40
41                 <geo>   this defaults to 0 to indicate that the driver
42                         should use the CHS geometry provided by the drive
43                         itself.  If set to 1, the driver will provide
44                         a logical geometry with 64 heads and 32 sectors
45                         per track, to be consistent with most SCSI
46                         drivers.  (0 if not given)
47
48                 <sby>   set this to zero to disable the power saving
49                         standby mode, if needed.  (1 if not given)
50
51                 <dly>   some parallel ports require the driver to 
52                         go more slowly.  -1 sets a default value that
53                         should work with the chosen protocol.  Otherwise,
54                         set this to a small integer, the larger it is
55                         the slower the port i/o.  In some cases, setting
56                         this to zero will speed up the device. (default -1)
57
58                 <slv>   IDE disks can be jumpered to master or slave.
59                         Set this to 0 to choose the master drive, 1 to
60                         choose the slave, -1 (the default) to choose the
61                         first drive found.
62                         
63
64             major       You may use this parameter to overide the
65                         default major number (45) that this driver
66                         will use.  Be sure to change the device
67                         name as well.
68
69             name        This parameter is a character string that
70                         contains the name the kernel will use for this
71                         device (in /proc output, for instance).
72                         (default "pd")
73
74             cluster     The driver will attempt to aggregate requests
75                         for adjacent blocks into larger multi-block
76                         clusters.  The maximum cluster size (in 512
77                         byte sectors) is set with this parameter.
78                         (default 64)
79
80             verbose     This parameter controls the amount of logging
81                         that the driver will do.  Set it to 0 for 
82                         normal operation, 1 to see autoprobe progress
83                         messages, or 2 to see additional debugging
84                         output.  (default 0)
85
86             nice        This parameter controls the driver's use of
87                         idle CPU time, at the expense of some speed.
88
89         If this driver is built into the kernel, you can use kernel
90         the following command line parameters, with the same values
91         as the corresponding module parameters listed above:
92
93             pd.drive0
94             pd.drive1
95             pd.drive2
96             pd.drive3
97             pd.cluster
98             pd.nice
99
100         In addition, you can use the parameter pd.disable to disable
101         the driver entirely.
102  
103 */
104
105 /* Changes:
106
107         1.01    GRG 1997.01.24  Restored pd_reset()
108                                 Added eject ioctl
109         1.02    GRG 1998.05.06  SMP spinlock changes, 
110                                 Added slave support
111         1.03    GRG 1998.06.16  Eliminate an Ugh.
112         1.04    GRG 1998.08.15  Extra debugging, use HZ in loop timing
113         1.05    GRG 1998.09.24  Added jumbo support
114
115 */
116
117 #define PD_VERSION      "1.05"
118 #define PD_MAJOR        45
119 #define PD_NAME         "pd"
120 #define PD_UNITS        4
121
122 /* Here are things one can override from the insmod command.
123    Most are autoprobed by paride unless set here.  Verbose is off
124    by default.
125
126 */
127
128 static int      verbose = 0;
129 static int      major = PD_MAJOR;
130 static char     *name = PD_NAME;
131 static int      cluster = 64;   
132 static int      nice = 0;
133 static int      disable = 0;
134
135 static int drive0[8] = {0,0,0,-1,0,1,-1,-1};
136 static int drive1[8] = {0,0,0,-1,0,1,-1,-1};
137 static int drive2[8] = {0,0,0,-1,0,1,-1,-1};
138 static int drive3[8] = {0,0,0,-1,0,1,-1,-1};
139
140 static int (*drives[4])[8] = {&drive0,&drive1,&drive2,&drive3};
141 static int pd_drive_count;
142
143 #define D_PRT   0
144 #define D_PRO   1
145 #define D_UNI   2
146 #define D_MOD   3
147 #define D_GEO   4
148 #define D_SBY   5
149 #define D_DLY   6
150 #define D_SLV   7
151
152 #define DU              (*drives[unit])
153
154 /* end of parameters */
155
156 #include <linux/module.h>
157 #include <linux/errno.h>
158 #include <linux/fs.h>
159 #include <linux/devfs_fs_kernel.h>
160 #include <linux/kernel.h>
161 #include <linux/delay.h>
162 #include <linux/genhd.h>
163 #include <linux/hdreg.h>
164 #include <linux/cdrom.h>        /* for the eject ioctl */
165 #include <linux/spinlock.h>
166
167 #include <asm/uaccess.h>
168
169 #ifndef MODULE
170
171 #include "setup.h"
172
173 static STT pd_stt[7] = {{"drive0",8,drive0},
174                         {"drive1",8,drive1},
175                         {"drive2",8,drive2},
176                         {"drive3",8,drive3},
177                         {"disable",1,&disable},
178                         {"cluster",1,&cluster},
179                         {"nice",1,&nice}};
180
181 void pd_setup( char *str, int *ints)
182
183 {       generic_setup(pd_stt,7,str);
184 }
185
186 #endif
187
188 MODULE_PARM(verbose,"i");
189 MODULE_PARM(major,"i");
190 MODULE_PARM(name,"s");
191 MODULE_PARM(cluster,"i");
192 MODULE_PARM(nice,"i");
193 MODULE_PARM(drive0,"1-8i");
194 MODULE_PARM(drive1,"1-8i");
195 MODULE_PARM(drive2,"1-8i");
196 MODULE_PARM(drive3,"1-8i");
197
198 #include "paride.h"
199
200 #define PD_BITS    4
201
202 /* set up defines for blk.h,  why don't all drivers do it this way ? */
203
204 #define MAJOR_NR   major
205 #define DEVICE_NAME "PD"
206 #define DEVICE_REQUEST do_pd_request
207 #define DEVICE_NR(device) (MINOR(device)>>PD_BITS)
208 #define DEVICE_ON(device)
209 #define DEVICE_OFF(device)
210
211 #include <linux/blk.h>
212 #include <linux/blkpg.h>
213
214 #include "pseudo.h"
215
216 #define PD_PARTNS       (1<<PD_BITS)
217 #define PD_DEVS         PD_PARTNS*PD_UNITS
218
219 /* numbers for "SCSI" geometry */
220
221 #define PD_LOG_HEADS    64
222 #define PD_LOG_SECTS    32
223
224 #define PD_ID_OFF       54
225 #define PD_ID_LEN       14
226
227 #define PD_MAX_RETRIES  5
228 #define PD_TMO          800             /* interrupt timeout in jiffies */
229 #define PD_SPIN_DEL     50              /* spin delay in micro-seconds  */
230
231 #define PD_SPIN         (1000000*PD_TMO)/(HZ*PD_SPIN_DEL)  
232
233 #define STAT_ERR        0x00001
234 #define STAT_INDEX      0x00002
235 #define STAT_ECC        0x00004
236 #define STAT_DRQ        0x00008
237 #define STAT_SEEK       0x00010
238 #define STAT_WRERR      0x00020
239 #define STAT_READY      0x00040
240 #define STAT_BUSY       0x00080
241
242 #define ERR_AMNF        0x00100
243 #define ERR_TK0NF       0x00200
244 #define ERR_ABRT        0x00400
245 #define ERR_MCR         0x00800
246 #define ERR_IDNF        0x01000
247 #define ERR_MC          0x02000
248 #define ERR_UNC         0x04000
249 #define ERR_TMO         0x10000
250
251 #define IDE_READ                0x20
252 #define IDE_WRITE               0x30
253 #define IDE_READ_VRFY           0x40
254 #define IDE_INIT_DEV_PARMS      0x91
255 #define IDE_STANDBY             0x96
256 #define IDE_ACKCHANGE           0xdb
257 #define IDE_DOORLOCK            0xde
258 #define IDE_DOORUNLOCK          0xdf
259 #define IDE_IDENTIFY            0xec
260 #define IDE_EJECT               0xed
261
262 int pd_init(void);
263 void pd_setup(char * str, int * ints);
264 #ifdef MODULE
265 void cleanup_module( void );
266 #endif
267 static int pd_open(struct inode *inode, struct file *file);
268 static void do_pd_request(request_queue_t * q);
269 static int pd_ioctl(struct inode *inode,struct file *file,
270                     unsigned int cmd, unsigned long arg);
271 static int pd_release (struct inode *inode, struct file *file);
272 static int pd_revalidate(kdev_t dev);
273 static int pd_detect(void);
274 static void do_pd_read(void);
275 static void do_pd_read_start(void);
276 static void do_pd_write(void);
277 static void do_pd_write_start(void);
278 static void do_pd_read_drq( void );
279 static void do_pd_write_done( void );
280
281 static int pd_identify (int unit);
282 static void pd_media_check(int unit);
283 static void pd_doorlock(int unit, int func);
284 static int pd_check_media(kdev_t dev);
285 static void pd_eject( int unit);
286
287 static struct hd_struct pd_hd[PD_DEVS];
288 static int pd_sizes[PD_DEVS];
289 static int pd_blocksizes[PD_DEVS];
290
291 #define PD_NAMELEN      8
292
293 struct pd_unit {
294         struct pi_adapter pia;          /* interface to paride layer */
295         struct pi_adapter *pi;
296         int access;                     /* count of active opens ... */
297         int capacity;                   /* Size of this volume in sectors */
298         int heads;                      /* physical geometry */
299         int sectors;
300         int cylinders;
301         int drive;                      /* master=0 slave=1 */
302         int changed;                    /* Have we seen a disk change ? */
303         int removable;                  /* removable media device  ?  */
304         int standby;
305         int alt_geom;
306         int present;
307         char name[PD_NAMELEN];          /* pda, pdb, etc ... */
308         };
309
310 struct pd_unit pd[PD_UNITS];
311
312 /*  'unit' must be defined in all functions - either as a local or a param */
313
314 #define PD pd[unit]
315 #define PI PD.pi
316
317 static int pd_valid = 1;                /* serialise partition checks */
318 static char pd_scratch[512];            /* scratch block buffer */
319
320 /* the variables below are used mainly in the I/O request engine, which
321    processes only one request at a time.
322 */
323
324 static int pd_retries = 0;              /* i/o error retry count */
325 static int pd_busy = 0;                 /* request being processed ? */
326 static int pd_block;                    /* address of next requested block */
327 static int pd_count;                    /* number of blocks still to do */
328 static int pd_run;                      /* sectors in current cluster */
329 static int pd_cmd;                      /* current command READ/WRITE */
330 static int pd_unit;                     /* unit of current request */
331 static int pd_dev;                      /* minor of current request */
332 static char * pd_buf;                   /* buffer for request in progress */
333
334 static DECLARE_WAIT_QUEUE_HEAD(pd_wait_open);
335
336 static char *pd_errs[17] = { "ERR","INDEX","ECC","DRQ","SEEK","WRERR",
337                              "READY","BUSY","AMNF","TK0NF","ABRT","MCR",
338                              "IDNF","MC","UNC","???","TMO"};
339
340 /* kernel glue structures */
341
342 extern struct block_device_operations pd_fops;
343
344 static struct gendisk pd_gendisk = {
345         major:          PD_MAJOR,
346         major_name:     PD_NAME,
347         minor_shift:    PD_BITS,
348         max_p:          PD_PARTNS,
349         part:           pd_hd,
350         sizes:          pd_sizes,
351         fops:           &pd_fops,
352 };
353
354 static struct block_device_operations pd_fops = {
355         owner:                  THIS_MODULE,
356         open:                   pd_open,
357         release:                pd_release,
358         ioctl:                  pd_ioctl,
359         check_media_change:     pd_check_media,
360         revalidate:             pd_revalidate
361 };
362
363 void pd_init_units( void )
364
365 {       int     unit, j;
366
367         pd_drive_count = 0;
368         for (unit=0;unit<PD_UNITS;unit++) {
369                 PD.pi = & PD.pia;
370                 PD.access = 0;
371                 PD.changed = 1;
372                 PD.capacity = 0;
373                 PD.drive = DU[D_SLV];
374                 PD.present = 0;
375                 j = 0;
376                 while ((j < PD_NAMELEN-2) && (PD.name[j]=name[j])) j++;
377                 PD.name[j++] = 'a' + unit;
378                 PD.name[j] = 0;
379                 PD.alt_geom = DU[D_GEO];
380                 PD.standby = DU[D_SBY];
381                 if (DU[D_PRT]) pd_drive_count++;
382         }
383 }
384
385 int pd_init (void)
386
387 {       int i;
388         request_queue_t * q; 
389
390         if (disable) return -1;
391         if (devfs_register_blkdev(MAJOR_NR,name,&pd_fops)) {
392                 printk("%s: unable to get major number %d\n",
393                         name,major);
394                 return -1;
395         }
396         q = BLK_DEFAULT_QUEUE(MAJOR_NR);
397         blk_init_queue(q, DEVICE_REQUEST);
398         blk_queue_max_sectors(q, cluster);
399         read_ahead[MAJOR_NR] = 8;       /* 8 sector (4kB) read ahead */
400         
401         pd_gendisk.major = major;
402         pd_gendisk.major_name = name;
403         add_gendisk(&pd_gendisk);
404
405         for(i=0;i<PD_DEVS;i++) pd_blocksizes[i] = 1024;
406         blksize_size[MAJOR_NR] = pd_blocksizes;
407
408         printk("%s: %s version %s, major %d, cluster %d, nice %d\n",
409                 name,name,PD_VERSION,major,cluster,nice);
410         pd_init_units();
411         pd_valid = 0;
412         pd_gendisk.nr_real = pd_detect();
413         pd_valid = 1;
414
415 #ifdef MODULE
416         if (!pd_gendisk.nr_real) {
417                 cleanup_module();
418                 return -1;
419         }
420 #endif
421         return 0;
422 }
423
424 static int pd_open (struct inode *inode, struct file *file)
425
426 {       int unit = DEVICE_NR(inode->i_rdev);
427
428         if ((unit >= PD_UNITS) || (!PD.present)) return -ENODEV;
429
430         wait_event (pd_wait_open, pd_valid);
431
432         PD.access++;
433
434         if (PD.removable) {
435                 pd_media_check(unit);
436                 pd_doorlock(unit,IDE_DOORLOCK);
437         }
438         return 0;
439 }
440
441 static int pd_ioctl(struct inode *inode,struct file *file,
442                     unsigned int cmd, unsigned long arg)
443 {
444         struct hd_geometry *geo = (struct hd_geometry *) arg;
445         int err, unit;
446
447         if (!inode || !inode->i_rdev)
448                 return -EINVAL;
449         unit = DEVICE_NR(inode->i_rdev);
450         if (!PD.present)
451                 return -ENODEV;
452
453         switch (cmd) {
454             case CDROMEJECT:
455                 if (PD.access == 1) pd_eject(unit);
456                 return 0;
457             case HDIO_GETGEO:
458                 if (!geo) return -EINVAL;
459                 err = verify_area(VERIFY_WRITE,geo,sizeof(*geo));
460                 if (err) return err;
461
462                 if (PD.alt_geom) {
463                     put_user(PD.capacity/(PD_LOG_HEADS*PD_LOG_SECTS), 
464                                 (short *) &geo->cylinders);
465                     put_user(PD_LOG_HEADS, (char *) &geo->heads);
466                     put_user(PD_LOG_SECTS, (char *) &geo->sectors);
467                 } else {
468                     put_user(PD.cylinders, (short *) &geo->cylinders);
469                     put_user(PD.heads, (char *) &geo->heads);
470                     put_user(PD.sectors, (char *) &geo->sectors);
471                 }
472                 put_user(get_start_sect(inode->i_rdev), (long *)&geo->start);
473                 return 0;
474             case BLKRRPART:
475                 if (!capable(CAP_SYS_ADMIN))
476                         return -EACCES;
477                 return pd_revalidate(inode->i_rdev);
478             case BLKGETSIZE:
479             case BLKGETSIZE64:
480             case BLKROSET:
481             case BLKROGET:
482             case BLKRASET:
483             case BLKRAGET:
484             case BLKFLSBUF:
485             case BLKPG:
486                 return blk_ioctl(inode->i_rdev, cmd, arg);
487             default:
488                 return -EINVAL;
489         }
490 }
491
492 static int pd_release (struct inode *inode, struct file *file)
493
494 {       kdev_t devp;
495         int     unit;
496
497         devp = inode->i_rdev;
498         unit = DEVICE_NR(devp);
499
500         if ((unit >= PD_UNITS) || (PD.access <= 0)) 
501                 return -EINVAL;
502
503         PD.access--;
504
505         if (!PD.access && PD.removable)
506                 pd_doorlock(unit,IDE_DOORUNLOCK);
507
508         return 0;
509 }
510
511 static int pd_check_media( kdev_t dev)
512
513 {       int r, unit;
514
515         unit = DEVICE_NR(dev);
516         if ((unit >= PD_UNITS) || (!PD.present)) return -ENODEV;
517         if (!PD.removable) return 0;
518         pd_media_check(unit);
519         r = PD.changed;
520         PD.changed = 0;
521         return r;
522 }
523
524 static int pd_revalidate(kdev_t dev)
525 {
526         int unit, res;
527         long flags;
528
529         unit = DEVICE_NR(dev);
530         if ((unit >= PD_UNITS) || !PD.present)
531                 return -ENODEV;
532
533         save_flags(flags);
534         cli(); 
535         if (PD.access > 1) {
536                 restore_flags(flags);
537                 return -EBUSY;
538         }
539         pd_valid = 0;
540         restore_flags(flags);   
541
542         res = wipe_partitions(dev);
543
544         if (res == 0 && pd_identify(unit))
545                 grok_partitions(dev, PD.capacity);
546
547         pd_valid = 1;
548         wake_up(&pd_wait_open);
549  
550         return res;
551 }
552
553 #ifdef MODULE
554
555 /* Glue for modules ... */
556
557 void    cleanup_module(void);
558
559 int     init_module(void)
560
561 {
562
563 #ifdef PARIDE_JUMBO
564        { extern paride_init();
565          paride_init();
566        } 
567 #endif
568         return pd_init();
569 }
570
571 void    cleanup_module(void)
572 {
573         int unit;
574
575         devfs_unregister_blkdev(MAJOR_NR, name);
576         del_gendisk(&pd_gendisk);
577
578         for (unit=0; unit<PD_UNITS; unit++) 
579                 if (PD.present)
580                         pi_release(PI);
581 }
582 #endif
583
584 #define WR(c,r,v)       pi_write_regr(PI,c,r,v)
585 #define RR(c,r)         (pi_read_regr(PI,c,r))
586
587 #define DRIVE           (0xa0+0x10*PD.drive)
588
589 /*  ide command interface */
590
591 static void pd_print_error( int unit, char * msg, int status )
592
593 {       int     i;
594
595         printk("%s: %s: status = 0x%x =",PD.name,msg,status);
596         for(i=0;i<18;i++) if (status & (1<<i)) printk(" %s",pd_errs[i]);
597         printk("\n");
598 }
599
600 static void pd_reset( int unit )    /* called only for MASTER drive */
601
602 {       pi_connect(PI);
603         WR(1,6,4);
604         udelay(50);
605         WR(1,6,0);
606         pi_disconnect(PI);
607         udelay(250);
608 }
609
610 #define DBMSG(msg)      ((verbose>1)?(msg):NULL)
611
612 static int pd_wait_for( int unit, int w, char * msg )    /* polled wait */
613
614 {       int     k, r, e;
615
616         k=0;
617         while(k < PD_SPIN) { 
618             r = RR(1,6);
619             k++;
620             if (((r & w) == w) && !(r & STAT_BUSY)) break;
621             udelay(PD_SPIN_DEL);
622         }
623         e = (RR(0,1)<<8) + RR(0,7);
624         if (k >= PD_SPIN)  e |= ERR_TMO;
625         if ((e & (STAT_ERR|ERR_TMO)) && (msg != NULL)) 
626                 pd_print_error(unit,msg,e);
627         return e;
628 }
629
630 static void pd_send_command( int unit, int n, int s, int h, 
631                              int c0, int c1, int func )
632
633 {
634         WR(0,6,DRIVE+h);
635         WR(0,1,0);                /* the IDE task file */
636         WR(0,2,n);
637         WR(0,3,s);
638         WR(0,4,c0);
639         WR(0,5,c1);
640         WR(0,7,func);
641
642         udelay(1);
643 }
644
645 static void pd_ide_command( int unit, int func, int block, int count )
646
647 /* Don't use this call if the capacity is zero. */
648
649 {       int c1, c0, h, s;
650
651         s  = ( block % PD.sectors) + 1;
652         h  = ( block / PD.sectors) % PD.heads;
653         c0 = ( block / (PD.sectors*PD.heads)) % 256;
654         c1 = ( block / (PD.sectors*PD.heads*256));
655
656         pd_send_command(unit,count,s,h,c0,c1,func);
657 }
658
659 /* According to the ATA standard, the default CHS geometry should be
660    available following a reset.  Some Western Digital drives come up
661    in a mode where only LBA addresses are accepted until the device
662    parameters are initialised.
663 */
664
665 static void pd_init_dev_parms( int unit )
666  
667 {       pi_connect(PI);
668         pd_wait_for(unit,0,DBMSG("before init_dev_parms"));
669         pd_send_command(unit,PD.sectors,0,PD.heads-1,0,0,IDE_INIT_DEV_PARMS);
670         udelay(300);
671         pd_wait_for(unit,0,"Initialise device parameters");
672         pi_disconnect(PI);
673 }
674
675 static void pd_doorlock( int unit, int func )
676
677 {       pi_connect(PI);
678         if (pd_wait_for(unit,STAT_READY,"Lock") & STAT_ERR) {
679                 pi_disconnect(PI);
680                 return;
681         }
682         pd_send_command(unit,1,0,0,0,0,func);
683         pd_wait_for(unit,STAT_READY,"Lock done");
684         pi_disconnect(PI);
685 }
686
687 static void pd_eject( int unit )
688
689 {       pi_connect(PI);
690         pd_wait_for(unit,0,DBMSG("before unlock on eject"));
691         pd_send_command(unit,1,0,0,0,0,IDE_DOORUNLOCK);
692         pd_wait_for(unit,0,DBMSG("after unlock on eject"));
693         pd_wait_for(unit,0,DBMSG("before eject"));
694         pd_send_command(unit,0,0,0,0,0,IDE_EJECT);
695         pd_wait_for(unit,0,DBMSG("after eject"));
696         pi_disconnect(PI);
697 }
698
699 static void pd_media_check( int unit )
700
701 {       int     r;
702
703         pi_connect(PI);
704         r = pd_wait_for(unit,STAT_READY,DBMSG("before media_check"));
705         if (!(r & STAT_ERR)) {
706                 pd_send_command(unit,1,1,0,0,0,IDE_READ_VRFY);  
707                 r = pd_wait_for(unit,STAT_READY,DBMSG("RDY after READ_VRFY"));
708         } else PD.changed = 1;   /* say changed if other error */
709         if (r & ERR_MC) {
710                 PD.changed = 1;
711                 pd_send_command(unit,1,0,0,0,0,IDE_ACKCHANGE);
712                 pd_wait_for(unit,STAT_READY,DBMSG("RDY after ACKCHANGE"));
713                 pd_send_command(unit,1,1,0,0,0,IDE_READ_VRFY);
714                 r = pd_wait_for(unit,STAT_READY,DBMSG("RDY after VRFY"));
715         }
716         pi_disconnect(PI);
717
718 }
719
720 static void pd_standby_off( int unit )
721
722 {       pi_connect(PI);
723         pd_wait_for(unit,0,DBMSG("before STANDBY"));
724         pd_send_command(unit,0,0,0,0,0,IDE_STANDBY);
725         pd_wait_for(unit,0,DBMSG("after STANDBY"));
726         pi_disconnect(PI);
727 }
728
729 #define  word_val(n) ((pd_scratch[2*n]&0xff)+256*(pd_scratch[2*n+1]&0xff))
730
731 static int pd_identify( int unit )
732
733 {       int     j;
734         char id[PD_ID_LEN+1];
735
736 /* WARNING:  here there may be dragons.  reset() applies to both drives,
737    but we call it only on probing the MASTER. This should allow most
738    common configurations to work, but be warned that a reset can clear
739    settings on the SLAVE drive.
740 */ 
741
742         if (PD.drive == 0) pd_reset(unit);
743
744         pi_connect(PI);
745         WR(0,6,DRIVE);
746         pd_wait_for(unit,0,DBMSG("before IDENT"));  
747         pd_send_command(unit,1,0,0,0,0,IDE_IDENTIFY);
748
749         if (pd_wait_for(unit,STAT_DRQ,DBMSG("IDENT DRQ")) & STAT_ERR) {
750                 pi_disconnect(PI);
751                 return 0;
752         }
753         pi_read_block(PI,pd_scratch,512);
754         pi_disconnect(PI);
755         PD.sectors = word_val(6);
756         PD.heads = word_val(3);
757         PD.cylinders  = word_val(1);
758         PD.capacity = PD.sectors*PD.heads*PD.cylinders;
759
760         for(j=0;j<PD_ID_LEN;j++) id[j^1] = pd_scratch[j+PD_ID_OFF];
761         j = PD_ID_LEN-1;
762         while ((j >= 0) && (id[j] <= 0x20)) j--;
763         j++; id[j] = 0;
764
765         PD.removable = (word_val(0) & 0x80);
766  
767         printk("%s: %s, %s, %d blocks [%dM], (%d/%d/%d), %s media\n",
768                     PD.name,id,
769                     PD.drive?"slave":"master",
770                     PD.capacity,PD.capacity/2048,
771                     PD.cylinders,PD.heads,PD.sectors,
772                     PD.removable?"removable":"fixed");
773
774         if (PD.capacity) pd_init_dev_parms(unit);
775         if (!PD.standby) pd_standby_off(unit);
776         
777         return 1;
778 }
779
780 static int pd_probe_drive( int unit )
781 {
782         if (PD.drive == -1) {
783                 for (PD.drive=0;PD.drive<=1;PD.drive++)
784                         if (pd_identify(unit))
785                                 return 1;
786                 return 0;
787         }
788         return pd_identify(unit);
789 }
790
791 static int pd_detect( void )
792
793 {       int     k, unit;
794
795         k = 0;
796         if (pd_drive_count == 0) {  /* nothing spec'd - so autoprobe for 1 */
797             unit = 0;
798             if (pi_init(PI,1,-1,-1,-1,-1,-1,pd_scratch,
799                      PI_PD,verbose,PD.name)) {
800                 if (pd_probe_drive(unit)) {
801                         PD.present = 1;
802                         k = 1;
803                 } else pi_release(PI);
804             }
805
806         } else for (unit=0;unit<PD_UNITS;unit++) if (DU[D_PRT])
807             if (pi_init(PI,0,DU[D_PRT],DU[D_MOD],DU[D_UNI],
808                         DU[D_PRO],DU[D_DLY],pd_scratch,
809                         PI_PD,verbose,PD.name)) {
810                 if (pd_probe_drive(unit)) {
811                         PD.present = 1;
812                         k = unit+1;
813                 } else pi_release(PI);
814             }
815         for (unit=0;unit<PD_UNITS;unit++)
816                 register_disk(&pd_gendisk,MKDEV(MAJOR_NR,unit<<PD_BITS),
817                                 PD_PARTNS,&pd_fops,
818                                 PD.present?PD.capacity:0);
819
820 /* We lie about the number of drives found, as the generic partition
821    scanner assumes that the drives are numbered sequentially from 0.
822    This can result in some bogus error messages if non-sequential
823    drive numbers are used.
824 */
825         if (k)
826                 return k; 
827         printk("%s: no valid drive found\n",name);
828         return 0;
829 }
830
831 /* The i/o request engine */
832
833 static int pd_ready( void )
834
835 {       int unit = pd_unit;
836
837         return (!(RR(1,6) & STAT_BUSY)) ;
838 }
839
840 static void do_pd_request (request_queue_t * q)
841
842 {       struct buffer_head * bh;
843         int     unit;
844
845         if (pd_busy) return;
846 repeat:
847         if (QUEUE_EMPTY || (CURRENT->rq_status == RQ_INACTIVE)) return;
848         INIT_REQUEST;
849
850         pd_dev = MINOR(CURRENT->rq_dev);
851         pd_unit = unit = DEVICE_NR(CURRENT->rq_dev);
852         pd_block = CURRENT->sector;
853         pd_run = CURRENT->nr_sectors;
854         pd_count = CURRENT->current_nr_sectors;
855
856         bh = CURRENT->bh;
857
858         if ((pd_dev >= PD_DEVS) || 
859             ((pd_block+pd_count) > pd_hd[pd_dev].nr_sects)) {
860                 end_request(0);
861                 goto repeat;
862         }
863
864         pd_cmd = CURRENT->cmd;
865         pd_buf = CURRENT->buffer;
866         pd_retries = 0;
867
868         pd_busy = 1;
869         if (pd_cmd == READ) pi_do_claimed(PI,do_pd_read);
870         else if (pd_cmd == WRITE) pi_do_claimed(PI,do_pd_write);
871         else {  pd_busy = 0;
872                 end_request(0);
873                 goto repeat;
874         }
875 }
876
877 static void pd_next_buf( int unit )
878
879 {       long    saved_flags;
880
881         spin_lock_irqsave(&io_request_lock,saved_flags);
882         end_request(1);
883         if (!pd_run) {  spin_unlock_irqrestore(&io_request_lock,saved_flags);
884                         return; 
885         }
886         
887 /* paranoia */
888
889         if (QUEUE_EMPTY ||
890             (CURRENT->cmd != pd_cmd) ||
891             (MINOR(CURRENT->rq_dev) != pd_dev) ||
892             (CURRENT->rq_status == RQ_INACTIVE) ||
893             (CURRENT->sector != pd_block)) 
894                 printk("%s: OUCH: request list changed unexpectedly\n",
895                         PD.name);
896
897         pd_count = CURRENT->current_nr_sectors;
898         pd_buf = CURRENT->buffer;
899         spin_unlock_irqrestore(&io_request_lock,saved_flags);
900 }
901
902 static void do_pd_read( void )
903
904 {       ps_set_intr(do_pd_read_start,0,0,nice);
905 }
906
907 static void do_pd_read_start( void )
908  
909 {       int     unit = pd_unit;
910         long    saved_flags;
911
912         pd_busy = 1;
913
914         pi_connect(PI);
915         if (pd_wait_for(unit,STAT_READY,"do_pd_read") & STAT_ERR) {
916                 pi_disconnect(PI);
917                 if (pd_retries < PD_MAX_RETRIES) {
918                         pd_retries++;
919                         pi_do_claimed(PI,do_pd_read_start);
920                         return;
921                 }
922                 spin_lock_irqsave(&io_request_lock,saved_flags);
923                 end_request(0);
924                 pd_busy = 0;
925                 do_pd_request(NULL);
926                 spin_unlock_irqrestore(&io_request_lock,saved_flags);
927                 return;
928         }
929         pd_ide_command(unit,IDE_READ,pd_block,pd_run);
930         ps_set_intr(do_pd_read_drq,pd_ready,PD_TMO,nice);
931 }
932
933 static void do_pd_read_drq( void )
934
935 {       int     unit = pd_unit;
936         long    saved_flags;
937
938         while (1) {
939             if (pd_wait_for(unit,STAT_DRQ,"do_pd_read_drq") & STAT_ERR) {
940                 pi_disconnect(PI);
941                 if (pd_retries < PD_MAX_RETRIES) {
942                         pd_retries++;
943                         pi_do_claimed(PI,do_pd_read_start);
944                         return;
945                 }
946                 spin_lock_irqsave(&io_request_lock,saved_flags);
947                 end_request(0);
948                 pd_busy = 0;
949                 do_pd_request(NULL);
950                 spin_unlock_irqrestore(&io_request_lock,saved_flags);
951                 return;
952             }
953             pi_read_block(PI,pd_buf,512);
954             pd_count--; pd_run--;
955             pd_buf += 512;
956             pd_block++;
957             if (!pd_run) break;
958             if (!pd_count) pd_next_buf(unit);
959         }
960         pi_disconnect(PI);
961         spin_lock_irqsave(&io_request_lock,saved_flags);
962         end_request(1);
963         pd_busy = 0;
964         do_pd_request(NULL);
965         spin_unlock_irqrestore(&io_request_lock,saved_flags);
966 }
967
968 static void do_pd_write( void )
969
970 {        ps_set_intr(do_pd_write_start,0,0,nice);
971 }
972
973 static void do_pd_write_start( void )
974
975 {       int     unit = pd_unit;
976         long    saved_flags;
977
978         pd_busy = 1;
979
980         pi_connect(PI);
981         if (pd_wait_for(unit,STAT_READY,"do_pd_write") & STAT_ERR) {
982                 pi_disconnect(PI);
983                 if (pd_retries < PD_MAX_RETRIES) {
984                         pd_retries++;
985                         pi_do_claimed(PI,do_pd_write_start);
986                         return;
987                 }
988                 spin_lock_irqsave(&io_request_lock,saved_flags);
989                 end_request(0);
990                 pd_busy = 0;
991                 do_pd_request(NULL);
992                 spin_unlock_irqrestore(&io_request_lock,saved_flags);
993                 return;
994         }
995         pd_ide_command(unit,IDE_WRITE,pd_block,pd_run);
996         while (1) {
997             if (pd_wait_for(unit,STAT_DRQ,"do_pd_write_drq") & STAT_ERR) {
998                 pi_disconnect(PI);
999                 if (pd_retries < PD_MAX_RETRIES) {
1000                         pd_retries++;
1001                         pi_do_claimed(PI,do_pd_write_start);
1002                         return;
1003                 }
1004                 spin_lock_irqsave(&io_request_lock,saved_flags);
1005                 end_request(0);
1006                 pd_busy = 0;
1007                 do_pd_request(NULL);
1008                 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1009                 return;
1010             }
1011             pi_write_block(PI,pd_buf,512);
1012             pd_count--; pd_run--;
1013             pd_buf += 512;
1014             pd_block++;
1015             if (!pd_run) break;
1016             if (!pd_count) pd_next_buf(unit);
1017         }
1018         ps_set_intr(do_pd_write_done,pd_ready,PD_TMO,nice);
1019 }
1020
1021 static void do_pd_write_done( void )
1022
1023 {       int     unit = pd_unit;
1024         long    saved_flags;
1025
1026         if (pd_wait_for(unit,STAT_READY,"do_pd_write_done") & STAT_ERR) {
1027                 pi_disconnect(PI);
1028                 if (pd_retries < PD_MAX_RETRIES) {
1029                         pd_retries++;
1030                         pi_do_claimed(PI,do_pd_write_start);
1031                         return;
1032                 }
1033                 spin_lock_irqsave(&io_request_lock,saved_flags);
1034                 end_request(0);
1035                 pd_busy = 0;
1036                 do_pd_request(NULL);
1037                 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1038                 return;
1039         }
1040         pi_disconnect(PI);
1041         spin_lock_irqsave(&io_request_lock,saved_flags);
1042         end_request(1);
1043         pd_busy = 0;
1044         do_pd_request(NULL);
1045         spin_unlock_irqrestore(&io_request_lock,saved_flags);
1046 }
1047
1048 /* end of pd.c */
1049
1050 MODULE_LICENSE("GPL");