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