xref: /openbmc/linux/drivers/block/ataflop.c (revision b60a5b8d)
1 /*
2  *  drivers/block/ataflop.c
3  *
4  *  Copyright (C) 1993  Greg Harp
5  *  Atari Support by Bjoern Brauel, Roman Hodek
6  *
7  *  Big cleanup Sep 11..14 1994 Roman Hodek:
8  *   - Driver now works interrupt driven
9  *   - Support for two drives; should work, but I cannot test that :-(
10  *   - Reading is done in whole tracks and buffered to speed up things
11  *   - Disk change detection and drive deselecting after motor-off
12  *     similar to TOS
13  *   - Autodetection of disk format (DD/HD); untested yet, because I
14  *     don't have an HD drive :-(
15  *
16  *  Fixes Nov 13 1994 Martin Schaller:
17  *   - Autodetection works now
18  *   - Support for 5 1/4'' disks
19  *   - Removed drive type (unknown on atari)
20  *   - Do seeks with 8 Mhz
21  *
22  *  Changes by Andreas Schwab:
23  *   - After errors in multiple read mode try again reading single sectors
24  *  (Feb 1995):
25  *   - Clean up error handling
26  *   - Set blk_size for proper size checking
27  *   - Initialize track register when testing presence of floppy
28  *   - Implement some ioctl's
29  *
30  *  Changes by Torsten Lang:
31  *   - When probing the floppies we should add the FDCCMDADD_H flag since
32  *     the FDC will otherwise wait forever when no disk is inserted...
33  *
34  * ++ Freddi Aschwanden (fa) 20.9.95 fixes for medusa:
35  *  - MFPDELAY() after each FDC access -> atari
36  *  - more/other disk formats
37  *  - DMA to the block buffer directly if we have a 32bit DMA
38  *  - for medusa, the step rate is always 3ms
39  *  - on medusa, use only cache_push()
40  * Roman:
41  *  - Make disk format numbering independent from minors
42  *  - Let user set max. supported drive type (speeds up format
43  *    detection, saves buffer space)
44  *
45  * Roman 10/15/95:
46  *  - implement some more ioctls
47  *  - disk formatting
48  *
49  * Andreas 95/12/12:
50  *  - increase gap size at start of track for HD/ED disks
51  *
52  * Michael (MSch) 11/07/96:
53  *  - implemented FDSETPRM and FDDEFPRM ioctl
54  *
55  * Andreas (97/03/19):
56  *  - implemented missing BLK* ioctls
57  *
58  *  Things left to do:
59  *   - Formatting
60  *   - Maybe a better strategy for disk change detection (does anyone
61  *     know one?)
62  */
63 
64 #include <linux/module.h>
65 
66 #include <linux/fd.h>
67 #include <linux/delay.h>
68 #include <linux/init.h>
69 #include <linux/blk-mq.h>
70 #include <linux/mutex.h>
71 #include <linux/completion.h>
72 #include <linux/wait.h>
73 
74 #include <asm/atariints.h>
75 #include <asm/atari_stdma.h>
76 #include <asm/atari_stram.h>
77 
78 #define	FD_MAX_UNITS 2
79 
80 #undef DEBUG
81 
82 static DEFINE_MUTEX(ataflop_mutex);
83 static struct request *fd_request;
84 
85 /*
86  * WD1772 stuff
87  */
88 
89 /* register codes */
90 
91 #define FDCSELREG_STP   (0x80)   /* command/status register */
92 #define FDCSELREG_TRA   (0x82)   /* track register */
93 #define FDCSELREG_SEC   (0x84)   /* sector register */
94 #define FDCSELREG_DTA   (0x86)   /* data register */
95 
96 /* register names for FDC_READ/WRITE macros */
97 
98 #define FDCREG_CMD		0
99 #define FDCREG_STATUS	0
100 #define FDCREG_TRACK	2
101 #define FDCREG_SECTOR	4
102 #define FDCREG_DATA		6
103 
104 /* command opcodes */
105 
106 #define FDCCMD_RESTORE  (0x00)   /*  -                   */
107 #define FDCCMD_SEEK     (0x10)   /*   |                  */
108 #define FDCCMD_STEP     (0x20)   /*   |  TYP 1 Commands  */
109 #define FDCCMD_STIN     (0x40)   /*   |                  */
110 #define FDCCMD_STOT     (0x60)   /*  -                   */
111 #define FDCCMD_RDSEC    (0x80)   /*  -   TYP 2 Commands  */
112 #define FDCCMD_WRSEC    (0xa0)   /*  -          "        */
113 #define FDCCMD_RDADR    (0xc0)   /*  -                   */
114 #define FDCCMD_RDTRA    (0xe0)   /*   |  TYP 3 Commands  */
115 #define FDCCMD_WRTRA    (0xf0)   /*  -                   */
116 #define FDCCMD_FORCI    (0xd0)   /*  -   TYP 4 Command   */
117 
118 /* command modifier bits */
119 
120 #define FDCCMDADD_SR6   (0x00)   /* step rate settings */
121 #define FDCCMDADD_SR12  (0x01)
122 #define FDCCMDADD_SR2   (0x02)
123 #define FDCCMDADD_SR3   (0x03)
124 #define FDCCMDADD_V     (0x04)   /* verify */
125 #define FDCCMDADD_H     (0x08)   /* wait for spin-up */
126 #define FDCCMDADD_U     (0x10)   /* update track register */
127 #define FDCCMDADD_M     (0x10)   /* multiple sector access */
128 #define FDCCMDADD_E     (0x04)   /* head settling flag */
129 #define FDCCMDADD_P     (0x02)   /* precompensation off */
130 #define FDCCMDADD_A0    (0x01)   /* DAM flag */
131 
132 /* status register bits */
133 
134 #define	FDCSTAT_MOTORON	(0x80)   /* motor on */
135 #define	FDCSTAT_WPROT	(0x40)   /* write protected (FDCCMD_WR*) */
136 #define	FDCSTAT_SPINUP	(0x20)   /* motor speed stable (Type I) */
137 #define	FDCSTAT_DELDAM	(0x20)   /* sector has deleted DAM (Type II+III) */
138 #define	FDCSTAT_RECNF	(0x10)   /* record not found */
139 #define	FDCSTAT_CRC		(0x08)   /* CRC error */
140 #define	FDCSTAT_TR00	(0x04)   /* Track 00 flag (Type I) */
141 #define	FDCSTAT_LOST	(0x04)   /* Lost Data (Type II+III) */
142 #define	FDCSTAT_IDX		(0x02)   /* Index status (Type I) */
143 #define	FDCSTAT_DRQ		(0x02)   /* DRQ status (Type II+III) */
144 #define	FDCSTAT_BUSY	(0x01)   /* FDC is busy */
145 
146 
147 /* PSG Port A Bit Nr 0 .. Side Sel .. 0 -> Side 1  1 -> Side 2 */
148 #define DSKSIDE     (0x01)
149 
150 #define DSKDRVNONE  (0x06)
151 #define DSKDRV0     (0x02)
152 #define DSKDRV1     (0x04)
153 
154 /* step rates */
155 #define	FDCSTEP_6	0x00
156 #define	FDCSTEP_12	0x01
157 #define	FDCSTEP_2	0x02
158 #define	FDCSTEP_3	0x03
159 
160 struct atari_format_descr {
161 	int track;		/* to be formatted */
162 	int head;		/*   ""     ""     */
163 	int sect_offset;	/* offset of first sector */
164 };
165 
166 /* Disk types: DD, HD, ED */
167 static struct atari_disk_type {
168 	const char	*name;
169 	unsigned	spt;		/* sectors per track */
170 	unsigned	blocks;		/* total number of blocks */
171 	unsigned	fdc_speed;	/* fdc_speed setting */
172 	unsigned 	stretch;	/* track doubling ? */
173 } atari_disk_type[] = {
174 	{ "d360",  9, 720, 0, 0},	/*  0: 360kB diskette */
175 	{ "D360",  9, 720, 0, 1},	/*  1: 360kb in 720k or 1.2MB drive */
176 	{ "D720",  9,1440, 0, 0},	/*  2: 720kb in 720k or 1.2MB drive */
177 	{ "D820", 10,1640, 0, 0},	/*  3: DD disk with 82 tracks/10 sectors */
178 /* formats above are probed for type DD */
179 #define	MAX_TYPE_DD 3
180 	{ "h1200",15,2400, 3, 0},	/*  4: 1.2MB diskette */
181 	{ "H1440",18,2880, 3, 0},	/*  5: 1.4 MB diskette (HD) */
182 	{ "H1640",20,3280, 3, 0},	/*  6: 1.64MB diskette (fat HD) 82 tr 20 sec */
183 /* formats above are probed for types DD and HD */
184 #define	MAX_TYPE_HD 6
185 	{ "E2880",36,5760, 3, 0},	/*  7: 2.8 MB diskette (ED) */
186 	{ "E3280",40,6560, 3, 0},	/*  8: 3.2 MB diskette (fat ED) 82 tr 40 sec */
187 /* formats above are probed for types DD, HD and ED */
188 #define	MAX_TYPE_ED 8
189 /* types below are never autoprobed */
190 	{ "H1680",21,3360, 3, 0},	/*  9: 1.68MB diskette (fat HD) 80 tr 21 sec */
191 	{ "h410",10,820, 0, 1},		/* 10: 410k diskette 41 tr 10 sec, stretch */
192 	{ "h1476",18,2952, 3, 0},	/* 11: 1.48MB diskette 82 tr 18 sec */
193 	{ "H1722",21,3444, 3, 0},	/* 12: 1.72MB diskette 82 tr 21 sec */
194 	{ "h420",10,840, 0, 1},		/* 13: 420k diskette 42 tr 10 sec, stretch */
195 	{ "H830",10,1660, 0, 0},	/* 14: 820k diskette 83 tr 10 sec */
196 	{ "h1494",18,2952, 3, 0},	/* 15: 1.49MB diskette 83 tr 18 sec */
197 	{ "H1743",21,3486, 3, 0},	/* 16: 1.74MB diskette 83 tr 21 sec */
198 	{ "h880",11,1760, 0, 0},	/* 17: 880k diskette 80 tr 11 sec */
199 	{ "D1040",13,2080, 0, 0},	/* 18: 1.04MB diskette 80 tr 13 sec */
200 	{ "D1120",14,2240, 0, 0},	/* 19: 1.12MB diskette 80 tr 14 sec */
201 	{ "h1600",20,3200, 3, 0},	/* 20: 1.60MB diskette 80 tr 20 sec */
202 	{ "H1760",22,3520, 3, 0},	/* 21: 1.76MB diskette 80 tr 22 sec */
203 	{ "H1920",24,3840, 3, 0},	/* 22: 1.92MB diskette 80 tr 24 sec */
204 	{ "E3200",40,6400, 3, 0},	/* 23: 3.2MB diskette 80 tr 40 sec */
205 	{ "E3520",44,7040, 3, 0},	/* 24: 3.52MB diskette 80 tr 44 sec */
206 	{ "E3840",48,7680, 3, 0},	/* 25: 3.84MB diskette 80 tr 48 sec */
207 	{ "H1840",23,3680, 3, 0},	/* 26: 1.84MB diskette 80 tr 23 sec */
208 	{ "D800",10,1600, 0, 0},	/* 27: 800k diskette 80 tr 10 sec */
209 };
210 
211 static int StartDiskType[] = {
212 	MAX_TYPE_DD,
213 	MAX_TYPE_HD,
214 	MAX_TYPE_ED
215 };
216 
217 #define	TYPE_DD		0
218 #define	TYPE_HD		1
219 #define	TYPE_ED		2
220 
221 static int DriveType = TYPE_HD;
222 
223 static DEFINE_SPINLOCK(ataflop_lock);
224 
225 /* Array for translating minors into disk formats */
226 static struct {
227 	int 	 index;
228 	unsigned drive_types;
229 } minor2disktype[] = {
230 	{  0, TYPE_DD },	/*  1: d360 */
231 	{  4, TYPE_HD },	/*  2: h1200 */
232 	{  1, TYPE_DD },	/*  3: D360 */
233 	{  2, TYPE_DD },	/*  4: D720 */
234 	{  1, TYPE_DD },	/*  5: h360 = D360 */
235 	{  2, TYPE_DD },	/*  6: h720 = D720 */
236 	{  5, TYPE_HD },	/*  7: H1440 */
237 	{  7, TYPE_ED },	/*  8: E2880 */
238 /* some PC formats :-) */
239 	{  8, TYPE_ED },	/*  9: E3280    <- was "CompaQ" == E2880 for PC */
240 	{  5, TYPE_HD },	/* 10: h1440 = H1440 */
241 	{  9, TYPE_HD },	/* 11: H1680 */
242 	{ 10, TYPE_DD },	/* 12: h410  */
243 	{  3, TYPE_DD },	/* 13: H820     <- == D820, 82x10 */
244 	{ 11, TYPE_HD },	/* 14: h1476 */
245 	{ 12, TYPE_HD },	/* 15: H1722 */
246 	{ 13, TYPE_DD },	/* 16: h420  */
247 	{ 14, TYPE_DD },	/* 17: H830  */
248 	{ 15, TYPE_HD },	/* 18: h1494 */
249 	{ 16, TYPE_HD },	/* 19: H1743 */
250 	{ 17, TYPE_DD },	/* 20: h880  */
251 	{ 18, TYPE_DD },	/* 21: D1040 */
252 	{ 19, TYPE_DD },	/* 22: D1120 */
253 	{ 20, TYPE_HD },	/* 23: h1600 */
254 	{ 21, TYPE_HD },	/* 24: H1760 */
255 	{ 22, TYPE_HD },	/* 25: H1920 */
256 	{ 23, TYPE_ED },	/* 26: E3200 */
257 	{ 24, TYPE_ED },	/* 27: E3520 */
258 	{ 25, TYPE_ED },	/* 28: E3840 */
259 	{ 26, TYPE_HD },	/* 29: H1840 */
260 	{ 27, TYPE_DD },	/* 30: D800  */
261 	{  6, TYPE_HD },	/* 31: H1640    <- was H1600 == h1600 for PC */
262 };
263 
264 #define NUM_DISK_MINORS ARRAY_SIZE(minor2disktype)
265 
266 /*
267  * Maximum disk size (in kilobytes). This default is used whenever the
268  * current disk size is unknown.
269  */
270 #define MAX_DISK_SIZE 3280
271 
272 /*
273  * MSch: User-provided type information. 'drive' points to
274  * the respective entry of this array. Set by FDSETPRM ioctls.
275  */
276 static struct atari_disk_type user_params[FD_MAX_UNITS];
277 
278 /*
279  * User-provided permanent type information. 'drive' points to
280  * the respective entry of this array.  Set by FDDEFPRM ioctls,
281  * restored upon disk change by floppy_revalidate() if valid (as seen by
282  * default_params[].blocks > 0 - a bit in unit[].flags might be used for this?)
283  */
284 static struct atari_disk_type default_params[FD_MAX_UNITS];
285 
286 /* current info on each unit */
287 static struct atari_floppy_struct {
288 	int connected;				/* !=0 : drive is connected */
289 	int autoprobe;				/* !=0 : do autoprobe	    */
290 
291 	struct atari_disk_type	*disktype;	/* current type of disk */
292 
293 	int track;		/* current head position or -1 if
294 				   unknown */
295 	unsigned int steprate;	/* steprate setting */
296 	unsigned int wpstat;	/* current state of WP signal (for
297 				   disk change detection) */
298 	int flags;		/* flags */
299 	struct gendisk *disk;
300 	int ref;
301 	int type;
302 	struct blk_mq_tag_set tag_set;
303 } unit[FD_MAX_UNITS];
304 
305 #define	UD	unit[drive]
306 #define	UDT	unit[drive].disktype
307 #define	SUD	unit[SelectedDrive]
308 #define	SUDT	unit[SelectedDrive].disktype
309 
310 
311 #define FDC_READ(reg) ({			\
312     /* unsigned long __flags; */		\
313     unsigned short __val;			\
314     /* local_irq_save(__flags); */		\
315     dma_wd.dma_mode_status = 0x80 | (reg);	\
316     udelay(25);					\
317     __val = dma_wd.fdc_acces_seccount;		\
318     MFPDELAY();					\
319     /* local_irq_restore(__flags); */		\
320     __val & 0xff;				\
321 })
322 
323 #define FDC_WRITE(reg,val)			\
324     do {					\
325 	/* unsigned long __flags; */		\
326 	/* local_irq_save(__flags); */		\
327 	dma_wd.dma_mode_status = 0x80 | (reg);	\
328 	udelay(25);				\
329 	dma_wd.fdc_acces_seccount = (val);	\
330 	MFPDELAY();				\
331         /* local_irq_restore(__flags); */	\
332     } while(0)
333 
334 
335 /* Buffering variables:
336  * First, there is a DMA buffer in ST-RAM that is used for floppy DMA
337  * operations. Second, a track buffer is used to cache a whole track
338  * of the disk to save read operations. These are two separate buffers
339  * because that allows write operations without clearing the track buffer.
340  */
341 
342 static int MaxSectors[] = {
343 	11, 22, 44
344 };
345 static int BufferSize[] = {
346 	15*512, 30*512, 60*512
347 };
348 
349 #define	BUFFER_SIZE	(BufferSize[DriveType])
350 
351 unsigned char *DMABuffer;			  /* buffer for writes */
352 static unsigned long PhysDMABuffer;   /* physical address */
353 
354 static int UseTrackbuffer = -1;		  /* Do track buffering? */
355 module_param(UseTrackbuffer, int, 0);
356 
357 unsigned char *TrackBuffer;			  /* buffer for reads */
358 static unsigned long PhysTrackBuffer; /* physical address */
359 static int BufferDrive, BufferSide, BufferTrack;
360 static int read_track;		/* non-zero if we are reading whole tracks */
361 
362 #define	SECTOR_BUFFER(sec)	(TrackBuffer + ((sec)-1)*512)
363 #define	IS_BUFFERED(drive,side,track) \
364     (BufferDrive == (drive) && BufferSide == (side) && BufferTrack == (track))
365 
366 /*
367  * These are global variables, as that's the easiest way to give
368  * information to interrupts. They are the data used for the current
369  * request.
370  */
371 static int SelectedDrive = 0;
372 static int ReqCmd, ReqBlock;
373 static int ReqSide, ReqTrack, ReqSector, ReqCnt;
374 static int HeadSettleFlag = 0;
375 static unsigned char *ReqData, *ReqBuffer;
376 static int MotorOn = 0, MotorOffTrys;
377 static int IsFormatting = 0, FormatError;
378 
379 static int UserSteprate[FD_MAX_UNITS] = { -1, -1 };
380 module_param_array(UserSteprate, int, NULL, 0);
381 
382 static DECLARE_COMPLETION(format_wait);
383 
384 static unsigned long changed_floppies = 0xff, fake_change = 0;
385 #define	CHECK_CHANGE_DELAY	HZ/2
386 
387 #define	FD_MOTOR_OFF_DELAY	(3*HZ)
388 #define	FD_MOTOR_OFF_MAXTRY	(10*20)
389 
390 #define FLOPPY_TIMEOUT		(6*HZ)
391 #define RECALIBRATE_ERRORS	4	/* After this many errors the drive
392 					 * will be recalibrated. */
393 #define MAX_ERRORS		8	/* After this many errors the driver
394 					 * will give up. */
395 
396 
397 /*
398  * The driver is trying to determine the correct media format
399  * while Probing is set. fd_rwsec_done() clears it after a
400  * successful access.
401  */
402 static int Probing = 0;
403 
404 /* This flag is set when a dummy seek is necessary to make the WP
405  * status bit accessible.
406  */
407 static int NeedSeek = 0;
408 
409 
410 #ifdef DEBUG
411 #define DPRINT(a)	printk a
412 #else
413 #define DPRINT(a)
414 #endif
415 
416 /***************************** Prototypes *****************************/
417 
418 static void fd_select_side( int side );
419 static void fd_select_drive( int drive );
420 static void fd_deselect( void );
421 static void fd_motor_off_timer(struct timer_list *unused);
422 static void check_change(struct timer_list *unused);
423 static irqreturn_t floppy_irq (int irq, void *dummy);
424 static void fd_error( void );
425 static int do_format(int drive, int type, struct atari_format_descr *desc);
426 static void do_fd_action( int drive );
427 static void fd_calibrate( void );
428 static void fd_calibrate_done( int status );
429 static void fd_seek( void );
430 static void fd_seek_done( int status );
431 static void fd_rwsec( void );
432 static void fd_readtrack_check(struct timer_list *unused);
433 static void fd_rwsec_done( int status );
434 static void fd_rwsec_done1(int status);
435 static void fd_writetrack( void );
436 static void fd_writetrack_done( int status );
437 static void fd_times_out(struct timer_list *unused);
438 static void finish_fdc( void );
439 static void finish_fdc_done( int dummy );
440 static void setup_req_params( int drive );
441 static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int
442                      cmd, unsigned long param);
443 static void fd_probe( int drive );
444 static int fd_test_drive_present( int drive );
445 static void config_types( void );
446 static int floppy_open(struct block_device *bdev, fmode_t mode);
447 static void floppy_release(struct gendisk *disk, fmode_t mode);
448 
449 /************************* End of Prototypes **************************/
450 
451 static DEFINE_TIMER(motor_off_timer, fd_motor_off_timer);
452 static DEFINE_TIMER(readtrack_timer, fd_readtrack_check);
453 static DEFINE_TIMER(timeout_timer, fd_times_out);
454 static DEFINE_TIMER(fd_timer, check_change);
455 
456 static void fd_end_request_cur(blk_status_t err)
457 {
458 	if (!blk_update_request(fd_request, err,
459 				blk_rq_cur_bytes(fd_request))) {
460 		__blk_mq_end_request(fd_request, err);
461 		fd_request = NULL;
462 	}
463 }
464 
465 static inline void start_motor_off_timer(void)
466 {
467 	mod_timer(&motor_off_timer, jiffies + FD_MOTOR_OFF_DELAY);
468 	MotorOffTrys = 0;
469 }
470 
471 static inline void start_check_change_timer( void )
472 {
473 	mod_timer(&fd_timer, jiffies + CHECK_CHANGE_DELAY);
474 }
475 
476 static inline void start_timeout(void)
477 {
478 	mod_timer(&timeout_timer, jiffies + FLOPPY_TIMEOUT);
479 }
480 
481 static inline void stop_timeout(void)
482 {
483 	del_timer(&timeout_timer);
484 }
485 
486 /* Select the side to use. */
487 
488 static void fd_select_side( int side )
489 {
490 	unsigned long flags;
491 
492 	/* protect against various other ints mucking around with the PSG */
493 	local_irq_save(flags);
494 
495 	sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
496 	sound_ym.wd_data = (side == 0) ? sound_ym.rd_data_reg_sel | 0x01 :
497 	                                 sound_ym.rd_data_reg_sel & 0xfe;
498 
499 	local_irq_restore(flags);
500 }
501 
502 
503 /* Select a drive, update the FDC's track register and set the correct
504  * clock speed for this disk's type.
505  */
506 
507 static void fd_select_drive( int drive )
508 {
509 	unsigned long flags;
510 	unsigned char tmp;
511 
512 	if (drive == SelectedDrive)
513 	  return;
514 
515 	/* protect against various other ints mucking around with the PSG */
516 	local_irq_save(flags);
517 	sound_ym.rd_data_reg_sel = 14; /* Select PSG Port A */
518 	tmp = sound_ym.rd_data_reg_sel;
519 	sound_ym.wd_data = (tmp | DSKDRVNONE) & ~(drive == 0 ? DSKDRV0 : DSKDRV1);
520 	atari_dont_touch_floppy_select = 1;
521 	local_irq_restore(flags);
522 
523 	/* restore track register to saved value */
524 	FDC_WRITE( FDCREG_TRACK, UD.track );
525 	udelay(25);
526 
527 	/* select 8/16 MHz */
528 	if (UDT)
529 		if (ATARIHW_PRESENT(FDCSPEED))
530 			dma_wd.fdc_speed = UDT->fdc_speed;
531 
532 	SelectedDrive = drive;
533 }
534 
535 
536 /* Deselect both drives. */
537 
538 static void fd_deselect( void )
539 {
540 	unsigned long flags;
541 
542 	/* protect against various other ints mucking around with the PSG */
543 	local_irq_save(flags);
544 	atari_dont_touch_floppy_select = 0;
545 	sound_ym.rd_data_reg_sel=14;	/* Select PSG Port A */
546 	sound_ym.wd_data = (sound_ym.rd_data_reg_sel |
547 			    (MACH_IS_FALCON ? 3 : 7)); /* no drives selected */
548 	/* On Falcon, the drive B select line is used on the printer port, so
549 	 * leave it alone... */
550 	SelectedDrive = -1;
551 	local_irq_restore(flags);
552 }
553 
554 
555 /* This timer function deselects the drives when the FDC switched the
556  * motor off. The deselection cannot happen earlier because the FDC
557  * counts the index signals, which arrive only if one drive is selected.
558  */
559 
560 static void fd_motor_off_timer(struct timer_list *unused)
561 {
562 	unsigned char status;
563 
564 	if (SelectedDrive < 0)
565 		/* no drive selected, needn't deselect anyone */
566 		return;
567 
568 	if (stdma_islocked())
569 		goto retry;
570 
571 	status = FDC_READ( FDCREG_STATUS );
572 
573 	if (!(status & 0x80)) {
574 		/* motor already turned off by FDC -> deselect drives */
575 		MotorOn = 0;
576 		fd_deselect();
577 		return;
578 	}
579 	/* not yet off, try again */
580 
581   retry:
582 	/* Test again later; if tested too often, it seems there is no disk
583 	 * in the drive and the FDC will leave the motor on forever (or,
584 	 * at least until a disk is inserted). So we'll test only twice
585 	 * per second from then on...
586 	 */
587 	mod_timer(&motor_off_timer,
588 		  jiffies + (MotorOffTrys++ < FD_MOTOR_OFF_MAXTRY ? HZ/20 : HZ/2));
589 }
590 
591 
592 /* This function is repeatedly called to detect disk changes (as good
593  * as possible) and keep track of the current state of the write protection.
594  */
595 
596 static void check_change(struct timer_list *unused)
597 {
598 	static int    drive = 0;
599 
600 	unsigned long flags;
601 	unsigned char old_porta;
602 	int			  stat;
603 
604 	if (++drive > 1 || !UD.connected)
605 		drive = 0;
606 
607 	/* protect against various other ints mucking around with the PSG */
608 	local_irq_save(flags);
609 
610 	if (!stdma_islocked()) {
611 		sound_ym.rd_data_reg_sel = 14;
612 		old_porta = sound_ym.rd_data_reg_sel;
613 		sound_ym.wd_data = (old_porta | DSKDRVNONE) &
614 			               ~(drive == 0 ? DSKDRV0 : DSKDRV1);
615 		stat = !!(FDC_READ( FDCREG_STATUS ) & FDCSTAT_WPROT);
616 		sound_ym.wd_data = old_porta;
617 
618 		if (stat != UD.wpstat) {
619 			DPRINT(( "wpstat[%d] = %d\n", drive, stat ));
620 			UD.wpstat = stat;
621 			set_bit (drive, &changed_floppies);
622 		}
623 	}
624 	local_irq_restore(flags);
625 
626 	start_check_change_timer();
627 }
628 
629 
630 /* Handling of the Head Settling Flag: This flag should be set after each
631  * seek operation, because we don't use seeks with verify.
632  */
633 
634 static inline void set_head_settle_flag(void)
635 {
636 	HeadSettleFlag = FDCCMDADD_E;
637 }
638 
639 static inline int get_head_settle_flag(void)
640 {
641 	int	tmp = HeadSettleFlag;
642 	HeadSettleFlag = 0;
643 	return( tmp );
644 }
645 
646 static inline void copy_buffer(void *from, void *to)
647 {
648 	ulong *p1 = (ulong *)from, *p2 = (ulong *)to;
649 	int cnt;
650 
651 	for (cnt = 512/4; cnt; cnt--)
652 		*p2++ = *p1++;
653 }
654 
655 
656 
657 
658 /* General Interrupt Handling */
659 
660 static void (*FloppyIRQHandler)( int status ) = NULL;
661 
662 static irqreturn_t floppy_irq (int irq, void *dummy)
663 {
664 	unsigned char status;
665 	void (*handler)( int );
666 
667 	handler = xchg(&FloppyIRQHandler, NULL);
668 
669 	if (handler) {
670 		nop();
671 		status = FDC_READ( FDCREG_STATUS );
672 		DPRINT(("FDC irq, status = %02x handler = %08lx\n",status,(unsigned long)handler));
673 		handler( status );
674 	}
675 	else {
676 		DPRINT(("FDC irq, no handler\n"));
677 	}
678 	return IRQ_HANDLED;
679 }
680 
681 
682 /* Error handling: If some error happened, retry some times, then
683  * recalibrate, then try again, and fail after MAX_ERRORS.
684  */
685 
686 static void fd_error( void )
687 {
688 	if (IsFormatting) {
689 		IsFormatting = 0;
690 		FormatError = 1;
691 		complete(&format_wait);
692 		return;
693 	}
694 
695 	if (!fd_request)
696 		return;
697 
698 	fd_request->error_count++;
699 	if (fd_request->error_count >= MAX_ERRORS) {
700 		printk(KERN_ERR "fd%d: too many errors.\n", SelectedDrive );
701 		fd_end_request_cur(BLK_STS_IOERR);
702 	}
703 	else if (fd_request->error_count == RECALIBRATE_ERRORS) {
704 		printk(KERN_WARNING "fd%d: recalibrating\n", SelectedDrive );
705 		if (SelectedDrive != -1)
706 			SUD.track = -1;
707 	}
708 }
709 
710 
711 
712 #define	SET_IRQ_HANDLER(proc) do { FloppyIRQHandler = (proc); } while(0)
713 
714 
715 /* ---------- Formatting ---------- */
716 
717 #define FILL(n,val)		\
718     do {			\
719 	memset( p, val, n );	\
720 	p += n;			\
721     } while(0)
722 
723 static int do_format(int drive, int type, struct atari_format_descr *desc)
724 {
725 	struct request_queue *q = unit[drive].disk->queue;
726 	unsigned char	*p;
727 	int sect, nsect;
728 	unsigned long	flags;
729 	int ret;
730 
731 	blk_mq_freeze_queue(q);
732 	blk_mq_quiesce_queue(q);
733 
734 	local_irq_save(flags);
735 	stdma_lock(floppy_irq, NULL);
736 	atari_turnon_irq( IRQ_MFP_FDC ); /* should be already, just to be sure */
737 	local_irq_restore(flags);
738 
739 	if (type) {
740 		if (--type >= NUM_DISK_MINORS ||
741 		    minor2disktype[type].drive_types > DriveType) {
742 			ret = -EINVAL;
743 			goto out;
744 		}
745 		type = minor2disktype[type].index;
746 		UDT = &atari_disk_type[type];
747 	}
748 
749 	if (!UDT || desc->track >= UDT->blocks/UDT->spt/2 || desc->head >= 2) {
750 		ret = -EINVAL;
751 		goto out;
752 	}
753 
754 	nsect = UDT->spt;
755 	p = TrackBuffer;
756 	/* The track buffer is used for the raw track data, so its
757 	   contents become invalid! */
758 	BufferDrive = -1;
759 	/* stop deselect timer */
760 	del_timer( &motor_off_timer );
761 
762 	FILL( 60 * (nsect / 9), 0x4e );
763 	for( sect = 0; sect < nsect; ++sect ) {
764 		FILL( 12, 0 );
765 		FILL( 3, 0xf5 );
766 		*p++ = 0xfe;
767 		*p++ = desc->track;
768 		*p++ = desc->head;
769 		*p++ = (nsect + sect - desc->sect_offset) % nsect + 1;
770 		*p++ = 2;
771 		*p++ = 0xf7;
772 		FILL( 22, 0x4e );
773 		FILL( 12, 0 );
774 		FILL( 3, 0xf5 );
775 		*p++ = 0xfb;
776 		FILL( 512, 0xe5 );
777 		*p++ = 0xf7;
778 		FILL( 40, 0x4e );
779 	}
780 	FILL( TrackBuffer+BUFFER_SIZE-p, 0x4e );
781 
782 	IsFormatting = 1;
783 	FormatError = 0;
784 	ReqTrack = desc->track;
785 	ReqSide  = desc->head;
786 	do_fd_action( drive );
787 
788 	wait_for_completion(&format_wait);
789 
790 	ret = FormatError ? -EIO : 0;
791 out:
792 	blk_mq_unquiesce_queue(q);
793 	blk_mq_unfreeze_queue(q);
794 	return ret;
795 }
796 
797 
798 /* do_fd_action() is the general procedure for a fd request: All
799  * required parameter settings (drive select, side select, track
800  * position) are checked and set if needed. For each of these
801  * parameters and the actual reading or writing exist two functions:
802  * one that starts the setting (or skips it if possible) and one
803  * callback for the "done" interrupt. Each done func calls the next
804  * set function to propagate the request down to fd_rwsec_done().
805  */
806 
807 static void do_fd_action( int drive )
808 {
809 	DPRINT(("do_fd_action\n"));
810 
811 	if (UseTrackbuffer && !IsFormatting) {
812 	repeat:
813 	    if (IS_BUFFERED( drive, ReqSide, ReqTrack )) {
814 		if (ReqCmd == READ) {
815 		    copy_buffer( SECTOR_BUFFER(ReqSector), ReqData );
816 		    if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
817 			/* read next sector */
818 			setup_req_params( drive );
819 			goto repeat;
820 		    }
821 		    else {
822 			/* all sectors finished */
823 			fd_end_request_cur(BLK_STS_OK);
824 			return;
825 		    }
826 		}
827 		else {
828 		    /* cmd == WRITE, pay attention to track buffer
829 		     * consistency! */
830 		    copy_buffer( ReqData, SECTOR_BUFFER(ReqSector) );
831 		}
832 	    }
833 	}
834 
835 	if (SelectedDrive != drive)
836 		fd_select_drive( drive );
837 
838 	if (UD.track == -1)
839 		fd_calibrate();
840 	else if (UD.track != ReqTrack << UDT->stretch)
841 		fd_seek();
842 	else if (IsFormatting)
843 		fd_writetrack();
844 	else
845 		fd_rwsec();
846 }
847 
848 
849 /* Seek to track 0 if the current track is unknown */
850 
851 static void fd_calibrate( void )
852 {
853 	if (SUD.track >= 0) {
854 		fd_calibrate_done( 0 );
855 		return;
856 	}
857 
858 	if (ATARIHW_PRESENT(FDCSPEED))
859 		dma_wd.fdc_speed = 0; 	/* always seek with 8 Mhz */;
860 	DPRINT(("fd_calibrate\n"));
861 	SET_IRQ_HANDLER( fd_calibrate_done );
862 	/* we can't verify, since the speed may be incorrect */
863 	FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | SUD.steprate );
864 
865 	NeedSeek = 1;
866 	MotorOn = 1;
867 	start_timeout();
868 	/* wait for IRQ */
869 }
870 
871 
872 static void fd_calibrate_done( int status )
873 {
874 	DPRINT(("fd_calibrate_done()\n"));
875 	stop_timeout();
876 
877 	/* set the correct speed now */
878 	if (ATARIHW_PRESENT(FDCSPEED))
879 		dma_wd.fdc_speed = SUDT->fdc_speed;
880 	if (status & FDCSTAT_RECNF) {
881 		printk(KERN_ERR "fd%d: restore failed\n", SelectedDrive );
882 		fd_error();
883 	}
884 	else {
885 		SUD.track = 0;
886 		fd_seek();
887 	}
888 }
889 
890 
891 /* Seek the drive to the requested track. The drive must have been
892  * calibrated at some point before this.
893  */
894 
895 static void fd_seek( void )
896 {
897 	if (SUD.track == ReqTrack << SUDT->stretch) {
898 		fd_seek_done( 0 );
899 		return;
900 	}
901 
902 	if (ATARIHW_PRESENT(FDCSPEED)) {
903 		dma_wd.fdc_speed = 0;	/* always seek witch 8 Mhz */
904 		MFPDELAY();
905 	}
906 
907 	DPRINT(("fd_seek() to track %d\n",ReqTrack));
908 	FDC_WRITE( FDCREG_DATA, ReqTrack << SUDT->stretch);
909 	udelay(25);
910 	SET_IRQ_HANDLER( fd_seek_done );
911 	FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK | SUD.steprate );
912 
913 	MotorOn = 1;
914 	set_head_settle_flag();
915 	start_timeout();
916 	/* wait for IRQ */
917 }
918 
919 
920 static void fd_seek_done( int status )
921 {
922 	DPRINT(("fd_seek_done()\n"));
923 	stop_timeout();
924 
925 	/* set the correct speed */
926 	if (ATARIHW_PRESENT(FDCSPEED))
927 		dma_wd.fdc_speed = SUDT->fdc_speed;
928 	if (status & FDCSTAT_RECNF) {
929 		printk(KERN_ERR "fd%d: seek error (to track %d)\n",
930 				SelectedDrive, ReqTrack );
931 		/* we don't know exactly which track we are on now! */
932 		SUD.track = -1;
933 		fd_error();
934 	}
935 	else {
936 		SUD.track = ReqTrack << SUDT->stretch;
937 		NeedSeek = 0;
938 		if (IsFormatting)
939 			fd_writetrack();
940 		else
941 			fd_rwsec();
942 	}
943 }
944 
945 
946 /* This does the actual reading/writing after positioning the head
947  * over the correct track.
948  */
949 
950 static int MultReadInProgress = 0;
951 
952 
953 static void fd_rwsec( void )
954 {
955 	unsigned long paddr, flags;
956 	unsigned int  rwflag, old_motoron;
957 	unsigned int track;
958 
959 	DPRINT(("fd_rwsec(), Sec=%d, Access=%c\n",ReqSector, ReqCmd == WRITE ? 'w' : 'r' ));
960 	if (ReqCmd == WRITE) {
961 		if (ATARIHW_PRESENT(EXTD_DMA)) {
962 			paddr = virt_to_phys(ReqData);
963 		}
964 		else {
965 			copy_buffer( ReqData, DMABuffer );
966 			paddr = PhysDMABuffer;
967 		}
968 		dma_cache_maintenance( paddr, 512, 1 );
969 		rwflag = 0x100;
970 	}
971 	else {
972 		if (read_track)
973 			paddr = PhysTrackBuffer;
974 		else
975 			paddr = ATARIHW_PRESENT(EXTD_DMA) ?
976 				virt_to_phys(ReqData) : PhysDMABuffer;
977 		rwflag = 0;
978 	}
979 
980 	fd_select_side( ReqSide );
981 
982 	/* Start sector of this operation */
983 	FDC_WRITE( FDCREG_SECTOR, read_track ? 1 : ReqSector );
984 	MFPDELAY();
985 	/* Cheat for track if stretch != 0 */
986 	if (SUDT->stretch) {
987 		track = FDC_READ( FDCREG_TRACK);
988 		MFPDELAY();
989 		FDC_WRITE( FDCREG_TRACK, track >> SUDT->stretch);
990 	}
991 	udelay(25);
992 
993 	/* Setup DMA */
994 	local_irq_save(flags);
995 	dma_wd.dma_lo = (unsigned char)paddr;
996 	MFPDELAY();
997 	paddr >>= 8;
998 	dma_wd.dma_md = (unsigned char)paddr;
999 	MFPDELAY();
1000 	paddr >>= 8;
1001 	if (ATARIHW_PRESENT(EXTD_DMA))
1002 		st_dma_ext_dmahi = (unsigned short)paddr;
1003 	else
1004 		dma_wd.dma_hi = (unsigned char)paddr;
1005 	MFPDELAY();
1006 	local_irq_restore(flags);
1007 
1008 	/* Clear FIFO and switch DMA to correct mode */
1009 	dma_wd.dma_mode_status = 0x90 | rwflag;
1010 	MFPDELAY();
1011 	dma_wd.dma_mode_status = 0x90 | (rwflag ^ 0x100);
1012 	MFPDELAY();
1013 	dma_wd.dma_mode_status = 0x90 | rwflag;
1014 	MFPDELAY();
1015 
1016 	/* How many sectors for DMA */
1017 	dma_wd.fdc_acces_seccount = read_track ? SUDT->spt : 1;
1018 
1019 	udelay(25);
1020 
1021 	/* Start operation */
1022 	dma_wd.dma_mode_status = FDCSELREG_STP | rwflag;
1023 	udelay(25);
1024 	SET_IRQ_HANDLER( fd_rwsec_done );
1025 	dma_wd.fdc_acces_seccount =
1026 	  (get_head_settle_flag() |
1027 	   (rwflag ? FDCCMD_WRSEC : (FDCCMD_RDSEC | (read_track ? FDCCMDADD_M : 0))));
1028 
1029 	old_motoron = MotorOn;
1030 	MotorOn = 1;
1031 	NeedSeek = 1;
1032 	/* wait for interrupt */
1033 
1034 	if (read_track) {
1035 		/* If reading a whole track, wait about one disk rotation and
1036 		 * then check if all sectors are read. The FDC will even
1037 		 * search for the first non-existent sector and need 1 sec to
1038 		 * recognise that it isn't present :-(
1039 		 */
1040 		MultReadInProgress = 1;
1041 		mod_timer(&readtrack_timer,
1042 			  /* 1 rot. + 5 rot.s if motor was off  */
1043 			  jiffies + HZ/5 + (old_motoron ? 0 : HZ));
1044 	}
1045 	start_timeout();
1046 }
1047 
1048 
1049 static void fd_readtrack_check(struct timer_list *unused)
1050 {
1051 	unsigned long flags, addr, addr2;
1052 
1053 	local_irq_save(flags);
1054 
1055 	if (!MultReadInProgress) {
1056 		/* This prevents a race condition that could arise if the
1057 		 * interrupt is triggered while the calling of this timer
1058 		 * callback function takes place. The IRQ function then has
1059 		 * already cleared 'MultReadInProgress'  when flow of control
1060 		 * gets here.
1061 		 */
1062 		local_irq_restore(flags);
1063 		return;
1064 	}
1065 
1066 	/* get the current DMA address */
1067 	/* ++ f.a. read twice to avoid being fooled by switcher */
1068 	addr = 0;
1069 	do {
1070 		addr2 = addr;
1071 		addr = dma_wd.dma_lo & 0xff;
1072 		MFPDELAY();
1073 		addr |= (dma_wd.dma_md & 0xff) << 8;
1074 		MFPDELAY();
1075 		if (ATARIHW_PRESENT( EXTD_DMA ))
1076 			addr |= (st_dma_ext_dmahi & 0xffff) << 16;
1077 		else
1078 			addr |= (dma_wd.dma_hi & 0xff) << 16;
1079 		MFPDELAY();
1080 	} while(addr != addr2);
1081 
1082 	if (addr >= PhysTrackBuffer + SUDT->spt*512) {
1083 		/* already read enough data, force an FDC interrupt to stop
1084 		 * the read operation
1085 		 */
1086 		SET_IRQ_HANDLER( NULL );
1087 		MultReadInProgress = 0;
1088 		local_irq_restore(flags);
1089 		DPRINT(("fd_readtrack_check(): done\n"));
1090 		FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1091 		udelay(25);
1092 
1093 		/* No error until now -- the FDC would have interrupted
1094 		 * otherwise!
1095 		 */
1096 		fd_rwsec_done1(0);
1097 	}
1098 	else {
1099 		/* not yet finished, wait another tenth rotation */
1100 		local_irq_restore(flags);
1101 		DPRINT(("fd_readtrack_check(): not yet finished\n"));
1102 		mod_timer(&readtrack_timer, jiffies + HZ/5/10);
1103 	}
1104 }
1105 
1106 
1107 static void fd_rwsec_done( int status )
1108 {
1109 	DPRINT(("fd_rwsec_done()\n"));
1110 
1111 	if (read_track) {
1112 		del_timer(&readtrack_timer);
1113 		if (!MultReadInProgress)
1114 			return;
1115 		MultReadInProgress = 0;
1116 	}
1117 	fd_rwsec_done1(status);
1118 }
1119 
1120 static void fd_rwsec_done1(int status)
1121 {
1122 	unsigned int track;
1123 
1124 	stop_timeout();
1125 
1126 	/* Correct the track if stretch != 0 */
1127 	if (SUDT->stretch) {
1128 		track = FDC_READ( FDCREG_TRACK);
1129 		MFPDELAY();
1130 		FDC_WRITE( FDCREG_TRACK, track << SUDT->stretch);
1131 	}
1132 
1133 	if (!UseTrackbuffer) {
1134 		dma_wd.dma_mode_status = 0x90;
1135 		MFPDELAY();
1136 		if (!(dma_wd.dma_mode_status & 0x01)) {
1137 			printk(KERN_ERR "fd%d: DMA error\n", SelectedDrive );
1138 			goto err_end;
1139 		}
1140 	}
1141 	MFPDELAY();
1142 
1143 	if (ReqCmd == WRITE && (status & FDCSTAT_WPROT)) {
1144 		printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1145 		goto err_end;
1146 	}
1147 	if ((status & FDCSTAT_RECNF) &&
1148 	    /* RECNF is no error after a multiple read when the FDC
1149 	       searched for a non-existent sector! */
1150 	    !(read_track && FDC_READ(FDCREG_SECTOR) > SUDT->spt)) {
1151 		if (Probing) {
1152 			if (SUDT > atari_disk_type) {
1153 			    if (SUDT[-1].blocks > ReqBlock) {
1154 				/* try another disk type */
1155 				SUDT--;
1156 				set_capacity(unit[SelectedDrive].disk,
1157 							SUDT->blocks);
1158 			    } else
1159 				Probing = 0;
1160 			}
1161 			else {
1162 				if (SUD.flags & FTD_MSG)
1163 					printk(KERN_INFO "fd%d: Auto-detected floppy type %s\n",
1164 					       SelectedDrive, SUDT->name );
1165 				Probing=0;
1166 			}
1167 		} else {
1168 /* record not found, but not probing. Maybe stretch wrong ? Restart probing */
1169 			if (SUD.autoprobe) {
1170 				SUDT = atari_disk_type + StartDiskType[DriveType];
1171 				set_capacity(unit[SelectedDrive].disk,
1172 							SUDT->blocks);
1173 				Probing = 1;
1174 			}
1175 		}
1176 		if (Probing) {
1177 			if (ATARIHW_PRESENT(FDCSPEED)) {
1178 				dma_wd.fdc_speed = SUDT->fdc_speed;
1179 				MFPDELAY();
1180 			}
1181 			setup_req_params( SelectedDrive );
1182 			BufferDrive = -1;
1183 			do_fd_action( SelectedDrive );
1184 			return;
1185 		}
1186 
1187 		printk(KERN_ERR "fd%d: sector %d not found (side %d, track %d)\n",
1188 		       SelectedDrive, FDC_READ (FDCREG_SECTOR), ReqSide, ReqTrack );
1189 		goto err_end;
1190 	}
1191 	if (status & FDCSTAT_CRC) {
1192 		printk(KERN_ERR "fd%d: CRC error (side %d, track %d, sector %d)\n",
1193 		       SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1194 		goto err_end;
1195 	}
1196 	if (status & FDCSTAT_LOST) {
1197 		printk(KERN_ERR "fd%d: lost data (side %d, track %d, sector %d)\n",
1198 		       SelectedDrive, ReqSide, ReqTrack, FDC_READ (FDCREG_SECTOR) );
1199 		goto err_end;
1200 	}
1201 
1202 	Probing = 0;
1203 
1204 	if (ReqCmd == READ) {
1205 		if (!read_track) {
1206 			void *addr;
1207 			addr = ATARIHW_PRESENT( EXTD_DMA ) ? ReqData : DMABuffer;
1208 			dma_cache_maintenance( virt_to_phys(addr), 512, 0 );
1209 			if (!ATARIHW_PRESENT( EXTD_DMA ))
1210 				copy_buffer (addr, ReqData);
1211 		} else {
1212 			dma_cache_maintenance( PhysTrackBuffer, MaxSectors[DriveType] * 512, 0 );
1213 			BufferDrive = SelectedDrive;
1214 			BufferSide  = ReqSide;
1215 			BufferTrack = ReqTrack;
1216 			copy_buffer (SECTOR_BUFFER (ReqSector), ReqData);
1217 		}
1218 	}
1219 
1220 	if (++ReqCnt < blk_rq_cur_sectors(fd_request)) {
1221 		/* read next sector */
1222 		setup_req_params( SelectedDrive );
1223 		do_fd_action( SelectedDrive );
1224 	}
1225 	else {
1226 		/* all sectors finished */
1227 		fd_end_request_cur(BLK_STS_OK);
1228 	}
1229 	return;
1230 
1231   err_end:
1232 	BufferDrive = -1;
1233 	fd_error();
1234 }
1235 
1236 
1237 static void fd_writetrack( void )
1238 {
1239 	unsigned long paddr, flags;
1240 	unsigned int track;
1241 
1242 	DPRINT(("fd_writetrack() Tr=%d Si=%d\n", ReqTrack, ReqSide ));
1243 
1244 	paddr = PhysTrackBuffer;
1245 	dma_cache_maintenance( paddr, BUFFER_SIZE, 1 );
1246 
1247 	fd_select_side( ReqSide );
1248 
1249 	/* Cheat for track if stretch != 0 */
1250 	if (SUDT->stretch) {
1251 		track = FDC_READ( FDCREG_TRACK);
1252 		MFPDELAY();
1253 		FDC_WRITE(FDCREG_TRACK,track >> SUDT->stretch);
1254 	}
1255 	udelay(40);
1256 
1257 	/* Setup DMA */
1258 	local_irq_save(flags);
1259 	dma_wd.dma_lo = (unsigned char)paddr;
1260 	MFPDELAY();
1261 	paddr >>= 8;
1262 	dma_wd.dma_md = (unsigned char)paddr;
1263 	MFPDELAY();
1264 	paddr >>= 8;
1265 	if (ATARIHW_PRESENT( EXTD_DMA ))
1266 		st_dma_ext_dmahi = (unsigned short)paddr;
1267 	else
1268 		dma_wd.dma_hi = (unsigned char)paddr;
1269 	MFPDELAY();
1270 	local_irq_restore(flags);
1271 
1272 	/* Clear FIFO and switch DMA to correct mode */
1273 	dma_wd.dma_mode_status = 0x190;
1274 	MFPDELAY();
1275 	dma_wd.dma_mode_status = 0x90;
1276 	MFPDELAY();
1277 	dma_wd.dma_mode_status = 0x190;
1278 	MFPDELAY();
1279 
1280 	/* How many sectors for DMA */
1281 	dma_wd.fdc_acces_seccount = BUFFER_SIZE/512;
1282 	udelay(40);
1283 
1284 	/* Start operation */
1285 	dma_wd.dma_mode_status = FDCSELREG_STP | 0x100;
1286 	udelay(40);
1287 	SET_IRQ_HANDLER( fd_writetrack_done );
1288 	dma_wd.fdc_acces_seccount = FDCCMD_WRTRA | get_head_settle_flag();
1289 
1290 	MotorOn = 1;
1291 	start_timeout();
1292 	/* wait for interrupt */
1293 }
1294 
1295 
1296 static void fd_writetrack_done( int status )
1297 {
1298 	DPRINT(("fd_writetrack_done()\n"));
1299 
1300 	stop_timeout();
1301 
1302 	if (status & FDCSTAT_WPROT) {
1303 		printk(KERN_NOTICE "fd%d: is write protected\n", SelectedDrive );
1304 		goto err_end;
1305 	}
1306 	if (status & FDCSTAT_LOST) {
1307 		printk(KERN_ERR "fd%d: lost data (side %d, track %d)\n",
1308 				SelectedDrive, ReqSide, ReqTrack );
1309 		goto err_end;
1310 	}
1311 
1312 	complete(&format_wait);
1313 	return;
1314 
1315   err_end:
1316 	fd_error();
1317 }
1318 
1319 static void fd_times_out(struct timer_list *unused)
1320 {
1321 	atari_disable_irq( IRQ_MFP_FDC );
1322 	if (!FloppyIRQHandler) goto end; /* int occurred after timer was fired, but
1323 					  * before we came here... */
1324 
1325 	SET_IRQ_HANDLER( NULL );
1326 	/* If the timeout occurred while the readtrack_check timer was
1327 	 * active, we need to cancel it, else bad things will happen */
1328 	if (UseTrackbuffer)
1329 		del_timer( &readtrack_timer );
1330 	FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1331 	udelay( 25 );
1332 
1333 	printk(KERN_ERR "floppy timeout\n" );
1334 	fd_error();
1335   end:
1336 	atari_enable_irq( IRQ_MFP_FDC );
1337 }
1338 
1339 
1340 /* The (noop) seek operation here is needed to make the WP bit in the
1341  * FDC status register accessible for check_change. If the last disk
1342  * operation would have been a RDSEC, this bit would always read as 0
1343  * no matter what :-( To save time, the seek goes to the track we're
1344  * already on.
1345  */
1346 
1347 static void finish_fdc( void )
1348 {
1349 	if (!NeedSeek) {
1350 		finish_fdc_done( 0 );
1351 	}
1352 	else {
1353 		DPRINT(("finish_fdc: dummy seek started\n"));
1354 		FDC_WRITE (FDCREG_DATA, SUD.track);
1355 		SET_IRQ_HANDLER( finish_fdc_done );
1356 		FDC_WRITE (FDCREG_CMD, FDCCMD_SEEK);
1357 		MotorOn = 1;
1358 		start_timeout();
1359 		/* we must wait for the IRQ here, because the ST-DMA
1360 		   is released immediately afterwards and the interrupt
1361 		   may be delivered to the wrong driver. */
1362 	  }
1363 }
1364 
1365 
1366 static void finish_fdc_done( int dummy )
1367 {
1368 	unsigned long flags;
1369 
1370 	DPRINT(("finish_fdc_done entered\n"));
1371 	stop_timeout();
1372 	NeedSeek = 0;
1373 
1374 	if (timer_pending(&fd_timer) && time_before(fd_timer.expires, jiffies + 5))
1375 		/* If the check for a disk change is done too early after this
1376 		 * last seek command, the WP bit still reads wrong :-((
1377 		 */
1378 		mod_timer(&fd_timer, jiffies + 5);
1379 	else
1380 		start_check_change_timer();
1381 	start_motor_off_timer();
1382 
1383 	local_irq_save(flags);
1384 	stdma_release();
1385 	local_irq_restore(flags);
1386 
1387 	DPRINT(("finish_fdc() finished\n"));
1388 }
1389 
1390 /* The detection of disk changes is a dark chapter in Atari history :-(
1391  * Because the "Drive ready" signal isn't present in the Atari
1392  * hardware, one has to rely on the "Write Protect". This works fine,
1393  * as long as no write protected disks are used. TOS solves this
1394  * problem by introducing tri-state logic ("maybe changed") and
1395  * looking at the serial number in block 0. This isn't possible for
1396  * Linux, since the floppy driver can't make assumptions about the
1397  * filesystem used on the disk and thus the contents of block 0. I've
1398  * chosen the method to always say "The disk was changed" if it is
1399  * unsure whether it was. This implies that every open or mount
1400  * invalidates the disk buffers if you work with write protected
1401  * disks. But at least this is better than working with incorrect data
1402  * due to unrecognised disk changes.
1403  */
1404 
1405 static unsigned int floppy_check_events(struct gendisk *disk,
1406 					unsigned int clearing)
1407 {
1408 	struct atari_floppy_struct *p = disk->private_data;
1409 	unsigned int drive = p - unit;
1410 	if (test_bit (drive, &fake_change)) {
1411 		/* simulated change (e.g. after formatting) */
1412 		return DISK_EVENT_MEDIA_CHANGE;
1413 	}
1414 	if (test_bit (drive, &changed_floppies)) {
1415 		/* surely changed (the WP signal changed at least once) */
1416 		return DISK_EVENT_MEDIA_CHANGE;
1417 	}
1418 	if (UD.wpstat) {
1419 		/* WP is on -> could be changed: to be sure, buffers should be
1420 		 * invalidated...
1421 		 */
1422 		return DISK_EVENT_MEDIA_CHANGE;
1423 	}
1424 
1425 	return 0;
1426 }
1427 
1428 static int floppy_revalidate(struct gendisk *disk)
1429 {
1430 	struct atari_floppy_struct *p = disk->private_data;
1431 	unsigned int drive = p - unit;
1432 
1433 	if (test_bit(drive, &changed_floppies) ||
1434 	    test_bit(drive, &fake_change) ||
1435 	    p->disktype == 0) {
1436 		if (UD.flags & FTD_MSG)
1437 			printk(KERN_ERR "floppy: clear format %p!\n", UDT);
1438 		BufferDrive = -1;
1439 		clear_bit(drive, &fake_change);
1440 		clear_bit(drive, &changed_floppies);
1441 		/* MSch: clearing geometry makes sense only for autoprobe
1442 		   formats, for 'permanent user-defined' parameter:
1443 		   restore default_params[] here if flagged valid! */
1444 		if (default_params[drive].blocks == 0)
1445 			UDT = NULL;
1446 		else
1447 			UDT = &default_params[drive];
1448 	}
1449 	return 0;
1450 }
1451 
1452 
1453 /* This sets up the global variables describing the current request. */
1454 
1455 static void setup_req_params( int drive )
1456 {
1457 	int block = ReqBlock + ReqCnt;
1458 
1459 	ReqTrack = block / UDT->spt;
1460 	ReqSector = block - ReqTrack * UDT->spt + 1;
1461 	ReqSide = ReqTrack & 1;
1462 	ReqTrack >>= 1;
1463 	ReqData = ReqBuffer + 512 * ReqCnt;
1464 
1465 	if (UseTrackbuffer)
1466 		read_track = (ReqCmd == READ && fd_request->error_count == 0);
1467 	else
1468 		read_track = 0;
1469 
1470 	DPRINT(("Request params: Si=%d Tr=%d Se=%d Data=%08lx\n",ReqSide,
1471 			ReqTrack, ReqSector, (unsigned long)ReqData ));
1472 }
1473 
1474 static void ataflop_commit_rqs(struct blk_mq_hw_ctx *hctx)
1475 {
1476 	spin_lock_irq(&ataflop_lock);
1477 	atari_disable_irq(IRQ_MFP_FDC);
1478 	finish_fdc();
1479 	atari_enable_irq(IRQ_MFP_FDC);
1480 	spin_unlock_irq(&ataflop_lock);
1481 }
1482 
1483 static blk_status_t ataflop_queue_rq(struct blk_mq_hw_ctx *hctx,
1484 				     const struct blk_mq_queue_data *bd)
1485 {
1486 	struct atari_floppy_struct *floppy = bd->rq->rq_disk->private_data;
1487 	int drive = floppy - unit;
1488 	int type = floppy->type;
1489 
1490 	spin_lock_irq(&ataflop_lock);
1491 	if (fd_request) {
1492 		spin_unlock_irq(&ataflop_lock);
1493 		return BLK_STS_DEV_RESOURCE;
1494 	}
1495 	if (!stdma_try_lock(floppy_irq, NULL))  {
1496 		spin_unlock_irq(&ataflop_lock);
1497 		return BLK_STS_RESOURCE;
1498 	}
1499 	fd_request = bd->rq;
1500 	blk_mq_start_request(fd_request);
1501 
1502 	atari_disable_irq( IRQ_MFP_FDC );
1503 
1504 	IsFormatting = 0;
1505 
1506 	if (!UD.connected) {
1507 		/* drive not connected */
1508 		printk(KERN_ERR "Unknown Device: fd%d\n", drive );
1509 		fd_end_request_cur(BLK_STS_IOERR);
1510 		goto out;
1511 	}
1512 
1513 	if (type == 0) {
1514 		if (!UDT) {
1515 			Probing = 1;
1516 			UDT = atari_disk_type + StartDiskType[DriveType];
1517 			set_capacity(floppy->disk, UDT->blocks);
1518 			UD.autoprobe = 1;
1519 		}
1520 	}
1521 	else {
1522 		/* user supplied disk type */
1523 		if (--type >= NUM_DISK_MINORS) {
1524 			printk(KERN_WARNING "fd%d: invalid disk format", drive );
1525 			fd_end_request_cur(BLK_STS_IOERR);
1526 			goto out;
1527 		}
1528 		if (minor2disktype[type].drive_types > DriveType)  {
1529 			printk(KERN_WARNING "fd%d: unsupported disk format", drive );
1530 			fd_end_request_cur(BLK_STS_IOERR);
1531 			goto out;
1532 		}
1533 		type = minor2disktype[type].index;
1534 		UDT = &atari_disk_type[type];
1535 		set_capacity(floppy->disk, UDT->blocks);
1536 		UD.autoprobe = 0;
1537 	}
1538 
1539 	/* stop deselect timer */
1540 	del_timer( &motor_off_timer );
1541 
1542 	ReqCnt = 0;
1543 	ReqCmd = rq_data_dir(fd_request);
1544 	ReqBlock = blk_rq_pos(fd_request);
1545 	ReqBuffer = bio_data(fd_request->bio);
1546 	setup_req_params( drive );
1547 	do_fd_action( drive );
1548 
1549 	if (bd->last)
1550 		finish_fdc();
1551 	atari_enable_irq( IRQ_MFP_FDC );
1552 
1553 out:
1554 	spin_unlock_irq(&ataflop_lock);
1555 	return BLK_STS_OK;
1556 }
1557 
1558 static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode,
1559 		    unsigned int cmd, unsigned long param)
1560 {
1561 	struct gendisk *disk = bdev->bd_disk;
1562 	struct atari_floppy_struct *floppy = disk->private_data;
1563 	int drive = floppy - unit;
1564 	int type = floppy->type;
1565 	struct atari_format_descr fmt_desc;
1566 	struct atari_disk_type *dtp;
1567 	struct floppy_struct getprm;
1568 	int settype;
1569 	struct floppy_struct setprm;
1570 	void __user *argp = (void __user *)param;
1571 
1572 	switch (cmd) {
1573 	case FDGETPRM:
1574 		if (type) {
1575 			if (--type >= NUM_DISK_MINORS)
1576 				return -ENODEV;
1577 			if (minor2disktype[type].drive_types > DriveType)
1578 				return -ENODEV;
1579 			type = minor2disktype[type].index;
1580 			dtp = &atari_disk_type[type];
1581 			if (UD.flags & FTD_MSG)
1582 			    printk (KERN_ERR "floppy%d: found dtp %p name %s!\n",
1583 			        drive, dtp, dtp->name);
1584 		}
1585 		else {
1586 			if (!UDT)
1587 				return -ENXIO;
1588 			else
1589 				dtp = UDT;
1590 		}
1591 		memset((void *)&getprm, 0, sizeof(getprm));
1592 		getprm.size = dtp->blocks;
1593 		getprm.sect = dtp->spt;
1594 		getprm.head = 2;
1595 		getprm.track = dtp->blocks/dtp->spt/2;
1596 		getprm.stretch = dtp->stretch;
1597 		if (copy_to_user(argp, &getprm, sizeof(getprm)))
1598 			return -EFAULT;
1599 		return 0;
1600 	}
1601 	switch (cmd) {
1602 	case FDSETPRM:
1603 	case FDDEFPRM:
1604 	        /*
1605 		 * MSch 7/96: simple 'set geometry' case: just set the
1606 		 * 'default' device params (minor == 0).
1607 		 * Currently, the drive geometry is cleared after each
1608 		 * disk change and subsequent revalidate()! simple
1609 		 * implementation of FDDEFPRM: save geometry from a
1610 		 * FDDEFPRM call and restore it in floppy_revalidate() !
1611 		 */
1612 
1613 		/* get the parameters from user space */
1614 		if (floppy->ref != 1 && floppy->ref != -1)
1615 			return -EBUSY;
1616 		if (copy_from_user(&setprm, argp, sizeof(setprm)))
1617 			return -EFAULT;
1618 		/*
1619 		 * first of all: check for floppy change and revalidate,
1620 		 * or the next access will revalidate - and clear UDT :-(
1621 		 */
1622 
1623 		if (floppy_check_events(disk, 0))
1624 		        floppy_revalidate(disk);
1625 
1626 		if (UD.flags & FTD_MSG)
1627 		    printk (KERN_INFO "floppy%d: setting size %d spt %d str %d!\n",
1628 			drive, setprm.size, setprm.sect, setprm.stretch);
1629 
1630 		/* what if type > 0 here? Overwrite specified entry ? */
1631 		if (type) {
1632 		        /* refuse to re-set a predefined type for now */
1633 			return -EINVAL;
1634 		}
1635 
1636 		/*
1637 		 * type == 0: first look for a matching entry in the type list,
1638 		 * and set the UD.disktype field to use the perdefined entry.
1639 		 * TODO: add user-defined format to head of autoprobe list ?
1640 		 * Useful to include the user-type for future autodetection!
1641 		 */
1642 
1643 		for (settype = 0; settype < NUM_DISK_MINORS; settype++) {
1644 			int setidx = 0;
1645 			if (minor2disktype[settype].drive_types > DriveType) {
1646 				/* skip this one, invalid for drive ... */
1647 				continue;
1648 			}
1649 			setidx = minor2disktype[settype].index;
1650 			dtp = &atari_disk_type[setidx];
1651 
1652 			/* found matching entry ?? */
1653 			if (   dtp->blocks  == setprm.size
1654 			    && dtp->spt     == setprm.sect
1655 			    && dtp->stretch == setprm.stretch ) {
1656 				if (UD.flags & FTD_MSG)
1657 				    printk (KERN_INFO "floppy%d: setting %s %p!\n",
1658 				        drive, dtp->name, dtp);
1659 				UDT = dtp;
1660 				set_capacity(floppy->disk, UDT->blocks);
1661 
1662 				if (cmd == FDDEFPRM) {
1663 				  /* save settings as permanent default type */
1664 				  default_params[drive].name    = dtp->name;
1665 				  default_params[drive].spt     = dtp->spt;
1666 				  default_params[drive].blocks  = dtp->blocks;
1667 				  default_params[drive].fdc_speed = dtp->fdc_speed;
1668 				  default_params[drive].stretch = dtp->stretch;
1669 				}
1670 
1671 				return 0;
1672 			}
1673 
1674 		}
1675 
1676 		/* no matching disk type found above - setting user_params */
1677 
1678 	       	if (cmd == FDDEFPRM) {
1679 			/* set permanent type */
1680 			dtp = &default_params[drive];
1681 		} else
1682 			/* set user type (reset by disk change!) */
1683 			dtp = &user_params[drive];
1684 
1685 		dtp->name   = "user format";
1686 		dtp->blocks = setprm.size;
1687 		dtp->spt    = setprm.sect;
1688 		if (setprm.sect > 14)
1689 			dtp->fdc_speed = 3;
1690 		else
1691 			dtp->fdc_speed = 0;
1692 		dtp->stretch = setprm.stretch;
1693 
1694 		if (UD.flags & FTD_MSG)
1695 			printk (KERN_INFO "floppy%d: blk %d spt %d str %d!\n",
1696 				drive, dtp->blocks, dtp->spt, dtp->stretch);
1697 
1698 		/* sanity check */
1699 		if (setprm.track != dtp->blocks/dtp->spt/2 ||
1700 		    setprm.head != 2)
1701 			return -EINVAL;
1702 
1703 		UDT = dtp;
1704 		set_capacity(floppy->disk, UDT->blocks);
1705 
1706 		return 0;
1707 	case FDMSGON:
1708 		UD.flags |= FTD_MSG;
1709 		return 0;
1710 	case FDMSGOFF:
1711 		UD.flags &= ~FTD_MSG;
1712 		return 0;
1713 	case FDSETEMSGTRESH:
1714 		return -EINVAL;
1715 	case FDFMTBEG:
1716 		return 0;
1717 	case FDFMTTRK:
1718 		if (floppy->ref != 1 && floppy->ref != -1)
1719 			return -EBUSY;
1720 		if (copy_from_user(&fmt_desc, argp, sizeof(fmt_desc)))
1721 			return -EFAULT;
1722 		return do_format(drive, type, &fmt_desc);
1723 	case FDCLRPRM:
1724 		UDT = NULL;
1725 		/* MSch: invalidate default_params */
1726 		default_params[drive].blocks  = 0;
1727 		set_capacity(floppy->disk, MAX_DISK_SIZE * 2);
1728 	case FDFMTEND:
1729 	case FDFLUSH:
1730 		/* invalidate the buffer track to force a reread */
1731 		BufferDrive = -1;
1732 		set_bit(drive, &fake_change);
1733 		check_disk_change(bdev);
1734 		return 0;
1735 	default:
1736 		return -EINVAL;
1737 	}
1738 }
1739 
1740 static int fd_ioctl(struct block_device *bdev, fmode_t mode,
1741 			     unsigned int cmd, unsigned long arg)
1742 {
1743 	int ret;
1744 
1745 	mutex_lock(&ataflop_mutex);
1746 	ret = fd_locked_ioctl(bdev, mode, cmd, arg);
1747 	mutex_unlock(&ataflop_mutex);
1748 
1749 	return ret;
1750 }
1751 
1752 /* Initialize the 'unit' variable for drive 'drive' */
1753 
1754 static void __init fd_probe( int drive )
1755 {
1756 	UD.connected = 0;
1757 	UDT  = NULL;
1758 
1759 	if (!fd_test_drive_present( drive ))
1760 		return;
1761 
1762 	UD.connected = 1;
1763 	UD.track     = 0;
1764 	switch( UserSteprate[drive] ) {
1765 	case 2:
1766 		UD.steprate = FDCSTEP_2;
1767 		break;
1768 	case 3:
1769 		UD.steprate = FDCSTEP_3;
1770 		break;
1771 	case 6:
1772 		UD.steprate = FDCSTEP_6;
1773 		break;
1774 	case 12:
1775 		UD.steprate = FDCSTEP_12;
1776 		break;
1777 	default: /* should be -1 for "not set by user" */
1778 		if (ATARIHW_PRESENT( FDCSPEED ) || MACH_IS_MEDUSA)
1779 			UD.steprate = FDCSTEP_3;
1780 		else
1781 			UD.steprate = FDCSTEP_6;
1782 		break;
1783 	}
1784 	MotorOn = 1;	/* from probe restore operation! */
1785 }
1786 
1787 
1788 /* This function tests the physical presence of a floppy drive (not
1789  * whether a disk is inserted). This is done by issuing a restore
1790  * command, waiting max. 2 seconds (that should be enough to move the
1791  * head across the whole disk) and looking at the state of the "TR00"
1792  * signal. This should now be raised if there is a drive connected
1793  * (and there is no hardware failure :-) Otherwise, the drive is
1794  * declared absent.
1795  */
1796 
1797 static int __init fd_test_drive_present( int drive )
1798 {
1799 	unsigned long timeout;
1800 	unsigned char status;
1801 	int ok;
1802 
1803 	if (drive >= (MACH_IS_FALCON ? 1 : 2)) return( 0 );
1804 	fd_select_drive( drive );
1805 
1806 	/* disable interrupt temporarily */
1807 	atari_turnoff_irq( IRQ_MFP_FDC );
1808 	FDC_WRITE (FDCREG_TRACK, 0xff00);
1809 	FDC_WRITE( FDCREG_CMD, FDCCMD_RESTORE | FDCCMDADD_H | FDCSTEP_6 );
1810 
1811 	timeout = jiffies + 2*HZ+HZ/2;
1812 	while (time_before(jiffies, timeout))
1813 		if (!(st_mfp.par_dt_reg & 0x20))
1814 			break;
1815 
1816 	status = FDC_READ( FDCREG_STATUS );
1817 	ok = (status & FDCSTAT_TR00) != 0;
1818 
1819 	/* force interrupt to abort restore operation (FDC would try
1820 	 * about 50 seconds!) */
1821 	FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1822 	udelay(500);
1823 	status = FDC_READ( FDCREG_STATUS );
1824 	udelay(20);
1825 
1826 	if (ok) {
1827 		/* dummy seek command to make WP bit accessible */
1828 		FDC_WRITE( FDCREG_DATA, 0 );
1829 		FDC_WRITE( FDCREG_CMD, FDCCMD_SEEK );
1830 		while( st_mfp.par_dt_reg & 0x20 )
1831 			;
1832 		status = FDC_READ( FDCREG_STATUS );
1833 	}
1834 
1835 	atari_turnon_irq( IRQ_MFP_FDC );
1836 	return( ok );
1837 }
1838 
1839 
1840 /* Look how many and which kind of drives are connected. If there are
1841  * floppies, additionally start the disk-change and motor-off timers.
1842  */
1843 
1844 static void __init config_types( void )
1845 {
1846 	int drive, cnt = 0;
1847 
1848 	/* for probing drives, set the FDC speed to 8 MHz */
1849 	if (ATARIHW_PRESENT(FDCSPEED))
1850 		dma_wd.fdc_speed = 0;
1851 
1852 	printk(KERN_INFO "Probing floppy drive(s):\n");
1853 	for( drive = 0; drive < FD_MAX_UNITS; drive++ ) {
1854 		fd_probe( drive );
1855 		if (UD.connected) {
1856 			printk(KERN_INFO "fd%d\n", drive);
1857 			++cnt;
1858 		}
1859 	}
1860 
1861 	if (FDC_READ( FDCREG_STATUS ) & FDCSTAT_BUSY) {
1862 		/* If FDC is still busy from probing, give it another FORCI
1863 		 * command to abort the operation. If this isn't done, the FDC
1864 		 * will interrupt later and its IRQ line stays low, because
1865 		 * the status register isn't read. And this will block any
1866 		 * interrupts on this IRQ line :-(
1867 		 */
1868 		FDC_WRITE( FDCREG_CMD, FDCCMD_FORCI );
1869 		udelay(500);
1870 		FDC_READ( FDCREG_STATUS );
1871 		udelay(20);
1872 	}
1873 
1874 	if (cnt > 0) {
1875 		start_motor_off_timer();
1876 		if (cnt == 1) fd_select_drive( 0 );
1877 		start_check_change_timer();
1878 	}
1879 }
1880 
1881 /*
1882  * floppy_open check for aliasing (/dev/fd0 can be the same as
1883  * /dev/PS0 etc), and disallows simultaneous access to the same
1884  * drive with different device numbers.
1885  */
1886 
1887 static int floppy_open(struct block_device *bdev, fmode_t mode)
1888 {
1889 	struct atari_floppy_struct *p = bdev->bd_disk->private_data;
1890 	int type  = MINOR(bdev->bd_dev) >> 2;
1891 
1892 	DPRINT(("fd_open: type=%d\n",type));
1893 	if (p->ref && p->type != type)
1894 		return -EBUSY;
1895 
1896 	if (p->ref == -1 || (p->ref && mode & FMODE_EXCL))
1897 		return -EBUSY;
1898 
1899 	if (mode & FMODE_EXCL)
1900 		p->ref = -1;
1901 	else
1902 		p->ref++;
1903 
1904 	p->type = type;
1905 
1906 	if (mode & FMODE_NDELAY)
1907 		return 0;
1908 
1909 	if (mode & (FMODE_READ|FMODE_WRITE)) {
1910 		check_disk_change(bdev);
1911 		if (mode & FMODE_WRITE) {
1912 			if (p->wpstat) {
1913 				if (p->ref < 0)
1914 					p->ref = 0;
1915 				else
1916 					p->ref--;
1917 				return -EROFS;
1918 			}
1919 		}
1920 	}
1921 	return 0;
1922 }
1923 
1924 static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode)
1925 {
1926 	int ret;
1927 
1928 	mutex_lock(&ataflop_mutex);
1929 	ret = floppy_open(bdev, mode);
1930 	mutex_unlock(&ataflop_mutex);
1931 
1932 	return ret;
1933 }
1934 
1935 static void floppy_release(struct gendisk *disk, fmode_t mode)
1936 {
1937 	struct atari_floppy_struct *p = disk->private_data;
1938 	mutex_lock(&ataflop_mutex);
1939 	if (p->ref < 0)
1940 		p->ref = 0;
1941 	else if (!p->ref--) {
1942 		printk(KERN_ERR "floppy_release with fd_ref == 0");
1943 		p->ref = 0;
1944 	}
1945 	mutex_unlock(&ataflop_mutex);
1946 }
1947 
1948 static const struct block_device_operations floppy_fops = {
1949 	.owner		= THIS_MODULE,
1950 	.open		= floppy_unlocked_open,
1951 	.release	= floppy_release,
1952 	.ioctl		= fd_ioctl,
1953 	.check_events	= floppy_check_events,
1954 	.revalidate_disk= floppy_revalidate,
1955 };
1956 
1957 static const struct blk_mq_ops ataflop_mq_ops = {
1958 	.queue_rq = ataflop_queue_rq,
1959 	.commit_rqs = ataflop_commit_rqs,
1960 };
1961 
1962 static struct kobject *floppy_find(dev_t dev, int *part, void *data)
1963 {
1964 	int drive = *part & 3;
1965 	int type  = *part >> 2;
1966 	if (drive >= FD_MAX_UNITS || type > NUM_DISK_MINORS)
1967 		return NULL;
1968 	*part = 0;
1969 	return get_disk_and_module(unit[drive].disk);
1970 }
1971 
1972 static int __init atari_floppy_init (void)
1973 {
1974 	int i;
1975 	int ret;
1976 
1977 	if (!MACH_IS_ATARI)
1978 		/* Amiga, Mac, ... don't have Atari-compatible floppy :-) */
1979 		return -ENODEV;
1980 
1981 	if (register_blkdev(FLOPPY_MAJOR,"fd"))
1982 		return -EBUSY;
1983 
1984 	for (i = 0; i < FD_MAX_UNITS; i++) {
1985 		unit[i].disk = alloc_disk(1);
1986 		if (!unit[i].disk) {
1987 			ret = -ENOMEM;
1988 			goto err;
1989 		}
1990 
1991 		unit[i].disk->queue = blk_mq_init_sq_queue(&unit[i].tag_set,
1992 							   &ataflop_mq_ops, 2,
1993 							   BLK_MQ_F_SHOULD_MERGE);
1994 		if (IS_ERR(unit[i].disk->queue)) {
1995 			put_disk(unit[i].disk);
1996 			ret = PTR_ERR(unit[i].disk->queue);
1997 			unit[i].disk->queue = NULL;
1998 			goto err;
1999 		}
2000 	}
2001 
2002 	if (UseTrackbuffer < 0)
2003 		/* not set by user -> use default: for now, we turn
2004 		   track buffering off for all Medusas, though it
2005 		   could be used with ones that have a counter
2006 		   card. But the test is too hard :-( */
2007 		UseTrackbuffer = !MACH_IS_MEDUSA;
2008 
2009 	/* initialize variables */
2010 	SelectedDrive = -1;
2011 	BufferDrive = -1;
2012 
2013 	DMABuffer = atari_stram_alloc(BUFFER_SIZE+512, "ataflop");
2014 	if (!DMABuffer) {
2015 		printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n");
2016 		ret = -ENOMEM;
2017 		goto err;
2018 	}
2019 	TrackBuffer = DMABuffer + 512;
2020 	PhysDMABuffer = atari_stram_to_phys(DMABuffer);
2021 	PhysTrackBuffer = virt_to_phys(TrackBuffer);
2022 	BufferDrive = BufferSide = BufferTrack = -1;
2023 
2024 	for (i = 0; i < FD_MAX_UNITS; i++) {
2025 		unit[i].track = -1;
2026 		unit[i].flags = 0;
2027 		unit[i].disk->major = FLOPPY_MAJOR;
2028 		unit[i].disk->first_minor = i;
2029 		sprintf(unit[i].disk->disk_name, "fd%d", i);
2030 		unit[i].disk->fops = &floppy_fops;
2031 		unit[i].disk->private_data = &unit[i];
2032 		set_capacity(unit[i].disk, MAX_DISK_SIZE * 2);
2033 		add_disk(unit[i].disk);
2034 	}
2035 
2036 	blk_register_region(MKDEV(FLOPPY_MAJOR, 0), 256, THIS_MODULE,
2037 				floppy_find, NULL, NULL);
2038 
2039 	printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n",
2040 	       DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E',
2041 	       UseTrackbuffer ? "" : "no ");
2042 	config_types();
2043 
2044 	return 0;
2045 
2046 err:
2047 	while (--i >= 0) {
2048 		struct gendisk *disk = unit[i].disk;
2049 
2050 		blk_cleanup_queue(disk->queue);
2051 		blk_mq_free_tag_set(&unit[i].tag_set);
2052 		put_disk(unit[i].disk);
2053 	}
2054 
2055 	unregister_blkdev(FLOPPY_MAJOR, "fd");
2056 	return ret;
2057 }
2058 
2059 #ifndef MODULE
2060 static int __init atari_floppy_setup(char *str)
2061 {
2062 	int ints[3 + FD_MAX_UNITS];
2063 	int i;
2064 
2065 	if (!MACH_IS_ATARI)
2066 		return 0;
2067 
2068 	str = get_options(str, 3 + FD_MAX_UNITS, ints);
2069 
2070 	if (ints[0] < 1) {
2071 		printk(KERN_ERR "ataflop_setup: no arguments!\n" );
2072 		return 0;
2073 	}
2074 	else if (ints[0] > 2+FD_MAX_UNITS) {
2075 		printk(KERN_ERR "ataflop_setup: too many arguments\n" );
2076 	}
2077 
2078 	if (ints[1] < 0 || ints[1] > 2)
2079 		printk(KERN_ERR "ataflop_setup: bad drive type\n" );
2080 	else
2081 		DriveType = ints[1];
2082 
2083 	if (ints[0] >= 2)
2084 		UseTrackbuffer = (ints[2] > 0);
2085 
2086 	for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) {
2087 		if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12)
2088 			printk(KERN_ERR "ataflop_setup: bad steprate\n" );
2089 		else
2090 			UserSteprate[i-3] = ints[i];
2091 	}
2092 	return 1;
2093 }
2094 
2095 __setup("floppy=", atari_floppy_setup);
2096 #endif
2097 
2098 static void __exit atari_floppy_exit(void)
2099 {
2100 	int i;
2101 	blk_unregister_region(MKDEV(FLOPPY_MAJOR, 0), 256);
2102 	for (i = 0; i < FD_MAX_UNITS; i++) {
2103 		del_gendisk(unit[i].disk);
2104 		blk_cleanup_queue(unit[i].disk->queue);
2105 		blk_mq_free_tag_set(&unit[i].tag_set);
2106 		put_disk(unit[i].disk);
2107 	}
2108 	unregister_blkdev(FLOPPY_MAJOR, "fd");
2109 
2110 	del_timer_sync(&fd_timer);
2111 	atari_stram_free( DMABuffer );
2112 }
2113 
2114 module_init(atari_floppy_init)
2115 module_exit(atari_floppy_exit)
2116 
2117 MODULE_LICENSE("GPL");
2118