File: /usr/src/linux/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 static int pd_maxsectors[PD_DEVS];
291
292 #define PD_NAMELEN 8
293
294 struct pd_unit {
295 struct pi_adapter pia; /* interface to paride layer */
296 struct pi_adapter *pi;
297 int access; /* count of active opens ... */
298 int capacity; /* Size of this volume in sectors */
299 int heads; /* physical geometry */
300 int sectors;
301 int cylinders;
302 int drive; /* master=0 slave=1 */
303 int changed; /* Have we seen a disk change ? */
304 int removable; /* removable media device ? */
305 int standby;
306 int alt_geom;
307 int present;
308 char name[PD_NAMELEN]; /* pda, pdb, etc ... */
309 };
310
311 struct pd_unit pd[PD_UNITS];
312
313 /* 'unit' must be defined in all functions - either as a local or a param */
314
315 #define PD pd[unit]
316 #define PI PD.pi
317
318 static int pd_valid = 1; /* serialise partition checks */
319 static char pd_scratch[512]; /* scratch block buffer */
320
321 /* the variables below are used mainly in the I/O request engine, which
322 processes only one request at a time.
323 */
324
325 static int pd_retries = 0; /* i/o error retry count */
326 static int pd_busy = 0; /* request being processed ? */
327 static int pd_block; /* address of next requested block */
328 static int pd_count; /* number of blocks still to do */
329 static int pd_run; /* sectors in current cluster */
330 static int pd_cmd; /* current command READ/WRITE */
331 static int pd_unit; /* unit of current request */
332 static int pd_dev; /* minor of current request */
333 static int pd_poffs; /* partition offset of current minor */
334 static char * pd_buf; /* buffer for request in progress */
335
336 static DECLARE_WAIT_QUEUE_HEAD(pd_wait_open);
337
338 static char *pd_errs[17] = { "ERR","INDEX","ECC","DRQ","SEEK","WRERR",
339 "READY","BUSY","AMNF","TK0NF","ABRT","MCR",
340 "IDNF","MC","UNC","???","TMO"};
341
342 /* kernel glue structures */
343
344 extern struct block_device_operations pd_fops;
345
346 static struct gendisk pd_gendisk = {
347 major: PD_MAJOR,
348 major_name: PD_NAME,
349 minor_shift: PD_BITS,
350 max_p: PD_PARTNS,
351 part: pd_hd,
352 sizes: pd_sizes,
353 fops: &pd_fops,
354 };
355
356 static struct block_device_operations pd_fops = {
357 open: pd_open,
358 release: pd_release,
359 ioctl: pd_ioctl,
360 check_media_change: pd_check_media,
361 revalidate: pd_revalidate
362 };
363
364 void pd_init_units( void )
365
366 { int unit, j;
367
368 pd_drive_count = 0;
369 for (unit=0;unit<PD_UNITS;unit++) {
370 PD.pi = & PD.pia;
371 PD.access = 0;
372 PD.changed = 1;
373 PD.capacity = 0;
374 PD.drive = DU[D_SLV];
375 PD.present = 0;
376 j = 0;
377 while ((j < PD_NAMELEN-2) && (PD.name[j]=name[j])) j++;
378 PD.name[j++] = 'a' + unit;
379 PD.name[j] = 0;
380 PD.alt_geom = DU[D_GEO];
381 PD.standby = DU[D_SBY];
382 if (DU[D_PRT]) pd_drive_count++;
383 }
384 }
385
386 int pd_init (void)
387
388 { int i;
389 request_queue_t * q;
390
391 if (disable) return -1;
392 if (devfs_register_blkdev(MAJOR_NR,name,&pd_fops)) {
393 printk("%s: unable to get major number %d\n",
394 name,major);
395 return -1;
396 }
397 q = BLK_DEFAULT_QUEUE(MAJOR_NR);
398 blk_init_queue(q, DEVICE_REQUEST);
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 for(i=0;i<PD_DEVS;i++) pd_maxsectors[i] = cluster;
409 max_sectors[MAJOR_NR] = pd_maxsectors;
410
411 printk("%s: %s version %s, major %d, cluster %d, nice %d\n",
412 name,name,PD_VERSION,major,cluster,nice);
413 pd_init_units();
414 pd_valid = 0;
415 pd_gendisk.nr_real = pd_detect();
416 pd_valid = 1;
417
418 #ifdef MODULE
419 if (!pd_gendisk.nr_real) {
420 cleanup_module();
421 return -1;
422 }
423 #endif
424 return 0;
425 }
426
427 static int pd_open (struct inode *inode, struct file *file)
428
429 { int unit = DEVICE_NR(inode->i_rdev);
430
431 if ((unit >= PD_UNITS) || (!PD.present)) return -ENODEV;
432
433 MOD_INC_USE_COUNT;
434
435 wait_event (pd_wait_open, pd_valid);
436
437 PD.access++;
438
439 if (PD.removable) {
440 pd_media_check(unit);
441 pd_doorlock(unit,IDE_DOORLOCK);
442 }
443 return 0;
444 }
445
446 static int pd_ioctl(struct inode *inode,struct file *file,
447 unsigned int cmd, unsigned long arg)
448
449 { struct hd_geometry *geo = (struct hd_geometry *) arg;
450 int dev, err, unit;
451
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;
457
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;
466
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);
476 }
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 BLKGETSIZE64:
486 return put_user((u64)pd_hd[dev].nr_sects << 9, (u64 *)arg);
487 case BLKRRPART:
488 if (!capable(CAP_SYS_ADMIN))
489 return -EACCES;
490 return pd_revalidate(inode->i_rdev);
491 case BLKROSET:
492 case BLKROGET:
493 case BLKRASET:
494 case BLKRAGET:
495 case BLKFLSBUF:
496 case BLKPG:
497 return blk_ioctl(inode->i_rdev, cmd, arg);
498 default:
499 return -EINVAL;
500 }
501 }
502
503 static int pd_release (struct inode *inode, struct file *file)
504
505 { kdev_t devp;
506 int unit;
507
508 devp = inode->i_rdev;
509 unit = DEVICE_NR(devp);
510
511 if ((unit >= PD_UNITS) || (PD.access <= 0))
512 return -EINVAL;
513
514 PD.access--;
515
516 if (!PD.access && PD.removable)
517 pd_doorlock(unit,IDE_DOORUNLOCK);
518
519 MOD_DEC_USE_COUNT;
520
521 return 0;
522 }
523
524 static int pd_check_media( kdev_t dev)
525
526 { int r, unit;
527
528 unit = DEVICE_NR(dev);
529 if ((unit >= PD_UNITS) || (!PD.present)) return -ENODEV;
530 if (!PD.removable) return 0;
531 pd_media_check(unit);
532 r = PD.changed;
533 PD.changed = 0;
534 return r;
535 }
536
537 static int pd_revalidate(kdev_t dev)
538
539 { int p, unit, minor;
540 long flags;
541
542 unit = DEVICE_NR(dev);
543 if ((unit >= PD_UNITS) || (!PD.present)) return -ENODEV;
544
545 save_flags(flags);
546 cli();
547 if (PD.access > 1) {
548 restore_flags(flags);
549 return -EBUSY;
550 }
551 pd_valid = 0;
552 restore_flags(flags);
553
554 for (p=(PD_PARTNS-1);p>=0;p--) {
555 minor = p + unit*PD_PARTNS;
556 invalidate_device(MKDEV(MAJOR_NR, minor), 1);
557 pd_hd[minor].start_sect = 0;
558 pd_hd[minor].nr_sects = 0;
559 }
560
561 if (pd_identify(unit))
562 grok_partitions(&pd_gendisk,unit,1<<PD_BITS,PD.capacity);
563
564 pd_valid = 1;
565 wake_up(&pd_wait_open);
566
567 return 0;
568 }
569
570 #ifdef MODULE
571
572 /* Glue for modules ... */
573
574 void cleanup_module(void);
575
576 int init_module(void)
577
578 {
579
580 #ifdef PARIDE_JUMBO
581 { extern paride_init();
582 paride_init();
583 }
584 #endif
585 return pd_init();
586 }
587
588 void cleanup_module(void)
589
590 { struct gendisk **gdp;
591 int unit;
592
593 devfs_unregister_blkdev(MAJOR_NR,name);
594 del_gendisk(&pd_gendisk);
595
596 for (unit=0;unit<PD_UNITS;unit++)
597 if (PD.present) pi_release(PI);
598
599 max_sectors[MAJOR_NR] = NULL;
600 }
601
602 #endif
603
604 #define WR(c,r,v) pi_write_regr(PI,c,r,v)
605 #define RR(c,r) (pi_read_regr(PI,c,r))
606
607 #define DRIVE (0xa0+0x10*PD.drive)
608
609 /* ide command interface */
610
611 static void pd_print_error( int unit, char * msg, int status )
612
613 { int i;
614
615 printk("%s: %s: status = 0x%x =",PD.name,msg,status);
616 for(i=0;i<18;i++) if (status & (1<<i)) printk(" %s",pd_errs[i]);
617 printk("\n");
618 }
619
620 static void pd_reset( int unit ) /* called only for MASTER drive */
621
622 { pi_connect(PI);
623 WR(1,6,4);
624 udelay(50);
625 WR(1,6,0);
626 pi_disconnect(PI);
627 udelay(250);
628 }
629
630 #define DBMSG(msg) ((verbose>1)?(msg):NULL)
631
632 static int pd_wait_for( int unit, int w, char * msg ) /* polled wait */
633
634 { int k, r, e;
635
636 k=0;
637 while(k < PD_SPIN) {
638 r = RR(1,6);
639 k++;
640 if (((r & w) == w) && !(r & STAT_BUSY)) break;
641 udelay(PD_SPIN_DEL);
642 }
643 e = (RR(0,1)<<8) + RR(0,7);
644 if (k >= PD_SPIN) e |= ERR_TMO;
645 if ((e & (STAT_ERR|ERR_TMO)) && (msg != NULL))
646 pd_print_error(unit,msg,e);
647 return e;
648 }
649
650 static void pd_send_command( int unit, int n, int s, int h,
651 int c0, int c1, int func )
652
653 {
654 WR(0,6,DRIVE+h);
655 WR(0,1,0); /* the IDE task file */
656 WR(0,2,n);
657 WR(0,3,s);
658 WR(0,4,c0);
659 WR(0,5,c1);
660 WR(0,7,func);
661
662 udelay(1);
663 }
664
665 static void pd_ide_command( int unit, int func, int block, int count )
666
667 /* Don't use this call if the capacity is zero. */
668
669 { int c1, c0, h, s;
670
671 s = ( block % PD.sectors) + 1;
672 h = ( block / PD.sectors) % PD.heads;
673 c0 = ( block / (PD.sectors*PD.heads)) % 256;
674 c1 = ( block / (PD.sectors*PD.heads*256));
675
676 pd_send_command(unit,count,s,h,c0,c1,func);
677 }
678
679 /* According to the ATA standard, the default CHS geometry should be
680 available following a reset. Some Western Digital drives come up
681 in a mode where only LBA addresses are accepted until the device
682 parameters are initialised.
683 */
684
685 static void pd_init_dev_parms( int unit )
686
687 { pi_connect(PI);
688 pd_wait_for(unit,0,DBMSG("before init_dev_parms"));
689 pd_send_command(unit,PD.sectors,0,PD.heads-1,0,0,IDE_INIT_DEV_PARMS);
690 udelay(300);
691 pd_wait_for(unit,0,"Initialise device parameters");
692 pi_disconnect(PI);
693 }
694
695 static void pd_doorlock( int unit, int func )
696
697 { pi_connect(PI);
698 if (pd_wait_for(unit,STAT_READY,"Lock") & STAT_ERR) {
699 pi_disconnect(PI);
700 return;
701 }
702 pd_send_command(unit,1,0,0,0,0,func);
703 pd_wait_for(unit,STAT_READY,"Lock done");
704 pi_disconnect(PI);
705 }
706
707 static void pd_eject( int unit )
708
709 { pi_connect(PI);
710 pd_wait_for(unit,0,DBMSG("before unlock on eject"));
711 pd_send_command(unit,1,0,0,0,0,IDE_DOORUNLOCK);
712 pd_wait_for(unit,0,DBMSG("after unlock on eject"));
713 pd_wait_for(unit,0,DBMSG("before eject"));
714 pd_send_command(unit,0,0,0,0,0,IDE_EJECT);
715 pd_wait_for(unit,0,DBMSG("after eject"));
716 pi_disconnect(PI);
717 }
718
719 static void pd_media_check( int unit )
720
721 { int r;
722
723 pi_connect(PI);
724 r = pd_wait_for(unit,STAT_READY,DBMSG("before media_check"));
725 if (!(r & STAT_ERR)) {
726 pd_send_command(unit,1,1,0,0,0,IDE_READ_VRFY);
727 r = pd_wait_for(unit,STAT_READY,DBMSG("RDY after READ_VRFY"));
728 } else PD.changed = 1; /* say changed if other error */
729 if (r & ERR_MC) {
730 PD.changed = 1;
731 pd_send_command(unit,1,0,0,0,0,IDE_ACKCHANGE);
732 pd_wait_for(unit,STAT_READY,DBMSG("RDY after ACKCHANGE"));
733 pd_send_command(unit,1,1,0,0,0,IDE_READ_VRFY);
734 r = pd_wait_for(unit,STAT_READY,DBMSG("RDY after VRFY"));
735 }
736 pi_disconnect(PI);
737
738 }
739
740 static void pd_standby_off( int unit )
741
742 { pi_connect(PI);
743 pd_wait_for(unit,0,DBMSG("before STANDBY"));
744 pd_send_command(unit,0,0,0,0,0,IDE_STANDBY);
745 pd_wait_for(unit,0,DBMSG("after STANDBY"));
746 pi_disconnect(PI);
747 }
748
749 #define word_val(n) ((pd_scratch[2*n]&0xff)+256*(pd_scratch[2*n+1]&0xff))
750
751 static int pd_identify( int unit )
752
753 { int j;
754 char id[PD_ID_LEN+1];
755
756 /* WARNING: here there may be dragons. reset() applies to both drives,
757 but we call it only on probing the MASTER. This should allow most
758 common configurations to work, but be warned that a reset can clear
759 settings on the SLAVE drive.
760 */
761
762 if (PD.drive == 0) pd_reset(unit);
763
764 pi_connect(PI);
765 WR(0,6,DRIVE);
766 pd_wait_for(unit,0,DBMSG("before IDENT"));
767 pd_send_command(unit,1,0,0,0,0,IDE_IDENTIFY);
768
769 if (pd_wait_for(unit,STAT_DRQ,DBMSG("IDENT DRQ")) & STAT_ERR) {
770 pi_disconnect(PI);
771 return 0;
772 }
773 pi_read_block(PI,pd_scratch,512);
774 pi_disconnect(PI);
775 PD.sectors = word_val(6);
776 PD.heads = word_val(3);
777 PD.cylinders = word_val(1);
778 PD.capacity = PD.sectors*PD.heads*PD.cylinders;
779
780 for(j=0;j<PD_ID_LEN;j++) id[j^1] = pd_scratch[j+PD_ID_OFF];
781 j = PD_ID_LEN-1;
782 while ((j >= 0) && (id[j] <= 0x20)) j--;
783 j++; id[j] = 0;
784
785 PD.removable = (word_val(0) & 0x80);
786
787 printk("%s: %s, %s, %d blocks [%dM], (%d/%d/%d), %s media\n",
788 PD.name,id,
789 PD.drive?"slave":"master",
790 PD.capacity,PD.capacity/2048,
791 PD.cylinders,PD.heads,PD.sectors,
792 PD.removable?"removable":"fixed");
793
794 if (PD.capacity) pd_init_dev_parms(unit);
795 if (!PD.standby) pd_standby_off(unit);
796
797 return 1;
798 }
799
800 static int pd_probe_drive( int unit )
801 {
802 if (PD.drive == -1) {
803 for (PD.drive=0;PD.drive<=1;PD.drive++)
804 if (pd_identify(unit))
805 return 1;
806 return 0;
807 }
808 return pd_identify(unit);
809 }
810
811 static int pd_detect( void )
812
813 { int k, unit;
814
815 k = 0;
816 if (pd_drive_count == 0) { /* nothing spec'd - so autoprobe for 1 */
817 unit = 0;
818 if (pi_init(PI,1,-1,-1,-1,-1,-1,pd_scratch,
819 PI_PD,verbose,PD.name)) {
820 if (pd_probe_drive(unit)) {
821 PD.present = 1;
822 k = 1;
823 } else pi_release(PI);
824 }
825
826 } else for (unit=0;unit<PD_UNITS;unit++) if (DU[D_PRT])
827 if (pi_init(PI,0,DU[D_PRT],DU[D_MOD],DU[D_UNI],
828 DU[D_PRO],DU[D_DLY],pd_scratch,
829 PI_PD,verbose,PD.name)) {
830 if (pd_probe_drive(unit)) {
831 PD.present = 1;
832 k = unit+1;
833 } else pi_release(PI);
834 }
835 for (unit=0;unit<PD_UNITS;unit++)
836 register_disk(&pd_gendisk,MKDEV(MAJOR_NR,unit<<PD_BITS),
837 PD_PARTNS,&pd_fops,
838 PD.present?PD.capacity:0);
839
840 /* We lie about the number of drives found, as the generic partition
841 scanner assumes that the drives are numbered sequentially from 0.
842 This can result in some bogus error messages if non-sequential
843 drive numbers are used.
844 */
845 if (k)
846 return k;
847 printk("%s: no valid drive found\n",name);
848 return 0;
849 }
850
851 /* The i/o request engine */
852
853 static int pd_ready( void )
854
855 { int unit = pd_unit;
856
857 return (!(RR(1,6) & STAT_BUSY)) ;
858 }
859
860 static void do_pd_request (request_queue_t * q)
861
862 { struct buffer_head * bh;
863 int unit;
864
865 if (pd_busy) return;
866 repeat:
867 if (QUEUE_EMPTY || (CURRENT->rq_status == RQ_INACTIVE)) return;
868 INIT_REQUEST;
869
870 pd_dev = MINOR(CURRENT->rq_dev);
871 pd_unit = unit = DEVICE_NR(CURRENT->rq_dev);
872 pd_block = CURRENT->sector;
873 pd_run = CURRENT->nr_sectors;
874 pd_count = CURRENT->current_nr_sectors;
875
876 bh = CURRENT->bh;
877
878 if ((pd_dev >= PD_DEVS) ||
879 ((pd_block+pd_count) > pd_hd[pd_dev].nr_sects)) {
880 end_request(0);
881 goto repeat;
882 }
883
884 pd_cmd = CURRENT->cmd;
885 pd_poffs = pd_hd[pd_dev].start_sect;
886 pd_block += pd_poffs;
887 pd_buf = CURRENT->buffer;
888 pd_retries = 0;
889
890 pd_busy = 1;
891 if (pd_cmd == READ) pi_do_claimed(PI,do_pd_read);
892 else if (pd_cmd == WRITE) pi_do_claimed(PI,do_pd_write);
893 else { pd_busy = 0;
894 end_request(0);
895 goto repeat;
896 }
897 }
898
899 static void pd_next_buf( int unit )
900
901 { long saved_flags;
902
903 spin_lock_irqsave(&io_request_lock,saved_flags);
904 end_request(1);
905 if (!pd_run) { spin_unlock_irqrestore(&io_request_lock,saved_flags);
906 return;
907 }
908
909 /* paranoia */
910
911 if (QUEUE_EMPTY ||
912 (CURRENT->cmd != pd_cmd) ||
913 (MINOR(CURRENT->rq_dev) != pd_dev) ||
914 (CURRENT->rq_status == RQ_INACTIVE) ||
915 (CURRENT->sector+pd_poffs != pd_block))
916 printk("%s: OUCH: request list changed unexpectedly\n",
917 PD.name);
918
919 pd_count = CURRENT->current_nr_sectors;
920 pd_buf = CURRENT->buffer;
921 spin_unlock_irqrestore(&io_request_lock,saved_flags);
922 }
923
924 static void do_pd_read( void )
925
926 { ps_set_intr(do_pd_read_start,0,0,nice);
927 }
928
929 static void do_pd_read_start( void )
930
931 { int unit = pd_unit;
932 long saved_flags;
933
934 pd_busy = 1;
935
936 pi_connect(PI);
937 if (pd_wait_for(unit,STAT_READY,"do_pd_read") & STAT_ERR) {
938 pi_disconnect(PI);
939 if (pd_retries < PD_MAX_RETRIES) {
940 pd_retries++;
941 pi_do_claimed(PI,do_pd_read_start);
942 return;
943 }
944 spin_lock_irqsave(&io_request_lock,saved_flags);
945 end_request(0);
946 pd_busy = 0;
947 do_pd_request(NULL);
948 spin_unlock_irqrestore(&io_request_lock,saved_flags);
949 return;
950 }
951 pd_ide_command(unit,IDE_READ,pd_block,pd_run);
952 ps_set_intr(do_pd_read_drq,pd_ready,PD_TMO,nice);
953 }
954
955 static void do_pd_read_drq( void )
956
957 { int unit = pd_unit;
958 long saved_flags;
959
960 while (1) {
961 if (pd_wait_for(unit,STAT_DRQ,"do_pd_read_drq") & STAT_ERR) {
962 pi_disconnect(PI);
963 if (pd_retries < PD_MAX_RETRIES) {
964 pd_retries++;
965 pi_do_claimed(PI,do_pd_read_start);
966 return;
967 }
968 spin_lock_irqsave(&io_request_lock,saved_flags);
969 end_request(0);
970 pd_busy = 0;
971 do_pd_request(NULL);
972 spin_unlock_irqrestore(&io_request_lock,saved_flags);
973 return;
974 }
975 pi_read_block(PI,pd_buf,512);
976 pd_count--; pd_run--;
977 pd_buf += 512;
978 pd_block++;
979 if (!pd_run) break;
980 if (!pd_count) pd_next_buf(unit);
981 }
982 pi_disconnect(PI);
983 spin_lock_irqsave(&io_request_lock,saved_flags);
984 end_request(1);
985 pd_busy = 0;
986 do_pd_request(NULL);
987 spin_unlock_irqrestore(&io_request_lock,saved_flags);
988 }
989
990 static void do_pd_write( void )
991
992 { ps_set_intr(do_pd_write_start,0,0,nice);
993 }
994
995 static void do_pd_write_start( void )
996
997 { int unit = pd_unit;
998 long saved_flags;
999
1000 pd_busy = 1;
1001
1002 pi_connect(PI);
1003 if (pd_wait_for(unit,STAT_READY,"do_pd_write") & STAT_ERR) {
1004 pi_disconnect(PI);
1005 if (pd_retries < PD_MAX_RETRIES) {
1006 pd_retries++;
1007 pi_do_claimed(PI,do_pd_write_start);
1008 return;
1009 }
1010 spin_lock_irqsave(&io_request_lock,saved_flags);
1011 end_request(0);
1012 pd_busy = 0;
1013 do_pd_request(NULL);
1014 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1015 return;
1016 }
1017 pd_ide_command(unit,IDE_WRITE,pd_block,pd_run);
1018 while (1) {
1019 if (pd_wait_for(unit,STAT_DRQ,"do_pd_write_drq") & STAT_ERR) {
1020 pi_disconnect(PI);
1021 if (pd_retries < PD_MAX_RETRIES) {
1022 pd_retries++;
1023 pi_do_claimed(PI,do_pd_write_start);
1024 return;
1025 }
1026 spin_lock_irqsave(&io_request_lock,saved_flags);
1027 end_request(0);
1028 pd_busy = 0;
1029 do_pd_request(NULL);
1030 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1031 return;
1032 }
1033 pi_write_block(PI,pd_buf,512);
1034 pd_count--; pd_run--;
1035 pd_buf += 512;
1036 pd_block++;
1037 if (!pd_run) break;
1038 if (!pd_count) pd_next_buf(unit);
1039 }
1040 ps_set_intr(do_pd_write_done,pd_ready,PD_TMO,nice);
1041 }
1042
1043 static void do_pd_write_done( void )
1044
1045 { int unit = pd_unit;
1046 long saved_flags;
1047
1048 if (pd_wait_for(unit,STAT_READY,"do_pd_write_done") & STAT_ERR) {
1049 pi_disconnect(PI);
1050 if (pd_retries < PD_MAX_RETRIES) {
1051 pd_retries++;
1052 pi_do_claimed(PI,do_pd_write_start);
1053 return;
1054 }
1055 spin_lock_irqsave(&io_request_lock,saved_flags);
1056 end_request(0);
1057 pd_busy = 0;
1058 do_pd_request(NULL);
1059 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1060 return;
1061 }
1062 pi_disconnect(PI);
1063 spin_lock_irqsave(&io_request_lock,saved_flags);
1064 end_request(1);
1065 pd_busy = 0;
1066 do_pd_request(NULL);
1067 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1068 }
1069
1070 /* end of pd.c */
1071
1072