Import 2.3.40pre5
[davej-history.git] / drivers / block / paride / pd.c
blob960d7c36919988410f739c6e2b81d7c9bf83f2f2
1 /*
2 pd.c (c) 1997-8 Grant R. Guenther <grant@torque.net>
3 Under the terms of the GNU public license.
5 This is the high-level driver for parallel port IDE hard
6 drives based on chips supported by the paride module.
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.
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:
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>
21 Where,
23 <prt> is the base of the parallel port address for
24 the corresponding drive. (required)
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)
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)
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)
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)
48 <sby> set this to zero to disable the power saving
49 standby mode, if needed. (1 if not given)
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)
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.
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.
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")
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)
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)
86 nice This parameter controls the driver's use of
87 idle CPU time, at the expense of some speed.
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:
93 pd.drive0
94 pd.drive1
95 pd.drive2
96 pd.drive3
97 pd.cluster
98 pd.nice
100 In addition, you can use the parameter pd.disable to disable
101 the driver entirely.
105 /* Changes:
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
117 #define PD_VERSION "1.05"
118 #define PD_MAJOR 45
119 #define PD_NAME "pd"
120 #define PD_UNITS 4
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.
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;
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};
140 static int (*drives[4])[8] = {&drive0,&drive1,&drive2,&drive3};
141 static int pd_drive_count;
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
152 #define DU (*drives[unit])
154 /* end of parameters */
156 #include <linux/module.h>
157 #include <linux/errno.h>
158 #include <linux/fs.h>
159 #include <linux/kernel.h>
160 #include <linux/delay.h>
161 #include <linux/genhd.h>
162 #include <linux/hdreg.h>
163 #include <linux/cdrom.h> /* for the eject ioctl */
164 #include <linux/spinlock.h>
166 #include <asm/uaccess.h>
168 #ifndef MODULE
170 #include "setup.h"
172 static STT pd_stt[7] = {{"drive0",8,drive0},
173 {"drive1",8,drive1},
174 {"drive2",8,drive2},
175 {"drive3",8,drive3},
176 {"disable",1,&disable},
177 {"cluster",1,&cluster},
178 {"nice",1,&nice}};
180 void pd_setup( char *str, int *ints)
182 { generic_setup(pd_stt,7,str);
185 #endif
187 MODULE_PARM(verbose,"i");
188 MODULE_PARM(major,"i");
189 MODULE_PARM(name,"s");
190 MODULE_PARM(cluster,"i");
191 MODULE_PARM(nice,"i");
192 MODULE_PARM(drive0,"1-8i");
193 MODULE_PARM(drive1,"1-8i");
194 MODULE_PARM(drive2,"1-8i");
195 MODULE_PARM(drive3,"1-8i");
197 #include "paride.h"
199 #define PD_BITS 4
201 /* set up defines for blk.h, why don't all drivers do it this way ? */
203 #define MAJOR_NR major
204 #define DEVICE_NAME "PD"
205 #define DEVICE_REQUEST do_pd_request
206 #define DEVICE_NR(device) (MINOR(device)>>PD_BITS)
207 #define DEVICE_ON(device)
208 #define DEVICE_OFF(device)
210 #include <linux/blk.h>
211 #include <linux/blkpg.h>
213 #include "pseudo.h"
215 #define PD_PARTNS (1<<PD_BITS)
216 #define PD_DEVS PD_PARTNS*PD_UNITS
218 /* numbers for "SCSI" geometry */
220 #define PD_LOG_HEADS 64
221 #define PD_LOG_SECTS 32
223 #define PD_ID_OFF 54
224 #define PD_ID_LEN 14
226 #define PD_MAX_RETRIES 5
227 #define PD_TMO 800 /* interrupt timeout in jiffies */
228 #define PD_SPIN_DEL 50 /* spin delay in micro-seconds */
230 #define PD_SPIN (1000000*PD_TMO)/(HZ*PD_SPIN_DEL)
232 #define STAT_ERR 0x00001
233 #define STAT_INDEX 0x00002
234 #define STAT_ECC 0x00004
235 #define STAT_DRQ 0x00008
236 #define STAT_SEEK 0x00010
237 #define STAT_WRERR 0x00020
238 #define STAT_READY 0x00040
239 #define STAT_BUSY 0x00080
241 #define ERR_AMNF 0x00100
242 #define ERR_TK0NF 0x00200
243 #define ERR_ABRT 0x00400
244 #define ERR_MCR 0x00800
245 #define ERR_IDNF 0x01000
246 #define ERR_MC 0x02000
247 #define ERR_UNC 0x04000
248 #define ERR_TMO 0x10000
250 #define IDE_READ 0x20
251 #define IDE_WRITE 0x30
252 #define IDE_READ_VRFY 0x40
253 #define IDE_INIT_DEV_PARMS 0x91
254 #define IDE_STANDBY 0x96
255 #define IDE_ACKCHANGE 0xdb
256 #define IDE_DOORLOCK 0xde
257 #define IDE_DOORUNLOCK 0xdf
258 #define IDE_IDENTIFY 0xec
259 #define IDE_EJECT 0xed
261 int pd_init(void);
262 void pd_setup(char * str, int * ints);
263 #ifdef MODULE
264 void cleanup_module( void );
265 #endif
266 static void pd_geninit(void);
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 );
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);
287 static struct hd_struct pd_hd[PD_DEVS];
288 static int pd_sizes[PD_DEVS];
289 static int pd_blocksizes[PD_DEVS];
291 #define PD_NAMELEN 8
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 ... */
310 struct pd_unit pd[PD_UNITS];
312 /* 'unit' must be defined in all functions - either as a local or a param */
314 #define PD pd[unit]
315 #define PI PD.pi
317 static int pd_valid = 1; /* serialise partition checks */
318 static char pd_scratch[512]; /* scratch block buffer */
320 /* the variables below are used mainly in the I/O request engine, which
321 processes only one request at a time.
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 int pd_poffs; /* partition offset of current minor */
333 static char * pd_buf; /* buffer for request in progress */
335 static DECLARE_WAIT_QUEUE_HEAD(pd_wait_open);
337 static char *pd_errs[17] = { "ERR","INDEX","ECC","DRQ","SEEK","WRERR",
338 "READY","BUSY","AMNF","TK0NF","ABRT","MCR",
339 "IDNF","MC","UNC","???","TMO"};
341 /* kernel glue structures */
343 static struct gendisk pd_gendisk = {
344 PD_MAJOR, /* Major number */
345 PD_NAME, /* Major name */
346 PD_BITS, /* Bits to shift to get real from partition */
347 PD_PARTNS, /* Number of partitions per real */
348 pd_hd, /* hd struct */
349 pd_sizes, /* block sizes */
350 0, /* number */
351 NULL, /* internal */
352 NULL /* next */
355 static struct block_device_operations pd_fops = {
356 open: pd_open,
357 release: pd_release,
358 ioctl: pd_ioctl,
359 check_media_change: pd_check_media,
360 revalidate: pd_revalidate
363 void pd_init_units( void )
365 { int unit, j;
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++;
385 int pd_init (void)
387 { int i;
389 if (disable) return -1;
391 if (register_blkdev(MAJOR_NR,name,&pd_fops)) {
392 printk("%s: unable to get major number %d\n",
393 name,major);
394 return -1;
396 blk_init_queue(BLK_DEFAULT_QUEUE(MAJOR_NR), DEVICE_REQUEST);
397 read_ahead[MAJOR_NR] = 8; /* 8 sector (4kB) read ahead */
399 pd_gendisk.major = major;
400 pd_gendisk.major_name = name;
401 pd_gendisk.next = gendisk_head;
402 gendisk_head = &pd_gendisk;
404 for(i=0;i<PD_DEVS;i++) pd_blocksizes[i] = 1024;
405 blksize_size[MAJOR_NR] = pd_blocksizes;
407 printk("%s: %s version %s, major %d, cluster %d, nice %d\n",
408 name,name,PD_VERSION,major,cluster,nice);
409 pd_geninit();
411 return 0;
414 static void pd_geninit (void)
416 pd_init_units();
417 pd_valid = 0;
418 pd_gendisk.nr_real = pd_detect();
419 pd_valid = 1;
421 #ifdef MODULE
422 if (!pd_gendisk.nr_real) cleanup_module();
423 #endif
427 static int pd_open (struct inode *inode, struct file *file)
429 { int unit = DEVICE_NR(inode->i_rdev);
431 if ((unit >= PD_UNITS) || (!PD.present)) return -ENODEV;
433 MOD_INC_USE_COUNT;
435 while (!pd_valid) sleep_on(&pd_wait_open);
437 PD.access++;
439 if (PD.removable) {
440 pd_media_check(unit);
441 pd_doorlock(unit,IDE_DOORLOCK);
443 return 0;
446 static int pd_ioctl(struct inode *inode,struct file *file,
447 unsigned int cmd, unsigned long arg)
449 { struct hd_geometry *geo = (struct hd_geometry *) arg;
450 int dev, err, unit;
452 if ((!inode) || (!inode->i_rdev)) return -EINVAL;
453 dev = MINOR(inode->i_rdev);
454 unit = DEVICE_NR(inode->i_rdev);
455 if (dev >= PD_DEVS) return -EINVAL;
456 if (!PD.present) return -ENODEV;
458 switch (cmd) {
459 case CDROMEJECT:
460 if (PD.access == 1) pd_eject(unit);
461 return 0;
462 case HDIO_GETGEO:
463 if (!geo) return -EINVAL;
464 err = verify_area(VERIFY_WRITE,geo,sizeof(*geo));
465 if (err) return err;
467 if (PD.alt_geom) {
468 put_user(PD.capacity/(PD_LOG_HEADS*PD_LOG_SECTS),
469 (short *) &geo->cylinders);
470 put_user(PD_LOG_HEADS, (char *) &geo->heads);
471 put_user(PD_LOG_SECTS, (char *) &geo->sectors);
472 } else {
473 put_user(PD.cylinders, (short *) &geo->cylinders);
474 put_user(PD.heads, (char *) &geo->heads);
475 put_user(PD.sectors, (char *) &geo->sectors);
477 put_user(pd_hd[dev].start_sect,(long *)&geo->start);
478 return 0;
479 case BLKGETSIZE:
480 if (!arg) return -EINVAL;
481 err = verify_area(VERIFY_WRITE,(long *) arg,sizeof(long));
482 if (err) return (err);
483 put_user(pd_hd[dev].nr_sects,(long *) arg);
484 return (0);
485 case BLKRRPART:
486 if (!capable(CAP_SYS_ADMIN))
487 return -EACCES;
488 return pd_revalidate(inode->i_rdev);
489 case BLKROSET:
490 case BLKROGET:
491 case BLKRASET:
492 case BLKRAGET:
493 case BLKFLSBUF:
494 case BLKPG:
495 return blk_ioctl(inode->i_rdev, cmd, arg);
496 default:
497 return -EINVAL;
501 static int pd_release (struct inode *inode, struct file *file)
503 { kdev_t devp;
504 int unit;
506 devp = inode->i_rdev;
507 unit = DEVICE_NR(devp);
509 if ((unit >= PD_UNITS) || (PD.access <= 0))
510 return -EINVAL;
512 PD.access--;
514 if (!PD.access && PD.removable)
515 pd_doorlock(unit,IDE_DOORUNLOCK);
517 MOD_DEC_USE_COUNT;
519 return 0;
522 static int pd_check_media( kdev_t dev)
524 { int r, unit;
526 unit = DEVICE_NR(dev);
527 if ((unit >= PD_UNITS) || (!PD.present)) return -ENODEV;
528 if (!PD.removable) return 0;
529 pd_media_check(unit);
530 r = PD.changed;
531 PD.changed = 0;
532 return r;
535 static int pd_revalidate(kdev_t dev)
537 { int p, unit, minor;
538 long flags;
539 kdev_t devp;
541 struct super_block *sb;
543 unit = DEVICE_NR(dev);
544 if ((unit >= PD_UNITS) || (!PD.present)) return -ENODEV;
546 save_flags(flags);
547 cli();
548 if (PD.access > 1) {
549 restore_flags(flags);
550 return -EBUSY;
552 pd_valid = 0;
553 restore_flags(flags);
555 for (p=(PD_PARTNS-1);p>=0;p--) {
556 minor = p + unit*PD_PARTNS;
557 devp = MKDEV(MAJOR_NR, minor);
558 fsync_dev(devp);
560 sb = get_super(devp);
561 if (sb) invalidate_inodes(sb);
563 invalidate_buffers(devp);
564 pd_hd[minor].start_sect = 0;
565 pd_hd[minor].nr_sects = 0;
568 if (pd_identify(unit))
569 grok_partitions(&pd_gendisk,unit,1<<PD_BITS,PD.capacity);
571 pd_valid = 1;
572 wake_up(&pd_wait_open);
574 return 0;
577 #ifdef MODULE
579 /* Glue for modules ... */
581 void cleanup_module(void);
583 int init_module(void)
585 { int err, unit;
587 #ifdef PARIDE_JUMBO
588 { extern paride_init();
589 paride_init();
591 #endif
593 err = pd_init();
594 if (err) return err;
596 if (!pd_gendisk.nr_real) return -1;
598 return 0;
601 void cleanup_module(void)
603 { struct gendisk **gdp;
604 int unit;
606 unregister_blkdev(MAJOR_NR,name);
608 for(gdp=&gendisk_head;*gdp;gdp=&((*gdp)->next))
609 if (*gdp == &pd_gendisk) break;
610 if (*gdp) *gdp = (*gdp)->next;
612 for (unit=0;unit<PD_UNITS;unit++)
613 if (PD.present) pi_release(PI);
616 #endif
618 #define WR(c,r,v) pi_write_regr(PI,c,r,v)
619 #define RR(c,r) (pi_read_regr(PI,c,r))
621 #define DRIVE (0xa0+0x10*PD.drive)
623 /* ide command interface */
625 static void pd_print_error( int unit, char * msg, int status )
627 { int i;
629 printk("%s: %s: status = 0x%x =",PD.name,msg,status);
630 for(i=0;i<18;i++) if (status & (1<<i)) printk(" %s",pd_errs[i]);
631 printk("\n");
634 static void pd_reset( int unit ) /* called only for MASTER drive */
636 { pi_connect(PI);
637 WR(1,6,4);
638 udelay(50);
639 WR(1,6,0);
640 pi_disconnect(PI);
641 udelay(250);
644 #define DBMSG(msg) ((verbose>1)?(msg):NULL)
646 static int pd_wait_for( int unit, int w, char * msg ) /* polled wait */
648 { int k, r, e;
650 k=0;
651 while(k < PD_SPIN) {
652 r = RR(1,6);
653 k++;
654 if (((r & w) == w) && !(r & STAT_BUSY)) break;
655 udelay(PD_SPIN_DEL);
657 e = (RR(0,1)<<8) + RR(0,7);
658 if (k >= PD_SPIN) e |= ERR_TMO;
659 if ((e & (STAT_ERR|ERR_TMO)) && (msg != NULL))
660 pd_print_error(unit,msg,e);
661 return e;
664 static void pd_send_command( int unit, int n, int s, int h,
665 int c0, int c1, int func )
668 WR(0,6,DRIVE+h);
669 WR(0,1,0); /* the IDE task file */
670 WR(0,2,n);
671 WR(0,3,s);
672 WR(0,4,c0);
673 WR(0,5,c1);
674 WR(0,7,func);
676 udelay(1);
679 static void pd_ide_command( int unit, int func, int block, int count )
681 /* Don't use this call if the capacity is zero. */
683 { int c1, c0, h, s;
685 s = ( block % PD.sectors) + 1;
686 h = ( block / PD.sectors) % PD.heads;
687 c0 = ( block / (PD.sectors*PD.heads)) % 256;
688 c1 = ( block / (PD.sectors*PD.heads*256));
690 pd_send_command(unit,count,s,h,c0,c1,func);
693 /* According to the ATA standard, the default CHS geometry should be
694 available following a reset. Some Western Digital drives come up
695 in a mode where only LBA addresses are accepted until the device
696 parameters are initialised.
699 static void pd_init_dev_parms( int unit )
701 { pi_connect(PI);
702 pd_wait_for(unit,0,DBMSG("before init_dev_parms"));
703 pd_send_command(unit,PD.sectors,0,PD.heads-1,0,0,IDE_INIT_DEV_PARMS);
704 udelay(300);
705 pd_wait_for(unit,0,"Initialise device parameters");
706 pi_disconnect(PI);
709 static void pd_doorlock( int unit, int func )
711 { pi_connect(PI);
712 if (pd_wait_for(unit,STAT_READY,"Lock") & STAT_ERR) {
713 pi_disconnect(PI);
714 return;
716 pd_send_command(unit,1,0,0,0,0,func);
717 pd_wait_for(unit,STAT_READY,"Lock done");
718 pi_disconnect(PI);
721 static void pd_eject( int unit )
723 { pi_connect(PI);
724 pd_wait_for(unit,0,DBMSG("before unlock on eject"));
725 pd_send_command(unit,1,0,0,0,0,IDE_DOORUNLOCK);
726 pd_wait_for(unit,0,DBMSG("after unlock on eject"));
727 pd_wait_for(unit,0,DBMSG("before eject"));
728 pd_send_command(unit,0,0,0,0,0,IDE_EJECT);
729 pd_wait_for(unit,0,DBMSG("after eject"));
730 pi_disconnect(PI);
733 static void pd_media_check( int unit )
735 { int r;
737 pi_connect(PI);
738 r = pd_wait_for(unit,STAT_READY,DBMSG("before media_check"));
739 if (!(r & STAT_ERR)) {
740 pd_send_command(unit,1,1,0,0,0,IDE_READ_VRFY);
741 r = pd_wait_for(unit,STAT_READY,DBMSG("RDY after READ_VRFY"));
742 } else PD.changed = 1; /* say changed if other error */
743 if (r & ERR_MC) {
744 PD.changed = 1;
745 pd_send_command(unit,1,0,0,0,0,IDE_ACKCHANGE);
746 pd_wait_for(unit,STAT_READY,DBMSG("RDY after ACKCHANGE"));
747 pd_send_command(unit,1,1,0,0,0,IDE_READ_VRFY);
748 r = pd_wait_for(unit,STAT_READY,DBMSG("RDY after VRFY"));
750 pi_disconnect(PI);
754 static void pd_standby_off( int unit )
756 { pi_connect(PI);
757 pd_wait_for(unit,0,DBMSG("before STANDBY"));
758 pd_send_command(unit,0,0,0,0,0,IDE_STANDBY);
759 pd_wait_for(unit,0,DBMSG("after STANDBY"));
760 pi_disconnect(PI);
763 #define word_val(n) ((pd_scratch[2*n]&0xff)+256*(pd_scratch[2*n+1]&0xff))
765 static int pd_identify( int unit )
767 { int j;
768 char id[PD_ID_LEN+1];
770 /* WARNING: here there may be dragons. reset() applies to both drives,
771 but we call it only on probing the MASTER. This should allow most
772 common configurations to work, but be warned that a reset can clear
773 settings on the SLAVE drive.
776 if (PD.drive == 0) pd_reset(unit);
778 pi_connect(PI);
779 WR(0,6,DRIVE);
780 pd_wait_for(unit,0,DBMSG("before IDENT"));
781 pd_send_command(unit,1,0,0,0,0,IDE_IDENTIFY);
783 if (pd_wait_for(unit,STAT_DRQ,DBMSG("IDENT DRQ")) & STAT_ERR) {
784 pi_disconnect(PI);
785 return 0;
787 pi_read_block(PI,pd_scratch,512);
788 pi_disconnect(PI);
789 PD.sectors = word_val(6);
790 PD.heads = word_val(3);
791 PD.cylinders = word_val(1);
792 PD.capacity = PD.sectors*PD.heads*PD.cylinders;
794 for(j=0;j<PD_ID_LEN;j++) id[j^1] = pd_scratch[j+PD_ID_OFF];
795 j = PD_ID_LEN-1;
796 while ((j >= 0) && (id[j] <= 0x20)) j--;
797 j++; id[j] = 0;
799 PD.removable = (word_val(0) & 0x80);
801 printk("%s: %s, %s, %d blocks [%dM], (%d/%d/%d), %s media\n",
802 PD.name,id,
803 PD.drive?"slave":"master",
804 PD.capacity,PD.capacity/2048,
805 PD.cylinders,PD.heads,PD.sectors,
806 PD.removable?"removable":"fixed");
808 if (PD.capacity) pd_init_dev_parms(unit);
809 if (!PD.standby) pd_standby_off(unit);
811 return 1;
814 static int pd_probe_drive( int unit )
816 if (PD.drive == -1) {
817 for (PD.drive=0;PD.drive<=1;PD.drive++)
818 if (pd_identify(unit))
819 return 1;
820 return 0;
822 return pd_identify(unit);
825 static int pd_detect( void )
827 { int k, unit;
829 k = 0;
830 if (pd_drive_count == 0) { /* nothing spec'd - so autoprobe for 1 */
831 unit = 0;
832 if (pi_init(PI,1,-1,-1,-1,-1,-1,pd_scratch,
833 PI_PD,verbose,PD.name)) {
834 if (pd_probe_drive(unit)) {
835 PD.present = 1;
836 grok_partitions(&pd_gendisk,unit,PD_PARTNS,PD.capacity);
837 k = 1;
838 } else pi_release(PI);
841 } else for (unit=0;unit<PD_UNITS;unit++) if (DU[D_PRT])
842 if (pi_init(PI,0,DU[D_PRT],DU[D_MOD],DU[D_UNI],
843 DU[D_PRO],DU[D_DLY],pd_scratch,
844 PI_PD,verbose,PD.name)) {
845 if (pd_probe_drive(unit)) {
846 PD.present = 1;
847 grok_partitions(&pd_gendisk,unit,PD_PARTNS,PD.capacity);
848 k = unit+1;
849 } else pi_release(PI);
852 /* We lie about the number of drives found, as the generic partition
853 scanner assumes that the drives are numbered sequentially from 0.
854 This can result in some bogus error messages if non-sequential
855 drive numbers are used.
857 if (k)
858 return k;
859 printk("%s: no valid drive found\n",name);
860 return 0;
863 /* The i/o request engine */
865 static int pd_ready( void )
867 { int unit = pd_unit;
869 return (!(RR(1,6) & STAT_BUSY)) ;
872 static void do_pd_request (request_queue_t * q)
874 { struct buffer_head * bh;
875 struct request * req;
876 int unit;
878 if (pd_busy) return;
879 repeat:
880 if ((!CURRENT) || (CURRENT->rq_status == RQ_INACTIVE)) return;
881 INIT_REQUEST;
883 pd_dev = MINOR(CURRENT->rq_dev);
884 pd_unit = unit = DEVICE_NR(CURRENT->rq_dev);
885 pd_block = CURRENT->sector;
886 pd_count = CURRENT->nr_sectors;
888 bh = CURRENT->bh;
889 req = CURRENT;
890 if (bh->b_reqnext)
891 printk("%s: OUCH: b_reqnext != NULL\n",PD.name);
893 if ((pd_dev >= PD_DEVS) ||
894 ((pd_block+pd_count) > pd_hd[pd_dev].nr_sects)) {
895 end_request(0);
896 goto repeat;
899 pd_cmd = CURRENT->cmd;
900 pd_run = pd_count;
901 while ((pd_run <= cluster) &&
902 (req = req->next) &&
903 (pd_block+pd_run == req->sector) &&
904 (pd_cmd == req->cmd) &&
905 (pd_dev == MINOR(req->rq_dev)))
906 pd_run += req->nr_sectors;
908 pd_poffs = pd_hd[pd_dev].start_sect;
909 pd_block += pd_poffs;
910 pd_buf = CURRENT->buffer;
911 pd_retries = 0;
913 pd_busy = 1;
914 if (pd_cmd == READ) pi_do_claimed(PI,do_pd_read);
915 else if (pd_cmd == WRITE) pi_do_claimed(PI,do_pd_write);
916 else { pd_busy = 0;
917 end_request(0);
918 goto repeat;
922 static void pd_next_buf( int unit )
924 { long saved_flags;
926 spin_lock_irqsave(&io_request_lock,saved_flags);
927 end_request(1);
928 if (!pd_run) { spin_unlock_irqrestore(&io_request_lock,saved_flags);
929 return;
932 /* paranoia */
934 if ((!CURRENT) ||
935 (CURRENT->cmd != pd_cmd) ||
936 (MINOR(CURRENT->rq_dev) != pd_dev) ||
937 (CURRENT->rq_status == RQ_INACTIVE) ||
938 (CURRENT->sector+pd_poffs != pd_block))
939 printk("%s: OUCH: request list changed unexpectedly\n",
940 PD.name);
942 pd_count = CURRENT->nr_sectors;
943 pd_buf = CURRENT->buffer;
944 spin_unlock_irqrestore(&io_request_lock,saved_flags);
947 static void do_pd_read( void )
949 { ps_set_intr(do_pd_read_start,0,0,nice);
952 static void do_pd_read_start( void )
954 { int unit = pd_unit;
955 long saved_flags;
957 pd_busy = 1;
959 pi_connect(PI);
960 if (pd_wait_for(unit,STAT_READY,"do_pd_read") & STAT_ERR) {
961 pi_disconnect(PI);
962 if (pd_retries < PD_MAX_RETRIES) {
963 pd_retries++;
964 pi_do_claimed(PI,do_pd_read_start);
965 return;
967 spin_lock_irqsave(&io_request_lock,saved_flags);
968 end_request(0);
969 pd_busy = 0;
970 do_pd_request(NULL);
971 spin_unlock_irqrestore(&io_request_lock,saved_flags);
972 return;
974 pd_ide_command(unit,IDE_READ,pd_block,pd_run);
975 ps_set_intr(do_pd_read_drq,pd_ready,PD_TMO,nice);
978 static void do_pd_read_drq( void )
980 { int unit = pd_unit;
981 long saved_flags;
983 while (1) {
984 if (pd_wait_for(unit,STAT_DRQ,"do_pd_read_drq") & STAT_ERR) {
985 pi_disconnect(PI);
986 if (pd_retries < PD_MAX_RETRIES) {
987 pd_retries++;
988 pi_do_claimed(PI,do_pd_read_start);
989 return;
991 spin_lock_irqsave(&io_request_lock,saved_flags);
992 end_request(0);
993 pd_busy = 0;
994 do_pd_request(NULL);
995 spin_unlock_irqrestore(&io_request_lock,saved_flags);
996 return;
998 pi_read_block(PI,pd_buf,512);
999 pd_count--; pd_run--;
1000 pd_buf += 512;
1001 pd_block++;
1002 if (!pd_run) break;
1003 if (!pd_count) pd_next_buf(unit);
1005 pi_disconnect(PI);
1006 spin_lock_irqsave(&io_request_lock,saved_flags);
1007 end_request(1);
1008 pd_busy = 0;
1009 do_pd_request(NULL);
1010 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1013 static void do_pd_write( void )
1015 { ps_set_intr(do_pd_write_start,0,0,nice);
1018 static void do_pd_write_start( void )
1020 { int unit = pd_unit;
1021 long saved_flags;
1023 pd_busy = 1;
1025 pi_connect(PI);
1026 if (pd_wait_for(unit,STAT_READY,"do_pd_write") & 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;
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;
1040 pd_ide_command(unit,IDE_WRITE,pd_block,pd_run);
1041 while (1) {
1042 if (pd_wait_for(unit,STAT_DRQ,"do_pd_write_drq") & STAT_ERR) {
1043 pi_disconnect(PI);
1044 if (pd_retries < PD_MAX_RETRIES) {
1045 pd_retries++;
1046 pi_do_claimed(PI,do_pd_write_start);
1047 return;
1049 spin_lock_irqsave(&io_request_lock,saved_flags);
1050 end_request(0);
1051 pd_busy = 0;
1052 do_pd_request(NULL);
1053 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1054 return;
1056 pi_write_block(PI,pd_buf,512);
1057 pd_count--; pd_run--;
1058 pd_buf += 512;
1059 pd_block++;
1060 if (!pd_run) break;
1061 if (!pd_count) pd_next_buf(unit);
1063 ps_set_intr(do_pd_write_done,pd_ready,PD_TMO,nice);
1066 static void do_pd_write_done( void )
1068 { int unit = pd_unit;
1069 long saved_flags;
1071 if (pd_wait_for(unit,STAT_READY,"do_pd_write_done") & STAT_ERR) {
1072 pi_disconnect(PI);
1073 if (pd_retries < PD_MAX_RETRIES) {
1074 pd_retries++;
1075 pi_do_claimed(PI,do_pd_write_start);
1076 return;
1078 spin_lock_irqsave(&io_request_lock,saved_flags);
1079 end_request(0);
1080 pd_busy = 0;
1081 do_pd_request(NULL);
1082 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1083 return;
1085 pi_disconnect(PI);
1086 spin_lock_irqsave(&io_request_lock,saved_flags);
1087 end_request(1);
1088 pd_busy = 0;
1089 do_pd_request(NULL);
1090 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1093 /* end of pd.c */