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