1 /*
2  *  linux/drivers/ide/ide-disk.c	Version 1.18	Mar 05, 2003
3  *
4  *  Copyright (C) 1994-1998  Linus Torvalds & authors (see below)
5  *  Copyright (C) 1998-2002  Linux ATA Developemt
6  *				Andre Hedrick <andre@linux-ide.org>
7  *  Copyright (C) 2003	     Red Hat <alan@redhat.com>
8  *
9  *
10  */
11 
12 /*
13  *  Mostly written by Mark Lord <mlord@pobox.com>
14  *                and Gadi Oxman <gadio@netvision.net.il>
15  *                and Andre Hedrick <andre@linux-ide.org>
16  *
17  * This is the IDE/ATA disk driver, as evolved from hd.c and ide.c.
18  *
19  * Version 1.00		move disk only code from ide.c to ide-disk.c
20  *			support optional byte-swapping of all data
21  * Version 1.01		fix previous byte-swapping code
22  * Version 1.02		remove ", LBA" from drive identification msgs
23  * Version 1.03		fix display of id->buf_size for big-endian
24  * Version 1.04		add /proc configurable settings and S.M.A.R.T support
25  * Version 1.05		add capacity support for ATA3 >= 8GB
26  * Version 1.06		get boot-up messages to show full cyl count
27  * Version 1.07		disable door-locking if it fails
28  * Version 1.08		fixed CHS/LBA translations for ATA4 > 8GB,
29  *			process of adding new ATA4 compliance.
30  *			fixed problems in allowing fdisk to see
31  *			the entire disk.
32  * Version 1.09		added increment of rq->sector in ide_multwrite
33  *			added UDMA 3/4 reporting
34  * Version 1.10		request queue changes, Ultra DMA 100
35  * Version 1.11		added 48-bit lba
36  * Version 1.12		adding taskfile io access method
37  * Version 1.13		added standby and flush-cache for notifier
38  * Version 1.14		added acoustic-wcache
39  * Version 1.15		convert all calls to ide_raw_taskfile
40  *				since args will return register content.
41  * Version 1.16		added suspend-resume-checkpower
42  * Version 1.17		do flush on standy, do flush on ATA < ATA6
43  *			fix wcache setup.
44  */
45 
46 #define IDEDISK_VERSION	"1.17"
47 
48 #undef REALLY_SLOW_IO		/* most systems can safely undef this */
49 
50 #include <linux/config.h>
51 #include <linux/module.h>
52 #include <linux/types.h>
53 #include <linux/string.h>
54 #include <linux/kernel.h>
55 #include <linux/timer.h>
56 #include <linux/mm.h>
57 #include <linux/interrupt.h>
58 #include <linux/major.h>
59 #include <linux/errno.h>
60 #include <linux/genhd.h>
61 #include <linux/slab.h>
62 #include <linux/delay.h>
63 
64 #define _IDE_DISK
65 
66 #include <linux/ide.h>
67 
68 #include <asm/byteorder.h>
69 #include <asm/irq.h>
70 #include <asm/uaccess.h>
71 #include <asm/io.h>
72 
73 /* FIXME: some day we shouldnt need to look in here! */
74 
75 #include "legacy/pdc4030.h"
76 
77 static int driver_blocked;
78 
idedisk_read_24(ide_drive_t * drive)79 static inline u32 idedisk_read_24 (ide_drive_t *drive)
80 {
81 #if 0
82 	return	(HWIF(drive)->INB(IDE_HCYL_REG)<<16) |
83 		(HWIF(drive)->INB(IDE_LCYL_REG)<<8) |
84 		 HWIF(drive)->INB(IDE_SECTOR_REG);
85 #else
86 	u8 hcyl = HWIF(drive)->INB(IDE_HCYL_REG);
87 	u8 lcyl = HWIF(drive)->INB(IDE_LCYL_REG);
88 	u8 sect = HWIF(drive)->INB(IDE_SECTOR_REG);
89 	return (hcyl<<16)|(lcyl<<8)|sect;
90 #endif
91 }
92 
93 static int idedisk_end_request(ide_drive_t *drive, int uptodate);
94 
95 /*
96  * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
97  * value for this drive (from its reported identification information).
98  *
99  * Returns:	1 if lba_capacity looks sensible
100  *		0 otherwise
101  *
102  * It is called only once for each drive.
103  */
lba_capacity_is_ok(struct hd_driveid * id)104 static int lba_capacity_is_ok (struct hd_driveid *id)
105 {
106 	unsigned long lba_sects, chs_sects, head, tail;
107 
108 	if ((id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)) {
109 		printk("48-bit Drive: %llu \n", id->lba_capacity_2);
110 		return 1;
111 	}
112 
113 	/*
114 	 * The ATA spec tells large drives to return
115 	 * C/H/S = 16383/16/63 independent of their size.
116 	 * Some drives can be jumpered to use 15 heads instead of 16.
117 	 * Some drives can be jumpered to use 4092 cyls instead of 16383.
118 	 */
119 	if ((id->cyls == 16383
120 	     || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
121 	    id->sectors == 63 &&
122 	    (id->heads == 15 || id->heads == 16) &&
123 	    (id->lba_capacity >= 16383*63*id->heads))
124 		return 1;
125 
126 	lba_sects   = id->lba_capacity;
127 	chs_sects   = id->cyls * id->heads * id->sectors;
128 
129 	/* perform a rough sanity check on lba_sects:  within 10% is OK */
130 	if ((lba_sects - chs_sects) < chs_sects/10)
131 		return 1;
132 
133 	/* some drives have the word order reversed */
134 	head = ((lba_sects >> 16) & 0xffff);
135 	tail = (lba_sects & 0xffff);
136 	lba_sects = (head | (tail << 16));
137 	if ((lba_sects - chs_sects) < chs_sects/10) {
138 		id->lba_capacity = lba_sects;
139 		return 1;	/* lba_capacity is (now) good */
140 	}
141 
142 	return 0;	/* lba_capacity value may be bad */
143 }
144 
145 #ifndef CONFIG_IDE_TASKFILE_IO
146 
147 /*
148  * read_intr() is the handler for disk read/multread interrupts
149  */
read_intr(ide_drive_t * drive)150 static ide_startstop_t read_intr (ide_drive_t *drive)
151 {
152 	ide_hwif_t *hwif	= HWIF(drive);
153 	u32 i = 0, nsect	= 0, msect = drive->mult_count;
154 	struct request *rq;
155 	unsigned long flags;
156 	u8 stat;
157 	char *to;
158 
159 	/* new way for dealing with premature shared PCI interrupts */
160 	if (!OK_STAT(stat=hwif->INB(IDE_STATUS_REG),DATA_READY,BAD_R_STAT)) {
161 		if (stat & (ERR_STAT|DRQ_STAT)) {
162 			return DRIVER(drive)->error(drive, "read_intr", stat);
163 		}
164 		/* no data yet, so wait for another interrupt */
165 		ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
166 		return ide_started;
167 	}
168 
169 read_next:
170 	rq = HWGROUP(drive)->rq;
171 	if (msect) {
172 		if ((nsect = rq->current_nr_sectors) > msect)
173 			nsect = msect;
174 		msect -= nsect;
175 	} else
176 		nsect = 1;
177 	to = ide_map_buffer(rq, &flags);
178 	taskfile_input_data(drive, to, nsect * SECTOR_WORDS);
179 
180 #ifdef DEBUG
181 	printk("%s:  read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
182 		drive->name, rq->sector, rq->sector+nsect-1,
183 		(unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
184 #endif
185 
186 	ide_unmap_buffer(to, &flags);
187 	rq->sector += nsect;
188 	rq->errors = 0;
189 	i = (rq->nr_sectors -= nsect);
190 	if (((long)(rq->current_nr_sectors -= nsect)) <= 0)
191 		idedisk_end_request(drive, 1);
192 	/*
193 	 * Another BH Page walker and DATA INTERGRITY Questioned on ERROR.
194 	 * If passed back up on multimode read, BAD DATA could be ACKED
195 	 * to FILE SYSTEMS above ...
196 	 */
197 	if (i > 0) {
198 		if (msect)
199 			goto read_next;
200 		ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
201                 return ide_started;
202 	}
203         return ide_stopped;
204 }
205 
206 /*
207  * write_intr() is the handler for disk write interrupts
208  */
write_intr(ide_drive_t * drive)209 static ide_startstop_t write_intr (ide_drive_t *drive)
210 {
211 	ide_hwgroup_t *hwgroup	= HWGROUP(drive);
212 	ide_hwif_t *hwif	= HWIF(drive);
213 	struct request *rq	= hwgroup->rq;
214 	u32 i = 0;
215 	u8 stat;
216 
217 	if (!OK_STAT(stat = hwif->INB(IDE_STATUS_REG),
218 			DRIVE_READY, drive->bad_wstat)) {
219 		printk("%s: write_intr error1: nr_sectors=%ld, stat=0x%02x\n",
220 			drive->name, rq->nr_sectors, stat);
221         } else {
222 #ifdef DEBUG
223 		printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
224 			drive->name, rq->sector, (unsigned long) rq->buffer,
225 			rq->nr_sectors-1);
226 #endif
227 		if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
228 			rq->sector++;
229 			rq->errors = 0;
230 			i = --rq->nr_sectors;
231 			--rq->current_nr_sectors;
232 			if (((long)rq->current_nr_sectors) <= 0)
233 				idedisk_end_request(drive, 1);
234 			if (i > 0) {
235 				unsigned long flags;
236 				char *to = ide_map_buffer(rq, &flags);
237 				taskfile_output_data(drive, to, SECTOR_WORDS);
238 				ide_unmap_buffer(to, &flags);
239 				ide_set_handler(drive, &write_intr, WAIT_CMD, NULL);
240                                 return ide_started;
241 			}
242                         return ide_stopped;
243 		}
244 		/* the original code did this here (?) */
245 		return ide_stopped;
246 	}
247 	return DRIVER(drive)->error(drive, "write_intr", stat);
248 }
249 
250 /*
251  * ide_multwrite() transfers a block of up to mcount sectors of data
252  * to a drive as part of a disk multiple-sector write operation.
253  *
254  * Returns 0 on success.
255  *
256  * Note that we may be called from two contexts - the do_rw_disk context
257  * and IRQ context. The IRQ can happen any time after we've output the
258  * full "mcount" number of sectors, so we must make sure we update the
259  * state _before_ we output the final part of the data!
260  *
261  * The update and return to BH is a BLOCK Layer Fakey to get more data
262  * to satisfy the hardware atomic segment.  If the hardware atomic segment
263  * is shorter or smaller than the BH segment then we should be OKAY.
264  * This is only valid if we can rewind the rq->current_nr_sectors counter.
265  */
ide_multwrite(ide_drive_t * drive,unsigned int mcount)266 int ide_multwrite (ide_drive_t *drive, unsigned int mcount)
267 {
268  	ide_hwgroup_t *hwgroup	= HWGROUP(drive);
269  	struct request *rq	= &hwgroup->wrq;
270 
271   	do {
272   		char *buffer;
273   		int nsect = rq->current_nr_sectors;
274 		unsigned long flags;
275 
276 		if (nsect > mcount)
277 			nsect = mcount;
278 		mcount -= nsect;
279 		buffer = ide_map_buffer(rq, &flags);
280 
281 		rq->sector += nsect;
282 		rq->nr_sectors -= nsect;
283 		rq->current_nr_sectors -= nsect;
284 
285 		/* Do we move to the next bh after this? */
286 		if (!rq->current_nr_sectors) {
287 			struct buffer_head *bh = rq->bh->b_reqnext;
288 
289 			/* end early early we ran out of requests */
290 			if (!bh) {
291 				mcount = 0;
292 			} else {
293 				rq->bh = bh;
294 				rq->current_nr_sectors = bh->b_size >> 9;
295 				rq->hard_cur_sectors = rq->current_nr_sectors;
296 				rq->buffer             = bh->b_data;
297 			}
298 		}
299 
300 		/*
301 		 * Ok, we're all setup for the interrupt
302 		 * re-entering us on the last transfer.
303 		 */
304 		taskfile_output_data(drive, buffer, nsect<<7);
305 		ide_unmap_buffer(buffer, &flags);
306 	} while (mcount);
307 
308         return 0;
309 }
310 
311 /*
312  * multwrite_intr() is the handler for disk multwrite interrupts
313  */
multwrite_intr(ide_drive_t * drive)314 static ide_startstop_t multwrite_intr (ide_drive_t *drive)
315 {
316 	ide_hwgroup_t *hwgroup	= HWGROUP(drive);
317 	ide_hwif_t *hwif	= HWIF(drive);
318 	struct request *rq	= &hwgroup->wrq;
319 	u32 i = 0;
320 	u8 stat;
321 
322 	if (OK_STAT(stat = hwif->INB(IDE_STATUS_REG),
323 			DRIVE_READY, drive->bad_wstat)) {
324 		if (stat & DRQ_STAT) {
325 			/*
326 			 *	The drive wants data. Remember rq is the copy
327 			 *	of the request
328 			 */
329 			if (rq->nr_sectors) {
330 				if (ide_multwrite(drive, drive->mult_count))
331 					return ide_stopped;
332 				ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
333 				return ide_started;
334 			}
335 		} else {
336 			/*
337 			 *	If the copy has all the blocks completed then
338 			 *	we can end the original request.
339 			 */
340 			if (!rq->nr_sectors) {	/* all done? */
341 				rq = hwgroup->rq;
342 				for (i = rq->nr_sectors; i > 0;) {
343 					i -= rq->current_nr_sectors;
344 					idedisk_end_request(drive, 1);
345 				}
346 				return ide_stopped;
347 			}
348 		}
349 		/* the original code did this here (?) */
350 		return ide_stopped;
351 	}
352 	return DRIVER(drive)->error(drive, "multwrite_intr", stat);
353 }
354 
355 
356 /*
357  * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
358  * using LBA if supported, or CHS otherwise, to address sectors.
359  * It also takes care of issuing special DRIVE_CMDs.
360  */
361 
__ide_do_rw_disk(ide_drive_t * drive,struct request * rq,unsigned long block)362 ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
363 {
364 	ide_hwif_t *hwif	= HWIF(drive);
365 	u8 lba48		= (drive->addressing == 1) ? 1 : 0;
366 	task_ioreg_t command	= WIN_NOP;
367 	ata_nsector_t		nsectors;
368 
369 	nsectors.all		= (u16) rq->nr_sectors;
370 
371 	if (driver_blocked)
372 		panic("Request while ide driver is blocked?");
373 
374 	if (IDE_CONTROL_REG)
375 		hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
376 
377 	if (drive->select.b.lba) {
378 		if (drive->addressing == 1) {
379 			task_ioreg_t tasklets[10];
380 
381 			tasklets[0] = 0;
382 			tasklets[1] = 0;
383 			tasklets[2] = nsectors.b.low;
384 			tasklets[3] = nsectors.b.high;
385 			tasklets[4] = (task_ioreg_t) block;
386 			tasklets[5] = (task_ioreg_t) (block>>8);
387 			tasklets[6] = (task_ioreg_t) (block>>16);
388 			tasklets[7] = (task_ioreg_t) (block>>24);
389 			tasklets[8] = (task_ioreg_t) 0;
390 			tasklets[9] = (task_ioreg_t) 0;
391 //			tasklets[8] = (task_ioreg_t) (block>>32);
392 //			tasklets[9] = (task_ioreg_t) (block>>40);
393 #ifdef DEBUG
394 			printk("%s: %sing: LBAsect=%lu, sectors=%ld, "
395 				"buffer=0x%08lx, LBAsect=0x%012lx\n",
396 				drive->name,
397 				(rq->cmd==READ)?"read":"writ",
398 				block,
399 				rq->nr_sectors,
400 				(unsigned long) rq->buffer,
401 				block);
402 			printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
403 				drive->name, tasklets[3], tasklets[2],
404 				tasklets[9], tasklets[8], tasklets[7],
405 				tasklets[6], tasklets[5], tasklets[4]);
406 #endif
407 			hwif->OUTB(tasklets[1], IDE_FEATURE_REG);
408 			hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
409 			hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
410 			hwif->OUTB(tasklets[8], IDE_LCYL_REG);
411 			hwif->OUTB(tasklets[9], IDE_HCYL_REG);
412 
413 			hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
414 			hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
415 			hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
416 			hwif->OUTB(tasklets[5], IDE_LCYL_REG);
417 			hwif->OUTB(tasklets[6], IDE_HCYL_REG);
418 			hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
419 		} else {
420 #ifdef DEBUG
421 			printk("%s: %sing: LBAsect=%ld, sectors=%ld, "
422 				"buffer=0x%08lx\n",
423 				drive->name, (rq->cmd==READ)?"read":"writ",
424 				block, rq->nr_sectors,
425 				(unsigned long) rq->buffer);
426 #endif
427 			hwif->OUTB(0x00, IDE_FEATURE_REG);
428 			hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
429 			hwif->OUTB(block, IDE_SECTOR_REG);
430 			hwif->OUTB(block>>=8, IDE_LCYL_REG);
431 			hwif->OUTB(block>>=8, IDE_HCYL_REG);
432 			hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
433 		}
434 	} else {
435 		unsigned int sect,head,cyl,track;
436 		track = block / drive->sect;
437 		sect  = block % drive->sect + 1;
438 		hwif->OUTB(sect, IDE_SECTOR_REG);
439 		head  = track % drive->head;
440 		cyl   = track / drive->head;
441 
442 		hwif->OUTB(0x00, IDE_FEATURE_REG);
443 		hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
444 		hwif->OUTB(cyl, IDE_LCYL_REG);
445 		hwif->OUTB(cyl>>8, IDE_HCYL_REG);
446 		hwif->OUTB(head|drive->select.all,IDE_SELECT_REG);
447 #ifdef DEBUG
448 		printk("%s: %sing: CHS=%d/%d/%d, sectors=%ld, buffer=0x%08lx\n",
449 			drive->name, (rq->cmd==READ)?"read":"writ", cyl,
450 			head, sect, rq->nr_sectors, (unsigned long) rq->buffer);
451 #endif
452 	}
453 
454 	if (rq_data_dir(rq) == READ) {
455 #ifdef CONFIG_BLK_DEV_IDEDMA
456 		if (drive->using_dma && !hwif->ide_dma_read(drive))
457 			return ide_started;
458 #endif /* CONFIG_BLK_DEV_IDEDMA */
459 		if (HWGROUP(drive)->handler != NULL)
460 			BUG();
461 
462 		command = ((drive->mult_count) ?
463 			   ((lba48) ? WIN_MULTREAD_EXT : WIN_MULTREAD) :
464 			   ((lba48) ? WIN_READ_EXT : WIN_READ));
465 
466 		ide_execute_command(drive, command, &read_intr, WAIT_CMD, NULL);
467 		return ide_started;
468 	} else if (rq_data_dir(rq) == WRITE) {
469 		ide_startstop_t startstop;
470 #ifdef CONFIG_BLK_DEV_IDEDMA
471 		if (drive->using_dma && !(HWIF(drive)->ide_dma_write(drive)))
472 			return ide_started;
473 #endif /* CONFIG_BLK_DEV_IDEDMA */
474 
475 		command = ((drive->mult_count) ?
476 			   ((lba48) ? WIN_MULTWRITE_EXT : WIN_MULTWRITE) :
477 			   ((lba48) ? WIN_WRITE_EXT : WIN_WRITE));
478 		hwif->OUTB(command, IDE_COMMAND_REG);
479 
480 		if (ide_wait_stat(&startstop, drive, DATA_READY,
481 				drive->bad_wstat, WAIT_DRQ)) {
482 			printk(KERN_ERR "%s: no DRQ after issuing %s\n",
483 				drive->name,
484 				drive->mult_count ? "MULTWRITE" : "WRITE");
485 			return startstop;
486 		}
487 		if (!drive->unmask)
488 			local_irq_disable();
489 		if (drive->mult_count) {
490 			ide_hwgroup_t *hwgroup = HWGROUP(drive);
491 	/*
492 	 * Ugh.. this part looks ugly because we MUST set up
493 	 * the interrupt handler before outputting the first block
494 	 * of data to be written.  If we hit an error (corrupted buffer list)
495 	 * in ide_multwrite(), then we need to remove the handler/timer
496 	 * before returning.  Fortunately, this NEVER happens (right?).
497 	 *
498 	 * Except when you get an error it seems...
499 	 *
500 	 * MAJOR DATA INTEGRITY BUG !!! only if we error
501 	 */
502 			hwgroup->wrq = *rq; /* scratchpad */
503 			ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
504 			if (ide_multwrite(drive, drive->mult_count)) {
505 				unsigned long flags;
506 				spin_lock_irqsave(&io_request_lock, flags);
507 				hwgroup->handler = NULL;
508 				del_timer(&hwgroup->timer);
509 				spin_unlock_irqrestore(&io_request_lock, flags);
510 				return ide_stopped;
511 			}
512 		} else {
513 			unsigned long flags;
514 			char *to = ide_map_buffer(rq, &flags);
515 			ide_set_handler(drive, &write_intr, WAIT_CMD, NULL);
516 			taskfile_output_data(drive, to, SECTOR_WORDS);
517 			ide_unmap_buffer(to, &flags);
518 		}
519 		return ide_started;
520 	}
521 	printk(KERN_ERR "%s: bad command: %d\n", drive->name, rq->cmd);
522 	idedisk_end_request(drive, 0);
523 	return ide_stopped;
524 }
525 
526 
527 #else /* CONFIG_IDE_TASKFILE_IO */
528 
529 static ide_startstop_t chs_rw_disk(ide_drive_t *, struct request *, unsigned long);
530 static ide_startstop_t lba_28_rw_disk(ide_drive_t *, struct request *, unsigned long);
531 static ide_startstop_t lba_48_rw_disk(ide_drive_t *, struct request *, unsigned long long);
532 
533 /*
534  * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
535  * using LBA if supported, or CHS otherwise, to address sectors.
536  * It also takes care of issuing special DRIVE_CMDs.
537  */
__ide_do_rw_disk(ide_drive_t * drive,struct request * rq,unsigned long block)538 ide_startstop_t __ide_do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
539 {
540 	if (!blk_fs_request(rq)) {
541 		printk(KERN_ERR "%s: bad command: %d\n", drive->name, rq->cmd);
542 		idedisk_end_request(drive, 0);
543 		return ide_stopped;
544 	}
545 
546 	/*
547 	 * 268435455  == 137439 MB or 28bit limit
548 	 *
549 	 * need to add split taskfile operations based on 28bit threshold.
550 	 */
551 
552 	if (drive->addressing == 1)		/* 48-bit LBA */
553 		return lba_48_rw_disk(drive, rq, (unsigned long long) block);
554 	if (drive->select.b.lba)		/* 28-bit LBA */
555 		return lba_28_rw_disk(drive, rq, (unsigned long) block);
556 
557 	/* 28-bit CHS : DIE DIE DIE piece of legacy crap!!! */
558 	return chs_rw_disk(drive, rq, (unsigned long) block);
559 }
560 
get_command(ide_drive_t * drive,int cmd)561 static task_ioreg_t get_command (ide_drive_t *drive, int cmd)
562 {
563 	int lba48bit = (drive->id->cfs_enable_2 & 0x0400) ? 1 : 0;
564 
565 #if 1
566 	lba48bit = (drive->addressing == 1) ? 1 : 0;
567 #endif
568 
569 	if ((cmd == READ) && (drive->using_dma))
570 		return (lba48bit) ? WIN_READDMA_EXT : WIN_READDMA;
571 	else if ((cmd == READ) && (drive->mult_count))
572 		return (lba48bit) ? WIN_MULTREAD_EXT : WIN_MULTREAD;
573 	else if (cmd == READ)
574 		return (lba48bit) ? WIN_READ_EXT : WIN_READ;
575 	else if ((cmd == WRITE) && (drive->using_dma))
576 		return (lba48bit) ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
577 	else if ((cmd == WRITE) && (drive->mult_count))
578 		return (lba48bit) ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
579 	else if (cmd == WRITE)
580 		return (lba48bit) ? WIN_WRITE_EXT : WIN_WRITE;
581 	else
582 		return WIN_NOP;
583 }
584 
chs_rw_disk(ide_drive_t * drive,struct request * rq,unsigned long block)585 static ide_startstop_t chs_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
586 {
587 	ide_task_t		args;
588 	int			sectors;
589 	ata_nsector_t		nsectors;
590 	task_ioreg_t command	= get_command(drive, rq_data_dir(rq));
591 	unsigned int track	= (block / drive->sect);
592 	unsigned int sect	= (block % drive->sect) + 1;
593 	unsigned int head	= (track % drive->head);
594 	unsigned int cyl	= (track / drive->head);
595 
596 	nsectors.all = (u16) rq->nr_sectors;
597 #ifdef DEBUG
598 	printk("%s: %sing: ", drive->name, (rq_data_dir(rq)==READ) ? "read" : "writ");
599 	printk("CHS=%d/%d/%d, ", cyl, head, sect);
600 	printk("sectors=%ld, ", rq->nr_sectors);
601 	printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
602 #endif
603 
604 	memset(&args, 0, sizeof(ide_task_t));
605 
606 	sectors	= (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
607 	args.tfRegister[IDE_NSECTOR_OFFSET]	= sectors;
608 	args.tfRegister[IDE_SECTOR_OFFSET]	= sect;
609 	args.tfRegister[IDE_LCYL_OFFSET]	= cyl;
610 	args.tfRegister[IDE_HCYL_OFFSET]	= (cyl>>8);
611 	args.tfRegister[IDE_SELECT_OFFSET]	= head;
612 	args.tfRegister[IDE_SELECT_OFFSET]	|= drive->select.all;
613 	args.tfRegister[IDE_COMMAND_OFFSET]	= command;
614 	args.command_type			= ide_cmd_type_parser(&args);
615 	args.rq					= (struct request *) rq;
616 	rq->special				= (ide_task_t *)&args;
617 	return do_rw_taskfile(drive, &args);
618 }
619 
lba_28_rw_disk(ide_drive_t * drive,struct request * rq,unsigned long block)620 static ide_startstop_t lba_28_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
621 {
622 	ide_task_t		args;
623 	int			sectors;
624 	ata_nsector_t		nsectors;
625 	task_ioreg_t command	= get_command(drive, rq_data_dir(rq));
626 
627 	nsectors.all = (u16) rq->nr_sectors;
628 
629 #ifdef DEBUG
630 	printk("%s: %sing: ", drive->name, (rq_data_dir(rq)==READ) ? "read" : "writ");
631 	printk("LBAsect=%lld, ", block);
632 	printk("sectors=%ld, ", rq->nr_sectors);
633 	printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
634 #endif
635 
636 	memset(&args, 0, sizeof(ide_task_t));
637 
638 	sectors = (rq->nr_sectors == 256) ? 0x00 : rq->nr_sectors;
639 	args.tfRegister[IDE_NSECTOR_OFFSET]	= sectors;
640 	args.tfRegister[IDE_SECTOR_OFFSET]	= block;
641 	args.tfRegister[IDE_LCYL_OFFSET]	= (block>>=8);
642 	args.tfRegister[IDE_HCYL_OFFSET]	= (block>>=8);
643 	args.tfRegister[IDE_SELECT_OFFSET]	= ((block>>8)&0x0f);
644 	args.tfRegister[IDE_SELECT_OFFSET]	|= drive->select.all;
645 	args.tfRegister[IDE_COMMAND_OFFSET]	= command;
646 	args.command_type			= ide_cmd_type_parser(&args);
647 	args.rq					= (struct request *) rq;
648 	rq->special				= (ide_task_t *)&args;
649 	return do_rw_taskfile(drive, &args);
650 }
651 
652 /*
653  * 268435455  == 137439 MB or 28bit limit
654  * 320173056  == 163929 MB or 48bit addressing
655  * 1073741822 == 549756 MB or 48bit addressing fake drive
656  */
657 
lba_48_rw_disk(ide_drive_t * drive,struct request * rq,unsigned long long block)658 static ide_startstop_t lba_48_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long long block)
659 {
660 	ide_task_t		args;
661 	int			sectors;
662 	ata_nsector_t		nsectors;
663 	task_ioreg_t command	= get_command(drive, rq_data_dir(rq));
664 
665 	nsectors.all = (u16) rq->nr_sectors;
666 
667 #ifdef DEBUG
668 	printk("%s: %sing: ", drive->name, (rq_data_dir(rq)==READ) ? "read" : "writ");
669 	printk("LBAsect=%lld, ", block);
670 	printk("sectors=%ld, ", rq->nr_sectors);
671 	printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
672 #endif
673 
674 	memset(&args, 0, sizeof(ide_task_t));
675 
676 	sectors = (rq->nr_sectors == 65536) ? 0 : rq->nr_sectors;
677 	args.tfRegister[IDE_NSECTOR_OFFSET]	= sectors;
678 	args.tfRegister[IDE_SECTOR_OFFSET]	= block;	/* low lba */
679 	args.tfRegister[IDE_LCYL_OFFSET]	= (block>>=8);	/* mid lba */
680 	args.tfRegister[IDE_HCYL_OFFSET]	= (block>>=8);	/* hi  lba */
681 	args.tfRegister[IDE_SELECT_OFFSET]	= drive->select.all;
682 	args.tfRegister[IDE_COMMAND_OFFSET]	= command;
683 	args.hobRegister[IDE_NSECTOR_OFFSET_HOB]= sectors >> 8;
684 	args.hobRegister[IDE_SECTOR_OFFSET_HOB]	= (block>>=8);	/* low lba */
685 	args.hobRegister[IDE_LCYL_OFFSET_HOB]	= (block>>=8);	/* mid lba */
686 	args.hobRegister[IDE_HCYL_OFFSET_HOB]	= (block>>=8);	/* hi  lba */
687 	args.hobRegister[IDE_SELECT_OFFSET_HOB]	= drive->select.all;
688 	args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
689 	args.command_type			= ide_cmd_type_parser(&args);
690 	args.rq					= (struct request *) rq;
691 	rq->special				= (ide_task_t *)&args;
692 	return do_rw_taskfile(drive, &args);
693 }
694 
695 #endif /* CONFIG_IDE_TASKFILE_IO */
696 
ide_do_rw_disk(ide_drive_t * drive,struct request * rq,unsigned long block)697 static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
698 {
699 	ide_hwif_t *hwif	= HWIF(drive);
700 	if (hwif->rw_disk)
701 		return hwif->rw_disk(drive, rq, block);
702 	else
703 		return __ide_do_rw_disk(drive, rq, block);
704 }
705 
706 EXPORT_SYMBOL_GPL(__ide_do_rw_disk);
707 
idedisk_open(struct inode * inode,struct file * filp,ide_drive_t * drive)708 static int idedisk_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
709 {
710 	MOD_INC_USE_COUNT;
711 	if (drive->removable && drive->usage == 1) {
712 		ide_task_t args;
713 		int cf;
714 		memset(&args, 0, sizeof(ide_task_t));
715 		args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK;
716 		args.command_type = ide_cmd_type_parser(&args);
717 		check_disk_change(inode->i_rdev);
718 		/*
719 		 * Ignore the return code from door_lock,
720 		 * since the open() has already succeeded,
721 		 * and the door_lock is irrelevant at this point.
722 		 */
723 		if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
724 			drive->doorlocking = 0;
725 		drive->wcache = 0;
726 		/* Cache enabled ? */
727 		if (drive->id->csfo & 1)
728 			drive->wcache = 1;
729 		/* Cache command set available ? */
730 		if (drive->id->cfs_enable_1 & (1<<5))
731 			drive->wcache = 1;
732 		/* ATA6 cache extended commands */
733 		cf = drive->id->command_set_2 >> 24;
734 		if((cf & 0xC0) == 0x40 && (cf & 0x30) != 0)
735 			drive->wcache = 1;
736 	}
737 	return 0;
738 }
739 
740 static int do_idedisk_flushcache(ide_drive_t *drive);
741 
ide_cacheflush_p(ide_drive_t * drive)742 static int ide_cacheflush_p(ide_drive_t *drive)
743 {
744 	if(drive->wcache)
745 	{
746 		if (do_idedisk_flushcache(drive))
747 		{
748 			printk (KERN_INFO "%s: Write Cache FAILED Flushing!\n",
749 				drive->name);
750 			return -EIO;
751 		}
752 		return 1;
753 	}
754 	return 0;
755 }
756 
idedisk_release(struct inode * inode,struct file * filp,ide_drive_t * drive)757 static void idedisk_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
758 {
759 	if (drive->removable && !drive->usage) {
760 		ide_task_t args;
761 		memset(&args, 0, sizeof(ide_task_t));
762 		args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
763 		args.command_type = ide_cmd_type_parser(&args);
764 		invalidate_bdev(inode->i_bdev, 0);
765 		if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
766 			drive->doorlocking = 0;
767 	}
768 	ide_cacheflush_p(drive);
769 	MOD_DEC_USE_COUNT;
770 }
771 
idedisk_media_change(ide_drive_t * drive)772 static int idedisk_media_change (ide_drive_t *drive)
773 {
774 	/* if removable, always assume it was changed */
775 	return drive->removable;
776 }
777 
idedisk_revalidate(ide_drive_t * drive)778 static void idedisk_revalidate (ide_drive_t *drive)
779 {
780 	grok_partitions(HWIF(drive)->gd, drive->select.b.unit,
781 			1<<PARTN_BITS,
782 			current_capacity(drive));
783 }
784 
idedisk_end_request(ide_drive_t * drive,int uptodate)785 static int idedisk_end_request (ide_drive_t *drive, int uptodate)
786 {
787 	struct request *rq;
788 	unsigned long flags;
789 	int ret = 1;
790 
791 	spin_lock_irqsave(&io_request_lock, flags);
792 	rq = HWGROUP(drive)->rq;
793 
794 	/*
795 	 * decide whether to reenable DMA -- 3 is a random magic for now,
796 	 * if we DMA timeout more than 3 times, just stay in PIO
797 	 */
798 	if (drive->state == DMA_PIO_RETRY && drive->retry_pio <= 3) {
799 		drive->state = 0;
800 		HWGROUP(drive)->hwif->ide_dma_on(drive);
801 	}
802 
803 	if (!end_that_request_first(rq, uptodate, drive->name)) {
804 		add_blkdev_randomness(MAJOR(rq->rq_dev));
805 		blkdev_dequeue_request(rq);
806 		HWGROUP(drive)->rq = NULL;
807 		end_that_request_last(rq);
808 		ret = 0;
809 	}
810 
811 	spin_unlock_irqrestore(&io_request_lock, flags);
812 	return ret;
813 }
814 
idedisk_dump_status(ide_drive_t * drive,const char * msg,u8 stat)815 static u8 idedisk_dump_status (ide_drive_t *drive, const char *msg, u8 stat)
816 {
817 	ide_hwif_t *hwif = HWIF(drive);
818 	unsigned long flags;
819 	u8 err = 0;
820 
821 	local_irq_set(flags);
822 	printk("%s: %s: status=0x%02x", drive->name, msg, stat);
823 #if FANCY_STATUS_DUMPS
824 	printk(" { ");
825 	if (stat & BUSY_STAT)
826 		printk("Busy ");
827 	else {
828 		if (stat & READY_STAT)	printk("DriveReady ");
829 		if (stat & WRERR_STAT)	printk("DeviceFault ");
830 		if (stat & SEEK_STAT)	printk("SeekComplete ");
831 		if (stat & DRQ_STAT)	printk("DataRequest ");
832 		if (stat & ECC_STAT)	printk("CorrectedError ");
833 		if (stat & INDEX_STAT)	printk("Index ");
834 		if (stat & ERR_STAT)	printk("Error ");
835 	}
836 	printk("}");
837 #endif	/* FANCY_STATUS_DUMPS */
838 	printk("\n");
839 	if ((stat & (BUSY_STAT|ERR_STAT)) == ERR_STAT) {
840 		err = hwif->INB(IDE_ERROR_REG);
841 		printk("%s: %s: error=0x%02x", drive->name, msg, err);
842 #if FANCY_STATUS_DUMPS
843 		printk(" { ");
844 		if (err & ABRT_ERR)	printk("DriveStatusError ");
845 		if (err & ICRC_ERR)
846 			printk("Bad%s ", (err & ABRT_ERR) ? "CRC" : "Sector");
847 		if (err & ECC_ERR)	printk("UncorrectableError ");
848 		if (err & ID_ERR)	printk("SectorIdNotFound ");
849 		if (err & TRK0_ERR)	printk("TrackZeroNotFound ");
850 		if (err & MARK_ERR)	printk("AddrMarkNotFound ");
851 		printk("}");
852 		if ((err & (BBD_ERR | ABRT_ERR)) == BBD_ERR ||
853 		    (err & (ECC_ERR|ID_ERR|MARK_ERR))) {
854 			if (drive->addressing == 1) {
855 				__u64 sectors = 0;
856 				u32 low = 0, high = 0;
857 				low = idedisk_read_24(drive);
858 				hwif->OUTB(drive->ctl|0x80, IDE_CONTROL_REG);
859 				high = idedisk_read_24(drive);
860 				sectors = ((__u64)high << 24) | low;
861 				printk(", LBAsect=%llu, high=%d, low=%d",
862 				       (unsigned long long) sectors,
863 				       high, low);
864 			} else {
865 				u8 cur = hwif->INB(IDE_SELECT_REG);
866 				if (cur & 0x40) {	/* using LBA? */
867 					printk(", LBAsect=%ld", (unsigned long)
868 					 ((cur&0xf)<<24)
869 					 |(hwif->INB(IDE_HCYL_REG)<<16)
870 					 |(hwif->INB(IDE_LCYL_REG)<<8)
871 					 | hwif->INB(IDE_SECTOR_REG));
872 				} else {
873 					printk(", CHS=%d/%d/%d",
874 					 (hwif->INB(IDE_HCYL_REG)<<8) +
875 					  hwif->INB(IDE_LCYL_REG),
876 					  cur & 0xf,
877 					  hwif->INB(IDE_SECTOR_REG));
878 				}
879 			}
880 			if (HWGROUP(drive) && HWGROUP(drive)->rq)
881 				printk(", sector=%ld",
882 					HWGROUP(drive)->rq->sector);
883 		}
884 	}
885 #endif	/* FANCY_STATUS_DUMPS */
886 	printk("\n");
887 	local_irq_restore(flags);
888 	return err;
889 }
890 
idedisk_error(ide_drive_t * drive,const char * msg,u8 stat)891 ide_startstop_t idedisk_error (ide_drive_t *drive, const char *msg, u8 stat)
892 {
893 	ide_hwif_t *hwif;
894 	struct request *rq;
895 	u8 err;
896 	int i = (drive->mult_count ? drive->mult_count : 1) * SECTOR_WORDS;
897 
898 	err = idedisk_dump_status(drive, msg, stat);
899 
900 	if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
901 		return ide_stopped;
902 
903 	hwif = HWIF(drive);
904 	/* retry only "normal" I/O: */
905 	switch (rq->cmd) {
906 		case IDE_DRIVE_CMD:
907 		case IDE_DRIVE_TASK:
908 		case IDE_DRIVE_TASKFILE:
909 			rq->errors = 1;
910 			ide_end_drive_cmd(drive, stat, err);
911 			return ide_stopped;
912 #if 0
913 		case IDE_DRIVE_TASKFILE:
914 			rq->errors = 1;
915 			ide_end_taskfile(drive, stat, err);
916 			return ide_stopped;
917 #endif
918 		default:
919 			break;
920 	}
921 
922 	if (stat & BUSY_STAT || ((stat & WRERR_STAT) && !drive->nowerr)) {
923 		/* other bits are useless when BUSY */
924 		rq->errors |= ERROR_RESET;
925 	} else if (stat & ERR_STAT) {
926 		/* err has different meaning on cdrom and tape */
927 		if (err == ABRT_ERR) {
928 			if (drive->select.b.lba &&
929 			    /* some newer drives don't support WIN_SPECIFY */
930 			    hwif->INB(IDE_COMMAND_REG) == WIN_SPECIFY)
931 				return ide_stopped;
932 		} else if ((err & BAD_CRC) == BAD_CRC) {
933 			/* UDMA crc error, just retry the operation */
934 			drive->crc_count++;
935 		} else if (err & (BBD_ERR | ECC_ERR)) {
936 			/* retries won't help these */
937 			rq->errors = ERROR_MAX;
938 		} else if (err & TRK0_ERR) {
939 			/* help it find track zero */
940 			rq->errors |= ERROR_RECAL;
941 		}
942 	}
943 	if ((stat & DRQ_STAT) && rq_data_dir(rq) == READ) {
944 		/*
945 		 * try_to_flush_leftover_data() is invoked in response to
946 		 * a drive unexpectedly having its DRQ_STAT bit set.  As
947 		 * an alternative to resetting the drive, this routine
948 		 * tries to clear the condition by read a sector's worth
949 		 * of data from the drive.  Of course, this may not help
950 		 * if the drive is *waiting* for data from *us*.
951 		 */
952 		while (i > 0) {
953 			u32 buffer[16];
954 			unsigned int wcount = (i > 16) ? 16 : i;
955 			i -= wcount;
956 			taskfile_input_data(drive, buffer, wcount);
957 		}
958 	}
959 	if (hwif->INB(IDE_STATUS_REG) & (BUSY_STAT|DRQ_STAT)) {
960 		/* force an abort */
961 		hwif->OUTB(WIN_IDLEIMMEDIATE,IDE_COMMAND_REG);
962 	}
963 	if (rq->errors >= ERROR_MAX)
964 		DRIVER(drive)->end_request(drive, 0);
965 	else {
966 		if ((rq->errors & ERROR_RESET) == ERROR_RESET) {
967 			++rq->errors;
968 			return ide_do_reset(drive);
969 		}
970 		if ((rq->errors & ERROR_RECAL) == ERROR_RECAL)
971 			drive->special.b.recalibrate = 1;
972 		++rq->errors;
973 	}
974 	return ide_stopped;
975 }
976 
idedisk_abort(ide_drive_t * drive,const char * msg)977 ide_startstop_t idedisk_abort(ide_drive_t *drive, const char *msg)
978 {
979 	ide_hwif_t *hwif;
980 	struct request *rq;
981 
982 	if (drive == NULL || (rq = HWGROUP(drive)->rq) == NULL)
983 		return ide_stopped;
984 
985 	hwif = HWIF(drive);
986 	/* retry only "normal" I/O: */
987 	switch (rq->cmd) {
988 		case IDE_DRIVE_CMD:
989 		case IDE_DRIVE_TASK:
990 		case IDE_DRIVE_TASKFILE:
991 			rq->errors = 1;
992 			ide_end_drive_cmd(drive, BUSY_STAT, 0);
993 			return ide_stopped;
994 #if 0
995 		case IDE_DRIVE_TASKFILE:
996 			rq->errors = 1;
997 			ide_end_taskfile(drive, BUSY_STAT, 0);
998 			return ide_stopped;
999 #endif
1000 		default:
1001 			break;
1002 	}
1003 
1004 	rq->errors |= ERROR_RESET;
1005 	DRIVER(drive)->end_request(drive, 0);
1006 	return ide_stopped;
1007 }
1008 
1009 /*
1010  * Queries for true maximum capacity of the drive.
1011  * Returns maximum LBA address (> 0) of the drive, 0 if failed.
1012  */
idedisk_read_native_max_address(ide_drive_t * drive)1013 static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
1014 {
1015 	ide_task_t args;
1016 	unsigned long addr = 0;
1017 
1018 	/* Create IDE/ATA command request structure */
1019 	memset(&args, 0, sizeof(ide_task_t));
1020 	args.tfRegister[IDE_SELECT_OFFSET]	= 0x40;
1021 	args.tfRegister[IDE_COMMAND_OFFSET]	= WIN_READ_NATIVE_MAX;
1022 	args.command_type			= ide_cmd_type_parser(&args);
1023 	/* submit command request */
1024 	ide_raw_taskfile(drive, &args, NULL);
1025 
1026 	/* if OK, compute maximum address value */
1027 	if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
1028 		addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
1029 		     | ((args.tfRegister[  IDE_HCYL_OFFSET]       ) << 16)
1030 		     | ((args.tfRegister[  IDE_LCYL_OFFSET]       ) <<  8)
1031 		     | ((args.tfRegister[IDE_SECTOR_OFFSET]       ));
1032 	}
1033 	addr++;	/* since the return value is (maxlba - 1), we add 1 */
1034 	return addr;
1035 }
1036 
idedisk_read_native_max_address_ext(ide_drive_t * drive)1037 static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
1038 {
1039 	ide_task_t args;
1040 	unsigned long long addr = 0;
1041 
1042 	/* Create IDE/ATA command request structure */
1043 	memset(&args, 0, sizeof(ide_task_t));
1044 
1045 	args.tfRegister[IDE_SELECT_OFFSET]	= 0x40;
1046 	args.tfRegister[IDE_COMMAND_OFFSET]	= WIN_READ_NATIVE_MAX_EXT;
1047 	args.command_type			= ide_cmd_type_parser(&args);
1048         /* submit command request */
1049         ide_raw_taskfile(drive, &args, NULL);
1050 
1051 	/* if OK, compute maximum address value */
1052 	if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
1053 		u32 high = ((args.hobRegister[IDE_HCYL_OFFSET_HOB])<<16) |
1054 			   ((args.hobRegister[IDE_LCYL_OFFSET_HOB])<<8) |
1055   			    (args.hobRegister[IDE_SECTOR_OFFSET_HOB]);
1056 		u32 low  = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
1057 			   ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
1058 			    (args.tfRegister[IDE_SECTOR_OFFSET]);
1059 		addr = ((__u64)high << 24) | low;
1060 	}
1061 	addr++;	/* since the return value is (maxlba - 1), we add 1 */
1062 	return addr;
1063 }
1064 
1065 #ifdef CONFIG_IDEDISK_STROKE
1066 /*
1067  * Sets maximum virtual LBA address of the drive.
1068  * Returns new maximum virtual LBA address (> 0) or 0 on failure.
1069  */
idedisk_set_max_address(ide_drive_t * drive,unsigned long addr_req)1070 static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
1071 {
1072 	ide_task_t args;
1073 	unsigned long addr_set = 0;
1074 
1075 	addr_req--;
1076 	/* Create IDE/ATA command request structure */
1077 	memset(&args, 0, sizeof(ide_task_t));
1078 	args.tfRegister[IDE_SECTOR_OFFSET]	= ((addr_req >>  0) & 0xff);
1079 	args.tfRegister[IDE_LCYL_OFFSET]	= ((addr_req >>  8) & 0xff);
1080 	args.tfRegister[IDE_HCYL_OFFSET]	= ((addr_req >> 16) & 0xff);
1081 	args.tfRegister[IDE_SELECT_OFFSET]	= ((addr_req >> 24) & 0x0f) | 0x40;
1082 	args.tfRegister[IDE_COMMAND_OFFSET]	= WIN_SET_MAX;
1083 	args.command_type			= ide_cmd_type_parser(&args);
1084 	/* submit command request */
1085 	ide_raw_taskfile(drive, &args, NULL);
1086 	/* if OK, read new maximum address value */
1087 	if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
1088 		addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
1089 			 | ((args.tfRegister[  IDE_HCYL_OFFSET]       ) << 16)
1090 			 | ((args.tfRegister[  IDE_LCYL_OFFSET]       ) <<  8)
1091 			 | ((args.tfRegister[IDE_SECTOR_OFFSET]       ));
1092 	}
1093 	addr_set++;
1094 	return addr_set;
1095 }
1096 
idedisk_set_max_address_ext(ide_drive_t * drive,unsigned long long addr_req)1097 static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
1098 {
1099 	ide_task_t args;
1100 	unsigned long long addr_set = 0;
1101 
1102 	addr_req--;
1103 	/* Create IDE/ATA command request structure */
1104 	memset(&args, 0, sizeof(ide_task_t));
1105 	args.tfRegister[IDE_SECTOR_OFFSET]	= ((addr_req >>  0) & 0xff);
1106 	args.tfRegister[IDE_LCYL_OFFSET]	= ((addr_req >>= 8) & 0xff);
1107 	args.tfRegister[IDE_HCYL_OFFSET]	= ((addr_req >>= 8) & 0xff);
1108 	args.tfRegister[IDE_SELECT_OFFSET]      = 0x40;
1109 	args.tfRegister[IDE_COMMAND_OFFSET]	= WIN_SET_MAX_EXT;
1110 	args.hobRegister[IDE_SECTOR_OFFSET_HOB]	= ((addr_req >>= 8) & 0xff);
1111 	args.hobRegister[IDE_LCYL_OFFSET_HOB]	= ((addr_req >>= 8) & 0xff);
1112 	args.hobRegister[IDE_HCYL_OFFSET_HOB]	= ((addr_req >>= 8) & 0xff);
1113 	args.hobRegister[IDE_SELECT_OFFSET_HOB]	= 0x40;
1114 	args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
1115 	args.command_type			= ide_cmd_type_parser(&args);
1116 	/* submit command request */
1117 	ide_raw_taskfile(drive, &args, NULL);
1118 	/* if OK, compute maximum address value */
1119 	if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
1120 		u32 high = ((args.hobRegister[IDE_HCYL_OFFSET_HOB])<<16) |
1121 			   ((args.hobRegister[IDE_LCYL_OFFSET_HOB])<<8) |
1122 			    (args.hobRegister[IDE_SECTOR_OFFSET_HOB]);
1123 		u32 low  = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
1124 			   ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
1125 			    (args.tfRegister[IDE_SECTOR_OFFSET]);
1126 		addr_set = ((__u64)high << 24) | low;
1127 	}
1128 	return addr_set;
1129 }
1130 
1131 #endif /* CONFIG_IDEDISK_STROKE */
1132 
1133 /*
1134  * Tests if the drive supports Host Protected Area feature.
1135  * Returns true if supported, false otherwise.
1136  */
idedisk_supports_host_protected_area(ide_drive_t * drive)1137 static inline int idedisk_supports_host_protected_area(ide_drive_t *drive)
1138 {
1139 	int flag = (drive->id->cfs_enable_1 & 0x0400) ? 1 : 0;
1140 	if (flag)
1141 		printk("%s: host protected area => %d\n", drive->name, flag);
1142 	return flag;
1143 }
1144 
1145 /*
1146  * Compute drive->capacity, the full capacity of the drive
1147  * Called with drive->id != NULL.
1148  *
1149  * To compute capacity, this uses either of
1150  *
1151  *    1. CHS value set by user       (whatever user sets will be trusted)
1152  *    2. LBA value from target drive (require new ATA feature)
1153  *    3. LBA value from system BIOS  (new one is OK, old one may break)
1154  *    4. CHS value from system BIOS  (traditional style)
1155  *
1156  * in above order (i.e., if value of higher priority is available,
1157  * reset will be ignored).
1158  */
1159 #define IDE_STROKE_LIMIT	(32000*1024*2)
init_idedisk_capacity(ide_drive_t * drive)1160 static void init_idedisk_capacity (ide_drive_t  *drive)
1161 {
1162 	struct hd_driveid *id = drive->id;
1163 	unsigned long capacity = drive->cyl * drive->head * drive->sect;
1164 	int have_setmax = idedisk_supports_host_protected_area(drive);
1165 	unsigned long set_max =
1166 		(have_setmax ? idedisk_read_native_max_address(drive) : 0);
1167 	unsigned long long capacity_2 = capacity;
1168 	unsigned long long set_max_ext;
1169 
1170 	drive->capacity48 = 0;
1171 	drive->select.b.lba = 0;
1172 
1173 	if (id->cfs_enable_2 & 0x0400) {
1174 		capacity_2 = id->lba_capacity_2;
1175 		if (!drive->forced_geom) {
1176 			drive->bios_head = 255;
1177 			drive->bios_sect = 63;
1178 		}
1179 		drive->head = drive->bios_head;
1180 		drive->sect = drive->bios_sect;
1181 		drive->cyl = (unsigned int) capacity_2 / (drive->head * drive->sect);
1182 		drive->select.b.lba	= 1;
1183 		set_max_ext = idedisk_read_native_max_address_ext(drive);
1184 		if (set_max_ext > capacity_2 && capacity_2 > IDE_STROKE_LIMIT) {
1185 #ifdef CONFIG_IDEDISK_STROKE
1186 			set_max_ext = idedisk_read_native_max_address_ext(drive);
1187 			set_max_ext = idedisk_set_max_address_ext(drive, set_max_ext);
1188 			if (set_max_ext) {
1189 				drive->capacity48 = capacity_2 = set_max_ext;
1190 				drive->cyl = (unsigned int) set_max_ext / (drive->head * drive->sect);
1191 				drive->select.b.lba = 1;
1192 				drive->id->lba_capacity_2 = capacity_2;
1193                         }
1194 #else /* !CONFIG_IDEDISK_STROKE */
1195 			printk(KERN_INFO "%s: setmax_ext LBA %llu, native  %llu\n",
1196 				drive->name, set_max_ext, capacity_2);
1197 #endif /* CONFIG_IDEDISK_STROKE */
1198 		}
1199 		drive->cyl = (unsigned int) capacity_2 / (drive->head * drive->sect);
1200 		drive->bios_cyl		= drive->cyl;
1201 		drive->capacity48	= capacity_2;
1202 		drive->capacity		= (unsigned long) capacity_2;
1203 		goto check_capacity48;
1204 	/* Determine capacity, and use LBA if the drive properly supports it */
1205 	} else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
1206 		capacity = id->lba_capacity;
1207 		drive->cyl = capacity / (drive->head * drive->sect);
1208 		drive->select.b.lba = 1;
1209 	}
1210 
1211 	if (set_max > capacity && capacity > IDE_STROKE_LIMIT) {
1212 #ifdef CONFIG_IDEDISK_STROKE
1213 		set_max = idedisk_read_native_max_address(drive);
1214 		set_max = idedisk_set_max_address(drive, set_max);
1215 		if (set_max) {
1216 			drive->capacity = capacity = set_max;
1217 			drive->cyl = set_max / (drive->head * drive->sect);
1218 			drive->select.b.lba = 1;
1219 			drive->id->lba_capacity = capacity;
1220 		}
1221 #else /* !CONFIG_IDEDISK_STROKE */
1222 		printk(KERN_INFO "%s: setmax LBA %lu, native  %lu\n",
1223 			drive->name, set_max, capacity);
1224 #endif /* CONFIG_IDEDISK_STROKE */
1225 	}
1226 
1227 	drive->capacity = capacity;
1228 
1229 	if ((id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)) {
1230 		drive->capacity48 = id->lba_capacity_2;
1231 		drive->head = 255;
1232 		drive->sect = 63;
1233 		drive->cyl = (unsigned long)(drive->capacity48) / (drive->head * drive->sect);
1234 	}
1235 
1236 check_capacity48:
1237 	/* Limit disk size to 137GB if LBA48 addressing is not supported */
1238 	if (drive->addressing == 0 && drive->capacity48 > (1ULL)<<28) {
1239 		printk("%s: cannot use LBA48 - capacity reset "
1240 			"from %llu to %llu\n",
1241 			drive->name, drive->capacity48, (1ULL)<<28);
1242 		drive->capacity48 = (1ULL)<<28;
1243 	}
1244 }
1245 
idedisk_capacity(ide_drive_t * drive)1246 static unsigned long idedisk_capacity (ide_drive_t *drive)
1247 {
1248 	if (drive->id->cfs_enable_2 & 0x0400)
1249 		return (drive->capacity48 - drive->sect0);
1250 	return (drive->capacity - drive->sect0);
1251 }
1252 
idedisk_special(ide_drive_t * drive)1253 static ide_startstop_t idedisk_special (ide_drive_t *drive)
1254 {
1255 	special_t *s = &drive->special;
1256 
1257 	if (s->b.set_geometry) {
1258 		s->b.set_geometry	= 0;
1259 		if (!IS_PDC4030_DRIVE) {
1260 			ide_task_t args;
1261 			memset(&args, 0, sizeof(ide_task_t));
1262 			args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1263 			args.tfRegister[IDE_SECTOR_OFFSET]  = drive->sect;
1264 			args.tfRegister[IDE_LCYL_OFFSET]    = drive->cyl;
1265 			args.tfRegister[IDE_HCYL_OFFSET]    = drive->cyl>>8;
1266 			args.tfRegister[IDE_SELECT_OFFSET]  = ((drive->head-1)|drive->select.all)&0xBF;
1267 			args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY;
1268 			args.command_type = ide_cmd_type_parser(&args);
1269 			do_rw_taskfile(drive, &args);
1270 		}
1271 	} else if (s->b.recalibrate) {
1272 		s->b.recalibrate = 0;
1273 		if (!IS_PDC4030_DRIVE) {
1274 			ide_task_t args;
1275 			memset(&args, 0, sizeof(ide_task_t));
1276 			args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
1277 			args.tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE;
1278 			args.command_type = ide_cmd_type_parser(&args);
1279 			do_rw_taskfile(drive, &args);
1280 		}
1281 	} else if (s->b.set_multmode) {
1282 		s->b.set_multmode = 0;
1283 		if (drive->mult_req > drive->id->max_multsect)
1284 			drive->mult_req = drive->id->max_multsect;
1285 		if (!IS_PDC4030_DRIVE) {
1286 			ide_task_t args;
1287 			memset(&args, 0, sizeof(ide_task_t));
1288 			args.tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req;
1289 			args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT;
1290 			args.command_type = ide_cmd_type_parser(&args);
1291 			do_rw_taskfile(drive, &args);
1292 		}
1293 	} else if (s->all) {
1294 		int special = s->all;
1295 		s->all = 0;
1296 		printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
1297 		return ide_stopped;
1298 	}
1299 	return IS_PDC4030_DRIVE ? ide_stopped : ide_started;
1300 }
1301 
idedisk_pre_reset(ide_drive_t * drive)1302 static void idedisk_pre_reset (ide_drive_t *drive)
1303 {
1304 	int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
1305 
1306 	drive->special.all = 0;
1307 	drive->special.b.set_geometry = legacy;
1308 	drive->special.b.recalibrate  = legacy;
1309 	if (OK_TO_RESET_CONTROLLER)
1310 		drive->mult_count = 0;
1311 	if (!drive->keep_settings && !drive->using_dma)
1312 		drive->mult_req = 0;
1313 	if (drive->mult_req != drive->mult_count)
1314 		drive->special.b.set_multmode = 1;
1315 }
1316 
1317 #ifdef CONFIG_PROC_FS
1318 
smart_enable(ide_drive_t * drive)1319 static int smart_enable(ide_drive_t *drive)
1320 {
1321 	ide_task_t args;
1322 
1323 	memset(&args, 0, sizeof(ide_task_t));
1324 	args.tfRegister[IDE_FEATURE_OFFSET]	= SMART_ENABLE;
1325 	args.tfRegister[IDE_LCYL_OFFSET]	= SMART_LCYL_PASS;
1326 	args.tfRegister[IDE_HCYL_OFFSET]	= SMART_HCYL_PASS;
1327 	args.tfRegister[IDE_COMMAND_OFFSET]	= WIN_SMART;
1328 	args.command_type			= ide_cmd_type_parser(&args);
1329 	return ide_raw_taskfile(drive, &args, NULL);
1330 }
1331 
get_smart_values(ide_drive_t * drive,u8 * buf)1332 static int get_smart_values(ide_drive_t *drive, u8 *buf)
1333 {
1334 	ide_task_t args;
1335 
1336 	memset(&args, 0, sizeof(ide_task_t));
1337 	args.tfRegister[IDE_FEATURE_OFFSET]	= SMART_READ_VALUES;
1338 	args.tfRegister[IDE_NSECTOR_OFFSET]	= 0x01;
1339 	args.tfRegister[IDE_LCYL_OFFSET]	= SMART_LCYL_PASS;
1340 	args.tfRegister[IDE_HCYL_OFFSET]	= SMART_HCYL_PASS;
1341 	args.tfRegister[IDE_COMMAND_OFFSET]	= WIN_SMART;
1342 	args.command_type			= ide_cmd_type_parser(&args);
1343 	(void) smart_enable(drive);
1344 	return ide_raw_taskfile(drive, &args, buf);
1345 }
1346 
get_smart_thresholds(ide_drive_t * drive,u8 * buf)1347 static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
1348 {
1349 	ide_task_t args;
1350 	memset(&args, 0, sizeof(ide_task_t));
1351 	args.tfRegister[IDE_FEATURE_OFFSET]	= SMART_READ_THRESHOLDS;
1352 	args.tfRegister[IDE_NSECTOR_OFFSET]	= 0x01;
1353 	args.tfRegister[IDE_LCYL_OFFSET]	= SMART_LCYL_PASS;
1354 	args.tfRegister[IDE_HCYL_OFFSET]	= SMART_HCYL_PASS;
1355 	args.tfRegister[IDE_COMMAND_OFFSET]	= WIN_SMART;
1356 	args.command_type			= ide_cmd_type_parser(&args);
1357 	(void) smart_enable(drive);
1358 	return ide_raw_taskfile(drive, &args, buf);
1359 }
1360 
proc_idedisk_read_cache(char * page,char ** start,off_t off,int count,int * eof,void * data)1361 static int proc_idedisk_read_cache
1362 	(char *page, char **start, off_t off, int count, int *eof, void *data)
1363 {
1364 	ide_drive_t	*drive = (ide_drive_t *) data;
1365 	char		*out = page;
1366 	int		len;
1367 
1368 	if (drive->id_read)
1369 		len = sprintf(out,"%i\n", drive->id->buf_size / 2);
1370 	else
1371 		len = sprintf(out,"(none)\n");
1372 	PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1373 }
1374 
proc_idedisk_read_smart_thresholds(char * page,char ** start,off_t off,int count,int * eof,void * data)1375 static int proc_idedisk_read_smart_thresholds
1376 	(char *page, char **start, off_t off, int count, int *eof, void *data)
1377 {
1378 	ide_drive_t	*drive = (ide_drive_t *)data;
1379 	int		len = 0, i = 0;
1380 
1381 	if (!get_smart_thresholds(drive, page)) {
1382 		unsigned short *val = (unsigned short *) page;
1383 		char *out = ((char *)val) + (SECTOR_WORDS * 4);
1384 		page = out;
1385 		do {
1386 			out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1387 			val += 1;
1388 		} while (i < (SECTOR_WORDS * 2));
1389 		len = out - page;
1390 	}
1391 	PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1392 }
1393 
proc_idedisk_read_smart_values(char * page,char ** start,off_t off,int count,int * eof,void * data)1394 static int proc_idedisk_read_smart_values
1395 	(char *page, char **start, off_t off, int count, int *eof, void *data)
1396 {
1397 	ide_drive_t	*drive = (ide_drive_t *)data;
1398 	int		len = 0, i = 0;
1399 
1400 	if (!get_smart_values(drive, page)) {
1401 		unsigned short *val = (unsigned short *) page;
1402 		char *out = ((char *)val) + (SECTOR_WORDS * 4);
1403 		page = out;
1404 		do {
1405 			out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1406 			val += 1;
1407 		} while (i < (SECTOR_WORDS * 2));
1408 		len = out - page;
1409 	}
1410 	PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1411 }
1412 
1413 static ide_proc_entry_t idedisk_proc[] = {
1414 	{ "cache",		S_IFREG|S_IRUGO,	proc_idedisk_read_cache,		NULL },
1415 	{ "geometry",		S_IFREG|S_IRUGO,	proc_ide_read_geometry,			NULL },
1416 	{ "smart_values",	S_IFREG|S_IRUSR,	proc_idedisk_read_smart_values,		NULL },
1417 	{ "smart_thresholds",	S_IFREG|S_IRUSR,	proc_idedisk_read_smart_thresholds,	NULL },
1418 	{ NULL, 0, NULL, NULL }
1419 };
1420 
1421 #else
1422 
1423 #define	idedisk_proc	NULL
1424 
1425 #endif	/* CONFIG_PROC_FS */
1426 
1427 /*
1428  * This is tightly woven into the driver->do_special can not touch.
1429  * DON'T do it again until a total personality rewrite is committed.
1430  */
set_multcount(ide_drive_t * drive,int arg)1431 static int set_multcount(ide_drive_t *drive, int arg)
1432 {
1433 	struct request rq;
1434 
1435 	if (drive->special.b.set_multmode)
1436 		return -EBUSY;
1437 	ide_init_drive_cmd (&rq);
1438 	rq.cmd = IDE_DRIVE_CMD;
1439 	drive->mult_req = arg;
1440 	drive->special.b.set_multmode = 1;
1441 	(void) ide_do_drive_cmd (drive, &rq, ide_wait);
1442 	return (drive->mult_count == arg) ? 0 : -EIO;
1443 }
1444 
set_nowerr(ide_drive_t * drive,int arg)1445 static int set_nowerr(ide_drive_t *drive, int arg)
1446 {
1447 	if (ide_spin_wait_hwgroup(drive))
1448 		return -EBUSY;
1449 	drive->nowerr = arg;
1450 	drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
1451 	spin_unlock_irq(&io_request_lock);
1452 	return 0;
1453 }
1454 
write_cache(ide_drive_t * drive,int arg)1455 static int write_cache (ide_drive_t *drive, int arg)
1456 {
1457 	ide_task_t args;
1458 
1459 	if (!(drive->id->cfs_enable_2 & 0x3000))
1460 		return 1;
1461 
1462 	memset(&args, 0, sizeof(ide_task_t));
1463 	args.tfRegister[IDE_FEATURE_OFFSET]	= (arg) ?
1464 			SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
1465 	args.tfRegister[IDE_COMMAND_OFFSET]	= WIN_SETFEATURES;
1466 	args.command_type			= ide_cmd_type_parser(&args);
1467 	(void) ide_raw_taskfile(drive, &args, NULL);
1468 
1469 	drive->wcache = arg;
1470 	return 0;
1471 }
1472 
call_idedisk_standby(ide_drive_t * drive,int arg)1473 static int call_idedisk_standby (ide_drive_t *drive, int arg)
1474 {
1475 	ide_task_t args;
1476 	u8 standby = (arg) ? WIN_STANDBYNOW2 : WIN_STANDBYNOW1;
1477 	memset(&args, 0, sizeof(ide_task_t));
1478 	args.tfRegister[IDE_COMMAND_OFFSET]	= standby;
1479 	args.command_type			= ide_cmd_type_parser(&args);
1480 	return ide_raw_taskfile(drive, &args, NULL);
1481 }
1482 
do_idedisk_standby(ide_drive_t * drive)1483 static int do_idedisk_standby (ide_drive_t *drive)
1484 {
1485 	return call_idedisk_standby(drive, 0);
1486 }
1487 
call_idedisk_suspend(ide_drive_t * drive,int arg)1488 static int call_idedisk_suspend (ide_drive_t *drive, int arg)
1489 {
1490 	ide_task_t args;
1491 	u8 suspend = (arg) ? WIN_SLEEPNOW2 : WIN_SLEEPNOW1;
1492 	memset(&args, 0, sizeof(ide_task_t));
1493 	args.tfRegister[IDE_COMMAND_OFFSET]	= suspend;
1494 	args.command_type			= ide_cmd_type_parser(&args);
1495 	return ide_raw_taskfile(drive, &args, NULL);
1496 }
1497 
do_idedisk_suspend(ide_drive_t * drive)1498 static int do_idedisk_suspend (ide_drive_t *drive)
1499 {
1500 	if (drive->suspend_reset)
1501 		return 1;
1502 	ide_cacheflush_p(drive);
1503 	return call_idedisk_suspend(drive, 0);
1504 }
1505 
1506 #if 0
1507 static int call_idedisk_checkpower (ide_drive_t *drive, int arg)
1508 {
1509 	ide_task_t args;
1510 	u8 ckpw = (arg) ? WIN_CHECKPOWERMODE2 : WIN_CHECKPOWERMODE1;
1511 	memset(&args, 0, sizeof(ide_task_t));
1512 	args.tfRegister[IDE_COMMAND_OFFSET]	= ckpw;
1513 	args.command_type			= ide_cmd_type_parser(&args);
1514 	ide_raw_taskfile(drive, &args, NULL);
1515 #if 0
1516 if (errno != EIO || args[0] != 0 || args[1] != 0)
1517    state = "unknown";
1518 else
1519    state = "sleeping";
1520 } else {
1521    state = (args[2] == 255) ? "active/idle" : "standby";
1522 #endif
1523 	return 0;
1524 }
1525 
1526 static int do_idedisk_checkpower (ide_drive_t *drive)
1527 {
1528 	return call_idedisk_checkpower(drive, 0);
1529 }
1530 #endif
1531 
do_idedisk_resume(ide_drive_t * drive)1532 static int do_idedisk_resume (ide_drive_t *drive)
1533 {
1534 	if (!drive->suspend_reset)
1535 		return 1;
1536 	return 0;
1537 }
1538 
do_idedisk_flushcache(ide_drive_t * drive)1539 static int do_idedisk_flushcache (ide_drive_t *drive)
1540 {
1541 	ide_task_t args;
1542 
1543 	memset(&args, 0, sizeof(ide_task_t));
1544 	if (drive->id->cfs_enable_2 & 0x2400)
1545 		args.tfRegister[IDE_COMMAND_OFFSET]	= WIN_FLUSH_CACHE_EXT;
1546 	else
1547 		args.tfRegister[IDE_COMMAND_OFFSET]	= WIN_FLUSH_CACHE;
1548 	args.command_type	 		= ide_cmd_type_parser(&args);
1549 	return ide_raw_taskfile(drive, &args, NULL);
1550 }
1551 
set_acoustic(ide_drive_t * drive,int arg)1552 static int set_acoustic (ide_drive_t *drive, int arg)
1553 {
1554 	ide_task_t args;
1555 
1556 	memset(&args, 0, sizeof(ide_task_t));
1557 	args.tfRegister[IDE_FEATURE_OFFSET]	= (arg) ? SETFEATURES_EN_AAM :
1558 							  SETFEATURES_DIS_AAM;
1559 	args.tfRegister[IDE_NSECTOR_OFFSET]	= arg;
1560 	args.tfRegister[IDE_COMMAND_OFFSET]	= WIN_SETFEATURES;
1561 	args.command_type = ide_cmd_type_parser(&args);
1562 	ide_raw_taskfile(drive, &args, NULL);
1563 	drive->acoustic = arg;
1564 	return 0;
1565 }
1566 
probe_lba_addressing(ide_drive_t * drive,int arg)1567 static int probe_lba_addressing (ide_drive_t *drive, int arg)
1568 {
1569 	drive->addressing =  0;
1570 
1571 	if (HWIF(drive)->addressing)
1572 		return 0;
1573 
1574 	if (!(drive->id->cfs_enable_2 & 0x0400))
1575                 return -EIO;
1576 	drive->addressing = arg;
1577 	return 0;
1578 }
1579 
set_lba_addressing(ide_drive_t * drive,int arg)1580 static int set_lba_addressing (ide_drive_t *drive, int arg)
1581 {
1582 	return (probe_lba_addressing(drive, arg));
1583 }
1584 
idedisk_add_settings(ide_drive_t * drive)1585 static void idedisk_add_settings(ide_drive_t *drive)
1586 {
1587 	struct hd_driveid *id = drive->id;
1588 	int major = HWIF(drive)->major;
1589 	int minor = drive->select.b.unit << PARTN_BITS;
1590 
1591 	ide_add_setting(drive,	"bios_cyl",		SETTING_RW,					-1,			-1,			TYPE_INT,	0,	65535,				1,	1,	&drive->bios_cyl,		NULL);
1592 	ide_add_setting(drive,	"bios_head",		SETTING_RW,					-1,			-1,			TYPE_BYTE,	0,	255,				1,	1,	&drive->bios_head,		NULL);
1593 	ide_add_setting(drive,	"bios_sect",		SETTING_RW,					-1,			-1,			TYPE_BYTE,	0,	63,				1,	1,	&drive->bios_sect,		NULL);
1594 	ide_add_setting(drive,	"address",		SETTING_RW,					HDIO_GET_ADDRESS,	HDIO_SET_ADDRESS,	TYPE_INTA,	0,	2,				1,	1,	&drive->addressing,	set_lba_addressing);
1595 	ide_add_setting(drive,	"bswap",		SETTING_READ,					-1,			-1,			TYPE_BYTE,	0,	1,				1,	1,	&drive->bswap,			NULL);
1596 	ide_add_setting(drive,	"multcount",		id ? SETTING_RW : SETTING_READ,			HDIO_GET_MULTCOUNT,	HDIO_SET_MULTCOUNT,	TYPE_BYTE,	0,	id ? id->max_multsect : 0,	1,	1,	&drive->mult_count,		set_multcount);
1597 	ide_add_setting(drive,	"nowerr",		SETTING_RW,					HDIO_GET_NOWERR,	HDIO_SET_NOWERR,	TYPE_BYTE,	0,	1,				1,	1,	&drive->nowerr,			set_nowerr);
1598 	ide_add_setting(drive,	"breada_readahead",	SETTING_RW,					BLKRAGET,		BLKRASET,		TYPE_INT,	0,	255,				1,	1,	&read_ahead[major],		NULL);
1599 	ide_add_setting(drive,	"file_readahead",	SETTING_RW,					BLKFRAGET,		BLKFRASET,		TYPE_INTA,	0,	4096,			PAGE_SIZE,	1024,	&max_readahead[major][minor],	NULL);
1600 	ide_add_setting(drive,	"max_kb_per_request",	SETTING_RW,					BLKSECTGET,		BLKSECTSET,		TYPE_INTA,	1,	255,				1,	1,	&max_sectors[major][minor],	NULL);
1601 	ide_add_setting(drive,	"lun",			SETTING_RW,					-1,			-1,			TYPE_INT,	0,	7,				1,	1,	&drive->lun,			NULL);
1602 	ide_add_setting(drive,	"wcache",		SETTING_RW,					HDIO_GET_WCACHE,	HDIO_SET_WCACHE,	TYPE_BYTE,	0,	1,				1,	1,	&drive->wcache,			write_cache);
1603 	ide_add_setting(drive,	"acoustic",		SETTING_RW,					HDIO_GET_ACOUSTIC,	HDIO_SET_ACOUSTIC,	TYPE_BYTE,	0,	254,				1,	1,	&drive->acoustic,		set_acoustic);
1604  	ide_add_setting(drive,	"failures",		SETTING_RW,					-1,			-1,			TYPE_INT,	0,	65535,				1,	1,	&drive->failures,		NULL);
1605  	ide_add_setting(drive,	"max_failures",		SETTING_RW,					-1,			-1,			TYPE_INT,	0,	65535,				1,	1,	&drive->max_failures,		NULL);
1606 }
1607 
idedisk_ioctl(ide_drive_t * drive,struct inode * inode,struct file * file,unsigned int cmd,unsigned long arg)1608 static int idedisk_ioctl (ide_drive_t *drive, struct inode *inode,
1609 		struct file *file, unsigned int cmd, unsigned long arg)
1610 {
1611 #if 0
1612 HDIO_GET_ADDRESS
1613 HDIO_SET_ADDRESS
1614 HDIO_GET_WCACHE
1615 HDIO_SET_WCACHE
1616 HDIO_GET_ACOUSTIC
1617 HDIO_SET_ACOUSTIC
1618 HDIO_GET_MULTCOUNT
1619 HDIO_SET_MULTCOUNT
1620 HDIO_GET_NOWERR
1621 HDIO_SET_NOWERR
1622 #endif
1623 	return -EINVAL;
1624 }
1625 
idedisk_setup(ide_drive_t * drive)1626 static void idedisk_setup (ide_drive_t *drive)
1627 {
1628 	int i;
1629 
1630 	struct hd_driveid *id = drive->id;
1631 	unsigned long capacity;
1632 
1633 	idedisk_add_settings(drive);
1634 
1635 	if (drive->id_read == 0)
1636 		return;
1637 
1638 	/*
1639 	 * CompactFlash cards and their brethern look just like hard drives
1640 	 * to us, but they are removable and don't have a doorlock mechanism.
1641 	 */
1642 	if (drive->removable && !(drive->is_flash)) {
1643 		/*
1644 		 * Removable disks (eg. SYQUEST); ignore 'WD' drives
1645 		 */
1646 		if (id->model[0] != 'W' || id->model[1] != 'D') {
1647 			drive->doorlocking = 1;
1648 		}
1649 	}
1650 	for (i = 0; i < MAX_DRIVES; ++i) {
1651 		ide_hwif_t *hwif = HWIF(drive);
1652 
1653 		if (drive != &hwif->drives[i]) continue;
1654 		hwif->gd->de_arr[i] = drive->de;
1655 		if (drive->removable)
1656 			hwif->gd->flags[i] |= GENHD_FL_REMOVABLE;
1657 		break;
1658 	}
1659 
1660 #if 1
1661 	(void) probe_lba_addressing(drive, 1);
1662 #else
1663 	/* if using 48-bit addressing bump the request size up */
1664 	if (probe_lba_addressing(drive, 1))
1665 		blk_queue_max_sectors(&drive->queue, 2048);
1666 #endif
1667 
1668 	/* Extract geometry if we did not already have one for the drive */
1669 	if (!drive->cyl || !drive->head || !drive->sect) {
1670 		drive->cyl     = drive->bios_cyl  = id->cyls;
1671 		drive->head    = drive->bios_head = id->heads;
1672 		drive->sect    = drive->bios_sect = id->sectors;
1673 	}
1674 
1675 	/* Handle logical geometry translation by the drive */
1676 	if ((id->field_valid & 1) && id->cur_cyls &&
1677 	    id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
1678 		drive->cyl  = id->cur_cyls;
1679 		drive->head = id->cur_heads;
1680 		drive->sect = id->cur_sectors;
1681 	}
1682 
1683 	/* Use physical geometry if what we have still makes no sense */
1684 	if (drive->head > 16 && id->heads && id->heads <= 16) {
1685 		drive->cyl  = id->cyls;
1686 		drive->head = id->heads;
1687 		drive->sect = id->sectors;
1688 	}
1689 
1690 	/* calculate drive capacity, and select LBA if possible */
1691 	init_idedisk_capacity (drive);
1692 
1693 	/*
1694 	 * if possible, give fdisk access to more of the drive,
1695 	 * by correcting bios_cyls:
1696 	 */
1697 	capacity = idedisk_capacity (drive);
1698 	if ((capacity >= (drive->bios_cyl * drive->bios_sect * drive->bios_head)) &&
1699 	    (!drive->forced_geom) && drive->bios_sect && drive->bios_head)
1700 		drive->bios_cyl = (capacity / drive->bios_sect) / drive->bios_head;
1701 	printk (KERN_INFO "%s: %ld sectors", drive->name, capacity);
1702 
1703 	/* Give size in megabytes (MB), not mebibytes (MiB). */
1704 	/* We compute the exact rounded value, avoiding overflow. */
1705 	printk (" (%ld MB)", (capacity - capacity/625 + 974)/1950);
1706 
1707 	/* Only print cache size when it was specified */
1708 	if (id->buf_size)
1709 		printk (" w/%dKiB Cache", id->buf_size/2);
1710 
1711 	printk(", CHS=%d/%d/%d",
1712 	       drive->bios_cyl, drive->bios_head, drive->bios_sect);
1713 #ifdef CONFIG_BLK_DEV_IDEDMA
1714 	if (drive->using_dma)
1715 		(void) HWIF(drive)->ide_dma_verbose(drive);
1716 #endif /* CONFIG_BLK_DEV_IDEDMA */
1717 	printk("\n");
1718 
1719 	drive->mult_count = 0;
1720 	if (id->max_multsect) {
1721 		id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
1722 		id->multsect_valid = id->multsect ? 1 : 0;
1723 		drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
1724 		drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
1725 	}
1726 	drive->no_io_32bit = id->dword_io ? 1 : 0;
1727 	if (drive->id->cfs_enable_2 & 0x3000)
1728 		write_cache(drive, (id->cfs_enable_2 & 0x3000));
1729 }
1730 
idedisk_cleanup(ide_drive_t * drive)1731 static int idedisk_cleanup(ide_drive_t *drive)
1732 {
1733 	ide_cacheflush_p(drive);
1734 	return ide_unregister_subdriver(drive);
1735 }
1736 
1737 int idedisk_init (void);
1738 int idedisk_attach(ide_drive_t *drive);
1739 
1740 /*
1741  *      IDE subdriver functions, registered with ide.c
1742  */
1743 static ide_driver_t idedisk_driver = {
1744 	name:			"ide-disk",
1745 	version:		IDEDISK_VERSION,
1746 	media:			ide_disk,
1747 	busy:			0,
1748 	supports_dma:		1,
1749 	supports_dsc_overlap:	0,
1750 	cleanup:		idedisk_cleanup,
1751 	standby:		do_idedisk_standby,
1752 	suspend:		do_idedisk_suspend,
1753 	resume:			do_idedisk_resume,
1754 	flushcache:		do_idedisk_flushcache,
1755 	do_request:		ide_do_rw_disk,
1756 	end_request:		idedisk_end_request,
1757 	sense:			idedisk_dump_status,
1758 	error:			idedisk_error,
1759 	abort:			idedisk_abort,
1760 	ioctl:			idedisk_ioctl,
1761 	open:			idedisk_open,
1762 	release:		idedisk_release,
1763 	media_change:		idedisk_media_change,
1764 	revalidate:		idedisk_revalidate,
1765 	pre_reset:		idedisk_pre_reset,
1766 	capacity:		idedisk_capacity,
1767 	special:		idedisk_special,
1768 	proc:			idedisk_proc,
1769 	init:			idedisk_init,
1770 	attach:			idedisk_attach,
1771 	ata_prebuilder:		NULL,
1772 	atapi_prebuilder:	NULL,
1773 };
1774 
1775 static ide_module_t idedisk_module = {
1776 	IDE_DRIVER_MODULE,
1777 	idedisk_init,
1778 	&idedisk_driver,
1779 	NULL
1780 };
1781 
1782 MODULE_DESCRIPTION("ATA DISK Driver");
1783 
idedisk_attach(ide_drive_t * drive)1784 int idedisk_attach (ide_drive_t *drive)
1785 {
1786 	int ret = 0;
1787 
1788 	MOD_INC_USE_COUNT;
1789 	if (ide_register_subdriver(drive,
1790 			&idedisk_driver, IDE_SUBDRIVER_VERSION)) {
1791 		printk(KERN_ERR "ide-disk: %s: Failed to register the "
1792 			"driver with ide.c\n", drive->name);
1793 		ret= 1;
1794 		goto bye_game_over;
1795 	}
1796 	DRIVER(drive)->busy++;
1797 	idedisk_setup(drive);
1798 	if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1799 		printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1800 			drive->name, drive->head);
1801 		(void) idedisk_cleanup(drive);
1802 		ret= 1;
1803 	}
1804 	DRIVER(drive)->busy--;
1805 bye_game_over:
1806 	MOD_DEC_USE_COUNT;
1807 	return ret;
1808 }
1809 
idedisk_exit(void)1810 static void __exit idedisk_exit (void)
1811 {
1812 	ide_drive_t *drive;
1813 	int failed = 0;
1814 
1815 	while ((drive = ide_scan_devices(ide_disk, idedisk_driver.name,
1816 			&idedisk_driver, failed)) != NULL) {
1817 		if (idedisk_cleanup (drive)) {
1818 			printk(KERN_ERR "%s: cleanup_module() called while "
1819 				"still busy\n", drive->name);
1820 			failed++;
1821 		}
1822 #ifdef CONFIG_PROC_FS
1823 		/* We must remove proc entries defined in this module.
1824 		 * Otherwise we oops while accessing these entries
1825 		 */
1826 		if (drive->proc)
1827 			ide_remove_proc_entries(drive->proc, idedisk_proc);
1828 #endif
1829 	}
1830 	ide_unregister_module(&idedisk_module);
1831 }
1832 
idedisk_init(void)1833 int idedisk_init (void)
1834 {
1835 #ifdef CLASSIC_BUILTINS_METHOD
1836 	ide_drive_t *drive;
1837 	int failed = 0;
1838 #endif /* CLASSIC_BUILTINS_METHOD */
1839 
1840 	MOD_INC_USE_COUNT;
1841 
1842 #ifdef CLASSIC_BUILTINS_METHOD
1843 	while ((drive = ide_scan_devices(ide_disk,
1844 			idedisk_driver.name, NULL, failed++)) != NULL) {
1845 		if (ide_register_subdriver(drive,
1846 				&idedisk_driver, IDE_SUBDRIVER_VERSION)) {
1847 			printk(KERN_ERR "ide-disk: %s: Failed to register "
1848 				"the driver with ide.c\n", drive->name);
1849 			continue;
1850 		}
1851 		DRIVER(drive)->busy++;
1852 		idedisk_setup(drive);
1853 		if ((!drive->head || drive->head > 16) &&
1854 		    (!drive->select.b.lba)) {
1855 			printk(KERN_ERR "%s: INVALID GEOMETRY: %d "
1856 				"PHYSICAL HEADS?\n", drive->name, drive->head);
1857 			(void) idedisk_cleanup(drive);
1858 			DRIVER(drive)->busy--;
1859 			continue;
1860 		}
1861 		DRIVER(drive)->busy--;
1862 		failed--;
1863 	}
1864 #endif /* CLASSIC_BUILTINS_METHOD */
1865 
1866 	ide_register_module(&idedisk_module);
1867 	MOD_DEC_USE_COUNT;
1868 	return 0;
1869 }
1870 
panic_box(ide_drive_t * drive)1871 ide_startstop_t panic_box(ide_drive_t *drive)
1872 {
1873 #if 0
1874 	panic("%s: Attempted to corrupt something: ide operation "
1875 #else
1876 	printk(KERN_ERR "%s: Attempted to corrupt something: ide operation "
1877 #endif
1878 		"was pending accross suspend/resume.\n", drive->name);
1879 	return ide_stopped;
1880 }
1881 
ide_disks_busy(void)1882 int ide_disks_busy(void)
1883 {
1884 	int i;
1885 	for (i=0; i<MAX_HWIFS; i++) {
1886 		struct hwgroup_s *hwgroup = ide_hwifs[i].hwgroup;
1887 		if (!hwgroup) continue;
1888 		if ((hwgroup->handler) && (hwgroup->handler != panic_box))
1889 			return 1;
1890 	}
1891 	return 0;
1892 }
1893 
ide_disk_suspend(void)1894 void ide_disk_suspend(void)
1895 {
1896 	int i;
1897 	while (ide_disks_busy()) {
1898 		printk("*");
1899 		schedule();
1900 	}
1901 	for (i=0; i<MAX_HWIFS; i++) {
1902 		struct hwgroup_s *hwgroup = ide_hwifs[i].hwgroup;
1903 
1904 		if (!hwgroup) continue;
1905 		hwgroup->handler_save = hwgroup->handler;
1906 		hwgroup->handler = panic_box;
1907 	}
1908 	driver_blocked = 1;
1909 	if (ide_disks_busy())
1910 		panic("How did you get that request through?!");
1911 }
1912 
1913 /* unsuspend and resume should be equal in the ideal world */
1914 
ide_disk_unsuspend(void)1915 void ide_disk_unsuspend(void)
1916 {
1917 	int i;
1918 	for (i=0; i<MAX_HWIFS; i++) {
1919 		struct hwgroup_s *hwgroup = ide_hwifs[i].hwgroup;
1920 
1921 		if (!hwgroup) continue;
1922 		hwgroup->handler = NULL; /* hwgroup->handler_save; */
1923 		hwgroup->handler_save = NULL;
1924 	}
1925 	driver_blocked = 0;
1926 }
1927 
ide_disk_resume(void)1928 void ide_disk_resume(void)
1929 {
1930 	int i;
1931 	for (i=0; i<MAX_HWIFS; i++) {
1932 		struct hwgroup_s *hwgroup = ide_hwifs[i].hwgroup;
1933 
1934 		if (!hwgroup) continue;
1935 		if (hwgroup->handler != panic_box)
1936 			panic("Handler was not set to panic?");
1937 		hwgroup->handler_save = NULL;
1938 		hwgroup->handler = NULL;
1939 	}
1940 	driver_blocked = 0;
1941 }
1942 
1943 module_init(idedisk_init);
1944 module_exit(idedisk_exit);
1945 MODULE_LICENSE("GPL");
1946