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