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