Import 2.1.118
[davej-history.git] / drivers / block / paride / pd.c
blob69274fe4873d366f56ccc7a754c5e49a663f25cc
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
116 #define PD_VERSION "1.04"
117 #define PD_MAJOR 45
118 #define PD_NAME "pd"
119 #define PD_UNITS 4
121 /* Here are things one can override from the insmod command.
122 Most are autoprobed by paride unless set here. Verbose is off
123 by default.
127 static int verbose = 0;
128 static int major = PD_MAJOR;
129 static char *name = PD_NAME;
130 static int cluster = 64;
131 static int nice = 0;
132 static int disable = 0;
134 static int drive0[8] = {0,0,0,-1,0,1,-1,-1};
135 static int drive1[8] = {0,0,0,-1,0,1,-1,-1};
136 static int drive2[8] = {0,0,0,-1,0,1,-1,-1};
137 static int drive3[8] = {0,0,0,-1,0,1,-1,-1};
139 static int (*drives[4])[8] = {&drive0,&drive1,&drive2,&drive3};
140 static int pd_drive_count;
142 #define D_PRT 0
143 #define D_PRO 1
144 #define D_UNI 2
145 #define D_MOD 3
146 #define D_GEO 4
147 #define D_SBY 5
148 #define D_DLY 6
149 #define D_SLV 7
151 #define DU (*drives[unit])
153 /* end of parameters */
155 #include <linux/module.h>
156 #include <linux/errno.h>
157 #include <linux/fs.h>
158 #include <linux/kernel.h>
159 #include <linux/delay.h>
160 #include <linux/genhd.h>
161 #include <linux/hdreg.h>
162 #include <linux/cdrom.h> /* for the eject ioctl */
164 #include <asm/spinlock.h>
165 #include <asm/uaccess.h>
167 #ifndef MODULE
169 #include "setup.h"
171 static STT pd_stt[7] = {{"drive0",8,drive0},
172 {"drive1",8,drive1},
173 {"drive2",8,drive2},
174 {"drive3",8,drive3},
175 {"disable",1,&disable},
176 {"cluster",1,&cluster},
177 {"nice",1,&nice}};
179 void pd_setup( char *str, int *ints)
181 { generic_setup(pd_stt,7,str);
184 #endif
186 MODULE_PARM(verbose,"i");
187 MODULE_PARM(major,"i");
188 MODULE_PARM(name,"s");
189 MODULE_PARM(cluster,"i");
190 MODULE_PARM(nice,"i");
191 MODULE_PARM(drive0,"1-8i");
192 MODULE_PARM(drive1,"1-8i");
193 MODULE_PARM(drive2,"1-8i");
194 MODULE_PARM(drive3,"1-8i");
196 #include "paride.h"
198 #define PD_BITS 4
200 /* set up defines for blk.h, why don't all drivers do it this way ? */
202 #define MAJOR_NR major
203 #define DEVICE_NAME "PD"
204 #define DEVICE_REQUEST do_pd_request
205 #define DEVICE_NR(device) (MINOR(device)>>PD_BITS)
206 #define DEVICE_ON(device)
207 #define DEVICE_OFF(device)
209 #include <linux/blk.h>
211 #include "pseudo.h"
213 #define PD_PARTNS (1<<PD_BITS)
214 #define PD_DEVS PD_PARTNS*PD_UNITS
216 /* numbers for "SCSI" geometry */
218 #define PD_LOG_HEADS 64
219 #define PD_LOG_SECTS 32
221 #define PD_ID_OFF 54
222 #define PD_ID_LEN 14
224 #define PD_MAX_RETRIES 5
225 #define PD_TMO 800 /* interrupt timeout in jiffies */
226 #define PD_SPIN_DEL 50 /* spin delay in micro-seconds */
228 #define PD_SPIN (1000000*PD_TMO)/(HZ*PD_SPIN_DEL)
230 #define STAT_ERR 0x00001
231 #define STAT_INDEX 0x00002
232 #define STAT_ECC 0x00004
233 #define STAT_DRQ 0x00008
234 #define STAT_SEEK 0x00010
235 #define STAT_WRERR 0x00020
236 #define STAT_READY 0x00040
237 #define STAT_BUSY 0x00080
239 #define ERR_AMNF 0x00100
240 #define ERR_TK0NF 0x00200
241 #define ERR_ABRT 0x00400
242 #define ERR_MCR 0x00800
243 #define ERR_IDNF 0x01000
244 #define ERR_MC 0x02000
245 #define ERR_UNC 0x04000
246 #define ERR_TMO 0x10000
248 #define IDE_READ 0x20
249 #define IDE_WRITE 0x30
250 #define IDE_READ_VRFY 0x40
251 #define IDE_INIT_DEV_PARMS 0x91
252 #define IDE_STANDBY 0x96
253 #define IDE_ACKCHANGE 0xdb
254 #define IDE_DOORLOCK 0xde
255 #define IDE_DOORUNLOCK 0xdf
256 #define IDE_IDENTIFY 0xec
257 #define IDE_EJECT 0xed
259 int pd_init(void);
260 void pd_setup(char * str, int * ints);
261 #ifdef MODULE
262 void cleanup_module( void );
263 #endif
264 static void pd_geninit(struct gendisk *ignored);
265 static int pd_open(struct inode *inode, struct file *file);
266 static void do_pd_request(void);
267 static int pd_ioctl(struct inode *inode,struct file *file,
268 unsigned int cmd, unsigned long arg);
269 static int pd_release (struct inode *inode, struct file *file);
270 static int pd_revalidate(kdev_t dev);
271 static int pd_detect(void);
272 static void do_pd_read(void);
273 static void do_pd_read_start(void);
274 static void do_pd_write(void);
275 static void do_pd_write_start(void);
276 static void do_pd_read_drq( void );
277 static void do_pd_write_done( void );
279 static int pd_identify (int unit);
280 static void pd_media_check(int unit);
281 static void pd_doorlock(int unit, int func);
282 static int pd_check_media(kdev_t dev);
283 static void pd_eject( int unit);
285 static struct hd_struct pd_hd[PD_DEVS];
286 static int pd_sizes[PD_DEVS];
287 static int pd_blocksizes[PD_DEVS];
289 #define PD_NAMELEN 8
291 struct pd_unit {
292 struct pi_adapter pia; /* interface to paride layer */
293 struct pi_adapter *pi;
294 int access; /* count of active opens ... */
295 int capacity; /* Size of this volume in sectors */
296 int heads; /* physical geometry */
297 int sectors;
298 int cylinders;
299 int drive; /* master=0 slave=1 */
300 int changed; /* Have we seen a disk change ? */
301 int removable; /* removable media device ? */
302 int standby;
303 int alt_geom;
304 int present;
305 char name[PD_NAMELEN]; /* pda, pdb, etc ... */
308 struct pd_unit pd[PD_UNITS];
310 /* 'unit' must be defined in all functions - either as a local or a param */
312 #define PD pd[unit]
313 #define PI PD.pi
315 static int pd_valid = 1; /* serialise partition checks */
316 static char pd_scratch[512]; /* scratch block buffer */
318 /* the variables below are used mainly in the I/O request engine, which
319 processes only one request at a time.
322 static int pd_retries = 0; /* i/o error retry count */
323 static int pd_busy = 0; /* request being processed ? */
324 static int pd_block; /* address of next requested block */
325 static int pd_count; /* number of blocks still to do */
326 static int pd_run; /* sectors in current cluster */
327 static int pd_cmd; /* current command READ/WRITE */
328 static int pd_unit; /* unit of current request */
329 static int pd_dev; /* minor of current request */
330 static int pd_poffs; /* partition offset of current minor */
331 static char * pd_buf; /* buffer for request in progress */
333 static struct wait_queue *pd_wait_open = NULL;
335 static char *pd_errs[17] = { "ERR","INDEX","ECC","DRQ","SEEK","WRERR",
336 "READY","BUSY","AMNF","TK0NF","ABRT","MCR",
337 "IDNF","MC","UNC","???","TMO"};
339 /* kernel glue structures */
341 static struct gendisk pd_gendisk = {
342 PD_MAJOR, /* Major number */
343 PD_NAME, /* Major name */
344 PD_BITS, /* Bits to shift to get real from partition */
345 PD_PARTNS, /* Number of partitions per real */
346 PD_UNITS, /* maximum number of real */
347 pd_geninit, /* init function */
348 pd_hd, /* hd struct */
349 pd_sizes, /* block sizes */
350 0, /* number */
351 NULL, /* internal */
352 NULL /* next */
355 static struct file_operations pd_fops = {
356 NULL, /* lseek - default */
357 block_read, /* read - general block-dev read */
358 block_write, /* write - general block-dev write */
359 NULL, /* readdir - bad */
360 NULL, /* select */
361 pd_ioctl, /* ioctl */
362 NULL, /* mmap */
363 pd_open, /* open */
364 NULL, /* flush */
365 pd_release, /* release */
366 block_fsync, /* fsync */
367 NULL, /* fasync */
368 pd_check_media, /* media change ? */
369 pd_revalidate /* revalidate new media */
372 void pd_init_units( void )
374 { int unit, j;
376 pd_drive_count = 0;
377 for (unit=0;unit<PD_UNITS;unit++) {
378 PD.pi = & PD.pia;
379 PD.access = 0;
380 PD.changed = 1;
381 PD.capacity = 0;
382 PD.drive = DU[D_SLV];
383 PD.present = 0;
384 j = 0;
385 while ((j < PD_NAMELEN-2) && (PD.name[j]=name[j])) j++;
386 PD.name[j++] = 'a' + unit;
387 PD.name[j] = 0;
388 PD.alt_geom = DU[D_GEO];
389 PD.standby = DU[D_SBY];
390 if (DU[D_PRT]) pd_drive_count++;
394 int pd_init (void)
396 { int i;
398 if (disable) return -1;
400 if (register_blkdev(MAJOR_NR,name,&pd_fops)) {
401 printk("%s: unable to get major number %d\n",
402 name,major);
403 return -1;
405 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
406 read_ahead[MAJOR_NR] = 8; /* 8 sector (4kB) read ahead */
408 pd_gendisk.major = major;
409 pd_gendisk.major_name = name;
410 pd_gendisk.next = gendisk_head;
411 gendisk_head = &pd_gendisk;
413 for(i=0;i<PD_DEVS;i++) pd_blocksizes[i] = 1024;
414 blksize_size[MAJOR_NR] = pd_blocksizes;
416 printk("%s: %s version %s, major %d, cluster %d, nice %d\n",
417 name,name,PD_VERSION,major,cluster,nice);
419 return 0;
422 static void pd_geninit (struct gendisk *ignored)
424 { pd_init_units();
425 pd_gendisk.nr_real = pd_detect();
427 #ifdef MODULE
428 if (!pd_gendisk.nr_real) cleanup_module();
429 #endif
433 static int pd_open (struct inode *inode, struct file *file)
435 { int unit = DEVICE_NR(inode->i_rdev);
437 if ((unit >= PD_UNITS) || (!PD.present)) return -ENODEV;
439 MOD_INC_USE_COUNT;
441 while (!pd_valid) sleep_on(&pd_wait_open);
443 PD.access++;
445 if (PD.removable) {
446 pd_media_check(unit);
447 pd_doorlock(unit,IDE_DOORLOCK);
449 return 0;
452 static int pd_ioctl(struct inode *inode,struct file *file,
453 unsigned int cmd, unsigned long arg)
455 { struct hd_geometry *geo = (struct hd_geometry *) arg;
456 int dev, err, unit;
458 if ((!inode) || (!inode->i_rdev)) return -EINVAL;
459 dev = MINOR(inode->i_rdev);
460 unit = DEVICE_NR(inode->i_rdev);
461 if (dev >= PD_DEVS) return -EINVAL;
462 if (!PD.present) return -ENODEV;
464 switch (cmd) {
465 case CDROMEJECT:
466 if (PD.access == 1) pd_eject(unit);
467 return 0;
468 case HDIO_GETGEO:
469 if (!geo) return -EINVAL;
470 err = verify_area(VERIFY_WRITE,geo,sizeof(*geo));
471 if (err) return err;
473 if (PD.alt_geom) {
474 put_user(PD.capacity/(PD_LOG_HEADS*PD_LOG_SECTS),
475 (short *) &geo->cylinders);
476 put_user(PD_LOG_HEADS, (char *) &geo->heads);
477 put_user(PD_LOG_SECTS, (char *) &geo->sectors);
478 } else {
479 put_user(PD.cylinders, (short *) &geo->cylinders);
480 put_user(PD.heads, (char *) &geo->heads);
481 put_user(PD.sectors, (char *) &geo->sectors);
483 put_user(pd_hd[dev].start_sect,(long *)&geo->start);
484 return 0;
485 case BLKRASET:
486 if(!capable(CAP_SYS_ADMIN)) return -EACCES;
487 if(!(inode->i_rdev)) return -EINVAL;
488 if(arg > 0xff) return -EINVAL;
489 read_ahead[MAJOR(inode->i_rdev)] = arg;
490 return 0;
491 case BLKRAGET:
492 if (!arg) return -EINVAL;
493 err = verify_area(VERIFY_WRITE,(long *) arg,sizeof(long));
494 if (err) return (err);
495 put_user(read_ahead[MAJOR(inode->i_rdev)],(long *) arg);
496 return (0);
497 case BLKGETSIZE:
498 if (!arg) return -EINVAL;
499 err = verify_area(VERIFY_WRITE,(long *) arg,sizeof(long));
500 if (err) return (err);
501 put_user(pd_hd[dev].nr_sects,(long *) arg);
502 return (0);
503 case BLKFLSBUF:
504 if(!capable(CAP_SYS_ADMIN)) return -EACCES;
505 if(!(inode->i_rdev)) return -EINVAL;
506 fsync_dev(inode->i_rdev);
507 invalidate_buffers(inode->i_rdev);
508 return 0;
509 case BLKRRPART:
510 if (!capable(CAP_SYS_ADMIN))
511 return -EACCES;
512 return pd_revalidate(inode->i_rdev);
513 RO_IOCTLS(inode->i_rdev,arg);
514 default:
515 return -EINVAL;
519 static int pd_release (struct inode *inode, struct file *file)
521 { kdev_t devp;
522 int unit;
524 struct super_block *sb;
526 devp = inode->i_rdev;
527 unit = DEVICE_NR(devp);
529 if ((unit >= PD_UNITS) || (PD.access <= 0))
530 return -EINVAL;
532 PD.access--;
534 if (!PD.access) {
535 fsync_dev(devp);
537 sb = get_super(devp);
538 if (sb) invalidate_inodes(sb);
540 invalidate_buffers(devp);
541 if (PD.removable) pd_doorlock(unit,IDE_DOORUNLOCK);
544 MOD_DEC_USE_COUNT;
546 return 0;
549 static int pd_check_media( kdev_t dev)
551 { int r, unit;
553 unit = DEVICE_NR(dev);
554 if ((unit >= PD_UNITS) || (!PD.present)) return -ENODEV;
555 if (!PD.removable) return 0;
556 pd_media_check(unit);
557 r = PD.changed;
558 PD.changed = 0;
559 return r;
562 static int pd_revalidate(kdev_t dev)
564 { int p, unit, minor;
565 long flags;
566 kdev_t devp;
568 struct super_block *sb;
570 unit = DEVICE_NR(dev);
571 if ((unit >= PD_UNITS) || (!PD.present)) return -ENODEV;
573 save_flags(flags);
574 cli();
575 if (PD.access > 1) {
576 restore_flags(flags);
577 return -EBUSY;
579 pd_valid = 0;
580 restore_flags(flags);
582 for (p=(PD_PARTNS-1);p>=0;p--) {
583 minor = p + unit*PD_PARTNS;
584 devp = MKDEV(MAJOR_NR, minor);
585 fsync_dev(devp);
587 sb = get_super(devp);
588 if (sb) invalidate_inodes(sb);
590 invalidate_buffers(devp);
591 pd_hd[minor].start_sect = 0;
592 pd_hd[minor].nr_sects = 0;
595 pd_identify(unit);
596 resetup_one_dev(&pd_gendisk,unit);
598 pd_valid = 1;
599 wake_up(&pd_wait_open);
601 return 0;
604 #ifdef MODULE
606 /* Glue for modules ... */
608 void cleanup_module(void);
610 int init_module(void)
612 { int err, unit;
614 err = pd_init();
615 if (err) return err;
617 pd_geninit(&pd_gendisk);
619 if (!pd_gendisk.nr_real) return -1;
621 pd_valid = 0;
622 for (unit=0;unit<PD_UNITS;unit++)
623 if (PD.present) resetup_one_dev(&pd_gendisk,unit);
624 pd_valid = 1;
626 return 0;
629 void cleanup_module(void)
631 { struct gendisk **gdp;
632 int unit;
634 unregister_blkdev(MAJOR_NR,name);
636 for(gdp=&gendisk_head;*gdp;gdp=&((*gdp)->next))
637 if (*gdp == &pd_gendisk) break;
638 if (*gdp) *gdp = (*gdp)->next;
640 for (unit=0;unit<PD_UNITS;unit++)
641 if (PD.present) pi_release(PI);
644 #endif
646 #define WR(c,r,v) pi_write_regr(PI,c,r,v)
647 #define RR(c,r) (pi_read_regr(PI,c,r))
649 #define DRIVE (0xa0+0x10*PD.drive)
651 /* ide command interface */
653 static void pd_print_error( int unit, char * msg, int status )
655 { int i;
657 printk("%s: %s: status = 0x%x =",PD.name,msg,status);
658 for(i=0;i<18;i++) if (status & (1<<i)) printk(" %s",pd_errs[i]);
659 printk("\n");
662 static void pd_reset( int unit ) /* called only for MASTER drive */
664 { pi_connect(PI);
665 WR(1,6,4);
666 udelay(50);
667 WR(1,6,0);
668 pi_disconnect(PI);
669 udelay(250);
672 #define DBMSG(msg) ((verbose>1)?(msg):NULL)
674 static int pd_wait_for( int unit, int w, char * msg ) /* polled wait */
676 { int k, r, e;
678 k=0;
679 while(k < PD_SPIN) {
680 r = RR(1,6);
681 k++;
682 if (((r & w) == w) && !(r & STAT_BUSY)) break;
683 udelay(PD_SPIN_DEL);
685 e = (RR(0,1)<<8) + RR(0,7);
686 if (k >= PD_SPIN) e |= ERR_TMO;
687 if ((e & (STAT_ERR|ERR_TMO)) && (msg != NULL))
688 pd_print_error(unit,msg,e);
689 return e;
692 static void pd_send_command( int unit, int n, int s, int h,
693 int c0, int c1, int func )
696 WR(0,6,DRIVE+h);
697 WR(0,1,0); /* the IDE task file */
698 WR(0,2,n);
699 WR(0,3,s);
700 WR(0,4,c0);
701 WR(0,5,c1);
702 WR(0,7,func);
704 udelay(1);
707 static void pd_ide_command( int unit, int func, int block, int count )
709 /* Don't use this call if the capacity is zero. */
711 { int c1, c0, h, s;
713 s = ( block % PD.sectors) + 1;
714 h = ( block / PD.sectors) % PD.heads;
715 c0 = ( block / (PD.sectors*PD.heads)) % 256;
716 c1 = ( block / (PD.sectors*PD.heads*256));
718 pd_send_command(unit,count,s,h,c0,c1,func);
721 /* According to the ATA standard, the default CHS geometry should be
722 available following a reset. Some Western Digital drives come up
723 in a mode where only LBA addresses are accepted until the device
724 parameters are initialised.
727 static void pd_init_dev_parms( int unit )
729 { pi_connect(PI);
730 pd_wait_for(unit,0,DBMSG("before init_dev_parms"));
731 pd_send_command(unit,PD.sectors,0,PD.heads-1,0,0,IDE_INIT_DEV_PARMS);
732 udelay(300);
733 pd_wait_for(unit,0,"Initialise device parameters");
734 pi_disconnect(PI);
737 static void pd_doorlock( int unit, int func )
739 { pi_connect(PI);
740 if (pd_wait_for(unit,STAT_READY,"Lock") & STAT_ERR) {
741 pi_disconnect(PI);
742 return;
744 pd_send_command(unit,1,0,0,0,0,func);
745 pd_wait_for(unit,STAT_READY,"Lock done");
746 pi_disconnect(PI);
749 static void pd_eject( int unit )
751 { pi_connect(PI);
752 pd_wait_for(unit,0,DBMSG("before unlock on eject"));
753 pd_send_command(unit,1,0,0,0,0,IDE_DOORUNLOCK);
754 pd_wait_for(unit,0,DBMSG("after unlock on eject"));
755 pd_wait_for(unit,0,DBMSG("before eject"));
756 pd_send_command(unit,0,0,0,0,0,IDE_EJECT);
757 pd_wait_for(unit,0,DBMSG("after eject"));
758 pi_disconnect(PI);
761 static void pd_media_check( int unit )
763 { int r;
765 pi_connect(PI);
766 r = pd_wait_for(unit,STAT_READY,DBMSG("before media_check"));
767 if (!(r & STAT_ERR)) {
768 pd_send_command(unit,1,1,0,0,0,IDE_READ_VRFY);
769 r = pd_wait_for(unit,STAT_READY,DBMSG("RDY after READ_VRFY"));
770 } else PD.changed = 1; /* say changed if other error */
771 if (r & ERR_MC) {
772 PD.changed = 1;
773 pd_send_command(unit,1,0,0,0,0,IDE_ACKCHANGE);
774 pd_wait_for(unit,STAT_READY,DBMSG("RDY after ACKCHANGE"));
775 pd_send_command(unit,1,1,0,0,0,IDE_READ_VRFY);
776 r = pd_wait_for(unit,STAT_READY,DBMSG("RDY after VRFY"));
778 pi_disconnect(PI);
782 static void pd_standby_off( int unit )
784 { pi_connect(PI);
785 pd_wait_for(unit,0,DBMSG("before STANDBY"));
786 pd_send_command(unit,0,0,0,0,0,IDE_STANDBY);
787 pd_wait_for(unit,0,DBMSG("after STANDBY"));
788 pi_disconnect(PI);
791 #define word_val(n) ((pd_scratch[2*n]&0xff)+256*(pd_scratch[2*n+1]&0xff))
793 static int pd_identify( int unit )
795 { int j;
796 char id[PD_ID_LEN+1];
798 /* WARNING: here there may be dragons. reset() applies to both drives,
799 but we call it only on probing the MASTER. This should allow most
800 common configurations to work, but be warned that a reset can clear
801 settings on the SLAVE drive.
804 if (PD.drive == 0) pd_reset(unit);
806 pi_connect(PI);
807 WR(0,6,DRIVE);
808 pd_wait_for(unit,0,DBMSG("before IDENT"));
809 pd_send_command(unit,1,0,0,0,0,IDE_IDENTIFY);
811 if (pd_wait_for(unit,STAT_DRQ,DBMSG("IDENT DRQ")) & STAT_ERR) {
812 pi_disconnect(PI);
813 return 0;
815 pi_read_block(PI,pd_scratch,512);
816 pi_disconnect(PI);
817 PD.sectors = word_val(6);
818 PD.heads = word_val(3);
819 PD.cylinders = word_val(1);
820 PD.capacity = PD.sectors*PD.heads*PD.cylinders;
822 for(j=0;j<PD_ID_LEN;j++) id[j^1] = pd_scratch[j+PD_ID_OFF];
823 j = PD_ID_LEN-1;
824 while ((j >= 0) && (id[j] <= 0x20)) j--;
825 j++; id[j] = 0;
827 PD.removable = (word_val(0) & 0x80);
829 printk("%s: %s, %s, %d blocks [%dM], (%d/%d/%d), %s media\n",
830 PD.name,id,
831 PD.drive?"slave":"master",
832 PD.capacity,PD.capacity/2048,
833 PD.cylinders,PD.heads,PD.sectors,
834 PD.removable?"removable":"fixed");
836 if (PD.capacity) pd_init_dev_parms(unit);
837 if (!PD.standby) pd_standby_off(unit);
839 pd_hd[unit<<PD_BITS].nr_sects = PD.capacity;
840 pd_hd[unit<<PD_BITS].start_sect = 0;
842 return 1;
845 static int pd_probe_drive( int unit )
847 { if (PD.drive == -1) {
848 for (PD.drive=0;PD.drive<=1;PD.drive++)
849 if (pd_identify(unit)) return 1;
850 return 0;
852 else return pd_identify(unit);
855 static int pd_detect( void )
857 { int k, unit;
859 k = 0;
860 if (pd_drive_count == 0) { /* nothing spec'd - so autoprobe for 1 */
861 unit = 0;
862 if (pi_init(PI,1,-1,-1,-1,-1,-1,pd_scratch,
863 PI_PD,verbose,PD.name)) {
864 if (pd_probe_drive(unit)) {
865 PD.present = 1;
866 k = 1;
867 } else pi_release(PI);
870 } else for (unit=0;unit<PD_UNITS;unit++) if (DU[D_PRT])
871 if (pi_init(PI,0,DU[D_PRT],DU[D_MOD],DU[D_UNI],
872 DU[D_PRO],DU[D_DLY],pd_scratch,
873 PI_PD,verbose,PD.name)) {
874 if (pd_probe_drive(unit)) {
875 PD.present = 1;
876 k = unit+1;
877 } else pi_release(PI);
880 /* We lie about the number of drives found, as the generic partition
881 scanner assumes that the drives are numbered sequentially from 0.
882 This can result in some bogus error messages if non-sequential
883 drive numbers are used.
886 if (k) return k;
888 printk("%s: no valid drive found\n",name);
889 return 0;
892 /* The i/o request engine */
894 static int pd_ready( void )
896 { int unit = pd_unit;
898 return (!(RR(1,6) & STAT_BUSY)) ;
901 static void do_pd_request (void)
903 { struct buffer_head * bh;
904 struct request * req;
905 int unit;
907 if (pd_busy) return;
908 repeat:
909 if ((!CURRENT) || (CURRENT->rq_status == RQ_INACTIVE)) return;
910 INIT_REQUEST;
912 pd_dev = MINOR(CURRENT->rq_dev);
913 pd_unit = unit = DEVICE_NR(CURRENT->rq_dev);
914 pd_block = CURRENT->sector;
915 pd_count = CURRENT->nr_sectors;
917 bh = CURRENT->bh;
918 req = CURRENT;
919 if (bh->b_reqnext)
920 printk("%s: OUCH: b_reqnext != NULL\n",PD.name);
922 if ((pd_dev >= PD_DEVS) ||
923 ((pd_block+pd_count) > pd_hd[pd_dev].nr_sects)) {
924 end_request(0);
925 goto repeat;
928 pd_cmd = CURRENT->cmd;
929 pd_run = pd_count;
930 while ((pd_run <= cluster) &&
931 (req = req->next) &&
932 (pd_block+pd_run == req->sector) &&
933 (pd_cmd == req->cmd) &&
934 (pd_dev == MINOR(req->rq_dev)))
935 pd_run += req->nr_sectors;
937 pd_poffs = pd_hd[pd_dev].start_sect;
938 pd_block += pd_poffs;
939 pd_buf = CURRENT->buffer;
940 pd_retries = 0;
942 pd_busy = 1;
943 if (pd_cmd == READ) pi_do_claimed(PI,do_pd_read);
944 else if (pd_cmd == WRITE) pi_do_claimed(PI,do_pd_write);
945 else { pd_busy = 0;
946 end_request(0);
947 goto repeat;
951 static void pd_next_buf( int unit )
953 { long saved_flags;
955 spin_lock_irqsave(&io_request_lock,saved_flags);
956 end_request(1);
957 if (!pd_run) { spin_unlock_irqrestore(&io_request_lock,saved_flags);
958 return;
961 /* paranoia */
963 if ((!CURRENT) ||
964 (CURRENT->cmd != pd_cmd) ||
965 (MINOR(CURRENT->rq_dev) != pd_dev) ||
966 (CURRENT->rq_status == RQ_INACTIVE) ||
967 (CURRENT->sector+pd_poffs != pd_block))
968 printk("%s: OUCH: request list changed unexpectedly\n",
969 PD.name);
971 pd_count = CURRENT->nr_sectors;
972 pd_buf = CURRENT->buffer;
973 spin_unlock_irqrestore(&io_request_lock,saved_flags);
976 static void do_pd_read( void )
978 { ps_set_intr(do_pd_read_start,0,0,nice);
981 static void do_pd_read_start( void )
983 { int unit = pd_unit;
984 long saved_flags;
986 pd_busy = 1;
988 pi_connect(PI);
989 if (pd_wait_for(unit,STAT_READY,"do_pd_read") & STAT_ERR) {
990 pi_disconnect(PI);
991 if (pd_retries < PD_MAX_RETRIES) {
992 pd_retries++;
993 pi_do_claimed(PI,do_pd_read_start);
994 return;
996 spin_lock_irqsave(&io_request_lock,saved_flags);
997 end_request(0);
998 pd_busy = 0;
999 do_pd_request();
1000 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1001 return;
1003 pd_ide_command(unit,IDE_READ,pd_block,pd_run);
1004 ps_set_intr(do_pd_read_drq,pd_ready,PD_TMO,nice);
1007 static void do_pd_read_drq( void )
1009 { int unit = pd_unit;
1010 long saved_flags;
1012 while (1) {
1013 if (pd_wait_for(unit,STAT_DRQ,"do_pd_read_drq") & STAT_ERR) {
1014 pi_disconnect(PI);
1015 if (pd_retries < PD_MAX_RETRIES) {
1016 pd_retries++;
1017 pi_do_claimed(PI,do_pd_read_start);
1018 return;
1020 spin_lock_irqsave(&io_request_lock,saved_flags);
1021 end_request(0);
1022 pd_busy = 0;
1023 do_pd_request();
1024 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1025 return;
1027 pi_read_block(PI,pd_buf,512);
1028 pd_count--; pd_run--;
1029 pd_buf += 512;
1030 pd_block++;
1031 if (!pd_run) break;
1032 if (!pd_count) pd_next_buf(unit);
1034 pi_disconnect(PI);
1035 spin_lock_irqsave(&io_request_lock,saved_flags);
1036 end_request(1);
1037 pd_busy = 0;
1038 do_pd_request();
1039 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1042 static void do_pd_write( void )
1044 { ps_set_intr(do_pd_write_start,0,0,nice);
1047 static void do_pd_write_start( void )
1049 { int unit = pd_unit;
1050 long saved_flags;
1052 pd_busy = 1;
1054 pi_connect(PI);
1055 if (pd_wait_for(unit,STAT_READY,"do_pd_write") & STAT_ERR) {
1056 pi_disconnect(PI);
1057 if (pd_retries < PD_MAX_RETRIES) {
1058 pd_retries++;
1059 pi_do_claimed(PI,do_pd_write_start);
1060 return;
1062 spin_lock_irqsave(&io_request_lock,saved_flags);
1063 end_request(0);
1064 pd_busy = 0;
1065 do_pd_request();
1066 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1067 return;
1069 pd_ide_command(unit,IDE_WRITE,pd_block,pd_run);
1070 while (1) {
1071 if (pd_wait_for(unit,STAT_DRQ,"do_pd_write_drq") & 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();
1082 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1083 return;
1085 pi_write_block(PI,pd_buf,512);
1086 pd_count--; pd_run--;
1087 pd_buf += 512;
1088 pd_block++;
1089 if (!pd_run) break;
1090 if (!pd_count) pd_next_buf(unit);
1092 ps_set_intr(do_pd_write_done,pd_ready,PD_TMO,nice);
1095 static void do_pd_write_done( void )
1097 { int unit = pd_unit;
1098 long saved_flags;
1100 if (pd_wait_for(unit,STAT_READY,"do_pd_write_done") & STAT_ERR) {
1101 pi_disconnect(PI);
1102 if (pd_retries < PD_MAX_RETRIES) {
1103 pd_retries++;
1104 pi_do_claimed(PI,do_pd_write_start);
1105 return;
1107 spin_lock_irqsave(&io_request_lock,saved_flags);
1108 end_request(0);
1109 pd_busy = 0;
1110 do_pd_request();
1111 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1112 return;
1114 pi_disconnect(PI);
1115 spin_lock_irqsave(&io_request_lock,saved_flags);
1116 end_request(1);
1117 pd_busy = 0;
1118 do_pd_request();
1119 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1122 /* end of pd.c */