floppy: don't write kernel-only members to FDRAWCMD ioctl output
[pandora-kernel.git] / drivers / block / floppy.c
1 /*
2  *  linux/drivers/block/floppy.c
3  *
4  *  Copyright (C) 1991, 1992  Linus Torvalds
5  *  Copyright (C) 1993, 1994  Alain Knaff
6  *  Copyright (C) 1998 Alan Cox
7  */
8
9 /*
10  * 02.12.91 - Changed to static variables to indicate need for reset
11  * and recalibrate. This makes some things easier (output_byte reset
12  * checking etc), and means less interrupt jumping in case of errors,
13  * so the code is hopefully easier to understand.
14  */
15
16 /*
17  * This file is certainly a mess. I've tried my best to get it working,
18  * but I don't like programming floppies, and I have only one anyway.
19  * Urgel. I should check for more errors, and do more graceful error
20  * recovery. Seems there are problems with several drives. I've tried to
21  * correct them. No promises.
22  */
23
24 /*
25  * As with hd.c, all routines within this file can (and will) be called
26  * by interrupts, so extreme caution is needed. A hardware interrupt
27  * handler may not sleep, or a kernel panic will happen. Thus I cannot
28  * call "floppy-on" directly, but have to set a special timer interrupt
29  * etc.
30  */
31
32 /*
33  * 28.02.92 - made track-buffering routines, based on the routines written
34  * by entropy@wintermute.wpi.edu (Lawrence Foard). Linus.
35  */
36
37 /*
38  * Automatic floppy-detection and formatting written by Werner Almesberger
39  * (almesber@nessie.cs.id.ethz.ch), who also corrected some problems with
40  * the floppy-change signal detection.
41  */
42
43 /*
44  * 1992/7/22 -- Hennus Bergman: Added better error reporting, fixed
45  * FDC data overrun bug, added some preliminary stuff for vertical
46  * recording support.
47  *
48  * 1992/9/17: Added DMA allocation & DMA functions. -- hhb.
49  *
50  * TODO: Errors are still not counted properly.
51  */
52
53 /* 1992/9/20
54  * Modifications for ``Sector Shifting'' by Rob Hooft (hooft@chem.ruu.nl)
55  * modeled after the freeware MS-DOS program fdformat/88 V1.8 by
56  * Christoph H. Hochst\"atter.
57  * I have fixed the shift values to the ones I always use. Maybe a new
58  * ioctl() should be created to be able to modify them.
59  * There is a bug in the driver that makes it impossible to format a
60  * floppy as the first thing after bootup.
61  */
62
63 /*
64  * 1993/4/29 -- Linus -- cleaned up the timer handling in the kernel, and
65  * this helped the floppy driver as well. Much cleaner, and still seems to
66  * work.
67  */
68
69 /* 1994/6/24 --bbroad-- added the floppy table entries and made
70  * minor modifications to allow 2.88 floppies to be run.
71  */
72
73 /* 1994/7/13 -- Paul Vojta -- modified the probing code to allow three or more
74  * disk types.
75  */
76
77 /*
78  * 1994/8/8 -- Alain Knaff -- Switched to fdpatch driver: Support for bigger
79  * format bug fixes, but unfortunately some new bugs too...
80  */
81
82 /* 1994/9/17 -- Koen Holtman -- added logging of physical floppy write
83  * errors to allow safe writing by specialized programs.
84  */
85
86 /* 1995/4/24 -- Dan Fandrich -- added support for Commodore 1581 3.5" disks
87  * by defining bit 1 of the "stretch" parameter to mean put sectors on the
88  * opposite side of the disk, leaving the sector IDs alone (i.e. Commodore's
89  * drives are "upside-down").
90  */
91
92 /*
93  * 1995/8/26 -- Andreas Busse -- added Mips support.
94  */
95
96 /*
97  * 1995/10/18 -- Ralf Baechle -- Portability cleanup; move machine dependent
98  * features to asm/floppy.h.
99  */
100
101 /*
102  * 1998/1/21 -- Richard Gooch <rgooch@atnf.csiro.au> -- devfs support
103  */
104
105 /*
106  * 1998/05/07 -- Russell King -- More portability cleanups; moved definition of
107  * interrupt and dma channel to asm/floppy.h. Cleaned up some formatting &
108  * use of '0' for NULL.
109  */
110
111 /*
112  * 1998/06/07 -- Alan Cox -- Merged the 2.0.34 fixes for resource allocation
113  * failures.
114  */
115
116 /*
117  * 1998/09/20 -- David Weinehall -- Added slow-down code for buggy PS/2-drives.
118  */
119
120 /*
121  * 1999/08/13 -- Paul Slootman -- floppy stopped working on Alpha after 24
122  * days, 6 hours, 32 minutes and 32 seconds (i.e. MAXINT jiffies; ints were
123  * being used to store jiffies, which are unsigned longs).
124  */
125
126 /*
127  * 2000/08/28 -- Arnaldo Carvalho de Melo <acme@conectiva.com.br>
128  * - get rid of check_region
129  * - s/suser/capable/
130  */
131
132 /*
133  * 2001/08/26 -- Paul Gortmaker - fix insmod oops on machines with no
134  * floppy controller (lingering task on list after module is gone... boom.)
135  */
136
137 /*
138  * 2002/02/07 -- Anton Altaparmakov - Fix io ports reservation to correct range
139  * (0x3f2-0x3f5, 0x3f7). This fix is a bit of a hack but the proper fix
140  * requires many non-obvious changes in arch dependent code.
141  */
142
143 /* 2003/07/28 -- Daniele Bellucci <bellucda@tiscali.it>.
144  * Better audit of register_blkdev.
145  */
146
147 #undef  FLOPPY_SILENT_DCL_CLEAR
148
149 #define REALLY_SLOW_IO
150
151 #define DEBUGT 2
152
153 #define DPRINT(format, args...) \
154         pr_info("floppy%d: " format, current_drive, ##args)
155
156 #define DCL_DEBUG               /* debug disk change line */
157 #ifdef DCL_DEBUG
158 #define debug_dcl(test, fmt, args...) \
159         do { if ((test) & FD_DEBUG) DPRINT(fmt, ##args); } while (0)
160 #else
161 #define debug_dcl(test, fmt, args...) \
162         do { if (0) DPRINT(fmt, ##args); } while (0)
163 #endif
164
165 /* do print messages for unexpected interrupts */
166 static int print_unex = 1;
167 #include <linux/module.h>
168 #include <linux/sched.h>
169 #include <linux/fs.h>
170 #include <linux/kernel.h>
171 #include <linux/timer.h>
172 #include <linux/workqueue.h>
173 #define FDPATCHES
174 #include <linux/fdreg.h>
175 #include <linux/fd.h>
176 #include <linux/hdreg.h>
177 #include <linux/errno.h>
178 #include <linux/slab.h>
179 #include <linux/mm.h>
180 #include <linux/bio.h>
181 #include <linux/string.h>
182 #include <linux/jiffies.h>
183 #include <linux/fcntl.h>
184 #include <linux/delay.h>
185 #include <linux/mc146818rtc.h>  /* CMOS defines */
186 #include <linux/ioport.h>
187 #include <linux/interrupt.h>
188 #include <linux/init.h>
189 #include <linux/platform_device.h>
190 #include <linux/mod_devicetable.h>
191 #include <linux/buffer_head.h>  /* for invalidate_buffers() */
192 #include <linux/mutex.h>
193 #include <linux/io.h>
194 #include <linux/uaccess.h>
195
196 /*
197  * PS/2 floppies have much slower step rates than regular floppies.
198  * It's been recommended that take about 1/4 of the default speed
199  * in some more extreme cases.
200  */
201 static DEFINE_MUTEX(floppy_mutex);
202 static int slow_floppy;
203
204 #include <asm/dma.h>
205 #include <asm/irq.h>
206 #include <asm/system.h>
207
208 static int FLOPPY_IRQ = 6;
209 static int FLOPPY_DMA = 2;
210 static int can_use_virtual_dma = 2;
211 /* =======
212  * can use virtual DMA:
213  * 0 = use of virtual DMA disallowed by config
214  * 1 = use of virtual DMA prescribed by config
215  * 2 = no virtual DMA preference configured.  By default try hard DMA,
216  * but fall back on virtual DMA when not enough memory available
217  */
218
219 static int use_virtual_dma;
220 /* =======
221  * use virtual DMA
222  * 0 using hard DMA
223  * 1 using virtual DMA
224  * This variable is set to virtual when a DMA mem problem arises, and
225  * reset back in floppy_grab_irq_and_dma.
226  * It is not safe to reset it in other circumstances, because the floppy
227  * driver may have several buffers in use at once, and we do currently not
228  * record each buffers capabilities
229  */
230
231 static DEFINE_SPINLOCK(floppy_lock);
232
233 static unsigned short virtual_dma_port = 0x3f0;
234 irqreturn_t floppy_interrupt(int irq, void *dev_id);
235 static int set_dor(int fdc, char mask, char data);
236
237 #define K_64    0x10000         /* 64KB */
238
239 /* the following is the mask of allowed drives. By default units 2 and
240  * 3 of both floppy controllers are disabled, because switching on the
241  * motor of these drives causes system hangs on some PCI computers. drive
242  * 0 is the low bit (0x1), and drive 7 is the high bit (0x80). Bits are on if
243  * a drive is allowed.
244  *
245  * NOTE: This must come before we include the arch floppy header because
246  *       some ports reference this variable from there. -DaveM
247  */
248
249 static int allowed_drive_mask = 0x33;
250
251 #include <asm/floppy.h>
252
253 static int irqdma_allocated;
254
255 #include <linux/blkdev.h>
256 #include <linux/blkpg.h>
257 #include <linux/cdrom.h>        /* for the compatibility eject ioctl */
258 #include <linux/completion.h>
259
260 static struct request *current_req;
261 static void do_fd_request(struct request_queue *q);
262 static int set_next_request(void);
263
264 #ifndef fd_get_dma_residue
265 #define fd_get_dma_residue() get_dma_residue(FLOPPY_DMA)
266 #endif
267
268 /* Dma Memory related stuff */
269
270 #ifndef fd_dma_mem_free
271 #define fd_dma_mem_free(addr, size) free_pages(addr, get_order(size))
272 #endif
273
274 #ifndef fd_dma_mem_alloc
275 #define fd_dma_mem_alloc(size) __get_dma_pages(GFP_KERNEL, get_order(size))
276 #endif
277
278 static inline void fallback_on_nodma_alloc(char **addr, size_t l)
279 {
280 #ifdef FLOPPY_CAN_FALLBACK_ON_NODMA
281         if (*addr)
282                 return;         /* we have the memory */
283         if (can_use_virtual_dma != 2)
284                 return;         /* no fallback allowed */
285         pr_info("DMA memory shortage. Temporarily falling back on virtual DMA\n");
286         *addr = (char *)nodma_mem_alloc(l);
287 #else
288         return;
289 #endif
290 }
291
292 /* End dma memory related stuff */
293
294 static unsigned long fake_change;
295 static bool initialized;
296
297 #define ITYPE(x)        (((x) >> 2) & 0x1f)
298 #define TOMINOR(x)      ((x & 3) | ((x & 4) << 5))
299 #define UNIT(x)         ((x) & 0x03)            /* drive on fdc */
300 #define FDC(x)          (((x) & 0x04) >> 2)     /* fdc of drive */
301         /* reverse mapping from unit and fdc to drive */
302 #define REVDRIVE(fdc, unit) ((unit) + ((fdc) << 2))
303
304 #define DP      (&drive_params[current_drive])
305 #define DRS     (&drive_state[current_drive])
306 #define DRWE    (&write_errors[current_drive])
307 #define FDCS    (&fdc_state[fdc])
308
309 #define UDP     (&drive_params[drive])
310 #define UDRS    (&drive_state[drive])
311 #define UDRWE   (&write_errors[drive])
312 #define UFDCS   (&fdc_state[FDC(drive)])
313
314 #define PH_HEAD(floppy, head) (((((floppy)->stretch & 2) >> 1) ^ head) << 2)
315 #define STRETCH(floppy) ((floppy)->stretch & FD_STRETCH)
316
317 /* read/write */
318 #define COMMAND         (raw_cmd->cmd[0])
319 #define DR_SELECT       (raw_cmd->cmd[1])
320 #define TRACK           (raw_cmd->cmd[2])
321 #define HEAD            (raw_cmd->cmd[3])
322 #define SECTOR          (raw_cmd->cmd[4])
323 #define SIZECODE        (raw_cmd->cmd[5])
324 #define SECT_PER_TRACK  (raw_cmd->cmd[6])
325 #define GAP             (raw_cmd->cmd[7])
326 #define SIZECODE2       (raw_cmd->cmd[8])
327 #define NR_RW 9
328
329 /* format */
330 #define F_SIZECODE      (raw_cmd->cmd[2])
331 #define F_SECT_PER_TRACK (raw_cmd->cmd[3])
332 #define F_GAP           (raw_cmd->cmd[4])
333 #define F_FILL          (raw_cmd->cmd[5])
334 #define NR_F 6
335
336 /*
337  * Maximum disk size (in kilobytes).
338  * This default is used whenever the current disk size is unknown.
339  * [Now it is rather a minimum]
340  */
341 #define MAX_DISK_SIZE 4         /* 3984 */
342
343 /*
344  * globals used by 'result()'
345  */
346 #define MAX_REPLIES 16
347 static unsigned char reply_buffer[MAX_REPLIES];
348 static int inr;         /* size of reply buffer, when called from interrupt */
349 #define ST0             (reply_buffer[0])
350 #define ST1             (reply_buffer[1])
351 #define ST2             (reply_buffer[2])
352 #define ST3             (reply_buffer[0])       /* result of GETSTATUS */
353 #define R_TRACK         (reply_buffer[3])
354 #define R_HEAD          (reply_buffer[4])
355 #define R_SECTOR        (reply_buffer[5])
356 #define R_SIZECODE      (reply_buffer[6])
357
358 #define SEL_DLY         (2 * HZ / 100)
359
360 /*
361  * this struct defines the different floppy drive types.
362  */
363 static struct {
364         struct floppy_drive_params params;
365         const char *name;       /* name printed while booting */
366 } default_drive_params[] = {
367 /* NOTE: the time values in jiffies should be in msec!
368  CMOS drive type
369   |     Maximum data rate supported by drive type
370   |     |   Head load time, msec
371   |     |   |   Head unload time, msec (not used)
372   |     |   |   |     Step rate interval, usec
373   |     |   |   |     |       Time needed for spinup time (jiffies)
374   |     |   |   |     |       |      Timeout for spinning down (jiffies)
375   |     |   |   |     |       |      |   Spindown offset (where disk stops)
376   |     |   |   |     |       |      |   |     Select delay
377   |     |   |   |     |       |      |   |     |     RPS
378   |     |   |   |     |       |      |   |     |     |    Max number of tracks
379   |     |   |   |     |       |      |   |     |     |    |     Interrupt timeout
380   |     |   |   |     |       |      |   |     |     |    |     |   Max nonintlv. sectors
381   |     |   |   |     |       |      |   |     |     |    |     |   | -Max Errors- flags */
382 {{0,  500, 16, 16, 8000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  80, 3*HZ, 20, {3,1,2,0,2}, 0,
383       0, { 7, 4, 8, 2, 1, 5, 3,10}, 3*HZ/2, 0 }, "unknown" },
384
385 {{1,  300, 16, 16, 8000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  40, 3*HZ, 17, {3,1,2,0,2}, 0,
386       0, { 1, 0, 0, 0, 0, 0, 0, 0}, 3*HZ/2, 1 }, "360K PC" }, /*5 1/4 360 KB PC*/
387
388 {{2,  500, 16, 16, 6000, 4*HZ/10, 3*HZ, 14, SEL_DLY, 6,  83, 3*HZ, 17, {3,1,2,0,2}, 0,
389       0, { 2, 5, 6,23,10,20,12, 0}, 3*HZ/2, 2 }, "1.2M" }, /*5 1/4 HD AT*/
390
391 {{3,  250, 16, 16, 3000,    1*HZ, 3*HZ,  0, SEL_DLY, 5,  83, 3*HZ, 20, {3,1,2,0,2}, 0,
392       0, { 4,22,21,30, 3, 0, 0, 0}, 3*HZ/2, 4 }, "720k" }, /*3 1/2 DD*/
393
394 {{4,  500, 16, 16, 4000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 20, {3,1,2,0,2}, 0,
395       0, { 7, 4,25,22,31,21,29,11}, 3*HZ/2, 7 }, "1.44M" }, /*3 1/2 HD*/
396
397 {{5, 1000, 15,  8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 40, {3,1,2,0,2}, 0,
398       0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M AMI BIOS" }, /*3 1/2 ED*/
399
400 {{6, 1000, 15,  8, 3000, 4*HZ/10, 3*HZ, 10, SEL_DLY, 5,  83, 3*HZ, 40, {3,1,2,0,2}, 0,
401       0, { 7, 8, 4,25,28,22,31,21}, 3*HZ/2, 8 }, "2.88M" } /*3 1/2 ED*/
402 /*    |  --autodetected formats---    |      |      |
403  *    read_track                      |      |    Name printed when booting
404  *                                    |     Native format
405  *                  Frequency of disk change checks */
406 };
407
408 static struct floppy_drive_params drive_params[N_DRIVE];
409 static struct floppy_drive_struct drive_state[N_DRIVE];
410 static struct floppy_write_errors write_errors[N_DRIVE];
411 static struct timer_list motor_off_timer[N_DRIVE];
412 static struct gendisk *disks[N_DRIVE];
413 static struct block_device *opened_bdev[N_DRIVE];
414 static DEFINE_MUTEX(open_lock);
415 static struct floppy_raw_cmd *raw_cmd, default_raw_cmd;
416 static int fdc_queue;
417
418 /*
419  * This struct defines the different floppy types.
420  *
421  * Bit 0 of 'stretch' tells if the tracks need to be doubled for some
422  * types (e.g. 360kB diskette in 1.2MB drive, etc.).  Bit 1 of 'stretch'
423  * tells if the disk is in Commodore 1581 format, which means side 0 sectors
424  * are located on side 1 of the disk but with a side 0 ID, and vice-versa.
425  * This is the same as the Sharp MZ-80 5.25" CP/M disk format, except that the
426  * 1581's logical side 0 is on physical side 1, whereas the Sharp's logical
427  * side 0 is on physical side 0 (but with the misnamed sector IDs).
428  * 'stretch' should probably be renamed to something more general, like
429  * 'options'.
430  *
431  * Bits 2 through 9 of 'stretch' tell the number of the first sector.
432  * The LSB (bit 2) is flipped. For most disks, the first sector
433  * is 1 (represented by 0x00<<2).  For some CP/M and music sampler
434  * disks (such as Ensoniq EPS 16plus) it is 0 (represented as 0x01<<2).
435  * For Amstrad CPC disks it is 0xC1 (represented as 0xC0<<2).
436  *
437  * Other parameters should be self-explanatory (see also setfdprm(8)).
438  */
439 /*
440             Size
441              |  Sectors per track
442              |  | Head
443              |  | |  Tracks
444              |  | |  | Stretch
445              |  | |  | |  Gap 1 size
446              |  | |  | |    |  Data rate, | 0x40 for perp
447              |  | |  | |    |    |  Spec1 (stepping rate, head unload
448              |  | |  | |    |    |    |    /fmt gap (gap2) */
449 static struct floppy_struct floppy_type[32] = {
450         {    0, 0,0, 0,0,0x00,0x00,0x00,0x00,NULL    }, /*  0 no testing    */
451         {  720, 9,2,40,0,0x2A,0x02,0xDF,0x50,"d360"  }, /*  1 360KB PC      */
452         { 2400,15,2,80,0,0x1B,0x00,0xDF,0x54,"h1200" }, /*  2 1.2MB AT      */
453         {  720, 9,1,80,0,0x2A,0x02,0xDF,0x50,"D360"  }, /*  3 360KB SS 3.5" */
454         { 1440, 9,2,80,0,0x2A,0x02,0xDF,0x50,"D720"  }, /*  4 720KB 3.5"    */
455         {  720, 9,2,40,1,0x23,0x01,0xDF,0x50,"h360"  }, /*  5 360KB AT      */
456         { 1440, 9,2,80,0,0x23,0x01,0xDF,0x50,"h720"  }, /*  6 720KB AT      */
457         { 2880,18,2,80,0,0x1B,0x00,0xCF,0x6C,"H1440" }, /*  7 1.44MB 3.5"   */
458         { 5760,36,2,80,0,0x1B,0x43,0xAF,0x54,"E2880" }, /*  8 2.88MB 3.5"   */
459         { 6240,39,2,80,0,0x1B,0x43,0xAF,0x28,"E3120" }, /*  9 3.12MB 3.5"   */
460
461         { 2880,18,2,80,0,0x25,0x00,0xDF,0x02,"h1440" }, /* 10 1.44MB 5.25"  */
462         { 3360,21,2,80,0,0x1C,0x00,0xCF,0x0C,"H1680" }, /* 11 1.68MB 3.5"   */
463         {  820,10,2,41,1,0x25,0x01,0xDF,0x2E,"h410"  }, /* 12 410KB 5.25"   */
464         { 1640,10,2,82,0,0x25,0x02,0xDF,0x2E,"H820"  }, /* 13 820KB 3.5"    */
465         { 2952,18,2,82,0,0x25,0x00,0xDF,0x02,"h1476" }, /* 14 1.48MB 5.25"  */
466         { 3444,21,2,82,0,0x25,0x00,0xDF,0x0C,"H1722" }, /* 15 1.72MB 3.5"   */
467         {  840,10,2,42,1,0x25,0x01,0xDF,0x2E,"h420"  }, /* 16 420KB 5.25"   */
468         { 1660,10,2,83,0,0x25,0x02,0xDF,0x2E,"H830"  }, /* 17 830KB 3.5"    */
469         { 2988,18,2,83,0,0x25,0x00,0xDF,0x02,"h1494" }, /* 18 1.49MB 5.25"  */
470         { 3486,21,2,83,0,0x25,0x00,0xDF,0x0C,"H1743" }, /* 19 1.74 MB 3.5"  */
471
472         { 1760,11,2,80,0,0x1C,0x09,0xCF,0x00,"h880"  }, /* 20 880KB 5.25"   */
473         { 2080,13,2,80,0,0x1C,0x01,0xCF,0x00,"D1040" }, /* 21 1.04MB 3.5"   */
474         { 2240,14,2,80,0,0x1C,0x19,0xCF,0x00,"D1120" }, /* 22 1.12MB 3.5"   */
475         { 3200,20,2,80,0,0x1C,0x20,0xCF,0x2C,"h1600" }, /* 23 1.6MB 5.25"   */
476         { 3520,22,2,80,0,0x1C,0x08,0xCF,0x2e,"H1760" }, /* 24 1.76MB 3.5"   */
477         { 3840,24,2,80,0,0x1C,0x20,0xCF,0x00,"H1920" }, /* 25 1.92MB 3.5"   */
478         { 6400,40,2,80,0,0x25,0x5B,0xCF,0x00,"E3200" }, /* 26 3.20MB 3.5"   */
479         { 7040,44,2,80,0,0x25,0x5B,0xCF,0x00,"E3520" }, /* 27 3.52MB 3.5"   */
480         { 7680,48,2,80,0,0x25,0x63,0xCF,0x00,"E3840" }, /* 28 3.84MB 3.5"   */
481         { 3680,23,2,80,0,0x1C,0x10,0xCF,0x00,"H1840" }, /* 29 1.84MB 3.5"   */
482
483         { 1600,10,2,80,0,0x25,0x02,0xDF,0x2E,"D800"  }, /* 30 800KB 3.5"    */
484         { 3200,20,2,80,0,0x1C,0x00,0xCF,0x2C,"H1600" }, /* 31 1.6MB 3.5"    */
485 };
486
487 #define SECTSIZE (_FD_SECTSIZE(*floppy))
488
489 /* Auto-detection: Disk type used until the next media change occurs. */
490 static struct floppy_struct *current_type[N_DRIVE];
491
492 /*
493  * User-provided type information. current_type points to
494  * the respective entry of this array.
495  */
496 static struct floppy_struct user_params[N_DRIVE];
497
498 static sector_t floppy_sizes[256];
499
500 static char floppy_device_name[] = "floppy";
501
502 /*
503  * The driver is trying to determine the correct media format
504  * while probing is set. rw_interrupt() clears it after a
505  * successful access.
506  */
507 static int probing;
508
509 /* Synchronization of FDC access. */
510 #define FD_COMMAND_NONE         -1
511 #define FD_COMMAND_ERROR        2
512 #define FD_COMMAND_OKAY         3
513
514 static volatile int command_status = FD_COMMAND_NONE;
515 static unsigned long fdc_busy;
516 static DECLARE_WAIT_QUEUE_HEAD(fdc_wait);
517 static DECLARE_WAIT_QUEUE_HEAD(command_done);
518
519 /* Errors during formatting are counted here. */
520 static int format_errors;
521
522 /* Format request descriptor. */
523 static struct format_descr format_req;
524
525 /*
526  * Rate is 0 for 500kb/s, 1 for 300kbps, 2 for 250kbps
527  * Spec1 is 0xSH, where S is stepping rate (F=1ms, E=2ms, D=3ms etc),
528  * H is head unload time (1=16ms, 2=32ms, etc)
529  */
530
531 /*
532  * Track buffer
533  * Because these are written to by the DMA controller, they must
534  * not contain a 64k byte boundary crossing, or data will be
535  * corrupted/lost.
536  */
537 static char *floppy_track_buffer;
538 static int max_buffer_sectors;
539
540 static int *errors;
541 typedef void (*done_f)(int);
542 static const struct cont_t {
543         void (*interrupt)(void);
544                                 /* this is called after the interrupt of the
545                                  * main command */
546         void (*redo)(void);     /* this is called to retry the operation */
547         void (*error)(void);    /* this is called to tally an error */
548         done_f done;            /* this is called to say if the operation has
549                                  * succeeded/failed */
550 } *cont;
551
552 static void floppy_ready(void);
553 static void floppy_start(void);
554 static void process_fd_request(void);
555 static void recalibrate_floppy(void);
556 static void floppy_shutdown(unsigned long);
557
558 static int floppy_request_regions(int);
559 static void floppy_release_regions(int);
560 static int floppy_grab_irq_and_dma(void);
561 static void floppy_release_irq_and_dma(void);
562
563 /*
564  * The "reset" variable should be tested whenever an interrupt is scheduled,
565  * after the commands have been sent. This is to ensure that the driver doesn't
566  * get wedged when the interrupt doesn't come because of a failed command.
567  * reset doesn't need to be tested before sending commands, because
568  * output_byte is automatically disabled when reset is set.
569  */
570 static void reset_fdc(void);
571
572 /*
573  * These are global variables, as that's the easiest way to give
574  * information to interrupts. They are the data used for the current
575  * request.
576  */
577 #define NO_TRACK        -1
578 #define NEED_1_RECAL    -2
579 #define NEED_2_RECAL    -3
580
581 static atomic_t usage_count = ATOMIC_INIT(0);
582
583 /* buffer related variables */
584 static int buffer_track = -1;
585 static int buffer_drive = -1;
586 static int buffer_min = -1;
587 static int buffer_max = -1;
588
589 /* fdc related variables, should end up in a struct */
590 static struct floppy_fdc_state fdc_state[N_FDC];
591 static int fdc;                 /* current fdc */
592
593 static struct floppy_struct *_floppy = floppy_type;
594 static unsigned char current_drive;
595 static long current_count_sectors;
596 static unsigned char fsector_t; /* sector in track */
597 static unsigned char in_sector_offset;  /* offset within physical sector,
598                                          * expressed in units of 512 bytes */
599
600 static inline bool drive_no_geom(int drive)
601 {
602         return !current_type[drive] && !ITYPE(UDRS->fd_device);
603 }
604
605 #ifndef fd_eject
606 static inline int fd_eject(int drive)
607 {
608         return -EINVAL;
609 }
610 #endif
611
612 /*
613  * Debugging
614  * =========
615  */
616 #ifdef DEBUGT
617 static long unsigned debugtimer;
618
619 static inline void set_debugt(void)
620 {
621         debugtimer = jiffies;
622 }
623
624 static inline void debugt(const char *func, const char *msg)
625 {
626         if (DP->flags & DEBUGT)
627                 pr_info("%s:%s dtime=%lu\n", func, msg, jiffies - debugtimer);
628 }
629 #else
630 static inline void set_debugt(void) { }
631 static inline void debugt(const char *func, const char *msg) { }
632 #endif /* DEBUGT */
633
634 typedef void (*timeout_fn)(unsigned long);
635 static DEFINE_TIMER(fd_timeout, floppy_shutdown, 0, 0);
636
637 static const char *timeout_message;
638
639 static void is_alive(const char *func, const char *message)
640 {
641         /* this routine checks whether the floppy driver is "alive" */
642         if (test_bit(0, &fdc_busy) && command_status < 2 &&
643             !timer_pending(&fd_timeout)) {
644                 DPRINT("%s: timeout handler died.  %s\n", func, message);
645         }
646 }
647
648 static void (*do_floppy)(void) = NULL;
649
650 #define OLOGSIZE 20
651
652 static void (*lasthandler)(void);
653 static unsigned long interruptjiffies;
654 static unsigned long resultjiffies;
655 static int resultsize;
656 static unsigned long lastredo;
657
658 static struct output_log {
659         unsigned char data;
660         unsigned char status;
661         unsigned long jiffies;
662 } output_log[OLOGSIZE];
663
664 static int output_log_pos;
665
666 #define current_reqD -1
667 #define MAXTIMEOUT -2
668
669 static void __reschedule_timeout(int drive, const char *message)
670 {
671         if (drive == current_reqD)
672                 drive = current_drive;
673         del_timer(&fd_timeout);
674         if (drive < 0 || drive >= N_DRIVE) {
675                 fd_timeout.expires = jiffies + 20UL * HZ;
676                 drive = 0;
677         } else
678                 fd_timeout.expires = jiffies + UDP->timeout;
679         add_timer(&fd_timeout);
680         if (UDP->flags & FD_DEBUG)
681                 DPRINT("reschedule timeout %s\n", message);
682         timeout_message = message;
683 }
684
685 static void reschedule_timeout(int drive, const char *message)
686 {
687         unsigned long flags;
688
689         spin_lock_irqsave(&floppy_lock, flags);
690         __reschedule_timeout(drive, message);
691         spin_unlock_irqrestore(&floppy_lock, flags);
692 }
693
694 #define INFBOUND(a, b) (a) = max_t(int, a, b)
695 #define SUPBOUND(a, b) (a) = min_t(int, a, b)
696
697 /*
698  * Bottom half floppy driver.
699  * ==========================
700  *
701  * This part of the file contains the code talking directly to the hardware,
702  * and also the main service loop (seek-configure-spinup-command)
703  */
704
705 /*
706  * disk change.
707  * This routine is responsible for maintaining the FD_DISK_CHANGE flag,
708  * and the last_checked date.
709  *
710  * last_checked is the date of the last check which showed 'no disk change'
711  * FD_DISK_CHANGE is set under two conditions:
712  * 1. The floppy has been changed after some i/o to that floppy already
713  *    took place.
714  * 2. No floppy disk is in the drive. This is done in order to ensure that
715  *    requests are quickly flushed in case there is no disk in the drive. It
716  *    follows that FD_DISK_CHANGE can only be cleared if there is a disk in
717  *    the drive.
718  *
719  * For 1., maxblock is observed. Maxblock is 0 if no i/o has taken place yet.
720  * For 2., FD_DISK_NEWCHANGE is watched. FD_DISK_NEWCHANGE is cleared on
721  *  each seek. If a disk is present, the disk change line should also be
722  *  cleared on each seek. Thus, if FD_DISK_NEWCHANGE is clear, but the disk
723  *  change line is set, this means either that no disk is in the drive, or
724  *  that it has been removed since the last seek.
725  *
726  * This means that we really have a third possibility too:
727  *  The floppy has been changed after the last seek.
728  */
729
730 static int disk_change(int drive)
731 {
732         int fdc = FDC(drive);
733
734         if (time_before(jiffies, UDRS->select_date + UDP->select_delay))
735                 DPRINT("WARNING disk change called early\n");
736         if (!(FDCS->dor & (0x10 << UNIT(drive))) ||
737             (FDCS->dor & 3) != UNIT(drive) || fdc != FDC(drive)) {
738                 DPRINT("probing disk change on unselected drive\n");
739                 DPRINT("drive=%d fdc=%d dor=%x\n", drive, FDC(drive),
740                        (unsigned int)FDCS->dor);
741         }
742
743         debug_dcl(UDP->flags,
744                   "checking disk change line for drive %d\n", drive);
745         debug_dcl(UDP->flags, "jiffies=%lu\n", jiffies);
746         debug_dcl(UDP->flags, "disk change line=%x\n", fd_inb(FD_DIR) & 0x80);
747         debug_dcl(UDP->flags, "flags=%lx\n", UDRS->flags);
748
749         if (UDP->flags & FD_BROKEN_DCL)
750                 return test_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
751         if ((fd_inb(FD_DIR) ^ UDP->flags) & 0x80) {
752                 set_bit(FD_VERIFY_BIT, &UDRS->flags);
753                                         /* verify write protection */
754
755                 if (UDRS->maxblock)     /* mark it changed */
756                         set_bit(FD_DISK_CHANGED_BIT, &UDRS->flags);
757
758                 /* invalidate its geometry */
759                 if (UDRS->keep_data >= 0) {
760                         if ((UDP->flags & FTD_MSG) &&
761                             current_type[drive] != NULL)
762                                 DPRINT("Disk type is undefined after disk change\n");
763                         current_type[drive] = NULL;
764                         floppy_sizes[TOMINOR(drive)] = MAX_DISK_SIZE << 1;
765                 }
766
767                 return 1;
768         } else {
769                 UDRS->last_checked = jiffies;
770                 clear_bit(FD_DISK_NEWCHANGE_BIT, &UDRS->flags);
771         }
772         return 0;
773 }
774
775 static inline int is_selected(int dor, int unit)
776 {
777         return ((dor & (0x10 << unit)) && (dor & 3) == unit);
778 }
779
780 static bool is_ready_state(int status)
781 {
782         int state = status & (STATUS_READY | STATUS_DIR | STATUS_DMA);
783         return state == STATUS_READY;
784 }
785
786 static int set_dor(int fdc, char mask, char data)
787 {
788         unsigned char unit;
789         unsigned char drive;
790         unsigned char newdor;
791         unsigned char olddor;
792
793         if (FDCS->address == -1)
794                 return -1;
795
796         olddor = FDCS->dor;
797         newdor = (olddor & mask) | data;
798         if (newdor != olddor) {
799                 unit = olddor & 0x3;
800                 if (is_selected(olddor, unit) && !is_selected(newdor, unit)) {
801                         drive = REVDRIVE(fdc, unit);
802                         debug_dcl(UDP->flags,
803                                   "calling disk change from set_dor\n");
804                         disk_change(drive);
805                 }
806                 FDCS->dor = newdor;
807                 fd_outb(newdor, FD_DOR);
808
809                 unit = newdor & 0x3;
810                 if (!is_selected(olddor, unit) && is_selected(newdor, unit)) {
811                         drive = REVDRIVE(fdc, unit);
812                         UDRS->select_date = jiffies;
813                 }
814         }
815         return olddor;
816 }
817
818 static void twaddle(void)
819 {
820         if (DP->select_delay)
821                 return;
822         fd_outb(FDCS->dor & ~(0x10 << UNIT(current_drive)), FD_DOR);
823         fd_outb(FDCS->dor, FD_DOR);
824         DRS->select_date = jiffies;
825 }
826
827 /*
828  * Reset all driver information about the current fdc.
829  * This is needed after a reset, and after a raw command.
830  */
831 static void reset_fdc_info(int mode)
832 {
833         int drive;
834
835         FDCS->spec1 = FDCS->spec2 = -1;
836         FDCS->need_configure = 1;
837         FDCS->perp_mode = 1;
838         FDCS->rawcmd = 0;
839         for (drive = 0; drive < N_DRIVE; drive++)
840                 if (FDC(drive) == fdc && (mode || UDRS->track != NEED_1_RECAL))
841                         UDRS->track = NEED_2_RECAL;
842 }
843
844 /* selects the fdc and drive, and enables the fdc's input/dma. */
845 static void set_fdc(int drive)
846 {
847         if (drive >= 0 && drive < N_DRIVE) {
848                 fdc = FDC(drive);
849                 current_drive = drive;
850         }
851         if (fdc != 1 && fdc != 0) {
852                 pr_info("bad fdc value\n");
853                 return;
854         }
855         set_dor(fdc, ~0, 8);
856 #if N_FDC > 1
857         set_dor(1 - fdc, ~8, 0);
858 #endif
859         if (FDCS->rawcmd == 2)
860                 reset_fdc_info(1);
861         if (fd_inb(FD_STATUS) != STATUS_READY)
862                 FDCS->reset = 1;
863 }
864
865 /* locks the driver */
866 static int lock_fdc(int drive, bool interruptible)
867 {
868         if (WARN(atomic_read(&usage_count) == 0,
869                  "Trying to lock fdc while usage count=0\n"))
870                 return -1;
871
872         if (wait_event_interruptible(fdc_wait, !test_and_set_bit(0, &fdc_busy)))
873                 return -EINTR;
874
875         command_status = FD_COMMAND_NONE;
876
877         __reschedule_timeout(drive, "lock fdc");
878         set_fdc(drive);
879         return 0;
880 }
881
882 /* unlocks the driver */
883 static void unlock_fdc(void)
884 {
885         unsigned long flags;
886
887         raw_cmd = NULL;
888         if (!test_bit(0, &fdc_busy))
889                 DPRINT("FDC access conflict!\n");
890
891         if (do_floppy)
892                 DPRINT("device interrupt still active at FDC release: %pf!\n",
893                        do_floppy);
894         command_status = FD_COMMAND_NONE;
895         spin_lock_irqsave(&floppy_lock, flags);
896         del_timer(&fd_timeout);
897         cont = NULL;
898         clear_bit(0, &fdc_busy);
899         if (current_req || set_next_request())
900                 do_fd_request(current_req->q);
901         spin_unlock_irqrestore(&floppy_lock, flags);
902         wake_up(&fdc_wait);
903 }
904
905 /* switches the motor off after a given timeout */
906 static void motor_off_callback(unsigned long nr)
907 {
908         unsigned char mask = ~(0x10 << UNIT(nr));
909
910         set_dor(FDC(nr), mask, 0);
911 }
912
913 /* schedules motor off */
914 static void floppy_off(unsigned int drive)
915 {
916         unsigned long volatile delta;
917         int fdc = FDC(drive);
918
919         if (!(FDCS->dor & (0x10 << UNIT(drive))))
920                 return;
921
922         del_timer(motor_off_timer + drive);
923
924         /* make spindle stop in a position which minimizes spinup time
925          * next time */
926         if (UDP->rps) {
927                 delta = jiffies - UDRS->first_read_date + HZ -
928                     UDP->spindown_offset;
929                 delta = ((delta * UDP->rps) % HZ) / UDP->rps;
930                 motor_off_timer[drive].expires =
931                     jiffies + UDP->spindown - delta;
932         }
933         add_timer(motor_off_timer + drive);
934 }
935
936 /*
937  * cycle through all N_DRIVE floppy drives, for disk change testing.
938  * stopping at current drive. This is done before any long operation, to
939  * be sure to have up to date disk change information.
940  */
941 static void scandrives(void)
942 {
943         int i;
944         int drive;
945         int saved_drive;
946
947         if (DP->select_delay)
948                 return;
949
950         saved_drive = current_drive;
951         for (i = 0; i < N_DRIVE; i++) {
952                 drive = (saved_drive + i + 1) % N_DRIVE;
953                 if (UDRS->fd_ref == 0 || UDP->select_delay != 0)
954                         continue;       /* skip closed drives */
955                 set_fdc(drive);
956                 if (!(set_dor(fdc, ~3, UNIT(drive) | (0x10 << UNIT(drive))) &
957                       (0x10 << UNIT(drive))))
958                         /* switch the motor off again, if it was off to
959                          * begin with */
960                         set_dor(fdc, ~(0x10 << UNIT(drive)), 0);
961         }
962         set_fdc(saved_drive);
963 }
964
965 static void empty(void)
966 {
967 }
968
969 static DECLARE_WORK(floppy_work, NULL);
970
971 static void schedule_bh(void (*handler)(void))
972 {
973         PREPARE_WORK(&floppy_work, (work_func_t)handler);
974         schedule_work(&floppy_work);
975 }
976
977 static DEFINE_TIMER(fd_timer, NULL, 0, 0);
978
979 static void cancel_activity(void)
980 {
981         unsigned long flags;
982
983         spin_lock_irqsave(&floppy_lock, flags);
984         do_floppy = NULL;
985         PREPARE_WORK(&floppy_work, (work_func_t)empty);
986         del_timer(&fd_timer);
987         spin_unlock_irqrestore(&floppy_lock, flags);
988 }
989
990 /* this function makes sure that the disk stays in the drive during the
991  * transfer */
992 static void fd_watchdog(void)
993 {
994         debug_dcl(DP->flags, "calling disk change from watchdog\n");
995
996         if (disk_change(current_drive)) {
997                 DPRINT("disk removed during i/o\n");
998                 cancel_activity();
999                 cont->done(0);
1000                 reset_fdc();
1001         } else {
1002                 del_timer(&fd_timer);
1003                 fd_timer.function = (timeout_fn)fd_watchdog;
1004                 fd_timer.expires = jiffies + HZ / 10;
1005                 add_timer(&fd_timer);
1006         }
1007 }
1008
1009 static void main_command_interrupt(void)
1010 {
1011         del_timer(&fd_timer);
1012         cont->interrupt();
1013 }
1014
1015 /* waits for a delay (spinup or select) to pass */
1016 static int fd_wait_for_completion(unsigned long delay, timeout_fn function)
1017 {
1018         if (FDCS->reset) {
1019                 reset_fdc();    /* do the reset during sleep to win time
1020                                  * if we don't need to sleep, it's a good
1021                                  * occasion anyways */
1022                 return 1;
1023         }
1024
1025         if (time_before(jiffies, delay)) {
1026                 del_timer(&fd_timer);
1027                 fd_timer.function = function;
1028                 fd_timer.expires = delay;
1029                 add_timer(&fd_timer);
1030                 return 1;
1031         }
1032         return 0;
1033 }
1034
1035 static void setup_DMA(void)
1036 {
1037         unsigned long f;
1038
1039         if (raw_cmd->length == 0) {
1040                 int i;
1041
1042                 pr_info("zero dma transfer size:");
1043                 for (i = 0; i < raw_cmd->cmd_count; i++)
1044                         pr_cont("%x,", raw_cmd->cmd[i]);
1045                 pr_cont("\n");
1046                 cont->done(0);
1047                 FDCS->reset = 1;
1048                 return;
1049         }
1050         if (((unsigned long)raw_cmd->kernel_data) % 512) {
1051                 pr_info("non aligned address: %p\n", raw_cmd->kernel_data);
1052                 cont->done(0);
1053                 FDCS->reset = 1;
1054                 return;
1055         }
1056         f = claim_dma_lock();
1057         fd_disable_dma();
1058 #ifdef fd_dma_setup
1059         if (fd_dma_setup(raw_cmd->kernel_data, raw_cmd->length,
1060                          (raw_cmd->flags & FD_RAW_READ) ?
1061                          DMA_MODE_READ : DMA_MODE_WRITE, FDCS->address) < 0) {
1062                 release_dma_lock(f);
1063                 cont->done(0);
1064                 FDCS->reset = 1;
1065                 return;
1066         }
1067         release_dma_lock(f);
1068 #else
1069         fd_clear_dma_ff();
1070         fd_cacheflush(raw_cmd->kernel_data, raw_cmd->length);
1071         fd_set_dma_mode((raw_cmd->flags & FD_RAW_READ) ?
1072                         DMA_MODE_READ : DMA_MODE_WRITE);
1073         fd_set_dma_addr(raw_cmd->kernel_data);
1074         fd_set_dma_count(raw_cmd->length);
1075         virtual_dma_port = FDCS->address;
1076         fd_enable_dma();
1077         release_dma_lock(f);
1078 #endif
1079 }
1080
1081 static void show_floppy(void);
1082
1083 /* waits until the fdc becomes ready */
1084 static int wait_til_ready(void)
1085 {
1086         int status;
1087         int counter;
1088
1089         if (FDCS->reset)
1090                 return -1;
1091         for (counter = 0; counter < 10000; counter++) {
1092                 status = fd_inb(FD_STATUS);
1093                 if (status & STATUS_READY)
1094                         return status;
1095         }
1096         if (initialized) {
1097                 DPRINT("Getstatus times out (%x) on fdc %d\n", status, fdc);
1098                 show_floppy();
1099         }
1100         FDCS->reset = 1;
1101         return -1;
1102 }
1103
1104 /* sends a command byte to the fdc */
1105 static int output_byte(char byte)
1106 {
1107         int status = wait_til_ready();
1108
1109         if (status < 0)
1110                 return -1;
1111
1112         if (is_ready_state(status)) {
1113                 fd_outb(byte, FD_DATA);
1114                 output_log[output_log_pos].data = byte;
1115                 output_log[output_log_pos].status = status;
1116                 output_log[output_log_pos].jiffies = jiffies;
1117                 output_log_pos = (output_log_pos + 1) % OLOGSIZE;
1118                 return 0;
1119         }
1120         FDCS->reset = 1;
1121         if (initialized) {
1122                 DPRINT("Unable to send byte %x to FDC. Fdc=%x Status=%x\n",
1123                        byte, fdc, status);
1124                 show_floppy();
1125         }
1126         return -1;
1127 }
1128
1129 /* gets the response from the fdc */
1130 static int result(void)
1131 {
1132         int i;
1133         int status = 0;
1134
1135         for (i = 0; i < MAX_REPLIES; i++) {
1136                 status = wait_til_ready();
1137                 if (status < 0)
1138                         break;
1139                 status &= STATUS_DIR | STATUS_READY | STATUS_BUSY | STATUS_DMA;
1140                 if ((status & ~STATUS_BUSY) == STATUS_READY) {
1141                         resultjiffies = jiffies;
1142                         resultsize = i;
1143                         return i;
1144                 }
1145                 if (status == (STATUS_DIR | STATUS_READY | STATUS_BUSY))
1146                         reply_buffer[i] = fd_inb(FD_DATA);
1147                 else
1148                         break;
1149         }
1150         if (initialized) {
1151                 DPRINT("get result error. Fdc=%d Last status=%x Read bytes=%d\n",
1152                        fdc, status, i);
1153                 show_floppy();
1154         }
1155         FDCS->reset = 1;
1156         return -1;
1157 }
1158
1159 #define MORE_OUTPUT -2
1160 /* does the fdc need more output? */
1161 static int need_more_output(void)
1162 {
1163         int status = wait_til_ready();
1164
1165         if (status < 0)
1166                 return -1;
1167
1168         if (is_ready_state(status))
1169                 return MORE_OUTPUT;
1170
1171         return result();
1172 }
1173
1174 /* Set perpendicular mode as required, based on data rate, if supported.
1175  * 82077 Now tested. 1Mbps data rate only possible with 82077-1.
1176  */
1177 static void perpendicular_mode(void)
1178 {
1179         unsigned char perp_mode;
1180
1181         if (raw_cmd->rate & 0x40) {
1182                 switch (raw_cmd->rate & 3) {
1183                 case 0:
1184                         perp_mode = 2;
1185                         break;
1186                 case 3:
1187                         perp_mode = 3;
1188                         break;
1189                 default:
1190                         DPRINT("Invalid data rate for perpendicular mode!\n");
1191                         cont->done(0);
1192                         FDCS->reset = 1;
1193                                         /*
1194                                          * convenient way to return to
1195                                          * redo without too much hassle
1196                                          * (deep stack et al.)
1197                                          */
1198                         return;
1199                 }
1200         } else
1201                 perp_mode = 0;
1202
1203         if (FDCS->perp_mode == perp_mode)
1204                 return;
1205         if (FDCS->version >= FDC_82077_ORIG) {
1206                 output_byte(FD_PERPENDICULAR);
1207                 output_byte(perp_mode);
1208                 FDCS->perp_mode = perp_mode;
1209         } else if (perp_mode) {
1210                 DPRINT("perpendicular mode not supported by this FDC.\n");
1211         }
1212 }                               /* perpendicular_mode */
1213
1214 static int fifo_depth = 0xa;
1215 static int no_fifo;
1216
1217 static int fdc_configure(void)
1218 {
1219         /* Turn on FIFO */
1220         output_byte(FD_CONFIGURE);
1221         if (need_more_output() != MORE_OUTPUT)
1222                 return 0;
1223         output_byte(0);
1224         output_byte(0x10 | (no_fifo & 0x20) | (fifo_depth & 0xf));
1225         output_byte(0);         /* pre-compensation from track
1226                                    0 upwards */
1227         return 1;
1228 }
1229
1230 #define NOMINAL_DTR 500
1231
1232 /* Issue a "SPECIFY" command to set the step rate time, head unload time,
1233  * head load time, and DMA disable flag to values needed by floppy.
1234  *
1235  * The value "dtr" is the data transfer rate in Kbps.  It is needed
1236  * to account for the data rate-based scaling done by the 82072 and 82077
1237  * FDC types.  This parameter is ignored for other types of FDCs (i.e.
1238  * 8272a).
1239  *
1240  * Note that changing the data transfer rate has a (probably deleterious)
1241  * effect on the parameters subject to scaling for 82072/82077 FDCs, so
1242  * fdc_specify is called again after each data transfer rate
1243  * change.
1244  *
1245  * srt: 1000 to 16000 in microseconds
1246  * hut: 16 to 240 milliseconds
1247  * hlt: 2 to 254 milliseconds
1248  *
1249  * These values are rounded up to the next highest available delay time.
1250  */
1251 static void fdc_specify(void)
1252 {
1253         unsigned char spec1;
1254         unsigned char spec2;
1255         unsigned long srt;
1256         unsigned long hlt;
1257         unsigned long hut;
1258         unsigned long dtr = NOMINAL_DTR;
1259         unsigned long scale_dtr = NOMINAL_DTR;
1260         int hlt_max_code = 0x7f;
1261         int hut_max_code = 0xf;
1262
1263         if (FDCS->need_configure && FDCS->version >= FDC_82072A) {
1264                 fdc_configure();
1265                 FDCS->need_configure = 0;
1266         }
1267
1268         switch (raw_cmd->rate & 0x03) {
1269         case 3:
1270                 dtr = 1000;
1271                 break;
1272         case 1:
1273                 dtr = 300;
1274                 if (FDCS->version >= FDC_82078) {
1275                         /* chose the default rate table, not the one
1276                          * where 1 = 2 Mbps */
1277                         output_byte(FD_DRIVESPEC);
1278                         if (need_more_output() == MORE_OUTPUT) {
1279                                 output_byte(UNIT(current_drive));
1280                                 output_byte(0xc0);
1281                         }
1282                 }
1283                 break;
1284         case 2:
1285                 dtr = 250;
1286                 break;
1287         }
1288
1289         if (FDCS->version >= FDC_82072) {
1290                 scale_dtr = dtr;
1291                 hlt_max_code = 0x00;    /* 0==256msec*dtr0/dtr (not linear!) */
1292                 hut_max_code = 0x0;     /* 0==256msec*dtr0/dtr (not linear!) */
1293         }
1294
1295         /* Convert step rate from microseconds to milliseconds and 4 bits */
1296         srt = 16 - DIV_ROUND_UP(DP->srt * scale_dtr / 1000, NOMINAL_DTR);
1297         if (slow_floppy)
1298                 srt = srt / 4;
1299
1300         SUPBOUND(srt, 0xf);
1301         INFBOUND(srt, 0);
1302
1303         hlt = DIV_ROUND_UP(DP->hlt * scale_dtr / 2, NOMINAL_DTR);
1304         if (hlt < 0x01)
1305                 hlt = 0x01;
1306         else if (hlt > 0x7f)
1307                 hlt = hlt_max_code;
1308
1309         hut = DIV_ROUND_UP(DP->hut * scale_dtr / 16, NOMINAL_DTR);
1310         if (hut < 0x1)
1311                 hut = 0x1;
1312         else if (hut > 0xf)
1313                 hut = hut_max_code;
1314
1315         spec1 = (srt << 4) | hut;
1316         spec2 = (hlt << 1) | (use_virtual_dma & 1);
1317
1318         /* If these parameters did not change, just return with success */
1319         if (FDCS->spec1 != spec1 || FDCS->spec2 != spec2) {
1320                 /* Go ahead and set spec1 and spec2 */
1321                 output_byte(FD_SPECIFY);
1322                 output_byte(FDCS->spec1 = spec1);
1323                 output_byte(FDCS->spec2 = spec2);
1324         }
1325 }                               /* fdc_specify */
1326
1327 /* Set the FDC's data transfer rate on behalf of the specified drive.
1328  * NOTE: with 82072/82077 FDCs, changing the data rate requires a reissue
1329  * of the specify command (i.e. using the fdc_specify function).
1330  */
1331 static int fdc_dtr(void)
1332 {
1333         /* If data rate not already set to desired value, set it. */
1334         if ((raw_cmd->rate & 3) == FDCS->dtr)
1335                 return 0;
1336
1337         /* Set dtr */
1338         fd_outb(raw_cmd->rate & 3, FD_DCR);
1339
1340         /* TODO: some FDC/drive combinations (C&T 82C711 with TEAC 1.2MB)
1341          * need a stabilization period of several milliseconds to be
1342          * enforced after data rate changes before R/W operations.
1343          * Pause 5 msec to avoid trouble. (Needs to be 2 jiffies)
1344          */
1345         FDCS->dtr = raw_cmd->rate & 3;
1346         return fd_wait_for_completion(jiffies + 2UL * HZ / 100,
1347                                       (timeout_fn)floppy_ready);
1348 }                               /* fdc_dtr */
1349
1350 static void tell_sector(void)
1351 {
1352         pr_cont(": track %d, head %d, sector %d, size %d",
1353                 R_TRACK, R_HEAD, R_SECTOR, R_SIZECODE);
1354 }                               /* tell_sector */
1355
1356 static void print_errors(void)
1357 {
1358         DPRINT("");
1359         if (ST0 & ST0_ECE) {
1360                 pr_cont("Recalibrate failed!");
1361         } else if (ST2 & ST2_CRC) {
1362                 pr_cont("data CRC error");
1363                 tell_sector();
1364         } else if (ST1 & ST1_CRC) {
1365                 pr_cont("CRC error");
1366                 tell_sector();
1367         } else if ((ST1 & (ST1_MAM | ST1_ND)) ||
1368                    (ST2 & ST2_MAM)) {
1369                 if (!probing) {
1370                         pr_cont("sector not found");
1371                         tell_sector();
1372                 } else
1373                         pr_cont("probe failed...");
1374         } else if (ST2 & ST2_WC) {      /* seek error */
1375                 pr_cont("wrong cylinder");
1376         } else if (ST2 & ST2_BC) {      /* cylinder marked as bad */
1377                 pr_cont("bad cylinder");
1378         } else {
1379                 pr_cont("unknown error. ST[0..2] are: 0x%x 0x%x 0x%x",
1380                         ST0, ST1, ST2);
1381                 tell_sector();
1382         }
1383         pr_cont("\n");
1384 }
1385
1386 /*
1387  * OK, this error interpreting routine is called after a
1388  * DMA read/write has succeeded
1389  * or failed, so we check the results, and copy any buffers.
1390  * hhb: Added better error reporting.
1391  * ak: Made this into a separate routine.
1392  */
1393 static int interpret_errors(void)
1394 {
1395         char bad;
1396
1397         if (inr != 7) {
1398                 DPRINT("-- FDC reply error\n");
1399                 FDCS->reset = 1;
1400                 return 1;
1401         }
1402
1403         /* check IC to find cause of interrupt */
1404         switch (ST0 & ST0_INTR) {
1405         case 0x40:              /* error occurred during command execution */
1406                 if (ST1 & ST1_EOC)
1407                         return 0;       /* occurs with pseudo-DMA */
1408                 bad = 1;
1409                 if (ST1 & ST1_WP) {
1410                         DPRINT("Drive is write protected\n");
1411                         clear_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
1412                         cont->done(0);
1413                         bad = 2;
1414                 } else if (ST1 & ST1_ND) {
1415                         set_bit(FD_NEED_TWADDLE_BIT, &DRS->flags);
1416                 } else if (ST1 & ST1_OR) {
1417                         if (DP->flags & FTD_MSG)
1418                                 DPRINT("Over/Underrun - retrying\n");
1419                         bad = 0;
1420                 } else if (*errors >= DP->max_errors.reporting) {
1421                         print_errors();
1422                 }
1423                 if (ST2 & ST2_WC || ST2 & ST2_BC)
1424                         /* wrong cylinder => recal */
1425                         DRS->track = NEED_2_RECAL;
1426                 return bad;
1427         case 0x80:              /* invalid command given */
1428                 DPRINT("Invalid FDC command given!\n");
1429                 cont->done(0);
1430                 return 2;
1431         case 0xc0:
1432                 DPRINT("Abnormal termination caused by polling\n");
1433                 cont->error();
1434                 return 2;
1435         default:                /* (0) Normal command termination */
1436                 return 0;
1437         }
1438 }
1439
1440 /*
1441  * This routine is called when everything should be correctly set up
1442  * for the transfer (i.e. floppy motor is on, the correct floppy is
1443  * selected, and the head is sitting on the right track).
1444  */
1445 static void setup_rw_floppy(void)
1446 {
1447         int i;
1448         int r;
1449         int flags;
1450         int dflags;
1451         unsigned long ready_date;
1452         timeout_fn function;
1453
1454         flags = raw_cmd->flags;
1455         if (flags & (FD_RAW_READ | FD_RAW_WRITE))
1456                 flags |= FD_RAW_INTR;
1457
1458         if ((flags & FD_RAW_SPIN) && !(flags & FD_RAW_NO_MOTOR)) {
1459                 ready_date = DRS->spinup_date + DP->spinup;
1460                 /* If spinup will take a long time, rerun scandrives
1461                  * again just before spinup completion. Beware that
1462                  * after scandrives, we must again wait for selection.
1463                  */
1464                 if (time_after(ready_date, jiffies + DP->select_delay)) {
1465                         ready_date -= DP->select_delay;
1466                         function = (timeout_fn)floppy_start;
1467                 } else
1468                         function = (timeout_fn)setup_rw_floppy;
1469
1470                 /* wait until the floppy is spinning fast enough */
1471                 if (fd_wait_for_completion(ready_date, function))
1472                         return;
1473         }
1474         dflags = DRS->flags;
1475
1476         if ((flags & FD_RAW_READ) || (flags & FD_RAW_WRITE))
1477                 setup_DMA();
1478
1479         if (flags & FD_RAW_INTR)
1480                 do_floppy = main_command_interrupt;
1481
1482         r = 0;
1483         for (i = 0; i < raw_cmd->cmd_count; i++)
1484                 r |= output_byte(raw_cmd->cmd[i]);
1485
1486         debugt(__func__, "rw_command");
1487
1488         if (r) {
1489                 cont->error();
1490                 reset_fdc();
1491                 return;
1492         }
1493
1494         if (!(flags & FD_RAW_INTR)) {
1495                 inr = result();
1496                 cont->interrupt();
1497         } else if (flags & FD_RAW_NEED_DISK)
1498                 fd_watchdog();
1499 }
1500
1501 static int blind_seek;
1502
1503 /*
1504  * This is the routine called after every seek (or recalibrate) interrupt
1505  * from the floppy controller.
1506  */
1507 static void seek_interrupt(void)
1508 {
1509         debugt(__func__, "");
1510         if (inr != 2 || (ST0 & 0xF8) != 0x20) {
1511                 DPRINT("seek failed\n");
1512                 DRS->track = NEED_2_RECAL;
1513                 cont->error();
1514                 cont->redo();
1515                 return;
1516         }
1517         if (DRS->track >= 0 && DRS->track != ST1 && !blind_seek) {
1518                 debug_dcl(DP->flags,
1519                           "clearing NEWCHANGE flag because of effective seek\n");
1520                 debug_dcl(DP->flags, "jiffies=%lu\n", jiffies);
1521                 clear_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
1522                                         /* effective seek */
1523                 DRS->select_date = jiffies;
1524         }
1525         DRS->track = ST1;
1526         floppy_ready();
1527 }
1528
1529 static void check_wp(void)
1530 {
1531         if (test_bit(FD_VERIFY_BIT, &DRS->flags)) {
1532                                         /* check write protection */
1533                 output_byte(FD_GETSTATUS);
1534                 output_byte(UNIT(current_drive));
1535                 if (result() != 1) {
1536                         FDCS->reset = 1;
1537                         return;
1538                 }
1539                 clear_bit(FD_VERIFY_BIT, &DRS->flags);
1540                 clear_bit(FD_NEED_TWADDLE_BIT, &DRS->flags);
1541                 debug_dcl(DP->flags,
1542                           "checking whether disk is write protected\n");
1543                 debug_dcl(DP->flags, "wp=%x\n", ST3 & 0x40);
1544                 if (!(ST3 & 0x40))
1545                         set_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
1546                 else
1547                         clear_bit(FD_DISK_WRITABLE_BIT, &DRS->flags);
1548         }
1549 }
1550
1551 static void seek_floppy(void)
1552 {
1553         int track;
1554
1555         blind_seek = 0;
1556
1557         debug_dcl(DP->flags, "calling disk change from %s\n", __func__);
1558
1559         if (!test_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags) &&
1560             disk_change(current_drive) && (raw_cmd->flags & FD_RAW_NEED_DISK)) {
1561                 /* the media changed flag should be cleared after the seek.
1562                  * If it isn't, this means that there is really no disk in
1563                  * the drive.
1564                  */
1565                 set_bit(FD_DISK_CHANGED_BIT, &DRS->flags);
1566                 cont->done(0);
1567                 cont->redo();
1568                 return;
1569         }
1570         if (DRS->track <= NEED_1_RECAL) {
1571                 recalibrate_floppy();
1572                 return;
1573         } else if (test_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags) &&
1574                    (raw_cmd->flags & FD_RAW_NEED_DISK) &&
1575                    (DRS->track <= NO_TRACK || DRS->track == raw_cmd->track)) {
1576                 /* we seek to clear the media-changed condition. Does anybody
1577                  * know a more elegant way, which works on all drives? */
1578                 if (raw_cmd->track)
1579                         track = raw_cmd->track - 1;
1580                 else {
1581                         if (DP->flags & FD_SILENT_DCL_CLEAR) {
1582                                 set_dor(fdc, ~(0x10 << UNIT(current_drive)), 0);
1583                                 blind_seek = 1;
1584                                 raw_cmd->flags |= FD_RAW_NEED_SEEK;
1585                         }
1586                         track = 1;
1587                 }
1588         } else {
1589                 check_wp();
1590                 if (raw_cmd->track != DRS->track &&
1591                     (raw_cmd->flags & FD_RAW_NEED_SEEK))
1592                         track = raw_cmd->track;
1593                 else {
1594                         setup_rw_floppy();
1595                         return;
1596                 }
1597         }
1598
1599         do_floppy = seek_interrupt;
1600         output_byte(FD_SEEK);
1601         output_byte(UNIT(current_drive));
1602         if (output_byte(track) < 0) {
1603                 reset_fdc();
1604                 return;
1605         }
1606         debugt(__func__, "");
1607 }
1608
1609 static void recal_interrupt(void)
1610 {
1611         debugt(__func__, "");
1612         if (inr != 2)
1613                 FDCS->reset = 1;
1614         else if (ST0 & ST0_ECE) {
1615                 switch (DRS->track) {
1616                 case NEED_1_RECAL:
1617                         debugt(__func__, "need 1 recal");
1618                         /* after a second recalibrate, we still haven't
1619                          * reached track 0. Probably no drive. Raise an
1620                          * error, as failing immediately might upset
1621                          * computers possessed by the Devil :-) */
1622                         cont->error();
1623                         cont->redo();
1624                         return;
1625                 case NEED_2_RECAL:
1626                         debugt(__func__, "need 2 recal");
1627                         /* If we already did a recalibrate,
1628                          * and we are not at track 0, this
1629                          * means we have moved. (The only way
1630                          * not to move at recalibration is to
1631                          * be already at track 0.) Clear the
1632                          * new change flag */
1633                         debug_dcl(DP->flags,
1634                                   "clearing NEWCHANGE flag because of second recalibrate\n");
1635
1636                         clear_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
1637                         DRS->select_date = jiffies;
1638                         /* fall through */
1639                 default:
1640                         debugt(__func__, "default");
1641                         /* Recalibrate moves the head by at
1642                          * most 80 steps. If after one
1643                          * recalibrate we don't have reached
1644                          * track 0, this might mean that we
1645                          * started beyond track 80.  Try
1646                          * again.  */
1647                         DRS->track = NEED_1_RECAL;
1648                         break;
1649                 }
1650         } else
1651                 DRS->track = ST1;
1652         floppy_ready();
1653 }
1654
1655 static void print_result(char *message, int inr)
1656 {
1657         int i;
1658
1659         DPRINT("%s ", message);
1660         if (inr >= 0)
1661                 for (i = 0; i < inr; i++)
1662                         pr_cont("repl[%d]=%x ", i, reply_buffer[i]);
1663         pr_cont("\n");
1664 }
1665
1666 /* interrupt handler. Note that this can be called externally on the Sparc */
1667 irqreturn_t floppy_interrupt(int irq, void *dev_id)
1668 {
1669         int do_print;
1670         unsigned long f;
1671         void (*handler)(void) = do_floppy;
1672
1673         lasthandler = handler;
1674         interruptjiffies = jiffies;
1675
1676         f = claim_dma_lock();
1677         fd_disable_dma();
1678         release_dma_lock(f);
1679
1680         do_floppy = NULL;
1681         if (fdc >= N_FDC || FDCS->address == -1) {
1682                 /* we don't even know which FDC is the culprit */
1683                 pr_info("DOR0=%x\n", fdc_state[0].dor);
1684                 pr_info("floppy interrupt on bizarre fdc %d\n", fdc);
1685                 pr_info("handler=%pf\n", handler);
1686                 is_alive(__func__, "bizarre fdc");
1687                 return IRQ_NONE;
1688         }
1689
1690         FDCS->reset = 0;
1691         /* We have to clear the reset flag here, because apparently on boxes
1692          * with level triggered interrupts (PS/2, Sparc, ...), it is needed to
1693          * emit SENSEI's to clear the interrupt line. And FDCS->reset blocks the
1694          * emission of the SENSEI's.
1695          * It is OK to emit floppy commands because we are in an interrupt
1696          * handler here, and thus we have to fear no interference of other
1697          * activity.
1698          */
1699
1700         do_print = !handler && print_unex && initialized;
1701
1702         inr = result();
1703         if (do_print)
1704                 print_result("unexpected interrupt", inr);
1705         if (inr == 0) {
1706                 int max_sensei = 4;
1707                 do {
1708                         output_byte(FD_SENSEI);
1709                         inr = result();
1710                         if (do_print)
1711                                 print_result("sensei", inr);
1712                         max_sensei--;
1713                 } while ((ST0 & 0x83) != UNIT(current_drive) &&
1714                          inr == 2 && max_sensei);
1715         }
1716         if (!handler) {
1717                 FDCS->reset = 1;
1718                 return IRQ_NONE;
1719         }
1720         schedule_bh(handler);
1721         is_alive(__func__, "normal interrupt end");
1722
1723         /* FIXME! Was it really for us? */
1724         return IRQ_HANDLED;
1725 }
1726
1727 static void recalibrate_floppy(void)
1728 {
1729         debugt(__func__, "");
1730         do_floppy = recal_interrupt;
1731         output_byte(FD_RECALIBRATE);
1732         if (output_byte(UNIT(current_drive)) < 0)
1733                 reset_fdc();
1734 }
1735
1736 /*
1737  * Must do 4 FD_SENSEIs after reset because of ``drive polling''.
1738  */
1739 static void reset_interrupt(void)
1740 {
1741         debugt(__func__, "");
1742         result();               /* get the status ready for set_fdc */
1743         if (FDCS->reset) {
1744                 pr_info("reset set in interrupt, calling %pf\n", cont->error);
1745                 cont->error();  /* a reset just after a reset. BAD! */
1746         }
1747         cont->redo();
1748 }
1749
1750 /*
1751  * reset is done by pulling bit 2 of DOR low for a while (old FDCs),
1752  * or by setting the self clearing bit 7 of STATUS (newer FDCs)
1753  */
1754 static void reset_fdc(void)
1755 {
1756         unsigned long flags;
1757
1758         do_floppy = reset_interrupt;
1759         FDCS->reset = 0;
1760         reset_fdc_info(0);
1761
1762         /* Pseudo-DMA may intercept 'reset finished' interrupt.  */
1763         /* Irrelevant for systems with true DMA (i386).          */
1764
1765         flags = claim_dma_lock();
1766         fd_disable_dma();
1767         release_dma_lock(flags);
1768
1769         if (FDCS->version >= FDC_82072A)
1770                 fd_outb(0x80 | (FDCS->dtr & 3), FD_STATUS);
1771         else {
1772                 fd_outb(FDCS->dor & ~0x04, FD_DOR);
1773                 udelay(FD_RESET_DELAY);
1774                 fd_outb(FDCS->dor, FD_DOR);
1775         }
1776 }
1777
1778 static void show_floppy(void)
1779 {
1780         int i;
1781
1782         pr_info("\n");
1783         pr_info("floppy driver state\n");
1784         pr_info("-------------------\n");
1785         pr_info("now=%lu last interrupt=%lu diff=%lu last called handler=%pf\n",
1786                 jiffies, interruptjiffies, jiffies - interruptjiffies,
1787                 lasthandler);
1788
1789         pr_info("timeout_message=%s\n", timeout_message);
1790         pr_info("last output bytes:\n");
1791         for (i = 0; i < OLOGSIZE; i++)
1792                 pr_info("%2x %2x %lu\n",
1793                         output_log[(i + output_log_pos) % OLOGSIZE].data,
1794                         output_log[(i + output_log_pos) % OLOGSIZE].status,
1795                         output_log[(i + output_log_pos) % OLOGSIZE].jiffies);
1796         pr_info("last result at %lu\n", resultjiffies);
1797         pr_info("last redo_fd_request at %lu\n", lastredo);
1798         print_hex_dump(KERN_INFO, "", DUMP_PREFIX_NONE, 16, 1,
1799                        reply_buffer, resultsize, true);
1800
1801         pr_info("status=%x\n", fd_inb(FD_STATUS));
1802         pr_info("fdc_busy=%lu\n", fdc_busy);
1803         if (do_floppy)
1804                 pr_info("do_floppy=%pf\n", do_floppy);
1805         if (work_pending(&floppy_work))
1806                 pr_info("floppy_work.func=%pf\n", floppy_work.func);
1807         if (timer_pending(&fd_timer))
1808                 pr_info("fd_timer.function=%pf\n", fd_timer.function);
1809         if (timer_pending(&fd_timeout)) {
1810                 pr_info("timer_function=%pf\n", fd_timeout.function);
1811                 pr_info("expires=%lu\n", fd_timeout.expires - jiffies);
1812                 pr_info("now=%lu\n", jiffies);
1813         }
1814         pr_info("cont=%p\n", cont);
1815         pr_info("current_req=%p\n", current_req);
1816         pr_info("command_status=%d\n", command_status);
1817         pr_info("\n");
1818 }
1819
1820 static void floppy_shutdown(unsigned long data)
1821 {
1822         unsigned long flags;
1823
1824         if (initialized)
1825                 show_floppy();
1826         cancel_activity();
1827
1828         flags = claim_dma_lock();
1829         fd_disable_dma();
1830         release_dma_lock(flags);
1831
1832         /* avoid dma going to a random drive after shutdown */
1833
1834         if (initialized)
1835                 DPRINT("floppy timeout called\n");
1836         FDCS->reset = 1;
1837         if (cont) {
1838                 cont->done(0);
1839                 cont->redo();   /* this will recall reset when needed */
1840         } else {
1841                 pr_info("no cont in shutdown!\n");
1842                 process_fd_request();
1843         }
1844         is_alive(__func__, "");
1845 }
1846
1847 /* start motor, check media-changed condition and write protection */
1848 static int start_motor(void (*function)(void))
1849 {
1850         int mask;
1851         int data;
1852
1853         mask = 0xfc;
1854         data = UNIT(current_drive);
1855         if (!(raw_cmd->flags & FD_RAW_NO_MOTOR)) {
1856                 if (!(FDCS->dor & (0x10 << UNIT(current_drive)))) {
1857                         set_debugt();
1858                         /* no read since this drive is running */
1859                         DRS->first_read_date = 0;
1860                         /* note motor start time if motor is not yet running */
1861                         DRS->spinup_date = jiffies;
1862                         data |= (0x10 << UNIT(current_drive));
1863                 }
1864         } else if (FDCS->dor & (0x10 << UNIT(current_drive)))
1865                 mask &= ~(0x10 << UNIT(current_drive));
1866
1867         /* starts motor and selects floppy */
1868         del_timer(motor_off_timer + current_drive);
1869         set_dor(fdc, mask, data);
1870
1871         /* wait_for_completion also schedules reset if needed. */
1872         return fd_wait_for_completion(DRS->select_date + DP->select_delay,
1873                                       (timeout_fn)function);
1874 }
1875
1876 static void floppy_ready(void)
1877 {
1878         if (FDCS->reset) {
1879                 reset_fdc();
1880                 return;
1881         }
1882         if (start_motor(floppy_ready))
1883                 return;
1884         if (fdc_dtr())
1885                 return;
1886
1887         debug_dcl(DP->flags, "calling disk change from floppy_ready\n");
1888         if (!(raw_cmd->flags & FD_RAW_NO_MOTOR) &&
1889             disk_change(current_drive) && !DP->select_delay)
1890                 twaddle();      /* this clears the dcl on certain
1891                                  * drive/controller combinations */
1892
1893 #ifdef fd_chose_dma_mode
1894         if ((raw_cmd->flags & FD_RAW_READ) || (raw_cmd->flags & FD_RAW_WRITE)) {
1895                 unsigned long flags = claim_dma_lock();
1896                 fd_chose_dma_mode(raw_cmd->kernel_data, raw_cmd->length);
1897                 release_dma_lock(flags);
1898         }
1899 #endif
1900
1901         if (raw_cmd->flags & (FD_RAW_NEED_SEEK | FD_RAW_NEED_DISK)) {
1902                 perpendicular_mode();
1903                 fdc_specify();  /* must be done here because of hut, hlt ... */
1904                 seek_floppy();
1905         } else {
1906                 if ((raw_cmd->flags & FD_RAW_READ) ||
1907                     (raw_cmd->flags & FD_RAW_WRITE))
1908                         fdc_specify();
1909                 setup_rw_floppy();
1910         }
1911 }
1912
1913 static void floppy_start(void)
1914 {
1915         reschedule_timeout(current_reqD, "floppy start");
1916
1917         scandrives();
1918         debug_dcl(DP->flags, "setting NEWCHANGE in floppy_start\n");
1919         set_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
1920         floppy_ready();
1921 }
1922
1923 /*
1924  * ========================================================================
1925  * here ends the bottom half. Exported routines are:
1926  * floppy_start, floppy_off, floppy_ready, lock_fdc, unlock_fdc, set_fdc,
1927  * start_motor, reset_fdc, reset_fdc_info, interpret_errors.
1928  * Initialization also uses output_byte, result, set_dor, floppy_interrupt
1929  * and set_dor.
1930  * ========================================================================
1931  */
1932 /*
1933  * General purpose continuations.
1934  * ==============================
1935  */
1936
1937 static void do_wakeup(void)
1938 {
1939         reschedule_timeout(MAXTIMEOUT, "do wakeup");
1940         cont = NULL;
1941         command_status += 2;
1942         wake_up(&command_done);
1943 }
1944
1945 static const struct cont_t wakeup_cont = {
1946         .interrupt      = empty,
1947         .redo           = do_wakeup,
1948         .error          = empty,
1949         .done           = (done_f)empty
1950 };
1951
1952 static const struct cont_t intr_cont = {
1953         .interrupt      = empty,
1954         .redo           = process_fd_request,
1955         .error          = empty,
1956         .done           = (done_f)empty
1957 };
1958
1959 static int wait_til_done(void (*handler)(void), bool interruptible)
1960 {
1961         int ret;
1962
1963         schedule_bh(handler);
1964
1965         if (interruptible)
1966                 wait_event_interruptible(command_done, command_status >= 2);
1967         else
1968                 wait_event(command_done, command_status >= 2);
1969
1970         if (command_status < 2) {
1971                 cancel_activity();
1972                 cont = &intr_cont;
1973                 reset_fdc();
1974                 return -EINTR;
1975         }
1976
1977         if (FDCS->reset)
1978                 command_status = FD_COMMAND_ERROR;
1979         if (command_status == FD_COMMAND_OKAY)
1980                 ret = 0;
1981         else
1982                 ret = -EIO;
1983         command_status = FD_COMMAND_NONE;
1984         return ret;
1985 }
1986
1987 static void generic_done(int result)
1988 {
1989         command_status = result;
1990         cont = &wakeup_cont;
1991 }
1992
1993 static void generic_success(void)
1994 {
1995         cont->done(1);
1996 }
1997
1998 static void generic_failure(void)
1999 {
2000         cont->done(0);
2001 }
2002
2003 static void success_and_wakeup(void)
2004 {
2005         generic_success();
2006         cont->redo();
2007 }
2008
2009 /*
2010  * formatting and rw support.
2011  * ==========================
2012  */
2013
2014 static int next_valid_format(void)
2015 {
2016         int probed_format;
2017
2018         probed_format = DRS->probed_format;
2019         while (1) {
2020                 if (probed_format >= 8 || !DP->autodetect[probed_format]) {
2021                         DRS->probed_format = 0;
2022                         return 1;
2023                 }
2024                 if (floppy_type[DP->autodetect[probed_format]].sect) {
2025                         DRS->probed_format = probed_format;
2026                         return 0;
2027                 }
2028                 probed_format++;
2029         }
2030 }
2031
2032 static void bad_flp_intr(void)
2033 {
2034         int err_count;
2035
2036         if (probing) {
2037                 DRS->probed_format++;
2038                 if (!next_valid_format())
2039                         return;
2040         }
2041         err_count = ++(*errors);
2042         INFBOUND(DRWE->badness, err_count);
2043         if (err_count > DP->max_errors.abort)
2044                 cont->done(0);
2045         if (err_count > DP->max_errors.reset)
2046                 FDCS->reset = 1;
2047         else if (err_count > DP->max_errors.recal)
2048                 DRS->track = NEED_2_RECAL;
2049 }
2050
2051 static void set_floppy(int drive)
2052 {
2053         int type = ITYPE(UDRS->fd_device);
2054
2055         if (type)
2056                 _floppy = floppy_type + type;
2057         else
2058                 _floppy = current_type[drive];
2059 }
2060
2061 /*
2062  * formatting support.
2063  * ===================
2064  */
2065 static void format_interrupt(void)
2066 {
2067         switch (interpret_errors()) {
2068         case 1:
2069                 cont->error();
2070         case 2:
2071                 break;
2072         case 0:
2073                 cont->done(1);
2074         }
2075         cont->redo();
2076 }
2077
2078 #define FM_MODE(x, y) ((y) & ~(((x)->rate & 0x80) >> 1))
2079 #define CT(x) ((x) | 0xc0)
2080
2081 static void setup_format_params(int track)
2082 {
2083         int n;
2084         int il;
2085         int count;
2086         int head_shift;
2087         int track_shift;
2088         struct fparm {
2089                 unsigned char track, head, sect, size;
2090         } *here = (struct fparm *)floppy_track_buffer;
2091
2092         raw_cmd = &default_raw_cmd;
2093         raw_cmd->track = track;
2094
2095         raw_cmd->flags = (FD_RAW_WRITE | FD_RAW_INTR | FD_RAW_SPIN |
2096                           FD_RAW_NEED_DISK | FD_RAW_NEED_SEEK);
2097         raw_cmd->rate = _floppy->rate & 0x43;
2098         raw_cmd->cmd_count = NR_F;
2099         COMMAND = FM_MODE(_floppy, FD_FORMAT);
2100         DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy, format_req.head);
2101         F_SIZECODE = FD_SIZECODE(_floppy);
2102         F_SECT_PER_TRACK = _floppy->sect << 2 >> F_SIZECODE;
2103         F_GAP = _floppy->fmt_gap;
2104         F_FILL = FD_FILL_BYTE;
2105
2106         raw_cmd->kernel_data = floppy_track_buffer;
2107         raw_cmd->length = 4 * F_SECT_PER_TRACK;
2108
2109         /* allow for about 30ms for data transport per track */
2110         head_shift = (F_SECT_PER_TRACK + 5) / 6;
2111
2112         /* a ``cylinder'' is two tracks plus a little stepping time */
2113         track_shift = 2 * head_shift + 3;
2114
2115         /* position of logical sector 1 on this track */
2116         n = (track_shift * format_req.track + head_shift * format_req.head)
2117             % F_SECT_PER_TRACK;
2118
2119         /* determine interleave */
2120         il = 1;
2121         if (_floppy->fmt_gap < 0x22)
2122                 il++;
2123
2124         /* initialize field */
2125         for (count = 0; count < F_SECT_PER_TRACK; ++count) {
2126                 here[count].track = format_req.track;
2127                 here[count].head = format_req.head;
2128                 here[count].sect = 0;
2129                 here[count].size = F_SIZECODE;
2130         }
2131         /* place logical sectors */
2132         for (count = 1; count <= F_SECT_PER_TRACK; ++count) {
2133                 here[n].sect = count;
2134                 n = (n + il) % F_SECT_PER_TRACK;
2135                 if (here[n].sect) {     /* sector busy, find next free sector */
2136                         ++n;
2137                         if (n >= F_SECT_PER_TRACK) {
2138                                 n -= F_SECT_PER_TRACK;
2139                                 while (here[n].sect)
2140                                         ++n;
2141                         }
2142                 }
2143         }
2144         if (_floppy->stretch & FD_SECTBASEMASK) {
2145                 for (count = 0; count < F_SECT_PER_TRACK; count++)
2146                         here[count].sect += FD_SECTBASE(_floppy) - 1;
2147         }
2148 }
2149
2150 static void redo_format(void)
2151 {
2152         buffer_track = -1;
2153         setup_format_params(format_req.track << STRETCH(_floppy));
2154         floppy_start();
2155         debugt(__func__, "queue format request");
2156 }
2157
2158 static const struct cont_t format_cont = {
2159         .interrupt      = format_interrupt,
2160         .redo           = redo_format,
2161         .error          = bad_flp_intr,
2162         .done           = generic_done
2163 };
2164
2165 static int do_format(int drive, struct format_descr *tmp_format_req)
2166 {
2167         int ret;
2168
2169         if (lock_fdc(drive, true))
2170                 return -EINTR;
2171
2172         set_floppy(drive);
2173         if (!_floppy ||
2174             _floppy->track > DP->tracks ||
2175             tmp_format_req->track >= _floppy->track ||
2176             tmp_format_req->head >= _floppy->head ||
2177             (_floppy->sect << 2) % (1 << FD_SIZECODE(_floppy)) ||
2178             !_floppy->fmt_gap) {
2179                 process_fd_request();
2180                 return -EINVAL;
2181         }
2182         format_req = *tmp_format_req;
2183         format_errors = 0;
2184         cont = &format_cont;
2185         errors = &format_errors;
2186         ret = wait_til_done(redo_format, true);
2187         if (ret == -EINTR)
2188                 return -EINTR;
2189         process_fd_request();
2190         return ret;
2191 }
2192
2193 /*
2194  * Buffer read/write and support
2195  * =============================
2196  */
2197
2198 static void floppy_end_request(struct request *req, int error)
2199 {
2200         unsigned int nr_sectors = current_count_sectors;
2201         unsigned int drive = (unsigned long)req->rq_disk->private_data;
2202
2203         /* current_count_sectors can be zero if transfer failed */
2204         if (error)
2205                 nr_sectors = blk_rq_cur_sectors(req);
2206         if (__blk_end_request(req, error, nr_sectors << 9))
2207                 return;
2208
2209         /* We're done with the request */
2210         floppy_off(drive);
2211         current_req = NULL;
2212 }
2213
2214 /* new request_done. Can handle physical sectors which are smaller than a
2215  * logical buffer */
2216 static void request_done(int uptodate)
2217 {
2218         struct request *req = current_req;
2219         struct request_queue *q;
2220         unsigned long flags;
2221         int block;
2222         char msg[sizeof("request done ") + sizeof(int) * 3];
2223
2224         probing = 0;
2225         snprintf(msg, sizeof(msg), "request done %d", uptodate);
2226         reschedule_timeout(MAXTIMEOUT, msg);
2227
2228         if (!req) {
2229                 pr_info("floppy.c: no request in request_done\n");
2230                 return;
2231         }
2232
2233         q = req->q;
2234
2235         if (uptodate) {
2236                 /* maintain values for invalidation on geometry
2237                  * change */
2238                 block = current_count_sectors + blk_rq_pos(req);
2239                 INFBOUND(DRS->maxblock, block);
2240                 if (block > _floppy->sect)
2241                         DRS->maxtrack = 1;
2242
2243                 /* unlock chained buffers */
2244                 spin_lock_irqsave(q->queue_lock, flags);
2245                 floppy_end_request(req, 0);
2246                 spin_unlock_irqrestore(q->queue_lock, flags);
2247         } else {
2248                 if (rq_data_dir(req) == WRITE) {
2249                         /* record write error information */
2250                         DRWE->write_errors++;
2251                         if (DRWE->write_errors == 1) {
2252                                 DRWE->first_error_sector = blk_rq_pos(req);
2253                                 DRWE->first_error_generation = DRS->generation;
2254                         }
2255                         DRWE->last_error_sector = blk_rq_pos(req);
2256                         DRWE->last_error_generation = DRS->generation;
2257                 }
2258                 spin_lock_irqsave(q->queue_lock, flags);
2259                 floppy_end_request(req, -EIO);
2260                 spin_unlock_irqrestore(q->queue_lock, flags);
2261         }
2262 }
2263
2264 /* Interrupt handler evaluating the result of the r/w operation */
2265 static void rw_interrupt(void)
2266 {
2267         int eoc;
2268         int ssize;
2269         int heads;
2270         int nr_sectors;
2271
2272         if (R_HEAD >= 2) {
2273                 /* some Toshiba floppy controllers occasionnally seem to
2274                  * return bogus interrupts after read/write operations, which
2275                  * can be recognized by a bad head number (>= 2) */
2276                 return;
2277         }
2278
2279         if (!DRS->first_read_date)
2280                 DRS->first_read_date = jiffies;
2281
2282         nr_sectors = 0;
2283         ssize = DIV_ROUND_UP(1 << SIZECODE, 4);
2284
2285         if (ST1 & ST1_EOC)
2286                 eoc = 1;
2287         else
2288                 eoc = 0;
2289
2290         if (COMMAND & 0x80)
2291                 heads = 2;
2292         else
2293                 heads = 1;
2294
2295         nr_sectors = (((R_TRACK - TRACK) * heads +
2296                        R_HEAD - HEAD) * SECT_PER_TRACK +
2297                       R_SECTOR - SECTOR + eoc) << SIZECODE >> 2;
2298
2299         if (nr_sectors / ssize >
2300             DIV_ROUND_UP(in_sector_offset + current_count_sectors, ssize)) {
2301                 DPRINT("long rw: %x instead of %lx\n",
2302                        nr_sectors, current_count_sectors);
2303                 pr_info("rs=%d s=%d\n", R_SECTOR, SECTOR);
2304                 pr_info("rh=%d h=%d\n", R_HEAD, HEAD);
2305                 pr_info("rt=%d t=%d\n", R_TRACK, TRACK);
2306                 pr_info("heads=%d eoc=%d\n", heads, eoc);
2307                 pr_info("spt=%d st=%d ss=%d\n",
2308                         SECT_PER_TRACK, fsector_t, ssize);
2309                 pr_info("in_sector_offset=%d\n", in_sector_offset);
2310         }
2311
2312         nr_sectors -= in_sector_offset;
2313         INFBOUND(nr_sectors, 0);
2314         SUPBOUND(current_count_sectors, nr_sectors);
2315
2316         switch (interpret_errors()) {
2317         case 2:
2318                 cont->redo();
2319                 return;
2320         case 1:
2321                 if (!current_count_sectors) {
2322                         cont->error();
2323                         cont->redo();
2324                         return;
2325                 }
2326                 break;
2327         case 0:
2328                 if (!current_count_sectors) {
2329                         cont->redo();
2330                         return;
2331                 }
2332                 current_type[current_drive] = _floppy;
2333                 floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
2334                 break;
2335         }
2336
2337         if (probing) {
2338                 if (DP->flags & FTD_MSG)
2339                         DPRINT("Auto-detected floppy type %s in fd%d\n",
2340                                _floppy->name, current_drive);
2341                 current_type[current_drive] = _floppy;
2342                 floppy_sizes[TOMINOR(current_drive)] = _floppy->size;
2343                 probing = 0;
2344         }
2345
2346         if (CT(COMMAND) != FD_READ ||
2347             raw_cmd->kernel_data == current_req->buffer) {
2348                 /* transfer directly from buffer */
2349                 cont->done(1);
2350         } else if (CT(COMMAND) == FD_READ) {
2351                 buffer_track = raw_cmd->track;
2352                 buffer_drive = current_drive;
2353                 INFBOUND(buffer_max, nr_sectors + fsector_t);
2354         }
2355         cont->redo();
2356 }
2357
2358 /* Compute maximal contiguous buffer size. */
2359 static int buffer_chain_size(void)
2360 {
2361         struct bio_vec *bv;
2362         int size;
2363         struct req_iterator iter;
2364         char *base;
2365
2366         base = bio_data(current_req->bio);
2367         size = 0;
2368
2369         rq_for_each_segment(bv, current_req, iter) {
2370                 if (page_address(bv->bv_page) + bv->bv_offset != base + size)
2371                         break;
2372
2373                 size += bv->bv_len;
2374         }
2375
2376         return size >> 9;
2377 }
2378
2379 /* Compute the maximal transfer size */
2380 static int transfer_size(int ssize, int max_sector, int max_size)
2381 {
2382         SUPBOUND(max_sector, fsector_t + max_size);
2383
2384         /* alignment */
2385         max_sector -= (max_sector % _floppy->sect) % ssize;
2386
2387         /* transfer size, beginning not aligned */
2388         current_count_sectors = max_sector - fsector_t;
2389
2390         return max_sector;
2391 }
2392
2393 /*
2394  * Move data from/to the track buffer to/from the buffer cache.
2395  */
2396 static void copy_buffer(int ssize, int max_sector, int max_sector_2)
2397 {
2398         int remaining;          /* number of transferred 512-byte sectors */
2399         struct bio_vec *bv;
2400         char *buffer;
2401         char *dma_buffer;
2402         int size;
2403         struct req_iterator iter;
2404
2405         max_sector = transfer_size(ssize,
2406                                    min(max_sector, max_sector_2),
2407                                    blk_rq_sectors(current_req));
2408
2409         if (current_count_sectors <= 0 && CT(COMMAND) == FD_WRITE &&
2410             buffer_max > fsector_t + blk_rq_sectors(current_req))
2411                 current_count_sectors = min_t(int, buffer_max - fsector_t,
2412                                               blk_rq_sectors(current_req));
2413
2414         remaining = current_count_sectors << 9;
2415         if (remaining > blk_rq_bytes(current_req) && CT(COMMAND) == FD_WRITE) {
2416                 DPRINT("in copy buffer\n");
2417                 pr_info("current_count_sectors=%ld\n", current_count_sectors);
2418                 pr_info("remaining=%d\n", remaining >> 9);
2419                 pr_info("current_req->nr_sectors=%u\n",
2420                         blk_rq_sectors(current_req));
2421                 pr_info("current_req->current_nr_sectors=%u\n",
2422                         blk_rq_cur_sectors(current_req));
2423                 pr_info("max_sector=%d\n", max_sector);
2424                 pr_info("ssize=%d\n", ssize);
2425         }
2426
2427         buffer_max = max(max_sector, buffer_max);
2428
2429         dma_buffer = floppy_track_buffer + ((fsector_t - buffer_min) << 9);
2430
2431         size = blk_rq_cur_bytes(current_req);
2432
2433         rq_for_each_segment(bv, current_req, iter) {
2434                 if (!remaining)
2435                         break;
2436
2437                 size = bv->bv_len;
2438                 SUPBOUND(size, remaining);
2439
2440                 buffer = page_address(bv->bv_page) + bv->bv_offset;
2441                 if (dma_buffer + size >
2442                     floppy_track_buffer + (max_buffer_sectors << 10) ||
2443                     dma_buffer < floppy_track_buffer) {
2444                         DPRINT("buffer overrun in copy buffer %d\n",
2445                                (int)((floppy_track_buffer - dma_buffer) >> 9));
2446                         pr_info("fsector_t=%d buffer_min=%d\n",
2447                                 fsector_t, buffer_min);
2448                         pr_info("current_count_sectors=%ld\n",
2449                                 current_count_sectors);
2450                         if (CT(COMMAND) == FD_READ)
2451                                 pr_info("read\n");
2452                         if (CT(COMMAND) == FD_WRITE)
2453                                 pr_info("write\n");
2454                         break;
2455                 }
2456                 if (((unsigned long)buffer) % 512)
2457                         DPRINT("%p buffer not aligned\n", buffer);
2458
2459                 if (CT(COMMAND) == FD_READ)
2460                         memcpy(buffer, dma_buffer, size);
2461                 else
2462                         memcpy(dma_buffer, buffer, size);
2463
2464                 remaining -= size;
2465                 dma_buffer += size;
2466         }
2467         if (remaining) {
2468                 if (remaining > 0)
2469                         max_sector -= remaining >> 9;
2470                 DPRINT("weirdness: remaining %d\n", remaining >> 9);
2471         }
2472 }
2473
2474 /* work around a bug in pseudo DMA
2475  * (on some FDCs) pseudo DMA does not stop when the CPU stops
2476  * sending data.  Hence we need a different way to signal the
2477  * transfer length:  We use SECT_PER_TRACK.  Unfortunately, this
2478  * does not work with MT, hence we can only transfer one head at
2479  * a time
2480  */
2481 static void virtualdmabug_workaround(void)
2482 {
2483         int hard_sectors;
2484         int end_sector;
2485
2486         if (CT(COMMAND) == FD_WRITE) {
2487                 COMMAND &= ~0x80;       /* switch off multiple track mode */
2488
2489                 hard_sectors = raw_cmd->length >> (7 + SIZECODE);
2490                 end_sector = SECTOR + hard_sectors - 1;
2491                 if (end_sector > SECT_PER_TRACK) {
2492                         pr_info("too many sectors %d > %d\n",
2493                                 end_sector, SECT_PER_TRACK);
2494                         return;
2495                 }
2496                 SECT_PER_TRACK = end_sector;
2497                                         /* make sure SECT_PER_TRACK
2498                                          * points to end of transfer */
2499         }
2500 }
2501
2502 /*
2503  * Formulate a read/write request.
2504  * this routine decides where to load the data (directly to buffer, or to
2505  * tmp floppy area), how much data to load (the size of the buffer, the whole
2506  * track, or a single sector)
2507  * All floppy_track_buffer handling goes in here. If we ever add track buffer
2508  * allocation on the fly, it should be done here. No other part should need
2509  * modification.
2510  */
2511
2512 static int make_raw_rw_request(void)
2513 {
2514         int aligned_sector_t;
2515         int max_sector;
2516         int max_size;
2517         int tracksize;
2518         int ssize;
2519
2520         if (WARN(max_buffer_sectors == 0, "VFS: Block I/O scheduled on unopened device\n"))
2521                 return 0;
2522
2523         set_fdc((long)current_req->rq_disk->private_data);
2524
2525         raw_cmd = &default_raw_cmd;
2526         raw_cmd->flags = FD_RAW_SPIN | FD_RAW_NEED_DISK | FD_RAW_NEED_DISK |
2527             FD_RAW_NEED_SEEK;
2528         raw_cmd->cmd_count = NR_RW;
2529         if (rq_data_dir(current_req) == READ) {
2530                 raw_cmd->flags |= FD_RAW_READ;
2531                 COMMAND = FM_MODE(_floppy, FD_READ);
2532         } else if (rq_data_dir(current_req) == WRITE) {
2533                 raw_cmd->flags |= FD_RAW_WRITE;
2534                 COMMAND = FM_MODE(_floppy, FD_WRITE);
2535         } else {
2536                 DPRINT("%s: unknown command\n", __func__);
2537                 return 0;
2538         }
2539
2540         max_sector = _floppy->sect * _floppy->head;
2541
2542         TRACK = (int)blk_rq_pos(current_req) / max_sector;
2543         fsector_t = (int)blk_rq_pos(current_req) % max_sector;
2544         if (_floppy->track && TRACK >= _floppy->track) {
2545                 if (blk_rq_cur_sectors(current_req) & 1) {
2546                         current_count_sectors = 1;
2547                         return 1;
2548                 } else
2549                         return 0;
2550         }
2551         HEAD = fsector_t / _floppy->sect;
2552
2553         if (((_floppy->stretch & (FD_SWAPSIDES | FD_SECTBASEMASK)) ||
2554              test_bit(FD_NEED_TWADDLE_BIT, &DRS->flags)) &&
2555             fsector_t < _floppy->sect)
2556                 max_sector = _floppy->sect;
2557
2558         /* 2M disks have phantom sectors on the first track */
2559         if ((_floppy->rate & FD_2M) && (!TRACK) && (!HEAD)) {
2560                 max_sector = 2 * _floppy->sect / 3;
2561                 if (fsector_t >= max_sector) {
2562                         current_count_sectors =
2563                             min_t(int, _floppy->sect - fsector_t,
2564                                   blk_rq_sectors(current_req));
2565                         return 1;
2566                 }
2567                 SIZECODE = 2;
2568         } else
2569                 SIZECODE = FD_SIZECODE(_floppy);
2570         raw_cmd->rate = _floppy->rate & 0x43;
2571         if ((_floppy->rate & FD_2M) && (TRACK || HEAD) && raw_cmd->rate == 2)
2572                 raw_cmd->rate = 1;
2573
2574         if (SIZECODE)
2575                 SIZECODE2 = 0xff;
2576         else
2577                 SIZECODE2 = 0x80;
2578         raw_cmd->track = TRACK << STRETCH(_floppy);
2579         DR_SELECT = UNIT(current_drive) + PH_HEAD(_floppy, HEAD);
2580         GAP = _floppy->gap;
2581         ssize = DIV_ROUND_UP(1 << SIZECODE, 4);
2582         SECT_PER_TRACK = _floppy->sect << 2 >> SIZECODE;
2583         SECTOR = ((fsector_t % _floppy->sect) << 2 >> SIZECODE) +
2584             FD_SECTBASE(_floppy);
2585
2586         /* tracksize describes the size which can be filled up with sectors
2587          * of size ssize.
2588          */
2589         tracksize = _floppy->sect - _floppy->sect % ssize;
2590         if (tracksize < _floppy->sect) {
2591                 SECT_PER_TRACK++;
2592                 if (tracksize <= fsector_t % _floppy->sect)
2593                         SECTOR--;
2594
2595                 /* if we are beyond tracksize, fill up using smaller sectors */
2596                 while (tracksize <= fsector_t % _floppy->sect) {
2597                         while (tracksize + ssize > _floppy->sect) {
2598                                 SIZECODE--;
2599                                 ssize >>= 1;
2600                         }
2601                         SECTOR++;
2602                         SECT_PER_TRACK++;
2603                         tracksize += ssize;
2604                 }
2605                 max_sector = HEAD * _floppy->sect + tracksize;
2606         } else if (!TRACK && !HEAD && !(_floppy->rate & FD_2M) && probing) {
2607                 max_sector = _floppy->sect;
2608         } else if (!HEAD && CT(COMMAND) == FD_WRITE) {
2609                 /* for virtual DMA bug workaround */
2610                 max_sector = _floppy->sect;
2611         }
2612
2613         in_sector_offset = (fsector_t % _floppy->sect) % ssize;
2614         aligned_sector_t = fsector_t - in_sector_offset;
2615         max_size = blk_rq_sectors(current_req);
2616         if ((raw_cmd->track == buffer_track) &&
2617             (current_drive == buffer_drive) &&
2618             (fsector_t >= buffer_min) && (fsector_t < buffer_max)) {
2619                 /* data already in track buffer */
2620                 if (CT(COMMAND) == FD_READ) {
2621                         copy_buffer(1, max_sector, buffer_max);
2622                         return 1;
2623                 }
2624         } else if (in_sector_offset || blk_rq_sectors(current_req) < ssize) {
2625                 if (CT(COMMAND) == FD_WRITE) {
2626                         unsigned int sectors;
2627
2628                         sectors = fsector_t + blk_rq_sectors(current_req);
2629                         if (sectors > ssize && sectors < ssize + ssize)
2630                                 max_size = ssize + ssize;
2631                         else
2632                                 max_size = ssize;
2633                 }
2634                 raw_cmd->flags &= ~FD_RAW_WRITE;
2635                 raw_cmd->flags |= FD_RAW_READ;
2636                 COMMAND = FM_MODE(_floppy, FD_READ);
2637         } else if ((unsigned long)current_req->buffer < MAX_DMA_ADDRESS) {
2638                 unsigned long dma_limit;
2639                 int direct, indirect;
2640
2641                 indirect =
2642                     transfer_size(ssize, max_sector,
2643                                   max_buffer_sectors * 2) - fsector_t;
2644
2645                 /*
2646                  * Do NOT use minimum() here---MAX_DMA_ADDRESS is 64 bits wide
2647                  * on a 64 bit machine!
2648                  */
2649                 max_size = buffer_chain_size();
2650                 dma_limit = (MAX_DMA_ADDRESS -
2651                              ((unsigned long)current_req->buffer)) >> 9;
2652                 if ((unsigned long)max_size > dma_limit)
2653                         max_size = dma_limit;
2654                 /* 64 kb boundaries */
2655                 if (CROSS_64KB(current_req->buffer, max_size << 9))
2656                         max_size = (K_64 -
2657                                     ((unsigned long)current_req->buffer) %
2658                                     K_64) >> 9;
2659                 direct = transfer_size(ssize, max_sector, max_size) - fsector_t;
2660                 /*
2661                  * We try to read tracks, but if we get too many errors, we
2662                  * go back to reading just one sector at a time.
2663                  *
2664                  * This means we should be able to read a sector even if there
2665                  * are other bad sectors on this track.
2666                  */
2667                 if (!direct ||
2668                     (indirect * 2 > direct * 3 &&
2669                      *errors < DP->max_errors.read_track &&
2670                      ((!probing ||
2671                        (DP->read_track & (1 << DRS->probed_format)))))) {
2672                         max_size = blk_rq_sectors(current_req);
2673                 } else {
2674                         raw_cmd->kernel_data = current_req->buffer;
2675                         raw_cmd->length = current_count_sectors << 9;
2676                         if (raw_cmd->length == 0) {
2677                                 DPRINT("%s: zero dma transfer attempted\n", __func__);
2678                                 DPRINT("indirect=%d direct=%d fsector_t=%d\n",
2679                                        indirect, direct, fsector_t);
2680                                 return 0;
2681                         }
2682                         virtualdmabug_workaround();
2683                         return 2;
2684                 }
2685         }
2686
2687         if (CT(COMMAND) == FD_READ)
2688                 max_size = max_sector;  /* unbounded */
2689
2690         /* claim buffer track if needed */
2691         if (buffer_track != raw_cmd->track ||   /* bad track */
2692             buffer_drive != current_drive ||    /* bad drive */
2693             fsector_t > buffer_max ||
2694             fsector_t < buffer_min ||
2695             ((CT(COMMAND) == FD_READ ||
2696               (!in_sector_offset && blk_rq_sectors(current_req) >= ssize)) &&
2697              max_sector > 2 * max_buffer_sectors + buffer_min &&
2698              max_size + fsector_t > 2 * max_buffer_sectors + buffer_min)) {
2699                 /* not enough space */
2700                 buffer_track = -1;
2701                 buffer_drive = current_drive;
2702                 buffer_max = buffer_min = aligned_sector_t;
2703         }
2704         raw_cmd->kernel_data = floppy_track_buffer +
2705                 ((aligned_sector_t - buffer_min) << 9);
2706
2707         if (CT(COMMAND) == FD_WRITE) {
2708                 /* copy write buffer to track buffer.
2709                  * if we get here, we know that the write
2710                  * is either aligned or the data already in the buffer
2711                  * (buffer will be overwritten) */
2712                 if (in_sector_offset && buffer_track == -1)
2713                         DPRINT("internal error offset !=0 on write\n");
2714                 buffer_track = raw_cmd->track;
2715                 buffer_drive = current_drive;
2716                 copy_buffer(ssize, max_sector,
2717                             2 * max_buffer_sectors + buffer_min);
2718         } else
2719                 transfer_size(ssize, max_sector,
2720                               2 * max_buffer_sectors + buffer_min -
2721                               aligned_sector_t);
2722
2723         /* round up current_count_sectors to get dma xfer size */
2724         raw_cmd->length = in_sector_offset + current_count_sectors;
2725         raw_cmd->length = ((raw_cmd->length - 1) | (ssize - 1)) + 1;
2726         raw_cmd->length <<= 9;
2727         if ((raw_cmd->length < current_count_sectors << 9) ||
2728             (raw_cmd->kernel_data != current_req->buffer &&
2729              CT(COMMAND) == FD_WRITE &&
2730              (aligned_sector_t + (raw_cmd->length >> 9) > buffer_max ||
2731               aligned_sector_t < buffer_min)) ||
2732             raw_cmd->length % (128 << SIZECODE) ||
2733             raw_cmd->length <= 0 || current_count_sectors <= 0) {
2734                 DPRINT("fractionary current count b=%lx s=%lx\n",
2735                        raw_cmd->length, current_count_sectors);
2736                 if (raw_cmd->kernel_data != current_req->buffer)
2737                         pr_info("addr=%d, length=%ld\n",
2738                                 (int)((raw_cmd->kernel_data -
2739                                        floppy_track_buffer) >> 9),
2740                                 current_count_sectors);
2741                 pr_info("st=%d ast=%d mse=%d msi=%d\n",
2742                         fsector_t, aligned_sector_t, max_sector, max_size);
2743                 pr_info("ssize=%x SIZECODE=%d\n", ssize, SIZECODE);
2744                 pr_info("command=%x SECTOR=%d HEAD=%d, TRACK=%d\n",
2745                         COMMAND, SECTOR, HEAD, TRACK);
2746                 pr_info("buffer drive=%d\n", buffer_drive);
2747                 pr_info("buffer track=%d\n", buffer_track);
2748                 pr_info("buffer_min=%d\n", buffer_min);
2749                 pr_info("buffer_max=%d\n", buffer_max);
2750                 return 0;
2751         }
2752
2753         if (raw_cmd->kernel_data != current_req->buffer) {
2754                 if (raw_cmd->kernel_data < floppy_track_buffer ||
2755                     current_count_sectors < 0 ||
2756                     raw_cmd->length < 0 ||
2757                     raw_cmd->kernel_data + raw_cmd->length >
2758                     floppy_track_buffer + (max_buffer_sectors << 10)) {
2759                         DPRINT("buffer overrun in schedule dma\n");
2760                         pr_info("fsector_t=%d buffer_min=%d current_count=%ld\n",
2761                                 fsector_t, buffer_min, raw_cmd->length >> 9);
2762                         pr_info("current_count_sectors=%ld\n",
2763                                 current_count_sectors);
2764                         if (CT(COMMAND) == FD_READ)
2765                                 pr_info("read\n");
2766                         if (CT(COMMAND) == FD_WRITE)
2767                                 pr_info("write\n");
2768                         return 0;
2769                 }
2770         } else if (raw_cmd->length > blk_rq_bytes(current_req) ||
2771                    current_count_sectors > blk_rq_sectors(current_req)) {
2772                 DPRINT("buffer overrun in direct transfer\n");
2773                 return 0;
2774         } else if (raw_cmd->length < current_count_sectors << 9) {
2775                 DPRINT("more sectors than bytes\n");
2776                 pr_info("bytes=%ld\n", raw_cmd->length >> 9);
2777                 pr_info("sectors=%ld\n", current_count_sectors);
2778         }
2779         if (raw_cmd->length == 0) {
2780                 DPRINT("zero dma transfer attempted from make_raw_request\n");
2781                 return 0;
2782         }
2783
2784         virtualdmabug_workaround();
2785         return 2;
2786 }
2787
2788 /*
2789  * Round-robin between our available drives, doing one request from each
2790  */
2791 static int set_next_request(void)
2792 {
2793         struct request_queue *q;
2794         int old_pos = fdc_queue;
2795
2796         do {
2797                 q = disks[fdc_queue]->queue;
2798                 if (++fdc_queue == N_DRIVE)
2799                         fdc_queue = 0;
2800                 if (q) {
2801                         current_req = blk_fetch_request(q);
2802                         if (current_req)
2803                                 break;
2804                 }
2805         } while (fdc_queue != old_pos);
2806
2807         return current_req != NULL;
2808 }
2809
2810 static void redo_fd_request(void)
2811 {
2812         int drive;
2813         int tmp;
2814
2815         lastredo = jiffies;
2816         if (current_drive < N_DRIVE)
2817                 floppy_off(current_drive);
2818
2819 do_request:
2820         if (!current_req) {
2821                 int pending;
2822
2823                 spin_lock_irq(&floppy_lock);
2824                 pending = set_next_request();
2825                 spin_unlock_irq(&floppy_lock);
2826
2827                 if (!pending) {
2828                         do_floppy = NULL;
2829                         unlock_fdc();
2830                         return;
2831                 }
2832         }
2833         drive = (long)current_req->rq_disk->private_data;
2834         set_fdc(drive);
2835         reschedule_timeout(current_reqD, "redo fd request");
2836
2837         set_floppy(drive);
2838         raw_cmd = &default_raw_cmd;
2839         raw_cmd->flags = 0;
2840         if (start_motor(redo_fd_request))
2841                 return;
2842
2843         disk_change(current_drive);
2844         if (test_bit(current_drive, &fake_change) ||
2845             test_bit(FD_DISK_CHANGED_BIT, &DRS->flags)) {
2846                 DPRINT("disk absent or changed during operation\n");
2847                 request_done(0);
2848                 goto do_request;
2849         }
2850         if (!_floppy) { /* Autodetection */
2851                 if (!probing) {
2852                         DRS->probed_format = 0;
2853                         if (next_valid_format()) {
2854                                 DPRINT("no autodetectable formats\n");
2855                                 _floppy = NULL;
2856                                 request_done(0);
2857                                 goto do_request;
2858                         }
2859                 }
2860                 probing = 1;
2861                 _floppy = floppy_type + DP->autodetect[DRS->probed_format];
2862         } else
2863                 probing = 0;
2864         errors = &(current_req->errors);
2865         tmp = make_raw_rw_request();
2866         if (tmp < 2) {
2867                 request_done(tmp);
2868                 goto do_request;
2869         }
2870
2871         if (test_bit(FD_NEED_TWADDLE_BIT, &DRS->flags))
2872                 twaddle();
2873         schedule_bh(floppy_start);
2874         debugt(__func__, "queue fd request");
2875         return;
2876 }
2877
2878 static const struct cont_t rw_cont = {
2879         .interrupt      = rw_interrupt,
2880         .redo           = redo_fd_request,
2881         .error          = bad_flp_intr,
2882         .done           = request_done
2883 };
2884
2885 static void process_fd_request(void)
2886 {
2887         cont = &rw_cont;
2888         schedule_bh(redo_fd_request);
2889 }
2890
2891 static void do_fd_request(struct request_queue *q)
2892 {
2893         if (WARN(max_buffer_sectors == 0,
2894                  "VFS: %s called on non-open device\n", __func__))
2895                 return;
2896
2897         if (WARN(atomic_read(&usage_count) == 0,
2898                  "warning: usage count=0, current_req=%p sect=%ld type=%x flags=%x\n",
2899                  current_req, (long)blk_rq_pos(current_req), current_req->cmd_type,
2900                  current_req->cmd_flags))
2901                 return;
2902
2903         if (test_bit(0, &fdc_busy)) {
2904                 /* fdc busy, this new request will be treated when the
2905                    current one is done */
2906                 is_alive(__func__, "old request running");
2907                 return;
2908         }
2909         lock_fdc(MAXTIMEOUT, false);
2910         process_fd_request();
2911         is_alive(__func__, "");
2912 }
2913
2914 static const struct cont_t poll_cont = {
2915         .interrupt      = success_and_wakeup,
2916         .redo           = floppy_ready,
2917         .error          = generic_failure,
2918         .done           = generic_done
2919 };
2920
2921 static int poll_drive(bool interruptible, int flag)
2922 {
2923         /* no auto-sense, just clear dcl */
2924         raw_cmd = &default_raw_cmd;
2925         raw_cmd->flags = flag;
2926         raw_cmd->track = 0;
2927         raw_cmd->cmd_count = 0;
2928         cont = &poll_cont;
2929         debug_dcl(DP->flags, "setting NEWCHANGE in poll_drive\n");
2930         set_bit(FD_DISK_NEWCHANGE_BIT, &DRS->flags);
2931
2932         return wait_til_done(floppy_ready, interruptible);
2933 }
2934
2935 /*
2936  * User triggered reset
2937  * ====================
2938  */
2939
2940 static void reset_intr(void)
2941 {
2942         pr_info("weird, reset interrupt called\n");
2943 }
2944
2945 static const struct cont_t reset_cont = {
2946         .interrupt      = reset_intr,
2947         .redo           = success_and_wakeup,
2948         .error          = generic_failure,
2949         .done           = generic_done
2950 };
2951
2952 static int user_reset_fdc(int drive, int arg, bool interruptible)
2953 {
2954         int ret;
2955
2956         if (lock_fdc(drive, interruptible))
2957                 return -EINTR;
2958
2959         if (arg == FD_RESET_ALWAYS)
2960                 FDCS->reset = 1;
2961         if (FDCS->reset) {
2962                 cont = &reset_cont;
2963                 ret = wait_til_done(reset_fdc, interruptible);
2964                 if (ret == -EINTR)
2965                         return -EINTR;
2966         }
2967         process_fd_request();
2968         return 0;
2969 }
2970
2971 /*
2972  * Misc Ioctl's and support
2973  * ========================
2974  */
2975 static inline int fd_copyout(void __user *param, const void *address,
2976                              unsigned long size)
2977 {
2978         return copy_to_user(param, address, size) ? -EFAULT : 0;
2979 }
2980
2981 static inline int fd_copyin(void __user *param, void *address,
2982                             unsigned long size)
2983 {
2984         return copy_from_user(address, param, size) ? -EFAULT : 0;
2985 }
2986
2987 static const char *drive_name(int type, int drive)
2988 {
2989         struct floppy_struct *floppy;
2990
2991         if (type)
2992                 floppy = floppy_type + type;
2993         else {
2994                 if (UDP->native_format)
2995                         floppy = floppy_type + UDP->native_format;
2996                 else
2997                         return "(null)";
2998         }
2999         if (floppy->name)
3000                 return floppy->name;
3001         else
3002                 return "(null)";
3003 }
3004
3005 /* raw commands */
3006 static void raw_cmd_done(int flag)
3007 {
3008         int i;
3009
3010         if (!flag) {
3011                 raw_cmd->flags |= FD_RAW_FAILURE;
3012                 raw_cmd->flags |= FD_RAW_HARDFAILURE;
3013         } else {
3014                 raw_cmd->reply_count = inr;
3015                 if (raw_cmd->reply_count > MAX_REPLIES)
3016                         raw_cmd->reply_count = 0;
3017                 for (i = 0; i < raw_cmd->reply_count; i++)
3018                         raw_cmd->reply[i] = reply_buffer[i];
3019
3020                 if (raw_cmd->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3021                         unsigned long flags;
3022                         flags = claim_dma_lock();
3023                         raw_cmd->length = fd_get_dma_residue();
3024                         release_dma_lock(flags);
3025                 }
3026
3027                 if ((raw_cmd->flags & FD_RAW_SOFTFAILURE) &&
3028                     (!raw_cmd->reply_count || (raw_cmd->reply[0] & 0xc0)))
3029                         raw_cmd->flags |= FD_RAW_FAILURE;
3030
3031                 if (disk_change(current_drive))
3032                         raw_cmd->flags |= FD_RAW_DISK_CHANGE;
3033                 else
3034                         raw_cmd->flags &= ~FD_RAW_DISK_CHANGE;
3035                 if (raw_cmd->flags & FD_RAW_NO_MOTOR_AFTER)
3036                         motor_off_callback(current_drive);
3037
3038                 if (raw_cmd->next &&
3039                     (!(raw_cmd->flags & FD_RAW_FAILURE) ||
3040                      !(raw_cmd->flags & FD_RAW_STOP_IF_FAILURE)) &&
3041                     ((raw_cmd->flags & FD_RAW_FAILURE) ||
3042                      !(raw_cmd->flags & FD_RAW_STOP_IF_SUCCESS))) {
3043                         raw_cmd = raw_cmd->next;
3044                         return;
3045                 }
3046         }
3047         generic_done(flag);
3048 }
3049
3050 static const struct cont_t raw_cmd_cont = {
3051         .interrupt      = success_and_wakeup,
3052         .redo           = floppy_start,
3053         .error          = generic_failure,
3054         .done           = raw_cmd_done
3055 };
3056
3057 static int raw_cmd_copyout(int cmd, void __user *param,
3058                                   struct floppy_raw_cmd *ptr)
3059 {
3060         int ret;
3061
3062         while (ptr) {
3063                 struct floppy_raw_cmd cmd = *ptr;
3064                 cmd.next = NULL;
3065                 cmd.kernel_data = NULL;
3066                 ret = copy_to_user(param, &cmd, sizeof(cmd));
3067                 if (ret)
3068                         return -EFAULT;
3069                 param += sizeof(struct floppy_raw_cmd);
3070                 if ((ptr->flags & FD_RAW_READ) && ptr->buffer_length) {
3071                         if (ptr->length >= 0 &&
3072                             ptr->length <= ptr->buffer_length) {
3073                                 long length = ptr->buffer_length - ptr->length;
3074                                 ret = fd_copyout(ptr->data, ptr->kernel_data,
3075                                                  length);
3076                                 if (ret)
3077                                         return ret;
3078                         }
3079                 }
3080                 ptr = ptr->next;
3081         }
3082
3083         return 0;
3084 }
3085
3086 static void raw_cmd_free(struct floppy_raw_cmd **ptr)
3087 {
3088         struct floppy_raw_cmd *next;
3089         struct floppy_raw_cmd *this;
3090
3091         this = *ptr;
3092         *ptr = NULL;
3093         while (this) {
3094                 if (this->buffer_length) {
3095                         fd_dma_mem_free((unsigned long)this->kernel_data,
3096                                         this->buffer_length);
3097                         this->buffer_length = 0;
3098                 }
3099                 next = this->next;
3100                 kfree(this);
3101                 this = next;
3102         }
3103 }
3104
3105 static int raw_cmd_copyin(int cmd, void __user *param,
3106                                  struct floppy_raw_cmd **rcmd)
3107 {
3108         struct floppy_raw_cmd *ptr;
3109         int ret;
3110         int i;
3111
3112         *rcmd = NULL;
3113
3114 loop:
3115         ptr = kmalloc(sizeof(struct floppy_raw_cmd), GFP_USER);
3116         if (!ptr)
3117                 return -ENOMEM;
3118         *rcmd = ptr;
3119         ret = copy_from_user(ptr, param, sizeof(*ptr));
3120         ptr->next = NULL;
3121         ptr->buffer_length = 0;
3122         ptr->kernel_data = NULL;
3123         if (ret)
3124                 return -EFAULT;
3125         param += sizeof(struct floppy_raw_cmd);
3126         if (ptr->cmd_count > 33)
3127                         /* the command may now also take up the space
3128                          * initially intended for the reply & the
3129                          * reply count. Needed for long 82078 commands
3130                          * such as RESTORE, which takes ... 17 command
3131                          * bytes. Murphy's law #137: When you reserve
3132                          * 16 bytes for a structure, you'll one day
3133                          * discover that you really need 17...
3134                          */
3135                 return -EINVAL;
3136
3137         for (i = 0; i < 16; i++)
3138                 ptr->reply[i] = 0;
3139         ptr->resultcode = 0;
3140
3141         if (ptr->flags & (FD_RAW_READ | FD_RAW_WRITE)) {
3142                 if (ptr->length <= 0)
3143                         return -EINVAL;
3144                 ptr->kernel_data = (char *)fd_dma_mem_alloc(ptr->length);
3145                 fallback_on_nodma_alloc(&ptr->kernel_data, ptr->length);
3146                 if (!ptr->kernel_data)
3147                         return -ENOMEM;
3148                 ptr->buffer_length = ptr->length;
3149         }
3150         if (ptr->flags & FD_RAW_WRITE) {
3151                 ret = fd_copyin(ptr->data, ptr->kernel_data, ptr->length);
3152                 if (ret)
3153                         return ret;
3154         }
3155
3156         if (ptr->flags & FD_RAW_MORE) {
3157                 rcmd = &(ptr->next);
3158                 ptr->rate &= 0x43;
3159                 goto loop;
3160         }
3161
3162         return 0;
3163 }
3164
3165 static int raw_cmd_ioctl(int cmd, void __user *param)
3166 {
3167         struct floppy_raw_cmd *my_raw_cmd;
3168         int drive;
3169         int ret2;
3170         int ret;
3171
3172         if (FDCS->rawcmd <= 1)
3173                 FDCS->rawcmd = 1;
3174         for (drive = 0; drive < N_DRIVE; drive++) {
3175                 if (FDC(drive) != fdc)
3176                         continue;
3177                 if (drive == current_drive) {
3178                         if (UDRS->fd_ref > 1) {
3179                                 FDCS->rawcmd = 2;
3180                                 break;
3181                         }
3182                 } else if (UDRS->fd_ref) {
3183                         FDCS->rawcmd = 2;
3184                         break;
3185                 }
3186         }
3187
3188         if (FDCS->reset)
3189                 return -EIO;
3190
3191         ret = raw_cmd_copyin(cmd, param, &my_raw_cmd);
3192         if (ret) {
3193                 raw_cmd_free(&my_raw_cmd);
3194  &nbs