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