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