1/*
2 *  linux/drivers/ide/ide-disk.c	Version 1.10	June 9, 2000
3 *
4 *  Copyright (C) 1994-1998  Linus Torvalds & authors (see below)
5 */
6
7/*
8 *  Mostly written by Mark Lord <mlord@pobox.com>
9 *                and Gadi Oxman <gadio@netvision.net.il>
10 *                and Andre Hedrick <andre@linux-ide.org>
11 *
12 * This is the IDE/ATA disk driver, as evolved from hd.c and ide.c.
13 *
14 * Version 1.00		move disk only code from ide.c to ide-disk.c
15 *			support optional byte-swapping of all data
16 * Version 1.01		fix previous byte-swapping code
17 * Version 1.02		remove ", LBA" from drive identification msgs
18 * Version 1.03		fix display of id->buf_size for big-endian
19 * Version 1.04		add /proc configurable settings and S.M.A.R.T support
20 * Version 1.05		add capacity support for ATA3 >= 8GB
21 * Version 1.06		get boot-up messages to show full cyl count
22 * Version 1.07		disable door-locking if it fails
23 * Version 1.08		fixed CHS/LBA translations for ATA4 > 8GB,
24 *			process of adding new ATA4 compliance.
25 *			fixed problems in allowing fdisk to see
26 *			the entire disk.
27 * Version 1.09		added increment of rq->sector in ide_multwrite
28 *			added UDMA 3/4 reporting
29 * Version 1.10		request queue changes, Ultra DMA 100
30 * Version 1.11		added 48-bit lba
31 * Version 1.12		adding taskfile io access method
32 *			Highmem I/O support, Jens Axboe <axboe@suse.de>
33 */
34
35#define IDEDISK_VERSION	"1.12"
36
37#undef REALLY_SLOW_IO		/* most systems can safely undef this */
38
39#include <linux/config.h>
40#include <linux/module.h>
41#include <linux/types.h>
42#include <linux/string.h>
43#include <linux/kernel.h>
44#include <linux/timer.h>
45#include <linux/mm.h>
46#include <linux/interrupt.h>
47#include <linux/major.h>
48#include <linux/errno.h>
49#include <linux/genhd.h>
50#include <linux/slab.h>
51#include <linux/delay.h>
52#include <linux/ide.h>
53
54#include <asm/byteorder.h>
55#include <asm/irq.h>
56#include <asm/uaccess.h>
57#include <asm/io.h>
58
59#ifdef CONFIG_BLK_DEV_PDC4030
60#define IS_PDC4030_DRIVE (HWIF(drive)->chipset == ide_pdc4030)
61#else
62#define IS_PDC4030_DRIVE (0)	/* auto-NULLs out pdc4030 code */
63#endif
64
65#ifdef CONFIG_IDE_TASKFILE_IO
66#  undef __TASKFILE__IO /* define __TASKFILE__IO */
67#else /* CONFIG_IDE_TASKFILE_IO */
68#  undef __TASKFILE__IO
69#endif /* CONFIG_IDE_TASKFILE_IO */
70
71#ifndef __TASKFILE__IO
72
73static void idedisk_bswap_data (void *buffer, int wcount)
74{
75	u16 *p = buffer;
76
77	while (wcount--) {
78		*p = *p << 8 | *p >> 8; p++;
79		*p = *p << 8 | *p >> 8; p++;
80	}
81}
82
83static inline void idedisk_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
84{
85	ide_input_data(drive, buffer, wcount);
86	if (drive->bswap)
87		idedisk_bswap_data(buffer, wcount);
88}
89
90static inline void idedisk_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
91{
92	if (drive->bswap) {
93		idedisk_bswap_data(buffer, wcount);
94		ide_output_data(drive, buffer, wcount);
95		idedisk_bswap_data(buffer, wcount);
96	} else
97		ide_output_data(drive, buffer, wcount);
98}
99
100#endif /* __TASKFILE__IO */
101
102/*
103 * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
104 * value for this drive (from its reported identification information).
105 *
106 * Returns:	1 if lba_capacity looks sensible
107 *		0 otherwise
108 *
109 * It is called only once for each drive.
110 */
111static int lba_capacity_is_ok (struct hd_driveid *id)
112{
113	unsigned long lba_sects, chs_sects, head, tail;
114
115	if ((id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)) {
116		printk("48-bit Drive: %llu \n", id->lba_capacity_2);
117		return 1;
118	}
119
120	/*
121	 * The ATA spec tells large drives to return
122	 * C/H/S = 16383/16/63 independent of their size.
123	 * Some drives can be jumpered to use 15 heads instead of 16.
124	 * Some drives can be jumpered to use 4092 cyls instead of 16383.
125	 */
126	if ((id->cyls == 16383
127	     || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
128	    id->sectors == 63 &&
129	    (id->heads == 15 || id->heads == 16) &&
130	    id->lba_capacity >= 16383*63*id->heads)
131		return 1;
132
133	lba_sects   = id->lba_capacity;
134	chs_sects   = id->cyls * id->heads * id->sectors;
135
136	/* perform a rough sanity check on lba_sects:  within 10% is OK */
137	if ((lba_sects - chs_sects) < chs_sects/10)
138		return 1;
139
140	/* some drives have the word order reversed */
141	head = ((lba_sects >> 16) & 0xffff);
142	tail = (lba_sects & 0xffff);
143	lba_sects = (head | (tail << 16));
144	if ((lba_sects - chs_sects) < chs_sects/10) {
145		id->lba_capacity = lba_sects;
146		return 1;	/* lba_capacity is (now) good */
147	}
148
149	return 0;	/* lba_capacity value may be bad */
150}
151
152#ifndef __TASKFILE__IO
153
154/*
155 * read_intr() is the handler for disk read/multread interrupts
156 */
157static ide_startstop_t read_intr (ide_drive_t *drive)
158{
159	byte stat;
160	int i;
161	unsigned int msect, nsect;
162	unsigned long flags;
163	struct request *rq;
164	char *to;
165
166	/* new way for dealing with premature shared PCI interrupts */
167	if (!OK_STAT(stat=GET_STAT(),DATA_READY,BAD_R_STAT)) {
168		if (stat & (ERR_STAT|DRQ_STAT)) {
169			return ide_error(drive, "read_intr", stat);
170		}
171		/* no data yet, so wait for another interrupt */
172		ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
173		return ide_started;
174	}
175
176	msect = drive->mult_count;
177read_next:
178	rq = HWGROUP(drive)->rq;
179	if (msect) {
180		if ((nsect = rq->current_nr_sectors) > msect)
181			nsect = msect;
182		msect -= nsect;
183	} else
184		nsect = 1;
185	to = ide_map_buffer(rq, &flags);
186	idedisk_input_data(drive, to, nsect * SECTOR_WORDS);
187#ifdef DEBUG
188	printk("%s:  read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
189		drive->name, rq->sector, rq->sector+nsect-1,
190		(unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
191#endif
192	ide_unmap_buffer(to, &flags);
193	rq->sector += nsect;
194	rq->errors = 0;
195	i = (rq->nr_sectors -= nsect);
196	if (((long)(rq->current_nr_sectors -= nsect)) <= 0)
197		ide_end_request(1, HWGROUP(drive));
198	if (i > 0) {
199		if (msect)
200			goto read_next;
201		ide_set_handler (drive, &read_intr, WAIT_CMD, NULL);
202                return ide_started;
203	}
204        return ide_stopped;
205}
206
207/*
208 * write_intr() is the handler for disk write interrupts
209 */
210static ide_startstop_t write_intr (ide_drive_t *drive)
211{
212	byte stat;
213	int i;
214	ide_hwgroup_t *hwgroup = HWGROUP(drive);
215	struct request *rq = hwgroup->rq;
216
217	if (!OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
218		printk("%s: write_intr error1: nr_sectors=%ld, stat=0x%02x\n", drive->name, rq->nr_sectors, stat);
219        } else {
220#ifdef DEBUG
221		printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
222			drive->name, rq->sector, (unsigned long) rq->buffer,
223			rq->nr_sectors-1);
224#endif
225		if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
226			rq->sector++;
227			rq->errors = 0;
228			i = --rq->nr_sectors;
229			--rq->current_nr_sectors;
230			if (((long)rq->current_nr_sectors) <= 0)
231				ide_end_request(1, hwgroup);
232			if (i > 0) {
233				unsigned long flags;
234				char *to = ide_map_buffer(rq, &flags);
235				idedisk_output_data (drive, to, SECTOR_WORDS);
236				ide_unmap_buffer(to, &flags);
237				ide_set_handler (drive, &write_intr, WAIT_CMD, NULL);
238                                return ide_started;
239			}
240                        return ide_stopped;
241		}
242		return ide_stopped;	/* the original code did this here (?) */
243	}
244	return ide_error(drive, "write_intr", stat);
245}
246
247/*
248 * ide_multwrite() transfers a block of up to mcount sectors of data
249 * to a drive as part of a disk multiple-sector write operation.
250 *
251 * Returns 0 on success.
252 *
253 * Note that we may be called from two contexts - the do_rw_disk context
254 * and IRQ context. The IRQ can happen any time after we've output the
255 * full "mcount" number of sectors, so we must make sure we update the
256 * state _before_ we output the final part of the data!
257 */
258int ide_multwrite (ide_drive_t *drive, unsigned int mcount)
259{
260 	ide_hwgroup_t	*hwgroup= HWGROUP(drive);
261 	struct request	*rq = &hwgroup->wrq;
262
263  	do {
264  		char *buffer;
265  		int nsect = rq->current_nr_sectors;
266		unsigned long flags;
267
268		if (nsect > mcount)
269			nsect = mcount;
270		mcount -= nsect;
271
272		buffer = ide_map_buffer(rq, &flags);
273		rq->sector += nsect;
274		rq->nr_sectors -= nsect;
275		rq->current_nr_sectors -= nsect;
276
277		/* Do we move to the next bh after this? */
278		if (!rq->current_nr_sectors) {
279			struct buffer_head *bh = rq->bh->b_reqnext;
280
281			/* end early early we ran out of requests */
282			if (!bh) {
283				mcount = 0;
284			} else {
285				rq->bh = bh;
286				rq->current_nr_sectors = bh->b_size >> 9;
287				rq->hard_cur_sectors = rq->current_nr_sectors;
288			}
289		}
290
291		/*
292		 * Ok, we're all setup for the interrupt
293		 * re-entering us on the last transfer.
294		 */
295		idedisk_output_data(drive, buffer, nsect<<7);
296		ide_unmap_buffer(buffer, &flags);
297	} while (mcount);
298
299        return 0;
300}
301
302/*
303 * multwrite_intr() is the handler for disk multwrite interrupts
304 */
305static ide_startstop_t multwrite_intr (ide_drive_t *drive)
306{
307	byte stat;
308	int i;
309	ide_hwgroup_t *hwgroup = HWGROUP(drive);
310	struct request *rq = &hwgroup->wrq;
311
312	if (OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
313		if (stat & DRQ_STAT) {
314			/*
315			 *	The drive wants data. Remember rq is the copy
316			 *	of the request
317			 */
318			if (rq->nr_sectors) {
319				if (ide_multwrite(drive, drive->mult_count))
320					return ide_stopped;
321				ide_set_handler (drive, &multwrite_intr, WAIT_CMD, NULL);
322				return ide_started;
323			}
324		} else {
325			/*
326			 *	If the copy has all the blocks completed then
327			 *	we can end the original request.
328			 */
329			if (!rq->nr_sectors) {	/* all done? */
330				rq = hwgroup->rq;
331				for (i = rq->nr_sectors; i > 0;){
332					i -= rq->current_nr_sectors;
333					ide_end_request(1, hwgroup);
334				}
335				return ide_stopped;
336			}
337		}
338		return ide_stopped;	/* the original code did this here (?) */
339	}
340	return ide_error(drive, "multwrite_intr", stat);
341}
342#endif /* __TASKFILE__IO */
343
344#ifdef __TASKFILE__IO
345
346static ide_startstop_t chs_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block);
347static ide_startstop_t lba_28_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block);
348static ide_startstop_t lba_48_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long long block);
349
350/*
351 * do_rw_disk() issues READ and WRITE commands to a disk,
352 * using LBA if supported, or CHS otherwise, to address sectors.
353 * It also takes care of issuing special DRIVE_CMDs.
354 */
355static ide_startstop_t do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
356{
357	if (rq->cmd == READ)
358		goto good_command;
359	if (rq->cmd == WRITE)
360		goto good_command;
361
362	printk(KERN_ERR "%s: bad command: %d\n", drive->name, rq->cmd);
363	ide_end_request(0, HWGROUP(drive));
364	return ide_stopped;
365
366good_command:
367
368#ifdef CONFIG_BLK_DEV_PDC4030
369	if (IS_PDC4030_DRIVE) {
370		extern ide_startstop_t promise_rw_disk(ide_drive_t *, struct request *, unsigned long);
371		return promise_rw_disk(drive, rq, block);
372	}
373#endif /* CONFIG_BLK_DEV_PDC4030 */
374
375	if ((drive->id->cfs_enable_2 & 0x0400) && (drive->addressing))	/* 48-bit LBA */
376		return lba_48_rw_disk(drive, rq, (unsigned long long) block);
377	if (drive->select.b.lba)		/* 28-bit LBA */
378		return lba_28_rw_disk(drive, rq, (unsigned long) block);
379
380	/* 28-bit CHS : DIE DIE DIE piece of legacy crap!!! */
381	return chs_rw_disk(drive, rq, (unsigned long) block);
382}
383
384static task_ioreg_t get_command (ide_drive_t *drive, int cmd)
385{
386	int lba48bit = (drive->id->cfs_enable_2 & 0x0400) ? 1 : 0;
387
388	lba48bit = drive->addressing;
389
390	if ((cmd == READ) && (drive->using_dma))
391		return (lba48bit) ? WIN_READDMA_EXT : WIN_READDMA;
392	else if ((cmd == READ) && (drive->mult_count))
393		return (lba48bit) ? WIN_MULTREAD_EXT : WIN_MULTREAD;
394	else if (cmd == READ)
395		return (lba48bit) ? WIN_READ_EXT : WIN_READ;
396	else if ((cmd == WRITE) && (drive->using_dma))
397		return (lba48bit) ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
398	else if ((cmd == WRITE) && (drive->mult_count))
399		return (lba48bit) ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
400	else if (cmd == WRITE)
401		return (lba48bit) ? WIN_WRITE_EXT : WIN_WRITE;
402	else
403		return WIN_NOP;
404}
405
406static ide_startstop_t chs_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
407{
408	struct hd_drive_task_hdr	taskfile;
409	struct hd_drive_hob_hdr		hobfile;
410	ide_task_t			args;
411
412	task_ioreg_t command	= get_command(drive, rq->cmd);
413	unsigned int track	= (block / drive->sect);
414	unsigned int sect	= (block % drive->sect) + 1;
415	unsigned int head	= (track % drive->head);
416	unsigned int cyl	= (track / drive->head);
417
418	memset(&taskfile, 0, sizeof(task_struct_t));
419	memset(&hobfile, 0, sizeof(hob_struct_t));
420
421	taskfile.sector_count	= (rq->nr_sectors==256)?0x00:rq->nr_sectors;
422	taskfile.sector_number	= sect;
423	taskfile.low_cylinder	= cyl;
424	taskfile.high_cylinder	= (cyl>>8);
425	taskfile.device_head	= head;
426	taskfile.device_head	|= drive->select.all;
427	taskfile.command	= command;
428
429#ifdef DEBUG
430	printk("%s: %sing: ", drive->name, (rq->cmd==READ) ? "read" : "writ");
431	if (lba)	printk("LBAsect=%lld, ", block);
432	else		printk("CHS=%d/%d/%d, ", cyl, head, sect);
433	printk("sectors=%ld, ", rq->nr_sectors);
434	printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
435#endif
436
437	memcpy(args.tfRegister, &taskfile, sizeof(struct hd_drive_task_hdr));
438	memcpy(args.hobRegister, &hobfile, sizeof(struct hd_drive_hob_hdr));
439	args.command_type	= ide_cmd_type_parser(&args);
440	args.prehandler		= ide_pre_handler_parser(&taskfile, &hobfile);
441	args.handler		= ide_handler_parser(&taskfile, &hobfile);
442	args.posthandler	= NULL;
443	args.rq			= (struct request *) rq;
444	args.block		= block;
445	rq->special		= NULL;
446	rq->special		= (ide_task_t *)&args;
447
448	return do_rw_taskfile(drive, &args);
449}
450
451static ide_startstop_t lba_28_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
452{
453	struct hd_drive_task_hdr	taskfile;
454	struct hd_drive_hob_hdr		hobfile;
455	ide_task_t			args;
456
457	task_ioreg_t command	= get_command(drive, rq->cmd);
458
459	memset(&taskfile, 0, sizeof(task_struct_t));
460	memset(&hobfile, 0, sizeof(hob_struct_t));
461
462	taskfile.sector_count	= (rq->nr_sectors==256)?0x00:rq->nr_sectors;
463	taskfile.sector_number	= block;
464	taskfile.low_cylinder	= (block>>=8);
465	taskfile.high_cylinder	= (block>>=8);
466	taskfile.device_head	= ((block>>8)&0x0f);
467	taskfile.device_head	|= drive->select.all;
468	taskfile.command	= command;
469
470
471#ifdef DEBUG
472	printk("%s: %sing: ", drive->name, (rq->cmd==READ) ? "read" : "writ");
473	if (lba)	printk("LBAsect=%lld, ", block);
474	else		printk("CHS=%d/%d/%d, ", cyl, head, sect);
475	printk("sectors=%ld, ", rq->nr_sectors);
476	printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
477#endif
478
479	memcpy(args.tfRegister, &taskfile, sizeof(struct hd_drive_task_hdr));
480	memcpy(args.hobRegister, &hobfile, sizeof(struct hd_drive_hob_hdr));
481	args.command_type	= ide_cmd_type_parser(&args);
482	args.prehandler		= ide_pre_handler_parser(&taskfile, &hobfile);
483	args.handler		= ide_handler_parser(&taskfile, &hobfile);
484	args.posthandler	= NULL;
485	args.rq			= (struct request *) rq;
486	args.block		= block;
487	rq->special		= NULL;
488	rq->special		= (ide_task_t *)&args;
489
490	return do_rw_taskfile(drive, &args);
491}
492
493/*
494 * 268435455  == 137439 MB or 28bit limit
495 * 320173056  == 163929 MB or 48bit addressing
496 * 1073741822 == 549756 MB or 48bit addressing fake drive
497 */
498
499static ide_startstop_t lba_48_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long long block)
500{
501	struct hd_drive_task_hdr	taskfile;
502	struct hd_drive_hob_hdr		hobfile;
503	ide_task_t			args;
504
505	task_ioreg_t command	= get_command(drive, rq->cmd);
506
507	memset(&taskfile, 0, sizeof(task_struct_t));
508	memset(&hobfile, 0, sizeof(hob_struct_t));
509
510	taskfile.sector_count	= rq->nr_sectors;
511	hobfile.sector_count	= (rq->nr_sectors>>8);
512
513	if (rq->nr_sectors == 65536) {
514		taskfile.sector_count	= 0x00;
515		hobfile.sector_count	= 0x00;
516	}
517
518	taskfile.sector_number	= block;	/* low lba */
519	taskfile.low_cylinder	= (block>>=8);	/* mid lba */
520	taskfile.high_cylinder	= (block>>=8);	/* hi  lba */
521	hobfile.sector_number	= (block>>=8);	/* low lba */
522	hobfile.low_cylinder	= (block>>=8);	/* mid lba */
523	hobfile.high_cylinder	= (block>>=8);	/* hi  lba */
524	taskfile.device_head	= drive->select.all;
525	hobfile.device_head	= taskfile.device_head;
526	hobfile.control		= (drive->ctl|0x80);
527	taskfile.command	= command;
528
529#ifdef DEBUG
530	printk("%s: %sing: ", drive->name, (rq->cmd==READ) ? "read" : "writ");
531	if (lba)	printk("LBAsect=%lld, ", block);
532	else		printk("CHS=%d/%d/%d, ", cyl, head, sect);
533	printk("sectors=%ld, ", rq->nr_sectors);
534	printk("buffer=0x%08lx\n", (unsigned long) rq->buffer);
535#endif
536
537	memcpy(args.tfRegister, &taskfile, sizeof(struct hd_drive_task_hdr));
538	memcpy(args.hobRegister, &hobfile, sizeof(struct hd_drive_hob_hdr));
539	args.command_type	= ide_cmd_type_parser(&args);
540	args.prehandler		= ide_pre_handler_parser(&taskfile, &hobfile);
541	args.handler		= ide_handler_parser(&taskfile, &hobfile);
542	args.posthandler	= NULL;
543	args.rq			= (struct request *) rq;
544	args.block		= block;
545	rq->special		= NULL;
546	rq->special		= (ide_task_t *)&args;
547
548	return do_rw_taskfile(drive, &args);
549}
550
551#else /* !__TASKFILE__IO */
552/*
553 * do_rw_disk() issues READ and WRITE commands to a disk,
554 * using LBA if supported, or CHS otherwise, to address sectors.
555 * It also takes care of issuing special DRIVE_CMDs.
556 */
557static ide_startstop_t do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
558{
559	if (IDE_CONTROL_REG)
560		OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
561
562#ifdef CONFIG_BLK_DEV_PDC4030
563	if (drive->select.b.lba || IS_PDC4030_DRIVE) {
564#else /* !CONFIG_BLK_DEV_PDC4030 */
565	if (drive->select.b.lba) {
566#endif /* CONFIG_BLK_DEV_PDC4030 */
567
568		if ((drive->id->cfs_enable_2 & 0x0400) && (drive->addressing)) {
569			task_ioreg_t tasklets[10];
570
571			tasklets[0] = 0;
572			tasklets[1] = 0;
573			tasklets[2] = rq->nr_sectors;
574			tasklets[3] = (rq->nr_sectors>>8);
575			if (rq->nr_sectors == 65536) {
576				tasklets[2] = 0x00;
577				tasklets[3] = 0x00;
578			}
579			tasklets[4] = (task_ioreg_t) block;
580			tasklets[5] = (task_ioreg_t) (block>>8);
581			tasklets[6] = (task_ioreg_t) (block>>16);
582			tasklets[7] = (task_ioreg_t) (block>>24);
583			tasklets[8] = (task_ioreg_t) 0;
584			tasklets[9] = (task_ioreg_t) 0;
585//			tasklets[8] = (task_ioreg_t) (block>>32);
586//			tasklets[9] = (task_ioreg_t) (block>>40);
587#ifdef DEBUG
588			printk("%s: %sing: LBAsect=%lu, sectors=%ld, buffer=0x%08lx, LBAsect=0x%012lx\n",
589				drive->name,
590				(rq->cmd==READ)?"read":"writ",
591				block,
592				rq->nr_sectors,
593				(unsigned long) rq->buffer,
594				block);
595			printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
596				drive->name, tasklets[3], tasklets[2],
597				tasklets[9], tasklets[8], tasklets[7],
598				tasklets[6], tasklets[5], tasklets[4]);
599#endif
600			OUT_BYTE(tasklets[1], IDE_FEATURE_REG);
601			OUT_BYTE(tasklets[3], IDE_NSECTOR_REG);
602			OUT_BYTE(tasklets[7], IDE_SECTOR_REG);
603			OUT_BYTE(tasklets[8], IDE_LCYL_REG);
604			OUT_BYTE(tasklets[9], IDE_HCYL_REG);
605
606			OUT_BYTE(tasklets[0], IDE_FEATURE_REG);
607			OUT_BYTE(tasklets[2], IDE_NSECTOR_REG);
608			OUT_BYTE(tasklets[4], IDE_SECTOR_REG);
609			OUT_BYTE(tasklets[5], IDE_LCYL_REG);
610			OUT_BYTE(tasklets[6], IDE_HCYL_REG);
611			OUT_BYTE(0x00|drive->select.all,IDE_SELECT_REG);
612		} else {
613#ifdef DEBUG
614			printk("%s: %sing: LBAsect=%ld, sectors=%ld, buffer=0x%08lx\n",
615				drive->name, (rq->cmd==READ)?"read":"writ",
616				block, rq->nr_sectors, (unsigned long) rq->buffer);
617#endif
618			OUT_BYTE(0x00, IDE_FEATURE_REG);
619			OUT_BYTE((rq->nr_sectors==256)?0x00:rq->nr_sectors,IDE_NSECTOR_REG);
620			OUT_BYTE(block,IDE_SECTOR_REG);
621			OUT_BYTE(block>>=8,IDE_LCYL_REG);
622			OUT_BYTE(block>>=8,IDE_HCYL_REG);
623			OUT_BYTE(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
624		}
625	} else {
626		unsigned int sect,head,cyl,track;
627		track = block / drive->sect;
628		sect  = block % drive->sect + 1;
629		OUT_BYTE(sect,IDE_SECTOR_REG);
630		head  = track % drive->head;
631		cyl   = track / drive->head;
632
633		OUT_BYTE(0x00, IDE_FEATURE_REG);
634		OUT_BYTE((rq->nr_sectors==256)?0x00:rq->nr_sectors,IDE_NSECTOR_REG);
635		OUT_BYTE(cyl,IDE_LCYL_REG);
636		OUT_BYTE(cyl>>8,IDE_HCYL_REG);
637		OUT_BYTE(head|drive->select.all,IDE_SELECT_REG);
638#ifdef DEBUG
639		printk("%s: %sing: CHS=%d/%d/%d, sectors=%ld, buffer=0x%08lx\n",
640			drive->name, (rq->cmd==READ)?"read":"writ", cyl,
641			head, sect, rq->nr_sectors, (unsigned long) rq->buffer);
642#endif
643	}
644#ifdef CONFIG_BLK_DEV_PDC4030
645	if (IS_PDC4030_DRIVE) {
646		extern ide_startstop_t do_pdc4030_io(ide_drive_t *, struct request *);
647		return do_pdc4030_io (drive, rq);
648	}
649#endif /* CONFIG_BLK_DEV_PDC4030 */
650	if (rq->cmd == READ) {
651#ifdef CONFIG_BLK_DEV_IDEDMA
652		if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_read, drive)))
653			return ide_started;
654#endif /* CONFIG_BLK_DEV_IDEDMA */
655		ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
656		if ((drive->id->cfs_enable_2 & 0x0400) && (drive->addressing)) {
657			OUT_BYTE(drive->mult_count ? WIN_MULTREAD_EXT : WIN_READ_EXT, IDE_COMMAND_REG);
658		} else {
659			OUT_BYTE(drive->mult_count ? WIN_MULTREAD : WIN_READ, IDE_COMMAND_REG);
660		}
661		return ide_started;
662	}
663	if (rq->cmd == WRITE) {
664		ide_startstop_t startstop;
665#ifdef CONFIG_BLK_DEV_IDEDMA
666		if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_write, drive)))
667			return ide_started;
668#endif /* CONFIG_BLK_DEV_IDEDMA */
669		if ((drive->id->cfs_enable_2 & 0x0400) && (drive->addressing)) {
670			OUT_BYTE(drive->mult_count ? WIN_MULTWRITE_EXT : WIN_WRITE_EXT, IDE_COMMAND_REG);
671		} else {
672			OUT_BYTE(drive->mult_count ? WIN_MULTWRITE : WIN_WRITE, IDE_COMMAND_REG);
673		}
674		if (ide_wait_stat(&startstop, drive, DATA_READY, drive->bad_wstat, WAIT_DRQ)) {
675			printk(KERN_ERR "%s: no DRQ after issuing %s\n", drive->name,
676				drive->mult_count ? "MULTWRITE" : "WRITE");
677			return startstop;
678		}
679		if (!drive->unmask)
680			__cli();	/* local CPU only */
681		if (drive->mult_count) {
682			ide_hwgroup_t *hwgroup = HWGROUP(drive);
683	/*
684	 * Ugh.. this part looks ugly because we MUST set up
685	 * the interrupt handler before outputting the first block
686	 * of data to be written.  If we hit an error (corrupted buffer list)
687	 * in ide_multwrite(), then we need to remove the handler/timer
688	 * before returning.  Fortunately, this NEVER happens (right?).
689	 *
690	 * Except when you get an error it seems...
691	 */
692			hwgroup->wrq = *rq; /* scratchpad */
693			ide_set_handler(drive, &multwrite_intr, WAIT_CMD, NULL);
694			if (ide_multwrite(drive, drive->mult_count)) {
695				unsigned long flags;
696				spin_lock_irqsave(&io_request_lock, flags);
697				hwgroup->handler = NULL;
698				del_timer(&hwgroup->timer);
699				spin_unlock_irqrestore(&io_request_lock, flags);
700				return ide_stopped;
701			}
702		} else {
703			unsigned long flags;
704			char *buffer = ide_map_buffer(rq, &flags);
705			ide_set_handler (drive, &write_intr, WAIT_CMD, NULL);
706			idedisk_output_data(drive, buffer, SECTOR_WORDS);
707			ide_unmap_buffer(buffer, &flags);
708		}
709		return ide_started;
710	}
711	printk(KERN_ERR "%s: bad command: %d\n", drive->name, rq->cmd);
712	ide_end_request(0, HWGROUP(drive));
713	return ide_stopped;
714}
715
716#endif /* __TASKFILE__IO */
717
718static int idedisk_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
719{
720	MOD_INC_USE_COUNT;
721	if (drive->removable && drive->usage == 1) {
722		struct hd_drive_task_hdr taskfile;
723		struct hd_drive_hob_hdr hobfile;
724		memset(&taskfile, 0, sizeof(struct hd_drive_task_hdr));
725		memset(&hobfile, 0, sizeof(struct hd_drive_hob_hdr));
726		taskfile.command = WIN_DOORLOCK;
727		check_disk_change(inode->i_rdev);
728		/*
729		 * Ignore the return code from door_lock,
730		 * since the open() has already succeeded,
731		 * and the door_lock is irrelevant at this point.
732		 */
733		if (drive->doorlocking && ide_wait_taskfile(drive, &taskfile, &hobfile, NULL))
734			drive->doorlocking = 0;
735	}
736	return 0;
737}
738
739static int do_idedisk_flushcache(ide_drive_t *drive);
740
741static void idedisk_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
742{
743	if (drive->removable && !drive->usage) {
744		struct hd_drive_task_hdr taskfile;
745		struct hd_drive_hob_hdr hobfile;
746		memset(&taskfile, 0, sizeof(struct hd_drive_task_hdr));
747		memset(&hobfile, 0, sizeof(struct hd_drive_hob_hdr));
748		taskfile.command = WIN_DOORUNLOCK;
749		invalidate_bdev(inode->i_bdev, 0);
750		if (drive->doorlocking && ide_wait_taskfile(drive, &taskfile, &hobfile, NULL))
751			drive->doorlocking = 0;
752	}
753	if ((drive->id->cfs_enable_2 & 0x3000) && drive->wcache)
754		if (do_idedisk_flushcache(drive))
755			printk (KERN_INFO "%s: Write Cache FAILED Flushing!\n",
756				drive->name);
757	MOD_DEC_USE_COUNT;
758}
759
760static int idedisk_media_change (ide_drive_t *drive)
761{
762	return drive->removable;	/* if removable, always assume it was changed */
763}
764
765static void idedisk_revalidate (ide_drive_t *drive)
766{
767	grok_partitions(HWIF(drive)->gd, drive->select.b.unit,
768			1<<PARTN_BITS,
769			current_capacity(drive));
770}
771
772/*
773 * Queries for true maximum capacity of the drive.
774 * Returns maximum LBA address (> 0) of the drive, 0 if failed.
775 */
776static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
777{
778	ide_task_t args;
779	unsigned long addr = 0;
780
781	if (!(drive->id->command_set_1 & 0x0400) &&
782	    !(drive->id->cfs_enable_2 & 0x0100))
783		return addr;
784
785	/* Create IDE/ATA command request structure */
786	memset(&args, 0, sizeof(ide_task_t));
787	args.tfRegister[IDE_SELECT_OFFSET]	= 0x40;
788	args.tfRegister[IDE_COMMAND_OFFSET]	= WIN_READ_NATIVE_MAX;
789	args.handler				= task_no_data_intr;
790
791	/* submit command request */
792	ide_raw_taskfile(drive, &args, NULL);
793
794	/* if OK, compute maximum address value */
795	if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
796		addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
797		     | ((args.tfRegister[  IDE_HCYL_OFFSET]       ) << 16)
798		     | ((args.tfRegister[  IDE_LCYL_OFFSET]       ) <<  8)
799		     | ((args.tfRegister[IDE_SECTOR_OFFSET]       ));
800	}
801	addr++;	/* since the return value is (maxlba - 1), we add 1 */
802	return addr;
803}
804
805static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
806{
807	ide_task_t args;
808	unsigned long long addr = 0;
809
810	/* Create IDE/ATA command request structure */
811	memset(&args, 0, sizeof(ide_task_t));
812
813	args.tfRegister[IDE_SELECT_OFFSET]	= 0x40;
814	args.tfRegister[IDE_COMMAND_OFFSET]	= WIN_READ_NATIVE_MAX_EXT;
815	args.handler				= task_no_data_intr;
816
817        /* submit command request */
818        ide_raw_taskfile(drive, &args, NULL);
819
820	/* if OK, compute maximum address value */
821	if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
822		u32 high = ((args.hobRegister[IDE_HCYL_OFFSET_HOB])<<16) |
823			   ((args.hobRegister[IDE_LCYL_OFFSET_HOB])<<8) |
824  			    (args.hobRegister[IDE_SECTOR_OFFSET_HOB]);
825		u32 low  = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
826			   ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
827			    (args.tfRegister[IDE_SECTOR_OFFSET]);
828		addr = ((__u64)high << 24) | low;
829	}
830	addr++;	/* since the return value is (maxlba - 1), we add 1 */
831	return addr;
832}
833
834#ifdef CONFIG_IDEDISK_STROKE
835/*
836 * Sets maximum virtual LBA address of the drive.
837 * Returns new maximum virtual LBA address (> 0) or 0 on failure.
838 */
839static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
840{
841	ide_task_t args;
842	unsigned long addr_set = 0;
843
844	addr_req--;
845	/* Create IDE/ATA command request structure */
846	memset(&args, 0, sizeof(ide_task_t));
847	args.tfRegister[IDE_SECTOR_OFFSET]	= ((addr_req >>  0) & 0xff);
848	args.tfRegister[IDE_LCYL_OFFSET]	= ((addr_req >>  8) & 0xff);
849	args.tfRegister[IDE_HCYL_OFFSET]	= ((addr_req >> 16) & 0xff);
850	args.tfRegister[IDE_SELECT_OFFSET]	= ((addr_req >> 24) & 0x0f) | 0x40;
851	args.tfRegister[IDE_COMMAND_OFFSET]	= WIN_SET_MAX;
852	args.handler				= task_no_data_intr;
853	/* submit command request */
854	ide_raw_taskfile(drive, &args, NULL);
855	/* if OK, read new maximum address value */
856	if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
857		addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
858			 | ((args.tfRegister[  IDE_HCYL_OFFSET]       ) << 16)
859			 | ((args.tfRegister[  IDE_LCYL_OFFSET]       ) <<  8)
860			 | ((args.tfRegister[IDE_SECTOR_OFFSET]       ));
861	}
862	addr_set++;
863	return addr_set;
864}
865
866static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
867{
868	ide_task_t args;
869	unsigned long long addr_set = 0;
870
871	addr_req--;
872	/* Create IDE/ATA command request structure */
873	memset(&args, 0, sizeof(ide_task_t));
874	args.tfRegister[IDE_SECTOR_OFFSET]	= ((addr_req >>  0) & 0xff);
875	args.tfRegister[IDE_LCYL_OFFSET]	= ((addr_req >>= 8) & 0xff);
876	args.tfRegister[IDE_HCYL_OFFSET]	= ((addr_req >>= 8) & 0xff);
877	args.tfRegister[IDE_SELECT_OFFSET]      = 0x40;
878	args.tfRegister[IDE_COMMAND_OFFSET]	= WIN_SET_MAX_EXT;
879	args.hobRegister[IDE_SECTOR_OFFSET_HOB]	= ((addr_req >>= 8) & 0xff);
880	args.hobRegister[IDE_LCYL_OFFSET_HOB]	= ((addr_req >>= 8) & 0xff);
881	args.hobRegister[IDE_HCYL_OFFSET_HOB]	= ((addr_req >>= 8) & 0xff);
882	args.hobRegister[IDE_SELECT_OFFSET_HOB]	= 0x40;
883	args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
884        args.handler				= task_no_data_intr;
885	/* submit command request */
886	ide_raw_taskfile(drive, &args, NULL);
887	/* if OK, compute maximum address value */
888	if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
889		u32 high = ((args.hobRegister[IDE_HCYL_OFFSET_HOB])<<16) |
890			   ((args.hobRegister[IDE_LCYL_OFFSET_HOB])<<8) |
891			    (args.hobRegister[IDE_SECTOR_OFFSET_HOB]);
892		u32 low  = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
893			   ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
894			    (args.tfRegister[IDE_SECTOR_OFFSET]);
895		addr_set = ((__u64)high << 24) | low;
896	}
897	return addr_set;
898}
899
900/*
901 * Tests if the drive supports Host Protected Area feature.
902 * Returns true if supported, false otherwise.
903 */
904static inline int idedisk_supports_host_protected_area(ide_drive_t *drive)
905{
906	int flag = (drive->id->cfs_enable_1 & 0x0400) ? 1 : 0;
907	printk("%s: host protected area => %d\n", drive->name, flag);
908	return flag;
909}
910
911#endif /* CONFIG_IDEDISK_STROKE */
912
913/*
914 * Compute drive->capacity, the full capacity of the drive
915 * Called with drive->id != NULL.
916 *
917 * To compute capacity, this uses either of
918 *
919 *    1. CHS value set by user       (whatever user sets will be trusted)
920 *    2. LBA value from target drive (require new ATA feature)
921 *    3. LBA value from system BIOS  (new one is OK, old one may break)
922 *    4. CHS value from system BIOS  (traditional style)
923 *
924 * in above order (i.e., if value of higher priority is available,
925 * reset will be ignored).
926 */
927static void init_idedisk_capacity (ide_drive_t  *drive)
928{
929	struct hd_driveid *id = drive->id;
930	unsigned long capacity = drive->cyl * drive->head * drive->sect;
931	unsigned long set_max = idedisk_read_native_max_address(drive);
932	unsigned long long capacity_2 = capacity;
933	unsigned long long set_max_ext;
934
935	drive->capacity48 = 0;
936	drive->select.b.lba = 0;
937
938	if (id->cfs_enable_2 & 0x0400) {
939		capacity_2 = id->lba_capacity_2;
940		drive->head		= drive->bios_head = 255;
941		drive->sect		= drive->bios_sect = 63;
942		drive->cyl = (unsigned int) capacity_2 / (drive->head * drive->sect);
943		drive->select.b.lba	= 1;
944		set_max_ext = idedisk_read_native_max_address_ext(drive);
945		if (set_max_ext > capacity_2) {
946#ifdef CONFIG_IDEDISK_STROKE
947			set_max_ext = idedisk_read_native_max_address_ext(drive);
948			set_max_ext = idedisk_set_max_address_ext(drive, set_max_ext);
949			if (set_max_ext) {
950				drive->capacity48 = capacity_2 = set_max_ext;
951				drive->cyl = (unsigned int) set_max_ext / (drive->head * drive->sect);
952				drive->select.b.lba = 1;
953				drive->id->lba_capacity_2 = capacity_2;
954                        }
955#else /* !CONFIG_IDEDISK_STROKE */
956			printk("%s: setmax_ext LBA %llu, native  %llu\n",
957				drive->name, set_max_ext, capacity_2);
958#endif /* CONFIG_IDEDISK_STROKE */
959		}
960		drive->bios_cyl		= drive->cyl;
961		drive->capacity48	= capacity_2;
962		drive->capacity		= (unsigned long) capacity_2;
963		return;
964	/* Determine capacity, and use LBA if the drive properly supports it */
965	} else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
966		capacity = id->lba_capacity;
967		drive->cyl = capacity / (drive->head * drive->sect);
968		drive->select.b.lba = 1;
969	}
970
971	if (set_max > capacity) {
972#ifdef CONFIG_IDEDISK_STROKE
973		set_max = idedisk_read_native_max_address(drive);
974		set_max = idedisk_set_max_address(drive, set_max);
975		if (set_max) {
976			drive->capacity = capacity = set_max;
977			drive->cyl = set_max / (drive->head * drive->sect);
978			drive->select.b.lba = 1;
979			drive->id->lba_capacity = capacity;
980		}
981#else /* !CONFIG_IDEDISK_STROKE */
982		printk("%s: setmax LBA %lu, native  %lu\n",
983			drive->name, set_max, capacity);
984#endif /* CONFIG_IDEDISK_STROKE */
985	}
986
987	drive->capacity = capacity;
988
989	if ((id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)) {
990                drive->capacity48 = id->lba_capacity_2;
991		drive->head = 255;
992		drive->sect = 63;
993		drive->cyl = (unsigned long)(drive->capacity48) / (drive->head * drive->sect);
994	}
995}
996
997static unsigned long idedisk_capacity (ide_drive_t *drive)
998{
999	if (drive->id->cfs_enable_2 & 0x0400)
1000		return (drive->capacity48 - drive->sect0);
1001	return (drive->capacity - drive->sect0);
1002}
1003
1004static ide_startstop_t idedisk_special (ide_drive_t *drive)
1005{
1006	special_t *s = &drive->special;
1007
1008	if (s->b.set_geometry) {
1009		struct hd_drive_task_hdr taskfile;
1010		struct hd_drive_hob_hdr hobfile;
1011		ide_handler_t *handler = NULL;
1012
1013		memset(&taskfile, 0, sizeof(struct hd_drive_task_hdr));
1014		memset(&hobfile, 0, sizeof(struct hd_drive_hob_hdr));
1015
1016		s->b.set_geometry	= 0;
1017		taskfile.sector_number	= drive->sect;
1018		taskfile.low_cylinder	= drive->cyl;
1019		taskfile.high_cylinder	= drive->cyl>>8;
1020		taskfile.device_head	= ((drive->head-1)|drive->select.all)&0xBF;
1021		if (!IS_PDC4030_DRIVE) {
1022			taskfile.sector_count	= drive->sect;
1023			taskfile.command	= WIN_SPECIFY;
1024			handler			= ide_handler_parser(&taskfile, &hobfile);
1025		}
1026		do_taskfile(drive, &taskfile, &hobfile, handler);
1027	} else if (s->b.recalibrate) {
1028		s->b.recalibrate = 0;
1029		if (!IS_PDC4030_DRIVE) {
1030			struct hd_drive_task_hdr taskfile;
1031			struct hd_drive_hob_hdr hobfile;
1032			memset(&taskfile, 0, sizeof(struct hd_drive_task_hdr));
1033			memset(&hobfile, 0, sizeof(struct hd_drive_hob_hdr));
1034			taskfile.sector_count	= drive->sect;
1035			taskfile.command	= WIN_RESTORE;
1036			do_taskfile(drive, &taskfile, &hobfile, ide_handler_parser(&taskfile, &hobfile));
1037		}
1038	} else if (s->b.set_multmode) {
1039		s->b.set_multmode = 0;
1040		if (drive->id && drive->mult_req > drive->id->max_multsect)
1041			drive->mult_req = drive->id->max_multsect;
1042		if (!IS_PDC4030_DRIVE) {
1043			struct hd_drive_task_hdr taskfile;
1044			struct hd_drive_hob_hdr hobfile;
1045			memset(&taskfile, 0, sizeof(struct hd_drive_task_hdr));
1046			memset(&hobfile, 0, sizeof(struct hd_drive_hob_hdr));
1047			taskfile.sector_count	= drive->mult_req;
1048			taskfile.command	= WIN_SETMULT;
1049			do_taskfile(drive, &taskfile, &hobfile, ide_handler_parser(&taskfile, &hobfile));
1050		}
1051	} else if (s->all) {
1052		int special = s->all;
1053		s->all = 0;
1054		printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
1055		return ide_stopped;
1056	}
1057	return IS_PDC4030_DRIVE ? ide_stopped : ide_started;
1058}
1059
1060static void idedisk_pre_reset (ide_drive_t *drive)
1061{
1062	int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
1063
1064	drive->special.all = 0;
1065	drive->special.b.set_geometry = legacy;
1066	drive->special.b.recalibrate  = legacy;
1067	if (OK_TO_RESET_CONTROLLER)
1068		drive->mult_count = 0;
1069	if (!drive->keep_settings && !drive->using_dma)
1070		drive->mult_req = 0;
1071	if (drive->mult_req != drive->mult_count)
1072		drive->special.b.set_multmode = 1;
1073}
1074
1075#ifdef CONFIG_PROC_FS
1076
1077static int smart_enable(ide_drive_t *drive)
1078{
1079	struct hd_drive_task_hdr taskfile;
1080	struct hd_drive_hob_hdr hobfile;
1081	memset(&taskfile, 0, sizeof(struct hd_drive_task_hdr));
1082	memset(&hobfile, 0, sizeof(struct hd_drive_hob_hdr));
1083	taskfile.feature	= SMART_ENABLE;
1084	taskfile.low_cylinder	= SMART_LCYL_PASS;
1085	taskfile.high_cylinder	= SMART_HCYL_PASS;
1086	taskfile.command	= WIN_SMART;
1087	return ide_wait_taskfile(drive, &taskfile, &hobfile, NULL);
1088}
1089
1090static int get_smart_values(ide_drive_t *drive, byte *buf)
1091{
1092	struct hd_drive_task_hdr taskfile;
1093	struct hd_drive_hob_hdr hobfile;
1094	memset(&taskfile, 0, sizeof(struct hd_drive_task_hdr));
1095	memset(&hobfile, 0, sizeof(struct hd_drive_hob_hdr));
1096	taskfile.feature	= SMART_READ_VALUES;
1097	taskfile.sector_count	= 0x01;
1098	taskfile.low_cylinder	= SMART_LCYL_PASS;
1099	taskfile.high_cylinder	= SMART_HCYL_PASS;
1100	taskfile.command	= WIN_SMART;
1101	(void) smart_enable(drive);
1102	return ide_wait_taskfile(drive, &taskfile, &hobfile, buf);
1103}
1104
1105static int get_smart_thresholds(ide_drive_t *drive, byte *buf)
1106{
1107	struct hd_drive_task_hdr taskfile;
1108	struct hd_drive_hob_hdr hobfile;
1109	memset(&taskfile, 0, sizeof(struct hd_drive_task_hdr));
1110	memset(&hobfile, 0, sizeof(struct hd_drive_hob_hdr));
1111	taskfile.feature	= SMART_READ_THRESHOLDS;
1112	taskfile.sector_count	= 0x01;
1113	taskfile.low_cylinder	= SMART_LCYL_PASS;
1114	taskfile.high_cylinder	= SMART_HCYL_PASS;
1115	taskfile.command	= WIN_SMART;
1116	(void) smart_enable(drive);
1117	return ide_wait_taskfile(drive, &taskfile, &hobfile, buf);
1118}
1119
1120static int proc_idedisk_read_cache
1121	(char *page, char **start, off_t off, int count, int *eof, void *data)
1122{
1123	ide_drive_t	*drive = (ide_drive_t *) data;
1124	char		*out = page;
1125	int		len;
1126
1127	if (drive->id)
1128		len = sprintf(out,"%i\n", drive->id->buf_size / 2);
1129	else
1130		len = sprintf(out,"(none)\n");
1131	PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1132}
1133
1134static int proc_idedisk_read_smart_thresholds
1135	(char *page, char **start, off_t off, int count, int *eof, void *data)
1136{
1137	ide_drive_t	*drive = (ide_drive_t *)data;
1138	int		len = 0, i = 0;
1139
1140	if (!get_smart_thresholds(drive, page)) {
1141		unsigned short *val = (unsigned short *) page;
1142		char *out = ((char *)val) + (SECTOR_WORDS * 4);
1143		page = out;
1144		do {
1145			out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1146			val += 1;
1147		} while (i < (SECTOR_WORDS * 2));
1148		len = out - page;
1149	}
1150	PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1151}
1152
1153static int proc_idedisk_read_smart_values
1154	(char *page, char **start, off_t off, int count, int *eof, void *data)
1155{
1156	ide_drive_t	*drive = (ide_drive_t *)data;
1157	int		len = 0, i = 0;
1158
1159	if (!get_smart_values(drive, page)) {
1160		unsigned short *val = (unsigned short *) page;
1161		char *out = ((char *)val) + (SECTOR_WORDS * 4);
1162		page = out;
1163		do {
1164			out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
1165			val += 1;
1166		} while (i < (SECTOR_WORDS * 2));
1167		len = out - page;
1168	}
1169	PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
1170}
1171
1172static ide_proc_entry_t idedisk_proc[] = {
1173	{ "cache",		S_IFREG|S_IRUGO,	proc_idedisk_read_cache,		NULL },
1174	{ "geometry",		S_IFREG|S_IRUGO,	proc_ide_read_geometry,			NULL },
1175	{ "smart_values",	S_IFREG|S_IRUSR,	proc_idedisk_read_smart_values,		NULL },
1176	{ "smart_thresholds",	S_IFREG|S_IRUSR,	proc_idedisk_read_smart_thresholds,	NULL },
1177	{ NULL, 0, NULL, NULL }
1178};
1179
1180#else
1181
1182#define	idedisk_proc	NULL
1183
1184#endif	/* CONFIG_PROC_FS */
1185
1186static int set_multcount(ide_drive_t *drive, int arg)
1187{
1188#ifdef __TASKFILE__IO
1189	struct hd_drive_task_hdr taskfile;
1190	struct hd_drive_hob_hdr hobfile;
1191
1192	if (drive->special.b.set_multmode)
1193		return -EBUSY;
1194
1195	memset(&taskfile, 0, sizeof(struct hd_drive_task_hdr));
1196	memset(&hobfile, 0, sizeof(struct hd_drive_hob_hdr));
1197	taskfile.sector_count	= drive->mult_req;
1198	taskfile.command	= WIN_SETMULT;
1199	drive->mult_req		= arg;
1200	drive->special.b.set_multmode = 1;
1201	ide_wait_taskfile(drive, &taskfile, &hobfile, NULL);
1202#else /* !__TASKFILE__IO */
1203	struct request rq;
1204
1205	if (drive->special.b.set_multmode)
1206		return -EBUSY;
1207	ide_init_drive_cmd (&rq);
1208	rq.cmd = IDE_DRIVE_CMD;
1209	drive->mult_req = arg;
1210	drive->special.b.set_multmode = 1;
1211	(void) ide_do_drive_cmd (drive, &rq, ide_wait);
1212#endif /* __TASKFILE__IO */
1213	return (drive->mult_count == arg) ? 0 : -EIO;
1214}
1215
1216static int set_nowerr(ide_drive_t *drive, int arg)
1217{
1218	if (ide_spin_wait_hwgroup(drive))
1219		return -EBUSY;
1220	drive->nowerr = arg;
1221	drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
1222	spin_unlock_irq(&io_request_lock);
1223	return 0;
1224}
1225
1226static int write_cache (ide_drive_t *drive, int arg)
1227{
1228	struct hd_drive_task_hdr taskfile;
1229	struct hd_drive_hob_hdr hobfile;
1230	memset(&taskfile, 0, sizeof(struct hd_drive_task_hdr));
1231	memset(&hobfile, 0, sizeof(struct hd_drive_hob_hdr));
1232	taskfile.feature	= (arg) ? SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
1233	taskfile.command	= WIN_SETFEATURES;
1234
1235	if (!(drive->id->cfs_enable_2 & 0x3000))
1236		return 1;
1237
1238	(void) ide_wait_taskfile(drive, &taskfile, &hobfile, NULL);
1239	drive->wcache = arg;
1240	return 0;
1241}
1242
1243static int do_idedisk_standby (ide_drive_t *drive)
1244{
1245	struct hd_drive_task_hdr taskfile;
1246	struct hd_drive_hob_hdr hobfile;
1247	memset(&taskfile, 0, sizeof(struct hd_drive_task_hdr));
1248	memset(&hobfile, 0, sizeof(struct hd_drive_hob_hdr));
1249	taskfile.command	= WIN_STANDBYNOW1;
1250	return ide_wait_taskfile(drive, &taskfile, &hobfile, NULL);
1251}
1252
1253static int do_idedisk_flushcache (ide_drive_t *drive)
1254{
1255	struct hd_drive_task_hdr taskfile;
1256	struct hd_drive_hob_hdr hobfile;
1257	memset(&taskfile, 0, sizeof(struct hd_drive_task_hdr));
1258	memset(&hobfile, 0, sizeof(struct hd_drive_hob_hdr));
1259	if (drive->id->cfs_enable_2 & 0x2400) {
1260		taskfile.command	= WIN_FLUSH_CACHE_EXT;
1261	} else {
1262		taskfile.command	= WIN_FLUSH_CACHE;
1263	}
1264	return ide_wait_taskfile(drive, &taskfile, &hobfile, NULL);
1265}
1266
1267static int set_acoustic (ide_drive_t *drive, int arg)
1268{
1269	struct hd_drive_task_hdr taskfile;
1270	struct hd_drive_hob_hdr hobfile;
1271	memset(&taskfile, 0, sizeof(struct hd_drive_task_hdr));
1272	memset(&hobfile, 0, sizeof(struct hd_drive_hob_hdr));
1273
1274	taskfile.feature	= (arg)?SETFEATURES_EN_AAM:SETFEATURES_DIS_AAM;
1275	taskfile.sector_count	= arg;
1276
1277	taskfile.command	= WIN_SETFEATURES;
1278	(void) ide_wait_taskfile(drive, &taskfile, &hobfile, NULL);
1279	drive->acoustic = arg;
1280	return 0;
1281}
1282
1283static int probe_lba_addressing (ide_drive_t *drive, int arg)
1284{
1285	drive->addressing =  0;
1286
1287	if (!(drive->id->cfs_enable_2 & 0x0400))
1288                return -EIO;
1289
1290	drive->addressing = arg;
1291	return 0;
1292}
1293
1294static int set_lba_addressing (ide_drive_t *drive, int arg)
1295{
1296	return (probe_lba_addressing(drive, arg));
1297}
1298
1299static void idedisk_add_settings(ide_drive_t *drive)
1300{
1301	struct hd_driveid *id = drive->id;
1302	int major = HWIF(drive)->major;
1303	int minor = drive->select.b.unit << PARTN_BITS;
1304
1305	ide_add_setting(drive,	"bios_cyl",		SETTING_RW,					-1,			-1,			TYPE_INT,	0,	65535,				1,	1,	&drive->bios_cyl,		NULL);
1306	ide_add_setting(drive,	"bios_head",		SETTING_RW,					-1,			-1,			TYPE_BYTE,	0,	255,				1,	1,	&drive->bios_head,		NULL);
1307	ide_add_setting(drive,	"bios_sect",		SETTING_RW,					-1,			-1,			TYPE_BYTE,	0,	63,				1,	1,	&drive->bios_sect,		NULL);
1308	ide_add_setting(drive,	"address",		SETTING_RW,					HDIO_GET_ADDRESS,	HDIO_SET_ADDRESS,	TYPE_INTA,	0,	2,				1,	1,	&drive->addressing,	set_lba_addressing);
1309	ide_add_setting(drive,	"bswap",		SETTING_READ,					-1,			-1,			TYPE_BYTE,	0,	1,				1,	1,	&drive->bswap,			NULL);
1310	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);
1311	ide_add_setting(drive,	"nowerr",		SETTING_RW,					HDIO_GET_NOWERR,	HDIO_SET_NOWERR,	TYPE_BYTE,	0,	1,				1,	1,	&drive->nowerr,			set_nowerr);
1312	ide_add_setting(drive,	"breada_readahead",	SETTING_RW,					BLKRAGET,		BLKRASET,		TYPE_INT,	0,	255,				1,	1,	&read_ahead[major],		NULL);
1313	ide_add_setting(drive,	"file_readahead",	SETTING_RW,					BLKFRAGET,		BLKFRASET,		TYPE_INTA,	0,	4096,			PAGE_SIZE,	1024,	&max_readahead[major][minor],	NULL);
1314	ide_add_setting(drive,	"max_kb_per_request",	SETTING_RW,					BLKSECTGET,		BLKSECTSET,		TYPE_INTA,	1,	255,				1,	1,	&max_sectors[major][minor],	NULL);
1315	ide_add_setting(drive,	"lun",			SETTING_RW,					-1,			-1,			TYPE_INT,	0,	7,				1,	1,	&drive->lun,			NULL);
1316	ide_add_setting(drive,	"wcache",		SETTING_RW,					HDIO_GET_WCACHE,	HDIO_SET_WCACHE,	TYPE_BYTE,	0,	1,				1,	1,	&drive->wcache,			write_cache);
1317	ide_add_setting(drive,	"acoustic",		SETTING_RW,					HDIO_GET_ACOUSTIC,	HDIO_SET_ACOUSTIC,	TYPE_BYTE,	0,	254,				1,	1,	&drive->acoustic,		set_acoustic);
1318 	ide_add_setting(drive,	"failures",		SETTING_RW,					-1,			-1,			TYPE_INT,	0,	65535,				1,	1,	&drive->failures,		NULL);
1319 	ide_add_setting(drive,	"max_failures",		SETTING_RW,					-1,			-1,			TYPE_INT,	0,	65535,				1,	1,	&drive->max_failures,		NULL);
1320}
1321
1322static void idedisk_setup (ide_drive_t *drive)
1323{
1324	int i;
1325
1326	struct hd_driveid *id = drive->id;
1327	unsigned long capacity;
1328
1329	idedisk_add_settings(drive);
1330
1331	if (id == NULL)
1332		return;
1333
1334	/*
1335	 * CompactFlash cards and their brethern look just like hard drives
1336	 * to us, but they are removable and don't have a doorlock mechanism.
1337	 */
1338	if (drive->removable && !drive_is_flashcard(drive)) {
1339		/*
1340		 * Removable disks (eg. SYQUEST); ignore 'WD' drives
1341		 */
1342		if (id->model[0] != 'W' || id->model[1] != 'D') {
1343			drive->doorlocking = 1;
1344		}
1345	}
1346	for (i = 0; i < MAX_DRIVES; ++i) {
1347		ide_hwif_t *hwif = HWIF(drive);
1348
1349		if (drive != &hwif->drives[i]) continue;
1350		hwif->gd->de_arr[i] = drive->de;
1351		if (drive->removable)
1352			hwif->gd->flags[i] |= GENHD_FL_REMOVABLE;
1353		break;
1354	}
1355
1356	/* Extract geometry if we did not already have one for the drive */
1357	if (!drive->cyl || !drive->head || !drive->sect) {
1358		drive->cyl     = drive->bios_cyl  = id->cyls;
1359		drive->head    = drive->bios_head = id->heads;
1360		drive->sect    = drive->bios_sect = id->sectors;
1361	}
1362
1363	/* Handle logical geometry translation by the drive */
1364	if ((id->field_valid & 1) && id->cur_cyls &&
1365	    id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
1366		drive->cyl  = id->cur_cyls;
1367		drive->head = id->cur_heads;
1368		drive->sect = id->cur_sectors;
1369	}
1370
1371	/* Use physical geometry if what we have still makes no sense */
1372	if (drive->head > 16 && id->heads && id->heads <= 16) {
1373		drive->cyl  = id->cyls;
1374		drive->head = id->heads;
1375		drive->sect = id->sectors;
1376	}
1377
1378	/* calculate drive capacity, and select LBA if possible */
1379	init_idedisk_capacity (drive);
1380
1381	/*
1382	 * if possible, give fdisk access to more of the drive,
1383	 * by correcting bios_cyls:
1384	 */
1385	capacity = idedisk_capacity (drive);
1386	if ((capacity >= (drive->bios_cyl * drive->bios_sect * drive->bios_head)) &&
1387	    (!drive->forced_geom) && drive->bios_sect && drive->bios_head)
1388		drive->bios_cyl = (capacity / drive->bios_sect) / drive->bios_head;
1389	printk (KERN_INFO "%s: %ld sectors", drive->name, capacity);
1390
1391	/* Give size in megabytes (MB), not mebibytes (MiB). */
1392	/* We compute the exact rounded value, avoiding overflow. */
1393	printk (" (%ld MB)", (capacity - capacity/625 + 974)/1950);
1394
1395	/* Only print cache size when it was specified */
1396	if (id->buf_size)
1397		printk (" w/%dKiB Cache", id->buf_size/2);
1398
1399	printk(", CHS=%d/%d/%d",
1400	       drive->bios_cyl, drive->bios_head, drive->bios_sect);
1401#ifdef CONFIG_BLK_DEV_IDEDMA
1402	if (drive->using_dma)
1403		(void) HWIF(drive)->dmaproc(ide_dma_verbose, drive);
1404#endif /* CONFIG_BLK_DEV_IDEDMA */
1405	printk("\n");
1406
1407	drive->mult_count = 0;
1408	if (id->max_multsect) {
1409#ifdef CONFIG_IDEDISK_MULTI_MODE
1410		id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
1411		id->multsect_valid = id->multsect ? 1 : 0;
1412		drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
1413		drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
1414#else	/* original, pre IDE-NFG, per request of AC */
1415		drive->mult_req = INITIAL_MULT_COUNT;
1416		if (drive->mult_req > id->max_multsect)
1417			drive->mult_req = id->max_multsect;
1418		if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
1419			drive->special.b.set_multmode = 1;
1420#endif	/* CONFIG_IDEDISK_MULTI_MODE */
1421	}
1422	drive->no_io_32bit = id->dword_io ? 1 : 0;
1423	if (drive->id->cfs_enable_2 & 0x3000)
1424		write_cache(drive, (id->cfs_enable_2 & 0x3000));
1425	(void) probe_lba_addressing(drive, 1);
1426}
1427
1428static int idedisk_cleanup (ide_drive_t *drive)
1429{
1430	if ((drive->id->cfs_enable_2 & 0x3000) && drive->wcache)
1431		if (do_idedisk_flushcache(drive))
1432			printk (KERN_INFO "%s: Write Cache FAILED Flushing!\n",
1433				drive->name);
1434	return ide_unregister_subdriver(drive);
1435}
1436
1437int idedisk_reinit(ide_drive_t *drive);
1438
1439/*
1440 *      IDE subdriver functions, registered with ide.c
1441 */
1442static ide_driver_t idedisk_driver = {
1443	name:			"ide-disk",
1444	version:		IDEDISK_VERSION,
1445	media:			ide_disk,
1446	busy:			0,
1447	supports_dma:		1,
1448	supports_dsc_overlap:	0,
1449	cleanup:		idedisk_cleanup,
1450	standby:		do_idedisk_standby,
1451	flushcache:		do_idedisk_flushcache,
1452	do_request:		do_rw_disk,
1453	end_request:		NULL,
1454	ioctl:			NULL,
1455	open:			idedisk_open,
1456	release:		idedisk_release,
1457	media_change:		idedisk_media_change,
1458	revalidate:		idedisk_revalidate,
1459	pre_reset:		idedisk_pre_reset,
1460	capacity:		idedisk_capacity,
1461	special:		idedisk_special,
1462	proc:			idedisk_proc,
1463	reinit:			idedisk_reinit,
1464	ata_prebuilder:		NULL,
1465	atapi_prebuilder:	NULL,
1466};
1467
1468int idedisk_init (void);
1469static ide_module_t idedisk_module = {
1470	IDE_DRIVER_MODULE,
1471	idedisk_init,
1472	&idedisk_driver,
1473	NULL
1474};
1475
1476MODULE_DESCRIPTION("ATA DISK Driver");
1477
1478int idedisk_reinit (ide_drive_t *drive)
1479{
1480	int failed = 0;
1481
1482	MOD_INC_USE_COUNT;
1483
1484	if (ide_register_subdriver (drive, &idedisk_driver, IDE_SUBDRIVER_VERSION)) {
1485		printk (KERN_ERR "ide-disk: %s: Failed to register the driver with ide.c\n", drive->name);
1486		return 1;
1487	}
1488	DRIVER(drive)->busy++;
1489	idedisk_setup(drive);
1490	if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1491		printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n", drive->name, drive->head);
1492		(void) idedisk_cleanup(drive);
1493		DRIVER(drive)->busy--;
1494		return 1;
1495	}
1496	DRIVER(drive)->busy--;
1497	failed--;
1498
1499	ide_register_module(&idedisk_module);
1500	MOD_DEC_USE_COUNT;
1501	return 0;
1502}
1503
1504static void __exit idedisk_exit (void)
1505{
1506	ide_drive_t *drive;
1507	int failed = 0;
1508
1509	while ((drive = ide_scan_devices (ide_disk, idedisk_driver.name, &idedisk_driver, failed)) != NULL) {
1510		if (idedisk_cleanup (drive)) {
1511			printk (KERN_ERR "%s: cleanup_module() called while still busy\n", drive->name);
1512			failed++;
1513		}
1514		/* We must remove proc entries defined in this module.
1515		   Otherwise we oops while accessing these entries */
1516#ifdef CONFIG_PROC_FS
1517		if (drive->proc)
1518			ide_remove_proc_entries(drive->proc, idedisk_proc);
1519#endif
1520	}
1521	ide_unregister_module(&idedisk_module);
1522}
1523
1524int idedisk_init (void)
1525{
1526	ide_drive_t *drive;
1527	int failed = 0;
1528
1529	MOD_INC_USE_COUNT;
1530	while ((drive = ide_scan_devices (ide_disk, idedisk_driver.name, NULL, failed++)) != NULL) {
1531		if (ide_register_subdriver (drive, &idedisk_driver, IDE_SUBDRIVER_VERSION)) {
1532			printk (KERN_ERR "ide-disk: %s: Failed to register the driver with ide.c\n", drive->name);
1533			continue;
1534		}
1535		DRIVER(drive)->busy++;
1536		idedisk_setup(drive);
1537		if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1538			printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n", drive->name, drive->head);
1539			(void) idedisk_cleanup(drive);
1540			DRIVER(drive)->busy--;
1541			continue;
1542		}
1543		DRIVER(drive)->busy--;
1544		failed--;
1545	}
1546	ide_register_module(&idedisk_module);
1547	MOD_DEC_USE_COUNT;
1548	return 0;
1549}
1550
1551module_init(idedisk_init);
1552module_exit(idedisk_exit);
1553MODULE_LICENSE("GPL");
1554