File: /usr/src/linux/drivers/ide/ide-disk.c

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      */
31     
32     #define IDEDISK_VERSION	"1.10"
33     
34     #undef REALLY_SLOW_IO		/* most systems can safely undef this */
35     
36     #include <linux/config.h>
37     #include <linux/module.h>
38     #include <linux/types.h>
39     #include <linux/string.h>
40     #include <linux/kernel.h>
41     #include <linux/timer.h>
42     #include <linux/mm.h>
43     #include <linux/interrupt.h>
44     #include <linux/major.h>
45     #include <linux/errno.h>
46     #include <linux/genhd.h>
47     #include <linux/slab.h>
48     #include <linux/delay.h>
49     #include <linux/ide.h>
50     
51     #include <asm/byteorder.h>
52     #include <asm/irq.h>
53     #include <asm/uaccess.h>
54     #include <asm/io.h>
55     
56     #ifdef CONFIG_BLK_DEV_PDC4030
57     #define IS_PDC4030_DRIVE (HWIF(drive)->chipset == ide_pdc4030)
58     #else
59     #define IS_PDC4030_DRIVE (0)	/* auto-NULLs out pdc4030 code */
60     #endif
61     
62     static void idedisk_bswap_data (void *buffer, int wcount)
63     {
64     	u16 *p = buffer;
65     
66     	while (wcount--) {
67     		*p = *p << 8 | *p >> 8; p++;
68     		*p = *p << 8 | *p >> 8; p++;
69     	}
70     }
71     
72     static inline void idedisk_input_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
73     {
74     	ide_input_data(drive, buffer, wcount);
75     	if (drive->bswap)
76     		idedisk_bswap_data(buffer, wcount);
77     }
78     
79     static inline void idedisk_output_data (ide_drive_t *drive, void *buffer, unsigned int wcount)
80     {
81     	if (drive->bswap) {
82     		idedisk_bswap_data(buffer, wcount);
83     		ide_output_data(drive, buffer, wcount);
84     		idedisk_bswap_data(buffer, wcount);
85     	} else
86     		ide_output_data(drive, buffer, wcount);
87     }
88     
89     /*
90      * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
91      * value for this drive (from its reported identification information).
92      *
93      * Returns:	1 if lba_capacity looks sensible
94      *		0 otherwise
95      *
96      * It is called only once for each drive.
97      */
98     static int lba_capacity_is_ok (struct hd_driveid *id)
99     {
100     	unsigned long lba_sects, chs_sects, head, tail;
101     
102     	/*
103     	 * The ATA spec tells large drives to return
104     	 * C/H/S = 16383/16/63 independent of their size.
105     	 * Some drives can be jumpered to use 15 heads instead of 16.
106     	 * Some drives can be jumpered to use 4092 cyls instead of 16383.
107     	 */
108     	if ((id->cyls == 16383
109     	     || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
110     	    id->sectors == 63 &&
111     	    (id->heads == 15 || id->heads == 16) &&
112     	    id->lba_capacity >= 16383*63*id->heads)
113     		return 1;
114     
115     	lba_sects   = id->lba_capacity;
116     	chs_sects   = id->cyls * id->heads * id->sectors;
117     
118     	/* perform a rough sanity check on lba_sects:  within 10% is OK */
119     	if ((lba_sects - chs_sects) < chs_sects/10)
120     		return 1;
121     
122     	/* some drives have the word order reversed */
123     	head = ((lba_sects >> 16) & 0xffff);
124     	tail = (lba_sects & 0xffff);
125     	lba_sects = (head | (tail << 16));
126     	if ((lba_sects - chs_sects) < chs_sects/10) {
127     		id->lba_capacity = lba_sects;
128     		return 1;	/* lba_capacity is (now) good */
129     	}
130     
131     	return 0;	/* lba_capacity value may be bad */
132     }
133     
134     /*
135      * read_intr() is the handler for disk read/multread interrupts
136      */
137     static ide_startstop_t read_intr (ide_drive_t *drive)
138     {
139     	byte stat;
140     	int i;
141     	unsigned int msect, nsect;
142     	struct request *rq;
143     
144     	/* new way for dealing with premature shared PCI interrupts */
145     	if (!OK_STAT(stat=GET_STAT(),DATA_READY,BAD_R_STAT)) {
146     		if (stat & (ERR_STAT|DRQ_STAT)) {
147     			return ide_error(drive, "read_intr", stat);
148     		}
149     		/* no data yet, so wait for another interrupt */
150     		ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
151     		return ide_started;
152     	}
153     	msect = drive->mult_count;
154     	
155     read_next:
156     	rq = HWGROUP(drive)->rq;
157     	if (msect) {
158     		if ((nsect = rq->current_nr_sectors) > msect)
159     			nsect = msect;
160     		msect -= nsect;
161     	} else
162     		nsect = 1;
163     	idedisk_input_data(drive, rq->buffer, nsect * SECTOR_WORDS);
164     #ifdef DEBUG
165     	printk("%s:  read: sectors(%ld-%ld), buffer=0x%08lx, remaining=%ld\n",
166     		drive->name, rq->sector, rq->sector+nsect-1,
167     		(unsigned long) rq->buffer+(nsect<<9), rq->nr_sectors-nsect);
168     #endif
169     	rq->sector += nsect;
170     	rq->buffer += nsect<<9;
171     	rq->errors = 0;
172     	i = (rq->nr_sectors -= nsect);
173     	if (((long)(rq->current_nr_sectors -= nsect)) <= 0)
174     		ide_end_request(1, HWGROUP(drive));
175     	if (i > 0) {
176     		if (msect)
177     			goto read_next;
178     		ide_set_handler (drive, &read_intr, WAIT_CMD, NULL);
179                     return ide_started;
180     	}
181             return ide_stopped;
182     }
183     
184     /*
185      * write_intr() is the handler for disk write interrupts
186      */
187     static ide_startstop_t write_intr (ide_drive_t *drive)
188     {
189     	byte stat;
190     	int i;
191     	ide_hwgroup_t *hwgroup = HWGROUP(drive);
192     	struct request *rq = hwgroup->rq;
193     
194     	if (!OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
195     		printk("%s: write_intr error1: nr_sectors=%ld, stat=0x%02x\n", drive->name, rq->nr_sectors, stat);
196             } else {
197     #ifdef DEBUG
198     		printk("%s: write: sector %ld, buffer=0x%08lx, remaining=%ld\n",
199     			drive->name, rq->sector, (unsigned long) rq->buffer,
200     			rq->nr_sectors-1);
201     #endif
202     		if ((rq->nr_sectors == 1) ^ ((stat & DRQ_STAT) != 0)) {
203     			rq->sector++;
204     			rq->buffer += 512;
205     			rq->errors = 0;
206     			i = --rq->nr_sectors;
207     			--rq->current_nr_sectors;
208     			if (((long)rq->current_nr_sectors) <= 0)
209     				ide_end_request(1, hwgroup);
210     			if (i > 0) {
211     				idedisk_output_data (drive, rq->buffer, SECTOR_WORDS);
212     				ide_set_handler (drive, &write_intr, WAIT_CMD, NULL);
213                                     return ide_started;
214     			}
215                             return ide_stopped;
216     		}
217     		return ide_stopped;	/* the original code did this here (?) */
218     	}
219     	return ide_error(drive, "write_intr", stat);
220     }
221     
222     /*
223      * ide_multwrite() transfers a block of up to mcount sectors of data
224      * to a drive as part of a disk multiple-sector write operation.
225      *
226      * Returns 0 on success.
227      *
228      * Note that we may be called from two contexts - the do_rw_disk context
229      * and IRQ context. The IRQ can happen any time after we've output the
230      * full "mcount" number of sectors, so we must make sure we update the
231      * state _before_ we output the final part of the data!
232      */
233     int ide_multwrite (ide_drive_t *drive, unsigned int mcount)
234     {
235      	ide_hwgroup_t	*hwgroup= HWGROUP(drive);
236      	struct request	*rq = &hwgroup->wrq;
237      
238       	do {
239       		char *buffer;
240       		int nsect = rq->current_nr_sectors;
241      
242     		if (nsect > mcount)
243     			nsect = mcount;
244     		mcount -= nsect;
245     		buffer = rq->buffer;
246     
247     		rq->sector += nsect;
248     		rq->buffer += nsect << 9;
249     		rq->nr_sectors -= nsect;
250     		rq->current_nr_sectors -= nsect;
251     
252     		/* Do we move to the next bh after this? */
253     		if (!rq->current_nr_sectors) {
254     			struct buffer_head *bh = rq->bh->b_reqnext;
255     
256     			/* end early early we ran out of requests */
257     			if (!bh) {
258     				mcount = 0;
259     			} else {
260     				rq->bh = bh;
261     				rq->current_nr_sectors = bh->b_size >> 9;
262     				rq->buffer             = bh->b_data;
263     			}
264     		}
265     
266     		/*
267     		 * Ok, we're all setup for the interrupt
268     		 * re-entering us on the last transfer.
269     		 */
270     		idedisk_output_data(drive, buffer, nsect<<7);
271     	} while (mcount);
272     
273             return 0;
274     }
275     
276     /*
277      * multwrite_intr() is the handler for disk multwrite interrupts
278      */
279     static ide_startstop_t multwrite_intr (ide_drive_t *drive)
280     {
281     	byte stat;
282     	int i;
283     	ide_hwgroup_t *hwgroup = HWGROUP(drive);
284     	struct request *rq = &hwgroup->wrq;
285     
286     	if (OK_STAT(stat=GET_STAT(),DRIVE_READY,drive->bad_wstat)) {
287     		if (stat & DRQ_STAT) {
288     			/*
289     			 *	The drive wants data. Remember rq is the copy
290     			 *	of the request
291     			 */
292     			if (rq->nr_sectors) {
293     				if (ide_multwrite(drive, drive->mult_count))
294     					return ide_stopped;
295     				ide_set_handler (drive, &multwrite_intr, WAIT_CMD, NULL);
296     				return ide_started;
297     			}
298     		} else {
299     			/*
300     			 *	If the copy has all the blocks completed then
301     			 *	we can end the original request.
302     			 */
303     			if (!rq->nr_sectors) {	/* all done? */
304     				rq = hwgroup->rq;
305     				for (i = rq->nr_sectors; i > 0;){
306     					i -= rq->current_nr_sectors;
307     					ide_end_request(1, hwgroup);
308     				}
309     				return ide_stopped;
310     			}
311     		}
312     		return ide_stopped;	/* the original code did this here (?) */
313     	}
314     	return ide_error(drive, "multwrite_intr", stat);
315     }
316     
317     /*
318      * set_multmode_intr() is invoked on completion of a WIN_SETMULT cmd.
319      */
320     static ide_startstop_t set_multmode_intr (ide_drive_t *drive)
321     {
322     	byte stat;
323     
324     	if (OK_STAT(stat=GET_STAT(),READY_STAT,BAD_STAT)) {
325     		drive->mult_count = drive->mult_req;
326     	} else {
327     		drive->mult_req = drive->mult_count = 0;
328     		drive->special.b.recalibrate = 1;
329     		(void) ide_dump_status(drive, "set_multmode", stat);
330     	}
331     	return ide_stopped;
332     }
333     
334     /*
335      * set_geometry_intr() is invoked on completion of a WIN_SPECIFY cmd.
336      */
337     static ide_startstop_t set_geometry_intr (ide_drive_t *drive)
338     {
339     	byte stat;
340     
341     	if (OK_STAT(stat=GET_STAT(),READY_STAT,BAD_STAT))
342     		return ide_stopped;
343     
344     	if (stat & (ERR_STAT|DRQ_STAT))
345     		return ide_error(drive, "set_geometry_intr", stat);
346     
347     	ide_set_handler(drive, &set_geometry_intr, WAIT_CMD, NULL);
348     	return ide_started;	
349     }
350     
351     /*
352      * recal_intr() is invoked on completion of a WIN_RESTORE (recalibrate) cmd.
353      */
354     static ide_startstop_t recal_intr (ide_drive_t *drive)
355     {
356     	byte stat = GET_STAT();
357     
358     	if (!OK_STAT(stat,READY_STAT,BAD_STAT))
359     		return ide_error(drive, "recal_intr", stat);
360     	return ide_stopped;
361     }
362     
363     /*
364      * do_rw_disk() issues READ and WRITE commands to a disk,
365      * using LBA if supported, or CHS otherwise, to address sectors.
366      * It also takes care of issuing special DRIVE_CMDs.
367      */
368     static ide_startstop_t do_rw_disk (ide_drive_t *drive, struct request *rq, unsigned long block)
369     {
370     	if (IDE_CONTROL_REG)
371     		OUT_BYTE(drive->ctl,IDE_CONTROL_REG);
372     	OUT_BYTE(0x00, IDE_FEATURE_REG);
373     	OUT_BYTE(rq->nr_sectors,IDE_NSECTOR_REG);
374     #ifdef CONFIG_BLK_DEV_PDC4030
375     	if (drive->select.b.lba || IS_PDC4030_DRIVE) {
376     #else /* !CONFIG_BLK_DEV_PDC4030 */
377     	if (drive->select.b.lba) {
378     #endif /* CONFIG_BLK_DEV_PDC4030 */
379     #ifdef DEBUG
380     		printk("%s: %sing: LBAsect=%ld, sectors=%ld, buffer=0x%08lx\n",
381     			drive->name, (rq->cmd==READ)?"read":"writ",
382     			block, rq->nr_sectors, (unsigned long) rq->buffer);
383     #endif
384     		OUT_BYTE(block,IDE_SECTOR_REG);
385     		OUT_BYTE(block>>=8,IDE_LCYL_REG);
386     		OUT_BYTE(block>>=8,IDE_HCYL_REG);
387     		OUT_BYTE(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
388     	} else {
389     		unsigned int sect,head,cyl,track;
390     		track = block / drive->sect;
391     		sect  = block % drive->sect + 1;
392     		OUT_BYTE(sect,IDE_SECTOR_REG);
393     		head  = track % drive->head;
394     		cyl   = track / drive->head;
395     		OUT_BYTE(cyl,IDE_LCYL_REG);
396     		OUT_BYTE(cyl>>8,IDE_HCYL_REG);
397     		OUT_BYTE(head|drive->select.all,IDE_SELECT_REG);
398     #ifdef DEBUG
399     		printk("%s: %sing: CHS=%d/%d/%d, sectors=%ld, buffer=0x%08lx\n",
400     			drive->name, (rq->cmd==READ)?"read":"writ", cyl,
401     			head, sect, rq->nr_sectors, (unsigned long) rq->buffer);
402     #endif
403     	}
404     #ifdef CONFIG_BLK_DEV_PDC4030
405     	if (IS_PDC4030_DRIVE) {
406     		extern ide_startstop_t do_pdc4030_io(ide_drive_t *, struct request *);
407     		return do_pdc4030_io (drive, rq);
408     	}
409     #endif /* CONFIG_BLK_DEV_PDC4030 */
410     	if (rq->cmd == READ) {
411     #ifdef CONFIG_BLK_DEV_IDEDMA
412     		if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_read, drive)))
413     			return ide_started;
414     #endif /* CONFIG_BLK_DEV_IDEDMA */
415     		ide_set_handler(drive, &read_intr, WAIT_CMD, NULL);
416     		OUT_BYTE(drive->mult_count ? WIN_MULTREAD : WIN_READ, IDE_COMMAND_REG);
417     		return ide_started;
418     	}
419     	if (rq->cmd == WRITE) {
420     		ide_startstop_t startstop;
421     #ifdef CONFIG_BLK_DEV_IDEDMA
422     		if (drive->using_dma && !(HWIF(drive)->dmaproc(ide_dma_write, drive)))
423     			return ide_started;
424     #endif /* CONFIG_BLK_DEV_IDEDMA */
425     		OUT_BYTE(drive->mult_count ? WIN_MULTWRITE : WIN_WRITE, IDE_COMMAND_REG);
426     		if (ide_wait_stat(&startstop, drive, DATA_READY, drive->bad_wstat, WAIT_DRQ)) {
427     			printk(KERN_ERR "%s: no DRQ after issuing %s\n", drive->name,
428     				drive->mult_count ? "MULTWRITE" : "WRITE");
429     			return startstop;
430     		}
431     		if (!drive->unmask)
432     			__cli();	/* local CPU only */
433     		if (drive->mult_count) {
434     			ide_hwgroup_t *hwgroup = HWGROUP(drive);
435     			/*
436     			 * Ugh.. this part looks ugly because we MUST set up
437     			 * the interrupt handler before outputting the first block
438     			 * of data to be written.  If we hit an error (corrupted buffer list)
439     			 * in ide_multwrite(), then we need to remove the handler/timer
440     			 * before returning.  Fortunately, this NEVER happens (right?).
441     			 *
442     			 * Except when you get an error it seems...
443     			 */
444     			hwgroup->wrq = *rq; /* scratchpad */
445     			ide_set_handler (drive, &multwrite_intr, WAIT_CMD, NULL);
446     			if (ide_multwrite(drive, drive->mult_count)) {
447     				unsigned long flags;
448     				spin_lock_irqsave(&io_request_lock, flags);
449     				hwgroup->handler = NULL;
450     				del_timer(&hwgroup->timer);
451     				spin_unlock_irqrestore(&io_request_lock, flags);
452     				return ide_stopped;
453     			}
454     		} else {
455     			ide_set_handler (drive, &write_intr, WAIT_CMD, NULL);
456     			idedisk_output_data(drive, rq->buffer, SECTOR_WORDS);
457     		}
458     		return ide_started;
459     	}
460     	printk(KERN_ERR "%s: bad command: %d\n", drive->name, rq->cmd);
461     	ide_end_request(0, HWGROUP(drive));
462     	return ide_stopped;
463     }
464     
465     static int idedisk_open (struct inode *inode, struct file *filp, ide_drive_t *drive)
466     {
467     	MOD_INC_USE_COUNT;
468     	if (drive->removable && drive->usage == 1) {
469     		check_disk_change(inode->i_rdev);
470     		/*
471     		 * Ignore the return code from door_lock,
472     		 * since the open() has already succeeded,
473     		 * and the door_lock is irrelevant at this point.
474     		 */
475     		if (drive->doorlocking && ide_wait_cmd(drive, WIN_DOORLOCK, 0, 0, 0, NULL))
476     			drive->doorlocking = 0;
477     	}
478     	return 0;
479     }
480     
481     static void idedisk_release (struct inode *inode, struct file *filp, ide_drive_t *drive)
482     {
483     	if (drive->removable && !drive->usage) {
484     		invalidate_buffers(inode->i_rdev);
485     		if (drive->doorlocking && ide_wait_cmd(drive, WIN_DOORUNLOCK, 0, 0, 0, NULL))
486     			drive->doorlocking = 0;
487     	}
488     	MOD_DEC_USE_COUNT;
489     }
490     
491     static int idedisk_media_change (ide_drive_t *drive)
492     {
493     	return drive->removable;	/* if removable, always assume it was changed */
494     }
495     
496     static void idedisk_revalidate (ide_drive_t *drive)
497     {
498     	grok_partitions(HWIF(drive)->gd, drive->select.b.unit,
499     			1<<PARTN_BITS,
500     			current_capacity(drive));
501     }
502     
503     /*
504      * Compute drive->capacity, the full capacity of the drive
505      * Called with drive->id != NULL.
506      */
507     static void init_idedisk_capacity (ide_drive_t  *drive)
508     {
509     	struct hd_driveid *id = drive->id;
510     	unsigned long capacity = drive->cyl * drive->head * drive->sect;
511     
512     	drive->select.b.lba = 0;
513     
514     	/* Determine capacity, and use LBA if the drive properly supports it */
515     	if ((id->capability & 2) && lba_capacity_is_ok(id)) {
516     		capacity = id->lba_capacity;
517     		drive->cyl = capacity / (drive->head * drive->sect);
518     		drive->select.b.lba = 1;
519     	}
520     	drive->capacity = capacity;
521     }
522     
523     static unsigned long idedisk_capacity (ide_drive_t  *drive)
524     {
525     	return (drive->capacity - drive->sect0);
526     }
527     
528     static ide_startstop_t idedisk_special (ide_drive_t *drive)
529     {
530     	special_t *s = &drive->special;
531     
532     	if (s->b.set_geometry) {
533     		s->b.set_geometry = 0;
534     		OUT_BYTE(drive->sect,IDE_SECTOR_REG);
535     		OUT_BYTE(drive->cyl,IDE_LCYL_REG);
536     		OUT_BYTE(drive->cyl>>8,IDE_HCYL_REG);
537     		OUT_BYTE(((drive->head-1)|drive->select.all)&0xBF,IDE_SELECT_REG);
538     		if (!IS_PDC4030_DRIVE)
539     			ide_cmd(drive, WIN_SPECIFY, drive->sect, &set_geometry_intr);
540     	} else if (s->b.recalibrate) {
541     		s->b.recalibrate = 0;
542     		if (!IS_PDC4030_DRIVE)
543     			ide_cmd(drive, WIN_RESTORE, drive->sect, &recal_intr);
544     	} else if (s->b.set_multmode) {
545     		s->b.set_multmode = 0;
546     		if (drive->id && drive->mult_req > drive->id->max_multsect)
547     			drive->mult_req = drive->id->max_multsect;
548     		if (!IS_PDC4030_DRIVE)
549     			ide_cmd(drive, WIN_SETMULT, drive->mult_req, &set_multmode_intr);
550     	} else if (s->all) {
551     		int special = s->all;
552     		s->all = 0;
553     		printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
554     		return ide_stopped;
555     	}
556     	return IS_PDC4030_DRIVE ? ide_stopped : ide_started;
557     }
558     
559     static void idedisk_pre_reset (ide_drive_t *drive)
560     {
561     	drive->special.all = 0;
562     	drive->special.b.set_geometry = 1;
563     	drive->special.b.recalibrate  = 1;
564     	if (OK_TO_RESET_CONTROLLER)
565     		drive->mult_count = 0;
566     	if (!drive->keep_settings && !drive->using_dma)
567     		drive->mult_req = 0;
568     	if (drive->mult_req != drive->mult_count)
569     		drive->special.b.set_multmode = 1;
570     }
571     
572     #ifdef CONFIG_PROC_FS
573     
574     static int smart_enable(ide_drive_t *drive)
575     {
576     	return ide_wait_cmd(drive, WIN_SMART, 0, SMART_ENABLE, 0, NULL);
577     }
578     
579     static int get_smart_values(ide_drive_t *drive, byte *buf)
580     {
581     	(void) smart_enable(drive);
582     	return ide_wait_cmd(drive, WIN_SMART, 0, SMART_READ_VALUES, 1, buf);
583     }
584     
585     static int get_smart_thresholds(ide_drive_t *drive, byte *buf)
586     {
587     	(void) smart_enable(drive);
588     	return ide_wait_cmd(drive, WIN_SMART, 0, SMART_READ_THRESHOLDS, 1, buf);
589     }
590     
591     static int proc_idedisk_read_cache
592     	(char *page, char **start, off_t off, int count, int *eof, void *data)
593     {
594     	ide_drive_t	*drive = (ide_drive_t *) data;
595     	char		*out = page;
596     	int		len;
597     
598     	if (drive->id)
599     		len = sprintf(out,"%i\n", drive->id->buf_size / 2);
600     	else
601     		len = sprintf(out,"(none)\n");
602     	PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
603     }
604     
605     static int proc_idedisk_read_smart_thresholds
606     	(char *page, char **start, off_t off, int count, int *eof, void *data)
607     {
608     	ide_drive_t	*drive = (ide_drive_t *)data;
609     	int		len = 0, i = 0;
610     
611     	if (!get_smart_thresholds(drive, page)) {
612     		unsigned short *val = ((unsigned short *)page) + 2;
613     		char *out = ((char *)val) + (SECTOR_WORDS * 4);
614     		page = out;
615     		do {
616     			out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
617     			val += 1;
618     		} while (i < (SECTOR_WORDS * 2));
619     		len = out - page;
620     	}
621     	PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
622     }
623     
624     static int proc_idedisk_read_smart_values
625     	(char *page, char **start, off_t off, int count, int *eof, void *data)
626     {
627     	ide_drive_t	*drive = (ide_drive_t *)data;
628     	int		len = 0, i = 0;
629     
630     	if (!get_smart_values(drive, page)) {
631     		unsigned short *val = ((unsigned short *)page) + 2;
632     		char *out = ((char *)val) + (SECTOR_WORDS * 4);
633     		page = out;
634     		do {
635     			out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
636     			val += 1;
637     		} while (i < (SECTOR_WORDS * 2));
638     		len = out - page;
639     	}
640     	PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
641     }
642     
643     static ide_proc_entry_t idedisk_proc[] = {
644     	{ "cache",		S_IFREG|S_IRUGO,	proc_idedisk_read_cache,		NULL },
645     	{ "geometry",		S_IFREG|S_IRUGO,	proc_ide_read_geometry,			NULL },
646     	{ "smart_values",	S_IFREG|S_IRUSR,	proc_idedisk_read_smart_values,		NULL },
647     	{ "smart_thresholds",	S_IFREG|S_IRUSR,	proc_idedisk_read_smart_thresholds,	NULL },
648     	{ NULL, 0, NULL, NULL }
649     };
650     
651     #else
652     
653     #define	idedisk_proc	NULL
654     
655     #endif	/* CONFIG_PROC_FS */
656     
657     static int set_multcount(ide_drive_t *drive, int arg)
658     {
659     	struct request rq;
660     
661     	if (drive->special.b.set_multmode)
662     		return -EBUSY;
663     	ide_init_drive_cmd (&rq);
664     	drive->mult_req = arg;
665     	drive->special.b.set_multmode = 1;
666     	(void) ide_do_drive_cmd (drive, &rq, ide_wait);
667     	return (drive->mult_count == arg) ? 0 : -EIO;
668     }
669     
670     static int set_nowerr(ide_drive_t *drive, int arg)
671     {
672     	if (ide_spin_wait_hwgroup(drive))
673     		return -EBUSY;
674     	drive->nowerr = arg;
675     	drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
676     	spin_unlock_irq(&io_request_lock);
677     	return 0;
678     }
679     
680     static void idedisk_add_settings(ide_drive_t *drive)
681     {
682     	struct hd_driveid *id = drive->id;
683     	int major = HWIF(drive)->major;
684     	int minor = drive->select.b.unit << PARTN_BITS;
685     
686     	ide_add_setting(drive,	"bios_cyl",		SETTING_RW,					-1,			-1,			TYPE_INT,	0,	65535,				1,	1,	&drive->bios_cyl,		NULL);
687     	ide_add_setting(drive,	"bios_head",		SETTING_RW,					-1,			-1,			TYPE_BYTE,	0,	255,				1,	1,	&drive->bios_head,		NULL);
688     	ide_add_setting(drive,	"bios_sect",		SETTING_RW,					-1,			-1,			TYPE_BYTE,	0,	63,				1,	1,	&drive->bios_sect,		NULL);
689     	ide_add_setting(drive,	"bswap",		SETTING_READ,					-1,			-1,			TYPE_BYTE,	0,	1,				1,	1,	&drive->bswap,			NULL);
690     	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,	2,	&drive->mult_count,		set_multcount);
691     	ide_add_setting(drive,	"nowerr",		SETTING_RW,					HDIO_GET_NOWERR,	HDIO_SET_NOWERR,	TYPE_BYTE,	0,	1,				1,	1,	&drive->nowerr,			set_nowerr);
692     	ide_add_setting(drive,	"breada_readahead",	SETTING_RW,					BLKRAGET,		BLKRASET,		TYPE_INT,	0,	255,				1,	2,	&read_ahead[major],		NULL);
693     	ide_add_setting(drive,	"file_readahead",	SETTING_RW,					BLKFRAGET,		BLKFRASET,		TYPE_INTA,	0,	INT_MAX,			1,	1024,	&max_readahead[major][minor],	NULL);
694     	ide_add_setting(drive,	"max_kb_per_request",	SETTING_RW,					BLKSECTGET,		BLKSECTSET,		TYPE_INTA,	1,	255,				1,	2,	&max_sectors[major][minor],	NULL);
695     	ide_add_setting(drive,	"lun",			SETTING_RW,					-1,			-1,			TYPE_INT,	0,	7,				1,	1,	&drive->lun,			NULL);
696     	ide_add_setting(drive,	"failures",		SETTING_RW,					-1,			-1,			TYPE_INT,	0,	65535,				1,	1,	&drive->failures,		NULL);
697     	ide_add_setting(drive,	"max_failures",		SETTING_RW,					-1,			-1,			TYPE_INT,	0,	65535,				1,	1,	&drive->max_failures,		NULL);
698     }
699     
700     static void idedisk_setup (ide_drive_t *drive)
701     {
702     	int i;
703     	
704     	struct hd_driveid *id = drive->id;
705     	unsigned long capacity;
706     	
707     	idedisk_add_settings(drive);
708     
709     	if (id == NULL)
710     		return;
711     
712     	/*
713     	 * CompactFlash cards and their brethern look just like hard drives
714     	 * to us, but they are removable and don't have a doorlock mechanism.
715     	 */
716     	if (drive->removable && !drive_is_flashcard(drive)) {
717     		/*
718     		 * Removable disks (eg. SYQUEST); ignore 'WD' drives 
719     		 */
720     		if (id->model[0] != 'W' || id->model[1] != 'D') {
721     			drive->doorlocking = 1;
722     		}
723     	}
724     	for (i = 0; i < MAX_DRIVES; ++i) {
725     		ide_hwif_t *hwif = HWIF(drive);
726     
727     		if (drive != &hwif->drives[i]) continue;
728     		hwif->gd->de_arr[i] = drive->de;
729     		if (drive->removable)
730     			hwif->gd->flags[i] |= GENHD_FL_REMOVABLE;
731     		break;
732     	}
733     
734     	/* Extract geometry if we did not already have one for the drive */
735     	if (!drive->cyl || !drive->head || !drive->sect) {
736     		drive->cyl     = drive->bios_cyl  = id->cyls;
737     		drive->head    = drive->bios_head = id->heads;
738     		drive->sect    = drive->bios_sect = id->sectors;
739     	}
740     
741     	/* Handle logical geometry translation by the drive */
742     	if ((id->field_valid & 1) && id->cur_cyls &&
743     	    id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
744     		drive->cyl  = id->cur_cyls;
745     		drive->head = id->cur_heads;
746     		drive->sect = id->cur_sectors;
747     	}
748     
749     	/* Use physical geometry if what we have still makes no sense */
750     	if (drive->head > 16 && id->heads && id->heads <= 16) {
751     		drive->cyl  = id->cyls;
752     		drive->head = id->heads;
753     		drive->sect = id->sectors;
754     	}
755     
756     	/* calculate drive capacity, and select LBA if possible */
757     	init_idedisk_capacity (drive);
758     
759     	/*
760     	 * if possible, give fdisk access to more of the drive,
761     	 * by correcting bios_cyls:
762     	 */
763     	capacity = idedisk_capacity (drive);
764     	if ((capacity >= (drive->bios_cyl * drive->bios_sect * drive->bios_head)) &&
765     	    (!drive->forced_geom) && drive->bios_sect && drive->bios_head)
766     		drive->bios_cyl = (capacity / drive->bios_sect) / drive->bios_head;
767     
768     	printk (KERN_INFO "%s: %ld sectors", drive->name, capacity);
769     
770     	/* Give size in megabytes (MB), not mebibytes (MiB). */
771     	/* We compute the exact rounded value, avoiding overflow. */
772     	printk (" (%ld MB)", (capacity - capacity/625 + 974)/1950);
773     
774     	/* Only print cache size when it was specified */
775     	if (id->buf_size)
776     		printk (" w/%dKiB Cache", id->buf_size/2);
777     
778     	printk(", CHS=%d/%d/%d", 
779     	       drive->bios_cyl, drive->bios_head, drive->bios_sect);
780     #ifdef CONFIG_BLK_DEV_IDEDMA
781     	if (drive->using_dma)
782     		(void) HWIF(drive)->dmaproc(ide_dma_verbose, drive);
783     #endif /* CONFIG_BLK_DEV_IDEDMA */
784     	printk("\n");
785     
786     	drive->mult_count = 0;
787     	if (id->max_multsect) {
788     #ifdef CONFIG_IDEDISK_MULTI_MODE
789     		id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
790     		id->multsect_valid = id->multsect ? 1 : 0;
791     		drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
792     		drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
793     #else	/* original, pre IDE-NFG, per request of AC */
794     		drive->mult_req = INITIAL_MULT_COUNT;
795     		if (drive->mult_req > id->max_multsect)
796     			drive->mult_req = id->max_multsect;
797     		if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
798     			drive->special.b.set_multmode = 1;
799     #endif
800     	}
801     	drive->no_io_32bit = id->dword_io ? 1 : 0;
802     }
803     
804     static int idedisk_reinit (ide_drive_t *drive)
805     {
806     	return 0;
807     }
808     
809     static int idedisk_cleanup (ide_drive_t *drive)
810     {
811     	return ide_unregister_subdriver(drive);
812     }
813     
814     /*
815      *      IDE subdriver functions, registered with ide.c
816      */
817     static ide_driver_t idedisk_driver = {
818     	name:			"ide-disk",
819     	version:		IDEDISK_VERSION,
820     	media:			ide_disk,
821     	busy:			0,
822     	supports_dma:		1,
823     	supports_dsc_overlap:	0,
824     	cleanup:		idedisk_cleanup,
825     	do_request:		do_rw_disk,
826     	end_request:		NULL,
827     	ioctl:			NULL,
828     	open:			idedisk_open,
829     	release:		idedisk_release,
830     	media_change:		idedisk_media_change,
831     	revalidate:		idedisk_revalidate,
832     	pre_reset:		idedisk_pre_reset,
833     	capacity:		idedisk_capacity,
834     	special:		idedisk_special,
835     	proc:			idedisk_proc,
836     	driver_reinit:		idedisk_reinit,
837     };
838     
839     int idedisk_init (void);
840     static ide_module_t idedisk_module = {
841     	IDE_DRIVER_MODULE,
842     	idedisk_init,
843     	&idedisk_driver,
844     	NULL
845     };
846     
847     MODULE_DESCRIPTION("ATA DISK Driver");
848     
849     static void __exit idedisk_exit (void)
850     {
851     	ide_drive_t *drive;
852     	int failed = 0;
853     
854     	while ((drive = ide_scan_devices (ide_disk, idedisk_driver.name, &idedisk_driver, failed)) != NULL) {
855     		if (idedisk_cleanup (drive)) {
856     			printk (KERN_ERR "%s: cleanup_module() called while still busy\n", drive->name);
857     			failed++;
858     		}
859     		/* We must remove proc entries defined in this module.
860     		   Otherwise we oops while accessing these entries */
861     		if (drive->proc)
862     			ide_remove_proc_entries(drive->proc, idedisk_proc);
863     	}
864     	ide_unregister_module(&idedisk_module);
865     }
866     
867     int idedisk_init (void)
868     {
869     	ide_drive_t *drive;
870     	int failed = 0;
871     	
872     	MOD_INC_USE_COUNT;
873     	while ((drive = ide_scan_devices (ide_disk, idedisk_driver.name, NULL, failed++)) != NULL) {
874     		if (ide_register_subdriver (drive, &idedisk_driver, IDE_SUBDRIVER_VERSION)) {
875     			printk (KERN_ERR "ide-disk: %s: Failed to register the driver with ide.c\n", drive->name);
876     			continue;
877     		}
878     		idedisk_setup(drive);
879     		if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
880     			printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n", drive->name, drive->head);
881     			(void) idedisk_cleanup(drive);
882     			continue;
883     		}
884     		failed--;
885     	}
886     	ide_register_module(&idedisk_module);
887     	MOD_DEC_USE_COUNT;
888     	return 0;
889     }
890     
891     module_init(idedisk_init);
892     module_exit(idedisk_exit);
893