Import 2.3.18pre1
[davej-history.git] / drivers / block / paride / pf.c
blob58a747cb7a73cbcb32d3ddd7e22c0217ec83784e
1 /*
2 pf.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 ATAPI disk
6 drives based on chips supported by the paride module.
8 By default, the driver will autoprobe for a single parallel
9 port ATAPI disk drive, but if their individual parameters are
10 specified, the driver can handle up to 4 drives.
12 The behaviour of the pf 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-7 integers as follows:
18 drive2
19 drive3 <prt>,<pro>,<uni>,<mod>,<slv>,<lun>,<dly>
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 <slv> ATAPI CDroms can be jumpered to master or slave.
42 Set this to 0 to choose the master drive, 1 to
43 choose the slave, -1 (the default) to choose the
44 first drive found.
46 <lun> Some ATAPI devices support multiple LUNs.
47 One example is the ATAPI PD/CD drive from
48 Matshita/Panasonic. This device has a
49 CD drive on LUN 0 and a PD drive on LUN 1.
50 By default, the driver will search for the
51 first LUN with a supported device. Set
52 this parameter to force it to use a specific
53 LUN. (default -1)
55 <dly> some parallel ports require the driver to
56 go more slowly. -1 sets a default value that
57 should work with the chosen protocol. Otherwise,
58 set this to a small integer, the larger it is
59 the slower the port i/o. In some cases, setting
60 this to zero will speed up the device. (default -1)
62 major You may use this parameter to overide the
63 default major number (47) that this driver
64 will use. Be sure to change the device
65 name as well.
67 name This parameter is a character string that
68 contains the name the kernel will use for this
69 device (in /proc output, for instance).
70 (default "pf").
72 cluster The driver will attempt to aggregate requests
73 for adjacent blocks into larger multi-block
74 clusters. The maximum cluster size (in 512
75 byte sectors) is set with this parameter.
76 (default 64)
78 verbose This parameter controls the amount of logging
79 that the driver will do. Set it to 0 for
80 normal operation, 1 to see autoprobe progress
81 messages, or 2 to see additional debugging
82 output. (default 0)
84 nice This parameter controls the driver's use of
85 idle CPU time, at the expense of some speed.
87 If this driver is built into the kernel, you can use the
88 following command line parameters, with the same values
89 as the corresponding module parameters listed above:
91 pf.drive0
92 pf.drive1
93 pf.drive2
94 pf.drive3
95 pf.cluster
96 pf.nice
98 In addition, you can use the parameter pf.disable to disable
99 the driver entirely.
103 /* Changes:
105 1.01 GRG 1998.05.03 Changes for SMP. Eliminate sti().
106 Fix for drives that don't clear STAT_ERR
107 until after next CDB delivered.
108 Small change in pf_completion to round
109 up transfer size.
110 1.02 GRG 1998.06.16 Eliminated an Ugh
111 1.03 GRG 1998.08.16 Use HZ in loop timings, extra debugging
112 1.04 GRG 1998.09.24 Added jumbo support
116 #define PF_VERSION "1.04"
117 #define PF_MAJOR 47
118 #define PF_NAME "pf"
119 #define PF_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 = PF_MAJOR;
129 static char *name = PF_NAME;
130 static int cluster = 64;
131 static int nice = 0;
132 static int disable = 0;
134 static int drive0[7] = {0,0,0,-1,-1,-1,-1};
135 static int drive1[7] = {0,0,0,-1,-1,-1,-1};
136 static int drive2[7] = {0,0,0,-1,-1,-1,-1};
137 static int drive3[7] = {0,0,0,-1,-1,-1,-1};
139 static int (*drives[4])[7] = {&drive0,&drive1,&drive2,&drive3};
140 static int pf_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_SLV 4
147 #define D_LUN 5
148 #define D_DLY 6
150 #define DU (*drives[unit])
152 /* 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>
163 #include <linux/spinlock.h>
165 #include <asm/uaccess.h>
167 #ifndef MODULE
169 #include "setup.h"
171 static STT pf_stt[7] = {{"drive0",7,drive0},
172 {"drive1",7,drive1},
173 {"drive2",7,drive2},
174 {"drive3",7,drive3},
175 {"disable",1,&disable},
176 {"cluster",1,&cluster},
177 {"nice",1,&nice}};
179 void pf_setup( char *str, int *ints)
181 { generic_setup(pf_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-7i");
192 MODULE_PARM(drive1,"1-7i");
193 MODULE_PARM(drive2,"1-7i");
194 MODULE_PARM(drive3,"1-7i");
196 #include "paride.h"
198 /* set up defines for blk.h, why don't all drivers do it this way ? */
200 #define MAJOR_NR major
201 #define DEVICE_NAME "PF"
202 #define DEVICE_REQUEST do_pf_request
203 #define DEVICE_NR(device) MINOR(device)
204 #define DEVICE_ON(device)
205 #define DEVICE_OFF(device)
207 #include <linux/blk.h>
208 #include <linux/blkpg.h>
210 #include "pseudo.h"
212 /* constants for faking geometry numbers */
214 #define PF_FD_MAX 8192 /* use FD geometry under this size */
215 #define PF_FD_HDS 2
216 #define PF_FD_SPT 18
217 #define PF_HD_HDS 64
218 #define PF_HD_SPT 32
220 #define PF_MAX_RETRIES 5
221 #define PF_TMO 800 /* interrupt timeout in jiffies */
222 #define PF_SPIN_DEL 50 /* spin delay in micro-seconds */
224 #define PF_SPIN (1000000*PF_TMO)/(HZ*PF_SPIN_DEL)
226 #define STAT_ERR 0x00001
227 #define STAT_INDEX 0x00002
228 #define STAT_ECC 0x00004
229 #define STAT_DRQ 0x00008
230 #define STAT_SEEK 0x00010
231 #define STAT_WRERR 0x00020
232 #define STAT_READY 0x00040
233 #define STAT_BUSY 0x00080
235 #define ATAPI_REQ_SENSE 0x03
236 #define ATAPI_LOCK 0x1e
237 #define ATAPI_DOOR 0x1b
238 #define ATAPI_MODE_SENSE 0x5a
239 #define ATAPI_CAPACITY 0x25
240 #define ATAPI_IDENTIFY 0x12
241 #define ATAPI_READ_10 0x28
242 #define ATAPI_WRITE_10 0x2a
244 int pf_init(void);
245 #ifdef MODULE
246 void cleanup_module( void );
247 #endif
248 static int pf_open(struct inode *inode, struct file *file);
249 static void do_pf_request(void);
250 static int pf_ioctl(struct inode *inode,struct file *file,
251 unsigned int cmd, unsigned long arg);
253 static int pf_release (struct inode *inode, struct file *file);
255 static int pf_detect(void);
256 static void do_pf_read(void);
257 static void do_pf_read_start(void);
258 static void do_pf_write(void);
259 static void do_pf_write_start(void);
260 static void do_pf_read_drq( void );
261 static void do_pf_write_done( void );
263 static int pf_identify (int unit);
264 static void pf_lock(int unit, int func);
265 static void pf_eject(int unit);
266 static int pf_check_media(kdev_t dev);
268 static int pf_blocksizes[PF_UNITS];
270 #define PF_NM 0
271 #define PF_RO 1
272 #define PF_RW 2
274 #define PF_NAMELEN 8
276 struct pf_unit {
277 struct pi_adapter pia; /* interface to paride layer */
278 struct pi_adapter *pi;
279 int removable; /* removable media device ? */
280 int media_status; /* media present ? WP ? */
281 int drive; /* drive */
282 int lun;
283 int access; /* count of active opens ... */
284 int capacity; /* Size of this volume in sectors */
285 int present; /* device present ? */
286 char name[PF_NAMELEN]; /* pf0, pf1, ... */
289 struct pf_unit pf[PF_UNITS];
291 /* 'unit' must be defined in all functions - either as a local or a param */
293 #define PF pf[unit]
294 #define PI PF.pi
296 static char pf_scratch[512]; /* scratch block buffer */
298 /* the variables below are used mainly in the I/O request engine, which
299 processes only one request at a time.
302 static int pf_retries = 0; /* i/o error retry count */
303 static int pf_busy = 0; /* request being processed ? */
304 static int pf_block; /* address of next requested block */
305 static int pf_count; /* number of blocks still to do */
306 static int pf_run; /* sectors in current cluster */
307 static int pf_cmd; /* current command READ/WRITE */
308 static int pf_unit; /* unit of current request */
309 static int pf_mask; /* stopper for pseudo-int */
310 static char * pf_buf; /* buffer for request in progress */
312 /* kernel glue structures */
314 static struct file_operations pf_fops = {
315 NULL, /* lseek - default */
316 block_read, /* read - general block-dev read */
317 block_write, /* write - general block-dev write */
318 NULL, /* readdir - bad */
319 NULL, /* select */
320 pf_ioctl, /* ioctl */
321 NULL, /* mmap */
322 pf_open, /* open */
323 NULL, /* flush */
324 pf_release, /* release */
325 block_fsync, /* fsync */
326 NULL, /* fasync */
327 pf_check_media, /* media change ? */
328 NULL /* revalidate new media */
331 void pf_init_units( void )
333 { int unit, j;
335 pf_drive_count = 0;
336 for (unit=0;unit<PF_UNITS;unit++) {
337 PF.pi = & PF.pia;
338 PF.access = 0;
339 PF.media_status = PF_NM;
340 PF.capacity = 0;
341 PF.present = 0;
342 PF.drive = DU[D_SLV];
343 PF.lun = DU[D_LUN];
344 j = 0;
345 while ((j < PF_NAMELEN-2) && (PF.name[j]=name[j])) j++;
346 PF.name[j++] = '0' + unit;
347 PF.name[j] = 0;
348 if (DU[D_PRT]) pf_drive_count++;
352 int pf_init (void) /* preliminary initialisation */
354 { int i;
356 if (disable) return -1;
358 pf_init_units();
360 if (pf_detect()) return -1;
361 pf_busy = 0;
363 if (register_blkdev(MAJOR_NR,name,&pf_fops)) {
364 printk("pf_init: unable to get major number %d\n",
365 major);
366 return -1;
368 blk_dev[MAJOR_NR].request_fn = DEVICE_REQUEST;
369 read_ahead[MAJOR_NR] = 8; /* 8 sector (4kB) read ahead */
371 for (i=0;i<PF_UNITS;i++) pf_blocksizes[i] = 1024;
372 blksize_size[MAJOR_NR] = pf_blocksizes;
374 return 0;
377 static int pf_open (struct inode *inode, struct file *file)
379 { int unit = DEVICE_NR(inode->i_rdev);
381 if ((unit >= PF_UNITS) || (!PF.present)) return -ENODEV;
383 MOD_INC_USE_COUNT;
385 pf_identify(unit);
387 if (PF.media_status == PF_NM) {
388 MOD_DEC_USE_COUNT;
389 return -ENODEV;
392 if ((PF.media_status == PF_RO) && (file ->f_mode & 2)) {
393 MOD_DEC_USE_COUNT;
394 return -EROFS;
397 PF.access++;
398 if (PF.removable) pf_lock(unit,1);
400 return 0;
403 static int pf_ioctl(struct inode *inode,struct file *file,
404 unsigned int cmd, unsigned long arg)
406 { int err, unit;
407 struct hd_geometry *geo = (struct hd_geometry *) arg;
409 if ((!inode) || (!inode->i_rdev)) return -EINVAL;
410 unit = DEVICE_NR(inode->i_rdev);
411 if (unit >= PF_UNITS) return -EINVAL;
412 if (!PF.present) return -ENODEV;
414 switch (cmd) {
415 case CDROMEJECT:
416 if (PF.access == 1) {
417 pf_eject(unit);
418 return 0;
420 case HDIO_GETGEO:
421 if (!geo) return -EINVAL;
422 err = verify_area(VERIFY_WRITE,geo,sizeof(*geo));
423 if (err) return err;
424 if (PF.capacity < PF_FD_MAX) {
425 put_user(PF.capacity/(PF_FD_HDS*PF_FD_SPT),
426 (short *) &geo->cylinders);
427 put_user(PF_FD_HDS, (char *) &geo->heads);
428 put_user(PF_FD_SPT, (char *) &geo->sectors);
429 } else {
430 put_user(PF.capacity/(PF_HD_HDS*PF_HD_SPT),
431 (short *) &geo->cylinders);
432 put_user(PF_HD_HDS, (char *) &geo->heads);
433 put_user(PF_HD_SPT, (char *) &geo->sectors);
435 put_user(0,(long *)&geo->start);
436 return 0;
437 case BLKGETSIZE:
438 if (!arg) return -EINVAL;
439 err = verify_area(VERIFY_WRITE,(long *) arg,sizeof(long));
440 if (err) return (err);
441 put_user(PF.capacity,(long *) arg);
442 return (0);
443 case BLKROSET:
444 case BLKROGET:
445 case BLKRASET:
446 case BLKRAGET:
447 case BLKFLSBUF:
448 return blk_ioctl(inode->i_rdev, cmd, arg);
449 default:
450 return -EINVAL;
455 static int pf_release (struct inode *inode, struct file *file)
457 { kdev_t devp;
458 int unit;
460 struct super_block *sb;
462 devp = inode->i_rdev;
463 unit = DEVICE_NR(devp);
465 if ((unit >= PF_UNITS) || (PF.access <= 0))
466 return -EINVAL;
468 PF.access--;
470 if (!PF.access) {
471 fsync_dev(devp);
473 sb = get_super(devp);
474 if (sb) invalidate_inodes(sb);
476 invalidate_buffers(devp);
477 if (PF.removable) pf_lock(unit,0);
480 MOD_DEC_USE_COUNT;
482 return 0;
486 static int pf_check_media( kdev_t dev)
488 { return 1;
491 #ifdef MODULE
493 /* Glue for modules ... */
495 void cleanup_module(void);
497 int init_module(void)
499 { int err;
501 #ifdef PARIDE_JUMBO
502 { extern paride_init();
503 paride_init();
505 #endif
507 err = pf_init();
509 return err;
512 void cleanup_module(void)
514 { int unit;
516 unregister_blkdev(MAJOR_NR,name);
518 for (unit=0;unit<PF_UNITS;unit++)
519 if (PF.present) pi_release(PI);
522 #endif
524 #define WR(c,r,v) pi_write_regr(PI,c,r,v)
525 #define RR(c,r) (pi_read_regr(PI,c,r))
527 #define LUN (0x20*PF.lun)
528 #define DRIVE (0xa0+0x10*PF.drive)
530 static int pf_wait( int unit, int go, int stop, char * fun, char * msg )
532 { int j, r, e, s, p;
534 j = 0;
535 while ((((r=RR(1,6))&go)||(stop&&(!(r&stop))))&&(j++<PF_SPIN))
536 udelay(PF_SPIN_DEL);
538 if ((r&(STAT_ERR&stop))||(j>=PF_SPIN)) {
539 s = RR(0,7);
540 e = RR(0,1);
541 p = RR(0,2);
542 if (j >= PF_SPIN) e |= 0x100;
543 if (fun) printk("%s: %s %s: alt=0x%x stat=0x%x err=0x%x"
544 " loop=%d phase=%d\n",
545 PF.name,fun,msg,r,s,e,j,p);
546 return (e<<8)+s;
548 return 0;
551 static int pf_command( int unit, char * cmd, int dlen, char * fun )
553 { pi_connect(PI);
555 WR(0,6,DRIVE);
557 if (pf_wait(unit,STAT_BUSY|STAT_DRQ,0,fun,"before command")) {
558 pi_disconnect(PI);
559 return -1;
562 WR(0,4,dlen % 256);
563 WR(0,5,dlen / 256);
564 WR(0,7,0xa0); /* ATAPI packet command */
566 if (pf_wait(unit,STAT_BUSY,STAT_DRQ,fun,"command DRQ")) {
567 pi_disconnect(PI);
568 return -1;
571 if (RR(0,2) != 1) {
572 printk("%s: %s: command phase error\n",PF.name,fun);
573 pi_disconnect(PI);
574 return -1;
577 pi_write_block(PI,cmd,12);
579 return 0;
582 static int pf_completion( int unit, char * buf, char * fun )
584 { int r, s, n;
586 r = pf_wait(unit,STAT_BUSY,STAT_DRQ|STAT_READY|STAT_ERR,
587 fun,"completion");
589 if ((RR(0,2)&2) && (RR(0,7)&STAT_DRQ)) {
590 n = (((RR(0,4)+256*RR(0,5))+3)&0xfffc);
591 pi_read_block(PI,buf,n);
594 s = pf_wait(unit,STAT_BUSY,STAT_READY|STAT_ERR,fun,"data done");
596 pi_disconnect(PI);
598 return (r?r:s);
601 static void pf_req_sense( int unit, int quiet )
603 { char rs_cmd[12] = { ATAPI_REQ_SENSE,LUN,0,0,16,0,0,0,0,0,0,0 };
604 char buf[16];
605 int r;
607 r = pf_command(unit,rs_cmd,16,"Request sense");
608 mdelay(1);
609 if (!r) pf_completion(unit,buf,"Request sense");
611 if ((!r)&&(!quiet))
612 printk("%s: Sense key: %x, ASC: %x, ASQ: %x\n",
613 PF.name,buf[2]&0xf,buf[12],buf[13]);
616 static int pf_atapi( int unit, char * cmd, int dlen, char * buf, char * fun )
618 { int r;
620 r = pf_command(unit,cmd,dlen,fun);
621 mdelay(1);
622 if (!r) r = pf_completion(unit,buf,fun);
623 if (r) pf_req_sense(unit,!fun);
625 return r;
628 #define DBMSG(msg) ((verbose>1)?(msg):NULL)
630 static void pf_lock(int unit, int func)
632 { char lo_cmd[12] = { ATAPI_LOCK,LUN,0,0,func,0,0,0,0,0,0,0 };
634 pf_atapi(unit,lo_cmd,0,pf_scratch,func?"unlock":"lock");
638 static void pf_eject( int unit )
640 { char ej_cmd[12] = { ATAPI_DOOR,LUN,0,0,2,0,0,0,0,0,0,0 };
642 pf_lock(unit,0);
643 pf_atapi(unit,ej_cmd,0,pf_scratch,"eject");
646 #define PF_RESET_TMO 30 /* in tenths of a second */
648 static void pf_sleep( int cs )
650 { current->state = TASK_INTERRUPTIBLE;
651 schedule_timeout(cs);
655 static int pf_reset( int unit )
657 /* the ATAPI standard actually specifies the contents of all 7 registers
658 after a reset, but the specification is ambiguous concerning the last
659 two bytes, and different drives interpret the standard differently.
662 { int i, k, flg;
663 int expect[5] = {1,1,1,0x14,0xeb};
665 pi_connect(PI);
666 WR(0,6,DRIVE);
667 WR(0,7,8);
669 pf_sleep(2);
671 k = 0;
672 while ((k++ < PF_RESET_TMO) && (RR(1,6)&STAT_BUSY))
673 pf_sleep(10);
675 flg = 1;
676 for(i=0;i<5;i++) flg &= (RR(0,i+1) == expect[i]);
678 if (verbose) {
679 printk("%s: Reset (%d) signature = ",PF.name,k);
680 for (i=0;i<5;i++) printk("%3x",RR(0,i+1));
681 if (!flg) printk(" (incorrect)");
682 printk("\n");
685 pi_disconnect(PI);
686 return flg-1;
689 static void pf_mode_sense( int unit )
691 { char ms_cmd[12] = { ATAPI_MODE_SENSE,LUN,0,0,0,0,0,0,8,0,0,0};
692 char buf[8];
694 pf_atapi(unit,ms_cmd,8,buf,DBMSG("mode sense"));
695 PF.media_status = PF_RW;
696 if (buf[3] & 0x80) PF.media_status = PF_RO;
699 static void xs( char *buf, char *targ, int offs, int len )
701 { int j,k,l;
703 j=0; l=0;
704 for (k=0;k<len;k++)
705 if((buf[k+offs]!=0x20)||(buf[k+offs]!=l))
706 l=targ[j++]=buf[k+offs];
707 if (l==0x20) j--; targ[j]=0;
710 static int xl( char *buf, int offs )
712 { int v,k;
714 v=0;
715 for(k=0;k<4;k++) v=v*256+(buf[k+offs]&0xff);
716 return v;
719 static void pf_get_capacity( int unit )
721 { char rc_cmd[12] = { ATAPI_CAPACITY,LUN,0,0,0,0,0,0,0,0,0,0};
722 char buf[8];
723 int bs;
725 if (pf_atapi(unit,rc_cmd,8,buf,DBMSG("get capacity"))) {
726 PF.media_status = PF_NM;
727 return;
729 PF.capacity = xl(buf,0) + 1;
730 bs = xl(buf,4);
731 if (bs != 512) {
732 PF.capacity = 0;
733 if (verbose) printk("%s: Drive %d, LUN %d,"
734 " unsupported block size %d\n",
735 PF.name,PF.drive,PF.lun,bs);
739 static int pf_identify( int unit )
741 { int dt, s;
742 char *ms[2] = {"master","slave"};
743 char mf[10], id[18];
744 char id_cmd[12] = { ATAPI_IDENTIFY,LUN,0,0,36,0,0,0,0,0,0,0};
745 char buf[36];
747 s = pf_atapi(unit,id_cmd,36,buf,"identify");
748 if (s) return -1;
750 dt = buf[0] & 0x1f;
751 if ((dt != 0) && (dt != 7)) {
752 if (verbose)
753 printk("%s: Drive %d, LUN %d, unsupported type %d\n",
754 PF.name,PF.drive,PF.lun,dt);
755 return -1;
758 xs(buf,mf,8,8);
759 xs(buf,id,16,16);
761 PF.removable = (buf[1] & 0x80);
763 pf_mode_sense(unit);
764 pf_mode_sense(unit);
765 pf_mode_sense(unit);
767 pf_get_capacity(unit);
769 printk("%s: %s %s, %s LUN %d, type %d",
770 PF.name,mf,id,ms[PF.drive],PF.lun,dt);
771 if (PF.removable) printk(", removable");
772 if (PF.media_status == PF_NM)
773 printk(", no media\n");
774 else { if (PF.media_status == PF_RO) printk(", RO");
775 printk(", %d blocks\n",PF.capacity);
778 return 0;
781 static int pf_probe( int unit )
783 /* returns 0, with id set if drive is detected
784 -1, if drive detection failed
787 { if (PF.drive == -1) {
788 for (PF.drive=0;PF.drive<=1;PF.drive++)
789 if (!pf_reset(unit)) {
790 if (PF.lun != -1) return pf_identify(unit);
791 else for (PF.lun=0;PF.lun<8;PF.lun++)
792 if (!pf_identify(unit)) return 0;
794 } else {
795 if (pf_reset(unit)) return -1;
796 if (PF.lun != -1) return pf_identify(unit);
797 for (PF.lun=0;PF.lun<8;PF.lun++)
798 if (!pf_identify(unit)) return 0;
800 return -1;
803 static int pf_detect( void )
805 { int k, unit;
807 printk("%s: %s version %s, major %d, cluster %d, nice %d\n",
808 name,name,PF_VERSION,major,cluster,nice);
810 k = 0;
811 if (pf_drive_count == 0) {
812 unit = 0;
813 if (pi_init(PI,1,-1,-1,-1,-1,-1,pf_scratch,
814 PI_PF,verbose,PF.name)) {
815 if (!pf_probe(unit)) {
816 PF.present = 1;
817 k++;
818 } else pi_release(PI);
821 } else for (unit=0;unit<PF_UNITS;unit++) if (DU[D_PRT])
822 if (pi_init(PI,0,DU[D_PRT],DU[D_MOD],DU[D_UNI],
823 DU[D_PRO],DU[D_DLY],pf_scratch,PI_PF,verbose,
824 PF.name)) {
825 if (!pf_probe(unit)) {
826 PF.present = 1;
827 k++;
828 } else pi_release(PI);
831 if (k) return 0;
833 printk("%s: No ATAPI disk detected\n",name);
834 return -1;
837 /* The i/o request engine */
839 static int pf_start( int unit, int cmd, int b, int c )
841 { int i;
842 char io_cmd[12] = {cmd,LUN,0,0,0,0,0,0,0,0,0,0};
844 for(i=0;i<4;i++) {
845 io_cmd[5-i] = b & 0xff;
846 b = b >> 8;
849 io_cmd[8] = c & 0xff;
850 io_cmd[7] = (c >> 8) & 0xff;
852 i = pf_command(unit,io_cmd,c*512,"start i/o");
854 mdelay(1);
856 return i;
859 static int pf_ready( void )
861 { int unit = pf_unit;
863 return (((RR(1,6)&(STAT_BUSY|pf_mask)) == pf_mask));
866 static void do_pf_request (void)
868 { struct buffer_head * bh;
869 struct request * req;
870 int unit;
872 if (pf_busy) return;
873 repeat:
874 if ((!CURRENT) || (CURRENT->rq_status == RQ_INACTIVE)) return;
875 INIT_REQUEST;
877 pf_unit = unit = DEVICE_NR(CURRENT->rq_dev);
878 pf_block = CURRENT->sector;
879 pf_count = CURRENT->nr_sectors;
881 bh = CURRENT->bh;
882 req = CURRENT;
883 if (bh->b_reqnext)
884 printk("%s: OUCH: b_reqnext != NULL\n",PF.name);
886 if ((pf_unit >= PF_UNITS) || (pf_block+pf_count > PF.capacity)) {
887 end_request(0);
888 goto repeat;
891 pf_cmd = CURRENT->cmd;
892 pf_run = pf_count;
893 while ((pf_run <= cluster) &&
894 (req = req->next) &&
895 (pf_block+pf_run == req->sector) &&
896 (pf_cmd == req->cmd) &&
897 (pf_unit == DEVICE_NR(req->rq_dev)))
898 pf_run += req->nr_sectors;
900 pf_buf = CURRENT->buffer;
901 pf_retries = 0;
903 pf_busy = 1;
904 if (pf_cmd == READ) pi_do_claimed(PI,do_pf_read);
905 else if (pf_cmd == WRITE) pi_do_claimed(PI,do_pf_write);
906 else { pf_busy = 0;
907 end_request(0);
908 goto repeat;
912 static void pf_next_buf( int unit )
914 { long saved_flags;
916 spin_lock_irqsave(&io_request_lock,saved_flags);
917 end_request(1);
918 if (!pf_run) { spin_unlock_irqrestore(&io_request_lock,saved_flags);
919 return;
922 /* paranoia */
924 if ((!CURRENT) ||
925 (CURRENT->cmd != pf_cmd) ||
926 (DEVICE_NR(CURRENT->rq_dev) != pf_unit) ||
927 (CURRENT->rq_status == RQ_INACTIVE) ||
928 (CURRENT->sector != pf_block))
929 printk("%s: OUCH: request list changed unexpectedly\n",
930 PF.name);
932 pf_count = CURRENT->nr_sectors;
933 pf_buf = CURRENT->buffer;
934 spin_unlock_irqrestore(&io_request_lock,saved_flags);
937 static void do_pf_read( void )
939 /* detach from the calling context - in case the spinlock is held */
941 { ps_set_intr(do_pf_read_start,0,0,nice);
944 static void do_pf_read_start( void )
946 { int unit = pf_unit;
947 long saved_flags;
949 pf_busy = 1;
951 if (pf_start(unit,ATAPI_READ_10,pf_block,pf_run)) {
952 pi_disconnect(PI);
953 if (pf_retries < PF_MAX_RETRIES) {
954 pf_retries++;
955 pi_do_claimed(PI,do_pf_read_start);
956 return;
958 spin_lock_irqsave(&io_request_lock,saved_flags);
959 end_request(0);
960 pf_busy = 0;
961 do_pf_request();
962 spin_unlock_irqrestore(&io_request_lock,saved_flags);
963 return;
965 pf_mask = STAT_DRQ;
966 ps_set_intr(do_pf_read_drq,pf_ready,PF_TMO,nice);
969 static void do_pf_read_drq( void )
971 { int unit = pf_unit;
972 long saved_flags;
974 while (1) {
975 if (pf_wait(unit,STAT_BUSY,STAT_DRQ|STAT_ERR,
976 "read block","completion") & STAT_ERR) {
977 pi_disconnect(PI);
978 if (pf_retries < PF_MAX_RETRIES) {
979 pf_req_sense(unit,0);
980 pf_retries++;
981 pi_do_claimed(PI,do_pf_read_start);
982 return;
984 spin_lock_irqsave(&io_request_lock,saved_flags);
985 end_request(0);
986 pf_busy = 0;
987 do_pf_request();
988 spin_unlock_irqrestore(&io_request_lock,saved_flags);
989 return;
991 pi_read_block(PI,pf_buf,512);
992 pf_count--; pf_run--;
993 pf_buf += 512;
994 pf_block++;
995 if (!pf_run) break;
996 if (!pf_count) pf_next_buf(unit);
998 pi_disconnect(PI);
999 spin_lock_irqsave(&io_request_lock,saved_flags);
1000 end_request(1);
1001 pf_busy = 0;
1002 do_pf_request();
1003 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1006 static void do_pf_write( void )
1008 { ps_set_intr(do_pf_write_start,0,0,nice);
1011 static void do_pf_write_start( void )
1013 { int unit = pf_unit;
1014 long saved_flags;
1016 pf_busy = 1;
1018 if (pf_start(unit,ATAPI_WRITE_10,pf_block,pf_run)) {
1019 pi_disconnect(PI);
1020 if (pf_retries < PF_MAX_RETRIES) {
1021 pf_retries++;
1022 pi_do_claimed(PI,do_pf_write_start);
1023 return;
1025 spin_lock_irqsave(&io_request_lock,saved_flags);
1026 end_request(0);
1027 pf_busy = 0;
1028 do_pf_request();
1029 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1030 return;
1033 while (1) {
1034 if (pf_wait(unit,STAT_BUSY,STAT_DRQ|STAT_ERR,
1035 "write block","data wait") & STAT_ERR) {
1036 pi_disconnect(PI);
1037 if (pf_retries < PF_MAX_RETRIES) {
1038 pf_retries++;
1039 pi_do_claimed(PI,do_pf_write_start);
1040 return;
1042 spin_lock_irqsave(&io_request_lock,saved_flags);
1043 end_request(0);
1044 pf_busy = 0;
1045 do_pf_request();
1046 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1047 return;
1049 pi_write_block(PI,pf_buf,512);
1050 pf_count--; pf_run--;
1051 pf_buf += 512;
1052 pf_block++;
1053 if (!pf_run) break;
1054 if (!pf_count) pf_next_buf(unit);
1056 pf_mask = 0;
1057 ps_set_intr(do_pf_write_done,pf_ready,PF_TMO,nice);
1060 static void do_pf_write_done( void )
1062 { int unit = pf_unit;
1063 long saved_flags;
1065 if (pf_wait(unit,STAT_BUSY,0,"write block","done") & STAT_ERR) {
1066 pi_disconnect(PI);
1067 if (pf_retries < PF_MAX_RETRIES) {
1068 pf_retries++;
1069 pi_do_claimed(PI,do_pf_write_start);
1070 return;
1072 spin_lock_irqsave(&io_request_lock,saved_flags);
1073 end_request(0);
1074 pf_busy = 0;
1075 do_pf_request();
1076 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1077 return;
1079 pi_disconnect(PI);
1080 spin_lock_irqsave(&io_request_lock,saved_flags);
1081 end_request(1);
1082 pf_busy = 0;
1083 do_pf_request();
1084 spin_unlock_irqrestore(&io_request_lock,saved_flags);
1087 /* end of pf.c */