block: remove i_bdev
[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             raw_cmd->kernel_data == bio_data(current_req->bio)) {
2404                 /* transfer directly from buffer */
2405                 cont->done(1);
2406         } else if (CT(raw_cmd->cmd[COMMAND]) == FD_READ) {
2407                 buffer_track = raw_cmd->track;
2408                 buffer_drive = current_drive;
2409                 INFBOUND(buffer_max, nr_sectors + fsector_t);
2410         }
2411         cont->redo();
2412 }
2413
2414 /* Compute maximal contiguous buffer size. */
2415 static int buffer_chain_size(void)
2416 {
2417         struct bio_vec bv;
2418         int size;
2419         struct req_iterator iter;
2420         char *base;
2421
2422         base = bio_data(current_req->bio);
2423         size = 0;
2424
2425         rq_for_each_segment(bv, current_req, iter) {
2426                 if (page_address(bv.bv_page) + bv.bv_offset != base + size)
2427                         break;
2428
2429                 size += bv.bv_len;
2430         }
2431
2432         return size >> 9;
2433 }
2434
2435 /* Compute the maximal transfer size */
2436 static int transfer_size(int ssize, int max_sector, int max_size)
2437 {
2438         SUPBOUND(max_sector, fsector_t + max_size);
2439
2440         /* alignment */
2441         max_sector -= (max_sector % _floppy->sect) % ssize;
2442
2443         /* transfer size, beginning not aligned */
2444         current_count_sectors = max_sector - fsector_t;
2445
2446         return max_sector;
2447 }
2448
2449 /*
2450  * Move data from/to the track buffer to/from the buffer cache.
2451  */
2452 static void copy_buffer(int ssize, int max_sector, int max_sector_2)
2453 {
2454         int remaining;          /* number of transferred 512-byte sectors */
2455         struct bio_vec bv;
2456         char *buffer;
2457         char *dma_buffer;
2458         int size;
2459         struct req_iterator iter;
2460
2461         max_sector = transfer_size(ssize,
2462                                    min(max_sector, max_sector_2),
2463                                    blk_rq_sectors(current_req));
2464
2465         if (current_count_sectors <= 0 && CT(raw_cmd->cmd[COMMAND]) == FD_WRITE &&
2466             buffer_max > fsector_t + blk_rq_sectors(current_req))
2467                 current_count_sectors = min_t(int, buffer_max - fsector_t,
2468                                               blk_rq_sectors(current_req));
2469
2470         remaining = current_count_sectors << 9;
2471         if (remaining > blk_rq_bytes(current_req) && CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) {
2472                 DPRINT("in copy buffer\n");
2473                 pr_info("current_count_sectors=%ld\n", current_count_sectors);
2474                 pr_info("remaining=%d\n", remaining >> 9);
2475                 pr_info("current_req->nr_sectors=%u\n",
2476                         blk_rq_sectors(current_req));
2477                 pr_info("current_req->current_nr_sectors=%u\n",
2478                         blk_rq_cur_sectors(current_req));
2479                 pr_info("max_sector=%d\n", max_sector);
2480                 pr_info("ssize=%d\n", ssize);
2481         }
2482
2483         buffer_max = max(max_sector, buffer_max);
2484
2485         dma_buffer = floppy_track_buffer + ((fsector_t - buffer_min) << 9);
2486
2487         size = blk_rq_cur_bytes(current_req);
2488
2489         rq_for_each_segment(bv, current_req, iter) {
2490                 if (!remaining)
2491                         break;
2492
2493                 size = bv.bv_len;
2494                 SUPBOUND(size, remaining);
2495
2496                 buffer = page_address(bv.bv_page) + bv.bv_offset;
2497                 if (dma_buffer + size >
2498                     floppy_track_buffer + (max_buffer_sectors << 10) ||
2499                     dma_buffer < floppy_track_buffer) {
2500                         DPRINT("buffer overrun in copy buffer %d\n",
2501                                (int)((floppy_track_buffer - dma_buffer) >> 9));
2502                         pr_info("fsector_t=%d buffer_min=%d\n",
2503                                 fsector_t, buffer_min);
2504                         pr_info("current_count_sectors=%ld\n",
2505                                 current_count_sectors);
2506                         if (CT(raw_cmd->cmd[COMMAND]) == FD_READ)
2507                                 pr_info("read\n");
2508                         if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE)
2509                                 pr_info("write\n");
2510                         break;
2511                 }
2512                 if (((unsigned long)buffer) % 512)
2513                         DPRINT("%p buffer not aligned\n", buffer);
2514
2515                 if (CT(raw_cmd->cmd[COMMAND]) == FD_READ)
2516                         memcpy(buffer, dma_buffer, size);
2517                 else
2518                         memcpy(dma_buffer, buffer, size);
2519
2520                 remaining -= size;
2521                 dma_buffer += size;
2522         }
2523         if (remaining) {
2524                 if (remaining > 0)
2525                         max_sector -= remaining >> 9;
2526                 DPRINT("weirdness: remaining %d\n", remaining >> 9);
2527         }
2528 }
2529
2530 /* work around a bug in pseudo DMA
2531  * (on some FDCs) pseudo DMA does not stop when the CPU stops
2532  * sending data.  Hence we need a different way to signal the
2533  * transfer length:  We use raw_cmd->cmd[SECT_PER_TRACK].  Unfortunately, this
2534  * does not work with MT, hence we can only transfer one head at
2535  * a time
2536  */
2537 static void virtualdmabug_workaround(void)
2538 {
2539         int hard_sectors;
2540         int end_sector;
2541
2542         if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) {
2543                 raw_cmd->cmd[COMMAND] &= ~0x80; /* switch off multiple track mode */
2544
2545                 hard_sectors = raw_cmd->length >> (7 + raw_cmd->cmd[SIZECODE]);
2546                 end_sector = raw_cmd->cmd[SECTOR] + hard_sectors - 1;
2547                 if (end_sector > raw_cmd->cmd[SECT_PER_TRACK]) {
2548                         pr_info("too many sectors %d > %d\n",
2549                                 end_sector, raw_cmd->cmd[SECT_PER_TRACK]);
2550                         return;
2551                 }
2552                 raw_cmd->cmd[SECT_PER_TRACK] = end_sector;
2553                                         /* make sure raw_cmd->cmd[SECT_PER_TRACK]
2554                                          * points to end of transfer */
2555         }
2556 }
2557
2558 /*
2559  * Formulate a read/write request.
2560  * this routine decides where to load the data (directly to buffer, or to
2561  * tmp floppy area), how much data to load (the size of the buffer, the whole
2562  * track, or a single sector)
2563  * All floppy_track_buffer handling goes in here. If we ever add track buffer
2564  * allocation on the fly, it should be done here. No other part should need
2565  * modification.
2566  */
2567
2568 static int make_raw_rw_request(void)
2569 {
2570         int aligned_sector_t;
2571         int max_sector;
2572         int max_size;
2573         int tracksize;
2574         int ssize;
2575
2576         if (WARN(max_buffer_sectors == 0, "VFS: Block I/O scheduled on unopened device\n"))
2577                 return 0;
2578
2579         set_fdc((long)current_req->rq_disk->private_data);
2580
2581         raw_cmd = &default_raw_cmd;
2582         raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK;
2583         raw_cmd->cmd_count = NR_RW;
2584         if (rq_data_dir(current_req) == READ) {
2585                 raw_cmd->flags |= FD_RAW_READ;
2586                 raw_cmd->cmd[COMMAND] = FM_MODE(_floppy, FD_READ);
2587         } else if (rq_data_dir(current_req) == WRITE) {
2588                 raw_cmd->flags |= FD_RAW_WRITE;
2589                 raw_cmd->cmd[COMMAND] = FM_MODE(_floppy, FD_WRITE);
2590         } else {
2591                 DPRINT("%s: unknown command\n", __func__);
2592                 return 0;
2593         }
2594
2595         max_sector = _floppy->sect * _floppy->head;
2596
2597         raw_cmd->cmd[TRACK] = (int)blk_rq_pos(current_req) / max_sector;
2598         fsector_t = (int)blk_rq_pos(current_req) % max_sector;
2599         if (_floppy->track && raw_cmd->cmd[TRACK] >= _floppy->track) {
2600                 if (blk_rq_cur_sectors(current_req) & 1) {
2601                         current_count_sectors = 1;
2602                         return 1;
2603                 } else
2604                         return 0;
2605         }
2606         raw_cmd->cmd[HEAD] = fsector_t / _floppy->sect;
2607
2608         if (((_floppy->stretch & (FD_SWAPSIDES | FD_SECTBASEMASK)) ||
2609              test_bit(FD_NEED_TWADDLE_BIT, &drive_state[current_drive].flags)) &&
2610             fsector_t < _floppy->sect)
2611                 max_sector = _floppy->sect;
2612
2613         /* 2M disks have phantom sectors on the first track */
2614         if ((_floppy->rate & FD_2M) && (!raw_cmd->cmd[TRACK]) && (!raw_cmd->cmd[HEAD])) {
2615                 max_sector = 2 * _floppy->sect / 3;
2616                 if (fsector_t >= max_sector) {
2617                         current_count_sectors =
2618                             min_t(int, _floppy->sect - fsector_t,
2619                                   blk_rq_sectors(current_req));
2620                         return 1;
2621                 }
2622                 raw_cmd->cmd[SIZECODE] = 2;
2623         } else
2624                 raw_cmd->cmd[SIZECODE] = FD_SIZECODE(_floppy);
2625         raw_cmd->rate = _floppy->rate & 0x43;
2626         if ((_floppy->rate & FD_2M) &&
2627             (raw_cmd->cmd[TRACK] || raw_cmd->cmd[HEAD]) && raw_cmd->rate == 2)
2628                 raw_cmd->rate = 1;
2629
2630         if (raw_cmd->cmd[SIZECODE])
2631                 raw_cmd->cmd[SIZECODE2] = 0xff;
2632         else
2633                 raw_cmd->cmd[SIZECODE2] = 0x80;
2634         raw_cmd->track = raw_cmd->cmd[TRACK] << STRETCH(_floppy);
2635         raw_cmd->cmd[DR_SELECT] = UNIT(current_drive) + PH_HEAD(_floppy, raw_cmd->cmd[HEAD]);
2636         raw_cmd->cmd[GAP] = _floppy->gap;
2637         ssize = DIV_ROUND_UP(1 << raw_cmd->cmd[SIZECODE], 4);
2638         raw_cmd->cmd[SECT_PER_TRACK] = _floppy->sect << 2 >> raw_cmd->cmd[SIZECODE];
2639         raw_cmd->cmd[SECTOR] = ((fsector_t % _floppy->sect) << 2 >> raw_cmd->cmd[SIZECODE]) +
2640             FD_SECTBASE(_floppy);
2641
2642         /* tracksize describes the size which can be filled up with sectors
2643          * of size ssize.
2644          */
2645         tracksize = _floppy->sect - _floppy->sect % ssize;
2646         if (tracksize < _floppy->sect) {
2647                 raw_cmd->cmd[SECT_PER_TRACK]++;
2648                 if (tracksize <= fsector_t % _floppy->sect)
2649                         raw_cmd->cmd[SECTOR]--;
2650
2651                 /* if we are beyond tracksize, fill up using smaller sectors */
2652                 while (tracksize <= fsector_t % _floppy->sect) {
2653                         while (tracksize + ssize > _floppy->sect) {
2654                                 raw_cmd->cmd[SIZECODE]--;
2655                                 ssize >>= 1;
2656                         }
2657                         raw_cmd->cmd[SECTOR]++;
2658                         raw_cmd->cmd[SECT_PER_TRACK]++;
2659                         tracksize += ssize;
2660                 }
2661                 max_sector = raw_cmd->cmd[HEAD] * _floppy->sect + tracksize;
2662         } else if (!raw_cmd->cmd[TRACK] && !raw_cmd->cmd[HEAD] && !(_floppy->rate & FD_2M) && probing) {
2663                 max_sector = _floppy->sect;
2664         } else if (!raw_cmd->cmd[HEAD] && CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) {
2665                 /* for virtual DMA bug workaround */
2666                 max_sector = _floppy->sect;
2667         }
2668
2669         in_sector_offset = (fsector_t % _floppy->sect) % ssize;
2670         aligned_sector_t = fsector_t - in_sector_offset;
2671         max_size = blk_rq_sectors(current_req);
2672         if ((raw_cmd->track == buffer_track) &&
2673             (current_drive == buffer_drive) &&
2674             (fsector_t >= buffer_min) && (fsector_t < buffer_max)) {
2675                 /* data already in track buffer */
2676                 if (CT(raw_cmd->cmd[COMMAND]) == FD_READ) {
2677                         copy_buffer(1, max_sector, buffer_max);
2678                         return 1;
2679                 }
2680         } else if (in_sector_offset || blk_rq_sectors(current_req) < ssize) {
2681                 if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) {
2682                         unsigned int sectors;
2683
2684                         sectors = fsector_t + blk_rq_sectors(current_req);
2685                         if (sectors > ssize && sectors < ssize + ssize)
2686                                 max_size = ssize + ssize;
2687                         else
2688                                 max_size = ssize;
2689                 }
2690                 raw_cmd->flags &= ~FD_RAW_WRITE;
2691                 raw_cmd->flags |= FD_RAW_READ;
2692                 raw_cmd->cmd[COMMAND] = FM_MODE(_floppy, FD_READ);
2693         } else if ((unsigned long)bio_data(current_req->bio) < MAX_DMA_ADDRESS) {
2694                 unsigned long dma_limit;
2695                 int direct, indirect;
2696
2697                 indirect =
2698                     transfer_size(ssize, max_sector,
2699                                   max_buffer_sectors * 2) - fsector_t;
2700
2701                 /*
2702                  * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2703                  * on a 64 bit machine!
2704                  */
2705                 max_size = buffer_chain_size();
2706                 dma_limit = (MAX_DMA_ADDRESS -
2707                              ((unsigned long)bio_data(current_req->bio))) >> 9;
2708                 if ((unsigned long)max_size > dma_limit)
2709                         max_size = dma_limit;
2710                 /* 64 kb boundaries */
2711                 if (CROSS_64KB(bio_data(current_req->bio), max_size << 9))
2712                         max_size = (K_64 -
2713                                     ((unsigned long)bio_data(current_req->bio)) %
2714                                     K_64) >> 9;
2715                 direct = transfer_size(ssize, max_sector, max_size) - fsector_t;
2716                 /*
2717                  * We try to read tracks, but if we get too many errors, we
2718                  * go back to reading just one sector at a time.
2719                  *
2720                  * This means we should be able to read a sector even if there
2721                  * are other bad sectors on this track.
2722                  */
2723                 if (!direct ||
2724                     (indirect * 2 > direct * 3 &&
2725                      *errors < drive_params[current_drive].max_errors.read_track &&
2726                      ((!probing ||
2727                        (drive_params[current_drive].read_track & (1 << drive_state[current_drive].probed_format)))))) {
2728                         max_size = blk_rq_sectors(current_req);
2729                 } else {
2730                         raw_cmd->kernel_data = bio_data(current_req->bio);
2731                         raw_cmd->length = current_count_sectors << 9;
2732                         if (raw_cmd->length == 0) {
2733                                 DPRINT("%s: zero dma transfer attempted\n", __func__);
2734                                 DPRINT("indirect=%d direct=%d fsector_t=%d\n",
2735                                        indirect, direct, fsector_t);
2736                                 return 0;
2737                         }
2738                         virtualdmabug_workaround();
2739                         return 2;
2740                 }
2741         }
2742
2743         if (CT(raw_cmd->cmd[COMMAND]) == FD_READ)
2744                 max_size = max_sector;  /* unbounded */
2745
2746         /* claim buffer track if needed */
2747         if (buffer_track != raw_cmd->track ||   /* bad track */
2748             buffer_drive != current_drive ||    /* bad drive */
2749             fsector_t > buffer_max ||
2750             fsector_t < buffer_min ||
2751             ((CT(raw_cmd->cmd[COMMAND]) == FD_READ ||
2752               (!in_sector_offset && blk_rq_sectors(current_req) >= ssize)) &&
2753              max_sector > 2 * max_buffer_sectors + buffer_min &&
2754              max_size + fsector_t > 2 * max_buffer_sectors + buffer_min)) {
2755                 /* not enough space */
2756                 buffer_track = -1;
2757                 buffer_drive = current_drive;
2758                 buffer_max = buffer_min = aligned_sector_t;
2759         }
2760         raw_cmd->kernel_data = floppy_track_buffer +
2761                 ((aligned_sector_t - buffer_min) << 9);
2762
2763         if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE) {
2764                 /* copy write buffer to track buffer.
2765                  * if we get here, we know that the write
2766                  * is either aligned or the data already in the buffer
2767                  * (buffer will be overwritten) */
2768                 if (in_sector_offset && buffer_track == -1)
2769                         DPRINT("internal error offset !=0 on write\n");
2770                 buffer_track = raw_cmd->track;
2771                 buffer_drive = current_drive;
2772                 copy_buffer(ssize, max_sector,
2773                             2 * max_buffer_sectors + buffer_min);
2774         } else
2775                 transfer_size(ssize, max_sector,
2776                               2 * max_buffer_sectors + buffer_min -
2777                               aligned_sector_t);
2778
2779         /* round up current_count_sectors to get dma xfer size */
2780         raw_cmd->length = in_sector_offset + current_count_sectors;
2781         raw_cmd->length = ((raw_cmd->length - 1) | (ssize - 1)) + 1;
2782         raw_cmd->length <<= 9;
2783         if ((raw_cmd->length < current_count_sectors << 9) ||
2784             (raw_cmd->kernel_data != bio_data(current_req->bio) &&
2785              CT(raw_cmd->cmd[COMMAND]) == FD_WRITE &&
2786              (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
2787               aligned_sector_t < buffer_min)) ||
2788             raw_cmd->length % (128 << raw_cmd->cmd[SIZECODE]) ||
2789             raw_cmd->length <= 0 || current_count_sectors <= 0) {
2790                 DPRINT("fractionary current count b=%lx s=%lx\n",
2791                        raw_cmd->length, current_count_sectors);
2792                 if (raw_cmd->kernel_data != bio_data(current_req->bio))
2793                         pr_info("addr=%d, length=%ld\n",
2794                                 (int)((raw_cmd->kernel_data -
2795                                        floppy_track_buffer) >> 9),
2796                                 current_count_sectors);
2797                 pr_info("st=%d ast=%d mse=%d msi=%d\n",
2798                         fsector_t, aligned_sector_t, max_sector, max_size);
2799                 pr_info("ssize=%x SIZECODE=%d\n", ssize, raw_cmd->cmd[SIZECODE]);
2800                 pr_info("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2801                         raw_cmd->cmd[COMMAND], raw_cmd->cmd[SECTOR],
2802                         raw_cmd->cmd[HEAD], raw_cmd->cmd[TRACK]);
2803                 pr_info("buffer drive=%d\n", buffer_drive);
2804                 pr_info("buffer track=%d\n", buffer_track);
2805                 pr_info("buffer_min=%d\n", buffer_min);
2806                 pr_info("buffer_max=%d\n", buffer_max);
2807                 return 0;
2808         }
2809
2810         if (raw_cmd->kernel_data != bio_data(current_req->bio)) {
2811                 if (raw_cmd->kernel_data < floppy_track_buffer ||
2812                     current_count_sectors < 0 ||
2813                     raw_cmd->length < 0 ||
2814                     raw_cmd->kernel_data + raw_cmd->length >
2815                     floppy_track_buffer + (max_buffer_sectors << 10)) {
2816                         DPRINT("buffer overrun in schedule dma\n");
2817                         pr_info("fsector_t=%d buffer_min=%d current_count=%ld\n",
2818                                 fsector_t, buffer_min, raw_cmd->length >> 9);
2819                         pr_info("current_count_sectors=%ld\n",
2820                                 current_count_sectors);
2821                         if (CT(raw_cmd->cmd[COMMAND]) == FD_READ)
2822                                 pr_info("read\n");
2823                         if (CT(raw_cmd->cmd[COMMAND]) == FD_WRITE)
2824                                 pr_info("write\n");
2825                         return 0;
2826                 }
2827         } else if (raw_cmd->length > blk_rq_bytes(current_req) ||
2828                    current_count_sectors > blk_rq_sectors(current_req)) {
2829                 DPRINT("buffer overrun in direct transfer\n");
2830                 return 0;
2831         } else if (raw_cmd->length < current_count_sectors << 9) {
2832                 DPRINT("more sectors than bytes\n");
2833                 pr_info("bytes=%ld\n", raw_cmd->length >> 9);
2834                 pr_info("sectors=%ld\n", current_count_sectors);
2835         }
2836         if (raw_cmd->length == 0) {
2837                 DPRINT("zero dma transfer attempted from make_raw_request\n");
2838                 return 0;
2839         }
2840
2841         virtualdmabug_workaround();
2842         return 2;
2843 }
2844
2845 static int set_next_request(void)
2846 {
2847         current_req = list_first_entry_or_null(&floppy_reqs, struct request,
2848                                                queuelist);
2849         if (current_req) {
2850                 current_req->error_count = 0;
2851                 list_del_init(&current_req->queuelist);
2852         }
2853         return current_req != NULL;
2854 }
2855
2856 /* Starts or continues processing request. Will automatically unlock the
2857  * driver at end of request.
2858  */
2859 static void redo_fd_request(void)
2860 {
2861         int drive;
2862         int tmp;
2863
2864         lastredo = jiffies;
2865         if (current_drive < N_DRIVE)
2866                 floppy_off(current_drive);
2867
2868 do_request:
2869         if (!current_req) {
2870                 int pending;
2871
2872                 spin_lock_irq(&floppy_lock);
2873                 pending = set_next_request();
2874                 spin_unlock_irq(&floppy_lock);
2875                 if (!pending) {
2876                         do_floppy = NULL;
2877                         unlock_fdc();
2878                         return;
2879                 }
2880         }
2881         drive = (long)current_req->rq_disk->private_data;
2882         set_fdc(drive);
2883         reschedule_timeout(current_drive, "redo fd request");
2884
2885         set_floppy(drive);
2886         raw_cmd = &default_raw_cmd;
2887         raw_cmd->flags = 0;
2888         if (start_motor(redo_fd_request))
2889                 return;
2890
2891         disk_change(current_drive);
2892         if (test_bit(current_drive, &fake_change) ||
2893             test_bit(FD_DISK_CHANGED_BIT, &drive_state[current_drive].flags)) {
2894                 DPRINT("disk absent or changed during operation\n");
2895                 request_done(0);
2896                 goto do_request;
2897         }
2898         if (!_floppy) { /* Autodetection */
2899                 if (!probing) {
2900                         drive_state[current_drive].probed_format = 0;
2901                         if (next_valid_format(current_drive)) {
2902                                 DPRINT("no autodetectable formats\n");
2903                                 _floppy = NULL;
2904                                 request_done(0);
2905                                 goto do_request;
2906                         }
2907                 }
2908                 probing = 1;
2909                 _floppy = floppy_type + drive_params[current_drive].autodetect[drive_state[current_drive].probed_format];
2910         } else
2911                 probing = 0;
2912         errors = &(current_req->error_count);
2913         tmp = make_raw_rw_request();
2914         if (tmp < 2) {
2915                 request_done(tmp);
2916                 goto do_request;
2917         }
2918
2919         if (test_bit(FD_NEED_TWADDLE_BIT, &drive_state[current_drive].flags))
2920                 twaddle(current_fdc, current_drive);
2921         schedule_bh(floppy_start);
2922         debugt(__func__, "queue fd request");
2923         return;
2924 }
2925
2926 static const struct cont_t rw_cont = {
2927         .interrupt      = rw_interrupt,
2928         .redo           = redo_fd_request,
2929         .error          = bad_flp_intr,
2930         .done           = request_done
2931 };
2932
2933 /* schedule the request and automatically unlock the driver on completion */
2934 static void process_fd_request(void)
2935 {
2936         cont = &rw_cont;
2937         schedule_bh(redo_fd_request);
2938 }
2939
2940 static blk_status_t floppy_queue_rq(struct blk_mq_hw_ctx *hctx,
2941                                     const struct blk_mq_queue_data *bd)
2942 {
2943         blk_mq_start_request(bd->rq);
2944
2945         if (WARN(max_buffer_sectors == 0,
2946                  "VFS: %s called on non-open device\n", __func__))
2947                 return BLK_STS_IOERR;
2948
2949         if (WARN(atomic_read(&usage_count) == 0,
2950                  "warning: usage count=0, current_req=%p sect=%ld flags=%llx\n",
2951                  current_req, (long)blk_rq_pos(current_req),
2952                  (unsigned long long) current_req->cmd_flags))
2953                 return BLK_STS_IOERR;
2954
2955         if (test_and_set_bit(0, &fdc_busy)) {
2956                 /* fdc busy, this new request will be treated when the
2957                    current one is done */
2958                 is_alive(__func__, "old request running");
2959                 return BLK_STS_RESOURCE;
2960         }
2961
2962         spin_lock_irq(&floppy_lock);
2963         list_add_tail(&bd->rq->queuelist, &floppy_reqs);
2964         spin_unlock_irq(&floppy_lock);
2965
2966         command_status = FD_COMMAND_NONE;
2967         __reschedule_timeout(MAXTIMEOUT, "fd_request");
2968         set_fdc(0);
2969         process_fd_request();
2970         is_alive(__func__, "");
2971         return BLK_STS_OK;
2972 }
2973
2974 static const struct cont_t poll_cont = {
2975         .interrupt      = success_and_wakeup,
2976         .redo           = floppy_ready,
2977         .error          = generic_failure,
2978         .done           = generic_done
2979 };
2980
2981 static int poll_drive(bool interruptible, int flag)
2982 {
2983         /* no auto-sense, just clear dcl */
2984         raw_cmd = &default_raw_cmd;
2985         raw_cmd->flags = flag;
2986         raw_cmd->track = 0;
2987         raw_cmd->cmd_count = 0;
2988         cont = &poll_cont;
2989         debug_dcl(drive_params[current_drive].flags,
2990                   "setting NEWCHANGE in poll_drive\n");
2991         set_bit(FD_DISK_NEWCHANGE_BIT, &drive_state[current_drive].flags);
2992
2993         return wait_til_done(floppy_ready, interruptible);
2994 }
2995
2996 /*
2997  * User triggered reset
2998  * ====================
2999  */
3000
3001 static void reset_intr(void)
3002 {
3003         pr_info("weird, reset interrupt called\n");
3004 }
3005
3006 static const struct cont_t reset_cont = {
3007         .interrupt      = reset_intr,
3008         .redo           = success_and_wakeup,
3009         .error          = generic_failure,
3010         .done           = generic_done
3011 };
3012
3013 /*
3014  * Resets the FDC connected to drive <drive>.
3015  * Both current_drive and current_fdc are changed to match the new drive.
3016  */
3017 static int user_reset_fdc(int drive, int arg, bool interruptible)
3018 {
3019         int ret;
3020
3021         if (lock_fdc(drive))
3022                 return -EINTR;
3023
3024         if (arg == FD_RESET_ALWAYS)
3025                 fdc_state[current_fdc].reset = 1;
3026         if (fdc_state[current_fdc].reset) {
3027                 /* note: reset_fdc will take care of unlocking the driver
3028                  * on completion.
3029                  */
3030                 cont = &reset_cont;
3031                 ret = wait_til_done(reset_fdc, interruptible);
3032                 if (ret == -EINTR)
3033                         return -EINTR;
3034         }
3035         process_fd_request();
3036         return 0;
3037 }
3038
3039 /*
3040  * Misc Ioctl's and support
3041  * ========================
3042  */
3043 static inline int fd_copyout(void __user *param, const void *address,
3044                              unsigned long size)
3045 {
3046         return copy_to_user(param, address, size) ? -EFAULT : 0;
3047 }
3048
3049 static inline int fd_copyin(void __user *param, void *address,
3050                             unsigned long size)
3051 {
3052         return copy_from_user(address, param, size) ? -EFAULT : 0;
3053 }
3054
3055 static const char *drive_name(int type, int drive)
3056 {
3057         struct floppy_struct *floppy;
3058
3059         if (type)
3060                 floppy = floppy_type + type;
3061         else {
3062                 if (drive_params[drive].native_format)
3063                         floppy = floppy_type + drive_params[drive].native_format;
3064                 else
3065                         return "(null)";
3066         }
3067         if (floppy->name)
3068                 return floppy->name;
3069         else
3070                 return "(null)";
3071 }
3072
3073 /* raw commands */
3074 static void raw_cmd_done(int flag)
3075 {
3076         int i;
3077
3078         if (!flag) {
3079                 raw_cmd->flags |= FD_RAW_FAILURE;
3080                 raw_cmd->flags |= FD_RAW_HARDFAILURE;
3081         } else {
3082                 raw_cmd->reply_count = inr;
3083                 if (raw_cmd->reply_count > FD_RAW_REPLY_SIZE)
3084                         raw_cmd->reply_count = 0;
3085                 for (i = 0; i < raw_cmd->reply_count; i++)
3086                         raw_cmd->reply[i] = reply_buffer[i];
3087
3088                 if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3089                         unsigned long flags;
3090                         flags = claim_dma_lock();
3091                         raw_cmd->length = fd_get_dma_residue();
3092                         release_dma_lock(flags);
3093                 }
3094
3095                 if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
3096                     (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
3097                         raw_cmd->flags |= FD_RAW_FAILURE;
3098
3099                 if (disk_change(current_drive))
3100                         raw_cmd->flags |= FD_RAW_DISK_CHANGE;
3101                 else
3102                         raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
3103                 if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
3104                         motor_off_callback(&motor_off_timer[current_drive]);
3105
3106                 if (raw_cmd->next &&
3107                     (!(raw_cmd->flags & FD_RAW_FAILURE) ||
3108                      !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
3109                     ((raw_cmd->flags & FD_RAW_FAILURE) ||
3110                      !(raw_cmd->flags & FD_RAW_STOP_IF_SUCCESS))) {
3111                         raw_cmd = raw_cmd->next;
3112                         return;
3113                 }
3114         }
3115         generic_done(flag);
3116 }
3117
3118 static const struct cont_t raw_cmd_cont = {
3119         .interrupt      = success_and_wakeup,
3120         .redo           = floppy_start,
3121         .error          = generic_failure,
3122         .done           = raw_cmd_done
3123 };
3124
3125 static int raw_cmd_copyout(int cmd, void __user *param,
3126                                   struct floppy_raw_cmd *ptr)
3127 {
3128         int ret;
3129
3130         while (ptr) {
3131                 struct floppy_raw_cmd cmd = *ptr;
3132                 cmd.next = NULL;
3133                 cmd.kernel_data = NULL;
3134                 ret = copy_to_user(param, &cmd, sizeof(cmd));
3135                 if (ret)
3136                         return -EFAULT;
3137                 param += sizeof(struct floppy_raw_cmd);
3138                 if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length) {
3139                         if (ptr->length >= 0 &&
3140                             ptr->length <= ptr->buffer_length) {
3141                                 long length = ptr->buffer_length - ptr->length;
3142                                 ret = fd_copyout(ptr->data, ptr->kernel_data,
3143                                                  length);
3144                                 if (ret)
3145                                         return ret;
3146                         }
3147                 }
3148                 ptr = ptr->next;
3149         }
3150
3151         return 0;
3152 }
3153
3154 static void raw_cmd_free(struct floppy_raw_cmd **ptr)
3155 {
3156         struct floppy_raw_cmd *next;
3157         struct floppy_raw_cmd *this;
3158
3159         this = *ptr;
3160         *ptr = NULL;
3161         while (this) {
3162                 if (this->buffer_length) {
3163                         fd_dma_mem_free((unsigned long)this->kernel_data,
3164                                         this->buffer_length);
3165                         this->buffer_length = 0;
3166                 }
3167                 next = this->next;
3168                 kfree(this);
3169                 this = next;
3170         }
3171 }
3172
3173 static int raw_cmd_copyin(int cmd, void __user *param,
3174                                  struct floppy_raw_cmd **rcmd)
3175 {
3176         struct floppy_raw_cmd *ptr;
3177         int ret;
3178         int i;
3179
3180         *rcmd = NULL;
3181
3182 loop:
3183         ptr = kmalloc(sizeof(struct floppy_raw_cmd), GFP_KERNEL);
3184         if (!ptr)
3185                 return -ENOMEM;
3186         *rcmd = ptr;
3187         ret = copy_from_user(ptr, param, sizeof(*ptr));
3188         ptr->next = NULL;
3189         ptr->buffer_length = 0;
3190         ptr->kernel_data = NULL;
3191         if (ret)
3192                 return -EFAULT;
3193         param += sizeof(struct floppy_raw_cmd);
3194         if (ptr->cmd_count > FD_RAW_CMD_FULLSIZE)
3195                 return -EINVAL;
3196
3197         for (i = 0; i < FD_RAW_REPLY_SIZE; i++)
3198                 ptr->reply[i] = 0;
3199         ptr->resultcode = 0;
3200
3201         if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3202                 if (ptr->length <= 0)
3203                         return -EINVAL;
3204                 ptr->kernel_data = (char *)fd_dma_mem_alloc(ptr->length);
3205                 fallback_on_nodma_alloc(&ptr->kernel_data, ptr->length);
3206                 if (!ptr->kernel_data)
3207                         return -ENOMEM;
3208                 ptr->buffer_length = ptr->length;
3209         }
3210         if (ptr->flags & FD_RAW_WRITE) {
3211                 ret = fd_copyin(ptr->data, ptr->kernel_data, ptr->length);
3212                 if (ret)
3213                         return ret;
3214         }
3215
3216         if (ptr->flags & FD_RAW_MORE) {
3217                 rcmd = &(ptr->next);
3218                 ptr->rate &= 0x43;
3219                 goto loop;
3220         }
3221
3222         return 0;
3223 }
3224
3225 static int raw_cmd_ioctl(int cmd, void __user *param)
3226 {
3227         struct floppy_raw_cmd *my_raw_cmd;
3228         int drive;
3229         int ret2;
3230         int ret;
3231
3232         if (fdc_state[current_fdc].rawcmd <= 1)
3233                 fdc_state[current_fdc].rawcmd = 1;
3234         for (drive = 0; drive < N_DRIVE; drive++) {
3235                 if (FDC(drive) != current_fdc)
3236                         continue;
3237                 if (drive == current_drive) {
3238                         if (drive_state[drive].fd_ref > 1) {
3239                                 fdc_state[current_fdc].rawcmd = 2;
3240                                 break;
3241                         }
3242                 } else if (drive_state[drive].fd_ref) {
3243                         fdc_state[current_fdc].rawcmd = 2;
3244                         break;
3245                 }
3246         }
3247
3248         if (fdc_state[current_fdc].reset)
3249                 return -EIO;
3250
3251         ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
3252         if (ret) {
3253                 raw_cmd_free(&my_raw_cmd);
3254                 return ret;
3255         }
3256
3257         raw_cmd = my_raw_cmd;
3258         cont = &raw_cmd_cont;
3259         ret = wait_til_done(floppy_start, true);
3260         debug_dcl(drive_params[current_drive].flags,
3261                   "calling disk change from raw_cmd ioctl\n");
3262
3263         if (ret != -EINTR && fdc_state[current_fdc].reset)
3264                 ret = -EIO;
3265
3266         drive_state[current_drive].track = NO_TRACK;
3267
3268         ret2 = raw_cmd_copyout(cmd, param, my_raw_cmd);
3269         if (!ret)
3270                 ret = ret2;
3271         raw_cmd_free(&my_raw_cmd);
3272         return ret;
3273 }
3274
3275 static int invalidate_drive(struct block_device *bdev)
3276 {
3277         /* invalidate the buffer track to force a reread */
3278         set_bit((long)bdev->bd_disk->private_data, &fake_change);
3279         process_fd_request();
3280         if (bdev_check_media_change(bdev))
3281                 floppy_revalidate(bdev->bd_disk);
3282         return 0;
3283 }
3284
3285 static int set_geometry(unsigned int cmd, struct floppy_struct *g,
3286                                int drive, int type, struct block_device *bdev)
3287 {
3288         int cnt;
3289
3290         /* sanity checking for parameters. */
3291         if ((int)g->sect <= 0 ||
3292             (int)g->head <= 0 ||
3293             /* check for overflow in max_sector */
3294             (int)(g->sect * g->head) <= 0 ||
3295             /* check for zero in raw_cmd->cmd[F_SECT_PER_TRACK] */
3296             (unsigned char)((g->sect << 2) >> FD_SIZECODE(g)) == 0 ||
3297             g->track <= 0 || g->track > drive_params[drive].tracks >> STRETCH(g) ||
3298             /* check if reserved bits are set */
3299             (g->stretch & ~(FD_STRETCH | FD_SWAPSIDES | FD_SECTBASEMASK)) != 0)
3300                 return -EINVAL;
3301         if (type) {
3302                 if (!capable(CAP_SYS_ADMIN))
3303                         return -EPERM;
3304                 mutex_lock(&open_lock);
3305                 if (lock_fdc(drive)) {
3306                         mutex_unlock(&open_lock);
3307                         return -EINTR;
3308                 }
3309                 floppy_type[type] = *g;
3310                 floppy_type[type].name = "user format";
3311                 for (cnt = type << 2; cnt < (type << 2) + 4; cnt++)
3312                         floppy_sizes[cnt] = floppy_sizes[cnt + 0x80] =
3313                             floppy_type[type].size + 1;
3314                 process_fd_request();
3315                 for (cnt = 0; cnt < N_DRIVE; cnt++) {
3316                         struct block_device *bdev = opened_bdev[cnt];
3317                         if (!bdev || ITYPE(drive_state[cnt].fd_device) != type)
3318                                 continue;
3319                         __invalidate_device(bdev, true);
3320                 }
3321                 mutex_unlock(&open_lock);
3322         } else {
3323                 int oldStretch;
3324
3325                 if (lock_fdc(drive))
3326                         return -EINTR;
3327                 if (cmd != FDDEFPRM) {
3328                         /* notice a disk change immediately, else
3329                          * we lose our settings immediately*/
3330                         if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
3331                                 return -EINTR;
3332                 }
3333                 oldStretch = g->stretch;
3334                 user_params[drive] = *g;
3335                 if (buffer_drive == drive)
3336                         SUPBOUND(buffer_max, user_params[drive].sect);
3337                 current_type[drive] = &user_params[drive];
3338                 floppy_sizes[drive] = user_params[drive].size;
3339                 if (cmd == FDDEFPRM)
3340                         drive_state[current_drive].keep_data = -1;
3341                 else
3342                         drive_state[current_drive].keep_data = 1;
3343                 /* invalidation. Invalidate only when needed, i.e.
3344                  * when there are already sectors in the buffer cache
3345                  * whose number will change. This is useful, because
3346                  * mtools often changes the geometry of the disk after
3347                  * looking at the boot block */
3348                 if (drive_state[current_drive].maxblock > user_params[drive].sect ||
3349                     drive_state[current_drive].maxtrack ||
3350                     ((user_params[drive].sect ^ oldStretch) &
3351                      (FD_SWAPSIDES | FD_SECTBASEMASK)))
3352                         invalidate_drive(bdev);
3353                 else
3354                         process_fd_request();
3355         }
3356         return 0;
3357 }
3358
3359 /* handle obsolete ioctl's */
3360 static unsigned int ioctl_table[] = {
3361         FDCLRPRM,
3362         FDSETPRM,
3363         FDDEFPRM,
3364         FDGETPRM,
3365         FDMSGON,
3366         FDMSGOFF,
3367         FDFMTBEG,
3368         FDFMTTRK,
3369         FDFMTEND,
3370         FDSETEMSGTRESH,
3371         FDFLUSH,
3372         FDSETMAXERRS,
3373         FDGETMAXERRS,
3374         FDGETDRVTYP,
3375         FDSETDRVPRM,
3376         FDGETDRVPRM,
3377         FDGETDRVSTAT,
3378         FDPOLLDRVSTAT,
3379         FDRESET,
3380         FDGETFDCSTAT,
3381         FDWERRORCLR,
3382         FDWERRORGET,
3383         FDRAWCMD,
3384         FDEJECT,
3385         FDTWADDLE
3386 };
3387
3388 static int normalize_ioctl(unsigned int *cmd, int *size)
3389 {
3390         int i;
3391
3392         for (i = 0; i < ARRAY_SIZE(ioctl_table); i++) {
3393                 if ((*cmd & 0xffff) == (ioctl_table[i] & 0xffff)) {
3394                         *size = _IOC_SIZE(*cmd);
3395                         *cmd = ioctl_table[i];
3396                         if (*size > _IOC_SIZE(*cmd)) {
3397                                 pr_info("ioctl not yet supported\n");
3398                                 return -EFAULT;
3399                         }
3400                         return 0;
3401                 }
3402         }
3403         return -EINVAL;
3404 }
3405
3406 static int get_floppy_geometry(int drive, int type, struct floppy_struct **g)
3407 {
3408         if (type)
3409                 *g = &floppy_type[type];
3410         else {
3411                 if (lock_fdc(drive))
3412                         return -EINTR;
3413                 if (poll_drive(false, 0) == -EINTR)
3414                         return -EINTR;
3415                 process_fd_request();
3416                 *g = current_type[drive];
3417         }
3418         if (!*g)
3419                 return -ENODEV;
3420         return 0;
3421 }
3422
3423 static int fd_getgeo(struct block_device *bdev, struct hd_geometry *geo)
3424 {
3425         int drive = (long)bdev->bd_disk->private_data;
3426         int type = ITYPE(drive_state[drive].fd_device);
3427         struct floppy_struct *g;
3428         int ret;
3429
3430         ret = get_floppy_geometry(drive, type, &g);
3431         if (ret)
3432                 return ret;
3433
3434         geo->heads = g->head;
3435         geo->sectors = g->sect;
3436         geo->cylinders = g->track;
3437         return 0;
3438 }
3439
3440 static bool valid_floppy_drive_params(const short autodetect[FD_AUTODETECT_SIZE],
3441                 int native_format)
3442 {
3443         size_t floppy_type_size = ARRAY_SIZE(floppy_type);
3444         size_t i = 0;
3445
3446         for (i = 0; i < FD_AUTODETECT_SIZE; ++i) {
3447                 if (autodetect[i] < 0 ||
3448                     autodetect[i] >= floppy_type_size)
3449                         return false;
3450         }
3451
3452         if (native_format < 0 || native_format >= floppy_type_size)
3453                 return false;
3454
3455         return true;
3456 }
3457
3458 static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
3459                     unsigned long param)
3460 {
3461         int drive = (long)bdev->bd_disk->private_data;
3462         int type = ITYPE(drive_state[drive].fd_device);
3463         int i;
3464         int ret;
3465         int size;
3466         union inparam {
3467                 struct floppy_struct g; /* geometry */
3468                 struct format_descr f;
3469                 struct floppy_max_errors max_errors;
3470                 struct floppy_drive_params dp;
3471         } inparam;              /* parameters coming from user space */
3472         const void *outparam;   /* parameters passed back to user space */
3473
3474         /* convert compatibility eject ioctls into floppy eject ioctl.
3475          * We do this in order to provide a means to eject floppy disks before
3476          * installing the new fdutils package */
3477         if (cmd == CDROMEJECT ||        /* CD-ROM eject */
3478             cmd == 0x6470) {            /* SunOS floppy eject */
3479                 DPRINT("obsolete eject ioctl\n");
3480                 DPRINT("please use floppycontrol --eject\n");
3481                 cmd = FDEJECT;
3482         }
3483
3484         if (!((cmd & 0xff00) == 0x0200))
3485                 return -EINVAL;
3486
3487         /* convert the old style command into a new style command */
3488         ret = normalize_ioctl(&cmd, &size);
3489         if (ret)
3490                 return ret;
3491
3492         /* permission checks */
3493         if (((cmd & 0x40) && !(mode & (FMODE_WRITE | FMODE_WRITE_IOCTL))) ||
3494             ((cmd & 0x80) && !capable(CAP_SYS_ADMIN)))
3495                 return -EPERM;
3496
3497         if (WARN_ON(size < 0 || size > sizeof(inparam)))
3498                 return -EINVAL;
3499
3500         /* copyin */
3501         memset(&inparam, 0, sizeof(inparam));
3502         if (_IOC_DIR(cmd) & _IOC_WRITE) {
3503                 ret = fd_copyin((void __user *)param, &inparam, size);
3504                 if (ret)
3505                         return ret;
3506         }
3507
3508         switch (cmd) {
3509         case FDEJECT:
3510                 if (drive_state[drive].fd_ref != 1)
3511                         /* somebody else has this drive open */
3512                         return -EBUSY;
3513                 if (lock_fdc(drive))
3514                         return -EINTR;
3515
3516                 /* do the actual eject. Fails on
3517                  * non-Sparc architectures */
3518                 ret = fd_eject(UNIT(drive));
3519
3520                 set_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags);
3521                 set_bit(FD_VERIFY_BIT, &drive_state[drive].flags);
3522                 process_fd_request();
3523                 return ret;
3524         case FDCLRPRM:
3525                 if (lock_fdc(drive))
3526                         return -EINTR;
3527                 current_type[drive] = NULL;
3528                 floppy_sizes[drive] = MAX_DISK_SIZE << 1;
3529                 drive_state[drive].keep_data = 0;
3530                 return invalidate_drive(bdev);
3531         case FDSETPRM:
3532         case FDDEFPRM:
3533                 return set_geometry(cmd, &inparam.g, drive, type, bdev);
3534         case FDGETPRM:
3535                 ret = get_floppy_geometry(drive, type,
3536                                           (struct floppy_struct **)&outparam);
3537                 if (ret)
3538                         return ret;
3539                 memcpy(&inparam.g, outparam,
3540                                 offsetof(struct floppy_struct, name));
3541                 outparam = &inparam.g;
3542                 break;
3543         case FDMSGON:
3544                 drive_params[drive].flags |= FTD_MSG;
3545                 return 0;
3546         case FDMSGOFF:
3547                 drive_params[drive].flags &= ~FTD_MSG;
3548                 return 0;
3549         case FDFMTBEG:
3550                 if (lock_fdc(drive))
3551                         return -EINTR;
3552                 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
3553                         return -EINTR;
3554                 ret = drive_state[drive].flags;
3555                 process_fd_request();
3556                 if (ret & FD_VERIFY)
3557                         return -ENODEV;
3558                 if (!(ret & FD_DISK_WRITABLE))
3559                         return -EROFS;
3560                 return 0;
3561         case FDFMTTRK:
3562                 if (drive_state[drive].fd_ref != 1)
3563                         return -EBUSY;
3564                 return do_format(drive, &inparam.f);
3565         case FDFMTEND:
3566         case FDFLUSH:
3567                 if (lock_fdc(drive))
3568                         return -EINTR;
3569                 return invalidate_drive(bdev);
3570         case FDSETEMSGTRESH:
3571                 drive_params[drive].max_errors.reporting = (unsigned short)(param & 0x0f);
3572                 return 0;
3573         case FDGETMAXERRS:
3574                 outparam = &drive_params[drive].max_errors;
3575                 break;
3576         case FDSETMAXERRS:
3577                 drive_params[drive].max_errors = inparam.max_errors;
3578                 break;
3579         case FDGETDRVTYP:
3580                 outparam = drive_name(type, drive);
3581                 SUPBOUND(size, strlen((const char *)outparam) + 1);
3582                 break;
3583         case FDSETDRVPRM:
3584                 if (!valid_floppy_drive_params(inparam.dp.autodetect,
3585                                 inparam.dp.native_format))
3586                         return -EINVAL;
3587                 drive_params[drive] = inparam.dp;
3588                 break;
3589         case FDGETDRVPRM:
3590                 outparam = &drive_params[drive];
3591                 break;
3592         case FDPOLLDRVSTAT:
3593                 if (lock_fdc(drive))
3594                         return -EINTR;
3595                 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
3596                         return -EINTR;
3597                 process_fd_request();
3598                 fallthrough;
3599         case FDGETDRVSTAT:
3600                 outparam = &drive_state[drive];
3601                 break;
3602         case FDRESET:
3603                 return user_reset_fdc(drive, (int)param, true);
3604         case FDGETFDCSTAT:
3605                 outparam = &fdc_state[FDC(drive)];
3606                 break;
3607         case FDWERRORCLR:
3608                 memset(&write_errors[drive], 0, sizeof(write_errors[drive]));
3609                 return 0;
3610         case FDWERRORGET:
3611                 outparam = &write_errors[drive];
3612                 break;
3613         case FDRAWCMD:
3614                 if (type)
3615                         return -EINVAL;
3616                 if (lock_fdc(drive))
3617                         return -EINTR;
3618                 set_floppy(drive);
3619                 i = raw_cmd_ioctl(cmd, (void __user *)param);
3620                 if (i == -EINTR)
3621                         return -EINTR;
3622                 process_fd_request();
3623                 return i;
3624         case FDTWADDLE:
3625                 if (lock_fdc(drive))
3626                         return -EINTR;
3627                 twaddle(current_fdc, current_drive);
3628                 process_fd_request();
3629                 return 0;
3630         default:
3631                 return -EINVAL;
3632         }
3633
3634         if (_IOC_DIR(cmd) & _IOC_READ)
3635                 return fd_copyout((void __user *)param, outparam, size);
3636
3637         return 0;
3638 }
3639
3640 static int fd_ioctl(struct block_device *bdev, fmode_t mode,
3641                              unsigned int cmd, unsigned long param)
3642 {
3643         int ret;
3644
3645         mutex_lock(&floppy_mutex);
3646         ret = fd_locked_ioctl(bdev, mode, cmd, param);
3647         mutex_unlock(&floppy_mutex);
3648
3649         return ret;
3650 }
3651
3652 #ifdef CONFIG_COMPAT
3653
3654 struct compat_floppy_drive_params {
3655         char            cmos;
3656         compat_ulong_t  max_dtr;
3657         compat_ulong_t  hlt;
3658         compat_ulong_t  hut;
3659         compat_ulong_t  srt;
3660         compat_ulong_t  spinup;
3661         compat_ulong_t  spindown;
3662         unsigned char   spindown_offset;
3663         unsigned char   select_delay;
3664         unsigned char   rps;
3665         unsigned char   tracks;
3666         compat_ulong_t  timeout;
3667         unsigned char   interleave_sect;
3668         struct floppy_max_errors max_errors;
3669         char            flags;
3670         char            read_track;
3671         short           autodetect[FD_AUTODETECT_SIZE];
3672         compat_int_t    checkfreq;
3673         compat_int_t    native_format;
3674 };
3675
3676 struct compat_floppy_drive_struct {
3677         signed char     flags;
3678         compat_ulong_t  spinup_date;
3679         compat_ulong_t  select_date;
3680         compat_ulong_t  first_read_date;
3681         short           probed_format;
3682         short           track;
3683         short           maxblock;
3684         short           maxtrack;
3685         compat_int_t    generation;
3686         compat_int_t    keep_data;
3687         compat_int_t    fd_ref;
3688         compat_int_t    fd_device;
3689         compat_int_t    last_checked;
3690         compat_caddr_t dmabuf;
3691         compat_int_t    bufblocks;
3692 };
3693
3694 struct compat_floppy_fdc_state {
3695         compat_int_t    spec1;
3696         compat_int_t    spec2;
3697         compat_int_t    dtr;
3698         unsigned char   version;
3699         unsigned char   dor;
3700         compat_ulong_t  address;
3701         unsigned int    rawcmd:2;
3702         unsigned int    reset:1;
3703         unsigned int    need_configure:1;
3704         unsigned int    perp_mode:2;
3705         unsigned int    has_fifo:1;
3706         unsigned int    driver_version;
3707         unsigned char   track[4];
3708 };
3709
3710 struct compat_floppy_write_errors {
3711         unsigned int    write_errors;
3712         compat_ulong_t  first_error_sector;
3713         compat_int_t    first_error_generation;
3714         compat_ulong_t  last_error_sector;
3715         compat_int_t    last_error_generation;
3716         compat_uint_t   badness;
3717 };
3718
3719 #define FDSETPRM32 _IOW(2, 0x42, struct compat_floppy_struct)
3720 #define FDDEFPRM32 _IOW(2, 0x43, struct compat_floppy_struct)
3721 #define FDSETDRVPRM32 _IOW(2, 0x90, struct compat_floppy_drive_params)
3722 #define FDGETDRVPRM32 _IOR(2, 0x11, struct compat_floppy_drive_params)
3723 #define FDGETDRVSTAT32 _IOR(2, 0x12, struct compat_floppy_drive_struct)
3724 #define FDPOLLDRVSTAT32 _IOR(2, 0x13, struct compat_floppy_drive_struct)
3725 #define FDGETFDCSTAT32 _IOR(2, 0x15, struct compat_floppy_fdc_state)
3726 #define FDWERRORGET32  _IOR(2, 0x17, struct compat_floppy_write_errors)
3727
3728 static int compat_set_geometry(struct block_device *bdev, fmode_t mode, unsigned int cmd,
3729                     struct compat_floppy_struct __user *arg)
3730 {
3731         struct floppy_struct v;
3732         int drive, type;
3733         int err;
3734
3735         BUILD_BUG_ON(offsetof(struct floppy_struct, name) !=
3736                      offsetof(struct compat_floppy_struct, name));
3737
3738         if (!(mode & (FMODE_WRITE | FMODE_WRITE_IOCTL)))
3739                 return -EPERM;
3740
3741         memset(&v, 0, sizeof(struct floppy_struct));
3742         if (copy_from_user(&v, arg, offsetof(struct floppy_struct, name)))
3743                 return -EFAULT;
3744
3745         mutex_lock(&floppy_mutex);
3746         drive = (long)bdev->bd_disk->private_data;
3747         type = ITYPE(drive_state[drive].fd_device);
3748         err = set_geometry(cmd == FDSETPRM32 ? FDSETPRM : FDDEFPRM,
3749                         &v, drive, type, bdev);
3750         mutex_unlock(&floppy_mutex);
3751         return err;
3752 }
3753
3754 static int compat_get_prm(int drive,
3755                           struct compat_floppy_struct __user *arg)
3756 {
3757         struct compat_floppy_struct v;
3758         struct floppy_struct *p;
3759         int err;
3760
3761         memset(&v, 0, sizeof(v));
3762         mutex_lock(&floppy_mutex);
3763         err = get_floppy_geometry(drive, ITYPE(drive_state[drive].fd_device),
3764                                   &p);
3765         if (err) {
3766                 mutex_unlock(&floppy_mutex);
3767                 return err;
3768         }
3769         memcpy(&v, p, offsetof(struct floppy_struct, name));
3770         mutex_unlock(&floppy_mutex);
3771         if (copy_to_user(arg, &v, sizeof(struct compat_floppy_struct)))
3772                 return -EFAULT;
3773         return 0;
3774 }
3775
3776 static int compat_setdrvprm(int drive,
3777                             struct compat_floppy_drive_params __user *arg)
3778 {
3779         struct compat_floppy_drive_params v;
3780
3781         if (!capable(CAP_SYS_ADMIN))
3782                 return -EPERM;
3783         if (copy_from_user(&v, arg, sizeof(struct compat_floppy_drive_params)))
3784                 return -EFAULT;
3785         if (!valid_floppy_drive_params(v.autodetect, v.native_format))
3786                 return -EINVAL;
3787         mutex_lock(&floppy_mutex);
3788         drive_params[drive].cmos = v.cmos;
3789         drive_params[drive].max_dtr = v.max_dtr;
3790         drive_params[drive].hlt = v.hlt;
3791         drive_params[drive].hut = v.hut;
3792         drive_params[drive].srt = v.srt;
3793         drive_params[drive].spinup = v.spinup;
3794         drive_params[drive].spindown = v.spindown;
3795         drive_params[drive].spindown_offset = v.spindown_offset;
3796         drive_params[drive].select_delay = v.select_delay;
3797         drive_params[drive].rps = v.rps;
3798         drive_params[drive].tracks = v.tracks;
3799         drive_params[drive].timeout = v.timeout;
3800         drive_params[drive].interleave_sect = v.interleave_sect;
3801         drive_params[drive].max_errors = v.max_errors;
3802         drive_params[drive].flags = v.flags;
3803         drive_params[drive].read_track = v.read_track;
3804         memcpy(drive_params[drive].autodetect, v.autodetect,
3805                sizeof(v.autodetect));
3806         drive_params[drive].checkfreq = v.checkfreq;
3807         drive_params[drive].native_format = v.native_format;
3808         mutex_unlock(&floppy_mutex);
3809         return 0;
3810 }
3811
3812 static int compat_getdrvprm(int drive,
3813                             struct compat_floppy_drive_params __user *arg)
3814 {
3815         struct compat_floppy_drive_params v;
3816
3817         memset(&v, 0, sizeof(struct compat_floppy_drive_params));
3818         mutex_lock(&floppy_mutex);
3819         v.cmos = drive_params[drive].cmos;
3820         v.max_dtr = drive_params[drive].max_dtr;
3821         v.hlt = drive_params[drive].hlt;
3822         v.hut = drive_params[drive].hut;
3823         v.srt = drive_params[drive].srt;
3824         v.spinup = drive_params[drive].spinup;
3825         v.spindown = drive_params[drive].spindown;
3826         v.spindown_offset = drive_params[drive].spindown_offset;
3827         v.select_delay = drive_params[drive].select_delay;
3828         v.rps = drive_params[drive].rps;
3829         v.tracks = drive_params[drive].tracks;
3830         v.timeout = drive_params[drive].timeout;
3831         v.interleave_sect = drive_params[drive].interleave_sect;
3832         v.max_errors = drive_params[drive].max_errors;
3833         v.flags = drive_params[drive].flags;
3834         v.read_track = drive_params[drive].read_track;
3835         memcpy(v.autodetect, drive_params[drive].autodetect,
3836                sizeof(v.autodetect));
3837         v.checkfreq = drive_params[drive].checkfreq;
3838         v.native_format = drive_params[drive].native_format;
3839         mutex_unlock(&floppy_mutex);
3840
3841         if (copy_to_user(arg, &v, sizeof(struct compat_floppy_drive_params)))
3842                 return -EFAULT;
3843         return 0;
3844 }
3845
3846 static int compat_getdrvstat(int drive, bool poll,
3847                             struct compat_floppy_drive_struct __user *arg)
3848 {
3849         struct compat_floppy_drive_struct v;
3850
3851         memset(&v, 0, sizeof(struct compat_floppy_drive_struct));
3852         mutex_lock(&floppy_mutex);
3853
3854         if (poll) {
3855                 if (lock_fdc(drive))
3856                         goto Eintr;
3857                 if (poll_drive(true, FD_RAW_NEED_DISK) == -EINTR)
3858                         goto Eintr;
3859                 process_fd_request();
3860         }
3861         v.spinup_date = drive_state[drive].spinup_date;
3862         v.select_date = drive_state[drive].select_date;
3863         v.first_read_date = drive_state[drive].first_read_date;
3864         v.probed_format = drive_state[drive].probed_format;
3865         v.track = drive_state[drive].track;
3866         v.maxblock = drive_state[drive].maxblock;
3867         v.maxtrack = drive_state[drive].maxtrack;
3868         v.generation = drive_state[drive].generation;
3869         v.keep_data = drive_state[drive].keep_data;
3870         v.fd_ref = drive_state[drive].fd_ref;
3871         v.fd_device = drive_state[drive].fd_device;
3872         v.last_checked = drive_state[drive].last_checked;
3873         v.dmabuf = (uintptr_t) drive_state[drive].dmabuf;
3874         v.bufblocks = drive_state[drive].bufblocks;
3875         mutex_unlock(&floppy_mutex);
3876
3877         if (copy_to_user(arg, &v, sizeof(struct compat_floppy_drive_struct)))
3878                 return -EFAULT;
3879         return 0;
3880 Eintr:
3881         mutex_unlock(&floppy_mutex);
3882         return -EINTR;
3883 }
3884
3885 static int compat_getfdcstat(int drive,
3886                             struct compat_floppy_fdc_state __user *arg)
3887 {
3888         struct compat_floppy_fdc_state v32;
3889         struct floppy_fdc_state v;
3890
3891         mutex_lock(&floppy_mutex);
3892         v = fdc_state[FDC(drive)];
3893         mutex_unlock(&floppy_mutex);
3894
3895         memset(&v32, 0, sizeof(struct compat_floppy_fdc_state));
3896         v32.spec1 = v.spec1;
3897         v32.spec2 = v.spec2;
3898         v32.dtr = v.dtr;
3899         v32.version = v.version;
3900         v32.dor = v.dor;
3901         v32.address = v.address;
3902         v32.rawcmd = v.rawcmd;
3903         v32.reset = v.reset;
3904         v32.need_configure = v.need_configure;
3905         v32.perp_mode = v.perp_mode;
3906         v32.has_fifo = v.has_fifo;
3907         v32.driver_version = v.driver_version;
3908         memcpy(v32.track, v.track, 4);
3909         if (copy_to_user(arg, &v32, sizeof(struct compat_floppy_fdc_state)))
3910                 return -EFAULT;
3911         return 0;
3912 }
3913
3914 static int compat_werrorget(int drive,
3915                             struct compat_floppy_write_errors __user *arg)
3916 {
3917         struct compat_floppy_write_errors v32;
3918         struct floppy_write_errors v;
3919
3920         memset(&v32, 0, sizeof(struct compat_floppy_write_errors));
3921         mutex_lock(&floppy_mutex);
3922         v = write_errors[drive];
3923         mutex_unlock(&floppy_mutex);
3924         v32.write_errors = v.write_errors;
3925         v32.first_error_sector = v.first_error_sector;
3926         v32.first_error_generation = v.first_error_generation;
3927         v32.last_error_sector = v.last_error_sector;
3928         v32.last_error_generation = v.last_error_generation;
3929         v32.badness = v.badness;
3930         if (copy_to_user(arg, &v32, sizeof(struct compat_floppy_write_errors)))
3931                 return -EFAULT;
3932         return 0;
3933 }
3934
3935 static int fd_compat_ioctl(struct block_device *bdev, fmode_t mode, unsigned int cmd,
3936                     unsigned long param)
3937 {
3938         int drive = (long)bdev->bd_disk->private_data;
3939         switch (cmd) {
3940         case CDROMEJECT: /* CD-ROM eject */
3941         case 0x6470:     /* SunOS floppy eject */
3942
3943         case FDMSGON:
3944         case FDMSGOFF:
3945         case FDSETEMSGTRESH:
3946         case FDFLUSH:
3947         case FDWERRORCLR:
3948         case FDEJECT:
3949         case FDCLRPRM:
3950         case FDFMTBEG:
3951         case FDRESET:
3952         case FDTWADDLE:
3953                 return fd_ioctl(bdev, mode, cmd, param);
3954         case FDSETMAXERRS:
3955         case FDGETMAXERRS:
3956         case FDGETDRVTYP:
3957         case FDFMTEND:
3958         case FDFMTTRK:
3959         case FDRAWCMD:
3960                 return fd_ioctl(bdev, mode, cmd,
3961                                 (unsigned long)compat_ptr(param));
3962         case FDSETPRM32:
3963         case FDDEFPRM32:
3964                 return compat_set_geometry(bdev, mode, cmd, compat_ptr(param));
3965         case FDGETPRM32:
3966                 return compat_get_prm(drive, compat_ptr(param));
3967         case FDSETDRVPRM32:
3968                 return compat_setdrvprm(drive, compat_ptr(param));
3969         case FDGETDRVPRM32:
3970                 return compat_getdrvprm(drive, compat_ptr(param));
3971         case FDPOLLDRVSTAT32:
3972                 return compat_getdrvstat(drive, true, compat_ptr(param));
3973         case FDGETDRVSTAT32:
3974                 return compat_getdrvstat(drive, false, compat_ptr(param));
3975         case FDGETFDCSTAT32:
3976                 return compat_getfdcstat(drive, compat_ptr(param));
3977         case FDWERRORGET32:
3978                 return compat_werrorget(drive, compat_ptr(param));
3979         }
3980         return -EINVAL;
3981 }
3982 #endif
3983
3984 static void __init config_types(void)
3985 {
3986         bool has_drive = false;
3987         int drive;
3988
3989         /* read drive info out of physical CMOS */
3990         drive = 0;
3991         if (!drive_params[drive].cmos)
3992                 drive_params[drive].cmos = FLOPPY0_TYPE;
3993         drive = 1;
3994         if (!drive_params[drive].cmos)
3995                 drive_params[drive].cmos = FLOPPY1_TYPE;
3996
3997         /* FIXME: additional physical CMOS drive detection should go here */
3998
3999         for (drive = 0; drive < N_DRIVE; drive++) {
4000                 unsigned int type = drive_params[drive].cmos;
4001                 struct floppy_drive_params *params;
4002                 const char *name = NULL;
4003                 char temparea[32];
4004
4005                 if (type < ARRAY_SIZE(default_drive_params)) {
4006                         params = &default_drive_params[type].params;
4007                         if (type) {
4008                                 name = default_drive_params[type].name;
4009                                 allowed_drive_mask |= 1 << drive;
4010                         } else
4011                                 allowed_drive_mask &= ~(1 << drive);
4012                 } else {
4013                         params = &default_drive_params[0].params;
4014                         snprintf(temparea, sizeof(temparea),
4015                                  "unknown type %d (usb?)", type);
4016                         name = temparea;
4017                 }
4018                 if (name) {
4019                         const char *prepend;
4020                         if (!has_drive) {
4021                                 prepend = "";
4022                                 has_drive = true;
4023                                 pr_info("Floppy drive(s):");
4024                         } else {
4025                                 prepend = ",";
4026                         }
4027
4028                         pr_cont("%s fd%d is %s", prepend, drive, name);
4029                 }
4030                 drive_params[drive] = *params;
4031         }
4032
4033         if (has_drive)
4034                 pr_cont("\n");
4035 }
4036
4037 static void floppy_release(struct gendisk *disk, fmode_t mode)
4038 {
4039         int drive = (long)disk->private_data;
4040
4041         mutex_lock(&floppy_mutex);
4042         mutex_lock(&open_lock);
4043         if (!drive_state[drive].fd_ref--) {
4044                 DPRINT("floppy_release with fd_ref == 0");
4045                 drive_state[drive].fd_ref = 0;
4046         }
4047         if (!drive_state[drive].fd_ref)
4048                 opened_bdev[drive] = NULL;
4049         mutex_unlock(&open_lock);
4050         mutex_unlock(&floppy_mutex);
4051 }
4052
4053 /*
4054  * floppy_open check for aliasing (/dev/fd0 can be the same as
4055  * /dev/PS0 etc), and disallows simultaneous access to the same
4056  * drive with different device numbers.
4057  */
4058 static int floppy_open(struct block_device *bdev, fmode_t mode)
4059 {
4060         int drive = (long)bdev->bd_disk->private_data;
4061         int old_dev, new_dev;
4062         int try;
4063         int res = -EBUSY;
4064         char *tmp;
4065
4066         mutex_lock(&floppy_mutex);
4067         mutex_lock(&open_lock);
4068         old_dev = drive_state[drive].fd_device;
4069         if (opened_bdev[drive] && opened_bdev[drive] != bdev)
4070                 goto out2;
4071
4072         if (!drive_state[drive].fd_ref && (drive_params[drive].flags & FD_BROKEN_DCL)) {
4073                 set_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags);
4074                 set_bit(FD_VERIFY_BIT, &drive_state[drive].flags);
4075         }
4076
4077         drive_state[drive].fd_ref++;
4078
4079         opened_bdev[drive] = bdev;
4080
4081         res = -ENXIO;
4082
4083         if (!floppy_track_buffer) {
4084                 /* if opening an ED drive, reserve a big buffer,
4085                  * else reserve a small one */
4086                 if ((drive_params[drive].cmos == 6) || (drive_params[drive].cmos == 5))
4087                         try = 64;       /* Only 48 actually useful */
4088                 else
4089                         try = 32;       /* Only 24 actually useful */
4090
4091                 tmp = (char *)fd_dma_mem_alloc(1024 * try);
4092                 if (!tmp && !floppy_track_buffer) {
4093                         try >>= 1;      /* buffer only one side */
4094                         INFBOUND(try, 16);
4095                         tmp = (char *)fd_dma_mem_alloc(1024 * try);
4096                 }
4097                 if (!tmp && !floppy_track_buffer)
4098                         fallback_on_nodma_alloc(&tmp, 2048 * try);
4099                 if (!tmp && !floppy_track_buffer) {
4100                         DPRINT("Unable to allocate DMA memory\n");
4101                         goto out;
4102                 }
4103                 if (floppy_track_buffer) {
4104                         if (tmp)
4105                                 fd_dma_mem_free((unsigned long)tmp, try * 1024);
4106                 } else {
4107                         buffer_min = buffer_max = -1;
4108                         floppy_track_buffer = tmp;
4109                         max_buffer_sectors = try;
4110                 }
4111         }
4112
4113         new_dev = MINOR(bdev->bd_dev);
4114         drive_state[drive].fd_device = new_dev;
4115         set_capacity(disks[drive][ITYPE(new_dev)], floppy_sizes[new_dev]);
4116         if (old_dev != -1 && old_dev != new_dev) {
4117                 if (buffer_drive == drive)
4118                         buffer_track = -1;
4119         }
4120
4121         if (fdc_state[FDC(drive)].rawcmd == 1)
4122                 fdc_state[FDC(drive)].rawcmd = 2;
4123
4124         if (!(mode & FMODE_NDELAY)) {
4125                 if (mode & (FMODE_READ|FMODE_WRITE)) {
4126                         drive_state[drive].last_checked = 0;
4127                         clear_bit(FD_OPEN_SHOULD_FAIL_BIT,
4128                                   &drive_state[drive].flags);
4129                         if (bdev_check_media_change(bdev))
4130                                 floppy_revalidate(bdev->bd_disk);
4131                         if (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags))
4132                                 goto out;
4133                         if (test_bit(FD_OPEN_SHOULD_FAIL_BIT, &drive_state[drive].flags))
4134                                 goto out;
4135                 }
4136                 res = -EROFS;
4137                 if ((mode & FMODE_WRITE) &&
4138                     !test_bit(FD_DISK_WRITABLE_BIT, &drive_state[drive].flags))
4139                         goto out;
4140         }
4141         mutex_unlock(&open_lock);
4142         mutex_unlock(&floppy_mutex);
4143         return 0;
4144 out:
4145         drive_state[drive].fd_ref--;
4146
4147         if (!drive_state[drive].fd_ref)
4148                 opened_bdev[drive] = NULL;
4149 out2:
4150         mutex_unlock(&open_lock);
4151         mutex_unlock(&floppy_mutex);
4152         return res;
4153 }
4154
4155 /*
4156  * Check if the disk has been changed or if a change has been faked.
4157  */
4158 static unsigned int floppy_check_events(struct gendisk *disk,
4159                                         unsigned int clearing)
4160 {
4161         int drive = (long)disk->private_data;
4162
4163         if (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags) ||
4164             test_bit(FD_VERIFY_BIT, &drive_state[drive].flags))
4165                 return DISK_EVENT_MEDIA_CHANGE;
4166
4167         if (time_after(jiffies, drive_state[drive].last_checked + drive_params[drive].checkfreq)) {
4168                 if (lock_fdc(drive))
4169                         return 0;
4170                 poll_drive(false, 0);
4171                 process_fd_request();
4172         }
4173
4174         if (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags) ||
4175             test_bit(FD_VERIFY_BIT, &drive_state[drive].flags) ||
4176             test_bit(drive, &fake_change) ||
4177             drive_no_geom(drive))
4178                 return DISK_EVENT_MEDIA_CHANGE;
4179         return 0;
4180 }
4181
4182 /*
4183  * This implements "read block 0" for floppy_revalidate().
4184  * Needed for format autodetection, checking whether there is
4185  * a disk in the drive, and whether that disk is writable.
4186  */
4187
4188 struct rb0_cbdata {
4189         int drive;
4190         struct completion complete;
4191 };
4192
4193 static void floppy_rb0_cb(struct bio *bio)
4194 {
4195         struct rb0_cbdata *cbdata = (struct rb0_cbdata *)bio->bi_private;
4196         int drive = cbdata->drive;
4197
4198         if (bio->bi_status) {
4199                 pr_info("floppy: error %d while reading block 0\n",
4200                         bio->bi_status);
4201                 set_bit(FD_OPEN_SHOULD_FAIL_BIT, &drive_state[drive].flags);
4202         }
4203         complete(&cbdata->complete);
4204 }
4205
4206 static int __floppy_read_block_0(struct block_device *bdev, int drive)
4207 {
4208         struct bio bio;
4209         struct bio_vec bio_vec;
4210         struct page *page;
4211         struct rb0_cbdata cbdata;
4212
4213         page = alloc_page(GFP_NOIO);
4214         if (!page) {
4215                 process_fd_request();
4216                 return -ENOMEM;
4217         }
4218
4219         cbdata.drive = drive;
4220
4221         bio_init(&bio, &bio_vec, 1);
4222         bio_set_dev(&bio, bdev);
4223         bio_add_page(&bio, page, block_size(bdev), 0);
4224
4225         bio.bi_iter.bi_sector = 0;
4226         bio.bi_flags |= (1 << BIO_QUIET);
4227         bio.bi_private = &cbdata;
4228         bio.bi_end_io = floppy_rb0_cb;
4229         bio_set_op_attrs(&bio, REQ_OP_READ, 0);
4230
4231         init_completion(&cbdata.complete);
4232
4233         submit_bio(&bio);
4234         process_fd_request();
4235
4236         wait_for_completion(&cbdata.complete);
4237
4238         __free_page(page);
4239
4240         return 0;
4241 }
4242
4243 /* revalidate the floppy disk, i.e. trigger format autodetection by reading
4244  * the bootblock (block 0). "Autodetection" is also needed to check whether
4245  * there is a disk in the drive at all... Thus we also do it for fixed
4246  * geometry formats */
4247 static int floppy_revalidate(struct gendisk *disk)
4248 {
4249         int drive = (long)disk->private_data;
4250         int cf;
4251         int res = 0;
4252
4253         if (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags) ||
4254             test_bit(FD_VERIFY_BIT, &drive_state[drive].flags) ||
4255             test_bit(drive, &fake_change) ||
4256             drive_no_geom(drive)) {
4257                 if (WARN(atomic_read(&usage_count) == 0,
4258                          "VFS: revalidate called on non-open device.\n"))
4259                         return -EFAULT;
4260
4261                 res = lock_fdc(drive);
4262                 if (res)
4263                         return res;
4264                 cf = (test_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags) ||
4265                       test_bit(FD_VERIFY_BIT, &drive_state[drive].flags));
4266                 if (!(cf || test_bit(drive, &fake_change) || drive_no_geom(drive))) {
4267                         process_fd_request();   /*already done by another thread */
4268                         return 0;
4269                 }
4270                 drive_state[drive].maxblock = 0;
4271                 drive_state[drive].maxtrack = 0;
4272                 if (buffer_drive == drive)
4273                         buffer_track = -1;
4274                 clear_bit(drive, &fake_change);
4275                 clear_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags);
4276                 if (cf)
4277                         drive_state[drive].generation++;
4278                 if (drive_no_geom(drive)) {
4279                         /* auto-sensing */
4280                         res = __floppy_read_block_0(opened_bdev[drive], drive);
4281                 } else {
4282                         if (cf)
4283                                 poll_drive(false, FD_RAW_NEED_DISK);
4284                         process_fd_request();
4285                 }
4286         }
4287         set_capacity(disk, floppy_sizes[drive_state[drive].fd_device]);
4288         return res;
4289 }
4290
4291 static const struct block_device_operations floppy_fops = {
4292         .owner                  = THIS_MODULE,
4293         .open                   = floppy_open,
4294         .release                = floppy_release,
4295         .ioctl                  = fd_ioctl,
4296         .getgeo                 = fd_getgeo,
4297         .check_events           = floppy_check_events,
4298 #ifdef CONFIG_COMPAT
4299         .compat_ioctl           = fd_compat_ioctl,
4300 #endif
4301 };
4302
4303 /*
4304  * Floppy Driver initialization
4305  * =============================
4306  */
4307
4308 /* Determine the floppy disk controller type */
4309 /* This routine was written by David C. Niemi */
4310 static char __init get_fdc_version(int fdc)
4311 {
4312         int r;
4313
4314         output_byte(fdc, FD_DUMPREGS);  /* 82072 and better know DUMPREGS */
4315         if (fdc_state[fdc].reset)
4316                 return FDC_NONE;
4317         r = result(fdc);
4318         if (r <= 0x00)
4319                 return FDC_NONE;        /* No FDC present ??? */
4320         if ((r == 1) && (reply_buffer[0] == 0x80)) {
4321                 pr_info("FDC %d is an 8272A\n", fdc);
4322                 return FDC_8272A;       /* 8272a/765 don't know DUMPREGS */
4323         }
4324         if (r != 10) {
4325                 pr_info("FDC %d init: DUMPREGS: unexpected return of %d bytes.\n",
4326                         fdc, r);
4327                 return FDC_UNKNOWN;
4328         }
4329
4330         if (!fdc_configure(fdc)) {
4331                 pr_info("FDC %d is an 82072\n", fdc);
4332                 return FDC_82072;       /* 82072 doesn't know CONFIGURE */
4333         }
4334
4335         output_byte(fdc, FD_PERPENDICULAR);
4336         if (need_more_output(fdc) == MORE_OUTPUT) {
4337                 output_byte(fdc, 0);
4338         } else {
4339                 pr_info("FDC %d is an 82072A\n", fdc);
4340                 return FDC_82072A;      /* 82072A as found on Sparcs. */
4341         }
4342
4343         output_byte(fdc, FD_UNLOCK);
4344         r = result(fdc);
4345         if ((r == 1) && (reply_buffer[0] == 0x80)) {
4346                 pr_info("FDC %d is a pre-1991 82077\n", fdc);
4347                 return FDC_82077_ORIG;  /* Pre-1991 82077, doesn't know
4348                                          * LOCK/UNLOCK */
4349         }
4350         if ((r != 1) || (reply_buffer[0] != 0x00)) {
4351                 pr_info("FDC %d init: UNLOCK: unexpected return of %d bytes.\n",
4352                         fdc, r);
4353                 return FDC_UNKNOWN;
4354         }
4355         output_byte(fdc, FD_PARTID);
4356         r = result(fdc);
4357         if (r != 1) {
4358                 pr_info("FDC %d init: PARTID: unexpected return of %d bytes.\n",
4359                         fdc, r);
4360                 return FDC_UNKNOWN;
4361         }
4362         if (reply_buffer[0] == 0x80) {
4363                 pr_info("FDC %d is a post-1991 82077\n", fdc);
4364                 return FDC_82077;       /* Revised 82077AA passes all the tests */
4365         }
4366         switch (reply_buffer[0] >> 5) {
4367         case 0x0:
4368                 /* Either a 82078-1 or a 82078SL running at 5Volt */
4369                 pr_info("FDC %d is an 82078.\n", fdc);
4370                 return FDC_82078;
4371         case 0x1:
4372                 pr_info("FDC %d is a 44pin 82078\n", fdc);
4373                 return FDC_82078;
4374         case 0x2:
4375                 pr_info("FDC %d is a S82078B\n", fdc);
4376                 return FDC_S82078B;
4377         case 0x3:
4378                 pr_info("FDC %d is a National Semiconductor PC87306\n", fdc);
4379                 return FDC_87306;
4380         default:
4381                 pr_info("FDC %d init: 82078 variant with unknown PARTID=%d.\n",
4382                         fdc, reply_buffer[0] >> 5);
4383                 return FDC_82078_UNKN;
4384         }
4385 }                               /* get_fdc_version */
4386
4387 /* lilo configuration */
4388
4389 static void __init floppy_set_flags(int *ints, int param, int param2)
4390 {
4391         int i;
4392
4393         for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
4394                 if (param)
4395                         default_drive_params[i].params.flags |= param2;
4396                 else
4397                         default_drive_params[i].params.flags &= ~param2;
4398         }
4399         DPRINT("%s flag 0x%x\n", param2 ? "Setting" : "Clearing", param);
4400 }
4401
4402 static void __init daring(int *ints, int param, int param2)
4403 {
4404         int i;
4405
4406         for (i = 0; i < ARRAY_SIZE(default_drive_params); i++) {
4407                 if (param) {
4408                         default_drive_params[i].params.select_delay = 0;
4409                         default_drive_params[i].params.flags |=
4410                             FD_SILENT_DCL_CLEAR;
4411                 } else {
4412                         default_drive_params[i].params.select_delay =
4413                             2 * HZ / 100;
4414                         default_drive_params[i].params.flags &=
4415                             ~FD_SILENT_DCL_CLEAR;
4416                 }
4417         }
4418         DPRINT("Assuming %s floppy hardware\n", param ? "standard" : "broken");
4419 }
4420
4421 static void __init set_cmos(int *ints, int dummy, int dummy2)
4422 {
4423         int current_drive = 0;
4424
4425         if (ints[0] != 2) {
4426                 DPRINT("wrong number of parameters for CMOS\n");
4427                 return;
4428         }
4429         current_drive = ints[1];
4430         if (current_drive < 0 || current_drive >= 8) {
4431                 DPRINT("bad drive for set_cmos\n");
4432                 return;
4433         }
4434 #if N_FDC > 1
4435         if (current_drive >= 4 && !FDC2)
4436                 FDC2 = 0x370;
4437 #endif
4438         drive_params[current_drive].cmos = ints[2];
4439         DPRINT("setting CMOS code to %d\n", ints[2]);
4440 }
4441
4442 static struct param_table {
4443         const char *name;
4444         void (*fn) (int *ints, int param, int param2);
4445         int *var;
4446         int def_param;
4447         int param2;
4448 } config_params[] __initdata = {
4449         {"allowed_drive_mask", NULL, &allowed_drive_mask, 0xff, 0}, /* obsolete */
4450         {"all_drives", NULL, &allowed_drive_mask, 0xff, 0},     /* obsolete */
4451         {"asus_pci", NULL, &allowed_drive_mask, 0x33, 0},
4452         {"irq", NULL, &FLOPPY_IRQ, 6, 0},
4453         {"dma", NULL, &FLOPPY_DMA, 2, 0},
4454         {"daring", daring, NULL, 1, 0},
4455 #if N_FDC > 1
4456         {"two_fdc", NULL, &FDC2, 0x370, 0},
4457         {"one_fdc", NULL, &FDC2, 0, 0},
4458 #endif
4459         {"thinkpad", floppy_set_flags, NULL, 1, FD_INVERTED_DCL},
4460         {"broken_dcl", floppy_set_flags, NULL, 1, FD_BROKEN_DCL},
4461         {"messages", floppy_set_flags, NULL, 1, FTD_MSG},
4462         {"silent_dcl_clear", floppy_set_flags, NULL, 1, FD_SILENT_DCL_CLEAR},
4463         {"debug", floppy_set_flags, NULL, 1, FD_DEBUG},
4464         {"nodma", NULL, &can_use_virtual_dma, 1, 0},
4465         {"omnibook", NULL, &can_use_virtual_dma, 1, 0},
4466         {"yesdma", NULL, &can_use_virtual_dma, 0, 0},
4467         {"fifo_depth", NULL, &fifo_depth, 0xa, 0},
4468         {"nofifo", NULL, &no_fifo, 0x20, 0},
4469         {"usefifo", NULL, &no_fifo, 0, 0},
4470         {"cmos", set_cmos, NULL, 0, 0},
4471         {"slow", NULL, &slow_floppy, 1, 0},
4472         {"unexpected_interrupts", NULL, &print_unex, 1, 0},
4473         {"no_unexpected_interrupts", NULL, &print_unex, 0, 0},
4474         {"L40SX", NULL, &print_unex, 0, 0}
4475
4476         EXTRA_FLOPPY_PARAMS
4477 };
4478
4479 static int __init floppy_setup(char *str)
4480 {
4481         int i;
4482         int param;
4483         int ints[11];
4484
4485         str = get_options(str, ARRAY_SIZE(ints), ints);
4486         if (str) {
4487                 for (i = 0; i < ARRAY_SIZE(config_params); i++) {
4488                         if (strcmp(str, config_params[i].name) == 0) {
4489                                 if (ints[0])
4490                                         param = ints[1];
4491                                 else
4492                                         param = config_params[i].def_param;
4493                                 if (config_params[i].fn)
4494                                         config_params[i].fn(ints, param,
4495                                                             config_params[i].
4496                                                             param2);
4497                                 if (config_params[i].var) {
4498                                         DPRINT("%s=%d\n", str, param);
4499                                         *config_params[i].var = param;
4500                                 }
4501                                 return 1;
4502                         }
4503                 }
4504         }
4505         if (str) {
4506                 DPRINT("unknown floppy option [%s]\n", str);
4507
4508                 DPRINT("allowed options are:");
4509                 for (i = 0; i < ARRAY_SIZE(config_params); i++)
4510                         pr_cont(" %s", config_params[i].name);
4511                 pr_cont("\n");
4512         } else
4513                 DPRINT("botched floppy option\n");
4514         DPRINT("Read Documentation/admin-guide/blockdev/floppy.rst\n");
4515         return 0;
4516 }
4517
4518 static int have_no_fdc = -ENODEV;
4519
4520 static ssize_t floppy_cmos_show(struct device *dev,
4521                                 struct device_attribute *attr, char *buf)
4522 {
4523         struct platform_device *p = to_platform_device(dev);
4524         int drive;
4525
4526         drive = p->id;
4527         return sprintf(buf, "%X\n", drive_params[drive].cmos);
4528 }
4529
4530 static DEVICE_ATTR(cmos, 0444, floppy_cmos_show, NULL);
4531
4532 static struct attribute *floppy_dev_attrs[] = {
4533         &dev_attr_cmos.attr,
4534         NULL
4535 };
4536
4537 ATTRIBUTE_GROUPS(floppy_dev);
4538
4539 static void floppy_device_release(struct device *dev)
4540 {
4541 }
4542
4543 static int floppy_resume(struct device *dev)
4544 {
4545         int fdc;
4546         int saved_drive;
4547
4548         saved_drive = current_drive;
4549         for (fdc = 0; fdc < N_FDC; fdc++)
4550                 if (fdc_state[fdc].address != -1)
4551                         user_reset_fdc(REVDRIVE(fdc, 0), FD_RESET_ALWAYS, false);
4552         set_fdc(saved_drive);
4553         return 0;
4554 }
4555
4556 static const struct dev_pm_ops floppy_pm_ops = {
4557         .resume = floppy_resume,
4558         .restore = floppy_resume,
4559 };
4560
4561 static struct platform_driver floppy_driver = {
4562         .driver = {
4563                    .name = "floppy",
4564                    .pm = &floppy_pm_ops,
4565         },
4566 };
4567
4568 static const struct blk_mq_ops floppy_mq_ops = {
4569         .queue_rq = floppy_queue_rq,
4570 };
4571
4572 static struct platform_device floppy_device[N_DRIVE];
4573
4574 static bool floppy_available(int drive)
4575 {
4576         if (!(allowed_drive_mask & (1 << drive)))
4577                 return false;
4578         if (fdc_state[FDC(drive)].version == FDC_NONE)
4579                 return false;
4580         return true;
4581 }
4582
4583 static int floppy_alloc_disk(unsigned int drive, unsigned int type)
4584 {
4585         struct gendisk *disk;
4586         int err;
4587
4588         disk = alloc_disk(1);
4589         if (!disk)
4590                 return -ENOMEM;
4591
4592         disk->queue = blk_mq_init_queue(&tag_sets[drive]);
4593         if (IS_ERR(disk->queue)) {
4594                 err = PTR_ERR(disk->queue);
4595                 disk->queue = NULL;
4596                 put_disk(disk);
4597                 return err;
4598         }
4599
4600         blk_queue_bounce_limit(disk->queue, BLK_BOUNCE_HIGH);
4601         blk_queue_max_hw_sectors(disk->queue, 64);
4602         disk->major = FLOPPY_MAJOR;
4603         disk->first_minor = TOMINOR(drive) | (type << 2);
4604         disk->fops = &floppy_fops;
4605         disk->events = DISK_EVENT_MEDIA_CHANGE;
4606         if (type)
4607                 sprintf(disk->disk_name, "fd%d_type%d", drive, type);
4608         else
4609                 sprintf(disk->disk_name, "fd%d", drive);
4610         /* to be cleaned up... */
4611         disk->private_data = (void *)(long)drive;
4612         disk->flags |= GENHD_FL_REMOVABLE;
4613
4614         disks[drive][type] = disk;
4615         return 0;
4616 }
4617
4618 static DEFINE_MUTEX(floppy_probe_lock);
4619
4620 static void floppy_probe(dev_t dev)
4621 {
4622         unsigned int drive = (MINOR(dev) & 3) | ((MINOR(dev) & 0x80) >> 5);
4623         unsigned int type = (MINOR(dev) >> 2) & 0x1f;
4624
4625         if (drive >= N_DRIVE || !floppy_available(drive) ||
4626             type >= ARRAY_SIZE(floppy_type))
4627                 return;
4628
4629         mutex_lock(&floppy_probe_lock);
4630         if (!disks[drive][type]) {
4631                 if (floppy_alloc_disk(drive, type) == 0)
4632                         add_disk(disks[drive][type]);
4633         }
4634         mutex_unlock(&floppy_probe_lock);
4635 }
4636
4637 static int __init do_floppy_init(void)
4638 {
4639         int i, unit, drive, err;
4640
4641         set_debugt();
4642         interruptjiffies = resultjiffies = jiffies;
4643
4644 #if defined(CONFIG_PPC)
4645         if (check_legacy_ioport(FDC1))
4646                 return -ENODEV;
4647 #endif
4648
4649         raw_cmd = NULL;
4650
4651         floppy_wq = alloc_ordered_workqueue("floppy", 0);
4652         if (!floppy_wq)
4653                 return -ENOMEM;
4654
4655         for (drive = 0; drive < N_DRIVE; drive++) {
4656                 memset(&tag_sets[drive], 0, sizeof(tag_sets[drive]));
4657                 tag_sets[drive].ops = &floppy_mq_ops;
4658                 tag_sets[drive].nr_hw_queues = 1;
4659                 tag_sets[drive].nr_maps = 1;
4660                 tag_sets[drive].queue_depth = 2;
4661                 tag_sets[drive].numa_node = NUMA_NO_NODE;
4662                 tag_sets[drive].flags = BLK_MQ_F_SHOULD_MERGE;
4663                 err = blk_mq_alloc_tag_set(&tag_sets[drive]);
4664                 if (err)
4665                         goto out_put_disk;
4666
4667                 err = floppy_alloc_disk(drive, 0);
4668                 if (err)
4669                         goto out_put_disk;
4670
4671                 timer_setup(&motor_off_timer[drive], motor_off_callback, 0);
4672         }
4673
4674         err = __register_blkdev(FLOPPY_MAJOR, "fd", floppy_probe);
4675         if (err)
4676                 goto out_put_disk;
4677
4678         err = platform_driver_register(&floppy_driver);
4679         if (err)
4680                 goto out_unreg_blkdev;
4681
4682         for (i = 0; i < 256; i++)
4683                 if (ITYPE(i))
4684                         floppy_sizes[i] = floppy_type[ITYPE(i)].size;
4685                 else
4686                         floppy_sizes[i] = MAX_DISK_SIZE << 1;
4687
4688         reschedule_timeout(MAXTIMEOUT, "floppy init");
4689         config_types();
4690
4691         for (i = 0; i < N_FDC; i++) {
4692                 memset(&fdc_state[i], 0, sizeof(*fdc_state));
4693                 fdc_state[i].dtr = -1;
4694                 fdc_state[i].dor = 0x4;
4695 #if defined(__sparc__) || defined(__mc68000__)
4696         /*sparcs/sun3x don't have a DOR reset which we can fall back on to */
4697 #ifdef __mc68000__
4698                 if (MACH_IS_SUN3X)
4699 #endif
4700                         fdc_state[i].version = FDC_82072A;
4701 #endif
4702         }
4703
4704         use_virtual_dma = can_use_virtual_dma & 1;
4705         fdc_state[0].address = FDC1;
4706         if (fdc_state[0].address == -1) {
4707                 cancel_delayed_work(&fd_timeout);
4708                 err = -ENODEV;
4709                 goto out_unreg_driver;
4710         }
4711 #if N_FDC > 1
4712         fdc_state[1].address = FDC2;
4713 #endif
4714
4715         current_fdc = 0;        /* reset fdc in case of unexpected interrupt */
4716         err = floppy_grab_irq_and_dma();
4717         if (err) {
4718                 cancel_delayed_work(&fd_timeout);
4719                 err = -EBUSY;
4720                 goto out_unreg_driver;
4721         }
4722
4723         /* initialise drive state */
4724         for (drive = 0; drive < N_DRIVE; drive++) {
4725                 memset(&drive_state[drive], 0, sizeof(drive_state[drive]));
4726                 memset(&write_errors[drive], 0, sizeof(write_errors[drive]));
4727                 set_bit(FD_DISK_NEWCHANGE_BIT, &drive_state[drive].flags);
4728                 set_bit(FD_DISK_CHANGED_BIT, &drive_state[drive].flags);
4729                 set_bit(FD_VERIFY_BIT, &drive_state[drive].flags);
4730                 drive_state[drive].fd_device = -1;
4731                 floppy_track_buffer = NULL;
4732                 max_buffer_sectors = 0;
4733         }
4734         /*
4735          * Small 10 msec delay to let through any interrupt that
4736          * initialization might have triggered, to not
4737          * confuse detection:
4738          */
4739         msleep(10);
4740
4741         for (i = 0; i < N_FDC; i++) {
4742                 fdc_state[i].driver_version = FD_DRIVER_VERSION;
4743                 for (unit = 0; unit < 4; unit++)
4744                         fdc_state[i].track[unit] = 0;
4745                 if (fdc_state[i].address == -1)
4746                         continue;
4747                 fdc_state[i].rawcmd = 2;
4748                 if (user_reset_fdc(REVDRIVE(i, 0), FD_RESET_ALWAYS, false)) {
4749                         /* free ioports reserved by floppy_grab_irq_and_dma() */
4750                         floppy_release_regions(i);
4751                         fdc_state[i].address = -1;
4752                         fdc_state[i].version = FDC_NONE;
4753                         continue;
4754                 }
4755                 /* Try to determine the floppy controller type */
4756                 fdc_state[i].version = get_fdc_version(i);
4757                 if (fdc_state[i].version == FDC_NONE) {
4758                         /* free ioports reserved by floppy_grab_irq_and_dma() */
4759                         floppy_release_regions(i);
4760                         fdc_state[i].address = -1;
4761                         continue;
4762                 }
4763                 if (can_use_virtual_dma == 2 &&
4764                     fdc_state[i].version < FDC_82072A)
4765                         can_use_virtual_dma = 0;
4766
4767                 have_no_fdc = 0;
4768                 /* Not all FDCs seem to be able to handle the version command
4769                  * properly, so force a reset for the standard FDC clones,
4770                  * to avoid interrupt garbage.
4771                  */
4772                 user_reset_fdc(REVDRIVE(i, 0), FD_RESET_ALWAYS, false);
4773         }
4774         current_fdc = 0;
4775         cancel_delayed_work(&fd_timeout);
4776         current_drive = 0;
4777         initialized = true;
4778         if (have_no_fdc) {
4779                 DPRINT("no floppy controllers found\n");
4780                 err = have_no_fdc;
4781                 goto out_release_dma;
4782         }
4783
4784         for (drive = 0; drive < N_DRIVE; drive++) {
4785                 if (!floppy_available(drive))
4786                         continue;
4787
4788                 floppy_device[drive].name = floppy_device_name;
4789                 floppy_device[drive].id = drive;
4790                 floppy_device[drive].dev.release = floppy_device_release;
4791                 floppy_device[drive].dev.groups = floppy_dev_groups;
4792
4793                 err = platform_device_register(&floppy_device[drive]);
4794                 if (err)
4795                         goto out_remove_drives;
4796
4797                 device_add_disk(&floppy_device[drive].dev, disks[drive][0],
4798                                 NULL);
4799         }
4800
4801         return 0;
4802
4803 out_remove_drives:
4804         while (drive--) {
4805                 if (floppy_available(drive)) {
4806                         del_gendisk(disks[drive][0]);
4807                         platform_device_unregister(&floppy_device[drive]);
4808                 }
4809         }
4810 out_release_dma:
4811         if (atomic_read(&usage_count))
4812                 floppy_release_irq_and_dma();
4813 out_unreg_driver:
4814         platform_driver_unregister(&floppy_driver);
4815 out_unreg_blkdev:
4816         unregister_blkdev(FLOPPY_MAJOR, "fd");
4817 out_put_disk:
4818         destroy_workqueue(floppy_wq);
4819         for (drive = 0; drive < N_DRIVE; drive++) {
4820                 if (!disks[drive][0])
4821                         break;
4822                 del_timer_sync(&motor_off_timer[drive]);
4823                 blk_cleanup_queue(disks[drive][0]->queue);
4824                 disks[drive][0]->queue = NULL;
4825                 blk_mq_free_tag_set(&tag_sets[drive]);
4826                 put_disk(disks[drive][0]);
4827         }
4828         return err;
4829 }
4830
4831 #ifndef MODULE
4832 static __init void floppy_async_init(void *data, async_cookie_t cookie)
4833 {
4834         do_floppy_init();
4835 }
4836 #endif
4837
4838 static int __init floppy_init(void)
4839 {
4840 #ifdef MODULE
4841         return do_floppy_init();
4842 #else
4843         /* Don't hold up the bootup by the floppy initialization */
4844         async_schedule(floppy_async_init, NULL);
4845         return 0;
4846 #endif
4847 }
4848
4849 static const struct io_region {
4850         int offset;
4851         int size;
4852 } io_regions[] = {
4853         { 2, 1 },
4854         /* address + 3 is sometimes reserved by pnp bios for motherboard */
4855         { 4, 2 },
4856         /* address + 6 is reserved, and may be taken by IDE.
4857          * Unfortunately, Adaptec doesn't know this :-(, */
4858         { 7, 1 },
4859 };
4860
4861 static void floppy_release_allocated_regions(int fdc, const struct io_region *p)
4862 {
4863         while (p != io_regions) {
4864                 p--;
4865                 release_region(fdc_state[fdc].address + p->offset, p->size);
4866         }
4867 }
4868
4869 #define ARRAY_END(X) (&((X)[ARRAY_SIZE(X)]))
4870
4871 static int floppy_request_regions(int fdc)
4872 {
4873         const struct io_region *p;
4874
4875         for (p = io_regions; p < ARRAY_END(io_regions); p++) {
4876                 if (!request_region(fdc_state[fdc].address + p->offset,
4877                                     p->size, "floppy")) {
4878                         DPRINT("Floppy io-port 0x%04lx in use\n",
4879                                fdc_state[fdc].address + p->offset);
4880                         floppy_release_allocated_regions(fdc, p);
4881                         return -EBUSY;
4882                 }
4883         }
4884         return 0;
4885 }
4886
4887 static void floppy_release_regions(int fdc)
4888 {
4889         floppy_release_allocated_regions(fdc, ARRAY_END(io_regions));
4890 }
4891
4892 static int floppy_grab_irq_and_dma(void)
4893 {
4894         int fdc;
4895
4896         if (atomic_inc_return(&usage_count) > 1)
4897                 return 0;
4898
4899         /*
4900          * We might have scheduled a free_irq(), wait it to
4901          * drain first:
4902          */
4903         flush_workqueue(floppy_wq);
4904
4905         if (fd_request_irq()) {
4906                 DPRINT("Unable to grab IRQ%d for the floppy driver\n",
4907                        FLOPPY_IRQ);
4908                 atomic_dec(&usage_count);
4909                 return -1;
4910         }
4911         if (fd_request_dma()) {
4912                 DPRINT("Unable to grab DMA%d for the floppy driver\n",
4913                        FLOPPY_DMA);
4914                 if (can_use_virtual_dma & 2)
4915                         use_virtual_dma = can_use_virtual_dma = 1;
4916                 if (!(can_use_virtual_dma & 1)) {
4917                         fd_free_irq();
4918                         atomic_dec(&usage_count);
4919                         return -1;
4920                 }
4921         }
4922
4923         for (fdc = 0; fdc < N_FDC; fdc++) {
4924                 if (fdc_state[fdc].address != -1) {
4925                         if (floppy_request_regions(fdc))
4926                                 goto cleanup;
4927                 }
4928         }
4929         for (fdc = 0; fdc < N_FDC; fdc++) {
4930                 if (fdc_state[fdc].address != -1) {
4931                         reset_fdc_info(fdc, 1);
4932                         fdc_outb(fdc_state[fdc].dor, fdc, FD_DOR);
4933                 }
4934         }
4935
4936         set_dor(0, ~0, 8);      /* avoid immediate interrupt */
4937
4938         for (fdc = 0; fdc < N_FDC; fdc++)
4939                 if (fdc_state[fdc].address != -1)
4940                         fdc_outb(fdc_state[fdc].dor, fdc, FD_DOR);
4941         /*
4942          * The driver will try and free resources and relies on us
4943          * to know if they were allocated or not.
4944          */
4945         current_fdc = 0;
4946         irqdma_allocated = 1;
4947         return 0;
4948 cleanup:
4949         fd_free_irq();
4950         fd_free_dma();
4951         while (--fdc >= 0)
4952                 floppy_release_regions(fdc);
4953         current_fdc = 0;
4954         atomic_dec(&usage_count);
4955         return -1;
4956 }
4957
4958 static void floppy_release_irq_and_dma(void)
4959 {
4960         int fdc;
4961 #ifndef __sparc__
4962         int drive;
4963 #endif
4964         long tmpsize;
4965         unsigned long tmpaddr;
4966
4967         if (!atomic_dec_and_test(&usage_count))
4968                 return;
4969
4970         if (irqdma_allocated) {
4971                 fd_disable_dma();
4972                 fd_free_dma();
4973                 fd_free_irq();
4974                 irqdma_allocated = 0;
4975         }
4976         set_dor(0, ~0, 8);
4977 #if N_FDC > 1
4978         set_dor(1, ~8, 0);
4979 #endif
4980
4981         if (floppy_track_buffer && max_buffer_sectors) {
4982                 tmpsize = max_buffer_sectors * 1024;
4983                 tmpaddr = (unsigned long)floppy_track_buffer;
4984                 floppy_track_buffer = NULL;
4985                 max_buffer_sectors = 0;
4986                 buffer_min = buffer_max = -1;
4987                 fd_dma_mem_free(tmpaddr, tmpsize);
4988         }
4989 #ifndef __sparc__
4990         for (drive = 0; drive < N_FDC * 4; drive++)
4991                 if (timer_pending(motor_off_timer + drive))
4992                         pr_info("motor off timer %d still active\n", drive);
4993 #endif
4994
4995         if (delayed_work_pending(&fd_timeout))
4996                 pr_info("floppy timer still active:%s\n", timeout_message);
4997         if (delayed_work_pending(&fd_timer))
4998                 pr_info("auxiliary floppy timer still active\n");
4999         if (work_pending(&floppy_work))
5000                 pr_info("work still pending\n");
5001         for (fdc = 0; fdc < N_FDC; fdc++)
5002                 if (fdc_state[fdc].address != -1)
5003                         floppy_release_regions(fdc);
5004 }
5005
5006 #ifdef MODULE
5007
5008 static char *floppy;
5009
5010 static void __init parse_floppy_cfg_string(char *cfg)
5011 {
5012         char *ptr;
5013
5014         while (*cfg) {
5015                 ptr = cfg;
5016                 while (*cfg && *cfg != ' ' && *cfg != '\t')
5017                         cfg++;
5018                 if (*cfg) {
5019                         *cfg = '\0';
5020                         cfg++;
5021                 }
5022                 if (*ptr)
5023                         floppy_setup(ptr);
5024         }
5025 }
5026
5027 static int __init floppy_module_init(void)
5028 {
5029         if (floppy)
5030                 parse_floppy_cfg_string(floppy);
5031         return floppy_init();
5032 }
5033 module_init(floppy_module_init);
5034
5035 static void __exit floppy_module_exit(void)
5036 {
5037         int drive, i;
5038
5039         unregister_blkdev(FLOPPY_MAJOR, "fd");
5040         platform_driver_unregister(&floppy_driver);
5041
5042         destroy_workqueue(floppy_wq);
5043
5044         for (drive = 0; drive < N_DRIVE; drive++) {
5045                 del_timer_sync(&motor_off_timer[drive]);
5046
5047                 if (floppy_available(drive)) {
5048                         for (i = 0; i < ARRAY_SIZE(floppy_type); i++) {
5049                                 if (disks[drive][i])
5050                                         del_gendisk(disks[drive][i]);
5051                         }
5052                         platform_device_unregister(&floppy_device[drive]);
5053                 }
5054                 for (i = 0; i < ARRAY_SIZE(floppy_type); i++) {
5055                         if (disks[drive][i])
5056                                 blk_cleanup_queue(disks[drive][i]->queue);
5057                 }
5058                 blk_mq_free_tag_set(&tag_sets[drive]);
5059
5060                 /*
5061                  * These disks have not called add_disk().  Don't put down
5062                  * queue reference in put_disk().
5063                  */
5064                 if (!(allowed_drive_mask & (1 << drive)) ||
5065                     fdc_state[FDC(drive)].version == FDC_NONE) {
5066                         for (i = 0; i < ARRAY_SIZE(floppy_type); i++) {
5067                                 if (disks[drive][i])
5068                                         disks[drive][i]->queue = NULL;
5069                         }
5070                 }
5071
5072                 for (i = 0; i < ARRAY_SIZE(floppy_type); i++) {
5073                         if (disks[drive][i])
5074                                 put_disk(disks[drive][i]);
5075                 }
5076         }
5077
5078         cancel_delayed_work_sync(&fd_timeout);
5079         cancel_delayed_work_sync(&fd_timer);
5080
5081         if (atomic_read(&usage_count))
5082                 floppy_release_irq_and_dma();
5083
5084         /* eject disk, if any */
5085         fd_eject(0);
5086 }
5087
5088 module_exit(floppy_module_exit);
5089
5090 module_param(floppy, charp, 0);
5091 module_param(FLOPPY_IRQ, int, 0);
5092 module_param(FLOPPY_DMA, int, 0);
5093 MODULE_AUTHOR("Alain L. Knaff");
5094 MODULE_SUPPORTED_DEVICE("fd");
5095 MODULE_LICENSE("GPL");
5096
5097 /* This doesn't actually get used other than for module information */
5098 static const struct pnp_device_id floppy_pnpids[] = {
5099         {"PNP0700", 0},
5100         {}
5101 };
5102
5103 MODULE_DEVICE_TABLE(pnp, floppy_pnpids);
5104
5105 #else
5106
5107 __setup("floppy=", floppy_setup);
5108 module_init(floppy_init)
5109 #endif
5110
5111 MODULE_ALIAS_BLOCKDEV_MAJOR(FLOPPY_MAJOR);