ide-floppy.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * IDE ATAPI floppy driver.
  4. *
  5. * Copyright (C) 1996-1999 Gadi Oxman <gadio@netvision.net.il>
  6. * Copyright (C) 2000-2002 Paul Bristow <paul@paulbristow.net>
  7. * Copyright (C) 2005 Bartlomiej Zolnierkiewicz
  8. *
  9. * This driver supports the following IDE floppy drives:
  10. *
  11. * LS-120/240 SuperDisk
  12. * Iomega Zip 100/250
  13. * Iomega PC Card Clik!/PocketZip
  14. *
  15. * For a historical changelog see
  16. * Documentation/ide/ChangeLog.ide-floppy.1996-2002
  17. */
  18. #include <linux/types.h>
  19. #include <linux/string.h>
  20. #include <linux/kernel.h>
  21. #include <linux/delay.h>
  22. #include <linux/timer.h>
  23. #include <linux/mm.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/major.h>
  26. #include <linux/errno.h>
  27. #include <linux/genhd.h>
  28. #include <linux/cdrom.h>
  29. #include <linux/ide.h>
  30. #include <linux/hdreg.h>
  31. #include <linux/bitops.h>
  32. #include <linux/mutex.h>
  33. #include <linux/scatterlist.h>
  34. #include <scsi/scsi_ioctl.h>
  35. #include <asm/byteorder.h>
  36. #include <linux/uaccess.h>
  37. #include <linux/io.h>
  38. #include <asm/unaligned.h>
  39. #include "ide-floppy.h"
  40. /*
  41. * After each failed packet command we issue a request sense command and retry
  42. * the packet command IDEFLOPPY_MAX_PC_RETRIES times.
  43. */
  44. #define IDEFLOPPY_MAX_PC_RETRIES 3
  45. /* format capacities descriptor codes */
  46. #define CAPACITY_INVALID 0x00
  47. #define CAPACITY_UNFORMATTED 0x01
  48. #define CAPACITY_CURRENT 0x02
  49. #define CAPACITY_NO_CARTRIDGE 0x03
  50. /*
  51. * The following delay solves a problem with ATAPI Zip 100 drive where BSY bit
  52. * was apparently being deasserted before the unit was ready to receive data.
  53. */
  54. #define IDEFLOPPY_PC_DELAY (HZ/20) /* default delay for ZIP 100 (50ms) */
  55. static int ide_floppy_callback(ide_drive_t *drive, int dsc)
  56. {
  57. struct ide_disk_obj *floppy = drive->driver_data;
  58. struct ide_atapi_pc *pc = drive->pc;
  59. struct request *rq = pc->rq;
  60. int uptodate = pc->error ? 0 : 1;
  61. ide_debug_log(IDE_DBG_FUNC, "enter");
  62. if (drive->failed_pc == pc)
  63. drive->failed_pc = NULL;
  64. if (pc->c[0] == GPCMD_READ_10 || pc->c[0] == GPCMD_WRITE_10 ||
  65. blk_rq_is_scsi(rq))
  66. uptodate = 1; /* FIXME */
  67. else if (pc->c[0] == GPCMD_REQUEST_SENSE) {
  68. u8 *buf = bio_data(rq->bio);
  69. if (!pc->error) {
  70. floppy->sense_key = buf[2] & 0x0F;
  71. floppy->asc = buf[12];
  72. floppy->ascq = buf[13];
  73. floppy->progress_indication = buf[15] & 0x80 ?
  74. (u16)get_unaligned((u16 *)&buf[16]) : 0x10000;
  75. if (drive->failed_pc)
  76. ide_debug_log(IDE_DBG_PC, "pc = %x",
  77. drive->failed_pc->c[0]);
  78. ide_debug_log(IDE_DBG_SENSE, "sense key = %x, asc = %x,"
  79. "ascq = %x", floppy->sense_key,
  80. floppy->asc, floppy->ascq);
  81. } else
  82. printk(KERN_ERR PFX "Error in REQUEST SENSE itself - "
  83. "Aborting request!\n");
  84. }
  85. if (ata_misc_request(rq))
  86. scsi_req(rq)->result = uptodate ? 0 : IDE_DRV_ERROR_GENERAL;
  87. return uptodate;
  88. }
  89. static void ide_floppy_report_error(struct ide_disk_obj *floppy,
  90. struct ide_atapi_pc *pc)
  91. {
  92. /* suppress error messages resulting from Medium not present */
  93. if (floppy->sense_key == 0x02 &&
  94. floppy->asc == 0x3a &&
  95. floppy->ascq == 0x00)
  96. return;
  97. printk(KERN_ERR PFX "%s: I/O error, pc = %2x, key = %2x, "
  98. "asc = %2x, ascq = %2x\n",
  99. floppy->drive->name, pc->c[0], floppy->sense_key,
  100. floppy->asc, floppy->ascq);
  101. }
  102. static ide_startstop_t ide_floppy_issue_pc(ide_drive_t *drive,
  103. struct ide_cmd *cmd,
  104. struct ide_atapi_pc *pc)
  105. {
  106. struct ide_disk_obj *floppy = drive->driver_data;
  107. if (drive->failed_pc == NULL &&
  108. pc->c[0] != GPCMD_REQUEST_SENSE)
  109. drive->failed_pc = pc;
  110. /* Set the current packet command */
  111. drive->pc = pc;
  112. if (pc->retries > IDEFLOPPY_MAX_PC_RETRIES) {
  113. unsigned int done = blk_rq_bytes(drive->hwif->rq);
  114. if (!(pc->flags & PC_FLAG_SUPPRESS_ERROR))
  115. ide_floppy_report_error(floppy, pc);
  116. /* Giving up */
  117. pc->error = IDE_DRV_ERROR_GENERAL;
  118. drive->failed_pc = NULL;
  119. drive->pc_callback(drive, 0);
  120. ide_complete_rq(drive, BLK_STS_IOERR, done);
  121. return ide_stopped;
  122. }
  123. ide_debug_log(IDE_DBG_FUNC, "retry #%d", pc->retries);
  124. pc->retries++;
  125. return ide_issue_pc(drive, cmd);
  126. }
  127. void ide_floppy_create_read_capacity_cmd(struct ide_atapi_pc *pc)
  128. {
  129. ide_init_pc(pc);
  130. pc->c[0] = GPCMD_READ_FORMAT_CAPACITIES;
  131. pc->c[7] = 255;
  132. pc->c[8] = 255;
  133. pc->req_xfer = 255;
  134. }
  135. /* A mode sense command is used to "sense" floppy parameters. */
  136. void ide_floppy_create_mode_sense_cmd(struct ide_atapi_pc *pc, u8 page_code)
  137. {
  138. u16 length = 8; /* sizeof(Mode Parameter Header) = 8 Bytes */
  139. ide_init_pc(pc);
  140. pc->c[0] = GPCMD_MODE_SENSE_10;
  141. pc->c[1] = 0;
  142. pc->c[2] = page_code;
  143. switch (page_code) {
  144. case IDEFLOPPY_CAPABILITIES_PAGE:
  145. length += 12;
  146. break;
  147. case IDEFLOPPY_FLEXIBLE_DISK_PAGE:
  148. length += 32;
  149. break;
  150. default:
  151. printk(KERN_ERR PFX "unsupported page code in %s\n", __func__);
  152. }
  153. put_unaligned(cpu_to_be16(length), (u16 *) &pc->c[7]);
  154. pc->req_xfer = length;
  155. }
  156. static void idefloppy_create_rw_cmd(ide_drive_t *drive,
  157. struct ide_atapi_pc *pc, struct request *rq,
  158. unsigned long sector)
  159. {
  160. struct ide_disk_obj *floppy = drive->driver_data;
  161. int block = sector / floppy->bs_factor;
  162. int blocks = blk_rq_sectors(rq) / floppy->bs_factor;
  163. int cmd = rq_data_dir(rq);
  164. ide_debug_log(IDE_DBG_FUNC, "block: %d, blocks: %d", block, blocks);
  165. ide_init_pc(pc);
  166. pc->c[0] = cmd == READ ? GPCMD_READ_10 : GPCMD_WRITE_10;
  167. put_unaligned(cpu_to_be16(blocks), (unsigned short *)&pc->c[7]);
  168. put_unaligned(cpu_to_be32(block), (unsigned int *) &pc->c[2]);
  169. memcpy(scsi_req(rq)->cmd, pc->c, 12);
  170. pc->rq = rq;
  171. if (cmd == WRITE)
  172. pc->flags |= PC_FLAG_WRITING;
  173. pc->flags |= PC_FLAG_DMA_OK;
  174. }
  175. static void idefloppy_blockpc_cmd(struct ide_disk_obj *floppy,
  176. struct ide_atapi_pc *pc, struct request *rq)
  177. {
  178. ide_init_pc(pc);
  179. memcpy(pc->c, scsi_req(rq)->cmd, sizeof(pc->c));
  180. pc->rq = rq;
  181. if (blk_rq_bytes(rq)) {
  182. pc->flags |= PC_FLAG_DMA_OK;
  183. if (rq_data_dir(rq) == WRITE)
  184. pc->flags |= PC_FLAG_WRITING;
  185. }
  186. }
  187. static ide_startstop_t ide_floppy_do_request(ide_drive_t *drive,
  188. struct request *rq, sector_t block)
  189. {
  190. struct ide_disk_obj *floppy = drive->driver_data;
  191. struct ide_cmd cmd;
  192. struct ide_atapi_pc *pc;
  193. ide_debug_log(IDE_DBG_FUNC, "enter, cmd: 0x%x\n", rq->cmd[0]);
  194. if (drive->debug_mask & IDE_DBG_RQ)
  195. blk_dump_rq_flags(rq, (rq->rq_disk
  196. ? rq->rq_disk->disk_name
  197. : "dev?"));
  198. if (scsi_req(rq)->result >= ERROR_MAX) {
  199. if (drive->failed_pc) {
  200. ide_floppy_report_error(floppy, drive->failed_pc);
  201. drive->failed_pc = NULL;
  202. } else
  203. printk(KERN_ERR PFX "%s: I/O error\n", drive->name);
  204. if (ata_misc_request(rq)) {
  205. scsi_req(rq)->result = 0;
  206. ide_complete_rq(drive, BLK_STS_OK, blk_rq_bytes(rq));
  207. return ide_stopped;
  208. } else
  209. goto out_end;
  210. }
  211. switch (req_op(rq)) {
  212. default:
  213. if (((long)blk_rq_pos(rq) % floppy->bs_factor) ||
  214. (blk_rq_sectors(rq) % floppy->bs_factor)) {
  215. printk(KERN_ERR PFX "%s: unsupported r/w rq size\n",
  216. drive->name);
  217. goto out_end;
  218. }
  219. pc = &floppy->queued_pc;
  220. idefloppy_create_rw_cmd(drive, pc, rq, (unsigned long)block);
  221. break;
  222. case REQ_OP_SCSI_IN:
  223. case REQ_OP_SCSI_OUT:
  224. pc = &floppy->queued_pc;
  225. idefloppy_blockpc_cmd(floppy, pc, rq);
  226. break;
  227. case REQ_OP_DRV_IN:
  228. case REQ_OP_DRV_OUT:
  229. switch (ide_req(rq)->type) {
  230. case ATA_PRIV_MISC:
  231. case ATA_PRIV_SENSE:
  232. pc = (struct ide_atapi_pc *)rq->special;
  233. break;
  234. default:
  235. BUG();
  236. }
  237. }
  238. ide_prep_sense(drive, rq);
  239. memset(&cmd, 0, sizeof(cmd));
  240. if (rq_data_dir(rq))
  241. cmd.tf_flags |= IDE_TFLAG_WRITE;
  242. cmd.rq = rq;
  243. if (!blk_rq_is_passthrough(rq) || blk_rq_bytes(rq)) {
  244. ide_init_sg_cmd(&cmd, blk_rq_bytes(rq));
  245. ide_map_sg(drive, &cmd);
  246. }
  247. pc->rq = rq;
  248. return ide_floppy_issue_pc(drive, &cmd, pc);
  249. out_end:
  250. drive->failed_pc = NULL;
  251. if (blk_rq_is_passthrough(rq) && scsi_req(rq)->result == 0)
  252. scsi_req(rq)->result = -EIO;
  253. ide_complete_rq(drive, BLK_STS_IOERR, blk_rq_bytes(rq));
  254. return ide_stopped;
  255. }
  256. /*
  257. * Look at the flexible disk page parameters. We ignore the CHS capacity
  258. * parameters and use the LBA parameters instead.
  259. */
  260. static int ide_floppy_get_flexible_disk_page(ide_drive_t *drive,
  261. struct ide_atapi_pc *pc)
  262. {
  263. struct ide_disk_obj *floppy = drive->driver_data;
  264. struct gendisk *disk = floppy->disk;
  265. u8 *page, buf[40];
  266. int capacity, lba_capacity;
  267. u16 transfer_rate, sector_size, cyls, rpm;
  268. u8 heads, sectors;
  269. ide_floppy_create_mode_sense_cmd(pc, IDEFLOPPY_FLEXIBLE_DISK_PAGE);
  270. if (ide_queue_pc_tail(drive, disk, pc, buf, pc->req_xfer)) {
  271. printk(KERN_ERR PFX "Can't get flexible disk page params\n");
  272. return 1;
  273. }
  274. if (buf[3] & 0x80)
  275. drive->dev_flags |= IDE_DFLAG_WP;
  276. else
  277. drive->dev_flags &= ~IDE_DFLAG_WP;
  278. set_disk_ro(disk, !!(drive->dev_flags & IDE_DFLAG_WP));
  279. page = &buf[8];
  280. transfer_rate = be16_to_cpup((__be16 *)&buf[8 + 2]);
  281. sector_size = be16_to_cpup((__be16 *)&buf[8 + 6]);
  282. cyls = be16_to_cpup((__be16 *)&buf[8 + 8]);
  283. rpm = be16_to_cpup((__be16 *)&buf[8 + 28]);
  284. heads = buf[8 + 4];
  285. sectors = buf[8 + 5];
  286. capacity = cyls * heads * sectors * sector_size;
  287. if (memcmp(page, &floppy->flexible_disk_page, 32))
  288. printk(KERN_INFO PFX "%s: %dkB, %d/%d/%d CHS, %d kBps, "
  289. "%d sector size, %d rpm\n",
  290. drive->name, capacity / 1024, cyls, heads,
  291. sectors, transfer_rate / 8, sector_size, rpm);
  292. memcpy(&floppy->flexible_disk_page, page, 32);
  293. drive->bios_cyl = cyls;
  294. drive->bios_head = heads;
  295. drive->bios_sect = sectors;
  296. lba_capacity = floppy->blocks * floppy->block_size;
  297. if (capacity < lba_capacity) {
  298. printk(KERN_NOTICE PFX "%s: The disk reports a capacity of %d "
  299. "bytes, but the drive only handles %d\n",
  300. drive->name, lba_capacity, capacity);
  301. floppy->blocks = floppy->block_size ?
  302. capacity / floppy->block_size : 0;
  303. drive->capacity64 = floppy->blocks * floppy->bs_factor;
  304. }
  305. return 0;
  306. }
  307. /*
  308. * Determine if a media is present in the floppy drive, and if so, its LBA
  309. * capacity.
  310. */
  311. static int ide_floppy_get_capacity(ide_drive_t *drive)
  312. {
  313. struct ide_disk_obj *floppy = drive->driver_data;
  314. struct gendisk *disk = floppy->disk;
  315. struct ide_atapi_pc pc;
  316. u8 *cap_desc;
  317. u8 pc_buf[256], header_len, desc_cnt;
  318. int i, rc = 1, blocks, length;
  319. ide_debug_log(IDE_DBG_FUNC, "enter");
  320. drive->bios_cyl = 0;
  321. drive->bios_head = drive->bios_sect = 0;
  322. floppy->blocks = 0;
  323. floppy->bs_factor = 1;
  324. drive->capacity64 = 0;
  325. ide_floppy_create_read_capacity_cmd(&pc);
  326. if (ide_queue_pc_tail(drive, disk, &pc, pc_buf, pc.req_xfer)) {
  327. printk(KERN_ERR PFX "Can't get floppy parameters\n");
  328. return 1;
  329. }
  330. header_len = pc_buf[3];
  331. cap_desc = &pc_buf[4];
  332. desc_cnt = header_len / 8; /* capacity descriptor of 8 bytes */
  333. for (i = 0; i < desc_cnt; i++) {
  334. unsigned int desc_start = 4 + i*8;
  335. blocks = be32_to_cpup((__be32 *)&pc_buf[desc_start]);
  336. length = be16_to_cpup((__be16 *)&pc_buf[desc_start + 6]);
  337. ide_debug_log(IDE_DBG_PROBE, "Descriptor %d: %dkB, %d blocks, "
  338. "%d sector size",
  339. i, blocks * length / 1024,
  340. blocks, length);
  341. if (i)
  342. continue;
  343. /*
  344. * the code below is valid only for the 1st descriptor, ie i=0
  345. */
  346. switch (pc_buf[desc_start + 4] & 0x03) {
  347. /* Clik! drive returns this instead of CAPACITY_CURRENT */
  348. case CAPACITY_UNFORMATTED:
  349. if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
  350. /*
  351. * If it is not a clik drive, break out
  352. * (maintains previous driver behaviour)
  353. */
  354. break;
  355. case CAPACITY_CURRENT:
  356. /* Normal Zip/LS-120 disks */
  357. if (memcmp(cap_desc, &floppy->cap_desc, 8))
  358. printk(KERN_INFO PFX "%s: %dkB, %d blocks, %d "
  359. "sector size\n",
  360. drive->name, blocks * length / 1024,
  361. blocks, length);
  362. memcpy(&floppy->cap_desc, cap_desc, 8);
  363. if (!length || length % 512) {
  364. printk(KERN_NOTICE PFX "%s: %d bytes block size"
  365. " not supported\n", drive->name, length);
  366. } else {
  367. floppy->blocks = blocks;
  368. floppy->block_size = length;
  369. floppy->bs_factor = length / 512;
  370. if (floppy->bs_factor != 1)
  371. printk(KERN_NOTICE PFX "%s: Warning: "
  372. "non 512 bytes block size not "
  373. "fully supported\n",
  374. drive->name);
  375. drive->capacity64 =
  376. floppy->blocks * floppy->bs_factor;
  377. rc = 0;
  378. }
  379. break;
  380. case CAPACITY_NO_CARTRIDGE:
  381. /*
  382. * This is a KERN_ERR so it appears on screen
  383. * for the user to see
  384. */
  385. printk(KERN_ERR PFX "%s: No disk in drive\n",
  386. drive->name);
  387. break;
  388. case CAPACITY_INVALID:
  389. printk(KERN_ERR PFX "%s: Invalid capacity for disk "
  390. "in drive\n", drive->name);
  391. break;
  392. }
  393. ide_debug_log(IDE_DBG_PROBE, "Descriptor 0 Code: %d",
  394. pc_buf[desc_start + 4] & 0x03);
  395. }
  396. /* Clik! disk does not support get_flexible_disk_page */
  397. if (!(drive->atapi_flags & IDE_AFLAG_CLIK_DRIVE))
  398. (void) ide_floppy_get_flexible_disk_page(drive, &pc);
  399. return rc;
  400. }
  401. static void ide_floppy_setup(ide_drive_t *drive)
  402. {
  403. struct ide_disk_obj *floppy = drive->driver_data;
  404. u16 *id = drive->id;
  405. drive->pc_callback = ide_floppy_callback;
  406. /*
  407. * We used to check revisions here. At this point however I'm giving up.
  408. * Just assume they are all broken, its easier.
  409. *
  410. * The actual reason for the workarounds was likely a driver bug after
  411. * all rather than a firmware bug, and the workaround below used to hide
  412. * it. It should be fixed as of version 1.9, but to be on the safe side
  413. * we'll leave the limitation below for the 2.2.x tree.
  414. */
  415. if (strstarts((char *)&id[ATA_ID_PROD], "IOMEGA ZIP 100 ATAPI")) {
  416. drive->atapi_flags |= IDE_AFLAG_ZIP_DRIVE;
  417. /* This value will be visible in the /proc/ide/hdx/settings */
  418. drive->pc_delay = IDEFLOPPY_PC_DELAY;
  419. blk_queue_max_hw_sectors(drive->queue, 64);
  420. }
  421. /*
  422. * Guess what? The IOMEGA Clik! drive also needs the above fix. It makes
  423. * nasty clicking noises without it, so please don't remove this.
  424. */
  425. if (strstarts((char *)&id[ATA_ID_PROD], "IOMEGA Clik!")) {
  426. blk_queue_max_hw_sectors(drive->queue, 64);
  427. drive->atapi_flags |= IDE_AFLAG_CLIK_DRIVE;
  428. /* IOMEGA Clik! drives do not support lock/unlock commands */
  429. drive->dev_flags &= ~IDE_DFLAG_DOORLOCKING;
  430. }
  431. (void) ide_floppy_get_capacity(drive);
  432. ide_proc_register_driver(drive, floppy->driver);
  433. drive->dev_flags |= IDE_DFLAG_ATTACH;
  434. }
  435. static void ide_floppy_flush(ide_drive_t *drive)
  436. {
  437. }
  438. static int ide_floppy_init_media(ide_drive_t *drive, struct gendisk *disk)
  439. {
  440. int ret = 0;
  441. if (ide_do_test_unit_ready(drive, disk))
  442. ide_do_start_stop(drive, disk, 1);
  443. ret = ide_floppy_get_capacity(drive);
  444. set_capacity(disk, ide_gd_capacity(drive));
  445. return ret;
  446. }
  447. const struct ide_disk_ops ide_atapi_disk_ops = {
  448. .check = ide_check_atapi_device,
  449. .get_capacity = ide_floppy_get_capacity,
  450. .setup = ide_floppy_setup,
  451. .flush = ide_floppy_flush,
  452. .init_media = ide_floppy_init_media,
  453. .set_doorlock = ide_set_media_lock,
  454. .do_request = ide_floppy_do_request,
  455. .ioctl = ide_floppy_ioctl,
  456. };