Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  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 Development
  6  *                              Andre Hedrick <andre@linux-ide.org>
  7  *  Copyright (C) 2003       Red Hat <alan@redhat.com>
  8  */
  9 
 10 /*
 11  *  Mostly written by Mark Lord <mlord@pobox.com>
 12  *                and Gadi Oxman <gadio@netvision.net.il>
 13  *                and Andre Hedrick <andre@linux-ide.org>
 14  *
 15  * This is the IDE/ATA disk driver, as evolved from hd.c and ide.c.
 16  *
 17  * Version 1.00         move disk only code from ide.c to ide-disk.c
 18  *                      support optional byte-swapping of all data
 19  * Version 1.01         fix previous byte-swapping code
 20  * Version 1.02         remove ", LBA" from drive identification msgs
 21  * Version 1.03         fix display of id->buf_size for big-endian
 22  * Version 1.04         add /proc configurable settings and S.M.A.R.T support
 23  * Version 1.05         add capacity support for ATA3 >= 8GB
 24  * Version 1.06         get boot-up messages to show full cyl count
 25  * Version 1.07         disable door-locking if it fails
 26  * Version 1.08         fixed CHS/LBA translations for ATA4 > 8GB,
 27  *                      process of adding new ATA4 compliance.
 28  *                      fixed problems in allowing fdisk to see
 29  *                      the entire disk.
 30  * Version 1.09         added increment of rq->sector in ide_multwrite
 31  *                      added UDMA 3/4 reporting
 32  * Version 1.10         request queue changes, Ultra DMA 100
 33  * Version 1.11         added 48-bit lba
 34  * Version 1.12         adding taskfile io access method
 35  * Version 1.13         added standby and flush-cache for notifier
 36  * Version 1.14         added acoustic-wcache
 37  * Version 1.15         convert all calls to ide_raw_taskfile
 38  *                              since args will return register content.
 39  * Version 1.16         added suspend-resume-checkpower
 40  * Version 1.17         do flush on standy, do flush on ATA < ATA6
 41  *                      fix wcache setup.
 42  */
 43 
 44 #define IDEDISK_VERSION "1.18"
 45 
 46 #undef REALLY_SLOW_IO           /* most systems can safely undef this */
 47 
 48 //#define DEBUG
 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 #include <asm/div64.h>
 73 
 74 /*
 75  * lba_capacity_is_ok() performs a sanity check on the claimed "lba_capacity"
 76  * value for this drive (from its reported identification information).
 77  *
 78  * Returns:     1 if lba_capacity looks sensible
 79  *              0 otherwise
 80  *
 81  * It is called only once for each drive.
 82  */
 83 static int lba_capacity_is_ok (struct hd_driveid *id)
 84 {
 85         unsigned long lba_sects, chs_sects, head, tail;
 86 
 87         /*
 88          * The ATA spec tells large drives to return
 89          * C/H/S = 16383/16/63 independent of their size.
 90          * Some drives can be jumpered to use 15 heads instead of 16.
 91          * Some drives can be jumpered to use 4092 cyls instead of 16383.
 92          */
 93         if ((id->cyls == 16383
 94              || (id->cyls == 4092 && id->cur_cyls == 16383)) &&
 95             id->sectors == 63 &&
 96             (id->heads == 15 || id->heads == 16) &&
 97             (id->lba_capacity >= 16383*63*id->heads))
 98                 return 1;
 99 
100         lba_sects   = id->lba_capacity;
101         chs_sects   = id->cyls * id->heads * id->sectors;
102 
103         /* perform a rough sanity check on lba_sects:  within 10% is OK */
104         if ((lba_sects - chs_sects) < chs_sects/10)
105                 return 1;
106 
107         /* some drives have the word order reversed */
108         head = ((lba_sects >> 16) & 0xffff);
109         tail = (lba_sects & 0xffff);
110         lba_sects = (head | (tail << 16));
111         if ((lba_sects - chs_sects) < chs_sects/10) {
112                 id->lba_capacity = lba_sects;
113                 return 1;       /* lba_capacity is (now) good */
114         }
115 
116         return 0;       /* lba_capacity value may be bad */
117 }
118 
119 /*
120  * __ide_do_rw_disk() issues READ and WRITE commands to a disk,
121  * using LBA if supported, or CHS otherwise, to address sectors.
122  */
123 static ide_startstop_t __ide_do_rw_disk(ide_drive_t *drive, struct request *rq, sector_t block)
124 {
125         ide_hwif_t *hwif        = HWIF(drive);
126         unsigned int dma        = drive->using_dma;
127         u8 lba48                = (drive->addressing == 1) ? 1 : 0;
128         task_ioreg_t command    = WIN_NOP;
129         ata_nsector_t           nsectors;
130 
131         nsectors.all            = (u16) rq->nr_sectors;
132 
133         if (hwif->no_lba48_dma && lba48 && dma) {
134                 if (block + rq->nr_sectors > 1ULL << 28)
135                         dma = 0;
136         }
137 
138         if (!dma) {
139                 ide_init_sg_cmd(drive, rq);
140                 ide_map_sg(drive, rq);
141         }
142 
143         if (IDE_CONTROL_REG)
144                 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
145 
146         /* FIXME: SELECT_MASK(drive, 0) ? */
147 
148         if (drive->select.b.lba) {
149                 if (drive->addressing == 1) {
150                         task_ioreg_t tasklets[10];
151 
152                         pr_debug("%s: LBA=0x%012llx\n", drive->name, block);
153 
154                         tasklets[0] = 0;
155                         tasklets[1] = 0;
156                         tasklets[2] = nsectors.b.low;
157                         tasklets[3] = nsectors.b.high;
158                         tasklets[4] = (task_ioreg_t) block;
159                         tasklets[5] = (task_ioreg_t) (block>>8);
160                         tasklets[6] = (task_ioreg_t) (block>>16);
161                         tasklets[7] = (task_ioreg_t) (block>>24);
162                         if (sizeof(block) == 4) {
163                                 tasklets[8] = (task_ioreg_t) 0;
164                                 tasklets[9] = (task_ioreg_t) 0;
165                         } else {
166                                 tasklets[8] = (task_ioreg_t)((u64)block >> 32);
167                                 tasklets[9] = (task_ioreg_t)((u64)block >> 40);
168                         }
169 #ifdef DEBUG
170                         printk("%s: 0x%02x%02x 0x%02x%02x%02x%02x%02x%02x\n",
171                                 drive->name, tasklets[3], tasklets[2],
172                                 tasklets[9], tasklets[8], tasklets[7],
173                                 tasklets[6], tasklets[5], tasklets[4]);
174 #endif
175                         hwif->OUTB(tasklets[1], IDE_FEATURE_REG);
176                         hwif->OUTB(tasklets[3], IDE_NSECTOR_REG);
177                         hwif->OUTB(tasklets[7], IDE_SECTOR_REG);
178                         hwif->OUTB(tasklets[8], IDE_LCYL_REG);
179                         hwif->OUTB(tasklets[9], IDE_HCYL_REG);
180 
181                         hwif->OUTB(tasklets[0], IDE_FEATURE_REG);
182                         hwif->OUTB(tasklets[2], IDE_NSECTOR_REG);
183                         hwif->OUTB(tasklets[4], IDE_SECTOR_REG);
184                         hwif->OUTB(tasklets[5], IDE_LCYL_REG);
185                         hwif->OUTB(tasklets[6], IDE_HCYL_REG);
186                         hwif->OUTB(0x00|drive->select.all,IDE_SELECT_REG);
187                 } else {
188                         hwif->OUTB(0x00, IDE_FEATURE_REG);
189                         hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
190                         hwif->OUTB(block, IDE_SECTOR_REG);
191                         hwif->OUTB(block>>=8, IDE_LCYL_REG);
192                         hwif->OUTB(block>>=8, IDE_HCYL_REG);
193                         hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
194                 }
195         } else {
196                 unsigned int sect,head,cyl,track;
197                 track = (int)block / drive->sect;
198                 sect  = (int)block % drive->sect + 1;
199                 hwif->OUTB(sect, IDE_SECTOR_REG);
200                 head  = track % drive->head;
201                 cyl   = track / drive->head;
202 
203                 pr_debug("%s: CHS=%u/%u/%u\n", drive->name, cyl, head, sect);
204 
205                 hwif->OUTB(0x00, IDE_FEATURE_REG);
206                 hwif->OUTB(nsectors.b.low, IDE_NSECTOR_REG);
207                 hwif->OUTB(cyl, IDE_LCYL_REG);
208                 hwif->OUTB(cyl>>8, IDE_HCYL_REG);
209                 hwif->OUTB(head|drive->select.all,IDE_SELECT_REG);
210         }
211 
212         if (dma) {
213                 if (!hwif->dma_setup(drive)) {
214                         if (rq_data_dir(rq)) {
215                                 command = lba48 ? WIN_WRITEDMA_EXT : WIN_WRITEDMA;
216                                 if (drive->vdma)
217                                         command = lba48 ? WIN_WRITE_EXT: WIN_WRITE;
218                         } else {
219                                 command = lba48 ? WIN_READDMA_EXT : WIN_READDMA;
220                                 if (drive->vdma)
221                                         command = lba48 ? WIN_READ_EXT: WIN_READ;
222                         }
223                         hwif->dma_exec_cmd(drive, command);
224                         hwif->dma_start(drive);
225                         return ide_started;
226                 }
227                 /* fallback to PIO */
228                 ide_init_sg_cmd(drive, rq);
229         }
230 
231         if (rq_data_dir(rq) == READ) {
232 
233                 if (drive->mult_count) {
234                         hwif->data_phase = TASKFILE_MULTI_IN;
235                         command = lba48 ? WIN_MULTREAD_EXT : WIN_MULTREAD;
236                 } else {
237                         hwif->data_phase = TASKFILE_IN;
238                         command = lba48 ? WIN_READ_EXT : WIN_READ;
239                 }
240 
241                 ide_execute_command(drive, command, &task_in_intr, WAIT_CMD, NULL);
242                 return ide_started;
243         } else {
244                 if (drive->mult_count) {
245                         hwif->data_phase = TASKFILE_MULTI_OUT;
246                         command = lba48 ? WIN_MULTWRITE_EXT : WIN_MULTWRITE;
247                 } else {
248                         hwif->data_phase = TASKFILE_OUT;
249                         command = lba48 ? WIN_WRITE_EXT : WIN_WRITE;
250                 }
251 
252                 /* FIXME: ->OUTBSYNC ? */
253                 hwif->OUTB(command, IDE_COMMAND_REG);
254 
255                 return pre_task_out_intr(drive, rq);
256         }
257 }
258 
259 /*
260  * 268435455  == 137439 MB or 28bit limit
261  * 320173056  == 163929 MB or 48bit addressing
262  * 1073741822 == 549756 MB or 48bit addressing fake drive
263  */
264 
265 static ide_startstop_t ide_do_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
266 {
267         ide_hwif_t *hwif = HWIF(drive);
268 
269         BUG_ON(drive->blocked);
270 
271         if (!blk_fs_request(rq)) {
272                 blk_dump_rq_flags(rq, "ide_do_rw_disk - bad command");
273                 ide_end_request(drive, 0, 0);
274                 return ide_stopped;
275         }
276 
277         pr_debug("%s: %sing: block=%llu, sectors=%lu, buffer=0x%08lx\n",
278                  drive->name, rq_data_dir(rq) == READ ? "read" : "writ",
279                  block, rq->nr_sectors, (unsigned long)rq->buffer);
280 
281         if (hwif->rw_disk)
282                 hwif->rw_disk(drive, rq);
283 
284         return __ide_do_rw_disk(drive, rq, block);
285 }
286 
287 /*
288  * Queries for true maximum capacity of the drive.
289  * Returns maximum LBA address (> 0) of the drive, 0 if failed.
290  */
291 static unsigned long idedisk_read_native_max_address(ide_drive_t *drive)
292 {
293         ide_task_t args;
294         unsigned long addr = 0;
295 
296         /* Create IDE/ATA command request structure */
297         memset(&args, 0, sizeof(ide_task_t));
298         args.tfRegister[IDE_SELECT_OFFSET]      = 0x40;
299         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_READ_NATIVE_MAX;
300         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
301         args.handler                            = &task_no_data_intr;
302         /* submit command request */
303         ide_raw_taskfile(drive, &args, NULL);
304 
305         /* if OK, compute maximum address value */
306         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
307                 addr = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
308                      | ((args.tfRegister[  IDE_HCYL_OFFSET]       ) << 16)
309                      | ((args.tfRegister[  IDE_LCYL_OFFSET]       ) <<  8)
310                      | ((args.tfRegister[IDE_SECTOR_OFFSET]       ));
311                 addr++; /* since the return value is (maxlba - 1), we add 1 */
312         }
313         return addr;
314 }
315 
316 static unsigned long long idedisk_read_native_max_address_ext(ide_drive_t *drive)
317 {
318         ide_task_t args;
319         unsigned long long addr = 0;
320 
321         /* Create IDE/ATA command request structure */
322         memset(&args, 0, sizeof(ide_task_t));
323 
324         args.tfRegister[IDE_SELECT_OFFSET]      = 0x40;
325         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_READ_NATIVE_MAX_EXT;
326         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
327         args.handler                            = &task_no_data_intr;
328         /* submit command request */
329         ide_raw_taskfile(drive, &args, NULL);
330 
331         /* if OK, compute maximum address value */
332         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
333                 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
334                            (args.hobRegister[IDE_LCYL_OFFSET] <<  8) |
335                             args.hobRegister[IDE_SECTOR_OFFSET];
336                 u32 low  = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
337                            ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
338                             (args.tfRegister[IDE_SECTOR_OFFSET]);
339                 addr = ((__u64)high << 24) | low;
340                 addr++; /* since the return value is (maxlba - 1), we add 1 */
341         }
342         return addr;
343 }
344 
345 /*
346  * Sets maximum virtual LBA address of the drive.
347  * Returns new maximum virtual LBA address (> 0) or 0 on failure.
348  */
349 static unsigned long idedisk_set_max_address(ide_drive_t *drive, unsigned long addr_req)
350 {
351         ide_task_t args;
352         unsigned long addr_set = 0;
353         
354         addr_req--;
355         /* Create IDE/ATA command request structure */
356         memset(&args, 0, sizeof(ide_task_t));
357         args.tfRegister[IDE_SECTOR_OFFSET]      = ((addr_req >>  0) & 0xff);
358         args.tfRegister[IDE_LCYL_OFFSET]        = ((addr_req >>  8) & 0xff);
359         args.tfRegister[IDE_HCYL_OFFSET]        = ((addr_req >> 16) & 0xff);
360         args.tfRegister[IDE_SELECT_OFFSET]      = ((addr_req >> 24) & 0x0f) | 0x40;
361         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SET_MAX;
362         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
363         args.handler                            = &task_no_data_intr;
364         /* submit command request */
365         ide_raw_taskfile(drive, &args, NULL);
366         /* if OK, read new maximum address value */
367         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
368                 addr_set = ((args.tfRegister[IDE_SELECT_OFFSET] & 0x0f) << 24)
369                          | ((args.tfRegister[  IDE_HCYL_OFFSET]       ) << 16)
370                          | ((args.tfRegister[  IDE_LCYL_OFFSET]       ) <<  8)
371                          | ((args.tfRegister[IDE_SECTOR_OFFSET]       ));
372                 addr_set++;
373         }
374         return addr_set;
375 }
376 
377 static unsigned long long idedisk_set_max_address_ext(ide_drive_t *drive, unsigned long long addr_req)
378 {
379         ide_task_t args;
380         unsigned long long addr_set = 0;
381 
382         addr_req--;
383         /* Create IDE/ATA command request structure */
384         memset(&args, 0, sizeof(ide_task_t));
385         args.tfRegister[IDE_SECTOR_OFFSET]      = ((addr_req >>  0) & 0xff);
386         args.tfRegister[IDE_LCYL_OFFSET]        = ((addr_req >>= 8) & 0xff);
387         args.tfRegister[IDE_HCYL_OFFSET]        = ((addr_req >>= 8) & 0xff);
388         args.tfRegister[IDE_SELECT_OFFSET]      = 0x40;
389         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SET_MAX_EXT;
390         args.hobRegister[IDE_SECTOR_OFFSET]     = (addr_req >>= 8) & 0xff;
391         args.hobRegister[IDE_LCYL_OFFSET]       = (addr_req >>= 8) & 0xff;
392         args.hobRegister[IDE_HCYL_OFFSET]       = (addr_req >>= 8) & 0xff;
393         args.hobRegister[IDE_SELECT_OFFSET]     = 0x40;
394         args.hobRegister[IDE_CONTROL_OFFSET_HOB]= (drive->ctl|0x80);
395         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
396         args.handler                            = &task_no_data_intr;
397         /* submit command request */
398         ide_raw_taskfile(drive, &args, NULL);
399         /* if OK, compute maximum address value */
400         if ((args.tfRegister[IDE_STATUS_OFFSET] & 0x01) == 0) {
401                 u32 high = (args.hobRegister[IDE_HCYL_OFFSET] << 16) |
402                            (args.hobRegister[IDE_LCYL_OFFSET] <<  8) |
403                             args.hobRegister[IDE_SECTOR_OFFSET];
404                 u32 low  = ((args.tfRegister[IDE_HCYL_OFFSET])<<16) |
405                            ((args.tfRegister[IDE_LCYL_OFFSET])<<8) |
406                             (args.tfRegister[IDE_SECTOR_OFFSET]);
407                 addr_set = ((__u64)high << 24) | low;
408                 addr_set++;
409         }
410         return addr_set;
411 }
412 
413 static unsigned long long sectors_to_MB(unsigned long long n)
414 {
415         n <<= 9;                /* make it bytes */
416         do_div(n, 1000000);     /* make it MB */
417         return n;
418 }
419 
420 /*
421  * Bits 10 of command_set_1 and cfs_enable_1 must be equal,
422  * so on non-buggy drives we need test only one.
423  * However, we should also check whether these fields are valid.
424  */
425 static inline int idedisk_supports_hpa(const struct hd_driveid *id)
426 {
427         return (id->command_set_1 & 0x0400) && (id->cfs_enable_1 & 0x0400);
428 }
429 
430 /*
431  * The same here.
432  */
433 static inline int idedisk_supports_lba48(const struct hd_driveid *id)
434 {
435         return (id->command_set_2 & 0x0400) && (id->cfs_enable_2 & 0x0400)
436                && id->lba_capacity_2;
437 }
438 
439 static inline void idedisk_check_hpa(ide_drive_t *drive)
440 {
441         unsigned long long capacity, set_max;
442         int lba48 = idedisk_supports_lba48(drive->id);
443 
444         capacity = drive->capacity64;
445         if (lba48)
446                 set_max = idedisk_read_native_max_address_ext(drive);
447         else
448                 set_max = idedisk_read_native_max_address(drive);
449 
450         if (set_max <= capacity)
451                 return;
452 
453         printk(KERN_INFO "%s: Host Protected Area detected.\n"
454                          "\tcurrent capacity is %llu sectors (%llu MB)\n"
455                          "\tnative  capacity is %llu sectors (%llu MB)\n",
456                          drive->name,
457                          capacity, sectors_to_MB(capacity),
458                          set_max, sectors_to_MB(set_max));
459 
460         if (lba48)
461                 set_max = idedisk_set_max_address_ext(drive, set_max);
462         else
463                 set_max = idedisk_set_max_address(drive, set_max);
464         if (set_max) {
465                 drive->capacity64 = set_max;
466                 printk(KERN_INFO "%s: Host Protected Area disabled.\n",
467                                  drive->name);
468         }
469 }
470 
471 /*
472  * Compute drive->capacity, the full capacity of the drive
473  * Called with drive->id != NULL.
474  *
475  * To compute capacity, this uses either of
476  *
477  *    1. CHS value set by user       (whatever user sets will be trusted)
478  *    2. LBA value from target drive (require new ATA feature)
479  *    3. LBA value from system BIOS  (new one is OK, old one may break)
480  *    4. CHS value from system BIOS  (traditional style)
481  *
482  * in above order (i.e., if value of higher priority is available,
483  * reset will be ignored).
484  */
485 static void init_idedisk_capacity (ide_drive_t  *drive)
486 {
487         struct hd_driveid *id = drive->id;
488         /*
489          * If this drive supports the Host Protected Area feature set,
490          * then we may need to change our opinion about the drive's capacity.
491          */
492         int hpa = idedisk_supports_hpa(id);
493 
494         if (idedisk_supports_lba48(id)) {
495                 /* drive speaks 48-bit LBA */
496                 drive->select.b.lba = 1;
497                 drive->capacity64 = id->lba_capacity_2;
498                 if (hpa)
499                         idedisk_check_hpa(drive);
500         } else if ((id->capability & 2) && lba_capacity_is_ok(id)) {
501                 /* drive speaks 28-bit LBA */
502                 drive->select.b.lba = 1;
503                 drive->capacity64 = id->lba_capacity;
504                 if (hpa)
505                         idedisk_check_hpa(drive);
506         } else {
507                 /* drive speaks boring old 28-bit CHS */
508                 drive->capacity64 = drive->cyl * drive->head * drive->sect;
509         }
510 }
511 
512 static sector_t idedisk_capacity (ide_drive_t *drive)
513 {
514         return drive->capacity64 - drive->sect0;
515 }
516 
517 #define IS_PDC4030_DRIVE        0
518 
519 static ide_startstop_t idedisk_special (ide_drive_t *drive)
520 {
521         special_t *s = &drive->special;
522 
523         if (s->b.set_geometry) {
524                 s->b.set_geometry       = 0;
525                 if (!IS_PDC4030_DRIVE) {
526                         ide_task_t args;
527                         memset(&args, 0, sizeof(ide_task_t));
528                         args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
529                         args.tfRegister[IDE_SECTOR_OFFSET]  = drive->sect;
530                         args.tfRegister[IDE_LCYL_OFFSET]    = drive->cyl;
531                         args.tfRegister[IDE_HCYL_OFFSET]    = drive->cyl>>8;
532                         args.tfRegister[IDE_SELECT_OFFSET]  = ((drive->head-1)|drive->select.all)&0xBF;
533                         args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SPECIFY;
534                         args.command_type = IDE_DRIVE_TASK_NO_DATA;
535                         args.handler      = &set_geometry_intr;
536                         do_rw_taskfile(drive, &args);
537                 }
538         } else if (s->b.recalibrate) {
539                 s->b.recalibrate = 0;
540                 if (!IS_PDC4030_DRIVE) {
541                         ide_task_t args;
542                         memset(&args, 0, sizeof(ide_task_t));
543                         args.tfRegister[IDE_NSECTOR_OFFSET] = drive->sect;
544                         args.tfRegister[IDE_COMMAND_OFFSET] = WIN_RESTORE;
545                         args.command_type = IDE_DRIVE_TASK_NO_DATA;
546                         args.handler      = &recal_intr;
547                         do_rw_taskfile(drive, &args);
548                 }
549         } else if (s->b.set_multmode) {
550                 s->b.set_multmode = 0;
551                 if (drive->mult_req > drive->id->max_multsect)
552                         drive->mult_req = drive->id->max_multsect;
553                 if (!IS_PDC4030_DRIVE) {
554                         ide_task_t args;
555                         memset(&args, 0, sizeof(ide_task_t));
556                         args.tfRegister[IDE_NSECTOR_OFFSET] = drive->mult_req;
557                         args.tfRegister[IDE_COMMAND_OFFSET] = WIN_SETMULT;
558                         args.command_type = IDE_DRIVE_TASK_NO_DATA;
559                         args.handler      = &set_multmode_intr;
560                         do_rw_taskfile(drive, &args);
561                 }
562         } else if (s->all) {
563                 int special = s->all;
564                 s->all = 0;
565                 printk(KERN_ERR "%s: bad special flag: 0x%02x\n", drive->name, special);
566                 return ide_stopped;
567         }
568         return IS_PDC4030_DRIVE ? ide_stopped : ide_started;
569 }
570 
571 static void idedisk_pre_reset (ide_drive_t *drive)
572 {
573         int legacy = (drive->id->cfs_enable_2 & 0x0400) ? 0 : 1;
574 
575         drive->special.all = 0;
576         drive->special.b.set_geometry = legacy;
577         drive->special.b.recalibrate  = legacy;
578         if (OK_TO_RESET_CONTROLLER)
579                 drive->mult_count = 0;
580         if (!drive->keep_settings && !drive->using_dma)
581                 drive->mult_req = 0;
582         if (drive->mult_req != drive->mult_count)
583                 drive->special.b.set_multmode = 1;
584 }
585 
586 #ifdef CONFIG_PROC_FS
587 
588 static int smart_enable(ide_drive_t *drive)
589 {
590         ide_task_t args;
591 
592         memset(&args, 0, sizeof(ide_task_t));
593         args.tfRegister[IDE_FEATURE_OFFSET]     = SMART_ENABLE;
594         args.tfRegister[IDE_LCYL_OFFSET]        = SMART_LCYL_PASS;
595         args.tfRegister[IDE_HCYL_OFFSET]        = SMART_HCYL_PASS;
596         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SMART;
597         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
598         args.handler                            = &task_no_data_intr;
599         return ide_raw_taskfile(drive, &args, NULL);
600 }
601 
602 static int get_smart_values(ide_drive_t *drive, u8 *buf)
603 {
604         ide_task_t args;
605 
606         memset(&args, 0, sizeof(ide_task_t));
607         args.tfRegister[IDE_FEATURE_OFFSET]     = SMART_READ_VALUES;
608         args.tfRegister[IDE_NSECTOR_OFFSET]     = 0x01;
609         args.tfRegister[IDE_LCYL_OFFSET]        = SMART_LCYL_PASS;
610         args.tfRegister[IDE_HCYL_OFFSET]        = SMART_HCYL_PASS;
611         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SMART;
612         args.command_type                       = IDE_DRIVE_TASK_IN;
613         args.data_phase                         = TASKFILE_IN;
614         args.handler                            = &task_in_intr;
615         (void) smart_enable(drive);
616         return ide_raw_taskfile(drive, &args, buf);
617 }
618 
619 static int get_smart_thresholds(ide_drive_t *drive, u8 *buf)
620 {
621         ide_task_t args;
622         memset(&args, 0, sizeof(ide_task_t));
623         args.tfRegister[IDE_FEATURE_OFFSET]     = SMART_READ_THRESHOLDS;
624         args.tfRegister[IDE_NSECTOR_OFFSET]     = 0x01;
625         args.tfRegister[IDE_LCYL_OFFSET]        = SMART_LCYL_PASS;
626         args.tfRegister[IDE_HCYL_OFFSET]        = SMART_HCYL_PASS;
627         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SMART;
628         args.command_type                       = IDE_DRIVE_TASK_IN;
629         args.data_phase                         = TASKFILE_IN;
630         args.handler                            = &task_in_intr;
631         (void) smart_enable(drive);
632         return ide_raw_taskfile(drive, &args, buf);
633 }
634 
635 static int proc_idedisk_read_cache
636         (char *page, char **start, off_t off, int count, int *eof, void *data)
637 {
638         ide_drive_t     *drive = (ide_drive_t *) data;
639         char            *out = page;
640         int             len;
641 
642         if (drive->id_read)
643                 len = sprintf(out,"%i\n", drive->id->buf_size / 2);
644         else
645                 len = sprintf(out,"(none)\n");
646         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
647 }
648 
649 static int proc_idedisk_read_smart_thresholds
650         (char *page, char **start, off_t off, int count, int *eof, void *data)
651 {
652         ide_drive_t     *drive = (ide_drive_t *)data;
653         int             len = 0, i = 0;
654 
655         if (!get_smart_thresholds(drive, page)) {
656                 unsigned short *val = (unsigned short *) page;
657                 char *out = ((char *)val) + (SECTOR_WORDS * 4);
658                 page = out;
659                 do {
660                         out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
661                         val += 1;
662                 } while (i < (SECTOR_WORDS * 2));
663                 len = out - page;
664         }
665         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
666 }
667 
668 static int proc_idedisk_read_smart_values
669         (char *page, char **start, off_t off, int count, int *eof, void *data)
670 {
671         ide_drive_t     *drive = (ide_drive_t *)data;
672         int             len = 0, i = 0;
673 
674         if (!get_smart_values(drive, page)) {
675                 unsigned short *val = (unsigned short *) page;
676                 char *out = ((char *)val) + (SECTOR_WORDS * 4);
677                 page = out;
678                 do {
679                         out += sprintf(out, "%04x%c", le16_to_cpu(*val), (++i & 7) ? ' ' : '\n');
680                         val += 1;
681                 } while (i < (SECTOR_WORDS * 2));
682                 len = out - page;
683         }
684         PROC_IDE_READ_RETURN(page,start,off,count,eof,len);
685 }
686 
687 static ide_proc_entry_t idedisk_proc[] = {
688         { "cache",              S_IFREG|S_IRUGO,        proc_idedisk_read_cache,                NULL },
689         { "geometry",           S_IFREG|S_IRUGO,        proc_ide_read_geometry,                 NULL },
690         { "smart_values",       S_IFREG|S_IRUSR,        proc_idedisk_read_smart_values,         NULL },
691         { "smart_thresholds",   S_IFREG|S_IRUSR,        proc_idedisk_read_smart_thresholds,     NULL },
692         { NULL, 0, NULL, NULL }
693 };
694 
695 #else
696 
697 #define idedisk_proc    NULL
698 
699 #endif  /* CONFIG_PROC_FS */
700 
701 static int idedisk_issue_flush(request_queue_t *q, struct gendisk *disk,
702                                sector_t *error_sector)
703 {
704         ide_drive_t *drive = q->queuedata;
705         struct request *rq;
706         int ret;
707 
708         if (!drive->wcache)
709                 return 0;
710 
711         rq = blk_get_request(q, WRITE, __GFP_WAIT);
712 
713         memset(rq->cmd, 0, sizeof(rq->cmd));
714 
715         if (ide_id_has_flush_cache_ext(drive->id) &&
716             (drive->capacity64 >= (1UL << 28)))
717                 rq->cmd[0] = WIN_FLUSH_CACHE_EXT;
718         else
719                 rq->cmd[0] = WIN_FLUSH_CACHE;
720 
721 
722         rq->flags |= REQ_DRIVE_TASK | REQ_SOFTBARRIER;
723         rq->buffer = rq->cmd;
724 
725         ret = blk_execute_rq(q, disk, rq);
726 
727         /*
728          * if we failed and caller wants error offset, get it
729          */
730         if (ret && error_sector)
731                 *error_sector = ide_get_error_location(drive, rq->cmd);
732 
733         blk_put_request(rq);
734         return ret;
735 }
736 
737 /*
738  * This is tightly woven into the driver->do_special can not touch.
739  * DON'T do it again until a total personality rewrite is committed.
740  */
741 static int set_multcount(ide_drive_t *drive, int arg)
742 {
743         struct request rq;
744 
745         if (drive->special.b.set_multmode)
746                 return -EBUSY;
747         ide_init_drive_cmd (&rq);
748         rq.flags = REQ_DRIVE_CMD;
749         drive->mult_req = arg;
750         drive->special.b.set_multmode = 1;
751         (void) ide_do_drive_cmd (drive, &rq, ide_wait);
752         return (drive->mult_count == arg) ? 0 : -EIO;
753 }
754 
755 static int set_nowerr(ide_drive_t *drive, int arg)
756 {
757         if (ide_spin_wait_hwgroup(drive))
758                 return -EBUSY;
759         drive->nowerr = arg;
760         drive->bad_wstat = arg ? BAD_R_STAT : BAD_W_STAT;
761         spin_unlock_irq(&ide_lock);
762         return 0;
763 }
764 
765 static int write_cache(ide_drive_t *drive, int arg)
766 {
767         ide_task_t args;
768         int err;
769 
770         if (!ide_id_has_flush_cache(drive->id))
771                 return 1;
772 
773         memset(&args, 0, sizeof(ide_task_t));
774         args.tfRegister[IDE_FEATURE_OFFSET]     = (arg) ?
775                         SETFEATURES_EN_WCACHE : SETFEATURES_DIS_WCACHE;
776         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SETFEATURES;
777         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
778         args.handler                            = &task_no_data_intr;
779 
780         err = ide_raw_taskfile(drive, &args, NULL);
781         if (err)
782                 return err;
783 
784         drive->wcache = arg;
785         return 0;
786 }
787 
788 static int do_idedisk_flushcache (ide_drive_t *drive)
789 {
790         ide_task_t args;
791 
792         memset(&args, 0, sizeof(ide_task_t));
793         if (ide_id_has_flush_cache_ext(drive->id))
794                 args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_FLUSH_CACHE_EXT;
795         else
796                 args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_FLUSH_CACHE;
797         args.command_type                       = IDE_DRIVE_TASK_NO_DATA;
798         args.handler                            = &task_no_data_intr;
799         return ide_raw_taskfile(drive, &args, NULL);
800 }
801 
802 static int set_acoustic (ide_drive_t *drive, int arg)
803 {
804         ide_task_t args;
805 
806         memset(&args, 0, sizeof(ide_task_t));
807         args.tfRegister[IDE_FEATURE_OFFSET]     = (arg) ? SETFEATURES_EN_AAM :
808                                                           SETFEATURES_DIS_AAM;
809         args.tfRegister[IDE_NSECTOR_OFFSET]     = arg;
810         args.tfRegister[IDE_COMMAND_OFFSET]     = WIN_SETFEATURES;
811         args.command_type = IDE_DRIVE_TASK_NO_DATA;
812         args.handler      = &task_no_data_intr;
813         ide_raw_taskfile(drive, &args, NULL);
814         drive->acoustic = arg;
815         return 0;
816 }
817 
818 /*
819  * drive->addressing:
820  *      0: 28-bit
821  *      1: 48-bit
822  *      2: 48-bit capable doing 28-bit
823  */
824 static int set_lba_addressing(ide_drive_t *drive, int arg)
825 {
826         drive->addressing =  0;
827 
828         if (HWIF(drive)->no_lba48)
829                 return 0;
830 
831         if (!idedisk_supports_lba48(drive->id))
832                 return -EIO;
833         drive->addressing = arg;
834         return 0;
835 }
836 
837 static void idedisk_add_settings(ide_drive_t *drive)
838 {
839         struct hd_driveid *id = drive->id;
840 
841         ide_add_setting(drive,  "bios_cyl",             SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      65535,                          1,      1,      &drive->bios_cyl,               NULL);
842         ide_add_setting(drive,  "bios_head",            SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      255,                            1,      1,      &drive->bios_head,              NULL);
843         ide_add_setting(drive,  "bios_sect",            SETTING_RW,                                     -1,                     -1,                     TYPE_BYTE,      0,      63,                             1,      1,      &drive->bios_sect,              NULL);
844         ide_add_setting(drive,  "address",              SETTING_RW,                                     HDIO_GET_ADDRESS,       HDIO_SET_ADDRESS,       TYPE_INTA,      0,      2,                              1,      1,      &drive->addressing,     set_lba_addressing);
845         ide_add_setting(drive,  "bswap",                SETTING_READ,                                   -1,                     -1,                     TYPE_BYTE,      0,      1,                              1,      1,      &drive->bswap,                  NULL);
846         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);
847         ide_add_setting(drive,  "nowerr",               SETTING_RW,                                     HDIO_GET_NOWERR,        HDIO_SET_NOWERR,        TYPE_BYTE,      0,      1,                              1,      1,      &drive->nowerr,                 set_nowerr);
848         ide_add_setting(drive,  "lun",                  SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      7,                              1,      1,      &drive->lun,                    NULL);
849         ide_add_setting(drive,  "wcache",               SETTING_RW,                                     HDIO_GET_WCACHE,        HDIO_SET_WCACHE,        TYPE_BYTE,      0,      1,                              1,      1,      &drive->wcache,                 write_cache);
850         ide_add_setting(drive,  "acoustic",             SETTING_RW,                                     HDIO_GET_ACOUSTIC,      HDIO_SET_ACOUSTIC,      TYPE_BYTE,      0,      254,                            1,      1,      &drive->acoustic,               set_acoustic);
851         ide_add_setting(drive,  "failures",             SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      65535,                          1,      1,      &drive->failures,               NULL);
852         ide_add_setting(drive,  "max_failures",         SETTING_RW,                                     -1,                     -1,                     TYPE_INT,       0,      65535,                          1,      1,      &drive->max_failures,           NULL);
853 }
854 
855 /*
856  * Power Management state machine. This one is rather trivial for now,
857  * we should probably add more, like switching back to PIO on suspend
858  * to help some BIOSes, re-do the door locking on resume, etc...
859  */
860 
861 enum {
862         idedisk_pm_flush_cache  = ide_pm_state_start_suspend,
863         idedisk_pm_standby,
864 
865         idedisk_pm_idle         = ide_pm_state_start_resume,
866         idedisk_pm_restore_dma,
867 };
868 
869 static void idedisk_complete_power_step (ide_drive_t *drive, struct request *rq, u8 stat, u8 error)
870 {
871         switch (rq->pm->pm_step) {
872         case idedisk_pm_flush_cache:    /* Suspend step 1 (flush cache) complete */
873                 if (rq->pm->pm_state == 4)
874                         rq->pm->pm_step = ide_pm_state_completed;
875                 else
876                         rq->pm->pm_step = idedisk_pm_standby;
877                 break;
878         case idedisk_pm_standby:        /* Suspend step 2 (standby) complete */
879                 rq->pm->pm_step = ide_pm_state_completed;
880                 break;
881         case idedisk_pm_idle:           /* Resume step 1 (idle) complete */
882                 rq->pm->pm_step = idedisk_pm_restore_dma;
883                 break;
884         }
885 }
886 
887 static ide_startstop_t idedisk_start_power_step (ide_drive_t *drive, struct request *rq)
888 {
889         ide_task_t *args = rq->special;
890 
891         memset(args, 0, sizeof(*args));
892 
893         switch (rq->pm->pm_step) {
894         case idedisk_pm_flush_cache:    /* Suspend step 1 (flush cache) */
895                 /* Not supported? Switch to next step now. */
896                 if (!drive->wcache || !ide_id_has_flush_cache(drive->id)) {
897                         idedisk_complete_power_step(drive, rq, 0, 0);
898                         return ide_stopped;
899                 }
900                 if (ide_id_has_flush_cache_ext(drive->id))
901                         args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE_EXT;
902                 else
903                         args->tfRegister[IDE_COMMAND_OFFSET] = WIN_FLUSH_CACHE;
904                 args->command_type = IDE_DRIVE_TASK_NO_DATA;
905                 args->handler      = &task_no_data_intr;
906                 return do_rw_taskfile(drive, args);
907 
908         case idedisk_pm_standby:        /* Suspend step 2 (standby) */
909                 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_STANDBYNOW1;
910                 args->command_type = IDE_DRIVE_TASK_NO_DATA;
911                 args->handler      = &task_no_data_intr;
912                 return do_rw_taskfile(drive, args);
913 
914         case idedisk_pm_idle:           /* Resume step 1 (idle) */
915                 args->tfRegister[IDE_COMMAND_OFFSET] = WIN_IDLEIMMEDIATE;
916                 args->command_type = IDE_DRIVE_TASK_NO_DATA;
917                 args->handler = task_no_data_intr;
918                 return do_rw_taskfile(drive, args);
919 
920         case idedisk_pm_restore_dma:    /* Resume step 2 (restore DMA) */
921                 /*
922                  * Right now, all we do is call hwif->ide_dma_check(drive),
923                  * we could be smarter and check for current xfer_speed
924                  * in struct drive etc...
925                  * Also, this step could be implemented as a generic helper
926                  * as most subdrivers will use it
927                  */
928                 if ((drive->id->capability & 1) == 0)
929                         break;
930                 if (HWIF(drive)->ide_dma_check == NULL)
931                         break;
932                 HWIF(drive)->ide_dma_check(drive);
933                 break;
934         }
935         rq->pm->pm_step = ide_pm_state_completed;
936         return ide_stopped;
937 }
938 
939 static void idedisk_setup (ide_drive_t *drive)
940 {
941         struct hd_driveid *id = drive->id;
942         unsigned long long capacity;
943         int barrier;
944 
945         idedisk_add_settings(drive);
946 
947         if (drive->id_read == 0)
948                 return;
949 
950         /*
951          * CompactFlash cards and their brethern look just like hard drives
952          * to us, but they are removable and don't have a doorlock mechanism.
953          */
954         if (drive->removable && !(drive->is_flash)) {
955                 /*
956                  * Removable disks (eg. SYQUEST); ignore 'WD' drives 
957                  */
958                 if (id->model[0] != 'W' || id->model[1] != 'D') {
959                         drive->doorlocking = 1;
960                 }
961         }
962 
963         (void)set_lba_addressing(drive, 1);
964 
965         if (drive->addressing == 1) {
966                 ide_hwif_t *hwif = HWIF(drive);
967                 int max_s = 2048;
968 
969                 if (max_s > hwif->rqsize)
970                         max_s = hwif->rqsize;
971 
972                 blk_queue_max_sectors(drive->queue, max_s);
973         }
974 
975         printk(KERN_INFO "%s: max request size: %dKiB\n", drive->name, drive->queue->max_sectors / 2);
976 
977         /* Extract geometry if we did not already have one for the drive */
978         if (!drive->cyl || !drive->head || !drive->sect) {
979                 drive->cyl     = drive->bios_cyl  = id->cyls;
980                 drive->head    = drive->bios_head = id->heads;
981                 drive->sect    = drive->bios_sect = id->sectors;
982         }
983 
984         /* Handle logical geometry translation by the drive */
985         if ((id->field_valid & 1) && id->cur_cyls &&
986             id->cur_heads && (id->cur_heads <= 16) && id->cur_sectors) {
987                 drive->cyl  = id->cur_cyls;
988                 drive->head = id->cur_heads;
989                 drive->sect = id->cur_sectors;
990         }
991 
992         /* Use physical geometry if what we have still makes no sense */
993         if (drive->head > 16 && id->heads && id->heads <= 16) {
994                 drive->cyl  = id->cyls;
995                 drive->head = id->heads;
996                 drive->sect = id->sectors;
997         }
998 
999         /* calculate drive capacity, and select LBA if possible */
1000         init_idedisk_capacity (drive);
1001 
1002         /* limit drive capacity to 137GB if LBA48 cannot be used */
1003         if (drive->addressing == 0 && drive->capacity64 > 1ULL << 28) {
1004                 printk(KERN_WARNING "%s: cannot use LBA48 - full capacity "
1005                        "%llu sectors (%llu MB)\n",
1006                        drive->name, (unsigned long long)drive->capacity64,
1007                        sectors_to_MB(drive->capacity64));
1008                 drive->capacity64 = 1ULL << 28;
1009         }
1010 
1011         if (drive->hwif->no_lba48_dma && drive->addressing) {
1012                 if (drive->capacity64 > 1ULL << 28) {
1013                         printk(KERN_INFO "%s: cannot use LBA48 DMA - PIO mode will"
1014                                          " be used for accessing sectors > %u\n",
1015                                          drive->name, 1 << 28);
1016                 } else
1017                         drive->addressing = 0;
1018         }
1019 
1020         /*
1021          * if possible, give fdisk access to more of the drive,
1022          * by correcting bios_cyls:
1023          */
1024         capacity = idedisk_capacity (drive);
1025         if (!drive->forced_geom) {
1026 
1027                 if (idedisk_supports_lba48(drive->id)) {
1028                         /* compatibility */
1029                         drive->bios_sect = 63;
1030                         drive->bios_head = 255;
1031                 }
1032 
1033                 if (drive->bios_sect && drive->bios_head) {
1034                         unsigned int cap0 = capacity; /* truncate to 32 bits */
1035                         unsigned int cylsz, cyl;
1036 
1037                         if (cap0 != capacity)
1038                                 drive->bios_cyl = 65535;
1039                         else {
1040                                 cylsz = drive->bios_sect * drive->bios_head;
1041                                 cyl = cap0 / cylsz;
1042                                 if (cyl > 65535)
1043                                         cyl = 65535;
1044                                 if (cyl > drive->bios_cyl)
1045                                         drive->bios_cyl = cyl;
1046                         }
1047                 }
1048         }
1049         printk(KERN_INFO "%s: %llu sectors (%llu MB)",
1050                          drive->name, capacity, sectors_to_MB(capacity));
1051 
1052         /* Only print cache size when it was specified */
1053         if (id->buf_size)
1054                 printk (" w/%dKiB Cache", id->buf_size/2);
1055 
1056         printk(", CHS=%d/%d/%d", 
1057                drive->bios_cyl, drive->bios_head, drive->bios_sect);
1058         if (drive->using_dma)
1059                 ide_dma_verbose(drive);
1060         printk("\n");
1061 
1062         drive->mult_count = 0;
1063         if (id->max_multsect) {
1064 #ifdef CONFIG_IDEDISK_MULTI_MODE
1065                 id->multsect = ((id->max_multsect/2) > 1) ? id->max_multsect : 0;
1066                 id->multsect_valid = id->multsect ? 1 : 0;
1067                 drive->mult_req = id->multsect_valid ? id->max_multsect : INITIAL_MULT_COUNT;
1068                 drive->special.b.set_multmode = drive->mult_req ? 1 : 0;
1069 #else   /* original, pre IDE-NFG, per request of AC */
1070                 drive->mult_req = INITIAL_MULT_COUNT;
1071                 if (drive->mult_req > id->max_multsect)
1072                         drive->mult_req = id->max_multsect;
1073                 if (drive->mult_req || ((id->multsect_valid & 1) && id->multsect))
1074                         drive->special.b.set_multmode = 1;
1075 #endif  /* CONFIG_IDEDISK_MULTI_MODE */
1076         }
1077         drive->no_io_32bit = id->dword_io ? 1 : 0;
1078 
1079         /* write cache enabled? */
1080         if ((id->csfo & 1) || (id->cfs_enable_1 & (1 << 5)))
1081                 drive->wcache = 1;
1082 
1083         write_cache(drive, 1);
1084 
1085         /*
1086          * We must avoid issuing commands a drive does not understand
1087          * or we may crash it. We check flush cache is supported. We also
1088          * check we have the LBA48 flush cache if the drive capacity is
1089          * too large. By this time we have trimmed the drive capacity if
1090          * LBA48 is not available so we don't need to recheck that.
1091          */
1092         barrier = 0;
1093         if (ide_id_has_flush_cache(id))
1094                 barrier = 1;
1095         if (drive->addressing == 1) {
1096                 /* Can't issue the correct flush ? */
1097                 if (capacity > (1ULL << 28) && !ide_id_has_flush_cache_ext(id))
1098                         barrier = 0;
1099         }
1100 
1101         printk(KERN_DEBUG "%s: cache flushes %ssupported\n",
1102                 drive->name, barrier ? "" : "not ");
1103         if (barrier) {
1104                 blk_queue_ordered(drive->queue, 1);
1105                 blk_queue_issue_flush_fn(drive->queue, idedisk_issue_flush);
1106         }
1107 }
1108 
1109 static void ide_cacheflush_p(ide_drive_t *drive)
1110 {
1111         if (!drive->wcache || !ide_id_has_flush_cache(drive->id))
1112                 return;
1113 
1114         if (do_idedisk_flushcache(drive))
1115                 printk(KERN_INFO "%s: wcache flush failed!\n", drive->name);
1116 }
1117 
1118 static int idedisk_cleanup (ide_drive_t *drive)
1119 {
1120         struct gendisk *g = drive->disk;
1121         ide_cacheflush_p(drive);
1122         if (ide_unregister_subdriver(drive))
1123                 return 1;
1124         del_gendisk(g);
1125         drive->devfs_name[0] = '\0';
1126         g->fops = ide_fops;
1127         return 0;
1128 }
1129 
1130 static int idedisk_attach(ide_drive_t *drive);
1131 
1132 static void ide_device_shutdown(struct device *dev)
1133 {
1134         ide_drive_t *drive = container_of(dev, ide_drive_t, gendev);
1135 
1136 #ifdef  CONFIG_ALPHA
1137         /* On Alpha, halt(8) doesn't actually turn the machine off,
1138            it puts you into the sort of firmware monitor. Typically,
1139            it's used to boot another kernel image, so it's not much
1140            different from reboot(8). Therefore, we don't need to
1141            spin down the disk in this case, especially since Alpha
1142            firmware doesn't handle disks in standby mode properly.
1143            On the other hand, it's reasonably safe to turn the power
1144            off when the shutdown process reaches the firmware prompt,
1145            as the firmware initialization takes rather long time -
1146            at least 10 seconds, which should be sufficient for
1147            the disk to expire its write cache. */
1148         if (system_state != SYSTEM_POWER_OFF) {
1149 #else
1150         if (system_state == SYSTEM_RESTART) {
1151 #endif
1152                 ide_cacheflush_p(drive);
1153                 return;
1154         }
1155 
1156         printk("Shutdown: %s\n", drive->name);
1157         dev->bus->suspend(dev, PM_SUSPEND_STANDBY);
1158 }
1159 
1160 /*
1161  *      IDE subdriver functions, registered with ide.c
1162  */
1163 static ide_driver_t idedisk_driver = {
1164         .owner                  = THIS_MODULE,
1165         .gen_driver = {
1166                 .shutdown       = ide_device_shutdown,
1167         },
1168         .name                   = "ide-disk",
1169         .version                = IDEDISK_VERSION,
1170         .media                  = ide_disk,
1171         .busy                   = 0,
1172         .supports_dsc_overlap   = 0,
1173         .cleanup                = idedisk_cleanup,
1174         .do_request             = ide_do_rw_disk,
1175         .pre_reset              = idedisk_pre_reset,
1176         .capacity               = idedisk_capacity,
1177         .special                = idedisk_special,
1178         .proc                   = idedisk_proc,
1179         .attach                 = idedisk_attach,
1180         .drives                 = LIST_HEAD_INIT(idedisk_driver.drives),
1181         .start_power_step       = idedisk_start_power_step,
1182         .complete_power_step    = idedisk_complete_power_step,
1183 };
1184 
1185 static int idedisk_open(struct inode *inode, struct file *filp)
1186 {
1187         ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1188         drive->usage++;
1189         if (drive->removable && drive->usage == 1) {
1190                 ide_task_t args;
1191                 memset(&args, 0, sizeof(ide_task_t));
1192                 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORLOCK;
1193                 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1194                 args.handler      = &task_no_data_intr;
1195                 check_disk_change(inode->i_bdev);
1196                 /*
1197                  * Ignore the return code from door_lock,
1198                  * since the open() has already succeeded,
1199                  * and the door_lock is irrelevant at this point.
1200                  */
1201                 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1202                         drive->doorlocking = 0;
1203         }
1204         return 0;
1205 }
1206 
1207 static int idedisk_release(struct inode *inode, struct file *filp)
1208 {
1209         ide_drive_t *drive = inode->i_bdev->bd_disk->private_data;
1210         if (drive->usage == 1)
1211                 ide_cacheflush_p(drive);
1212         if (drive->removable && drive->usage == 1) {
1213                 ide_task_t args;
1214                 memset(&args, 0, sizeof(ide_task_t));
1215                 args.tfRegister[IDE_COMMAND_OFFSET] = WIN_DOORUNLOCK;
1216                 args.command_type = IDE_DRIVE_TASK_NO_DATA;
1217                 args.handler      = &task_no_data_intr;
1218                 if (drive->doorlocking && ide_raw_taskfile(drive, &args, NULL))
1219                         drive->doorlocking = 0;
1220         }
1221         drive->usage--;
1222         return 0;
1223 }
1224 
1225 static int idedisk_ioctl(struct inode *inode, struct file *file,
1226                         unsigned int cmd, unsigned long arg)
1227 {
1228         struct block_device *bdev = inode->i_bdev;
1229         return generic_ide_ioctl(file, bdev, cmd, arg);
1230 }
1231 
1232 static int idedisk_media_changed(struct gendisk *disk)
1233 {
1234         ide_drive_t *drive = disk->private_data;
1235 
1236         /* do not scan partitions twice if this is a removable device */
1237         if (drive->attach) {
1238                 drive->attach = 0;
1239                 return 0;
1240         }
1241         /* if removable, always assume it was changed */
1242         return drive->removable;
1243 }
1244 
1245 static int idedisk_revalidate_disk(struct gendisk *disk)
1246 {
1247         ide_drive_t *drive = disk->private_data;
1248         set_capacity(disk, idedisk_capacity(drive));
1249         return 0;
1250 }
1251 
1252 static struct block_device_operations idedisk_ops = {
1253         .owner          = THIS_MODULE,
1254         .open           = idedisk_open,
1255         .release        = idedisk_release,
1256         .ioctl          = idedisk_ioctl,
1257         .media_changed  = idedisk_media_changed,
1258         .revalidate_disk= idedisk_revalidate_disk
1259 };
1260 
1261 MODULE_DESCRIPTION("ATA DISK Driver");
1262 
1263 static int idedisk_attach(ide_drive_t *drive)
1264 {
1265         struct gendisk *g = drive->disk;
1266 
1267         /* strstr("foo", "") is non-NULL */
1268         if (!strstr("ide-disk", drive->driver_req))
1269                 goto failed;
1270         if (!drive->present)
1271                 goto failed;
1272         if (drive->media != ide_disk)
1273                 goto failed;
1274 
1275         if (ide_register_subdriver(drive, &idedisk_driver)) {
1276                 printk (KERN_ERR "ide-disk: %s: Failed to register the driver with ide.c\n", drive->name);
1277                 goto failed;
1278         }
1279         DRIVER(drive)->busy++;
1280         idedisk_setup(drive);
1281         if ((!drive->head || drive->head > 16) && !drive->select.b.lba) {
1282                 printk(KERN_ERR "%s: INVALID GEOMETRY: %d PHYSICAL HEADS?\n",
1283                         drive->name, drive->head);
1284                 drive->attach = 0;
1285         } else
1286                 drive->attach = 1;
1287         DRIVER(drive)->busy--;
1288         g->minors = 1 << PARTN_BITS;
1289         strcpy(g->devfs_name, drive->devfs_name);
1290         g->driverfs_dev = &drive->gendev;
1291         g->flags = drive->removable ? GENHD_FL_REMOVABLE : 0;
1292         set_capacity(g, idedisk_capacity(drive));
1293         g->fops = &idedisk_ops;
1294         add_disk(g);
1295         return 0;
1296 failed:
1297         return 1;
1298 }
1299 
1300 static void __exit idedisk_exit (void)
1301 {
1302         ide_unregister_driver(&idedisk_driver);
1303 }
1304 
1305 static int idedisk_init (void)
1306 {
1307         return ide_register_driver(&idedisk_driver);
1308 }
1309 
1310 module_init(idedisk_init);
1311 module_exit(idedisk_exit);
1312 MODULE_LICENSE("GPL");
1313 
  This page was automatically generated by the LXR engine.