1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/drivers/block/floppy.c 4 * 5 * Copyright (C) 1991, 1992 Linus Torvalds 6 * Copyright (C) 1993, 1994 Alain Knaff 7 * Copyright (C) 1998 Alan Cox 8 */ 9 10 /* 11 * 02.12.91 - Changed to static variables to indicate need for reset 12 * and recalibrate. This makes some things easier (output_byte reset 13 * checking etc), and means less interrupt jumping in case of errors, 14 * so the code is hopefully easier to understand. 15 */ 16 17 /* 18 * This file is certainly a mess. I've tried my best to get it working, 19 * but I don't like programming floppies, and I have only one anyway. 20 * Urgel. I should check for more errors, and do more graceful error 21 * recovery. Seems there are problems with several drives. I've tried to 22 * correct them. No promises. 23 */ 24 25 /* 26 * As with hd.c, all routines within this file can (and will) be called 27 * by interrupts, so extreme caution is needed. A hardware interrupt 28 * handler may not sleep, or a kernel panic will happen. Thus I cannot 29 * call "floppy-on" directly, but have to set a special timer interrupt 30 * etc. 31 */ 32 33 /* 34 * 28.02.92 - made track-buffering routines, based on the routines written 35 * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus. 36 */ 37 38 /* 39 * Automatic floppy-detection and formatting written by Werner Almesberger 40 * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with 41 * the floppy-change signal detection. 42 */ 43 44 /* 45 * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed 46 * FDC data overrun bug, added some preliminary stuff for vertical 47 * recording support. 48 * 49 * 1992/9/17: Added DMA allocation & DMA functions. -- hhb. 50 * 51 * TODO: Errors are still not counted properly. 52 */ 53 54 /* 1992/9/20 55 * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl) 56 * modeled after the freeware MS-DOS program fdformat/88 V1.8 by 57 * Christoph H. Hochst\"atter. 58 * I have fixed the shift values to the ones I always use. Maybe a new 59 * ioctl() should be created to be able to modify them. 60 * There is a bug in the driver that makes it impossible to format a 61 * floppy as the first thing after bootup. 62 */ 63 64 /* 65 * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and 66 * this helped the floppy driver as well. Much cleaner, and still seems to 67 * work. 68 */ 69 70 /* 1994/6/24 --bbroad-- added the floppy table entries and made 71 * minor modifications to allow 2.88 floppies to be run. 72 */ 73 74 /* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more 75 * disk types. 76 */ 77 78 /* 79 * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger 80 * format bug fixes, but unfortunately some new bugs too... 81 */ 82 83 /* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write 84 * errors to allow safe writing by specialized programs. 85 */ 86 87 /* 1995/4/24 -- Dan Fandrich -- added support for Commodore 1581 3.5" disks 88 * by defining bit 1 of the "stretch" parameter to mean put sectors on the 89 * opposite side of the disk, leaving the sector IDs alone (i.e. Commodore's 90 * drives are "upside-down"). 91 */ 92 93 /* 94 * 1995/8/26 -- Andreas Busse -- added Mips support. 95 */ 96 97 /* 98 * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent 99 * features to asm/floppy.h. 100 */ 101 102 /* 103 * 1998/1/21 -- Richard Gooch <rgooch@atnf.csiro.au> -- devfs support 104 */ 105 106 /* 107 * 1998/05/07 -- Russell King -- More portability cleanups; moved definition of 108 * interrupt and dma channel to asm/floppy.h. Cleaned up some formatting & 109 * use of '0' for NULL. 110 */ 111 112 /* 113 * 1998/06/07 -- Alan Cox -- Merged the 2.0.34 fixes for resource allocation 114 * failures. 115 */ 116 117 /* 118 * 1998/09/20 -- David Weinehall -- Added slow-down code for buggy PS/2-drives. 119 */ 120 121 /* 122 * 1999/08/13 -- Paul Slootman -- floppy stopped working on Alpha after 24 123 * days, 6 hours, 32 minutes and 32 seconds (i.e. MAXINT jiffies; ints were 124 * being used to store jiffies, which are unsigned longs). 125 */ 126 127 /* 128 * 2000/08/28 -- Arnaldo Carvalho de Melo <acme@conectiva.com.br> 129 * - get rid of check_region 130 * - s/suser/capable/ 131 */ 132 133 /* 134 * 2001/08/26 -- Paul Gortmaker - fix insmod oops on machines with no 135 * floppy controller (lingering task on list after module is gone... boom.) 136 */ 137 138 /* 139 * 2002/02/07 -- Anton Altaparmakov - Fix io ports reservation to correct range 140 * (0x3f2-0x3f5, 0x3f7). This fix is a bit of a hack but the proper fix 141 * requires many non-obvious changes in arch dependent code. 142 */ 143 144 /* 2003/07/28 -- Daniele Bellucci <bellucda@tiscali.it>. 145 * Better audit of register_blkdev. 146 */ 147 148 #define DEBUGT 2 149 150 #define DPRINT(format, args...) \ 151 pr_info("floppy%d: " format, current_drive, ##args) 152 153 #define DCL_DEBUG /* debug disk change line */ 154 #ifdef DCL_DEBUG 155 #define debug_dcl(test, fmt, args...) \ 156 do { if ((test) & FD_DEBUG) DPRINT(fmt, ##args); } while (0) 157 #else 158 #define debug_dcl(test, fmt, args...) \ 159 do { if (0) DPRINT(fmt, ##args); } while (0) 160 #endif 161 162 /* do print messages for unexpected interrupts */ 163 static int print_unex = 1; 164 #include <linux/async.h> 165 #include <linux/bio.h> 166 #include <linux/compat.h> 167 #include <linux/delay.h> 168 #include <linux/errno.h> 169 #include <linux/fcntl.h> 170 #include <linux/fd.h> 171 #include <linux/fdreg.h> 172 #include <linux/fs.h> 173 #include <linux/hdreg.h> 174 #include <linux/init.h> 175 #include <linux/interrupt.h> 176 #include <linux/io.h> 177 #include <linux/ioport.h> 178 #include <linux/jiffies.h> 179 #include <linux/kernel.h> 180 #include <linux/major.h> 181 #include <linux/mc146818rtc.h> /* CMOS defines */ 182 #include <linux/mm.h> 183 #include <linux/device-id/pnp.h> 184 #include <linux/module.h> 185 #include <linux/mutex.h> 186 #include <linux/platform_device.h> 187 #include <linux/sched.h> 188 #include <linux/slab.h> 189 #include <linux/string.h> 190 #include <linux/timer.h> 191 #include <linux/uaccess.h> 192 #include <linux/workqueue.h> 193 194 /* 195 * PS/2 floppies have much slower step rates than regular floppies. 196 * It's been recommended that take about 1/4 of the default speed 197 * in some more extreme cases. 198 */ 199 static DEFINE_MUTEX(floppy_mutex); 200 static int slow_floppy; 201 202 #include <asm/dma.h> 203 #include <asm/irq.h> 204 205 static int FLOPPY_IRQ = 6; 206 static int FLOPPY_DMA = 2; 207 static int can_use_virtual_dma = 2; 208 /* ======= 209 * can use virtual DMA: 210 * 0 = use of virtual DMA disallowed by config 211 * 1 = use of virtual DMA prescribed by config 212 * 2 = no virtual DMA preference configured. By default try hard DMA, 213 * but fall back on virtual DMA when not enough memory available 214 */ 215 216 static int use_virtual_dma; 217 /* ======= 218 * use virtual DMA 219 * 0 using hard DMA 220 * 1 using virtual DMA 221 * This variable is set to virtual when a DMA mem problem arises, and 222 * reset back in floppy_grab_irq_and_dma. 223 * It is not safe to reset it in other circumstances, because the floppy 224 * driver may have several buffers in use at once, and we do currently not 225 * record each buffers capabilities 226 */ 227 228 static DEFINE_SPINLOCK(floppy_lock); 229 230 static unsigned short virtual_dma_port = 0x3f0; 231 irqreturn_t floppy_interrupt(int irq, void *dev_id); 232 static int set_dor(int fdc, char mask, char data); 233 234 /* the following is the mask of allowed drives. By default units 2 and 235 * 3 of both floppy controllers are disabled, because switching on the 236 * motor of these drives causes system hangs on some PCI computers. drive 237 * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if 238 * a drive is allowed. 239 * 240 * NOTE: This must come before we include the arch floppy header because 241 * some ports reference this variable from there. -DaveM 242 */ 243 244 static int allowed_drive_mask = 0x33; 245 246 #include <asm/floppy.h> 247 248 static int irqdma_allocated; 249 250 #include <linux/blk-mq.h> 251 #include <linux/blkpg.h> 252 #include <linux/cdrom.h> /* for the compatibility eject ioctl */ 253 #include <linux/completion.h> 254 255 static LIST_HEAD(floppy_reqs); 256 static struct request *current_req; 257 static int set_next_request(void); 258 259 #ifndef fd_get_dma_residue 260 #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA) 261 #endif 262 263 /* Dma Memory related stuff */ 264 265 #ifndef fd_dma_mem_free 266 #define fd_dma_mem_free(addr, size) free_pages(addr, get_order(size)) 267 #endif 268 269 #ifndef fd_dma_mem_alloc 270 #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL, get_order(size)) 271 #endif 272 273 #ifndef fd_cacheflush 274 #define fd_cacheflush(addr, size) /* nothing... */ 275 #endif 276 277 static inline void fallback_on_nodma_alloc(char **addr, size_t l) 278 { 279 #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA 280 if (*addr) 281 return; /* we have the memory */ 282 if (can_use_virtual_dma != 2) 283 return; /* no fallback allowed */ 284 pr_info("DMA memory shortage. Temporarily falling back on virtual DMA\n"); 285 *addr = (char *)nodma_mem_alloc(l); 286 #else 287 return; 288 #endif 289 } 290 291 /* End dma memory related stuff */ 292 293 static unsigned long fake_change; 294 static bool initialized; 295 296 #define ITYPE(x) (((x) >> 2) & 0x1f) 297 #define TOMINOR(x) ((x & 3) | ((x & 4) << 5)) 298 #define UNIT(x) ((x) & 0x03) /* drive on fdc */ 299 #define FDC(x) (((x) & 0x04) >> 2) /* fdc of drive */ 300 /* reverse mapping from unit and fdc to drive */ 301 #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2)) 302 303 #define PH_HEAD(floppy, head) (((((floppy)->stretch & 2) >> 1) ^ head) << 2) 304 #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH) 305 306 /* read/write commands */ 307 #define COMMAND 0 308 #define DR_SELECT 1 309 #define TRACK 2 310 #define HEAD 3 311 #define SECTOR 4 312 #define SIZECODE 5 313 #define SECT_PER_TRACK 6 314 #define GAP 7 315 #define SIZECODE2 8 316 #define NR_RW 9 317 318 /* format commands */ 319 #define F_SIZECODE 2 320 #define F_SECT_PER_TRACK 3 321 #define F_GAP 4 322 #define F_FILL 5 323 #define NR_F 6 324 325 /* 326 * Maximum disk size (in kilobytes). 327 * This default is used whenever the current disk size is unknown. 328 * [Now it is rather a minimum] 329 */ 330 #define MAX_DISK_SIZE (PAGE_SIZE / 1024) 331 332 /* 333 * globals used by 'result()' 334 */ 335 static unsigned char reply_buffer[FD_RAW_REPLY_SIZE]; 336 static int inr; /* size of reply buffer, when called from interrupt */ 337 #define ST0 0 338 #define ST1 1 339 #define ST2 2 340 #define ST3 0 /* result of GETSTATUS */ 341 #define R_TRACK 3 342 #define R_HEAD 4 343 #define R_SECTOR 5 344 #define R_SIZECODE 6 345 346 #define SEL_DLY (2 * HZ / 100) 347 348 /* 349 * this struct defines the different floppy drive types. 350 */ 351 static struct { 352 struct floppy_drive_params params; 353 const char *name; /* name printed while booting */ 354 } default_drive_params[] = { 355 /* NOTE: the time values in jiffies should be in msec! 356 CMOS drive type 357 | Maximum data rate supported by drive type 358 | | Head load time, msec 359 | | | Head unload time, msec (not used) 360 | | | | Step rate interval, usec 361 | | | | | Time needed for spinup time (jiffies) 362 | | | | | | Timeout for spinning down (jiffies) 363 | | | | | | | Spindown offset (where disk stops) 364 | | | | | | | | Select delay 365 | | | | | | | | | RPS 366 | | | | | | | | | | Max number of tracks 367 | | | | | | | | | | | Interrupt timeout 368 | | | | | | | | | | | | Max nonintlv. sectors 369 | | | | | | | | | | | | | -Max Errors- flags */ 370 {{0, 500, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 80, 3*HZ, 20, {3,1,2,0,2}, 0, 371 0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" }, 372 373 {{1, 300, 16, 16, 8000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 40, 3*HZ, 17, {3,1,2,0,2}, 0, 374 0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/ 375 376 {{2, 500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6, 83, 3*HZ, 17, {3,1,2,0,2}, 0, 377 0, { 2, 5, 6,23,10,20,12, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/ 378 379 {{3, 250, 16, 16, 3000, 1*HZ, 3*HZ, 0, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0, 380 0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/ 381 382 {{4, 500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 20, {3,1,2,0,2}, 0, 383 0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/ 384 385 {{5, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0, 386 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/ 387 388 {{6, 1000, 15, 8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5, 83, 3*HZ, 40, {3,1,2,0,2}, 0, 389 0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/ 390 /* | --autodetected formats--- | | | 391 * read_track | | Name printed when booting 392 * | Native format 393 * Frequency of disk change checks */ 394 }; 395 396 static struct floppy_drive_params drive_params[N_DRIVE]; 397 static struct floppy_drive_struct drive_state[N_DRIVE]; 398 static struct floppy_write_errors write_errors[N_DRIVE]; 399 static struct timer_list motor_off_timer[N_DRIVE]; 400 static struct blk_mq_tag_set tag_sets[N_DRIVE]; 401 static struct gendisk *opened_disk[N_DRIVE]; 402 static DEFINE_MUTEX(open_lock); 403 static struct floppy_raw_cmd *raw_cmd, default_raw_cmd; 404 405 /* 406 * This struct defines the different floppy types. 407 * 408 * Bit 0 of 'stretch' tells if the tracks need to be doubled for some 409 * types (e.g. 360kB diskette in 1.2MB drive, etc.). Bit 1 of 'stretch' 410 * tells if the disk is in Commodore 1581 format, which means side 0 sectors 411 * are located on side 1 of the disk but with a side 0 ID, and vice-versa. 412 * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the 413 * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical 414 * side 0 is on physical side 0 (but with the misnamed sector IDs). 415 * 'stretch' should probably be renamed to something more general, like 416 * 'options'. 417 * 418 * Bits 2 through 9 of 'stretch' tell the number of the first sector. 419 * The LSB (bit 2) is flipped. For most disks, the first sector 420 * is 1 (represented by 0x00<<2). For some CP/M and music sampler 421 * disks (such as Ensoniq EPS 16plus) it is 0 (represented as 0x01<<2). 422 * For Amstrad CPC disks it is 0xC1 (represented as 0xC0<<2). 423 * 424 * Other parameters should be self-explanatory (see also setfdprm(8)). 425 */ 426 /* 427 Size 428 | Sectors per track 429 | | Head 430 | | | Tracks 431 | | | | Stretch 432 | | | | | Gap 1 size 433 | | | | | | Data rate, | 0x40 for perp 434 | | | | | | | Spec1 (stepping rate, head unload 435 | | | | | | | | /fmt gap (gap2) */ 436 static struct floppy_struct floppy_type[32] = { 437 { 0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL }, /* 0 no testing */ 438 { 720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360" }, /* 1 360KB PC */ 439 { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" }, /* 2 1.2MB AT */ 440 { 720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360" }, /* 3 360KB SS 3.5" */ 441 { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720" }, /* 4 720KB 3.5" */ 442 { 720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360" }, /* 5 360KB AT */ 443 { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720" }, /* 6 720KB AT */ 444 { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" }, /* 7 1.44MB 3.5" */ 445 { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" }, /* 8 2.88MB 3.5" */ 446 { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120" }, /* 9 3.12MB 3.5" */ 447 448 { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25" */ 449 { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5" */ 450 { 820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410" }, /* 12 410KB 5.25" */ 451 { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820" }, /* 13 820KB 3.5" */ 452 { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" }, /* 14 1.48MB 5.25" */ 453 { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" }, /* 15 1.72MB 3.5" */ 454 { 840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420" }, /* 16 420KB 5.25" */ 455 { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830" }, /* 17 830KB 3.5" */ 456 { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" }, /* 18 1.49MB 5.25" */ 457 { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5" */ 458 459 { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880" }, /* 20 880KB 5.25" */ 460 { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5" */ 461 { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5" */ 462 { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25" */ 463 { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5" */ 464 { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5" */ 465 { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5" */ 466 { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5" */ 467 { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5" */ 468 { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5" */ 469 470 { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800" }, /* 30 800KB 3.5" */ 471 { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5" */ 472 }; 473 474 static struct gendisk *disks[N_DRIVE][ARRAY_SIZE(floppy_type)]; 475 476 #define SECTSIZE (_FD_SECTSIZE(*floppy)) 477 478 /* Auto-detection: Disk type used until the next media change occurs. */ 479 static struct floppy_struct *current_type[N_DRIVE]; 480 481 /* 482 * User-provided type information. current_type points to 483 * the respective entry of this array. 484 */ 485 static struct floppy_struct user_params[N_DRIVE]; 486 487 static sector_t floppy_sizes[256]; 488 489 static char floppy_device_name[] = "floppy"; 490 491 /* 492 * The driver is trying to determine the correct media format 493 * while probing is set. rw_interrupt() clears it after a 494 * successful access. 495 */ 496 static int probing; 497 498 /* Synchronization of FDC access. */ 499 #define FD_COMMAND_NONE -1 500 #define FD_COMMAND_ERROR 2 501 #define FD_COMMAND_OKAY 3 502 503 static int command_status = FD_COMMAND_NONE; 504 static unsigned long fdc_busy; 505 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait); 506 static DECLARE_WAIT_QUEUE_HEAD(command_done); 507 508 /* errors encountered on the current (or last) request */ 509 static int floppy_errors; 510 511 /* Format request descriptor. */ 512 static struct format_descr format_req; 513 514 /* 515 * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps 516 * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc), 517 * H is head unload time (1=16ms, 2=32ms, etc) 518 */ 519 520 /* 521 * Track buffer 522 * Because these are written to by the DMA controller, they must 523 * not contain a 64k byte boundary crossing, or data will be 524 * corrupted/lost. 525 */ 526 static char *floppy_track_buffer; 527 static int max_buffer_sectors; 528 529 static const struct cont_t { 530 void (*interrupt)(void); 531 /* this is called after the interrupt of the 532 * main command */ 533 void (*redo)(void); /* this is called to retry the operation */ 534 void (*error)(void); /* this is called to tally an error */ 535 void (*done)(int); /* this is called to say if the operation has 536 * succeeded/failed */ 537 } *cont; 538 539 static void floppy_ready(void); 540 static void floppy_start(void); 541 static void process_fd_request(void); 542 static void recalibrate_floppy(void); 543 static void floppy_shutdown(struct work_struct *); 544 545 static int floppy_request_regions(int); 546 static void floppy_release_regions(int); 547 static int floppy_grab_irq_and_dma(void); 548 static void floppy_release_irq_and_dma(void); 549 550 /* 551 * The "reset" variable should be tested whenever an interrupt is scheduled, 552 * after the commands have been sent. This is to ensure that the driver doesn't 553 * get wedged when the interrupt doesn't come because of a failed command. 554 * reset doesn't need to be tested before sending commands, because 555 * output_byte is automatically disabled when reset is set. 556 */ 557 static void reset_fdc(void); 558 static int floppy_revalidate(struct gendisk *disk); 559 560 /* 561 * These are global variables, as that's the easiest way to give 562 * information to interrupts. They are the data used for the current 563 * request. 564 */ 565 #define NO_TRACK -1 566 #define NEED_1_RECAL -2 567 #define NEED_2_RECAL -3 568 569 static atomic_t usage_count = ATOMIC_INIT(0); 570 571 /* buffer related variables */ 572 static int buffer_track = -1; 573 static int buffer_drive = -1; 574 static int buffer_min = -1; 575 static int buffer_max = -1; 576 577 /* fdc related variables, should end up in a struct */ 578 static struct floppy_fdc_state fdc_state[N_FDC]; 579 static int current_fdc; /* current fdc */ 580 581 static struct workqueue_struct *floppy_wq; 582 583 static struct floppy_struct *_floppy = floppy_type; 584 static unsigned char current_drive; 585 static long current_count_sectors; 586 static unsigned char fsector_t; /* sector in track */ 587 static unsigned char in_sector_offset; /* offset within physical sector, 588 * expressed in units of 512 bytes */ 589 590 static inline unsigned char fdc_inb(int fdc, int reg) 591 { 592 return fd_inb(fdc_state[fdc].address, reg); 593 } 594 595 static inline void fdc_outb(unsigned char value, int fdc, int reg) 596 { 597 fd_outb(value, fdc_state[fdc].address, reg); 598 } 599 600 static inline bool drive_no_geom(int drive) 601 { 602 return !READ_ONCE(current_type[drive]) && 603 !ITYPE(drive_state[drive].fd_device); 604 } 605 606 #ifndef fd_eject 607 static inline int fd_eject(int drive) 608 { 609 return -EINVAL; 610 } 611 #endif 612 613 /* 614 * Debugging 615 * ========= 616 */ 617 #ifdef DEBUGT 618 static long unsigned debugtimer; 619 620 static inline void set_debugt(void) 621 { 622 debugtimer = jiffies; 623 } 624 625 static inline void debugt(const char *func, const char *msg) 626 { 627 if (drive_params[current_drive].flags & DEBUGT) 628 pr_info("%s:%s dtime=%lu\n", func, msg, jiffies - debugtimer); 629 } 630 #else 631 static inline void set_debugt(void) { } 632 static inline void debugt(const char *func, const char *msg) { } 633 #endif /* DEBUGT */ 634 635 636 static DECLARE_DELAYED_WORK(fd_timeout, floppy_shutdown); 637 static const char *timeout_message; 638 639 static void is_alive(const char *func, const char *message) 640 { 641 /* this routine checks whether the floppy driver is "alive" */ 642 if (test_bit(0, &fdc_busy) && READ_ONCE(command_status) < 2 && 643 !delayed_work_pending(&fd_timeout)) { 644 DPRINT("%s: timeout handler died. %s\n", func, message); 645 } 646 } 647 648 static void (*do_floppy)(void) = NULL; 649 650 #define OLOGSIZE 20 651 652 static void (*lasthandler)(void); 653 static unsigned long interruptjiffies; 654 static unsigned long resultjiffies; 655 static int resultsize; 656 static unsigned long lastredo; 657 658 static struct output_log { 659 unsigned char data; 660 unsigned char status; 661 unsigned long jiffies; 662 } output_log[OLOGSIZE]; 663 664 static int output_log_pos; 665 666 #define MAXTIMEOUT -2 667 668 static void __reschedule_timeout(int drive, const char *message) 669 { 670 unsigned long delay; 671 672 if (drive < 0 || drive >= N_DRIVE) { 673 delay = 20UL * HZ; 674 drive = 0; 675 } else 676 delay = drive_params[drive].timeout; 677 678 mod_delayed_work(floppy_wq, &fd_timeout, delay); 679 if (drive_params[drive].flags & FD_DEBUG) 680 DPRINT("reschedule timeout %s\n", message); 681 timeout_message = message; 682 } 683 684 static void reschedule_timeout(int drive, const char *message) 685 { 686 unsigned long flags; 687 688 spin_lock_irqsave(&floppy_lock, flags); 689 __reschedule_timeout(drive, message); 690 spin_unlock_irqrestore(&floppy_lock, flags); 691 } 692 693 #define INFBOUND(a, b) (a) = max_t(int, a, b) 694 #define SUPBOUND(a, b) (a) = min_t(int, a, b) 695 696 /* 697 * Bottom half floppy driver. 698 * ========================== 699 * 700 * This part of the file contains the code talking directly to the hardware, 701 * and also the main service loop (seek-configure-spinup-command) 702 */ 703 704 /* 705 * disk change. 706 * This routine is responsible for maintaining the FD_DISK_CHANGE flag, 707 * and the last_checked date. 708 * 709 * last_checked is the date of the last check which showed 'no disk change' 710 * FD_DISK_CHANGE is set under two conditions: 711 * 1. The floppy has been changed after some i/o to that floppy already 712 * took place. 713 * 2. No floppy disk is in the drive. This is done in order to ensure that 714 * requests are quickly flushed in case there is no disk in the drive. It 715 * follows that FD_DISK_CHANGE can only be cleared if there is a disk in 716 * the drive. 717 * 718 * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet. 719 * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on 720 * each seek. If a disk is present, the disk change line should also be 721 * cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk 722 * change line is set, this means either that no disk is in the drive, or 723 * that it has been removed since the last seek. 724 * 725 * This means that we really have a third possibility too: 726 * The floppy has been changed after the last seek. 727 */ 728 729 static int disk_change(int drive) 730 { 731 int fdc = FDC(drive); 732 733 if (time_before(jiffies, drive_state[drive].select_date + drive_params[drive].select_delay)) 734 DPRINT("WARNING disk change called early\n"); 735 if (!(fdc_state[fdc].dor & (0x10 << UNIT(drive))) || 736 (fdc_state[fdc].dor & 3) != UNIT(drive) || fdc != FDC(drive)) { 737 DPRINT("probing disk change on unselected drive\n"); 738 DPRINT("drive=%d fdc=%d dor=%x\n", drive, FDC(drive), 739 (unsigned int)fdc_state[fdc].dor); 740 } 741 742 debug_dcl(drive_params[drive].flags, 743 "checking disk change line for drive %d\n", drive); 744 debug_dcl(drive_params[drive].flags, "jiffies=%lu\n", jiffies); 745 debug_dcl(drive_params[drive].flags, "disk change line=%x\n", 746 fdc_inb(fdc, FD_DIR) & 0x80); 747 debug_dcl(drive_params[drive].flags, "flags=%lx\n", 748 drive_state[drive].flags); 749 750 if (drive_params[drive].flags & FD_BROKEN_DCL) 751 return test_bit(FD_DISK_CHANGED_BIT, 752 &drive_state[drive].flags); 753 if ((fdc_inb(fdc, FD_DIR) ^ drive_params[drive].flags) & 0x80) { 754 set_bit(FD_VERIFY_BIT, &drive_state[drive].flags); 755 /* verify write protection */ 756 757 if (drive_state[drive].maxblock) /* mark it changed */ 758 set_bit(FD_DISK_CHANGED_BIT, 759 &drive_state[drive].flags); 760 761 /* invalidate its geometry */ 762 if (drive_state[drive].keep_data >= 0) { 763 if ((drive_params[drive].flags & FTD_MSG) && 764 current_type[drive] != NULL) 765 DPRINT("Disk type is undefined after disk change\n"); 766 current_type[drive] = NULL; 767 floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE << 1; 768 } 769 770 return 1; 771 } else { 772 drive_state[drive].last_checked = jiffies; 773 clear_bit(FD_DISK_NEWCHANGE_BIT, &drive_state[drive].flags); 774 } 775 return 0; 776 } 777 778 static inline int is_selected(int dor, int unit) 779 { 780 return ((dor & (0x10 << unit)) && (dor & 3) == unit); 781 } 782 783 static bool is_ready_state(int status) 784 { 785 int state = status & (STATUS_READY | STATUS_DIR | STATUS_DMA); 786 return state == STATUS_READY; 787 } 788 789 static int set_dor(int fdc, char mask, char data) 790 { 791 unsigned char unit; 792 unsigned char drive; 793 unsigned char newdor; 794 unsigned char olddor; 795 796 if (fdc_state[fdc].address == -1) 797 return -1; 798 799 olddor = fdc_state[fdc].dor; 800 newdor = (olddor & mask) | data; 801 if (newdor != olddor) { 802 unit = olddor & 0x3; 803 if (is_selected(olddor, unit) && !is_selected(newdor, unit)) { 804 drive = REVDRIVE(fdc, unit); 805 debug_dcl(drive_params[drive].flags, 806 "calling disk change from set_dor\n"); 807 disk_change(drive); 808 } 809 fdc_state[fdc].dor = newdor; 810 fdc_outb(newdor, fdc, FD_DOR); 811 812 unit = newdor & 0x3; 813 if (!is_selected(olddor, unit) && is_selected(newdor, unit)) { 814 drive = REVDRIVE(fdc, unit); 815 drive_state[drive].select_date = jiffies; 816 } 817 } 818 return olddor; 819 } 820 821 static void twaddle(int fdc, int drive) 822 { 823 if (drive_params[drive].select_delay) 824 return; 825 fdc_outb(fdc_state[fdc].dor & ~(0x10 << UNIT(drive)), 826 fdc, FD_DOR); 827 fdc_outb(fdc_state[fdc].dor, fdc, FD_DOR); 828 drive_state[drive].select_date = jiffies; 829 } 830 831 /* 832 * Reset all driver information about the specified fdc. 833 * This is needed after a reset, and after a raw command. 834 */ 835 static void reset_fdc_info(int fdc, int mode) 836 { 837 int drive; 838 839 fdc_state[fdc].spec1 = fdc_state[fdc].spec2 = -1; 840 fdc_state[fdc].need_configure = 1; 841 fdc_state[fdc].perp_mode = 1; 842 fdc_state[fdc].rawcmd = 0; 843 for (drive = 0; drive < N_DRIVE; drive++) 844 if (FDC(drive) == fdc && 845 (mode || drive_state[drive].track != NEED_1_RECAL)) 846 drive_state[drive].track = NEED_2_RECAL; 847 } 848 849 /* 850 * selects the fdc and drive, and enables the fdc's input/dma. 851 * Both current_drive and current_fdc are changed to match the new drive. 852 */ 853 static void set_fdc(int drive) 854 { 855 unsigned int fdc; 856 857 if (drive < 0 || drive >= N_DRIVE) { 858 pr_info("bad drive value %d\n", drive); 859 return; 860 } 861 862 fdc = FDC(drive); 863 if (fdc >= N_FDC) { 864 pr_info("bad fdc value\n"); 865 return; 866 } 867 868 set_dor(fdc, ~0, 8); 869 #if N_FDC > 1 870 set_dor(1 - fdc, ~8, 0); 871 #endif 872 if (fdc_state[fdc].rawcmd == 2) 873 reset_fdc_info(fdc, 1); 874 if (fdc_inb(fdc, FD_STATUS) != STATUS_READY) 875 fdc_state[fdc].reset = 1; 876 877 current_drive = drive; 878 current_fdc = fdc; 879 } 880 881 /* 882 * locks the driver. 883 * Both current_drive and current_fdc are changed to match the new drive. 884 */ 885 static int lock_fdc(int drive) 886 { 887 if (WARN(atomic_read(&usage_count) == 0, 888 "Trying to lock fdc while usage count=0\n")) 889 return -1; 890 891 if (wait_event_interruptible(fdc_wait, !test_and_set_bit(0, &fdc_busy))) 892 return -EINTR; 893 894 WRITE_ONCE(command_status, FD_COMMAND_NONE); 895 896 reschedule_timeout(drive, "lock fdc"); 897 set_fdc(drive); 898 return 0; 899 } 900 901 /* unlocks the driver */ 902 static void unlock_fdc(void) 903 { 904 if (!test_bit(0, &fdc_busy)) 905 DPRINT("FDC access conflict!\n"); 906 907 raw_cmd = NULL; 908 WRITE_ONCE(command_status, FD_COMMAND_NONE); 909 cancel_delayed_work(&fd_timeout); 910 do_floppy = NULL; 911 cont = NULL; 912 clear_bit(0, &fdc_busy); 913 wake_up(&fdc_wait); 914 } 915 916 /* switches the motor off after a given timeout */ 917 static void motor_off_callback(struct timer_list *t) 918 { 919 unsigned long nr = t - motor_off_timer; 920 unsigned char mask = ~(0x10 << UNIT(nr)); 921 922 if (WARN_ON_ONCE(nr >= N_DRIVE)) 923 return; 924 925 set_dor(FDC(nr), mask, 0); 926 } 927 928 /* schedules motor off */ 929 static void floppy_off(unsigned int drive) 930 { 931 unsigned long volatile delta; 932 int fdc = FDC(drive); 933 934 if (!(fdc_state[fdc].dor & (0x10 << UNIT(drive)))) 935 return; 936 937 timer_delete(motor_off_timer + drive); 938 939 /* make spindle stop in a position which minimizes spinup time 940 * next time */ 941 if (drive_params[drive].rps) { 942 delta = jiffies - drive_state[drive].first_read_date + HZ - 943 drive_params[drive].spindown_offset; 944 delta = ((delta * drive_params[drive].rps) % HZ) / drive_params[drive].rps; 945 motor_off_timer[drive].expires = 946 jiffies + drive_params[drive].spindown - delta; 947 } 948 add_timer(motor_off_timer + drive); 949 } 950 951 /* 952 * cycle through all N_DRIVE floppy drives, for disk change testing. 953 * stopping at current drive. This is done before any long operation, to 954 * be sure to have up to date disk change information. 955 */ 956 static void scandrives(void) 957 { 958 int i; 959 int drive; 960 int saved_drive; 961 962 if (drive_params[current_drive].select_delay) 963 return; 964 965 saved_drive = current_drive; 966 for (i = 0; i < N_DRIVE; i++) { 967 drive = (saved_drive + i + 1) % N_DRIVE; 968 if (drive_state[drive].fd_ref == 0 || drive_params[drive].select_delay != 0) 969 continue; /* skip closed drives */ 970 set_fdc(drive); 971 if (!(set_dor(current_fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) & 972 (0x10 << UNIT(drive)))) 973 /* switch the motor off again, if it was off to 974 * begin with */ 975 set_dor(current_fdc, ~(0x10 << UNIT(drive)), 0); 976 } 977 set_fdc(saved_drive); 978 } 979 980 static void empty(void) 981 { 982 } 983 984 static void empty_done(int result) 985 { 986 } 987 988 static void (*floppy_work_fn)(void); 989 990 static void floppy_work_workfn(struct work_struct *work) 991 { 992 void (*fn)(void) = READ_ONCE(floppy_work_fn); 993 994 fn(); 995 } 996 997 static DECLARE_WORK(floppy_work, floppy_work_workfn); 998 999 static void schedule_bh(void (*handler)(void)) 1000 { 1001 WARN_ON(work_pending(&floppy_work)); 1002 1003 WRITE_ONCE(floppy_work_fn, handler); 1004 queue_work(floppy_wq, &floppy_work); 1005 } 1006 1007 static void (*fd_timer_fn)(void) = NULL; 1008 1009 static void fd_timer_workfn(struct work_struct *work) 1010 { 1011 fd_timer_fn(); 1012 } 1013 1014 static DECLARE_DELAYED_WORK(fd_timer, fd_timer_workfn); 1015 1016 static void cancel_activity(void) 1017 { 1018 do_floppy = NULL; 1019 cancel_delayed_work(&fd_timer); 1020 cancel_work_sync(&floppy_work); 1021 } 1022 1023 /* this function makes sure that the disk stays in the drive during the 1024 * transfer */ 1025 static void fd_watchdog(void) 1026 { 1027 debug_dcl(drive_params[current_drive].flags, 1028 "calling disk change from watchdog\n"); 1029 1030 if (disk_change(current_drive)) { 1031 DPRINT("disk removed during i/o\n"); 1032 cancel_activity(); 1033 cont->done(0); 1034 reset_fdc(); 1035 } else { 1036 cancel_delayed_work(&fd_timer); 1037 fd_timer_fn = fd_watchdog; 1038 queue_delayed_work(floppy_wq, &fd_timer, HZ / 10); 1039 } 1040 } 1041 1042 static void main_command_interrupt(void) 1043 { 1044 cancel_delayed_work(&fd_timer); 1045 cont->interrupt(); 1046 } 1047 1048 /* waits for a delay (spinup or select) to pass */ 1049 static int fd_wait_for_completion(unsigned long expires, 1050 void (*function)(void)) 1051 { 1052 if (fdc_state[current_fdc].reset) { 1053 reset_fdc(); /* do the reset during sleep to win time 1054 * if we don't need to sleep, it's a good 1055 * occasion anyways */ 1056 return 1; 1057 } 1058 1059 if (time_before(jiffies, expires)) { 1060 cancel_delayed_work(&fd_timer); 1061 fd_timer_fn = function; 1062 queue_delayed_work(floppy_wq, &fd_timer, expires - jiffies); 1063 return 1; 1064 } 1065 return 0; 1066 } 1067 1068 static void setup_DMA(void) 1069 { 1070 unsigned long f; 1071 1072 if (raw_cmd->length == 0) { 1073 print_hex_dump(KERN_INFO, "zero dma transfer size: ", 1074 DUMP_PREFIX_NONE, 16, 1, 1075 raw_cmd->fullcmd, raw_cmd->cmd_count, false); 1076 cont->done(0); 1077 fdc_state[current_fdc].reset = 1; 1078 return; 1079 } 1080 if (((unsigned long)raw_cmd->kernel_data) % 512) { 1081 pr_info("non aligned address: %p\n", raw_cmd->kernel_data); 1082 cont->done(0); 1083 fdc_state[current_fdc].reset = 1; 1084 return; 1085 } 1086 f = claim_dma_lock(); 1087 fd_disable_dma(); 1088 #ifdef fd_dma_setup 1089 if (fd_dma_setup(raw_cmd->kernel_data, raw_cmd->length, 1090 (raw_cmd->flags & FD_RAW_READ) ? 1091 DMA_MODE_READ : DMA_MODE_WRITE, 1092 fdc_state[current_fdc].address) < 0) { 1093 release_dma_lock(f); 1094 cont->done(0); 1095 fdc_state[current_fdc].reset = 1; 1096 return; 1097 } 1098 release_dma_lock(f); 1099 #else 1100 fd_clear_dma_ff(); 1101 fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length); 1102 fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ) ? 1103 DMA_MODE_READ : DMA_MODE_WRITE); 1104 fd_set_dma_addr(raw_cmd->kernel_data); 1105 fd_set_dma_count(raw_cmd->length); 1106 virtual_dma_port = fdc_state[current_fdc].address; 1107 fd_enable_dma(); 1108 release_dma_lock(f); 1109 #endif 1110 } 1111 1112 static void show_floppy(int fdc); 1113 1114 /* waits until the fdc becomes ready */ 1115 static int wait_til_ready(int fdc) 1116 { 1117 int status; 1118 int counter; 1119 1120 if (fdc_state[fdc].reset) 1121 return -1; 1122 for (counter = 0; counter < 10000; counter++) { 1123 status = fdc_inb(fdc, FD_STATUS); 1124 if (status & STATUS_READY) 1125 return status; 1126 } 1127 if (initialized) { 1128 DPRINT("Getstatus times out (%x) on fdc %d\n", status, fdc); 1129 show_floppy(fdc); 1130 } 1131 fdc_state[fdc].reset = 1; 1132 return -1; 1133 } 1134 1135 /* sends a command byte to the fdc */ 1136 static int output_byte(int fdc, char byte) 1137 { 1138 int status = wait_til_ready(fdc); 1139 1140 if (status < 0) 1141 return -1; 1142 1143 if (is_ready_state(status)) { 1144 fdc_outb(byte, fdc, FD_DATA); 1145 output_log[output_log_pos].data = byte; 1146 output_log[output_log_pos].status = status; 1147 output_log[output_log_pos].jiffies = jiffies; 1148 output_log_pos = (output_log_pos + 1) % OLOGSIZE; 1149 return 0; 1150 } 1151 fdc_state[fdc].reset = 1; 1152 if (initialized) { 1153 DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n", 1154 byte, fdc, status); 1155 show_floppy(fdc); 1156 } 1157 return -1; 1158 } 1159 1160 /* gets the response from the fdc */ 1161 static int result(int fdc) 1162 { 1163 int i; 1164 int status = 0; 1165 1166 for (i = 0; i < FD_RAW_REPLY_SIZE; i++) { 1167 status = wait_til_ready(fdc); 1168 if (status < 0) 1169 break; 1170 status &= STATUS_DIR | STATUS_READY | STATUS_BUSY | STATUS_DMA; 1171 if ((status & ~STATUS_BUSY) == STATUS_READY) { 1172 resultjiffies = jiffies; 1173 resultsize = i; 1174 return i; 1175 } 1176 if (status == (STATUS_DIR | STATUS_READY | STATUS_BUSY)) 1177 reply_buffer[i] = fdc_inb(fdc, FD_DATA); 1178 else 1179 break; 1180 } 1181 if (initialized) { 1182 DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n", 1183 fdc, status, i); 1184 show_floppy(fdc); 1185 } 1186 fdc_state[fdc].reset = 1; 1187 return -1; 1188 } 1189 1190 #define MORE_OUTPUT -2 1191 /* does the fdc need more output? */ 1192 static int need_more_output(int fdc) 1193 { 1194 int status = wait_til_ready(fdc); 1195 1196 if (status < 0) 1197 return -1; 1198 1199 if (is_ready_state(status)) 1200 return MORE_OUTPUT; 1201 1202 return result(fdc); 1203 } 1204 1205 /* Set perpendicular mode as required, based on data rate, if supported. 1206 * 82077 Now tested. 1Mbps data rate only possible with 82077-1. 1207 */ 1208 static void perpendicular_mode(int fdc) 1209 { 1210 unsigned char perp_mode; 1211 1212 if (raw_cmd->rate & 0x40) { 1213 switch (raw_cmd->rate & 3) { 1214 case 0: 1215 perp_mode = 2; 1216 break; 1217 case 3: 1218 perp_mode = 3; 1219 break; 1220 default: 1221 DPRINT("Invalid data rate for perpendicular mode!\n"); 1222 cont->done(0); 1223 fdc_state[fdc].reset = 1; 1224 /* 1225 * convenient way to return to 1226 * redo without too much hassle 1227 * (deep stack et al.) 1228 */ 1229 return; 1230 } 1231 } else 1232 perp_mode = 0; 1233 1234 if (fdc_state[fdc].perp_mode == perp_mode) 1235 return; 1236 if (fdc_state[fdc].version >= FDC_82077_ORIG) { 1237 output_byte(fdc, FD_PERPENDICULAR); 1238 output_byte(fdc, perp_mode); 1239 fdc_state[fdc].perp_mode = perp_mode; 1240 } else if (perp_mode) { 1241 DPRINT("perpendicular mode not supported by this FDC.\n"); 1242 } 1243 } /* perpendicular_mode */ 1244 1245 static int fifo_depth = 0xa; 1246 static int no_fifo; 1247 1248 static int fdc_configure(int fdc) 1249 { 1250 /* Turn on FIFO */ 1251 output_byte(fdc, FD_CONFIGURE); 1252 if (need_more_output(fdc) != MORE_OUTPUT) 1253 return 0; 1254 output_byte(fdc, 0); 1255 output_byte(fdc, 0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf)); 1256 output_byte(fdc, 0); /* pre-compensation from track 0 upwards */ 1257 return 1; 1258 } 1259 1260 #define NOMINAL_DTR 500 1261 1262 /* Issue a "SPECIFY" command to set the step rate time, head unload time, 1263 * head load time, and DMA disable flag to values needed by floppy. 1264 * 1265 * The value "dtr" is the data transfer rate in Kbps. It is needed 1266 * to account for the data rate-based scaling done by the 82072 and 82077 1267 * FDC types. This parameter is ignored for other types of FDCs (i.e. 1268 * 8272a). 1269 * 1270 * Note that changing the data transfer rate has a (probably deleterious) 1271 * effect on the parameters subject to scaling for 82072/82077 FDCs, so 1272 * fdc_specify is called again after each data transfer rate 1273 * change. 1274 * 1275 * srt: 1000 to 16000 in microseconds 1276 * hut: 16 to 240 milliseconds 1277 * hlt: 2 to 254 milliseconds 1278 * 1279 * These values are rounded up to the next highest available delay time. 1280 */ 1281 static void fdc_specify(int fdc, int drive) 1282 { 1283 unsigned char spec1; 1284 unsigned char spec2; 1285 unsigned long srt; 1286 unsigned long hlt; 1287 unsigned long hut; 1288 unsigned long dtr = NOMINAL_DTR; 1289 unsigned long scale_dtr = NOMINAL_DTR; 1290 int hlt_max_code = 0x7f; 1291 int hut_max_code = 0xf; 1292 1293 if (fdc_state[fdc].need_configure && 1294 fdc_state[fdc].version >= FDC_82072A) { 1295 fdc_configure(fdc); 1296 fdc_state[fdc].need_configure = 0; 1297 } 1298 1299 switch (raw_cmd->rate & 0x03) { 1300 case 3: 1301 dtr = 1000; 1302 break; 1303 case 1: 1304 dtr = 300; 1305 if (fdc_state[fdc].version >= FDC_82078) { 1306 /* chose the default rate table, not the one 1307 * where 1 = 2 Mbps */ 1308 output_byte(fdc, FD_DRIVESPEC); 1309 if (need_more_output(fdc) == MORE_OUTPUT) { 1310 output_byte(fdc, UNIT(drive)); 1311 output_byte(fdc, 0xc0); 1312 } 1313 } 1314 break; 1315 case 2: 1316 dtr = 250; 1317 break; 1318 } 1319 1320 if (fdc_state[fdc].version >= FDC_82072) { 1321 scale_dtr = dtr; 1322 hlt_max_code = 0x00; /* 0==256msec*dtr0/dtr (not linear!) */ 1323 hut_max_code = 0x0; /* 0==256msec*dtr0/dtr (not linear!) */ 1324 } 1325 1326 /* Convert step rate from microseconds to milliseconds and 4 bits */ 1327 srt = 16 - DIV_ROUND_UP(drive_params[drive].srt * scale_dtr / 1000, 1328 NOMINAL_DTR); 1329 if (slow_floppy) 1330 srt = srt / 4; 1331 1332 SUPBOUND(srt, 0xf); 1333 INFBOUND(srt, 0); 1334 1335 hlt = DIV_ROUND_UP(drive_params[drive].hlt * scale_dtr / 2, 1336 NOMINAL_DTR); 1337 if (hlt < 0x01) 1338 hlt = 0x01; 1339 else if (hlt > 0x7f) 1340 hlt = hlt_max_code; 1341 1342 hut = DIV_ROUND_UP(drive_params[drive].hut * scale_dtr / 16, 1343 NOMINAL_DTR); 1344 if (hut < 0x1) 1345 hut = 0x1; 1346 else if (hut > 0xf) 1347 hut = hut_max_code; 1348 1349 spec1 = (srt << 4) | hut; 1350 spec2 = (hlt << 1) | (use_virtual_dma & 1); 1351 1352 /* If these parameters did not change, just return with success */ 1353 if (fdc_state[fdc].spec1 != spec1 || 1354 fdc_state[fdc].spec2 != spec2) { 1355 /* Go ahead and set spec1 and spec2 */ 1356 output_byte(fdc, FD_SPECIFY); 1357 output_byte(fdc, fdc_state[fdc].spec1 = spec1); 1358 output_byte(fdc, fdc_state[fdc].spec2 = spec2); 1359 } 1360 } /* fdc_specify */ 1361 1362 /* Set the FDC's data transfer rate on behalf of the specified drive. 1363 * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue 1364 * of the specify command (i.e. using the fdc_specify function). 1365 */ 1366 static int fdc_dtr(void) 1367 { 1368 /* If data rate not already set to desired value, set it. */ 1369 if ((raw_cmd->rate & 3) == fdc_state[current_fdc].dtr) 1370 return 0; 1371 1372 /* Set dtr */ 1373 fdc_outb(raw_cmd->rate & 3, current_fdc, FD_DCR); 1374 1375 /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB) 1376 * need a stabilization period of several milliseconds to be 1377 * enforced after data rate changes before R/W operations. 1378 * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies) 1379 */ 1380 fdc_state[current_fdc].dtr = raw_cmd->rate & 3; 1381 return fd_wait_for_completion(jiffies + 2UL * HZ / 100, floppy_ready); 1382 } /* fdc_dtr */ 1383 1384 static void tell_sector(void) 1385 { 1386 pr_cont(": track %d, head %d, sector %d, size %d", 1387 reply_buffer[R_TRACK], reply_buffer[R_HEAD], 1388 reply_buffer[R_SECTOR], 1389 reply_buffer[R_SIZECODE]); 1390 } /* tell_sector */ 1391 1392 static void print_errors(void) 1393 { 1394 DPRINT(""); 1395 if (reply_buffer[ST0] & ST0_ECE) { 1396 pr_cont("Recalibrate failed!"); 1397 } else if (reply_buffer[ST2] & ST2_CRC) { 1398 pr_cont("data CRC error"); 1399 tell_sector(); 1400 } else if (reply_buffer[ST1] & ST1_CRC) { 1401 pr_cont("CRC error"); 1402 tell_sector(); 1403 } else if ((reply_buffer[ST1] & (ST1_MAM | ST1_ND)) || 1404 (reply_buffer[ST2] & ST2_MAM)) { 1405 if (!probing) { 1406 pr_cont("sector not found"); 1407 tell_sector(); 1408 } else 1409 pr_cont("probe failed..."); 1410 } else if (reply_buffer[ST2] & ST2_WC) { /* seek error */ 1411 pr_cont("wrong cylinder"); 1412 } else if (reply_buffer[ST2] & ST2_BC) { /* cylinder marked as bad */ 1413 pr_cont("bad cylinder"); 1414 } else { 1415 pr_cont("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x", 1416 reply_buffer[ST0], reply_buffer[ST1], 1417 reply_buffer[ST2]); 1418 tell_sector(); 1419 } 1420 pr_cont("\n"); 1421 } 1422 1423 /* 1424 * OK, this error interpreting routine is called after a 1425 * DMA read/write has succeeded 1426 * or failed, so we check the results, and copy any buffers. 1427 * hhb: Added better error reporting. 1428 * ak: Made this into a separate routine. 1429 */ 1430 static int interpret_errors(void) 1431 { 1432 char bad; 1433 1434 if (inr != 7) { 1435 DPRINT("-- FDC reply error\n"); 1436 fdc_state[current_fdc].reset = 1; 1437 return 1; 1438 } 1439 1440 /* check IC to find cause of interrupt */ 1441 switch (reply_buffer[ST0] & ST0_INTR) { 1442 case 0x40: /* error occurred during command execution */ 1443 if (reply_buffer[ST1] & ST1_EOC) 1444 return 0; /* occurs with pseudo-DMA */ 1445 bad = 1; 1446 if (reply_buffer[ST1] & ST1_WP) { 1447 DPRINT("Drive is write protected\n"); 1448 clear_bit(FD_DISK_WRITABLE_BIT, 1449 &drive_state[current_drive].flags); 1450 cont->done(0); 1451 bad = 2; 1452 } else if (reply_buffer[ST1] & ST1_ND) { 1453 set_bit(FD_NEED_TWADDLE_BIT, 1454 &drive_state[current_drive].flags); 1455 } else if (reply_buffer[ST1] & ST1_OR) { 1456 if (drive_params[current_drive].flags & FTD_MSG) 1457 DPRINT("Over/Underrun - retrying\n"); 1458 bad = 0; 1459 } else if (floppy_errors >= drive_params[current_drive].max_errors.reporting) { 1460 print_errors(); 1461 } 1462 if (reply_buffer[ST2] & ST2_WC || reply_buffer[ST2] & ST2_BC) 1463 /* wrong cylinder => recal */ 1464 drive_state[current_drive].track = NEED_2_RECAL; 1465 return bad; 1466 case 0x80: /* invalid command given */ 1467 DPRINT("Invalid FDC command given!\n"); 1468 cont->done(0); 1469 return 2; 1470 case 0xc0: 1471 DPRINT("Abnormal termination caused by polling\n"); 1472 cont->error(); 1473 return 2; 1474 default: /* (0) Normal command termination */ 1475 return 0; 1476 } 1477 } 1478 1479 /* 1480 * This routine is called when everything should be correctly set up 1481 * for the transfer (i.e. floppy motor is on, the correct floppy is 1482 * selected, and the head is sitting on the right track). 1483 */ 1484 static void setup_rw_floppy(void) 1485 { 1486 int i; 1487 int r; 1488 int flags; 1489 unsigned long ready_date; 1490 void (*function)(void); 1491 1492 flags = raw_cmd->flags; 1493 if (flags & (FD_RAW_READ | FD_RAW_WRITE)) 1494 flags |= FD_RAW_INTR; 1495 1496 if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)) { 1497 ready_date = drive_state[current_drive].spinup_date + drive_params[current_drive].spinup; 1498 /* If spinup will take a long time, rerun scandrives 1499 * again just before spinup completion. Beware that 1500 * after scandrives, we must again wait for selection. 1501 */ 1502 if (time_after(ready_date, jiffies + drive_params[current_drive].select_delay)) { 1503 ready_date -= drive_params[current_drive].select_delay; 1504 function = floppy_start; 1505 } else 1506 function = setup_rw_floppy; 1507 1508 /* wait until the floppy is spinning fast enough */ 1509 if (fd_wait_for_completion(ready_date, function)) 1510 return; 1511 } 1512 if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE)) 1513 setup_DMA(); 1514 1515 if (flags & FD_RAW_INTR) 1516 do_floppy = main_command_interrupt; 1517 1518 r = 0; 1519 for (i = 0; i < raw_cmd->cmd_count; i++) 1520 r |= output_byte(current_fdc, raw_cmd->fullcmd[i]); 1521 1522 debugt(__func__, "rw_command"); 1523 1524 if (r) { 1525 cont->error(); 1526 reset_fdc(); 1527 return; 1528 } 1529 1530 if (!(flags & FD_RAW_INTR)) { 1531 inr = result(current_fdc); 1532 cont->interrupt(); 1533 } else if (flags & FD_RAW_NEED_DISK) 1534 fd_watchdog(); 1535 } 1536 1537 static int blind_seek; 1538 1539 /* 1540 * This is the routine called after every seek (or recalibrate) interrupt 1541 * from the floppy controller. 1542 */ 1543 static void seek_interrupt(void) 1544 { 1545 debugt(__func__, ""); 1546 if (inr != 2 || (reply_buffer[ST0] & 0xF8) != 0x20) { 1547 DPRINT("seek failed\n"); 1548 drive_state[current_drive].track = NEED_2_RECAL; 1549 cont->error(); 1550 cont->redo(); 1551 return; 1552 } 1553 if (drive_state[current_drive].track >= 0 && 1554 drive_state[current_drive].track != reply_buffer[ST1] && 1555 !blind_seek) { 1556 debug_dcl(drive_params[current_drive].flags, 1557 "clearing NEWCHANGE flag because of effective seek\n"); 1558 debug_dcl(drive_params[current_drive].flags, "jiffies=%lu\n", 1559 jiffies); 1560 clear_bit(FD_DISK_NEWCHANGE_BIT, 1561 &drive_state[current_drive].flags); 1562 /* effective seek */ 1563 drive_state[current_drive].select_date = jiffies; 1564 } 1565 drive_state[current_drive].track = reply_buffer[ST1]; 1566 floppy_ready(); 1567 } 1568 1569 static void check_wp(int fdc, int drive) 1570 { 1571 if (test_bit(FD_VERIFY_BIT, &drive_state[drive].flags)) { 1572 /* check write protection */ 1573 output_byte(fdc, FD_GETSTATUS); 1574 output_byte(fdc, UNIT(drive)); 1575 if (result(fdc) != 1) { 1576 fdc_state[fdc].reset = 1; 1577 return; 1578 } 1579 clear_bit(FD_VERIFY_BIT, &drive_state[drive].flags); 1580 clear_bit(FD_NEED_TWADDLE_BIT, 1581 &drive_state[drive].flags); 1582 debug_dcl(drive_params[drive].flags, 1583 "checking whether disk is write protected\n"); 1584 debug_dcl(drive_params[drive].flags, "wp=%x\n", 1585 reply_buffer[ST3] & 0x40); 1586 if (!(reply_buffer[ST3] & 0x40)) 1587 set_bit(FD_DISK_WRITABLE_BIT, 1588 &drive_state[drive].flags); 1589 else 1590 clear_bit(FD_DISK_WRITABLE_BIT, 1591 &drive_state[drive].flags); 1592 } 1593 } 1594 1595 static void seek_floppy(void) 1596 { 1597 int track; 1598 1599 blind_seek = 0; 1600 1601 debug_dcl(drive_params[current_drive].flags, 1602 "calling disk change from %s\n", __func__); 1603 1604 if (!test_bit(FD_DISK_NEWCHANGE_BIT, &drive_state[current_drive].flags) && 1605 disk_change(current_drive) && (raw_cmd->flags & FD_RAW_NEED_DISK)) { 1606 /* the media changed flag should be cleared after the seek. 1607 * If it isn't, this means that there is really no disk in 1608 * the drive. 1609 */ 1610 set_bit(FD_DISK_CHANGED_BIT, 1611 &drive_state[current_drive].flags); 1612 cont->done(0); 1613 cont->redo(); 1614 return; 1615 } 1616 if (drive_state[current_drive].track <= NEED_1_RECAL) { 1617 recalibrate_floppy(); 1618 return; 1619 } else if (test_bit(FD_DISK_NEWCHANGE_BIT, &drive_state[current_drive].flags) && 1620 (raw_cmd->flags & FD_RAW_NEED_DISK) && 1621 (drive_state[current_drive].track <= NO_TRACK || drive_state[current_drive].track == raw_cmd->track)) { 1622 /* we seek to clear the media-changed condition. Does anybody 1623 * know a more elegant way, which works on all drives? */ 1624 if (raw_cmd->track) 1625 track = raw_cmd->track - 1; 1626 else { 1627 if (drive_params[current_drive].flags & FD_SILENT_DCL_CLEAR) { 1628 set_dor(current_fdc, ~(0x10 << UNIT(current_drive)), 0); 1629 blind_seek = 1; 1630 raw_cmd->flags |= FD_RAW_NEED_SEEK; 1631 } 1632 track = 1; 1633 } 1634 } else { 1635 check_wp(current_fdc, current_drive); 1636 if (raw_cmd->track != drive_state[current_drive].track && 1637 (raw_cmd->flags & FD_RAW_NEED_SEEK)) 1638 track = raw_cmd->track; 1639 else { 1640 setup_rw_floppy(); 1641 return; 1642 } 1643 } 1644 1645 do_floppy = seek_interrupt; 1646 output_byte(current_fdc, FD_SEEK); 1647 output_byte(current_fdc, UNIT(current_drive)); 1648 if (output_byte(current_fdc, track) < 0) { 1649 reset_fdc(); 1650 return; 1651 } 1652 debugt(__func__, ""); 1653 } 1654 1655 static void recal_interrupt(void) 1656 { 1657 debugt(__func__, ""); 1658 if (inr != 2) 1659 fdc_state[current_fdc].reset = 1; 1660 else if (reply_buffer[ST0] & ST0_ECE) { 1661 switch (drive_state[current_drive].track) { 1662 case NEED_1_RECAL: 1663 debugt(__func__, "need 1 recal"); 1664 /* after a second recalibrate, we still haven't 1665 * reached track 0. Probably no drive. Raise an 1666 * error, as failing immediately might upset 1667 * computers possessed by the Devil :-) */ 1668 cont->error(); 1669 cont->redo(); 1670 return; 1671 case NEED_2_RECAL: 1672 debugt(__func__, "need 2 recal"); 1673 /* If we already did a recalibrate, 1674 * and we are not at track 0, this 1675 * means we have moved. (The only way 1676 * not to move at recalibration is to 1677 * be already at track 0.) Clear the 1678 * new change flag */ 1679 debug_dcl(drive_params[current_drive].flags, 1680 "clearing NEWCHANGE flag because of second recalibrate\n"); 1681 1682 clear_bit(FD_DISK_NEWCHANGE_BIT, 1683 &drive_state[current_drive].flags); 1684 drive_state[current_drive].select_date = jiffies; 1685 fallthrough; 1686 default: 1687 debugt(__func__, "default"); 1688 /* Recalibrate moves the head by at 1689 * most 80 steps. If after one 1690 * recalibrate we don't have reached 1691 * track 0, this might mean that we 1692 * started beyond track 80. Try 1693 * again. */ 1694 drive_state[current_drive].track = NEED_1_RECAL; 1695 break; 1696 } 1697 } else 1698 drive_state[current_drive].track = reply_buffer[ST1]; 1699 floppy_ready(); 1700 } 1701 1702 static void print_result(char *message, int inr) 1703 { 1704 int i; 1705 1706 DPRINT("%s ", message); 1707 if (inr >= 0) 1708 for (i = 0; i < inr; i++) 1709 pr_cont("repl[%d]=%x ", i, reply_buffer[i]); 1710 pr_cont("\n"); 1711 } 1712 1713 /* interrupt handler. Note that this can be called externally on the Sparc */ 1714 irqreturn_t floppy_interrupt(int irq, void *dev_id) 1715 { 1716 int do_print; 1717 unsigned long f; 1718 void (*handler)(void) = do_floppy; 1719 1720 lasthandler = handler; 1721 interruptjiffies = jiffies; 1722 1723 f = claim_dma_lock(); 1724 fd_disable_dma(); 1725 release_dma_lock(f); 1726 1727 do_floppy = NULL; 1728 if (current_fdc >= N_FDC || fdc_state[current_fdc].address == -1) { 1729 /* we don't even know which FDC is the culprit */ 1730 pr_info("DOR0=%x\n", fdc_state[0].dor); 1731 pr_info("floppy interrupt on bizarre fdc %d\n", current_fdc); 1732 pr_info("handler=%ps\n", handler); 1733 is_alive(__func__, "bizarre fdc"); 1734 return IRQ_NONE; 1735 } 1736 1737 fdc_state[current_fdc].reset = 0; 1738 /* We have to clear the reset flag here, because apparently on boxes 1739 * with level triggered interrupts (PS/2, Sparc, ...), it is needed to 1740 * emit SENSEI's to clear the interrupt line. And fdc_state[fdc].reset 1741 * blocks the emission of the SENSEI's. 1742 * It is OK to emit floppy commands because we are in an interrupt 1743 * handler here, and thus we have to fear no interference of other 1744 * activity. 1745 */ 1746 1747 do_print = !handler && print_unex && initialized; 1748 1749 inr = result(current_fdc); 1750 if (do_print) 1751 print_result("unexpected interrupt", inr); 1752 if (inr == 0) { 1753 int max_sensei = 4; 1754 do { 1755 output_byte(current_fdc, FD_SENSEI); 1756 inr = result(current_fdc); 1757 if (do_print) 1758 print_result("sensei", inr); 1759 max_sensei--; 1760 } while ((reply_buffer[ST0] & 0x83) != UNIT(current_drive) && 1761 inr == 2 && max_sensei); 1762 } 1763 if (!handler) { 1764 fdc_state[current_fdc].reset = 1; 1765 return IRQ_NONE; 1766 } 1767 schedule_bh(handler); 1768 is_alive(__func__, "normal interrupt end"); 1769 1770 /* FIXME! Was it really for us? */ 1771 return IRQ_HANDLED; 1772 } 1773 1774 static void recalibrate_floppy(void) 1775 { 1776 debugt(__func__, ""); 1777 do_floppy = recal_interrupt; 1778 output_byte(current_fdc, FD_RECALIBRATE); 1779 if (output_byte(current_fdc, UNIT(current_drive)) < 0) 1780 reset_fdc(); 1781 } 1782 1783 /* 1784 * Must do 4 FD_SENSEIs after reset because of ``drive polling''. 1785 */ 1786 static void reset_interrupt(void) 1787 { 1788 debugt(__func__, ""); 1789 result(current_fdc); /* get the status ready for set_fdc */ 1790 /* Stale work: unlock_fdc()/do_wakeup() may have cleared cont. */ 1791 if (!cont) 1792 return; 1793 if (fdc_state[current_fdc].reset) { 1794 pr_info("reset set in interrupt, calling %ps\n", cont->error); 1795 cont->error(); /* a reset just after a reset. BAD! */ 1796 } 1797 cont->redo(); 1798 } 1799 1800 /* 1801 * reset is done by pulling bit 2 of DOR low for a while (old FDCs), 1802 * or by setting the self clearing bit 7 of STATUS (newer FDCs). 1803 * This WILL trigger an interrupt, causing the handlers in the current 1804 * cont's ->redo() to be called via reset_interrupt(). 1805 */ 1806 static void reset_fdc(void) 1807 { 1808 unsigned long flags; 1809 1810 do_floppy = reset_interrupt; 1811 fdc_state[current_fdc].reset = 0; 1812 reset_fdc_info(current_fdc, 0); 1813 1814 /* Pseudo-DMA may intercept 'reset finished' interrupt. */ 1815 /* Irrelevant for systems with true DMA (i386). */ 1816 1817 flags = claim_dma_lock(); 1818 fd_disable_dma(); 1819 release_dma_lock(flags); 1820 1821 if (fdc_state[current_fdc].version >= FDC_82072A) 1822 fdc_outb(0x80 | (fdc_state[current_fdc].dtr & 3), 1823 current_fdc, FD_STATUS); 1824 else { 1825 fdc_outb(fdc_state[current_fdc].dor & ~0x04, current_fdc, FD_DOR); 1826 udelay(FD_RESET_DELAY); 1827 fdc_outb(fdc_state[current_fdc].dor, current_fdc, FD_DOR); 1828 } 1829 } 1830 1831 static void show_floppy(int fdc) 1832 { 1833 int i; 1834 1835 pr_info("\n"); 1836 pr_info("floppy driver state\n"); 1837 pr_info("-------------------\n"); 1838 pr_info("now=%lu last interrupt=%lu diff=%lu last called handler=%ps\n", 1839 jiffies, interruptjiffies, jiffies - interruptjiffies, 1840 lasthandler); 1841 1842 pr_info("timeout_message=%s\n", timeout_message); 1843 pr_info("last output bytes:\n"); 1844 for (i = 0; i < OLOGSIZE; i++) 1845 pr_info("%2x %2x %lu\n", 1846 output_log[(i + output_log_pos) % OLOGSIZE].data, 1847 output_log[(i + output_log_pos) % OLOGSIZE].status, 1848 output_log[(i + output_log_pos) % OLOGSIZE].jiffies); 1849 pr_info("last result at %lu\n", resultjiffies); 1850 pr_info("last redo_fd_request at %lu\n", lastredo); 1851 print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1, 1852 reply_buffer, resultsize, true); 1853 1854 pr_info("status=%x\n", fdc_inb(fdc, FD_STATUS)); 1855 pr_info("fdc_busy=%lu\n", fdc_busy); 1856 if (do_floppy) 1857 pr_info("do_floppy=%ps\n", do_floppy); 1858 if (work_pending(&floppy_work)) 1859 pr_info("floppy_work.func=%ps\n", floppy_work.func); 1860 if (delayed_work_pending(&fd_timer)) 1861 pr_info("delayed work.function=%p expires=%ld\n", 1862 fd_timer.work.func, 1863 fd_timer.timer.expires - jiffies); 1864 if (delayed_work_pending(&fd_timeout)) 1865 pr_info("timer_function=%p expires=%ld\n", 1866 fd_timeout.work.func, 1867 fd_timeout.timer.expires - jiffies); 1868 1869 pr_info("cont=%p\n", cont); 1870 pr_info("current_req=%p\n", current_req); 1871 pr_info("command_status=%d\n", READ_ONCE(command_status)); 1872 pr_info("\n"); 1873 } 1874 1875 static void floppy_shutdown(struct work_struct *arg) 1876 { 1877 unsigned long flags; 1878 1879 if (initialized) 1880 show_floppy(current_fdc); 1881 cancel_activity(); 1882 1883 flags = claim_dma_lock(); 1884 fd_disable_dma(); 1885 release_dma_lock(flags); 1886 1887 /* avoid dma going to a random drive after shutdown */ 1888 1889 if (initialized) 1890 DPRINT("floppy timeout called\n"); 1891 fdc_state[current_fdc].reset = 1; 1892 if (cont) { 1893 cont->done(0); 1894 cont->redo(); /* this will recall reset when needed */ 1895 } else { 1896 pr_info("no cont in shutdown!\n"); 1897 process_fd_request(); 1898 } 1899 is_alive(__func__, ""); 1900 } 1901 1902 /* start motor, check media-changed condition and write protection */ 1903 static int start_motor(void (*function)(void)) 1904 { 1905 int mask; 1906 int data; 1907 1908 mask = 0xfc; 1909 data = UNIT(current_drive); 1910 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)) { 1911 if (!(fdc_state[current_fdc].dor & (0x10 << UNIT(current_drive)))) { 1912 set_debugt(); 1913 /* no read since this drive is running */ 1914 drive_state[current_drive].first_read_date = 0; 1915 /* note motor start time if motor is not yet running */ 1916 drive_state[current_drive].spinup_date = jiffies; 1917 data |= (0x10 << UNIT(current_drive)); 1918 } 1919 } else if (fdc_state[current_fdc].dor & (0x10 << UNIT(current_drive))) 1920 mask &= ~(0x10 << UNIT(current_drive)); 1921 1922 /* starts motor and selects floppy */ 1923 timer_delete(motor_off_timer + current_drive); 1924 set_dor(current_fdc, mask, data); 1925 1926 /* wait_for_completion also schedules reset if needed. */ 1927 return fd_wait_for_completion(drive_state[current_drive].select_date + drive_params[current_drive].select_delay, 1928 function); 1929 } 1930 1931 static void floppy_ready(void) 1932 { 1933 if (fdc_state[current_fdc].reset) { 1934 reset_fdc(); 1935 return; 1936 } 1937 if (start_motor(floppy_ready)) 1938 return; 1939 if (fdc_dtr()) 1940 return; 1941 1942 debug_dcl(drive_params[current_drive].flags, 1943 "calling disk change from floppy_ready\n"); 1944 if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) && 1945 disk_change(current_drive) && !drive_params[current_drive].select_delay) 1946 twaddle(current_fdc, current_drive); /* this clears the dcl on certain 1947 * drive/controller combinations */ 1948 1949 #ifdef fd_chose_dma_mode 1950 if ((raw_cmd->flags & FD_RAW_READ) || (raw_cmd->flags & FD_RAW_WRITE)) { 1951 unsigned long flags = claim_dma_lock(); 1952 fd_chose_dma_mode(raw_cmd->kernel_data, raw_cmd->length); 1953 release_dma_lock(flags); 1954 } 1955 #endif 1956 1957 if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)) { 1958 perpendicular_mode(current_fdc); 1959 fdc_specify(current_fdc, current_drive); /* must be done here because of hut, hlt ... */ 1960 seek_floppy(); 1961 } else { 1962 if ((raw_cmd->flags & FD_RAW_READ) || 1963 (raw_cmd->flags & FD_RAW_WRITE)) 1964 fdc_specify(current_fdc, current_drive); 1965 setup_rw_floppy(); 1966 } 1967 } 1968 1969 static void floppy_start(void) 1970 { 1971 reschedule_timeout(current_drive, "floppy start"); 1972 1973 scandrives(); 1974 debug_dcl(drive_params[current_drive].flags, 1975 "setting NEWCHANGE in floppy_start\n"); 1976 set_bit(FD_DISK_NEWCHANGE_BIT, &drive_state[current_drive].flags); 1977 floppy_ready(); 1978 } 1979 1980 /* 1981 * ======================================================================== 1982 * here ends the bottom half. Exported routines are: 1983 * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc, 1984 * start_motor, reset_fdc, reset_fdc_info, interpret_errors. 1985 * Initialization also uses output_byte, result, set_dor, floppy_interrupt 1986 * and set_dor. 1987 * ======================================================================== 1988 */ 1989 /* 1990 * General purpose continuations. 1991 * ============================== 1992 */ 1993 1994 static void do_wakeup(void) 1995 { 1996 reschedule_timeout(MAXTIMEOUT, "do wakeup"); 1997 cont = NULL; 1998 WRITE_ONCE(command_status, READ_ONCE(command_status) + 2); 1999 wake_up(&command_done); 2000 } 2001 2002 static const struct cont_t wakeup_cont = { 2003 .interrupt = empty, 2004 .redo = do_wakeup, 2005 .error = empty, 2006 .done = empty_done, 2007 }; 2008 2009 static const struct cont_t intr_cont = { 2010 .interrupt = empty, 2011 .redo = process_fd_request, 2012 .error = empty, 2013 .done = empty_done, 2014 }; 2015 2016 /* schedules handler, waiting for completion. May be interrupted, will then 2017 * return -EINTR, in which case the driver will automatically be unlocked. 2018 */ 2019 static int wait_til_done(void (*handler)(void), bool interruptible) 2020 { 2021 int ret; 2022 2023 schedule_bh(handler); 2024 2025 if (interruptible) 2026 wait_event_interruptible(command_done, 2027 READ_ONCE(command_status) >= 2); 2028 else 2029 wait_event(command_done, READ_ONCE(command_status) >= 2); 2030 2031 if (READ_ONCE(command_status) < 2) { 2032 cancel_activity(); 2033 cont = &intr_cont; 2034 reset_fdc(); 2035 return -EINTR; 2036 } 2037 2038 if (fdc_state[current_fdc].reset) 2039 WRITE_ONCE(command_status, FD_COMMAND_ERROR); 2040 if (READ_ONCE(command_status) == FD_COMMAND_OKAY) 2041 ret = 0; 2042 else 2043 ret = -EIO; 2044 WRITE_ONCE(command_status, FD_COMMAND_NONE); 2045 return ret; 2046 } 2047 2048 static void generic_done(int result) 2049 { 2050 WRITE_ONCE(command_status, result); 2051 cont = &wakeup_cont; 2052 } 2053 2054 static void generic_success(void) 2055 { 2056 cont->done(1); 2057 } 2058 2059 static void generic_failure(void) 2060 { 2061 cont->done(0); 2062 } 2063 2064 static void success_and_wakeup(void) 2065 { 2066 generic_success(); 2067 cont->redo(); 2068 } 2069 2070 /* 2071 * formatting and rw support. 2072 * ========================== 2073 */ 2074 2075 static int next_valid_format(int drive) 2076 { 2077 int probed_format; 2078 2079 probed_format = drive_state[drive].probed_format; 2080 while (1) { 2081 if (probed_format >= FD_AUTODETECT_SIZE || 2082 !drive_params[drive].autodetect[probed_format]) { 2083 drive_state[drive].probed_format = 0; 2084 return 1; 2085 } 2086 if (floppy_type[drive_params[drive].autodetect[probed_format]].sect) { 2087 drive_state[drive].probed_format = probed_format; 2088 return 0; 2089 } 2090 probed_format++; 2091 } 2092 } 2093 2094 static void bad_flp_intr(void) 2095 { 2096 int err_count; 2097 2098 if (probing) { 2099 drive_state[current_drive].probed_format++; 2100 if (!next_valid_format(current_drive)) 2101 return; 2102 } 2103 err_count = ++floppy_errors; 2104 INFBOUND(write_errors[current_drive].badness, err_count); 2105 if (err_count > drive_params[current_drive].max_errors.abort) 2106 cont->done(0); 2107 if (err_count > drive_params[current_drive].max_errors.reset) 2108 fdc_state[current_fdc].reset = 1; 2109 else if (err_count > drive_params[current_drive].max_errors.recal) 2110 drive_state[current_drive].track = NEED_2_RECAL; 2111 } 2112 2113 static void set_floppy(int drive) 2114 { 2115 int type = ITYPE(drive_state[drive].fd_device); 2116 2117 if (type) 2118 _floppy = floppy_type + type; 2119 else 2120 _floppy = current_type[drive]; 2121 } 2122 2123 /* 2124 * formatting support. 2125 * =================== 2126 */ 2127 static void format_interrupt(void) 2128 { 2129 switch (interpret_errors()) { 2130 case 1: 2131 cont->error(); 2132 break; 2133 case 2: 2134 break; 2135 case 0: 2136 cont->done(1); 2137 } 2138 cont->redo(); 2139 } 2140 2141 #define FM_MODE(x, y) ((y) & ~(((x)->rate & 0x80) >> 1)) 2142 #define CT(x) ((x) | 0xc0) 2143 2144 static void setup_format_params(int track) 2145 { 2146 int n; 2147 int il; 2148 int count; 2149 int head_shift; 2150 int track_shift; 2151 struct fparm { 2152 unsigned char track, head, sect, size; 2153 } *here = (struct fparm *)floppy_track_buffer; 2154 2155 raw_cmd = &default_raw_cmd; 2156 raw_cmd->track = track; 2157 2158 raw_cmd->flags = (FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN | 2159 FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK); 2160 raw_cmd->rate = _floppy->rate & 0x43; 2161 raw_cmd->cmd_count = NR_F; 2162 raw_cmd->cmd[COMMAND] = FM_MODE(_floppy, FD_FORMAT); 2163 raw_cmd->cmd[DR_SELECT] = UNIT(current_drive) + PH_HEAD(_floppy, format_req.head); 2164 raw_cmd->cmd[F_SIZECODE] = FD_SIZECODE(_floppy); 2165 raw_cmd->cmd[F_SECT_PER_TRACK] = _floppy->sect << 2 >> raw_cmd->cmd[F_SIZECODE]; 2166 raw_cmd->cmd[F_GAP] = _floppy->fmt_gap; 2167 raw_cmd->cmd[F_FILL] = FD_FILL_BYTE; 2168 2169 raw_cmd->kernel_data = floppy_track_buffer; 2170 raw_cmd->length = 4 * raw_cmd->cmd[F_SECT_PER_TRACK]; 2171 2172 if (!raw_cmd->cmd[F_SECT_PER_TRACK]) 2173 return; 2174 2175 /* allow for about 30ms for data transport per track */ 2176 head_shift = (raw_cmd->cmd[F_SECT_PER_TRACK] + 5) / 6; 2177 2178 /* a ``cylinder'' is two tracks plus a little stepping time */ 2179 track_shift = 2 * head_shift + 3; 2180 2181 /* position of logical sector 1 on this track */ 2182 n = (track_shift * format_req.track + head_shift * format_req.head) 2183 % raw_cmd->cmd[F_SECT_PER_TRACK]; 2184 2185 /* determine interleave */ 2186 il = 1; 2187 if (_floppy->fmt_gap < 0x22) 2188 il++; 2189 2190 /* initialize field */ 2191 for (count = 0; count < raw_cmd->cmd[F_SECT_PER_TRACK]; ++count) { 2192 here[count].track = format_req.track; 2193 here[count].head = format_req.head; 2194 here[count].sect = 0; 2195 here[count].size = raw_cmd->cmd[F_SIZECODE]; 2196 } 2197 /* place logical sectors */ 2198 for (count = 1; count <= raw_cmd->cmd[F_SECT_PER_TRACK]; ++count) { 2199 here[n].sect = count; 2200 n = (n + il) % raw_cmd->cmd[F_SECT_PER_TRACK]; 2201 if (here[n].sect) { /* sector busy, find next free sector */ 2202 ++n; 2203 if (n >= raw_cmd->cmd[F_SECT_PER_TRACK]) { 2204 n -= raw_cmd->cmd[F_SECT_PER_TRACK]; 2205 while (here[n].sect) 2206 ++n; 2207 } 2208 } 2209 } 2210 if (_floppy->stretch & FD_SECTBASEMASK) { 2211 for (count = 0; count < raw_cmd->cmd[F_SECT_PER_TRACK]; count++) 2212 here[count].sect += FD_SECTBASE(_floppy) - 1; 2213 } 2214 } 2215 2216 static void redo_format(void) 2217 { 2218 buffer_track = -1; 2219 setup_format_params(format_req.track << STRETCH(_floppy)); 2220 floppy_start(); 2221 debugt(__func__, "queue format request"); 2222 } 2223 2224 static const struct cont_t format_cont = { 2225 .interrupt = format_interrupt, 2226 .redo = redo_format, 2227 .error = bad_flp_intr, 2228 .done = generic_done 2229 }; 2230 2231 static int do_format(int drive, struct format_descr *tmp_format_req) 2232 { 2233 int ret; 2234 2235 if (lock_fdc(drive)) 2236 return -EINTR; 2237 2238 set_floppy(drive); 2239 if (!_floppy || 2240 _floppy->track > drive_params[current_drive].tracks || 2241 tmp_format_req->track >= _floppy->track || 2242 tmp_format_req->head >= _floppy->head || 2243 (_floppy->sect << 2) % (1 << FD_SIZECODE(_floppy)) || 2244 !_floppy->fmt_gap) { 2245 process_fd_request(); 2246 return -EINVAL; 2247 } 2248 format_req = *tmp_format_req; 2249 cont = &format_cont; 2250 floppy_errors = 0; 2251 ret = wait_til_done(redo_format, true); 2252 if (ret == -EINTR) 2253 return -EINTR; 2254 process_fd_request(); 2255 return ret; 2256 } 2257 2258 /* 2259 * Buffer read/write and support 2260 * ============================= 2261 */ 2262 2263 static void floppy_end_request(struct request *req, blk_status_t error) 2264 { 2265 unsigned int nr_sectors = current_count_sectors; 2266 unsigned int drive = (unsigned long)req->q->disk->private_data; 2267 2268 /* current_count_sectors can be zero if transfer failed */ 2269 if (error) 2270 nr_sectors = blk_rq_cur_sectors(req); 2271 if (blk_update_request(req, error, nr_sectors << 9)) 2272 return; 2273 __blk_mq_end_request(req, error); 2274 2275 /* We're done with the request */ 2276 floppy_off(drive); 2277 current_req = NULL; 2278 } 2279 2280 /* new request_done. Can handle physical sectors which are smaller than a 2281 * logical buffer */ 2282 static void request_done(int uptodate) 2283 { 2284 struct request *req = current_req; 2285 int block; 2286 2287 probing = 0; 2288 reschedule_timeout(MAXTIMEOUT, 2289 uptodate ? "request done 1" : "request done 0"); 2290 2291 if (!req) { 2292 pr_info("floppy.c: no request in request_done\n"); 2293 return; 2294 } 2295 2296 if (uptodate) { 2297 /* maintain values for invalidation on geometry 2298 * change */ 2299 block = current_count_sectors + blk_rq_pos(req); 2300 INFBOUND(drive_state[current_drive].maxblock, block); 2301 if (block > _floppy->sect) 2302 drive_state[current_drive].maxtrack = 1; 2303 2304 floppy_end_request(req, 0); 2305 } else { 2306 if (rq_data_dir(req) == WRITE) { 2307 /* record write error information */ 2308 write_errors[current_drive].write_errors++; 2309 if (write_errors[current_drive].write_errors == 1) { 2310 write_errors[current_drive].first_error_sector = blk_rq_pos(req); 2311 write_errors[current_drive].first_error_generation = drive_state[current_drive].generation; 2312 } 2313 write_errors[current_drive].last_error_sector = blk_rq_pos(req); 2314 write_errors[current_drive].last_error_generation = drive_state[current_drive].generation; 2315 } 2316 floppy_end_request(req, BLK_STS_IOERR); 2317 } 2318 } 2319 2320 /* Interrupt handler evaluating the result of the r/w operation */ 2321 static void rw_interrupt(void) 2322 { 2323 int eoc; 2324 int ssize; 2325 int heads; 2326 int nr_sectors; 2327 2328 if (reply_buffer[R_HEAD] >= 2) { 2329 /* some Toshiba floppy controllers occasionnally seem to 2330 * return bogus interrupts after read/write operations, which 2331 * can be recognized by a bad head number (>= 2) */ 2332 return; 2333 } 2334 2335 if (!drive_state[current_drive].first_read_date) 2336 drive_state[current_drive].first_read_date = jiffies; 2337 2338 ssize = DIV_ROUND_UP(1 << raw_cmd->cmd[SIZECODE], 4); 2339 2340 if (reply_buffer[ST1] & ST1_EOC) 2341 eoc = 1; 2342 else 2343 eoc = 0; 2344 2345 if (raw_cmd->cmd[COMMAND] & 0x80) 2346 heads = 2; 2347 else 2348 heads = 1; 2349 2350 nr_sectors = (((reply_buffer[R_TRACK] - raw_cmd->cmd[TRACK]) * heads + 2351 reply_buffer[R_HEAD] - raw_cmd->cmd[HEAD]) * raw_cmd->cmd[SECT_PER_TRACK] + 2352 reply_buffer[R_SECTOR] - raw_cmd->cmd[SECTOR] + eoc) << raw_cmd->cmd[SIZECODE] >> 2; 2353 2354 if (nr_sectors / ssize > 2355 DIV_ROUND_UP(in_sector_offset + current_count_sectors, ssize)) { 2356 DPRINT("long rw: %x instead of %lx\n", 2357 nr_sectors, current_count_sectors); 2358 pr_info("rs=%d s=%d\n", reply_buffer[R_SECTOR], 2359 raw_cmd->cmd[SECTOR]); 2360 pr_info("rh=%d h=%d\n", reply_buffer[R_HEAD], 2361 raw_cmd->cmd[HEAD]); 2362 pr_info("rt=%d t=%d\n", reply_buffer[R_TRACK], 2363 raw_cmd->cmd[TRACK]); 2364 pr_info("heads=%d eoc=%d\n", heads, eoc); 2365 pr_info("spt=%d st=%d ss=%d\n", 2366 raw_cmd->cmd[SECT_PER_TRACK], fsector_t, ssize); 2367 pr_info("in_sector_offset=%d\n", in_sector_offset); 2368 } 2369 2370 nr_sectors -= in_sector_offset; 2371 INFBOUND(nr_sectors, 0); 2372 SUPBOUND(current_count_sectors, nr_sectors); 2373 2374 switch (interpret_errors()) { 2375 case 2: 2376 cont->redo(); 2377 return; 2378 case 1: 2379 if (!current_count_sectors) { 2380 cont->error(); 2381 cont->redo(); 2382 return; 2383 } 2384 break; 2385 case 0: 2386 if (!current_count_sectors) { 2387 cont->redo(); 2388 return; 2389 } 2390 current_type[current_drive] = _floppy; 2391 floppy_sizes[TOMINOR(current_drive)] = _floppy->size; 2392 break; 2393 } 2394 2395 if (probing) { 2396 if (drive_params[current_drive].flags & FTD_MSG) 2397 DPRINT("Auto-detected floppy type %s in fd%d\n", 2398 _floppy->name, current_drive); 2399 current_type[current_drive] = _floppy; 2400 floppy_sizes[TOMINOR(current_drive)] = _floppy->size; 2401 probing = 0; 2402 } 2403 2404 if (CT(raw_cmd->cmd[COMMAND]) != FD_READ) { 2405 /* transfer directly from buffer */ 2406 cont->done(1); 2407 } else { 2408 buffer_track = raw_cmd->track; 2409 buffer_drive = current_drive; 2410 INFBOUND(buffer_max, nr_sectors + fsector_t); 2411 } 2412 cont->redo(); 2413 } 2414 2415 /* Compute the maximal transfer size */ 2416 static int transfer_size(int ssize, int max_sector, int max_size) 2417 { 2418 SUPBOUND(max_sector, fsector_t + max_size); 2419 2420 /* alignment */ 2421 max_sector -= (max_sector % _floppy->sect) % ssize; 2422 2423 /* transfer size, beginning not aligned */ 2424 current_count_sectors = max_sector - fsector_t; 2425 2426 return max_sector; 2427 } 2428 2429 /* 2430 * Move data from/to the track buffer to/from the buffer cache. 2431 */ 2432 static void copy_buffer(int ssize, int max_sector, int max_sector_2) 2433 { 2434 int remaining; /* number of transferred 512-byte sectors */ 2435 struct bio_vec bv; 2436 char *dma_buffer; 2437 int size; 2438 struct req_iterator iter; 2439 2440 max_sector = transfer_size(ssize, 2441 min(max_sector, max_sector_2), 2442 blk_rq_sectors(current_req)); 2443 2444 if (current_count_sectors <= 0 && CT(raw_cmd->cmd[COMMAND]) == FD_WRITE && 2445 buffer_max > fsector_t + blk_rq_sectors(current_req)) 2446 current_count_sectors = min_t(int, buffer_max - fsector_t, 2447 blk_rq_sectors(current_req)); 2448 2449 remaining = current_count_sectors << 9; 2450 if (remaining > blk_rq_bytes(current_req) && CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) { 2451 DPRINT("in copy buffer\n"); 2452 pr_info("current_count_sectors=%ld\n", current_count_sectors); 2453 pr_info("remaining=%d\n", remaining >> 9); 2454 pr_info("current_req->nr_sectors=%u\n", 2455 blk_rq_sectors(current_req)); 2456 pr_info("current_req->current_nr_sectors=%u\n", 2457 blk_rq_cur_sectors(current_req)); 2458 pr_info("max_sector=%d\n", max_sector); 2459 pr_info("ssize=%d\n", ssize); 2460 } 2461 2462 buffer_max = max(max_sector, buffer_max); 2463 2464 dma_buffer = floppy_track_buffer + ((fsector_t - buffer_min) << 9); 2465 2466 size = blk_rq_cur_bytes(current_req); 2467 2468 rq_for_each_segment(bv, current_req, iter) { 2469 if (!remaining) 2470 break; 2471 2472 size = bv.bv_len; 2473 SUPBOUND(size, remaining); 2474 if (dma_buffer + size > 2475 floppy_track_buffer + (max_buffer_sectors << 10) || 2476 dma_buffer < floppy_track_buffer) { 2477 DPRINT("buffer overrun in copy buffer %d\n", 2478 (int)((floppy_track_buffer - dma_buffer) >> 9)); 2479 pr_info("fsector_t=%d buffer_min=%d\n", 2480 fsector_t, buffer_min); 2481 pr_info("current_count_sectors=%ld\n", 2482 current_count_sectors); 2483 if (CT(raw_cmd->cmd[COMMAND]) == FD_READ) 2484 pr_info("read\n"); 2485 if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) 2486 pr_info("write\n"); 2487 break; 2488 } 2489 2490 if (CT(raw_cmd->cmd[COMMAND]) == FD_READ) 2491 memcpy_to_bvec(&bv, dma_buffer); 2492 else 2493 memcpy_from_bvec(dma_buffer, &bv); 2494 2495 remaining -= size; 2496 dma_buffer += size; 2497 } 2498 if (remaining) { 2499 if (remaining > 0) 2500 max_sector -= remaining >> 9; 2501 DPRINT("weirdness: remaining %d\n", remaining >> 9); 2502 } 2503 } 2504 2505 /* work around a bug in pseudo DMA 2506 * (on some FDCs) pseudo DMA does not stop when the CPU stops 2507 * sending data. Hence we need a different way to signal the 2508 * transfer length: We use raw_cmd->cmd[SECT_PER_TRACK]. Unfortunately, this 2509 * does not work with MT, hence we can only transfer one head at 2510 * a time 2511 */ 2512 static void virtualdmabug_workaround(void) 2513 { 2514 int hard_sectors; 2515 int end_sector; 2516 2517 if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) { 2518 raw_cmd->cmd[COMMAND] &= ~0x80; /* switch off multiple track mode */ 2519 2520 hard_sectors = raw_cmd->length >> (7 + raw_cmd->cmd[SIZECODE]); 2521 end_sector = raw_cmd->cmd[SECTOR] + hard_sectors - 1; 2522 if (end_sector > raw_cmd->cmd[SECT_PER_TRACK]) { 2523 pr_info("too many sectors %d > %d\n", 2524 end_sector, raw_cmd->cmd[SECT_PER_TRACK]); 2525 return; 2526 } 2527 raw_cmd->cmd[SECT_PER_TRACK] = end_sector; 2528 /* make sure raw_cmd->cmd[SECT_PER_TRACK] 2529 * points to end of transfer */ 2530 } 2531 } 2532 2533 /* 2534 * Formulate a read/write request. 2535 * this routine decides where to load the data (directly to buffer, or to 2536 * tmp floppy area), how much data to load (the size of the buffer, the whole 2537 * track, or a single sector) 2538 * All floppy_track_buffer handling goes in here. If we ever add track buffer 2539 * allocation on the fly, it should be done here. No other part should need 2540 * modification. 2541 */ 2542 2543 static int make_raw_rw_request(void) 2544 { 2545 int aligned_sector_t; 2546 int max_sector; 2547 int max_size; 2548 int tracksize; 2549 int ssize; 2550 2551 if (WARN(max_buffer_sectors == 0, "VFS: Block I/O scheduled on unopened device\n")) 2552 return 0; 2553 2554 set_fdc((long)current_req->q->disk->private_data); 2555 2556 raw_cmd = &default_raw_cmd; 2557 raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK; 2558 raw_cmd->cmd_count = NR_RW; 2559 if (rq_data_dir(current_req) == READ) { 2560 raw_cmd->flags |= FD_RAW_READ; 2561 raw_cmd->cmd[COMMAND] = FM_MODE(_floppy, FD_READ); 2562 } else if (rq_data_dir(current_req) == WRITE) { 2563 raw_cmd->flags |= FD_RAW_WRITE; 2564 raw_cmd->cmd[COMMAND] = FM_MODE(_floppy, FD_WRITE); 2565 } else { 2566 DPRINT("%s: unknown command\n", __func__); 2567 return 0; 2568 } 2569 2570 max_sector = _floppy->sect * _floppy->head; 2571 2572 raw_cmd->cmd[TRACK] = (int)blk_rq_pos(current_req) / max_sector; 2573 fsector_t = (int)blk_rq_pos(current_req) % max_sector; 2574 if (_floppy->track && raw_cmd->cmd[TRACK] >= _floppy->track) { 2575 if (blk_rq_cur_sectors(current_req) & 1) { 2576 current_count_sectors = 1; 2577 return 1; 2578 } else 2579 return 0; 2580 } 2581 raw_cmd->cmd[HEAD] = fsector_t / _floppy->sect; 2582 2583 if (((_floppy->stretch & (FD_SWAPSIDES | FD_SECTBASEMASK)) || 2584 test_bit(FD_NEED_TWADDLE_BIT, &drive_state[current_drive].flags)) && 2585 fsector_t < _floppy->sect) 2586 max_sector = _floppy->sect; 2587 2588 /* 2M disks have phantom sectors on the first track */ 2589 if ((_floppy->rate & FD_2M) && (!raw_cmd->cmd[TRACK]) && (!raw_cmd->cmd[HEAD])) { 2590 max_sector = 2 * _floppy->sect / 3; 2591 if (fsector_t >= max_sector) { 2592 current_count_sectors = 2593 min_t(int, _floppy->sect - fsector_t, 2594 blk_rq_sectors(current_req)); 2595 return 1; 2596 } 2597 raw_cmd->cmd[SIZECODE] = 2; 2598 } else 2599 raw_cmd->cmd[SIZECODE] = FD_SIZECODE(_floppy); 2600 raw_cmd->rate = _floppy->rate & 0x43; 2601 if ((_floppy->rate & FD_2M) && 2602 (raw_cmd->cmd[TRACK] || raw_cmd->cmd[HEAD]) && raw_cmd->rate == 2) 2603 raw_cmd->rate = 1; 2604 2605 if (raw_cmd->cmd[SIZECODE]) 2606 raw_cmd->cmd[SIZECODE2] = 0xff; 2607 else 2608 raw_cmd->cmd[SIZECODE2] = 0x80; 2609 raw_cmd->track = raw_cmd->cmd[TRACK] << STRETCH(_floppy); 2610 raw_cmd->cmd[DR_SELECT] = UNIT(current_drive) + PH_HEAD(_floppy, raw_cmd->cmd[HEAD]); 2611 raw_cmd->cmd[GAP] = _floppy->gap; 2612 ssize = DIV_ROUND_UP(1 << raw_cmd->cmd[SIZECODE], 4); 2613 raw_cmd->cmd[SECT_PER_TRACK] = _floppy->sect << 2 >> raw_cmd->cmd[SIZECODE]; 2614 raw_cmd->cmd[SECTOR] = ((fsector_t % _floppy->sect) << 2 >> raw_cmd->cmd[SIZECODE]) + 2615 FD_SECTBASE(_floppy); 2616 2617 /* tracksize describes the size which can be filled up with sectors 2618 * of size ssize. 2619 */ 2620 tracksize = _floppy->sect - _floppy->sect % ssize; 2621 if (tracksize < _floppy->sect) { 2622 raw_cmd->cmd[SECT_PER_TRACK]++; 2623 if (tracksize <= fsector_t % _floppy->sect) 2624 raw_cmd->cmd[SECTOR]--; 2625 2626 /* if we are beyond tracksize, fill up using smaller sectors */ 2627 while (tracksize <= fsector_t % _floppy->sect) { 2628 while (tracksize + ssize > _floppy->sect) { 2629 raw_cmd->cmd[SIZECODE]--; 2630 ssize >>= 1; 2631 } 2632 raw_cmd->cmd[SECTOR]++; 2633 raw_cmd->cmd[SECT_PER_TRACK]++; 2634 tracksize += ssize; 2635 } 2636 max_sector = raw_cmd->cmd[HEAD] * _floppy->sect + tracksize; 2637 } else if (!raw_cmd->cmd[TRACK] && !raw_cmd->cmd[HEAD] && !(_floppy->rate & FD_2M) && probing) { 2638 max_sector = _floppy->sect; 2639 } else if (!raw_cmd->cmd[HEAD] && CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) { 2640 /* for virtual DMA bug workaround */ 2641 max_sector = _floppy->sect; 2642 } 2643 2644 in_sector_offset = (fsector_t % _floppy->sect) % ssize; 2645 aligned_sector_t = fsector_t - in_sector_offset; 2646 max_size = blk_rq_sectors(current_req); 2647 if ((raw_cmd->track == buffer_track) && 2648 (current_drive == buffer_drive) && 2649 (fsector_t >= buffer_min) && (fsector_t < buffer_max)) { 2650 /* data already in track buffer */ 2651 if (CT(raw_cmd->cmd[COMMAND]) == FD_READ) { 2652 copy_buffer(1, max_sector, buffer_max); 2653 return 1; 2654 } 2655 } else if (in_sector_offset || blk_rq_sectors(current_req) < ssize) { 2656 if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) { 2657 unsigned int sectors; 2658 2659 sectors = fsector_t + blk_rq_sectors(current_req); 2660 if (sectors > ssize && sectors < ssize + ssize) 2661 max_size = ssize + ssize; 2662 else 2663 max_size = ssize; 2664 } 2665 raw_cmd->flags &= ~FD_RAW_WRITE; 2666 raw_cmd->flags |= FD_RAW_READ; 2667 raw_cmd->cmd[COMMAND] = FM_MODE(_floppy, FD_READ); 2668 } 2669 2670 if (CT(raw_cmd->cmd[COMMAND]) == FD_READ) 2671 max_size = max_sector; /* unbounded */ 2672 2673 /* claim buffer track if needed */ 2674 if (buffer_track != raw_cmd->track || /* bad track */ 2675 buffer_drive != current_drive || /* bad drive */ 2676 fsector_t > buffer_max || 2677 fsector_t < buffer_min || 2678 ((CT(raw_cmd->cmd[COMMAND]) == FD_READ || 2679 (!in_sector_offset && blk_rq_sectors(current_req) >= ssize)) && 2680 max_sector > 2 * max_buffer_sectors + buffer_min && 2681 max_size + fsector_t > 2 * max_buffer_sectors + buffer_min)) { 2682 /* not enough space */ 2683 buffer_track = -1; 2684 buffer_drive = current_drive; 2685 buffer_max = buffer_min = aligned_sector_t; 2686 } 2687 raw_cmd->kernel_data = floppy_track_buffer + 2688 ((aligned_sector_t - buffer_min) << 9); 2689 2690 if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) { 2691 /* copy write buffer to track buffer. 2692 * if we get here, we know that the write 2693 * is either aligned or the data already in the buffer 2694 * (buffer will be overwritten) */ 2695 if (in_sector_offset && buffer_track == -1) 2696 DPRINT("internal error offset !=0 on write\n"); 2697 buffer_track = raw_cmd->track; 2698 buffer_drive = current_drive; 2699 copy_buffer(ssize, max_sector, 2700 2 * max_buffer_sectors + buffer_min); 2701 } else 2702 transfer_size(ssize, max_sector, 2703 2 * max_buffer_sectors + buffer_min - 2704 aligned_sector_t); 2705 2706 /* round up current_count_sectors to get dma xfer size */ 2707 raw_cmd->length = in_sector_offset + current_count_sectors; 2708 raw_cmd->length = ((raw_cmd->length - 1) | (ssize - 1)) + 1; 2709 raw_cmd->length <<= 9; 2710 if ((raw_cmd->length < current_count_sectors << 9) || 2711 (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE && 2712 (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max || 2713 aligned_sector_t < buffer_min)) || 2714 raw_cmd->length % (128 << raw_cmd->cmd[SIZECODE]) || 2715 raw_cmd->length <= 0 || current_count_sectors <= 0) { 2716 DPRINT("fractionary current count b=%lx s=%lx\n", 2717 raw_cmd->length, current_count_sectors); 2718 pr_info("addr=%d, length=%ld\n", 2719 (int)((raw_cmd->kernel_data - 2720 floppy_track_buffer) >> 9), 2721 current_count_sectors); 2722 pr_info("st=%d ast=%d mse=%d msi=%d\n", 2723 fsector_t, aligned_sector_t, max_sector, max_size); 2724 pr_info("ssize=%x SIZECODE=%d\n", ssize, raw_cmd->cmd[SIZECODE]); 2725 pr_info("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n", 2726 raw_cmd->cmd[COMMAND], raw_cmd->cmd[SECTOR], 2727 raw_cmd->cmd[HEAD], raw_cmd->cmd[TRACK]); 2728 pr_info("buffer drive=%d\n", buffer_drive); 2729 pr_info("buffer track=%d\n", buffer_track); 2730 pr_info("buffer_min=%d\n", buffer_min); 2731 pr_info("buffer_max=%d\n", buffer_max); 2732 return 0; 2733 } 2734 2735 if (raw_cmd->kernel_data < floppy_track_buffer || 2736 current_count_sectors < 0 || 2737 raw_cmd->length < 0 || 2738 raw_cmd->kernel_data + raw_cmd->length > 2739 floppy_track_buffer + (max_buffer_sectors << 10)) { 2740 DPRINT("buffer overrun in schedule dma\n"); 2741 pr_info("fsector_t=%d buffer_min=%d current_count=%ld\n", 2742 fsector_t, buffer_min, raw_cmd->length >> 9); 2743 pr_info("current_count_sectors=%ld\n", 2744 current_count_sectors); 2745 if (CT(raw_cmd->cmd[COMMAND]) == FD_READ) 2746 pr_info("read\n"); 2747 if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) 2748 pr_info("write\n"); 2749 return 0; 2750 } 2751 if (raw_cmd->length == 0) { 2752 DPRINT("zero dma transfer attempted from make_raw_request\n"); 2753 return 0; 2754 } 2755 2756 virtualdmabug_workaround(); 2757 return 2; 2758 } 2759 2760 static int set_next_request(void) 2761 { 2762 current_req = list_first_entry_or_null(&floppy_reqs, struct request, 2763 queuelist); 2764 if (current_req) { 2765 floppy_errors = 0; 2766 list_del_init(¤t_req->queuelist); 2767 return 1; 2768 } 2769 return 0; 2770 } 2771 2772 /* Starts or continues processing request. Will automatically unlock the 2773 * driver at end of request. 2774 */ 2775 static void redo_fd_request(void) 2776 { 2777 int drive; 2778 int tmp; 2779 2780 lastredo = jiffies; 2781 if (current_drive < N_DRIVE) 2782 floppy_off(current_drive); 2783 2784 do_request: 2785 if (!current_req) { 2786 int pending; 2787 2788 spin_lock_irq(&floppy_lock); 2789 pending = set_next_request(); 2790 spin_unlock_irq(&floppy_lock); 2791 if (!pending) { 2792 unlock_fdc(); 2793 return; 2794 } 2795 } 2796 drive = (long)current_req->q->disk->private_data; 2797 set_fdc(drive); 2798 reschedule_timeout(current_drive, "redo fd request"); 2799 2800 set_floppy(drive); 2801 raw_cmd = &default_raw_cmd; 2802 raw_cmd->flags = 0; 2803 if (start_motor(redo_fd_request)) 2804 return; 2805 2806 disk_change(current_drive); 2807 if (test_bit(current_drive, &fake_change) || 2808 test_bit(FD_DISK_CHANGED_BIT, &drive_state[current_drive].flags)) { 2809 DPRINT("disk absent or changed during operation\n"); 2810 request_done(0); 2811 goto do_request; 2812 } 2813 if (!_floppy) { /* Autodetection */ 2814 if (!probing) { 2815 drive_state[current_drive].probed_format = 0; 2816 if (next_valid_format(current_drive)) { 2817 DPRINT("no autodetectable formats\n"); 2818 _floppy = NULL; 2819 request_done(0); 2820 goto do_request; 2821 } 2822 } 2823 probing = 1; 2824 _floppy = floppy_type + drive_params[current_drive].autodetect[drive_state[current_drive].probed_format]; 2825 } else 2826 probing = 0; 2827 tmp = make_raw_rw_request(); 2828 if (tmp < 2) { 2829 request_done(tmp); 2830 goto do_request; 2831 } 2832 2833 if (test_bit(FD_NEED_TWADDLE_BIT, &drive_state[current_drive].flags)) 2834 twaddle(current_fdc, current_drive); 2835 schedule_bh(floppy_start); 2836 debugt(__func__, "queue fd request"); 2837 return; 2838 } 2839 2840 static const struct cont_t rw_cont = { 2841 .interrupt = rw_interrupt, 2842 .redo = redo_fd_request, 2843 .error = bad_flp_intr, 2844 .done = request_done 2845 }; 2846 2847 /* schedule the request and automatically unlock the driver on completion */ 2848 static void process_fd_request(void) 2849 { 2850 cont = &rw_cont; 2851 schedule_bh(redo_fd_request); 2852 } 2853 2854 static blk_status_t floppy_queue_rq(struct blk_mq_hw_ctx *hctx, 2855 const struct blk_mq_queue_data *bd) 2856 { 2857 blk_mq_start_request(bd->rq); 2858 2859 if (WARN(max_buffer_sectors == 0, 2860 "VFS: %s called on non-open device\n", __func__)) 2861 return BLK_STS_IOERR; 2862 2863 if (WARN(atomic_read(&usage_count) == 0, 2864 "warning: usage count=0, current_req=%p sect=%ld flags=%llx\n", 2865 current_req, (long)blk_rq_pos(current_req), 2866 (__force unsigned long long) current_req->cmd_flags)) 2867 return BLK_STS_IOERR; 2868 2869 if (test_and_set_bit(0, &fdc_busy)) { 2870 /* fdc busy, this new request will be treated when the 2871 current one is done */ 2872 is_alive(__func__, "old request running"); 2873 return BLK_STS_RESOURCE; 2874 } 2875 2876 spin_lock_irq(&floppy_lock); 2877 list_add_tail(&bd->rq->queuelist, &floppy_reqs); 2878 spin_unlock_irq(&floppy_lock); 2879 2880 WRITE_ONCE(command_status, FD_COMMAND_NONE); 2881 __reschedule_timeout(MAXTIMEOUT, "fd_request"); 2882 set_fdc(0); 2883 process_fd_request(); 2884 is_alive(__func__, ""); 2885 return BLK_STS_OK; 2886 } 2887 2888 static const struct cont_t poll_cont = { 2889 .interrupt = success_and_wakeup, 2890 .redo = floppy_ready, 2891 .error = generic_failure, 2892 .done = generic_done 2893 }; 2894 2895 static int poll_drive(bool interruptible, int flag) 2896 { 2897 /* no auto-sense, just clear dcl */ 2898 raw_cmd = &default_raw_cmd; 2899 raw_cmd->flags = flag; 2900 raw_cmd->track = 0; 2901 raw_cmd->cmd_count = 0; 2902 cont = &poll_cont; 2903 debug_dcl(drive_params[current_drive].flags, 2904 "setting NEWCHANGE in poll_drive\n"); 2905 set_bit(FD_DISK_NEWCHANGE_BIT, &drive_state[current_drive].flags); 2906 2907 return wait_til_done(floppy_ready, interruptible); 2908 } 2909 2910 /* 2911 * User triggered reset 2912 * ==================== 2913 */ 2914 2915 static void reset_intr(void) 2916 { 2917 pr_info("weird, reset interrupt called\n"); 2918 } 2919 2920 static const struct cont_t reset_cont = { 2921 .interrupt = reset_intr, 2922 .redo = success_and_wakeup, 2923 .error = generic_failure, 2924 .done = generic_done 2925 }; 2926 2927 /* 2928 * Resets the FDC connected to drive <drive>. 2929 * Both current_drive and current_fdc are changed to match the new drive. 2930 */ 2931 static int user_reset_fdc(int drive, int arg, bool interruptible) 2932 { 2933 int ret; 2934 2935 if (lock_fdc(drive)) 2936 return -EINTR; 2937 2938 if (arg == FD_RESET_ALWAYS) 2939 fdc_state[current_fdc].reset = 1; 2940 if (fdc_state[current_fdc].reset) { 2941 /* note: reset_fdc will take care of unlocking the driver 2942 * on completion. 2943 */ 2944 cont = &reset_cont; 2945 ret = wait_til_done(reset_fdc, interruptible); 2946 if (ret == -EINTR) 2947 return -EINTR; 2948 } 2949 process_fd_request(); 2950 return 0; 2951 } 2952 2953 /* 2954 * Misc Ioctl's and support 2955 * ======================== 2956 */ 2957 static inline int fd_copyout(void __user *param, const void *address, 2958 unsigned long size) 2959 { 2960 return copy_to_user(param, address, size) ? -EFAULT : 0; 2961 } 2962 2963 static inline int fd_copyin(void __user *param, void *address, 2964 unsigned long size) 2965 { 2966 return copy_from_user(address, param, size) ? -EFAULT : 0; 2967 } 2968 2969 static const char *drive_name(int type, int drive) 2970 { 2971 struct floppy_struct *floppy; 2972 2973 if (type) 2974 floppy = floppy_type + type; 2975 else { 2976 if (drive_params[drive].native_format) 2977 floppy = floppy_type + drive_params[drive].native_format; 2978 else 2979 return "(null)"; 2980 } 2981 if (floppy->name) 2982 return floppy->name; 2983 else 2984 return "(null)"; 2985 } 2986 2987 #ifdef CONFIG_BLK_DEV_FD_RAWCMD 2988 2989 /* raw commands */ 2990 static void raw_cmd_done(int flag) 2991 { 2992 if (!flag) { 2993 raw_cmd->flags |= FD_RAW_FAILURE; 2994 raw_cmd->flags |= FD_RAW_HARDFAILURE; 2995 } else { 2996 raw_cmd->reply_count = inr; 2997 if (raw_cmd->reply_count > FD_RAW_REPLY_SIZE) 2998 raw_cmd->reply_count = 0; 2999 memcpy(raw_cmd->reply, reply_buffer, raw_cmd->reply_count); 3000 3001 if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE)) { 3002 unsigned long flags; 3003 flags = claim_dma_lock(); 3004 raw_cmd->length = fd_get_dma_residue(); 3005 release_dma_lock(flags); 3006 } 3007 3008 if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) && 3009 (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0))) 3010 raw_cmd->flags |= FD_RAW_FAILURE; 3011 3012 if (disk_change(current_drive)) 3013 raw_cmd->flags |= FD_RAW_DISK_CHANGE; 3014 else 3015 raw_cmd->flags &= ~FD_RAW_DISK_CHANGE; 3016 if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER) 3017 motor_off_callback(&motor_off_timer[current_drive]); 3018 3019 if (raw_cmd->next && 3020 (!(raw_cmd->flags & FD_RAW_FAILURE) || 3021 !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) && 3022 ((raw_cmd->flags & FD_RAW_FAILURE) || 3023 !(raw_cmd->flags & FD_RAW_STOP_IF_SUCCESS))) { 3024 raw_cmd = raw_cmd->next; 3025 return; 3026 } 3027 } 3028 generic_done(flag); 3029 } 3030 3031 static const struct cont_t raw_cmd_cont = { 3032 .interrupt = success_and_wakeup, 3033 .redo = floppy_start, 3034 .error = generic_failure, 3035 .done = raw_cmd_done 3036 }; 3037 3038 static int raw_cmd_copyout(int cmd, void __user *param, 3039 struct floppy_raw_cmd *ptr) 3040 { 3041 int ret; 3042 3043 while (ptr) { 3044 struct floppy_raw_cmd cmd = *ptr; 3045 cmd.next = NULL; 3046 cmd.kernel_data = NULL; 3047 ret = copy_to_user(param, &cmd, sizeof(cmd)); 3048 if (ret) 3049 return -EFAULT; 3050 param += sizeof(struct floppy_raw_cmd); 3051 if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length) { 3052 if (ptr->length >= 0 && 3053 ptr->length <= ptr->buffer_length) { 3054 long length = ptr->buffer_length - ptr->length; 3055 ret = fd_copyout(ptr->data, ptr->kernel_data, 3056 length); 3057 if (ret) 3058 return ret; 3059 } 3060 } 3061 ptr = ptr->next; 3062 } 3063 3064 return 0; 3065 } 3066 3067 static void raw_cmd_free(struct floppy_raw_cmd **ptr) 3068 { 3069 struct floppy_raw_cmd *next; 3070 struct floppy_raw_cmd *this; 3071 3072 this = *ptr; 3073 *ptr = NULL; 3074 while (this) { 3075 if (this->buffer_length) { 3076 fd_dma_mem_free((unsigned long)this->kernel_data, 3077 this->buffer_length); 3078 this->buffer_length = 0; 3079 } 3080 next = this->next; 3081 kfree(this); 3082 this = next; 3083 } 3084 } 3085 3086 #define MAX_LEN (1UL << MAX_PAGE_ORDER << PAGE_SHIFT) 3087 3088 static int raw_cmd_copyin(int cmd, void __user *param, 3089 struct floppy_raw_cmd **rcmd) 3090 { 3091 struct floppy_raw_cmd *ptr; 3092 int ret; 3093 3094 *rcmd = NULL; 3095 3096 loop: 3097 ptr = memdup_user(param, sizeof(*ptr)); 3098 if (IS_ERR(ptr)) 3099 return PTR_ERR(ptr); 3100 *rcmd = ptr; 3101 ptr->next = NULL; 3102 ptr->buffer_length = 0; 3103 ptr->kernel_data = NULL; 3104 param += sizeof(struct floppy_raw_cmd); 3105 if (ptr->cmd_count > FD_RAW_CMD_FULLSIZE) 3106 return -EINVAL; 3107 3108 memset(ptr->reply, 0, FD_RAW_REPLY_SIZE); 3109 ptr->resultcode = 0; 3110 3111 if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) { 3112 if (ptr->length <= 0 || ptr->length > MAX_LEN) 3113 return -EINVAL; 3114 ptr->kernel_data = (char *)fd_dma_mem_alloc(ptr->length); 3115 fallback_on_nodma_alloc(&ptr->kernel_data, ptr->length); 3116 if (!ptr->kernel_data) 3117 return -ENOMEM; 3118 ptr->buffer_length = ptr->length; 3119 } 3120 if (ptr->flags & FD_RAW_WRITE) { 3121 ret = fd_copyin(ptr->data, ptr->kernel_data, ptr->length); 3122 if (ret) 3123 return ret; 3124 } 3125 3126 if (ptr->flags & FD_RAW_MORE) { 3127 rcmd = &(ptr->next); 3128 ptr->rate &= 0x43; 3129 goto loop; 3130 } 3131 3132 return 0; 3133 } 3134 3135 static int raw_cmd_ioctl(int cmd, void __user *param) 3136 { 3137 struct floppy_raw_cmd *my_raw_cmd; 3138 int drive; 3139 int ret2; 3140 int ret; 3141 3142 if (fdc_state[current_fdc].rawcmd <= 1) 3143 fdc_state[current_fdc].rawcmd = 1; 3144 for (drive = 0; drive < N_DRIVE; drive++) { 3145 if (FDC(drive) != current_fdc) 3146 continue; 3147 if (drive == current_drive) { 3148 if (drive_state[drive].fd_ref > 1) { 3149 fdc_state[current_fdc].rawcmd = 2; 3150 break; 3151 } 3152 } else if (drive_state[drive].fd_ref) { 3153 fdc_state[current_fdc].rawcmd = 2; 3154 break; 3155 } 3156 } 3157 3158 if (fdc_state[current_fdc].reset) 3159 return -EIO; 3160 3161 ret = raw_cmd_copyin(cmd, param, &my_raw_cmd); 3162 if (ret) { 3163 raw_cmd_free(&my_raw_cmd); 3164 return ret; 3165 } 3166 3167 raw_cmd = my_raw_cmd; 3168 cont = &raw_cmd_cont; 3169 ret = wait_til_done(floppy_start, true); 3170 debug_dcl(drive_params[current_drive].flags, 3171 "calling disk change from raw_cmd ioctl\n"); 3172 3173 if (ret != -EINTR && fdc_state[current_fdc].reset) 3174 ret = -EIO; 3175 3176 drive_state[current_drive].track = NO_TRACK; 3177 3178 ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd); 3179 if (!ret) 3180 ret = ret2; 3181 raw_cmd_free(&my_raw_cmd); 3182 return ret; 3183 } 3184 3185 static int floppy_raw_cmd_ioctl(int type, int drive, int cmd, 3186 void __user *param) 3187 { 3188 int ret; 3189 3190 pr_warn_once("Note: FDRAWCMD is deprecated and will be removed from the kernel in the near future.\n"); 3191 3192 if (type) 3193 return -EINVAL; 3194 if (lock_fdc(drive)) 3195 return -EINTR; 3196 set_floppy(drive); 3197 ret = raw_cmd_ioctl(cmd, param); 3198 if (ret == -EINTR) 3199 return -EINTR; 3200 process_fd_request(); 3201 return ret; 3202 } 3203 3204 #else /* CONFIG_BLK_DEV_FD_RAWCMD */ 3205 3206 static int floppy_raw_cmd_ioctl(int type, int drive, int cmd, 3207 void __user *param) 3208 { 3209 return -EOPNOTSUPP; 3210 } 3211 3212 #endif 3213 3214 static int invalidate_drive(struct gendisk *disk) 3215 { 3216 /* invalidate the buffer track to force a reread */ 3217 set_bit((long)disk->private_data, &fake_change); 3218 process_fd_request(); 3219 if (disk_check_media_change(disk)) { 3220 bdev_mark_dead(disk->part0, true); 3221 floppy_revalidate(disk); 3222 } 3223 return 0; 3224 } 3225 3226 static int set_geometry(unsigned int cmd, struct floppy_struct *g, 3227 int drive, int type, struct block_device *bdev) 3228 { 3229 int cnt; 3230 3231 /* sanity checking for parameters. */ 3232 if ((int)g->sect <= 0 || 3233 (int)g->head <= 0 || 3234 /* check for overflow in max_sector */ 3235 (int)(g->sect * g->head) <= 0 || 3236 /* check for zero in raw_cmd->cmd[F_SECT_PER_TRACK] */ 3237 (unsigned char)((g->sect << 2) >> FD_SIZECODE(g)) == 0 || 3238 g->track <= 0 || g->track > drive_params[drive].tracks >> STRETCH(g) || 3239 /* check if reserved bits are set */ 3240 (g->stretch & ~(FD_STRETCH | FD_SWAPSIDES | FD_SECTBASEMASK)) != 0) 3241 return -EINVAL; 3242 if (type) { 3243 if (!capable(CAP_SYS_ADMIN)) 3244 return -EPERM; 3245 mutex_lock(&open_lock); 3246 if (lock_fdc(drive)) { 3247 mutex_unlock(&open_lock); 3248 return -EINTR; 3249 } 3250 floppy_type[type] = *g; 3251 floppy_type[type].name = "user format"; 3252 for (cnt = type << 2; cnt < (type << 2) + 4; cnt++) 3253 floppy_sizes[cnt] = floppy_sizes[cnt + 0x80] = 3254 floppy_type[type].size + 1; 3255 process_fd_request(); 3256 for (cnt = 0; cnt < N_DRIVE; cnt++) { 3257 struct gendisk *disk = opened_disk[cnt]; 3258 3259 if (!disk || ITYPE(drive_state[cnt].fd_device) != type) 3260 continue; 3261 disk_force_media_change(disk); 3262 } 3263 mutex_unlock(&open_lock); 3264 } else { 3265 int oldStretch; 3266 3267 if (lock_fdc(drive)) 3268 return -EINTR; 3269 if (cmd != FDDEFPRM) { 3270 /* notice a disk change immediately, else 3271 * we lose our settings immediately*/ 3272 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR) 3273 return -EINTR; 3274 } 3275 oldStretch = g->stretch; 3276 user_params[drive] = *g; 3277 if (buffer_drive == drive) 3278 SUPBOUND(buffer_max, user_params[drive].sect); 3279 current_type[drive] = &user_params[drive]; 3280 floppy_sizes[drive] = user_params[drive].size; 3281 if (cmd == FDDEFPRM) 3282 drive_state[current_drive].keep_data = -1; 3283 else 3284 drive_state[current_drive].keep_data = 1; 3285 /* invalidation. Invalidate only when needed, i.e. 3286 * when there are already sectors in the buffer cache 3287 * whose number will change. This is useful, because 3288 * mtools often changes the geometry of the disk after 3289 * looking at the boot block */ 3290 if (drive_state[current_drive].maxblock > user_params[drive].sect || 3291 drive_state[current_drive].maxtrack || 3292 ((user_params[drive].sect ^ oldStretch) & 3293 (FD_SWAPSIDES | FD_SECTBASEMASK))) 3294 invalidate_drive(bdev->bd_disk); 3295 else 3296 process_fd_request(); 3297 } 3298 return 0; 3299 } 3300 3301 /* handle obsolete ioctl's */ 3302 static unsigned int ioctl_table[] = { 3303 FDCLRPRM, 3304 FDSETPRM, 3305 FDDEFPRM, 3306 FDGETPRM, 3307 FDMSGON, 3308 FDMSGOFF, 3309 FDFMTBEG, 3310 FDFMTTRK, 3311 FDFMTEND, 3312 FDSETEMSGTRESH, 3313 FDFLUSH, 3314 FDSETMAXERRS, 3315 FDGETMAXERRS, 3316 FDGETDRVTYP, 3317 FDSETDRVPRM, 3318 FDGETDRVPRM, 3319 FDGETDRVSTAT, 3320 FDPOLLDRVSTAT, 3321 FDRESET, 3322 FDGETFDCSTAT, 3323 FDWERRORCLR, 3324 FDWERRORGET, 3325 FDRAWCMD, 3326 FDEJECT, 3327 FDTWADDLE 3328 }; 3329 3330 static int normalize_ioctl(unsigned int *cmd, int *size) 3331 { 3332 int i; 3333 3334 for (i = 0; i < ARRAY_SIZE(ioctl_table); i++) { 3335 if ((*cmd & 0xffff) == (ioctl_table[i] & 0xffff)) { 3336 *size = _IOC_SIZE(*cmd); 3337 *cmd = ioctl_table[i]; 3338 if (*size > _IOC_SIZE(*cmd)) { 3339 pr_info("ioctl not yet supported\n"); 3340 return -EFAULT; 3341 } 3342 return 0; 3343 } 3344 } 3345 return -EINVAL; 3346 } 3347 3348 static int get_floppy_geometry(int drive, int type, struct floppy_struct **g) 3349 { 3350 if (type) 3351 *g = &floppy_type[type]; 3352 else { 3353 if (lock_fdc(drive)) 3354 return -EINTR; 3355 if (poll_drive(false, 0) == -EINTR) 3356 return -EINTR; 3357 process_fd_request(); 3358 *g = current_type[drive]; 3359 } 3360 if (!*g) 3361 return -ENODEV; 3362 return 0; 3363 } 3364 3365 static int fd_getgeo(struct gendisk *disk, struct hd_geometry *geo) 3366 { 3367 int drive = (long)disk->private_data; 3368 int type = ITYPE(drive_state[drive].fd_device); 3369 struct floppy_struct *g; 3370 int ret; 3371 3372 ret = get_floppy_geometry(drive, type, &g); 3373 if (ret) 3374 return ret; 3375 3376 geo->heads = g->head; 3377 geo->sectors = g->sect; 3378 geo->cylinders = g->track; 3379 return 0; 3380 } 3381 3382 static bool valid_floppy_drive_params(const short autodetect[FD_AUTODETECT_SIZE], 3383 int native_format) 3384 { 3385 size_t floppy_type_size = ARRAY_SIZE(floppy_type); 3386 size_t i = 0; 3387 3388 for (i = 0; i < FD_AUTODETECT_SIZE; ++i) { 3389 if (autodetect[i] < 0 || 3390 autodetect[i] >= floppy_type_size) 3391 return false; 3392 } 3393 3394 if (native_format < 0 || native_format >= floppy_type_size) 3395 return false; 3396 3397 return true; 3398 } 3399 3400 static int fd_locked_ioctl(struct block_device *bdev, blk_mode_t mode, 3401 unsigned int cmd, unsigned long param) 3402 { 3403 int drive = (long)bdev->bd_disk->private_data; 3404 int type = ITYPE(drive_state[drive].fd_device); 3405 int ret; 3406 int size; 3407 union inparam { 3408 struct floppy_struct g; /* geometry */ 3409 struct format_descr f; 3410 struct floppy_max_errors max_errors; 3411 struct floppy_drive_params dp; 3412 } inparam; /* parameters coming from user space */ 3413 const void *outparam = NULL; /* parameters passed back to user space */ 3414 3415 /* convert compatibility eject ioctls into floppy eject ioctl. 3416 * We do this in order to provide a means to eject floppy disks before 3417 * installing the new fdutils package */ 3418 if (cmd == CDROMEJECT || /* CD-ROM eject */ 3419 cmd == 0x6470) { /* SunOS floppy eject */ 3420 DPRINT("obsolete eject ioctl\n"); 3421 DPRINT("please use floppycontrol --eject\n"); 3422 cmd = FDEJECT; 3423 } 3424 3425 if (!((cmd & 0xff00) == 0x0200)) 3426 return -EINVAL; 3427 3428 /* convert the old style command into a new style command */ 3429 ret = normalize_ioctl(&cmd, &size); 3430 if (ret) 3431 return ret; 3432 3433 /* permission checks */ 3434 if (((cmd & 0x40) && 3435 !(mode & (BLK_OPEN_WRITE | BLK_OPEN_WRITE_IOCTL))) || 3436 ((cmd & 0x80) && !capable(CAP_SYS_ADMIN))) 3437 return -EPERM; 3438 3439 if (WARN_ON(size < 0 || size > sizeof(inparam))) 3440 return -EINVAL; 3441 3442 /* copyin */ 3443 memset(&inparam, 0, sizeof(inparam)); 3444 if (_IOC_DIR(cmd) & _IOC_WRITE) { 3445 ret = fd_copyin((void __user *)param, &inparam, size); 3446 if (ret) 3447 return ret; 3448 } 3449 3450 switch (cmd) { 3451 case FDEJECT: 3452 if (drive_state[drive].fd_ref != 1) 3453 /* somebody else has this drive open */ 3454 return -EBUSY; 3455 if (lock_fdc(drive)) 3456 return -EINTR; 3457 3458 /* do the actual eject. Fails on 3459 * non-Sparc architectures */ 3460 ret = fd_eject(UNIT(drive)); 3461 3462 set_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags); 3463 set_bit(FD_VERIFY_BIT, &drive_state[drive].flags); 3464 process_fd_request(); 3465 return ret; 3466 case FDCLRPRM: 3467 if (lock_fdc(drive)) 3468 return -EINTR; 3469 current_type[drive] = NULL; 3470 floppy_sizes[drive] = MAX_DISK_SIZE << 1; 3471 drive_state[drive].keep_data = 0; 3472 return invalidate_drive(bdev->bd_disk); 3473 case FDSETPRM: 3474 case FDDEFPRM: 3475 return set_geometry(cmd, &inparam.g, drive, type, bdev); 3476 case FDGETPRM: 3477 ret = get_floppy_geometry(drive, type, 3478 (struct floppy_struct **)&outparam); 3479 if (ret) 3480 return ret; 3481 memcpy(&inparam.g, outparam, 3482 offsetof(struct floppy_struct, name)); 3483 outparam = &inparam.g; 3484 break; 3485 case FDMSGON: 3486 drive_params[drive].flags |= FTD_MSG; 3487 return 0; 3488 case FDMSGOFF: 3489 drive_params[drive].flags &= ~FTD_MSG; 3490 return 0; 3491 case FDFMTBEG: 3492 if (lock_fdc(drive)) 3493 return -EINTR; 3494 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR) 3495 return -EINTR; 3496 ret = drive_state[drive].flags; 3497 process_fd_request(); 3498 if (ret & FD_VERIFY) 3499 return -ENODEV; 3500 if (!(ret & FD_DISK_WRITABLE)) 3501 return -EROFS; 3502 return 0; 3503 case FDFMTTRK: 3504 if (drive_state[drive].fd_ref != 1) 3505 return -EBUSY; 3506 return do_format(drive, &inparam.f); 3507 case FDFMTEND: 3508 case FDFLUSH: 3509 if (lock_fdc(drive)) 3510 return -EINTR; 3511 return invalidate_drive(bdev->bd_disk); 3512 case FDSETEMSGTRESH: 3513 drive_params[drive].max_errors.reporting = (unsigned short)(param & 0x0f); 3514 return 0; 3515 case FDGETMAXERRS: 3516 outparam = &drive_params[drive].max_errors; 3517 break; 3518 case FDSETMAXERRS: 3519 drive_params[drive].max_errors = inparam.max_errors; 3520 break; 3521 case FDGETDRVTYP: 3522 outparam = drive_name(type, drive); 3523 SUPBOUND(size, strlen((const char *)outparam) + 1); 3524 break; 3525 case FDSETDRVPRM: 3526 if (!valid_floppy_drive_params(inparam.dp.autodetect, 3527 inparam.dp.native_format)) 3528 return -EINVAL; 3529 drive_params[drive] = inparam.dp; 3530 break; 3531 case FDGETDRVPRM: 3532 outparam = &drive_params[drive]; 3533 break; 3534 case FDPOLLDRVSTAT: 3535 if (lock_fdc(drive)) 3536 return -EINTR; 3537 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR) 3538 return -EINTR; 3539 process_fd_request(); 3540 fallthrough; 3541 case FDGETDRVSTAT: 3542 outparam = &drive_state[drive]; 3543 break; 3544 case FDRESET: 3545 return user_reset_fdc(drive, (int)param, true); 3546 case FDGETFDCSTAT: 3547 outparam = &fdc_state[FDC(drive)]; 3548 break; 3549 case FDWERRORCLR: 3550 memset(&write_errors[drive], 0, sizeof(write_errors[drive])); 3551 return 0; 3552 case FDWERRORGET: 3553 outparam = &write_errors[drive]; 3554 break; 3555 case FDRAWCMD: 3556 return floppy_raw_cmd_ioctl(type, drive, cmd, (void __user *)param); 3557 case FDTWADDLE: 3558 if (lock_fdc(drive)) 3559 return -EINTR; 3560 twaddle(current_fdc, current_drive); 3561 process_fd_request(); 3562 return 0; 3563 default: 3564 return -EINVAL; 3565 } 3566 3567 if (_IOC_DIR(cmd) & _IOC_READ) 3568 return fd_copyout((void __user *)param, outparam, size); 3569 3570 return 0; 3571 } 3572 3573 static int fd_ioctl(struct block_device *bdev, blk_mode_t mode, 3574 unsigned int cmd, unsigned long param) 3575 { 3576 int ret; 3577 3578 mutex_lock(&floppy_mutex); 3579 ret = fd_locked_ioctl(bdev, mode, cmd, param); 3580 mutex_unlock(&floppy_mutex); 3581 3582 return ret; 3583 } 3584 3585 #ifdef CONFIG_COMPAT 3586 3587 struct compat_floppy_drive_params { 3588 char cmos; 3589 compat_ulong_t max_dtr; 3590 compat_ulong_t hlt; 3591 compat_ulong_t hut; 3592 compat_ulong_t srt; 3593 compat_ulong_t spinup; 3594 compat_ulong_t spindown; 3595 unsigned char spindown_offset; 3596 unsigned char select_delay; 3597 unsigned char rps; 3598 unsigned char tracks; 3599 compat_ulong_t timeout; 3600 unsigned char interleave_sect; 3601 struct floppy_max_errors max_errors; 3602 char flags; 3603 char read_track; 3604 short autodetect[FD_AUTODETECT_SIZE]; 3605 compat_int_t checkfreq; 3606 compat_int_t native_format; 3607 }; 3608 3609 struct compat_floppy_drive_struct { 3610 compat_ulong_t flags; 3611 compat_ulong_t spinup_date; 3612 compat_ulong_t select_date; 3613 compat_ulong_t first_read_date; 3614 short probed_format; 3615 short track; 3616 short maxblock; 3617 short maxtrack; 3618 compat_int_t generation; 3619 compat_int_t keep_data; 3620 compat_int_t fd_ref; 3621 compat_int_t fd_device; 3622 compat_int_t last_checked; 3623 compat_caddr_t dmabuf; 3624 compat_int_t bufblocks; 3625 }; 3626 3627 struct compat_floppy_fdc_state { 3628 compat_int_t spec1; 3629 compat_int_t spec2; 3630 compat_int_t dtr; 3631 unsigned char version; 3632 unsigned char dor; 3633 compat_ulong_t address; 3634 unsigned int rawcmd:2; 3635 unsigned int reset:1; 3636 unsigned int need_configure:1; 3637 unsigned int perp_mode:2; 3638 unsigned int has_fifo:1; 3639 unsigned int driver_version; 3640 unsigned char track[4]; 3641 }; 3642 3643 struct compat_floppy_write_errors { 3644 unsigned int write_errors; 3645 compat_ulong_t first_error_sector; 3646 compat_int_t first_error_generation; 3647 compat_ulong_t last_error_sector; 3648 compat_int_t last_error_generation; 3649 compat_uint_t badness; 3650 }; 3651 3652 #define FDSETPRM32 _IOW(2, 0x42, struct compat_floppy_struct) 3653 #define FDDEFPRM32 _IOW(2, 0x43, struct compat_floppy_struct) 3654 #define FDSETDRVPRM32 _IOW(2, 0x90, struct compat_floppy_drive_params) 3655 #define FDGETDRVPRM32 _IOR(2, 0x11, struct compat_floppy_drive_params) 3656 #define FDGETDRVSTAT32 _IOR(2, 0x12, struct compat_floppy_drive_struct) 3657 #define FDPOLLDRVSTAT32 _IOR(2, 0x13, struct compat_floppy_drive_struct) 3658 #define FDGETFDCSTAT32 _IOR(2, 0x15, struct compat_floppy_fdc_state) 3659 #define FDWERRORGET32 _IOR(2, 0x17, struct compat_floppy_write_errors) 3660 3661 static int compat_set_geometry(struct block_device *bdev, blk_mode_t mode, 3662 unsigned int cmd, struct compat_floppy_struct __user *arg) 3663 { 3664 struct floppy_struct v; 3665 int drive, type; 3666 int err; 3667 3668 BUILD_BUG_ON(offsetof(struct floppy_struct, name) != 3669 offsetof(struct compat_floppy_struct, name)); 3670 3671 if (!(mode & (BLK_OPEN_WRITE | BLK_OPEN_WRITE_IOCTL))) 3672 return -EPERM; 3673 3674 memset(&v, 0, sizeof(struct floppy_struct)); 3675 if (copy_from_user(&v, arg, offsetof(struct floppy_struct, name))) 3676 return -EFAULT; 3677 3678 mutex_lock(&floppy_mutex); 3679 drive = (long)bdev->bd_disk->private_data; 3680 type = ITYPE(drive_state[drive].fd_device); 3681 err = set_geometry(cmd == FDSETPRM32 ? FDSETPRM : FDDEFPRM, 3682 &v, drive, type, bdev); 3683 mutex_unlock(&floppy_mutex); 3684 return err; 3685 } 3686 3687 static int compat_get_prm(int drive, 3688 struct compat_floppy_struct __user *arg) 3689 { 3690 struct compat_floppy_struct v; 3691 struct floppy_struct *p; 3692 int err; 3693 3694 memset(&v, 0, sizeof(v)); 3695 mutex_lock(&floppy_mutex); 3696 err = get_floppy_geometry(drive, ITYPE(drive_state[drive].fd_device), 3697 &p); 3698 if (err) { 3699 mutex_unlock(&floppy_mutex); 3700 return err; 3701 } 3702 memcpy(&v, p, offsetof(struct floppy_struct, name)); 3703 mutex_unlock(&floppy_mutex); 3704 if (copy_to_user(arg, &v, sizeof(struct compat_floppy_struct))) 3705 return -EFAULT; 3706 return 0; 3707 } 3708 3709 static int compat_setdrvprm(int drive, 3710 struct compat_floppy_drive_params __user *arg) 3711 { 3712 struct compat_floppy_drive_params v; 3713 3714 if (!capable(CAP_SYS_ADMIN)) 3715 return -EPERM; 3716 if (copy_from_user(&v, arg, sizeof(struct compat_floppy_drive_params))) 3717 return -EFAULT; 3718 if (!valid_floppy_drive_params(v.autodetect, v.native_format)) 3719 return -EINVAL; 3720 mutex_lock(&floppy_mutex); 3721 drive_params[drive].cmos = v.cmos; 3722 drive_params[drive].max_dtr = v.max_dtr; 3723 drive_params[drive].hlt = v.hlt; 3724 drive_params[drive].hut = v.hut; 3725 drive_params[drive].srt = v.srt; 3726 drive_params[drive].spinup = v.spinup; 3727 drive_params[drive].spindown = v.spindown; 3728 drive_params[drive].spindown_offset = v.spindown_offset; 3729 drive_params[drive].select_delay = v.select_delay; 3730 drive_params[drive].rps = v.rps; 3731 drive_params[drive].tracks = v.tracks; 3732 drive_params[drive].timeout = v.timeout; 3733 drive_params[drive].interleave_sect = v.interleave_sect; 3734 drive_params[drive].max_errors = v.max_errors; 3735 drive_params[drive].flags = v.flags; 3736 drive_params[drive].read_track = v.read_track; 3737 memcpy(drive_params[drive].autodetect, v.autodetect, 3738 sizeof(v.autodetect)); 3739 drive_params[drive].checkfreq = v.checkfreq; 3740 drive_params[drive].native_format = v.native_format; 3741 mutex_unlock(&floppy_mutex); 3742 return 0; 3743 } 3744 3745 static int compat_getdrvprm(int drive, 3746 struct compat_floppy_drive_params __user *arg) 3747 { 3748 struct compat_floppy_drive_params v; 3749 3750 memset(&v, 0, sizeof(struct compat_floppy_drive_params)); 3751 mutex_lock(&floppy_mutex); 3752 v.cmos = drive_params[drive].cmos; 3753 v.max_dtr = drive_params[drive].max_dtr; 3754 v.hlt = drive_params[drive].hlt; 3755 v.hut = drive_params[drive].hut; 3756 v.srt = drive_params[drive].srt; 3757 v.spinup = drive_params[drive].spinup; 3758 v.spindown = drive_params[drive].spindown; 3759 v.spindown_offset = drive_params[drive].spindown_offset; 3760 v.select_delay = drive_params[drive].select_delay; 3761 v.rps = drive_params[drive].rps; 3762 v.tracks = drive_params[drive].tracks; 3763 v.timeout = drive_params[drive].timeout; 3764 v.interleave_sect = drive_params[drive].interleave_sect; 3765 v.max_errors = drive_params[drive].max_errors; 3766 v.flags = drive_params[drive].flags; 3767 v.read_track = drive_params[drive].read_track; 3768 memcpy(v.autodetect, drive_params[drive].autodetect, 3769 sizeof(v.autodetect)); 3770 v.checkfreq = drive_params[drive].checkfreq; 3771 v.native_format = drive_params[drive].native_format; 3772 mutex_unlock(&floppy_mutex); 3773 3774 if (copy_to_user(arg, &v, sizeof(struct compat_floppy_drive_params))) 3775 return -EFAULT; 3776 return 0; 3777 } 3778 3779 static int compat_getdrvstat(int drive, bool poll, 3780 struct compat_floppy_drive_struct __user *arg) 3781 { 3782 struct compat_floppy_drive_struct v; 3783 3784 memset(&v, 0, sizeof(struct compat_floppy_drive_struct)); 3785 mutex_lock(&floppy_mutex); 3786 3787 if (poll) { 3788 if (lock_fdc(drive)) 3789 goto Eintr; 3790 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR) 3791 goto Eintr; 3792 process_fd_request(); 3793 } 3794 v.flags = drive_state[drive].flags; 3795 v.spinup_date = drive_state[drive].spinup_date; 3796 v.select_date = drive_state[drive].select_date; 3797 v.first_read_date = drive_state[drive].first_read_date; 3798 v.probed_format = drive_state[drive].probed_format; 3799 v.track = drive_state[drive].track; 3800 v.maxblock = drive_state[drive].maxblock; 3801 v.maxtrack = drive_state[drive].maxtrack; 3802 v.generation = drive_state[drive].generation; 3803 v.keep_data = drive_state[drive].keep_data; 3804 v.fd_ref = drive_state[drive].fd_ref; 3805 v.fd_device = drive_state[drive].fd_device; 3806 v.last_checked = drive_state[drive].last_checked; 3807 v.dmabuf = (uintptr_t) drive_state[drive].dmabuf; 3808 v.bufblocks = drive_state[drive].bufblocks; 3809 mutex_unlock(&floppy_mutex); 3810 3811 if (copy_to_user(arg, &v, sizeof(struct compat_floppy_drive_struct))) 3812 return -EFAULT; 3813 return 0; 3814 Eintr: 3815 mutex_unlock(&floppy_mutex); 3816 return -EINTR; 3817 } 3818 3819 static int compat_getfdcstat(int drive, 3820 struct compat_floppy_fdc_state __user *arg) 3821 { 3822 struct compat_floppy_fdc_state v32; 3823 struct floppy_fdc_state v; 3824 3825 mutex_lock(&floppy_mutex); 3826 v = fdc_state[FDC(drive)]; 3827 mutex_unlock(&floppy_mutex); 3828 3829 memset(&v32, 0, sizeof(struct compat_floppy_fdc_state)); 3830 v32.spec1 = v.spec1; 3831 v32.spec2 = v.spec2; 3832 v32.dtr = v.dtr; 3833 v32.version = v.version; 3834 v32.dor = v.dor; 3835 v32.address = v.address; 3836 v32.rawcmd = v.rawcmd; 3837 v32.reset = v.reset; 3838 v32.need_configure = v.need_configure; 3839 v32.perp_mode = v.perp_mode; 3840 v32.has_fifo = v.has_fifo; 3841 v32.driver_version = v.driver_version; 3842 memcpy(v32.track, v.track, 4); 3843 if (copy_to_user(arg, &v32, sizeof(struct compat_floppy_fdc_state))) 3844 return -EFAULT; 3845 return 0; 3846 } 3847 3848 static int compat_werrorget(int drive, 3849 struct compat_floppy_write_errors __user *arg) 3850 { 3851 struct compat_floppy_write_errors v32; 3852 struct floppy_write_errors v; 3853 3854 memset(&v32, 0, sizeof(struct compat_floppy_write_errors)); 3855 mutex_lock(&floppy_mutex); 3856 v = write_errors[drive]; 3857 mutex_unlock(&floppy_mutex); 3858 v32.write_errors = v.write_errors; 3859 v32.first_error_sector = v.first_error_sector; 3860 v32.first_error_generation = v.first_error_generation; 3861 v32.last_error_sector = v.last_error_sector; 3862 v32.last_error_generation = v.last_error_generation; 3863 v32.badness = v.badness; 3864 if (copy_to_user(arg, &v32, sizeof(struct compat_floppy_write_errors))) 3865 return -EFAULT; 3866 return 0; 3867 } 3868 3869 static int fd_compat_ioctl(struct block_device *bdev, blk_mode_t mode, 3870 unsigned int cmd, unsigned long param) 3871 { 3872 int drive = (long)bdev->bd_disk->private_data; 3873 switch (cmd) { 3874 case CDROMEJECT: /* CD-ROM eject */ 3875 case 0x6470: /* SunOS floppy eject */ 3876 3877 case FDMSGON: 3878 case FDMSGOFF: 3879 case FDSETEMSGTRESH: 3880 case FDFLUSH: 3881 case FDWERRORCLR: 3882 case FDEJECT: 3883 case FDCLRPRM: 3884 case FDFMTBEG: 3885 case FDRESET: 3886 case FDTWADDLE: 3887 return fd_ioctl(bdev, mode, cmd, param); 3888 case FDSETMAXERRS: 3889 case FDGETMAXERRS: 3890 case FDGETDRVTYP: 3891 case FDFMTEND: 3892 case FDFMTTRK: 3893 case FDRAWCMD: 3894 return fd_ioctl(bdev, mode, cmd, 3895 (unsigned long)compat_ptr(param)); 3896 case FDSETPRM32: 3897 case FDDEFPRM32: 3898 return compat_set_geometry(bdev, mode, cmd, compat_ptr(param)); 3899 case FDGETPRM32: 3900 return compat_get_prm(drive, compat_ptr(param)); 3901 case FDSETDRVPRM32: 3902 return compat_setdrvprm(drive, compat_ptr(param)); 3903 case FDGETDRVPRM32: 3904 return compat_getdrvprm(drive, compat_ptr(param)); 3905 case FDPOLLDRVSTAT32: 3906 return compat_getdrvstat(drive, true, compat_ptr(param)); 3907 case FDGETDRVSTAT32: 3908 return compat_getdrvstat(drive, false, compat_ptr(param)); 3909 case FDGETFDCSTAT32: 3910 return compat_getfdcstat(drive, compat_ptr(param)); 3911 case FDWERRORGET32: 3912 return compat_werrorget(drive, compat_ptr(param)); 3913 } 3914 return -EINVAL; 3915 } 3916 #endif 3917 3918 static void __init config_types(void) 3919 { 3920 bool has_drive = false; 3921 int drive; 3922 3923 /* read drive info out of physical CMOS */ 3924 drive = 0; 3925 if (!drive_params[drive].cmos) 3926 drive_params[drive].cmos = FLOPPY0_TYPE; 3927 drive = 1; 3928 if (!drive_params[drive].cmos) 3929 drive_params[drive].cmos = FLOPPY1_TYPE; 3930 3931 /* FIXME: additional physical CMOS drive detection should go here */ 3932 3933 for (drive = 0; drive < N_DRIVE; drive++) { 3934 unsigned int type = drive_params[drive].cmos; 3935 struct floppy_drive_params *params; 3936 const char *name = NULL; 3937 char temparea[32]; 3938 3939 if (type < ARRAY_SIZE(default_drive_params)) { 3940 params = &default_drive_params[type].params; 3941 if (type) { 3942 name = default_drive_params[type].name; 3943 allowed_drive_mask |= 1 << drive; 3944 } else 3945 allowed_drive_mask &= ~(1 << drive); 3946 } else { 3947 params = &default_drive_params[0].params; 3948 snprintf(temparea, sizeof(temparea), 3949 "unknown type %d (usb?)", type); 3950 name = temparea; 3951 } 3952 if (name) { 3953 const char *prepend; 3954 if (!has_drive) { 3955 prepend = ""; 3956 has_drive = true; 3957 pr_info("Floppy drive(s):"); 3958 } else { 3959 prepend = ","; 3960 } 3961 3962 pr_cont("%s fd%d is %s", prepend, drive, name); 3963 } 3964 drive_params[drive] = *params; 3965 } 3966 3967 if (has_drive) 3968 pr_cont("\n"); 3969 } 3970 3971 static void floppy_release(struct gendisk *disk) 3972 { 3973 int drive = (long)disk->private_data; 3974 3975 mutex_lock(&floppy_mutex); 3976 mutex_lock(&open_lock); 3977 if (!drive_state[drive].fd_ref--) { 3978 DPRINT("floppy_release with fd_ref == 0"); 3979 drive_state[drive].fd_ref = 0; 3980 } 3981 if (!drive_state[drive].fd_ref) 3982 opened_disk[drive] = NULL; 3983 mutex_unlock(&open_lock); 3984 mutex_unlock(&floppy_mutex); 3985 } 3986 3987 /* 3988 * floppy_open check for aliasing (/dev/fd0 can be the same as 3989 * /dev/PS0 etc), and disallows simultaneous access to the same 3990 * drive with different device numbers. 3991 */ 3992 static int floppy_open(struct gendisk *disk, blk_mode_t mode) 3993 { 3994 int drive = (long)disk->private_data; 3995 int old_dev, new_dev; 3996 int try; 3997 int res = -EBUSY; 3998 char *tmp; 3999 4000 mutex_lock(&floppy_mutex); 4001 mutex_lock(&open_lock); 4002 old_dev = drive_state[drive].fd_device; 4003 if (opened_disk[drive] && opened_disk[drive] != disk) 4004 goto out2; 4005 4006 if (!drive_state[drive].fd_ref && (drive_params[drive].flags & FD_BROKEN_DCL)) { 4007 set_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags); 4008 set_bit(FD_VERIFY_BIT, &drive_state[drive].flags); 4009 } 4010 4011 drive_state[drive].fd_ref++; 4012 4013 opened_disk[drive] = disk; 4014 4015 res = -ENXIO; 4016 4017 if (!floppy_track_buffer) { 4018 /* if opening an ED drive, reserve a big buffer, 4019 * else reserve a small one */ 4020 if ((drive_params[drive].cmos == 6) || (drive_params[drive].cmos == 5)) 4021 try = 64; /* Only 48 actually useful */ 4022 else 4023 try = 32; /* Only 24 actually useful */ 4024 4025 tmp = (char *)fd_dma_mem_alloc(1024 * try); 4026 if (!tmp && !floppy_track_buffer) { 4027 try >>= 1; /* buffer only one side */ 4028 INFBOUND(try, 16); 4029 tmp = (char *)fd_dma_mem_alloc(1024 * try); 4030 } 4031 if (!tmp && !floppy_track_buffer) 4032 fallback_on_nodma_alloc(&tmp, 2048 * try); 4033 if (!tmp && !floppy_track_buffer) { 4034 DPRINT("Unable to allocate DMA memory\n"); 4035 goto out; 4036 } 4037 if (floppy_track_buffer) { 4038 if (tmp) 4039 fd_dma_mem_free((unsigned long)tmp, try * 1024); 4040 } else { 4041 buffer_min = buffer_max = -1; 4042 floppy_track_buffer = tmp; 4043 max_buffer_sectors = try; 4044 } 4045 } 4046 4047 new_dev = disk->first_minor; 4048 drive_state[drive].fd_device = new_dev; 4049 set_capacity(disks[drive][ITYPE(new_dev)], floppy_sizes[new_dev]); 4050 if (old_dev != -1 && old_dev != new_dev) { 4051 if (buffer_drive == drive) 4052 buffer_track = -1; 4053 } 4054 4055 if (fdc_state[FDC(drive)].rawcmd == 1) 4056 fdc_state[FDC(drive)].rawcmd = 2; 4057 if (!(mode & BLK_OPEN_NDELAY)) { 4058 if (mode & (BLK_OPEN_READ | BLK_OPEN_WRITE)) { 4059 drive_state[drive].last_checked = 0; 4060 clear_bit(FD_OPEN_SHOULD_FAIL_BIT, 4061 &drive_state[drive].flags); 4062 if (disk_check_media_change(disk)) 4063 floppy_revalidate(disk); 4064 if (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags)) 4065 goto out; 4066 if (test_bit(FD_OPEN_SHOULD_FAIL_BIT, &drive_state[drive].flags)) 4067 goto out; 4068 } 4069 res = -EROFS; 4070 if ((mode & BLK_OPEN_WRITE) && 4071 !test_bit(FD_DISK_WRITABLE_BIT, &drive_state[drive].flags)) 4072 goto out; 4073 } 4074 mutex_unlock(&open_lock); 4075 mutex_unlock(&floppy_mutex); 4076 return 0; 4077 out: 4078 drive_state[drive].fd_ref--; 4079 4080 if (!drive_state[drive].fd_ref) 4081 opened_disk[drive] = NULL; 4082 out2: 4083 mutex_unlock(&open_lock); 4084 mutex_unlock(&floppy_mutex); 4085 return res; 4086 } 4087 4088 /* 4089 * Check if the disk has been changed or if a change has been faked. 4090 */ 4091 static unsigned int floppy_check_events(struct gendisk *disk, 4092 unsigned int clearing) 4093 { 4094 int drive = (long)disk->private_data; 4095 4096 if (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags) || 4097 test_bit(FD_VERIFY_BIT, &drive_state[drive].flags)) 4098 return DISK_EVENT_MEDIA_CHANGE; 4099 4100 if (time_after(jiffies, drive_state[drive].last_checked + drive_params[drive].checkfreq)) { 4101 if (lock_fdc(drive)) 4102 return 0; 4103 poll_drive(false, 0); 4104 process_fd_request(); 4105 } 4106 4107 if (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags) || 4108 test_bit(FD_VERIFY_BIT, &drive_state[drive].flags) || 4109 test_bit(drive, &fake_change) || 4110 drive_no_geom(drive)) 4111 return DISK_EVENT_MEDIA_CHANGE; 4112 return 0; 4113 } 4114 4115 /* 4116 * This implements "read block 0" for floppy_revalidate(). 4117 * Needed for format autodetection, checking whether there is 4118 * a disk in the drive, and whether that disk is writable. 4119 */ 4120 4121 struct rb0_cbdata { 4122 int drive; 4123 struct completion complete; 4124 }; 4125 4126 static void floppy_rb0_cb(struct bio *bio) 4127 { 4128 struct rb0_cbdata *cbdata = (struct rb0_cbdata *)bio->bi_private; 4129 int drive = cbdata->drive; 4130 4131 if (bio->bi_status) { 4132 pr_info("floppy: error %d while reading block 0\n", 4133 bio->bi_status); 4134 set_bit(FD_OPEN_SHOULD_FAIL_BIT, &drive_state[drive].flags); 4135 } 4136 complete(&cbdata->complete); 4137 } 4138 4139 static int __floppy_read_block_0(struct block_device *bdev, int drive) 4140 { 4141 struct bio bio; 4142 struct bio_vec bio_vec; 4143 struct page *page; 4144 struct rb0_cbdata cbdata; 4145 4146 page = alloc_page(GFP_NOIO); 4147 if (!page) { 4148 process_fd_request(); 4149 return -ENOMEM; 4150 } 4151 4152 cbdata.drive = drive; 4153 4154 bio_init(&bio, bdev, &bio_vec, 1, REQ_OP_READ); 4155 __bio_add_page(&bio, page, block_size(bdev), 0); 4156 4157 bio.bi_iter.bi_sector = 0; 4158 bio.bi_flags |= (1 << BIO_QUIET); 4159 bio.bi_private = &cbdata; 4160 bio.bi_end_io = floppy_rb0_cb; 4161 4162 init_completion(&cbdata.complete); 4163 4164 submit_bio(&bio); 4165 process_fd_request(); 4166 4167 wait_for_completion(&cbdata.complete); 4168 4169 __free_page(page); 4170 4171 return 0; 4172 } 4173 4174 /* revalidate the floppy disk, i.e. trigger format autodetection by reading 4175 * the bootblock (block 0). "Autodetection" is also needed to check whether 4176 * there is a disk in the drive at all... Thus we also do it for fixed 4177 * geometry formats */ 4178 static int floppy_revalidate(struct gendisk *disk) 4179 { 4180 int drive = (long)disk->private_data; 4181 int cf; 4182 int res = 0; 4183 4184 if (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags) || 4185 test_bit(FD_VERIFY_BIT, &drive_state[drive].flags) || 4186 test_bit(drive, &fake_change) || 4187 drive_no_geom(drive)) { 4188 if (WARN(atomic_read(&usage_count) == 0, 4189 "VFS: revalidate called on non-open device.\n")) 4190 return -EFAULT; 4191 4192 res = lock_fdc(drive); 4193 if (res) 4194 return res; 4195 cf = (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags) || 4196 test_bit(FD_VERIFY_BIT, &drive_state[drive].flags)); 4197 if (!(cf || test_bit(drive, &fake_change) || drive_no_geom(drive))) { 4198 process_fd_request(); /*already done by another thread */ 4199 return 0; 4200 } 4201 drive_state[drive].maxblock = 0; 4202 drive_state[drive].maxtrack = 0; 4203 if (buffer_drive == drive) 4204 buffer_track = -1; 4205 clear_bit(drive, &fake_change); 4206 clear_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags); 4207 if (cf) 4208 drive_state[drive].generation++; 4209 if (drive_no_geom(drive)) { 4210 /* auto-sensing */ 4211 res = __floppy_read_block_0(opened_disk[drive]->part0, 4212 drive); 4213 } else { 4214 if (cf) 4215 poll_drive(false, FD_RAW_NEED_DISK); 4216 process_fd_request(); 4217 } 4218 } 4219 set_capacity(disk, floppy_sizes[drive_state[drive].fd_device]); 4220 return res; 4221 } 4222 4223 static const struct block_device_operations floppy_fops = { 4224 .owner = THIS_MODULE, 4225 .open = floppy_open, 4226 .release = floppy_release, 4227 .ioctl = fd_ioctl, 4228 .getgeo = fd_getgeo, 4229 .check_events = floppy_check_events, 4230 #ifdef CONFIG_COMPAT 4231 .compat_ioctl = fd_compat_ioctl, 4232 #endif 4233 }; 4234 4235 /* 4236 * Floppy Driver initialization 4237 * ============================= 4238 */ 4239 4240 /* Determine the floppy disk controller type */ 4241 /* This routine was written by David C. Niemi */ 4242 static char __init get_fdc_version(int fdc) 4243 { 4244 int r; 4245 4246 output_byte(fdc, FD_DUMPREGS); /* 82072 and better know DUMPREGS */ 4247 if (fdc_state[fdc].reset) 4248 return FDC_NONE; 4249 r = result(fdc); 4250 if (r <= 0x00) 4251 return FDC_NONE; /* No FDC present ??? */ 4252 if ((r == 1) && (reply_buffer[ST0] == 0x80)) { 4253 pr_info("FDC %d is an 8272A\n", fdc); 4254 return FDC_8272A; /* 8272a/765 don't know DUMPREGS */ 4255 } 4256 if (r != 10) { 4257 pr_info("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n", 4258 fdc, r); 4259 return FDC_UNKNOWN; 4260 } 4261 4262 if (!fdc_configure(fdc)) { 4263 pr_info("FDC %d is an 82072\n", fdc); 4264 return FDC_82072; /* 82072 doesn't know CONFIGURE */ 4265 } 4266 4267 output_byte(fdc, FD_PERPENDICULAR); 4268 if (need_more_output(fdc) == MORE_OUTPUT) { 4269 output_byte(fdc, 0); 4270 } else { 4271 pr_info("FDC %d is an 82072A\n", fdc); 4272 return FDC_82072A; /* 82072A as found on Sparcs. */ 4273 } 4274 4275 output_byte(fdc, FD_UNLOCK); 4276 r = result(fdc); 4277 if ((r == 1) && (reply_buffer[ST0] == 0x80)) { 4278 pr_info("FDC %d is a pre-1991 82077\n", fdc); 4279 return FDC_82077_ORIG; /* Pre-1991 82077, doesn't know 4280 * LOCK/UNLOCK */ 4281 } 4282 if ((r != 1) || (reply_buffer[ST0] != 0x00)) { 4283 pr_info("FDC %d init: UNLOCK: unexpected return of %d bytes.\n", 4284 fdc, r); 4285 return FDC_UNKNOWN; 4286 } 4287 output_byte(fdc, FD_PARTID); 4288 r = result(fdc); 4289 if (r != 1) { 4290 pr_info("FDC %d init: PARTID: unexpected return of %d bytes.\n", 4291 fdc, r); 4292 return FDC_UNKNOWN; 4293 } 4294 if (reply_buffer[ST0] == 0x80) { 4295 pr_info("FDC %d is a post-1991 82077\n", fdc); 4296 return FDC_82077; /* Revised 82077AA passes all the tests */ 4297 } 4298 switch (reply_buffer[ST0] >> 5) { 4299 case 0x0: 4300 /* Either a 82078-1 or a 82078SL running at 5Volt */ 4301 pr_info("FDC %d is an 82078.\n", fdc); 4302 return FDC_82078; 4303 case 0x1: 4304 pr_info("FDC %d is a 44pin 82078\n", fdc); 4305 return FDC_82078; 4306 case 0x2: 4307 pr_info("FDC %d is a S82078B\n", fdc); 4308 return FDC_S82078B; 4309 case 0x3: 4310 pr_info("FDC %d is a National Semiconductor PC87306\n", fdc); 4311 return FDC_87306; 4312 default: 4313 pr_info("FDC %d init: 82078 variant with unknown PARTID=%d.\n", 4314 fdc, reply_buffer[ST0] >> 5); 4315 return FDC_82078_UNKN; 4316 } 4317 } /* get_fdc_version */ 4318 4319 /* lilo configuration */ 4320 4321 static void __init floppy_set_flags(int *ints, int param, int param2) 4322 { 4323 int i; 4324 4325 for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) { 4326 if (param) 4327 default_drive_params[i].params.flags |= param2; 4328 else 4329 default_drive_params[i].params.flags &= ~param2; 4330 } 4331 DPRINT("%s flag 0x%x\n", param2 ? "Setting" : "Clearing", param); 4332 } 4333 4334 static void __init daring(int *ints, int param, int param2) 4335 { 4336 int i; 4337 4338 for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) { 4339 if (param) { 4340 default_drive_params[i].params.select_delay = 0; 4341 default_drive_params[i].params.flags |= 4342 FD_SILENT_DCL_CLEAR; 4343 } else { 4344 default_drive_params[i].params.select_delay = 4345 2 * HZ / 100; 4346 default_drive_params[i].params.flags &= 4347 ~FD_SILENT_DCL_CLEAR; 4348 } 4349 } 4350 DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken"); 4351 } 4352 4353 static void __init set_cmos(int *ints, int dummy, int dummy2) 4354 { 4355 int current_drive = 0; 4356 4357 if (ints[0] != 2) { 4358 DPRINT("wrong number of parameters for CMOS\n"); 4359 return; 4360 } 4361 current_drive = ints[1]; 4362 if (current_drive < 0 || current_drive >= 8) { 4363 DPRINT("bad drive for set_cmos\n"); 4364 return; 4365 } 4366 #if N_FDC > 1 4367 if (current_drive >= 4 && !FDC2) 4368 FDC2 = 0x370; 4369 #endif 4370 drive_params[current_drive].cmos = ints[2]; 4371 DPRINT("setting CMOS code to %d\n", ints[2]); 4372 } 4373 4374 static struct param_table { 4375 const char *name; 4376 void (*fn) (int *ints, int param, int param2); 4377 int *var; 4378 int def_param; 4379 int param2; 4380 } config_params[] __initdata = { 4381 {"allowed_drive_mask", NULL, &allowed_drive_mask, 0xff, 0}, /* obsolete */ 4382 {"all_drives", NULL, &allowed_drive_mask, 0xff, 0}, /* obsolete */ 4383 {"asus_pci", NULL, &allowed_drive_mask, 0x33, 0}, 4384 {"irq", NULL, &FLOPPY_IRQ, 6, 0}, 4385 {"dma", NULL, &FLOPPY_DMA, 2, 0}, 4386 {"daring", daring, NULL, 1, 0}, 4387 #if N_FDC > 1 4388 {"two_fdc", NULL, &FDC2, 0x370, 0}, 4389 {"one_fdc", NULL, &FDC2, 0, 0}, 4390 #endif 4391 {"thinkpad", floppy_set_flags, NULL, 1, FD_INVERTED_DCL}, 4392 {"broken_dcl", floppy_set_flags, NULL, 1, FD_BROKEN_DCL}, 4393 {"messages", floppy_set_flags, NULL, 1, FTD_MSG}, 4394 {"silent_dcl_clear", floppy_set_flags, NULL, 1, FD_SILENT_DCL_CLEAR}, 4395 {"debug", floppy_set_flags, NULL, 1, FD_DEBUG}, 4396 {"nodma", NULL, &can_use_virtual_dma, 1, 0}, 4397 {"omnibook", NULL, &can_use_virtual_dma, 1, 0}, 4398 {"yesdma", NULL, &can_use_virtual_dma, 0, 0}, 4399 {"fifo_depth", NULL, &fifo_depth, 0xa, 0}, 4400 {"nofifo", NULL, &no_fifo, 0x20, 0}, 4401 {"usefifo", NULL, &no_fifo, 0, 0}, 4402 {"cmos", set_cmos, NULL, 0, 0}, 4403 {"slow", NULL, &slow_floppy, 1, 0}, 4404 {"unexpected_interrupts", NULL, &print_unex, 1, 0}, 4405 {"no_unexpected_interrupts", NULL, &print_unex, 0, 0}, 4406 {"L40SX", NULL, &print_unex, 0, 0} 4407 4408 EXTRA_FLOPPY_PARAMS 4409 }; 4410 4411 static int __init floppy_setup(char *str) 4412 { 4413 int i; 4414 int param; 4415 int ints[11]; 4416 4417 str = get_options(str, ARRAY_SIZE(ints), ints); 4418 if (str) { 4419 for (i = 0; i < ARRAY_SIZE(config_params); i++) { 4420 if (strcmp(str, config_params[i].name) == 0) { 4421 if (ints[0]) 4422 param = ints[1]; 4423 else 4424 param = config_params[i].def_param; 4425 if (config_params[i].fn) 4426 config_params[i].fn(ints, param, 4427 config_params[i]. 4428 param2); 4429 if (config_params[i].var) { 4430 DPRINT("%s=%d\n", str, param); 4431 *config_params[i].var = param; 4432 } 4433 return 1; 4434 } 4435 } 4436 } 4437 if (str) { 4438 DPRINT("unknown floppy option [%s]\n", str); 4439 4440 DPRINT("allowed options are:"); 4441 for (i = 0; i < ARRAY_SIZE(config_params); i++) 4442 pr_cont(" %s", config_params[i].name); 4443 pr_cont("\n"); 4444 } else 4445 DPRINT("botched floppy option\n"); 4446 DPRINT("Read Documentation/admin-guide/blockdev/floppy.rst\n"); 4447 return 0; 4448 } 4449 4450 static int have_no_fdc = -ENODEV; 4451 4452 static ssize_t floppy_cmos_show(struct device *dev, 4453 struct device_attribute *attr, char *buf) 4454 { 4455 struct platform_device *p = to_platform_device(dev); 4456 int drive; 4457 4458 drive = p->id; 4459 return sprintf(buf, "%X\n", drive_params[drive].cmos); 4460 } 4461 4462 static DEVICE_ATTR(cmos, 0444, floppy_cmos_show, NULL); 4463 4464 static struct attribute *floppy_dev_attrs[] = { 4465 &dev_attr_cmos.attr, 4466 NULL 4467 }; 4468 4469 ATTRIBUTE_GROUPS(floppy_dev); 4470 4471 static void floppy_device_release(struct device *dev) 4472 { 4473 } 4474 4475 static int floppy_resume(struct device *dev) 4476 { 4477 int fdc; 4478 int saved_drive; 4479 4480 saved_drive = current_drive; 4481 for (fdc = 0; fdc < N_FDC; fdc++) 4482 if (fdc_state[fdc].address != -1) 4483 user_reset_fdc(REVDRIVE(fdc, 0), FD_RESET_ALWAYS, false); 4484 set_fdc(saved_drive); 4485 return 0; 4486 } 4487 4488 static const struct dev_pm_ops floppy_pm_ops = { 4489 .resume = floppy_resume, 4490 .restore = floppy_resume, 4491 }; 4492 4493 static struct platform_driver floppy_driver = { 4494 .driver = { 4495 .name = "floppy", 4496 .pm = &floppy_pm_ops, 4497 }, 4498 }; 4499 4500 static const struct blk_mq_ops floppy_mq_ops = { 4501 .queue_rq = floppy_queue_rq, 4502 }; 4503 4504 static struct platform_device floppy_device[N_DRIVE]; 4505 static bool registered[N_DRIVE]; 4506 4507 static bool floppy_available(int drive) 4508 { 4509 if (!(allowed_drive_mask & (1 << drive))) 4510 return false; 4511 if (fdc_state[FDC(drive)].version == FDC_NONE) 4512 return false; 4513 return true; 4514 } 4515 4516 static int floppy_alloc_disk(unsigned int drive, unsigned int type) 4517 { 4518 struct queue_limits lim = { 4519 .max_hw_sectors = 64, 4520 .features = BLK_FEAT_ROTATIONAL, 4521 }; 4522 struct gendisk *disk; 4523 4524 disk = blk_mq_alloc_disk(&tag_sets[drive], &lim, NULL); 4525 if (IS_ERR(disk)) 4526 return PTR_ERR(disk); 4527 4528 disk->major = FLOPPY_MAJOR; 4529 disk->first_minor = TOMINOR(drive) | (type << 2); 4530 disk->minors = 1; 4531 disk->fops = &floppy_fops; 4532 disk->flags |= GENHD_FL_NO_PART; 4533 disk->events = DISK_EVENT_MEDIA_CHANGE; 4534 if (type) 4535 sprintf(disk->disk_name, "fd%d_type%d", drive, type); 4536 else 4537 sprintf(disk->disk_name, "fd%d", drive); 4538 /* to be cleaned up... */ 4539 disk->private_data = (void *)(long)drive; 4540 disk->flags |= GENHD_FL_REMOVABLE; 4541 4542 disks[drive][type] = disk; 4543 return 0; 4544 } 4545 4546 static DEFINE_MUTEX(floppy_probe_lock); 4547 4548 static void floppy_probe(dev_t dev) 4549 { 4550 unsigned int drive = (MINOR(dev) & 3) | ((MINOR(dev) & 0x80) >> 5); 4551 unsigned int type = (MINOR(dev) >> 2) & 0x1f; 4552 4553 if (drive >= N_DRIVE || !floppy_available(drive) || 4554 type >= ARRAY_SIZE(floppy_type)) 4555 return; 4556 4557 mutex_lock(&floppy_probe_lock); 4558 if (disks[drive][type]) 4559 goto out; 4560 if (floppy_alloc_disk(drive, type)) 4561 goto out; 4562 if (add_disk(disks[drive][type])) 4563 goto cleanup_disk; 4564 out: 4565 mutex_unlock(&floppy_probe_lock); 4566 return; 4567 4568 cleanup_disk: 4569 put_disk(disks[drive][type]); 4570 disks[drive][type] = NULL; 4571 mutex_unlock(&floppy_probe_lock); 4572 } 4573 4574 static int __init do_floppy_init(void) 4575 { 4576 int i, unit, drive, err; 4577 4578 set_debugt(); 4579 interruptjiffies = resultjiffies = jiffies; 4580 4581 #if defined(CONFIG_PPC) 4582 if (check_legacy_ioport(FDC1)) 4583 return -ENODEV; 4584 #endif 4585 4586 raw_cmd = NULL; 4587 4588 floppy_wq = alloc_ordered_workqueue("floppy", 0); 4589 if (!floppy_wq) 4590 return -ENOMEM; 4591 4592 for (drive = 0; drive < N_DRIVE; drive++) { 4593 memset(&tag_sets[drive], 0, sizeof(tag_sets[drive])); 4594 tag_sets[drive].ops = &floppy_mq_ops; 4595 tag_sets[drive].nr_hw_queues = 1; 4596 tag_sets[drive].nr_maps = 1; 4597 tag_sets[drive].queue_depth = 2; 4598 tag_sets[drive].numa_node = NUMA_NO_NODE; 4599 err = blk_mq_alloc_tag_set(&tag_sets[drive]); 4600 if (err) 4601 goto out_put_disk; 4602 4603 err = floppy_alloc_disk(drive, 0); 4604 if (err) { 4605 blk_mq_free_tag_set(&tag_sets[drive]); 4606 goto out_put_disk; 4607 } 4608 4609 timer_setup(&motor_off_timer[drive], motor_off_callback, 0); 4610 } 4611 4612 err = __register_blkdev(FLOPPY_MAJOR, "fd", floppy_probe); 4613 if (err) 4614 goto out_put_disk; 4615 4616 err = platform_driver_register(&floppy_driver); 4617 if (err) 4618 goto out_unreg_blkdev; 4619 4620 for (i = 0; i < 256; i++) 4621 if (ITYPE(i)) 4622 floppy_sizes[i] = floppy_type[ITYPE(i)].size; 4623 else 4624 floppy_sizes[i] = MAX_DISK_SIZE << 1; 4625 4626 reschedule_timeout(MAXTIMEOUT, "floppy init"); 4627 config_types(); 4628 4629 for (i = 0; i < N_FDC; i++) { 4630 memset(&fdc_state[i], 0, sizeof(*fdc_state)); 4631 fdc_state[i].dtr = -1; 4632 fdc_state[i].dor = 0x4; 4633 #if defined(__sparc__) || defined(__mc68000__) 4634 /*sparcs/sun3x don't have a DOR reset which we can fall back on to */ 4635 #ifdef __mc68000__ 4636 if (MACH_IS_SUN3X) 4637 #endif 4638 fdc_state[i].version = FDC_82072A; 4639 #endif 4640 } 4641 4642 use_virtual_dma = can_use_virtual_dma & 1; 4643 fdc_state[0].address = FDC1; 4644 if (fdc_state[0].address == -1) { 4645 cancel_delayed_work(&fd_timeout); 4646 err = -ENODEV; 4647 goto out_unreg_driver; 4648 } 4649 #if N_FDC > 1 4650 fdc_state[1].address = FDC2; 4651 #endif 4652 4653 current_fdc = 0; /* reset fdc in case of unexpected interrupt */ 4654 err = floppy_grab_irq_and_dma(); 4655 if (err) { 4656 cancel_delayed_work(&fd_timeout); 4657 err = -EBUSY; 4658 goto out_unreg_driver; 4659 } 4660 4661 /* initialise drive state */ 4662 for (drive = 0; drive < N_DRIVE; drive++) { 4663 memset(&drive_state[drive], 0, sizeof(drive_state[drive])); 4664 memset(&write_errors[drive], 0, sizeof(write_errors[drive])); 4665 set_bit(FD_DISK_NEWCHANGE_BIT, &drive_state[drive].flags); 4666 set_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags); 4667 set_bit(FD_VERIFY_BIT, &drive_state[drive].flags); 4668 drive_state[drive].fd_device = -1; 4669 floppy_track_buffer = NULL; 4670 max_buffer_sectors = 0; 4671 } 4672 /* 4673 * Small 10 msec delay to let through any interrupt that 4674 * initialization might have triggered, to not 4675 * confuse detection: 4676 */ 4677 msleep(10); 4678 4679 for (i = 0; i < N_FDC; i++) { 4680 fdc_state[i].driver_version = FD_DRIVER_VERSION; 4681 for (unit = 0; unit < 4; unit++) 4682 fdc_state[i].track[unit] = 0; 4683 if (fdc_state[i].address == -1) 4684 continue; 4685 fdc_state[i].rawcmd = 2; 4686 if (user_reset_fdc(REVDRIVE(i, 0), FD_RESET_ALWAYS, false)) { 4687 /* free ioports reserved by floppy_grab_irq_and_dma() */ 4688 floppy_release_regions(i); 4689 fdc_state[i].address = -1; 4690 fdc_state[i].version = FDC_NONE; 4691 continue; 4692 } 4693 /* Try to determine the floppy controller type */ 4694 fdc_state[i].version = get_fdc_version(i); 4695 if (fdc_state[i].version == FDC_NONE) { 4696 /* free ioports reserved by floppy_grab_irq_and_dma() */ 4697 floppy_release_regions(i); 4698 fdc_state[i].address = -1; 4699 continue; 4700 } 4701 if (can_use_virtual_dma == 2 && 4702 fdc_state[i].version < FDC_82072A) 4703 can_use_virtual_dma = 0; 4704 4705 have_no_fdc = 0; 4706 /* Not all FDCs seem to be able to handle the version command 4707 * properly, so force a reset for the standard FDC clones, 4708 * to avoid interrupt garbage. 4709 */ 4710 user_reset_fdc(REVDRIVE(i, 0), FD_RESET_ALWAYS, false); 4711 } 4712 current_fdc = 0; 4713 cancel_delayed_work(&fd_timeout); 4714 current_drive = 0; 4715 initialized = true; 4716 if (have_no_fdc) { 4717 DPRINT("no floppy controllers found\n"); 4718 err = have_no_fdc; 4719 goto out_release_dma; 4720 } 4721 4722 for (drive = 0; drive < N_DRIVE; drive++) { 4723 if (!floppy_available(drive)) 4724 continue; 4725 4726 floppy_device[drive].name = floppy_device_name; 4727 floppy_device[drive].id = drive; 4728 floppy_device[drive].dev.release = floppy_device_release; 4729 floppy_device[drive].dev.groups = floppy_dev_groups; 4730 4731 err = platform_device_register(&floppy_device[drive]); 4732 if (err) 4733 goto out_remove_drives; 4734 4735 registered[drive] = true; 4736 4737 err = device_add_disk(&floppy_device[drive].dev, 4738 disks[drive][0], NULL); 4739 if (err) { 4740 platform_device_unregister(&floppy_device[drive]); 4741 registered[drive] = false; 4742 goto out_remove_drives; 4743 } 4744 } 4745 4746 return 0; 4747 4748 out_remove_drives: 4749 while (drive--) { 4750 if (floppy_available(drive)) { 4751 del_gendisk(disks[drive][0]); 4752 if (registered[drive]) 4753 platform_device_unregister(&floppy_device[drive]); 4754 } 4755 } 4756 out_release_dma: 4757 /* Drain the queued redo_fd_request() before releasing IRQ and DMA. */ 4758 flush_workqueue(floppy_wq); 4759 if (atomic_read(&usage_count)) 4760 floppy_release_irq_and_dma(); 4761 out_unreg_driver: 4762 platform_driver_unregister(&floppy_driver); 4763 out_unreg_blkdev: 4764 unregister_blkdev(FLOPPY_MAJOR, "fd"); 4765 out_put_disk: 4766 destroy_workqueue(floppy_wq); 4767 for (drive = 0; drive < N_DRIVE; drive++) { 4768 if (!disks[drive][0]) 4769 break; 4770 timer_delete_sync(&motor_off_timer[drive]); 4771 put_disk(disks[drive][0]); 4772 blk_mq_free_tag_set(&tag_sets[drive]); 4773 } 4774 return err; 4775 } 4776 4777 #ifndef MODULE 4778 static __init void floppy_async_init(void *data, async_cookie_t cookie) 4779 { 4780 do_floppy_init(); 4781 } 4782 #endif 4783 4784 static int __init floppy_init(void) 4785 { 4786 #ifdef MODULE 4787 return do_floppy_init(); 4788 #else 4789 /* Don't hold up the bootup by the floppy initialization */ 4790 async_schedule(floppy_async_init, NULL); 4791 return 0; 4792 #endif 4793 } 4794 4795 static const struct io_region { 4796 int offset; 4797 int size; 4798 } io_regions[] = { 4799 { 2, 1 }, 4800 /* address + 3 is sometimes reserved by pnp bios for motherboard */ 4801 { 4, 2 }, 4802 /* address + 6 is reserved, and may be taken by IDE. 4803 * Unfortunately, Adaptec doesn't know this :-(, */ 4804 { 7, 1 }, 4805 }; 4806 4807 static void floppy_release_allocated_regions(int fdc, const struct io_region *p) 4808 { 4809 while (p != io_regions) { 4810 p--; 4811 release_region(fdc_state[fdc].address + p->offset, p->size); 4812 } 4813 } 4814 4815 static int floppy_request_regions(int fdc) 4816 { 4817 const struct io_region *p; 4818 4819 for (p = io_regions; p < ARRAY_END(io_regions); p++) { 4820 if (!request_region(fdc_state[fdc].address + p->offset, 4821 p->size, "floppy")) { 4822 DPRINT("Floppy io-port 0x%04lx in use\n", 4823 fdc_state[fdc].address + p->offset); 4824 floppy_release_allocated_regions(fdc, p); 4825 return -EBUSY; 4826 } 4827 } 4828 return 0; 4829 } 4830 4831 static void floppy_release_regions(int fdc) 4832 { 4833 floppy_release_allocated_regions(fdc, ARRAY_END(io_regions)); 4834 } 4835 4836 static int floppy_grab_irq_and_dma(void) 4837 { 4838 int fdc; 4839 4840 if (atomic_inc_return(&usage_count) > 1) 4841 return 0; 4842 4843 /* 4844 * We might have scheduled a free_irq(), wait it to 4845 * drain first: 4846 */ 4847 flush_workqueue(floppy_wq); 4848 4849 if (fd_request_irq()) { 4850 DPRINT("Unable to grab IRQ%d for the floppy driver\n", 4851 FLOPPY_IRQ); 4852 atomic_dec(&usage_count); 4853 return -1; 4854 } 4855 if (fd_request_dma()) { 4856 DPRINT("Unable to grab DMA%d for the floppy driver\n", 4857 FLOPPY_DMA); 4858 if (can_use_virtual_dma & 2) 4859 use_virtual_dma = can_use_virtual_dma = 1; 4860 if (!(can_use_virtual_dma & 1)) { 4861 fd_free_irq(); 4862 atomic_dec(&usage_count); 4863 return -1; 4864 } 4865 } 4866 4867 for (fdc = 0; fdc < N_FDC; fdc++) { 4868 if (fdc_state[fdc].address != -1) { 4869 if (floppy_request_regions(fdc)) 4870 goto cleanup; 4871 } 4872 } 4873 for (fdc = 0; fdc < N_FDC; fdc++) { 4874 if (fdc_state[fdc].address != -1) { 4875 reset_fdc_info(fdc, 1); 4876 fdc_outb(fdc_state[fdc].dor, fdc, FD_DOR); 4877 } 4878 } 4879 4880 set_dor(0, ~0, 8); /* avoid immediate interrupt */ 4881 4882 for (fdc = 0; fdc < N_FDC; fdc++) 4883 if (fdc_state[fdc].address != -1) 4884 fdc_outb(fdc_state[fdc].dor, fdc, FD_DOR); 4885 /* 4886 * The driver will try and free resources and relies on us 4887 * to know if they were allocated or not. 4888 */ 4889 current_fdc = 0; 4890 irqdma_allocated = 1; 4891 return 0; 4892 cleanup: 4893 fd_free_irq(); 4894 fd_free_dma(); 4895 while (--fdc >= 0) 4896 floppy_release_regions(fdc); 4897 current_fdc = 0; 4898 atomic_dec(&usage_count); 4899 return -1; 4900 } 4901 4902 static void floppy_release_irq_and_dma(void) 4903 { 4904 int fdc; 4905 #ifndef __sparc__ 4906 int drive; 4907 #endif 4908 long tmpsize; 4909 unsigned long tmpaddr; 4910 4911 if (!atomic_dec_and_test(&usage_count)) 4912 return; 4913 4914 if (irqdma_allocated) { 4915 fd_disable_dma(); 4916 fd_free_dma(); 4917 fd_free_irq(); 4918 irqdma_allocated = 0; 4919 } 4920 set_dor(0, ~0, 8); 4921 #if N_FDC > 1 4922 set_dor(1, ~8, 0); 4923 #endif 4924 4925 if (floppy_track_buffer && max_buffer_sectors) { 4926 tmpsize = max_buffer_sectors * 1024; 4927 tmpaddr = (unsigned long)floppy_track_buffer; 4928 floppy_track_buffer = NULL; 4929 max_buffer_sectors = 0; 4930 buffer_min = buffer_max = -1; 4931 fd_dma_mem_free(tmpaddr, tmpsize); 4932 } 4933 #ifndef __sparc__ 4934 for (drive = 0; drive < N_FDC * 4; drive++) 4935 if (timer_pending(motor_off_timer + drive)) 4936 pr_info("motor off timer %d still active\n", drive); 4937 #endif 4938 4939 if (delayed_work_pending(&fd_timeout)) 4940 pr_info("floppy timer still active:%s\n", timeout_message); 4941 if (delayed_work_pending(&fd_timer)) 4942 pr_info("auxiliary floppy timer still active\n"); 4943 if (work_pending(&floppy_work)) 4944 pr_info("work still pending\n"); 4945 for (fdc = 0; fdc < N_FDC; fdc++) 4946 if (fdc_state[fdc].address != -1) 4947 floppy_release_regions(fdc); 4948 } 4949 4950 #ifdef MODULE 4951 4952 static char *floppy; 4953 4954 static void __init parse_floppy_cfg_string(char *cfg) 4955 { 4956 char *ptr; 4957 4958 while (*cfg) { 4959 ptr = cfg; 4960 while (*cfg && *cfg != ' ' && *cfg != '\t') 4961 cfg++; 4962 if (*cfg) { 4963 *cfg = '\0'; 4964 cfg++; 4965 } 4966 if (*ptr) 4967 floppy_setup(ptr); 4968 } 4969 } 4970 4971 static int __init floppy_module_init(void) 4972 { 4973 if (floppy) 4974 parse_floppy_cfg_string(floppy); 4975 return floppy_init(); 4976 } 4977 module_init(floppy_module_init); 4978 4979 static void __exit floppy_module_exit(void) 4980 { 4981 int drive, i; 4982 4983 unregister_blkdev(FLOPPY_MAJOR, "fd"); 4984 platform_driver_unregister(&floppy_driver); 4985 4986 destroy_workqueue(floppy_wq); 4987 4988 for (drive = 0; drive < N_DRIVE; drive++) { 4989 timer_delete_sync(&motor_off_timer[drive]); 4990 4991 if (floppy_available(drive)) { 4992 for (i = 0; i < ARRAY_SIZE(floppy_type); i++) { 4993 if (disks[drive][i]) 4994 del_gendisk(disks[drive][i]); 4995 } 4996 if (registered[drive]) 4997 platform_device_unregister(&floppy_device[drive]); 4998 } 4999 for (i = 0; i < ARRAY_SIZE(floppy_type); i++) { 5000 if (disks[drive][i]) 5001 put_disk(disks[drive][i]); 5002 } 5003 blk_mq_free_tag_set(&tag_sets[drive]); 5004 } 5005 5006 cancel_delayed_work_sync(&fd_timeout); 5007 cancel_delayed_work_sync(&fd_timer); 5008 5009 if (atomic_read(&usage_count)) 5010 floppy_release_irq_and_dma(); 5011 5012 /* eject disk, if any */ 5013 fd_eject(0); 5014 } 5015 5016 module_exit(floppy_module_exit); 5017 5018 module_param(floppy, charp, 0); 5019 module_param(FLOPPY_IRQ, int, 0); 5020 module_param(FLOPPY_DMA, int, 0); 5021 MODULE_AUTHOR("Alain L. Knaff"); 5022 MODULE_DESCRIPTION("Normal floppy disk support"); 5023 MODULE_LICENSE("GPL"); 5024 5025 /* This doesn't actually get used other than for module information */ 5026 static const struct pnp_device_id floppy_pnpids[] = { 5027 { .id = "PNP0700" }, 5028 { } 5029 }; 5030 5031 MODULE_DEVICE_TABLE(pnp, floppy_pnpids); 5032 5033 #else 5034 5035 __setup("floppy=", floppy_setup); 5036 module_init(floppy_init) 5037 #endif 5038 5039 MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR); 5040