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