0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053
0054
0055
0056
0057
0058
0059
0060
0061
0062
0063
0064
0065
0066
0067
0068
0069
0070
0071
0072
0073
0074
0075
0076
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086
0087
0088
0089
0090
0091
0092
0093
0094
0095
0096
0097
0098
0099
0100
0101
0102
0103
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117 #define PD_VERSION "1.05"
0118 #define PD_MAJOR 45
0119 #define PD_NAME "pd"
0120 #define PD_UNITS 4
0121
0122
0123
0124
0125
0126
0127 #include <linux/types.h>
0128
0129 static int verbose = 0;
0130 static int major = PD_MAJOR;
0131 static char *name = PD_NAME;
0132 static int cluster = 64;
0133 static int nice = 0;
0134 static int disable = 0;
0135
0136 static int drive0[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
0137 static int drive1[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
0138 static int drive2[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
0139 static int drive3[8] = { 0, 0, 0, -1, 0, 1, -1, -1 };
0140
0141 static int (*drives[4])[8] = {&drive0, &drive1, &drive2, &drive3};
0142
0143 enum {D_PRT, D_PRO, D_UNI, D_MOD, D_GEO, D_SBY, D_DLY, D_SLV};
0144
0145
0146
0147 #include <linux/init.h>
0148 #include <linux/module.h>
0149 #include <linux/gfp.h>
0150 #include <linux/fs.h>
0151 #include <linux/delay.h>
0152 #include <linux/hdreg.h>
0153 #include <linux/cdrom.h> /* for the eject ioctl */
0154 #include <linux/blk-mq.h>
0155 #include <linux/blkpg.h>
0156 #include <linux/kernel.h>
0157 #include <linux/mutex.h>
0158 #include <linux/uaccess.h>
0159 #include <linux/workqueue.h>
0160
0161 static DEFINE_MUTEX(pd_mutex);
0162 static DEFINE_SPINLOCK(pd_lock);
0163
0164 module_param(verbose, int, 0);
0165 module_param(major, int, 0);
0166 module_param(name, charp, 0);
0167 module_param(cluster, int, 0);
0168 module_param(nice, int, 0);
0169 module_param_array(drive0, int, NULL, 0);
0170 module_param_array(drive1, int, NULL, 0);
0171 module_param_array(drive2, int, NULL, 0);
0172 module_param_array(drive3, int, NULL, 0);
0173
0174 #include "paride.h"
0175
0176 #define PD_BITS 4
0177
0178
0179
0180 #define PD_LOG_HEADS 64
0181 #define PD_LOG_SECTS 32
0182
0183 #define PD_ID_OFF 54
0184 #define PD_ID_LEN 14
0185
0186 #define PD_MAX_RETRIES 5
0187 #define PD_TMO 800
0188 #define PD_SPIN_DEL 50
0189
0190 #define PD_SPIN (1000000*PD_TMO)/(HZ*PD_SPIN_DEL)
0191
0192 #define STAT_ERR 0x00001
0193 #define STAT_INDEX 0x00002
0194 #define STAT_ECC 0x00004
0195 #define STAT_DRQ 0x00008
0196 #define STAT_SEEK 0x00010
0197 #define STAT_WRERR 0x00020
0198 #define STAT_READY 0x00040
0199 #define STAT_BUSY 0x00080
0200
0201 #define ERR_AMNF 0x00100
0202 #define ERR_TK0NF 0x00200
0203 #define ERR_ABRT 0x00400
0204 #define ERR_MCR 0x00800
0205 #define ERR_IDNF 0x01000
0206 #define ERR_MC 0x02000
0207 #define ERR_UNC 0x04000
0208 #define ERR_TMO 0x10000
0209
0210 #define IDE_READ 0x20
0211 #define IDE_WRITE 0x30
0212 #define IDE_READ_VRFY 0x40
0213 #define IDE_INIT_DEV_PARMS 0x91
0214 #define IDE_STANDBY 0x96
0215 #define IDE_ACKCHANGE 0xdb
0216 #define IDE_DOORLOCK 0xde
0217 #define IDE_DOORUNLOCK 0xdf
0218 #define IDE_IDENTIFY 0xec
0219 #define IDE_EJECT 0xed
0220
0221 #define PD_NAMELEN 8
0222
0223 struct pd_unit {
0224 struct pi_adapter pia;
0225 struct pi_adapter *pi;
0226 int access;
0227 int capacity;
0228 int heads;
0229 int sectors;
0230 int cylinders;
0231 int can_lba;
0232 int drive;
0233 int changed;
0234 int removable;
0235 int standby;
0236 int alt_geom;
0237 char name[PD_NAMELEN];
0238 struct gendisk *gd;
0239 struct blk_mq_tag_set tag_set;
0240 struct list_head rq_list;
0241 };
0242
0243 static struct pd_unit pd[PD_UNITS];
0244
0245 struct pd_req {
0246
0247 enum action (*func)(struct pd_unit *disk);
0248 };
0249
0250 static char pd_scratch[512];
0251
0252 static char *pd_errs[17] = { "ERR", "INDEX", "ECC", "DRQ", "SEEK", "WRERR",
0253 "READY", "BUSY", "AMNF", "TK0NF", "ABRT", "MCR",
0254 "IDNF", "MC", "UNC", "???", "TMO"
0255 };
0256
0257 static void *par_drv;
0258
0259 static inline int status_reg(struct pd_unit *disk)
0260 {
0261 return pi_read_regr(disk->pi, 1, 6);
0262 }
0263
0264 static inline int read_reg(struct pd_unit *disk, int reg)
0265 {
0266 return pi_read_regr(disk->pi, 0, reg);
0267 }
0268
0269 static inline void write_status(struct pd_unit *disk, int val)
0270 {
0271 pi_write_regr(disk->pi, 1, 6, val);
0272 }
0273
0274 static inline void write_reg(struct pd_unit *disk, int reg, int val)
0275 {
0276 pi_write_regr(disk->pi, 0, reg, val);
0277 }
0278
0279 static inline u8 DRIVE(struct pd_unit *disk)
0280 {
0281 return 0xa0+0x10*disk->drive;
0282 }
0283
0284
0285
0286 static void pd_print_error(struct pd_unit *disk, char *msg, int status)
0287 {
0288 int i;
0289
0290 printk("%s: %s: status = 0x%x =", disk->name, msg, status);
0291 for (i = 0; i < ARRAY_SIZE(pd_errs); i++)
0292 if (status & (1 << i))
0293 printk(" %s", pd_errs[i]);
0294 printk("\n");
0295 }
0296
0297 static void pd_reset(struct pd_unit *disk)
0298 {
0299 write_status(disk, 4);
0300 udelay(50);
0301 write_status(disk, 0);
0302 udelay(250);
0303 }
0304
0305 #define DBMSG(msg) ((verbose>1)?(msg):NULL)
0306
0307 static int pd_wait_for(struct pd_unit *disk, int w, char *msg)
0308 {
0309 int k, r, e;
0310
0311 k = 0;
0312 while (k < PD_SPIN) {
0313 r = status_reg(disk);
0314 k++;
0315 if (((r & w) == w) && !(r & STAT_BUSY))
0316 break;
0317 udelay(PD_SPIN_DEL);
0318 }
0319 e = (read_reg(disk, 1) << 8) + read_reg(disk, 7);
0320 if (k >= PD_SPIN)
0321 e |= ERR_TMO;
0322 if ((e & (STAT_ERR | ERR_TMO)) && (msg != NULL))
0323 pd_print_error(disk, msg, e);
0324 return e;
0325 }
0326
0327 static void pd_send_command(struct pd_unit *disk, int n, int s, int h, int c0, int c1, int func)
0328 {
0329 write_reg(disk, 6, DRIVE(disk) + h);
0330 write_reg(disk, 1, 0);
0331 write_reg(disk, 2, n);
0332 write_reg(disk, 3, s);
0333 write_reg(disk, 4, c0);
0334 write_reg(disk, 5, c1);
0335 write_reg(disk, 7, func);
0336
0337 udelay(1);
0338 }
0339
0340 static void pd_ide_command(struct pd_unit *disk, int func, int block, int count)
0341 {
0342 int c1, c0, h, s;
0343
0344 if (disk->can_lba) {
0345 s = block & 255;
0346 c0 = (block >>= 8) & 255;
0347 c1 = (block >>= 8) & 255;
0348 h = ((block >>= 8) & 15) + 0x40;
0349 } else {
0350 s = (block % disk->sectors) + 1;
0351 h = (block /= disk->sectors) % disk->heads;
0352 c0 = (block /= disk->heads) % 256;
0353 c1 = (block >>= 8);
0354 }
0355 pd_send_command(disk, count, s, h, c0, c1, func);
0356 }
0357
0358
0359
0360 enum action {Fail = 0, Ok = 1, Hold, Wait};
0361
0362 static struct request *pd_req;
0363 static enum action (*phase)(void);
0364
0365 static void run_fsm(void);
0366
0367 static void ps_tq_int(struct work_struct *work);
0368
0369 static DECLARE_DELAYED_WORK(fsm_tq, ps_tq_int);
0370
0371 static void schedule_fsm(void)
0372 {
0373 if (!nice)
0374 schedule_delayed_work(&fsm_tq, 0);
0375 else
0376 schedule_delayed_work(&fsm_tq, nice-1);
0377 }
0378
0379 static void ps_tq_int(struct work_struct *work)
0380 {
0381 run_fsm();
0382 }
0383
0384 static enum action do_pd_io_start(void);
0385 static enum action pd_special(void);
0386 static enum action do_pd_read_start(void);
0387 static enum action do_pd_write_start(void);
0388 static enum action do_pd_read_drq(void);
0389 static enum action do_pd_write_done(void);
0390
0391 static int pd_queue;
0392 static int pd_claimed;
0393
0394 static struct pd_unit *pd_current;
0395 static PIA *pi_current;
0396
0397 static int set_next_request(void)
0398 {
0399 struct gendisk *disk;
0400 struct request_queue *q;
0401 int old_pos = pd_queue;
0402
0403 do {
0404 disk = pd[pd_queue].gd;
0405 q = disk ? disk->queue : NULL;
0406 if (++pd_queue == PD_UNITS)
0407 pd_queue = 0;
0408 if (q) {
0409 struct pd_unit *disk = q->queuedata;
0410
0411 if (list_empty(&disk->rq_list))
0412 continue;
0413
0414 pd_req = list_first_entry(&disk->rq_list,
0415 struct request,
0416 queuelist);
0417 list_del_init(&pd_req->queuelist);
0418 blk_mq_start_request(pd_req);
0419 break;
0420 }
0421 } while (pd_queue != old_pos);
0422
0423 return pd_req != NULL;
0424 }
0425
0426 static void run_fsm(void)
0427 {
0428 while (1) {
0429 enum action res;
0430 int stop = 0;
0431
0432 if (!phase) {
0433 pd_current = pd_req->q->disk->private_data;
0434 pi_current = pd_current->pi;
0435 phase = do_pd_io_start;
0436 }
0437
0438 switch (pd_claimed) {
0439 case 0:
0440 pd_claimed = 1;
0441 if (!pi_schedule_claimed(pi_current, run_fsm))
0442 return;
0443 fallthrough;
0444 case 1:
0445 pd_claimed = 2;
0446 pi_current->proto->connect(pi_current);
0447 }
0448
0449 switch(res = phase()) {
0450 case Ok: case Fail: {
0451 blk_status_t err;
0452
0453 err = res == Ok ? 0 : BLK_STS_IOERR;
0454 pi_disconnect(pi_current);
0455 pd_claimed = 0;
0456 phase = NULL;
0457 spin_lock_irq(&pd_lock);
0458 if (!blk_update_request(pd_req, err,
0459 blk_rq_cur_bytes(pd_req))) {
0460 __blk_mq_end_request(pd_req, err);
0461 pd_req = NULL;
0462 stop = !set_next_request();
0463 }
0464 spin_unlock_irq(&pd_lock);
0465 if (stop)
0466 return;
0467 }
0468 fallthrough;
0469 case Hold:
0470 schedule_fsm();
0471 return;
0472 case Wait:
0473 pi_disconnect(pi_current);
0474 pd_claimed = 0;
0475 }
0476 }
0477 }
0478
0479 static int pd_retries = 0;
0480 static int pd_block;
0481 static int pd_count;
0482 static int pd_run;
0483 static char *pd_buf;
0484
0485 static enum action do_pd_io_start(void)
0486 {
0487 switch (req_op(pd_req)) {
0488 case REQ_OP_DRV_IN:
0489 phase = pd_special;
0490 return pd_special();
0491 case REQ_OP_READ:
0492 case REQ_OP_WRITE:
0493 pd_block = blk_rq_pos(pd_req);
0494 pd_count = blk_rq_cur_sectors(pd_req);
0495 if (pd_block + pd_count > get_capacity(pd_req->q->disk))
0496 return Fail;
0497 pd_run = blk_rq_sectors(pd_req);
0498 pd_buf = bio_data(pd_req->bio);
0499 pd_retries = 0;
0500 if (req_op(pd_req) == REQ_OP_READ)
0501 return do_pd_read_start();
0502 else
0503 return do_pd_write_start();
0504 default:
0505 break;
0506 }
0507 return Fail;
0508 }
0509
0510 static enum action pd_special(void)
0511 {
0512 struct pd_req *req = blk_mq_rq_to_pdu(pd_req);
0513
0514 return req->func(pd_current);
0515 }
0516
0517 static int pd_next_buf(void)
0518 {
0519 unsigned long saved_flags;
0520
0521 pd_count--;
0522 pd_run--;
0523 pd_buf += 512;
0524 pd_block++;
0525 if (!pd_run)
0526 return 1;
0527 if (pd_count)
0528 return 0;
0529 spin_lock_irqsave(&pd_lock, saved_flags);
0530 if (!blk_update_request(pd_req, 0, blk_rq_cur_bytes(pd_req))) {
0531 __blk_mq_end_request(pd_req, 0);
0532 pd_req = NULL;
0533 pd_count = 0;
0534 pd_buf = NULL;
0535 } else {
0536 pd_count = blk_rq_cur_sectors(pd_req);
0537 pd_buf = bio_data(pd_req->bio);
0538 }
0539 spin_unlock_irqrestore(&pd_lock, saved_flags);
0540 return !pd_count;
0541 }
0542
0543 static unsigned long pd_timeout;
0544
0545 static enum action do_pd_read_start(void)
0546 {
0547 if (pd_wait_for(pd_current, STAT_READY, "do_pd_read") & STAT_ERR) {
0548 if (pd_retries < PD_MAX_RETRIES) {
0549 pd_retries++;
0550 return Wait;
0551 }
0552 return Fail;
0553 }
0554 pd_ide_command(pd_current, IDE_READ, pd_block, pd_run);
0555 phase = do_pd_read_drq;
0556 pd_timeout = jiffies + PD_TMO;
0557 return Hold;
0558 }
0559
0560 static enum action do_pd_write_start(void)
0561 {
0562 if (pd_wait_for(pd_current, STAT_READY, "do_pd_write") & STAT_ERR) {
0563 if (pd_retries < PD_MAX_RETRIES) {
0564 pd_retries++;
0565 return Wait;
0566 }
0567 return Fail;
0568 }
0569 pd_ide_command(pd_current, IDE_WRITE, pd_block, pd_run);
0570 while (1) {
0571 if (pd_wait_for(pd_current, STAT_DRQ, "do_pd_write_drq") & STAT_ERR) {
0572 if (pd_retries < PD_MAX_RETRIES) {
0573 pd_retries++;
0574 return Wait;
0575 }
0576 return Fail;
0577 }
0578 pi_write_block(pd_current->pi, pd_buf, 512);
0579 if (pd_next_buf())
0580 break;
0581 }
0582 phase = do_pd_write_done;
0583 pd_timeout = jiffies + PD_TMO;
0584 return Hold;
0585 }
0586
0587 static inline int pd_ready(void)
0588 {
0589 return !(status_reg(pd_current) & STAT_BUSY);
0590 }
0591
0592 static enum action do_pd_read_drq(void)
0593 {
0594 if (!pd_ready() && !time_after_eq(jiffies, pd_timeout))
0595 return Hold;
0596
0597 while (1) {
0598 if (pd_wait_for(pd_current, STAT_DRQ, "do_pd_read_drq") & STAT_ERR) {
0599 if (pd_retries < PD_MAX_RETRIES) {
0600 pd_retries++;
0601 phase = do_pd_read_start;
0602 return Wait;
0603 }
0604 return Fail;
0605 }
0606 pi_read_block(pd_current->pi, pd_buf, 512);
0607 if (pd_next_buf())
0608 break;
0609 }
0610 return Ok;
0611 }
0612
0613 static enum action do_pd_write_done(void)
0614 {
0615 if (!pd_ready() && !time_after_eq(jiffies, pd_timeout))
0616 return Hold;
0617
0618 if (pd_wait_for(pd_current, STAT_READY, "do_pd_write_done") & STAT_ERR) {
0619 if (pd_retries < PD_MAX_RETRIES) {
0620 pd_retries++;
0621 phase = do_pd_write_start;
0622 return Wait;
0623 }
0624 return Fail;
0625 }
0626 return Ok;
0627 }
0628
0629
0630
0631
0632
0633
0634
0635
0636
0637 static void pd_init_dev_parms(struct pd_unit *disk)
0638 {
0639 pd_wait_for(disk, 0, DBMSG("before init_dev_parms"));
0640 pd_send_command(disk, disk->sectors, 0, disk->heads - 1, 0, 0,
0641 IDE_INIT_DEV_PARMS);
0642 udelay(300);
0643 pd_wait_for(disk, 0, "Initialise device parameters");
0644 }
0645
0646 static enum action pd_door_lock(struct pd_unit *disk)
0647 {
0648 if (!(pd_wait_for(disk, STAT_READY, "Lock") & STAT_ERR)) {
0649 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORLOCK);
0650 pd_wait_for(disk, STAT_READY, "Lock done");
0651 }
0652 return Ok;
0653 }
0654
0655 static enum action pd_door_unlock(struct pd_unit *disk)
0656 {
0657 if (!(pd_wait_for(disk, STAT_READY, "Lock") & STAT_ERR)) {
0658 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORUNLOCK);
0659 pd_wait_for(disk, STAT_READY, "Lock done");
0660 }
0661 return Ok;
0662 }
0663
0664 static enum action pd_eject(struct pd_unit *disk)
0665 {
0666 pd_wait_for(disk, 0, DBMSG("before unlock on eject"));
0667 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_DOORUNLOCK);
0668 pd_wait_for(disk, 0, DBMSG("after unlock on eject"));
0669 pd_wait_for(disk, 0, DBMSG("before eject"));
0670 pd_send_command(disk, 0, 0, 0, 0, 0, IDE_EJECT);
0671 pd_wait_for(disk, 0, DBMSG("after eject"));
0672 return Ok;
0673 }
0674
0675 static enum action pd_media_check(struct pd_unit *disk)
0676 {
0677 int r = pd_wait_for(disk, STAT_READY, DBMSG("before media_check"));
0678 if (!(r & STAT_ERR)) {
0679 pd_send_command(disk, 1, 1, 0, 0, 0, IDE_READ_VRFY);
0680 r = pd_wait_for(disk, STAT_READY, DBMSG("RDY after READ_VRFY"));
0681 } else
0682 disk->changed = 1;
0683 if (r & ERR_MC) {
0684 disk->changed = 1;
0685 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_ACKCHANGE);
0686 pd_wait_for(disk, STAT_READY, DBMSG("RDY after ACKCHANGE"));
0687 pd_send_command(disk, 1, 1, 0, 0, 0, IDE_READ_VRFY);
0688 r = pd_wait_for(disk, STAT_READY, DBMSG("RDY after VRFY"));
0689 }
0690 return Ok;
0691 }
0692
0693 static void pd_standby_off(struct pd_unit *disk)
0694 {
0695 pd_wait_for(disk, 0, DBMSG("before STANDBY"));
0696 pd_send_command(disk, 0, 0, 0, 0, 0, IDE_STANDBY);
0697 pd_wait_for(disk, 0, DBMSG("after STANDBY"));
0698 }
0699
0700 static enum action pd_identify(struct pd_unit *disk)
0701 {
0702 int j;
0703 char id[PD_ID_LEN + 1];
0704
0705
0706
0707
0708
0709
0710
0711 if (disk->drive == 0)
0712 pd_reset(disk);
0713
0714 write_reg(disk, 6, DRIVE(disk));
0715 pd_wait_for(disk, 0, DBMSG("before IDENT"));
0716 pd_send_command(disk, 1, 0, 0, 0, 0, IDE_IDENTIFY);
0717
0718 if (pd_wait_for(disk, STAT_DRQ, DBMSG("IDENT DRQ")) & STAT_ERR)
0719 return Fail;
0720 pi_read_block(disk->pi, pd_scratch, 512);
0721 disk->can_lba = pd_scratch[99] & 2;
0722 disk->sectors = le16_to_cpu(*(__le16 *) (pd_scratch + 12));
0723 disk->heads = le16_to_cpu(*(__le16 *) (pd_scratch + 6));
0724 disk->cylinders = le16_to_cpu(*(__le16 *) (pd_scratch + 2));
0725 if (disk->can_lba)
0726 disk->capacity = le32_to_cpu(*(__le32 *) (pd_scratch + 120));
0727 else
0728 disk->capacity = disk->sectors * disk->heads * disk->cylinders;
0729
0730 for (j = 0; j < PD_ID_LEN; j++)
0731 id[j ^ 1] = pd_scratch[j + PD_ID_OFF];
0732 j = PD_ID_LEN - 1;
0733 while ((j >= 0) && (id[j] <= 0x20))
0734 j--;
0735 j++;
0736 id[j] = 0;
0737
0738 disk->removable = pd_scratch[0] & 0x80;
0739
0740 printk("%s: %s, %s, %d blocks [%dM], (%d/%d/%d), %s media\n",
0741 disk->name, id,
0742 disk->drive ? "slave" : "master",
0743 disk->capacity, disk->capacity / 2048,
0744 disk->cylinders, disk->heads, disk->sectors,
0745 disk->removable ? "removable" : "fixed");
0746
0747 if (disk->capacity)
0748 pd_init_dev_parms(disk);
0749 if (!disk->standby)
0750 pd_standby_off(disk);
0751
0752 return Ok;
0753 }
0754
0755
0756
0757 static blk_status_t pd_queue_rq(struct blk_mq_hw_ctx *hctx,
0758 const struct blk_mq_queue_data *bd)
0759 {
0760 struct pd_unit *disk = hctx->queue->queuedata;
0761
0762 spin_lock_irq(&pd_lock);
0763 if (!pd_req) {
0764 pd_req = bd->rq;
0765 blk_mq_start_request(pd_req);
0766 } else
0767 list_add_tail(&bd->rq->queuelist, &disk->rq_list);
0768 spin_unlock_irq(&pd_lock);
0769
0770 run_fsm();
0771 return BLK_STS_OK;
0772 }
0773
0774 static int pd_special_command(struct pd_unit *disk,
0775 enum action (*func)(struct pd_unit *disk))
0776 {
0777 struct request *rq;
0778 struct pd_req *req;
0779
0780 rq = blk_mq_alloc_request(disk->gd->queue, REQ_OP_DRV_IN, 0);
0781 if (IS_ERR(rq))
0782 return PTR_ERR(rq);
0783 req = blk_mq_rq_to_pdu(rq);
0784
0785 req->func = func;
0786 blk_execute_rq(rq, false);
0787 blk_mq_free_request(rq);
0788 return 0;
0789 }
0790
0791
0792
0793 static int pd_open(struct block_device *bdev, fmode_t mode)
0794 {
0795 struct pd_unit *disk = bdev->bd_disk->private_data;
0796
0797 mutex_lock(&pd_mutex);
0798 disk->access++;
0799
0800 if (disk->removable) {
0801 pd_special_command(disk, pd_media_check);
0802 pd_special_command(disk, pd_door_lock);
0803 }
0804 mutex_unlock(&pd_mutex);
0805 return 0;
0806 }
0807
0808 static int pd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
0809 {
0810 struct pd_unit *disk = bdev->bd_disk->private_data;
0811
0812 if (disk->alt_geom) {
0813 geo->heads = PD_LOG_HEADS;
0814 geo->sectors = PD_LOG_SECTS;
0815 geo->cylinders = disk->capacity / (geo->heads * geo->sectors);
0816 } else {
0817 geo->heads = disk->heads;
0818 geo->sectors = disk->sectors;
0819 geo->cylinders = disk->cylinders;
0820 }
0821
0822 return 0;
0823 }
0824
0825 static int pd_ioctl(struct block_device *bdev, fmode_t mode,
0826 unsigned int cmd, unsigned long arg)
0827 {
0828 struct pd_unit *disk = bdev->bd_disk->private_data;
0829
0830 switch (cmd) {
0831 case CDROMEJECT:
0832 mutex_lock(&pd_mutex);
0833 if (disk->access == 1)
0834 pd_special_command(disk, pd_eject);
0835 mutex_unlock(&pd_mutex);
0836 return 0;
0837 default:
0838 return -EINVAL;
0839 }
0840 }
0841
0842 static void pd_release(struct gendisk *p, fmode_t mode)
0843 {
0844 struct pd_unit *disk = p->private_data;
0845
0846 mutex_lock(&pd_mutex);
0847 if (!--disk->access && disk->removable)
0848 pd_special_command(disk, pd_door_unlock);
0849 mutex_unlock(&pd_mutex);
0850 }
0851
0852 static unsigned int pd_check_events(struct gendisk *p, unsigned int clearing)
0853 {
0854 struct pd_unit *disk = p->private_data;
0855 int r;
0856 if (!disk->removable)
0857 return 0;
0858 pd_special_command(disk, pd_media_check);
0859 r = disk->changed;
0860 disk->changed = 0;
0861 return r ? DISK_EVENT_MEDIA_CHANGE : 0;
0862 }
0863
0864 static const struct block_device_operations pd_fops = {
0865 .owner = THIS_MODULE,
0866 .open = pd_open,
0867 .release = pd_release,
0868 .ioctl = pd_ioctl,
0869 .compat_ioctl = pd_ioctl,
0870 .getgeo = pd_getgeo,
0871 .check_events = pd_check_events,
0872 };
0873
0874
0875
0876 static const struct blk_mq_ops pd_mq_ops = {
0877 .queue_rq = pd_queue_rq,
0878 };
0879
0880 static int pd_probe_drive(struct pd_unit *disk, int autoprobe, int port,
0881 int mode, int unit, int protocol, int delay)
0882 {
0883 int index = disk - pd;
0884 int *parm = *drives[index];
0885 struct gendisk *p;
0886 int ret;
0887
0888 disk->pi = &disk->pia;
0889 disk->access = 0;
0890 disk->changed = 1;
0891 disk->capacity = 0;
0892 disk->drive = parm[D_SLV];
0893 snprintf(disk->name, PD_NAMELEN, "%s%c", name, 'a' + index);
0894 disk->alt_geom = parm[D_GEO];
0895 disk->standby = parm[D_SBY];
0896 INIT_LIST_HEAD(&disk->rq_list);
0897
0898 if (!pi_init(disk->pi, autoprobe, port, mode, unit, protocol, delay,
0899 pd_scratch, PI_PD, verbose, disk->name))
0900 return -ENXIO;
0901
0902 memset(&disk->tag_set, 0, sizeof(disk->tag_set));
0903 disk->tag_set.ops = &pd_mq_ops;
0904 disk->tag_set.cmd_size = sizeof(struct pd_req);
0905 disk->tag_set.nr_hw_queues = 1;
0906 disk->tag_set.nr_maps = 1;
0907 disk->tag_set.queue_depth = 2;
0908 disk->tag_set.numa_node = NUMA_NO_NODE;
0909 disk->tag_set.flags = BLK_MQ_F_SHOULD_MERGE | BLK_MQ_F_BLOCKING;
0910 ret = blk_mq_alloc_tag_set(&disk->tag_set);
0911 if (ret)
0912 goto pi_release;
0913
0914 p = blk_mq_alloc_disk(&disk->tag_set, disk);
0915 if (IS_ERR(p)) {
0916 ret = PTR_ERR(p);
0917 goto free_tag_set;
0918 }
0919 disk->gd = p;
0920
0921 strcpy(p->disk_name, disk->name);
0922 p->fops = &pd_fops;
0923 p->major = major;
0924 p->first_minor = (disk - pd) << PD_BITS;
0925 p->minors = 1 << PD_BITS;
0926 p->events = DISK_EVENT_MEDIA_CHANGE;
0927 p->private_data = disk;
0928 blk_queue_max_hw_sectors(p->queue, cluster);
0929 blk_queue_bounce_limit(p->queue, BLK_BOUNCE_HIGH);
0930
0931 if (disk->drive == -1) {
0932 for (disk->drive = 0; disk->drive <= 1; disk->drive++) {
0933 ret = pd_special_command(disk, pd_identify);
0934 if (ret == 0)
0935 break;
0936 }
0937 } else {
0938 ret = pd_special_command(disk, pd_identify);
0939 }
0940 if (ret)
0941 goto put_disk;
0942 set_capacity(disk->gd, disk->capacity);
0943 ret = add_disk(disk->gd);
0944 if (ret)
0945 goto cleanup_disk;
0946 return 0;
0947 cleanup_disk:
0948 put_disk(disk->gd);
0949 put_disk:
0950 put_disk(p);
0951 disk->gd = NULL;
0952 free_tag_set:
0953 blk_mq_free_tag_set(&disk->tag_set);
0954 pi_release:
0955 pi_release(disk->pi);
0956 return ret;
0957 }
0958
0959 static int __init pd_init(void)
0960 {
0961 int found = 0, unit, pd_drive_count = 0;
0962 struct pd_unit *disk;
0963
0964 if (disable)
0965 return -ENODEV;
0966
0967 if (register_blkdev(major, name))
0968 return -ENODEV;
0969
0970 printk("%s: %s version %s, major %d, cluster %d, nice %d\n",
0971 name, name, PD_VERSION, major, cluster, nice);
0972
0973 par_drv = pi_register_driver(name);
0974 if (!par_drv) {
0975 pr_err("failed to register %s driver\n", name);
0976 goto out_unregister_blkdev;
0977 }
0978
0979 for (unit = 0; unit < PD_UNITS; unit++) {
0980 int *parm = *drives[unit];
0981
0982 if (parm[D_PRT])
0983 pd_drive_count++;
0984 }
0985
0986 if (pd_drive_count == 0) {
0987 if (!pd_probe_drive(pd, 1, -1, -1, -1, -1, -1))
0988 found++;
0989 } else {
0990 for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
0991 int *parm = *drives[unit];
0992 if (!parm[D_PRT])
0993 continue;
0994 if (!pd_probe_drive(disk, 0, parm[D_PRT], parm[D_MOD],
0995 parm[D_UNI], parm[D_PRO], parm[D_DLY]))
0996 found++;
0997 }
0998 }
0999 if (!found) {
1000 printk("%s: no valid drive found\n", name);
1001 goto out_pi_unregister_driver;
1002 }
1003
1004 return 0;
1005
1006 out_pi_unregister_driver:
1007 pi_unregister_driver(par_drv);
1008 out_unregister_blkdev:
1009 unregister_blkdev(major, name);
1010 return -ENODEV;
1011 }
1012
1013 static void __exit pd_exit(void)
1014 {
1015 struct pd_unit *disk;
1016 int unit;
1017 unregister_blkdev(major, name);
1018 for (unit = 0, disk = pd; unit < PD_UNITS; unit++, disk++) {
1019 struct gendisk *p = disk->gd;
1020 if (p) {
1021 disk->gd = NULL;
1022 del_gendisk(p);
1023 put_disk(p);
1024 blk_mq_free_tag_set(&disk->tag_set);
1025 pi_release(disk->pi);
1026 }
1027 }
1028 }
1029
1030 MODULE_LICENSE("GPL");
1031 module_init(pd_init)
1032 module_exit(pd_exit)