blob: 9cfd2e06a64917a99a3f70da9753cbc8bd27401c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Masahiro Yamada505d3082017-03-09 16:16:33 -080064 major You may use this parameter to override the
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 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*/
Rusty Russell90ab5ee2012-01-13 09:32:20 +1030127#include <linux/types.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Arnd Bergmanndec63a42016-03-15 14:53:29 -0700129static int verbose = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130static int major = PD_MAJOR;
131static char *name = PD_NAME;
132static int cluster = 64;
133static int nice = 0;
134static int disable = 0;
135
136static int drive0[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
137static int drive1[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
138static int drive2[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
139static int drive3[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
140
141static int (*drives[4])[8] = {&drive0, &drive1, &drive2, &drive3};
142
143enum {D_PRT, D_PRO, D_UNI, D_MOD, D_GEO, D_SBY, D_DLY, D_SLV};
144
145/* end of parameters */
146
147#include <linux/init.h>
148#include <linux/module.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +0900149#include <linux/gfp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150#include <linux/fs.h>
151#include <linux/delay.h>
152#include <linux/hdreg.h>
153#include <linux/cdrom.h> /* for the eject ioctl */
154#include <linux/blkdev.h>
155#include <linux/blkpg.h>
Adrian Bunk3b717972006-03-28 01:56:25 -0800156#include <linux/kernel.h>
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200157#include <linux/mutex.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -0800158#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159#include <linux/workqueue.h>
160
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200161static DEFINE_MUTEX(pd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162static DEFINE_SPINLOCK(pd_lock);
163
Arnd Bergmanndec63a42016-03-15 14:53:29 -0700164module_param(verbose, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165module_param(major, int, 0);
166module_param(name, charp, 0);
167module_param(cluster, int, 0);
168module_param(nice, int, 0);
169module_param_array(drive0, int, NULL, 0);
170module_param_array(drive1, int, NULL, 0);
171module_param_array(drive2, int, NULL, 0);
172module_param_array(drive3, int, NULL, 0);
173
174#include "paride.h"
175
176#define PD_BITS 4
177
178/* numbers for "SCSI" geometry */
179
180#define PD_LOG_HEADS 64
181#define PD_LOG_SECTS 32
182
183#define PD_ID_OFF 54
184#define PD_ID_LEN 14
185
186#define PD_MAX_RETRIES 5
187#define PD_TMO 800 /* interrupt timeout in jiffies */
188#define PD_SPIN_DEL 50 /* spin delay in micro-seconds */
189
190#define PD_SPIN (1000000*PD_TMO)/(HZ*PD_SPIN_DEL)
191
192#define STAT_ERR 0x00001
193#define STAT_INDEX 0x00002
194#define STAT_ECC 0x00004
195#define STAT_DRQ 0x00008
196#define STAT_SEEK 0x00010
197#define STAT_WRERR 0x00020
198#define STAT_READY 0x00040
199#define STAT_BUSY 0x00080
200
201#define ERR_AMNF 0x00100
202#define ERR_TK0NF 0x00200
203#define ERR_ABRT 0x00400
204#define ERR_MCR 0x00800
205#define ERR_IDNF 0x01000
206#define ERR_MC 0x02000
207#define ERR_UNC 0x04000
208#define ERR_TMO 0x10000
209
210#define IDE_READ 0x20
211#define IDE_WRITE 0x30
212#define IDE_READ_VRFY 0x40
213#define IDE_INIT_DEV_PARMS 0x91
214#define IDE_STANDBY 0x96
215#define IDE_ACKCHANGE 0xdb
216#define IDE_DOORLOCK 0xde
217#define IDE_DOORUNLOCK 0xdf
218#define IDE_IDENTIFY 0xec
219#define IDE_EJECT 0xed
220
221#define PD_NAMELEN 8
222
223struct pd_unit {
224 struct pi_adapter pia; /* interface to paride layer */
225 struct pi_adapter *pi;
226 int access; /* count of active opens ... */
227 int capacity; /* Size of this volume in sectors */
228 int heads; /* physical geometry */
229 int sectors;
230 int cylinders;
231 int can_lba;
232 int drive; /* master=0 slave=1 */
233 int changed; /* Have we seen a disk change ? */
234 int removable; /* removable media device ? */
235 int standby;
236 int alt_geom;
237 char name[PD_NAMELEN]; /* pda, pdb, etc ... */
238 struct gendisk *gd;
239};
240
241static struct pd_unit pd[PD_UNITS];
242
243static char pd_scratch[512]; /* scratch block buffer */
244
245static char *pd_errs[17] = { "ERR", "INDEX", "ECC", "DRQ", "SEEK", "WRERR",
246 "READY", "BUSY", "AMNF", "TK0NF", "ABRT", "MCR",
247 "IDNF", "MC", "UNC", "???", "TMO"
248};
249
Sudip Mukherjee9f4ba6b2015-05-20 20:57:01 +0530250static void *par_drv; /* reference of parport driver */
251
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252static inline int status_reg(struct pd_unit *disk)
253{
254 return pi_read_regr(disk->pi, 1, 6);
255}
256
257static inline int read_reg(struct pd_unit *disk, int reg)
258{
259 return pi_read_regr(disk->pi, 0, reg);
260}
261
262static inline void write_status(struct pd_unit *disk, int val)
263{
264 pi_write_regr(disk->pi, 1, 6, val);
265}
266
267static inline void write_reg(struct pd_unit *disk, int reg, int val)
268{
269 pi_write_regr(disk->pi, 0, reg, val);
270}
271
272static inline u8 DRIVE(struct pd_unit *disk)
273{
274 return 0xa0+0x10*disk->drive;
275}
276
277/* ide command interface */
278
279static void pd_print_error(struct pd_unit *disk, char *msg, int status)
280{
281 int i;
282
283 printk("%s: %s: status = 0x%x =", disk->name, msg, status);
Adrian Bunk3b717972006-03-28 01:56:25 -0800284 for (i = 0; i < ARRAY_SIZE(pd_errs); i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 if (status & (1 << i))
286 printk(" %s", pd_errs[i]);
287 printk("\n");
288}
289
290static void pd_reset(struct pd_unit *disk)
291{ /* called only for MASTER drive */
292 write_status(disk, 4);
293 udelay(50);
294 write_status(disk, 0);
295 udelay(250);
296}
297
298#define DBMSG(msg) ((verbose>1)?(msg):NULL)
299
300static int pd_wait_for(struct pd_unit *disk, int w, char *msg)
301{ /* polled wait */
302 int k, r, e;
303
304 k = 0;
305 while (k < PD_SPIN) {
306 r = status_reg(disk);
307 k++;
308 if (((r & w) == w) && !(r & STAT_BUSY))
309 break;
310 udelay(PD_SPIN_DEL);
311 }
312 e = (read_reg(disk, 1) << 8) + read_reg(disk, 7);
313 if (k >= PD_SPIN)
314 e |= ERR_TMO;
315 if ((e & (STAT_ERR | ERR_TMO)) && (msg != NULL))
316 pd_print_error(disk, msg, e);
317 return e;
318}
319
320static void pd_send_command(struct pd_unit *disk, int n, int s, int h, int c0, int c1, int func)
321{
322 write_reg(disk, 6, DRIVE(disk) + h);
323 write_reg(disk, 1, 0); /* the IDE task file */
324 write_reg(disk, 2, n);
325 write_reg(disk, 3, s);
326 write_reg(disk, 4, c0);
327 write_reg(disk, 5, c1);
328 write_reg(disk, 7, func);
329
330 udelay(1);
331}
332
333static void pd_ide_command(struct pd_unit *disk, int func, int block, int count)
334{
335 int c1, c0, h, s;
336
337 if (disk->can_lba) {
338 s = block & 255;
339 c0 = (block >>= 8) & 255;
340 c1 = (block >>= 8) & 255;
341 h = ((block >>= 8) & 15) + 0x40;
342 } else {
343 s = (block % disk->sectors) + 1;
344 h = (block /= disk->sectors) % disk->heads;
345 c0 = (block /= disk->heads) % 256;
346 c1 = (block >>= 8);
347 }
348 pd_send_command(disk, count, s, h, c0, c1, func);
349}
350
351/* The i/o request engine */
352
353enum action {Fail = 0, Ok = 1, Hold, Wait};
354
355static struct request *pd_req; /* current request */
356static enum action (*phase)(void);
357
358static void run_fsm(void);
359
David Howellsc4028952006-11-22 14:57:56 +0000360static void ps_tq_int(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
David Howellsc4028952006-11-22 14:57:56 +0000362static DECLARE_DELAYED_WORK(fsm_tq, ps_tq_int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
364static void schedule_fsm(void)
365{
366 if (!nice)
David Howellsc4028952006-11-22 14:57:56 +0000367 schedule_delayed_work(&fsm_tq, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 else
369 schedule_delayed_work(&fsm_tq, nice-1);
370}
371
David Howellsc4028952006-11-22 14:57:56 +0000372static void ps_tq_int(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373{
374 run_fsm();
375}
376
377static enum action do_pd_io_start(void);
378static enum action pd_special(void);
379static enum action do_pd_read_start(void);
380static enum action do_pd_write_start(void);
381static enum action do_pd_read_drq(void);
382static enum action do_pd_write_done(void);
383
384static struct request_queue *pd_queue;
385static int pd_claimed;
386
387static struct pd_unit *pd_current; /* current request's drive */
388static PIA *pi_current; /* current request's PIA */
389
390static void run_fsm(void)
391{
392 while (1) {
393 enum action res;
394 unsigned long saved_flags;
395 int stop = 0;
396
397 if (!phase) {
398 pd_current = pd_req->rq_disk->private_data;
399 pi_current = pd_current->pi;
400 phase = do_pd_io_start;
401 }
402
403 switch (pd_claimed) {
404 case 0:
405 pd_claimed = 1;
406 if (!pi_schedule_claimed(pi_current, run_fsm))
407 return;
408 case 1:
409 pd_claimed = 2;
410 pi_current->proto->connect(pi_current);
411 }
412
413 switch(res = phase()) {
414 case Ok: case Fail:
415 pi_disconnect(pi_current);
416 pd_claimed = 0;
417 phase = NULL;
418 spin_lock_irqsave(&pd_lock, saved_flags);
Tejun Heob12d4f82009-05-08 11:54:06 +0900419 if (!__blk_end_request_cur(pd_req,
420 res == Ok ? 0 : -EIO)) {
Tejun Heo9934c8c2009-05-08 11:54:16 +0900421 pd_req = blk_fetch_request(pd_queue);
Tejun Heob12d4f82009-05-08 11:54:06 +0900422 if (!pd_req)
423 stop = 1;
Tejun Heob12d4f82009-05-08 11:54:06 +0900424 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 spin_unlock_irqrestore(&pd_lock, saved_flags);
426 if (stop)
427 return;
428 case Hold:
429 schedule_fsm();
430 return;
431 case Wait:
432 pi_disconnect(pi_current);
433 pd_claimed = 0;
434 }
435 }
436}
437
438static int pd_retries = 0; /* i/o error retry count */
439static int pd_block; /* address of next requested block */
440static int pd_count; /* number of blocks still to do */
441static int pd_run; /* sectors in current cluster */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442static char *pd_buf; /* buffer for request in progress */
443
444static enum action do_pd_io_start(void)
445{
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100446 switch (req_op(pd_req)) {
447 case REQ_OP_DRV_IN:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 phase = pd_special;
449 return pd_special();
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100450 case REQ_OP_READ:
451 case REQ_OP_WRITE:
Tejun Heo83096eb2009-05-07 22:24:39 +0900452 pd_block = blk_rq_pos(pd_req);
453 pd_count = blk_rq_cur_sectors(pd_req);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (pd_block + pd_count > get_capacity(pd_req->rq_disk))
455 return Fail;
Tejun Heo83096eb2009-05-07 22:24:39 +0900456 pd_run = blk_rq_sectors(pd_req);
Jens Axboeb4f42e22014-04-10 09:46:28 -0600457 pd_buf = bio_data(pd_req->bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 pd_retries = 0;
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100459 if (req_op(pd_req) == REQ_OP_READ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 return do_pd_read_start();
461 else
462 return do_pd_write_start();
463 }
464 return Fail;
465}
466
467static enum action pd_special(void)
468{
469 enum action (*func)(struct pd_unit *) = pd_req->special;
470 return func(pd_current);
471}
472
473static int pd_next_buf(void)
474{
475 unsigned long saved_flags;
476
477 pd_count--;
478 pd_run--;
479 pd_buf += 512;
480 pd_block++;
481 if (!pd_run)
482 return 1;
483 if (pd_count)
484 return 0;
485 spin_lock_irqsave(&pd_lock, saved_flags);
Tejun Heof06d9a22009-04-23 11:05:19 +0900486 __blk_end_request_cur(pd_req, 0);
Tejun Heo83096eb2009-05-07 22:24:39 +0900487 pd_count = blk_rq_cur_sectors(pd_req);
Jens Axboeb4f42e22014-04-10 09:46:28 -0600488 pd_buf = bio_data(pd_req->bio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 spin_unlock_irqrestore(&pd_lock, saved_flags);
490 return 0;
491}
492
493static unsigned long pd_timeout;
494
495static enum action do_pd_read_start(void)
496{
497 if (pd_wait_for(pd_current, STAT_READY, "do_pd_read") & STAT_ERR) {
498 if (pd_retries < PD_MAX_RETRIES) {
499 pd_retries++;
500 return Wait;
501 }
502 return Fail;
503 }
504 pd_ide_command(pd_current, IDE_READ, pd_block, pd_run);
505 phase = do_pd_read_drq;
506 pd_timeout = jiffies + PD_TMO;
507 return Hold;
508}
509
510static enum action do_pd_write_start(void)
511{
512 if (pd_wait_for(pd_current, STAT_READY, "do_pd_write") & STAT_ERR) {
513 if (pd_retries < PD_MAX_RETRIES) {
514 pd_retries++;
515 return Wait;
516 }
517 return Fail;
518 }
519 pd_ide_command(pd_current, IDE_WRITE, pd_block, pd_run);
520 while (1) {
521 if (pd_wait_for(pd_current, STAT_DRQ, "do_pd_write_drq") & STAT_ERR) {
522 if (pd_retries < PD_MAX_RETRIES) {
523 pd_retries++;
524 return Wait;
525 }
526 return Fail;
527 }
528 pi_write_block(pd_current->pi, pd_buf, 512);
529 if (pd_next_buf())
530 break;
531 }
532 phase = do_pd_write_done;
533 pd_timeout = jiffies + PD_TMO;
534 return Hold;
535}
536
537static inline int pd_ready(void)
538{
539 return !(status_reg(pd_current) & STAT_BUSY);
540}
541
542static enum action do_pd_read_drq(void)
543{
544 if (!pd_ready() && !time_after_eq(jiffies, pd_timeout))
545 return Hold;
546
547 while (1) {
548 if (pd_wait_for(pd_current, STAT_DRQ, "do_pd_read_drq") & STAT_ERR) {
549 if (pd_retries < PD_MAX_RETRIES) {
550 pd_retries++;
551 phase = do_pd_read_start;
552 return Wait;
553 }
554 return Fail;
555 }
556 pi_read_block(pd_current->pi, pd_buf, 512);
557 if (pd_next_buf())
558 break;
559 }
560 return Ok;
561}
562
563static enum action do_pd_write_done(void)
564{
565 if (!pd_ready() && !time_after_eq(jiffies, pd_timeout))
566 return Hold;
567
568 if (pd_wait_for(pd_current, STAT_READY, "do_pd_write_done") & STAT_ERR) {
569 if (pd_retries < PD_MAX_RETRIES) {
570 pd_retries++;
571 phase = do_pd_write_start;
572 return Wait;
573 }
574 return Fail;
575 }
576 return Ok;
577}
578
579/* special io requests */
580
581/* According to the ATA standard, the default CHS geometry should be
582 available following a reset. Some Western Digital drives come up
583 in a mode where only LBA addresses are accepted until the device
584 parameters are initialised.
585*/
586
587static void pd_init_dev_parms(struct pd_unit *disk)
588{
589 pd_wait_for(disk, 0, DBMSG("before init_dev_parms"));
590 pd_send_command(disk, disk->sectors, 0, disk->heads - 1, 0, 0,
591 IDE_INIT_DEV_PARMS);
592 udelay(300);
593 pd_wait_for(disk, 0, "Initialise device parameters");
594}
595
596static enum action pd_door_lock(struct pd_unit *disk)
597{
598 if (!(pd_wait_for(disk, STAT_READY, "Lock") & STAT_ERR)) {
599 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORLOCK);
600 pd_wait_for(disk, STAT_READY, "Lock done");
601 }
602 return Ok;
603}
604
605static enum action pd_door_unlock(struct pd_unit *disk)
606{
607 if (!(pd_wait_for(disk, STAT_READY, "Lock") & STAT_ERR)) {
608 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORUNLOCK);
609 pd_wait_for(disk, STAT_READY, "Lock done");
610 }
611 return Ok;
612}
613
614static enum action pd_eject(struct pd_unit *disk)
615{
616 pd_wait_for(disk, 0, DBMSG("before unlock on eject"));
617 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORUNLOCK);
618 pd_wait_for(disk, 0, DBMSG("after unlock on eject"));
619 pd_wait_for(disk, 0, DBMSG("before eject"));
620 pd_send_command(disk, 0, 0, 0, 0, 0, IDE_EJECT);
621 pd_wait_for(disk, 0, DBMSG("after eject"));
622 return Ok;
623}
624
625static enum action pd_media_check(struct pd_unit *disk)
626{
627 int r = pd_wait_for(disk, STAT_READY, DBMSG("before media_check"));
628 if (!(r & STAT_ERR)) {
629 pd_send_command(disk, 1, 1, 0, 0, 0, IDE_READ_VRFY);
630 r = pd_wait_for(disk, STAT_READY, DBMSG("RDY after READ_VRFY"));
631 } else
632 disk->changed = 1; /* say changed if other error */
633 if (r & ERR_MC) {
634 disk->changed = 1;
635 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_ACKCHANGE);
636 pd_wait_for(disk, STAT_READY, DBMSG("RDY after ACKCHANGE"));
637 pd_send_command(disk, 1, 1, 0, 0, 0, IDE_READ_VRFY);
638 r = pd_wait_for(disk, STAT_READY, DBMSG("RDY after VRFY"));
639 }
640 return Ok;
641}
642
643static void pd_standby_off(struct pd_unit *disk)
644{
645 pd_wait_for(disk, 0, DBMSG("before STANDBY"));
646 pd_send_command(disk, 0, 0, 0, 0, 0, IDE_STANDBY);
647 pd_wait_for(disk, 0, DBMSG("after STANDBY"));
648}
649
650static enum action pd_identify(struct pd_unit *disk)
651{
652 int j;
653 char id[PD_ID_LEN + 1];
654
655/* WARNING: here there may be dragons. reset() applies to both drives,
656 but we call it only on probing the MASTER. This should allow most
657 common configurations to work, but be warned that a reset can clear
658 settings on the SLAVE drive.
659*/
660
661 if (disk->drive == 0)
662 pd_reset(disk);
663
664 write_reg(disk, 6, DRIVE(disk));
665 pd_wait_for(disk, 0, DBMSG("before IDENT"));
666 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_IDENTIFY);
667
668 if (pd_wait_for(disk, STAT_DRQ, DBMSG("IDENT DRQ")) & STAT_ERR)
669 return Fail;
670 pi_read_block(disk->pi, pd_scratch, 512);
671 disk->can_lba = pd_scratch[99] & 2;
Al Viro27d87182007-03-14 09:20:30 +0000672 disk->sectors = le16_to_cpu(*(__le16 *) (pd_scratch + 12));
673 disk->heads = le16_to_cpu(*(__le16 *) (pd_scratch + 6));
674 disk->cylinders = le16_to_cpu(*(__le16 *) (pd_scratch + 2));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 if (disk->can_lba)
Al Viro27d87182007-03-14 09:20:30 +0000676 disk->capacity = le32_to_cpu(*(__le32 *) (pd_scratch + 120));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 else
678 disk->capacity = disk->sectors * disk->heads * disk->cylinders;
679
680 for (j = 0; j < PD_ID_LEN; j++)
681 id[j ^ 1] = pd_scratch[j + PD_ID_OFF];
682 j = PD_ID_LEN - 1;
683 while ((j >= 0) && (id[j] <= 0x20))
684 j--;
685 j++;
686 id[j] = 0;
687
688 disk->removable = pd_scratch[0] & 0x80;
689
690 printk("%s: %s, %s, %d blocks [%dM], (%d/%d/%d), %s media\n",
691 disk->name, id,
692 disk->drive ? "slave" : "master",
693 disk->capacity, disk->capacity / 2048,
694 disk->cylinders, disk->heads, disk->sectors,
695 disk->removable ? "removable" : "fixed");
696
697 if (disk->capacity)
698 pd_init_dev_parms(disk);
699 if (!disk->standby)
700 pd_standby_off(disk);
701
702 return Ok;
703}
704
705/* end of io request engine */
706
Jens Axboe165125e2007-07-24 09:28:11 +0200707static void do_pd_request(struct request_queue * q)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708{
709 if (pd_req)
710 return;
Tejun Heo9934c8c2009-05-08 11:54:16 +0900711 pd_req = blk_fetch_request(q);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (!pd_req)
713 return;
714
715 schedule_fsm();
716}
717
718static int pd_special_command(struct pd_unit *disk,
719 enum action (*func)(struct pd_unit *disk))
720{
FUJITA Tomonorid79c5a62008-07-15 21:21:45 +0200721 struct request *rq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 int err = 0;
723
Christoph Hellwigaebf5262017-01-31 16:57:31 +0100724 rq = blk_get_request(disk->gd->queue, REQ_OP_DRV_IN, __GFP_RECLAIM);
Joe Lawrencea492f072014-08-28 08:15:21 -0600725 if (IS_ERR(rq))
726 return PTR_ERR(rq);
FUJITA Tomonorid79c5a62008-07-15 21:21:45 +0200727
FUJITA Tomonorid79c5a62008-07-15 21:21:45 +0200728 rq->special = func;
729
730 err = blk_execute_rq(disk->gd->queue, disk->gd, rq, 0);
731
732 blk_put_request(rq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return err;
734}
735
736/* kernel glue structures */
737
Al Virob6a89532008-03-02 09:35:54 -0500738static int pd_open(struct block_device *bdev, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739{
Al Virob6a89532008-03-02 09:35:54 -0500740 struct pd_unit *disk = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200742 mutex_lock(&pd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 disk->access++;
744
745 if (disk->removable) {
746 pd_special_command(disk, pd_media_check);
747 pd_special_command(disk, pd_door_lock);
748 }
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200749 mutex_unlock(&pd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 return 0;
751}
752
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800753static int pd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
754{
755 struct pd_unit *disk = bdev->bd_disk->private_data;
756
757 if (disk->alt_geom) {
758 geo->heads = PD_LOG_HEADS;
759 geo->sectors = PD_LOG_SECTS;
760 geo->cylinders = disk->capacity / (geo->heads * geo->sectors);
761 } else {
762 geo->heads = disk->heads;
763 geo->sectors = disk->sectors;
764 geo->cylinders = disk->cylinders;
765 }
766
767 return 0;
768}
769
Al Virob6a89532008-03-02 09:35:54 -0500770static int pd_ioctl(struct block_device *bdev, fmode_t mode,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 unsigned int cmd, unsigned long arg)
772{
Al Virob6a89532008-03-02 09:35:54 -0500773 struct pd_unit *disk = bdev->bd_disk->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 switch (cmd) {
776 case CDROMEJECT:
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200777 mutex_lock(&pd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 if (disk->access == 1)
779 pd_special_command(disk, pd_eject);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200780 mutex_unlock(&pd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 default:
783 return -EINVAL;
784 }
785}
786
Al Virodb2a1442013-05-05 21:52:57 -0400787static void pd_release(struct gendisk *p, fmode_t mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
Al Virob6a89532008-03-02 09:35:54 -0500789 struct pd_unit *disk = p->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200791 mutex_lock(&pd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 if (!--disk->access && disk->removable)
793 pd_special_command(disk, pd_door_unlock);
Arnd Bergmann2a48fc02010-06-02 14:28:52 +0200794 mutex_unlock(&pd_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
796
Tejun Heob1b56b92011-03-09 19:54:28 +0100797static unsigned int pd_check_events(struct gendisk *p, unsigned int clearing)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
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;
Tejun Heob1b56b92011-03-09 19:54:28 +0100806 return r ? DISK_EVENT_MEDIA_CHANGE : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
809static 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
Alexey Dobriyan83d5cde2009-09-21 17:01:13 -0700819static const struct block_device_operations pd_fops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 .owner = THIS_MODULE,
Al Virob6a89532008-03-02 09:35:54 -0500821 .open = pd_open,
822 .release = pd_release,
Arnd Bergmann8a6cfeb2010-07-08 10:18:46 +0200823 .ioctl = pd_ioctl,
Christoph Hellwiga885c8c2006-01-08 01:02:50 -0800824 .getgeo = pd_getgeo,
Tejun Heob1b56b92011-03-09 19:54:28 +0100825 .check_events = pd_check_events,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 .revalidate_disk= pd_revalidate
827};
828
829/* probing */
830
831static 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
854static 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
Sudip Mukherjee9f4ba6b2015-05-20 20:57:01 +0530874 par_drv = pi_register_driver(name);
875 if (!par_drv) {
876 pr_err("failed to register %s driver\n", name);
877 return -1;
878 }
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if (pd_drive_count == 0) { /* nothing spec'd - so autoprobe for 1 */
881 disk = pd;
882 if (pi_init(disk->pi, 1, -1, -1, -1, -1, -1, pd_scratch,
883 PI_PD, verbose, disk->name)) {
884 pd_probe_drive(disk);
885 if (!disk->gd)
886 pi_release(disk->pi);
887 }
888
889 } else {
890 for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
891 int *parm = *drives[unit];
892 if (!parm[D_PRT])
893 continue;
894 if (pi_init(disk->pi, 0, parm[D_PRT], parm[D_MOD],
895 parm[D_UNI], parm[D_PRO], parm[D_DLY],
896 pd_scratch, PI_PD, verbose, disk->name)) {
897 pd_probe_drive(disk);
898 if (!disk->gd)
899 pi_release(disk->pi);
900 }
901 }
902 }
903 for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
904 if (disk->gd) {
905 set_capacity(disk->gd, disk->capacity);
906 add_disk(disk->gd);
907 found = 1;
908 }
909 }
Sudip Mukherjee9f4ba6b2015-05-20 20:57:01 +0530910 if (!found) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 printk("%s: no valid drive found\n", name);
Sudip Mukherjee9f4ba6b2015-05-20 20:57:01 +0530912 pi_unregister_driver(par_drv);
913 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 return found;
915}
916
917static int __init pd_init(void)
918{
919 if (disable)
920 goto out1;
921
922 pd_queue = blk_init_queue(do_pd_request, &pd_lock);
923 if (!pd_queue)
924 goto out1;
925
Martin K. Petersen086fa5f2010-02-26 00:20:38 -0500926 blk_queue_max_hw_sectors(pd_queue, cluster);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (register_blkdev(major, name))
929 goto out2;
930
931 printk("%s: %s version %s, major %d, cluster %d, nice %d\n",
932 name, name, PD_VERSION, major, cluster, nice);
933 if (!pd_detect())
934 goto out3;
935
936 return 0;
937
938out3:
939 unregister_blkdev(major, name);
940out2:
941 blk_cleanup_queue(pd_queue);
942out1:
943 return -ENODEV;
944}
945
946static void __exit pd_exit(void)
947{
948 struct pd_unit *disk;
949 int unit;
950 unregister_blkdev(major, name);
951 for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
952 struct gendisk *p = disk->gd;
953 if (p) {
954 disk->gd = NULL;
955 del_gendisk(p);
956 put_disk(p);
957 pi_release(disk->pi);
958 }
959 }
960 blk_cleanup_queue(pd_queue);
961}
962
963MODULE_LICENSE("GPL");
964module_init(pd_init)
965module_exit(pd_exit)