Thomas Gleixner | 09c434b | 2019-05-19 13:08:20 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 70 | #include <linux/blk-mq.h> |
Christoph Hellwig | b81e0c2 | 2021-09-20 14:33:25 +0200 | [diff] [blame] | 71 | #include <linux/major.h> |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 72 | #include <linux/mutex.h> |
Arnd Bergmann | 7b8a3d2 | 2014-02-26 12:01:41 +0100 | [diff] [blame] | 73 | #include <linux/completion.h> |
| 74 | #include <linux/wait.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 84 | static DEFINE_MUTEX(ataflop_mutex); |
Tejun Heo | a336ca6 | 2009-05-08 11:54:03 +0900 | [diff] [blame] | 85 | static struct request *fd_request; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | |
Omar Sandoval | 3e6b8c3c | 2018-10-11 12:20:48 -0700 | [diff] [blame] | 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 | |
| 162 | struct atari_format_descr { |
| 163 | int track; /* to be formatted */ |
| 164 | int head; /* "" "" */ |
| 165 | int sect_offset; /* offset of first sector */ |
| 166 | }; |
| 167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | /* Disk types: DD, HD, ED */ |
| 169 | static 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 ? */ |
Geert Uytterhoeven | 5ceadd2 | 2008-02-06 01:38:09 -0800 | [diff] [blame] | 175 | } atari_disk_type[] = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 213 | static 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 | |
| 223 | static int DriveType = TYPE_HD; |
| 224 | |
| 225 | static DEFINE_SPINLOCK(ataflop_lock); |
| 226 | |
| 227 | /* Array for translating minors into disk formats */ |
| 228 | static 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 | |
Tobias Klauser | 945f390 | 2006-01-08 01:05:11 -0800 | [diff] [blame] | 266 | #define NUM_DISK_MINORS ARRAY_SIZE(minor2disktype) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
| 278 | static 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 | */ |
| 286 | static struct atari_disk_type default_params[FD_MAX_UNITS]; |
| 287 | |
| 288 | /* current info on each unit */ |
| 289 | static 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 */ |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 301 | struct gendisk *disk[NUM_DISK_MINORS]; |
Luis Chamberlain | 573effb | 2021-09-27 15:03:00 -0700 | [diff] [blame] | 302 | bool registered[NUM_DISK_MINORS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | int ref; |
| 304 | int type; |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 305 | struct blk_mq_tag_set tag_set; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 345 | static int MaxSectors[] = { |
| 346 | 11, 22, 44 |
| 347 | }; |
| 348 | static int BufferSize[] = { |
| 349 | 15*512, 30*512, 60*512 |
| 350 | }; |
| 351 | |
| 352 | #define BUFFER_SIZE (BufferSize[DriveType]) |
| 353 | |
| 354 | unsigned char *DMABuffer; /* buffer for writes */ |
| 355 | static unsigned long PhysDMABuffer; /* physical address */ |
| 356 | |
| 357 | static int UseTrackbuffer = -1; /* Do track buffering? */ |
Rusty Russell | 8d3b33f | 2006-03-25 03:07:05 -0800 | [diff] [blame] | 358 | module_param(UseTrackbuffer, int, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | |
| 360 | unsigned char *TrackBuffer; /* buffer for reads */ |
| 361 | static unsigned long PhysTrackBuffer; /* physical address */ |
| 362 | static int BufferDrive, BufferSide, BufferTrack; |
| 363 | static 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 | */ |
| 374 | static int SelectedDrive = 0; |
| 375 | static int ReqCmd, ReqBlock; |
| 376 | static int ReqSide, ReqTrack, ReqSector, ReqCnt; |
| 377 | static int HeadSettleFlag = 0; |
| 378 | static unsigned char *ReqData, *ReqBuffer; |
| 379 | static int MotorOn = 0, MotorOffTrys; |
| 380 | static int IsFormatting = 0, FormatError; |
| 381 | |
| 382 | static int UserSteprate[FD_MAX_UNITS] = { -1, -1 }; |
Rusty Russell | 8d3b33f | 2006-03-25 03:07:05 -0800 | [diff] [blame] | 383 | module_param_array(UserSteprate, int, NULL, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | |
Arnd Bergmann | 7b8a3d2 | 2014-02-26 12:01:41 +0100 | [diff] [blame] | 385 | static DECLARE_COMPLETION(format_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
| 387 | static 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 | */ |
| 405 | static int Probing = 0; |
| 406 | |
| 407 | /* This flag is set when a dummy seek is necessary to make the WP |
| 408 | * status bit accessible. |
| 409 | */ |
| 410 | static 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 | |
| 421 | static void fd_select_side( int side ); |
| 422 | static void fd_select_drive( int drive ); |
| 423 | static void fd_deselect( void ); |
Kees Cook | 24ed960 | 2017-08-28 11:28:21 -0700 | [diff] [blame] | 424 | static void fd_motor_off_timer(struct timer_list *unused); |
| 425 | static void check_change(struct timer_list *unused); |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 426 | static irqreturn_t floppy_irq (int irq, void *dummy); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | static void fd_error( void ); |
| 428 | static int do_format(int drive, int type, struct atari_format_descr *desc); |
| 429 | static void do_fd_action( int drive ); |
| 430 | static void fd_calibrate( void ); |
| 431 | static void fd_calibrate_done( int status ); |
| 432 | static void fd_seek( void ); |
| 433 | static void fd_seek_done( int status ); |
| 434 | static void fd_rwsec( void ); |
Kees Cook | 24ed960 | 2017-08-28 11:28:21 -0700 | [diff] [blame] | 435 | static void fd_readtrack_check(struct timer_list *unused); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | static void fd_rwsec_done( int status ); |
| 437 | static void fd_rwsec_done1(int status); |
| 438 | static void fd_writetrack( void ); |
| 439 | static void fd_writetrack_done( int status ); |
Kees Cook | 24ed960 | 2017-08-28 11:28:21 -0700 | [diff] [blame] | 440 | static void fd_times_out(struct timer_list *unused); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | static void finish_fdc( void ); |
| 442 | static void finish_fdc_done( int dummy ); |
| 443 | static void setup_req_params( int drive ); |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 444 | static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, unsigned int |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 445 | cmd, unsigned long param); |
| 446 | static void fd_probe( int drive ); |
| 447 | static int fd_test_drive_present( int drive ); |
| 448 | static void config_types( void ); |
Al Viro | 60ad234 | 2008-03-02 09:23:54 -0500 | [diff] [blame] | 449 | static int floppy_open(struct block_device *bdev, fmode_t mode); |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 450 | static void floppy_release(struct gendisk *disk, fmode_t mode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | |
| 452 | /************************* End of Prototypes **************************/ |
| 453 | |
Kees Cook | 1d27e3e | 2017-10-04 16:27:04 -0700 | [diff] [blame] | 454 | static DEFINE_TIMER(motor_off_timer, fd_motor_off_timer); |
| 455 | static DEFINE_TIMER(readtrack_timer, fd_readtrack_check); |
| 456 | static DEFINE_TIMER(timeout_timer, fd_times_out); |
| 457 | static DEFINE_TIMER(fd_timer, check_change); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 458 | |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 459 | static void fd_end_request_cur(blk_status_t err) |
Tejun Heo | a336ca6 | 2009-05-08 11:54:03 +0900 | [diff] [blame] | 460 | { |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 461 | if (!blk_update_request(fd_request, err, |
| 462 | blk_rq_cur_bytes(fd_request))) { |
| 463 | __blk_mq_end_request(fd_request, err); |
Tejun Heo | a336ca6 | 2009-05-08 11:54:03 +0900 | [diff] [blame] | 464 | fd_request = NULL; |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 465 | } |
Tejun Heo | a336ca6 | 2009-05-08 11:54:03 +0900 | [diff] [blame] | 466 | } |
| 467 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | static inline void start_motor_off_timer(void) |
| 469 | { |
| 470 | mod_timer(&motor_off_timer, jiffies + FD_MOTOR_OFF_DELAY); |
| 471 | MotorOffTrys = 0; |
| 472 | } |
| 473 | |
| 474 | static inline void start_check_change_timer( void ) |
| 475 | { |
| 476 | mod_timer(&fd_timer, jiffies + CHECK_CHANGE_DELAY); |
| 477 | } |
| 478 | |
| 479 | static inline void start_timeout(void) |
| 480 | { |
| 481 | mod_timer(&timeout_timer, jiffies + FLOPPY_TIMEOUT); |
| 482 | } |
| 483 | |
| 484 | static inline void stop_timeout(void) |
| 485 | { |
| 486 | del_timer(&timeout_timer); |
| 487 | } |
| 488 | |
| 489 | /* Select the side to use. */ |
| 490 | |
| 491 | static 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 | |
| 510 | static 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 | |
| 541 | static 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 | |
Kees Cook | 24ed960 | 2017-08-28 11:28:21 -0700 | [diff] [blame] | 563 | static void fd_motor_off_timer(struct timer_list *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Kees Cook | 24ed960 | 2017-08-28 11:28:21 -0700 | [diff] [blame] | 599 | static void check_change(struct timer_list *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 637 | static inline void set_head_settle_flag(void) |
| 638 | { |
| 639 | HeadSettleFlag = FDCCMDADD_E; |
| 640 | } |
| 641 | |
| 642 | static inline int get_head_settle_flag(void) |
| 643 | { |
| 644 | int tmp = HeadSettleFlag; |
| 645 | HeadSettleFlag = 0; |
| 646 | return( tmp ); |
| 647 | } |
| 648 | |
| 649 | static 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 | |
| 663 | static void (*FloppyIRQHandler)( int status ) = NULL; |
| 664 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 665 | static irqreturn_t floppy_irq (int irq, void *dummy) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 689 | static void fd_error( void ) |
| 690 | { |
| 691 | if (IsFormatting) { |
| 692 | IsFormatting = 0; |
| 693 | FormatError = 1; |
Arnd Bergmann | 7b8a3d2 | 2014-02-26 12:01:41 +0100 | [diff] [blame] | 694 | complete(&format_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 695 | return; |
| 696 | } |
| 697 | |
Tejun Heo | a336ca6 | 2009-05-08 11:54:03 +0900 | [diff] [blame] | 698 | if (!fd_request) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | return; |
| 700 | |
Christoph Hellwig | c8e9078 | 2017-04-20 16:03:13 +0200 | [diff] [blame] | 701 | fd_request->error_count++; |
| 702 | if (fd_request->error_count >= MAX_ERRORS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | printk(KERN_ERR "fd%d: too many errors.\n", SelectedDrive ); |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 704 | fd_end_request_cur(BLK_STS_IOERR); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | } |
Christoph Hellwig | c8e9078 | 2017-04-20 16:03:13 +0200 | [diff] [blame] | 706 | else if (fd_request->error_count == RECALIBRATE_ERRORS) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | printk(KERN_WARNING "fd%d: recalibrating\n", SelectedDrive ); |
| 708 | if (SelectedDrive != -1) |
| 709 | SUD.track = -1; |
| 710 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 726 | static int do_format(int drive, int type, struct atari_format_descr *desc) |
| 727 | { |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 728 | struct request_queue *q; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 729 | unsigned char *p; |
| 730 | int sect, nsect; |
| 731 | unsigned long flags; |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 732 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
Dan Carpenter | 1ffec38 | 2021-04-21 13:18:35 +0300 | [diff] [blame] | 734 | if (type) { |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 735 | type--; |
Dan Carpenter | 1ffec38 | 2021-04-21 13:18:35 +0300 | [diff] [blame] | 736 | if (type >= NUM_DISK_MINORS || |
| 737 | minor2disktype[type].drive_types > DriveType) |
| 738 | return -EINVAL; |
| 739 | } |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 740 | |
| 741 | q = unit[drive].disk[type]->queue; |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 742 | blk_mq_freeze_queue(q); |
| 743 | blk_mq_quiesce_queue(q); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 744 | |
| 745 | local_irq_save(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 751 | type = minor2disktype[type].index; |
Geert Uytterhoeven | 5ceadd2 | 2008-02-06 01:38:09 -0800 | [diff] [blame] | 752 | UDT = &atari_disk_type[type]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | } |
| 754 | |
| 755 | if (!UDT || desc->track >= UDT->blocks/UDT->spt/2 || desc->head >= 2) { |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 756 | ret = -EINVAL; |
| 757 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Arnd Bergmann | 7b8a3d2 | 2014-02-26 12:01:41 +0100 | [diff] [blame] | 794 | wait_for_completion(&format_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 795 | |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 796 | ret = FormatError ? -EIO : 0; |
| 797 | out: |
| 798 | blk_mq_unquiesce_queue(q); |
| 799 | blk_mq_unfreeze_queue(q); |
| 800 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 813 | static 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 ); |
Tejun Heo | a336ca6 | 2009-05-08 11:54:03 +0900 | [diff] [blame] | 822 | if (++ReqCnt < blk_rq_cur_sectors(fd_request)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 823 | /* read next sector */ |
| 824 | setup_req_params( drive ); |
| 825 | goto repeat; |
| 826 | } |
| 827 | else { |
| 828 | /* all sectors finished */ |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 829 | fd_end_request_cur(BLK_STS_OK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 857 | static void fd_calibrate( void ) |
| 858 | { |
| 859 | if (SUD.track >= 0) { |
| 860 | fd_calibrate_done( 0 ); |
| 861 | return; |
| 862 | } |
| 863 | |
| 864 | if (ATARIHW_PRESENT(FDCSPEED)) |
zhengbin | 9677d64 | 2019-11-28 11:09:32 +0800 | [diff] [blame] | 865 | dma_wd.fdc_speed = 0; /* always seek with 8 Mhz */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 878 | static 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 | |
| 901 | static 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 | |
| 926 | static 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 | |
| 956 | static int MultReadInProgress = 0; |
| 957 | |
| 958 | |
| 959 | static 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 | |
Kees Cook | 24ed960 | 2017-08-28 11:28:21 -0700 | [diff] [blame] | 1055 | static void fd_readtrack_check(struct timer_list *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 1113 | static 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 | |
| 1126 | static 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) { |
Geert Uytterhoeven | 5ceadd2 | 2008-02-06 01:38:09 -0800 | [diff] [blame] | 1158 | if (SUDT > atari_disk_type) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1159 | if (SUDT[-1].blocks > ReqBlock) { |
| 1160 | /* try another disk type */ |
| 1161 | SUDT--; |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 1162 | set_capacity(unit[SelectedDrive].disk[0], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) { |
Geert Uytterhoeven | 5ceadd2 | 2008-02-06 01:38:09 -0800 | [diff] [blame] | 1176 | SUDT = atari_disk_type + StartDiskType[DriveType]; |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 1177 | set_capacity(unit[SelectedDrive].disk[0], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Tejun Heo | a336ca6 | 2009-05-08 11:54:03 +0900 | [diff] [blame] | 1226 | if (++ReqCnt < blk_rq_cur_sectors(fd_request)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1227 | /* read next sector */ |
| 1228 | setup_req_params( SelectedDrive ); |
| 1229 | do_fd_action( SelectedDrive ); |
| 1230 | } |
| 1231 | else { |
| 1232 | /* all sectors finished */ |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1233 | fd_end_request_cur(BLK_STS_OK); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1234 | } |
| 1235 | return; |
| 1236 | |
| 1237 | err_end: |
| 1238 | BufferDrive = -1; |
| 1239 | fd_error(); |
| 1240 | } |
| 1241 | |
| 1242 | |
| 1243 | static 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 | |
| 1302 | static 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 | |
Arnd Bergmann | 7b8a3d2 | 2014-02-26 12:01:41 +0100 | [diff] [blame] | 1318 | complete(&format_wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1319 | return; |
| 1320 | |
| 1321 | err_end: |
| 1322 | fd_error(); |
| 1323 | } |
| 1324 | |
Kees Cook | 24ed960 | 2017-08-28 11:28:21 -0700 | [diff] [blame] | 1325 | static void fd_times_out(struct timer_list *unused) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 1353 | static 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 | |
| 1372 | static 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(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Tejun Heo | 1a8a74f | 2011-03-09 19:54:27 +0100 | [diff] [blame] | 1411 | static unsigned int floppy_check_events(struct gendisk *disk, |
| 1412 | unsigned int clearing) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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) */ |
Tejun Heo | 1a8a74f | 2011-03-09 19:54:27 +0100 | [diff] [blame] | 1418 | return DISK_EVENT_MEDIA_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1419 | } |
| 1420 | if (test_bit (drive, &changed_floppies)) { |
| 1421 | /* surely changed (the WP signal changed at least once) */ |
Tejun Heo | 1a8a74f | 2011-03-09 19:54:27 +0100 | [diff] [blame] | 1422 | return DISK_EVENT_MEDIA_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1423 | } |
| 1424 | if (UD.wpstat) { |
| 1425 | /* WP is on -> could be changed: to be sure, buffers should be |
| 1426 | * invalidated... |
| 1427 | */ |
Tejun Heo | 1a8a74f | 2011-03-09 19:54:27 +0100 | [diff] [blame] | 1428 | return DISK_EVENT_MEDIA_CHANGE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | } |
| 1430 | |
| 1431 | return 0; |
| 1432 | } |
| 1433 | |
| 1434 | static 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) |
Al Viro | 2c7af51 | 2006-01-12 01:06:29 -0800 | [diff] [blame] | 1451 | UDT = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 1461 | static 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) |
Christoph Hellwig | c8e9078 | 2017-04-20 16:03:13 +0200 | [diff] [blame] | 1472 | read_track = (ReqCmd == READ && fd_request->error_count == 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Jens Axboe | 80ff204 | 2018-11-28 06:09:07 -0700 | [diff] [blame] | 1480 | static 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 | |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 1489 | static blk_status_t ataflop_queue_rq(struct blk_mq_hw_ctx *hctx, |
| 1490 | const struct blk_mq_queue_data *bd) |
Vivek Goyal | 639e2f2 | 2010-09-24 20:35:45 +0200 | [diff] [blame] | 1491 | { |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 1492 | struct atari_floppy_struct *floppy = bd->rq->rq_disk->private_data; |
| 1493 | int drive = floppy - unit; |
| 1494 | int type = floppy->type; |
Vivek Goyal | 639e2f2 | 2010-09-24 20:35:45 +0200 | [diff] [blame] | 1495 | |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 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); |
Vivek Goyal | 639e2f2 | 2010-09-24 20:35:45 +0200 | [diff] [blame] | 1507 | |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 1508 | atari_disable_irq( IRQ_MFP_FDC ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1509 | |
| 1510 | IsFormatting = 0; |
| 1511 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1512 | if (!UD.connected) { |
| 1513 | /* drive not connected */ |
| 1514 | printk(KERN_ERR "Unknown Device: fd%d\n", drive ); |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1515 | fd_end_request_cur(BLK_STS_IOERR); |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 1516 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1517 | } |
| 1518 | |
| 1519 | if (type == 0) { |
| 1520 | if (!UDT) { |
| 1521 | Probing = 1; |
Geert Uytterhoeven | 5ceadd2 | 2008-02-06 01:38:09 -0800 | [diff] [blame] | 1522 | UDT = atari_disk_type + StartDiskType[DriveType]; |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 1523 | set_capacity(bd->rq->rq_disk, UDT->blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 ); |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1531 | fd_end_request_cur(BLK_STS_IOERR); |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 1532 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | } |
| 1534 | if (minor2disktype[type].drive_types > DriveType) { |
| 1535 | printk(KERN_WARNING "fd%d: unsupported disk format", drive ); |
Christoph Hellwig | 2a842ac | 2017-06-03 09:38:04 +0200 | [diff] [blame] | 1536 | fd_end_request_cur(BLK_STS_IOERR); |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 1537 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1538 | } |
| 1539 | type = minor2disktype[type].index; |
Geert Uytterhoeven | 5ceadd2 | 2008-02-06 01:38:09 -0800 | [diff] [blame] | 1540 | UDT = &atari_disk_type[type]; |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 1541 | set_capacity(bd->rq->rq_disk, UDT->blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1542 | UD.autoprobe = 0; |
| 1543 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1544 | |
| 1545 | /* stop deselect timer */ |
| 1546 | del_timer( &motor_off_timer ); |
| 1547 | |
| 1548 | ReqCnt = 0; |
Tejun Heo | a336ca6 | 2009-05-08 11:54:03 +0900 | [diff] [blame] | 1549 | ReqCmd = rq_data_dir(fd_request); |
| 1550 | ReqBlock = blk_rq_pos(fd_request); |
Jens Axboe | b4f42e2 | 2014-04-10 09:46:28 -0600 | [diff] [blame] | 1551 | ReqBuffer = bio_data(fd_request->bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1552 | setup_req_params( drive ); |
| 1553 | do_fd_action( drive ); |
| 1554 | |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 1555 | if (bd->last) |
| 1556 | finish_fdc(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1557 | atari_enable_irq( IRQ_MFP_FDC ); |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 1558 | |
| 1559 | out: |
| 1560 | spin_unlock_irq(&ataflop_lock); |
| 1561 | return BLK_STS_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1562 | } |
| 1563 | |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1564 | static int fd_locked_ioctl(struct block_device *bdev, fmode_t mode, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1565 | unsigned int cmd, unsigned long param) |
| 1566 | { |
Al Viro | 60ad234 | 2008-03-02 09:23:54 -0500 | [diff] [blame] | 1567 | struct gendisk *disk = bdev->bd_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Al Viro | 2c7af51 | 2006-01-12 01:06:29 -0800 | [diff] [blame] | 1576 | void __user *argp = (void __user *)param; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Geert Uytterhoeven | 5ceadd2 | 2008-02-06 01:38:09 -0800 | [diff] [blame] | 1586 | dtp = &atari_disk_type[type]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Al Viro | 2c7af51 | 2006-01-12 01:06:29 -0800 | [diff] [blame] | 1603 | if (copy_to_user(argp, &getprm, sizeof(getprm))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Al Viro | 2c7af51 | 2006-01-12 01:06:29 -0800 | [diff] [blame] | 1622 | if (copy_from_user(&setprm, argp, sizeof(setprm))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Tejun Heo | 1a8a74f | 2011-03-09 19:54:27 +0100 | [diff] [blame] | 1629 | if (floppy_check_events(disk, 0)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Geert Uytterhoeven | 5ceadd2 | 2008-02-06 01:38:09 -0800 | [diff] [blame] | 1656 | dtp = &atari_disk_type[setidx]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 1666 | set_capacity(disk, UDT->blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Julia Lawall | 8f47428 | 2009-07-13 22:43:41 +0200 | [diff] [blame] | 1705 | if (setprm.track != dtp->blocks/dtp->spt/2 || |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 1706 | setprm.head != 2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1707 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | |
| 1709 | UDT = dtp; |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 1710 | set_capacity(disk, UDT->blocks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Al Viro | 2c7af51 | 2006-01-12 01:06:29 -0800 | [diff] [blame] | 1726 | if (copy_from_user(&fmt_desc, argp, sizeof(fmt_desc))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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; |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 1733 | set_capacity(disk, MAX_DISK_SIZE * 2); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1734 | fallthrough; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | case FDFMTEND: |
| 1736 | case FDFLUSH: |
| 1737 | /* invalidate the buffer track to force a reread */ |
| 1738 | BufferDrive = -1; |
| 1739 | set_bit(drive, &fake_change); |
Christoph Hellwig | 22f9716 | 2020-09-08 16:53:31 +0200 | [diff] [blame] | 1740 | if (bdev_check_media_change(bdev)) |
| 1741 | floppy_revalidate(bdev->bd_disk); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | return 0; |
| 1743 | default: |
| 1744 | return -EINVAL; |
| 1745 | } |
| 1746 | } |
| 1747 | |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1748 | static int fd_ioctl(struct block_device *bdev, fmode_t mode, |
| 1749 | unsigned int cmd, unsigned long arg) |
| 1750 | { |
| 1751 | int ret; |
| 1752 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1753 | mutex_lock(&ataflop_mutex); |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1754 | ret = fd_locked_ioctl(bdev, mode, cmd, arg); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1755 | mutex_unlock(&ataflop_mutex); |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1756 | |
| 1757 | return ret; |
| 1758 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1759 | |
| 1760 | /* Initialize the 'unit' variable for drive 'drive' */ |
| 1761 | |
| 1762 | static 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 | |
| 1805 | static 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)) |
Geert Uytterhoeven | 3d92e8f | 2009-02-22 09:38:47 +0100 | [diff] [blame] | 1821 | if (!(st_mfp.par_dt_reg & 0x20)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 ); |
Geert Uytterhoeven | 3d92e8f | 2009-02-22 09:38:47 +0100 | [diff] [blame] | 1838 | while( st_mfp.par_dt_reg & 0x20 ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
| 1852 | static 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 | |
Al Viro | 60ad234 | 2008-03-02 09:23:54 -0500 | [diff] [blame] | 1895 | static int floppy_open(struct block_device *bdev, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1896 | { |
Al Viro | 60ad234 | 2008-03-02 09:23:54 -0500 | [diff] [blame] | 1897 | struct atari_floppy_struct *p = bdev->bd_disk->private_data; |
| 1898 | int type = MINOR(bdev->bd_dev) >> 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1899 | |
| 1900 | DPRINT(("fd_open: type=%d\n",type)); |
| 1901 | if (p->ref && p->type != type) |
| 1902 | return -EBUSY; |
| 1903 | |
Al Viro | 60ad234 | 2008-03-02 09:23:54 -0500 | [diff] [blame] | 1904 | if (p->ref == -1 || (p->ref && mode & FMODE_EXCL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | return -EBUSY; |
| 1906 | |
Al Viro | 60ad234 | 2008-03-02 09:23:54 -0500 | [diff] [blame] | 1907 | if (mode & FMODE_EXCL) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1908 | p->ref = -1; |
| 1909 | else |
| 1910 | p->ref++; |
| 1911 | |
| 1912 | p->type = type; |
| 1913 | |
Al Viro | 60ad234 | 2008-03-02 09:23:54 -0500 | [diff] [blame] | 1914 | if (mode & FMODE_NDELAY) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1915 | return 0; |
| 1916 | |
Al Viro | 60ad234 | 2008-03-02 09:23:54 -0500 | [diff] [blame] | 1917 | if (mode & (FMODE_READ|FMODE_WRITE)) { |
Christoph Hellwig | 22f9716 | 2020-09-08 16:53:31 +0200 | [diff] [blame] | 1918 | if (bdev_check_media_change(bdev)) |
| 1919 | floppy_revalidate(bdev->bd_disk); |
Al Viro | 60ad234 | 2008-03-02 09:23:54 -0500 | [diff] [blame] | 1920 | if (mode & FMODE_WRITE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | if (p->wpstat) { |
| 1922 | if (p->ref < 0) |
| 1923 | p->ref = 0; |
| 1924 | else |
| 1925 | p->ref--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | return -EROFS; |
| 1927 | } |
| 1928 | } |
| 1929 | } |
| 1930 | return 0; |
| 1931 | } |
| 1932 | |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1933 | static int floppy_unlocked_open(struct block_device *bdev, fmode_t mode) |
| 1934 | { |
| 1935 | int ret; |
| 1936 | |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1937 | mutex_lock(&ataflop_mutex); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1938 | ret = floppy_open(bdev, mode); |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1939 | mutex_unlock(&ataflop_mutex); |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1940 | |
| 1941 | return ret; |
| 1942 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1943 | |
Al Viro | db2a144 | 2013-05-05 21:52:57 -0400 | [diff] [blame] | 1944 | static void floppy_release(struct gendisk *disk, fmode_t mode) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | { |
Al Viro | 60ad234 | 2008-03-02 09:23:54 -0500 | [diff] [blame] | 1946 | struct atari_floppy_struct *p = disk->private_data; |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1947 | mutex_lock(&ataflop_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | } |
Arnd Bergmann | 2a48fc0 | 2010-06-02 14:28:52 +0200 | [diff] [blame] | 1954 | mutex_unlock(&ataflop_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | } |
| 1956 | |
Alexey Dobriyan | 83d5cde | 2009-09-21 17:01:13 -0700 | [diff] [blame] | 1957 | static const struct block_device_operations floppy_fops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | .owner = THIS_MODULE, |
Arnd Bergmann | 6e9624b | 2010-08-07 18:25:34 +0200 | [diff] [blame] | 1959 | .open = floppy_unlocked_open, |
Al Viro | 60ad234 | 2008-03-02 09:23:54 -0500 | [diff] [blame] | 1960 | .release = floppy_release, |
Arnd Bergmann | 8a6cfeb | 2010-07-08 10:18:46 +0200 | [diff] [blame] | 1961 | .ioctl = fd_ioctl, |
Tejun Heo | 1a8a74f | 2011-03-09 19:54:27 +0100 | [diff] [blame] | 1962 | .check_events = floppy_check_events, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1963 | }; |
| 1964 | |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 1965 | static const struct blk_mq_ops ataflop_mq_ops = { |
| 1966 | .queue_rq = ataflop_queue_rq, |
Jens Axboe | 80ff204 | 2018-11-28 06:09:07 -0700 | [diff] [blame] | 1967 | .commit_rqs = ataflop_commit_rqs, |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 1968 | }; |
| 1969 | |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 1970 | static int ataflop_alloc_disk(unsigned int drive, unsigned int type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | { |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 1972 | struct gendisk *disk; |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 1973 | |
Christoph Hellwig | fd71c8a | 2021-06-02 09:53:44 +0300 | [diff] [blame] | 1974 | disk = blk_mq_alloc_disk(&unit[drive].tag_set, NULL); |
| 1975 | if (IS_ERR(disk)) |
| 1976 | return PTR_ERR(disk); |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 1977 | |
| 1978 | disk->major = FLOPPY_MAJOR; |
| 1979 | disk->first_minor = drive + (type << 2); |
Christoph Hellwig | fd71c8a | 2021-06-02 09:53:44 +0300 | [diff] [blame] | 1980 | disk->minors = 1; |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 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 | |
| 1991 | static DEFINE_MUTEX(ataflop_probe_lock); |
| 1992 | |
| 1993 | static void ataflop_probe(dev_t dev) |
| 1994 | { |
| 1995 | int drive = MINOR(dev) & 3; |
| 1996 | int type = MINOR(dev) >> 2; |
| 1997 | |
Dan Carpenter | b777f4c | 2021-04-21 13:19:45 +0300 | [diff] [blame] | 1998 | if (type) |
| 1999 | type--; |
| 2000 | |
| 2001 | if (drive >= FD_MAX_UNITS || type >= NUM_DISK_MINORS) |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 2002 | return; |
| 2003 | mutex_lock(&ataflop_probe_lock); |
| 2004 | if (!unit[drive].disk[type]) { |
Luis Chamberlain | 573effb | 2021-09-27 15:03:00 -0700 | [diff] [blame] | 2005 | if (ataflop_alloc_disk(drive, type) == 0) { |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 2006 | add_disk(unit[drive].disk[type]); |
Luis Chamberlain | 573effb | 2021-09-27 15:03:00 -0700 | [diff] [blame] | 2007 | unit[drive].registered[type] = true; |
| 2008 | } |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 2009 | } |
| 2010 | mutex_unlock(&ataflop_probe_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | } |
| 2012 | |
Luis Chamberlain | deae113 | 2021-09-27 15:03:01 -0700 | [diff] [blame] | 2013 | static void atari_cleanup_floppy_disk(struct atari_floppy_struct *fs) |
| 2014 | { |
| 2015 | int type; |
| 2016 | |
| 2017 | for (type = 0; type < NUM_DISK_MINORS; type++) { |
| 2018 | if (!fs->disk[type]) |
| 2019 | continue; |
| 2020 | if (fs->registered[type]) |
| 2021 | del_gendisk(fs->disk[type]); |
| 2022 | blk_cleanup_disk(fs->disk[type]); |
| 2023 | } |
| 2024 | blk_mq_free_tag_set(&fs->tag_set); |
| 2025 | } |
| 2026 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | static int __init atari_floppy_init (void) |
| 2028 | { |
| 2029 | int i; |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 2030 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2031 | |
| 2032 | if (!MACH_IS_ATARI) |
| 2033 | /* Amiga, Mac, ... don't have Atari-compatible floppy :-) */ |
Geert Uytterhoeven | e945b56 | 2008-07-17 21:16:10 +0200 | [diff] [blame] | 2034 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2035 | |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 2036 | mutex_lock(&ataflop_probe_lock); |
| 2037 | ret = __register_blkdev(FLOPPY_MAJOR, "fd", ataflop_probe); |
| 2038 | if (ret) |
| 2039 | goto out_unlock; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2040 | |
| 2041 | for (i = 0; i < FD_MAX_UNITS; i++) { |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 2042 | memset(&unit[i].tag_set, 0, sizeof(unit[i].tag_set)); |
| 2043 | unit[i].tag_set.ops = &ataflop_mq_ops; |
| 2044 | unit[i].tag_set.nr_hw_queues = 1; |
| 2045 | unit[i].tag_set.nr_maps = 1; |
| 2046 | unit[i].tag_set.queue_depth = 2; |
| 2047 | unit[i].tag_set.numa_node = NUMA_NO_NODE; |
| 2048 | unit[i].tag_set.flags = BLK_MQ_F_SHOULD_MERGE; |
| 2049 | ret = blk_mq_alloc_tag_set(&unit[i].tag_set); |
| 2050 | if (ret) |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 2051 | goto err; |
Omar Sandoval | 71327f5 | 2018-10-11 12:20:49 -0700 | [diff] [blame] | 2052 | |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 2053 | ret = ataflop_alloc_disk(i, 0); |
| 2054 | if (ret) { |
| 2055 | blk_mq_free_tag_set(&unit[i].tag_set); |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 2056 | goto err; |
| 2057 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2058 | } |
| 2059 | |
| 2060 | if (UseTrackbuffer < 0) |
| 2061 | /* not set by user -> use default: for now, we turn |
| 2062 | track buffering off for all Medusas, though it |
| 2063 | could be used with ones that have a counter |
| 2064 | card. But the test is too hard :-( */ |
| 2065 | UseTrackbuffer = !MACH_IS_MEDUSA; |
| 2066 | |
| 2067 | /* initialize variables */ |
| 2068 | SelectedDrive = -1; |
| 2069 | BufferDrive = -1; |
| 2070 | |
| 2071 | DMABuffer = atari_stram_alloc(BUFFER_SIZE+512, "ataflop"); |
| 2072 | if (!DMABuffer) { |
| 2073 | printk(KERN_ERR "atari_floppy_init: cannot get dma buffer\n"); |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 2074 | ret = -ENOMEM; |
| 2075 | goto err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | } |
| 2077 | TrackBuffer = DMABuffer + 512; |
Michael Schmitz | a4de73f | 2014-03-31 21:06:07 +1300 | [diff] [blame] | 2078 | PhysDMABuffer = atari_stram_to_phys(DMABuffer); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | PhysTrackBuffer = virt_to_phys(TrackBuffer); |
| 2080 | BufferDrive = BufferSide = BufferTrack = -1; |
| 2081 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2082 | for (i = 0; i < FD_MAX_UNITS; i++) { |
| 2083 | unit[i].track = -1; |
| 2084 | unit[i].flags = 0; |
Luis Chamberlain | 2f15107 | 2021-09-27 15:03:02 -0700 | [diff] [blame^] | 2085 | ret = add_disk(unit[i].disk[0]); |
| 2086 | if (ret) |
| 2087 | goto err_out_dma; |
Luis Chamberlain | 573effb | 2021-09-27 15:03:00 -0700 | [diff] [blame] | 2088 | unit[i].registered[0] = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2089 | } |
| 2090 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | printk(KERN_INFO "Atari floppy driver: max. %cD, %strack buffering\n", |
| 2092 | DriveType == 0 ? 'D' : DriveType == 1 ? 'H' : 'E', |
| 2093 | UseTrackbuffer ? "" : "no "); |
| 2094 | config_types(); |
| 2095 | |
| 2096 | return 0; |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 2097 | |
Luis Chamberlain | 2f15107 | 2021-09-27 15:03:02 -0700 | [diff] [blame^] | 2098 | err_out_dma: |
| 2099 | atari_stram_free(DMABuffer); |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 2100 | err: |
Luis Chamberlain | deae113 | 2021-09-27 15:03:01 -0700 | [diff] [blame] | 2101 | while (--i >= 0) |
| 2102 | atari_cleanup_floppy_disk(&unit[i]); |
Vivek Goyal | 639e2f2 | 2010-09-24 20:35:45 +0200 | [diff] [blame] | 2103 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2104 | unregister_blkdev(FLOPPY_MAJOR, "fd"); |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 2105 | out_unlock: |
| 2106 | mutex_unlock(&ataflop_probe_lock); |
Omar Sandoval | 6ec3938 | 2018-10-15 09:18:24 -0600 | [diff] [blame] | 2107 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | } |
| 2109 | |
Al Viro | 6390743 | 2006-01-12 01:06:36 -0800 | [diff] [blame] | 2110 | #ifndef MODULE |
| 2111 | static int __init atari_floppy_setup(char *str) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2112 | { |
Al Viro | 6390743 | 2006-01-12 01:06:36 -0800 | [diff] [blame] | 2113 | int ints[3 + FD_MAX_UNITS]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | int i; |
Al Viro | 6390743 | 2006-01-12 01:06:36 -0800 | [diff] [blame] | 2115 | |
| 2116 | if (!MACH_IS_ATARI) |
| 2117 | return 0; |
| 2118 | |
| 2119 | str = get_options(str, 3 + FD_MAX_UNITS, ints); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2120 | |
| 2121 | if (ints[0] < 1) { |
| 2122 | printk(KERN_ERR "ataflop_setup: no arguments!\n" ); |
Al Viro | 6390743 | 2006-01-12 01:06:36 -0800 | [diff] [blame] | 2123 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2124 | } |
| 2125 | else if (ints[0] > 2+FD_MAX_UNITS) { |
| 2126 | printk(KERN_ERR "ataflop_setup: too many arguments\n" ); |
| 2127 | } |
| 2128 | |
| 2129 | if (ints[1] < 0 || ints[1] > 2) |
| 2130 | printk(KERN_ERR "ataflop_setup: bad drive type\n" ); |
| 2131 | else |
| 2132 | DriveType = ints[1]; |
| 2133 | |
| 2134 | if (ints[0] >= 2) |
| 2135 | UseTrackbuffer = (ints[2] > 0); |
| 2136 | |
| 2137 | for( i = 3; i <= ints[0] && i-3 < FD_MAX_UNITS; ++i ) { |
| 2138 | if (ints[i] != 2 && ints[i] != 3 && ints[i] != 6 && ints[i] != 12) |
| 2139 | printk(KERN_ERR "ataflop_setup: bad steprate\n" ); |
| 2140 | else |
| 2141 | UserSteprate[i-3] = ints[i]; |
| 2142 | } |
Al Viro | 6390743 | 2006-01-12 01:06:36 -0800 | [diff] [blame] | 2143 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2144 | } |
| 2145 | |
Al Viro | 6390743 | 2006-01-12 01:06:36 -0800 | [diff] [blame] | 2146 | __setup("floppy=", atari_floppy_setup); |
| 2147 | #endif |
| 2148 | |
| 2149 | static void __exit atari_floppy_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | { |
Luis Chamberlain | deae113 | 2021-09-27 15:03:01 -0700 | [diff] [blame] | 2151 | int i; |
Christoph Hellwig | bf9c053 | 2020-10-29 15:58:38 +0100 | [diff] [blame] | 2152 | |
Luis Chamberlain | deae113 | 2021-09-27 15:03:01 -0700 | [diff] [blame] | 2153 | for (i = 0; i < FD_MAX_UNITS; i++) |
| 2154 | atari_cleanup_floppy_disk(&unit[i]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2155 | unregister_blkdev(FLOPPY_MAJOR, "fd"); |
| 2156 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2157 | del_timer_sync(&fd_timer); |
| 2158 | atari_stram_free( DMABuffer ); |
| 2159 | } |
| 2160 | |
| 2161 | module_init(atari_floppy_init) |
| 2162 | module_exit(atari_floppy_exit) |
| 2163 | |
| 2164 | MODULE_LICENSE("GPL"); |