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