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