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