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