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