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