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