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