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 
142 enum {D_PRT, D_PRO, D_UNI, D_MOD, D_GEO, D_SBY, D_DLY, D_SLV};
143 
144 /* end of parameters */
145 
146 #include <linux/init.h>
147 #include <linux/module.h>
148 #include <linux/gfp.h>
149 #include <linux/fs.h>
150 #include <linux/delay.h>
151 #include <linux/hdreg.h>
152 #include <linux/cdrom.h>	/* for the eject ioctl */
153 #include <linux/blkdev.h>
154 #include <linux/blkpg.h>
155 #include <linux/kernel.h>
156 #include <linux/mutex.h>
157 #include <asm/uaccess.h>
158 #include <linux/workqueue.h>
159 
160 static DEFINE_MUTEX(pd_mutex);
161 static DEFINE_SPINLOCK(pd_lock);
162 
163 module_param(verbose, bool, 0);
164 module_param(major, int, 0);
165 module_param(name, charp, 0);
166 module_param(cluster, int, 0);
167 module_param(nice, int, 0);
168 module_param_array(drive0, int, NULL, 0);
169 module_param_array(drive1, int, NULL, 0);
170 module_param_array(drive2, int, NULL, 0);
171 module_param_array(drive3, int, NULL, 0);
172 
173 #include "paride.h"
174 
175 #define PD_BITS    4
176 
177 /* numbers for "SCSI" geometry */
178 
179 #define PD_LOG_HEADS    64
180 #define PD_LOG_SECTS    32
181 
182 #define PD_ID_OFF       54
183 #define PD_ID_LEN       14
184 
185 #define PD_MAX_RETRIES  5
186 #define PD_TMO          800	/* interrupt timeout in jiffies */
187 #define PD_SPIN_DEL     50	/* spin delay in micro-seconds  */
188 
189 #define PD_SPIN         (1000000*PD_TMO)/(HZ*PD_SPIN_DEL)
190 
191 #define STAT_ERR        0x00001
192 #define STAT_INDEX      0x00002
193 #define STAT_ECC        0x00004
194 #define STAT_DRQ        0x00008
195 #define STAT_SEEK       0x00010
196 #define STAT_WRERR      0x00020
197 #define STAT_READY      0x00040
198 #define STAT_BUSY       0x00080
199 
200 #define ERR_AMNF        0x00100
201 #define ERR_TK0NF       0x00200
202 #define ERR_ABRT        0x00400
203 #define ERR_MCR         0x00800
204 #define ERR_IDNF        0x01000
205 #define ERR_MC          0x02000
206 #define ERR_UNC         0x04000
207 #define ERR_TMO         0x10000
208 
209 #define IDE_READ        	0x20
210 #define IDE_WRITE       	0x30
211 #define IDE_READ_VRFY		0x40
212 #define IDE_INIT_DEV_PARMS	0x91
213 #define IDE_STANDBY     	0x96
214 #define IDE_ACKCHANGE   	0xdb
215 #define IDE_DOORLOCK    	0xde
216 #define IDE_DOORUNLOCK  	0xdf
217 #define IDE_IDENTIFY    	0xec
218 #define IDE_EJECT		0xed
219 
220 #define PD_NAMELEN	8
221 
222 struct pd_unit {
223 	struct pi_adapter pia;	/* interface to paride layer */
224 	struct pi_adapter *pi;
225 	int access;		/* count of active opens ... */
226 	int capacity;		/* Size of this volume in sectors */
227 	int heads;		/* physical geometry */
228 	int sectors;
229 	int cylinders;
230 	int can_lba;
231 	int drive;		/* master=0 slave=1 */
232 	int changed;		/* Have we seen a disk change ? */
233 	int removable;		/* removable media device  ?  */
234 	int standby;
235 	int alt_geom;
236 	char name[PD_NAMELEN];	/* pda, pdb, etc ... */
237 	struct gendisk *gd;
238 };
239 
240 static struct pd_unit pd[PD_UNITS];
241 
242 static char pd_scratch[512];	/* scratch block buffer */
243 
244 static char *pd_errs[17] = { "ERR", "INDEX", "ECC", "DRQ", "SEEK", "WRERR",
245 	"READY", "BUSY", "AMNF", "TK0NF", "ABRT", "MCR",
246 	"IDNF", "MC", "UNC", "???", "TMO"
247 };
248 
status_reg(struct pd_unit * disk)249 static inline int status_reg(struct pd_unit *disk)
250 {
251 	return pi_read_regr(disk->pi, 1, 6);
252 }
253 
read_reg(struct pd_unit * disk,int reg)254 static inline int read_reg(struct pd_unit *disk, int reg)
255 {
256 	return pi_read_regr(disk->pi, 0, reg);
257 }
258 
write_status(struct pd_unit * disk,int val)259 static inline void write_status(struct pd_unit *disk, int val)
260 {
261 	pi_write_regr(disk->pi, 1, 6, val);
262 }
263 
write_reg(struct pd_unit * disk,int reg,int val)264 static inline void write_reg(struct pd_unit *disk, int reg, int val)
265 {
266 	pi_write_regr(disk->pi, 0, reg, val);
267 }
268 
DRIVE(struct pd_unit * disk)269 static inline u8 DRIVE(struct pd_unit *disk)
270 {
271 	return 0xa0+0x10*disk->drive;
272 }
273 
274 /*  ide command interface */
275 
pd_print_error(struct pd_unit * disk,char * msg,int status)276 static void pd_print_error(struct pd_unit *disk, char *msg, int status)
277 {
278 	int i;
279 
280 	printk("%s: %s: status = 0x%x =", disk->name, msg, status);
281 	for (i = 0; i < ARRAY_SIZE(pd_errs); i++)
282 		if (status & (1 << i))
283 			printk(" %s", pd_errs[i]);
284 	printk("\n");
285 }
286 
pd_reset(struct pd_unit * disk)287 static void pd_reset(struct pd_unit *disk)
288 {				/* called only for MASTER drive */
289 	write_status(disk, 4);
290 	udelay(50);
291 	write_status(disk, 0);
292 	udelay(250);
293 }
294 
295 #define DBMSG(msg)	((verbose>1)?(msg):NULL)
296 
pd_wait_for(struct pd_unit * disk,int w,char * msg)297 static int pd_wait_for(struct pd_unit *disk, int w, char *msg)
298 {				/* polled wait */
299 	int k, r, e;
300 
301 	k = 0;
302 	while (k < PD_SPIN) {
303 		r = status_reg(disk);
304 		k++;
305 		if (((r & w) == w) && !(r & STAT_BUSY))
306 			break;
307 		udelay(PD_SPIN_DEL);
308 	}
309 	e = (read_reg(disk, 1) << 8) + read_reg(disk, 7);
310 	if (k >= PD_SPIN)
311 		e |= ERR_TMO;
312 	if ((e & (STAT_ERR | ERR_TMO)) && (msg != NULL))
313 		pd_print_error(disk, msg, e);
314 	return e;
315 }
316 
pd_send_command(struct pd_unit * disk,int n,int s,int h,int c0,int c1,int func)317 static void pd_send_command(struct pd_unit *disk, int n, int s, int h, int c0, int c1, int func)
318 {
319 	write_reg(disk, 6, DRIVE(disk) + h);
320 	write_reg(disk, 1, 0);		/* the IDE task file */
321 	write_reg(disk, 2, n);
322 	write_reg(disk, 3, s);
323 	write_reg(disk, 4, c0);
324 	write_reg(disk, 5, c1);
325 	write_reg(disk, 7, func);
326 
327 	udelay(1);
328 }
329 
pd_ide_command(struct pd_unit * disk,int func,int block,int count)330 static void pd_ide_command(struct pd_unit *disk, int func, int block, int count)
331 {
332 	int c1, c0, h, s;
333 
334 	if (disk->can_lba) {
335 		s = block & 255;
336 		c0 = (block >>= 8) & 255;
337 		c1 = (block >>= 8) & 255;
338 		h = ((block >>= 8) & 15) + 0x40;
339 	} else {
340 		s = (block % disk->sectors) + 1;
341 		h = (block /= disk->sectors) % disk->heads;
342 		c0 = (block /= disk->heads) % 256;
343 		c1 = (block >>= 8);
344 	}
345 	pd_send_command(disk, count, s, h, c0, c1, func);
346 }
347 
348 /* The i/o request engine */
349 
350 enum action {Fail = 0, Ok = 1, Hold, Wait};
351 
352 static struct request *pd_req;	/* current request */
353 static enum action (*phase)(void);
354 
355 static void run_fsm(void);
356 
357 static void ps_tq_int(struct work_struct *work);
358 
359 static DECLARE_DELAYED_WORK(fsm_tq, ps_tq_int);
360 
schedule_fsm(void)361 static void schedule_fsm(void)
362 {
363 	if (!nice)
364 		schedule_delayed_work(&fsm_tq, 0);
365 	else
366 		schedule_delayed_work(&fsm_tq, nice-1);
367 }
368 
ps_tq_int(struct work_struct * work)369 static void ps_tq_int(struct work_struct *work)
370 {
371 	run_fsm();
372 }
373 
374 static enum action do_pd_io_start(void);
375 static enum action pd_special(void);
376 static enum action do_pd_read_start(void);
377 static enum action do_pd_write_start(void);
378 static enum action do_pd_read_drq(void);
379 static enum action do_pd_write_done(void);
380 
381 static struct request_queue *pd_queue;
382 static int pd_claimed;
383 
384 static struct pd_unit *pd_current; /* current request's drive */
385 static PIA *pi_current; /* current request's PIA */
386 
run_fsm(void)387 static void run_fsm(void)
388 {
389 	while (1) {
390 		enum action res;
391 		unsigned long saved_flags;
392 		int stop = 0;
393 
394 		if (!phase) {
395 			pd_current = pd_req->rq_disk->private_data;
396 			pi_current = pd_current->pi;
397 			phase = do_pd_io_start;
398 		}
399 
400 		switch (pd_claimed) {
401 			case 0:
402 				pd_claimed = 1;
403 				if (!pi_schedule_claimed(pi_current, run_fsm))
404 					return;
405 			case 1:
406 				pd_claimed = 2;
407 				pi_current->proto->connect(pi_current);
408 		}
409 
410 		switch(res = phase()) {
411 			case Ok: case Fail:
412 				pi_disconnect(pi_current);
413 				pd_claimed = 0;
414 				phase = NULL;
415 				spin_lock_irqsave(&pd_lock, saved_flags);
416 				if (!__blk_end_request_cur(pd_req,
417 						res == Ok ? 0 : -EIO)) {
418 					pd_req = blk_fetch_request(pd_queue);
419 					if (!pd_req)
420 						stop = 1;
421 				}
422 				spin_unlock_irqrestore(&pd_lock, saved_flags);
423 				if (stop)
424 					return;
425 			case Hold:
426 				schedule_fsm();
427 				return;
428 			case Wait:
429 				pi_disconnect(pi_current);
430 				pd_claimed = 0;
431 		}
432 	}
433 }
434 
435 static int pd_retries = 0;	/* i/o error retry count */
436 static int pd_block;		/* address of next requested block */
437 static int pd_count;		/* number of blocks still to do */
438 static int pd_run;		/* sectors in current cluster */
439 static int pd_cmd;		/* current command READ/WRITE */
440 static char *pd_buf;		/* buffer for request in progress */
441 
do_pd_io_start(void)442 static enum action do_pd_io_start(void)
443 {
444 	if (pd_req->cmd_type == REQ_TYPE_SPECIAL) {
445 		phase = pd_special;
446 		return pd_special();
447 	}
448 
449 	pd_cmd = rq_data_dir(pd_req);
450 	if (pd_cmd == READ || pd_cmd == WRITE) {
451 		pd_block = blk_rq_pos(pd_req);
452 		pd_count = blk_rq_cur_sectors(pd_req);
453 		if (pd_block + pd_count > get_capacity(pd_req->rq_disk))
454 			return Fail;
455 		pd_run = blk_rq_sectors(pd_req);
456 		pd_buf = pd_req->buffer;
457 		pd_retries = 0;
458 		if (pd_cmd == READ)
459 			return do_pd_read_start();
460 		else
461 			return do_pd_write_start();
462 	}
463 	return Fail;
464 }
465 
pd_special(void)466 static enum action pd_special(void)
467 {
468 	enum action (*func)(struct pd_unit *) = pd_req->special;
469 	return func(pd_current);
470 }
471 
pd_next_buf(void)472 static int pd_next_buf(void)
473 {
474 	unsigned long saved_flags;
475 
476 	pd_count--;
477 	pd_run--;
478 	pd_buf += 512;
479 	pd_block++;
480 	if (!pd_run)
481 		return 1;
482 	if (pd_count)
483 		return 0;
484 	spin_lock_irqsave(&pd_lock, saved_flags);
485 	__blk_end_request_cur(pd_req, 0);
486 	pd_count = blk_rq_cur_sectors(pd_req);
487 	pd_buf = pd_req->buffer;
488 	spin_unlock_irqrestore(&pd_lock, saved_flags);
489 	return 0;
490 }
491 
492 static unsigned long pd_timeout;
493 
do_pd_read_start(void)494 static enum action do_pd_read_start(void)
495 {
496 	if (pd_wait_for(pd_current, STAT_READY, "do_pd_read") & STAT_ERR) {
497 		if (pd_retries < PD_MAX_RETRIES) {
498 			pd_retries++;
499 			return Wait;
500 		}
501 		return Fail;
502 	}
503 	pd_ide_command(pd_current, IDE_READ, pd_block, pd_run);
504 	phase = do_pd_read_drq;
505 	pd_timeout = jiffies + PD_TMO;
506 	return Hold;
507 }
508 
do_pd_write_start(void)509 static enum action do_pd_write_start(void)
510 {
511 	if (pd_wait_for(pd_current, STAT_READY, "do_pd_write") & STAT_ERR) {
512 		if (pd_retries < PD_MAX_RETRIES) {
513 			pd_retries++;
514 			return Wait;
515 		}
516 		return Fail;
517 	}
518 	pd_ide_command(pd_current, IDE_WRITE, pd_block, pd_run);
519 	while (1) {
520 		if (pd_wait_for(pd_current, STAT_DRQ, "do_pd_write_drq") & STAT_ERR) {
521 			if (pd_retries < PD_MAX_RETRIES) {
522 				pd_retries++;
523 				return Wait;
524 			}
525 			return Fail;
526 		}
527 		pi_write_block(pd_current->pi, pd_buf, 512);
528 		if (pd_next_buf())
529 			break;
530 	}
531 	phase = do_pd_write_done;
532 	pd_timeout = jiffies + PD_TMO;
533 	return Hold;
534 }
535 
pd_ready(void)536 static inline int pd_ready(void)
537 {
538 	return !(status_reg(pd_current) & STAT_BUSY);
539 }
540 
do_pd_read_drq(void)541 static enum action do_pd_read_drq(void)
542 {
543 	if (!pd_ready() && !time_after_eq(jiffies, pd_timeout))
544 		return Hold;
545 
546 	while (1) {
547 		if (pd_wait_for(pd_current, STAT_DRQ, "do_pd_read_drq") & STAT_ERR) {
548 			if (pd_retries < PD_MAX_RETRIES) {
549 				pd_retries++;
550 				phase = do_pd_read_start;
551 				return Wait;
552 			}
553 			return Fail;
554 		}
555 		pi_read_block(pd_current->pi, pd_buf, 512);
556 		if (pd_next_buf())
557 			break;
558 	}
559 	return Ok;
560 }
561 
do_pd_write_done(void)562 static enum action do_pd_write_done(void)
563 {
564 	if (!pd_ready() && !time_after_eq(jiffies, pd_timeout))
565 		return Hold;
566 
567 	if (pd_wait_for(pd_current, STAT_READY, "do_pd_write_done") & STAT_ERR) {
568 		if (pd_retries < PD_MAX_RETRIES) {
569 			pd_retries++;
570 			phase = do_pd_write_start;
571 			return Wait;
572 		}
573 		return Fail;
574 	}
575 	return Ok;
576 }
577 
578 /* special io requests */
579 
580 /* According to the ATA standard, the default CHS geometry should be
581    available following a reset.  Some Western Digital drives come up
582    in a mode where only LBA addresses are accepted until the device
583    parameters are initialised.
584 */
585 
pd_init_dev_parms(struct pd_unit * disk)586 static void pd_init_dev_parms(struct pd_unit *disk)
587 {
588 	pd_wait_for(disk, 0, DBMSG("before init_dev_parms"));
589 	pd_send_command(disk, disk->sectors, 0, disk->heads - 1, 0, 0,
590 			IDE_INIT_DEV_PARMS);
591 	udelay(300);
592 	pd_wait_for(disk, 0, "Initialise device parameters");
593 }
594 
pd_door_lock(struct pd_unit * disk)595 static enum action pd_door_lock(struct pd_unit *disk)
596 {
597 	if (!(pd_wait_for(disk, STAT_READY, "Lock") & STAT_ERR)) {
598 		pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORLOCK);
599 		pd_wait_for(disk, STAT_READY, "Lock done");
600 	}
601 	return Ok;
602 }
603 
pd_door_unlock(struct pd_unit * disk)604 static enum action pd_door_unlock(struct pd_unit *disk)
605 {
606 	if (!(pd_wait_for(disk, STAT_READY, "Lock") & STAT_ERR)) {
607 		pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORUNLOCK);
608 		pd_wait_for(disk, STAT_READY, "Lock done");
609 	}
610 	return Ok;
611 }
612 
pd_eject(struct pd_unit * disk)613 static enum action pd_eject(struct pd_unit *disk)
614 {
615 	pd_wait_for(disk, 0, DBMSG("before unlock on eject"));
616 	pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORUNLOCK);
617 	pd_wait_for(disk, 0, DBMSG("after unlock on eject"));
618 	pd_wait_for(disk, 0, DBMSG("before eject"));
619 	pd_send_command(disk, 0, 0, 0, 0, 0, IDE_EJECT);
620 	pd_wait_for(disk, 0, DBMSG("after eject"));
621 	return Ok;
622 }
623 
pd_media_check(struct pd_unit * disk)624 static enum action pd_media_check(struct pd_unit *disk)
625 {
626 	int r = pd_wait_for(disk, STAT_READY, DBMSG("before media_check"));
627 	if (!(r & STAT_ERR)) {
628 		pd_send_command(disk, 1, 1, 0, 0, 0, IDE_READ_VRFY);
629 		r = pd_wait_for(disk, STAT_READY, DBMSG("RDY after READ_VRFY"));
630 	} else
631 		disk->changed = 1;	/* say changed if other error */
632 	if (r & ERR_MC) {
633 		disk->changed = 1;
634 		pd_send_command(disk, 1, 0, 0, 0, 0, IDE_ACKCHANGE);
635 		pd_wait_for(disk, STAT_READY, DBMSG("RDY after ACKCHANGE"));
636 		pd_send_command(disk, 1, 1, 0, 0, 0, IDE_READ_VRFY);
637 		r = pd_wait_for(disk, STAT_READY, DBMSG("RDY after VRFY"));
638 	}
639 	return Ok;
640 }
641 
pd_standby_off(struct pd_unit * disk)642 static void pd_standby_off(struct pd_unit *disk)
643 {
644 	pd_wait_for(disk, 0, DBMSG("before STANDBY"));
645 	pd_send_command(disk, 0, 0, 0, 0, 0, IDE_STANDBY);
646 	pd_wait_for(disk, 0, DBMSG("after STANDBY"));
647 }
648 
pd_identify(struct pd_unit * disk)649 static enum action pd_identify(struct pd_unit *disk)
650 {
651 	int j;
652 	char id[PD_ID_LEN + 1];
653 
654 /* WARNING:  here there may be dragons.  reset() applies to both drives,
655    but we call it only on probing the MASTER. This should allow most
656    common configurations to work, but be warned that a reset can clear
657    settings on the SLAVE drive.
658 */
659 
660 	if (disk->drive == 0)
661 		pd_reset(disk);
662 
663 	write_reg(disk, 6, DRIVE(disk));
664 	pd_wait_for(disk, 0, DBMSG("before IDENT"));
665 	pd_send_command(disk, 1, 0, 0, 0, 0, IDE_IDENTIFY);
666 
667 	if (pd_wait_for(disk, STAT_DRQ, DBMSG("IDENT DRQ")) & STAT_ERR)
668 		return Fail;
669 	pi_read_block(disk->pi, pd_scratch, 512);
670 	disk->can_lba = pd_scratch[99] & 2;
671 	disk->sectors = le16_to_cpu(*(__le16 *) (pd_scratch + 12));
672 	disk->heads = le16_to_cpu(*(__le16 *) (pd_scratch + 6));
673 	disk->cylinders = le16_to_cpu(*(__le16 *) (pd_scratch + 2));
674 	if (disk->can_lba)
675 		disk->capacity = le32_to_cpu(*(__le32 *) (pd_scratch + 120));
676 	else
677 		disk->capacity = disk->sectors * disk->heads * disk->cylinders;
678 
679 	for (j = 0; j < PD_ID_LEN; j++)
680 		id[j ^ 1] = pd_scratch[j + PD_ID_OFF];
681 	j = PD_ID_LEN - 1;
682 	while ((j >= 0) && (id[j] <= 0x20))
683 		j--;
684 	j++;
685 	id[j] = 0;
686 
687 	disk->removable = pd_scratch[0] & 0x80;
688 
689 	printk("%s: %s, %s, %d blocks [%dM], (%d/%d/%d), %s media\n",
690 	       disk->name, id,
691 	       disk->drive ? "slave" : "master",
692 	       disk->capacity, disk->capacity / 2048,
693 	       disk->cylinders, disk->heads, disk->sectors,
694 	       disk->removable ? "removable" : "fixed");
695 
696 	if (disk->capacity)
697 		pd_init_dev_parms(disk);
698 	if (!disk->standby)
699 		pd_standby_off(disk);
700 
701 	return Ok;
702 }
703 
704 /* end of io request engine */
705 
do_pd_request(struct request_queue * q)706 static void do_pd_request(struct request_queue * q)
707 {
708 	if (pd_req)
709 		return;
710 	pd_req = blk_fetch_request(q);
711 	if (!pd_req)
712 		return;
713 
714 	schedule_fsm();
715 }
716 
pd_special_command(struct pd_unit * disk,enum action (* func)(struct pd_unit * disk))717 static int pd_special_command(struct pd_unit *disk,
718 		      enum action (*func)(struct pd_unit *disk))
719 {
720 	struct request *rq;
721 	int err = 0;
722 
723 	rq = blk_get_request(disk->gd->queue, READ, __GFP_WAIT);
724 
725 	rq->cmd_type = REQ_TYPE_SPECIAL;
726 	rq->special = func;
727 
728 	err = blk_execute_rq(disk->gd->queue, disk->gd, rq, 0);
729 
730 	blk_put_request(rq);
731 	return err;
732 }
733 
734 /* kernel glue structures */
735 
pd_open(struct block_device * bdev,fmode_t mode)736 static int pd_open(struct block_device *bdev, fmode_t mode)
737 {
738 	struct pd_unit *disk = bdev->bd_disk->private_data;
739 
740 	mutex_lock(&pd_mutex);
741 	disk->access++;
742 
743 	if (disk->removable) {
744 		pd_special_command(disk, pd_media_check);
745 		pd_special_command(disk, pd_door_lock);
746 	}
747 	mutex_unlock(&pd_mutex);
748 	return 0;
749 }
750 
pd_getgeo(struct block_device * bdev,struct hd_geometry * geo)751 static int pd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
752 {
753 	struct pd_unit *disk = bdev->bd_disk->private_data;
754 
755 	if (disk->alt_geom) {
756 		geo->heads = PD_LOG_HEADS;
757 		geo->sectors = PD_LOG_SECTS;
758 		geo->cylinders = disk->capacity / (geo->heads * geo->sectors);
759 	} else {
760 		geo->heads = disk->heads;
761 		geo->sectors = disk->sectors;
762 		geo->cylinders = disk->cylinders;
763 	}
764 
765 	return 0;
766 }
767 
pd_ioctl(struct block_device * bdev,fmode_t mode,unsigned int cmd,unsigned long arg)768 static int pd_ioctl(struct block_device *bdev, fmode_t mode,
769 	 unsigned int cmd, unsigned long arg)
770 {
771 	struct pd_unit *disk = bdev->bd_disk->private_data;
772 
773 	switch (cmd) {
774 	case CDROMEJECT:
775 		mutex_lock(&pd_mutex);
776 		if (disk->access == 1)
777 			pd_special_command(disk, pd_eject);
778 		mutex_unlock(&pd_mutex);
779 		return 0;
780 	default:
781 		return -EINVAL;
782 	}
783 }
784 
pd_release(struct gendisk * p,fmode_t mode)785 static int pd_release(struct gendisk *p, fmode_t mode)
786 {
787 	struct pd_unit *disk = p->private_data;
788 
789 	mutex_lock(&pd_mutex);
790 	if (!--disk->access && disk->removable)
791 		pd_special_command(disk, pd_door_unlock);
792 	mutex_unlock(&pd_mutex);
793 
794 	return 0;
795 }
796 
pd_check_events(struct gendisk * p,unsigned int clearing)797 static unsigned int pd_check_events(struct gendisk *p, unsigned int clearing)
798 {
799 	struct pd_unit *disk = p->private_data;
800 	int r;
801 	if (!disk->removable)
802 		return 0;
803 	pd_special_command(disk, pd_media_check);
804 	r = disk->changed;
805 	disk->changed = 0;
806 	return r ? DISK_EVENT_MEDIA_CHANGE : 0;
807 }
808 
pd_revalidate(struct gendisk * p)809 static int pd_revalidate(struct gendisk *p)
810 {
811 	struct pd_unit *disk = p->private_data;
812 	if (pd_special_command(disk, pd_identify) == 0)
813 		set_capacity(p, disk->capacity);
814 	else
815 		set_capacity(p, 0);
816 	return 0;
817 }
818 
819 static const struct block_device_operations pd_fops = {
820 	.owner		= THIS_MODULE,
821 	.open		= pd_open,
822 	.release	= pd_release,
823 	.ioctl		= pd_ioctl,
824 	.getgeo		= pd_getgeo,
825 	.check_events	= pd_check_events,
826 	.revalidate_disk= pd_revalidate
827 };
828 
829 /* probing */
830 
pd_probe_drive(struct pd_unit * disk)831 static void pd_probe_drive(struct pd_unit *disk)
832 {
833 	struct gendisk *p = alloc_disk(1 << PD_BITS);
834 	if (!p)
835 		return;
836 	strcpy(p->disk_name, disk->name);
837 	p->fops = &pd_fops;
838 	p->major = major;
839 	p->first_minor = (disk - pd) << PD_BITS;
840 	disk->gd = p;
841 	p->private_data = disk;
842 	p->queue = pd_queue;
843 
844 	if (disk->drive == -1) {
845 		for (disk->drive = 0; disk->drive <= 1; disk->drive++)
846 			if (pd_special_command(disk, pd_identify) == 0)
847 				return;
848 	} else if (pd_special_command(disk, pd_identify) == 0)
849 		return;
850 	disk->gd = NULL;
851 	put_disk(p);
852 }
853 
pd_detect(void)854 static int pd_detect(void)
855 {
856 	int found = 0, unit, pd_drive_count = 0;
857 	struct pd_unit *disk;
858 
859 	for (unit = 0; unit < PD_UNITS; unit++) {
860 		int *parm = *drives[unit];
861 		struct pd_unit *disk = pd + unit;
862 		disk->pi = &disk->pia;
863 		disk->access = 0;
864 		disk->changed = 1;
865 		disk->capacity = 0;
866 		disk->drive = parm[D_SLV];
867 		snprintf(disk->name, PD_NAMELEN, "%s%c", name, 'a'+unit);
868 		disk->alt_geom = parm[D_GEO];
869 		disk->standby = parm[D_SBY];
870 		if (parm[D_PRT])
871 			pd_drive_count++;
872 	}
873 
874 	if (pd_drive_count == 0) { /* nothing spec'd - so autoprobe for 1 */
875 		disk = pd;
876 		if (pi_init(disk->pi, 1, -1, -1, -1, -1, -1, pd_scratch,
877 			    PI_PD, verbose, disk->name)) {
878 			pd_probe_drive(disk);
879 			if (!disk->gd)
880 				pi_release(disk->pi);
881 		}
882 
883 	} else {
884 		for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
885 			int *parm = *drives[unit];
886 			if (!parm[D_PRT])
887 				continue;
888 			if (pi_init(disk->pi, 0, parm[D_PRT], parm[D_MOD],
889 				     parm[D_UNI], parm[D_PRO], parm[D_DLY],
890 				     pd_scratch, PI_PD, verbose, disk->name)) {
891 				pd_probe_drive(disk);
892 				if (!disk->gd)
893 					pi_release(disk->pi);
894 			}
895 		}
896 	}
897 	for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
898 		if (disk->gd) {
899 			set_capacity(disk->gd, disk->capacity);
900 			add_disk(disk->gd);
901 			found = 1;
902 		}
903 	}
904 	if (!found)
905 		printk("%s: no valid drive found\n", name);
906 	return found;
907 }
908 
pd_init(void)909 static int __init pd_init(void)
910 {
911 	if (disable)
912 		goto out1;
913 
914 	pd_queue = blk_init_queue(do_pd_request, &pd_lock);
915 	if (!pd_queue)
916 		goto out1;
917 
918 	blk_queue_max_hw_sectors(pd_queue, cluster);
919 
920 	if (register_blkdev(major, name))
921 		goto out2;
922 
923 	printk("%s: %s version %s, major %d, cluster %d, nice %d\n",
924 	       name, name, PD_VERSION, major, cluster, nice);
925 	if (!pd_detect())
926 		goto out3;
927 
928 	return 0;
929 
930 out3:
931 	unregister_blkdev(major, name);
932 out2:
933 	blk_cleanup_queue(pd_queue);
934 out1:
935 	return -ENODEV;
936 }
937 
pd_exit(void)938 static void __exit pd_exit(void)
939 {
940 	struct pd_unit *disk;
941 	int unit;
942 	unregister_blkdev(major, name);
943 	for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
944 		struct gendisk *p = disk->gd;
945 		if (p) {
946 			disk->gd = NULL;
947 			del_gendisk(p);
948 			put_disk(p);
949 			pi_release(disk->pi);
950 		}
951 	}
952 	blk_cleanup_queue(pd_queue);
953 }
954 
955 MODULE_LICENSE("GPL");
956 module_init(pd_init)
957 module_exit(pd_exit)
958