Ok. I didn't make 2.4.0 in 2000. Tough. I tried, but we had some
[davej-history.git] / drivers / block / paride / pd.c
blob9b1ff3a32fe6d66704d3b49e4bd37e4b6e371d01
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/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>
167 #include <asm/uaccess.h>
169 #ifndef MODULE
171 #include "setup.h"
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}};
181 void pd_setup( char *str, int *ints)
183 { generic_setup(pd_stt,7,str);
186 #endif
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");
198 #include "paride.h"
200 #define PD_BITS 4
202 /* set up defines for blk.h, why don't all drivers do it this way ? */
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)
211 #include <linux/blk.h>
212 #include <linux/blkpg.h>
214 #include "pseudo.h"
216 #define PD_PARTNS (1<<PD_BITS)
217 #define PD_DEVS PD_PARTNS*PD_UNITS
219 /* numbers for "SCSI" geometry */
221 #define PD_LOG_HEADS 64
222 #define PD_LOG_SECTS 32
224 #define PD_ID_OFF 54
225 #define PD_ID_LEN 14
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 */
231 #define PD_SPIN (1000000*PD_TMO)/(HZ*PD_SPIN_DEL)
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
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
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
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 );
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 extern struct block_device_operations pd_fops;
345 static struct gendisk pd_gendisk = {
346 PD_MAJOR, /* Major number */
347 PD_NAME, /* Major name */
348 PD_BITS, /* Bits to shift to get real from partition */
349 PD_PARTNS, /* Number of partitions per real */
350 pd_hd, /* hd struct */
351 pd_sizes, /* block sizes */
352 0, /* number */
353 NULL, /* internal */
354 NULL, /* next */
355 &pd_fops, /* block device operations */
358 static struct block_device_operations pd_fops = {
359 open: pd_open,
360 release: pd_release,
361 ioctl: pd_ioctl,
362 check_media_change: pd_check_media,
363 revalidate: pd_revalidate
366 void pd_init_units( void )
368 { int unit, j;
370 pd_drive_count = 0;
371 for (unit=0;unit<PD_UNITS;unit++) {
372 PD.pi = & PD.pia;
373 PD.access = 0;
374 PD.changed = 1;
375 PD.capacity = 0;
376 PD.drive = DU[D_SLV];
377 PD.present = 0;
378 j = 0;
379 while ((j < PD_NAMELEN-2) && (PD.name[j]=name[j])) j++;
380 PD.name[j++] = 'a' + unit;
381 PD.name[j] = 0;
382 PD.alt_geom = DU[D_GEO];
383 PD.standby = DU[D_SBY];
384 if (DU[D_PRT]) pd_drive_count++;
388 static inline int pd_new_segment(request_queue_t *q, struct request *req, int max_segments)
390 if (max_segments > cluster)
391 max_segments = cluster;
393 if (req->nr_segments < max_segments) {
394 req->nr_segments++;
395 q->elevator.nr_segments++;
396 return 1;
398 return 0;
401 static int pd_back_merge_fn(request_queue_t *q, struct request *req,
402 struct buffer_head *bh, int max_segments)
404 if (req->bhtail->b_data + req->bhtail->b_size == bh->b_data)
405 return 1;
406 return pd_new_segment(q, req, max_segments);
409 static int pd_front_merge_fn(request_queue_t *q, struct request *req,
410 struct buffer_head *bh, int max_segments)
412 if (bh->b_data + bh->b_size == req->bh->b_data)
413 return 1;
414 return pd_new_segment(q, req, max_segments);
417 static int pd_merge_requests_fn(request_queue_t *q, struct request *req,
418 struct request *next, int max_segments)
420 int total_segments = req->nr_segments + next->nr_segments;
421 int same_segment;
423 if (max_segments > cluster)
424 max_segments = cluster;
426 same_segment = 0;
427 if (req->bhtail->b_data + req->bhtail->b_size == next->bh->b_data) {
428 total_segments--;
429 same_segment = 1;
432 if (total_segments > max_segments)
433 return 0;
435 q->elevator.nr_segments -= same_segment;
436 req->nr_segments = total_segments;
437 return 1;
440 int pd_init (void)
442 { int i;
443 request_queue_t * q;
445 if (disable) return -1;
446 if (devfs_register_blkdev(MAJOR_NR,name,&pd_fops)) {
447 printk("%s: unable to get major number %d\n",
448 name,major);
449 return -1;
451 q = BLK_DEFAULT_QUEUE(MAJOR_NR);
452 blk_init_queue(q, DEVICE_REQUEST);
453 q->back_merge_fn = pd_back_merge_fn;
454 q->front_merge_fn = pd_front_merge_fn;
455 q->merge_requests_fn = pd_merge_requests_fn;
456 read_ahead[MAJOR_NR] = 8; /* 8 sector (4kB) read ahead */
458 pd_gendisk.major = major;
459 pd_gendisk.major_name = name;
460 pd_gendisk.next = gendisk_head;
461 gendisk_head = &pd_gendisk;
463 for(i=0;i<PD_DEVS;i++) pd_blocksizes[i] = 1024;
464 blksize_size[MAJOR_NR] = pd_blocksizes;
466 printk("%s: %s version %s, major %d, cluster %d, nice %d\n",
467 name,name,PD_VERSION,major,cluster,nice);
468 pd_init_units();
469 pd_valid = 0;
470 pd_gendisk.nr_real = pd_detect();
471 pd_valid = 1;
473 #ifdef MODULE
474 if (!pd_gendisk.nr_real) {
475 cleanup_module();
476 return -1;
478 #endif
479 return 0;
482 static int pd_open (struct inode *inode, struct file *file)
484 { int unit = DEVICE_NR(inode->i_rdev);
486 if ((unit >= PD_UNITS) || (!PD.present)) return -ENODEV;
488 MOD_INC_USE_COUNT;
490 wait_event (pd_wait_open, pd_valid);
492 PD.access++;
494 if (PD.removable) {
495 pd_media_check(unit);
496 pd_doorlock(unit,IDE_DOORLOCK);
498 return 0;
501 static int pd_ioctl(struct inode *inode,struct file *file,
502 unsigned int cmd, unsigned long arg)
504 { struct hd_geometry *geo = (struct hd_geometry *) arg;
505 int dev, err, unit;
507 if ((!inode) || (!inode->i_rdev)) return -EINVAL;
508 dev = MINOR(inode->i_rdev);
509 unit = DEVICE_NR(inode->i_rdev);
510 if (dev >= PD_DEVS) return -EINVAL;
511 if (!PD.present) return -ENODEV;
513 switch (cmd) {
514 case CDROMEJECT:
515 if (PD.access == 1) pd_eject(unit);
516 return 0;
517 case HDIO_GETGEO:
518 if (!geo) return -EINVAL;
519 err = verify_area(VERIFY_WRITE,geo,sizeof(*geo));
520 if (err) return err;
522 if (PD.alt_geom) {
523 put_user(PD.capacity/(PD_LOG_HEADS*PD_LOG_SECTS),
524 (short *) &geo->cylinders);
525 put_user(PD_LOG_HEADS, (char *) &geo->heads);
526 put_user(PD_LOG_SECTS, (char *) &geo->sectors);
527 } else {
528 put_user(PD.cylinders, (short *) &geo->cylinders);
529 put_user(PD.heads, (char *) &geo->heads);
530 put_user(PD.sectors, (char *) &geo->sectors);
532 put_user(pd_hd[dev].start_sect,(long *)&geo->start);
533 return 0;
534 case BLKGETSIZE:
535 if (!arg) return -EINVAL;
536 err = verify_area(VERIFY_WRITE,(long *) arg,sizeof(long));
537 if (err) return (err);
538 put_user(pd_hd[dev].nr_sects,(long *) arg);
539 return (0);
540 case BLKRRPART:
541 if (!capable(CAP_SYS_ADMIN))
542 return -EACCES;
543 return pd_revalidate(inode->i_rdev);
544 case BLKROSET:
545 case BLKROGET:
546 case BLKRASET:
547 case BLKRAGET:
548 case BLKFLSBUF:
549 case BLKPG:
550 return blk_ioctl(inode->i_rdev, cmd, arg);
551 default:
552 return -EINVAL;
556 static int pd_release (struct inode *inode, struct file *file)
558 { kdev_t devp;
559 int unit;
561 devp = inode->i_rdev;
562 unit = DEVICE_NR(devp);
564 if ((unit >= PD_UNITS) || (PD.access <= 0))
565 return -EINVAL;
567 PD.access--;
569 if (!PD.access && PD.removable)
570 pd_doorlock(unit,IDE_DOORUNLOCK);
572 MOD_DEC_USE_COUNT;
574 return 0;
577 static int pd_check_media( kdev_t dev)
579 { int r, unit;
581 unit = DEVICE_NR(dev);
582 if ((unit >= PD_UNITS) || (!PD.present)) return -ENODEV;
583 if (!PD.removable) return 0;
584 pd_media_check(unit);
585 r = PD.changed;
586 PD.changed = 0;
587 return r;
590 static int pd_revalidate(kdev_t dev)
592 { int p, unit, minor;
593 long flags;
594 kdev_t devp;
596 struct super_block *sb;
598 unit = DEVICE_NR(dev);
599 if ((unit >= PD_UNITS) || (!PD.present)) return -ENODEV;
601 save_flags(flags);
602 cli();
603 if (PD.access > 1) {
604 restore_flags(flags);
605 return -EBUSY;
607 pd_valid = 0;
608 restore_flags(flags);
610 for (p=(PD_PARTNS-1);p>=0;p--) {
611 minor = p + unit*PD_PARTNS;
612 devp = MKDEV(MAJOR_NR, minor);
613 fsync_dev(devp);
615 sb = get_super(devp);
616 if (sb) invalidate_inodes(sb);
618 invalidate_buffers(devp);
619 pd_hd[minor].start_sect = 0;
620 pd_hd[minor].nr_sects = 0;
623 if (pd_identify(unit))
624 grok_partitions(&pd_gendisk,unit,1<<PD_BITS,PD.capacity);
626 pd_valid = 1;
627 wake_up(&pd_wait_open);
629 return 0;
632 #ifdef MODULE
634 /* Glue for modules ... */
636 void cleanup_module(void);
638 int init_module(void)
642 #ifdef PARIDE_JUMBO
643 { extern paride_init();
644 paride_init();
646 #endif
647 return pd_init();
650 void cleanup_module(void)
652 { struct gendisk **gdp;
653 int unit;
655 devfs_unregister_blkdev(MAJOR_NR,name);
656 for(gdp=&gendisk_head;*gdp;gdp=&((*gdp)->next))
657 if (*gdp == &pd_gendisk) break;
658 if (*gdp) *gdp = (*gdp)->next;
660 for (unit=0;unit<PD_UNITS;unit++)
661 if (PD.present) pi_release(PI);
664 #endif
666 #define WR(c,r,v) pi_write_regr(PI,c,r,v)
667 #define RR(c,r) (pi_read_regr(PI,c,r))
669 #define DRIVE (0xa0+0x10*PD.drive)
671 /* ide command interface */
673 static void pd_print_error( int unit, char * msg, int status )
675 { int i;
677 printk("%s: %s: status = 0x%x =",PD.name,msg,status);
678 for(i=0;i<18;i++) if (status & (1<<i)) printk(" %s",pd_errs[i]);
679 printk("\n");
682 static void pd_reset( int unit ) /* called only for MASTER drive */
684 { pi_connect(PI);
685 WR(1,6,4);
686 udelay(50);
687 WR(1,6,0);
688 pi_disconnect(PI);
689 udelay(250);
692 #define DBMSG(msg) ((verbose>1)?(msg):NULL)
694 static int pd_wait_for( int unit, int w, char * msg ) /* polled wait */
696 { int k, r, e;
698 k=0;
699 while(k < PD_SPIN) {
700 r = RR(1,6);
701 k++;
702 if (((r & w) == w) && !(r & STAT_BUSY)) break;
703 udelay(PD_SPIN_DEL);
705 e = (RR(0,1)<<8) + RR(0,7);
706 if (k >= PD_SPIN) e |= ERR_TMO;
707 if ((e & (STAT_ERR|ERR_TMO)) && (msg != NULL))
708 pd_print_error(unit,msg,e);
709 return e;
712 static void pd_send_command( int unit, int n, int s, int h,
713 int c0, int c1, int func )
716 WR(0,6,DRIVE+h);
717 WR(0,1,0); /* the IDE task file */
718 WR(0,2,n);
719 WR(0,3,s);
720 WR(0,4,c0);
721 WR(0,5,c1);
722 WR(0,7,func);
724 udelay(1);
727 static void pd_ide_command( int unit, int func, int block, int count )
729 /* Don't use this call if the capacity is zero. */
731 { int c1, c0, h, s;
733 s = ( block % PD.sectors) + 1;
734 h = ( block / PD.sectors) % PD.heads;
735 c0 = ( block / (PD.sectors*PD.heads)) % 256;
736 c1 = ( block / (PD.sectors*PD.heads*256));
738 pd_send_command(unit,count,s,h,c0,c1,func);
741 /* According to the ATA standard, the default CHS geometry should be
742 available following a reset. Some Western Digital drives come up
743 in a mode where only LBA addresses are accepted until the device
744 parameters are initialised.
747 static void pd_init_dev_parms( int unit )
749 { pi_connect(PI);
750 pd_wait_for(unit,0,DBMSG("before init_dev_parms"));
751 pd_send_command(unit,PD.sectors,0,PD.heads-1,0,0,IDE_INIT_DEV_PARMS);
752 udelay(300);
753 pd_wait_for(unit,0,"Initialise device parameters");
754 pi_disconnect(PI);
757 static void pd_doorlock( int unit, int func )
759 { pi_connect(PI);
760 if (pd_wait_for(unit,STAT_READY,"Lock") & STAT_ERR) {
761 pi_disconnect(PI);
762 return;
764 pd_send_command(unit,1,0,0,0,0,func);
765 pd_wait_for(unit,STAT_READY,"Lock done");
766 pi_disconnect(PI);
769 static void pd_eject( int unit )
771 { pi_connect(PI);
772 pd_wait_for(unit,0,DBMSG("before unlock on eject"));
773 pd_send_command(unit,1,0,0,0,0,IDE_DOORUNLOCK);
774 pd_wait_for(unit,0,DBMSG("after unlock on eject"));
775 pd_wait_for(unit,0,DBMSG("before eject"));
776 pd_send_command(unit,0,0,0,0,0,IDE_EJECT);
777 pd_wait_for(unit,0,DBMSG("after eject"));
778 pi_disconnect(PI);
781 static void pd_media_check( int unit )
783 { int r;
785 pi_connect(PI);
786 r = pd_wait_for(unit,STAT_READY,DBMSG("before media_check"));
787 if (!(r & STAT_ERR)) {
788 pd_send_command(unit,1,1,0,0,0,IDE_READ_VRFY);
789 r = pd_wait_for(unit,STAT_READY,DBMSG("RDY after READ_VRFY"));
790 } else PD.changed = 1; /* say changed if other error */
791 if (r & ERR_MC) {
792 PD.changed = 1;
793 pd_send_command(unit,1,0,0,0,0,IDE_ACKCHANGE);
794 pd_wait_for(unit,STAT_READY,DBMSG("RDY after ACKCHANGE"));
795 pd_send_command(unit,1,1,0,0,0,IDE_READ_VRFY);
796 r = pd_wait_for(unit,STAT_READY,DBMSG("RDY after VRFY"));
798 pi_disconnect(PI);
802 static void pd_standby_off( int unit )
804 { pi_connect(PI);
805 pd_wait_for(unit,0,DBMSG("before STANDBY"));
806 pd_send_command(unit,0,0,0,0,0,IDE_STANDBY);
807 pd_wait_for(unit,0,DBMSG("after STANDBY"));
808 pi_disconnect(PI);
811 #define word_val(n) ((pd_scratch[2*n]&0xff)+256*(pd_scratch[2*n+1]&0xff))
813 static int pd_identify( int unit )
815 { int j;
816 char id[PD_ID_LEN+1];
818 /* WARNING: here there may be dragons. reset() applies to both drives,
819 but we call it only on probing the MASTER. This should allow most
820 common configurations to work, but be warned that a reset can clear
821 settings on the SLAVE drive.
824 if (PD.drive == 0) pd_reset(unit);
826 pi_connect(PI);
827 WR(0,6,DRIVE);
828 pd_wait_for(unit,0,DBMSG("before IDENT"));
829 pd_send_command(unit,1,0,0,0,0,IDE_IDENTIFY);
831 if (pd_wait_for(unit,STAT_DRQ,DBMSG("IDENT DRQ")) & STAT_ERR) {
832 pi_disconnect(PI);
833 return 0;
835 pi_read_block(PI,pd_scratch,512);
836 pi_disconnect(PI);
837 PD.sectors = word_val(6);
838 PD.heads = word_val(3);
839 PD.cylinders = word_val(1);
840 PD.capacity = PD.sectors*PD.heads*PD.cylinders;
842 for(j=0;j<PD_ID_LEN;j++) id[j^1] = pd_scratch[j+PD_ID_OFF];
843 j = PD_ID_LEN-1;
844 while ((j >= 0) && (id[j] <= 0x20)) j--;
845 j++; id[j] = 0;
847 PD.removable = (word_val(0) & 0x80);
849 printk("%s: %s, %s, %d blocks [%dM], (%d/%d/%d), %s media\n",
850 PD.name,id,
851 PD.drive?"slave":"master",
852 PD.capacity,PD.capacity/2048,
853 PD.cylinders,PD.heads,PD.sectors,
854 PD.removable?"removable":"fixed");
856 if (PD.capacity) pd_init_dev_parms(unit);
857 if (!PD.standby) pd_standby_off(unit);
859 return 1;
862 static int pd_probe_drive( int unit )
864 if (PD.drive == -1) {
865 for (PD.drive=0;PD.drive<=1;PD.drive++)
866 if (pd_identify(unit))
867 return 1;
868 return 0;
870 return pd_identify(unit);
873 static int pd_detect( void )
875 { int k, unit;
877 k = 0;
878 if (pd_drive_count == 0) { /* nothing spec'd - so autoprobe for 1 */
879 unit = 0;
880 if (pi_init(PI,1,-1,-1,-1,-1,-1,pd_scratch,
881 PI_PD,verbose,PD.name)) {
882 if (pd_probe_drive(unit)) {
883 PD.present = 1;
884 k = 1;
885 } else pi_release(PI);
888 } else for (unit=0;unit<PD_UNITS;unit++) if (DU[D_PRT])
889 if (pi_init(PI,0,DU[D_PRT],DU[D_MOD],DU[D_UNI],
890 DU[D_PRO],DU[D_DLY],pd_scratch,
891 PI_PD,verbose,PD.name)) {
892 if (pd_probe_drive(unit)) {
893 PD.present = 1;
894 k = unit+1;
895 } else pi_release(PI);
897 for (unit=0;unit<PD_UNITS;unit++)
898 register_disk(&pd_gendisk,MKDEV(MAJOR_NR,unit<<PD_BITS),
899 PD_PARTNS,&pd_fops,
900 PD.present?PD.capacity:0);
902 /* We lie about the number of drives found, as the generic partition
903 scanner assumes that the drives are numbered sequentially from 0.
904 This can result in some bogus error messages if non-sequential
905 drive numbers are used.
907 if (k)
908 return k;
909 printk("%s: no valid drive found\n",name);
910 return 0;
913 /* The i/o request engine */
915 static int pd_ready( void )
917 { int unit = pd_unit;
919 return (!(RR(1,6) & STAT_BUSY)) ;
922 static void do_pd_request (request_queue_t * q)
924 { struct buffer_head * bh;
925 int unit;
927 if (pd_busy) return;
928 repeat:
929 if (QUEUE_EMPTY || (CURRENT->rq_status == RQ_INACTIVE)) return;
930 INIT_REQUEST;
932 pd_dev = MINOR(CURRENT->rq_dev);
933 pd_unit = unit = DEVICE_NR(CURRENT->rq_dev);
934 pd_block = CURRENT->sector;
935 pd_run = CURRENT->nr_sectors;
936 pd_count = CURRENT->current_nr_sectors;
938 bh = CURRENT->bh;
940 if ((pd_dev >= PD_DEVS) ||
941 ((pd_block+pd_count) > pd_hd[pd_dev].nr_sects)) {
942 end_request(0);
943 goto repeat;
946 pd_cmd = CURRENT->cmd;
947 pd_poffs = pd_hd[pd_dev].start_sect;
948 pd_block += pd_poffs;
949 pd_buf = CURRENT->buffer;
950 pd_retries = 0;
952 pd_busy = 1;
953 if (pd_cmd == READ) pi_do_claimed(PI,do_pd_read);
954 else if (pd_cmd == WRITE) pi_do_claimed(PI,do_pd_write);
955 else { pd_busy = 0;
956 end_request(0);
957 goto repeat;
961 static void pd_next_buf( int unit )
963 { long saved_flags;
965 spin_lock_irqsave(&io_request_lock,saved_flags);
966 end_request(1);
967 if (!pd_run) { spin_unlock_irqrestore(&io_request_lock,saved_flags);
968 return;
971 /* paranoia */
973 if (QUEUE_EMPTY ||
974 (CURRENT->cmd != pd_cmd) ||
975 (MINOR(CURRENT->rq_dev) != pd_dev) ||
976 (CURRENT->rq_status == RQ_INACTIVE) ||
977 (CURRENT->sector+pd_poffs != pd_block))
978 printk("%s: OUCH: request list changed unexpectedly\n",
979 PD.name);
981 pd_count = CURRENT->current_nr_sectors;
982 pd_buf = CURRENT->buffer;
983 spin_unlock_irqrestore(&io_request_lock,saved_flags);
986 static void do_pd_read( void )
988 { ps_set_intr(do_pd_read_start,0,0,nice);
991 static void do_pd_read_start( void )
993 { int unit = pd_unit;
994 long saved_flags;
996 pd_busy = 1;
998 pi_connect(PI);
999 if (pd_wait_for(unit,STAT_READY,"do_pd_read") & STAT_ERR) {
1000 pi_disconnect(PI);
1001 if (pd_retries < PD_MAX_RETRIES) {
1002 pd_retries++;
1003 pi_do_claimed(PI,do_pd_read_start);
1004 return;
1006 spin_lock_irqsave(&io_request_lock,saved_flags);
1007 end_request(0);
1008 pd_busy = 0;
1009 do_pd_request(NULL);
1010 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1011 return;
1013 pd_ide_command(unit,IDE_READ,pd_block,pd_run);
1014 ps_set_intr(do_pd_read_drq,pd_ready,PD_TMO,nice);
1017 static void do_pd_read_drq( void )
1019 { int unit = pd_unit;
1020 long saved_flags;
1022 while (1) {
1023 if (pd_wait_for(unit,STAT_DRQ,"do_pd_read_drq") & STAT_ERR) {
1024 pi_disconnect(PI);
1025 if (pd_retries < PD_MAX_RETRIES) {
1026 pd_retries++;
1027 pi_do_claimed(PI,do_pd_read_start);
1028 return;
1030 spin_lock_irqsave(&io_request_lock,saved_flags);
1031 end_request(0);
1032 pd_busy = 0;
1033 do_pd_request(NULL);
1034 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1035 return;
1037 pi_read_block(PI,pd_buf,512);
1038 pd_count--; pd_run--;
1039 pd_buf += 512;
1040 pd_block++;
1041 if (!pd_run) break;
1042 if (!pd_count) pd_next_buf(unit);
1044 pi_disconnect(PI);
1045 spin_lock_irqsave(&io_request_lock,saved_flags);
1046 end_request(1);
1047 pd_busy = 0;
1048 do_pd_request(NULL);
1049 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1052 static void do_pd_write( void )
1054 { ps_set_intr(do_pd_write_start,0,0,nice);
1057 static void do_pd_write_start( void )
1059 { int unit = pd_unit;
1060 long saved_flags;
1062 pd_busy = 1;
1064 pi_connect(PI);
1065 if (pd_wait_for(unit,STAT_READY,"do_pd_write") & STAT_ERR) {
1066 pi_disconnect(PI);
1067 if (pd_retries < PD_MAX_RETRIES) {
1068 pd_retries++;
1069 pi_do_claimed(PI,do_pd_write_start);
1070 return;
1072 spin_lock_irqsave(&io_request_lock,saved_flags);
1073 end_request(0);
1074 pd_busy = 0;
1075 do_pd_request(NULL);
1076 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1077 return;
1079 pd_ide_command(unit,IDE_WRITE,pd_block,pd_run);
1080 while (1) {
1081 if (pd_wait_for(unit,STAT_DRQ,"do_pd_write_drq") & STAT_ERR) {
1082 pi_disconnect(PI);
1083 if (pd_retries < PD_MAX_RETRIES) {
1084 pd_retries++;
1085 pi_do_claimed(PI,do_pd_write_start);
1086 return;
1088 spin_lock_irqsave(&io_request_lock,saved_flags);
1089 end_request(0);
1090 pd_busy = 0;
1091 do_pd_request(NULL);
1092 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1093 return;
1095 pi_write_block(PI,pd_buf,512);
1096 pd_count--; pd_run--;
1097 pd_buf += 512;
1098 pd_block++;
1099 if (!pd_run) break;
1100 if (!pd_count) pd_next_buf(unit);
1102 ps_set_intr(do_pd_write_done,pd_ready,PD_TMO,nice);
1105 static void do_pd_write_done( void )
1107 { int unit = pd_unit;
1108 long saved_flags;
1110 if (pd_wait_for(unit,STAT_READY,"do_pd_write_done") & STAT_ERR) {
1111 pi_disconnect(PI);
1112 if (pd_retries < PD_MAX_RETRIES) {
1113 pd_retries++;
1114 pi_do_claimed(PI,do_pd_write_start);
1115 return;
1117 spin_lock_irqsave(&io_request_lock,saved_flags);
1118 end_request(0);
1119 pd_busy = 0;
1120 do_pd_request(NULL);
1121 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1122 return;
1124 pi_disconnect(PI);
1125 spin_lock_irqsave(&io_request_lock,saved_flags);
1126 end_request(1);
1127 pd_busy = 0;
1128 do_pd_request(NULL);
1129 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1132 /* end of pd.c */