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